Skip to content

Commit

Permalink
Allow constant expressions for compartment sizes (#525)
Browse files Browse the repository at this point in the history
Previously, only parameters were allowed for compartment sizes.
This change allows specifying compartment sizes using constant expressions.

Closes #524
Co-authored-by: Jeremy Muhlich <jmuhlich@bitflood.org>
  • Loading branch information
dweindl committed Dec 17, 2020
1 parent 38d96d4 commit cc94061
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
13 changes: 8 additions & 5 deletions pysb/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1345,9 +1345,10 @@ class Compartment(Component):
dimension : integer, optional
The number of spatial dimensions in the compartment, either 2 (i.e. a
membrane) or 3 (a volume).
size : Parameter, optional
A parameter object whose value defines the volume or area of the
compartment. If not specified, the size will be fixed at 1.0.
size : Parameter or Expression, optional
A parameter or constant expression object whose value defines the
volume or area of the compartment. If not specified, the size will be
fixed at 1.0.
Attributes
----------
Expand All @@ -1372,8 +1373,10 @@ def __init__(self, name, parent=None, dimension=3, size=None, _export=True):
if parent != None and isinstance(parent, Compartment) == False:
raise Exception("parent must be a predefined Compartment or None")
#FIXME: check for only ONE "None" parent? i.e. only one compartment can have a parent None?
if size is not None and not isinstance(size, Parameter):
raise Exception("size must be a parameter (or omitted)")
if size is not None and not isinstance(size, Parameter) and not \
(isinstance(size, Expression) and size.is_constant_expression()):
raise Exception("size must be a parameter or a constant expression"
" (or omitted)")
self.parent = parent
self.dimension = dimension
self.size = size
Expand Down
18 changes: 18 additions & 0 deletions pysb/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,17 @@ def test_compartment_initial_error():
Initial(A(s=None)**c1, A_0)
Initial(A(s=None)**c2, A_0)

@with_model
def test_compartment():
# Ensure that compartment size can be specified by a parameter,
# a constant expression, or be omitted.
Parameter('A', 1.0)
Parameter('B', 2.0)
Expression('E', A * B)
Compartment("C1")
Compartment("C2", C1, 2, A)
Compartment("C3", C1, 2, E)

@with_model
def test_monomer_pattern_add_to_none():
"""Ensure that MonomerPattern + None returns a ReactionPattern."""
Expand Down Expand Up @@ -530,6 +541,13 @@ def test_invalid_parameter():
@with_model
def test_invalid_compartment():
assert_raises(Exception, Compartment, 'c1', 'invalid_parent')

# Invalid dynamic expression as compartment size
Monomer('A')
Observable('O', A)
Expression('E', O)
assert_raises(Exception, Compartment, 'c2', size=E)

assert len(model.compartments) == 0


Expand Down

0 comments on commit cc94061

Please sign in to comment.