Beispiel #1
0
def writeModelSbml(model, out=None):
    if out:
        writer = XmlWriter(out)
    else:
        writer = XmlWriter()
    writer.beginDocument()
    model.writeSbml(writer, 3)
    writer.endDocument()
Beispiel #2
0
def writeMethodXml(simulation, out=None):
    if out:
        writer = XmlWriter(out)
    else:
        writer = XmlWriter()
    writer.beginDocument()
    simulation.writeXml(writer)
    writer.endDocument()
Beispiel #3
0
def main():
    import math
    import sys
    sys.path.insert(1, '..')

    from Value import Value
    from ParameterEvaluation import evaluateInitialAmounts
    from io.XmlWriter import XmlWriter
    writer = XmlWriter()

    x = Species('C1', 'species 1', '0')
    x.writeXml(writer, 's1')

    x = Species('C1', 'species 1', '')
    print evaluateInitialAmounts({'s1': x}, {})

    x = Species('C1', 'species 2', '1')
    x.writeXml(writer, 's2')

    x = Species('C1', 'species 2', '-1')
    # I don't check the initial amount expression with hasErrors().
    print evaluateInitialAmounts({'s2': x}, {})

    x = Species('C1', 'species 2', 'p1')
    print evaluateInitialAmounts({'s1': x}, {})

    x = Species('C1', 'species 2', 'p2')
    p = Value('', '5.0')
    p.value = 5.0
    print evaluateInitialAmounts({'s1': x}, {'p1': p})
Beispiel #4
0
    def test(self):
        assert TriggerEvent('e1', '', '', '', 0, True).hasErrors()
        assert TriggerEvent('e1', '', '', '', '', True).hasErrors()
        assert TriggerEvent('e1', '', '', '', 0., 0).hasErrors()
        assert not TriggerEvent('e1', '', '', '', 0., True).hasErrors()

        # CONTINUE: '>' is converted to '>'. is this OK?
        x = TriggerEvent('e1', '', 't>1', 'p1=1; p2=2', 1., True)
        assert not x.hasErrors()
        writer = XmlWriter(StringIO())
        x.writeXml(writer)
Beispiel #5
0
def main():
    import math
    from ParameterEvaluation import evaluateValues
    from io.XmlWriter import XmlWriter

    writer = XmlWriter()

    x = Value('', '')
    x.name = 'Parameter 1'
    x.expression = '3'
    x.value = 3.0
    x.writeParameterXml(writer, 'P1')
    x.writeParameterSbml(writer, 'P1')
    x.writeCompartmentXml(writer, 'C1')
    x.writeCompartmentSbml(writer, 'C1')

    # Invalid identifers.
    print('\nInvalid identifers:')
    x = Value('', '')
    result = evaluateValues({'': x})
    print(result)
    result = evaluateValues({' ': x})
    print(result)
    result = evaluateValues({'2x': x})
    print(result)
    result = evaluateValues({'a.txt': x})
    print(result)

    # Invalid expressions.
    print('\nInvalid expressions:')
    x.expression = ''
    result = evaluateValues({'a': x})
    print(result)
    x.expression = ' '
    result = evaluateValues({'a': x})
    print(result)
    x.expression = 'x'
    result = evaluateValues({'a': x})
    print(result)
    x.expression = '1 2'
    result = evaluateValues({'a': x})
    print(result)
    x.expression = 'a'
    result = evaluateValues({'a': x})
    print(result)

    # Invalid expressions for two parameters.
    print('\nInvalid expressions for two parameters:')
    y = Value('', '')
    x.expression = 'b'
    y.expression = 'a'
    result = evaluateValues({'a': x, 'b': y})
    print(result)
Beispiel #6
0
    def test(self):
        assert TimeEvent('e1', '', '', '').hasErrors()

        x = TimeEvent('e1', '', '[0]', 'p1=1; p2=2')
        assert not x.hasErrors()
        writer = XmlWriter(StringIO())
        x.writeXml(writer)

        assert TimeEvent('e1', '', '1', '').hasErrors()
        assert not TimeEvent('e1', '', 'range(10)', '').hasErrors()
        assert not TimeEvent('e1', '', '[0.1 * i for i in range(10)]', '').\
               hasErrors()
Beispiel #7
0
    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()
Beispiel #8
0
def main():
    import sys
    sys.path.insert(1, '..')
    from io.XmlWriter import XmlWriter

    writer = XmlWriter()
    
    identifiers = ['s1', 's2']
    x = SpeciesReference('s1')
    x.writeXml(writer)

    x = SpeciesReference('s1', 1)
    x.writeXml(writer)

    x = SpeciesReference('s2', 2)
    x.writeXml(writer)
Beispiel #9
0
    def test(self):
        trajectory = TimeSeriesFrames([0, 1])
        trajectory.recordedSpecies = [0, 1]
        trajectory.recordedReactions = [0, 1]
        trajectory.appendPopulations([2, 3, 7, 11])
        trajectory.appendReactionCounts([13, 17, 19, 23])
        assert not trajectory.hasErrors()

        writer = XmlWriter(StringIO())
        writer.beginDocument()
        trajectory.writeXml(writer, 'model', 'method')
        writer.endDocument()
Beispiel #10
0
def main():
    import sys
    sys.path.insert(1, '..')
    from io.XmlWriter import XmlWriter

    recordedSpecies = [0, 1, 2]
    x = StatisticsAverage(recordedSpecies)
    x.setStatistics([1, 2, 3, 4, 5, 6])
    writer = XmlWriter()
    writer.beginDocument()
    x.writeXml(writer, 'model', 'method')
    writer.endDocument()
Beispiel #11
0
    def test(self):
        # 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])
        assert not trajectory.hasErrors()

        writer = XmlWriter(StringIO())
        writer.beginDocument()
        trajectory.writeXml(writer, 'model', 'method')
        writer.endDocument()
Beispiel #12
0
    def test(self):
        recordedSpecies = [0, 1, 2]
        x = StatisticsAverage(recordedSpecies)
        x.setStatistics([1, 2, 3, 4, 5, 6])
        assert x.statistics[0] == (1, 2)
        assert x.statistics[1] == (3, 4)
        assert x.statistics[2] == (5, 6)
        assert not x.hasErrors()

        writer = XmlWriter(StringIO())
        writer.beginDocument()
        x.writeXml(writer, 'model', 'method')
        writer.endDocument()
Beispiel #13
0
    def test(self):
        stream = StringIO()
        writer = XmlWriter(stream)

        identifiers = ['s1', 's2']
        x = SpeciesReference('s1')
        assert not x.hasErrors(identifiers)
        assert x.hasErrors([])
        x.writeXml(writer)

        x = SpeciesReference('s1', 1)
        assert not x.hasErrors(identifiers)
        assert x.hasErrors([])
        x.writeXml(writer)

        x = SpeciesReference('s2', 2)
        assert not x.hasErrors(identifiers)
        assert x.hasErrors([])
        x.writeXml(writer)
Beispiel #14
0
def main():
    import sys
    sys.path.insert(1, '..')
    from io.XmlWriter import XmlWriter

    trajectory = TimeSeriesFrames([0, 1])
    trajectory.recordedSpecies = [0, 1]
    trajectory.recordedReactions = [0, 1]
    trajectory.appendPopulations([2, 3, 7, 11])
    trajectory.appendReactionCounts([13, 17, 19, 23])

    writer = XmlWriter()
    writer.beginDocument()
    trajectory.writeXml(writer, 'model', 'method')
    writer.endDocument()
Beispiel #15
0
def main():
    import sys
    sys.path.insert(1, '..')
    from io.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()
Beispiel #16
0
def main():
    import sys
    sys.path.insert(1, '..')
    from io.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()
Beispiel #17
0
    def test(self):
        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])
        assert x.statistics[0][0] == (1, 2)
        assert x.statistics[0][1] == (3, 4)
        assert x.statistics[0][2] == (5, 6)
        assert x.statistics[1][0] == (7, 8)
        assert x.statistics[1][1] == (9, 10)
        assert x.statistics[1][2] == (11, 12)
        assert not x.hasErrors()

        writer = XmlWriter(StringIO())
        writer.beginDocument()
        x.writeXml(writer, 'model', 'method')
        writer.endDocument()
Beispiel #18
0
    def testPoisson1(self):
        # Poisson with mean 4. 20 bins. PMF = e^-lambda lambda^n / n!
        # Store in the first array of bins.
        lam = 4.
        size = 20
        poisson = [math.exp(-lam)]
        for n in range(1, size):
            poisson.append(poisson[-1] * lam / n)
        cardinality = size
        sumOfWeights = sum(poisson)
        mean = 0.
        for i in range(size):
            mean += poisson[i] * i
        mean /= sumOfWeights
        summedSecondCenteredMoment = 0.
        for i in range(size):
            summedSecondCenteredMoment += poisson[i] * (i - mean)**2

        stream = StringIO(repr(cardinality) + '\n' + 
                          repr(sumOfWeights) + '\n' +
                          repr(mean) + '\n' + 
                          repr(summedSecondCenteredMoment) + '\n' + 
                          '0\n1\n' +
                          ''.join([repr(_x) + ' ' for _x in poisson]) + '\n' +
                          '0 ' * len(poisson) + '\n')

        x = Histogram()
        x.read(stream, 2)
        assert x.cardinality == cardinality
        assert x.sumOfWeights == sumOfWeights
        assert x.mean == mean
        assert x.summedSecondCenteredMoment == summedSecondCenteredMoment
        assert x.size() == len(poisson)
        self.assertAlmostEqual(sum(x.getProbabilities()), 1)

        stream = StringIO()
        writer = XmlWriter(stream)
        writer.beginDocument()
        x.writeXml(writer, 0, 0)
        writer.endDocument()
Beispiel #19
0
    def test(self):
        writer = XmlWriter(StringIO())

        x = Value('', '')
        assert x.hasErrors()

        x.name = 'Parameter 1'
        x.expression = '3'
        x.value = 3.0
        assert not x.hasErrors()
        x.writeParameterXml(writer, 'P1')
        x.writeParameterSbml(writer, 'P1')
        x.writeCompartmentXml(writer, 'C1')
        x.writeCompartmentSbml(writer, 'C1')

        # Invalid identifers.
        x = Value('', '')
        result = evaluateValues({'':x})
        assert result
        result = evaluateValues({' ':x})
        assert result
        result = evaluateValues({'2x':x})
        assert result
        result = evaluateValues({'a.txt':x})
        assert result

        # Invalid expressions.
        x.expression = ''
        result = evaluateValues({'a':x})
        assert result
        x.expression = ' '
        result = evaluateValues({'a':x})
        assert result
        x.expression = 'x'
        result = evaluateValues({'a':x})
        assert result
        x.expression = '1 2'
        result = evaluateValues({'a':x})
        assert result
        x.expression = 'a'
        result = evaluateValues({'a':x})
        assert result

        # Valid expressions for a single parameter.
        for key in math.__dict__.keys():
            x.expression = '1'
            assert not evaluateValues({key:x})
            assert x.value == 1

        x.expression = '1.0'
        result = evaluateValues({'a':x})
        assert not result
        assert x.value == 1

        x.expression = '1.0'
        result = evaluateValues({'lambda':x})
        assert not result
        assert x.value == 1

        x.expression = '1.0'
        result = evaluateValues({'e':x})
        assert not result
        assert x.value == 1

        x.expression = '1.0'
        result = evaluateValues({'pi':x})
        assert not result
        assert x.value == 1

        x.expression = '1.0'
        result = evaluateValues({'sin':x})
        assert not result
        assert x.value == 1

        x.expression = '1'
        result = evaluateValues({'a':x})
        assert not result
        assert x.value == 1

        x.expression = '2 * 3'
        result = evaluateValues({'a':x})
        assert not result
        assert x.value == 2 * 3

        x.expression = '2 - 3'
        result = evaluateValues({'a':x})
        assert not result
        assert x.value == 2 - 3

        x.expression = '2 ** 3'
        result = evaluateValues({'a':x})
        assert not result
        assert x.value == 2**3

        x.expression = '2./3'
        result = evaluateValues({'a':x})
        assert not result
        assert x.value == 2./3

        x.expression = 'pi'
        result = evaluateValues({'a':x})
        assert not result
        assert x.value == math.pi

        x.expression = 'e'
        result = evaluateValues({'a':x})
        assert not result
        assert x.value == math.e

        x.expression = 'sqrt(2)'
        result = evaluateValues({'a':x})
        assert not result
        assert x.value == math.sqrt(2)

        x.expression = 'log(2)'
        result = evaluateValues({'a':x})
        assert not result
        assert x.value == math.log(2)


        # Valid expressions for two parameters.
        y = Value('', '')
        x.expression = 'pi'
        y.expression = 'e'
        result = evaluateValues({'a':x, 'b':y})
        assert not result
        assert x.value == math.pi
        assert y.value == math.e

        x.expression = '1.0'
        y.expression = 'a'
        result = evaluateValues({'a':x, 'b':y})
        assert not result
        assert x.value == 1
        assert y.value == 1

        x.expression = 'b'
        y.expression = '1'
        result = evaluateValues({'a':x, 'b':y})
        assert not result
        assert x.value == 1
        assert y.value == 1

        x.expression = '2'
        y.expression = 'sqrt(a)'
        result = evaluateValues({'a':x, 'b':y})
        assert not result
        assert x.value == 2
        assert y.value == math.sqrt(2)

        x.expression = '2**3'
        y.expression = 'sqrt(a)'
        result = evaluateValues({'a':x, 'b':y})
        assert not result
        assert x.value == 2**3
        assert y.value == math.sqrt(2**3)

        # Invalid expressions for two parameters.
        y = Value('', '')
        x.expression = 'b'
        y.expression = 'a'
        result = evaluateValues({'a':x, 'b':y})
        assert result

        # Valid expressions for three parameters.
        z = Value('', '')
        x.expression = 'pi'
        y.expression = 'a**2'
        z.expression = 'sqrt(a + b)'
        result = evaluateValues({'a':x, 'b':y, 'c':z})
        assert not result
        assert x.value == math.pi
        assert y.value == x.value**2
        assert z.value == math.sqrt(x.value + y.value)
Beispiel #20
0
            attributes['name'] = self.name
        writer.writeEmptyElement('timeEvent', attributes)

    def readXml(self, attributes):
        """Read from an attributes dictionary. Return any errors encountered."""
        # The attribute "dictionary" may not work as it should. In particular
        # the test "x in attributes" may not work. Thus we need to directly
        # use attributes.keys().
        keys = attributes.keys()
        for x in ['id', 'times', 'assignments']:
            if not x in keys:
                return 'Missing ' + x + ' attribute in time event.\n'
        self.id = attributes['id']
        if 'name' in keys:
            self.name = attributes['name']
        else:
            self.name = ''
        self.times = attributes['times']
        self.assignments = attributes['assignments']
        return ''


if __name__ == '__main__':
    import sys
    sys.path.insert(1, '..')
    from io.XmlWriter import XmlWriter

    x = TimeEvent('e1', '', '[0]', 'p1=1; p2=2')
    writer = XmlWriter()
    x.writeXml(writer)
Beispiel #21
0
def main():
    import sys

    sys.path.insert(1, '..')
    from io.XmlWriter import XmlWriter
    from ParameterEvaluation import evaluatePropensityFactors

    writer = XmlWriter()

    assert Reaction('r1', '', [], [], False, '').hasErrors([], True)

    # Mass action kinetics.

    identifiers = ['s1', 's2']
    a = SpeciesReference('s1', 1)
    x = Reaction('r1', '1', [a], [], True, '0')
    evaluatePropensityFactors([x], {})
    x.writeXml(writer)
    print(x.makeMassActionPropensityFunction('x', identifiers, True))
    print(x.makeMassActionPropensityFunction('x', identifiers, False))
    print(x.makeMassActionPropensityFunctionMathematica(identifiers))
    print('Mass action term:')
    print(x.makeMassActionTerm())
    print('')

    p = SpeciesReference('s2', 1)
    x = Reaction('r1', 'reaction', [a], [p], True, '1')
    evaluatePropensityFactors([x], {})
    x.writeXml(writer)
    print(x.makeMassActionPropensityFunction('x', identifiers, True))
    print(x.makeMassActionPropensityFunction('x', identifiers, False))
    print(x.makeMassActionPropensityFunctionMathematica(identifiers))
    print('')

    b = SpeciesReference('s2', 2)
    x = Reaction('r1', 'name', [a, b], [p], True, '3')
    evaluatePropensityFactors([x], {})
    x.writeXml(writer)
    print(x.makeMassActionPropensityFunction('x', identifiers, True))
    print(x.makeMassActionPropensityFunction('x', identifiers, False))
    print(x.makeMassActionPropensityFunctionMathematica(identifiers))
    print('')

    print('Time inhomogeneous')
    p = SpeciesReference('s2', 1)
    x = Reaction('r1', 'reaction', [a], [p], True, '2+sin(t)')
    x.writeXml(writer)
    print(x.makeInhomogeneousMassActionPropensityFunction
          ('x', identifiers, True))
    print(x.makeInhomogeneousMassActionPropensityFunction
          ('x', identifiers, False))
    print('')

    p = SpeciesReference('s2', 1)
    x = Reaction('r1', 'name', [a, b], [p], True, '2+sin(t)')
    x.writeXml(writer)
    print(x.makeInhomogeneousMassActionPropensityFunction
          ('x', identifiers, True))
    print(x.makeInhomogeneousMassActionPropensityFunction
          ('x', identifiers, False))
    print('')

    # Custom kinetics.

    x.massAction = False
    x.propensity = '5*s1*s2'
    x.writeXml(writer)
    print('')

    x.propensity = '5'
    x.writeXml(writer)
    print('')

    x.propensity = 's10'
    x.writeXml(writer)
    print('')

    identifiers.append('s3')
    x.propensity = 's3'
    x.writeXml(writer)
    print(x.hasErrors(identifiers, True))
    print('')