1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
"""Records sets of histograms that hold average species populations."""
import numpy
from Histogram import Histogram
from SimulationOutput import SimulationOutput
class HistogramAverage(SimulationOutput):
"""A set of histograms."""
def __init__(self, numberOfBins, multiplicity, recordedSpecies=[]):
"""Construct an empty data structure."""
SimulationOutput.__init__(self, recordedSpecies, [])
self.numberOfBins = numberOfBins
self.multiplicity = multiplicity
self.numberOfTrajectories = 0
self.setRecordedSpecies(recordedSpecies)
def setRecordedSpecies(self, recordedSpecies):
"""Construct histograms for each recorded species."""
self.recordedSpecies = recordedSpecies
self.histograms = []
for s in self.recordedSpecies:
self.histograms.append(Histogram(self.numberOfBins,
self.multiplicity))
def setCurrentToMinimum(self):
"""Set the current histogram to the one with the minimum sum."""
index = self.histograms[0].findMinimum()
for h in self.histograms:
h.setCurrent(index)
def empty(self):
"""Return true if there are no trajectories."""
return self.numberOfTrajectories == 0
def size(self):
"""Return the number of trajectories."""
return self.numberOfTrajectories
def hasErrors(self):
"""Return None if data structure is valid. Otherwise return an error
message."""
error = SimulationOutput.hasErrors(self)
if error:
return error
if len(self.histograms) != len(self.recordedSpecies):
return 'The list of histograms is incomplete.'
# CONTINUE: Check the histograms.
return None
def writeXml(self, writer, model, method):
writer.beginElement('histogramAverage',
{'model':model, 'method':method,
'multiplicity':
str(self.histograms[0].multiplicity()),
'numberOfTrajectories':
repr(self.numberOfTrajectories)})
writer.beginElement('recordedSpecies')
writer.writeData(' '.join([repr(x) for x in self.recordedSpecies]))
writer.endElement() # recordedSpecies
for i in range(len(self.histograms)):
self.histograms[i].writeXml(writer, species=i)
writer.endElement() # histogramAverage
|