File tree Expand file tree Collapse file tree 3 files changed +11
-10
lines changed
Expand file tree Collapse file tree 3 files changed +11
-10
lines changed Original file line number Diff line number Diff line change 1616]
1717
1818from abc import abstractmethod
19+ import atexit
1920from collections import OrderedDict
2021from enum import Enum
2122import numpy as np
@@ -229,6 +230,8 @@ def __init__(
229230
230231 super ().__init__ (config = config , parent = parent , ** kwargs )
231232
233+ # Register the destructor to close all open files properly
234+ atexit .register (self .__destructor )
232235 # Initialize the Table data quality query
233236 self .quality_query = TableQualityQuery (parent = self )
234237
@@ -936,10 +939,13 @@ def generate_stereo_batch(self, batch_indices) -> Table:
936939 batch .sort (["obs_id" , "event_id" , "tel_type_id" , "tel_id" ])
937940 return batch
938941
939- def close_files (self ):
940- """Close all open HDF5 files."""
941- for file in self .files .values ():
942- file .close ()
942+ def __destructor (self ):
943+ """Destructor to ensure all opened HDF5 files are properly closed."""
944+ if hasattr (self , "files" ): # Ensure self.files exists before attempting to close
945+ for file_name in list (self .files .keys ()):
946+ if self .files [file_name ].isopen : # Check if file is still open
947+ self .files [file_name ].close ()
948+
943949
944950 @abstractmethod
945951 def _append_features (self , batch ) -> Table :
Original file line number Diff line number Diff line change @@ -21,8 +21,6 @@ def test_dl1_image_reading(dl1_tmp_path, dl1_gamma_file):
2121 mono_batch = dl1_reader .generate_mono_batch ([0 ])
2222 assert mono_batch ["tel_id" ] == 4
2323 assert mono_batch ["features" ].shape == (1 , 110 , 110 , 2 )
24- # Close the files
25- dl1_reader .close_files ()
2624
2725def test_r1_waveform_reading (r1_tmp_path , r1_gamma_file ):
2826 """check reading from pixel-wise waveform data files"""
@@ -42,6 +40,4 @@ def test_r1_waveform_reading(r1_tmp_path, r1_gamma_file):
4240 # Test the generation of a mono batch
4341 mono_batch = r1_reader .generate_mono_batch ([0 ])
4442 assert mono_batch ["tel_id" ] == 4
45- assert mono_batch ["features" ].shape == (1 , 110 , 110 , 20 )
46- # Close the files
47- r1_reader .close_files ()
43+ assert mono_batch ["features" ].shape == (1 , 110 , 110 , 20 )
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments