44import logging
55import os
66import re
7- import typing
87import warnings
98from abc import abstractmethod
109from pathlib import Path
11- from typing import Union , cast
10+ from typing import cast
1211
1312import cv2
1413import numpy as np
@@ -71,7 +70,7 @@ def load_meta(self):
7170 @abstractmethod
7271 def interpolate (
7372 self , times : np .ndarray , return_valid : bool = False
74- ) -> Union [ tuple [np .ndarray , np .ndarray ], np .ndarray ] :
73+ ) -> tuple [np .ndarray , np .ndarray ] | np .ndarray :
7574 """Map an array of time points to interpolated data values."""
7675 ...
7776
@@ -85,7 +84,7 @@ def __exit__(self, *exc):
8584 self .close ()
8685
8786 @staticmethod
88- def create (root_folder : str , cache_data : bool = False , ** kwargs ) -> " Interpolator" :
87+ def create (root_folder : str , cache_data : bool = False , ** kwargs ) -> Interpolator :
8988 """Factory method to create the appropriate interpolator for a modality.
9089
9190 Reads the ``meta.yml`` file in the folder to determine the modality type
@@ -110,7 +109,7 @@ def create(root_folder: str, cache_data: bool = False, **kwargs) -> "Interpolato
110109 ValueError
111110 If the modality type is not supported.
112111 """
113- with open (Path (root_folder ) / "meta.yml" , "r" ) as file :
112+ with open (Path (root_folder ) / "meta.yml" ) as file :
114113 meta_data = yaml .safe_load (file )
115114 modality = meta_data .get ("modality" )
116115
@@ -197,7 +196,7 @@ def __init__(
197196 interpolation_mode : str = "nearest_neighbor" ,
198197 normalize : bool = False ,
199198 normalize_subtract_mean : bool = False ,
200- normalize_std_threshold : typing . Optional [ float ] = None , # or 0.01
199+ normalize_std_threshold : float | None = None , # or 0.01
201200 ** kwargs ,
202201 ) -> None :
203202 super ().__init__ (root_folder )
@@ -262,7 +261,7 @@ def normalize_data(self, data):
262261
263262 def interpolate (
264263 self , times : np .ndarray , return_valid : bool = False
265- ) -> Union [ tuple [np .ndarray , np .ndarray ], np .ndarray ] :
264+ ) -> tuple [np .ndarray , np .ndarray ] | np .ndarray :
266265 valid = self .valid_times (times )
267266 valid_times = times [valid ]
268267
@@ -361,7 +360,7 @@ def __init__(
361360 interpolation_mode : str = "nearest_neighbor" ,
362361 normalize : bool = False ,
363362 normalize_subtract_mean : bool = False ,
364- normalize_std_threshold : typing . Optional [ float ] = None , # or 0.01
363+ normalize_std_threshold : float | None = None , # or 0.01
365364 ** kwargs ,
366365 ) -> None :
367366 super ().__init__ (
@@ -385,7 +384,7 @@ def __init__(
385384
386385 def interpolate (
387386 self , times : np .ndarray , return_valid : bool = False
388- ) -> Union [ tuple [np .ndarray , np .ndarray ], np .ndarray ] :
387+ ) -> tuple [np .ndarray , np .ndarray ] | np .ndarray :
389388 valid = self .valid_times (times )
390389 valid_times = times [valid ]
391390
@@ -500,7 +499,7 @@ def __init__(
500499 root_folder : str ,
501500 cache_data : bool = False , # New parameter
502501 rescale : bool = False ,
503- rescale_size : typing . Optional [ tuple [int , int ]] = None ,
502+ rescale_size : tuple [int , int ] | None = None ,
504503 normalize : bool = False ,
505504 ** kwargs ,
506505 ) -> None :
@@ -565,7 +564,7 @@ def is_numbered_yml(file_name):
565564
566565 # Read each YAML file and store under its filename
567566 for meta_file in meta_files :
568- with open (meta_file , "r" ) as file :
567+ with open (meta_file ) as file :
569568 file_base_name = meta_file .stem
570569 yaml_content = yaml .safe_load (file )
571570 all_data [file_base_name ] = yaml_content
@@ -579,7 +578,7 @@ def read_combined_meta(self) -> tuple[list, list]:
579578 logger .info ("Combining metadata files..." )
580579 self ._combine_metadatas ()
581580
582- with open (self .root_folder / "combined_meta.json" , "r" ) as file :
581+ with open (self .root_folder / "combined_meta.json" ) as file :
583582 self .combined_meta = json .load (file )
584583
585584 metadatas = []
@@ -605,7 +604,7 @@ def _parse_trials(self) -> None:
605604
606605 def interpolate (
607606 self , times : np .ndarray , return_valid : bool = False
608- ) -> Union [ tuple [np .ndarray , np .ndarray ], np .ndarray ] :
607+ ) -> tuple [np .ndarray , np .ndarray ] | np .ndarray :
609608 valid = self .valid_times (times )
610609 valid_times = times [valid ]
611610 valid_times += 1e-4 # add small offset to avoid numerical issues
@@ -724,7 +723,7 @@ def __init__(self, root_folder: str, cache_data: bool = False, **kwargs):
724723
725724 def interpolate (
726725 self , times : np .ndarray , return_valid : bool = False
727- ) -> Union [ tuple [np .ndarray , np .ndarray ], np .ndarray ] :
726+ ) -> tuple [np .ndarray , np .ndarray ] | np .ndarray :
728727 valid = self .valid_times (times )
729728 valid_times = times [valid ]
730729
@@ -791,7 +790,7 @@ class ScreenTrial:
791790
792791 def __init__ (
793792 self ,
794- data_file_name : Union [ str , Path ] ,
793+ data_file_name : str | Path ,
795794 meta_data : dict ,
796795 image_size : tuple ,
797796 first_frame_idx : int ,
@@ -811,10 +810,10 @@ def __init__(
811810
812811 @staticmethod
813812 def create (
814- data_file_name : Union [ str , Path ] ,
813+ data_file_name : str | Path ,
815814 meta_data : dict ,
816815 cache_data : bool = False ,
817- ) -> " ScreenTrial" :
816+ ) -> ScreenTrial :
818817 modality = meta_data .get ("modality" )
819818 assert modality is not None
820819 class_name = modality .lower ().capitalize () + "Trial"
@@ -1050,7 +1049,7 @@ def __init__(
10501049
10511050 def interpolate (
10521051 self , times : np .ndarray , return_valid : bool = False
1053- ) -> Union [ tuple [np .ndarray , np .ndarray ], np .ndarray ] :
1052+ ) -> tuple [np .ndarray , np .ndarray ] | np .ndarray :
10541053 # 1. Filter for valid times
10551054 valid = self .valid_times (times )
10561055 valid_times = times [valid ]
0 commit comments