-
Notifications
You must be signed in to change notification settings - Fork 7
Functions for adding conditions/observables/parameter to Problem #328
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,8 +3,9 @@ | |||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| import os | ||||||||||||||||||||||||||
| import tempfile | ||||||||||||||||||||||||||
| from collections.abc import Iterable | ||||||||||||||||||||||||||
| from collections.abc import Iterable, Sequence | ||||||||||||||||||||||||||
| from math import nan | ||||||||||||||||||||||||||
| from numbers import Number | ||||||||||||||||||||||||||
| from pathlib import Path, PurePosixPath | ||||||||||||||||||||||||||
| from typing import TYPE_CHECKING | ||||||||||||||||||||||||||
| from warnings import warn | ||||||||||||||||||||||||||
|
|
@@ -1005,3 +1006,177 @@ def n_priors(self) -> int: | |||||||||||||||||||||||||
| return 0 | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| return self.parameter_df[OBJECTIVE_PRIOR_PARAMETERS].notna().sum() | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def add_condition(self, id_: str, name: str = None, **kwargs): | ||||||||||||||||||||||||||
| """Add a simulation condition to the problem. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Arguments: | ||||||||||||||||||||||||||
| id_: The condition id | ||||||||||||||||||||||||||
| name: The condition name | ||||||||||||||||||||||||||
| kwargs: Parameter, value pairs to add to the condition table. | ||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||
| record = {CONDITION_ID: [id_], **kwargs} | ||||||||||||||||||||||||||
| if name is not None: | ||||||||||||||||||||||||||
| record[CONDITION_NAME] = name | ||||||||||||||||||||||||||
| tmp_df = pd.DataFrame(record).set_index([CONDITION_ID]) | ||||||||||||||||||||||||||
| if self.condition_df is None: | ||||||||||||||||||||||||||
| self.condition_df = tmp_df | ||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||
| self.condition_df = pd.concat([self.condition_df, tmp_df]) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def add_observable( | ||||||||||||||||||||||||||
| self, | ||||||||||||||||||||||||||
| id_: str, | ||||||||||||||||||||||||||
| formula: str | float | int, | ||||||||||||||||||||||||||
| noise_formula: str | float | int = None, | ||||||||||||||||||||||||||
| noise_distribution: str = None, | ||||||||||||||||||||||||||
| transform: str = None, | ||||||||||||||||||||||||||
| name: str = None, | ||||||||||||||||||||||||||
|
Comment on lines
+1030
to
+1035
Member
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. As above
Suggested change
|
||||||||||||||||||||||||||
| **kwargs, | ||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||
| """Add an observable to the problem. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Arguments: | ||||||||||||||||||||||||||
| id_: The observable id | ||||||||||||||||||||||||||
| formula: The observable formula | ||||||||||||||||||||||||||
| noise_formula: The noise formula | ||||||||||||||||||||||||||
| noise_distribution: The noise distribution | ||||||||||||||||||||||||||
| transform: The observable transformation | ||||||||||||||||||||||||||
| name: The observable name | ||||||||||||||||||||||||||
| kwargs: additional columns/values to add to the observable table | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||
| record = { | ||||||||||||||||||||||||||
| OBSERVABLE_ID: [id_], | ||||||||||||||||||||||||||
| OBSERVABLE_FORMULA: [formula], | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| if name is not None: | ||||||||||||||||||||||||||
| record[OBSERVABLE_NAME] = [name] | ||||||||||||||||||||||||||
| if noise_formula is not None: | ||||||||||||||||||||||||||
| record[NOISE_FORMULA] = [noise_formula] | ||||||||||||||||||||||||||
| if noise_distribution is not None: | ||||||||||||||||||||||||||
| record[NOISE_DISTRIBUTION] = [noise_distribution] | ||||||||||||||||||||||||||
| if transform is not None: | ||||||||||||||||||||||||||
| record[OBSERVABLE_TRANSFORMATION] = [transform] | ||||||||||||||||||||||||||
| record.update(kwargs) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| tmp_df = pd.DataFrame(record).set_index([OBSERVABLE_ID]) | ||||||||||||||||||||||||||
| if self.observable_df is None: | ||||||||||||||||||||||||||
| self.observable_df = tmp_df | ||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||
| self.observable_df = pd.concat([self.observable_df, tmp_df]) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
| tmp_df = pd.DataFrame(record).set_index([OBSERVABLE_ID]) | |
| if self.observable_df is None: | |
| self.observable_df = tmp_df | |
| else: | |
| self.observable_df = pd.concat([self.observable_df, tmp_df]) | |
| tmp_df = pd.DataFrame(record).set_index([OBSERVABLE_ID]) | |
| self.observable_df = pd.concat([self.observable_df, tmp_df]) |
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.
There is still some unnecessary things happening in pandas before this not-None object is returned. Then again, these functions here aren't so much about efficiency. I will think about it, thanks for the suggestion.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -136,6 +136,9 @@ def _copy_file(src: Path | str, dest: Path | str): | |
| src = str(src) | ||
| dest = str(dest) | ||
|
|
||
| if src == dest: | ||
| return | ||
|
||
|
|
||
| if is_url(src): | ||
| with get_handle(src, mode="r") as src_handle: | ||
| with open(dest, "w") as dest_handle: | ||
|
|
||
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.
To match column headers/
C.py? Not exactly the same due to underscores... but we could decide whether to go for consistent or context-specific IDs everywhere.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.
Alternative: change all of these
add_*methods to take**kwargsthat are used to create apd.Series, which is then validated and concatenated.get_*_dfcan be used to set the index. Then no table-specific code, and no need to redefine column names here?if not isinstance(kwarg, str) and isinstance(kwarg, list): kwarg = PARAMETER_SEPARATOR.join(map(str, kwarg))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.
Yeah, I was struggling with what would be preferable. In
add_condition, it feels redundant to prefix everything withcondition_. Then again, it might be considered confusing if the arguments don't match the table columns. For me, the former felt more important. Also with regards to potentially introducing a proper object model, I think we'd want things to be more pythonic and less petaby.I don't think the kwargs-solution would be very convenient. That would mean, you'd have write
add_observable(**{petab.OBSERVABLE_ID:"foo", petab.SIMULATION_CONDITION_ID: "bar"}), I think then I rather directly go back to constructing dataframes.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.
I'll leave it as is for. As discussed elsewhere, the v2 API is likely to change drastically overall where these points will be addressed.
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.
Alright, fine for v1, we can revisit v2 in case we change the column names there