示例#1
0
def strip_bg(x, y0, width, niter):
    """Extract and return the strip bg from y0.

    Use anchors coordinates in CONFIG["AnchorsList"] if flag
    CONFIG["AnchorsFlag"] is True. Convert anchors from x coordinate
    to array index prior to passing it to silx.math.fit.filters.strip

    :param x: Abscissa array
    :param x: Ordinate array (data values at x positions)
    :param width: strip width
    :param niter: strip niter
    """
    global _BG_STRIP_OLDY
    global _BG_STRIP_OLDPARS
    global _BG_STRIP_OLDBG
    global _BG_SMOOTH_OLDWIDTH
    global _BG_SMOOTH_OLDFLAG
    global _BG_OLD_ANCHORS
    global _BG_OLD_ANCHORS_FLAG

    parameters_changed =\
        _BG_STRIP_OLDPARS != [width, niter] or\
        _BG_SMOOTH_OLDWIDTH != CONFIG["SmoothingWidth"] or\
        _BG_SMOOTH_OLDFLAG != CONFIG["SmoothingFlag"] or\
        _BG_OLD_ANCHORS_FLAG != CONFIG["AnchorsFlag"] or\
        _BG_OLD_ANCHORS != CONFIG["AnchorsList"]

    # same parameters
    if not parameters_changed:
        # same data
        if numpy.array_equal(_BG_STRIP_OLDY, y0):
            # same result
            return _BG_STRIP_OLDBG

    _BG_STRIP_OLDY = y0
    _BG_STRIP_OLDPARS = [width, niter]
    _BG_SMOOTH_OLDWIDTH = CONFIG["SmoothingWidth"]
    _BG_SMOOTH_OLDFLAG = CONFIG["SmoothingFlag"]
    _BG_OLD_ANCHORS = CONFIG["AnchorsList"]
    _BG_OLD_ANCHORS_FLAG = CONFIG["AnchorsFlag"]

    y1 = savitsky_golay(
        y0, CONFIG["SmoothingWidth"]) if CONFIG["SmoothingFlag"] else y0

    anchors_indices = _convert_anchors_to_indices(x)

    background = strip(y1,
                       w=width,
                       niterations=niter,
                       factor=CONFIG["StripThresholdFactor"],
                       anchors=anchors_indices)

    _BG_STRIP_OLDBG = background

    return background
示例#2
0
def snip_bg(x, y0, width):
    """Compute the snip bg for y0"""
    global _BG_SNIP_OLDY
    global _BG_SNIP_OLDWIDTH
    global _BG_SNIP_OLDBG
    global _BG_SMOOTH_OLDWIDTH
    global _BG_SMOOTH_OLDFLAG
    global _BG_OLD_ANCHORS
    global _BG_OLD_ANCHORS_FLAG

    parameters_changed =\
        _BG_SNIP_OLDWIDTH != width or\
        _BG_SMOOTH_OLDWIDTH != CONFIG["SmoothingWidth"] or\
        _BG_SMOOTH_OLDFLAG != CONFIG["SmoothingFlag"] or\
        _BG_OLD_ANCHORS_FLAG != CONFIG["AnchorsFlag"] or\
        _BG_OLD_ANCHORS != CONFIG["AnchorsList"]

    # same parameters
    if not parameters_changed:
        # same data
        if numpy.sum(_BG_SNIP_OLDY == y0) == len(y0):
            # same result
            return _BG_SNIP_OLDBG

    _BG_SNIP_OLDY = y0
    _BG_SNIP_OLDWIDTH = width
    _BG_SMOOTH_OLDWIDTH = CONFIG["SmoothingWidth"]
    _BG_SMOOTH_OLDFLAG = CONFIG["SmoothingFlag"]
    _BG_OLD_ANCHORS = CONFIG["AnchorsList"]
    _BG_OLD_ANCHORS_FLAG = CONFIG["AnchorsFlag"]

    y1 = savitsky_golay(
        y0, CONFIG["SmoothingWidth"]) if CONFIG["SmoothingFlag"] else y0

    anchors_indices = _convert_anchors_to_indices(x)

    if anchors_indices is None or not len(anchors_indices):
        anchors_indices = [0, len(y1) - 1]

    background = numpy.zeros_like(y1)
    previous_anchor = 0
    for anchor_index in anchors_indices:
        if (anchor_index > previous_anchor) and (anchor_index < len(y1)):
            background[previous_anchor:anchor_index] =\
                        snip1d(y1[previous_anchor:anchor_index],
                               width)
            previous_anchor = anchor_index

    if previous_anchor < len(y1):
        background[previous_anchor:] = snip1d(y1[previous_anchor:], width)

    _BG_SNIP_OLDBG = background

    return background
示例#3
0
def snip_bg(x, y0, width):
    """Compute the snip bg for y0"""
    global _BG_SNIP_OLDY
    global _BG_SNIP_OLDWIDTH
    global _BG_SNIP_OLDBG
    global _BG_SMOOTH_OLDWIDTH
    global _BG_SMOOTH_OLDFLAG
    global _BG_OLD_ANCHORS
    global _BG_OLD_ANCHORS_FLAG

    parameters_changed =\
        _BG_SNIP_OLDWIDTH != width or\
        _BG_SMOOTH_OLDWIDTH != CONFIG["SmoothingWidth"] or\
        _BG_SMOOTH_OLDFLAG != CONFIG["SmoothingFlag"] or\
        _BG_OLD_ANCHORS_FLAG != CONFIG["AnchorsFlag"] or\
        _BG_OLD_ANCHORS != CONFIG["AnchorsList"]

    # same parameters
    if not parameters_changed:
        # same data
        if numpy.sum(_BG_SNIP_OLDY == y0) == len(y0):
            # same result
            return _BG_SNIP_OLDBG

    _BG_SNIP_OLDY = y0
    _BG_SNIP_OLDWIDTH = width
    _BG_SMOOTH_OLDWIDTH = CONFIG["SmoothingWidth"]
    _BG_SMOOTH_OLDFLAG = CONFIG["SmoothingFlag"]
    _BG_OLD_ANCHORS = CONFIG["AnchorsList"]
    _BG_OLD_ANCHORS_FLAG = CONFIG["AnchorsFlag"]

    y1 = savitsky_golay(y0, CONFIG["SmoothingWidth"]) if CONFIG["SmoothingFlag"] else y0

    anchors_indices = _convert_anchors_to_indices(x)

    if anchors_indices is None or not len(anchors_indices):
        anchors_indices = [0, len(y1) - 1]

    background = numpy.zeros_like(y1)
    previous_anchor = 0
    for anchor_index in anchors_indices:
        if (anchor_index > previous_anchor) and (anchor_index < len(y1)):
                background[previous_anchor:anchor_index] =\
                            snip1d(y1[previous_anchor:anchor_index],
                                   width)
                previous_anchor = anchor_index

    if previous_anchor < len(y1):
        background[previous_anchor:] = snip1d(y1[previous_anchor:],
                                              width)

    _BG_SNIP_OLDBG = background

    return background
示例#4
0
def strip_bg(x, y0, width, niter):
    """Extract and return the strip bg from y0.

    Use anchors coordinates in CONFIG["AnchorsList"] if flag
    CONFIG["AnchorsFlag"] is True. Convert anchors from x coordinate
    to array index prior to passing it to silx.math.fit.filters.strip

    :param x: Abscissa array
    :param x: Ordinate array (data values at x positions)
    :param width: strip width
    :param niter: strip niter
    """
    global _BG_STRIP_OLDY
    global _BG_STRIP_OLDPARS
    global _BG_STRIP_OLDBG
    global _BG_SMOOTH_OLDWIDTH
    global _BG_SMOOTH_OLDFLAG
    global _BG_OLD_ANCHORS
    global _BG_OLD_ANCHORS_FLAG

    parameters_changed =\
        _BG_STRIP_OLDPARS != [width, niter] or\
        _BG_SMOOTH_OLDWIDTH != CONFIG["SmoothingWidth"] or\
        _BG_SMOOTH_OLDFLAG != CONFIG["SmoothingFlag"] or\
        _BG_OLD_ANCHORS_FLAG != CONFIG["AnchorsFlag"] or\
        _BG_OLD_ANCHORS != CONFIG["AnchorsList"]

    # same parameters
    if not parameters_changed:
        # same data
        if numpy.sum(_BG_STRIP_OLDY == y0) == len(y0):
            # same result
            return _BG_STRIP_OLDBG

    _BG_STRIP_OLDY = y0
    _BG_STRIP_OLDPARS = [width, niter]
    _BG_SMOOTH_OLDWIDTH = CONFIG["SmoothingWidth"]
    _BG_SMOOTH_OLDFLAG = CONFIG["SmoothingFlag"]
    _BG_OLD_ANCHORS = CONFIG["AnchorsList"]
    _BG_OLD_ANCHORS_FLAG = CONFIG["AnchorsFlag"]

    y1 = savitsky_golay(y0, CONFIG["SmoothingWidth"]) if CONFIG["SmoothingFlag"] else y0

    anchors_indices = _convert_anchors_to_indices(x)

    background = strip(y1,
                       w=width,
                       niterations=niter,
                       factor=CONFIG["StripThresholdFactor"],
                       anchors=anchors_indices)

    _BG_STRIP_OLDBG = background

    return background
示例#5
0
文件: test_filters.py 项目: PiRK/silx
    def testSavitskyGolay(self):
        npts = 25
        for y in [self.y1, self.y2, self.y3]:
            smoothed_y = filters.savitsky_golay(y, npoints=npts)

            # we added +-5% of random noise. The difference must be much lower
            # than 5%.
            diff = abs(sum(smoothed_y) - sum(y)) / sum(y)
            self.assertLess(diff, 0.05,
                            "Difference between data with 5%% noise and " +
                            "smoothed data is > 5%% (%f %%)" % (diff * 100))
            # Try various smoothing levels
            npts += 25
示例#6
0
    def testSavitskyGolay(self):
        npts = 25
        for y in [self.y1, self.y2, self.y3]:
            smoothed_y = filters.savitsky_golay(y, npoints=npts)

            # we added +-5% of random noise. The difference must be much lower
            # than 5%.
            diff = abs(sum(smoothed_y) - sum(y)) / sum(y)
            self.assertLess(
                diff, 0.05, "Difference between data with 5%% noise and " +
                "smoothed data is > 5%% (%f %%)" % (diff * 100))

            # Try various smoothing levels
            npts += 25
示例#7
0
    def _update(self, resetzoom=False):
        """Compute strip and snip backgrounds, update the curves
        """
        if self._y is None:
            return

        pars = self.getParameters()

        # smoothed data
        y = numpy.ravel(numpy.array(self._y)).astype(numpy.float64)
        if pars["SmoothingFlag"]:
            ysmooth = filters.savitsky_golay(y, pars['SmoothingWidth'])
            f = [0.25, 0.5, 0.25]
            ysmooth[1:-1] = numpy.convolve(ysmooth, f, mode=0)
            ysmooth[0] = 0.5 * (ysmooth[0] + ysmooth[1])
            ysmooth[-1] = 0.5 * (ysmooth[-1] + ysmooth[-2])
        else:
            ysmooth = y


        # loop for anchors
        x = self._x
        niter = pars['StripIterations']
        anchors_indices = []
        if pars['AnchorsFlag'] and pars['AnchorsList'] is not None:
            ravelled = x
            for channel in pars['AnchorsList']:
                if channel <= ravelled[0]:
                    continue
                index = numpy.nonzero(ravelled >= channel)[0]
                if len(index):
                    index = min(index)
                    if index > 0:
                        anchors_indices.append(index)

        stripBackground = filters.strip(ysmooth,
                                        w=pars['StripWidth'],
                                        niterations=niter,
                                        factor=pars['StripThreshold'],
                                        anchors=anchors_indices)

        if niter >= 1000:
            # final smoothing
            stripBackground = filters.strip(stripBackground,
                                            w=1,
                                            niterations=50*pars['StripWidth'],
                                            factor=pars['StripThreshold'],
                                            anchors=anchors_indices)

        if len(anchors_indices) == 0:
            anchors_indices = [0, len(ysmooth)-1]
        anchors_indices.sort()
        snipBackground = 0.0 * ysmooth
        lastAnchor = 0
        for anchor in anchors_indices:
            if (anchor > lastAnchor) and (anchor < len(ysmooth)):
                snipBackground[lastAnchor:anchor] =\
                            filters.snip1d(ysmooth[lastAnchor:anchor],
                                           pars['SnipWidth'])
                lastAnchor = anchor
        if lastAnchor < len(ysmooth):
            snipBackground[lastAnchor:] =\
                            filters.snip1d(ysmooth[lastAnchor:],
                                           pars['SnipWidth'])

        self.graphWidget.addCurve(x, y,
                                  legend='Input Data',
                                  replace=True,
                                  resetzoom=resetzoom)
        self.graphWidget.addCurve(x, stripBackground,
                                  legend='Strip Background',
                                  resetzoom=False)
        self.graphWidget.addCurve(x, snipBackground,
                                  legend='SNIP Background',
                                  resetzoom=False)
        if self._xmin is not None and self._xmax is not None:
            self.graphWidget.getXAxis().setLimits(self._xmin, self._xmax)
示例#8
0
    def _update(self, resetzoom=False):
        """Compute strip and snip backgrounds, update the curves
        """
        if self._y is None:
            return

        pars = self.getParameters()

        # smoothed data
        y = numpy.ravel(numpy.array(self._y)).astype(numpy.float)
        if pars["SmoothingFlag"]:
            ysmooth = filters.savitsky_golay(y, pars['SmoothingWidth'])
            f = [0.25, 0.5, 0.25]
            ysmooth[1:-1] = numpy.convolve(ysmooth, f, mode=0)
            ysmooth[0] = 0.5 * (ysmooth[0] + ysmooth[1])
            ysmooth[-1] = 0.5 * (ysmooth[-1] + ysmooth[-2])
        else:
            ysmooth = y


        # loop for anchors
        x = self._x
        niter = pars['StripIterations']
        anchors_indices = []
        if pars['AnchorsFlag'] and pars['AnchorsList'] is not None:
            ravelled = x
            for channel in pars['AnchorsList']:
                if channel <= ravelled[0]:
                    continue
                index = numpy.nonzero(ravelled >= channel)[0]
                if len(index):
                    index = min(index)
                    if index > 0:
                        anchors_indices.append(index)

        stripBackground = filters.strip(ysmooth,
                                        w=pars['StripWidth'],
                                        niterations=niter,
                                        factor=pars['StripThreshold'],
                                        anchors=anchors_indices)

        if niter >= 1000:
            # final smoothing
            stripBackground = filters.strip(stripBackground,
                                            w=1,
                                            niterations=50*pars['StripWidth'],
                                            factor=pars['StripThreshold'],
                                            anchors=anchors_indices)

        if len(anchors_indices) == 0:
            anchors_indices = [0, len(ysmooth)-1]
        anchors_indices.sort()
        snipBackground = 0.0 * ysmooth
        lastAnchor = 0
        for anchor in anchors_indices:
            if (anchor > lastAnchor) and (anchor < len(ysmooth)):
                snipBackground[lastAnchor:anchor] =\
                            filters.snip1d(ysmooth[lastAnchor:anchor],
                                           pars['SnipWidth'])
                lastAnchor = anchor
        if lastAnchor < len(ysmooth):
            snipBackground[lastAnchor:] =\
                            filters.snip1d(ysmooth[lastAnchor:],
                                           pars['SnipWidth'])

        self.graphWidget.addCurve(x, y,
                                  legend='Input Data',
                                  replace=True,
                                  resetzoom=resetzoom)
        self.graphWidget.addCurve(x, stripBackground,
                                  legend='Strip Background',
                                  resetzoom=False)
        self.graphWidget.addCurve(x, snipBackground,
                                  legend='SNIP Background',
                                  resetzoom=False)
        if self._xmin is not None and self._xmax is not None:
            self.graphWidget.getXAxis().setLimits(self._xmin, self._xmax)