User guide
This guide follows the work of a modeler in order. It starts from a ready-made system, changes it, builds a system of your own, and then simulates, analyses, evaluates, calibrates, and shares it. Each page builds on the ones before it. The first run takes three lines.
from skcomplex.systems.biology.epidemiology import SIRSystem
SIRSystem().prepare().simulate().get_values('I.x').max().round()
304.0
The epidemic of SIRSystem peaks at 304 infected people out of 1000.
If you know scikit-learn
This section maps the names of scikit-learn to those of the library. What you define is an estimator, and what you do to it is a function.
| scikit-learn | scikit-complexity |
|---|---|
an estimator, such as Ridge, with get_params, set_params, and clone |
a component of a system, an element, an interaction, a space, or a system, such as SIRSystem |
Pipeline |
System, the composite of elements, interactions, and nested systems, with nested parameters such as gravity__g |
fit |
prepare, which checks and compiles a system once |
predict |
simulate, which returns a Trajectory |
| estimator tags | capability tags, from get_tags() |
all_estimators() |
all_systems() |
solver= of LogisticRegression |
solver= of a system, such as GillespieSolver() or ScipyODESolver() |
n_jobs |
backend= |
sklearn.metrics |
the analysis functions, such as lyapunov and entropy |
validation_curve and repeated cross-validation |
the experiment functions, such as sweep and ensemble |
GridSearchCV |
calibrate, which returns the fitted system in its result |
A system is a class whose constructor takes the parameters of its model and stores them.
set_params changes them, and clone copies them. prepare compiles the system once, and simulate, the analyses,
and the experiments read it and return frozen results. They never change its parameters.
The running examples
Four small systems come back on many pages, each built on one kind of law. The README builds all four, and the pages below take them further.
- A falling ball.
Element('ball', values={'x': 10.0, 'm': 1.0})and a gravity whoseRateLawsets the second derivativex.diff(t, 2)to-g. It shows states, parameters, equations, and one prepared system run many times. - A reaction network. The reaction
A + B -> Cas aJumpLawthat removes oneAand oneBand adds oneC. It runs as random jumps withGillespieSolverand as rate equations withScipyODESolver, by changing onlysolver=. - Heat in a rod. One value
uper cell of aLatticeSpace, withRateLaw(u.diff(t), D * laplacian(u)). It shows elements with one member per site and the operators of a space. - The Ising model. One spin per site of a square lattice, with its energy over the temperature as an
ObjectiveLaw, sampled byMetropolisSolver. It shows objectives, sampling solvers, and sweeps over a parameter.
The four look like this in one block. The README explains each line.
import numpy as np
from skcomplex.core.components.elements import Element
from skcomplex.core.components.interactions import Interaction
from skcomplex.core.components.laws import JumpLaw, ObjectiveLaw, RateLaw
from skcomplex.core.components.laws.utils import laplacian, neighbors
from skcomplex.core.components.spaces import LatticeNeighborhood, LatticeSpace
from skcomplex.core.engine.solvers import MetropolisSolver, ScipyODESolver
from skcomplex.core.systems import System
gravity = Interaction('gravity', roles={'target': 'ball'}, laws=lambda s: [RateLaw(s.target.x.diff(s.t, 2), -9.81)])
ball = System(elements=[Element('ball', values={'x': 10.0, 'm': 1.0})], interactions=[gravity])
bind = Interaction(
'bind',
roles={'a': 'A', 'b': 'B', 'c': 'C'},
laws=lambda s: [JumpLaw(0.01 * s.a.n * s.b.n, {s.a.n: -1, s.b.n: -1, s.c.n: +1})],
)
molecules = [Element(name, values={'n': n}) for name, n in (('A', 100), ('B', 80), ('C', 0))]
jumps = System(elements=molecules, interactions=[bind])
mean = System(elements=molecules, interactions=[bind], solver=ScipyODESolver())
start = np.zeros(21)
start[10] = 1.0
rod = Element('rod', values={'u': start}, space=LatticeSpace(21, boundary={'u': ('fixed', 0.0)}))
conduction = Interaction(
'conduction',
roles={'target': 'rod'},
laws=lambda s: [RateLaw(s.target.u.diff(s.t), laplacian(s.target.u))],
)
heat = System(elements=[rod], interactions=[conduction])
nearest = LatticeNeighborhood(((-1, 0), (0, -1), (0, 1), (1, 0)))
def ising(s):
spin = s.target.spin
return [ObjectiveLaw(-spin * sum(neighbors(spin, nearest, reduce='each')) / 1.5, spin)]
spins = Element('spins', values={'spin': np.ones(256)}, space=LatticeSpace((16, 16)))
magnet = System(
elements=[spins],
interactions=[Interaction('ising', roles={'target': 'spins'}, laws=ising)],
solver=MetropolisSolver(moves={'spins.spin': 'flip'}, neighborhood=nearest),
)
[system.prepare().form_ for system in (ball, jumps, mean, heat, magnet)]
['ordinary differential equations', 'jump process', 'ordinary differential equations',
'ordinary differential equations', 'Monte Carlo sampling']
The five systems are ordinary differential equations, a jump process, ordinary differential equations again, ordinary
differential equations with one equation per cell, and Monte Carlo sampling. Nobody named the problem: prepare read
it from the kinds of law and the solver.
Two ways in
You can start from the catalog or from the core.
- Select a ready-made system. Import a system class from its subfield, such as
SIRSystemfromskcomplex.systems.biology.epidemiology, build it with the parameters you choose, and run it withprepare().simulate().all_systemslists the classes of the catalog by field, topic, or tag. - Build a custom system. Write elements and interactions with
skcomplex.core.components, put them in aSystemofskcomplex.core.systems, run it withprepare().simulate(), and study the results withskcomplex.core.analysis.
Every ready-made system is written with the same classes as a custom one, so the page of a system under Systems also serves as a pattern.
The order of the guide
Read the pages in order. Each line says what you can do after reading the page.
- Select a ready-made system. Find a system of the catalog, read what it models, and run it with its tuned settings.
- Change it. Give a ready-made system your own parameters and run settings, and nest it in a larger system.
- System classes. Change what a ready-made system builds by subclassing it, and write a system class of your own.
- Learn the core components. Know how elements, interactions, laws, spaces, and systems fit together. The reference pages on elements, interactions, laws, spaces, and systems explain each class, the page on general realizations explains the recipes that several fields share, and Choosing components leads from the features of a problem to its components.
- Build a custom system. Write a model of hares and lynx from core components, as a subclass of a shared recipe, and as a system class.
- Check a system. Read the tags and requirements of components, and fix the validation errors a builder meets most.
- Simulate. Choose the run settings, the solver, and the backend, and read the trajectory a run returns.
- Analyse. Find the analyses that apply to a system and read their results.
- Evaluate. Compare a model with data and judge the fit and the identifiability of its parameters.
- Calibrate. Fit the parameters of a system to data with each method of
calibrate. - Run experiments. Sweep parameters, run ensembles, measure sensitivity, and compare scenarios over many runs.
- Visualise. Stream a run to the Rerun viewer and plot trajectories and results.
- Share. Turn a model into a system class of the catalog with its inventory entry and gallery example.
- Extend the library. Give the general classes the words of your field, and find where a new component belongs.
- Case studies. Follow one system of each of three fields through the whole workflow, against data or an exact result.
The glossary defines the terms of the guide, and Troubleshooting shows each error of the library with its cause and its fix.