Exemplo n.º 1
0
def main():
    parser = optparse.OptionParser(description="Produces `.dot` output from `.chem` input.")
    parser.add_option("-i", "--infile",  dest="infile",  help="read from INFILE in .chem format (if ommited, use stdin)", metavar="INFILE")
    parser.add_option("-o", "--outfile", dest="outfile", help="write to OUTFILE in .dot format (if ommited, use stdout)", metavar="OUTFILE")
    parser.add_option("-n", "--names", dest="names", help="style of molecular species naming. One of 'full', 'id', 'blank'", metavar="NAMES", default=None)
    parser.add_option("-l", "--layout", dest="layout", help="layout to assign to the dot file")
    
    (options, args) = parser.parse_args()
    rn = None
    if options.infile is None:
        #read from standard in
        rn = ReactionNetwork.from_string(sys.stdin.read())
    else:
        #read from provided filename
        rn = ReactionNetwork.from_filename(options.infile)

    
    dot = net_to_dot(rn, names=options.names)
    if options.layout is not None:
        dot["graph"]["layout"] = options.layout
        
    dotstr = str(dot)

    if options.outfile is None:
        #print to standard out
        sys.stdout.write(dotstr)
    else:
        #write to provided filename
        outfile = open(options.outfile, "w")
        outfile.write(dotstr)
        outfile.close()
Exemplo n.º 2
0
def main():
    parser = optparse.OptionParser(
        description="Pretty-printer and syntax checker for .chem files.")
    parser.add_option("-i",
                      "--infile",
                      dest="infile",
                      help="read from INFILE (if ommited, use stdin)",
                      metavar="INFILE")
    parser.add_option(
        "-o",
        "--outfile",
        dest="outfile",
        help="write to OUTFILE in .chem format (if ommited, use stdout)",
        metavar="OUTFILE")
    (options, args) = parser.parse_args()
    rn = None
    if options.infile is None:
        #read from standard in
        rn = ReactionNetwork.from_string(sys.stdin.read())
    else:
        #read from provided filename
        rn = ReactionNetwork.from_filename(options.infile)

    chemstr = str(rn)

    if options.outfile is None:
        #print to standard out
        sys.stdout.write(chemstr)
    else:
        #write to provided filename
        outfile = open(options.outfile, "w")
        outfile.write(chemstr)
        outfile.close()
Exemplo n.º 3
0
def main():
    parser = optparse.OptionParser(description="Pretty-printer and syntax checker for .chem files.")
    parser.add_option(
        "-i", "--infile", dest="infile", help="read from INFILE (if ommited, use stdin)", metavar="INFILE"
    )
    parser.add_option(
        "-o",
        "--outfile",
        dest="outfile",
        help="write to OUTFILE in .chem format (if ommited, use stdout)",
        metavar="OUTFILE",
    )
    (options, args) = parser.parse_args()
    rn = None
    if options.infile is None:
        # read from standard in
        rn = ReactionNetwork.from_string(sys.stdin.read())
    else:
        # read from provided filename
        rn = ReactionNetwork.from_filename(options.infile)

    chemstr = str(rn)

    if options.outfile is None:
        # print to standard out
        sys.stdout.write(chemstr)
    else:
        # write to provided filename
        outfile = open(options.outfile, "w")
        outfile.write(chemstr)
        outfile.close()
Exemplo n.º 4
0
    def setUp(self):
        self.rates = {(("A", "B"), ("B", "C")): 2.0}
        self.net = ReactionNetwork(self.rates)

        self.othernet = ReactionNetwork(self.rates)

        self.wrongrates = {(("A", "B"), ("B", "C")): 3.0}
        self.wrongnet = ReactionNetwork(self.wrongrates)
Exemplo n.º 5
0
    def setUp(self):
        self.rates = {
            (OrderedFrozenBag(["A", "B"]), OrderedFrozenBag(["B", "C"])): 2.0
        }
        self.net = ReactionNetwork(self.rates)

        self.othernet = ReactionNetwork(self.rates)

        self.wrongrates = {
            (OrderedFrozenBag(["A", "B"]), OrderedFrozenBag(["B", "C"])): 3.0
        }
        self.wrongnet = ReactionNetwork(self.wrongrates)
Exemplo n.º 6
0
    def setUp(self):
        self.instring = "A + B -2.0>\tC +\tB"
        #this string includes:
        #  different kinds of whitespace
        #  disordered products
        #  floating-point rate
        self.net = ReactionNetwork.from_string(self.instring)

        self.othernet = ReactionNetwork.from_string(self.instring)

        self.wrongstring = "A + B\t-3.0> C + B"
        self.wrongnet = ReactionNetwork.from_string(self.wrongstring)
Exemplo n.º 7
0
 def setUp(self):
     self.instring = "A + B -2.0>\tC +\tB"
     #this string includes:
     #  different kinds of whitespace
     #  disordered products
     #  floating-point rate
     self.net = ReactionNetwork.from_string(self.instring)
     
     self.othernet = ReactionNetwork.from_string(self.instring)
     
     self.wrongstring = "A + B\t-3.0> C + B"
     self.wrongnet = ReactionNetwork.from_string(self.wrongstring)
Exemplo n.º 8
0
    def reactionnet(self):
        """
        A property that generates and caches a :py:class:`~.ReactionNetwork` object
        representing the reaction network exhibited by this bucket.
        
        Rates are calculated based on repeats of events in this bucket. This may have a large sampling
        error depending on how many repeates there were.
        """
        if self._reactionnet == None:
            #need to create a rates structure, then convert it to a reactionnet
            rates = {}
            seenreactants = {}

            for event in self.events:
                reactants = event.reactants
                products = event.products
                reaction = (reactants, products)
                if event.rateconstant is None:
                    if reaction not in rates:
                        rates[reaction] = 0
                    rates[reaction] += 1
                else:
                    if reaction not in rates:
                        rates[reaction] = event.rateconstant
                    else:
                        assert rates[reaction] == event.rateconstant

            self._reactionnet = ReactionNetwork(rates)

        return self._reactionnet
Exemplo n.º 9
0
 def setUp(self): 
     self.rates = {(OrderedFrozenBag(["A", "B"]), OrderedFrozenBag(["B", "C"])):2.0}
     self.net = ReactionNetwork(self.rates)
     
     self.othernet = ReactionNetwork(self.rates)
     
     self.wrongrates = {(OrderedFrozenBag(["A", "B"]), OrderedFrozenBag(["B", "C"])):3.0}
     self.wrongnet = ReactionNetwork(self.wrongrates)
Exemplo n.º 10
0
class TestReactionNetwork(unittest.TestCase):
    """
    This is the main class to test ReactionNetwork class. 
    
    It relies on setUp to generate a ReactionNetwork instance which
    is then probed by the other functions
    """
    def setUp(self):
        self.rates = {
            (OrderedFrozenBag(["A", "B"]), OrderedFrozenBag(["B", "C"])): 2.0
        }
        self.net = ReactionNetwork(self.rates)

        self.othernet = ReactionNetwork(self.rates)

        self.wrongrates = {
            (OrderedFrozenBag(["A", "B"]), OrderedFrozenBag(["B", "C"])): 3.0
        }
        self.wrongnet = ReactionNetwork(self.wrongrates)

    def test_seen(self):
        """
        Makes sure the molecules that were specified are in seen.
        Also checks that they are in sorted order.
        """
        self.assertEqual(self.net.seen, ("A", "B", "C"))

    def test_reactions(self):
        """
        Makes sure the reactions that were specified are in reactions.
        Also checks that they are in sorted order
        TODO check sorted order between reactions
        """
        self.assertEqual(
            self.net.reactions,
            ((OrderedFrozenBag(["A", "B"]), OrderedFrozenBag(["B", "C"])), ))

    def test_rate(self):
        self.assertEqual(
            self.net.rate(OrderedFrozenBag(["A", "B"]),
                          OrderedFrozenBag(["B", "C"])), 2.0)

    def test_reaction_to_string(self):
        """
        Check that it convert a reaction to a string correctly
        """
        target = """A + B\t-2.0>\tB + C"""
        self.assertEqual(
            self.net.reaction_to_string(
                (OrderedFrozenBag(["A", "B"]), OrderedFrozenBag(["B", "C"])),
                2.0), target)

    def test_to_string(self):
        """
        Check that it converts to a string correctly
        """
        target = """A + B\t-2.0>\tB + C"""
        self.assertEqual(str(self.net), target)

    def test_equal(self):
        """
        As ReactionNetwork has a custom __eq__ function, it is tested
        here.
        
        Needs to both pass and fail.
        """
        self.assertTrue(self.net != None)
        self.assertTrue(self.net != "SPAM!")
        self.assertEqual(self.net, self.othernet)
        self.assertTrue(self.net != self.wrongnet)

    def test_hash(self):
        """
        As ReactionNetwork has a custom __hash__ function, it is tested
        here.
        
        Needs to both pass and fail.
        
        Technically, the fail here could be true and still be a hash
        but it is supposed to usually be wrong so assume that it will be
        wrong.
        """
        self.assertEqual(hash(self.net), hash(self.othernet))
        #technically, this could be true and still be a hash
        #but it is supposed to usually be wrong
        self.assertTrue(hash(self.net) != hash(self.wrongnet))
Exemplo n.º 11
0
class TestReactionNetwork(unittest.TestCase):
    """
    This is the main class to test ReactionNetwork class. 
    
    It relies on setUp to generate a ReactionNetwork instance which
    is then probed by the other functions
    """
    def setUp(self): 
        self.rates = {(OrderedFrozenBag(["A", "B"]), OrderedFrozenBag(["B", "C"])):2.0}
        self.net = ReactionNetwork(self.rates)
        
        self.othernet = ReactionNetwork(self.rates)
        
        self.wrongrates = {(OrderedFrozenBag(["A", "B"]), OrderedFrozenBag(["B", "C"])):3.0}
        self.wrongnet = ReactionNetwork(self.wrongrates)

    def test_seen(self):
        """
        Makes sure the molecules that were specified are in seen.
        Also checks that they are in sorted order.
        """
        self.assertEqual(self.net.seen, ("A", "B", "C"))
        
    def test_reactions(self):
        """
        Makes sure the reactions that were specified are in reactions.
        Also checks that they are in sorted order
        TODO check sorted order between reactions
        """
        self.assertEqual(self.net.reactions, ((OrderedFrozenBag(["A", "B"]), OrderedFrozenBag(["B", "C"])),) )
        
    def test_rate(self):
        self.assertEqual(self.net.rate(OrderedFrozenBag(["A", "B"]), OrderedFrozenBag(["B", "C"])), 2.0)
        
    def test_reaction_to_string(self):
        """
        Check that it convert a reaction to a string correctly
        """
        target = """A + B\t-2.0>\tB + C"""
        self.assertEqual(self.net.reaction_to_string((OrderedFrozenBag(["A", "B"]), OrderedFrozenBag(["B", "C"])), 2.0), target)
        
    def test_to_string(self):
        """
        Check that it converts to a string correctly
        """
        target = """A + B\t-2.0>\tB + C"""
        self.assertEqual(str(self.net), target)
        
    def test_equal(self):
        """
        As ReactionNetwork has a custom __eq__ function, it is tested
        here.
        
        Needs to both pass and fail.
        """
        self.assertTrue(self.net != None)
        self.assertTrue(self.net != "SPAM!")
        self.assertEqual(self.net, self.othernet)
        self.assertTrue(self.net != self.wrongnet)
        
    def test_hash(self):
        """
        As ReactionNetwork has a custom __hash__ function, it is tested
        here.
        
        Needs to both pass and fail.
        
        Technically, the fail here could be true and still be a hash
        but it is supposed to usually be wrong so assume that it will be
        wrong.
        """
        self.assertEqual(hash(self.net), hash(self.othernet))
        #technically, this could be true and still be a hash
        #but it is supposed to usually be wrong
        self.assertTrue(hash(self.net) != hash(self.wrongnet))