mxlpy 0.16.0__py3-none-any.whl → 0.18.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mxlpy/__init__.py +4 -1
- mxlpy/fit.py +173 -7
- mxlpy/fns.py +513 -21
- mxlpy/identify.py +7 -1
- mxlpy/meta/codegen_latex.py +279 -14
- mxlpy/meta/source_tools.py +122 -4
- mxlpy/model.py +50 -24
- mxlpy/nn/_torch.py +61 -1
- mxlpy/npe/__init__.py +38 -0
- mxlpy/npe/_torch.py +365 -0
- mxlpy/plot.py +194 -50
- mxlpy/report.py +33 -6
- mxlpy/sbml/_import.py +5 -2
- mxlpy/surrogates/__init__.py +7 -6
- mxlpy/surrogates/_poly.py +12 -9
- mxlpy/surrogates/_torch.py +118 -114
- mxlpy/symbolic/strikepy.py +1 -3
- mxlpy/types.py +17 -7
- {mxlpy-0.16.0.dist-info → mxlpy-0.18.0.dist-info}/METADATA +7 -8
- {mxlpy-0.16.0.dist-info → mxlpy-0.18.0.dist-info}/RECORD +22 -21
- mxlpy-0.18.0.dist-info/licenses/LICENSE +21 -0
- mxlpy/npe.py +0 -277
- mxlpy-0.16.0.dist-info/licenses/LICENSE +0 -674
- {mxlpy-0.16.0.dist-info → mxlpy-0.18.0.dist-info}/WHEEL +0 -0
mxlpy/__init__.py
CHANGED
@@ -48,6 +48,7 @@ from . import (
|
|
48
48
|
fns,
|
49
49
|
mc,
|
50
50
|
mca,
|
51
|
+
npe,
|
51
52
|
plot,
|
52
53
|
report,
|
53
54
|
sbml,
|
@@ -66,7 +67,7 @@ from .scan import (
|
|
66
67
|
)
|
67
68
|
from .simulator import Simulator
|
68
69
|
from .symbolic import SymbolicModel, to_symbolic_model
|
69
|
-
from .types import Derived, IntegratorProtocol
|
70
|
+
from .types import Derived, IntegratorProtocol, unwrap
|
70
71
|
|
71
72
|
with contextlib.suppress(ImportError):
|
72
73
|
from .integrators import Assimulo
|
@@ -95,6 +96,7 @@ __all__ = [
|
|
95
96
|
"make_protocol",
|
96
97
|
"mc",
|
97
98
|
"mca",
|
99
|
+
"npe",
|
98
100
|
"plot",
|
99
101
|
"report",
|
100
102
|
"sbml",
|
@@ -104,6 +106,7 @@ __all__ = [
|
|
104
106
|
"time_course",
|
105
107
|
"time_course_over_protocol",
|
106
108
|
"to_symbolic_model",
|
109
|
+
"unwrap",
|
107
110
|
]
|
108
111
|
|
109
112
|
|
mxlpy/fit.py
CHANGED
@@ -28,12 +28,16 @@ from mxlpy.types import (
|
|
28
28
|
|
29
29
|
__all__ = [
|
30
30
|
"InitialGuess",
|
31
|
+
"LossFn",
|
31
32
|
"MinimizeFn",
|
33
|
+
"ProtocolResidualFn",
|
32
34
|
"ResidualFn",
|
33
35
|
"SteadyStateResidualFn",
|
34
36
|
"TimeSeriesResidualFn",
|
37
|
+
"rmse",
|
35
38
|
"steady_state",
|
36
39
|
"time_course",
|
40
|
+
"time_course_over_protocol",
|
37
41
|
]
|
38
42
|
|
39
43
|
if TYPE_CHECKING:
|
@@ -44,6 +48,21 @@ if TYPE_CHECKING:
|
|
44
48
|
type InitialGuess = dict[str, float]
|
45
49
|
type ResidualFn = Callable[[Array], float]
|
46
50
|
type MinimizeFn = Callable[[ResidualFn, InitialGuess], dict[str, float]]
|
51
|
+
type LossFn = Callable[
|
52
|
+
[
|
53
|
+
pd.DataFrame | pd.Series,
|
54
|
+
pd.DataFrame | pd.Series,
|
55
|
+
],
|
56
|
+
float,
|
57
|
+
]
|
58
|
+
|
59
|
+
|
60
|
+
def rmse(
|
61
|
+
y_pred: pd.DataFrame | pd.Series,
|
62
|
+
y_true: pd.DataFrame | pd.Series,
|
63
|
+
) -> float:
|
64
|
+
"""Calculate root mean square error between model and data."""
|
65
|
+
return cast(float, np.sqrt(np.mean(np.square(y_pred - y_true))))
|
47
66
|
|
48
67
|
|
49
68
|
class SteadyStateResidualFn(Protocol):
|
@@ -58,6 +77,7 @@ class SteadyStateResidualFn(Protocol):
|
|
58
77
|
model: Model,
|
59
78
|
y0: dict[str, float],
|
60
79
|
integrator: IntegratorType,
|
80
|
+
loss_fn: LossFn,
|
61
81
|
) -> float:
|
62
82
|
"""Calculate residual error between model steady state and experimental data."""
|
63
83
|
...
|
@@ -75,6 +95,27 @@ class TimeSeriesResidualFn(Protocol):
|
|
75
95
|
model: Model,
|
76
96
|
y0: dict[str, float],
|
77
97
|
integrator: IntegratorType,
|
98
|
+
loss_fn: LossFn,
|
99
|
+
) -> float:
|
100
|
+
"""Calculate residual error between model time course and experimental data."""
|
101
|
+
...
|
102
|
+
|
103
|
+
|
104
|
+
class ProtocolResidualFn(Protocol):
|
105
|
+
"""Protocol for time series residual functions."""
|
106
|
+
|
107
|
+
def __call__(
|
108
|
+
self,
|
109
|
+
par_values: Array,
|
110
|
+
# This will be filled out by partial
|
111
|
+
par_names: list[str],
|
112
|
+
data: pd.DataFrame,
|
113
|
+
model: Model,
|
114
|
+
y0: dict[str, float],
|
115
|
+
integrator: IntegratorType,
|
116
|
+
loss_fn: LossFn,
|
117
|
+
protocol: pd.DataFrame,
|
118
|
+
time_points_per_step: int = 10,
|
78
119
|
) -> float:
|
79
120
|
"""Calculate residual error between model time course and experimental data."""
|
80
121
|
...
|
@@ -109,6 +150,7 @@ def _steady_state_residual(
|
|
109
150
|
model: Model,
|
110
151
|
y0: dict[str, float] | None,
|
111
152
|
integrator: IntegratorType,
|
153
|
+
loss_fn: LossFn,
|
112
154
|
) -> float:
|
113
155
|
"""Calculate residual error between model steady state and experimental data.
|
114
156
|
|
@@ -119,6 +161,7 @@ def _steady_state_residual(
|
|
119
161
|
y0: Initial conditions
|
120
162
|
par_names: Names of parameters being fit
|
121
163
|
integrator: ODE integrator class to use
|
164
|
+
loss_fn: Loss function to use for residual calculation
|
122
165
|
|
123
166
|
Returns:
|
124
167
|
float: Root mean square error between model and data
|
@@ -143,9 +186,11 @@ def _steady_state_residual(
|
|
143
186
|
)
|
144
187
|
if res is None:
|
145
188
|
return cast(float, np.inf)
|
146
|
-
|
147
|
-
|
148
|
-
|
189
|
+
|
190
|
+
return loss_fn(
|
191
|
+
res.get_combined().loc[:, cast(list, data.index)],
|
192
|
+
data,
|
193
|
+
)
|
149
194
|
|
150
195
|
|
151
196
|
def _time_course_residual(
|
@@ -156,6 +201,53 @@ def _time_course_residual(
|
|
156
201
|
model: Model,
|
157
202
|
y0: dict[str, float] | None,
|
158
203
|
integrator: IntegratorType,
|
204
|
+
loss_fn: LossFn,
|
205
|
+
) -> float:
|
206
|
+
"""Calculate residual error between model time course and experimental data.
|
207
|
+
|
208
|
+
Args:
|
209
|
+
par_values: Parameter values to test
|
210
|
+
data: Experimental time course data
|
211
|
+
model: Model instance to simulate
|
212
|
+
y0: Initial conditions
|
213
|
+
par_names: Names of parameters being fit
|
214
|
+
integrator: ODE integrator class to use
|
215
|
+
loss_fn: Loss function to use for residual calculation
|
216
|
+
|
217
|
+
Returns:
|
218
|
+
float: Root mean square error between model and data
|
219
|
+
|
220
|
+
"""
|
221
|
+
res = (
|
222
|
+
Simulator(
|
223
|
+
model.update_parameters(dict(zip(par_names, par_values, strict=True))),
|
224
|
+
y0=y0,
|
225
|
+
integrator=integrator,
|
226
|
+
)
|
227
|
+
.simulate_time_course(cast(list, data.index))
|
228
|
+
.get_result()
|
229
|
+
)
|
230
|
+
if res is None:
|
231
|
+
return cast(float, np.inf)
|
232
|
+
results_ss = res.get_combined()
|
233
|
+
|
234
|
+
return loss_fn(
|
235
|
+
results_ss.loc[:, cast(list, data.columns)],
|
236
|
+
data,
|
237
|
+
)
|
238
|
+
|
239
|
+
|
240
|
+
def _protocol_residual(
|
241
|
+
par_values: ArrayLike,
|
242
|
+
# This will be filled out by partial
|
243
|
+
par_names: list[str],
|
244
|
+
data: pd.DataFrame,
|
245
|
+
model: Model,
|
246
|
+
y0: dict[str, float] | None,
|
247
|
+
integrator: IntegratorType,
|
248
|
+
loss_fn: LossFn,
|
249
|
+
protocol: pd.DataFrame,
|
250
|
+
time_points_per_step: int = 10,
|
159
251
|
) -> float:
|
160
252
|
"""Calculate residual error between model time course and experimental data.
|
161
253
|
|
@@ -166,6 +258,9 @@ def _time_course_residual(
|
|
166
258
|
y0: Initial conditions
|
167
259
|
par_names: Names of parameters being fit
|
168
260
|
integrator: ODE integrator class to use
|
261
|
+
loss_fn: Loss function to use for residual calculation
|
262
|
+
protocol: Experimental protocol
|
263
|
+
time_points_per_step: Number of time points per step in the protocol
|
169
264
|
|
170
265
|
Returns:
|
171
266
|
float: Root mean square error between model and data
|
@@ -177,14 +272,20 @@ def _time_course_residual(
|
|
177
272
|
y0=y0,
|
178
273
|
integrator=integrator,
|
179
274
|
)
|
180
|
-
.
|
275
|
+
.simulate_over_protocol(
|
276
|
+
protocol=protocol,
|
277
|
+
time_points_per_step=time_points_per_step,
|
278
|
+
)
|
181
279
|
.get_result()
|
182
280
|
)
|
183
281
|
if res is None:
|
184
282
|
return cast(float, np.inf)
|
185
283
|
results_ss = res.get_combined()
|
186
|
-
|
187
|
-
return
|
284
|
+
|
285
|
+
return loss_fn(
|
286
|
+
results_ss.loc[:, cast(list, data.columns)],
|
287
|
+
data,
|
288
|
+
)
|
188
289
|
|
189
290
|
|
190
291
|
def steady_state(
|
@@ -195,6 +296,7 @@ def steady_state(
|
|
195
296
|
minimize_fn: MinimizeFn = _default_minimize_fn,
|
196
297
|
residual_fn: SteadyStateResidualFn = _steady_state_residual,
|
197
298
|
integrator: IntegratorType = DefaultIntegrator,
|
299
|
+
loss_fn: LossFn = rmse,
|
198
300
|
) -> dict[str, float]:
|
199
301
|
"""Fit model parameters to steady-state experimental data.
|
200
302
|
|
@@ -210,6 +312,7 @@ def steady_state(
|
|
210
312
|
minimize_fn: Function to minimize fitting error
|
211
313
|
residual_fn: Function to calculate fitting error
|
212
314
|
integrator: ODE integrator class
|
315
|
+
loss_fn: Loss function to use for residual calculation
|
213
316
|
|
214
317
|
Returns:
|
215
318
|
dict[str, float]: Fitted parameters as {parameter_name: fitted_value}
|
@@ -232,6 +335,7 @@ def steady_state(
|
|
232
335
|
y0=y0,
|
233
336
|
par_names=par_names,
|
234
337
|
integrator=integrator,
|
338
|
+
loss_fn=loss_fn,
|
235
339
|
),
|
236
340
|
)
|
237
341
|
res = minimize_fn(fn, p0)
|
@@ -249,6 +353,62 @@ def time_course(
|
|
249
353
|
minimize_fn: MinimizeFn = _default_minimize_fn,
|
250
354
|
residual_fn: TimeSeriesResidualFn = _time_course_residual,
|
251
355
|
integrator: IntegratorType = DefaultIntegrator,
|
356
|
+
loss_fn: LossFn = rmse,
|
357
|
+
) -> dict[str, float]:
|
358
|
+
"""Fit model parameters to time course of experimental data.
|
359
|
+
|
360
|
+
Examples:
|
361
|
+
>>> time_course(model, p0, data)
|
362
|
+
{'k1': 0.1, 'k2': 0.2}
|
363
|
+
|
364
|
+
Args:
|
365
|
+
model: Model instance to fit
|
366
|
+
data: Experimental time course data
|
367
|
+
p0: Initial parameter guesses as {parameter_name: value}
|
368
|
+
y0: Initial conditions as {species_name: value}
|
369
|
+
minimize_fn: Function to minimize fitting error
|
370
|
+
residual_fn: Function to calculate fitting error
|
371
|
+
integrator: ODE integrator class
|
372
|
+
loss_fn: Loss function to use for residual calculation
|
373
|
+
|
374
|
+
Returns:
|
375
|
+
dict[str, float]: Fitted parameters as {parameter_name: fitted_value}
|
376
|
+
|
377
|
+
Note:
|
378
|
+
Uses L-BFGS-B optimization with bounds [1e-12, 1e6] for all parameters
|
379
|
+
|
380
|
+
"""
|
381
|
+
par_names = list(p0.keys())
|
382
|
+
p_orig = model.parameters
|
383
|
+
|
384
|
+
fn = cast(
|
385
|
+
ResidualFn,
|
386
|
+
partial(
|
387
|
+
residual_fn,
|
388
|
+
data=data,
|
389
|
+
model=model,
|
390
|
+
y0=y0,
|
391
|
+
par_names=par_names,
|
392
|
+
integrator=integrator,
|
393
|
+
loss_fn=loss_fn,
|
394
|
+
),
|
395
|
+
)
|
396
|
+
res = minimize_fn(fn, p0)
|
397
|
+
model.update_parameters(p_orig)
|
398
|
+
return res
|
399
|
+
|
400
|
+
|
401
|
+
def time_course_over_protocol(
|
402
|
+
model: Model,
|
403
|
+
p0: dict[str, float],
|
404
|
+
data: pd.DataFrame,
|
405
|
+
protocol: pd.DataFrame,
|
406
|
+
y0: dict[str, float] | None = None,
|
407
|
+
minimize_fn: MinimizeFn = _default_minimize_fn,
|
408
|
+
residual_fn: ProtocolResidualFn = _protocol_residual,
|
409
|
+
integrator: IntegratorType = DefaultIntegrator,
|
410
|
+
loss_fn: LossFn = rmse,
|
411
|
+
time_points_per_step: int = 10,
|
252
412
|
) -> dict[str, float]:
|
253
413
|
"""Fit model parameters to time course of experimental data.
|
254
414
|
|
@@ -258,12 +418,15 @@ def time_course(
|
|
258
418
|
|
259
419
|
Args:
|
260
420
|
model: Model instance to fit
|
261
|
-
data: Experimental time course data as pandas DataFrame
|
262
421
|
p0: Initial parameter guesses as {parameter_name: value}
|
422
|
+
data: Experimental time course data
|
423
|
+
protocol: Experimental protocol
|
263
424
|
y0: Initial conditions as {species_name: value}
|
264
425
|
minimize_fn: Function to minimize fitting error
|
265
426
|
residual_fn: Function to calculate fitting error
|
266
427
|
integrator: ODE integrator class
|
428
|
+
loss_fn: Loss function to use for residual calculation
|
429
|
+
time_points_per_step: Number of time points per step in the protocol
|
267
430
|
|
268
431
|
Returns:
|
269
432
|
dict[str, float]: Fitted parameters as {parameter_name: fitted_value}
|
@@ -284,6 +447,9 @@ def time_course(
|
|
284
447
|
y0=y0,
|
285
448
|
par_names=par_names,
|
286
449
|
integrator=integrator,
|
450
|
+
loss_fn=loss_fn,
|
451
|
+
protocol=protocol,
|
452
|
+
time_points_per_step=time_points_per_step,
|
287
453
|
),
|
288
454
|
)
|
289
455
|
res = minimize_fn(fn, p0)
|