def test(self): numberOfBins = 4 multiplicity = 2 h = Histogram(numberOfBins, multiplicity) h.setCurrentToMinimum() h.accumulate(0, 1) h.accumulate(1, 2) h.accumulate(2, 2) h.accumulate(3, 1) frameTimes = [0, 1] recordedSpecies = [0, 1, 2] x = HistogramFrames(numberOfBins, multiplicity, recordedSpecies) x.setFrameTimes(frameTimes) for i in range(2): x.setCurrentToMinimum() for i in range(len(frameTimes)): for j in range(len(recordedSpecies)): x.histograms[i][j].merge(h) assert not x.hasErrors() stream = StringIO() writer = XmlWriter(stream) writer.beginDocument() x.writeXml(writer, 'model', 'method') writer.endDocument()
def testMoreBasic(self): # More basic tests. x = Histogram(8, 2) x.setCurrentToMinimum() x.accumulate(4, 1.) assert x.getMean() == 4 assert x.getUnbiasedVariance() == float('inf') assert x.size() == 8 assert x.min() == 4. assert x.max() == 5. assert x._upperBound() == 8.
def testConstantValueBins(self): # Basic tests with constant-value bins. size = 8 values = range(size) weights = [1.] * size mean, variance = _computeMeanAndVariance(values, weights) x = Histogram(size, 2) x.setCurrentToMinimum() for i in range(8): x.accumulate(i, 1.) self.assertAlmostEqual(x.getMean(), mean) self.assertAlmostEqual(x.getUnbiasedVariance(), variance) self.assertEqual(x.size(), 8) self.assertEqual(x.min(), 0.) self.assertEqual(x.max(), 8.) self.assertEqual(x._upperBound(), 8.) p = x.getProbabilities() self.assertTrue((p == 1./8.).all())
def testPoisson2(self): # Poisson with mean 4. Use accumulate. lam = 4. size = 20 x = Histogram(size, 2) x.setCurrentToMinimum() weight = math.exp(-lam) for i in range(50): x.accumulate(i, weight) weight *= lam / (i + 1) assert abs(x.getMean() - 4) < 1e-4 assert abs(x.getUnbiasedVariance() - 4) < 1e-1 # Uniform, 20 bins. size = 20 weights = [1.] * size cardinality = size sumOfWeights = sum(weights) mean = 0. for i in range(size): mean += weights[i] * i mean /= sumOfWeights summedSecondCenteredMoment = 0. for i in range(size): summedSecondCenteredMoment += weights[i] * (i - mean)**2 stream = StringIO(repr(cardinality) + '\n' + repr(sumOfWeights) + '\n' + repr(mean) + '\n' + repr(summedSecondCenteredMoment) + '\n' + '0\n1\n' + ''.join([str(_x) + ' ' for _x in [1.] * size]) + '\n' + '0 ' * size + '\n') y = Histogram() y.read(stream, 2) # Distance between Poisson and uniform. assert histogramDistance(x, y) > 0 # Distance between Poisson and Poisson. assert histogramDistance(x, x) == 0 # Uniform, 30 bins. x.merge(y)
def testAccumulate1(self): x = Histogram(8, 2) x.setCurrentToMinimum() x.accumulate(0, 1) assert x.size() == 8 assert x.min() == 0. assert x.max() == 1. assert x._upperBound() == 8. x.accumulate(7, 1) assert x.size() == 8 assert x.min() == 0. assert x.max() == 8. assert x._upperBound() == 8. x.accumulate(8, 1) assert x.size() == 8 assert x.min() == 0. assert x.max() == 10. assert x._upperBound() == 16. x.accumulate(15, 1) assert x.size() == 8 assert x.min() == 0. assert x.max() == 16. assert x._upperBound() == 16.
def testAccumulate2(self): x = Histogram(8, 2) x.setCurrentToMinimum() x.accumulate(10, 1) assert x.size() == 8 assert x.min() == 10. assert x.max() == 11. assert x._upperBound() == 18. x.accumulate(5, 1) assert x.size() == 8 assert x.min() == 5. assert x.max() == 11. assert x._upperBound() == 13. x.accumulate(0, 1) assert x.size() == 8 assert x.min() == 0. assert x.max() == 12. assert x._upperBound() == 16.
# For each sample size. histogram = Histogram(converged[0].size()) for sample in samples: # The average run time. time = 0. for start in range(0, size, sample): if start + sample > size: break for i in range(start, start + sample): time += float(lines[i * (numberOfHistograms + 1)]) time /= size // sample # Compute the average error. averageError = 0. # For each recorded species. for h in range(numberOfHistograms): error = 0. for start in range(0, size, sample): histogram.clear() if start + sample > size: break for i in range(start, start + sample): n = i * (numberOfHistograms + 1) + h + 1 data = [float(_x) for _x in lines[n].split()] for j in range(0, len(data), 2): histogram.accumulate(data[j], data[j + 1], 0) error += histogramDistance(histogram, converged[h]) error /= size // sample averageError += error averageError /= numberOfHistograms report.write('%s %s\n' % (time, averageError))
converged.read(open('converged.txt', 'r')) histogram = Histogram(converged.size()) lines = open(directory + '/output.txt', 'r').readlines() size = len(lines) report = open(directory + '/error.txt', 'w') report.write('#time %s\n' % directory) # Determine the samples. samples = [] sample = size while sample > 0: samples.append(sample) sample //= 2 samples.sort() # For each sample size. for sample in samples: error = 0. time = 0. for start in range(0, size, sample): histogram.clear() if start + sample > size: break for i in range(start, start + sample): data = [float(_x) for _x in lines[i].split()] time += data[0] for i in range(1, len(data), 2): histogram.accumulate(data[i], data[i + 1], 0) error += histogramDistance(histogram, converged) time /= size // sample error /= size // sample report.write('%s %s\n' % (time, error))