Skip to content

Commit 7093e95

Browse files
saeubpre-commit-ci[bot]SiQube
authored
feat!: Custom patterns for parsing logged metadata in ASC files (#767)
* Return metadata from from_asc() * Parse metadata from ASC files based on custom patterns * Refactor * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Refactor * Fix test coverage * Fix docstrings * Fix docstring --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: David R. Reich <[email protected]>
1 parent 84f1416 commit 7093e95

File tree

6 files changed

+173
-114
lines changed

6 files changed

+173
-114
lines changed

src/pymovements/dataset/dataset_files.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ def load_gaze_file(
377377
column_schema_overrides=definition.filename_format_schema_overrides['gaze'],
378378
)
379379
elif filepath.suffix == '.asc':
380-
gaze_df = from_asc(
380+
gaze_df, _ = from_asc(
381381
filepath,
382382
experiment=definition.experiment,
383383
add_columns=add_columns,

src/pymovements/gaze/io.py

+19-12
Original file line numberDiff line numberDiff line change
@@ -271,44 +271,48 @@ def from_csv(
271271
def from_asc(
272272
file: str | Path,
273273
*,
274-
patterns: str | list | None = 'eyelink',
274+
patterns: str | list[dict[str, Any] | str] | None = 'eyelink',
275+
metadata_patterns: list[dict[str, Any] | str] | None = None,
275276
schema: dict[str, Any] | None = None,
276277
experiment: Experiment | None = None,
277278
add_columns: dict[str, str] | None = None,
278-
column_schema_overrides: dict[str, type] | None = None,
279-
) -> GazeDataFrame:
279+
column_schema_overrides: dict[str, Any] | None = None,
280+
) -> tuple[GazeDataFrame, dict[str, Any]]:
280281
"""Initialize a :py:class:`pymovements.gaze.gaze_dataframe.GazeDataFrame`.
281282
282283
Parameters
283284
----------
284285
file: str | Path
285286
Path of IPC/feather file.
286-
patterns: str | list | None
287-
list of patterns to match for additional columns or a key identifier of eye tracker specific
287+
patterns: str | list[dict[str, Any] | str] | None
288+
List of patterns to match for additional columns or a key identifier of eye tracker specific
288289
default patterns. Supported values are: eyelink. (default: 'eyelink')
290+
metadata_patterns: list[dict[str, Any] | str] | None
291+
List of patterns to match for extracting metadata from custom logged messages.
292+
(default: None)
289293
schema: dict[str, Any] | None
290294
Dictionary to optionally specify types of columns parsed by patterns. (default: None)
291295
experiment: Experiment | None
292296
The experiment definition. (default: None)
293297
add_columns: dict[str, str] | None
294298
Dictionary containing columns to add to loaded data frame.
295299
(default: None)
296-
column_schema_overrides: dict[str, type] | None
300+
column_schema_overrides: dict[str, Any] | None
297301
Dictionary containing types for columns.
298302
(default: None)
299303
300304
Returns
301305
-------
302-
GazeDataFrame
303-
The gaze data frame read from the asc file.
306+
tuple[GazeDataFrame, dict[str, Any]]
307+
The gaze data frame and a metadata dictionary read from the asc file.
304308
305309
Examples
306310
--------
307311
Let's assume we have an EyeLink asc file stored at `tests/files/eyelink_monocular_example.asc`.
308312
We can then load the data into a ``GazeDataFrame``:
309313
310314
>>> from pymovements.gaze.io import from_asc
311-
>>> gaze = from_asc(file='tests/files/eyelink_monocular_example.asc', patterns='eyelink')
315+
>>> gaze, metadata = from_asc(file='tests/files/eyelink_monocular_example.asc')
312316
>>> gaze.frame
313317
shape: (16, 3)
314318
┌─────────┬───────┬────────────────┐
@@ -328,7 +332,8 @@ def from_asc(
328332
│ 2339290 ┆ 618.0 ┆ [637.6, 531.4] │
329333
│ 2339291 ┆ 618.0 ┆ [637.3, 531.2] │
330334
└─────────┴───────┴────────────────┘
331-
335+
>>> metadata['sampling_rate']
336+
1000.0
332337
"""
333338
if isinstance(patterns, str):
334339
if patterns == 'eyelink':
@@ -338,7 +343,9 @@ def from_asc(
338343
raise ValueError(f"unknown pattern key '{patterns}'. Supported keys are: eyelink")
339344

340345
# Read data.
341-
gaze_data, _ = parse_eyelink(file, patterns=patterns, schema=schema)
346+
gaze_data, metadata = parse_eyelink(
347+
file, patterns=patterns, schema=schema, metadata_patterns=metadata_patterns,
348+
)
342349

343350
if add_columns is not None:
344351
gaze_data = gaze_data.with_columns([
@@ -361,7 +368,7 @@ def from_asc(
361368
time_unit='ms',
362369
pixel_columns=['x_pix', 'y_pix'],
363370
)
364-
return gaze_df
371+
return gaze_df, metadata
365372

366373

367374
def from_ipc(

0 commit comments

Comments
 (0)