-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add PetabProblem class for handling PEtab-defined simulation con… #2255
Merged
+459
−266
Merged
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7d23f50
Add AmiciPetabProblem class for handling PEtab-defined simulation con…
dweindl f2f0d12
Remove solver
dweindl d726c8f
..
dweindl 82a9409
test
dweindl 886567c
Merge branch 'develop' into amici_petab_problem
dweindl 625f2b3
test more
dweindl 7179189
doc
dweindl ce7a3d9
Avoid warning when simulating with default parameters
dweindl 4932f83
petab notebook makeover
dweindl 9ab1463
Merge branch 'develop' into amici_petab_problem
dweindl d806546
AmiciPetabProblem -> PetabProblem
dweindl 7c2f6b1
clean up scaled_parameters
dweindl b449c5b
Update python/sdist/amici/petab/petab_problem.py
dweindl f9d5966
fix test + cov
dweindl c8609c3
Merge branch 'develop' into amici_petab_problem
dweindl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
AmiciPetabProblem -> PetabProblem
- Loading branch information
commit d8065465a48b069f76b6d579fdab0bbdb879c2a4
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
from .parameter_mapping import create_parameter_mapping | ||
|
||
|
||
class AmiciPetabProblem: | ||
class PetabProblem: | ||
"""Manage experimental conditions based on a PEtab problem definition. | ||
|
||
Create :class:`ExpData` objects from a PEtab problem definition, and handle | ||
|
@@ -33,7 +33,7 @@ | |
memory if the given PEtab problem comprises many simulation conditions. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
petab_problem: petab.Problem, | ||
amici_model: Optional[amici.Model] = None, | ||
|
@@ -42,57 +42,57 @@ | |
simulation_conditions: Union[pd.DataFrame, list[dict]] = None, | ||
store_edatas: bool = True, | ||
): | ||
self._petab_problem = copy.deepcopy(petab_problem) | ||
|
||
if amici_model is not None: | ||
self._amici_model = amici_model | ||
else: | ||
from .petab_import import import_petab_problem | ||
|
||
self._amici_model = import_petab_problem(petab_problem) | ||
|
||
self._scaled_parameters = scaled_parameters | ||
|
||
self._simulation_conditions = simulation_conditions or ( | ||
petab_problem.get_simulation_conditions_from_measurement_df() | ||
) | ||
if not isinstance(self._simulation_conditions, pd.DataFrame): | ||
self._simulation_conditions = pd.DataFrame( | ||
self._simulation_conditions | ||
) | ||
if ( | ||
preeq_id := PREEQUILIBRATION_CONDITION_ID | ||
) in self._simulation_conditions: | ||
self._simulation_conditions[ | ||
preeq_id | ||
] = self._simulation_conditions[preeq_id].fillna("") | ||
|
||
if problem_parameters is None: | ||
# Use PEtab nominal values as default | ||
self._problem_parameters = self._default_parameters() | ||
if scaled_parameters is True: | ||
raise NotImplementedError( | ||
"scaled_parameters=True in combination with default " | ||
"parameters is not implemented yet." | ||
) | ||
scaled_parameters = False | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove? |
||
else: | ||
self._problem_parameters = problem_parameters | ||
self._scaled_parameters = scaled_parameters | ||
|
||
if store_edatas: | ||
self._parameter_mapping = create_parameter_mapping( | ||
petab_problem=self._petab_problem, | ||
simulation_conditions=self._simulation_conditions, | ||
scaled_parameters=self._scaled_parameters, | ||
amici_model=self._amici_model, | ||
) | ||
self._create_edatas() | ||
else: | ||
self._parameter_mapping = None | ||
self._edatas = None | ||
|
||
def set_parameters( | ||
self, | ||
problem_parameters: dict[str, float], | ||
scaled_parameters: bool = False, | ||
|
@@ -104,38 +104,38 @@ | |
:param scaled_parameters: Whether the provided parameters are on PEtab | ||
`parameterScale` or not. | ||
""" | ||
if scaled_parameters != self._scaled_parameters: | ||
dweindl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# redo parameter mapping if scale changed | ||
self._parameter_mapping = create_parameter_mapping( | ||
petab_problem=self._petab_problem, | ||
simulation_conditions=self._simulation_conditions, | ||
scaled_parameters=scaled_parameters, | ||
amici_model=self._amici_model, | ||
) | ||
|
||
if set(self._problem_parameters) - set(problem_parameters): | ||
# not all parameters are provided - update | ||
# bring previously set parameters to the same scale if necessary | ||
if scaled_parameters and not self._scaled_parameters: | ||
self._problem_parameters = ( | ||
self._petab_problem.scale_parameters( | ||
self._problem_parameters, | ||
) | ||
) | ||
elif not scaled_parameters and self._scaled_parameters: | ||
self._problem_parameters = ( | ||
self._petab_problem.unscale_parameters( | ||
self._problem_parameters, | ||
) | ||
) | ||
self._problem_parameters |= problem_parameters | ||
else: | ||
self._problem_parameters = problem_parameters | ||
|
||
self._scaled_parameters = scaled_parameters | ||
|
||
if self._edatas: | ||
fill_in_parameters( | ||
edatas=self._edatas, | ||
problem_parameters=self._problem_parameters, | ||
scaled_parameters=self._scaled_parameters, | ||
|
@@ -143,7 +143,7 @@ | |
amici_model=self._amici_model, | ||
) | ||
|
||
def get_edata( | ||
self, condition_id: str, preequilibration_condition_id: str = None | ||
) -> amici.ExpData: | ||
"""Get ExpData object for a given condition. | ||
|
@@ -151,7 +151,7 @@ | |
NOTE: If ``store_edatas=True`` was passed to the constructor and the | ||
returned object is modified, the changes will be reflected in the | ||
internal `ExpData` objects. Also, if parameter values of | ||
`AmiciPetabProblem` are changed, all `ExpData` objects will be updated. | ||
`PetabProblem` are changed, all `ExpData` objects will be updated. | ||
Create a deep copy if you want to avoid this. | ||
|
||
:param condition_id: PEtab condition ID | ||
|
@@ -159,45 +159,45 @@ | |
:return: ExpData object | ||
""" | ||
# exists or has to be created? | ||
if self._edatas: | ||
edata_id = condition_id | ||
if preequilibration_condition_id: | ||
edata_id += "+" + preequilibration_condition_id | ||
|
||
for edata in self._edatas: | ||
if edata.id == edata_id: | ||
return edata | ||
|
||
return self._create_edata(condition_id, preequilibration_condition_id) | ||
|
||
def get_edatas(self): | ||
"""Get all ExpData objects. | ||
|
||
NOTE: If ``store_edatas=True`` was passed to the constructor and the | ||
returned objects are modified, the changes will be reflected in the | ||
internal `ExpData` objects. Also, if parameter values of | ||
`AmiciPetabProblem` are changed, all `ExpData` objects will be updated. | ||
`PetabProblem` are changed, all `ExpData` objects will be updated. | ||
Create a deep copy if you want to avoid this. | ||
|
||
:return: List of ExpData objects | ||
""" | ||
if self._edatas: | ||
# shallow copy | ||
return self._edatas.copy() | ||
|
||
# not storing edatas - create and return | ||
self._parameter_mapping = create_parameter_mapping( | ||
petab_problem=self._petab_problem, | ||
simulation_conditions=self._simulation_conditions, | ||
scaled_parameters=self._scaled_parameters, | ||
amici_model=self._amici_model, | ||
) | ||
self._create_edatas() | ||
result = self._edatas | ||
self._edatas = [] | ||
return result | ||
|
||
def _create_edata( | ||
self, condition_id: str, preequilibration_condition_id: str | ||
) -> amici.ExpData: | ||
"""Create ExpData object for a given condition. | ||
|
@@ -206,7 +206,7 @@ | |
:param preequilibration_condition_id: PEtab preequilibration condition ID | ||
:return: ExpData object | ||
""" | ||
simulation_condition = pd.DataFrame( | ||
[ | ||
{ | ||
SIMULATION_CONDITION_ID: condition_id, | ||
|
@@ -215,12 +215,12 @@ | |
} | ||
] | ||
) | ||
edatas = create_edatas( | ||
amici_model=self._amici_model, | ||
petab_problem=self._petab_problem, | ||
simulation_conditions=simulation_condition, | ||
) | ||
parameter_mapping = create_parameter_mapping( | ||
petab_problem=self._petab_problem, | ||
simulation_conditions=simulation_condition, | ||
scaled_parameters=self._scaled_parameters, | ||
|
@@ -228,7 +228,7 @@ | |
) | ||
|
||
# Fill parameters in ExpDatas (in-place) | ||
fill_in_parameters( | ||
edatas=edatas, | ||
problem_parameters={ | ||
p: self._problem_parameters[p] | ||
|
@@ -240,21 +240,21 @@ | |
amici_model=self._amici_model, | ||
) | ||
|
||
if len(edatas) != 1: | ||
raise AssertionError("Expected exactly one ExpData object.") | ||
return edatas[0] | ||
|
||
def _create_edatas( | ||
self, | ||
): | ||
"""Create ExpData objects from PEtab problem definition.""" | ||
self._edatas = create_edatas( | ||
amici_model=self._amici_model, | ||
petab_problem=self._petab_problem, | ||
simulation_conditions=self._simulation_conditions, | ||
) | ||
|
||
fill_in_parameters( | ||
edatas=self._edatas, | ||
problem_parameters=self._problem_parameters, | ||
scaled_parameters=self._scaled_parameters, | ||
|
@@ -262,16 +262,16 @@ | |
amici_model=self._amici_model, | ||
) | ||
|
||
def _default_parameters(self) -> dict[str, float]: | ||
"""Get unscaled default parameters.""" | ||
return { | ||
t.Index: getattr(t, petab.NOMINAL_VALUE) | ||
for t in self._petab_problem.parameter_df[ | ||
self._petab_problem.parameter_df[petab.ESTIMATE] == 1 | ||
].itertuples() | ||
} | ||
|
||
@property | ||
def model(self) -> amici.Model: | ||
"""AMICI model.""" | ||
return self._amici_model | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is codecov not reporting any coverage here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops. Because I started putting tests in a separate PEtab directory which was not included in the pytest invocation.