コード例 #1
0
def tvb_model(dt, weight, delay, id_proxy):
    """
        Initialise TVB with Wong-Wang models and default connectivity

        WARNING : in this first example most of the parameter for the simulation is define. In the future, this function
        will be disappear and replace only by the tvb_init. This function is only here in order to constraint the usage
         of proxy
    :param dt: the resolution of the raw monitor (ms)
    :param weight: weight on the connexion
    :param delay: delay on the connexion
    :param id_proxy: id of the proxy
    :return:
        populations: model in TVB
        white_matter: Connectivity in TVB
        white_matter_coupling: Coupling in TVB
        heunint: Integrator in TVB
        what_to_watch: Monitor in TVB
    """
    region_label = np.repeat(['real'], len(weight))  # name of region fixed can be modify for parameter of function
    region_label[id_proxy] = 'proxy'
    populations = ReducedWongWangProxy()
    white_matter = lab.connectivity.Connectivity(region_labels=region_label,
                                                 weights=weight,
                                                 speed=np.array(1.0),
                                                 tract_lengths=delay,
                                                 # TVB don't take care about the delay only the track length and speed
                                                 #  delai = tract_lengths/speed
                                                 centres=np.ones((weight.shape[0], 3))
                                                 )
    white_matter_coupling = lab.coupling.Linear(a=np.array(0.0154))
    heunint = lab.integrators.EulerDeterministic(dt=dt, bounded_state_variable_indices=np.array([0]),
                                                state_variable_boundaries=np.array([[0.0, 1.0]]))
    return populations, white_matter, white_matter_coupling, heunint, id_proxy
コード例 #2
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)])
コード例 #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 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])
コード例 #5
0
    def _prepare_reference_simulation():
        # reference simulation
        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 = ReducedWongWangProxy(tau_s=np.random.rand(76))
        connectivity = lab.connectivity.Connectivity().from_file()
        connectivity.speed = np.array([4.0])
        connectivity.configure()
        coupling = lab.coupling.Linear(a=np.array(0.0154))
        integrator = lab.integrators.HeunDeterministic(dt=0.1, bounded_state_variable_indices=np.array([0]),
                                                       state_variable_boundaries=np.array([[0.0, 1.0]]))
        monitors = lab.monitors.Raw(period=0.1, variables_of_interest=np.array(0, dtype=np.int))

        return model, connectivity, coupling, init, integrator, monitors
コード例 #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
 def test_without_voi(self):
     model, connectivity, coupling, init, integrator, monitors = self._prepare_reference_simulation()
     np.random.seed(42)
     id_proxy = range(11)
     model = ReducedWongWangProxy(tau_s=np.random.rand(76))
     # Initialise a Simulator -- Model, Connectivity, Integrator, and Monitors.
     sim = CoSimulator(
         voi=np.array([], dtype=np.int),
         synchronization_time=1.,
         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,
     )
     with pytest.raises(ValueError):
         sim.configure()
コード例 #8
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, :, :])