Skip to content

Commit ca30adf

Browse files
authored
fix noise formula check (#49)
* fixups * fixup scientific notation * even better * Update lint.py
1 parent 3cf9cb3 commit ca30adf

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

petab/lint.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,24 @@ def assert_parameter_estimate_is_boolean(parameter_df: pd.DataFrame) -> None:
526526
f"Expected 0 or 1 but got {estimate} in {ESTIMATE} column.")
527527

528528

529+
def is_scalar_float(x: Any):
530+
"""
531+
Checks whether input is a number or can be transformed into a number
532+
via float
533+
:param x:
534+
input
535+
:return:
536+
True if is or can be converted to number, False otherwise.
537+
"""
538+
if isinstance(x, numbers.Number):
539+
return True
540+
try:
541+
float(x)
542+
return True
543+
except (ValueError, TypeError):
544+
return False
545+
546+
529547
def measurement_table_has_timepoint_specific_mappings(
530548
measurement_df: pd.DataFrame,
531549
allow_scalar_numeric_noise_parameters: bool = False,
@@ -553,23 +571,6 @@ def measurement_table_has_timepoint_specific_mappings(
553571
# since we edit it, copy it first
554572
measurement_df = copy.deepcopy(measurement_df)
555573

556-
def is_scalar_float(x: Any):
557-
"""
558-
Checks whether input is a number or can be transformed into a number
559-
via float
560-
:param x:
561-
input
562-
:return:
563-
True if is or can be converted to number, False otherwise.
564-
"""
565-
if isinstance(x, numbers.Number):
566-
return True
567-
try:
568-
float(x)
569-
return True
570-
except (ValueError, TypeError):
571-
return False
572-
573574
# mask numeric values
574575
for col, allow_scalar_numeric in [
575576
(OBSERVABLE_PARAMETERS, allow_scalar_numeric_observable_parameters),
@@ -622,7 +623,8 @@ def observable_table_has_nontrivial_noise_formula(
622623
"""
623624

624625
return not observable_df[NOISE_FORMULA].apply(
625-
lambda x: re.match(r'^[\w_\.]+$', x) is not None
626+
lambda x: is_scalar_float(x) or
627+
re.match(r'^[\w]+$', str(x)) is not None
626628
).all()
627629

628630

tests/test_lint.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,14 @@ def test_observable_table_has_nontrivial_noise_formula():
8686
# Ensure we fail if we have nontrivial noise formulas
8787

8888
observable_df = pd.DataFrame(data={
89-
OBSERVABLE_ID: ['0obsPar1noisePar', '2obsPar0noisePar'],
89+
OBSERVABLE_ID: ['0obsPar1noisePar', '2obsPar0noisePar',
90+
'3obsPar0noisePar'],
9091
OBSERVABLE_FORMULA: ['1.0',
92+
'1.0',
9193
'1.0'],
9294
NOISE_FORMULA: ['noiseParameter1_0obsPar1noisePar + 3.0',
93-
'1.0']
95+
1e18,
96+
'1e18']
9497
})
9598

9699
assert lint.observable_table_has_nontrivial_noise_formula(observable_df)\

0 commit comments

Comments
 (0)