3434from zea .data .datasets import Dataset , H5FileHandleCache , count_samples_per_directory
3535from zea .data .file import File
3636from zea .data .layers import Resizer
37- from zea .data .utils import json_dumps
3837from zea .utils import map_negative_indices
3938
4039DEFAULT_NORMALIZATION_RANGE = (0 , 1 )
@@ -84,12 +83,12 @@ def generate_h5_indices(
8483 (
8584 "/folder/path_to_file.hdf5",
8685 "data/image",
87- (range(0 , 1), slice(None, 256, None), slice(None, 256, None)),
86+ (slice(0, 1 , 1), slice(None, 256, None), slice(None, 256, None)),
8887 ),
8988 (
9089 "/folder/path_to_file.hdf5",
9190 "data/image",
92- (range (1, 2), slice(None, 256, None), slice(None, 256, None)),
91+ (slice (1, 2, 1 ), slice(None, 256, None), slice(None, 256, None)),
9392 ),
9493 ...,
9594 ]
@@ -136,7 +135,7 @@ def axis_indices_files():
136135 # Optionally limit frames to load from each file
137136 n_frames_in_file = min (n_frames_in_file , limit_n_frames )
138137 indices = [
139- list ( range ( i , i + block_size , frame_index_stride ) )
138+ slice ( i , i + block_size , frame_index_stride )
140139 for i in range (0 , n_frames_in_file - block_size + 1 , block_step_size )
141140 ]
142141 yield [indices ]
@@ -293,18 +292,16 @@ def __getitem__(self, index: int):
293292 return self ._data_cache [index ]
294293
295294 file_name , key , indices = self .indices [index ]
296- file_cache = self ._get_cache ()
297- file = file_cache .get_file (file_name )
295+ file_handle_cache = self ._get_file_handle_cache ()
296+ file = file_handle_cache .get_file (file_name )
298297 image = self ._load (file , key , indices )
299298
300299 if self .return_filename :
301- file_data = json_dumps (
302- {
303- "fullpath" : file .filename ,
304- "filename" : Path (file_name ).stem ,
305- "indices" : indices ,
306- }
307- )
300+ file_data = {
301+ "fullpath" : file .filename ,
302+ "filename" : Path (file_name ).stem ,
303+ "indices" : indices ,
304+ }
308305 result = (image , file_data )
309306 else :
310307 result = image
@@ -321,7 +318,7 @@ def __repr__(self) -> str:
321318
322319 # -- internals -------------------------------------------------------------
323320
324- def _get_cache (self ) -> H5FileHandleCache :
321+ def _get_file_handle_cache (self ) -> H5FileHandleCache :
325322 """Return the file-handle cache for the current thread."""
326323 if not hasattr (self ._local , "cache" ):
327324 self ._local .cache = H5FileHandleCache ()
@@ -335,14 +332,10 @@ def _load(self, file: File, key: str, indices):
335332 images = file .load_data (key , indices )
336333 except (OSError , IOError ):
337334 # Invalidate cache entry and retry once
338- cache = self ._get_cache ()
339335 fname = file .filename
340- cache ._file_handle_cache .pop (fname , None )
341- try :
342- file .close ()
343- except Exception :
344- pass
345- file = cache .get_file (fname )
336+ file_handle_cache = self ._get_file_handle_cache ()
337+ file_handle_cache .pop (fname )
338+ file = file_handle_cache .get_file (fname )
346339 images = file .load_data (key , indices )
347340
348341 if self .insert_frame_axis :
0 commit comments