Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for single images in PyFAI integrators #78

Merged
merged 11 commits into from
Apr 4, 2024
Merged
15 changes: 10 additions & 5 deletions src/PyHyperScattering/PFEnergySeriesIntegrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def integrateSingleImage(self,img):
# get the energy and locate the matching integrator
# use that integrator to reduce
# return single reduced frame

#print(f' img.energy is a {type(img.energy)}, len = {len(img.energy)} and is {img.energy}')
#print(f' img.system is a {type(img.system)}, len = {len(img.energy)} and is {img.system}')
#print(f' img.system.levels: {img.indexes["system"].names}')
Expand Down Expand Up @@ -75,10 +76,12 @@ def integrateSingleImage(self,img):
except IndexError:
en = float(img.energy)
except AttributeError:
en = img.energy[0]
warnings.warn(f'Using the first energy value of {img.energy}, check that this is correct.',stacklevel=2)
try:
en = img.energy[0]
warnings.warn(f'Using the first energy value of {img.energy}, if this contains more than one energy, your data are likely wrong.',stacklevel=2)
except IndexError:
en = float(img.energy)


try:
self.integrator = self.integrator_stack[en]
except KeyError:
Expand All @@ -89,7 +92,7 @@ def integrateSingleImage(self,img):
return res.interp(q=self.dest_q)
else:
return res
except TypeError:
except (TypeError,AttributeError):
pbeaucage marked this conversation as resolved.
Show resolved Hide resolved
return res
def setupIntegrators(self,energies):
'''
Expand Down Expand Up @@ -122,7 +125,7 @@ def integrateImageStack_dask(self,img_stack,chunksize=5):
# idx_name_to_use = 'energy'#indexes[0]
# idx_val_to_use = img_stack.indexes[idx_name_to_use]


if 'energy' in indexes:
dim_to_chunk = 'energy'
else:
Expand Down Expand Up @@ -196,6 +199,8 @@ def integrateImageStack_legacy(self,img_stack):
if img_stack.__getattr__(indexes[0]).to_pandas().drop_duplicates().shape[0] != img_stack.__getattr__(indexes[0]).shape[0]:
warnings.warn(f'Axis {indexes[0]} contains duplicate conditions. This is not supported and may not work. Try adding additional coords to separate image conditions',stacklevel=2)
data_int = data.groupby(indexes[0],squeeze=False).progress_apply(self.integrateSingleImage)
elif len(indexes) == 0:
data_int = self.integrateSingleImage(data).isel(image_num=0)
else:
#some kinda logic to check for existing multiindexes and stack into them appropriately maybe
data = data.stack({'pyhyper_internal_multiindex':indexes})
Expand Down
8 changes: 8 additions & 0 deletions src/PyHyperScattering/PFGeneralIntegrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ def integrateImageStack_legacy(self,data):

if len(indexes) == 1:
data_int = data.groupby(indexes[0],squeeze=False).progress_map(self.integrateSingleImage)
elif len(indexes) == 0:
data_int = self.integrateSingleImage(data).isel(image_num=0)
else:
#some kinda logic to check for existing multiindexes and stack into them appropriately maybe
data = data.stack({'pyhyper_internal_multiindex':indexes})
Expand Down Expand Up @@ -471,6 +473,12 @@ def calibrationFromTemplateXRParams(self, raw_xr):
self.pixel1 = raw_xr.pixel1
self.pixel2 = raw_xr.pixel2

try:
self.energy = float(raw_xr.energy)
except TypeError:
pass


if self.mask is None:
self.mask = np.zeros((len(raw_xr.pix_y),len(raw_xr.pix_x)))
warnings.warn(f'Since mask was none, creating an empty mask with shape {self.mask.shape}',stacklevel=2)
Expand Down
Loading