Skip to content

Commit 34ebde1

Browse files
committed
resolve paths
1 parent 6b9bff5 commit 34ebde1

File tree

3 files changed

+60
-45
lines changed

3 files changed

+60
-45
lines changed

petab/v1/problem.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,10 +1019,11 @@ def add_condition(self, id_: str, name: str = None, **kwargs):
10191019
if name is not None:
10201020
record[CONDITION_NAME] = name
10211021
tmp_df = pd.DataFrame(record).set_index([CONDITION_ID])
1022-
if self.condition_df is None:
1023-
self.condition_df = tmp_df
1024-
else:
1025-
self.condition_df = pd.concat([self.condition_df, tmp_df])
1022+
self.condition_df = (
1023+
pd.concat([self.condition_df, tmp_df])
1024+
if self.condition_df is not None
1025+
else tmp_df
1026+
)
10261027

10271028
def add_observable(
10281029
self,
@@ -1061,10 +1062,11 @@ def add_observable(
10611062
record.update(kwargs)
10621063

10631064
tmp_df = pd.DataFrame(record).set_index([OBSERVABLE_ID])
1064-
if self.observable_df is None:
1065-
self.observable_df = tmp_df
1066-
else:
1067-
self.observable_df = pd.concat([self.observable_df, tmp_df])
1065+
self.observable_df = (
1066+
pd.concat([self.observable_df, tmp_df])
1067+
if self.observable_df is not None
1068+
else tmp_df
1069+
)
10681070

10691071
def add_parameter(
10701072
self,
@@ -1132,10 +1134,11 @@ def add_parameter(
11321134
record.update(kwargs)
11331135

11341136
tmp_df = pd.DataFrame(record).set_index([PARAMETER_ID])
1135-
if self.parameter_df is None:
1136-
self.parameter_df = tmp_df
1137-
else:
1138-
self.parameter_df = pd.concat([self.parameter_df, tmp_df])
1137+
self.parameter_df = (
1138+
pd.concat([self.parameter_df, tmp_df])
1139+
if self.parameter_df is not None
1140+
else tmp_df
1141+
)
11391142

11401143
def add_measurement(
11411144
self,
@@ -1176,7 +1179,8 @@ def add_measurement(
11761179
record[PREEQUILIBRATION_CONDITION_ID] = [preeq_cond_id]
11771180

11781181
tmp_df = pd.DataFrame(record)
1179-
if self.measurement_df is None:
1180-
self.measurement_df = tmp_df
1181-
else:
1182-
self.measurement_df = pd.concat([self.measurement_df, tmp_df])
1182+
self.measurement_df = (
1183+
pd.concat([self.measurement_df, tmp_df])
1184+
if self.measurement_df is not None
1185+
else tmp_df
1186+
)

petab/v2/petab1to2.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import shutil
33
from itertools import chain
44
from pathlib import Path
5+
from urllib.parse import urlparse
56

67
from pandas.io.common import get_handle, is_url
78

@@ -76,7 +77,7 @@ def petab1to2(yaml_config: Path | str, output_dir: Path | str = None):
7677
# condition tables, observable tables, SBML files, parameter table:
7778
# no changes - just copy
7879
file = yaml_config[C.PARAMETER_FILE]
79-
_copy_file(get_src_path(file), get_dest_path(file))
80+
_copy_file(get_src_path(file), Path(get_dest_path(file)))
8081

8182
for problem_config in yaml_config[C.PROBLEMS]:
8283
for file in chain(
@@ -89,7 +90,7 @@ def petab1to2(yaml_config: Path | str, output_dir: Path | str = None):
8990
problem_config.get(C.MEASUREMENT_FILES, []),
9091
problem_config.get(C.VISUALIZATION_FILES, []),
9192
):
92-
_copy_file(get_src_path(file), get_dest_path(file))
93+
_copy_file(get_src_path(file), Path(get_dest_path(file)))
9394

9495
# TODO: Measurements: preequilibration to experiments/timecourses once
9596
# finalized
@@ -131,18 +132,23 @@ def _update_yaml(yaml_config: dict) -> dict:
131132
return yaml_config
132133

133134

134-
def _copy_file(src: Path | str, dest: Path | str):
135+
def _copy_file(src: Path | str, dest: Path):
135136
"""Copy file."""
136-
src = str(src)
137-
dest = str(dest)
138-
139-
if src == dest:
140-
return
137+
# src might be a URL - convert to Path if local
138+
src_url = urlparse(src)
139+
if not src_url.scheme:
140+
src = Path(src)
141+
elif src_url.scheme == "file" and not src_url.netloc:
142+
src = Path(src.removeprefix("file:/"))
141143

142144
if is_url(src):
143145
with get_handle(src, mode="r") as src_handle:
144146
with open(dest, "w") as dest_handle:
145147
dest_handle.write(src_handle.handle.read())
146148
return
147149

148-
shutil.copy(str(src), str(dest))
150+
try:
151+
if dest.samefile(src):
152+
return
153+
except FileNotFoundError:
154+
shutil.copy(str(src), str(dest))

petab/v2/problem.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -739,10 +739,11 @@ def add_condition(self, id_: str, name: str = None, **kwargs):
739739
if name is not None:
740740
record[CONDITION_NAME] = name
741741
tmp_df = pd.DataFrame(record).set_index([CONDITION_ID])
742-
if self.condition_df is None:
743-
self.condition_df = tmp_df
744-
else:
745-
self.condition_df = pd.concat([self.condition_df, tmp_df])
742+
self.condition_df = (
743+
pd.concat([self.condition_df, tmp_df])
744+
if self.condition_df is not None
745+
else tmp_df
746+
)
746747

747748
def add_observable(
748749
self,
@@ -781,10 +782,11 @@ def add_observable(
781782
record.update(kwargs)
782783

783784
tmp_df = pd.DataFrame(record).set_index([OBSERVABLE_ID])
784-
if self.observable_df is None:
785-
self.observable_df = tmp_df
786-
else:
787-
self.observable_df = pd.concat([self.observable_df, tmp_df])
785+
self.observable_df = (
786+
pd.concat([self.observable_df, tmp_df])
787+
if self.observable_df is not None
788+
else tmp_df
789+
)
788790

789791
def add_parameter(
790792
self,
@@ -852,10 +854,11 @@ def add_parameter(
852854
record.update(kwargs)
853855

854856
tmp_df = pd.DataFrame(record).set_index([PARAMETER_ID])
855-
if self.parameter_df is None:
856-
self.parameter_df = tmp_df
857-
else:
858-
self.parameter_df = pd.concat([self.parameter_df, tmp_df])
857+
self.parameter_df = (
858+
pd.concat([self.parameter_df, tmp_df])
859+
if self.parameter_df is not None
860+
else tmp_df
861+
)
859862

860863
def add_measurement(
861864
self,
@@ -896,10 +899,11 @@ def add_measurement(
896899
record[PREEQUILIBRATION_CONDITION_ID] = [preeq_cond_id]
897900

898901
tmp_df = pd.DataFrame(record)
899-
if self.measurement_df is None:
900-
self.measurement_df = tmp_df
901-
else:
902-
self.measurement_df = pd.concat([self.measurement_df, tmp_df])
902+
self.measurement_df = (
903+
pd.concat([self.measurement_df, tmp_df])
904+
if self.measurement_df is not None
905+
else tmp_df
906+
)
903907

904908
def add_mapping(self, petab_id: str, model_id: str):
905909
"""Add a mapping table entry to the problem.
@@ -913,7 +917,8 @@ def add_mapping(self, petab_id: str, model_id: str):
913917
MODEL_ENTITY_ID: [model_id],
914918
}
915919
tmp_df = pd.DataFrame(record).set_index([PETAB_ENTITY_ID])
916-
if self.mapping_df is None:
917-
self.mapping_df = tmp_df
918-
else:
919-
self.mapping_df = pd.concat([self.mapping_df, tmp_df])
920+
self.mapping_df = (
921+
pd.concat([self.mapping_df, tmp_df])
922+
if self.mapping_df is not None
923+
else tmp_df
924+
)

0 commit comments

Comments
 (0)