Skip to content

Commit 3a52039

Browse files
authored
Rename reader argument as path (#65)
To clarify intent, as file is often reserved for open file handles.
1 parent 815c707 commit 3a52039

File tree

2 files changed

+46
-46
lines changed

2 files changed

+46
-46
lines changed

src/swc/aeon/io/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,11 @@ def __init__(self, pattern: str, columns: SequenceNotStr[str], extension: str):
207207
self.columns = columns
208208
self.extension = extension
209209

210-
def read(self, file: Path) -> pd.DataFrame:
210+
def read(self, path: Path) -> pd.DataFrame:
211211
"""Reads data from the specified file.
212212
213213
Args:
214-
file: Path to the data file.
214+
path: Path to the data file.
215215
216216
Returns:
217217
A DataFrame representing the data extracted from the specified file.

src/swc/aeon/io/reader.py

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ def __init__(self, pattern: str, columns: SequenceNotStr[str], extension="bin"):
2222
"""Initialize the object."""
2323
super().__init__(pattern, columns, extension)
2424

25-
def read(self, file: Path) -> pd.DataFrame:
25+
def read(self, path: Path) -> pd.DataFrame:
2626
"""Reads data from the specified Harp binary file.
2727
2828
Args:
29-
file: Path to the Harp binary file.
29+
path: Path to the Harp binary file.
3030
3131
Returns:
3232
A DataFrame representing the data extracted from the Harp binary file.
3333
"""
34-
return harp.read(file, columns=self.columns)
34+
return harp.read(path, columns=self.columns)
3535

3636

3737
class Chunk(Reader):
@@ -48,17 +48,17 @@ def __init__(
4848
raise ValueError("reader must be specified if pattern or extension are None.")
4949
super().__init__(pattern, columns=("path", "epoch"), extension=extension)
5050

51-
def read(self, file: Path) -> pd.DataFrame:
51+
def read(self, path: Path) -> pd.DataFrame:
5252
"""Returns path and epoch information for the chunk associated with the specified file.
5353
5454
Args:
55-
file: Path to the data file.
55+
path: Path to the data file.
5656
5757
Returns:
5858
A DataFrame representing the path and epoch information for the specified file.
5959
"""
60-
epoch, chunk = chunk_key(file)
61-
data = {"path": file, "epoch": epoch}
60+
epoch, chunk = chunk_key(path)
61+
data = {"path": path, "epoch": epoch}
6262
return pd.DataFrame(data, index=pd.Series(chunk), columns=self.columns)
6363

6464

@@ -69,19 +69,19 @@ def __init__(self, pattern="Metadata"):
6969
"""Initialize the object with the specified pattern."""
7070
super().__init__(pattern, columns=("workflow", "commit", "metadata"), extension="yml")
7171

72-
def read(self, file: Path) -> pd.DataFrame:
72+
def read(self, path: Path) -> pd.DataFrame:
7373
"""Returns epoch metadata stored in the specified file.
7474
7575
Args:
76-
file: Path to the data file.
76+
path: Path to the data file.
7777
7878
Returns:
7979
A DataFrame representing the epoch metadata stored in the specified file.
8080
"""
81-
epoch_str = file.parts[-2]
81+
epoch_str = path.parts[-2]
8282
date_str, time_str = epoch_str.split("T")
8383
time = datetime.datetime.fromisoformat(date_str + "T" + time_str.replace("-", ":"))
84-
with open(file) as fp:
84+
with open(path) as fp:
8585
metadata = json.load(fp)
8686
workflow = metadata.pop("Workflow")
8787
commit = metadata.pop("Commit", pd.NA)
@@ -103,25 +103,25 @@ def __init__(
103103
self.dtype = dtype
104104
self._names = tuple(columns)
105105

106-
def read(self, file: Path) -> pd.DataFrame:
106+
def read(self, path: Path) -> pd.DataFrame:
107107
"""Reads data from the specified CSV text file.
108108
109109
If the file is non-empty, the first column is assumed to contain the Aeon timestamp
110110
and is set as the index of the DataFrame. If the file is empty, pandas defaults to
111111
using pandas.RangeIndex as the index.
112112
113113
Args:
114-
file: Path to the CSV text file.
114+
path: Path to the CSV text file.
115115
116116
Returns:
117117
A DataFrame representing the data extracted from the CSV text file.
118118
"""
119119
return pd.read_csv(
120-
file,
120+
path,
121121
header=0,
122122
names=self._names,
123123
dtype=self.dtype,
124-
index_col=0 if file.stat().st_size else None,
124+
index_col=0 if path.stat().st_size else None,
125125
)
126126

127127

@@ -150,19 +150,19 @@ def __init__(
150150
self.columns = columns
151151
self.root_key = root_key
152152

153-
def read(self, file: Path) -> pd.DataFrame:
153+
def read(self, path: Path) -> pd.DataFrame:
154154
"""Reads data from the specified jsonl file.
155155
156156
Args:
157-
file: Path to the jsonl file to read. The file must contain a
157+
path: Path to the jsonl file to read. The file must contain a
158158
"seconds" key that stores the Aeon timestamp, and the `root_key` must
159159
contain a dictionary with keys corresponding to the specified `columns`.
160160
161161
Returns:
162162
A DataFrame with "seconds" as the index, other keys as columns,
163163
and the specified columns extracted from the `root_key` dictionary (if any).
164164
"""
165-
with open(file) as f:
165+
with open(path) as f:
166166
df = pd.read_json(f, lines=True)
167167
df.set_index("seconds", inplace=True)
168168
for column in self.columns:
@@ -270,18 +270,18 @@ def __init__(self, pattern: str, value: int, tag: str):
270270
self.value = value
271271
self.tag = tag
272272

273-
def read(self, file: Path) -> pd.DataFrame:
273+
def read(self, path: Path) -> pd.DataFrame:
274274
"""Reads a specific event code from digital data.
275275
276276
Each data value is matched against the unique event identifier.
277277
278278
Args:
279-
file: Path to the Harp binary file.
279+
path: Path to the Harp binary file.
280280
281281
Returns:
282282
A DataFrame representing the event data extracted from the Harp binary file.
283283
"""
284-
data = super().read(file)
284+
data = super().read(path)
285285
data = data[(data.event & self.value) == self.value]
286286
data["event"] = self.tag
287287
return data
@@ -303,18 +303,18 @@ def __init__(self, pattern: str, mask: int, columns: SequenceNotStr[str]):
303303
super().__init__(pattern, columns)
304304
self.mask = mask
305305

306-
def read(self, file: Path) -> pd.DataFrame:
306+
def read(self, path: Path) -> pd.DataFrame:
307307
"""Reads a specific event code from digital data.
308308
309309
Each data value is checked against the specified bitmask.
310310
311311
Args:
312-
file: Path to the Harp binary file.
312+
path: Path to the Harp binary file.
313313
314314
Returns:
315315
A DataFrame representing the bitmask data extracted from the Harp binary file.
316316
"""
317-
data = super().read(file)
317+
data = super().read(path)
318318
state = data[self.columns] & self.mask
319319
return state[(state.diff() != 0).values] != 0
320320

@@ -336,19 +336,19 @@ def __init__(self, pattern: str):
336336
super().__init__(pattern, columns=("hw_counter", "hw_timestamp", "_frame", "_path", "_epoch"))
337337
self._rawcolumns = ("time",) + tuple(self.columns[0:2])
338338

339-
def read(self, file: Path) -> pd.DataFrame:
339+
def read(self, path: Path) -> pd.DataFrame:
340340
"""Reads video metadata from the specified file.
341341
342342
Args:
343-
file: Path to the video metadata CSV file.
343+
path: Path to the video metadata CSV file.
344344
345345
Returns:
346346
A DataFrame containing the video metadata.
347347
"""
348-
data = pd.read_csv(file, header=0, names=self._rawcolumns)
348+
data = pd.read_csv(path, header=0, names=self._rawcolumns)
349349
data["_frame"] = data.index
350-
data["_path"] = os.path.splitext(file)[0] + ".avi"
351-
data["_epoch"] = file.parts[-3]
350+
data["_path"] = os.path.splitext(path)[0] + ".avi"
351+
data["_epoch"] = path.parts[-3]
352352
data.set_index("time", inplace=True)
353353
return data
354354

@@ -379,22 +379,22 @@ def __init__(self, pattern: str, model_root: str | None = None):
379379
self._model_root = model_root
380380
self._pattern_offset = pattern.rfind("_") + 1
381381

382-
def read(self, file: Path, include_model: bool = False) -> pd.DataFrame:
382+
def read(self, path: Path, include_model: bool = False) -> pd.DataFrame:
383383
"""Reads data from the Harp-binarized tracking file.
384384
385385
Args:
386-
file: Path to the Harp binary file.
386+
path: Path to the Harp binary file.
387387
include_model: Specifies whether to include the path to the pose tracking model.
388388
389389
Returns:
390390
A DataFrame representing the data extracted from the Harp binary file.
391391
"""
392392
# Get config file from `file`, then bodyparts from config file.
393-
model_dir = Path(file.stem[self._pattern_offset :].replace("_", "/")).parent
393+
model_dir = Path(path.stem[self._pattern_offset :].replace("_", "/")).parent
394394

395395
# Check if model directory exists in local or shared directories.
396396
# Local directory is prioritized over shared directory.
397-
local_config_file_dir = file.parent / model_dir
397+
local_config_file_dir = path.parent / model_dir
398398
shared_config_file_dir = Path(self._model_root) / model_dir if self._model_root else None
399399
if local_config_file_dir.exists():
400400
config_file_dir = local_config_file_dir
@@ -419,15 +419,15 @@ def read(self, file: Path, include_model: bool = False) -> pd.DataFrame:
419419
for part in parts:
420420
columns.extend([f"{part}_x", f"{part}_y", f"{part}_likelihood"])
421421
self.columns = columns
422-
data = super().read(file)
422+
data = super().read(path)
423423
except ValueError: # column mismatch; Bonsai.Sleap0.3
424424
bonsai_sleap_v = BONSAI_SLEAP_V3
425425
columns = ["identity"]
426426
columns.extend([f"{identity}_likelihood" for identity in identities])
427427
for part in parts:
428428
columns.extend([f"{part}_x", f"{part}_y", f"{part}_likelihood"])
429429
self.columns = columns
430-
data = super().read(file)
430+
data = super().read(path)
431431

432432
# combine all identity_likelihood cols into a single column
433433
if bonsai_sleap_v == BONSAI_SLEAP_V3:
@@ -461,12 +461,12 @@ def read(self, file: Path, include_model: bool = False) -> pd.DataFrame:
461461
return data
462462

463463
@staticmethod
464-
def get_class_names(config_file: Path) -> list[str]:
464+
def get_class_names(config_file_path: Path) -> list[str]:
465465
"""Returns a list of classes from a model's config file."""
466-
with open(config_file) as f:
466+
with open(config_file_path) as f:
467467
config = json.load(f)
468-
if config_file.stem != "confmap_config": # SLEAP
469-
raise ValueError(f"The model config file '{config_file}' is not supported.")
468+
if config_file_path.stem != "confmap_config": # SLEAP
469+
raise ValueError(f"The model config file '{config_file_path}' is not supported.")
470470

471471
try:
472472
heads = config["model"]["heads"]
@@ -476,21 +476,21 @@ def get_class_names(config_file: Path) -> list[str]:
476476
else:
477477
return list[str]()
478478
except KeyError as err:
479-
raise KeyError(f"Cannot find class_vectors in {config_file}.") from err
479+
raise KeyError(f"Cannot find class_vectors in {config_file_path}.") from err
480480

481481
@staticmethod
482-
def get_bodyparts(config_file: Path) -> list[str]:
482+
def get_bodyparts(config_file_path: Path) -> list[str]:
483483
"""Returns a list of bodyparts from a model's config file."""
484484
parts = []
485-
with open(config_file) as f:
485+
with open(config_file_path) as f:
486486
config = json.load(f)
487-
if config_file.stem == "confmap_config": # SLEAP
487+
if config_file_path.stem == "confmap_config": # SLEAP
488488
try:
489489
heads = config["model"]["heads"]
490490
parts = [f"anchor_{Pose._find_nested_key(heads, 'anchor_part')}"]
491491
parts += Pose._find_nested_key(heads, "part_names")
492492
except KeyError as err:
493-
raise KeyError(f"Cannot find anchor or bodyparts in {config_file}.") from err
493+
raise KeyError(f"Cannot find anchor or bodyparts in {config_file_path}.") from err
494494
return parts
495495

496496
@staticmethod

0 commit comments

Comments
 (0)