Skip to content

Commit

Permalink
Add Dataset Groups (#851)
Browse files Browse the repository at this point in the history
* Added DatasetGroup and DatasetGroupModel.

* Replaced Problem with Optimization Groups and Calculators.

* Removed non-negative-least-squares option from scheme.

* πŸ‘ŒπŸ—‘οΈ Added back non_negative_least_squares and deprecated group_tolerance in Scheme initialization

* 🩹 Fixed optimization groups using datasets outside of their groups

This bug led to result creation crashing, because of missing labels.

* 🩹 Fix Benchmarks - remove result creation benchmarks.

* Added test for multiple groups

* Renamed OptimizationGroupCalculators

* πŸ—‘οΈ Deprecated Scheme args group and non_negative_least_squares

* ♻️ Renamed 'DatasetGroupIndexModel' -> 'DatasetIndexModelGroup'

* ♻️ Changed ChainMap in favor of more readable dict updating

* ♻️ Renamed 'test_problem' -> 'test_optimization_group'

Co-authored-by: s-weigand <[email protected]>
Co-authored-by: Sourcery AI <>
  • Loading branch information
joernweissenborn and s-weigand authored Oct 18, 2021
1 parent d1e36a9 commit 385006c
Show file tree
Hide file tree
Showing 28 changed files with 967 additions and 604 deletions.
33 changes: 0 additions & 33 deletions benchmark/benchmarks/integration/ex_two_datasets/benchmark.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import pickle
from pathlib import Path

from scipy.optimize import OptimizeResult

from glotaran.analysis.optimize import _create_result
from glotaran.analysis.optimize import optimize
from glotaran.analysis.problem_grouped import GroupedProblem
from glotaran.io import load_dataset
from glotaran.io import load_model
from glotaran.io import load_parameters
Expand Down Expand Up @@ -37,41 +32,13 @@ def setup(self):
non_negative_least_squares=True,
optimization_method="TrustRegionReflection",
)
# Values extracted from a previous run of IntegrationTwoDatasets.time_optimize()
self.problem = GroupedProblem(self.scheme)
# pickled OptimizeResult
with open(SCRIPT_DIR / "data/ls_result.pcl", "rb") as ls_result_file:
self.ls_result: OptimizeResult = pickle.load(ls_result_file)
self.free_parameter_labels = [
"inputs.2",
"inputs.3",
"inputs.7",
"inputs.8",
"scale.2",
"rates.k1",
"rates.k2",
"rates.k3",
"irf.center",
"irf.width",
]
self.termination_reason = "The maximum number of function evaluations is exceeded."

def time_optimize(self):
optimize(self.scheme)

def peakmem_optimize(self):
optimize(self.scheme)

def time_create_result(self):
_create_result(
self.problem, self.ls_result, self.free_parameter_labels, self.termination_reason
)

def peakmem_create_result(self):
_create_result(
self.problem, self.ls_result, self.free_parameter_labels, self.termination_reason
)


if __name__ == "__main__":
test = IntegrationTwoDatasets()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import pytest
import xarray as xr

from glotaran.analysis.problem_grouped import GroupedProblem
from glotaran.analysis.problem_ungrouped import UngroupedProblem
from glotaran.analysis.optimization_group import OptimizationGroup
from glotaran.model import Megacomplex
from glotaran.model import Model
from glotaran.model import megacomplex
Expand Down Expand Up @@ -55,9 +54,10 @@ def finalize_data(


@monkeypatch_plugin_registry(test_megacomplex={"benchmark": BenchmarkMegacomplex})
def setup_model(index_dependent):
def setup_model(index_dependent, link_clp):
model_dict = {
"megacomplex": {"m1": {"is_index_dependent": index_dependent}},
"dataset_groups": {"default": {"link_clp": link_clp}},
"dataset": {
"dataset1": {"megacomplex": ["m1"]},
"dataset2": {"megacomplex": ["m1"]},
Expand All @@ -83,90 +83,93 @@ def setup_scheme(model):
)


def setup_problem(scheme, grouped):
return GroupedProblem(scheme) if grouped else UngroupedProblem(scheme)
def setup_optimization_group(scheme):
return OptimizationGroup(scheme, scheme.model.get_dataset_groups()["default"])


def test_benchmark_bag_creation(benchmark):

model = setup_model(False)
model = setup_model(False, True)
assert model.valid()

scheme = setup_scheme(model)
problem = setup_problem(scheme, True)
optimization_group = setup_optimization_group(scheme)

benchmark(problem.init_bag)
benchmark(optimization_group._calculator.init_bag)


@pytest.mark.parametrize("grouped", [True, False])
@pytest.mark.parametrize("link_clp", [True, False])
@pytest.mark.parametrize("index_dependent", [True, False])
def test_benchmark_calculate_matrix(benchmark, grouped, index_dependent):
def test_benchmark_calculate_matrix(benchmark, link_clp, index_dependent):

model = setup_model(index_dependent)
model = setup_model(index_dependent, link_clp)
assert model.valid()

scheme = setup_scheme(model)
problem = setup_problem(scheme, grouped)
optimization_group = setup_optimization_group(scheme)

if grouped:
problem.init_bag()
if link_clp:
optimization_group._calculator.init_bag()

benchmark(problem.calculate_matrices)
benchmark(optimization_group._calculator.calculate_matrices)


@pytest.mark.parametrize("grouped", [True, False])
@pytest.mark.parametrize("link_clp", [True, False])
@pytest.mark.parametrize("index_dependent", [True, False])
def test_benchmark_calculate_residual(benchmark, grouped, index_dependent):
def test_benchmark_calculate_residual(benchmark, link_clp, index_dependent):

model = setup_model(index_dependent)
model = setup_model(index_dependent, link_clp)
assert model.valid()

scheme = setup_scheme(model)
problem = setup_problem(scheme, grouped)
optimization_group = setup_optimization_group(scheme)

if grouped:
problem.init_bag()
problem.calculate_matrices()
if link_clp:
optimization_group._calculator.init_bag()

benchmark(problem.calculate_residual)
optimization_group._calculator.calculate_matrices()

benchmark(optimization_group._calculator.calculate_residual)

@pytest.mark.parametrize("grouped", [True, False])

@pytest.mark.parametrize("link_clp", [True, False])
@pytest.mark.parametrize("index_dependent", [True, False])
def test_benchmark_calculate_result_data(benchmark, grouped, index_dependent):
def test_benchmark_calculate_result_data(benchmark, link_clp, index_dependent):

model = setup_model(index_dependent)
model = setup_model(index_dependent, link_clp)
assert model.valid()

scheme = setup_scheme(model)
problem = setup_problem(scheme, grouped)
optimization_group = setup_optimization_group(scheme)

if link_clp:
optimization_group._calculator.init_bag()

optimization_group._calculator.calculate_matrices()

if grouped:
problem.init_bag()
problem.calculate_matrices()
problem.calculate_residual()
optimization_group._calculator.calculate_residual()

benchmark(problem.create_result_data)
benchmark(optimization_group.create_result_data)


# @pytest.mark.skip(reason="To time consuming atm.")
@pytest.mark.parametrize("grouped", [True, False])
@pytest.mark.parametrize("link_clp", [True, False])
@pytest.mark.parametrize("index_dependent", [True, False])
def test_benchmark_optimize_20_runs(benchmark, grouped, index_dependent):
def test_benchmark_optimize_20_runs(benchmark, link_clp, index_dependent):

model = setup_model(index_dependent)
model = setup_model(index_dependent, link_clp)
assert model.valid()

scheme = setup_scheme(model)
problem = setup_problem(scheme, grouped)
optimization_group = setup_optimization_group(scheme)

@benchmark
def run():
if grouped:
problem.init_bag()
if link_clp:
optimization_group._calculator.init_bag()

for _ in range(20):
problem.reset()
problem.full_penalty
optimization_group.reset()
optimization_group._calculator.calculate_full_penalty()

problem.create_result_data()
optimization_group.create_result_data()
Loading

0 comments on commit 385006c

Please sign in to comment.