Skip to content

Commit

Permalink
Merge pull request #1831 from kif/1825_worker_sigmaclip
Browse files Browse the repository at this point in the history
Enable sigma_clip_legacy to be used within a worker
  • Loading branch information
kif authored Feb 24, 2023
2 parents 0b3e1e5 + f9a3b8a commit 05b68fc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
36 changes: 19 additions & 17 deletions pyFAI/azimuthalIntegrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
__contact__ = "[email protected]"
__license__ = "MIT"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
__date__ = "06/01/2023"
__date__ = "22/02/2023"
__status__ = "stable"
__docformat__ = 'restructuredtext'

Expand Down Expand Up @@ -2899,21 +2899,21 @@ def medfilt1d(self, data, npt_rad=1024, npt_azim=512,
return result

def sigma_clip_legacy(self, data, npt_rad=1024, npt_azim=512,
correctSolidAngle=True, polarization_factor=None,
radial_range=None, azimuth_range=None,
dark=None, flat=None,
method="splitpixel", unit=units.Q,
thres=3, max_iter=5, dummy=None, delta_dummy=None,
mask=None, normalization_factor=1.0, metadata=None):
correctSolidAngle=True, polarization_factor=None,
radial_range=None, azimuth_range=None,
dark=None, flat=None,
method=("full", "histogram", "cython"), unit=units.Q,
thres=3, max_iter=5, dummy=None, delta_dummy=None,
mask=None, normalization_factor=1.0, metadata=None,
safe=True, **kwargs):
"""Perform first a 2D integration and then an iterative sigma-clipping
filter along each row. See the doc of scipy.stats.sigmaclip for the
options `thres` and `max_iter`.
:param data: input image as numpy array
:param npt_rad: number of radial points
:param npt_rad: number of radial points (alias: npt)
:param npt_azim: number of azimuthal points
:param bool correctSolidAngle: correct for solid angle of each pixel
if True
:param bool correctSolidAngle: correct for solid angle of each pixel when set
:param float polarization_factor: polarization factor between -1 (vertical)
and +1 (horizontal).
Expand All @@ -2928,17 +2928,22 @@ def sigma_clip_legacy(self, data, npt_rad=1024, npt_azim=512,
:param ndarray flat: flat field image
:param unit: unit to be used for integration
:param method: pathway for integration and sort
:param thres: cut-off for n*sigma: discard any values with `|I-<I>|/sigma > thres`.
:param thres: cut-off for n*sigma: discard any values with `|I-<I>| > thres`.
The threshold can be a 2-tuple with sigma_low and sigma_high.
:param max_iter: maximum number of iterations
:param mask: masked out pixels array
:param float normalization_factor: Value of a normalization monitor
:param metadata: any other metadata,
:type metadata: JSON serializable dict
:param safe: unset to save some checks on sparse matrix shape/content.
:return: Integrate1D-like result
Nota: The initial 2D-integration requires pixel splitting
"""
#compatibility layer with sigma_clip_ng
if "npt" in kwargs:
npt_rad = kwargs["npt"]

# We use NaN as dummies
if dummy is None:
dummy = numpy.NaN
Expand Down Expand Up @@ -2966,7 +2971,7 @@ def sigma_clip_legacy(self, data, npt_rad=1024, npt_azim=512,
dummy=dummy, delta_dummy=delta_dummy,
correctSolidAngle=correctSolidAngle,
polarization_factor=polarization_factor,
normalization_factor=normalization_factor)
normalization_factor=normalization_factor,safe=safe)
image = res2d.intensity
if (method.impl_lower == "opencl"):
if (method.algo_lower == "csr") and \
Expand Down Expand Up @@ -3079,7 +3084,7 @@ def sigma_clip_ng(self, data,
Keep only pixels with intensty:
``|I - <I>| < thres * std(I)``
``|I - <I>| < thres * σ(I)``
This enforces a symmetric, bell-shaped distibution (i.e. gaussian-like) and is very good at extracting
background or amorphous isotropic scattering out of Bragg peaks.
Expand Down Expand Up @@ -3326,10 +3331,7 @@ def sigma_clip_ng(self, data,
result._set_error_model(error_model)
return result

@deprecated(reason="will be replaced by `sigma_clip_ng` in version 0.23.0. Please use either `_sigma_clip_legacy` for full compatibility or upgrade your code to accomodate the new API",
replacement="sigma_clip_ng", since_version="0.21.0", only_once=True, skip_backtrace_count=1, deprecated_since="0.22.0")
def sigma_clip(self, *args, **kwargs):
return self._sigma_clip_legacy(*args, **kwargs)
sigma_clip = sigma_clip_ng

def separate(self, data, npt_rad=1024, npt_azim=512, unit="2th_deg", method="splitpixel",
percentile=50, mask=None, restore_mask=True):
Expand Down
14 changes: 13 additions & 1 deletion pyFAI/test/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
__contact__ = "[email protected]"
__license__ = "MIT"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
__date__ = "26/11/2022"
__date__ = "22/02/2023"

import unittest
import logging
Expand Down Expand Up @@ -307,8 +307,20 @@ def test_sigma_clip(self):
extra_options={"thres":2, "error_model":"azimuthal"},
integrator_name="sigma_clip_ng",
shapeOut=(1, 100))

self.assertEqual(worker.error_model, "azimuthal", "error model is OK")
img = numpy.random.random(ai.detector.shape)
worker(img)

def test_sigma_clip_legacy(self):
"Non regression test for #1825"
ai = AzimuthalIntegrator.sload({"detector": "Pilatus100k", "wavelength":1e-10})
worker = Worker(azimuthalIntegrator=ai,
extra_options={"thres":2},
integrator_name="sigma_clip_legacy",
shapeOut=(1, 100))
img = numpy.random.random(ai.detector.shape)
worker(img)

class TestWorkerConfig(unittest.TestCase):

Expand Down

0 comments on commit 05b68fc

Please sign in to comment.