1515from .core_tools import is_path_remote
1616
1717
18+ def anononymous_zarr_open (folder_path : str | Path , mode : str = "r" , storage_options : dict | None = None ):
19+ if is_path_remote (str (folder_path )) and storage_options is None :
20+ try :
21+ root = zarr .open (str (folder_path ), mode = "r" , storage_options = storage_options )
22+ except Exception as e :
23+ storage_options = {"anon" : True }
24+ root = zarr .open (str (folder_path ), mode = "r" , storage_options = storage_options )
25+ else :
26+ root = zarr .open (str (folder_path ), mode = "r" , storage_options = storage_options )
27+ return root
28+
29+
1830class ZarrRecordingExtractor (BaseRecording ):
1931 """
2032 RecordingExtractor for a zarr format
@@ -40,14 +52,7 @@ def __init__(self, folder_path: Path | str, storage_options: dict | None = None)
4052
4153 folder_path , folder_path_kwarg = resolve_zarr_path (folder_path )
4254
43- if is_path_remote (str (folder_path )) and storage_options is None :
44- try :
45- self ._root = zarr .open (str (folder_path ), mode = "r" , storage_options = storage_options )
46- except Exception as e :
47- storage_options = {"anon" : True }
48- self ._root = zarr .open (str (folder_path ), mode = "r" , storage_options = storage_options )
49- else :
50- self ._root = zarr .open (str (folder_path ), mode = "r" , storage_options = storage_options )
55+ self ._root = anononymous_zarr_open (folder_path , mode = "r" , storage_options = storage_options )
5156
5257 sampling_frequency = self ._root .attrs .get ("sampling_frequency" , None )
5358 num_segments = self ._root .attrs .get ("num_segments" , None )
@@ -93,7 +98,10 @@ def __init__(self, folder_path: Path | str, storage_options: dict | None = None)
9398
9499 nbytes_segment = self ._root [trace_name ].nbytes
95100 nbytes_stored_segment = self ._root [trace_name ].nbytes_stored
96- cr_by_segment [segment_index ] = nbytes_segment / nbytes_stored_segment
101+ if nbytes_stored_segment > 0 :
102+ cr_by_segment [segment_index ] = nbytes_segment / nbytes_stored_segment
103+ else :
104+ cr_by_segment [segment_index ] = np .nan
97105
98106 total_nbytes += nbytes_segment
99107 total_nbytes_stored += nbytes_stored_segment
@@ -117,7 +125,10 @@ def __init__(self, folder_path: Path | str, storage_options: dict | None = None)
117125 if annotations is not None :
118126 self .annotate (** annotations )
119127 # annotate compression ratios
120- cr = total_nbytes / total_nbytes_stored
128+ if total_nbytes_stored > 0 :
129+ cr = total_nbytes / total_nbytes_stored
130+ else :
131+ cr = np .nan
121132 self .annotate (compression_ratio = cr , compression_ratio_segments = cr_by_segment )
122133
123134 self ._kwargs = {"folder_path" : folder_path_kwarg , "storage_options" : storage_options }
@@ -181,14 +192,7 @@ def __init__(self, folder_path: Path | str, storage_options: dict | None = None,
181192
182193 folder_path , folder_path_kwarg = resolve_zarr_path (folder_path )
183194
184- if is_path_remote (str (folder_path )) and storage_options is None :
185- try :
186- zarr_root = zarr .open (str (folder_path ), mode = "r" , storage_options = storage_options )
187- except Exception as e :
188- storage_options = {"anon" : True }
189- zarr_root = zarr .open (str (folder_path ), mode = "r" , storage_options = storage_options )
190- else :
191- zarr_root = zarr .open (str (folder_path ), mode = "r" , storage_options = storage_options )
195+ zarr_root = anononymous_zarr_open (folder_path , mode = "r" , storage_options = storage_options )
192196
193197 if zarr_group is None :
194198 self ._root = zarr_root
@@ -267,7 +271,7 @@ def read_zarr(
267271 """
268272 # TODO @alessio : we should have something more explicit in our zarr format to tell which object it is.
269273 # for the futur SortingAnalyzer we will have this 2 fields!!!
270- root = zarr . open ( str ( folder_path ) , mode = "r" , storage_options = storage_options )
274+ root = anononymous_zarr_open ( folder_path , mode = "r" , storage_options = storage_options )
271275 if "channel_ids" in root .keys ():
272276 return read_zarr_recording (folder_path , storage_options = storage_options )
273277 elif "unit_ids" in root .keys ():
0 commit comments