diff --git a/nxtomomill/converter/edf/edfconverter.py b/nxtomomill/converter/edf/edfconverter.py index 942e6e1caca47b2c479c5f822ec235ae14d152e2..8d1699ed37b1e6d2a9d9b3b7d19ff4d6374de72d 100644 --- a/nxtomomill/converter/edf/edfconverter.py +++ b/nxtomomill/converter/edf/edfconverter.py @@ -36,6 +36,7 @@ __date__ = "27/11/2020" from collections import namedtuple from typing import Optional + from nxtomomill.io.config.edfconfig import TomoEDFConfig from nxtomomill.io.utils import PathType from nxtomomill.nexus.nxsource import SourceType @@ -343,10 +344,10 @@ def from_edf_to_nx(configuration: TomoEDFConfig, progress=None) -> tuple: source_grp = h5d["/entry/instrument"].require_group("source") source_grp["probe"] = configuration.source_probe.value - if configuration.force_angle_calculation_endpoint: - proj_angle = scan.scan_range / (scan.tomo_n - 1) - else: - proj_angle = scan.scan_range / scan.tomo_n + if scan.scan_range is None or scan.tomo_n is None: + raise ValueError( + f"Cannot find scan_range ({scan.scan_range}) and / or tomo_n ({scan.tomo_n}). Is the .info file here and / or valid ?" + ) distance = scan.retrieve_information( scan=os.path.abspath(scan.path), @@ -629,8 +630,33 @@ def from_edf_to_nx(configuration: TomoEDFConfig, progress=None) -> tuple: nproj = 0 iref_pj = 0 + if configuration.force_angle_calculation: + if ( + configuration.angle_calculation_rev_neg_scan_range + and scan.scan_range < 0 + ): + proj_angles = numpy.linspace( + 0, + scan.scan_range, + scan.tomo_n, + endpoint=configuration.force_angle_calculation_endpoint, + ) + else: + proj_angles = numpy.linspace( + min(0, scan.scan_range), + max(0, scan.scan_range), + scan.tomo_n, + endpoint=configuration.force_angle_calculation_endpoint, + ) + revert_angles_in_nx = ( + configuration.angle_calculation_rev_neg_scan_range + and (scan.scan_range < 0) + ) + if revert_angles_in_nx: + proj_angles = proj_angles[::-1] + alignment_indices = [] - for proj_index in proj_indexes: + for i_proj, proj_index in enumerate(proj_indexes): proj_url = proj_urls[proj_index] if ignore(os.path.basename(proj_url.file_path())): _logger.info("ignore " + proj_url.file_path()) @@ -676,33 +702,20 @@ def from_edf_to_nx(configuration: TomoEDFConfig, progress=None) -> tuple: keys_control_dataset[nf] = ImageKey.ALIGNMENT.value motor_pos_key = _get_valid_key(header, configuration.motor_position_keys) + + if configuration.force_angle_calculation: + if i_proj < len(proj_angles): + rotation_dataset[nf] = proj_angles[i_proj] + else: + # case of return / control projection + rotation_dataset[nf] = 0.0 + if motor_pos_key in header: str_mot_val = header[motor_pos_key].split(" ") # continuous scan - rot angle is unknown. Compute it - if ( - configuration.force_angle_calculation is True - and nproj < scan.tomo_n - ): - angle = nproj * proj_angle - if ( - scan.scan_range < 0 - and configuration.angle_calculation_rev_neg_scan_range is False - ): - angle = scan.scan_range - angle - - rotation_dataset[nf] = angle - else: - if configuration.force_angle_calculation: - # FIXMe: for alignment projection when calculation is force the - # angle will always be set to 0.0 when it should be computed. - # it would be wiser to compute all the rotation angle at the - # end when each frame is tag. - rotation_dataset[nf] = 0.0 - if configuration.force_angle_calculation: - alignment_indices.append(nf) - else: - rotation_dataset[nf] = float(str_mot_val[rot_angle_index]) + if not configuration.force_angle_calculation: + rotation_dataset[nf] = float(str_mot_val[rot_angle_index]) if x_trans_index == -1: x_dataset[nf] = 0.0 diff --git a/nxtomomill/nexus/nxobject.py b/nxtomomill/nexus/nxobject.py index 21f83ad5fea16f15f576d6f8cf1a991fa36296db..3d34d237bbc5951d67921f128ed2f4b4be094d22 100644 --- a/nxtomomill/nexus/nxobject.py +++ b/nxtomomill/nexus/nxobject.py @@ -273,8 +273,8 @@ class NXobject: """ raise NotImplementedError("Base class") - def __str__(self): - f"{type(self)}: {self.path}" + def __str__(self) -> str: + return f"{type(self)}: {self.path}" @staticmethod def _get_virtual_sources(ddict) -> tuple: diff --git a/nxtomomill/nexus/test/test_nxdetector.py b/nxtomomill/nexus/test/test_nxdetector.py index 84fb282b0d7710ec69ab7ec8fe89a19a372177cf..71cdb179c44f4cd11a197ed170f7499f4d364cb5 100644 --- a/nxtomomill/nexus/test/test_nxdetector.py +++ b/nxtomomill/nexus/test/test_nxdetector.py @@ -328,8 +328,6 @@ def test_nx_detector_with_external_urls(): ], ) def test_load_detector_data(tmp_path, load_data_as, expected_type): - print("load_data_as is", load_data_as) - print("expected_type is", expected_type) layout = h5py.VirtualLayout(shape=(4 * 2, 100, 100), dtype="i4") for n in range(0, 4): diff --git a/nxtomomill/nexus/test/test_nxtomo.py b/nxtomomill/nexus/test/test_nxtomo.py index 4ec1b5957f209b78214a3345ee07897e1edf728b..56bc9c019fcc22021d6db0c00420073630c1c57f 100644 --- a/nxtomomill/nexus/test/test_nxtomo.py +++ b/nxtomomill/nexus/test/test_nxtomo.py @@ -337,7 +337,7 @@ def test_nx_tomo_subselection(nexus_path_version): data_dark = numpy.ones(shape) data_flat = numpy.ones(shape) * 2.0 data_projection = numpy.ones(shape) * 3.0 - + str(nx_tomo) nx_tomo.instrument.detector.data = numpy.concatenate( ( data_dark, @@ -464,10 +464,6 @@ def test_nx_tomo_subselection(nexus_path_version): angle_interval=None, shift_angles=False, ) - print( - "nx_tomo_sub_4.instrument.detector.image_key_control", - nx_tomo_sub_4.instrument.detector.image_key_control, - ) numpy.testing.assert_equal( nx_tomo_sub_4.instrument.detector.image_key_control, numpy.array(