Skip to content

Commit 6e5aaa6

Browse files
Merge pull request #694 from ICB-DCM/develop
Release 0.10.7
2 parents e42d272 + b044ffa commit 6e5aaa6

File tree

3 files changed

+58
-13
lines changed

3 files changed

+58
-13
lines changed

python/amici/sbml_import.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,10 @@ def __init__(
8686
8787
Arguments:
8888
89-
sbml_source: Either a path to SBML file where the model is specified.
90-
Or a model string as created by sbml.sbmlWriter().writeSBMLToString().
91-
@type string
89+
sbml_source: Either a path to SBML file where the model is
90+
specified, or a model string as created by
91+
sbml.sbmlWriter().writeSBMLToString() or an instance of
92+
libsbml.Model. @type str
9293
9394
show_sbml_warnings: Indicates whether libSBML warnings should be
9495
displayed (default = True). @type bool
@@ -103,13 +104,15 @@ def __init__(
103104
Raises:
104105
105106
"""
106-
self.sbml_reader = sbml.SBMLReader()
107-
108-
if from_file:
109-
sbml_doc = self.sbml_reader.readSBMLFromFile(sbml_source)
107+
if isinstance(sbml_source, sbml.Model):
108+
self.sbml_doc = sbml_source.getSBMLDocument()
110109
else:
111-
sbml_doc = self.sbml_reader.readSBMLFromString(sbml_source)
112-
self.sbml_doc = sbml_doc
110+
self.sbml_reader = sbml.SBMLReader()
111+
if from_file:
112+
sbml_doc = self.sbml_reader.readSBMLFromFile(sbml_source)
113+
else:
114+
sbml_doc = self.sbml_reader.readSBMLFromString(sbml_source)
115+
self.sbml_doc = sbml_doc
113116

114117
self.show_sbml_warnings = show_sbml_warnings
115118

@@ -878,13 +881,17 @@ def processObservables(self, observables, sigmas, noise_distributions):
878881

879882
# set cost functions
880883
llhYStrings = []
881-
for y_name in observables:
884+
for y_name in observableNames:
882885
llhYStrings.append(noise_distribution_to_cost_function(
883886
noise_distributions.get(y_name, 'normal')))
884887

885888
llhYValues = []
886-
for llhYString, o_sym, m_sym, s_sym in zip(llhYStrings, observableSyms, measurementYSyms, sigmaYSyms):
887-
f = sp.sympify(llhYString(o_sym), locals={str(o_sym): o_sym, str(m_sym): m_sym, str(s_sym): s_sym})
889+
for llhYString, o_sym, m_sym, s_sym \
890+
in zip(llhYStrings, observableSyms,
891+
measurementYSyms, sigmaYSyms):
892+
f = sp.sympify(llhYString(o_sym), locals={str(o_sym): o_sym,
893+
str(m_sym): m_sym,
894+
str(s_sym): s_sym})
888895
llhYValues.append(f)
889896
llhYValues = sp.Matrix(llhYValues)
890897

tests/testMisc.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import os
99
import unittest
1010
import sympy as sp
11+
import libsbml
12+
from tempfile import TemporaryDirectory
13+
1114

1215
class TestAmiciMisc(unittest.TestCase):
1316
"""TestCase class various AMICI Python interface functions"""
@@ -47,6 +50,18 @@ def test_csc_matrix(self):
4750
assert symbolList == ['a0', 'a1', 'a2']
4851
assert str(sparseMatrix) == 'Matrix([[a0, 0], [a1, a2]])'
4952

53+
def test_csc_matrix_empty(self):
54+
"""Test sparse CSC matrix creation for empty matrix"""
55+
matrix = sp.Matrix()
56+
symbolColPtrs, symbolRowVals, sparseList, symbolList, sparseMatrix = \
57+
amici.ode_export.csc_matrix(matrix, 'a')
58+
print(symbolColPtrs, symbolRowVals, sparseList, symbolList, sparseMatrix)
59+
assert symbolColPtrs == [0]
60+
assert symbolRowVals == []
61+
assert sparseList == sp.Matrix(0, 0, [])
62+
assert symbolList == []
63+
assert str(sparseMatrix) == 'Matrix(0, 0, [])'
64+
5065
def test_csc_matrix_vector(self):
5166
"""Test sparse CSC matrix creation from matrix slice"""
5267
matrix = sp.Matrix([[1, 0], [2, 3]])
@@ -70,6 +85,29 @@ def test_csc_matrix_vector(self):
7085
assert symbolList == ['a2']
7186
assert str(sparseMatrix) == 'Matrix([[0], [a2]])'
7287

88+
def test_sbml2amici_no_observables(self):
89+
"""Test model generation works for model without observables"""
90+
91+
document = libsbml.SBMLDocument(3, 1)
92+
model = document.createModel()
93+
model.setTimeUnits("second")
94+
model.setExtentUnits("mole")
95+
model.setSubstanceUnits('mole')
96+
c1 = model.createCompartment()
97+
c1.setId('C1')
98+
model.addCompartment(c1)
99+
s1 = model.createSpecies()
100+
s1.setId('S1')
101+
s1.setCompartment('C1')
102+
model.addSpecies(s1)
103+
104+
sbml_importer = amici.sbml_import.SbmlImporter(sbml_source=model,
105+
from_file=False)
106+
tmpdir = TemporaryDirectory()
107+
sbml_importer.sbml2amici(modelName="test",
108+
output_dir=tmpdir.name,
109+
observables=None)
110+
73111

74112
if __name__ == '__main__':
75113
suite = unittest.TestSuite()

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.10.6
1+
0.10.7

0 commit comments

Comments
 (0)