Ejemplo n.º 1
0
    def _prepare_simulator_from_view_model(self, view_model):
        simulator = Simulator()
        simulator.gid = view_model.gid

        conn = self.load_traited_by_gid(view_model.connectivity)
        simulator.connectivity = conn

        simulator.conduction_speed = view_model.conduction_speed
        simulator.coupling = view_model.coupling

        rm_surface = None

        if view_model.surface:
            simulator.surface = Cortex()
            rm_index = self.load_entity_by_gid(
                view_model.surface.region_mapping_data.hex)
            rm = h5.load_from_index(rm_index)

            rm_surface_index = self.load_entity_by_gid(rm_index.fk_surface_gid)
            rm_surface = h5.load_from_index(rm_surface_index, CorticalSurface)
            rm.surface = rm_surface
            rm.connectivity = conn

            simulator.surface.region_mapping_data = rm
            if simulator.surface.local_connectivity:
                lc = self.load_traited_by_gid(
                    view_model.surface.local_connectivity)
                assert lc.surface.gid == rm_index.fk_surface_gid
                lc.surface = rm_surface
                simulator.surface.local_connectivity = lc

        if view_model.stimulus:
            stimulus_index = self.load_entity_by_gid(view_model.stimulus.hex)
            stimulus = h5.load_from_index(stimulus_index)
            simulator.stimulus = stimulus

            if isinstance(stimulus, StimuliSurface):
                simulator.stimulus.surface = rm_surface
            else:
                simulator.stimulus.connectivity = simulator.connectivity

        simulator.model = view_model.model
        simulator.integrator = view_model.integrator
        simulator.initial_conditions = view_model.initial_conditions
        simulator.monitors = view_model.monitors
        simulator.simulation_length = view_model.simulation_length

        # TODO: why not load history here?
        # if view_model.history:
        #     history_index = dao.get_datatype_by_gid(view_model.history.hex)
        #     history = h5.load_from_index(history_index)
        #     assert isinstance(history, SimulationHistory)
        #     history.fill_into(self.algorithm)
        return simulator
Ejemplo n.º 2
0
    def test_models_list(self, mocker):
        models_form = SimulatorModelFragment()
        simulator = Simulator()
        simulator.model = ModelsEnum.EPILEPTOR.instance
        models_form.fill_from_trait(simulator)

        rendering_rules = SimulatorFragmentRenderingRules(is_model_fragment=True)
        soup = self.prepare_simulator_form_for_search(mocker, rendering_rules, form=models_form)

        select_field = soup.find_all('select')
        assert len(select_field) == 1, 'Number of select inputs is different than 1'
        select_field_options = soup.find_all('option')
        assert len(select_field_options) == len(ModelsEnum), 'Number of select field options != number of models'
        select_field_choice = soup.find_all('option', selected=True)
        assert len(select_field_choice) == 1
        assert 'Epileptor' in select_field_choice[0].attrs['value']
Ejemplo n.º 3
0
    def test_models_list(self):
        all_models_for_ui = get_ui_name_to_model()
        models_form = SimulatorModelFragment()
        simulator = Simulator()
        simulator.model = ModelsEnum.EPILEPTOR.get_class()()
        models_form.fill_from_trait(simulator)

        html = str(models_form)
        soup = BeautifulSoup(html)

        select_field = soup.find_all('select')
        assert len(select_field) == 1, 'Number of select inputs is different than 1'
        select_field_options = soup.find_all('option')
        assert len(select_field_options) == len(all_models_for_ui), 'Number of select field options != number of models'
        select_field_choice = soup.find_all('option', selected=True)
        assert len(select_field_choice) == 1
        assert 'Epileptor' in select_field_choice[0].attrs['value']
def test(dt=0.1, noise_strength=0.001, config=CONFIGURED):

    # Select the regions for the fine scale modeling with NEST spiking networks
    nest_nodes_ids = []  # the indices of fine scale regions modeled with NEST
    # In this example, we model parahippocampal cortices (left and right) with NEST
    connectivity = Connectivity.from_file(CONFIGURED.DEFAULT_CONNECTIVITY_ZIP)
    for id in range(connectivity.region_labels.shape[0]):
        if connectivity.region_labels[id].find("hippo") > 0:
            nest_nodes_ids.append(id)
    connectivity.configure()

    # Create a TVB simulator and set all desired inputs
    # (connectivity, model, surface, stimuli etc)
    # We choose all defaults in this example
    simulator = Simulator()
    simulator.integrator.dt = dt
    simulator.integrator.noise.nsig = np.array([noise_strength])
    simulator.model = ReducedWongWangExcIOInhI()

    simulator.connectivity = connectivity
    mon_raw = Raw(period=simulator.integrator.dt)
    simulator.monitors = (mon_raw, )

    # Build a NEST network model with the corresponding builder
    # Using all default parameters for this example
    nest_model_builder = RedWWExcIOInhIMultisynapseBuilder(simulator,
                                                           nest_nodes_ids,
                                                           config=config)
    nest_model_builder.configure()
    for prop in [
            "min_delay", "tvb_dt", "tvb_model", "tvb_connectivity",
            "tvb_weights", "tvb_delays", "number_of_nodes",
            "number_of_spiking_nodes", "spiking_nodes_labels",
            "number_of_populations", "populations_models", "populations_nodes",
            "populations_scales", "populations_sizes", "populations_params",
            "populations_connections_labels", "populations_connections_models",
            "populations_connections_nodes", "populations_connections_weights",
            "populations_connections_delays",
            "populations_connections_receptor_types",
            "populations_connections_conn_spec", "nodes_connections_labels",
            "nodes_connections_models", "nodes_connections_source_nodes",
            "nodes_connections_target_nodes", "nodes_connections_weights",
            "nodes_connections_delays", "nodes_connections_receptor_types",
            "nodes_connections_conn_spec"
    ]:
        print("%s:\n%s\n\n" % (prop, str(getattr(nest_model_builder, prop))))
    def build(self, **model_params):
        # Load, normalize and configure connectivity
        if isinstance(self.connectivity, string_types):
            connectivity = Connectivity.from_file(self.connectivity)
        else:
            connectivity = self.connectivity
        if self.scale_connectivity_weights is not None:
            if isinstance(self.scale_connectivity_weights, string_types):
                connectivity.weights = connectivity.scaled_weights(
                    mode=self.scale_connectivity_weights)
            else:
                connectivity.weights /= self.scale_connectivity_weights
        if not self.delays_flag:
            connectivity.configure()  # to set speed
            # Given that
            # idelays = numpy.rint(delays / dt).astype(numpy.int32)
            # and delays = tract_lengths / speed
            connectivity.tract_lengths = 0.1 * self.dt * connectivity.speed
        connectivity.configure()

        # Build model:
        model = self.model(**model_params)

        # Build integrator
        integrator = self.integrator(dt=self.dt)
        integrator.noise.nsig = np.array(ensure_list(self.noise_strength))

        # Build monitors:
        assert Raw in self.monitors
        monitors = []
        for monitor in self.monitors:
            monitors.append(monitor(period=self.dt))
        monitors = tuple(monitors)

        # Build simulator
        simulator = Simulator()

        simulator._config = self.config
        simulator.connectivity = connectivity
        simulator.model = model
        simulator.integrator = integrator
        simulator.monitors = monitors

        return simulator
Ejemplo n.º 6
0
def test(dt=0.1, noise_strength=0.001, config=CONFIGURED):
    # Select the regions for the fine scale modeling with ANNarchy spiking networks
    anarchy_nodes_ids = list(
        range(10))  # the indices of fine scale regions modeled with ANNarchy
    # In this example, we model parahippocampal cortices (left and right) with ANNarchy
    connectivity = Connectivity.from_file(CONFIGURED.DEFAULT_CONNECTIVITY_ZIP)
    connectivity.configure()

    # Create a TVB simulator and set all desired inputs
    # (connectivity, model, surface, stimuli etc)
    # We choose all defaults in this example
    simulator = Simulator()
    simulator.integrator.dt = dt
    # simulator.integrator.noise.nsig = np.array([noise_strength])
    simulator.model = ReducedWongWangExcIOInhI()

    simulator.connectivity = connectivity
    mon_raw = Raw(period=simulator.integrator.dt)
    simulator.monitors = (mon_raw, )

    # Build a ANNarchy network model with the corresponding builder
    # Using all default parameters for this example
    anarchy_model_builder = BasalGangliaIzhikevichBuilder(simulator,
                                                          anarchy_nodes_ids,
                                                          config=config)
    anarchy_model_builder.configure()
    for prop in [
            "min_delay", "tvb_dt", "tvb_model", "tvb_connectivity",
            "tvb_weights", "tvb_delays", "number_of_nodes",
            "number_of_spiking_nodes", "spiking_nodes_labels",
            "number_of_populations", "populations_models", "populations_nodes",
            "populations_scales", "populations_sizes", "populations_params",
            "populations_connections_labels", "populations_connections_models",
            "populations_connections_nodes", "populations_connections_weights",
            "populations_connections_delays",
            "populations_connections_receptor_types",
            "populations_connections_conn_spec", "nodes_connections_labels",
            "nodes_connections_models", "nodes_connections_source_nodes",
            "nodes_connections_target_nodes", "nodes_connections_weights",
            "nodes_connections_delays", "nodes_connections_receptor_types",
            "nodes_connections_conn_spec"
    ]:
        print("%s:\n%s\n\n" %
              (prop, str(getattr(anarchy_model_builder, prop))))
Ejemplo n.º 7
0
def main_example(tvb_sim_model,
                 connectivity_zip=CONFIGURED.DEFAULT_CONNECTIVITY_ZIP,
                 dt=0.1,
                 noise_strength=0.001,
                 simulation_length=100.0,
                 config=CONFIGURED):

    plotter = Plotter(config)

    # --------------------------------------1. Load TVB connectivity----------------------------------------------------
    connectivity = Connectivity.from_file(connectivity_zip)
    connectivity.configure()
    plotter.plot_tvb_connectivity(connectivity)

    # ----------------------2. Define a TVB simulator (model, integrator, monitors...)----------------------------------

    # Create a TVB simulator and set all desired inputs
    # (connectivity, model, surface, stimuli etc)
    # We choose all defaults in this example
    simulator = Simulator()
    simulator.integrator = HeunStochastic(dt=dt)
    simulator.integrator.noise.nsig = np.array(ensure_list(noise_strength))
    simulator.model = tvb_sim_model
    simulator.connectivity = connectivity
    mon_raw = Raw(period=simulator.integrator.dt)
    simulator.monitors = (mon_raw, )

    # -----------------------------------3. Simulate and gather results-------------------------------------------------

    # Configure the simulator with the TVB-NEST interface...
    # simulator.configure(tvb_nest_interface=tvb_nest_model)
    simulator.configure()
    # ...and simulate!
    t_start = time.time()
    results = simulator.run(simulation_length=simulation_length)
    print("\nSimulated in %f secs!" % (time.time() - t_start))

    # -------------------------------------------6. Plot results--------------------------------------------------------

    plot_results(results, simulator, None, "State Variables",
                 simulator.model.variables_of_interest, plotter)

    return connectivity, results