Skip to content

Commit

Permalink
Merge branch 'main' into default-pde-ex
Browse files Browse the repository at this point in the history
  • Loading branch information
yb6599 authored May 14, 2024
2 parents 4a6768a + cbaa059 commit c29e096
Show file tree
Hide file tree
Showing 16 changed files with 1,920 additions and 1,648 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
pip install -e .[dev]
- name: run mypy
run: |
mypy -v
mypy
Tests:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ gen_experiments/trials/
scratch/
*.png
debugme*.py
trials/*

# IDE files
.vscode
Expand Down
36 changes: 31 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ classifiers = [
# Since the point of the package is reproducibility, incl. all dev
# dependencies
dependencies = [
"mitosis >=0.3.0rc1, <0.4.0",
"derivative @ git+https://github.com/Jacob-Stevens-Haas/derivative@hyperparams",
"pysindy[cvxpy,miosr] @ git+https://github.com/dynamicslab/pysindy@master",
"mitosis >=0.4.0rc2",
"derivative @ git+https://github.com/andgoldschmidt/derivative@f0d566d",
"pysindy[cvxpy,miosr] @ git+https://github.com/dynamicslab/pysindy@a43e217",
"kalman @ git+https://github.com/Jacob-Stevens-Haas/[email protected]",
"auto_ks @ git+https://github.com/cvxgrp/auto_ks.git@e60bcc6",
"pytest >= 6.0.0",
Expand Down Expand Up @@ -74,7 +74,7 @@ extend-exclude = '''
| env
)/
'''
preview = 1
preview = true

[tool.codespell]
skip = '*.html,./env,./scratch/*,todo'
Expand All @@ -92,4 +92,30 @@ addopts = '-m "not slow"'
markers = ["slow"]

[tool.mypy]
files = ["src/gen_experiments/__init__.py"]
files = [
"src/gen_experiments/__init__.py",
"src/gen_experiments/utils.py",
"src/gen_experiments/gridsearch/typing.py",
"tests/test_all.py",
"tests/test_gridsearch.py",
]

[[tool.mypy.overrides]]
module="auto_ks.*"
ignore_missing_imports=true

[[tool.mypy.overrides]]
module="sklearn.*"
ignore_missing_imports=true

[[tool.mypy.overrides]]
module="pysindy.*"
ignore_missing_imports=true

[[tool.mypy.overrides]]
module="kalman.*"
ignore_missing_imports=true

[[tool.mypy.overrides]]
module="scipy.*"
ignore_missing_imports=true
14 changes: 7 additions & 7 deletions src/gen_experiments/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

import numpy as np
from numpy.typing import NDArray
from pysindy import BaseDifferentiation, FiniteDifference, SINDy # type: ignore
from pysindy import BaseDifferentiation, FiniteDifference, SINDy

from . import gridsearch, odes, pdes
from .utils import TrialData
from .utils import SINDyTrialData, make_model # noqa: F401

this_module = importlib.import_module(__name__)
BORING_ARRAY = np.ones((2, 2))
BORING_ARRAY = np.ones((2, 2), dtype=float)

Scores = Mapping[str, float]

Expand All @@ -38,20 +38,20 @@ class NoExperiment:
@staticmethod
def run(
*args: Any, return_all: bool = True, **kwargs: Any
) -> Scores | tuple[Scores, TrialData]:
) -> Scores | tuple[Scores, SINDyTrialData]:
metrics = defaultdict(
lambda: 1,
main=1,
)
if return_all:
trial_data: TrialData = {
trial_data: SINDyTrialData = {
"dt": 1,
"coeff_true": BORING_ARRAY[:1],
"coeff_fit": BORING_ARRAY[:1],
# "coefficients": boring_array,
"feature_names": ["1"],
"input_features": ["x", "y"],
"t_train": np.arange(0, 1, 1),
"t_train": np.arange(0, 1, 1, dtype=float),
"x_train": BORING_ARRAY,
"x_true": BORING_ARRAY,
"smooth_train": BORING_ARRAY,
Expand All @@ -67,7 +67,7 @@ def run(
return metrics


experiments = {
experiments: dict[str, tuple[Any, str | None]] = {
"sho": (odes, "sho"),
"lorenz": (odes, "lorenz"),
"lorenz_2d": (odes, "lorenz_2d"),
Expand Down
158 changes: 70 additions & 88 deletions src/gen_experiments/config.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
from typing import TypeVar
from collections.abc import Iterable, Sequence
from typing import TypeVar, cast

import numpy as np
import pysindy as ps
from numpy.typing import NDArray

from gen_experiments.utils import (
FullTrialData,
NestedDict,
from gen_experiments.data import _signal_avg_power
from gen_experiments.gridsearch.typing import (
GridLocator,
SeriesDef,
SeriesList,
_PlotPrefs,
_signal_avg_power,
SkinnySpecs,
)
from gen_experiments.plotting import _PlotPrefs
from gen_experiments.typing import NestedDict
from gen_experiments.utils import FullSINDyTrialData

T = TypeVar("T")
T = TypeVar("T", bound=str)
U = TypeVar("U")


Expand All @@ -21,15 +25,17 @@ def ND(d: dict[T, U]) -> NestedDict[T, U]:


def _convert_abs_rel_noise(
grid_vals: list, grid_params: list, recent_results: FullTrialData
):
scan_grid: dict[str, NDArray[np.floating]],
recent_results: FullSINDyTrialData,
) -> dict[str, Sequence[np.floating]]:
"""Convert abs_noise grid_vals to rel_noise"""
signal = np.stack(recent_results["x_true"], axis=-1)
signal_power = _signal_avg_power(signal)
ind = grid_params.index("sim_params.noise_abs")
grid_vals[ind] = grid_vals[ind] / signal_power
grid_params[ind] = "sim_params.noise_rel"
return grid_vals, grid_params
plot_grid = scan_grid.copy()
new_vals = plot_grid["sim_params.noise_abs"] / signal_power
plot_grid["sim_params.noise_rel"] = new_vals
plot_grid.pop("sim_params.noise_abs")
return cast(dict[str, Sequence[np.floating]], plot_grid)


# To allow pickling
Expand All @@ -46,77 +52,43 @@ def addn(x):


plot_prefs = {
"test": _PlotPrefs(True, False, ({"sim_params.t_end": 10},)),
"test": _PlotPrefs(),
"test-absrel": _PlotPrefs(
True, _convert_abs_rel_noise, ({"sim_params.noise_abs": 1},)
True, False, GridLocator(..., {("sim_params.noise_abs", (1,))})
),
"test-absrel2": _PlotPrefs(
True,
_convert_abs_rel_noise,
(
{"sim_params.noise_abs": 0.1},
{"sim_params.noise_abs": 0.5},
{"sim_params.noise_abs": 1},
{"sim_params.noise_abs": 2},
{"sim_params.noise_abs": 4},
{"sim_params.noise_abs": 8},
False,
GridLocator(
...,
(..., ...),
(
{"sim_params.noise_abs": 0.1},
{"sim_params.noise_abs": 0.5},
{"sim_params.noise_abs": 1},
{"sim_params.noise_abs": 2},
{"sim_params.noise_abs": 4},
{"sim_params.noise_abs": 8},
),
),
),
"test-absrel3": _PlotPrefs(
"absrel-newloc": _PlotPrefs(
True,
_convert_abs_rel_noise,
(
{
"sim_params.noise_abs": 1,
"diff_params.smoother_kws.window_length": 15,
},
{"sim_params.noise_abs": 1, "diff_params.meas_var": 1},
{"sim_params.noise_abs": 1, "diff_params.alpha": 1e-2},
False,
GridLocator(
["coeff_mse", "coeff_f1"],
(..., (2, 3, 4)),
(
{"diff_params.kind": "kalman", "diff_params.alpha": None},
{
"diff_params.kind": "kalman",
"diff_params.alpha": lambda a: isinstance(a, float | int),
},
{"diff_params.kind": "trend_filtered"},
{"diff_params.diffcls": "SmoothedFiniteDifference"},
),
),
),
"test-absrel4": _PlotPrefs(
True,
_convert_abs_rel_noise,
(
{
"sim_params.noise_abs": 1,
"diff_params.smoother_kws.window_length": 15,
},
{"sim_params.noise_abs": 1, "diff_params.meas_var": 1},
{"sim_params.noise_abs": 1, "diff_params.alpha": 1e0},
{
"sim_params.noise_abs": 2,
"diff_params.smoother_kws.window_length": 15,
},
{"sim_params.noise_abs": 2, "diff_params.meas_var": 4},
{"sim_params.noise_abs": 2, "diff_params.alpha": 1e-1},
),
),
"test-absrel5": _PlotPrefs(
True,
_convert_abs_rel_noise,
(
{
"sim_params.noise_abs": 1,
"diff_params.diffcls": "SmoothedFiniteDifference",
},
{"sim_params.noise_abs": 1, "diff_params.kind": "kalman"},
{"sim_params.noise_abs": 1, "diff_params.kind": "trend_filtered"},
{
"sim_params.noise_abs": 2,
"diff_params.diffcls": "SmoothedFiniteDifference",
},
{"sim_params.noise_abs": 2, "diff_params.kind": "kalman"},
{"sim_params.noise_abs": 2, "diff_params.kind": "trend_filtered"},
{
"sim_params.noise_abs": 4,
"diff_params.diffcls": "SmoothedFiniteDifference",
},
{"sim_params.noise_abs": 4, "diff_params.kind": "kalman"},
{"sim_params.noise_abs": 4, "diff_params.kind": "trend_filtered"},
),
{(0, 2), (3, 2), (0, 3), (3, 3), (0, 4), (3, 4)},
),
}
sim_params = {
"test": ND({"n_trajectories": 2}),
Expand Down Expand Up @@ -181,23 +153,23 @@ def addn(x):
),
"ensmio-ho-vdp-lv-duff": ND({
"optcls": "ensemble",
"opt": ps.MIOSR(target_sparsity=4),
"opt": ps.MIOSR(target_sparsity=4, unbias=True),
"bagging": True,
"n_models": 20,
}),
"ensmio-hopf": ND({
"optcls": "ensemble",
"opt": ps.MIOSR(target_sparsity=8),
"opt": ps.MIOSR(target_sparsity=8, unbias=True),
"bagging": True,
"n_models": 20,
}),
"ensmio-lorenz-ross": ND({
"optcls": "ensemble",
"opt": ps.MIOSR(target_sparsity=7),
"opt": ps.MIOSR(target_sparsity=7, unbias=True),
"bagging": True,
"n_models": 20,
}),
"mio-lorenz-ross": ND({"optcls": "MIOSR", "target_sparsity": 7}),
"mio-lorenz-ross": ND({"optcls": "MIOSR", "target_sparsity": 7, "unbias": True}),
}

# Grid search parameters
Expand Down Expand Up @@ -305,7 +277,7 @@ def addn(x):
"duration-absnoise": ["sim_params.t_end", "sim_params.noise_abs"],
"rel_noise": ["sim_params.t_end", "sim_params.noise_rel"],
}
grid_vals = {
grid_vals: dict[str, list[Iterable]] = {
"test": [[5, 10, 15, 20]],
"abs_noise": [[0.1, 0.5, 1, 2, 4, 8]],
"abs_noise-kalman": [[0.1, 0.5, 1, 2, 4, 8], [0.1, 0.5, 1, 2, 4, 8]],
Expand All @@ -315,15 +287,15 @@ def addn(x):
"lorenzk": [[1, 9, 27], [0.1, 0.8], np.logspace(-6, -1, 4)],
"lorenz1": [[1, 3, 9, 27], [0.01, 0.1, 1]],
"duration-absnoise": [[0.5, 1, 2, 4, 8, 16], [0.1, 0.5, 1, 2, 4, 8]],
"rel_noise": [[0.25, 1, 4, 16], [0.05, 0.1, 0.15, 0.2, 0.25, 0.3]],
"rel_noise": [[0.5, 1, 2, 4, 8, 16], [0.05, 0.1, 0.15, 0.2, 0.25, 0.3]],
}
grid_decisions = {
"test": ["plot"],
"plot1": ["plot", "max"],
"lorenzk": ["plot", "plot", "max"],
"plot2": ["plot", "plot"],
}
diff_series = {
diff_series: dict[str, SeriesDef] = {
"kalman1": SeriesDef(
"Kalman",
diff_params["kalman"],
Expand All @@ -337,19 +309,19 @@ def addn(x):
[np.logspace(-4, 0, 5)],
),
"auto-kalman": SeriesDef(
"Kalman",
"Auto Kalman",
diff_params["kalman"],
["diff_params.alpha", "diff_params.meas_var"],
[(None,), (0.1, 0.5, 1, 2, 4, 8)],
),
"auto-kalman2": SeriesDef(
"Kalman",
"Auto Kalman",
diff_params["kalman"],
["diff_params.alpha", "diff_params.meas_var"],
[(None,), (0.01, 0.25, 1, 4, 16, 64)],
),
"auto-kalman3": SeriesDef(
"Kalman",
"Auto Kalman",
diff_params["kalman"],
["diff_params.alpha"],
[(None,)],
Expand Down Expand Up @@ -379,7 +351,7 @@ def addn(x):
[[5, 8, 12, 15]],
),
}
series_params = {
series_params: dict[str, SeriesList] = {
"test": SeriesList(
"diff_params",
"Differentiation Method",
Expand Down Expand Up @@ -425,10 +397,20 @@ def addn(x):
diff_series["sg2"],
],
),
"multikalman": SeriesList(
"diff_params",
"Differentiation Method",
[
diff_series["auto-kalman3"],
diff_series["kalman2"],
diff_series["tv2"],
diff_series["sg2"],
],
),
}


skinny_specs = {
skinny_specs: dict[str, SkinnySpecs] = {
"exp3": (
("sim_params.noise_abs", "diff_params.meas_var"),
((identity,), (identity,)),
Expand All @@ -441,5 +423,5 @@ def addn(x):
("sim_params.t_end", "sim_params.noise_abs", "diff_params.meas_var"),
((1, 1), (-1, identity), (-1, identity)),
),
"duration-noise": (("sim_params.t_end", "sim_params.noise_abs"), ((1,), (-1,))),
"duration-noise": (("sim_params.t_end", "sim_params.noise_rel"), ((1,), (-1,))),
}
Loading

0 comments on commit c29e096

Please sign in to comment.