Commit 8e678ca3 authored by Thomas Vincent's avatar Thomas Vincent

Add possibility to use a mask when binning and median filter are

disabled
parent 8e10bf09
......@@ -106,6 +106,12 @@ class QSpaceConverter(object):
normalizer = property(lambda self: self.__params['normalizer'])
""" Selected normalizer name in measurement group (str) or None """
mask = property(lambda self: self.__params['mask'])
""" Mask to apply on images (2D numpy.ndarray) or None.
A non-zero value means that the pixel is masked.
"""
def __init__(self,
xsocsH5_f,
qspace_dims=None,
......@@ -163,6 +169,7 @@ class QSpaceConverter(object):
''.format('\n -'.join(diff)))
self.__params = {'qspace_dims': None,
'mask': None,
'normalizer': None,
'image_binning': None,
'sample_indices': None,
......@@ -310,16 +317,32 @@ class QSpaceConverter(object):
normalizer = str(normalizer)
# Check for valid input in all entries
xsocsH5 = XsocsH5.XsocsH5(self.__xsocsH5_f)
for entry in xsocsH5.entries():
if xsocsH5.measurement(
entry=entry, measurement=normalizer) is None:
raise ValueError(
'normalizer %s is not available in measurement group of entry %s' %
normalizer, entry)
with XsocsH5.XsocsH5(self.__xsocsH5_f) as xsocsH5:
for entry in xsocsH5.entries():
if xsocsH5.measurement(
entry=entry, measurement=normalizer) is None:
raise ValueError(
'normalizer %s is not available in measurement group of entry %s' %
normalizer, entry)
self.__params['normalizer'] = normalizer
@mask.setter
def mask(self, mask):
"""Mask array or None to mask pixels in input images"""
if mask is not None:
mask = np.array(mask)
if mask.ndim != 2:
raise ValueError('Mask is not an image')
# TODO this might be a problem when saving a subset of the images
with XsocsH5.XsocsH5(self.__xsocsH5_f) as xsocsH5:
image_size = xsocsH5.image_size()
if image_size != mask.shape:
raise ValueError('Mask has not the size of the images')
self.__params['mask'] = mask
@image_binning.setter
def image_binning(self, image_binning):
"""
......@@ -667,6 +690,23 @@ class QSpaceConverter(object):
raise ValueError('Invalid image size (img_size={0}).'
''.format(img_size))
mask = self.mask
if mask is not None:
if np.count_nonzero(mask) == mask.size:
# Mask is empty, disable mask
mask = None
else:
if mask.shape != img_size:
raise ValueError('Invalid mask size')
if (image_binning is not None and
not np.all(np.equal(image_binning, (1, 1)))):
raise ValueError(
'Image binning is not implemented with mask')
if (medfilt_dims is not None and
not np.all(np.equal(medfilt_dims, (1, 1)))):
raise ValueError(
'Median filter is not implemented with mask')
shiftH5 = self.__shiftH5
if shiftH5:
......@@ -691,6 +731,8 @@ class QSpaceConverter(object):
print('\t- beam energy : {0}'.format(beam_energy))
print('\t- center chan : {0}'.format(center_chan))
print('\t- chan per deg : {0}'.format(chan_per_deg))
print('\t- mask : {0}'.format(
'Yes' if mask is not None else 'No'))
print('\t- normalizer : {0}'.format(normalizer))
print('\t- img binning : {0}'.format(image_binning))
print('\t- medfilt dims : {0}'.format(medfilt_dims))
......@@ -776,6 +818,9 @@ class QSpaceConverter(object):
'be of the same type. Found {0} and {1}.'
''.format(img_dtype, entry_dtype))
# Mark masked pixels (i.e., non zero in the mask) with NaN
q_ar[:, mask.reshape(-1) != 0, :] = np.nan
# custom bins range to have the same histo as
# xrayutilities.gridder3d
# bins centered around the qx, qy, qz
......@@ -784,12 +829,12 @@ class QSpaceConverter(object):
# bin_2 = [min - step/2, min + 3*step/2]
# ...
# bin_N = [max - step/2, max + step/2]
qx_min = q_ar[:, :, 0].min()
qy_min = q_ar[:, :, 1].min()
qz_min = q_ar[:, :, 2].min()
qx_max = q_ar[:, :, 0].max()
qy_max = q_ar[:, :, 1].max()
qz_max = q_ar[:, :, 2].max()
qx_min = np.nanmin(q_ar[:, :, 0])
qy_min = np.nanmin(q_ar[:, :, 1])
qz_min = np.nanmin(q_ar[:, :, 2])
qx_max = np.nanmax(q_ar[:, :, 0])
qy_max = np.nanmax(q_ar[:, :, 1])
qz_max = np.nanmax(q_ar[:, :, 2])
step_x = (qx_max - qx_min) / (nx - 1.)
step_y = (qy_max - qy_min) / (ny - 1.)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment