Skip to content

Commit ac77767

Browse files
dweindldilpath
andcommitted
Apply suggestions from code review
Co-authored-by: Dilan Pathirana <[email protected]>
1 parent 35e4098 commit ac77767

File tree

4 files changed

+38
-43
lines changed

4 files changed

+38
-43
lines changed

petab/v2/C.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@
134134
OPERATION_TYPE = "operationType"
135135
#: Column in the condition table with the new value of the target entity
136136
TARGET_VALUE = "targetValue"
137-
# opeartion types:
137+
# operation types:
138138
OT_CUR_VAL = "setCurrentValue"
139139
OT_NO_CHANGE = "noChange"
140140

petab/v2/core.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def validate_id(cls, v):
144144
mode="before",
145145
)
146146
@classmethod
147-
def convert_nan_to_none(cls, v, info: ValidationInfo):
147+
def convert_nan_to_default(cls, v, info: ValidationInfo):
148148
if isinstance(v, float) and np.isnan(v):
149149
return cls.model_fields[info.field_name].default
150150
return v
@@ -177,7 +177,7 @@ def __getitem__(self, observable_id: str) -> Observable:
177177
raise KeyError(f"Observable ID {observable_id} not found")
178178

179179
@classmethod
180-
def from_dataframe(cls, df: pd.DataFrame) -> ObservablesTable:
180+
def from_df(cls, df: pd.DataFrame) -> ObservablesTable:
181181
if df is None:
182182
return cls(observables=[])
183183

@@ -188,16 +188,16 @@ def from_dataframe(cls, df: pd.DataFrame) -> ObservablesTable:
188188

189189
return cls(observables=observables)
190190

191-
def to_dataframe(self) -> pd.DataFrame:
191+
def to_df(self) -> pd.DataFrame:
192192
return pd.DataFrame(self.model_dump()["observables"])
193193

194194
@classmethod
195195
def from_tsv(cls, file_path: str | Path) -> ObservablesTable:
196196
df = pd.read_csv(file_path, sep="\t")
197-
return cls.from_dataframe(df)
197+
return cls.from_df(df)
198198

199199
def to_tsv(self, file_path: str | Path) -> None:
200-
df = self.to_dataframe()
200+
df = self.to_df()
201201
df.to_csv(file_path, sep="\t", index=False)
202202

203203
def __add__(self, other: Observable) -> ObservablesTable:
@@ -313,7 +313,7 @@ def __getitem__(self, condition_id: str) -> ChangeSet:
313313
raise KeyError(f"Condition ID {condition_id} not found")
314314

315315
@classmethod
316-
def from_dataframe(cls, df: pd.DataFrame) -> ConditionsTable:
316+
def from_df(cls, df: pd.DataFrame) -> ConditionsTable:
317317
if df is None:
318318
return cls(conditions=[])
319319

@@ -324,7 +324,7 @@ def from_dataframe(cls, df: pd.DataFrame) -> ConditionsTable:
324324

325325
return cls(conditions=conditions)
326326

327-
def to_dataframe(self) -> pd.DataFrame:
327+
def to_df(self) -> pd.DataFrame:
328328
records = [
329329
{C.CONDITION_ID: condition.id, **change.model_dump()}
330330
for condition in self.conditions
@@ -335,10 +335,10 @@ def to_dataframe(self) -> pd.DataFrame:
335335
@classmethod
336336
def from_tsv(cls, file_path: str | Path) -> ConditionsTable:
337337
df = pd.read_csv(file_path, sep="\t")
338-
return cls.from_dataframe(df)
338+
return cls.from_df(df)
339339

340340
def to_tsv(self, file_path: str | Path) -> None:
341-
df = self.to_dataframe()
341+
df = self.to_df()
342342
df.to_csv(file_path, sep="\t", index=False)
343343

344344
def __add__(self, other: ChangeSet) -> ConditionsTable:
@@ -421,7 +421,7 @@ class ExperimentsTable(BaseModel):
421421
experiments: list[Experiment]
422422

423423
@classmethod
424-
def from_dataframe(cls, df: pd.DataFrame) -> ExperimentsTable:
424+
def from_df(cls, df: pd.DataFrame) -> ExperimentsTable:
425425
if df is None:
426426
return cls(experiments=[])
427427

@@ -437,16 +437,16 @@ def from_dataframe(cls, df: pd.DataFrame) -> ExperimentsTable:
437437

438438
return cls(experiments=experiments)
439439

440-
def to_dataframe(self) -> pd.DataFrame:
440+
def to_df(self) -> pd.DataFrame:
441441
return pd.DataFrame(self.model_dump()["experiments"])
442442

443443
@classmethod
444444
def from_tsv(cls, file_path: str | Path) -> ExperimentsTable:
445445
df = pd.read_csv(file_path, sep="\t")
446-
return cls.from_dataframe(df)
446+
return cls.from_df(df)
447447

448448
def to_tsv(self, file_path: str | Path) -> None:
449-
df = self.to_dataframe()
449+
df = self.to_df()
450450
df.to_csv(file_path, sep="\t", index=False)
451451

452452
def __add__(self, other: Experiment) -> ExperimentsTable:
@@ -528,7 +528,7 @@ class MeasurementTable(BaseModel):
528528
measurements: list[Measurement]
529529

530530
@classmethod
531-
def from_dataframe(
531+
def from_df(
532532
cls,
533533
df: pd.DataFrame,
534534
) -> MeasurementTable:
@@ -544,16 +544,16 @@ def from_dataframe(
544544

545545
return cls(measurements=measurements)
546546

547-
def to_dataframe(self) -> pd.DataFrame:
547+
def to_df(self) -> pd.DataFrame:
548548
return pd.DataFrame(self.model_dump()["measurements"])
549549

550550
@classmethod
551551
def from_tsv(cls, file_path: str | Path) -> MeasurementTable:
552552
df = pd.read_csv(file_path, sep="\t")
553-
return cls.from_dataframe(df)
553+
return cls.from_df(df)
554554

555555
def to_tsv(self, file_path: str | Path) -> None:
556-
df = self.to_dataframe()
556+
df = self.to_df()
557557
df.to_csv(file_path, sep="\t", index=False)
558558

559559
def __add__(self, other: Measurement) -> MeasurementTable:
@@ -597,7 +597,7 @@ class MappingTable(BaseModel):
597597
mappings: list[Mapping]
598598

599599
@classmethod
600-
def from_dataframe(cls, df: pd.DataFrame) -> MappingTable:
600+
def from_df(cls, df: pd.DataFrame) -> MappingTable:
601601
if df is None:
602602
return cls(mappings=[])
603603

@@ -607,16 +607,16 @@ def from_dataframe(cls, df: pd.DataFrame) -> MappingTable:
607607

608608
return cls(mappings=mappings)
609609

610-
def to_dataframe(self) -> pd.DataFrame:
610+
def to_df(self) -> pd.DataFrame:
611611
return pd.DataFrame(self.model_dump()["mappings"])
612612

613613
@classmethod
614614
def from_tsv(cls, file_path: str | Path) -> MappingTable:
615615
df = pd.read_csv(file_path, sep="\t")
616-
return cls.from_dataframe(df)
616+
return cls.from_df(df)
617617

618618
def to_tsv(self, file_path: str | Path) -> None:
619-
df = self.to_dataframe()
619+
df = self.to_df()
620620
df.to_csv(file_path, sep="\t", index=False)
621621

622622
def __add__(self, other: Mapping) -> MappingTable:
@@ -674,7 +674,7 @@ class ParameterTable(BaseModel):
674674
parameters: list[Parameter]
675675

676676
@classmethod
677-
def from_dataframe(cls, df: pd.DataFrame) -> ParameterTable:
677+
def from_df(cls, df: pd.DataFrame) -> ParameterTable:
678678
if df is None:
679679
return cls(parameters=[])
680680

@@ -685,16 +685,16 @@ def from_dataframe(cls, df: pd.DataFrame) -> ParameterTable:
685685

686686
return cls(parameters=parameters)
687687

688-
def to_dataframe(self) -> pd.DataFrame:
688+
def to_df(self) -> pd.DataFrame:
689689
return pd.DataFrame(self.model_dump()["parameters"])
690690

691691
@classmethod
692692
def from_tsv(cls, file_path: str | Path) -> ParameterTable:
693693
df = pd.read_csv(file_path, sep="\t")
694-
return cls.from_dataframe(df)
694+
return cls.from_df(df)
695695

696696
def to_tsv(self, file_path: str | Path) -> None:
697-
df = self.to_dataframe()
697+
df = self.to_df()
698698
df.to_csv(file_path, sep="\t", index=False)
699699

700700
def __add__(self, other: Parameter) -> ParameterTable:

petab/v2/problem.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,31 +105,29 @@ def __init__(
105105
ParameterTable,
106106
)
107107

108-
self.observables_table: ObservablesTable = (
109-
ObservablesTable.from_dataframe(self.observable_df)
108+
self.observables_table: ObservablesTable = ObservablesTable.from_df(
109+
self.observable_df
110110
)
111111
self.observables: list[Observable] = self.observables_table.observables
112112

113-
self.conditions_table: ConditionsTable = (
114-
ConditionsTable.from_dataframe(self.condition_df)
113+
self.conditions_table: ConditionsTable = ConditionsTable.from_df(
114+
self.condition_df
115115
)
116116
self.conditions: list[ChangeSet] = self.conditions_table.conditions
117117

118-
self.experiments_table: ExperimentsTable = (
119-
ExperimentsTable.from_dataframe(self.experiment_df)
118+
self.experiments_table: ExperimentsTable = ExperimentsTable.from_df(
119+
self.experiment_df
120120
)
121121
self.experiments: list[Experiment] = self.experiments_table.experiments
122122

123-
self.measurement_table: MeasurementTable = (
124-
MeasurementTable.from_dataframe(
125-
self.measurement_df,
126-
)
123+
self.measurement_table: MeasurementTable = MeasurementTable.from_df(
124+
self.measurement_df,
127125
)
128126

129-
self.mapping_table: MappingTable = MappingTable.from_dataframe(
127+
self.mapping_table: MappingTable = MappingTable.from_df(
130128
self.mapping_df
131129
)
132-
self.parameter_table: ParameterTable = ParameterTable.from_dataframe(
130+
self.parameter_table: ParameterTable = ParameterTable.from_df(
133131
self.parameter_df
134132
)
135133
# TODO: visualization table

tests/v2/test_core.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515
example_dir_fujita = Path(__file__).parents[2] / "doc/example/example_Fujita"
1616

1717

18-
def test_observables_table():
18+
def test_observables_table_round_trip():
1919
file = example_dir_fujita / "Fujita_observables.tsv"
20-
21-
# read-write-read round trip
2220
observables = ObservablesTable.from_tsv(file)
2321

2422
with tempfile.TemporaryDirectory() as tmp_dir:
@@ -28,11 +26,10 @@ def test_observables_table():
2826
assert observables == observables2
2927

3028

31-
def test_conditions_table():
29+
def test_conditions_table_round_trip():
3230
with tempfile.TemporaryDirectory() as tmp_dir:
3331
petab1to2(example_dir_fujita / "Fujita.yaml", tmp_dir)
3432
file = Path(tmp_dir, "Fujita_experimentalCondition.tsv")
35-
# read-write-read round trip
3633
conditions = ConditionsTable.from_tsv(file)
3734
tmp_file = Path(tmp_dir) / "conditions.tsv"
3835
conditions.to_tsv(tmp_file)

0 commit comments

Comments
 (0)