コード例 #1
0
    def test_with_proxy_bad_input(self):
        connectivity, coupling, integrator, monitors, sim, result, result_all = self._reference_simulation()
        # The modify model without proxy
        np.random.seed(42)
        init = np.concatenate((np.random.random_sample((385, 1, 76, 1)),
                               np.random.random_sample((385, 1, 76, 1))), axis=1)
        np.random.seed(42)
        id_proxy = range(11)
        model = ReducedWongWangProxy(tau_s=np.random.rand(76))
        synchronization_time = 1.
        # Initialise a Simulator -- Model, Connectivity, Integrator, and Monitors.
        sim_4 = CoSimulator(
            voi=np.array([0]),
            synchronization_time=synchronization_time,
            cosim_monitors=(RawCosim(),),
            proxy_inds=np.asarray(id_proxy, dtype=np.int),
            model=model,
            connectivity=connectivity,
            coupling=coupling,
            integrator=integrator,
            monitors=(monitors,),
            initial_conditions=init,
        )
        sim_4.configure()
        sim_4.run() # run the first steps because the history is delayed

        sim_to_sync_time = int(SIMULATION_LENGTH / synchronization_time)
        sync_steps = int(synchronization_time / integrator.dt)

        result_4_all = [np.empty((0,)), np.empty((sync_steps, 2, 76, 1))]

        for j in range(0, sim_to_sync_time):
            result_4_all_step = sim_4.run(
                cosim_updates=[np.array([result_all[0][0][(sync_steps * j) + i] for i in range(sync_steps)]),
                               np.ones((sync_steps, 1, len(id_proxy), 1)) * 0.7])
            result_4_all[0] = np.concatenate((result_4_all[0], result_4_all_step[0][0]))
            result_4_all[1] = np.concatenate((result_4_all[1], result_4_all_step[0][1]))

        simulation_n_steps = int(SIMULATION_LENGTH / integrator.dt)
        # The beggining is good for rate
        for i in range(np.min(sim_4.connectivity.idelays[np.nonzero(sim_4.connectivity.idelays)])+1):
            np.testing.assert_array_equal(result_all[0][1][i][0][len(id_proxy):], result_4_all[1][i+sync_steps, 0, len(id_proxy):])
            np.testing.assert_array_compare(operator.__ne__,result_all[0][1][i][0][:len(id_proxy)], result_4_all[1][i+sync_steps, 0, :len(id_proxy)])
        # after the delayed impact the simulation, This create some difference for rate
        for i in range(np.min(sim_4.connectivity.idelays[np.nonzero(sim_4.connectivity.idelays)])+1,
                       simulation_n_steps):
            diff = result_all[0][1][i][0][len(id_proxy):] - result_4_all[1][i+sync_steps, 0, len(id_proxy):]
            assert np.sum(diff) != 0
            np.testing.assert_array_compare(operator.__ne__,result_all[0][1][i][0][:len(id_proxy)], result_4_all[1][i+sync_steps, 0, :len(id_proxy)])

        # The beggining is good for S
        for i in range(np.min(sim_4.connectivity.idelays[np.nonzero(sim_4.connectivity.idelays)])+1):
            np.testing.assert_array_equal(result_all[0][1][i][1][len(id_proxy):], result_4_all[1][i+sync_steps, 1, len(id_proxy):])
            np.testing.assert_array_equal(result_all[0][1][i][1][:len(id_proxy)]*np.NAN, result_4_all[1][i+sync_steps, 1, :len(id_proxy)])
        # after the delayed impact the simulation, This create some difference for S
        for i in range(np.min(sim_4.connectivity.idelays[np.nonzero(sim_4.connectivity.idelays)])+1,
                       simulation_n_steps):
            diff = result_all[0][1][i][1][len(id_proxy):] - result_4_all[1][i+sync_steps, 1, len(id_proxy):]
            assert np.sum(diff) != 0
            np.testing.assert_array_equal(result_all[0][1][i][1][:len(id_proxy)]*np.NAN, result_4_all[1][i+sync_steps, 1, :len(id_proxy)])
コード例 #2
0
 def test_without_proxy(self):
     model, connectivity, coupling, init, integrator, monitors = self._prepare_reference_simulation()
     np.random.seed(42)
     # Initialise a Simulator -- Model, Connectivity, Integrator, and Monitors.
     sim = CoSimulator(
         voi=np.array([0]),
         synchronization_time=1.,
         cosim_monitors=(RawCosim(),),
         proxy_inds=np.array([], dtype=np.int),
         model=model,
         connectivity=connectivity,
         coupling=coupling,
         integrator=integrator,
         monitors=(monitors,),
         initial_conditions=init,
     )
     with pytest.raises(ValueError):
         sim.configure()
コード例 #3
0
    def test_with_proxy_right_input(self):
        connectivity, coupling, integrator, monitors, sim, result, result_all = self._reference_simulation(
        )
        # The modify model without proxy
        np.random.seed(42)
        init = np.concatenate((np.random.random_sample(
            (385, 1, 76, 1)), np.random.random_sample((385, 1, 76, 1))),
                              axis=1)
        np.random.seed(42)
        id_proxy = range(11)
        model = ReducedWongWangProxy(tau_s=np.random.rand(76))
        synchronization_time = 1.0
        # Initialise a Simulator -- Model, Connectivity, Integrator, and Monitors.
        sim_5 = CoSimulator(
            voi=np.array([0]),
            synchronization_time=synchronization_time,
            cosim_monitors=(RawCosim(), ),
            proxy_inds=np.asarray(id_proxy, dtype=np.int),
            model=model,
            connectivity=connectivity,
            coupling=coupling,
            integrator=integrator,
            monitors=(monitors, ),
            initial_conditions=init,
        )
        sim_5.configure()
        sim_5.run()  # run the first steps because the history is delayed

        sim_to_sync_time = int(SIMULATION_LENGTH / synchronization_time)
        sync_steps = int(synchronization_time / integrator.dt)

        result_5_all = [np.empty((0, )), np.empty((sync_steps, 2, 76, 1))]

        for j in range(0, sim_to_sync_time):
            result_5_all_step = sim_5.run(cosim_updates=[
                np.array([
                    result_all[0][0][(sync_steps * j) + i]
                    for i in range(sync_steps)
                ]),
                np.array([
                    result_all[0][1][(sync_steps * j) + i][0][id_proxy]
                    for i in range(sync_steps)
                ]).reshape((sync_steps, 1, len(id_proxy), 1))
            ])
            result_5_all[0] = np.concatenate(
                (result_5_all[0], result_5_all_step[0][0]))
            result_5_all[1] = np.concatenate(
                (result_5_all[1], result_5_all_step[0][1]))

        for i in range(int(SIMULATION_LENGTH / integrator.dt)):
            np.testing.assert_array_equal(
                result_all[0][1][i][0][len(id_proxy):],
                result_5_all[1][i + sync_steps, 0,
                                len(id_proxy):])
            np.testing.assert_array_equal(
                result_all[0][1][i][0][:len(id_proxy)],
                result_5_all[1][i + sync_steps, 0, :len(id_proxy)])
コード例 #4
0
def tvb_init(parameters, time_synchronize, initial_condition):
    """
        To initialise Nest and to create the connectome model
    :param parameters : (model,connectivity,coupling,integrator) : parameter for the simulation without monitor
    :param time_synchronize : the time of synchronization for the proxy
    :param initial_condition: the initial condition of the model
    :return:
        sim : the TVB simulator,
        (weights_in,delay_in): the connectivity of disconnect region input
        (weights_out,delay_out): the connectivity of disconnect region ouput
    """
    model, connectivity, coupling, integrator, id_proxy = parameters
    # Initialise some Monitors with period in physical time
    monitors = (lab.monitors.Raw(variables_of_interest=np.array(0)),)

    # Initialise a Simulator -- Model, Connectivity, Integrator, and Monitors.
    if len(id_proxy) == 0:
        sim = lab.simulator.Simulator(
            model=model,
            connectivity=connectivity,
            coupling=coupling,
            integrator=integrator,
            monitors=monitors,
            initial_conditions=initial_condition
        )
    else:
        # Initialise a Simulator -- Model, Connectivity, Integrator, and Monitors.
        sim = CoSimulator(
                          voi=np.array([0]),
                          synchronization_time=time_synchronize,
                          cosim_monitors=(RawCosim(),),
                          proxy_inds=np.asarray(id_proxy, dtype=np.int),
                          model=model,
                          connectivity=connectivity,
                          coupling=coupling,
                          integrator=integrator,
                          monitors=monitors,
                          initial_conditions=initial_condition
                          )
    sim.configure()
    return sim
コード例 #5
0
    def test_precision_with_proxy(self):
        connectivity, coupling, integrator, monitors, sim, result, result_all = self._reference_simulation(
            self._simulation_length)
        # New simulator with proxy
        np.random.seed(42)
        init = np.concatenate((np.random.random_sample(
            (385, 1, 76, 1)), np.random.random_sample((385, 1, 76, 1))),
                              axis=1)
        np.random.seed(42)
        model_1 = ReducedWongWangProxy(tau_s=np.random.rand(76))
        synchronization_time = 1.
        # Initialise a Simulator -- Model, Connectivity, Integrator, and Monitors.
        sim_1 = CoSimulator(
            voi=np.array([0]),
            synchronization_time=synchronization_time,
            cosim_monitors=(RawCosim(), ),
            proxy_inds=np.asarray([0], dtype=np.int),
            model=model_1,
            connectivity=connectivity,
            coupling=coupling,
            integrator=integrator,
            monitors=(monitors, ),
            initial_conditions=init,
        )
        sim_1.configure()

        sim_to_sync_time = int(self._simulation_length / synchronization_time)
        sync_steps = int(synchronization_time / integrator.dt)

        result_1_all = [np.empty((0, )), np.empty((sync_steps, 2, 76, 1))]
        sim_1.run()  # run the first steps because the history is delayed

        for j in range(0, sim_to_sync_time):
            result_1_all_step = sim_1.run(cosim_updates=[
                np.array([
                    result_all[0][0][(sync_steps * j) + i]
                    for i in range(sync_steps)
                ]),
                np.array([
                    result_all[0][1][(sync_steps * j) + i][0][0]
                    for i in range(sync_steps)
                ]).reshape((sync_steps, 1, 1, 1))
            ])
            result_1_all[0] = np.concatenate(
                (result_1_all[0], result_1_all_step[0][0]))
            result_1_all[1] = np.concatenate(
                (result_1_all[1], result_1_all_step[0][1]))

        for i in range(int(self._simulation_length / integrator.dt)):
            np.testing.assert_array_equal(
                result_all[0][1][i][0][1:], result_1_all[1][i + sync_steps, 0,
                                                            1:])
            np.testing.assert_array_equal(
                result_all[0][1][i][0][:1], result_1_all[1][i + sync_steps,
                                                            0, :1])
コード例 #6
0
    def test_without_proxy_coupling(self):
        connectivity, coupling, integrator, monitors, sim, result, result_all = self._reference_simulation(
        )
        # The modify model without proxy
        np.random.seed(42)
        init = np.concatenate((np.random.random_sample(
            (385, 1, 76, 1)), np.random.random_sample((385, 1, 76, 1))),
                              axis=1)
        model = ReducedWongWangProxy(tau_s=np.random.rand(76))
        synchronization_time = 1.0
        # Initialise a Simulator -- Model, Connectivity, Integrator, and Monitors.
        sim_6 = CoSimulator(
            voi=np.array([0]),
            synchronization_time=synchronization_time,
            cosim_monitors=(CosimCoupling(coupling=coupling), ),
            proxy_inds=np.asarray([0], dtype=np.int),
            model=model,
            connectivity=connectivity,
            coupling=coupling,
            integrator=integrator,
            monitors=(monitors, ),
            initial_conditions=init,
        )
        sim_6.configure()
        result_2_all = sim_6.run(
        )[0][1][:, 0, 0,
                0]  # run the first steps because the history is delayed

        sim_to_sync_time = int(SIMULATION_LENGTH / synchronization_time)
        sync_steps = int(synchronization_time / integrator.dt)

        with pytest.raises(ValueError):
            coupling_future = sim_6.loop_cosim_monitor_output(sync_steps, 1)

        coupling_future = sim_6.loop_cosim_monitor_output()

        for i in range(sim_to_sync_time):
            result_2 = sim_6.run()[0][1][:, 0, 0, 0]
            np.testing.assert_array_equal(
                result[i * sync_steps:(i + 1) * sync_steps] * np.NAN, result_2)
            assert np.sum(np.isnan(
                sim_6.loop_cosim_monitor_output()[0][1])) == 0
コード例 #7
0
ファイル: monitors_test.py プロジェクト: liadomide/tvb-root
    def test_monitor(self):
        connectivity, coupling, integrator, monitors, sim, result, result_all = self._reference_simulation(
            self._simulation_length)
        # New simulator with proxy
        np.random.seed(42)
        init = np.concatenate((np.random.random_sample(
            (385, 1, 76, 1)), np.random.random_sample((385, 1, 76, 1))),
                              axis=1)
        np.random.seed(42)
        model_1 = ReducedWongWangProxy(tau_s=np.random.rand(76))
        # Initialise a Simulator -- Model, Connectivity, Integrator, and Monitors.
        synchronization_time = 1.0
        sim_1 = CoSimulator(
            voi=np.array([0]),
            synchronization_time=synchronization_time,
            cosim_monitors=(RawCosim(),
                            RawVoiCosim(variables_of_interest=np.array([0])),
                            RawDelayed(),
                            RawVoiDelayed(variables_of_interest=np.array([0])),
                            CosimCoupling(coupling=coupling),
                            CosimCoupling(coupling=coupling,
                                          variables_of_interest=np.array([0
                                                                          ]))),
            proxy_inds=np.asarray([0], dtype=np.int),
            model=model_1,
            connectivity=connectivity,
            coupling=coupling,
            integrator=integrator,
            monitors=(monitors, ),
            initial_conditions=init,
        )
        sim_1.configure()

        sim_to_sync_time = int(self._simulation_length / synchronization_time)
        sync_steps = int(synchronization_time / integrator.dt)

        result_1_all = [np.empty((0, )), np.empty((sync_steps, 2, 76, 1))]
        result_cosim_monitors = []
        sim_1.run()  # run the first steps because the history is delayed

        for j in range(0, sim_to_sync_time):
            # This should fail for CosimCoupling that can only return FUTURE coupling values!!!
            result_cosim_monitors.append(
                sim_1.loop_cosim_monitor_output(sync_steps, 0))
            result_1_all_step = sim_1.run(cosim_updates=[
                np.array([
                    result_all[0][0][(sync_steps * j) + i]
                    for i in range(sync_steps)
                ]),
                np.array([
                    result_all[0][1][(sync_steps * j) + i][0][0]
                    for i in range(sync_steps)
                ]).reshape((sync_steps, 1, 1, 1))
            ])
            result_1_all[0] = np.concatenate(
                (result_1_all[0], result_1_all_step[0][0]))
            result_1_all[1] = np.concatenate(
                (result_1_all[1], result_1_all_step[0][1]))

        for i in range(int(self._simulation_length / integrator.dt)):
            np.testing.assert_array_equal(
                result_all[0][1][i][0][1:], result_1_all[1][i + sync_steps, 0,
                                                            1:])
            np.testing.assert_array_equal(
                result_all[0][1][i][0][:1], result_1_all[1][i + sync_steps,
                                                            0, :1])

        for i in range(sim_to_sync_time):
            result_step = result_cosim_monitors[i]
            # check the dimension of the monitors
            np.testing.assert_array_equal(result_step[0][1].shape,
                                          (sync_steps, 2, 76, 1))
            np.testing.assert_array_equal(result_step[1][1].shape,
                                          (sync_steps, 1, 76, 1))
            np.testing.assert_array_equal(result_step[3][1].shape,
                                          (sync_steps, 1, 76, 1))
            np.testing.assert_array_equal(result_step[4][1].shape,
                                          (sync_steps, 1, 76, 1))
            np.testing.assert_array_equal(result_step[5][1].shape,
                                          (sync_steps, 1, 76, 1))
            # compare the monitors between them
            np.testing.assert_array_equal(result_step[2][1], result_step[3][1])
            np.testing.assert_array_equal(result_step[4][1], result_step[5][1])
            np.testing.assert_array_equal(result_step[0][1][:, 0, :, :],
                                          result_step[1][1][:, 0, :, :])