File: StatisticsFrames.py

package info (click to toggle)
cain 1.10%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 29,856 kB
  • sloc: cpp: 49,612; python: 14,988; xml: 11,654; ansic: 3,644; makefile: 133; sh: 2
file content (99 lines) | stat: -rw-r--r-- 3,812 bytes parent folder | download | duplicates (4)
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
"""Records the mean and standard deviation for solutions."""

import numpy

from SimulationOutput import SimulationOutput, isSorted

class StatisticsFrames(SimulationOutput):
    """The mean and standard deviation of the species populations at each frame.
    This class is used for representing solutions that are generated outside of
    Cain. The solution may be exact or emperical. We do not record the
    number of trajectories used to generate the solution. In Cain, the
    solution is treated as a reference whether it is exact or approximate."""
    
    def __init__(self, recordedSpecies=[]):
        """Construct an empty data structure."""
        # No recorded reactions.
        SimulationOutput.__init__(self, recordedSpecies, [])
        # The list of frame times.
        self.frameTimes = None
        # The 2-D list of (mean, standard deviation) tuples.
        # self.statistics[frame][species] gives the tuple for the specified
        # frame and species indices.
        self.statistics = None

    def setFrameTimes(self, frameTimes):
        self.frameTimes = numpy.array(frameTimes, numpy.float64)

    def setRecordedSpecies(self, recordedSpecies):
        self.recordedSpecies = recordedSpecies

    def setStatistics(self, data):
        """Use the flat list of mean/standard deviation pairs to set the
        statistics data."""
        f = len(self.frameTimes)
        n = len(self.recordedSpecies)
        assert len(data) == 2 * f * n
        self.statistics = [[(data[r*2*n+2*i], data[r*2*n+2*i+1]) for i in
                            range(n)] for r in range(f)]

    def size(self):
        """The number of trajectories is not tracked. Thus return the
        string 'n. a.' for not applicable."""
        return 'n. a.'

    def empty(self):
        """Return true if the statistics have not been set."""
        return not bool(self.statistics)

    def hasErrors(self):
        """Return None if data structure is valid. Otherwise return an error
        message."""
        error = SimulationOutput.hasErrors(self)
        if error:
            return error
        if self.frameTimes is None or len(self.frameTimes) <= 0:
            return 'There are no frame times.'
        if not isSorted(self.frameTimes):
            return 'The frame times are not in order.'
        if not len(self.statistics) == len(self.frameTimes):
            return 'The list of frames is incomplete.'
        for x in self.statistics:
            if not len(x) == len(self.recordedSpecies):
                return 'The list of statistics is incomplete.'
        return None

    def writeXml(self, writer, model, method):
        writer.beginElement('statisticsFrames',
                            {'model':model, 'method':method})
        writer.beginElement('frameTimes')
        writer.writeData(' '.join([repr(x) for x in self.frameTimes]))
        writer.endElement()
        writer.beginElement('recordedSpecies')
        writer.writeData(' '.join([repr(x) for x in self.recordedSpecies]))
        writer.endElement()
        writer.beginElement('statistics')
        writer.writeData(' '.join([' '.join([repr(x[0]) + ' ' + repr(x[1])
                                             for x in f]) for f in
                                   self.statistics]))
        writer.endElement()
        writer.endElement() # statisticsFrames

def main():
    import sys
    sys.path.insert(1, '..')
    from fio.XmlWriter import XmlWriter

    frameTimes = [0, 1]
    recordedSpecies = [0, 1, 2]
    x = StatisticsFrames(recordedSpecies)
    x.setFrameTimes(frameTimes)
    x.setStatistics([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])

    writer = XmlWriter()
    writer.beginDocument()
    x.writeXml(writer, 'model', 'method')
    writer.endDocument()

if __name__ == '__main__':
    main()