def test_hill_negative():
    sbml_bs = os.path.join(model_path, "hill_negative_crn_bs.xml")
    sbml_general = os.path.join(model_path, "hill_negative_crn.xml")
    CRN_bs = Model(sbml_filename = sbml_bs, sbml_warnings = False)
    CRN_general = Model(sbml_filename = sbml_general, sbml_warnings = False)

    #Test Deterministic Simulation
    R_bs_det = py_simulate_model(Model = CRN_bs, timepoints = timepoints)
    R_general_det = py_simulate_model(Model = CRN_general, timepoints = timepoints)

    #test that the models have the same species
    assert set(R_bs_det.columns) == set(R_general_det.columns)
    #test that the output of X is the same deterministically
    assert np.allclose(R_bs_det["protein_X"], R_general_det["protein_X"])

    #Test Stochastic Simulation
    test_utils.set_seed(seed)
    R_bs_stoch = py_simulate_model(Model = CRN_bs, timepoints = timepoints, stochastic = True)
    test_utils.set_seed(seed)
    R_general_stoch = py_simulate_model(Model = CRN_general, timepoints = timepoints, stochastic = True)

    #test that the models have the same species
    assert set(R_general_stoch.columns) == set(R_bs_stoch.columns)
    #test that the output of X is the same deterministically
    assert np.allclose(R_general_stoch["protein_X"], R_bs_stoch["protein_X"])
def test_massaction():
    #First model
    sbml_bs_1 = os.path.join(model_path, "massaction_crn_1_bs.xml")
    sbml_general_1 = os.path.join(model_path, "massaction_crn_1.xml")
    CRN_bs_1 = Model(sbml_filename = sbml_bs_1, sbml_warnings = False)
    CRN_general_1 = Model(sbml_filename = sbml_general_1, sbml_warnings = False)

    #Test Deterministic Simulation
    R_bs_1_det = py_simulate_model(Model = CRN_bs_1, timepoints = timepoints)
    R_general_1_det = py_simulate_model(Model = CRN_general_1, timepoints = timepoints)

    #test that the models have the same species
    assert set(R_bs_1_det.columns) == set(R_general_1_det.columns)
    #test that the output of X is the same deterministically
    assert np.allclose(R_bs_1_det["protein_X"], R_general_1_det["protein_X"])

    #Test Stochastic Simulation
    test_utils.set_seed(seed)
    R_bs_1_stoch = py_simulate_model(Model = CRN_bs_1, timepoints = timepoints, stochastic = True)
    test_utils.set_seed(seed)
    R_general_1_stoch = py_simulate_model(Model = CRN_general_1, timepoints = timepoints, stochastic = True)

    #test that the models have the same species
    assert set(R_general_1_stoch.columns) == set(R_bs_1_stoch.columns)
    #test that the output of X is the same deterministically
    assert np.allclose(R_general_1_stoch["protein_X"], R_bs_1_stoch["protein_X"])

    sbml_bs_2 = os.path.join(model_path, "massaction_crn_2_bs.xml")
    sbml_general_2 = os.path.join(model_path, "massaction_crn_2.xml")
    CRN_bs_2 = Model(sbml_filename = sbml_bs_2, sbml_warnings = False)
    CRN_general_2 = Model(sbml_filename = sbml_general_2, sbml_warnings = False)

    #Test Deterministic Simulation
    R_bs_2_det = py_simulate_model(Model = CRN_bs_2, timepoints = timepoints)
    R_general_2_det = py_simulate_model(Model = CRN_general_2, timepoints = timepoints)

    #test that the models have the same species
    assert set(R_bs_2_det.columns) == set(R_general_2_det.columns)
    #test that the output of X is the same deterministically
    assert np.allclose(R_bs_2_det["protein_X"], R_general_2_det["protein_X"])

    #Test Stochastic Simulation
    test_utils.set_seed(seed)
    R_bs_2_stoch = py_simulate_model(Model = CRN_bs_2, timepoints = timepoints, stochastic = True)
    test_utils.set_seed(seed)
    R_general_2_stoch = py_simulate_model(Model = CRN_general_2, timepoints = timepoints, stochastic = True)

    #test that the models have the same species
    assert set(R_general_2_stoch.columns) == set(R_bs_2_stoch.columns)
    #test that the output of X is the same deterministically
    assert np.allclose(R_general_2_stoch["protein_X"], R_bs_2_stoch["protein_X"])
示例#3
0
def random_delay_model(delay_type):
    '''
    Returns a randomish model with a non-delayed reaction and a delay reaction. 
    Set to always return the same model, for any particular delay type. 

    WARNING: To produce consistent Models, this function resets the random seeds
    used during Model construction. This may have unexpected effects on random
    number generation outside this function as a side-effect.
    '''

    test_utils.set_seed(seed)

    # Will always consider the reactions A-->B and A+B-->C, where only the
    # first reaction has delay.
    all_species = ["A", "B", "C"]
    x0 = {"A": 25, "B": 5, "C": 0}

    # A+B-->C
    consol_k = round(np.exp(np.random.uniform(low=param_min, high=param_max)),
                     3)
    consolidation_rxn = (["A", "B"], ["C"], "massaction", {'k': consol_k})

    # A-->B reaction, with delay.
    delay_class = delay_classes[delay_type]
    delay_k = round(np.exp(np.random.uniform(low=-1, high=1)), 3)
    delay_params = dict()
    for p in delay_required_params[delay_type]:
        delay_params[p] = round(
            np.exp(np.random.uniform(low=param_min, high=param_max)), 3)
    conversion_rxn = (["A"], [], "massaction", {
        "k": delay_k
    }, delay_type, [], ["B"], delay_params)

    M = Model(reactions=[consolidation_rxn, conversion_rxn])
    M.set_species(x0)

    return M
def random_prop_model(prop_type):
    '''
	Returns a randomish model with a specified propensity type. Set to always
	return the same model, for any particular propensity type.

	WARNING: To produce consistent Models, this function resets the random seeds
	used during Model construction. This may have unexpected effects on random
	number generation outside this function as a side-effect.
	'''

    test_utils.set_seed(seed)

    #Will always consider the reaction: A+B-->C
    inputs = ["A", "B"]
    outputs = ["C"]
    all_species = inputs + outputs
    x0 = {"A": 25, "B": 25, "C": 0}

    if debug:
        print('simulating propensity type ', prop_type)

    param_dict = {}
    # Here we will use a random(ish) rational function
    if prop_type == 'general':
        rate_str = "(1+"
        numerator_terms = np.random.randint(0, 5)
        denominator_terms = np.random.randint(0, 5)
        for i in range(numerator_terms):
            coef = str(
                round(np.exp(np.random.uniform(low=param_min, high=param_max)),
                      3))
            exp = str(round(np.random.uniform(low=0, high=param_max), 3))
            species = all_species[np.random.randint(len(all_species))]
            rate_str += coef + "*" + species + "^" + exp + "+"
        rate_str = rate_str[:-1] + ")"
        rate_str += "/(1+"
        for i in range(denominator_terms):
            coef = str(
                round(np.exp(np.random.uniform(low=param_min, high=param_max)),
                      3))
            exp = str(round(np.random.uniform(low=0, high=param_max), 3))
            species = all_species[np.random.randint(len(all_species))]
            rate_str += coef + "*" + species + "^" + exp + "+"
        rate_str = rate_str[:-1] + ")"
        param_dict['rate'] = rate_str
    else:
        required_params = propensity_param_requirements[prop_type]
        required_species = propensity_species_requirements[prop_type]
        param_dict = {}
        for p in required_params:
            param_dict[p] = \
              round(np.exp(np.random.uniform(low = param_min,
                        high = param_max)), 3)
        for i in range(len(required_species)):
            k = required_species[i]
            param_dict[k] = inputs[i]
    if debug:
        print('\t params =', param_dict)

    rxn = (inputs, outputs, prop_type, param_dict)
    M = Model(reactions=[rxn], initial_condition_dict=x0)
    M.set_species(x0)

    return M