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 _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 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 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 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_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 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(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 _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 __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 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 _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, 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(period=period, **kwds) else: result = instance result.sensors = type(cls.sensors)(load_file=sensors_fname) result.projection = ProjectionMatrix(load_file=projection_fname) result.region_mapping = RegionMapping(load_file=rm_f_name) return result
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 test_regionmapping(self): dt = RegionMapping(load_file="regionMapping_16k_76.txt") assert isinstance(dt, RegionMapping) assert dt.mapping.shape == (16384, )
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 test_regionmapping(self): dt = RegionMapping(load_default=True) assert isinstance(dt, RegionMapping) assert dt.shape == (16384, )
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)
def test_regionmapping(self): dt = RegionMapping(load_default=True) self.assertTrue(isinstance(dt, RegionMapping)) self.assertEqual(dt.shape, (16384, ))
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)
# zipped directory that contains connectivity/tractography info conn_fname = "/home/annajo/git/tvb/tvb-data/tvb_data/berlinSubjects/DH_20120806/connectivity/DH_20120806_Connectivity/DHconn.zip" conn = Connectivity(load_file=conn_fname) conn.configure() print("connectivity loaded") # print(conn.summary_info) plot_connectivity(connectivity=conn) # pyplot.show() # triangulated cortical surface mesh ctx_fname = "/home/annajo/git/tvb/tvb-data/tvb_data/berlinSubjects/DH_20120806/cortex/DH_20120806_Surface_Cortex.zip" # maps nodes from cortex mesh to brain regions region_fname = "/home/annajo/git/tvb/tvb-data/tvb_data/berlinSubjects/DH_20120806/DH_20120806_RegionMapping.txt" rm = RegionMapping(load_file=region_fname) # matlab matrix that describes how waves propagate to the surface eeg_fname = "/home/annajo/git/tvb/tvb-data/tvb_data/berlinSubjects/DH_20120806/DH_20120806_ProjectionMatrix.mat" ctx = Cortex(load_file=ctx_fname, region_mapping_data=rm) 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 = "/home/annajo/git/tvb/tvb-data/tvb_data/berlinSubjects/DH_20120806/DH_20120806_EEGLocations.txt"