Skip to content

Commit 63676b7

Browse files
dweindldilpath
andauthored
Lint: Check for valid parameter IDs in observable/noise parameters (#69)
* Lint: Check for valid parameter IDs in observable/noise parameters Closes #61 * Update petab/measurements.py Co-authored-by: Dilan Pathirana <59329744+dilpath@users.noreply.github.com> * Update petab/measurements.py Co-authored-by: Dilan Pathirana <59329744+dilpath@users.noreply.github.com> * no strip Co-authored-by: Dilan Pathirana <59329744+dilpath@users.noreply.github.com>
1 parent dde5478 commit 63676b7

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

petab/measurements.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def get_unique_parameters(series):
190190

191191
def split_parameter_replacement_list(
192192
list_string: Union[str, numbers.Number],
193-
delim: str = ';') -> List[Union[str, float]]:
193+
delim: str = ';') -> List[Union[str, numbers.Number]]:
194194
"""
195195
Split values in observableParameters and noiseParameters in measurement
196196
table.
@@ -200,9 +200,10 @@ def split_parameter_replacement_list(
200200
delim: delimiter
201201
202202
Returns:
203-
List of split values. Numeric values converted to float.
203+
List of split values. Numeric values may be converted to `float`,
204+
and parameter IDs are kept as strings.
204205
"""
205-
if list_string is None:
206+
if list_string is None or list_string == '':
206207
return []
207208

208209
if isinstance(list_string, numbers.Number):
@@ -212,8 +213,21 @@ def split_parameter_replacement_list(
212213
return []
213214
return [list_string]
214215

215-
result = [x.strip() for x in list_string.split(delim) if len(x.strip())]
216-
return [core.to_float_if_float(x) for x in result]
216+
result = [x.strip() for x in list_string.split(delim)]
217+
218+
def convert_and_check(x):
219+
x = core.to_float_if_float(x)
220+
if isinstance(x, float):
221+
return x
222+
if lint.is_valid_identifier(x):
223+
return x
224+
225+
raise ValueError(
226+
f"The value '{x}' in the parameter replacement list "
227+
f"'{list_string}' is neither a number, nor a valid parameter ID."
228+
)
229+
230+
return list(map(convert_and_check, result))
217231

218232

219233
def create_measurement_df() -> pd.DataFrame:

tests/test_petab.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ def test_split_parameter_replacement_list():
129129
assert petab.split_parameter_replacement_list(1.5) == [1.5]
130130
assert petab.split_parameter_replacement_list(None) == []
131131

132+
with pytest.raises(ValueError):
133+
assert petab.split_parameter_replacement_list('1.0;')
134+
135+
with pytest.raises(ValueError):
136+
assert petab.split_parameter_replacement_list(';1.0')
137+
132138

133139
def test_get_measurement_parameter_ids():
134140
measurement_df = pd.DataFrame(

0 commit comments

Comments
 (0)