Commit c5f1b243 authored by payno's avatar payno
Browse files

[hdf5scan] add some check one master_file and entry when read data

parent eaa8399f
......@@ -127,6 +127,8 @@ class HDF5TomoScan(TomoScanBase):
else:
self._entry = entry or self._get_entry_at(index=index,
file_path=self.master_file)
if self._entry is None:
raise ValueError('unable to find a valid entry for %s' % self.master_file)
# for now the default entry is 1_tomo but should change with time
# data caches
......@@ -333,7 +335,7 @@ class HDF5TomoScan(TomoScanBase):
def update(self) -> None:
"""update list of radio and reconstruction by parsing the scan folder
"""
if not os.path.exists(self.master_file):
if self.master_file is None or not os.path.exists(self.master_file):
return
self.projections = self._get_projections_url()
# TODO: update darks and flats too
......@@ -381,6 +383,7 @@ class HDF5TomoScan(TomoScanBase):
@property
def rotation_angle(self) -> typing.Union[None, list]:
if self._rotation_angles is None:
self._check_hdf5scan_validity()
with h5py.File(self.master_file, 'r') as h5_file:
_rotation_angles = h5_file[self._entry][self._ROTATION_ANGLE_PATH][()]
# cast in float
......@@ -389,9 +392,9 @@ class HDF5TomoScan(TomoScanBase):
@property
def image_key(self) -> typing.Union[list, None]:
if self._image_keys is None:
if self._entry and self._image_keys is None:
self._check_hdf5scan_validity()
with h5py.File(self.master_file, 'r') as h5_file:
assert self._entry in h5_file
self._image_keys = h5_file[self._entry][self._IMG_KEY_PATH][()]
return self._image_keys
......@@ -481,6 +484,7 @@ class HDF5TomoScan(TomoScanBase):
def distance(self) -> typing.Union[None, float]:
if (self._distance is None and self.master_file and
os.path.exists(self.master_file)):
self._check_hdf5scan_validity()
with h5py.File(self.master_file, 'r') as h5_file:
distance_dataset = h5_file[self._entry][self._DISTANCE_PATH]
self._distance = self._get_value(distance_dataset, default_unit='m')
......@@ -490,6 +494,7 @@ class HDF5TomoScan(TomoScanBase):
def energy(self) -> typing.Union[None, float]:
if (self._energy is None and self.master_file and
os.path.exists(self.master_file)):
self._check_hdf5scan_validity()
with h5py.File(self.master_file, 'r') as h5_file:
energy_dataset = h5_file[self._entry][self._ENERGY_PATH]
self._energy = self._get_value(energy_dataset, default_unit='keV')
......@@ -576,6 +581,16 @@ class HDF5TomoScan(TomoScanBase):
unit = default_unit
return value * metricsystem.MetricSystem.from_value(unit).value
def _check_hdf5scan_validity(self):
if self.master_file is None:
raise ValueError('No master file provided')
if self.entry is None:
raise ValueError('No entry provided')
with h5py.File(self.master_file, 'r') as h5_file:
if self._entry not in h5_file:
raise ValueError('Given entry %s is not in the master '
'file %s' % (self._entry, self.master_file))
class Frame:
"""class to store all metadata information of a frame"""
......
......@@ -307,10 +307,6 @@ class TestScanValidatorFindFiles(unittest.TestCase):
if os.path.isdir(self.path):
shutil.rmtree(self.path)
def testGetRadioPaths(self):
nFound = len(EDFTomoScan.get_proj_urls(self.path))
self.assertTrue(nFound == self.N_RADIO)
class TestRadioPath(unittest.TestCase):
"""Test static method getRadioPaths for EDFTomoScan"""
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment