Comparisons¶
mxlpy provides simple routines to numerically compare two models.
For this, we use the compare module.
import numpy as np
from example_models import get_sir, get_sird
from mxlpy import compare, make_protocol
Steady-states¶
You can compare the steady-states of two models using the compare_steady_states function.
ssc = compare.steady_states(get_sir(), get_sird())
This returns a SteadyStateComparison object, for which you can either access the direct comparisons
ssc.variables
| m1 | m2 | diff | rel_diff | |
|---|---|---|---|---|
| d | NaN | 7.121887e-02 | NaN | NaN |
| i | 2.815325e-11 | -8.667566e-12 | -3.682081e-11 | -1.307871 |
| r | 8.282880e-01 | 7.121887e-01 | -1.160993e-01 | -0.140168 |
| s | 1.717120e-01 | 2.165924e-01 | 4.488038e-02 | 0.261370 |
ssc.fluxes
| m1 | m2 | diff | rel_diff | |
|---|---|---|---|---|
| death | NaN | -8.667566e-14 | NaN | NaN |
| infection | 9.668503e-13 | -3.754658e-13 | -1.342316e-12 | -1.388339 |
| recovery | 2.815325e-12 | -8.667566e-13 | -3.682081e-12 | -1.307871 |
or plot the results
_ = ssc.plot_variables()
_ = ssc.plot_fluxes()
_ = ssc.plot_all()
Time courses¶
For comparing time courses, you can use the compare_time_courses function.
This returns a TimeCourseComparison object, which you can directly use to plot for example the relative differences of variables or fluxes.
pc = compare.time_courses(
get_sir(),
get_sird(),
time_points=np.linspace(0, 100, 101),
)
_ = pc.plot_variables_relative_difference()
_ = pc.plot_fluxes_relative_difference()
Protocol time courses¶
For comparing protocol time courses, you can use the protocol_time_courses function.
This returns a ProtocolComparison object, which you can directly use to plot for example the relative differences of variables or fluxes.
pc = compare.protocol_time_courses(
get_sir(),
get_sird(),
protocol=make_protocol(
[
(10, {"beta": 0.2}),
(10, {"beta": 1.0}),
(80, {"beta": 0.2}),
]
),
)
_ = pc.plot_variables_relative_difference()
Optionally, if you want to indicate one of the protocol parameters on the plots, you can use the shade_protocol_variable argument
_ = pc.plot_variables_relative_difference(shade_protocol_variable="beta")
_ = pc.plot_fluxes_relative_difference(shade_protocol_variable="beta")
Trajectory similarity¶
To find candidates for simplifying a model, trajectory_similarity regresses every trajectory linearly onto every other and reports the \(R^2\).
Pairs with \(R^2 \approx 1\) behave as scaled copies of each other (fast equilibria, redundant intermediates, conserved pools) and might be lumped into one state.
With several results (conditions, mutants) the minimum is taken, since a reduction has to hold in every one of them.
from mxlpy import Simulator, plot
results = [
Simulator(get_sird())
.update_parameter("beta", beta)
.simulate(100)
.get_result()
.unwrap_or_err()
for beta in (0.2, 0.4)
]
similarity = compare.trajectory_similarity(results)
similarity.r2.round(3)
| s | i | r | d | |
|---|---|---|---|---|
| s | 1.000 | 0.055 | 0.880 | 0.880 |
| i | 0.055 | 1.000 | 0.309 | 0.309 |
| r | 0.880 | 0.309 | 1.000 | 1.000 |
| d | 0.880 | 0.309 | 1.000 | 1.000 |
groups lists the connected groups above a threshold, and plot.trajectory_similarity shows the matrix ordered by hierarchical clustering, outlining pairs above the threshold.
print(similarity.groups(threshold=0.95))
_ = plot.trajectory_similarity(similarity, threshold=0.95)
[['r', 'd']]