Skip to content

Commit f30fe66

Browse files
dweindldilpath
andauthored
Misc fixes & annotations (#450)
* type annotations * fix accessing non-existing model * remove misleading comments --------- Co-authored-by: Dilan Pathirana <59329744+dilpath@users.noreply.github.com>
1 parent 880741a commit f30fe66

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

petab/v1/core.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,18 @@ def get_notnull_columns(df: pd.DataFrame, candidates: Iterable):
133133
]
134134

135135

136-
def get_observable_replacement_id(groupvars, groupvar) -> str:
136+
def get_observable_replacement_id(
137+
groupvars: list[str], groupvar: Sequence
138+
) -> str:
137139
"""Get the replacement ID for an observable.
138140
139141
Arguments:
140142
groupvars:
141143
The columns of a PEtab measurement table that should be unique
142144
between observables in a flattened PEtab problem.
143145
groupvar:
144-
A specific grouping of `groupvars`.
146+
A specific grouping of `groupvars`. Same length and order as
147+
`groupvars`.
145148
146149
Returns:
147150
The observable replacement ID.

petab/v1/measurements.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,6 @@ def assert_overrides_match_parameter_count(
307307
row.get(OBSERVABLE_PARAMETERS, None)
308308
)
309309
)
310-
# No overrides are also allowed
311310
if actual != expected:
312311
formula = observable_df.loc[row[OBSERVABLE_ID], OBSERVABLE_FORMULA]
313312
raise AssertionError(
@@ -324,7 +323,6 @@ def assert_overrides_match_parameter_count(
324323
try:
325324
expected = noise_parameters_count[row[OBSERVABLE_ID]]
326325

327-
# No overrides are also allowed
328326
if len(replacements) != expected:
329327
raise AssertionError(
330328
f"Mismatch of noise parameter overrides in:\n{row}\n"

petab/v2/core.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171
"ParameterTable",
7272
]
7373

74+
logger = logging.getLogger(__name__)
75+
7476

7577
def _is_finite_or_neg_inf(v: float, info: ValidationInfo) -> float:
7678
if not np.isfinite(v) and v != -np.inf:
@@ -112,7 +114,7 @@ def _valid_petab_id(v: str) -> str:
112114
return v
113115

114116

115-
def _valid_petab_id_or_none(v: str) -> str:
117+
def _valid_petab_id_or_none(v: str) -> str | None:
116118
"""Field validator for optional PEtab IDs."""
117119
if not v:
118120
return None
@@ -252,7 +254,7 @@ def __getitem__(self, id_: str) -> T:
252254

253255
@classmethod
254256
@abstractmethod
255-
def from_df(cls, df: pd.DataFrame) -> BaseTable[T]:
257+
def from_df(cls, df: pd.DataFrame, **kwargs) -> BaseTable[T]:
256258
"""Create a table from a DataFrame."""
257259
pass
258260

@@ -1143,7 +1145,11 @@ def __str__(self):
11431145
f"{observables}, {measurements}, {parameters}"
11441146
)
11451147

1146-
def __getitem__(self, key):
1148+
def __getitem__(
1149+
self, key
1150+
) -> (
1151+
Condition | Experiment | Observable | Measurement | Parameter | Mapping
1152+
):
11471153
"""Get PEtab entity by ID.
11481154
11491155
This allows accessing PEtab entities such as conditions, experiments,
@@ -1202,7 +1208,7 @@ def from_yaml(
12021208
from .petab1to2 import petab1to2
12031209

12041210
if format_version[0] == 1 and yaml_file:
1205-
logging.debug(
1211+
logger.debug(
12061212
"Auto-upgrading problem from PEtab 1.0 to PEtab 2.0"
12071213
)
12081214
with TemporaryDirectory() as tmpdirname:
@@ -2320,7 +2326,9 @@ def get_output_parameters(
23202326
# filter out symbols that are defined in the model or mapped to
23212327
# such symbols
23222328
for candidate in sorted(candidates):
2323-
if self.model.symbol_allowed_in_observable_formula(candidate):
2329+
if self.model and self.model.symbol_allowed_in_observable_formula(
2330+
candidate
2331+
):
23242332
continue
23252333

23262334
# does it map to a model entity?
@@ -2329,8 +2337,11 @@ def get_output_parameters(
23292337
mapping.petab_id == candidate
23302338
and mapping.model_id is not None
23312339
):
2332-
if self.model.symbol_allowed_in_observable_formula(
2333-
mapping.model_id
2340+
if (
2341+
self.model
2342+
and self.model.symbol_allowed_in_observable_formula(
2343+
mapping.model_id
2344+
)
23342345
):
23352346
break
23362347
else:

0 commit comments

Comments
 (0)