Skip to content

Commit

Permalink
distrib tests and version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiaskoenig committed Mar 26, 2020
1 parent e5f3659 commit 27ae130
Show file tree
Hide file tree
Showing 3 changed files with 276 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pip install -e .

## Release notes
### 0.3.9
* full support for distrib (distributions and uncertainty)

### 0.3.8
* python 3.7 support (dropping py3.5)
Expand Down
2 changes: 1 addition & 1 deletion sbmlutils/_version.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Definition of version string.
"""
__version__ = "0.3.9a2"
__version__ = "0.3.9"

PROGRAM_VERSION = __version__
PROGRAM_NAME = 'sbmlutils'
274 changes: 274 additions & 0 deletions sbmlutils/tests/test_distrib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
import pytest

from sbmlutils.units import *
from sbmlutils.factory import *
from sbmlutils.modelcreator.creator import CoreModel
from sbmlutils.validation import check_doc


def check_model_dict(d):
"""Check that no errors."""
# create model and print SBML
core_model = CoreModel.from_dict(model_dict=d)
core_model.create_sbml()
assert core_model.doc is not None
[Nall, Nerr, Nwar] = check_doc(core_model.doc, units_consistency=False)
assert Nerr == 0


def test_assign_distribution():
model_dict = {
'mid': 'distrib_assignment',
'packages': ['distrib'],
'model_units': ModelUnits(time=UNIT_hr, extent=UNIT_KIND_MOLE,
substance=UNIT_KIND_MOLE,
length=UNIT_m, area=UNIT_m2,
volume=UNIT_KIND_LITRE),
'units': [UNIT_hr, UNIT_m, UNIT_m2, UNIT_mM],
'parameters': [
Parameter(sid="p1", value=0.0, unit=UNIT_mM)
],
'assignments': [
InitialAssignment('p1', 'normal(0 mM, 1 mM)'),
]
}
check_model_dict(model_dict)


def test_normal_distribution():
model_dict = {
'mid': 'normal',
'packages': ['distrib'],
'parameters': [
Parameter('y', value=1.0),
Parameter('z', value=1.0),
],
'assignments': [
InitialAssignment('y', 'normal(z, 10)'),
]
}
check_model_dict(model_dict)


def test_trunctated_normal_distribution():
model_dict = {
'mid': 'truncated_normal',
'packages': ['distrib'],
'parameters': [
Parameter('y', value=1.0),
Parameter('z', value=1.0),
],
'assignments': [
InitialAssignment('y', 'normal(z, 10, z-2, z+2)'),
]
}
check_model_dict(model_dict)


def test_conditional_event():
model_dict = {
'mid': 'conditional_events',
'packages': ['distrib'],
'parameters': [
Parameter('x', value=1.0, constant=False)
],
'events': [
Event(
"E0",
trigger="time>2 && x<1",
priority="uniform(0, 1)",
trigger_initialValue=True, trigger_persistent=False,
assignments={"x": "3"}
),
Event(
"E1",
trigger="time>2 && x<1",
priority="uniform(0, 2)",
trigger_initialValue=True, trigger_persistent=False,
assignments={"x": "5"}
)
]
}
check_model_dict(model_dict)


def test_overview_distributions():
model_dict = {
'mid': 'all_distributions',
'packages': ['distrib'],
'assignments': [
InitialAssignment('p_normal_1', 'normal(0, 1)'),
InitialAssignment('p_normal_2', 'normal(0, 1, 0, 10)'),
InitialAssignment('p_uniform', 'uniform(5, 10)'),
InitialAssignment('p_bernoulli', 'bernoulli(0.4)'),
InitialAssignment('p_binomial_1', 'binomial(100, 0.3)'),
InitialAssignment('p_binomial_2', 'binomial(100, 0.3, 0, 2)'),
InitialAssignment('p_cauchy_1', 'cauchy(0, 1)'),
InitialAssignment('p_cauchy_2', 'cauchy(0, 1, 0, 5)'),
InitialAssignment('p_chisquare_1', 'chisquare(10)'),
InitialAssignment('p_chisquare_2', 'chisquare(10, 0, 10)'),
InitialAssignment('p_exponential_1', 'exponential(1.0)'),
InitialAssignment('p_exponential_2', 'exponential(1.0, 0, 10)'),
InitialAssignment('p_gamma_1', 'gamma(0, 1)'),
InitialAssignment('p_gamma_2', 'gamma(0, 1, 0, 10)'),
InitialAssignment('p_laplace_1', 'laplace(0, 1)'),
InitialAssignment('p_laplace_2', 'laplace(0, 1, 0, 10)'),
InitialAssignment('p_lognormal_1', 'lognormal(0, 1)'),
InitialAssignment('p_lognormal_2', 'lognormal(0, 1, 0, 10)'),
InitialAssignment('p_poisson_1', 'poisson(0.5)'),
InitialAssignment('p_poisson_2', 'poisson(0.5, 0, 10)'),
InitialAssignment('p_raleigh_1', 'rayleigh(0.5)'),
InitialAssignment('p_raleigh_2', 'rayleigh(0.5, 0, 10)'),
]
}
check_model_dict(model_dict)

def test_basic_uncertainty_example():
import libsbml
model_dict = {
'mid': 'basic_example_1',
'packages': ['distrib'],
'compartments': [
Compartment("C", value=1.0)
],
'species': [
Species(sid="s1", compartment="C", initialAmount=3.22,
uncertainties=[
Uncertainty(uncertParameters=[
UncertParameter(
type=libsbml.DISTRIB_UNCERTTYPE_STANDARDDEVIATION,
value=0.3)
])
])
],
}
check_model_dict(model_dict)


def test_multiple_uncertainties():
import libsbml
model_dict = {
'mid': 'multiple_uncertainties',
'packages': ['distrib'],
'model_units': ModelUnits(time=UNIT_hr, extent=UNIT_KIND_MOLE,
substance=UNIT_KIND_MOLE,
length=UNIT_m, area=UNIT_m2,
volume=UNIT_KIND_LITRE),
'units': [UNIT_hr, UNIT_m, UNIT_m2, UNIT_mM],
'parameters': [
Parameter(sid="p1", value=5.0, unit=UNIT_mM,
uncertainties=[
Uncertainty('p1_uncertainty_1', uncertParameters=[
UncertParameter(
type=libsbml.DISTRIB_UNCERTTYPE_MEAN,
value=5.0, unit=UNIT_mM),
UncertParameter(
type=libsbml.DISTRIB_UNCERTTYPE_STANDARDDEVIATION,
value=0.3, unit=UNIT_mM),
UncertSpan(type=libsbml.DISTRIB_UNCERTTYPE_RANGE,
valueLower=2.0, valueUpper=8.0,
unit=UNIT_mM),
]),
Uncertainty('p1_uncertainty_2', uncertParameters=[
UncertParameter(
type=libsbml.DISTRIB_UNCERTTYPE_MEAN,
value=4.5, unit=UNIT_mM),
UncertParameter(
type=libsbml.DISTRIB_UNCERTTYPE_STANDARDDEVIATION,
value=1.1, unit=UNIT_mM),
UncertSpan(type=libsbml.DISTRIB_UNCERTTYPE_RANGE,
valueLower=1.0, valueUpper=10.0,
unit=UNIT_mM),
])
])
],
'assignments': [
InitialAssignment('p1', 'normal(0 mM, 1 mM)'),
]
}
check_model_dict(model_dict)


def test_define_random_variable():
import libsbml
model_dict = {
'mid': 'random_variable',
'packages': ['distrib'],
'parameters': [
Parameter("shape_Z", value=10.0),
Parameter("scale_Z", value=0.1),
Parameter("Z", value=0.1,
uncertainties=[
Uncertainty(formula="gamma(shape_Z, scale_Z)",
uncertParameters=[
UncertParameter(
type=libsbml.DISTRIB_UNCERTTYPE_MEAN,
value=1.03),
UncertParameter(
type=libsbml.DISTRIB_UNCERTTYPE_VARIANCE,
value=0.97),
])
])
]
}
check_model_dict(model_dict)


def test_paramerts_and_spans():
import libsbml
model_dict = {
'mid': 'parameters_spans',
'packages': ['distrib'],
'parameters': [
Parameter("p",
uncertainties=[
Uncertainty(
formula="normal(0, 1)", # distribution
uncertParameters=[
UncertParameter(
type=libsbml.DISTRIB_UNCERTTYPE_COEFFIENTOFVARIATION,
value=1.0),
UncertParameter(
type=libsbml.DISTRIB_UNCERTTYPE_KURTOSIS,
value=2.0),
UncertParameter(
type=libsbml.DISTRIB_UNCERTTYPE_MEAN,
value=3.0),
UncertParameter(
type=libsbml.DISTRIB_UNCERTTYPE_MEDIAN,
value=4.0),
UncertParameter(
type=libsbml.DISTRIB_UNCERTTYPE_MODE,
value=5.0),
UncertParameter(
type=libsbml.DISTRIB_UNCERTTYPE_SAMPLESIZE,
value=6.0),
UncertParameter(
type=libsbml.DISTRIB_UNCERTTYPE_SKEWNESS,
value=7.0),
UncertParameter(
type=libsbml.DISTRIB_UNCERTTYPE_STANDARDDEVIATION,
value=8.0),
UncertParameter(
type=libsbml.DISTRIB_UNCERTTYPE_STANDARDERROR,
value=9.0),
UncertParameter(
type=libsbml.DISTRIB_UNCERTTYPE_VARIANCE,
value=10.0),
UncertSpan(
type=libsbml.DISTRIB_UNCERTTYPE_CONFIDENCEINTERVAL,
valueLower=1.0, valueUpper=2.0),
UncertSpan(
type=libsbml.DISTRIB_UNCERTTYPE_CREDIBLEINTERVAL,
valueLower=2.0, valueUpper=3.0),
UncertSpan(
type=libsbml.DISTRIB_UNCERTTYPE_INTERQUARTILERANGE,
valueLower=3.0, valueUpper=4.0),
UncertSpan(
type=libsbml.DISTRIB_UNCERTTYPE_RANGE,
valueLower=4.0, valueUpper=5.0),
])
])
]
}
check_model_dict(model_dict)

0 comments on commit 27ae130

Please sign in to comment.