Skip to content

Commit

Permalink
Merge pull request #2114 from EdgarGF93/2113_diffmap_variables
Browse files Browse the repository at this point in the history
2113 diffmap variables
  • Loading branch information
kif authored Apr 2, 2024
2 parents 829ef3a + aaf3b9a commit f22eea3
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/pyFAI/app/diff_tomo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
128 changes: 91 additions & 37 deletions src/pyFAI/diffmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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')
Expand All @@ -62,23 +63,36 @@ 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,
**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.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

# 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))
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
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
Expand All @@ -100,8 +114,8 @@ def __init__(self, npt_fast=0, npt_slow=1, npt_rad=1000, npt_azim=None):
# method is a property from worker

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)
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
def to_tuple(name):
Expand Down Expand Up @@ -277,23 +291,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)
Expand Down Expand Up @@ -364,11 +378,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"
Expand All @@ -386,37 +400,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"

Expand Down Expand Up @@ -514,7 +528,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):
"""
Expand Down Expand Up @@ -630,7 +644,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

Expand All @@ -651,3 +665,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
25 changes: 15 additions & 10 deletions src/pyFAI/gui/diffmap_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
}
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit f22eea3

Please sign in to comment.