From 756627caa2cb56cbe751779b11f9b00183f63f86 Mon Sep 17 00:00:00 2001 From: Edgar Gutierrez Fernandez Date: Tue, 5 Nov 2024 18:30:57 +0100 Subject: [PATCH 1/5] deepcopy of fiber units --- src/pyFAI/units.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/pyFAI/units.py b/src/pyFAI/units.py index 58ae3a93d..5c4515d9c 100644 --- a/src/pyFAI/units.py +++ b/src/pyFAI/units.py @@ -41,6 +41,7 @@ __status__ = "production" __docformat__ = 'restructuredtext' +import copy import logging logger = logging.getLogger(__name__) import numpy @@ -990,7 +991,8 @@ def get_unit_fiber(name, incident_angle:float =0.0, tilt_angle:float =0.0, sampl :param float tilt angle: roll angle. Its rotation axis is orthogonal to the beam, the horizontal axis of the lab frame :param sample_orientation: 1-4, four different orientation of the fiber axis regarding the detector main axis, from 1 to 4 is +90ยบ """ - unit = RADIAL_UNITS.get(name, None) + unit = copy.deepcopy(RADIAL_UNITS.get(name, None)) + if unit is not None: unit.set_incident_angle(incident_angle) unit.set_tilt_angle(tilt_angle) From 2c7eac9e40f8cdacd692460d56c58c88458a43c7 Mon Sep 17 00:00:00 2001 From: Edgar Gutierrez Fernandez Date: Tue, 5 Nov 2024 19:15:42 +0100 Subject: [PATCH 2/5] parsing fiber units --- src/pyFAI/integrator/fiber.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/pyFAI/integrator/fiber.py b/src/pyFAI/integrator/fiber.py index 815a1b634..383c4ee8d 100644 --- a/src/pyFAI/integrator/fiber.py +++ b/src/pyFAI/integrator/fiber.py @@ -64,12 +64,20 @@ def sample_orientation(self): def parse_units(self, unit_ip, unit_oop, incident_angle=None, tilt_angle=None, sample_orientation=None): if unit_ip is None: - unit_ip = units.get_unit_fiber("qip_nm^-1") + unit_ip = units.get_unit_fiber(name="qip_nm^-1") + elif isinstance(unit_ip, str): + unit_ip = units.get_unit_fiber(name=unit_ip) + elif isinstance(unit_ip, units.UnitFiber): + ... else: unit_ip = units.to_unit(unit_ip) if unit_oop is None: - unit_oop = units.get_unit_fiber("qoop_nm^-1") + unit_oop = units.get_unit_fiber(name="qoop_nm^-1") + elif isinstance(unit_oop, str): + unit_oop = units.get_unit_fiber(name=unit_oop) + elif isinstance(unit_oop, units.UnitFiber): + ... else: unit_oop = units.to_unit(unit_oop) From 70e0857ac5dc6fdaeb74cfde30ae5e72a49bb283 Mon Sep 17 00:00:00 2001 From: Edgar Gutierrez Fernandez Date: Wed, 13 Nov 2024 14:51:06 +0100 Subject: [PATCH 3/5] clean up FiberIntegrator --- src/pyFAI/integrator/fiber.py | 261 ++++++++++-------------- src/pyFAI/test/test_fiber_integrator.py | 97 +++++++++ src/pyFAI/units.py | 24 ++- 3 files changed, 223 insertions(+), 159 deletions(-) diff --git a/src/pyFAI/integrator/fiber.py b/src/pyFAI/integrator/fiber.py index 383c4ee8d..8cc15a6ad 100644 --- a/src/pyFAI/integrator/fiber.py +++ b/src/pyFAI/integrator/fiber.py @@ -30,7 +30,7 @@ __contact__ = "edgar.gutierrez-fernandez@esr.fr" __license__ = "MIT" __copyright__ = "European Synchrotron Radiation Facility, Grenoble, France" -__date__ = "24/10/2024" +__date__ = "13/11/2024" __status__ = "stable" __docformat__ = 'restructuredtext' @@ -41,7 +41,7 @@ from ..containers import Integrate1dResult from ..method_registry import IntegrationMethod from ..io import save_integrate_result -from .. import units +from ..units import parse_fiber_unit from ..utils.decorators import deprecated_warning class FiberIntegrator(AzimuthalIntegrator): @@ -62,61 +62,6 @@ def tilt_angle(self): def sample_orientation(self): return self._cache_parameters.get('sample_orientation', 1) - def parse_units(self, unit_ip, unit_oop, incident_angle=None, tilt_angle=None, sample_orientation=None): - if unit_ip is None: - unit_ip = units.get_unit_fiber(name="qip_nm^-1") - elif isinstance(unit_ip, str): - unit_ip = units.get_unit_fiber(name=unit_ip) - elif isinstance(unit_ip, units.UnitFiber): - ... - else: - unit_ip = units.to_unit(unit_ip) - - if unit_oop is None: - unit_oop = units.get_unit_fiber(name="qoop_nm^-1") - elif isinstance(unit_oop, str): - unit_oop = units.get_unit_fiber(name=unit_oop) - elif isinstance(unit_oop, units.UnitFiber): - ... - else: - unit_oop = units.to_unit(unit_oop) - - if incident_angle is None: - if isinstance(unit_ip, units.UnitFiber): - incident_angle = unit_ip.incident_angle - elif isinstance(unit_oop, units.UnitFiber): - incident_angle = unit_oop.incident_angle - else: - incident_angle = 0.0 - - if tilt_angle is None: - if isinstance(unit_ip, units.UnitFiber): - tilt_angle = unit_ip.tilt_angle - elif isinstance(unit_oop, units.UnitFiber): - tilt_angle = unit_oop.tilt_angle - else: - tilt_angle = 0.0 - - if sample_orientation is None: - if isinstance(unit_ip, units.UnitFiber): - sample_orientation = unit_ip.sample_orientation - elif isinstance(unit_oop, units.UnitFiber): - sample_orientation = unit_oop.sample_orientation - else: - sample_orientation = 1 - - unit_ip = units.to_unit(unit_ip) - unit_ip.set_incident_angle(incident_angle) - unit_ip.set_tilt_angle(tilt_angle) - unit_ip.set_sample_orientation(sample_orientation) - - unit_oop = units.to_unit(unit_oop) - unit_oop.set_incident_angle(incident_angle) - unit_oop.set_tilt_angle(tilt_angle) - unit_oop.set_sample_orientation(sample_orientation) - - return unit_ip, unit_oop - def reset_integrator(self, incident_angle, tilt_angle, sample_orientation): reset = False if incident_angle != self.incident_angle: @@ -137,6 +82,56 @@ def reset_integrator(self, incident_angle, tilt_angle, sample_orientation): self._cache_parameters['tilt_angle'] = tilt_angle self._cache_parameters['sample_orientation'] = sample_orientation + def _get_deprecated_params_1d(self, **kwargs) -> dict: + deprecated = {} + if "npt_output" in kwargs: + deprecated_warning(type_=type(kwargs["npt_output"]), name="npt_output", replacement=("npt_oop, npt_ip, vertical_integration instead"), since_version="2024.11/12") + deprecated['npt_oop'] = kwargs["npt_output"] + deprecated['vertical_integration'] = True + if "npt_integrated" in kwargs: + deprecated_warning(type_=type(kwargs["npt_integrated"]), name="npt_integrated", replacement=("npt_oop, npt_ip, vertical_integration instead"), since_version="2024.11/12") + deprecated['npt_ip'] = kwargs["npt_integrated"] + deprecated['vertical_integration'] = True + if "output_unit" in kwargs: + deprecated_warning(type_=type(kwargs["output_unit"]), name="output_unit", replacement=("unit_oop, unit_ip, vertical_integration instead"), since_version="2024.11/12") + deprecated['unit_oop'] = kwargs["output_unit"] + deprecated['vertical_integration'] = True + if "integrated_unit" in kwargs: + deprecated_warning(type_=type(kwargs["integrated_unit"]), name="integrated_unit", replacement=("unit_oop, unit_ip, vertical_integration instead"), since_version="2024.11/12") + deprecated['unit_ip'] = kwargs["integrated_unit"] + deprecated['vertical_integration'] = True + if "output_unit_range" in kwargs: + deprecated_warning(type_=type(kwargs["output_unit_range"]), name="output_unit_range", replacement=("oop_range, ip_range, vertical_integration instead"), since_version="2024.11/12") + deprecated['oop_range'] = kwargs["output_unit_range"] + deprecated['vertical_integration'] = True + if "integrated_unit_range" in kwargs: + deprecated_warning(type_=type(kwargs["integrated_unit_range"]), name="integrated_unit_range", replacement=("oop_range, ip_range, vertical_integration instead"), since_version="2024.11/12") + deprecated['ip_range'] = kwargs["integrated_unit_range"] + deprecated['vertical_integration'] = True + return deprecated + + def _get_deprecated_params_2d(self, **kwargs) -> dict: + deprecated = {} + if "npt_horizontal" in kwargs: + deprecated_warning(type_=type(kwargs["npt_horizontal"]), name="npt_horizontal", replacement="npt_ip", since_version="2024.11/12") + deprecated['npt_ip'] = kwargs["npt_horizontal"] + if "npt_vertical" in kwargs: + deprecated_warning(type_=type(kwargs["npt_vertical"]), name="npt_vertical", replacement="npt_oop", since_version="2024.11/12") + deprecated['npt_oop'] = kwargs["npt_vertical"] + if "horizontal_unit" in kwargs: + deprecated_warning(type_=type(kwargs["horizontal_unit"]), name="horizontal_unit", replacement="unit_ip", since_version="2024.11/12") + deprecated['unit_ip'] = kwargs["horizontal_unit"] + if "vertical_unit" in kwargs: + deprecated_warning(type_=type(kwargs["vertical_unit"]), name="vertical_unit", replacement="unit_oop", since_version="2024.11/12") + deprecated['unit_oop'] = kwargs["vertical_unit"] + if "horizontal_unit_range" in kwargs: + deprecated_warning(type_=type(kwargs["horizontal_unit_range"]), name="horizontal_unit_range", replacement="ip_range", since_version="2024.11/12") + deprecated['ip_range'] = kwargs["horizontal_unit_range"] + if "vertical_unit_range" in kwargs: + deprecated_warning(type_=type(kwargs["vertical_unit_range"]), name="vertical_unit_range", replacement="oop_range", since_version="2024.11/12") + deprecated['oop_range'] = kwargs["vertical_unit_range"] + return deprecated + def integrate_fiber(self, data, npt_oop=None, unit_oop=None, oop_range=None, @@ -177,33 +172,19 @@ def integrate_fiber(self, data, :return: chi bins center positions and regrouped intensity :rtype: Integrate1dResult """ - if "npt_output" in kwargs: - deprecated_warning(type_=type(kwargs["npt_output"]), name="npt_output", replacement=("npt_oop, npt_ip, vertical_integration instead"), since_version="2024.11/12") - npt_oop = kwargs["npt_output"] - vertical_integration = True - if "npt_integrated" in kwargs: - deprecated_warning(type_=type(kwargs["npt_integrated"]), name="npt_integrated", replacement=("npt_oop, npt_ip, vertical_integration instead"), since_version="2024.11/12") - npt_ip = kwargs["npt_integrated"] - vertical_integration = True - if "output_unit" in kwargs: - deprecated_warning(type_=type(kwargs["output_unit"]), name="output_unit", replacement=("unit_oop, unit_ip, vertical_integration instead"), since_version="2024.11/12") - unit_oop = kwargs["output_unit"] - vertical_integration = True - if "integrated_unit" in kwargs: - deprecated_warning(type_=type(kwargs["integrated_unit"]), name="integrated_unit", replacement=("unit_oop, unit_ip, vertical_integration instead"), since_version="2024.11/12") - unit_ip = kwargs["integrated_unit"] - vertical_integration = True - if "output_unit_range" in kwargs: - deprecated_warning(type_=type(kwargs["output_unit_range"]), name="output_unit_range", replacement=("oop_range, ip_range, vertical_integration instead"), since_version="2024.11/12") - oop_range = kwargs["output_unit_range"] - vertical_integration = True - if "integrated_unit_range" in kwargs: - deprecated_warning(type_=type(kwargs["integrated_unit_range"]), name="integrated_unit_range", replacement=("oop_range, ip_range, vertical_integration instead"), since_version="2024.11/12") - ip_range = kwargs["integrated_unit_range"] - vertical_integration = True - - unit_ip, unit_oop = self.parse_units(unit_ip=unit_ip, unit_oop=unit_oop, - sample_orientation=sample_orientation) + deprecated_params = self._get_deprecated_params_1d(**kwargs) + npt_oop = deprecated_params.get('npt_oop', None) or npt_oop + npt_ip = deprecated_params.get('npt_ip', None) or npt_ip + unit_oop = deprecated_params.get('unit_oop', None) or unit_oop + unit_ip = deprecated_params.get('unit_ip', None) or unit_ip + oop_range = deprecated_params.get('oop_range', None) or oop_range + ip_range = deprecated_params.get('ip_range', None) or ip_range + vertical_integration = deprecated_params.get('vertical_integration', None) or vertical_integration + + unit_ip = unit_ip or 'qip_nm^-1' + unit_oop = unit_oop or 'qoop_nm^-1' + unit_ip = parse_fiber_unit(unit=unit_ip, sample_orientation=sample_orientation) + unit_oop = parse_fiber_unit(unit=unit_oop, sample_orientation=unit_ip.sample_orientation) self.reset_integrator(incident_angle=unit_ip.incident_angle, tilt_angle=unit_ip.tilt_angle, @@ -322,35 +303,19 @@ def integrate_grazing_incidence(self, data, :return: chi bins center positions and regrouped intensity :rtype: Integrate1dResult """ - if "npt_output" in kwargs: - deprecated_warning(type_=type(kwargs["npt_output"]), name="npt_output", replacement=("npt_oop, npt_ip, vertical_integration instead"), since_version="2024.11/12") - npt_oop = kwargs["npt_output"] - vertical_integration = True - if "npt_integrated" in kwargs: - deprecated_warning(type_=type(kwargs["npt_integrated"]), name="npt_integrated", replacement=("npt_oop, npt_ip, vertical_integration instead"), since_version="2024.11/12") - npt_ip = kwargs["npt_integrated"] - vertical_integration = True - if "output_unit" in kwargs: - deprecated_warning(type_=type(kwargs["output_unit"]), name="output_unit", replacement=("unit_oop, unit_ip, vertical_integration instead"), since_version="2024.11/12") - unit_oop = kwargs["output_unit"] - vertical_integration = True - if "integrated_unit" in kwargs: - deprecated_warning(type_=type(kwargs["integrated_unit"]), name="integrated_unit", replacement=("unit_oop, unit_ip, vertical_integration instead"), since_version="2024.11/12") - unit_ip = kwargs["integrated_unit"] - vertical_integration = True - if "output_unit_range" in kwargs: - deprecated_warning(type_=type(kwargs["output_unit_range"]), name="output_unit_range", replacement=("oop_range, ip_range, vertical_integration instead"), since_version="2024.11/12") - oop_range = kwargs["output_unit_range"] - vertical_integration = True - if "integrated_unit_range" in kwargs: - deprecated_warning(type_=type(kwargs["integrated_unit_range"]), name="integrated_unit_range", replacement=("oop_range, ip_range, vertical_integration instead"), since_version="2024.11/12") - ip_range = kwargs["integrated_unit_range"] - vertical_integration = True - - unit_ip, unit_oop = self.parse_units(unit_ip=unit_ip, unit_oop=unit_oop, - incident_angle=incident_angle, - tilt_angle=tilt_angle, - sample_orientation=sample_orientation) + deprecated_params = self._get_deprecated_params_1d(**kwargs) + npt_oop = deprecated_params.get('npt_oop', None) or npt_oop + npt_ip = deprecated_params.get('npt_ip', None) or npt_ip + unit_oop = deprecated_params.get('unit_oop', None) or unit_oop + unit_ip = deprecated_params.get('unit_ip', None) or unit_ip + oop_range = deprecated_params.get('oop_range', None) or oop_range + ip_range = deprecated_params.get('ip_range', None) or ip_range + vertical_integration = deprecated_params.get('vertical_integration', None) or vertical_integration + + unit_ip = unit_ip or 'qip_nm^-1' + unit_oop = unit_oop or 'qoop_nm^-1' + unit_ip = parse_fiber_unit(unit=unit_ip, incident_angle=incident_angle, tilt_angle=tilt_angle, sample_orientation=sample_orientation) + unit_oop = parse_fiber_unit(unit=unit_oop, incident_angle=unit_ip.incident_angle, tilt_angle=unit_ip.tilt_angle, sample_orientation=unit_ip.sample_orientation) self.reset_integrator(incident_angle=unit_ip.incident_angle, tilt_angle=unit_ip.tilt_angle, @@ -408,27 +373,18 @@ def integrate2d_fiber(self, data, :return: regrouped intensity and unit arrays :rtype: Integrate2dResult """ - if "npt_horizontal" in kwargs: - deprecated_warning(type_=type(kwargs["npt_horizontal"]), name="npt_horizontal", replacement="npt_ip", since_version="2024.11/12") - npt_ip = kwargs["npt_horizontal"] - if "npt_vertical" in kwargs: - deprecated_warning(type_=type(kwargs["npt_vertical"]), name="npt_vertical", replacement="npt_oop", since_version="2024.11/12") - npt_oop = kwargs["npt_vertical"] - if "horizontal_unit" in kwargs: - deprecated_warning(type_=type(kwargs["horizontal_unit"]), name="horizontal_unit", replacement="unit_ip", since_version="2024.11/12") - unit_ip = kwargs["horizontal_unit"] - if "vertical_unit" in kwargs: - deprecated_warning(type_=type(kwargs["vertical_unit"]), name="vertical_unit", replacement="unit_oop", since_version="2024.11/12") - unit_oop = kwargs["vertical_unit"] - if "horizontal_unit_range" in kwargs: - deprecated_warning(type_=type(kwargs["horizontal_unit_range"]), name="horizontal_unit_range", replacement="ip_range", since_version="2024.11/12") - ip_range = kwargs["horizontal_unit_range"] - if "vertical_unit_range" in kwargs: - deprecated_warning(type_=type(kwargs["vertical_unit_range"]), name="vertical_unit_range", replacement="oop_range", since_version="2024.11/12") - oop_range = kwargs["vertical_unit_range"] - - unit_ip, unit_oop = self.parse_units(unit_ip=unit_ip, unit_oop=unit_oop, - sample_orientation=sample_orientation) + deprecated_params = self._get_deprecated_params_2d(**kwargs) + npt_oop = deprecated_params.get('npt_oop', None) or npt_oop + npt_ip = deprecated_params.get('npt_ip', None) or npt_ip + unit_oop = deprecated_params.get('unit_oop', None) or unit_oop + unit_ip = deprecated_params.get('unit_ip', None) or unit_ip + oop_range = deprecated_params.get('oop_range', None) or oop_range + ip_range = deprecated_params.get('ip_range', None) or ip_range + + unit_ip = unit_ip or 'qip_nm^-1' + unit_oop = unit_oop or 'qoop_nm^-1' + unit_ip = parse_fiber_unit(unit=unit_ip, sample_orientation=sample_orientation) + unit_oop = parse_fiber_unit(unit=unit_oop, sample_orientation=unit_ip.sample_orientation) self.reset_integrator(incident_angle=unit_ip.incident_angle, tilt_angle=unit_ip.tilt_angle, @@ -436,7 +392,7 @@ def integrate2d_fiber(self, data, if (isinstance(method, (tuple, list)) and method[0] != "no") or (isinstance(method, IntegrationMethod) and method.split != "no"): logger.warning(f"Method {method} is using a pixel-splitting scheme. GI integration should be use WITHOUT PIXEL-SPLITTING! The results could be wrong!") - + return self.integrate2d_ng(data, npt_rad=npt_ip, npt_azim=npt_oop, correctSolidAngle=correctSolidAngle, mask=mask, dummy=dummy, delta_dummy=delta_dummy, @@ -486,29 +442,18 @@ def integrate2d_grazing_incidence(self, data, :return: regrouped intensity and unit arrays :rtype: Integrate2dResult """ - if "npt_horizontal" in kwargs: - deprecated_warning(type_=type(kwargs["npt_horizontal"]), name="npt_horizontal", replacement="npt_ip", since_version="2024.11/12") - npt_ip = kwargs["npt_horizontal"] - if "npt_vertical" in kwargs: - deprecated_warning(type_=type(kwargs["npt_vertical"]), name="npt_vertical", replacement="npt_oop", since_version="2024.11/12") - npt_oop = kwargs["npt_vertical"] - if "horizontal_unit" in kwargs: - deprecated_warning(type_=type(kwargs["horizontal_unit"]), name="horizontal_unit", replacement="unit_ip", since_version="2024.11/12") - unit_ip = kwargs["horizontal_unit"] - if "vertical_unit" in kwargs: - deprecated_warning(type_=type(kwargs["vertical_unit"]), name="vertical_unit", replacement="unit_oop", since_version="2024.11/12") - unit_oop = kwargs["vertical_unit"] - if "horizontal_unit_range" in kwargs: - deprecated_warning(type_=type(kwargs["horizontal_unit_range"]), name="horizontal_unit_range", replacement="ip_range", since_version="2024.11/12") - ip_range = kwargs["horizontal_unit_range"] - if "vertical_unit_range" in kwargs: - deprecated_warning(type_=type(kwargs["vertical_unit_range"]), name="vertical_unit_range", replacement="oop_range", since_version="2024.11/12") - oop_range = kwargs["vertical_unit_range"] - - unit_ip, unit_oop = self.parse_units(unit_ip=unit_ip, unit_oop=unit_oop, - incident_angle=incident_angle, - tilt_angle=tilt_angle, - sample_orientation=sample_orientation) + deprecated_params = self._get_deprecated_params_2d(**kwargs) + npt_oop = deprecated_params.get('npt_oop', None) or npt_oop + npt_ip = deprecated_params.get('npt_ip', None) or npt_ip + unit_oop = deprecated_params.get('unit_oop', None) or unit_oop + unit_ip = deprecated_params.get('unit_ip', None) or unit_ip + oop_range = deprecated_params.get('oop_range', None) or oop_range + ip_range = deprecated_params.get('ip_range', None) or ip_range + + unit_ip = unit_ip or 'qip_nm^-1' + unit_oop = unit_oop or 'qoop_nm^-1' + unit_ip = parse_fiber_unit(unit=unit_ip, incident_angle=incident_angle, tilt_angle=tilt_angle, sample_orientation=sample_orientation) + unit_oop = parse_fiber_unit(unit=unit_oop, incident_angle=unit_ip.incident_angle, tilt_angle=unit_ip.tilt_angle, sample_orientation=unit_ip.sample_orientation) self.reset_integrator(incident_angle=unit_ip.incident_angle, tilt_angle=unit_ip.tilt_angle, diff --git a/src/pyFAI/test/test_fiber_integrator.py b/src/pyFAI/test/test_fiber_integrator.py index 99406f46e..013815fcd 100644 --- a/src/pyFAI/test/test_fiber_integrator.py +++ b/src/pyFAI/test/test_fiber_integrator.py @@ -43,6 +43,8 @@ from ..integrator.azimuthal import AzimuthalIntegrator from ..detectors import detector_factory from ..units import get_unit_fiber +from ..units import parse_fiber_unit +from ..units import UnitFiber from ..test.utilstest import UtilsTest from .. import load @@ -91,6 +93,101 @@ def test_instantiation(self): self.assertEqual(abs(res2d_load.azimuthal - res2d_from_ai.azimuthal).max(), 0) self.assertEqual(abs(res2d_load.intensity - res2d_from_ai.intensity).max(), 0) + + def test_parse_units(self): + gi_parameters_default = {"incident_angle" : 0.0, + "tilt_angle" : 0.0, + "sample_orientation" : 1, + } + gi_parameters_1 = {"incident_angle" : 0.2, + "tilt_angle" : 0.4, + "sample_orientation" : 2, + } + + qip_str_1 = parse_fiber_unit(unit='qip_nm^-1') + qoop_str_1 = parse_fiber_unit(unit='qoop_nm^-1') + qip_str_2 = parse_fiber_unit(unit='qip_nm^-1', **gi_parameters_1) + qoop_str_2 = parse_fiber_unit(unit='qoop_nm^-1', **gi_parameters_1) + + qip_unit_1 = parse_fiber_unit(unit=get_unit_fiber(name='qip_A^-1')) + qoop_unit_1 = parse_fiber_unit(unit=get_unit_fiber(name='qoop_A^-1')) + qip_unit_2 = parse_fiber_unit(unit=get_unit_fiber(name='qip_A^-1'), **gi_parameters_1) + qoop_unit_2 = parse_fiber_unit(unit=get_unit_fiber(name='qoop_A^-1'), **gi_parameters_1) + + self.assertIsInstance(qip_str_1, UnitFiber) + self.assertIsInstance(qoop_str_1, UnitFiber) + for k,v in gi_parameters_default.items(): + self.assertEqual(getattr(qip_str_1, k), v) + self.assertEqual(getattr(qoop_str_1, k), v) + + self.assertIsInstance(qip_str_2, UnitFiber) + self.assertIsInstance(qoop_str_2, UnitFiber) + for k,v in gi_parameters_1.items(): + self.assertEqual(getattr(qip_str_2, k), v) + self.assertEqual(getattr(qoop_str_2, k), v) + + self.assertIsInstance(qip_unit_1, UnitFiber) + self.assertIsInstance(qoop_unit_1, UnitFiber) + for k,v in gi_parameters_default.items(): + self.assertEqual(getattr(qip_unit_1, k), v) + self.assertEqual(getattr(qoop_unit_1, k), v) + + self.assertIsInstance(qip_unit_2, UnitFiber) + self.assertIsInstance(qoop_unit_2, UnitFiber) + for k,v in gi_parameters_1.items(): + self.assertEqual(getattr(qip_unit_2, k), v) + self.assertEqual(getattr(qoop_unit_2, k), v) + + def test_parse_wrong_units(self): + correct = parse_fiber_unit(unit='qip_nm^-1') + def wrong(): + _ = parse_fiber_unit(unit='q_nm^-1') + + self.assertRaises(Exception, wrong) + + def test_unique_units(self): + gi_parameters_1 = {"incident_angle" : 0.2, + "tilt_angle" : 0.4, + "sample_orientation" : 2, + } + + gi_parameters_2 = {"incident_angle" : 0.6, + "tilt_angle" : 0.9, + "sample_orientation" : 3, + } + + gi_parameters_default = {"incident_angle" : 0.0, + "tilt_angle" : 0.0, + "sample_orientation" : 1, + } + + qip_1 = get_unit_fiber(name='qip_nm^-1', **gi_parameters_1) + qoop_1 = get_unit_fiber(name='qoop_nm^-1', **gi_parameters_1) + + qip_2 = get_unit_fiber(name='qip_nm^-1') + qoop_2 = get_unit_fiber(name='qoop_nm^-1') + qip_2.set_incident_angle(gi_parameters_2['incident_angle']) + qoop_2.set_incident_angle(gi_parameters_2['incident_angle']) + qip_2.set_tilt_angle(gi_parameters_2['tilt_angle']) + qoop_2.set_tilt_angle(gi_parameters_2['tilt_angle']) + qip_2.set_sample_orientation(gi_parameters_2['sample_orientation']) + qoop_2.set_sample_orientation(gi_parameters_2['sample_orientation']) + + for k,v in gi_parameters_1.items(): + self.assertEqual(getattr(qip_1, k), v) + self.assertEqual(getattr(qoop_1, k), v) + + for k,v in gi_parameters_2.items(): + self.assertEqual(getattr(qip_2, k), v) + self.assertEqual(getattr(qoop_2, k), v) + + qip_3 = get_unit_fiber(name='qip_nm^-1') + qoop_3 = get_unit_fiber(name='qip_nm^-1') + + for k,v in gi_parameters_default.items(): + self.assertEqual(getattr(qip_3, k), v) + self.assertEqual(getattr(qoop_3, k), v) + def test_integrate2d_default(self): res2d_ref = self.fi.integrate2d_grazing_incidence(data=self.data) res2d_parameters = self.fi.integrate2d_grazing_incidence(data=self.data, diff --git a/src/pyFAI/units.py b/src/pyFAI/units.py index 5c4515d9c..2a9c5a3ea 100644 --- a/src/pyFAI/units.py +++ b/src/pyFAI/units.py @@ -993,8 +993,30 @@ def get_unit_fiber(name, incident_angle:float =0.0, tilt_angle:float =0.0, sampl """ unit = copy.deepcopy(RADIAL_UNITS.get(name, None)) - if unit is not None: + if isinstance(unit, UnitFiber): unit.set_incident_angle(incident_angle) unit.set_tilt_angle(tilt_angle) unit.set_sample_orientation(sample_orientation) return unit + +def parse_fiber_unit(unit, incident_angle=None, tilt_angle=None, sample_orientation=None): + if isinstance(unit, str): + unit = get_unit_fiber(name=unit) + elif isinstance(unit, UnitFiber): + pass + else: + unit = to_unit(unit) + + if not isinstance(unit, UnitFiber): + raise Exception(f"{unit} cannot be used as a FiberUnit") + + if incident_angle is not None: + unit.set_incident_angle(incident_angle) + + if tilt_angle is not None: + unit.set_tilt_angle(tilt_angle) + + if sample_orientation is not None: + unit.set_sample_orientation(sample_orientation) + + return unit From 28bf9c57cf567f72698c8d2ca27017da74669314 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 13 Nov 2024 13:51:42 +0000 Subject: [PATCH 4/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/pyFAI/integrator/fiber.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pyFAI/integrator/fiber.py b/src/pyFAI/integrator/fiber.py index 8cc15a6ad..8b81736b0 100644 --- a/src/pyFAI/integrator/fiber.py +++ b/src/pyFAI/integrator/fiber.py @@ -180,7 +180,7 @@ def integrate_fiber(self, data, oop_range = deprecated_params.get('oop_range', None) or oop_range ip_range = deprecated_params.get('ip_range', None) or ip_range vertical_integration = deprecated_params.get('vertical_integration', None) or vertical_integration - + unit_ip = unit_ip or 'qip_nm^-1' unit_oop = unit_oop or 'qoop_nm^-1' unit_ip = parse_fiber_unit(unit=unit_ip, sample_orientation=sample_orientation) @@ -392,7 +392,7 @@ def integrate2d_fiber(self, data, if (isinstance(method, (tuple, list)) and method[0] != "no") or (isinstance(method, IntegrationMethod) and method.split != "no"): logger.warning(f"Method {method} is using a pixel-splitting scheme. GI integration should be use WITHOUT PIXEL-SPLITTING! The results could be wrong!") - + return self.integrate2d_ng(data, npt_rad=npt_ip, npt_azim=npt_oop, correctSolidAngle=correctSolidAngle, mask=mask, dummy=dummy, delta_dummy=delta_dummy, From 06622cf88a4ddea7f3e18ab6e6afa7e9a15a4cc9 Mon Sep 17 00:00:00 2001 From: Edgar Gutierrez Fernandez Date: Wed, 13 Nov 2024 15:12:47 +0100 Subject: [PATCH 5/5] deprecated params out of class --- src/pyFAI/integrator/fiber.py | 108 +++++++++++++++++----------------- 1 file changed, 54 insertions(+), 54 deletions(-) diff --git a/src/pyFAI/integrator/fiber.py b/src/pyFAI/integrator/fiber.py index 8b81736b0..d92eb022e 100644 --- a/src/pyFAI/integrator/fiber.py +++ b/src/pyFAI/integrator/fiber.py @@ -44,6 +44,56 @@ from ..units import parse_fiber_unit from ..utils.decorators import deprecated_warning +def get_deprecated_params_1d(**kwargs) -> dict: + deprecated = {} + if "npt_output" in kwargs: + deprecated_warning(type_=type(kwargs["npt_output"]), name="npt_output", replacement=("npt_oop, npt_ip, vertical_integration instead"), since_version="2024.11/12") + deprecated['npt_oop'] = kwargs["npt_output"] + deprecated['vertical_integration'] = True + if "npt_integrated" in kwargs: + deprecated_warning(type_=type(kwargs["npt_integrated"]), name="npt_integrated", replacement=("npt_oop, npt_ip, vertical_integration instead"), since_version="2024.11/12") + deprecated['npt_ip'] = kwargs["npt_integrated"] + deprecated['vertical_integration'] = True + if "output_unit" in kwargs: + deprecated_warning(type_=type(kwargs["output_unit"]), name="output_unit", replacement=("unit_oop, unit_ip, vertical_integration instead"), since_version="2024.11/12") + deprecated['unit_oop'] = kwargs["output_unit"] + deprecated['vertical_integration'] = True + if "integrated_unit" in kwargs: + deprecated_warning(type_=type(kwargs["integrated_unit"]), name="integrated_unit", replacement=("unit_oop, unit_ip, vertical_integration instead"), since_version="2024.11/12") + deprecated['unit_ip'] = kwargs["integrated_unit"] + deprecated['vertical_integration'] = True + if "output_unit_range" in kwargs: + deprecated_warning(type_=type(kwargs["output_unit_range"]), name="output_unit_range", replacement=("oop_range, ip_range, vertical_integration instead"), since_version="2024.11/12") + deprecated['oop_range'] = kwargs["output_unit_range"] + deprecated['vertical_integration'] = True + if "integrated_unit_range" in kwargs: + deprecated_warning(type_=type(kwargs["integrated_unit_range"]), name="integrated_unit_range", replacement=("oop_range, ip_range, vertical_integration instead"), since_version="2024.11/12") + deprecated['ip_range'] = kwargs["integrated_unit_range"] + deprecated['vertical_integration'] = True + return deprecated + +def get_deprecated_params_2d(**kwargs) -> dict: + deprecated = {} + if "npt_horizontal" in kwargs: + deprecated_warning(type_=type(kwargs["npt_horizontal"]), name="npt_horizontal", replacement="npt_ip", since_version="2024.11/12") + deprecated['npt_ip'] = kwargs["npt_horizontal"] + if "npt_vertical" in kwargs: + deprecated_warning(type_=type(kwargs["npt_vertical"]), name="npt_vertical", replacement="npt_oop", since_version="2024.11/12") + deprecated['npt_oop'] = kwargs["npt_vertical"] + if "horizontal_unit" in kwargs: + deprecated_warning(type_=type(kwargs["horizontal_unit"]), name="horizontal_unit", replacement="unit_ip", since_version="2024.11/12") + deprecated['unit_ip'] = kwargs["horizontal_unit"] + if "vertical_unit" in kwargs: + deprecated_warning(type_=type(kwargs["vertical_unit"]), name="vertical_unit", replacement="unit_oop", since_version="2024.11/12") + deprecated['unit_oop'] = kwargs["vertical_unit"] + if "horizontal_unit_range" in kwargs: + deprecated_warning(type_=type(kwargs["horizontal_unit_range"]), name="horizontal_unit_range", replacement="ip_range", since_version="2024.11/12") + deprecated['ip_range'] = kwargs["horizontal_unit_range"] + if "vertical_unit_range" in kwargs: + deprecated_warning(type_=type(kwargs["vertical_unit_range"]), name="vertical_unit_range", replacement="oop_range", since_version="2024.11/12") + deprecated['oop_range'] = kwargs["vertical_unit_range"] + return deprecated + class FiberIntegrator(AzimuthalIntegrator): def __init__(self, *args, **kwargs): @@ -82,56 +132,6 @@ def reset_integrator(self, incident_angle, tilt_angle, sample_orientation): self._cache_parameters['tilt_angle'] = tilt_angle self._cache_parameters['sample_orientation'] = sample_orientation - def _get_deprecated_params_1d(self, **kwargs) -> dict: - deprecated = {} - if "npt_output" in kwargs: - deprecated_warning(type_=type(kwargs["npt_output"]), name="npt_output", replacement=("npt_oop, npt_ip, vertical_integration instead"), since_version="2024.11/12") - deprecated['npt_oop'] = kwargs["npt_output"] - deprecated['vertical_integration'] = True - if "npt_integrated" in kwargs: - deprecated_warning(type_=type(kwargs["npt_integrated"]), name="npt_integrated", replacement=("npt_oop, npt_ip, vertical_integration instead"), since_version="2024.11/12") - deprecated['npt_ip'] = kwargs["npt_integrated"] - deprecated['vertical_integration'] = True - if "output_unit" in kwargs: - deprecated_warning(type_=type(kwargs["output_unit"]), name="output_unit", replacement=("unit_oop, unit_ip, vertical_integration instead"), since_version="2024.11/12") - deprecated['unit_oop'] = kwargs["output_unit"] - deprecated['vertical_integration'] = True - if "integrated_unit" in kwargs: - deprecated_warning(type_=type(kwargs["integrated_unit"]), name="integrated_unit", replacement=("unit_oop, unit_ip, vertical_integration instead"), since_version="2024.11/12") - deprecated['unit_ip'] = kwargs["integrated_unit"] - deprecated['vertical_integration'] = True - if "output_unit_range" in kwargs: - deprecated_warning(type_=type(kwargs["output_unit_range"]), name="output_unit_range", replacement=("oop_range, ip_range, vertical_integration instead"), since_version="2024.11/12") - deprecated['oop_range'] = kwargs["output_unit_range"] - deprecated['vertical_integration'] = True - if "integrated_unit_range" in kwargs: - deprecated_warning(type_=type(kwargs["integrated_unit_range"]), name="integrated_unit_range", replacement=("oop_range, ip_range, vertical_integration instead"), since_version="2024.11/12") - deprecated['ip_range'] = kwargs["integrated_unit_range"] - deprecated['vertical_integration'] = True - return deprecated - - def _get_deprecated_params_2d(self, **kwargs) -> dict: - deprecated = {} - if "npt_horizontal" in kwargs: - deprecated_warning(type_=type(kwargs["npt_horizontal"]), name="npt_horizontal", replacement="npt_ip", since_version="2024.11/12") - deprecated['npt_ip'] = kwargs["npt_horizontal"] - if "npt_vertical" in kwargs: - deprecated_warning(type_=type(kwargs["npt_vertical"]), name="npt_vertical", replacement="npt_oop", since_version="2024.11/12") - deprecated['npt_oop'] = kwargs["npt_vertical"] - if "horizontal_unit" in kwargs: - deprecated_warning(type_=type(kwargs["horizontal_unit"]), name="horizontal_unit", replacement="unit_ip", since_version="2024.11/12") - deprecated['unit_ip'] = kwargs["horizontal_unit"] - if "vertical_unit" in kwargs: - deprecated_warning(type_=type(kwargs["vertical_unit"]), name="vertical_unit", replacement="unit_oop", since_version="2024.11/12") - deprecated['unit_oop'] = kwargs["vertical_unit"] - if "horizontal_unit_range" in kwargs: - deprecated_warning(type_=type(kwargs["horizontal_unit_range"]), name="horizontal_unit_range", replacement="ip_range", since_version="2024.11/12") - deprecated['ip_range'] = kwargs["horizontal_unit_range"] - if "vertical_unit_range" in kwargs: - deprecated_warning(type_=type(kwargs["vertical_unit_range"]), name="vertical_unit_range", replacement="oop_range", since_version="2024.11/12") - deprecated['oop_range'] = kwargs["vertical_unit_range"] - return deprecated - def integrate_fiber(self, data, npt_oop=None, unit_oop=None, oop_range=None, @@ -172,7 +172,7 @@ def integrate_fiber(self, data, :return: chi bins center positions and regrouped intensity :rtype: Integrate1dResult """ - deprecated_params = self._get_deprecated_params_1d(**kwargs) + deprecated_params = get_deprecated_params_1d(**kwargs) npt_oop = deprecated_params.get('npt_oop', None) or npt_oop npt_ip = deprecated_params.get('npt_ip', None) or npt_ip unit_oop = deprecated_params.get('unit_oop', None) or unit_oop @@ -303,7 +303,7 @@ def integrate_grazing_incidence(self, data, :return: chi bins center positions and regrouped intensity :rtype: Integrate1dResult """ - deprecated_params = self._get_deprecated_params_1d(**kwargs) + deprecated_params = get_deprecated_params_1d(**kwargs) npt_oop = deprecated_params.get('npt_oop', None) or npt_oop npt_ip = deprecated_params.get('npt_ip', None) or npt_ip unit_oop = deprecated_params.get('unit_oop', None) or unit_oop @@ -373,7 +373,7 @@ def integrate2d_fiber(self, data, :return: regrouped intensity and unit arrays :rtype: Integrate2dResult """ - deprecated_params = self._get_deprecated_params_2d(**kwargs) + deprecated_params = get_deprecated_params_2d(**kwargs) npt_oop = deprecated_params.get('npt_oop', None) or npt_oop npt_ip = deprecated_params.get('npt_ip', None) or npt_ip unit_oop = deprecated_params.get('unit_oop', None) or unit_oop @@ -442,7 +442,7 @@ def integrate2d_grazing_incidence(self, data, :return: regrouped intensity and unit arrays :rtype: Integrate2dResult """ - deprecated_params = self._get_deprecated_params_2d(**kwargs) + deprecated_params = get_deprecated_params_2d(**kwargs) npt_oop = deprecated_params.get('npt_oop', None) or npt_oop npt_ip = deprecated_params.get('npt_ip', None) or npt_ip unit_oop = deprecated_params.get('unit_oop', None) or unit_oop