def from_tvb_file(self, filepath, remove_leading_zeros_from_labels=True): self._tvb = TVBSensorsInternal.from_file(filepath, self._tvb) if len(self._tvb.labels) > 0: if remove_leading_zeros_from_labels: self.remove_leading_zeros_from_labels() self.file_path = filepath return self
def setup_method(self): self.sim = simulator.Simulator( connectivity=connectivity.Connectivity.from_file( 'connectivity_192.zip'), monitors=(monitors.iEEG( sensors=SensorsInternal(load_default=True), region_mapping=RegionMapping.from_file( 'regionMapping_16k_192.txt')))).configure()
def setup_method(self): self.sim = simulator.Simulator( connectivity=connectivity.Connectivity( load_file='connectivity_192.zip'), monitors=(monitors.iEEG( sensors=SensorsInternal(load_file="seeg_39.txt.bz2"), region_mapping=RegionMapping( load_file='regionMapping_16k_192.txt')))).configure()
def launch(self, sensors_file, sensors_type): """ Creates required sensors from the uploaded file. :param sensors_file: the file containing sensor data :param sensors_type: a string from "EEG Sensors", "MEG sensors", "Internal Sensors" :returns: a list of sensors instances of the specified type :raises LaunchException: when * no sensors_file specified * sensors_type is invalid (not one of the mentioned options) * sensors_type is "MEG sensors" and no orientation is specified """ if sensors_file is None: raise LaunchException( "Please select sensors file which contains data to import") self.logger.debug("Create sensors instance") if sensors_type == SensorsEEG.sensors_type.default: sensors_inst = SensorsEEG() elif sensors_type == SensorsMEG.sensors_type.default: sensors_inst = SensorsMEG() elif sensors_type == SensorsInternal.sensors_type.default: sensors_inst = SensorsInternal() else: exception_str = "Could not determine sensors type (selected option %s)" % sensors_type raise LaunchException(exception_str) locations = self.read_list_data(sensors_file, usecols=[1, 2, 3]) # NOTE: TVB has the nose pointing -y and left ear pointing +x # If the sensors are in CTF coordinates : nose pointing +x left ear +y # to rotate the sensors by -90 along z uncomment below # locations = numpy.array([[0, 1, 0], [-1, 0, 0], [0, 0, 1]]).dot(locations.T).T sensors_inst.locations = locations sensors_inst.labels = self.read_list_data(sensors_file, dtype=MEMORY_STRING, usecols=[0]) sensors_inst.number_of_sensors = sensors_inst.labels.size if isinstance(sensors_inst, SensorsMEG): try: sensors_inst.orientations = self.read_list_data( sensors_file, usecols=[4, 5, 6]) sensors_inst.has_orientation = True except IndexError: raise LaunchException( "Uploaded file does not contains sensors orientation.") sensors_inst.configure() self.logger.debug("Sensors instance ready to be stored") sensors_idx = h5.store_complete(sensors_inst, self.storage_path) self.generic_attributes.user_tag_1 = sensors_inst.sensors_type return sensors_idx
def launch(self, sensors_file, sensors_type): """ Creates required sensors from the uploaded file. :param sensors_file: the file containing sensor data :param sensors_type: a string from "EEG Sensors", "MEG sensors", "Internal Sensors" :returns: a list of sensors instances of the specified type :raises LaunchException: when * no sensors_file specified * sensors_type is invalid (not one of the mentioned options) * sensors_type is "MEG sensors" and no orientation is specified """ if sensors_file is None: raise LaunchException( "Please select sensors file which contains data to import") self.logger.debug("Create sensors instance") if sensors_type == self.EEG_SENSORS: sensors_inst = SensorsEEG() elif sensors_type == self.MEG_SENSORS: sensors_inst = SensorsMEG() elif sensors_type == self.INTERNAL_SENSORS: sensors_inst = SensorsInternal() else: exception_str = "Could not determine sensors type (selected option %s)" % sensors_type raise LaunchException(exception_str) sensors_inst.storage_path = self.storage_path locations = self.read_list_data(sensors_file, usecols=[1, 2, 3]) # NOTE: TVB has the nose pointing -y and left ear pointing +x # If the sensors are in CTF coordinates : nose pointing +x left ear +y # to rotate the sensors by -90 along z uncomment below # locations = numpy.array([[0, 1, 0], [-1, 0, 0], [0, 0, 1]]).dot(locations.T).T sensors_inst.locations = locations sensors_inst.labels = self.read_list_data(sensors_file, dtype=numpy.str, usecols=[0]) if isinstance(sensors_inst, SensorsMEG): try: sensors_inst.orientations = self.read_list_data( sensors_file, usecols=[4, 5, 6]) except IndexError: raise LaunchException( "Uploaded file does not contains sensors orientation.") self.logger.debug("Sensors instance ready to be stored") return [sensors_inst]
def test_gain_size(self): sim = simulator.Simulator( connectivity=connectivity.Connectivity.from_file('connectivity_192.zip'), monitors=(monitors.iEEG( sensors=SensorsInternal.from_file(), region_mapping=RegionMapping.from_file('regionMapping_16k_192.txt') ),) ).configure() ieeg = sim.monitors[0] # type: SensorsInternal n_sens, n_reg = ieeg.gain.shape assert ieeg.sensors.locations.shape[0] == n_sens assert sim.connectivity.number_of_regions == n_reg
def deserialize_simulator(simulator_gid, storage_path): simulator_in_path = h5.path_for(storage_path, SimulatorH5, simulator_gid) simulator_in = SimulatorAdapterModel() with SimulatorH5(simulator_in_path) as simulator_in_h5: simulator_in_h5.load_into(simulator_in) simulator_in.connectivity = simulator_in_h5.connectivity.load() simulator_in.stimulus = simulator_in_h5.stimulus.load() simulator_in.history_gid = simulator_in_h5.simulation_state.load() if isinstance(simulator_in.monitors[0], Projection): # TODO: simplify this part with SimulatorH5(simulator_in_path) as simulator_in_h5: monitor_h5_path = simulator_in_h5.get_reference_path( simulator_in.monitors[0].gid) monitor_h5_class = h5_factory.monitor_h5_factory( type(simulator_in.monitors[0])) with monitor_h5_class(monitor_h5_path) as monitor_h5: sensors_gid = monitor_h5.sensors.load() region_mapping_gid = monitor_h5.region_mapping.load() sensors = ABCAdapter.load_traited_by_gid(sensors_gid) if isinstance(simulator_in.monitors[0], EEG): sensors = SensorsEEG.build_sensors_subclass(sensors) elif isinstance(simulator_in.monitors[0], MEG): sensors = SensorsMEG.build_sensors_subclass(sensors) elif isinstance(simulator_in.monitors[0], iEEG): sensors = SensorsInternal.build_sensors_subclass(sensors) simulator_in.monitors[0].sensors = sensors region_mapping = ABCAdapter.load_traited_by_gid(region_mapping_gid) simulator_in.monitors[0].region_mapping = region_mapping if simulator_in.surface: cortex_path = h5.path_for(storage_path, CortexH5, simulator_in.surface.gid) with CortexH5(cortex_path) as cortex_h5: simulator_in.surface.local_connectivity = cortex_h5.local_connectivity.load( ) simulator_in.surface.region_mapping_data = cortex_h5.region_mapping_data.load( ) rm_index = dao.get_datatype_by_gid( simulator_in.surface.region_mapping_data.hex) simulator_in.surface.surface_gid = uuid.UUID( rm_index.surface_gid) return simulator_in
def test_import_internal_sensors(self): """ This method tests import of a file containing internal sensors. """ internal_sensors = self._import(self.EEG_FILE, self.importer.INTERNAL_SENSORS, SensorsInternal()) expected_size = 62 assert internal_sensors.labels is not None assert expected_size == len(internal_sensors.labels) assert expected_size == len(internal_sensors.locations) assert (expected_size, 3) == internal_sensors.locations.shape assert expected_size == internal_sensors.number_of_sensors
def launch(self, sensors_file, sensors_type): """ Creates required sensors from the uploaded file. :param sensors_file: the file containing sensor data :param sensors_type: a string from "EEG Sensors", "MEG sensors", "Internal Sensors" :returns: a list of sensors instances of the specified type :raises LaunchException: when * no sensors_file specified * sensors_type is invalid (not one of the mentioned options) * sensors_type is "MEG sensors" and no orientation is specified """ if sensors_file is None: raise LaunchException( "Please select sensors file which contains data to import") sensors_inst = None self.logger.debug("Create sensors instance") if sensors_type == self.EEG_SENSORS: sensors_inst = SensorsEEG() elif sensors_type == self.MEG_SENSORS: sensors_inst = SensorsMEG() elif sensors_type == self.INTERNAL_SENSORS: sensors_inst = SensorsInternal() else: exception_str = "Could not determine sensors type (selected option %s)" % sensors_type raise LaunchException(exception_str) sensors_inst.storage_path = self.storage_path sensors_inst.locations = read_list_data(sensors_file, usecols=[1, 2, 3]) sensors_inst.labels = read_list_data(sensors_file, dtype=numpy.str, usecols=[0]) if isinstance(sensors_inst, SensorsMEG): try: sensors_inst.orientations = read_list_data(sensors_file, usecols=[4, 5, 6]) except IndexError: raise LaunchException( "Uploaded file does not contains sensors orientation.") self.logger.debug("Sensors instance ready to be stored") return [sensors_inst]
def test_launch_internal(self): """ Check that all required keys are present in output from InternalSensorViewer launch. """ zip_path = os.path.join(os.path.dirname(tvb_data.sensors.__file__), 'internal_39.txt.bz2') TestFactory.import_sensors(self.test_user, self.test_project, zip_path, Sensors_Importer.INTERNAL_SENSORS) sensors = TestFactory.get_entity(self.test_project, SensorsInternal()) viewer = SensorsViewer() viewer.current_project_id = self.test_project.id result = viewer.launch(sensors) self.assert_compliant_dictionary(self.EXPECTED_KEYS_INTERNAL, result)
def launch(self, sensors_file, sensors_type): """ Created required sensors from the uploaded file. """ if sensors_file is None: raise LaunchException( "Please select sensors file which contains data to import") sensors_inst = None self.logger.debug("Create sensors instance") if sensors_type == self.EEG_SENSORS: sensors_inst = SensorsEEG() elif sensors_type == self.MEG_SENSORS: sensors_inst = SensorsMEG() elif sensors_type == self.INTERNAL_SENSORS: sensors_inst = SensorsInternal() else: exception_str = "Could not determine sensors type (selected option %s)" % sensors_type raise LaunchException(exception_str) sensors_inst.storage_path = self.storage_path sensors_inst.locations = read_list_data(sensors_file, usecols=[1, 2, 3]) sensors_inst.labels = read_list_data(sensors_file, dtype=numpy.str, usecols=[0]) if isinstance(sensors_inst, SensorsMEG): try: sensors_inst.orientations = read_list_data(sensors_file, usecols=[4, 5, 6]) except IndexError: raise LaunchException( "Uploaded file does not contains sensors orientation.") self.logger.debug("Sensors instance ready to be stored") return [sensors_inst]
def setup_method(self): oscillator = models.Generic2dOscillator() white_matter = connectivity.Connectivity(load_file='connectivity_' + str(self.n_regions) + '.zip') white_matter.speed = numpy.array([self.speed]) white_matter_coupling = coupling.Difference(a=self.coupling_a) heunint = integrators.HeunStochastic( dt=2**-4, noise=noise.Additive(nsig=numpy.array([ 2**-10, ]))) mons = ( monitors.EEG(projection=ProjectionMatrix( load_file='projection_eeg_65_surface_16k.npy'), sensors=SensorsEEG(load_file="eeg_brainstorm_65.txt"), period=self.period), monitors.MEG( projection=ProjectionMatrix( load_file='projection_meg_276_surface_16k.npy'), sensors=SensorsMEG(load_file='meg_brainstorm_276.txt'), period=self.period), monitors.iEEG(projection=ProjectionMatrix( load_file='projection_seeg_588_surface_16k.npy'), sensors=SensorsInternal(load_file='seeg_588.txt'), period=self.period), ) local_coupling_strength = numpy.array([2**-10]) region_mapping = RegionMapping(load_file='regionMapping_16k_' + str(self.n_regions) + '.txt') default_cortex = Cortex( region_mapping_data=region_mapping, load_file="cortex_16384.zip" ) #region_mapping_file="regionMapping_16k_192.txt") default_cortex.coupling_strength = local_coupling_strength self.sim = simulator.Simulator(model=oscillator, connectivity=white_matter, coupling=white_matter_coupling, integrator=heunint, monitors=mons, surface=default_cortex) self.sim.configure()
def init(param_tvb_connection, param_tvb_coupling, param_tvb_integrator, param_tvb_model, param_tvb_monitor, cosim=None): ''' Initialise the simulator with parameter :param param_tvb_connection : parameters for the connection :param param_tvb_coupling : parameters for the coupling between nodes :param param_tvb_integrator : parameters of the integrator and the noise :param param_tvb_model : parameters for the models of TVB :param param_tvb_monitor : parameters for TVB monitors :param cosim : if use or not mpi :return: the simulator initialize ''' ## initialise the random generator rgn.seed(param_tvb_integrator['seed_init'] - 1) ## Model configuration if param_tvb_model['order'] == 1: model = Zerlaut.ZerlautAdaptationFirstOrder( variables_of_interest='E I W_e W_i'.split()) elif param_tvb_model['order'] == 2: model = Zerlaut.ZerlautAdaptationSecondOrder( variables_of_interest='E I C_ee C_ei C_ii W_e W_i'.split()) else: raise Exception('Bad order for the model') model.g_L = np.array(param_tvb_model['g_L']) model.E_L_e = np.array(param_tvb_model['E_L_e']) model.E_L_i = np.array(param_tvb_model['E_L_i']) model.C_m = np.array(param_tvb_model['C_m']) model.b_e = np.array(param_tvb_model['b_e']) model.a_e = np.array(param_tvb_model['a_e']) model.b_i = np.array(param_tvb_model['b_i']) model.a_i = np.array(param_tvb_model['a_i']) model.tau_w_e = np.array(param_tvb_model['tau_w_e']) model.tau_w_i = np.array(param_tvb_model['tau_w_i']) model.E_e = np.array(param_tvb_model['E_e']) model.E_i = np.array(param_tvb_model['E_i']) model.Q_e = np.array(param_tvb_model['Q_e']) model.Q_i = np.array(param_tvb_model['Q_i']) model.tau_e = np.array(param_tvb_model['tau_e']) model.tau_i = np.array(param_tvb_model['tau_i']) model.N_tot = np.array(param_tvb_model['N_tot']) model.p_connect = np.array(param_tvb_model['p_connect']) model.g = np.array(param_tvb_model['g']) model.T = np.array(param_tvb_model['T']) model.P_e = np.array(param_tvb_model['P_e']) model.P_i = np.array(param_tvb_model['P_i']) model.K_ext_e = np.array(param_tvb_model['K_ext_e']) model.K_ext_i = np.array(0) model.external_input_ex_ex = np.array(0.) model.external_input_ex_in = np.array(0.) model.external_input_in_ex = np.array(0.0) model.external_input_in_in = np.array(0.0) model.state_variable_range['E'] = np.array( param_tvb_model['initial_condition']['E']) model.state_variable_range['I'] = np.array( param_tvb_model['initial_condition']['I']) if param_tvb_model['order'] == 2: model.state_variable_range['C_ee'] = np.array( param_tvb_model['initial_condition']['C_ee']) model.state_variable_range['C_ei'] = np.array( param_tvb_model['initial_condition']['C_ei']) model.state_variable_range['C_ii'] = np.array( param_tvb_model['initial_condition']['C_ii']) model.state_variable_range['W_e'] = np.array( param_tvb_model['initial_condition']['W_e']) model.state_variable_range['W_i'] = np.array( param_tvb_model['initial_condition']['W_i']) ## Connection nb_region = int(param_tvb_connection['nb_region']) tract_lengths = np.load(param_tvb_connection['path_distance']) weights = np.load(param_tvb_connection['path_weight']) if 'path_region_labels' in param_tvb_connection.keys(): region_labels = np.loadtxt(param_tvb_connection['path_region_labels'], dtype=str) else: region_labels = np.array([], dtype=np.dtype('<U128')) if 'path_centers' in param_tvb_connection.keys(): centers = np.loadtxt(param_tvb_connection['path_centers']) else: centers = np.array([]) if 'orientation' in param_tvb_connection.keys( ) and param_tvb_connection['orientation']: orientation = [] for i in np.transpose(centers): orientation.append(findVec(i, np.mean(centers, axis=1))) orientation = np.array(orientation) else: orientation = None if 'path_cortical' in param_tvb_connection.keys(): cortical = np.load(param_tvb_connection['path_cortical']) else: cortical = None connection = lab.connectivity.Connectivity( number_of_regions=nb_region, tract_lengths=tract_lengths[:nb_region, :nb_region], weights=weights[:nb_region, :nb_region], region_labels=region_labels, centres=centers.T, cortical=cortical, orientations=orientation) # if 'normalised' in param_tvb_connection.keys() or param_tvb_connection['normalised']: # connection.weights = connection.weights / np.sum(connection.weights, axis=0) connection.speed = np.array(param_tvb_connection['velocity']) ## Coupling coupling = lab.coupling.Linear(a=np.array(param_tvb_coupling['a']), b=np.array(0.0)) ## Integrator noise = my_noise.Ornstein_Ulhenbeck_process( tau_OU=param_tvb_integrator['tau_OU'], mu=np.array(param_tvb_integrator['mu']).reshape((7, 1, 1)), nsig=np.array(param_tvb_integrator['nsig']), weights=np.array(param_tvb_integrator['weights']).reshape((7, 1, 1))) noise.random_stream.seed(param_tvb_integrator['seed']) integrator = lab.integrators.HeunStochastic( noise=noise, dt=param_tvb_integrator['sim_resolution']) # integrator = lab.integrators.HeunDeterministic() ## Monitors monitors = [] if param_tvb_monitor['Raw']: monitors.append(lab.monitors.Raw()) if param_tvb_monitor['TemporalAverage']: monitor_TAVG = lab.monitors.TemporalAverage( variables_of_interest=param_tvb_monitor[ 'parameter_TemporalAverage']['variables_of_interest'], period=param_tvb_monitor['parameter_TemporalAverage']['period']) monitors.append(monitor_TAVG) if param_tvb_monitor['Bold']: monitor_Bold = lab.monitors.Bold( variables_of_interest=np.array( param_tvb_monitor['parameter_Bold']['variables_of_interest']), period=param_tvb_monitor['parameter_Bold']['period']) monitors.append(monitor_Bold) if param_tvb_monitor['SEEG']: sensor = SensorsInternal().from_file( param_tvb_monitor['parameter_SEEG']['path']) projection_matrix = param_tvb_monitor['parameter_SEEG'][ 'scaling_projection'] / np.array([ np.linalg.norm(np.expand_dims(i, 1) - centers[:, cortical], axis=0) for i in sensor.locations ]) np.save(param_tvb_monitor['path_result'] + '/projection.npy', projection_matrix) monitors.append( lab.monitors.iEEG.from_file( param_tvb_monitor['parameter_SEEG']['path'], param_tvb_monitor['path_result'] + '/projection.npy')) if cosim is not None: # special monitor for MPI monitor_IO = Interface_co_simulation( id_proxy=cosim['id_proxy'], time_synchronize=cosim['time_synchronize']) monitors.append(monitor_IO) #initialize the simulator: simulator = lab.simulator.Simulator(model=model, connectivity=connection, coupling=coupling, integrator=integrator, monitors=monitors) simulator.configure() # save the initial condition np.save(param_tvb_monitor['path_result'] + '/step_init.npy', simulator.history.buffer) # end edit return simulator