From 92e2ee5cbaf461f8d40eedd666b8f722d1003c38 Mon Sep 17 00:00:00 2001 From: "Frank T. Bergmann" Date: Thu, 18 Apr 2024 10:01:28 +0200 Subject: [PATCH] #49 fix an issue when using pandas dataframes for setting compartment sizes --- basico/model_info.py | 2 +- tests/test_basico_info.py | 41 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/basico/model_info.py b/basico/model_info.py index d830374..1e0058c 100644 --- a/basico/model_info.py +++ b/basico/model_info.py @@ -2897,7 +2897,7 @@ def _set_compartment(compartment, c_model, **kwargs): _set_expression(compartment, kwargs['expression'], model=c_model.getObjectDataModel()) c_model.setCompileFlag(True) if 'dimensionality' in kwargs: - compartment.setDimensionality(kwargs['dimensionality']) + compartment.setDimensionality(int(kwargs['dimensionality'])) if 'notes' in kwargs: compartment.setNotes(kwargs['notes']) if 'sbml_id' in kwargs: diff --git a/tests/test_basico_info.py b/tests/test_basico_info.py index ef00231..76a311f 100644 --- a/tests/test_basico_info.py +++ b/tests/test_basico_info.py @@ -350,9 +350,26 @@ def test_compartment(self): self.assertEqual(v['type'], 'assignment') self.assertEqual(v['expression'], 'Time') self.assertEqual(v['initial_expression'], '') + self.assertEqual(v['dimensionality'], 3) + + + # change dimensions to two + basico.set_compartment('v', exact=True, dimensionality=2) + v_df = basico.get_compartments('v') + v = basico.as_dict(v_df) + self.assertEqual(v['dimensionality'], 2) + + basico.set_compartment('v', exact=True, dimensionality=v['dimensionality']) + basico.set_compartment('v', exact=True, dimensionality=v_df.iloc[0]['dimensionality']) + + # apply all recorded changes + basico.set_compartment(exact=True, **v) + + basico.remove_compartment('v') self.assertIsNone(basico.get_compartments('v', exact=True)) + def test_global_parameters(self): basico.add_parameter('p', initial_value=3) v = basico.get_parameters('p', exact=True) @@ -532,5 +549,29 @@ def test_retrieval_from_multiple_models(self): basico.remove_datamodel(dm1) basico.remove_datamodel(dm2) + def test_expresssions(self): + dm = basico.new_model(name='Test Model'); + basico.add_species('X'); + basico.add_species('Y'); + basico.add_parameter('a', initial_value=1); + + basico.add_parameter('b', initial_value=1); + basico.set_parameters('b', exact=True, expression='{Values[a].InitialValue}/2', status='assignment') + params1 = basico.as_dict(basico.get_parameters(type='assignment')) + basico.set_parameters('b', exact=True, expression='Values[a].InitialValue/2', status='assignment') + params2 = basico.as_dict(basico.get_parameters(type='assignment')) + + basico.add_parameter('c', initial_value=1); + basico.set_parameters('c', exact=True, expression='{[X]}+{[Y]}', status='assignment') + params31 = basico.as_dict(basico.get_parameters(type='assignment')) + basico.set_parameters('c', exact=True, expression=' {[X]} + {[Y]}', status='assignment') + params3 = basico.as_dict(basico.get_parameters(type='assignment')) + basico.set_parameters('c', exact=True, expression=' [X] + [Y]', status='assignment') + params4 = basico.as_dict(basico.get_parameters(type='assignment')) + params = basico.as_dict(basico.get_parameters(type='assignment')) + + + basico.remove_datamodel(dm); + if __name__ == "__main__": unittest.main()