Commit 1e6e4dc4 authored by payno's avatar payno
Browse files

[hdf5scan] PEP8 & typing

parent fb626b4e
Pipeline #21865 failed with stages
in 55 seconds
......@@ -94,7 +94,8 @@ class HDF5TomoScan(TomoScanBase):
_EPSILON_ROT_ANGLE = 0.02
def __init__(self, scan, entry=None, index=0):
def __init__(self, scan: str, entry: str = None,
index: typing.Union[int, None] = 0):
if entry is not None:
index = None
# if the user give the master file instead of the scan dir...
......@@ -140,9 +141,10 @@ class HDF5TomoScan(TomoScanBase):
self._frames = None
self._image_keys = None
self._rotation_angles = None
self._distance = None
@docstring(TomoScanBase.clear_caches)
def clear_caches(self):
def clear_caches(self) -> None:
self._projections = None
self._flats = None
self._darks = None
......@@ -154,9 +156,10 @@ class HDF5TomoScan(TomoScanBase):
self._x_pixel_size = None
self._y_pixel_size = None
self._rotation_angles = None
self._distance = None
@staticmethod
def _get_entry_at(index, file_path):
def _get_entry_at(index: int, file_path: str) -> str:
"""
:param index:
......@@ -170,7 +173,7 @@ class HDF5TomoScan(TomoScanBase):
return entries[index]
@staticmethod
def _get_valid_entries(file_path):
def _get_valid_entries(file_path: str) -> tuple:
"""
return the list of 'Nxtomo' entries at the root level
......@@ -194,7 +197,7 @@ class HDF5TomoScan(TomoScanBase):
return tuple(res)
@staticmethod
def node_is_nxtomo(node):
def node_is_nxtomo(node: h5py.Group) -> bool:
"""check if the given h5py node is an nxtomo node or not"""
if 'NX_class' in node.attrs or 'NXclass' in node.attrs:
_logger.info(node.name + ' is recognized as an nx class.')
......@@ -209,7 +212,7 @@ class HDF5TomoScan(TomoScanBase):
@docstring(TomoScanBase.is_tomoscan_dir)
@staticmethod
def is_tomoscan_dir(directory, **kwargs):
def is_tomoscan_dir(directory: str, **kwargs):
master_file_base = os.path.join(directory, os.path.basename(directory))
if os.path.exists(master_file_base + '.hdf5'):
return True
......@@ -228,13 +231,13 @@ class HDF5TomoScan(TomoScanBase):
return res
@staticmethod
def from_dict(_dict):
def from_dict(_dict: dict):
scan = HDF5TomoScan(scan=None)
scan.load_from_dict(_dict=_dict)
return scan
@docstring(TomoScanBase.load_from_dict)
def load_from_dict(self, _dict):
def load_from_dict(self, _dict: dict) -> TomoScanBase:
"""
:param _dict:
......@@ -255,12 +258,12 @@ class HDF5TomoScan(TomoScanBase):
return self
@property
def entry(self):
def entry(self) -> str:
return self._entry
@property
@docstring(TomoScanBase.projections)
def projections(self):
def projections(self) -> typing.Union[list, None]:
"""projections / radio, does not include the return projections"""
if self._projections is None:
if self.frames:
......@@ -269,12 +272,12 @@ class HDF5TomoScan(TomoScanBase):
return self._projections
@projections.setter
def projections(self, projections):
def projections(self, projections: list):
self._projections = projections
@property
@docstring(TomoScanBase.darks)
def darks(self):
def darks(self) -> typing.Union[list, None]:
if self._darks is None:
if self.frames:
dark_frames = tuple(filter(lambda x: x.image_key is ImageKey.DARK_FIELD, self.frames))
......@@ -283,7 +286,7 @@ class HDF5TomoScan(TomoScanBase):
@property
@docstring(TomoScanBase.flats)
def flats(self):
def flats(self) -> typing.Union[list, None]:
if self._flats is None:
if self.frames:
flat_frames = tuple(filter(lambda x: x.image_key is ImageKey.FLAT_FIELD, self.frames))
......@@ -291,7 +294,7 @@ class HDF5TomoScan(TomoScanBase):
return self._flats
@docstring(TomoScanBase.update)
def update(self):
def update(self) -> None:
"""update list of radio and reconstruction by parsing the scan folder
"""
if not os.path.exists(self.master_file):
......@@ -315,12 +318,13 @@ class HDF5TomoScan(TomoScanBase):
@docstring(TomoScanBase.tomo_n)
@property
def tomo_n(self):
def tomo_n(self) -> typing.Union[None, int]:
"""we are making two asumptions for computing tomo_n:
- if a rotation = scan_range +/- EPSILON this is a return projection
- The delta between each projections is constant
"""
if self._tomo_n is None and self.master_file and os.path.exists(self.master_file):
if (self._tomo_n is None and self.master_file and
os.path.exists(self.master_file)):
if self.projections:
return len(self.projections)
else:
......@@ -329,7 +333,7 @@ class HDF5TomoScan(TomoScanBase):
return None
@property
def return_projs(self):
def return_projs(self) -> typing.Union[None, list]:
""""""
frames = self.frames
if frames:
......@@ -339,7 +343,7 @@ class HDF5TomoScan(TomoScanBase):
return None
@property
def rotation_angle(self):
def rotation_angle(self) -> typing.Union[None, list]:
if self._rotation_angles is None:
with h5py.File(self.master_file, 'r') as h5_file:
_rotation_angles = h5_file[self._entry][self._ROTATION_ANGLE_PATH][()]
......@@ -348,7 +352,7 @@ class HDF5TomoScan(TomoScanBase):
return self._rotation_angles
@property
def image_key(self) -> typing.Union:
def image_key(self) -> typing.Union[list, None]:
if self._image_keys is None:
with h5py.File(self.master_file, 'r') as h5_file:
assert self._entry in h5_file
......@@ -357,7 +361,7 @@ class HDF5TomoScan(TomoScanBase):
@docstring(TomoScanBase.dark_n)
@property
def dark_n(self):
def dark_n(self) -> typing.Union[None, int]:
if self.darks is not None:
return len(self.darks)
else:
......@@ -365,7 +369,7 @@ class HDF5TomoScan(TomoScanBase):
@docstring(TomoScanBase.ref_n)
@property
def ref_n(self):
def ref_n(self) -> typing.Union[None, int]:
if self.flats is not None:
return len(self.flats)
else:
......@@ -379,7 +383,7 @@ class HDF5TomoScan(TomoScanBase):
@docstring(TomoScanBase.scan_range)
@property
def scan_range(self):
def scan_range(self) -> typing.Union[None, int]:
"""For now scan range should return 180 or 360. We don't expect other value."""
if (self._scan_range is None and self.master_file and
os.path.exists(self.master_file) and self._entry is not None):
......@@ -394,7 +398,7 @@ class HDF5TomoScan(TomoScanBase):
return self._scan_range
@property
def dim_1(self):
def dim_1(self) -> typing.Union[None, int]:
if self._dim_1 is None and self.master_file and os.path.exists(self.master_file):
if self.projections is not None:
if len(self.projections) > 0:
......@@ -402,7 +406,7 @@ class HDF5TomoScan(TomoScanBase):
return self._dim_1
@property
def dim_2(self):
def dim_2(self) -> typing.Union[None, int]:
if self._dim_2 is None and self.master_file and os.path.exists(self.master_file):
if self.projections is not None:
if len(self.projections) > 0:
......@@ -410,12 +414,13 @@ class HDF5TomoScan(TomoScanBase):
return self._dim_2
@property
def pixel_size(self):
def pixel_size(self) -> typing.Union[None, float]:
return self.x_pixel_size
@property
def x_pixel_size(self):
if self._x_pixel_size is None and self.master_file and os.path.exists(self.master_file):
def x_pixel_size(self) -> typing.Union[None, float]:
if (self._x_pixel_size is None and self.master_file and
os.path.exists(self.master_file)):
self._x_pixel_size, self._y_pixel_size = self._get_x_y_pixel_values()
return self._x_pixel_size
......@@ -430,21 +435,23 @@ class HDF5TomoScan(TomoScanBase):
return _x_pixel_size, _y_pixel_size
@property
def y_pixel_size(self):
if self._y_pixel_size is None and self.master_file and os.path.exists(self.master_file):
def y_pixel_size(self) -> typing.Union[None, float]:
if (self._y_pixel_size is None and self.master_file and
os.path.exists(self.master_file)):
self._x_pixel_size, self._y_pixel_size = self._get_x_y_pixel_values()
return self._y_pixel_size
@property
def distance(self):
if self._distance is None and self.master_file and os.path.exists(self.master_file):
def distance(self) -> typing.Union[None, float]:
if (self._distance is None and self.master_file and
os.path.exists(self.master_file)):
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')
return self._distance
@property
def frames(self) -> tuple:
def frames(self) -> typing.Union[None, tuple]:
"""return tuple of frames. Frames contains """
if self._frames is None:
image_keys = self.image_key
......@@ -491,7 +498,7 @@ class HDF5TomoScan(TomoScanBase):
return self._frames
@docstring(TomoScanBase.get_proj_angle_url)
def get_proj_angle_url(self) -> dict:
def get_proj_angle_url(self) -> typing.Union[dict, None]:
if self.frames is not None:
res = {}
for frame in self.frames:
......@@ -540,15 +547,15 @@ class Frame:
self._data = None
@property
def index(self):
def index(self) -> int:
return self._index
@property
def image_key(self):
def image_key(self) -> ImageKey:
return self._image_key
@image_key.setter
def image_key(self, image_key) -> ImageKey:
def image_key(self, image_key: ImageKey) -> None:
self._image_key = image_key
@property
......@@ -556,17 +563,17 @@ class Frame:
return self._rotation_angle
@rotation_angle.setter
def rotation_angle(self, angle):
def rotation_angle(self, angle: float) -> None:
self._rotation_angle = angle
@property
def url(self):
def url(self) -> DataUrl:
return self._url
@property
def is_return(self):
def is_return(self) -> bool:
return self._is_return_frame
@is_return.setter
def is_return(self, is_return):
def is_return(self, is_return: bool):
self._is_return_frame = is_return
......@@ -34,6 +34,7 @@ import os
import tempfile
from tomoscan.test.utils import UtilsTest
from tomoscan.esrf.hdf5scan import HDF5TomoScan, ImageKey, Frame
from ...unitsystem import metricsystem
from silx.io.utils import get_data
import numpy
......@@ -186,8 +187,19 @@ class TestHDF5Scan(HDF5TestBaseClass):
def testDarkRefUtils(self):
self.assertEqual(self.scan.tomo_n, 1500)
# TODO: add after merge
# self.assertTrue(numpy.isclose(self.scan.get_pixel_size(unit='mm'), 0.05))
pixel_size = self.scan.pixel_size
self.assertTrue(pixel_size is not None)
self.assertTrue(numpy.isclose(self.scan.pixel_size,
0.05 * metricsystem.MetricSystem.MILLIMETER.value))
self.assertTrue(numpy.isclose(self.scan.get_pixel_size(unit='mm'), 0.05))
self.assertTrue(numpy.isclose(self.scan.x_pixel_size,
0.05 * metricsystem.MetricSystem.MILLIMETER.value))
self.assertTrue(numpy.isclose(self.scan.y_pixel_size,
0.05 * metricsystem.MetricSystem.MILLIMETER.value))
def testNabuUtil(self):
self.assertTrue(numpy.isclose(self.scan.distance, -19.9735))
self.assertTrue(numpy.isclose(self.scan.get_distance(unit='cm'), -1997.35))
def suite():
......
......@@ -153,7 +153,7 @@ class TomoScanBase:
def get_pixel_size(self, unit='m') -> Union[None, float]:
if self.pixel_size:
return self.pixel_size / MetricSystem.from_value(unit)
return self.pixel_size / MetricSystem.from_value(unit).value
else:
return None
......
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