Example #1
0
    def test_happy_flow_surface_import(self):
        """
        Verifies the happy flow for importing a surface.
        """
        dt_count_before = TestFactory.get_entity_count(self.test_project,
                                                       ProjectionSurfaceEEG())
        group = dao.find_group(
            'tvb.adapters.uploaders.projection_matrix_importer',
            'ProjectionMatrixSurfaceEEGImporter')
        importer = ABCAdapter.build_adapter(group)

        zip_path = os.path.join(
            os.path.abspath(os.path.dirname(dataset.__file__)),
            'surface_reg_13_eeg_62.mat')
        args = {
            'projection_file': zip_path,
            'dataset_name': 'ProjectionMatrix',
            'connectivity': self.connectivity.gid,
            'sensors': self.sensors.gid,
            'surface': self.surface.gid,
            DataTypeMetaData.KEY_SUBJECT: DataTypeMetaData.DEFAULT_SUBJECT
        }

        FlowService().fire_operation(importer, self.test_user,
                                     self.test_project.id, **args)
        dt_count_after = TestFactory.get_entity_count(self.test_project,
                                                      ProjectionSurfaceEEG())

        self.assertEqual(dt_count_before + 1, dt_count_after)
Example #2
0
    def test_prepare_inputs_with_eeg_monitor(self, operation_factory,
                                             simulator_factory,
                                             surface_index_factory,
                                             sensors_index_factory,
                                             region_mapping_index_factory,
                                             connectivity_index_factory):
        surface_idx, surface = surface_index_factory(cortical=True)
        sensors_idx, sensors = sensors_index_factory()
        proj = ProjectionSurfaceEEG(sensors=sensors,
                                    sources=surface,
                                    projection_data=numpy.ones(3))

        op = operation_factory()
        storage_path = FilesHelper().get_project_folder(op.project, str(op.id))
        prj_db_db = h5.store_complete(proj, storage_path)
        prj_db_db.fk_from_operation = op.id
        dao.store_entity(prj_db_db)

        connectivity = connectivity_index_factory(76, op)
        rm_index = region_mapping_index_factory(conn_gid=connectivity.gid,
                                                surface_gid=surface_idx.gid)

        eeg_monitor = EEGViewModel(projection=proj.gid, sensors=sensors.gid)
        eeg_monitor.region_mapping = rm_index.gid

        sim_folder, sim_gid = simulator_factory(op=op,
                                                monitor=eeg_monitor,
                                                conn_gid=connectivity.gid)
        hpc_client = HPCSchedulerClient()
        input_files = hpc_client._prepare_input(op, sim_gid)
        assert len(input_files) == 11
Example #3
0
 def test_happy_flow_surface_import(self):
     dt_count_before = TestFactory.get_entity_count(self.test_project,
                                                    ProjectionSurfaceEEG())
     group = dao.find_group(
         'tvb.adapters.uploaders.projection_matrix_importer',
         'ProjectionMatrixSurfaceEEGImporter')
     importer = ABCAdapter.build_adapter(group)
     importer.meta_data = {
         DataTypeMetaData.KEY_SUBJECT: DataTypeMetaData.DEFAULT_SUBJECT,
         DataTypeMetaData.KEY_STATE: "RAW"
     }
     zip_path = os.path.join(
         os.path.abspath(os.path.dirname(dataset.__file__)),
         'region_conn_74_eeg_1020_62.mat')
     args = {
         'projection_file': zip_path,
         'dataset_name': 'ProjectionMatrix',
         'connectivity': self.connectivity.gid,
         'sensors': self.sensors.gid,
         'surface': self.surface.gid
     }
     FlowService().fire_operation(importer, self.test_user,
                                  self.test_project.id, **args)
     dt_count_after = TestFactory.get_entity_count(self.test_project,
                                                   ProjectionRegionEEG())
     self.assertTrue(dt_count_after == dt_count_before + 1)
Example #4
0
def test_store_simulator_view_model_eeg(connectivity_index_factory, surface_index_factory, region_mapping_factory,
                                        sensors_index_factory, operation_factory):
    conn = connectivity_index_factory()
    surface_idx, surface = surface_index_factory(cortical=True)
    region_mapping = region_mapping_factory()
    sensors_idx, sensors = sensors_index_factory()
    proj = ProjectionSurfaceEEG(sensors=sensors, sources=surface, projection_data=numpy.ones(3))

    op = operation_factory()
    storage_path = FilesHelper().get_project_folder(op.project, str(op.id))
    prj_db_db = h5.store_complete(proj, storage_path)
    prj_db_db.fk_from_operation = op.id
    dao.store_entity(prj_db_db)

    seeg_monitor = EEGViewModel(projection=proj.gid, sensors=sensors.gid)
    seeg_monitor.region_mapping = region_mapping.gid.hex
    sim_view_model = SimulatorAdapterModel()
    sim_view_model.connectivity = conn.gid
    sim_view_model.monitors = [seeg_monitor]

    op = operation_factory()
    storage_path = FilesHelper().get_project_folder(op.project, str(op.id))

    h5.store_view_model(sim_view_model, storage_path)

    loaded_sim_view_model = h5.load_view_model(sim_view_model.gid, storage_path)

    assert isinstance(sim_view_model, SimulatorAdapterModel)
    assert isinstance(loaded_sim_view_model, SimulatorAdapterModel)
    assert sim_view_model.monitors[0].projection == loaded_sim_view_model.monitors[0].projection
Example #5
0
    def test_happy_flow_surface_import(self):
        """
        Verifies the happy flow for importing a surface.
        """
        dt_count_before = TestFactory.get_entity_count(self.test_project, ProjectionSurfaceEEG())
        file_path = os.path.join(os.path.abspath(os.path.dirname(dataset.__file__)),
                                 'projection_eeg_65_surface_16k.npy')
        args = {'projection_file': file_path,
                'dataset_name': 'ProjectionMatrix',
                'sensors': self.sensors.gid,
                'surface': self.surface.gid,
                DataTypeMetaData.KEY_SUBJECT: DataTypeMetaData.DEFAULT_SUBJECT}

        FlowService().fire_operation(self.importer, self.test_user, self.test_project.id, **args)
        dt_count_after = TestFactory.get_entity_count(self.test_project, ProjectionSurfaceEEG())

        self.assertEqual(dt_count_before + 1, dt_count_after)
def build_projection_instance(sensors, storage_path):
    if isinstance(sensors, SensorsEEG):
        projection_matrix = ProjectionSurfaceEEG(storage_path=storage_path)
    elif isinstance(sensors, SensorsMEG):
        projection_matrix = ProjectionSurfaceMEG(storage_path=storage_path)
    else:
        projection_matrix = ProjectionSurfaceSEEG(storage_path=storage_path)

    return projection_matrix
Example #7
0
 def launch(self, projection_file, surface, sensors, dataset_name =DATA_NAME_PROJECTION):
     """
     Creates ProjectionMatrix entity from uploaded data.
     """
     if surface is None:
         raise LaunchException("No source selected. Please initiate upload again and select a source.")
     projection_matrix = ProjectionSurfaceEEG(storage_path = self.storage_path)
     expected_shape = surface.number_of_vertices
     
     return self.generic_launch(projection_file, dataset_name, surface, sensors, expected_shape, projection_matrix)
Example #8
0
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
Example #9
0
def test_gather_view_model_and_datatype_references_multiple_monitors(
        connectivity_index_factory, operation_factory, sensors_index_factory,
        surface_index_factory, region_mapping_index_factory):
    conn = connectivity_index_factory()
    _, surface = surface_index_factory(cortical=True)
    region_mapping_idx = region_mapping_index_factory(
        conn_gid=conn.gid, surface_gid=surface.gid.hex)
    sensors_idx, sensors = sensors_index_factory()
    proj = ProjectionSurfaceEEG(sensors=sensors,
                                sources=surface,
                                projection_data=numpy.ones(3))

    op = operation_factory()
    storage_path = StorageInterface().get_project_folder(
        op.project.name, str(op.id))
    prj_db = h5.store_complete(proj, storage_path)
    prj_db.fk_from_operation = op.id
    dao.store_entity(prj_db)

    seeg_monitor = EEGViewModel(projection=proj.gid, sensors=sensors.gid)
    seeg_monitor.region_mapping = region_mapping_idx.gid

    sim_view_model = SimulatorAdapterModel()
    sim_view_model.connectivity = conn.gid
    sim_view_model.monitors = [TemporalAverageViewModel(), seeg_monitor]

    op = operation_factory()
    storage_path = StorageInterface().get_project_folder(
        op.project.name, str(op.id))
    h5.store_view_model(sim_view_model, storage_path)

    only_vm_references, _ = h5.gather_references_of_view_model(
        sim_view_model.gid, storage_path, True)
    assert len(only_vm_references) == 7

    vm_references, dt_references = h5.gather_references_of_view_model(
        sim_view_model.gid, storage_path)
    assert len(vm_references + dt_references) == 12
Example #10
0
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

    # model: Generic 2D Oscillator - neural mass has two state variables that
    # represent a neuron's membrane potential and recovery; see the
Example #11
0
  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
Example #12
0
class EEG(Projection):
    """
    Forward solution monitor for electroencephalogy (EEG). If a
    precomputed lead field is not available, a single sphere analytic
    formula due to Sarvas 1987 is used.

    **References**:

    .. [Sarvas_1987] Sarvas, J., *Basic mathematical and electromagnetic
        concepts of the biomagnetic inverse problem*, Physics in Medicine and
        Biology, 1987.

    """
    _ui_name = "EEG"

    projection = ProjectionSurfaceEEG(
        default=None, label='Projection matrix', order=2,
        doc='Projection matrix to apply to sources.')

    reference = basic.String(required=False, label="EEG Reference", order=5,
                             doc='EEG Electrode to be used as reference, or "average" to '
                                 'apply an average reference. If none is provided, the '
                                 'produced time-series are the idealized or reference-free.')

    sensors = SensorsEEG(required=True, label="EEG Sensors", order=1,
                         doc='Sensors to use for this EEG monitor')

    sigma = basic.Float(label="Conductivity (w/o projection)", default=1.0, order=4,
                        doc='When a projection matrix is not used, this provides '
                            'the value of conductivity in the formula for the single '
                            'sphere approximation of the head (Sarvas 1987).')


    @classmethod
    def from_file(cls, sensors_fname='eeg_brainstorm_65.txt', projection_fname='projection_eeg_65_surface_16k.npy', **kwargs):
        return Projection.from_file.im_func(cls, sensors_fname, projection_fname, **kwargs)

    def config_for_sim(self, simulator):
        super(EEG, self).config_for_sim(simulator)
        self._ref_vec = numpy.zeros((self.sensors.number_of_sensors, ))
        if self.reference:
            if self.reference.lower() != 'average':
                sensor_names = self.sensors.labels.tolist()
                self._ref_vec[sensor_names.index(self.reference)] = 1.0
            else:
                self._ref_vec[:] = 1.0 / self.sensors.number_of_sensors
        self._ref_vec_mask = numpy.isfinite(self.gain).all(axis=1)
        self._ref_vec = self._ref_vec[self._ref_vec_mask]

    def analytic(self, loc, ori):
        "Equation 12 of [Sarvas_1987]_"
        # r => sensor positions
        # r_0 => source positions
        # a => vector from sources_to_sensor
        # Q => source unit vectors
        r_0, Q = loc, ori
        center = numpy.mean(r_0, axis=0)[numpy.newaxis, ]
        radius = 1.05125 * max(numpy.sqrt(numpy.sum((r_0 - center)**2, axis=1)))
        loc = self.sensors.locations.copy()
        sen_dis = numpy.sqrt(numpy.sum((loc)**2, axis=1))
        loc = loc / sen_dis[:, numpy.newaxis] * radius + center
        V_r = numpy.zeros((loc.shape[0], r_0.shape[0]))
        for sensor_k in numpy.arange(loc.shape[0]):
            a = loc[sensor_k, :] - r_0
            na = numpy.sqrt(numpy.sum(a**2, axis=1))[:, numpy.newaxis]
            V_r[sensor_k, :] = numpy.sum(Q * (a / na**3), axis=1 ) / (4.0 * numpy.pi * self.sigma)
        return V_r

    def sample(self, step, state):
        maybe_sample = super(EEG, self).sample(step, state)
        if maybe_sample is not None:
            time, sample = maybe_sample
            sample -= self._ref_vec.dot(sample[:, self._ref_vec_mask])[:, numpy.newaxis]
            return time, sample.reshape((state.shape[0], -1, 1))

    def create_time_series(self, storage_path, connectivity=None, surface=None,
                           region_map=None, region_volume_map=None):
        return TimeSeriesEEG(storage_path=storage_path,
                             sensors=self.sensors,
                             sample_period=self.period,
                             title=' ' + self.__class__.__name__,
                             **self._transform_user_tags())
Example #13
0
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"

sensorsEEG = SensorsEEG(load_file=sensoreeg_fname)
prEEG = ProjectionSurfaceEEG(load_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

    # model: Generic 2D Oscillator - neural mass has two state variables that
    # represent a neuron's membrane potential and recovery; see the