Skip to content

Commit

Permalink
clean up prev use of Union
Browse files Browse the repository at this point in the history
  • Loading branch information
qian-chu committed Dec 31, 2024
1 parent 913eb42 commit e7f5c34
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 29 deletions.
5 changes: 2 additions & 3 deletions pyneon/dataset.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from pathlib import Path
from typing import Union

import pandas as pd

Expand Down Expand Up @@ -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}")
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pyneon/export/export_bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -13,7 +13,7 @@

def export_motion_bids(
rec: "NeonRecording",
motion_dir: Union[str, Path],
motion_dir: str | Path,
prefix: str = "",
extra_metadata: dict = {},
):
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions pyneon/preprocess/preprocess.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions pyneon/recording.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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}")
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 = {},
):
Expand Down Expand Up @@ -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 = {},
):
Expand Down
4 changes: 2 additions & 2 deletions pyneon/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
Expand Down
1 change: 0 additions & 1 deletion pyneon/utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
import pandas as pd
from typing import Union
from numbers import Number
2 changes: 1 addition & 1 deletion pyneon/video/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 3 additions & 4 deletions pyneon/video/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
):
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 2 additions & 3 deletions pyneon/vis/vis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit e7f5c34

Please sign in to comment.