def from_file(cls, source_file="cortex_16384.zip", region_mapping_file=os.path.join("regionMapping_16k_76.txt"), local_connectivity_file=None, eeg_projection_file=None, instance=None): result = super(Cortex, cls).from_file(source_file, instance) if instance is not None: # Called through constructor directly if result.region_mapping is None: result.region_mapping_data = RegionMapping.from_file() if not result.eeg_projection: result.eeg_projection = Cortex.from_file_projection_array() if result.local_connectivity is None: result.local_connectivity = LocalConnectivity.from_file() if region_mapping_file is not None: result.region_mapping_data = RegionMapping.from_file(region_mapping_file) if local_connectivity_file is not None: result.local_connectivity = LocalConnectivity.from_file(local_connectivity_file) if eeg_projection_file is not None: result.eeg_projection = Cortex.from_file_projection_array(eeg_projection_file) return result
def configure(self, dt=2**-3, model=models.Generic2dOscillator, speed=4.0, coupling_strength=0.00042, method="HeunDeterministic", surface_sim=False, default_connectivity=True): """ Create an instance of the Simulator class, by default use the generic plane oscillator local dynamic model and the deterministic version of Heun's method for the numerical integration. """ self.method = method if default_connectivity: white_matter = connectivity.Connectivity(load_default=True) # NOTE: This is the default region mapping should consider changing the name. region_mapping = RegionMapping.from_file( source_file= "cortex_reg13/region_mapping/o52r00_irp2008_hemisphere_both_subcortical_false_regions_74.txt.bz2" ) else: white_matter = connectivity.Connectivity.from_file( source_file="connectivity_190.zip") region_mapping = RegionMapping.from_file( source_file= "cortex_reg13/region_mapping/o52r00_irp2008_hemisphere_both_subcortical_true_regions_190.txt.bz2" ) white_matter_coupling = coupling.Linear(a=coupling_strength) white_matter.speed = speed dynamics = model() if method[-10:] == "Stochastic": hisss = noise.Additive(nsig=numpy.array([2**-11])) integrator = eval("integrators." + method + "(dt=dt, noise=hisss)") else: integrator = eval("integrators." + method + "(dt=dt)") if surface_sim: local_coupling_strength = numpy.array([2**-10]) default_cortex = Cortex(load_default=True, region_mapping_data=region_mapping) default_cortex.coupling_strength = local_coupling_strength default_cortex.local_connectivity = LocalConnectivity( load_default=default_connectivity, surface=default_cortex) else: default_cortex = None # Order of monitors determines order of returned values. self.sim = simulator.Simulator(model=dynamics, connectivity=white_matter, coupling=white_matter_coupling, integrator=integrator, monitors=self.monitors, surface=default_cortex) self.sim.configure()
def test_store_load_complete_region_mapping(tmph5factory, connectivity_factory, surface_factory, region_mapping_factory): connectivity = connectivity_factory(2) surface = surface_factory(5) region_mapping = region_mapping_factory(surface, connectivity) with ConnectivityH5(tmph5factory('Connectivity_{}.h5'.format(connectivity.gid))) as conn_h5: conn_h5.store(connectivity) conn_stored = Connectivity() conn_h5.load_into(conn_stored) with SurfaceH5(tmph5factory('Surface_{}.h5'.format(surface.gid))) as surf_h5: surf_h5.store(surface) surf_stored = Surface() surf_h5.load_into(surf_stored) with RegionMappingH5(tmph5factory('RegionMapping_{}.h5'.format(region_mapping.gid))) as rm_h5: rm_h5.store(region_mapping) rm_stored = RegionMapping() rm_h5.load_into(rm_stored) # load_into will not load dependent datatypes. connectivity and surface are undefined with pytest.raises(TraitAttributeError): rm_stored.connectivity with pytest.raises(TraitAttributeError): rm_stored.surface rm_stored.connectivity = conn_stored rm_stored.surface = surf_stored assert rm_stored.connectivity is not None assert rm_stored.surface is not None
def deserialize_simulator(simulator_gid, storage_path): simulator_in_path = h5.path_for(storage_path, SimulatorH5, simulator_gid) simulator_in = Simulator() with SimulatorH5(simulator_in_path) as simulator_in_h5: simulator_in_h5.load_into(simulator_in) connectivity_gid = simulator_in_h5.connectivity.load() stimulus_gid = simulator_in_h5.stimulus.load() simulation_state_gid = simulator_in_h5.simulation_state.load() conn_index = dao.get_datatype_by_gid(connectivity_gid.hex) conn = h5.load_from_index(conn_index) simulator_in.connectivity = conn if simulator_in.surface: cortex_path = h5.path_for(storage_path, CortexH5, simulator_in.surface.gid) with CortexH5(cortex_path) as cortex_h5: local_conn_gid = cortex_h5.local_connectivity.load() region_mapping_gid = cortex_h5.region_mapping_data.load() region_mapping_index = dao.get_datatype_by_gid( region_mapping_gid.hex) region_mapping_path = h5.path_for_stored_index( region_mapping_index) region_mapping = RegionMapping() with RegionMappingH5(region_mapping_path) as region_mapping_h5: region_mapping_h5.load_into(region_mapping) region_mapping.gid = region_mapping_h5.gid.load() surf_gid = region_mapping_h5.surface.load() surf_index = dao.get_datatype_by_gid(surf_gid.hex) surf_h5 = h5.h5_file_for_index(surf_index) surf = CorticalSurface() surf_h5.load_into(surf) surf_h5.close() region_mapping.surface = surf simulator_in.surface.region_mapping_data = region_mapping if local_conn_gid: local_conn_index = dao.get_datatype_by_gid(local_conn_gid.hex) local_conn = h5.load_from_index(local_conn_index) simulator_in.surface.local_connectivity = local_conn if stimulus_gid: stimulus_index = dao.get_datatype_by_gid(stimulus_gid.hex) stimulus = h5.load_from_index(stimulus_index) simulator_in.stimulus = stimulus return simulator_in, simulation_state_gid
def setup_method(self): oscillator = models.Generic2dOscillator() white_matter = connectivity.Connectivity.from_file( 'connectivity_%d.zip' % (self.n_regions, )) 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.from_file(period=self.period), monitors.MEG.from_file(period=self.period), monitors.iEEG.from_file(period=self.period), ) local_coupling_strength = numpy.array([2**-10]) region_mapping = RegionMapping.from_file('regionMapping_16k_%d.txt' % (self.n_regions, )) default_cortex = Cortex(region_mapping_data=region_mapping, load_default=True) 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 _import(self, import_file_path, surface_gid, connectivity_gid): """ This method is used for importing region mappings :param import_file_path: absolute path of the file to be imported """ ### Retrieve Adapter instance group = dao.find_group( 'tvb.adapters.uploaders.region_mapping_importer', 'RegionMapping_Importer') importer = ABCAdapter.build_adapter(group) args = { 'mapping_file': import_file_path, 'surface': surface_gid, 'connectivity': connectivity_gid, DataTypeMetaData.KEY_SUBJECT: "test" } now = datetime.datetime.now() ### Launch import Operation FlowService().fire_operation(importer, self.test_user, self.test_project.id, **args) # During setup we import a CFF which creates an additional RegionMapping # So, here we have to find our mapping (just imported) data_filter = FilterChain( fields=[FilterChain.datatype + ".create_date"], operations=[">"], values=[now]) region_mapping = self._get_entity(RegionMapping(), data_filter) return region_mapping
def configure(self, dt=2 ** -3, model=models.Generic2dOscillator, speed=4.0, coupling_strength=0.00042, method="HeunDeterministic", surface_sim=False, default_connectivity=True): """ Create an instance of the Simulator class, by default use the generic plane oscillator local dynamic model and the deterministic version of Heun's method for the numerical integration. """ self.method = method if default_connectivity: white_matter = Connectivity(load_file="connectivity_76.zip") region_mapping = RegionMapping(load_file="regionMapping_16k_76.txt") else: white_matter = Connectivity(load_file="connectivity_192.zip") region_mapping = RegionMapping(load_file="regionMapping_16k_192.txt") white_matter_coupling = coupling.Linear(a=coupling_strength) white_matter.speed = speed dynamics = model() if method[-10:] == "Stochastic": hisss = noise.Additive(nsig=numpy.array([2 ** -11])) integrator = eval("integrators." + method + "(dt=dt, noise=hisss)") else: integrator = eval("integrators." + method + "(dt=dt)") if surface_sim: local_coupling_strength = numpy.array([2 ** -10]) default_cortex = Cortex(region_mapping_data=region_mapping, load_file="cortex_16384.zip") default_cortex.coupling_strength = local_coupling_strength default_cortex.local_connectivity = LocalConnectivity(load_file="local_connectivity_16384.mat") else: default_cortex = None # Order of monitors determines order of returned values. self.sim = simulator.Simulator(model=dynamics, connectivity=white_matter, coupling=white_matter_coupling, integrator=integrator, monitors=self.monitors, surface=default_cortex) self.sim.configure()
def _store_related_region_mappings(self, original_conn_gid, new_connectivity_ht): result = [] linked_region_mappings = dao.get_generic_entity(RegionMappingIndex, original_conn_gid, 'fk_connectivity_gid') for mapping in linked_region_mappings: original_rm = h5.load_from_index(mapping) surface = self.load_traited_by_gid(mapping.fk_surface_gid) new_rm = RegionMapping() new_rm.connectivity = new_connectivity_ht new_rm.surface = surface new_rm.array_data = original_rm.array_data result_rm_index = self.store_complete(new_rm) result.append(result_rm_index) return result
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 build(surface=None, connectivity=None): if not surface: surface = surface_factory(5) if not connectivity: connectivity = connectivity_factory(2) return RegionMapping(array_data=numpy.arange( surface.number_of_vertices), connectivity=connectivity, surface=surface)
def test_surface_sim_with_projections(self): # Setup Simulator obj oscillator = models.Generic2dOscillator() white_matter = connectivity.Connectivity.from_file('connectivity_%d.zip' % (self.n_regions,)) 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.from_file(period=self.period), monitors.MEG.from_file(period=self.period), # monitors.iEEG.from_file(period=self.period), # SEEG projection data is not part of tvb-data on Pypi, thus this can not work generic ) local_coupling_strength = numpy.array([2 ** -10]) region_mapping = RegionMapping.from_file('regionMapping_16k_%d.txt' % (self.n_regions,)) region_mapping.surface = CorticalSurface.from_file() default_cortex = Cortex.from_file() default_cortex.region_mapping_data = region_mapping default_cortex.coupling_strength = local_coupling_strength sim = simulator.Simulator(model=oscillator, connectivity=white_matter, coupling=white_matter_coupling, integrator=heunint, monitors=mons, surface=default_cortex) sim.configure() # check configured simulation connectivity attribute conn = sim.connectivity assert conn.number_of_regions == self.n_regions assert conn.speed == self.speed # test monitor properties lc_n_node = sim.surface.local_connectivity.matrix.shape[0] for mon in sim.monitors: assert mon.period == self.period n_sens, g_n_node = mon.gain.shape assert g_n_node == sim.number_of_nodes assert n_sens == mon.sensors.number_of_sensors assert lc_n_node == g_n_node # check output shape ys = {} mons = 'eeg meg seeg'.split() for key in mons: ys[key] = [] for data in sim(simulation_length=3.0): for key, dat in zip(mons, data): if dat: _, y = dat ys[key].append(y) for mon, key in zip(sim.monitors, mons): ys[key] = numpy.array(ys[key]) assert ys[key].shape[2] == mon.gain.shape[0]
def test_store_load_region_mapping(tmph5factory, region_mapping_factory): region_mapping = region_mapping_factory() rm_h5 = RegionMappingH5(tmph5factory()) rm_h5.store(region_mapping) rm_h5.close() rm_stored = RegionMapping() with pytest.raises(TraitAttributeError): rm_stored.array_data rm_h5.load_into(rm_stored) # loads connectivity/surface as None inside rm_stored assert rm_stored.array_data.shape == (5,)
def from_file(cls, sensors_fname, projection_fname, rm_f_name="regionMapping_16k_76.txt", period=1e3/1024.0, **kwds): """ Build Projection-based monitor from sensors and projection files, and any extra keyword arguments are passed to the monitor class constructor. """ result = cls(period=period, **kwds) result.sensors = cls.sensors.field_type.from_file(sensors_fname) result.projection = cls.projection_class().from_file(projection_fname) result.region_mapping = RegionMapping.from_file(rm_f_name) return result
def _store_related_region_mappings(self, original_conn_gid, new_connectivity_ht): result = [] linked_region_mappings = dao.get_generic_entity( RegionMappingIndex, original_conn_gid, 'connectivity_gid') for mapping in linked_region_mappings: original_rm = h5.load_from_index(mapping) surface_idx = dao.get_generic_entity(SurfaceIndex, mapping.surface_gid, 'gid')[0] surface = h5.load_from_index(surface_idx) new_rm = RegionMapping() new_rm.connectivity = new_connectivity_ht new_rm.surface = surface new_rm.array_data = original_rm.array_data result_rm_index = h5.store_complete(new_rm, self.storage_path) result.append(result_rm_index) return result
def from_file( cls, source_file=os.path.join("cortex_reg13", "surface_cortex_reg13.zip"), region_mapping_file=os.path. join( "cortex_reg13", "region_mapping", "o52r00_irp2008_hemisphere_both_subcortical_false_regions_74.txt.bz2" ), local_connectivity_file=None, eeg_projection_file=None, instance=None): result = super(Cortex, cls).from_file(source_file, instance) if instance is not None: # Called through constructor directly if result.region_mapping is None: result.region_mapping_data = RegionMapping.from_file() if not result.eeg_projection: result.eeg_projection = Cortex.from_file_projection_array() if result.local_connectivity is None: result.local_connectivity = LocalConnectivity.from_file() if region_mapping_file is not None: result.region_mapping_data = RegionMapping.from_file( region_mapping_file) if local_connectivity_file is not None: result.local_connectivity = LocalConnectivity.from_file( local_connectivity_file) if eeg_projection_file is not None: result.eeg_projection = Cortex.from_file_projection_array( eeg_projection_file) return result
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 setUp(self): """ Sets up the environment for running the tests; creates a test user, a test project, a connectivity and a surface; imports a CFF data-set """ self.test_user = TestFactory.create_user() self.test_project = TestFactory.import_default_project(self.test_user) self.surface = TestFactory.get_entity(self.test_project, CorticalSurface()) self.assertTrue(self.surface is not None) self.region_mapping = TestFactory.get_entity(self.test_project, RegionMapping()) self.assertTrue(self.region_mapping is not None)
def __init__(self, region_mapping=None, sensors=None, projection=None, *args, **kwargs): if region_mapping is None: region_mapping = RegionMapping() self.region_mapping = region_mapping if sensors is None: sensors = SensorsEEG() self.sensors = sensors self.projection = projection super(Projection, self).__init__(*args, **kwargs)
def from_file(cls, sensors_fname, projection_fname, rm_f_name="regionMapping_16k_76.txt", period=1e3/1024.0, instance=None, **kwds): """ Build Projection-based monitor from sensors and projection files, and any extra keyword arguments are passed to the monitor class constructor. """ if instance is None: result = cls(**kwds) else: result = instance result.sensors = type(cls.sensors).from_file(sensors_fname) result.projection = cls._projection_class().from_file(projection_fname) result.region_mapping = RegionMapping.from_file(rm_f_name) return result
def configure_simulation(stimulate): """ Set up a Simulator object (a brain network model and all its individual components + output modalities) """ # eeg projection matrix from regions to sensors LOG.info("Reading sensors info") pr = ProjectionSurfaceEEG(load_default=True) sensors = SensorsEEG.from_file(source_file="eeg_brainstorm_65.txt") rm = RegionMapping(load_default=True) #Initialise a Model, Connectivity, Coupling, set speed. oscilator = models.Generic2dOscillator(a=-0.5, b=-10., c=0.0, d=0.02) white_matter = connectivity.Connectivity(load_default=True) white_matter.speed = numpy.array([4.0]) white_matter_coupling = coupling.Linear(a=0.042) #Initialise an Integrator hiss = noise.Additive(nsig=numpy.array([0.00])) # nsigm 0.015 heunint = integrators.HeunStochastic(dt=2**-6, noise=hiss) # Recording techniques what_to_watch = (monitors.TemporalAverage(period=1e3 / 4096.), monitors.EEG(projection=pr, sensors=sensors, region_mapping=rm, period=1e3 / 4096.)) # Stimulation paradigm if stimulate: stimulus = build_stimulus(white_matter) else: stimulus = None #Initialise a Simulator -- Model, Connectivity, Integrator, and Monitors. sim = simulator.Simulator(model=oscilator, connectivity=white_matter, coupling=white_matter_coupling, integrator=heunint, monitors=what_to_watch, stimulus=stimulus) sim.configure() return sim
def test_cortexdata(self): dt = Cortex(load_file="cortex_16384.zip", region_mapping_data=RegionMapping( load_file="regionMapping_16k_76.txt")) assert isinstance(dt, Cortex) assert dt.region_mapping_data is not None ## Initialize Local Connectivity, to avoid long computation time. dt.local_connectivity = LocalConnectivity( load_file="local_connectivity_16384.mat") dt.configure() summary_info = dt._find_summary_info() assert abs(summary_info['Region area, maximum (mm:math:`^2`)'] - 9333.39) < 0.01 assert abs(summary_info['Region area, mean (mm:math:`^2`)'] - 3038.51) < 0.01 assert abs(summary_info['Region area, minimum (mm:math:`^2`)'] - 540.90) < 0.01 assert dt.vertices.shape == (16384, 3) assert dt.vertex_normals.shape == (16384, 3) assert dt.triangles.shape == (32760, 3)
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()
region_mapping_file=region_fname, eeg_projection_file=eeg_fname) ctx.configure() print("cortex loaded") pyplot.figure() ax = pyplot.subplot(111, projection='3d') x, y, z = ctx.vertices.T ax.plot_trisurf(x, y, z, triangles=ctx.triangles, alpha=0.1, edgecolor='k') # pyplot.show() print("cortex plot ready") # unit vectors that describe the location of eeg sensors sensoreeg_fname = os.path.join(master_path, 'DH_20120806_EEGLocations.txt') rm = RegionMapping.from_file(region_fname) sensorsEEG = SensorsEEG.from_file(sensoreeg_fname) prEEG = ProjectionSurfaceEEG.from_file(eeg_fname) fsamp = 1e3 / 1024.0 # 1024 Hz mon = monitors.EEG(sensors=sensorsEEG, projection=prEEG, region_mapping=rm, period=fsamp) sim = simulator.Simulator( connectivity=conn, # conduction speed: 3 mm/ms # coupling: linear - rescales activity propagated # stimulus: None - can be a spatiotemporal function
def test_regionmapping(self): dt = RegionMapping.from_file() assert isinstance(dt, RegionMapping) assert dt.array_data.shape == (16384, )
def launch(self, mapping_file, surface, connectivity): """ Creates region mapping from uploaded data. :param mapping_file: an archive containing data for mapping surface to connectivity :raises LaunchException: when * a parameter is None or missing * archive has more than one file * uploaded files are empty * number of vertices in imported file is different to the number of surface vertices * imported file has negative values * imported file has regions which are not in connectivity """ if mapping_file is None: raise LaunchException("Please select mappings file which contains data to import") if surface is None: raise LaunchException("No surface selected. Please initiate upload again and select a brain surface.") if connectivity is None: raise LaunchException("No connectivity selected. Please initiate upload again and select one.") self.logger.debug("Reading mappings from uploaded file") if zipfile.is_zipfile(mapping_file): tmp_folder = tempfile.mkdtemp(prefix='region_mapping_zip_', dir=TvbProfile.current.TVB_TEMP_FOLDER) try: files = FilesHelper().unpack_zip(mapping_file, tmp_folder) if len(files) > 1: raise LaunchException("Please upload a ZIP file containing only one file.") array_data = self.read_list_data(files[0], dtype=numpy.int32) finally: if os.path.exists(tmp_folder): shutil.rmtree(tmp_folder) else: array_data = self.read_list_data(mapping_file, dtype=numpy.int32) # Now we do some checks before building final RegionMapping if array_data is None or len(array_data) == 0: raise LaunchException("Uploaded file does not contains any data. Please initiate upload with another file.") # Check if we have a mapping for each surface vertex. if len(array_data) != surface.number_of_vertices: msg = "Imported file contains a different number of values than the number of surface vertices. " \ "Imported: %d values while surface has: %d vertices." % (len(array_data), surface.number_of_vertices) raise LaunchException(msg) # Now check if the values from imported file correspond to connectivity regions if array_data.min() < 0: raise LaunchException("Imported file contains negative values. Please fix problem and re-import file") if array_data.max() >= connectivity.number_of_regions: msg = "Imported file contains invalid regions. Found region: %d while selected connectivity has: %d " \ "regions defined (0 based)." % (array_data.max(), connectivity.number_of_regions) raise LaunchException(msg) self.logger.debug("Creating RegionMapping instance") region_mapping_inst = RegionMapping() region_mapping_inst.storage_path = self.storage_path region_mapping_inst.set_operation_id(self.operation_id) region_mapping_inst.surface = surface region_mapping_inst.connectivity = connectivity if array_data is not None: region_mapping_inst.array_data = array_data return [region_mapping_inst]
def launch(self, view_model): # type: (RegionMappingImporterModel) -> [RegionMappingIndex] """ Creates region mapping from uploaded data. :raises LaunchException: when * a parameter is None or missing * archive has more than one file * uploaded files are empty * number of vertices in imported file is different to the number of surface vertices * imported file has negative values * imported file has regions which are not in connectivity """ if view_model.mapping_file is None: raise LaunchException( "Please select mappings file which contains data to import") if view_model.surface is None: raise LaunchException( "No surface selected. Please initiate upload again and select a brain surface." ) if view_model.connectivity is None: raise LaunchException( "No connectivity selected. Please initiate upload again and select one." ) self.logger.debug("Reading mappings from uploaded file") if zipfile.is_zipfile(view_model.mapping_file): tmp_folder = tempfile.mkdtemp( prefix='region_mapping_zip_', dir=TvbProfile.current.TVB_TEMP_FOLDER) try: files = FilesHelper().unpack_zip(view_model.mapping_file, tmp_folder) if len(files) > 1: raise LaunchException( "Please upload a ZIP file containing only one file.") array_data = self.read_list_data(files[0], dtype=numpy.int32) finally: if os.path.exists(tmp_folder): shutil.rmtree(tmp_folder) else: array_data = self.read_list_data(view_model.mapping_file, dtype=numpy.int32) # Now we do some checks before building final RegionMapping if array_data is None or len(array_data) == 0: raise LaunchException( "Uploaded file does not contains any data. Please initiate upload with another file." ) # Check if we have a mapping for each surface vertex. surface_index = self.load_entity_by_gid(view_model.surface) if len(array_data) != surface_index.number_of_vertices: msg = "Imported file contains a different number of values than the number of surface vertices. " \ "Imported: %d values while surface has: %d vertices." % ( len(array_data), surface_index.number_of_vertices) raise LaunchException(msg) # Now check if the values from imported file correspond to connectivity regions if array_data.min() < 0: raise LaunchException( "Imported file contains negative values. Please fix problem and re-import file" ) connectivity_index = self.load_entity_by_gid(view_model.connectivity) if array_data.max() >= connectivity_index.number_of_regions: msg = "Imported file contains invalid regions. Found region: %d while selected connectivity has: %d " \ "regions defined (0 based)." % (array_data.max(), connectivity_index.number_of_regions) raise LaunchException(msg) self.logger.debug("Creating RegionMapping instance") connectivity_ht = h5.load_from_index(connectivity_index) surface_ht = h5.load_from_index(surface_index) region_mapping = RegionMapping(surface=surface_ht, connectivity=connectivity_ht, array_data=array_data) return h5.store_complete(region_mapping, self.storage_path)
def test_regionmapping(self): dt = RegionMapping(load_default=True) assert isinstance(dt, RegionMapping) assert dt.shape == (16384, )
def __init__(self,Ps): """ Initialize simulation ---------------------- """ sim_length = Ps['sim_params']['length'] outdir = Ps['sim_params']['outdir'] if not os.path.isdir(outdir): os.mkdir(outdir) print '\nConfiguring sim...' sim = simulator.Simulator() _classes = [models, connectivity, coupling, integrators, monitors ] _names = ['model', 'connectivity', 'coupling', 'integrator', 'monitors'] for _class,_name in zip(_classes,_names): if _name is 'monitors': thisattr = tuple([getattr(_class,m['type'])(**m['params']) for m in Ps['monitors'] ]) else: if 'type' in Ps[_name]: thisattr = getattr(_class,Ps[_name]['type'])(**Ps[_name]['params']) setattr(sim,_name,thisattr) # Additionals - parameters that are functions of other classes # (example = larter_breakdspear demo) if 'additionals' in Ps: for a in Ps['additionals']: setattr(eval(a[0]), a[1],eval(a[2])) #sim,eval(a[0]),eval(a[1])) # Stochastic integrator if 'HeunStochastic' in Ps['integrator']: from tvb.simulator.lab import noise hiss = noise.Additive(nsig=np.array(Ps['integrator']['stochastic_nsig'])) # nsigm 0.015 sim.integrator.noise = hiss # Non-default connectivity # (to add here: # - load from other data structures, e.g. .cff file # - load weights, lengths, etc. directly from data matrices etc if 'connectivity' in Ps: if 'folder_path' in Ps['connectivity']: # (this is from the deterministic_stimulus demo) sim.connectivity.default.reload(sim.connectivity, Ps['connectivity']['folder_path']) sim.connectivity.configure() # EEG projections # (need to do this separately because don't seem to be able to do EEG(projection_matrix='<file>') for m_it, m in enumerate(Ps['monitors']): # (yes I know enumerate isn't necessary here; but it's more transparent imho) # assumption here is that the sim object doesn't re-order the list of monitors for any bizarre reason... # (which would almost certainly cause an error anyway...) #if m['type'] is 'EEG' and 'proj_mat_path' in m: # proj_mat = loadmat(m['proj_mat_path'])['ProjectionMatrix'] # pr = projections.ProjectionRegionEEG(projection_data=proj_mat) # sim.monitors[m_it].projection_matrix_data=pr if m['type'] is 'EEG': if m['proj_surf'] is 'default': pr = ProjectionSurfaceEEG(load_default=True) else: pr = ProjectionSurfaceEEG.from_file(m['proj_surf']) eeg_sens = SensorsEEG.from_file(source_file=m['source_file']) if m['reg_map'] is 'default': rm = RegionMapping(load_default=True) else: rm = RegionMapping.from_file(m['reg_map']) sim.monitors[m_it].projection = pr sim.monitors[m_it].sensors = eeg_sens sim.monitors[m_it].region_mapping = rm # Surface if 'surface' in Ps: surf = getattr(surfaces,Ps['surface']['surface_type']).default() if 'local_connectivity_params' in Ps['surface']: localsurfconn = getattr(surfaces,'LocalConnectivity')(**Ps['surface']['local_connectivity_params']) for ep in Ps['surface']['local_connectivity_equation_params'].items(): localsurfconn.equation.parameters[ep[0]] = ep[1] surf.local_connectivity = localsurfconn localcoupling = np.array( Ps['surface']['local_coupling_strength'] ) surf.coupling_strength = localcoupling sim.surface = surf # Stimulus if 'stimulus' in Ps: stim = getattr(patterns,Ps['stimulus']['type'])() if 'equation' in Ps['stimulus']: # looks like need to do this to keep the other params as default; slightly different to above stim_eqn_params = Ps['stimulus']['equation']['params'] # use this if need to evaluate text # (stim_eqn_params = {p[0]: eval(p[1]) for p in Ps['stimulus']['equation']['params'].items() } ( stim_eqn_t = getattr(equations,Ps['stimulus']['equation']['type'])() stim_eqn_t.parameters.update(**stim_eqn_params) stim.temporal = stim_eqn_t elif 'equation' not in Ps['stimulus']: # (still need to do this...) print 'something to do here' sim.connectivity.configure() stim_weighting = np.zeros((sim.connectivity.number_of_regions,)) stim_weighting[Ps['stimulus']['nodes']] = np.array(Ps['stimulus']['node_weightings']) stim.connectivity = sim.connectivity stim.weight = stim_weighting sim.stimulus = stim # Configure sim sim.configure() # Configure smooth parameter variation (if used) spv = {} if 'smooth_pvar' in Ps: par_length = eval(Ps['smooth_pvar']['par_length_str']) spv['mon_type'] = Ps['smooth_pvar']['monitor_type'] spv['mon_num'] = [m_it for m_it, m in enumerate(Ps['monitors']) if m == spv['mon_type'] ] # (yes, a bit clumsy..) # a) as an equally spaced range if 'equation' not in Ps['smooth_pvar']: spv['a'] = eval(Ps['smooth_pvar']['spv_a_str']) # b) using an Equation datadtype else: spv['params'] = {} for p in Ps['smooth_pvar']['equation']['params'].items(): spv['params'][p[0]] = eval(p[1]) #sim_length = Ps['sim_params']['length'] # temporary fix] #spv_a_params = {p[0]: eval(p[1]) for p in Ps['smooth_pvar']['equation']['params'].items() } spv['eqn_t'] = getattr(equations,Ps['smooth_pvar']['equation']['type'])() spv['eqn_t'].parameters.update(**spv['params']) spv['pattern'] = eval(Ps['smooth_pvar']['equation']['pattern_str']) spv['a'] = spv['pattern'] # omit above line? At moment this follows tutorial code # recent additions.... self.sim = sim self.Ps = Ps self.sim_length = sim_length self.spv = spv
triangle_normals=numpy.zeros((3, 3)), number_of_vertices=5, number_of_triangles=3, edge_mean_length=1.0, edge_min_length=0.0, edge_max_length=2.0, zero_based_triangles=False, split_triangles=numpy.arange(0), number_of_split_slices=1, split_slices=dict(), bi_hemispheric=False, surface_type="surface", valid_for_simulations=True) region_mapping = RegionMapping(array_data=numpy.arange(5), connectivity=connectivity, surface=surface) cortical_surface = CorticalSurface( vertices=numpy.zeros((5, 3)), triangles=numpy.zeros((3, 3), dtype=int), vertex_normals=numpy.zeros((5, 3)), triangle_normals=numpy.zeros((3, 3)), number_of_vertices=5, number_of_triangles=3, edge_mean_length=1.0, edge_min_length=0.0, edge_max_length=2.0, zero_based_triangles=False, split_triangles=numpy.arange(0), number_of_split_slices=1,
def configure(self, dt=2**-3, model=ModelsEnum.GENERIC_2D_OSCILLATOR.get_class(), speed=4.0, coupling_strength=0.00042, method=HeunDeterministic, surface_sim=False, default_connectivity=True, with_stimulus=False): """ Create an instance of the Simulator class, by default use the generic plane oscillator local dynamic model and the deterministic version of Heun's method for the numerical integration. """ self.method = method if default_connectivity: white_matter = Connectivity.from_file() region_mapping = RegionMapping.from_file( source_file="regionMapping_16k_76.txt") else: white_matter = Connectivity.from_file( source_file="connectivity_192.zip") region_mapping = RegionMapping.from_file( source_file="regionMapping_16k_192.txt") region_mapping.surface = CorticalSurface.from_file() white_matter_coupling = coupling.Linear( a=numpy.array([coupling_strength])) white_matter.speed = numpy.array( [speed]) # no longer allow scalars to numpy array promotion dynamics = model() if issubclass(method, IntegratorStochastic): hisss = noise.Additive(nsig=numpy.array([2**-11])) integrator = method(dt=dt, noise=hisss) else: integrator = method(dt=dt) if surface_sim: local_coupling_strength = numpy.array([2**-10]) default_cortex = Cortex.from_file() default_cortex.region_mapping_data = region_mapping default_cortex.coupling_strength = local_coupling_strength if default_connectivity: default_cortex.local_connectivity = LocalConnectivity.from_file( ) else: default_cortex.local_connectivity = LocalConnectivity() default_cortex.local_connectivity.surface = default_cortex.region_mapping_data.surface # TODO stimulus else: default_cortex = None if with_stimulus: weights = StimuliRegion.get_default_weights( white_matter.weights.shape[0]) weights[self.stim_nodes] = 1. stimulus = StimuliRegion(temporal=Linear(parameters={ "a": 0.0, "b": self.stim_value }), connectivity=white_matter, weight=weights) # Order of monitors determines order of returned values. self.sim = simulator.Simulator() self.sim.surface = default_cortex self.sim.model = dynamics self.sim.integrator = integrator self.sim.connectivity = white_matter self.sim.coupling = white_matter_coupling self.sim.monitors = self.monitors if with_stimulus: self.sim.stimulus = stimulus self.sim.configure()
class Projection(Monitor): "Base class monitor providing lead field suppport." _ui_name = "Projection matrix" region_mapping = RegionMapping( required=False, label="region mapping", order=3, doc="A region mapping specifies how vertices of a surface correspond to given regions in the" " connectivity. For iEEG/EEG/MEG monitors, this must be specified when performing a region" " simulation but is optional for a surface simulation.") @staticmethod def oriented_gain(gain, orient): "Apply orientations to gain matrix." return (gain.reshape((gain.shape[0], -1, 3)) * orient).sum(axis=-1) @classmethod def _projection_class(cls): if hasattr(cls, 'projection'): return type(cls.projection) else: return ProjectionMatrix @classmethod def from_file(cls, sensors_fname, projection_fname, rm_f_name="regionMapping_16k_76.txt", period=1e3/1024.0, instance=None, **kwds): """ Build Projection-based monitor from sensors and projection files, and any extra keyword arguments are passed to the monitor class constructor. """ if instance is None: result = cls(**kwds) else: result = instance result.sensors = type(cls.sensors).from_file(sensors_fname) result.projection = cls._projection_class().from_file(projection_fname) result.region_mapping = RegionMapping.from_file(rm_f_name) return result def analytic(self, loc, ori): "Construct analytic or default set of spatial filters for simulation." # this will not be implemented but kept for API uniformity raise NotImplementedError( "No general purpose analytic formula available for spatial " "projection matrices. Please select an appropriate projection " "matrix." ) def config_for_sim(self, simulator): "Configure projection matrix monitor for given simulation." super(Projection, self).config_for_sim(simulator) self._sim = simulator if hasattr(self, 'sensors'): self.sensors.configure() # handle region vs simulation, analytic vs numerical proj, cortical vs subcortical. # setup convenient locals surf = simulator.surface conn = simulator.connectivity using_cortical_surface = surf is not None if using_cortical_surface: non_cortical_indices, = numpy.where(numpy.bincount(surf.region_mapping) == 1) self.rmap = surf.region_mapping else: # assume all cortical if no info if conn.cortical.size == 0: conn.cortical = numpy.array([True] * conn.weights.shape[0]) non_cortical_indices, = numpy.where(~conn.cortical) if self.region_mapping is None: raise Exception("Please specify a region mapping on the EEG/MEG/iEEG monitor when " "performing a region simulation.") else: self.rmap = self.region_mapping LOG.debug('Projection used in region sim has %d non-cortical regions', non_cortical_indices.size) have_subcortical = len(non_cortical_indices) > 0 # determine source space if using_cortical_surface: sources = {'loc': surf.vertices, 'ori': surf.vertex_normals} else: sources = {'loc': conn.centres[conn.cortical], 'ori': conn.orientations[conn.cortical]} # compute analytic if not provided if self.projection is None: LOG.debug('Precomputed projection not unavailable using analytic approximation.') self.gain = self.analytic(**sources) # reduce to region lead field if region sim if not using_cortical_surface and self.gain.shape[1] == self.rmap.size: gain = numpy.zeros((self.gain.shape[0], conn.number_of_regions)) numpy_add_at(gain.T, self.rmap, self.gain.T) LOG.debug('Region mapping gain shape %s to %s', self.gain.shape, gain.shape) self.gain = gain # append analytic sub-cortical to lead field if have_subcortical: # need matrix of shape (proj.shape[0], len(sc_ind)) src = conn.centres[non_cortical_indices], conn.orientations[non_cortical_indices] self.gain = numpy.hstack((self.gain, self.analytic(*src))) LOG.debug('Added subcortical analytic gain, for final shape %s', self.gain.shape) if self.sensors.usable is not None and not self.sensors.usable.all(): mask_unusable = ~self.sensors.usable self.gain[mask_unusable] = 0.0 LOG.debug('Zeroed gain coefficients for %d unusable sensors', mask_unusable.sum()) # unconditionally zero NaN elements; framework not prepared for NaNs. nan_mask = numpy.isfinite(self.gain).all(axis=1) self.gain[~nan_mask] = 0.0 LOG.debug('Zeroed %d NaN gain coefficients', nan_mask.sum()) # attrs used for recording self._state = numpy.zeros((self.gain.shape[0], len(self.voi))) self._period_in_steps = int(self.period / self.dt) LOG.debug('State shape %s, period in steps %s', self._state.shape, self._period_in_steps) LOG.info('Projection configured gain shape %s', self.gain.shape) def sample(self, step, state): "Record state, returning sample at sampling frequency / period." self._state += self.gain.dot(state[self.voi].sum(axis=-1).T) if step % self._period_in_steps == 0: time = (step - self._period_in_steps / 2.0) * self.dt sample = self._state.copy() / self._period_in_steps self._state[:] = 0.0 return time, sample.T[..., numpy.newaxis] # for compatibility _gain = None def _get_gain(self): if self._gain is None: self._gain = self.projection.projection_data return self._gain def _set_gain(self, new_gain): self._gain = new_gain gain = property(_get_gain, _set_gain) _rmap = None def _reg_map_data(self, reg_map): return getattr(reg_map, 'array_data', reg_map) def _get_rmap(self): "Normalize obtaining reg map vector over various possibilities." if self._rmap is None: self._rmap = self._reg_map_data(self.region_mapping) return self._rmap def _set_rmap(self, reg_map): if self._rmap is not None: self._rmap = self._reg_map_data(self.region_mapping) rmap = property(_get_rmap, _set_rmap)