def toJSON(self): """ _toJSON_ Bin the histogram if any, calculate the standard deviation. Store the internal data needed for reconstruction of the histogram from JSON and call superclass toJSON method. """ if self.nPoints: self.stdDev = calculateStdDevFromQ(self.QValue, self.nPoints) if not self.binned and self.storeHistogram: self.binHistogram() self.jsonInternal = {} self.jsonInternal['yLabel'] = self.yLabel self.jsonInternal['QValue'] = self.QValue self.jsonInternal['nPoints'] = self.nPoints return SummaryHistogram.toJSON(self)
def binHistogram(self): """ _binHistogram_ Histograms of continuous data must be binned, this takes care of that using given or optimal parameters. Note that this modifies the data object, and points can't be added to the histogram after this. """ if not self.nPoints: return self.binned = True # Number of bins can be specified or calculated based on number of points nBins = self.fixedNBins if nBins is None: nBins = int( math.floor((5.0 / 3.0) * math.pow(self.nPoints, 1.0 / 3.0))) # Define min and max if not self.dropOutliers: upperLimit = max(self.data.keys()) lowerLimit = min(self.data.keys()) else: stdDev = calculateStdDevFromQ(self.QValue, self.nPoints) upperLimit = self.average + (stdDev * self.sigmaLimit) lowerLimit = self.average - (stdDev * self.sigmaLimit) # Incremental delta delta = abs(float(upperLimit - lowerLimit)) / nBins # Build the bins, it's a list of tuples for now bins = [] a = lowerLimit b = lowerLimit + delta while len(bins) < nBins: bins.append((a, b)) a += delta b += delta # Go through data and populate the binned histogram binnedHisto = {} currentBin = 0 currentPoint = 0 sortedData = sorted(self.data.keys()) while currentPoint < len(sortedData): point = sortedData[currentPoint] encodedTuple = "%s,%s" % (bins[currentBin][0], bins[currentBin][1]) if encodedTuple not in binnedHisto: binnedHisto[encodedTuple] = 0 if point > upperLimit or point < lowerLimit: currentPoint += 1 elif currentBin == len(bins) - 1: binnedHisto[encodedTuple] += self.data[point] currentPoint += 1 elif point >= bins[currentBin][0] and point < bins[currentBin][1]: binnedHisto[encodedTuple] += self.data[point] currentPoint += 1 else: currentBin += 1 self.data = binnedHisto return
def binHistogram(self): """ _binHistogram_ Histograms of continuous data must be binned, this takes care of that using given or optimal parameters. Note that this modifies the data object, and points can't be added to the histogram after this. """ if not self.nPoints: return self.binned = True # Number of bins can be specified or calculated based on number of points nBins = self.fixedNBins if nBins is None: nBins = int(math.floor((5.0 / 3.0) * math.pow(self.nPoints, 1.0 / 3.0))) # Define min and max if not self.dropOutliers: upperLimit = max(self.data.keys()) lowerLimit = min(self.data.keys()) else: stdDev = calculateStdDevFromQ(self.QValue, self.nPoints) upperLimit = self.average + (stdDev * self.sigmaLimit) lowerLimit = self.average - (stdDev * self.sigmaLimit) # Incremental delta delta = abs(float(upperLimit - lowerLimit)) / nBins # Build the bins, it's a list of tuples for now bins = [] a = lowerLimit b = lowerLimit + delta while len(bins) < nBins: bins.append((a, b)) a += delta b += delta # Go through data and populate the binned histogram binnedHisto = {} currentBin = 0 currentPoint = 0 sortedData = sorted(self.data.keys()) while currentPoint < len(sortedData): point = sortedData[currentPoint] encodedTuple = "%s,%s" % (bins[currentBin][0], bins[currentBin][1]) if encodedTuple not in binnedHisto: binnedHisto[encodedTuple] = 0 if point > upperLimit or point < lowerLimit: currentPoint += 1 elif currentBin == len(bins) - 1: binnedHisto[encodedTuple] += self.data[point] currentPoint += 1 elif point >= bins[currentBin][0] and point < bins[currentBin][1]: binnedHisto[encodedTuple] += self.data[point] currentPoint += 1 else: currentBin += 1 self.data = binnedHisto return