From 684dc2cfe4a85754abdbacba03f8226aaf5c12eb Mon Sep 17 00:00:00 2001 From: Henri Payno Date: Thu, 8 Sep 2022 13:19:39 +0200 Subject: [PATCH 1/5] remove useless print --- nxtomomill/nexus/test/test_nxdetector.py | 2 -- nxtomomill/nexus/test/test_nxtomo.py | 4 ---- 2 files changed, 6 deletions(-) diff --git a/nxtomomill/nexus/test/test_nxdetector.py b/nxtomomill/nexus/test/test_nxdetector.py index 84fb282..71cdb17 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 4ec1b59..2d62abf 100644 --- a/nxtomomill/nexus/test/test_nxtomo.py +++ b/nxtomomill/nexus/test/test_nxtomo.py @@ -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( -- GitLab From 04a3910ce7eb39524e7656da68b601bea0ff63cd Mon Sep 17 00:00:00 2001 From: Henri Payno Date: Thu, 8 Sep 2022 13:23:23 +0200 Subject: [PATCH 2/5] nexus: nxobject: fix: __str__ was not returning a string /close #117 --- nxtomomill/nexus/nxobject.py | 4 ++-- nxtomomill/nexus/test/test_nxtomo.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/nxtomomill/nexus/nxobject.py b/nxtomomill/nexus/nxobject.py index 21f83ad..3d34d23 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_nxtomo.py b/nxtomomill/nexus/test/test_nxtomo.py index 2d62abf..56bc9c0 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, -- GitLab From de6339fe80dbc71bf85bc13781271eb8ba4970de Mon Sep 17 00:00:00 2001 From: Henri Payno Date: Thu, 8 Sep 2022 16:38:11 +0200 Subject: [PATCH 3/5] raise an error is scan_range or tomo_n is None (fail to discover .info file) /close #115 --- nxtomomill/converter/edf/edfconverter.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/nxtomomill/converter/edf/edfconverter.py b/nxtomomill/converter/edf/edfconverter.py index 942e6e1..e2a7968 100644 --- a/nxtomomill/converter/edf/edfconverter.py +++ b/nxtomomill/converter/edf/edfconverter.py @@ -343,6 +343,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 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 ?" + ) if configuration.force_angle_calculation_endpoint: proj_angle = scan.scan_range / (scan.tomo_n - 1) else: -- GitLab From 70c6d5a34af9c2a9803256ef1cdf955c1165e8ec Mon Sep 17 00:00:00 2001 From: Henri Payno Date: Thu, 8 Sep 2022 16:38:51 +0200 Subject: [PATCH 4/5] edf2nx: fix computing rotation angle when no key in the header /close #116 --- nxtomomill/converter/edf/edfconverter.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/nxtomomill/converter/edf/edfconverter.py b/nxtomomill/converter/edf/edfconverter.py index e2a7968..1536de6 100644 --- a/nxtomomill/converter/edf/edfconverter.py +++ b/nxtomomill/converter/edf/edfconverter.py @@ -747,6 +747,16 @@ def from_edf_to_nx(configuration: TomoEDFConfig, progress=None) -> tuple: else: progress = 100.0 + if configuration.force_angle_calculation: + proj_angles = numpy.linspace( + 0, + scan.scan_range, + len(proj_indexes), + endpoint=configuration.force_angle_calculation_endpoint, + ) + for proj_index, angle in zip(proj_indexes, proj_angles): + rotation_dataset[proj_index] = angle + # we need to update alignement angles values. I wanted to avoid to redo all the previous existing processing. n_alignment_angles = len(alignment_indices) if n_alignment_angles == 3: -- GitLab From d2947e449de03a322cfa3379bf1504ed624038b7 Mon Sep 17 00:00:00 2001 From: Henri Payno Date: Thu, 8 Sep 2022 17:07:25 +0200 Subject: [PATCH 5/5] edf2nx: rework rotation angle creation when forced. /close #116 --- nxtomomill/converter/edf/edfconverter.py | 75 ++++++++++++------------ 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/nxtomomill/converter/edf/edfconverter.py b/nxtomomill/converter/edf/edfconverter.py index 1536de6..8d1699e 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 @@ -347,10 +348,6 @@ def from_edf_to_nx(configuration: TomoEDFConfig, progress=None) -> tuple: 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 ?" ) - if configuration.force_angle_calculation_endpoint: - proj_angle = scan.scan_range / (scan.tomo_n - 1) - else: - proj_angle = scan.scan_range / scan.tomo_n distance = scan.retrieve_information( scan=os.path.abspath(scan.path), @@ -633,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()) @@ -680,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 @@ -747,16 +756,6 @@ def from_edf_to_nx(configuration: TomoEDFConfig, progress=None) -> tuple: else: progress = 100.0 - if configuration.force_angle_calculation: - proj_angles = numpy.linspace( - 0, - scan.scan_range, - len(proj_indexes), - endpoint=configuration.force_angle_calculation_endpoint, - ) - for proj_index, angle in zip(proj_indexes, proj_angles): - rotation_dataset[proj_index] = angle - # we need to update alignement angles values. I wanted to avoid to redo all the previous existing processing. n_alignment_angles = len(alignment_indices) if n_alignment_angles == 3: -- GitLab