From e7f5c3476f05a4b235463d5dcb32b2d40f4247a0 Mon Sep 17 00:00:00 2001 From: Qian Chu <97355086+qian-chu@users.noreply.github.com> Date: Tue, 31 Dec 2024 16:04:23 +0100 Subject: [PATCH] clean up prev use of `Union` --- pyneon/dataset.py | 5 ++--- pyneon/export/export_bids.py | 6 +++--- pyneon/preprocess/preprocess.py | 8 ++++---- pyneon/recording.py | 16 ++++++++-------- pyneon/stream.py | 4 ++-- pyneon/utils/utils.py | 1 - pyneon/video/mapping.py | 2 +- pyneon/video/video.py | 7 +++---- pyneon/vis/vis.py | 5 ++--- 9 files changed, 25 insertions(+), 29 deletions(-) diff --git a/pyneon/dataset.py b/pyneon/dataset.py index 81a6413..7414694 100644 --- a/pyneon/dataset.py +++ b/pyneon/dataset.py @@ -1,5 +1,4 @@ from pathlib import Path -from typing import Union import pandas as pd @@ -46,7 +45,7 @@ class NeonDataset: DataFrame containing the sections of the dataset. """ - def __init__(self, dataset_dir: Union[str, Path]): + def __init__(self, dataset_dir: str | Path): dataset_dir = Path(dataset_dir) if not dataset_dir.is_dir(): raise FileNotFoundError(f"Directory not found: {dataset_dir}") @@ -91,7 +90,7 @@ def __getitem__(self, index): """Get a NeonRecording by index.""" return self.recordings[index] - def load_enrichment(self, enrichment_dir: Union[str, Path]): + def load_enrichment(self, enrichment_dir: str | Path): """ Load enrichment information from an enrichment directory. The directory must contain an enrichment_info.txt file. Enrichment data will be parsed for each diff --git a/pyneon/export/export_bids.py b/pyneon/export/export_bids.py index f5da279..a2b7b7f 100644 --- a/pyneon/export/export_bids.py +++ b/pyneon/export/export_bids.py @@ -3,7 +3,7 @@ import json import datetime import re -from typing import Union, TYPE_CHECKING +from typing import TYPE_CHECKING from ._bids_parameters import MOTION_META_DEFAULT @@ -13,7 +13,7 @@ def export_motion_bids( rec: "NeonRecording", - motion_dir: Union[str, Path], + motion_dir: str | Path, prefix: str = "", extra_metadata: dict = {}, ): @@ -127,7 +127,7 @@ def export_motion_bids( scans.to_csv(scans_path, sep="\t", index=False) -def export_eye_bids(rec: "NeonRecording", output_dir: Union[str, Path]): +def export_eye_bids(rec: "NeonRecording", output_dir: str | Path): gaze = rec.gaze eye_states = rec.eye_states output_dir = Path(output_dir) diff --git a/pyneon/preprocess/preprocess.py b/pyneon/preprocess/preprocess.py index 2784ea0..763c201 100644 --- a/pyneon/preprocess/preprocess.py +++ b/pyneon/preprocess/preprocess.py @@ -1,7 +1,7 @@ import pandas as pd import numpy as np -from typing import TYPE_CHECKING, Union, Optional +from typing import TYPE_CHECKING, Optional from scipy.interpolate import interp1d from numbers import Number @@ -158,8 +158,8 @@ def window_average( def concat_streams( rec: "NeonRecording", - stream_names: Union[str, list[str]] = "all", - sampling_freq: Union[Number, str] = "min", + stream_names: str | list[str] = "all", + sampling_freq: Number | str = "min", interp_float_kind: str = "cubic", interp_other_kind: str = "nearest", inplace: bool = False, @@ -341,7 +341,7 @@ def concat_streams( def concat_events( rec: "NeonRecording", - event_names: Union[str, list[str]], + event_names: str | list[str], ) -> pd.DataFrame: """ Concatenate different events. All columns in the selected event type will be diff --git a/pyneon/recording.py b/pyneon/recording.py index d5c7cb4..5861246 100644 --- a/pyneon/recording.py +++ b/pyneon/recording.py @@ -1,5 +1,5 @@ from pathlib import Path -from typing import Union, Literal, Optional +from typing import Literal, Optional import pandas as pd import json from datetime import datetime @@ -80,7 +80,7 @@ class NeonRecording: ``filename`` (str), and ``path`` (Path). """ - def __init__(self, recording_dir: Union[str, Path]): + def __init__(self, recording_dir: str | Path): recording_dir = Path(recording_dir) if not recording_dir.is_dir(): raise FileNotFoundError(f"Directory not found: {recording_dir}") @@ -271,8 +271,8 @@ def video(self) -> Optional[NeonVideo]: def concat_streams( self, - stream_names: Union[str, list[str]], - sampling_freq: Union[Number, str] = "min", + stream_names: str | list[str], + sampling_freq: Number | str = "min", interp_float_kind: str = "cubic", interp_other_kind: str = "nearest", inplace: bool = False, @@ -321,7 +321,7 @@ def concat_streams( ) return CustomStream(new_data) - def concat_events(self, event_names: Union[str, list[str]]) -> pd.DataFrame: + def concat_events(self, event_names: str | list[str]) -> pd.DataFrame: """ Concatenate different events. All columns in the selected event type will be present in the final DataFrame. An additional ``"type"`` column denotes the event @@ -468,7 +468,7 @@ def plot_scanpath_on_video( line_thickness: int = 2, max_fixations: int = 10, show_video: bool = False, - video_output_path: Union[Path, str] = "scanpath.mp4", + video_output_path: Path | str = "scanpath.mp4", ) -> None: """ Plot scanpath on top of the video frames. The resulting video can be displayed and/or saved. @@ -506,7 +506,7 @@ def plot_scanpath_on_video( def export_motion_bids( self, - motion_dir: Union[str, Path], + motion_dir: str | Path, prefix: str = "", extra_metadata: dict = {}, ): @@ -541,7 +541,7 @@ def export_motion_bids( def export_eye_bids( self, - output_dir: Union[str, Path], + output_dir: str | Path, prefix: str = "", extra_metadata: dict = {}, ): diff --git a/pyneon/stream.py b/pyneon/stream.py index 37f7fcb..136e34e 100644 --- a/pyneon/stream.py +++ b/pyneon/stream.py @@ -2,7 +2,7 @@ import pandas as pd import numpy as np from numbers import Number -from typing import Union, Literal, Optional +from typing import Literal, Optional import copy from .tabular import NeonTabular @@ -93,7 +93,7 @@ def columns(self): def dtypes(self): return self.data.dtypes - def time_to_ts(self, time: Union[Number, np.ndarray]) -> np.ndarray: + def time_to_ts(self, time: Number | np.ndarray) -> np.ndarray: """Convert relative time(s) in seconds to closest timestamp(s) in nanoseconds.""" time = np.array([time]) return np.array([self.ts[np.absolute(self.times - t).argmin()] for t in time]) diff --git a/pyneon/utils/utils.py b/pyneon/utils/utils.py index 4d1844b..a6bb816 100644 --- a/pyneon/utils/utils.py +++ b/pyneon/utils/utils.py @@ -1,3 +1,2 @@ import pandas as pd -from typing import Union from numbers import Number diff --git a/pyneon/video/mapping.py b/pyneon/video/mapping.py index bd50b0d..3a9bde7 100644 --- a/pyneon/video/mapping.py +++ b/pyneon/video/mapping.py @@ -2,7 +2,7 @@ import numpy as np import cv2 from tqdm import tqdm -from typing import TYPE_CHECKING, Union, Optional +from typing import TYPE_CHECKING, Optional if TYPE_CHECKING: diff --git a/pyneon/video/video.py b/pyneon/video/video.py index eee010b..07b0583 100644 --- a/pyneon/video/video.py +++ b/pyneon/video/video.py @@ -2,10 +2,9 @@ import numpy as np import pandas as pd from pathlib import Path -from typing import Union +from typing import Optional import matplotlib.pyplot as plt import json -from typing import Union from ..vis import plot_frame, plot_scanpath_on_video @@ -65,7 +64,7 @@ def __len__(self) -> int: def plot_frame( self, index: int = 0, - ax: Union[plt.Axes, None] = None, + ax: Optional[plt.Axes] = None, auto_title: bool = True, show: bool = True, ): @@ -100,7 +99,7 @@ def plot_scanpath_on_video( line_thickness: int = 2, max_fixations: int = 10, show_video: bool = False, - video_output_path: Union[Path, str] = "scanpath.mp4", + video_output_path: Path | str = "scanpath.mp4", ) -> None: """ Plot scanpath on top of the video frames. The resulting video can be displayed and/or saved. diff --git a/pyneon/vis/vis.py b/pyneon/vis/vis.py index dda7f08..9ac36e5 100644 --- a/pyneon/vis/vis.py +++ b/pyneon/vis/vis.py @@ -4,8 +4,7 @@ import cv2 import matplotlib.pyplot as plt from pathlib import Path -from numbers import Number -from typing import TYPE_CHECKING, Union, Literal, Optional +from typing import TYPE_CHECKING, Literal, Optional from tqdm import tqdm if TYPE_CHECKING: @@ -185,7 +184,7 @@ def plot_scanpath_on_video( line_thickness: int = 2, max_fixations: int = 10, show_video: bool = False, - video_output_path: Optional[Union[Path, str]] = "scanpath.mp4", + video_output_path: Optional[Path | str] = "scanpath.mp4", ) -> None: """ Plot scanpath on top of the video frames. The resulting video can be displayed and/or saved.