Skip to content

Commit

Permalink
Merge pull request #78 from usnistgov/56-integratesingleimage-indexin…
Browse files Browse the repository at this point in the history
…g-error

Add support for single images in PyFAI integrators
  • Loading branch information
pdudenas authored Apr 4, 2024
2 parents f11bcf5 + 3a79052 commit 1b43ab4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
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):
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
12 changes: 8 additions & 4 deletions src/PyHyperScattering/PFGeneralIntegrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ def integrateImageStack_legacy(self, data):
indexes.remove('pix_y')

if len(indexes) == 1:
data_int = data.groupby(indexes[0], squeeze=False).progress_map(
self.integrateSingleImage
)
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 @@ -545,7 +545,11 @@ def calibrationFromTemplateXRParams(self, raw_xr):
self.rot3 = raw_xr.rot3

self.pixel1 = raw_xr.pixel1
self.pixel2 = raw_xr.pixel2
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)))
Expand Down

0 comments on commit 1b43ab4

Please sign in to comment.