def cmapRange(self):
        if self._cmapRange is None:  # Auto-scale mode
            if self._cmapRangeCache is None:
                # Build data , positive ranges
                min_, minPos, max_ = minMax(self.data, minPositive=True)
                maxPos = max_ if max_ > 0. else 1.
                if minPos is None:
                    minPos = maxPos
                self._cmapRangeCache = {'range': (min_, max_),
                                        'pos': (minPos, maxPos)}

            return self._cmapRangeCache['pos' if self.cmapIsLog else 'range']

        else:
            if not self.cmapIsLog:
                return self._cmapRange  # Return range as is
            else:
                if self._cmapRangeCache is None:
                    # Build a strictly positive range from cmapRange
                    min_, max_ = self._cmapRange
                    if min_ > 0. and max_ > 0.:
                        minPos, maxPos = min_, max_
                    else:
                        dataMin, minPos, dataMax = minMax(self.data,
                                                          minPositive=True)
                        if max_ > 0.:
                            maxPos = max_
                        elif dataMax > 0.:
                            maxPos = dataMax
                        else:
                            maxPos = 1.  # Arbitrary fallback
                        if minPos is None:
                            minPos = maxPos
                    self._cmapRangeCache = minPos, maxPos
                return self._cmapRangeCache  # Strictly positive range
Exemple #2
0
    def _testMinMaxVsNumpy(self, data, minPos=False):
        """Single test C minMax and min positive vs Numpy min/max."""
        startTime = time.time()
        if minPos:
            min_, minPositive, max_ = ctools.minMax(data, minPositive=True)
        else:
            min_, max_ = ctools.minMax(data, minPositive=False)
        duration = time.time() - startTime

        startTime = time.time()
        try:
            minNumpy, maxNumpy = np.nanmin(data), np.nanmax(data)
        except ValueError:
            minNumpy, maxNumpy = None, None
        if minPos:
            minPositiveNumpy = self._minPos(data)
        durationNumpy = time.time() - startTime

        self._log(data.dtype, data.size, 'duration C (s):', duration,
                  'duration Numpy (s):', durationNumpy)

        self.assertEqual(min_, minNumpy)
        if minPos:
            self.assertEqual(minPositive, minPositiveNumpy)
        self.assertEqual(max_, maxNumpy)
Exemple #3
0
    def testMinMaxNoData(self):
        """Test C minMax and min positive with no data.
        """
        self._log("testMinMaxNoData")
        for dtype in self.DTYPES:
            # No data
            data = np.array((), dtype=dtype)
            with self.assertRaises(ValueError):
                ctools.minMax(data, minPositive=False)

            with self.assertRaises(ValueError):
                ctools.minMax(data, minPositive=True)
Exemple #4
0
    def testMinMaxNoData(self):
        """Test C minMax and min positive with no data.
        """
        self._log("testMinMaxNoData")
        for dtype in self.DTYPES:
            # No data
            data = np.array((), dtype=dtype)
            with self.assertRaises(ValueError):
                ctools.minMax(data, minPositive=False)

            with self.assertRaises(ValueError):
                ctools.minMax(data, minPositive=True)
Exemple #5
0
    def testMinMaxNan(self):
        """Test C minMax and min positive with NaN.
        """
        self._log("testMinMaxNan")

        for dtype in self.FLOATING_DTYPES:
            # All NaN
            data = np.array((float("nan"), float("nan")), dtype=dtype)
            min_, minPositive, max_ = ctools.minMax(data, minPositive=True)
            self.assertTrue(math.isnan(min_))
            self.assertEqual(minPositive, None)
            self.assertTrue(math.isnan(max_))

            # NaN first and positive
            data = np.array((float("nan"), 1.0), dtype=dtype)
            self._testMinMaxVsNumpy(data)

            # NaN first and negative
            data = np.array((float("nan"), -1.0), dtype=dtype)
            self._testMinMaxVsNumpy(data)

            # NaN last and positive
            data = np.array((1.0, 2.0, float("nan")), dtype=dtype)
            self._testMinMaxVsNumpy(data)

            # NaN last and negative
            data = np.array((-1.0, -2.0, float("nan")), dtype=dtype)
            self._testMinMaxVsNumpy(data)

            # Some NaN
            data = np.array((1.0, float("nan"), -1.0), dtype=dtype)
            self._testMinMaxVsNumpy(data)
Exemple #6
0
    def testMinMaxNan(self):
        """Test C minMax and min positive with NaN.
        """
        self._log("testMinMaxNan")

        for dtype in self.FLOATING_DTYPES:
            # All NaN
            data = np.array((float('nan'), float('nan')), dtype=dtype)
            min_, minPositive, max_ = ctools.minMax(data, minPositive=True)
            self.assertTrue(math.isnan(min_))
            self.assertEqual(minPositive, None)
            self.assertTrue(math.isnan(max_))

            # NaN first and positive
            data = np.array((float('nan'), 1.0), dtype=dtype)
            self._testMinMaxVsNumpy(data)

            # NaN first and negative
            data = np.array((float('nan'), -1.0), dtype=dtype)
            self._testMinMaxVsNumpy(data)
 
            # NaN last and positive
            data = np.array((1.0, 2.0, float('nan')), dtype=dtype)
            self._testMinMaxVsNumpy(data)

            # NaN last and negative
            data = np.array((-1.0, -2.0, float('nan')), dtype=dtype)
            self._testMinMaxVsNumpy(data)

            # Some NaN
            data = np.array((1.0, float('nan'), -1.0), dtype=dtype)
            self._testMinMaxVsNumpy(data)
    def cmapRange(self):
        if self._cmapRange is None:  # Auto-scale mode
            if self._cmapRangeCache is None:
                # Build data , positive ranges
                min_, minPos, max_ = minMax(self.data, minPositive=True)
                maxPos = max_ if max_ > 0. else 1.
                if minPos is None:
                    minPos = maxPos
                self._cmapRangeCache = {
                    'range': (min_, max_),
                    'pos': (minPos, maxPos)
                }

            return self._cmapRangeCache['pos' if self.cmapIsLog else 'range']

        else:
            if not self.cmapIsLog:
                return self._cmapRange  # Return range as is
            else:
                if self._cmapRangeCache is None:
                    # Build a strictly positive range from cmapRange
                    min_, max_ = self._cmapRange
                    if min_ > 0. and max_ > 0.:
                        minPos, maxPos = min_, max_
                    else:
                        dataMin, minPos, dataMax = minMax(self.data,
                                                          minPositive=True)
                        if max_ > 0.:
                            maxPos = max_
                        elif dataMax > 0.:
                            maxPos = dataMax
                        else:
                            maxPos = 1.  # Arbitrary fallback
                        if minPos is None:
                            minPos = maxPos
                    self._cmapRangeCache = minPos, maxPos
                return self._cmapRangeCache  # Strictly positive range
Exemple #8
0
    def _testMinMaxVsNumpy(self, data, minPos=False):
        """Single test C minMax and min positive vs Numpy min/max."""
        startTime = time.time()
        if minPos:
            min_, minPositive, max_ = ctools.minMax(data, minPositive=True)
        else:
            min_, max_ = ctools.minMax(data, minPositive=False)
        duration = time.time() - startTime

        startTime = time.time()
        try:
            minNumpy, maxNumpy = np.nanmin(data), np.nanmax(data)
        except ValueError:
            minNumpy, maxNumpy = None, None
        if minPos:
            minPositiveNumpy = self._minPos(data)
        durationNumpy = time.time() - startTime

        self._log(data.dtype, data.size, "duration C (s):", duration, "duration Numpy (s):", durationNumpy)

        self.assertEqual(min_, minNumpy)
        if minPos:
            self.assertEqual(minPositive, minPositiveNumpy)
        self.assertEqual(max_, maxNumpy)
Exemple #9
0
    def __init__(self, xData, yData, colorData=None,
                 xError=None, yError=None,
                 lineStyle=None, lineColor=None,
                 lineWidth=None, lineDashPeriod=None,
                 marker=None, markerColor=None, markerSize=None,
                 fillColor=None):
        self._isXLog = False
        self._isYLog = False
        self.xData, self.yData, self.colorData = xData, yData, colorData

        if fillColor is not None:
            self.fill = _Fill2D(color=fillColor)
        else:
            self.fill = None

        # Compute x bounds
        if xError is None:
            self.xMin, self.xMinPos, self.xMax = minMax(xData,
                                                        minPositive=True)
        else:
            # Takes the error into account
            if hasattr(xError, 'shape') and len(xError.shape) == 2:
                xErrorPlus, xErrorMinus = xError[0], xError[1]
            else:
                xErrorPlus, xErrorMinus = xError, xError
            self.xMin, self.xMinPos, _ = minMax(xData - xErrorMinus,
                                                minPositive=True)
            self.xMax = (xData + xErrorPlus).max()

        # Compute y bounds
        if yError is None:
            self.yMin, self.yMinPos, self.yMax = minMax(yData,
                                                        minPositive=True)
        else:
            # Takes the error into account
            if hasattr(yError, 'shape') and len(yError.shape) == 2:
                yErrorPlus, yErrorMinus = yError[0], yError[1]
            else:
                yErrorPlus, yErrorMinus = yError, yError
            self.yMin, self.yMinPos, _ = minMax(yData - yErrorMinus,
                                                minPositive=True)
            self.yMax = (yData + yErrorPlus).max()

        self._errorBars = _ErrorBars(xData, yData, xError, yError,
                                     self.xMin, self.yMin)

        kwargs = {'style': lineStyle}
        if lineColor is not None:
            kwargs['color'] = lineColor
        if lineWidth is not None:
            kwargs['width'] = lineWidth
        if lineDashPeriod is not None:
            kwargs['dashPeriod'] = lineDashPeriod
        self.lines = _Lines2D(**kwargs)

        kwargs = {'marker': marker}
        if markerColor is not None:
            kwargs['color'] = markerColor
        if markerSize is not None:
            kwargs['size'] = markerSize
        self.points = _Points2D(**kwargs)
Exemple #10
0
    def __init__(self,
                 xData,
                 yData,
                 colorData=None,
                 xError=None,
                 yError=None,
                 lineStyle=None,
                 lineColor=None,
                 lineWidth=None,
                 lineDashPeriod=None,
                 marker=None,
                 markerColor=None,
                 markerSize=None,
                 fillColor=None):
        self._isXLog = False
        self._isYLog = False
        self.xData, self.yData, self.colorData = xData, yData, colorData

        if fillColor is not None:
            self.fill = _Fill2D(color=fillColor)
        else:
            self.fill = None

        # Compute x bounds
        if xError is None:
            self.xMin, self.xMinPos, self.xMax = minMax(xData,
                                                        minPositive=True)
        else:
            # Takes the error into account
            if hasattr(xError, 'shape') and len(xError.shape) == 2:
                xErrorPlus, xErrorMinus = xError[0], xError[1]
            else:
                xErrorPlus, xErrorMinus = xError, xError
            self.xMin, self.xMinPos, _ = minMax(xData - xErrorMinus,
                                                minPositive=True)
            self.xMax = (xData + xErrorPlus).max()

        # Compute y bounds
        if yError is None:
            self.yMin, self.yMinPos, self.yMax = minMax(yData,
                                                        minPositive=True)
        else:
            # Takes the error into account
            if hasattr(yError, 'shape') and len(yError.shape) == 2:
                yErrorPlus, yErrorMinus = yError[0], yError[1]
            else:
                yErrorPlus, yErrorMinus = yError, yError
            self.yMin, self.yMinPos, _ = minMax(yData - yErrorMinus,
                                                minPositive=True)
            self.yMax = (yData + yErrorPlus).max()

        self._errorBars = _ErrorBars(xData, yData, xError, yError, self.xMin,
                                     self.yMin)

        kwargs = {'style': lineStyle}
        if lineColor is not None:
            kwargs['color'] = lineColor
        if lineWidth is not None:
            kwargs['width'] = lineWidth
        if lineDashPeriod is not None:
            kwargs['dashPeriod'] = lineDashPeriod
        self.lines = _Lines2D(**kwargs)

        kwargs = {'marker': marker}
        if markerColor is not None:
            kwargs['color'] = markerColor
        if markerSize is not None:
            kwargs['size'] = markerSize
        self.points = _Points2D(**kwargs)