def test_base_bmodel_no_hold(): """Test the no hold option""" n_nodes = 12 n_runs_pert = 16 bmodel = Bmodel(J=random_interaction_matrix(N=n_nodes)) bmodel.runs(16) hold = False for config in bmodel.steady_states.values: for node_to_switch in bmodel.node_labels: for switch_to in ["ON", "OFF"]: bmodel.perturbe(initial_condition=config, node_to_switch=node_to_switch, switch_to=switch_to, n_runs=n_runs_pert, hold=hold) # if node is not holded, then final state can or cannot be what we asked for # this is difficult to test checks = [] for switched_node in bmodel.node_labels: for switch_to in ["ON", "OFF"]: perturbations = bmodel.get_perturbations( switched_node=switched_node, switch_to=switch_to, hold=hold) checks.append((perturbations[switched_node] == { "ON": -1, "OFF": 1 }[switch_to]).values) checks = list(np.concatenate(checks)) checks = sorted(list(set(checks))) assert checks == [False, True]
def test_base_bmodel_get_perturbations(): """Test that we retrieve the right number of perturbations""" n_nodes = 16 n_runs_pert = 5 bmodel = Bmodel(J=random_interaction_matrix(N=n_nodes), maxT=4) bmodel.runs(20) # do the perturbations for config in bmodel.steady_states.values: for hold in [True, False]: for node_to_switch in bmodel.node_labels: for switch_to in ["ON", "OFF"]: bmodel.perturbe(initial_condition=config, node_to_switch=node_to_switch, switch_to=switch_to, n_runs=n_runs_pert, hold=hold) # count the perturbations for hold in [True, False]: for switched_node in bmodel.node_labels: for switch_to in ["ON", "OFF"]: perturbations = bmodel.get_perturbations( switched_node=switched_node, switch_to=switch_to, hold=hold) n_ics = (bmodel.steady_states[switched_node] == { "ON": -1, "OFF": 1 }[switch_to]).sum() assert perturbations.shape == (n_ics * n_runs_pert, n_nodes)
def test_base_bmodel_hold(): """Test the hold option""" n_nodes = 12 n_runs_pert = 4 bmodel = Bmodel(J=random_interaction_matrix(N=n_nodes)) bmodel.runs(16) hold = True for config in bmodel.steady_states.values: for node_to_switch in bmodel.node_labels: for switch_to in ["ON", "OFF"]: bmodel.perturbe(initial_condition=config, node_to_switch=node_to_switch, switch_to=switch_to, n_runs=n_runs_pert, hold=hold) # If node is holded, its final state must be what we asked for for switched_node in bmodel.node_labels: for switch_to in ["ON", "OFF"]: perturbations = bmodel.get_perturbations( switched_node=switched_node, switch_to=switch_to, hold=hold) assert np.all(perturbations[switched_node] == { "ON": 1, "OFF": -1 }[switch_to])
def test_base_bmodel_indicator_can_fail(): n_nodes = 16 J = random_interaction_matrix(N=n_nodes) node_labels = ["node_%d" % i for i in range(n_nodes)] indicator_nodes = ["node_a", "node_b"] with pytest.raises(IndicatorError): _ = Bmodel(J=J, node_labels=node_labels, indicator_nodes=indicator_nodes)
def test_base_bmodel_indicator_unique(): """Test that we cannot pass non-unique indicators""" n_nodes = 16 J = random_interaction_matrix(N=n_nodes) node_labels = ["node_%d" % i for i in range(n_nodes)] indicator_nodes = ["node_0", "node_1", "node_0"] with pytest.raises(IndicatorError): _ = Bmodel(J=J, node_labels=node_labels, indicator_nodes=indicator_nodes)
def test_generate_random_interaction_matrix_avk5_N100(): """Test that the generate matrix has right avk.""" N = 100 avk = 5 avks = [ np.abs(random_interaction_matrix(N=N, avk=avk)).mean() * N for _ in range(1000) ] rel_err = np.abs(np.mean(avks) / avk - 1) assert rel_err < 0.05
def test_generate_random_interaction_matrix_mu04(): """Test that the generate matrix has right mu.""" N = 40 avk = 3 mu = 0.4 mus = [] for _ in range(1000): J = random_interaction_matrix(N=N, avk=avk, mu=mu) mus.append(np.sum(J == 1) / np.sum(J != 0)) rel_err = np.abs(np.mean(mus) / mu - 1) assert rel_err < 0.01
def test_base_bmodel_indicator_is_set(): n_nodes = 16 node_labels = ["node_%d" % i for i in range(n_nodes)] # let us get at least two indicator nodes num_indicators = 0 while num_indicators <= 1: J = random_interaction_matrix(N=n_nodes) indicator_nodes = [ node for i, node in enumerate(node_labels) if np.all(J[:, i] == 0) ] num_indicators = len(indicator_nodes) bmodel = Bmodel(J=J, node_labels=node_labels, indicator_nodes=indicator_nodes) assert bmodel.indicator_nodes == list(indicator_nodes)
def test_generate_random_interaction_matrix_connected_N100(): """Test that the generate matrix is connected.""" J = random_interaction_matrix(N=100, avk=3, mu=0.8, connected=True) n, _ = connected_components(np.abs(J)) assert n == 1
def test_generate_random_interaction_matrix_N(): """Test that the generate matrix has right N.""" J = random_interaction_matrix(N=100, avk=3, mu=0.8, connected=True) assert J.shape == (100, 100)
def random_small(): N = 10 J = random_interaction_matrix(N=N).astype(float) J_pseudo = np.identity(N) + 2 * J return N, J, J_pseudo