From 02829e8e901922a4f971190b48905c486a8480a9 Mon Sep 17 00:00:00 2001 From: edgar Date: Mon, 25 Mar 2024 16:53:42 +0100 Subject: [PATCH 1/4] general instantiation of Diffmap and unified variables --- src/pyFAI/app/diff_tomo.py | 2 +- src/pyFAI/diffmap.py | 70 ++++++++++++++++----------------- src/pyFAI/gui/diffmap_widget.py | 25 +++++++----- 3 files changed, 51 insertions(+), 46 deletions(-) diff --git a/src/pyFAI/app/diff_tomo.py b/src/pyFAI/app/diff_tomo.py index 0e576b5a1..55125bae1 100755 --- a/src/pyFAI/app/diff_tomo.py +++ b/src/pyFAI/app/diff_tomo.py @@ -68,7 +68,7 @@ def __init__(self, nTrans=1, nRot=1, nDiff=1000): :param nRot: number of translations :param nDiff: number of points in diffraction pattern """ - DiffMap.__init__(self, npt_slow=nRot, npt_fast=nTrans, npt_rad=nDiff) + DiffMap.__init__(self, nbpt_slow=nRot, nbpt_fast=nTrans, nbpt_rad=nDiff) self.hdf5path = "diff_tomo/data/sinogram" self.experiment_title = "Diffraction Tomography" self.slow_motor_name = "rotation" diff --git a/src/pyFAI/diffmap.py b/src/pyFAI/diffmap.py index b8de78d43..17d5f2686 100644 --- a/src/pyFAI/diffmap.py +++ b/src/pyFAI/diffmap.py @@ -62,7 +62,7 @@ class DiffMap(object): Basic class for diffraction mapping experiment using pyFAI """ - def __init__(self, npt_fast=0, npt_slow=1, npt_rad=1000, npt_azim=None): + def __init__(self, nbpt_fast=0, nbpt_slow=1, nbpt_rad=1000, nbpt_azim=None): """Constructor of the class DiffMap for diffraction mapping :param npt_fast: number of translations @@ -70,15 +70,15 @@ def __init__(self, npt_fast=0, npt_slow=1, npt_rad=1000, npt_azim=None): :param npt_rad: number of points in diffraction pattern (radial dimension) :param npt_azim: number of points in diffraction pattern (azimuthal dimension) """ - self.npt_fast = npt_fast - self.npt_slow = npt_slow - self.npt_rad = npt_rad - self.npt_azim = npt_azim + self.nbpt_fast = nbpt_fast + self.nbpt_slow = nbpt_slow + self.nbpt_rad = nbpt_rad + self.nbpt_azim = nbpt_azim self.slow_motor_name = "slow" self.fast_motor_name = "fast" self.offset = 0 self.poni = None - self.worker = Worker(unit="2th_deg", shapeOut=(1, npt_rad)) + self.worker = Worker(unit="2th_deg", shapeOut=(1, nbpt_rad)) self.worker.output = "raw" # exchange IntegrateResults, not numpy arrays self.dark = None self.flat = None @@ -101,7 +101,7 @@ def __init__(self, npt_fast=0, npt_slow=1, npt_rad=1000, npt_azim=None): def __repr__(self): return "%s experiment with ntp_slow: %s ntp_fast: %s, npt_diff: %s" % \ - (self.experiment_title, self.npt_slow, self.npt_fast, self.npt_rad) + (self.experiment_title, self.nbpt_slow, self.nbpt_fast, self.nbpt_rad) @staticmethod def to_tuple(name): @@ -277,23 +277,23 @@ def parse(self, sysargv=None, with_config=False): else: logger.warning("No such poni file %s", options.poni) if options.fast is None: - self.npt_fast = config.get("fast_motor_points", self.npt_fast) + self.nbpt_fast = config.get("nbpt_fast", self.nbpt_fast) else: - self.npt_fast = int(options.fast) - config["fast_motor_points"] = self.npt_fast + self.nbpt_fast = int(options.fast) + config["nbpt_fast"] = self.nbpt_fast if options.slow is None: - self.npt_slow = config.get("slow_motor_points", self.npt_slow) + self.nbpt_slow = config.get("nbpt_slow", self.nbpt_slow) else: - self.npt_slow = int(options.slow) - config["slow_motor_points"] = self.npt_slow + self.nbpt_slow = int(options.slow) + config["nbpt_slow"] = self.nbpt_slow if options.npt_rad is not None: - ai["nbpt_rad"] = self.npt_rad = int(options.npt_rad) + ai["nbpt_rad"] = self.nbpt_rad = int(options.npt_rad) elif "nbpt_rad" in ai: - self.npt_rad = ai["nbpt_rad"] + self.nbpt_rad = ai["nbpt_rad"] if options.npt_azim is not None: - ai["nbpt_azim"] = self.npt_azim = int(options.npt_azim) + ai["nbpt_azim"] = self.nbpt_azim = int(options.npt_azim) elif "nbpt_azim" in ai: - self.npt_azim = ai["nbpt_azim"] + self.nbpt_azim = ai["nbpt_azim"] if options.offset is not None: self.offset = int(options.offset) @@ -364,11 +364,11 @@ def makeHDF5(self, rewrite=False): process_grp["PONIfile"] = self.poni process_grp["inputfiles"] = numpy.array([i for i in self.inputfiles], dtype=dtype) - process_grp["dim0"] = self.npt_slow + process_grp["dim0"] = self.nbpt_slow process_grp["dim0"].attrs["axis"] = self.slow_motor_name - process_grp["dim1"] = self.npt_fast + process_grp["dim1"] = self.nbpt_fast process_grp["dim1"].attrs["axis"] = self.fast_motor_name - process_grp["dim2"] = self.npt_rad + process_grp["dim2"] = self.nbpt_rad process_grp["dim2"].attrs["axis"] = "diffraction" config = nxs.new_class(process_grp, "configuration", "NXnote") config["type"] = "text/json" @@ -386,37 +386,37 @@ def makeHDF5(self, rewrite=False): if self.worker.do_2D(): self.dataset = self.nxdata_grp.create_dataset( name="intensity", - shape=(self.npt_slow, self.npt_fast, self.npt_azim, self.npt_rad), + shape=(self.nbpt_slow, self.nbpt_fast, self.nbpt_azim, self.nbpt_rad), dtype="float32", - chunks=(1, 1, self.npt_azim, self.npt_rad), - maxshape=(None, None, self.npt_azim, self.npt_rad), + chunks=(1, 1, self.nbpt_azim, self.nbpt_rad), + maxshape=(None, None, self.nbpt_azim, self.nbpt_rad), fillvalue=numpy.nan) self.dataset.attrs["interpretation"] = "image" self.nxdata_grp.attrs["axes"] = [".", ".", "azimuthal", str(self.unit).split("_")[0]] # Build a transposed view to display the mapping experiment - layout = h5py.VirtualLayout(shape=(self.npt_azim, self.npt_rad, self.npt_slow, self.npt_fast), dtype=self.dataset.dtype) + layout = h5py.VirtualLayout(shape=(self.nbpt_azim, self.nbpt_rad, self.nbpt_slow, self.nbpt_fast), dtype=self.dataset.dtype) source = h5py.VirtualSource(self.dataset) - for i in range(self.npt_slow): - for j in range(self.npt_fast): + for i in range(self.nbpt_slow): + for j in range(self.nbpt_fast): layout[:,:, i, j] = source[i, j] self.nxdata_grp.create_virtual_dataset('map', layout, fillvalue=numpy.nan).attrs["interpretation"] = "image" else: - print(f"shape for dataset: {self.npt_slow}, {self.npt_fast}, {self.npt_rad}") + print(f"shape for dataset: {self.nbpt_slow}, {self.nbpt_fast}, {self.nbpt_rad}") self.dataset = self.nxdata_grp.create_dataset( name="intensity", - shape=(self.npt_slow, self.npt_fast, self.npt_rad), + shape=(self.nbpt_slow, self.nbpt_fast, self.nbpt_rad), dtype="float32", - chunks=(1, self.npt_fast, self.npt_rad), - maxshape=(None, None, self.npt_rad), + chunks=(1, self.nbpt_fast, self.nbpt_rad), + maxshape=(None, None, self.nbpt_rad), fillvalue=numpy.nan) self.dataset.attrs["interpretation"] = "spectrum" self.nxdata_grp.attrs["axes"] = [".", ".", str(self.unit).split("_")[0]] # Build a transposed view to display the mapping experiment - layout = h5py.VirtualLayout(shape=(self.npt_rad, self.npt_slow, self.npt_fast), dtype=self.dataset.dtype) + layout = h5py.VirtualLayout(shape=(self.nbpt_rad, self.nbpt_slow, self.nbpt_fast), dtype=self.dataset.dtype) source = h5py.VirtualSource(self.dataset) - for i in range(self.npt_slow): - for j in range(self.npt_fast): + for i in range(self.nbpt_slow): + for j in range(self.nbpt_fast): layout[:, i, j] = source[i, j] self.nxdata_grp.create_virtual_dataset('map', layout, fillvalue=numpy.nan).attrs["interpretation"] = "image" @@ -514,7 +514,7 @@ def get_pos(self, filename=None, idx=None): n = self.inputfiles.index(filename) - self.offset else: n = idx - self.offset - return Position(n, n // self.npt_fast, n % self.npt_fast) + return Position(n, n // self.nbpt_fast, n % self.nbpt_fast) def process_one_file(self, filename, callback=None): """ @@ -630,7 +630,7 @@ def ai(self): @ai.setter def ai(self, value): if self.worker is None: - self.worker = Worker(value, unit=self.unit, shapeOut=(1, self.npt_rad)) + self.worker = Worker(value, unit=self.unit, shapeOut=(1, self.nbpt_rad)) else: self.worker.ai = value diff --git a/src/pyFAI/gui/diffmap_widget.py b/src/pyFAI/gui/diffmap_widget.py index 5b2afcd5e..325d91ee4 100644 --- a/src/pyFAI/gui/diffmap_widget.py +++ b/src/pyFAI/gui/diffmap_widget.py @@ -382,8 +382,8 @@ def get_config(self): "experiment_title": str_(self.experimentTitle.text()).strip(), "fast_motor_name": str_(self.fastMotorName.text()).strip(), "slow_motor_name": str_(self.slowMotorName.text()).strip(), - "fast_motor_points": int_(self.fastMotorPts.text()), - "slow_motor_points": int_(self.slowMotorPts.text()), + "nbpt_fast": int_(self.fastMotorPts.text()), + "nbpt_slow": int_(self.slowMotorPts.text()), "offset": int_(self.offset.text()), "output_file": str_(self.outputFile.text()).strip(), "input_data": [i.as_tuple() for i in self.list_dataset] @@ -400,8 +400,8 @@ def set_config(self, dico): setup_data = {"experiment_title": self.experimentTitle.setText, "fast_motor_name": self.fastMotorName.setText, "slow_motor_name": self.slowMotorName.setText, - "fast_motor_points": lambda a: self.fastMotorPts.setText(str_(a)), - "slow_motor_points": lambda a: self.slowMotorPts.setText(str_(a)), + "nbpt_fast": lambda a: self.fastMotorPts.setText(str_(a)), + "nbpt_slow": lambda a: self.slowMotorPts.setText(str_(a)), "offset": lambda a: self.offset.setText(str_(a)), "output_file": self.outputFile.setText } @@ -464,10 +464,15 @@ def process(self, config=None): config = self.dump() config_ai = config.get("ai", {}) config_ai = config_ai.copy() - diffmap = DiffMap(npt_fast=config.get("fast_motor_points", 1), - npt_slow=config.get("slow_motor_points", 1), - npt_rad=config_ai.get("nbpt_rad", 1000), - npt_azim=config_ai.get("nbpt_azim", 1) if config_ai.get("do_2D") else None) + diffmap_kwargs = {} + + for key in ["nbpt_fast", "nbpt_slow", "nbpt_rad"]: + if key in config: + diffmap_kwargs[key] = config[key] + if config_ai.get("do_2D"): + diffmap_kwargs["nbpt_azim"] = config_ai.get("nbpt_azim", 1) + + diffmap = DiffMap(**diffmap_kwargs) diffmap.inputfiles = [i.path for i in self.list_dataset] # in case generic detector without shape diffmap.configure_worker(config_ai) diffmap.hdf5 = config.get("output_file", "unamed.h5") @@ -498,8 +503,8 @@ def display_processing(self, config): self.aximg = self.fig.add_subplot(1, 2, 1, xlabel=config.get("fast_motor_name", "Fast motor"), ylabel=config.get("slow_motor_name", "Slow motor"), - xlim=(-0.5, (config.get("fast_motor_points", 1) or 1) - 0.5), - ylim=(-0.5, (config.get("slow_motor_points", 1) or 1) - 0.5)) + xlim=(-0.5, (config.get("nbpt_fast", 1) or 1) - 0.5), + ylim=(-0.5, (config.get("nbpt_slow", 1) or 1) - 0.5)) self.aximg.set_title(config.get("experiment_title", "Diffraction imaging")) # print(config) self.axplt = self.fig.add_subplot(1, 2, 2, From ae48d72da7e8e28b7f94161c4d1c5615f2703a68 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 25 Mar 2024 15:57:20 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/pyFAI/gui/diffmap_widget.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pyFAI/gui/diffmap_widget.py b/src/pyFAI/gui/diffmap_widget.py index 325d91ee4..e10a6f251 100644 --- a/src/pyFAI/gui/diffmap_widget.py +++ b/src/pyFAI/gui/diffmap_widget.py @@ -471,7 +471,7 @@ def process(self, config=None): diffmap_kwargs[key] = config[key] if config_ai.get("do_2D"): diffmap_kwargs["nbpt_azim"] = config_ai.get("nbpt_azim", 1) - + diffmap = DiffMap(**diffmap_kwargs) diffmap.inputfiles = [i.path for i in self.list_dataset] # in case generic detector without shape diffmap.configure_worker(config_ai) From fedd3392008591c200bdd43f2983efeecdc888af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Kieffer?= Date: Mon, 1 Apr 2024 13:33:28 +0200 Subject: [PATCH 3/4] Deprecate former attributes/arguments --- src/pyFAI/diffmap.py | 61 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 58 insertions(+), 3 deletions(-) diff --git a/src/pyFAI/diffmap.py b/src/pyFAI/diffmap.py index 17d5f2686..146a39a01 100644 --- a/src/pyFAI/diffmap.py +++ b/src/pyFAI/diffmap.py @@ -31,7 +31,7 @@ __contact__ = "Jerome.Kieffer@ESRF.eu" __license__ = "MIT" __copyright__ = "European Synchrotron Radiation Facility, Grenoble, France" -__date__ = "22/03/2024" +__date__ = "01/04/2024" __status__ = "development" __docformat__ = 'restructuredtext' @@ -52,6 +52,7 @@ from .io import Nexus, get_isotime, h5py from .worker import Worker, _reduce_images from .method_registry import Method, IntegrationMethod +from .utils.decorators import deprecated, deprecated_warning DIGITS = [str(i) for i in range(10)] Position = collections.namedtuple('Position', 'index slow fast') @@ -62,18 +63,32 @@ class DiffMap(object): Basic class for diffraction mapping experiment using pyFAI """ - def __init__(self, nbpt_fast=0, nbpt_slow=1, nbpt_rad=1000, nbpt_azim=None): + def __init__(self, nbpt_fast=0, nbpt_slow=1, nbpt_rad=1000, nbpt_azim=None, + **kwargs): """Constructor of the class DiffMap for diffraction mapping :param npt_fast: number of translations :param npt_slow: number of translations :param npt_rad: number of points in diffraction pattern (radial dimension) :param npt_azim: number of points in diffraction pattern (azimuthal dimension) + :param kwargs: former variables named npt_fast, npt_slow, npt_rad, npt_azim which are now deprecated """ self.nbpt_fast = nbpt_fast self.nbpt_slow = nbpt_slow self.nbpt_rad = nbpt_rad self.nbpt_azim = nbpt_azim + + # handle deprecated attributes + deprecated_args = {"npt_fast", "npt_fast", "npt_rad", "npt_azim"} + for key in deprecated_args: + if (key in kwargs): + valid = key.replace("npt_", "nbpt_") + self.__setattr__(valid, kwargs.pop(key)) + print(key, valid) + deprecated_warning("Argument", key, replacement=valid) + if kwargs: + raise TypeError(f"DiffMap got unexpected kwargs: {', '.join(kwargs)}") + self.slow_motor_name = "slow" self.fast_motor_name = "fast" self.offset = 0 @@ -100,7 +115,7 @@ def __init__(self, nbpt_fast=0, nbpt_slow=1, nbpt_rad=1000, nbpt_azim=None): # method is a property from worker def __repr__(self): - return "%s experiment with ntp_slow: %s ntp_fast: %s, npt_diff: %s" % \ + return "%s experiment with nbpt_slow: %s nbpt_fast: %s, nbpt_diff: %s" % \ (self.experiment_title, self.nbpt_slow, self.nbpt_fast, self.nbpt_rad) @staticmethod @@ -651,3 +666,43 @@ def unit(self): @unit.setter def unit(self, value): self.worker.unit = value + + @property + @deprecated(replacement="nbpt_fast") + def npt_fast(self): + return self.nbpt_fast + + @npt_fast.setter + @deprecated(replacement="nbpt_fast") + def npt_fast(self, value): + self.nbpt_fast = value + + @property + @deprecated(replacement="nbpt_slow") + def npt_slow(self): + return self.nbpt_slow + + @npt_slow.setter + @deprecated(replacement="nbpt_slow") + def npt_slow(self, value): + self.nbpt_slow = value + + @property + @deprecated(replacement="nbpt_rad") + def npt_rad(self): + return self.nbpt_rad + + @npt_rad.setter + @deprecated(replacement="nbpt_rad") + def npt_rad(self, value): + self.nbpt_rad = value + + @property + @deprecated(replacement="nbpt_azim") + def npt_azim(self): + return self.nbpt_azim + + @npt_azim.setter + @deprecated(replacement="nbpt_azim") + def npt_azim(self, value): + self.nbpt_azim = value From aaf3b9a96727a91b591273dd4308c64e1e004636 Mon Sep 17 00:00:00 2001 From: Jerome Kieffer Date: Tue, 2 Apr 2024 17:25:36 +0200 Subject: [PATCH 4/4] remove debug print --- src/pyFAI/diffmap.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/pyFAI/diffmap.py b/src/pyFAI/diffmap.py index 146a39a01..81b665cdb 100644 --- a/src/pyFAI/diffmap.py +++ b/src/pyFAI/diffmap.py @@ -84,7 +84,6 @@ def __init__(self, nbpt_fast=0, nbpt_slow=1, nbpt_rad=1000, nbpt_azim=None, if (key in kwargs): valid = key.replace("npt_", "nbpt_") self.__setattr__(valid, kwargs.pop(key)) - print(key, valid) deprecated_warning("Argument", key, replacement=valid) if kwargs: raise TypeError(f"DiffMap got unexpected kwargs: {', '.join(kwargs)}")