Пример #1
0
    def testA_BasicTest(self):
        """
        _testA_BasicTest_

        Build a histogram from a set of uniformly
        distributed pseudorandom numbers. Check
        that the statistic properties
        in the histogram are accurate to some degree,
        that the histogram binning is done right and
        that this can become a document an uploaded to couch
        """
        inputData = self.buildRandomNumberList(1000)

        histogram = ContinuousSummaryHistogram('TestHisto', 'MyLabel',
                                               'SomeoneElsesLabel')

        # Populate the histogram
        for point in inputData:
            histogram.addPoint(point)

        # Get the JSON
        jsonHistogram = histogram.toJSON()

        # Check the histogram core data
        self.assertEqual(jsonHistogram["title"], "TestHisto")
        self.assertEqual(jsonHistogram["xLabel"], "MyLabel")
        self.assertAlmostEqual(jsonHistogram["average"], 0.0, places=0)
        self.assertAlmostEqual(jsonHistogram["stdDev"], 1.0, places=0)
        self.assertEqual(len(jsonHistogram["data"]), 16)
        self.assertTrue(jsonHistogram["continuous"])

        # Check the internal data
        self.assertEqual(jsonHistogram["internalData"]["yLabel"],
                         "SomeoneElsesLabel")
        self.assertEqual(jsonHistogram["internalData"]["nPoints"], 1000)

        # Try to commit it to couch
        jsonHistogram["_id"] = jsonHistogram["title"]
        self.histogramDB.commitOne(jsonHistogram)

        storedJSON = self.histogramDB.document("TestHisto")
        self.assertEqual(len(storedJSON["data"]), 16)

        return
Пример #2
0
    def testC_compactHistogram(self):
        """
        _testC_compactHistogram_

        Check that we can create smaller histograms objects
        by chopping outliers and dropping the data all together
        """

        # Input normally distributed data and chop anything above 1 stdev (32% of data)
        histogram = ContinuousSummaryHistogram('TestHisto',
                                               'MyLabel',
                                               'SomeoneElsesLabel',
                                               dropOutliers=True,
                                               sigmaLimit=1)
        inputData = self.buildRandomNumberList(1000)
        for point in inputData:
            histogram.addPoint(point)
        jsonHistogram = histogram.toJSON()
        self.assertAlmostEqual(jsonHistogram["average"], 0.0, places=0)
        self.assertAlmostEqual(jsonHistogram["stdDev"], 1.0, places=0)
        self.assertEqual(len(jsonHistogram["data"]), 16)
        self.assertEqual(jsonHistogram["internalData"]["nPoints"], 1000)
        pointsInHistogram = sum([x for x in jsonHistogram["data"].values()])

        # With high probability we must have chopped at least one point
        self.assertTrue(pointsInHistogram < 1000)
        self.assertAlmostEqual(pointsInHistogram / 1000.0, 0.68, places=1)

        # Create a histogram without histogram data
        histogram = ContinuousSummaryHistogram('TestHisto',
                                               'MyLabel',
                                               'SomeoneElsesLabel',
                                               storeHistogram=False)
        inputData = self.buildRandomNumberList(1000)
        for point in inputData:
            histogram.addPoint(point)
        jsonHistogram = histogram.toJSON()
        self.assertAlmostEqual(jsonHistogram["average"], 0.0, places=0)
        self.assertAlmostEqual(jsonHistogram["stdDev"], 1.0, places=0)
        self.assertEqual(len(jsonHistogram["data"]), 0)
        self.assertEqual(jsonHistogram["internalData"]["nPoints"], 1000)

        return
    def testA_BasicTest(self):
        """
        _testA_BasicTest_

        Build a histogram from a set of uniformly
        distributed pseudorandom numbers. Check
        that the statistic properties
        in the histogram are accurate to some degree,
        that the histogram binning is done right and
        that this can become a document an uploaded to couch
        """
        inputData = self.buildRandomNumberList(1000)

        histogram = ContinuousSummaryHistogram('TestHisto', 'MyLabel', 'SomeoneElsesLabel')

        # Populate the histogram
        for point in inputData:
            histogram.addPoint(point)

        # Get the JSON
        jsonHistogram = histogram.toJSON()

        # Check the histogram core data
        self.assertEqual(jsonHistogram["title"], "TestHisto")
        self.assertEqual(jsonHistogram["xLabel"], "MyLabel")
        self.assertAlmostEqual(jsonHistogram["average"], 0.0, places = 0)
        self.assertAlmostEqual(jsonHistogram["stdDev"], 1.0, places = 0)
        self.assertEqual(len(jsonHistogram["data"]), 16)
        self.assertTrue(jsonHistogram["continuous"])

        # Check the internal data
        self.assertEqual(jsonHistogram["internalData"]["yLabel"], "SomeoneElsesLabel")
        self.assertEqual(jsonHistogram["internalData"]["nPoints"], 1000)

        # Try to commit it to couch
        jsonHistogram["_id"] = jsonHistogram["title"]
        self.histogramDB.commitOne(jsonHistogram)

        storedJSON = self.histogramDB.document("TestHisto")
        self.assertEqual(len(storedJSON["data"]), 16)

        return
    def testC_compactHistogram(self):
        """
        _testC_compactHistogram_

        Check that we can create smaller histograms objects
        by chopping outliers and dropping the data all together
        """

        # Input normally distributed data and chop anything above 1 stdev (32% of data)
        histogram = ContinuousSummaryHistogram('TestHisto', 'MyLabel', 'SomeoneElsesLabel',
                                               dropOutliers = True, sigmaLimit = 1)
        inputData = self.buildRandomNumberList(1000)
        for point in inputData:
            histogram.addPoint(point)
        jsonHistogram = histogram.toJSON()
        self.assertAlmostEqual(jsonHistogram["average"], 0.0, places = 0)
        self.assertAlmostEqual(jsonHistogram["stdDev"], 1.0, places = 0)
        self.assertEqual(len(jsonHistogram["data"]), 16)
        self.assertEqual(jsonHistogram["internalData"]["nPoints"], 1000)
        pointsInHistogram = sum([x for x in jsonHistogram["data"].values()])

        # With high probability we must have chopped at least one point
        self.assertTrue(pointsInHistogram < 1000)
        self.assertAlmostEqual(pointsInHistogram / 1000.0, 0.68, places = 1)

        # Create a histogram without histogram data
        histogram = ContinuousSummaryHistogram('TestHisto', 'MyLabel', 'SomeoneElsesLabel',
                                               storeHistogram = False)
        inputData = self.buildRandomNumberList(1000)
        for point in inputData:
            histogram.addPoint(point)
        jsonHistogram = histogram.toJSON()
        self.assertAlmostEqual(jsonHistogram["average"], 0.0, places = 0)
        self.assertAlmostEqual(jsonHistogram["stdDev"], 1.0, places = 0)
        self.assertEqual(len(jsonHistogram["data"]), 0)
        self.assertEqual(jsonHistogram["internalData"]["nPoints"], 1000)

        return
    def testB_extremeData(self):
        """
        _testB_extremeData_

        Put extreme points in the data and try to build a histogram.
        Check that it can process all this correctly
        """

        # First no data
        histogram = ContinuousSummaryHistogram('TestHisto', 'MyLabel', 'SomeoneElsesLabel')
        jsonHistogram = histogram.toJSON()
        self.assertEqual(jsonHistogram["title"], "TestHisto")
        self.assertEqual(jsonHistogram["xLabel"], "MyLabel")
        self.assertEqual(jsonHistogram["average"], 0.0)
        self.assertEqual(jsonHistogram["stdDev"], 0.0)
        self.assertEqual(len(jsonHistogram["data"]), 0)

        # Data with NaNs and Infs
        inputData = self.buildRandomNumberList(100)
        inputData.append(float('NaN'))
        inputData.append(float('Inf'))
        histogram = ContinuousSummaryHistogram('TestHisto', 'MyLabel', 'SomeoneElsesLabel')
        for point in inputData:
            histogram.addPoint(point)
        jsonHistogram = histogram.toJSON()
        self.assertAlmostEqual(jsonHistogram["average"], 0.0, places = 0)
        self.assertAlmostEqual(jsonHistogram["stdDev"], 1.0, places = 0)
        self.assertEqual(len(jsonHistogram["data"]), 7)
        self.assertEqual(jsonHistogram["internalData"]["nPoints"], 100)

        # One single point, P5
        histogram = ContinuousSummaryHistogram('TestHisto', 'MyLabel', 'SomeoneElsesLabel')
        histogram.addPoint(5)
        jsonHistogram = histogram.toJSON()
        self.assertEqual(jsonHistogram["average"], 5.0)
        self.assertEqual(jsonHistogram["stdDev"], 0.0)
        self.assertEqual(len(jsonHistogram["data"]), 1)
        self.assertEqual(jsonHistogram["data"]["5.0,5.0"], 1)
        self.assertEqual(jsonHistogram["internalData"]["nPoints"], 1)

        # Test that toJSON is idempotent
        inputData = self.buildRandomNumberList(100)
        histogram = ContinuousSummaryHistogram('TestHisto', 'MyLabel', 'SomeoneElsesLabel')
        for point in inputData:
            histogram.addPoint(point)
        jsonHistogram = histogram.toJSON()
        oldData = jsonHistogram["data"]
        jsonHistogram = histogram.toJSON()
        self.assertAlmostEqual(jsonHistogram["average"], 0.0, places = 0)
        self.assertAlmostEqual(jsonHistogram["stdDev"], 1.0, places = 0)
        self.assertEqual(len(jsonHistogram["data"]), 7)
        self.assertEqual(jsonHistogram["data"], oldData)
        self.assertEqual(jsonHistogram["internalData"]["nPoints"], 100)

        return
    def testB_extremeData(self):
        """
        _testB_extremeData_

        Put extreme points in the data and try to build a histogram.
        Check that it can process all this correctly
        """

        # First no data
        histogram = ContinuousSummaryHistogram('TestHisto', 'MyLabel', 'SomeoneElsesLabel')
        jsonHistogram = histogram.toJSON()
        self.assertEqual(jsonHistogram["title"], "TestHisto")
        self.assertEqual(jsonHistogram["xLabel"], "MyLabel")
        self.assertEqual(jsonHistogram["average"], 0.0)
        self.assertEqual(jsonHistogram["stdDev"], 0.0)
        self.assertEqual(len(jsonHistogram["data"]), 0)

        # Data with NaNs and Infs
        inputData = self.buildRandomNumberList(100)
        inputData.append(float('NaN'))
        inputData.append(float('Inf'))
        histogram = ContinuousSummaryHistogram('TestHisto', 'MyLabel', 'SomeoneElsesLabel')
        for point in inputData:
            histogram.addPoint(point)
        jsonHistogram = histogram.toJSON()
        self.assertAlmostEqual(jsonHistogram["average"], 0.0, places = 0)
        self.assertAlmostEqual(jsonHistogram["stdDev"], 1.0, places = 0)
        self.assertEqual(len(jsonHistogram["data"]), 7)
        self.assertEqual(jsonHistogram["internalData"]["nPoints"], 100)

        # One single point, P5
        histogram = ContinuousSummaryHistogram('TestHisto', 'MyLabel', 'SomeoneElsesLabel')
        histogram.addPoint(5)
        jsonHistogram = histogram.toJSON()
        self.assertEqual(jsonHistogram["average"], 5.0)
        self.assertEqual(jsonHistogram["stdDev"], 0.0)
        self.assertEqual(len(jsonHistogram["data"]), 1)
        self.assertEqual(jsonHistogram["data"]["5.0,5.0"], 1)
        self.assertEqual(jsonHistogram["internalData"]["nPoints"], 1)

        # Test that toJSON is idempotent
        inputData = self.buildRandomNumberList(100)
        histogram = ContinuousSummaryHistogram('TestHisto', 'MyLabel', 'SomeoneElsesLabel')
        for point in inputData:
            histogram.addPoint(point)
        jsonHistogram = histogram.toJSON()
        oldData = jsonHistogram["data"]
        jsonHistogram = histogram.toJSON()
        self.assertAlmostEqual(jsonHistogram["average"], 0.0, places = 0)
        self.assertAlmostEqual(jsonHistogram["stdDev"], 1.0, places = 0)
        self.assertEqual(len(jsonHistogram["data"]), 7)
        self.assertEqual(jsonHistogram["data"], oldData)
        self.assertEqual(jsonHistogram["internalData"]["nPoints"], 100)

        return