Skip to content

Commit

Permalink
- when adding fit parameters, ensure that they are for initial refere…
Browse files Browse the repository at this point in the history
…nces, not transient ones
  • Loading branch information
fbergmann committed Jul 27, 2022
1 parent 7ad6194 commit 2e58a98
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
48 changes: 41 additions & 7 deletions basico/model_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -4469,12 +4469,15 @@ def _get_named_value(obj, name):
return value


def _get_object(name_or_reference, **kwargs):
def _get_object(name_or_reference, initial=False, **kwargs):
"""Returns the reference object for the given name
:param name_or_reference: display name of model element
:type name_or_reference: str or COPASI.CDataObject
:param initial: if True, an initial reference will be returned, rather than a transient one
:type initial: bool
:param kwargs: optional parameters
- | `model`: to specify the data model to be used (if not specified
Expand All @@ -4491,17 +4494,45 @@ def _get_object(name_or_reference, **kwargs):
obj = model.findObjectByDisplayName(name_or_reference)
if obj is None:
return None
if obj.getObjectType() != 'Reference':
obj = obj.getValueReference()
if obj.getObjectType() == 'Metabolite':
if initial:
obj = obj.getInitialConcentrationReference()
else:
obj = obj.getConcentrationReference()
elif obj.getObjectType() != 'Reference':
if initial and not isinstance(obj, COPASI.CCopasiParameter):
obj = obj.getInitialValueReference()
else:
obj = obj.getValueReference()

# ensure reference is initial or transient as required
if initial:
obj_name = obj.getObjectName()
if obj_name == 'Concentration':
obj = obj.getObjectParent().getInitialConcentrationReference()
if obj_name in ['Value', 'Volume', 'ParticleNumber']:
parent = obj.getObjectParent()
if not isinstance(parent, COPASI.CCopasiParameter):
obj = parent.getInitialValueReference()
else:
obj_name = obj.getObjectName()
if obj_name == 'InitialConcentration':
obj = obj.getObjectParent().getConcentrationReference()
if obj_name in ['InitialValue', 'InitialVolume', 'InitialParticleNumber']:
parent = obj.getObjectParent()
obj = parent.getValueReference()
return obj


def get_value(name_or_reference, **kwargs):
def get_value(name_or_reference, initial=False, **kwargs):
"""Gets the value of the named element or nones
:param name_or_reference: display name of model element
:type name_or_reference: str or COPASI.CDataObject
:param initial: if True, an initial value will be returned, rather than a transient one
:type initial: bool
:param kwargs: optional parameters
- | `model`: to specify the data model to be used (if not specified
Expand All @@ -4512,20 +4543,23 @@ def get_value(name_or_reference, **kwargs):
"""
model = model_io.get_model_from_dict_or_default(kwargs)

obj = _get_object(name_or_reference, model=model)
obj = _get_object(name_or_reference, initial=initial, model=model)

if obj is None:
return None

return _get_value_from_reference(obj)


def get_cn(name_or_reference, **kwargs):
def get_cn(name_or_reference, initial=False, **kwargs):
"""Gets the cn of the named element or none
:param name_or_reference: display name of model element
:type name_or_reference: str or COPASI.CDataObject
:param initial: if True, an initial reference cn will be returned, rather than a transient one
:type initial: bool
:param kwargs: optional parameters
- | `model`: to specify the data model to be used (if not specified
Expand All @@ -4536,7 +4570,7 @@ def get_cn(name_or_reference, **kwargs):
"""
model = model_io.get_model_from_dict_or_default(kwargs)

obj = _get_object(name_or_reference, model=model)
obj = _get_object(name_or_reference, initial=initial, model=model)

if obj is None:
return None
Expand Down
6 changes: 2 additions & 4 deletions basico/task_parameterestimation.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,14 +686,12 @@ def set_fit_parameters(fit_parameters, model=None):
if 'cn' in item:
cn = COPASI.CCommonName(item.cn)

if 'name' in item:
elif 'name' in item:
name = item['name']
if not cn:
obj = model.findObjectByDisplayName(name)
obj = basico.model_info._get_object(name, initial=True, model=model)
if obj:
cn = obj.getCN()
if _get_role_for_reference(obj.getObjectName()) == COPASI.CExperiment.ignore:
cn = obj.getValueReference().getCN()

if not cn:
logging.warning('object {0} not found'.format(name))
Expand Down

0 comments on commit 2e58a98

Please sign in to comment.