Exemple #1
0
def main():

    import numpy
    from cmepy import domain
    import cmepy.solver
    import cmepy.recorder

    max_s1_copies = 40
    max_s2_copies = 100
    m = create_model_gene_toggle(max_s1_copies, max_s2_copies)

    # define domain states as union of two rectangular regions along the axes
    a_shape = (max_s1_copies, 6)
    b_shape = (10, max_s2_copies)
    domain_states_a = set(domain.to_iter(domain.from_rect(a_shape)))
    domain_states_b = set(domain.to_iter(domain.from_rect(b_shape)))
    states = domain.from_iter(domain_states_a | domain_states_b)

    solver = cmepy.solver.create(m, sink=True, domain_states=states)

    recorder = cmepy.recorder.create((m.species, m.species_counts))

    time_steps = numpy.linspace(0.0, 100.0, 101)
    for t in time_steps:
        solver.step(t)
        p, p_sink = solver.y
        print('t = {:.4}; p_sink = {:.4E}'.format(t, p_sink))
        recorder.write(t, p)

    cmepy.recorder.display_plots(recorder, title=m.name)
Exemple #2
0
    def test_iter_from_dense_states(self):
        sparse_states = set([(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6),
                             (2, 3, 4), (2, 3, 4), (1, 2, 3)])

        dense_states = domain.from_iter(sparse_states)
        state_iter = domain.to_iter(dense_states)
        for (state, goal_state) in itertools.izip(state_iter, sparse_states):
            assert state == goal_state
Exemple #3
0
    def test_sparse_domain_from_dict(self):
        p_0 = {(4, 3): 0.1, (9, 4): 0.3, (11, -1): 0.2, (44, 44): 0.4}

        states = domain.from_iter(p_0)

        goal_states = list(p_0)
        for i, goal_state in enumerate(goal_states):
            assert_array_equal(states[:, i], goal_state)
Exemple #4
0
    def test_sparse_domain_from_set(self):
        sparse_states = set([(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6),
                             (2, 3, 4), (2, 3, 4), (1, 2, 3)])

        states = domain.from_iter(sparse_states)

        goal_states = list(sparse_states)
        for i, goal_state in enumerate(goal_states):
            assert_array_equal(states[:, i], goal_state)
Exemple #5
0
 def test_sparse_domain_from_dict(self):
     p_0 = {(4, 3) : 0.1,
            (9, 4) : 0.3,
            (11, -1) : 0.2,
            (44, 44) : 0.4}
     
     states = domain.from_iter(p_0)
     
     goal_states = list(p_0)
     for i, goal_state in enumerate(goal_states):
         assert_array_equal(states[:, i], goal_state)
Exemple #6
0
 def test_iter_from_dense_states(self):
     sparse_states = set([(1, 2, 3),
                          (2, 3, 4),
                          (3, 4, 5),
                          (4, 5, 6),
                          (2, 3, 4),
                          (2, 3, 4),
                          (1, 2, 3)])
     
     dense_states = domain.from_iter(sparse_states)
     state_iter = domain.to_iter(dense_states)
     for (state, goal_state) in itertools.izip(state_iter, sparse_states):
         assert state == goal_state
Exemple #7
0
 def test_sparse_domain_from_set(self):
     sparse_states = set([(1, 2, 3),
                          (2, 3, 4),
                          (3, 4, 5),
                          (4, 5, 6),
                          (2, 3, 4),
                          (2, 3, 4),
                          (1, 2, 3)])
     
     states = domain.from_iter(sparse_states)
     
     goal_states = list(sparse_states)
     for i, goal_state in enumerate(goal_states):
         assert_array_equal(states[:, i], goal_state)
Exemple #8
0
def main():
    
    import numpy
    from cmepy import domain
    import cmepy.solver
    import cmepy.recorder

    max_s1_copies = 40
    max_s2_copies = 100
    m = create_model_gene_toggle(max_s1_copies, max_s2_copies)
    
    # define domain states as union of two rectangular regions along the axes
    a_shape = (max_s1_copies, 6)
    b_shape = (10, max_s2_copies)
    domain_states_a = set(domain.to_iter(domain.from_rect(a_shape)))
    domain_states_b = set(domain.to_iter(domain.from_rect(b_shape)))
    states = domain.from_iter(domain_states_a | domain_states_b)
    
    solver = cmepy.solver.create(
        m,
        sink = True,
        domain_states = states
    )
    
    recorder = cmepy.recorder.create(
        (m.species,
         m.species_counts)
    )
    
    time_steps = numpy.linspace(0.0, 100.0, 101)
    for t in time_steps:
        solver.step(t)
        p, p_sink = solver.y
        print 't = %g; p_sink = %g' % (t, p_sink)
        recorder.write(t, p)
    
    cmepy.recorder.display_plots(recorder,
                                 title = m.name)
	
Exemple #9
0
def create(model, sink, p_0=None, t_0=None, sink_0=None, time_dependencies=None, domain_states=None):
    """
    Returns a solver for the Chemical Master Equation of the given model.
    
    arguments:
    
        model : the CME model to solve
        
        sink : If sink is True, the solver will include a 'sink' state used
            to accumulate any probability that may flow outside the domain.
            This can be used to measure the error in the solution due to
            truncation of the domain. If sink is False, the solver will not
            include a 'sink' state, and probability will be artificially
            prevented from flowing outside of the domain.
        
        p_0 : (optional) mapping from states in the domain to probabilities,
            for the initial probability distribution. If not specified,
            and the initial state of the state space is given by the model,
            defaults to all probability concentrated at the initial state,
            otherwise, a ValueError will be raised.
        
        t_0 : (optional) initial time, defaults to 0.0
        
        sink_0 : (optional) initial sink probability, defaults to 0.0
            Only a valid argument if sink is set to True.
        
        time_dependencies : (optional) By default, reaction propensities are
            time independent. If specified, time_dependencies must be of the
            form { s_1 : phi_1, ..., s_n : phi_n }, where each (s_j, phi_j)
            item satisifes :
            
                s_j : set of reaction indices
                phi_j : phi_j(t) -> time dependent coefficient 
            
            The propensities of the reactions with indicies contained in s_j
            will all be multiplied by the coefficient phi_j(t), at time t.
            Reactions are indexed according to the ordering of the propensities
            in the model.
            
            The reaction index sets s_j must be *disjoint*. It is not necessary
            for the union of the s_j to include all the reaction indices.
            If a reaction's index is not contained in any s_j then the reaction
            is treated as time-independent. 
        
        mapping of time dependent coefficient
            functions keyed by subsets of reaction indices, with respect to the
            ordering of reactions determined by the order of the propensity
            functions inside the model. The propensities of the reactions
            with indices included in each subset are multiplied by the time
            dependent coefficient function. By default, no time dependent
            coefficient functions are specified, that is, the CME has
            time-independent propensities.
        
        domain_states : (optional) array of states in the domain.
            By default, generate the rectangular lattice of states defined by
            the 'shape' entry of the model. A ValueError is raised if both
            domain_states and 'shape' are unspecified.
    """

    mdl.validate_model(model)

    if sink_0 is not None:
        if not sink:
            raise ValueError("sink_0 may not be specified if sink is False")
        sink_0 = float(sink_0)
    else:
        sink_0 = 0.0

    # determine states in domain, then construct an enumeration of the
    # domain states
    if domain_states is None:
        if mdl.SHAPE not in model:
            lament = "if no states given, model must contain key '%s'"
            raise KeyError(lament % mdl.SHAPE)
        else:
            domain_states = domain.from_rect(shape=model.shape)

    domain_enum = state_enum.create(domain_states)

    # determine p_0, then construct a dense representation with respect to
    # the domain enumeration
    initial_state = model.get(mdl.INITIAL_STATE, None)
    if p_0 is None:
        if initial_state is None:
            lament = "if no p_0 given, model must contain key '%s'"
            raise ValueError(lament % mdl.INITIAL_STATE)
        else:
            p_0 = {initial_state: 1.0}

    if t_0 is None:
        t_0 = 0.0

    member_flags = domain_enum.contains(domain.from_iter(p_0))
    if not numpy.logical_and.reduce(member_flags):
        raise ValueError("support of p_0 is not a subset of domain_states")

    # compute reaction matrices and use them to define differential equations
    gen_matrices = cme_matrix.gen_reaction_matrices(model, domain_enum, sink, cme_matrix.non_neg_states)
    reaction_matrices = list(gen_matrices)
    dy_dt = cme_matrix.create_diff_eqs(reaction_matrices, phi=time_dependencies)

    # construct and initialise solver
    if sink:
        cme_solver = ode_solver.Solver(dy_dt, y_0=(p_0, sink_0), t_0=t_0)
        pack, unpack = create_packing_functions(domain_enum)
        cme_solver.set_packing(pack, unpack, transform_dy_dt=False)
    else:
        pack = domain_enum.pack_distribution
        unpack = domain_enum.unpack_distribution
        cme_solver = ode_solver.Solver(dy_dt, y_0=p_0, t_0=t_0)
        cme_solver.set_packing(pack, unpack, transform_dy_dt=False)
    return cme_solver
Exemple #10
0
def create(model,
           sink,
           p_0=None,
           t_0=None,
           sink_0=None,
           time_dependencies=None,
           domain_states=None,
           solver=ode_solver.Solver,
           outflow=False,
           **solver_args):
    """
    Returns a solver for the Chemical Master Equation of the given model.
    
    arguments:
    
        model : the CME model to solve
        
        sink : If sink is True, the solver will include a 'sink' state used
            to accumulate any probability that may flow outside the domain.
            This can be used to measure the error in the solution due to
            truncation of the domain. If sink is False, the solver will not
            include a 'sink' state, and probability will be artificially
            prevented from flowing outside of the domain.
        
        p_0 : (optional) mapping from states in the domain to probabilities,
            for the initial probability distribution. If not specified,
            and the initial state of the state space is given by the model,
            defaults to all probability concentrated at the initial state,
            otherwise, a ValueError will be raised.
        
        t_0 : (optional) initial time, defaults to 0.0
        
        sink_0 : (optional) initial sink probability, defaults to 0.0
            Only a valid argument if sink is set to True.
        
        time_dependencies : (optional) By default, reaction propensities are
            time independent. If specified, time_dependencies must be of the
            form { s_1 : phi_1, ..., s_n : phi_n }, where each (s_j, phi_j)
            item satisifes :
            
                s_j : set of reaction indices
                phi_j : phi_j(t) -> time dependent coefficient 
            
            The propensities of the reactions with indicies contained in s_j
            will all be multiplied by the coefficient phi_j(t), at time t.
            Reactions are indexed according to the ordering of the propensities
            in the model.
            
            The reaction index sets s_j must be *disjoint*. It is not necessary
            for the union of the s_j to include all the reaction indices.
            If a reaction's index is not contained in any s_j then the reaction
            is treated as time-independent. 
        
        mapping of time dependent coefficient
            functions keyed by subsets of reaction indices, with respect to the
            ordering of reactions determined by the order of the propensity
            functions inside the model. The propensities of the reactions
            with indices included in each subset are multiplied by the time
            dependent coefficient function. By default, no time dependent
            coefficient functions are specified, that is, the CME has
            time-independent propensities.
        
        domain_states : (optional) array of states in the domain.
            By default, generate the rectangular lattice of states defined by
            the 'shape' entry of the model. A ValueError is raised if both
            domain_states and 'shape' are unspecified.
    """

    mdl.validate_model(model)

    if sink and outflow:
        raise ValueError('sink and outflow cannot be both True')

    if sink_0 is not None:
        if not sink:
            raise ValueError('sink_0 may not be specified if sink is False')
        sink_0 = float(sink_0)
    else:
        sink_0 = 0.0

    # determine states in domain, then construct an enumeration of the
    # domain states
    if domain_states is None:
        if mdl.SHAPE not in model:
            lament = 'if no states given, model must contain key \'%s\''
            raise KeyError(lament % mdl.SHAPE)
        else:
            domain_states = domain.from_rect(shape=model.shape)

    domain_enum = state_enum.create(domain_states)

    # determine p_0, then construct a dense representation with respect to
    # the domain enumeration
    initial_state = model.get(mdl.INITIAL_STATE, None)
    if p_0 is None:
        if initial_state is None:
            lament = 'if no p_0 given, model must contain key \'%s\''
            raise ValueError(lament % mdl.INITIAL_STATE)
        else:
            p_0 = {initial_state: 1.0}

    if t_0 is None:
        t_0 = 0.0

    member_flags = domain_enum.contains(domain.from_iter(p_0))
    if not numpy.logical_and.reduce(member_flags):
        raise ValueError('support of p_0 is not a subset of domain_states')

    # compute reaction matrices and use them to define differential equations
    gen_matrices = cme_matrix.gen_reaction_matrices(model,
                                                    domain_enum,
                                                    sink,
                                                    cme_matrix.non_neg_states,
                                                    outflow=outflow)
    reaction_matrices = list(gen_matrices)
    dy_dt = cme_matrix.create_diff_eqs(reaction_matrices,
                                       phi=time_dependencies)

    if solver_args:
        solver_args['reaction_matrices'] = reaction_matrices

    # construct and initialise solver
    if sink:
        cme_solver = solver(dy_dt, y_0=(p_0, sink_0), t_0=t_0, **solver_args)
        pack, unpack = create_packing_functions(domain_enum)
        cme_solver.set_packing(pack, unpack, transform_dy_dt=False)
    else:
        pack = domain_enum.pack_distribution
        unpack = domain_enum.unpack_distribution
        cme_solver = solver(dy_dt, y_0=p_0, t_0=t_0, **solver_args)
        cme_solver.set_packing(pack, unpack, transform_dy_dt=False)
    return cme_solver