/usr/share/cain/state/TimeSeriesAllReactions.py is in cain 1.9-8.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | """Implements the TimeSeriesAllReactions class."""
import numpy
from SimulationOutput import SimulationOutput
def isSorted(x):
if len(x) == 0:
return True
for i in range(len(x) - 1):
if x[i] > x[i + 1]:
return False
return True
class TimeSeriesAllReactions(SimulationOutput):
"""Record the reaction indices and times."""
def __init__(self, recordedSpecies, recordedReactions, initialTime,
finalTime):
"""The recorded species and reactions should be all of the species
and reactions."""
SimulationOutput.__init__(self, recordedSpecies, recordedReactions)
# The initial time.
self.initialTime = initialTime
# The final time.
self.finalTime = finalTime
# The initial species populations.
self.initialPopulations = []
# The list of reaction index arrays.
self.indices = []
# The list of reaction time arrays.
self.times = []
def setRecordedSpecies(self, recordedSpecies):
self.recordedSpecies = recordedSpecies
def appendInitialPopulations(self, initialPopulations):
self.initialPopulations.append(numpy.array(initialPopulations,
numpy.float64))
def appendIndices(self, indices):
self.indices.append(numpy.array(indices, numpy.int32))
def appendTimes(self, times):
self.times.append(numpy.array(times, numpy.float64))
def empty(self):
"""Return true if there are no trajectories."""
return not self.indices
def size(self):
"""Return the number of trajectories."""
return len(self.indices)
def hasErrors(self):
"""Return None if the trajectory is valid. Otherwise return an error
message."""
error = SimulationOutput.hasErrors(self)
if error:
return error
numberOfReactions = len(self.recordedReactions)
if numberOfReactions == 0:
return 'There are no reactions.'
if not (self.initialTime < self.finalTime):
return 'Invalid time interval: [' + str(self.initialTime) +\
' .. ' + str(self.finalTime) + '].'
if len(self.initialPopulations) != len(self.indices):
return 'The number of initial populations and reation indices does not match.'
if len(self.initialPopulations) != len(self.times):
return 'The number of initial populations and reation times does not match.'
for i in range(len(self.initialPopulations)):
initialPopulations = self.initialPopulations[i]
indices = self.indices[i]
times = self.times[i]
if min(initialPopulations) < 0:
return 'The initial populations must be non-negative.'
if len(indices) != len(times):
return 'The number of reaction indices does not match the number of reaction times.'
if min(indices) < 0 or max(indices) >= numberOfReactions:
return 'Invalid reaction index.'
if min(times) < self.initialTime or max(times) > self.finalTime:
return 'Reaction time is outside the simulation time interval.'
if not isSorted(times):
return 'The reaction times are not ordered.'
return None
def writeXml(self, writer, model, method):
writer.beginElement('timeSeriesAllReactions',
{'model':model, 'method':method,
'initialTime':repr(self.initialTime),
'finalTime':repr(self.finalTime)})
for initialPopulations in self.initialPopulations:
writer.beginElement('initialPopulations')
writer.writeData(' '.join([repr(x) for x in initialPopulations]))
writer.endElement() # initialPopulations
for indices in self.indices:
writer.beginElement('indices')
writer.writeData(' '.join([repr(x) for x in indices]))
writer.endElement() # indices
for times in self.times:
writer.beginElement('times')
writer.writeData(' '.join([repr(x) for x in times]))
writer.endElement() # times
writer.endElement() # timeSeriesAllReactions
def main():
import sys
sys.path.insert(1, '..')
from fio.XmlWriter import XmlWriter
# One species, two reactions.
trajectory = TimeSeriesAllReactions([0], [0, 1], 1., 5.)
trajectory.appendInitialPopulations([7.])
trajectory.appendIndices([0, 1, 1, 0])
trajectory.appendTimes([1, 2, 3, 4])
writer = XmlWriter()
writer.beginDocument()
trajectory.writeXml(writer, 'model', 'method')
writer.endDocument()
if __name__ == '__main__':
main()
|