Example #1
0
class TestAxisSync(TestCaseQt):
    """Tests AxisSync class"""
    def setUp(self):
        TestCaseQt.setUp(self)
        self.plot1 = PlotWidget()
        self.plot2 = PlotWidget()
        self.plot3 = PlotWidget()

    def tearDown(self):
        self.plot1 = None
        self.plot2 = None
        self.plot3 = None
        TestCaseQt.tearDown(self)

    def testMoveFirstAxis(self):
        """Test synchronization after construction"""
        _sync = SyncAxes([
            self.plot1.getXAxis(),
            self.plot2.getXAxis(),
            self.plot3.getXAxis()
        ])

        self.plot1.getXAxis().setLimits(10, 500)
        self.assertEqual(self.plot1.getXAxis().getLimits(), (10, 500))
        self.assertEqual(self.plot2.getXAxis().getLimits(), (10, 500))
        self.assertEqual(self.plot3.getXAxis().getLimits(), (10, 500))

    def testMoveSecondAxis(self):
        """Test synchronization after construction"""
        _sync = SyncAxes([
            self.plot1.getXAxis(),
            self.plot2.getXAxis(),
            self.plot3.getXAxis()
        ])

        self.plot2.getXAxis().setLimits(10, 500)
        self.assertEqual(self.plot1.getXAxis().getLimits(), (10, 500))
        self.assertEqual(self.plot2.getXAxis().getLimits(), (10, 500))
        self.assertEqual(self.plot3.getXAxis().getLimits(), (10, 500))

    def testMoveTwoAxes(self):
        """Test synchronization after construction"""
        _sync = SyncAxes([
            self.plot1.getXAxis(),
            self.plot2.getXAxis(),
            self.plot3.getXAxis()
        ])

        self.plot1.getXAxis().setLimits(1, 50)
        self.plot2.getXAxis().setLimits(10, 500)
        self.assertEqual(self.plot1.getXAxis().getLimits(), (10, 500))
        self.assertEqual(self.plot2.getXAxis().getLimits(), (10, 500))
        self.assertEqual(self.plot3.getXAxis().getLimits(), (10, 500))

    def testDestruction(self):
        """Test synchronization when sync object is destroyed"""
        sync = SyncAxes([
            self.plot1.getXAxis(),
            self.plot2.getXAxis(),
            self.plot3.getXAxis()
        ])
        del sync

        self.plot1.getXAxis().setLimits(10, 500)
        self.assertEqual(self.plot1.getXAxis().getLimits(), (10, 500))
        self.assertNotEqual(self.plot2.getXAxis().getLimits(), (10, 500))
        self.assertNotEqual(self.plot3.getXAxis().getLimits(), (10, 500))

    def testAxisDestruction(self):
        """Test synchronization when an axis disappear"""
        _sync = SyncAxes([
            self.plot1.getXAxis(),
            self.plot2.getXAxis(),
            self.plot3.getXAxis()
        ])

        # Destroy the plot is possible
        import weakref
        plot = weakref.ref(self.plot2)
        self.plot2 = None
        result = self.qWaitForDestroy(plot)
        if not result:
            # We can't test
            self.skipTest("Object not destroyed")

        self.plot1.getXAxis().setLimits(10, 500)
        self.assertEqual(self.plot3.getXAxis().getLimits(), (10, 500))

    def testStop(self):
        """Test synchronization after calling stop"""
        sync = SyncAxes([
            self.plot1.getXAxis(),
            self.plot2.getXAxis(),
            self.plot3.getXAxis()
        ])
        sync.stop()

        self.plot1.getXAxis().setLimits(10, 500)
        self.assertEqual(self.plot1.getXAxis().getLimits(), (10, 500))
        self.assertNotEqual(self.plot2.getXAxis().getLimits(), (10, 500))
        self.assertNotEqual(self.plot3.getXAxis().getLimits(), (10, 500))

    def testStopMovingStart(self):
        """Test synchronization after calling stop, moving an axis, then start again"""
        sync = SyncAxes([
            self.plot1.getXAxis(),
            self.plot2.getXAxis(),
            self.plot3.getXAxis()
        ])
        sync.stop()
        self.plot1.getXAxis().setLimits(10, 500)
        self.plot2.getXAxis().setLimits(1, 50)
        self.assertEqual(self.plot1.getXAxis().getLimits(), (10, 500))
        sync.start()

        # The first axis is the reference
        self.assertEqual(self.plot1.getXAxis().getLimits(), (10, 500))
        self.assertEqual(self.plot2.getXAxis().getLimits(), (10, 500))
        self.assertEqual(self.plot3.getXAxis().getLimits(), (10, 500))

    def testDoubleStop(self):
        """Test double stop"""
        sync = SyncAxes([
            self.plot1.getXAxis(),
            self.plot2.getXAxis(),
            self.plot3.getXAxis()
        ])
        sync.stop()
        self.assertRaises(RuntimeError, sync.stop)

    def testDoubleStart(self):
        """Test double stop"""
        sync = SyncAxes([
            self.plot1.getXAxis(),
            self.plot2.getXAxis(),
            self.plot3.getXAxis()
        ])
        self.assertRaises(RuntimeError, sync.start)

    def testScale(self):
        """Test scale change"""
        _sync = SyncAxes([
            self.plot1.getXAxis(),
            self.plot2.getXAxis(),
            self.plot3.getXAxis()
        ])
        self.plot1.getXAxis().setScale(self.plot1.getXAxis().LOGARITHMIC)
        self.assertEqual(self.plot1.getXAxis().getScale(),
                         self.plot1.getXAxis().LOGARITHMIC)
        self.assertEqual(self.plot2.getXAxis().getScale(),
                         self.plot1.getXAxis().LOGARITHMIC)
        self.assertEqual(self.plot3.getXAxis().getScale(),
                         self.plot1.getXAxis().LOGARITHMIC)

    def testDirection(self):
        """Test direction change"""
        _sync = SyncAxes([
            self.plot1.getYAxis(),
            self.plot2.getYAxis(),
            self.plot3.getYAxis()
        ])
        self.plot1.getYAxis().setInverted(True)
        self.assertEqual(self.plot1.getYAxis().isInverted(), True)
        self.assertEqual(self.plot2.getYAxis().isInverted(), True)
        self.assertEqual(self.plot3.getYAxis().isInverted(), True)
class TestLimitConstaints(unittest.TestCase):
    """Tests setLimitConstaints class"""
    def setUp(self):
        self.plot = PlotWidget()

    def tearDown(self):
        self.plot = None

    def testApi(self):
        """Test availability of the API"""
        self.plot.getXAxis().setLimitsConstraints(minPos=1, maxPos=10)
        self.plot.getXAxis().setRangeConstraints(minRange=1, maxRange=1)
        self.plot.getYAxis().setLimitsConstraints(minPos=1, maxPos=10)
        self.plot.getYAxis().setRangeConstraints(minRange=1, maxRange=1)

    def testXMinMax(self):
        """Test limit constains on x-axis"""
        self.plot.getXAxis().setLimitsConstraints(minPos=0, maxPos=100)
        self.plot.setLimits(xmin=-1, xmax=101, ymin=-1, ymax=101)
        self.assertEqual(self.plot.getXAxis().getLimits(), (0, 100))
        self.assertEqual(self.plot.getYAxis().getLimits(), (-1, 101))

    def testYMinMax(self):
        """Test limit constains on y-axis"""
        self.plot.getYAxis().setLimitsConstraints(minPos=0, maxPos=100)
        self.plot.setLimits(xmin=-1, xmax=101, ymin=-1, ymax=101)
        self.assertEqual(self.plot.getXAxis().getLimits(), (-1, 101))
        self.assertEqual(self.plot.getYAxis().getLimits(), (0, 100))

    def testMinXRange(self):
        """Test min range constains on x-axis"""
        self.plot.getXAxis().setRangeConstraints(minRange=100)
        self.plot.setLimits(xmin=1, xmax=99, ymin=1, ymax=99)
        limits = self.plot.getXAxis().getLimits()
        self.assertEqual(limits[1] - limits[0], 100)
        limits = self.plot.getYAxis().getLimits()
        self.assertNotEqual(limits[1] - limits[0], 100)

    def testMaxXRange(self):
        """Test max range constains on x-axis"""
        self.plot.getXAxis().setRangeConstraints(maxRange=100)
        self.plot.setLimits(xmin=-1, xmax=101, ymin=-1, ymax=101)
        limits = self.plot.getXAxis().getLimits()
        self.assertEqual(limits[1] - limits[0], 100)
        limits = self.plot.getYAxis().getLimits()
        self.assertNotEqual(limits[1] - limits[0], 100)

    def testMinYRange(self):
        """Test min range constains on y-axis"""
        self.plot.getYAxis().setRangeConstraints(minRange=100)
        self.plot.setLimits(xmin=1, xmax=99, ymin=1, ymax=99)
        limits = self.plot.getXAxis().getLimits()
        self.assertNotEqual(limits[1] - limits[0], 100)
        limits = self.plot.getYAxis().getLimits()
        self.assertEqual(limits[1] - limits[0], 100)

    def testMaxYRange(self):
        """Test max range constains on y-axis"""
        self.plot.getYAxis().setRangeConstraints(maxRange=100)
        self.plot.setLimits(xmin=-1, xmax=101, ymin=-1, ymax=101)
        limits = self.plot.getXAxis().getLimits()
        self.assertNotEqual(limits[1] - limits[0], 100)
        limits = self.plot.getYAxis().getLimits()
        self.assertEqual(limits[1] - limits[0], 100)

    def testChangeOfConstraints(self):
        """Test changing of the constraints"""
        self.plot.getXAxis().setRangeConstraints(minRange=10, maxRange=10)
        # There is no more constraints on the range
        self.plot.getXAxis().setRangeConstraints(minRange=None, maxRange=None)
        self.plot.setLimits(xmin=-1, xmax=101, ymin=-1, ymax=101)
        self.assertEqual(self.plot.getXAxis().getLimits(), (-1, 101))

    def testSettingConstraints(self):
        """Test setting a constaint (setLimits first then the constaint)"""
        self.plot.setLimits(xmin=-1, xmax=101, ymin=-1, ymax=101)
        self.plot.getXAxis().setLimitsConstraints(minPos=0, maxPos=100)
        self.assertEqual(self.plot.getXAxis().getLimits(), (0, 100))
Example #3
0
class BackgroundWidget(qt.QWidget):
    """Background configuration widget, with a plot to preview the results.

    Strip and snip filters parameters can be adjusted using input widgets,
    and the computed backgrounds are plotted next to the original data to
    show the result."""
    def __init__(self, parent=None):
        qt.QWidget.__init__(self, parent)
        self.setWindowTitle("Strip and SNIP Configuration Window")
        self.mainLayout = qt.QVBoxLayout(self)
        self.mainLayout.setContentsMargins(0, 0, 0, 0)
        self.mainLayout.setSpacing(2)
        self.parametersWidget = BackgroundParamWidget(self)
        self.graphWidget = PlotWidget(parent=self)
        self.mainLayout.addWidget(self.parametersWidget)
        self.mainLayout.addWidget(self.graphWidget)
        self._x = None
        self._y = None
        self.parametersWidget.sigBackgroundParamWidgetSignal.connect(self._slot)

    def getParameters(self):
        """Return dictionary of parameters defined in the GUI

        The returned dictionary contains following values:

            - *algorithm*: *"strip"* or *"snip"*
            - *StripWidth*: width of strip iterator
            - *StripIterations*: number of iterations
            - *StripThreshold*: strip curvature (currently fixed to 1.0)
            - *SnipWidth*: width of snip algorithm
            - *SmoothingFlag*: flag to enable/disable smoothing
            - *SmoothingWidth*: width of Savitsky-Golay smoothing filter
            - *AnchorsFlag*: flag to enable/disable anchors
            - *AnchorsList*: list of anchors (X coordinates of fixed values)
        """
        return self.parametersWidget.getParameters()

    def setParameters(self, ddict):
        """Set values for all input widgets.

        :param dict ddict: Input dictionary, must have the same
            keys as the dictionary output by :meth:`getParameters`
        """
        return self.parametersWidget.setParameters(ddict)

    def setData(self, x, y, xmin=None, xmax=None):
        """Set data for the original curve, and _update strip and snip
        curves accordingly.

        :param x: Array or sequence of curve abscissa values
        :param y: Array or sequence of curve ordinate values
        :param xmin: Min value to be displayed on the X axis
        :param xmax: Max value to be displayed on the X axis
        """
        self._x = x
        self._y = y
        self._xmin = xmin
        self._xmax = xmax
        self._update(resetzoom=True)

    def _slot(self, ddict):
        self._update()

    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)
Example #4
0
class TestLimitConstaints(unittest.TestCase):
    """Tests setLimitConstaints class"""

    def setUp(self):
        self.plot = PlotWidget()

    def tearDown(self):
        self.plot = None

    def testApi(self):
        """Test availability of the API"""
        self.plot.getXAxis().setLimitsConstraints(minPos=1, maxPos=10)
        self.plot.getXAxis().setRangeConstraints(minRange=1, maxRange=1)
        self.plot.getYAxis().setLimitsConstraints(minPos=1, maxPos=10)
        self.plot.getYAxis().setRangeConstraints(minRange=1, maxRange=1)

    def testXMinMax(self):
        """Test limit constains on x-axis"""
        self.plot.getXAxis().setLimitsConstraints(minPos=0, maxPos=100)
        self.plot.setLimits(xmin=-1, xmax=101, ymin=-1, ymax=101)
        self.assertEqual(self.plot.getXAxis().getLimits(), (0, 100))
        self.assertEqual(self.plot.getYAxis().getLimits(), (-1, 101))

    def testYMinMax(self):
        """Test limit constains on y-axis"""
        self.plot.getYAxis().setLimitsConstraints(minPos=0, maxPos=100)
        self.plot.setLimits(xmin=-1, xmax=101, ymin=-1, ymax=101)
        self.assertEqual(self.plot.getXAxis().getLimits(), (-1, 101))
        self.assertEqual(self.plot.getYAxis().getLimits(), (0, 100))

    def testMinXRange(self):
        """Test min range constains on x-axis"""
        self.plot.getXAxis().setRangeConstraints(minRange=100)
        self.plot.setLimits(xmin=1, xmax=99, ymin=1, ymax=99)
        limits = self.plot.getXAxis().getLimits()
        self.assertEqual(limits[1] - limits[0], 100)
        limits = self.plot.getYAxis().getLimits()
        self.assertNotEqual(limits[1] - limits[0], 100)

    def testMaxXRange(self):
        """Test max range constains on x-axis"""
        self.plot.getXAxis().setRangeConstraints(maxRange=100)
        self.plot.setLimits(xmin=-1, xmax=101, ymin=-1, ymax=101)
        limits = self.plot.getXAxis().getLimits()
        self.assertEqual(limits[1] - limits[0], 100)
        limits = self.plot.getYAxis().getLimits()
        self.assertNotEqual(limits[1] - limits[0], 100)

    def testMinYRange(self):
        """Test min range constains on y-axis"""
        self.plot.getYAxis().setRangeConstraints(minRange=100)
        self.plot.setLimits(xmin=1, xmax=99, ymin=1, ymax=99)
        limits = self.plot.getXAxis().getLimits()
        self.assertNotEqual(limits[1] - limits[0], 100)
        limits = self.plot.getYAxis().getLimits()
        self.assertEqual(limits[1] - limits[0], 100)

    def testMaxYRange(self):
        """Test max range constains on y-axis"""
        self.plot.getYAxis().setRangeConstraints(maxRange=100)
        self.plot.setLimits(xmin=-1, xmax=101, ymin=-1, ymax=101)
        limits = self.plot.getXAxis().getLimits()
        self.assertNotEqual(limits[1] - limits[0], 100)
        limits = self.plot.getYAxis().getLimits()
        self.assertEqual(limits[1] - limits[0], 100)

    def testChangeOfConstraints(self):
        """Test changing of the constraints"""
        self.plot.getXAxis().setRangeConstraints(minRange=10, maxRange=10)
        # There is no more constraints on the range
        self.plot.getXAxis().setRangeConstraints(minRange=None, maxRange=None)
        self.plot.setLimits(xmin=-1, xmax=101, ymin=-1, ymax=101)
        self.assertEqual(self.plot.getXAxis().getLimits(), (-1, 101))

    def testSettingConstraints(self):
        """Test setting a constaint (setLimits first then the constaint)"""
        self.plot.setLimits(xmin=-1, xmax=101, ymin=-1, ymax=101)
        self.plot.getXAxis().setLimitsConstraints(minPos=0, maxPos=100)
        self.assertEqual(self.plot.getXAxis().getLimits(), (0, 100))
Example #5
0
class TestAxisSync(TestCaseQt):
    """Tests AxisSync class"""

    def setUp(self):
        TestCaseQt.setUp(self)
        self.plot1 = PlotWidget()
        self.plot2 = PlotWidget()
        self.plot3 = PlotWidget()

    def tearDown(self):
        self.plot1 = None
        self.plot2 = None
        self.plot3 = None
        TestCaseQt.tearDown(self)

    def testMoveFirstAxis(self):
        """Test synchronization after construction"""
        _sync = SyncAxes([self.plot1.getXAxis(), self.plot2.getXAxis(), self.plot3.getXAxis()])

        self.plot1.getXAxis().setLimits(10, 500)
        self.assertEqual(self.plot1.getXAxis().getLimits(), (10, 500))
        self.assertEqual(self.plot2.getXAxis().getLimits(), (10, 500))
        self.assertEqual(self.plot3.getXAxis().getLimits(), (10, 500))

    def testMoveSecondAxis(self):
        """Test synchronization after construction"""
        _sync = SyncAxes([self.plot1.getXAxis(), self.plot2.getXAxis(), self.plot3.getXAxis()])

        self.plot2.getXAxis().setLimits(10, 500)
        self.assertEqual(self.plot1.getXAxis().getLimits(), (10, 500))
        self.assertEqual(self.plot2.getXAxis().getLimits(), (10, 500))
        self.assertEqual(self.plot3.getXAxis().getLimits(), (10, 500))

    def testMoveTwoAxes(self):
        """Test synchronization after construction"""
        _sync = SyncAxes([self.plot1.getXAxis(), self.plot2.getXAxis(), self.plot3.getXAxis()])

        self.plot1.getXAxis().setLimits(1, 50)
        self.plot2.getXAxis().setLimits(10, 500)
        self.assertEqual(self.plot1.getXAxis().getLimits(), (10, 500))
        self.assertEqual(self.plot2.getXAxis().getLimits(), (10, 500))
        self.assertEqual(self.plot3.getXAxis().getLimits(), (10, 500))

    def testDestruction(self):
        """Test synchronization when sync object is destroyed"""
        sync = SyncAxes([self.plot1.getXAxis(), self.plot2.getXAxis(), self.plot3.getXAxis()])
        del sync

        self.plot1.getXAxis().setLimits(10, 500)
        self.assertEqual(self.plot1.getXAxis().getLimits(), (10, 500))
        self.assertNotEqual(self.plot2.getXAxis().getLimits(), (10, 500))
        self.assertNotEqual(self.plot3.getXAxis().getLimits(), (10, 500))

    def testAxisDestruction(self):
        """Test synchronization when an axis disappear"""
        _sync = SyncAxes([self.plot1.getXAxis(), self.plot2.getXAxis(), self.plot3.getXAxis()])

        # Destroy the plot is possible
        import weakref
        plot = weakref.ref(self.plot2)
        self.plot2 = None
        result = self.qWaitForDestroy(plot)
        if not result:
            # We can't test
            self.skipTest("Object not destroyed")

        self.plot1.getXAxis().setLimits(10, 500)
        self.assertEqual(self.plot3.getXAxis().getLimits(), (10, 500))

    def testStop(self):
        """Test synchronization after calling stop"""
        sync = SyncAxes([self.plot1.getXAxis(), self.plot2.getXAxis(), self.plot3.getXAxis()])
        sync.stop()

        self.plot1.getXAxis().setLimits(10, 500)
        self.assertEqual(self.plot1.getXAxis().getLimits(), (10, 500))
        self.assertNotEqual(self.plot2.getXAxis().getLimits(), (10, 500))
        self.assertNotEqual(self.plot3.getXAxis().getLimits(), (10, 500))

    def testStopMovingStart(self):
        """Test synchronization after calling stop, moving an axis, then start again"""
        sync = SyncAxes([self.plot1.getXAxis(), self.plot2.getXAxis(), self.plot3.getXAxis()])
        sync.stop()
        self.plot1.getXAxis().setLimits(10, 500)
        self.plot2.getXAxis().setLimits(1, 50)
        self.assertEqual(self.plot1.getXAxis().getLimits(), (10, 500))
        sync.start()

        # The first axis is the reference
        self.assertEqual(self.plot1.getXAxis().getLimits(), (10, 500))
        self.assertEqual(self.plot2.getXAxis().getLimits(), (10, 500))
        self.assertEqual(self.plot3.getXAxis().getLimits(), (10, 500))

    def testDoubleStop(self):
        """Test double stop"""
        sync = SyncAxes([self.plot1.getXAxis(), self.plot2.getXAxis(), self.plot3.getXAxis()])
        sync.stop()
        self.assertRaises(RuntimeError, sync.stop)

    def testDoubleStart(self):
        """Test double stop"""
        sync = SyncAxes([self.plot1.getXAxis(), self.plot2.getXAxis(), self.plot3.getXAxis()])
        self.assertRaises(RuntimeError, sync.start)

    def testScale(self):
        """Test scale change"""
        _sync = SyncAxes([self.plot1.getXAxis(), self.plot2.getXAxis(), self.plot3.getXAxis()])
        self.plot1.getXAxis().setScale(self.plot1.getXAxis().LOGARITHMIC)
        self.assertEqual(self.plot1.getXAxis().getScale(), self.plot1.getXAxis().LOGARITHMIC)
        self.assertEqual(self.plot2.getXAxis().getScale(), self.plot1.getXAxis().LOGARITHMIC)
        self.assertEqual(self.plot3.getXAxis().getScale(), self.plot1.getXAxis().LOGARITHMIC)

    def testDirection(self):
        """Test direction change"""
        _sync = SyncAxes([self.plot1.getYAxis(), self.plot2.getYAxis(), self.plot3.getYAxis()])
        self.plot1.getYAxis().setInverted(True)
        self.assertEqual(self.plot1.getYAxis().isInverted(), True)
        self.assertEqual(self.plot2.getYAxis().isInverted(), True)
        self.assertEqual(self.plot3.getYAxis().isInverted(), True)
Example #6
0
class BackgroundWidget(qt.QWidget):
    """Background configuration widget, with a plot to preview the results.

    Strip and snip filters parameters can be adjusted using input widgets,
    and the computed backgrounds are plotted next to the original data to
    show the result."""
    def __init__(self, parent=None):
        qt.QWidget.__init__(self, parent)
        self.setWindowTitle("Strip and SNIP Configuration Window")
        self.mainLayout = qt.QVBoxLayout(self)
        self.mainLayout.setContentsMargins(0, 0, 0, 0)
        self.mainLayout.setSpacing(2)
        self.parametersWidget = BackgroundParamWidget(self)
        self.graphWidget = PlotWidget(parent=self)
        self.mainLayout.addWidget(self.parametersWidget)
        self.mainLayout.addWidget(self.graphWidget)
        self._x = None
        self._y = None
        self.parametersWidget.sigBackgroundParamWidgetSignal.connect(self._slot)

    def getParameters(self):
        """Return dictionary of parameters defined in the GUI

        The returned dictionary contains following values:

            - *algorithm*: *"strip"* or *"snip"*
            - *StripWidth*: width of strip iterator
            - *StripIterations*: number of iterations
            - *StripThreshold*: strip curvature (currently fixed to 1.0)
            - *SnipWidth*: width of snip algorithm
            - *SmoothingFlag*: flag to enable/disable smoothing
            - *SmoothingWidth*: width of Savitsky-Golay smoothing filter
            - *AnchorsFlag*: flag to enable/disable anchors
            - *AnchorsList*: list of anchors (X coordinates of fixed values)
        """
        return self.parametersWidget.getParameters()

    def setParameters(self, ddict):
        """Set values for all input widgets.

        :param dict ddict: Input dictionary, must have the same
            keys as the dictionary output by :meth:`getParameters`
        """
        return self.parametersWidget.setParameters(ddict)

    def setData(self, x, y, xmin=None, xmax=None):
        """Set data for the original curve, and _update strip and snip
        curves accordingly.

        :param x: Array or sequence of curve abscissa values
        :param y: Array or sequence of curve ordinate values
        :param xmin: Min value to be displayed on the X axis
        :param xmax: Max value to be displayed on the X axis
        """
        self._x = x
        self._y = y
        self._xmin = xmin
        self._xmax = xmax
        self._update(resetzoom=True)

    def _slot(self, ddict):
        self._update()

    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)
Example #7
0
class TestAxisSync(unittest.TestCase):
    """Tests AxisSync class"""
    def setUp(self):
        self.plot1 = PlotWidget()
        self.plot2 = PlotWidget()
        self.plot3 = PlotWidget()

    def tearDown(self):
        self.plot1 = None
        self.plot2 = None
        self.plot3 = None

    def testMoveFirstAxis(self):
        """Test synchronization after construction"""
        _sync = SyncAxes([
            self.plot1.getXAxis(),
            self.plot2.getXAxis(),
            self.plot3.getXAxis()
        ])

        self.plot1.getXAxis().setLimits(10, 500)
        self.assertEqual(self.plot1.getXAxis().getLimits(), (10, 500))
        self.assertEqual(self.plot2.getXAxis().getLimits(), (10, 500))
        self.assertEqual(self.plot3.getXAxis().getLimits(), (10, 500))

    def testMoveSecondAxis(self):
        """Test synchronization after construction"""
        _sync = SyncAxes([
            self.plot1.getXAxis(),
            self.plot2.getXAxis(),
            self.plot3.getXAxis()
        ])

        self.plot2.getXAxis().setLimits(10, 500)
        self.assertEqual(self.plot1.getXAxis().getLimits(), (10, 500))
        self.assertEqual(self.plot2.getXAxis().getLimits(), (10, 500))
        self.assertEqual(self.plot3.getXAxis().getLimits(), (10, 500))

    def testMoveTwoAxes(self):
        """Test synchronization after construction"""
        _sync = SyncAxes([
            self.plot1.getXAxis(),
            self.plot2.getXAxis(),
            self.plot3.getXAxis()
        ])

        self.plot1.getXAxis().setLimits(1, 50)
        self.plot2.getXAxis().setLimits(10, 500)
        self.assertEqual(self.plot1.getXAxis().getLimits(), (10, 500))
        self.assertEqual(self.plot2.getXAxis().getLimits(), (10, 500))
        self.assertEqual(self.plot3.getXAxis().getLimits(), (10, 500))

    def testDestruction(self):
        """Test synchronization when sync object is destroyed"""
        sync = SyncAxes([
            self.plot1.getXAxis(),
            self.plot2.getXAxis(),
            self.plot3.getXAxis()
        ])
        del sync

        self.plot1.getXAxis().setLimits(10, 500)
        self.assertEqual(self.plot1.getXAxis().getLimits(), (10, 500))
        self.assertNotEqual(self.plot2.getXAxis().getLimits(), (10, 500))
        self.assertNotEqual(self.plot3.getXAxis().getLimits(), (10, 500))

    def testStop(self):
        """Test synchronization after calling stop"""
        sync = SyncAxes([
            self.plot1.getXAxis(),
            self.plot2.getXAxis(),
            self.plot3.getXAxis()
        ])
        sync.stop()

        self.plot1.getXAxis().setLimits(10, 500)
        self.assertEqual(self.plot1.getXAxis().getLimits(), (10, 500))
        self.assertNotEqual(self.plot2.getXAxis().getLimits(), (10, 500))
        self.assertNotEqual(self.plot3.getXAxis().getLimits(), (10, 500))

    def testStopMovingStart(self):
        """Test synchronization after calling stop, moving an axis, then start again"""
        sync = SyncAxes([
            self.plot1.getXAxis(),
            self.plot2.getXAxis(),
            self.plot3.getXAxis()
        ])
        sync.stop()
        self.plot1.getXAxis().setLimits(10, 500)
        self.plot2.getXAxis().setLimits(1, 50)
        self.assertEqual(self.plot1.getXAxis().getLimits(), (10, 500))
        sync.start()

        # The first axis is the reference
        self.assertEqual(self.plot1.getXAxis().getLimits(), (10, 500))
        self.assertEqual(self.plot2.getXAxis().getLimits(), (10, 500))
        self.assertEqual(self.plot3.getXAxis().getLimits(), (10, 500))

    def testDoubleStop(self):
        """Test double stop"""
        sync = SyncAxes([
            self.plot1.getXAxis(),
            self.plot2.getXAxis(),
            self.plot3.getXAxis()
        ])
        sync.stop()
        self.assertRaises(RuntimeError, sync.stop)

    def testDoubleStart(self):
        """Test double stop"""
        sync = SyncAxes([
            self.plot1.getXAxis(),
            self.plot2.getXAxis(),
            self.plot3.getXAxis()
        ])
        self.assertRaises(RuntimeError, sync.start)

    def testScale(self):
        """Test scale change"""
        _sync = SyncAxes([
            self.plot1.getXAxis(),
            self.plot2.getXAxis(),
            self.plot3.getXAxis()
        ])
        self.plot1.getXAxis().setScale(self.plot1.getXAxis().LOGARITHMIC)
        self.assertEqual(self.plot1.getXAxis().getScale(),
                         self.plot1.getXAxis().LOGARITHMIC)
        self.assertEqual(self.plot2.getXAxis().getScale(),
                         self.plot1.getXAxis().LOGARITHMIC)
        self.assertEqual(self.plot3.getXAxis().getScale(),
                         self.plot1.getXAxis().LOGARITHMIC)

    def testDirection(self):
        """Test direction change"""
        _sync = SyncAxes([
            self.plot1.getYAxis(),
            self.plot2.getYAxis(),
            self.plot3.getYAxis()
        ])
        self.plot1.getYAxis().setInverted(True)
        self.assertEqual(self.plot1.getYAxis().isInverted(), True)
        self.assertEqual(self.plot2.getYAxis().isInverted(), True)
        self.assertEqual(self.plot3.getYAxis().isInverted(), True)