def testFinalizeAndQuery(self): """ Test the finalization and query functions. """ tsd = TimeStepDistribution() # Set the member data. histogram = numpy.array(numpy.random.rand(12) * 34, dtype=int) normalized_histogram = histogram / float(numpy.sum(histogram)) binsize = 2.13 tsd._TimeStepDistribution__binsize = binsize tsd._TimeStepDistribution__histogram = histogram # Call the finalize function. tsd.finalize() # We now have the normalized histogram and timesteps on the class. ret_histogram = tsd.histogram() ret_norm_histogram = tsd.normalizedHistogram() ret_time_steps = tsd.timeSteps() # Check the histograms. self.assertAlmostEqual(numpy.linalg.norm(ret_histogram - histogram), 0.0, 12) self.assertAlmostEqual( numpy.linalg.norm(ret_norm_histogram - normalized_histogram), 0.0, 12) # Check the time steps. time_steps = (numpy.arange(12) + 1) * binsize - binsize / 2.0 self.assertAlmostEqual(numpy.linalg.norm(ret_time_steps - time_steps), 0.0, 12)
def testFinalizeAndQuery(self): """ Test the finalization and query functions. """ tsd = TimeStepDistribution() # Set the member data. histogram = numpy.array(numpy.random.rand(12)*34, dtype=int) normalized_histogram = histogram / float(numpy.sum(histogram)) binsize = 2.13 tsd._TimeStepDistribution__binsize = binsize tsd._TimeStepDistribution__histogram = histogram # Call the finalize function. tsd.finalize() # We now have the normalized histogram and timesteps on the class. ret_histogram = tsd.histogram() ret_norm_histogram = tsd.normalizedHistogram() ret_time_steps = tsd.timeSteps() # Check the histograms. self.assertAlmostEqual(numpy.linalg.norm(ret_histogram - histogram), 0.0, 12) self.assertAlmostEqual(numpy.linalg.norm(ret_norm_histogram - normalized_histogram), 0.0, 12) # Check the time steps. time_steps = (numpy.arange(12)+1)*binsize - binsize/2.0 self.assertAlmostEqual(numpy.linalg.norm(ret_time_steps - time_steps), 0.0, 12)
def testPrintResults(self): """ Test the time step distribution print result function. """ tsd = TimeStepDistribution() # Set the member data. histogram = numpy.array([12,41,55, 23,43,12, 11,19,98, 97,95,93]) normalized_histogram = histogram / float(numpy.sum(histogram)) binsize = 2.13 tsd._TimeStepDistribution__binsize = binsize tsd._TimeStepDistribution__histogram = histogram # Call the finalize function. tsd.finalize() # Print the results to a stream. stream = StringIO.StringIO() tsd.printResults(stream) ref_value = """ 1.06500 12 0.020033388981636 3.19500 41 0.068447412353923 5.32500 55 0.091819699499165 7.45500 23 0.038397328881469 9.58500 43 0.071786310517529 11.71500 12 0.020033388981636 13.84500 11 0.018363939899833 15.97500 19 0.031719532554257 18.10500 98 0.163606010016695 20.23500 97 0.161936560934891 22.36500 95 0.158597662771285 24.49500 93 0.155258764607679 """ # Check the values. if MPICommons.isMaster(): self.assertEqual(stream.getvalue(), ref_value) else: self.assertEqual(stream.getvalue(), "")
def testPrintResults(self): """ Test the time step distribution print result function. """ tsd = TimeStepDistribution() # Set the member data. histogram = numpy.array( [12, 41, 55, 23, 43, 12, 11, 19, 98, 97, 95, 93]) normalized_histogram = histogram / float(numpy.sum(histogram)) binsize = 2.13 tsd._TimeStepDistribution__binsize = binsize tsd._TimeStepDistribution__histogram = histogram # Call the finalize function. tsd.finalize() # Print the results to a stream. stream = StringIO.StringIO() tsd.printResults(stream) ref_value = """ 1.06500 12 0.020033388981636 3.19500 41 0.068447412353923 5.32500 55 0.091819699499165 7.45500 23 0.038397328881469 9.58500 43 0.071786310517529 11.71500 12 0.020033388981636 13.84500 11 0.018363939899833 15.97500 19 0.031719532554257 18.10500 98 0.163606010016695 20.23500 97 0.161936560934891 22.36500 95 0.158597662771285 24.49500 93 0.155258764607679 """ # Check the values. if MPICommons.isMaster(): self.assertEqual(stream.getvalue(), ref_value) else: self.assertEqual(stream.getvalue(), "")