Ejemplo n.º 1
0
    def __init__(self):
        """
        Initialise the structural information, coupling function, and monitors.

        """

        # Initialise some Monitors with period in physical time
        raw = monitors.Raw()
        gavg = monitors.GlobalAverage(period=2 ** -2)
        subsamp = monitors.SubSample(period=2 ** -2)
        tavg = monitors.TemporalAverage(period=2 ** -2)
        # DON'T load a projection because it'll make this behave like it
        # has a surface and do very, very bad things
        eeg = monitors.EEG(sensors=SensorsEEG(load_file="eeg_brainstorm_65.txt"),
                           period=2 ** -2)
        eeg2 = monitors.EEG(sensors=SensorsEEG(load_file="eeg_brainstorm_65.txt"),
                            period=2 ** -2,
                            reference='Fp2')  # EEG with a reference electrode
        meg = monitors.MEG(sensors=SensorsMEG(load_file='meg_brainstorm_276.txt'),
                           period=2 ** -2)

        self.monitors = (raw, gavg, subsamp, tavg, eeg, eeg2, meg)

        self.method = None
        self.sim = None
    def setUp(self):
        """
        Reset the database before each test.
        """
        self.test_user = TestFactory.create_user("UserPM")
        self.test_project = TestFactory.create_project(self.test_user)

        zip_path = os.path.join(os.path.dirname(tvb_data.sensors.__file__),
                                'eeg_brainstorm_65.txt')
        TestFactory.import_sensors(self.test_user, self.test_project, zip_path,
                                   Sensors_Importer.EEG_SENSORS)

        zip_path = os.path.join(os.path.dirname(tvb_data.surfaceData.__file__),
                                'cortex_16384.zip')
        TestFactory.import_surface_zip(self.test_user, self.test_project,
                                       zip_path, CORTICAL, True)

        self.surface = TestFactory.get_entity(self.test_project,
                                              CorticalSurface())
        self.assertTrue(self.surface is not None)
        self.sensors = TestFactory.get_entity(self.test_project, SensorsEEG())
        self.assertTrue(self.sensors is not None)

        self.importer = TestFactory.create_adapter(
            'tvb.adapters.uploaders.projection_matrix_importer',
            'ProjectionMatrixSurfaceEEGImporter')
Ejemplo n.º 3
0
    def test_launch_eeg(self):
        """
        Check that all required keys are present in output from EegSensorViewer launch.
        """
        ## Import Sensors
        zip_path = os.path.join(os.path.dirname(tvb_data.sensors.__file__),
                                'eeg_unitvector_62.txt.bz2')
        TestFactory.import_sensors(self.test_user, self.test_project, zip_path,
                                   Sensors_Importer.EEG_SENSORS)
        sensors = TestFactory.get_entity(self.test_project, SensorsEEG())

        ## Import EEGCap
        cap_path = os.path.join(os.path.dirname(tvb_data.obj.__file__),
                                'eeg_cap.obj')
        TestFactory.import_surface_obj(self.test_user, self.test_project,
                                       cap_path, EEG_CAP)
        eeg_cap_surface = TestFactory.get_entity(self.test_project, EEGCap())

        viewer = SensorsViewer()
        viewer.current_project_id = self.test_project.id

        ## Launch with EEG Cap selected
        result = viewer.launch(sensors, eeg_cap_surface)
        self.assert_compliant_dictionary(self.EXPECTED_KEYS_EEG, result)
        for key in ['urlVertices', 'urlTriangles', 'urlLines', 'urlNormals']:
            assert result[
                key] is not None, "Value at key %s should not be None" % key

        ## Launch without EEG Cap
        result = viewer.launch(sensors)
        self.assert_compliant_dictionary(self.EXPECTED_KEYS_EEG, result)
        for key in ['urlVertices', 'urlTriangles', 'urlLines', 'urlNormals']:
            assert not result[key] or result[key] == "[]", "Value at key %s should be None or empty, " \
                                                           "but is %s" % (key, result[key])
Ejemplo n.º 4
0
 def test_projection_surface_eeg(self):
     dt = projections.ProjectionSurfaceEEG(sensors=SensorsEEG(),projection_data=numpy.array([]), sources=CorticalSurface())
     assert dt.sources is not None
     assert dt.skin_air is None
     assert dt.skull_skin is None
     assert dt.sensors is not None
     assert dt.projection_data is not None
Ejemplo n.º 5
0
    def launch(self, matfile, fdtfile):

        self.mat = loadmat(matfile)
        self.fs = self.mat['EEG']['srate'][0, 0][0, 0]
        self.nsamp = self.mat['EEG']['pnts'][0, 0][0, 0]
        self.data = numpy.fromfile(fdtfile, dtype=numpy.float32)
        self.data = self.data.reshape((self.nsamp, -1)).T
        self.nchan = self.data.shape[0]
        self.labels = [
            c[0] for c in self.mat['EEG']['chanlocs'][0, 0]['labels'][0]
        ]

        ch = SensorsEEG(storage_path=self.storage_path,
                        labels=self.labels,
                        number_of_sensors=len(self.labels))
        self._capture_operation_results([ch])

        ts = TimeSeriesEEG(sensors=ch, storage_path=self.storage_path)

        # (nchan x ntime) -> (t, sv, ch, mo)
        dat = self.data.T[:, numpy.newaxis, :, numpy.newaxis]

        # write data
        ts.write_data_slice(dat)

        # fill in header info
        ts.length_1d, ts.length_2d, ts.length_3d, ts.length_4d = dat.shape
        ts.labels_ordering = 'Time 1 Channel 1'.split()
        ts.write_time_slice(numpy.r_[:dat.shape[0]] * 1.0 / self.fs)
        ts.start_time = 0.0
        ts.sample_period_unit = 's'
        ts.sample_period = 1.0 / float(self.fs)
        ts.close_file()

        return ts
Ejemplo n.º 6
0
    def test_launch_eeg(self):
        """
        Tests successful launch of a BrainEEG and that all required keys are present in returned template dictionary
        """
        zip_path = os.path.join(os.path.dirname(sensors_dataset.__file__),
                                'EEG_unit_vectors_BrainProducts_62.txt.bz2')

        TestFactory.import_sensors(self.test_user, self.test_project, zip_path,
                                   'EEG Sensors')
        sensors = TestFactory.get_entity(self.test_project, SensorsEEG())
        time_series = self.datatypeFactory.create_timeseries(
            self.connectivity, 'EEG', sensors)
        time_series.configure()
        viewer = BrainEEG()
        result = viewer.launch(time_series)
        expected_keys = [
            'urlVertices', 'urlNormals', 'urlTriangles',
            'urlMeasurePointsLabels', 'title', 'time_series', 'shelfObject',
            'pageSize', 'labelsStateVar', 'nrOfPages', 'labelsModes',
            'minActivityLabels', 'minActivity', 'measure_points',
            'maxActivity', 'isOneToOneMapping', 'isAdapter', 'extended_view',
            'base_activity_url', 'alphas_indices'
        ]
        for key in expected_keys:
            self.assertTrue(key in result and result[key] is not None)
        self.assertTrue(result['extended_view'])
Ejemplo n.º 7
0
    def launch(self, vhdr, dat):

        self.filename = vhdr
        self.wd, _ = os.path.split(vhdr)

        # read file
        with open(vhdr, 'r') as fd:
            self.srclines = fd.readlines()

        # config parser expects each section to have header
        # but vhdr has some decorative information at the beginning
        while not self.srclines[0].startswith('['):
            self.srclines.pop(0)

        self.sio = StringIO()
        self.sio.write('\n'.join(self.srclines))
        self.sio.seek(0)

        self.cp = ConfigParser.ConfigParser()
        self.cp.readfp(self.sio)

        for opt in self.cp.options('Common Infos'):
            setattr(self, opt, self.cp.get('Common Infos', opt))

        self.binaryformat = self.cp.get('Binary Infos', 'BinaryFormat')

        self.labels = [
            self.cp.get('Channel Infos', o).split(',')[0]
            for o in self.cp.options('Channel Infos')
        ]

        self.fs = self.srate = 1e6 / float(self.samplinginterval)
        self.nchan = int(self.numberofchannels)

        # important if not in same directory
        self.datafile = os.path.join(self.wd, self.datafile)

        self.read_data()

        # create TVB datatypes
        ch = SensorsEEG(storage_path=self.storage_path,
                        labels=self.labels,
                        number_of_sensors=len(self.labels))
        uid = vhdr + '-sensors'
        self._capture_operation_results([ch], uid=uid)

        ts = TimeSeriesEEG(sensors=ch, storage_path=self.storage_path)
        dat = self.data.T[:, numpy.newaxis, :, numpy.newaxis]
        ts.write_data_slice(dat)
        ts.length_1d, ts.length_2d, ts.length_3d, ts.length_4d = dat.shape
        ts.labels_ordering = 'Time 1 Channel 1'.split()
        ts.write_time_slice(numpy.r_[:dat.shape[0]] * 1.0 / self.fs)
        ts.start_time = 0.0
        ts.sample_period_unit = 's'
        ts.sample_period = 1.0 / float(self.fs)
        ts.close_file()

        return ts
Ejemplo n.º 8
0
    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
Ejemplo n.º 9
0
 def test_timeserieseeg(self):
     data = numpy.random.random((10, 10))
     dt = time_series.TimeSeriesEEG(data=data, sensors=SensorsEEG())
     assert dt.data.shape == (10, 10)
     assert ('Time', '1', 'EEG Sensor', '1') == dt.labels_ordering
     assert dt.sample_period == 1.0
     assert dt.sample_rate == 1.0
     assert dt.sensors is not None
     assert dt.start_time == 0.0
     assert dt.time is None
Ejemplo n.º 10
0
    def setUp(self):
        """
        Reset the database before each test.
        """
        self.test_user = TestFactory.create_user("UserPM")
        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.sensors = TestFactory.get_entity(self.test_project, SensorsEEG())
        self.assertTrue(self.sensors is not None)
    def test_import_eeg_sensors(self):
        """
        This method tests import of a file containing EEG sensors.
        """
        eeg_sensors = self._import(self.EEG_FILE, self.importer.EEG_SENSORS, SensorsEEG())

        expected_size = 62
        self.assertTrue(eeg_sensors.labels is not None)
        self.assertEqual(expected_size, len(eeg_sensors.labels))
        self.assertEqual(expected_size, len(eeg_sensors.locations))
        self.assertEqual((expected_size, 3), eeg_sensors.locations.shape)
        self.assertEqual(expected_size, eeg_sensors.number_of_sensors)
Ejemplo n.º 12
0
    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]
Ejemplo n.º 13
0
    def test_import_eeg_sensors(self):
        """
        This method tests import of a file containing EEG sensors.
        """
        eeg_sensors = self._import(self.EEG_FILE, self.importer.EEG_SENSORS,
                                   SensorsEEG())

        expected_size = 62
        assert eeg_sensors.labels is not None
        assert expected_size == len(eeg_sensors.labels)
        assert expected_size == len(eeg_sensors.locations)
        assert (expected_size, 3) == eeg_sensors.locations.shape
        assert expected_size == eeg_sensors.number_of_sensors
Ejemplo n.º 14
0
 def test_launch_eeg(self):
     """
     Tests successful launch of a BrainEEG and that all required keys are present in returned template dictionary
     """
     sensors = TestFactory.get_entity(self.test_project, SensorsEEG())
     time_series = self.datatypeFactory.create_timeseries(
         self.connectivity, 'EEG', sensors)
     time_series.configure()
     viewer = DualBrainViewer()
     viewer.current_project_id = self.test_project.id
     result = viewer.launch(time_series)
     for key in TestBrainViewer.EXPECTED_KEYS + TestBrainViewer.EXPECTED_EXTRA_KEYS:
         assert key in result and result[key] is not None
     assert result['extended_view']
Ejemplo n.º 15
0
    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]
Ejemplo n.º 16
0
 def build(type="EEG", nr_sensors=3):
     if type == "EEG":
         return SensorsEEG(sensors_type=type,
                           labels=numpy.array(["s"] * nr_sensors),
                           locations=numpy.ones((nr_sensors, 3)),
                           number_of_sensors=nr_sensors,
                           has_orientation=True,
                           orientations=numpy.zeros((nr_sensors, 3)),
                           usable=numpy.array([True] * nr_sensors))
     return Sensors(sensors_type=type,
                    labels=numpy.array(["s"] * nr_sensors),
                    locations=numpy.ones((nr_sensors, 3)),
                    number_of_sensors=nr_sensors,
                    has_orientation=True,
                    orientations=numpy.zeros((nr_sensors, 3)),
                    usable=numpy.array([True] * nr_sensors))
Ejemplo n.º 17
0
    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)
Ejemplo n.º 18
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(load_default=True)
    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
Ejemplo n.º 19
0
 def setUp(self):
     """
     Reset the database before each test.
     """
     self.test_user = TestFactory.create_user('CFF_User')
     self.test_project = TestFactory.create_project(self.test_user,
                                                    "CFF_Project")
     TestFactory.import_cff(test_user=self.test_user,
                            test_project=self.test_project)
     zip_path = os.path.join(os.path.dirname(sensors_dataset.__file__),
                             'EEG_unit_vectors_BrainProducts_62.txt.bz2')
     TestFactory.import_sensors(self.test_user, self.test_project, zip_path,
                                'EEG Sensors')
     self.connectivity = TestFactory.get_entity(self.test_project,
                                                Connectivity())
     self.assertTrue(self.connectivity is not None)
     self.surface = TestFactory.get_entity(self.test_project,
                                           CorticalSurface())
     self.assertTrue(self.surface is not None)
     self.sensors = TestFactory.get_entity(self.test_project, SensorsEEG())
     self.assertTrue(self.sensors is not None)
Ejemplo n.º 20
0
 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()
Ejemplo n.º 21
0
    def test_launch(self):
        """
        Check that all required keys are present in output from BrainViewer launch.
        """
        zip_path = os.path.join(os.path.dirname(sensors_dataset.__file__),
                                'EEG_unit_vectors_BrainProducts_62.txt.bz2')

        TestFactory.import_sensors(self.test_user, self.test_project, zip_path,
                                   'EEG Sensors')
        sensors = TestFactory.get_entity(self.test_project, SensorsEEG())
        time_series = self.datatypeFactory.create_timeseries(
            self.connectivity, 'EEG', sensors)
        viewer = EegMonitor()
        result = viewer.launch(time_series)
        expected_keys = [
            'tsStateVars', 'tsModes', 'translationStep', 'total_length',
            'title', 'timeSetPaths', 'number_of_visible_points',
            'normalizedSteps', 'noOfChannels', 'labelsForCheckBoxes',
            'label_x', 'graphLabels', 'entities', 'channelsPage'
        ]
        for key in expected_keys:
            self.assertTrue(key in result)
Ejemplo n.º 22
0
    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]
Ejemplo n.º 23
0
    def test_launch(self):
        """
        Check that all required keys are present in output from BrainViewer launch.
        """
        zip_path = os.path.join(os.path.dirname(sensors_dataset.__file__),
                                'EEG_unit_vectors_BrainProducts_62.txt.bz2')

        TestFactory.import_sensors(self.test_user, self.test_project, zip_path,
                                   'EEG Sensors')
        sensors = TestFactory.get_entity(self.test_project, SensorsEEG())
        time_series = self.datatypeFactory.create_timeseries(
            self.connectivity, 'EEG', sensors)
        viewer = EegMonitor()
        result = viewer.launch(time_series)
        expected_keys = [
            'tsNames', 'groupedLabels', 'tsModes', 'tsStateVars',
            'longestChannelLength', 'label_x', 'entities', 'page_size',
            'number_of_visible_points', 'extended_view', 'initialSelection',
            'ag_settings', 'ag_settings'
        ]

        for key in expected_keys:
            self.assertTrue(key in result, "key not found %s" % key)

        expected_ag_settings = [
            'channelsPerSet', 'channelLabels', 'noOfChannels',
            'translationStep', 'normalizedSteps', 'nan_value_found',
            'baseURLS', 'pageSize', 'nrOfPages', 'timeSetPaths', 'totalLength',
            'number_of_visible_points', 'extended_view',
            'measurePointsSelectionGIDs'
        ]

        ag_settings = json.loads(result['ag_settings'])

        for key in expected_ag_settings:
            self.assertTrue(key in ag_settings,
                            "ag_settings should have the key %s" % key)
Ejemplo n.º 24
0
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"

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
Ejemplo n.º 25
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())