Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Move optimization to new module glotaran.optimization #1047

Merged
merged 4 commits into from
Apr 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from pathlib import Path

from glotaran.analysis.optimize import optimize
try:
from glotaran.analysis.optimize import optimize
except ImportError:
from glotaran.optimization.optimize import optimize

from glotaran.io import load_dataset
from glotaran.io import load_model
from glotaran.io import load_parameters
Expand Down
19 changes: 10 additions & 9 deletions benchmark/benchmarks/unit/analysis/bench_optimize.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import numpy as np
import xarray as xr

from glotaran.analysis.optimize import optimize
from glotaran.analysis.simulation import simulate
from glotaran.analysis.test.models import MultichannelMulticomponentDecay
try:
from glotaran.analysis.optimize import optimize
from glotaran.analysis.simulation import simulate
from glotaran.analysis.test.models import MultichannelMulticomponentDecay
except ImportError:
from glotaran.optimization.optimize import optimize
from glotaran.simulation import simulate
from glotaran.optimization.test.models import MultichannelMulticomponentDecay

from glotaran.project import Scheme


class BenchmarkOptimize:
"""
Integration test for a two dataset analysis.

Ref:
https://github.com/glotaran/pyglotaran-examples/tree/main/pyglotaran_examples/ex_two_datasets
"""
"""Optimization benchmark based on unitests."""

timeout = 300
params = (
Expand Down
2 changes: 1 addition & 1 deletion benchmark/pytest/analysis/test_optimization_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import pytest
import xarray as xr

from glotaran.analysis.optimization_group import OptimizationGroup
from glotaran.model import Megacomplex
from glotaran.model import Model
from glotaran.model import megacomplex
from glotaran.optimization.optimization_group import OptimizationGroup
from glotaran.parameter import ParameterGroup
from glotaran.project import Scheme
from glotaran.testing.plugin_system import monkeypatch_plugin_registry
Expand Down
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- 👌 Improve Result, Parameter and ParameterGroup markdown (#1012)
- 👌🧹 Add suffix to rate and lifetime and guard for missing datasets (#1022)
- ♻️ Move simulation to own module (#1041)
- ♻️ Move optimization to new module glotaran.optimization (#1047)

### 🩹 Bug fixes

Expand All @@ -32,6 +33,7 @@

- `glotaran.io.save_result(result, result_path, format_name='legacy')` -> `glotaran.io.save_result(result, Path(result_path) / 'result.yml')`
- `glotaran.analysis.simulation` -> `glotaran.simulation.simulation`
- `glotaran.analysis.optimize` -> `glotaran.optimization.optimize`

### 🚧 Maintenance

Expand Down
2 changes: 1 addition & 1 deletion docs/source/notebooks/quickstart/quickstart.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
"metadata": {},
"outputs": [],
"source": [
"from glotaran.analysis.optimize import optimize\n",
"from glotaran.io import load_model\n",
"from glotaran.io import load_parameters\n",
"from glotaran.io import save_dataset\n",
"from glotaran.io.prepare_dataset import prepare_time_trace_dataset\n",
"from glotaran.optimization.optimize import optimize\n",
"from glotaran.project.scheme import Scheme"
]
},
Expand Down
6 changes: 6 additions & 0 deletions glotaran/analysis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@
new_module_name="glotaran.simulation.simulation",
to_be_removed_in_version="0.8.0",
)

optimize = deprecate_submodule(
deprecated_module_name="glotaran.analysis.optimize",
new_module_name="glotaran.optimization.optimize",
to_be_removed_in_version="0.8.0",
)
2 changes: 1 addition & 1 deletion glotaran/builtin/io/folder/test/test_folder_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

import pytest

from glotaran.analysis.optimize import optimize
from glotaran.deprecation import GlotaranApiDeprecationWarning
from glotaran.io import save_result
from glotaran.optimization.optimize import optimize
from glotaran.project.result import Result
from glotaran.testing.simulated_data.sequential_spectral_decay import SCHEME

Expand Down
2 changes: 1 addition & 1 deletion glotaran/builtin/io/yml/test/test_save_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import pytest

from glotaran import __version__
from glotaran.analysis.optimize import optimize
from glotaran.io import save_result
from glotaran.optimization.optimize import optimize
from glotaran.project.result import Result
from glotaran.testing.simulated_data.sequential_spectral_decay import SCHEME

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import numpy as np

from glotaran.analysis.util import calculate_matrix
from glotaran.builtin.megacomplexes.baseline import BaselineMegacomplex
from glotaran.builtin.megacomplexes.decay import DecayMegacomplex
from glotaran.model import Model
from glotaran.optimization.util import calculate_matrix
from glotaran.parameter import ParameterGroup


Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import numpy as np

from glotaran.analysis.optimize import optimize
from glotaran.analysis.simulation import simulate
from glotaran.builtin.megacomplexes.clp_guide import ClpGuideMegacomplex
from glotaran.builtin.megacomplexes.decay import DecaySequentialMegacomplex
from glotaran.builtin.megacomplexes.decay.test.test_decay_megacomplex import create_gaussian_clp
from glotaran.model import Model
from glotaran.optimization.optimize import optimize
from glotaran.parameter import ParameterGroup
from glotaran.project import Scheme
from glotaran.simulation.simulation import simulate


def test_clp_guide():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import pytest
import xarray as xr

from glotaran.analysis.optimize import optimize
from glotaran.analysis.util import calculate_matrix
from glotaran.builtin.megacomplexes.coherent_artifact import CoherentArtifactMegacomplex
from glotaran.builtin.megacomplexes.decay import DecayMegacomplex
from glotaran.model import Model
from glotaran.optimization.optimize import optimize
from glotaran.optimization.util import calculate_matrix
from glotaran.parameter import ParameterGroup
from glotaran.project import Scheme
from glotaran.simulation import simulate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import numpy as np
import pytest

from glotaran.analysis.optimize import optimize
from glotaran.builtin.megacomplexes.damped_oscillation import DampedOscillationMegacomplex
from glotaran.builtin.megacomplexes.decay import DecayMegacomplex
from glotaran.builtin.megacomplexes.spectral import SpectralMegacomplex
from glotaran.model import Megacomplex
from glotaran.model import Model
from glotaran.optimization.optimize import optimize
from glotaran.parameter import ParameterGroup
from glotaran.project import Scheme
from glotaran.simulation import simulate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import pytest
import xarray as xr

from glotaran.analysis.optimize import optimize
from glotaran.model import Model
from glotaran.optimization.optimize import optimize
from glotaran.parameter import ParameterGroup
from glotaran.project import Scheme
from glotaran.simulation import simulate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import numpy as np
import pytest

from glotaran.analysis.optimize import optimize
from glotaran.io import load_model
from glotaran.io import load_parameters
from glotaran.optimization.optimize import optimize
from glotaran.project import Scheme
from glotaran.simulation import simulate

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import pytest
import xarray as xr

from glotaran.analysis.optimize import optimize
from glotaran.analysis.util import calculate_matrix
from glotaran.builtin.megacomplexes.decay.test.test_decay_megacomplex import DecayModel
from glotaran.builtin.megacomplexes.spectral import SpectralMegacomplex
from glotaran.model import Megacomplex
from glotaran.model import Model
from glotaran.optimization.optimize import optimize
from glotaran.optimization.util import calculate_matrix
from glotaran.parameter import ParameterGroup
from glotaran.project import Scheme
from glotaran.simulation import simulate
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import numpy as np
import pytest

from glotaran.analysis.optimize import optimize
from glotaran.io import load_model
from glotaran.io import load_parameters
from glotaran.optimization.optimize import optimize
from glotaran.project import Scheme
from glotaran.simulation import simulate

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import numpy as np
import pytest

from glotaran.analysis.optimize import optimize
from glotaran.io import load_model
from glotaran.io import load_parameters
from glotaran.optimization.optimize import optimize
from glotaran.project import Scheme
from glotaran.simulation import simulate

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

import numpy as np

from glotaran.analysis.optimize import optimize
from glotaran.builtin.megacomplexes.decay import DecayMegacomplex
from glotaran.builtin.megacomplexes.spectral import SpectralMegacomplex
from glotaran.io import prepare_time_trace_dataset
from glotaran.model import Megacomplex
from glotaran.model import Model
from glotaran.optimization.optimize import optimize
from glotaran.parameter import ParameterGroup
from glotaran.project import Scheme
from glotaran.simulation import simulate
Expand Down
2 changes: 1 addition & 1 deletion glotaran/cli/commands/optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import click

from glotaran.analysis.optimize import optimize
from glotaran.cli.commands import util
from glotaran.optimization.optimize import optimize
from glotaran.plugin_system.data_io_registration import known_data_formats
from glotaran.plugin_system.project_io_registration import save_result
from glotaran.project.scheme import Scheme
Expand Down
18 changes: 18 additions & 0 deletions glotaran/deprecation/modules/test/test_changed_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from glotaran.deprecation.deprecation_utils import GlotaranApiDeprecationWarning
from glotaran.deprecation.deprecation_utils import module_attribute
from glotaran.io import load_dataset
from glotaran.optimization import optimize as optimize_module
from glotaran.parameter import ParameterGroup
from glotaran.project import Result
from glotaran.project import Scheme
Expand Down Expand Up @@ -105,6 +106,23 @@ def test_analysis_result_from_import(recwarn: WarningsRecorder):
assert analysis_result == Result


def test_analysis_optimization(recwarn: WarningsRecorder):
"""Usage of glotaran.analysis.optimization"""
warnings.simplefilter("always")

from glotaran.analysis import optimize as analysis_optimize

assert len(recwarn) == 0
assert analysis_optimize.optimize == optimize_module.optimize # type: ignore[attr-defined]

check_recwarn(recwarn)


def test_analysis_optimization_from_import(recwarn: WarningsRecorder):
"""Same as 'from glotaran.analysis.optimize import optimizations analysis_scheme'"""
changed_import_test_warn(recwarn, "glotaran.analysis.optimize", attribute_name="optimize")


def test_analysis_simulation(recwarn: WarningsRecorder):
"""Usage of glotaran.analysis.simulation"""
warnings.simplefilter("always")
Expand Down
2 changes: 1 addition & 1 deletion glotaran/deprecation/modules/test/test_project_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import pytest

from glotaran.analysis.optimize import optimize
from glotaran.deprecation.modules.test import deprecation_warning_on_call_test_helper
from glotaran.optimization.optimize import optimize
from glotaran.project.result import Result
from glotaran.testing.simulated_data.sequential_spectral_decay import SCHEME

Expand Down
1 change: 1 addition & 0 deletions glotaran/optimization/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""This package contains functions for optimization."""
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@
import numpy as np
import xarray as xr

from glotaran.analysis.nnls import residual_nnls
from glotaran.analysis.optimization_group_calculator import OptimizationGroupCalculator
from glotaran.analysis.optimization_group_calculator_linked import (
OptimizationGroupCalculatorLinked,
)
from glotaran.analysis.optimization_group_calculator_unlinked import (
OptimizationGroupCalculatorUnlinked,
)
from glotaran.analysis.util import get_min_max_from_interval
from glotaran.analysis.variable_projection import residual_variable_projection
from glotaran.io.prepare_dataset import add_svd_to_dataset
from glotaran.model import DatasetGroup
from glotaran.model import DatasetModel
from glotaran.model import Model
from glotaran.optimization.nnls import residual_nnls
from glotaran.optimization.optimization_group_calculator import OptimizationGroupCalculator
from glotaran.optimization.optimization_group_calculator_linked import (
OptimizationGroupCalculatorLinked,
)
from glotaran.optimization.optimization_group_calculator_unlinked import (
OptimizationGroupCalculatorUnlinked,
)
from glotaran.optimization.util import get_min_max_from_interval
from glotaran.optimization.variable_projection import residual_variable_projection
from glotaran.parameter import ParameterGroup
from glotaran.parameter import ParameterHistory
from glotaran.project import Scheme
Expand Down Expand Up @@ -53,10 +53,10 @@ def __init__(
scheme: Scheme,
dataset_group: DatasetGroup,
):
"""Create OptimizationGroup instance from a scheme (:class:`glotaran.analysis.scheme.Scheme`)
"""Create OptimizationGroup instance from a scheme (:class:`.Scheme`)

Args:
scheme (Scheme): An instance of :class:`glotaran.analysis.scheme.Scheme`
scheme (Scheme): An instance of :class:`.Scheme`
which defines your model, parameters, and data
"""

Expand All @@ -69,11 +69,11 @@ def __init__(

try:
self._residual_function = residual_functions[dataset_group.model.residual_function]
except KeyError:
except KeyError as e:
raise ValueError(
f"Unknown residual function '{dataset_group.model.residual_function}', "
f"allowed functions are: {list(residual_functions.keys())}."
)
) from e
self._dataset_models = dataset_group.dataset_models

self._overwrite_index_dependent = self.model.need_index_dependent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import xarray as xr

if TYPE_CHECKING:
from glotaran.analysis.optimization_group import OptimizationGroup
from glotaran.optimization.optimization_group import OptimizationGroup


class OptimizationGroupCalculator:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
import numpy as np
import xarray as xr

from glotaran.analysis.optimization_group_calculator import OptimizationGroupCalculator
from glotaran.analysis.util import CalculatedMatrix
from glotaran.analysis.util import apply_weight
from glotaran.analysis.util import calculate_clp_penalties
from glotaran.analysis.util import calculate_matrix
from glotaran.analysis.util import find_closest_index
from glotaran.analysis.util import find_overlap
from glotaran.analysis.util import reduce_matrix
from glotaran.analysis.util import retrieve_clps
from glotaran.model import DatasetModel
from glotaran.optimization.optimization_group_calculator import OptimizationGroupCalculator
from glotaran.optimization.util import CalculatedMatrix
from glotaran.optimization.util import apply_weight
from glotaran.optimization.util import calculate_clp_penalties
from glotaran.optimization.util import calculate_matrix
from glotaran.optimization.util import find_closest_index
from glotaran.optimization.util import find_overlap
from glotaran.optimization.util import reduce_matrix
from glotaran.optimization.util import retrieve_clps

if TYPE_CHECKING:
from glotaran.analysis.optimization_group import OptimizationGroup
from glotaran.optimization.optimization_group import OptimizationGroup


class DatasetIndexModel(NamedTuple):
Expand Down
Loading