UC-Davis-molecular-computing / ppsim

Python package for simulating population protocols
MIT License
9 stars 4 forks source link

give more useful error message when initial configuration contains invalid states #36

Open dave-doty opened 2 years ago

dave-doty commented 2 years ago

I had a bug where I specified species in CRN notation, but forgot to use the Specie objects in the initial configuration:

import ppsim as pp
p0,p1 = pp.species('0 1')
init_crn = { 0: 100, 1: 50 } # error; keys should be p0 and p1
rxns = [ p0+p0 >> p1+p1 ]
sim = pp.Simulation(init_crn, rxns)

which results in this confusing exception in the Simulation constructor:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_1992/3887239111.py in <module>
      4 init_crn = { 0: 100, 1: 50 }
      5 rxns = [ p0+p0 >> p1+p1 ]
----> 6 sim = pp.Simulation(init_crn, rxns)

C:\Dropbox\git\ppsim\ppsim\simulation.py in __init__(self, init_config, rule, simulator_method, transition_order, seed, volume, continuous_time, time_units, **kwargs)
    260             raise ValueError('simulator_method must be multibatch or sequential')
    261         self._transition_order = transition_order
--> 262         self.initialize_simulator(self.array_from_dict(init_config))
    263 
    264         # Check an arbitrary state to see if it has fields.

C:\Dropbox\git\ppsim\ppsim\simulation.py in array_from_dict(self, d)
    385         a = np.zeros(len(self.state_list), dtype=np.int64)
    386         for k in d.keys():
--> 387             a[self.state_dict[k]] += d[k]
    388         return a
    389 

KeyError: 0

Instead, when the second parameter rule to the Simulation constructor is a list of CRN reactions rxns, check that the keys of the initial configuration (first parameter init_config) are each one of the Specie objects appearing in reactions in rxns.