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 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.º 4
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.º 5
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.º 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 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.º 9
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]
    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.º 11
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)
Ejemplo n.º 12
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.º 13
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.º 14
0
    def deserialize_simulator(simulator_gid, storage_path):
        simulator_in_path = h5.path_for(storage_path, SimulatorH5,
                                        simulator_gid)
        simulator_in = SimulatorAdapterModel()

        with SimulatorH5(simulator_in_path) as simulator_in_h5:
            simulator_in_h5.load_into(simulator_in)
            simulator_in.connectivity = simulator_in_h5.connectivity.load()
            simulator_in.stimulus = simulator_in_h5.stimulus.load()
            simulator_in.history_gid = simulator_in_h5.simulation_state.load()

        if isinstance(simulator_in.monitors[0], Projection):
            # TODO: simplify this part
            with SimulatorH5(simulator_in_path) as simulator_in_h5:
                monitor_h5_path = simulator_in_h5.get_reference_path(
                    simulator_in.monitors[0].gid)

            monitor_h5_class = h5_factory.monitor_h5_factory(
                type(simulator_in.monitors[0]))

            with monitor_h5_class(monitor_h5_path) as monitor_h5:
                sensors_gid = monitor_h5.sensors.load()
                region_mapping_gid = monitor_h5.region_mapping.load()

            sensors = ABCAdapter.load_traited_by_gid(sensors_gid)

            if isinstance(simulator_in.monitors[0], EEG):
                sensors = SensorsEEG.build_sensors_subclass(sensors)
            elif isinstance(simulator_in.monitors[0], MEG):
                sensors = SensorsMEG.build_sensors_subclass(sensors)
            elif isinstance(simulator_in.monitors[0], iEEG):
                sensors = SensorsInternal.build_sensors_subclass(sensors)

            simulator_in.monitors[0].sensors = sensors
            region_mapping = ABCAdapter.load_traited_by_gid(region_mapping_gid)
            simulator_in.monitors[0].region_mapping = region_mapping

        if simulator_in.surface:
            cortex_path = h5.path_for(storage_path, CortexH5,
                                      simulator_in.surface.gid)
            with CortexH5(cortex_path) as cortex_h5:
                simulator_in.surface.local_connectivity = cortex_h5.local_connectivity.load(
                )
                simulator_in.surface.region_mapping_data = cortex_h5.region_mapping_data.load(
                )
                rm_index = dao.get_datatype_by_gid(
                    simulator_in.surface.region_mapping_data.hex)
                simulator_in.surface.surface_gid = uuid.UUID(
                    rm_index.surface_gid)

        return simulator_in
Ejemplo n.º 15
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.º 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 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.º 19
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
Ejemplo n.º 20
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.º 21
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)
    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.º 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 = [
            '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.º 24
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.º 25
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)
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
Ejemplo n.º 27
0
 def from_tvb_file(self, filepath, remove_leading_zeros_from_labels=False):
     self._tvb = TVBSensorsEEG.from_file(filepath, self._tvb)
     if len(self._tvb.labels) > 0:
         if remove_leading_zeros_from_labels:
             self.remove_leading_zeros_from_labels()
     return self
Ejemplo n.º 28
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())
Ejemplo n.º 29
0
    def launch(self, view_model):
        # type: (SensorsImporterModel) -> [SensorsIndex]
        """
        Creates required sensors from the uploaded file.
        :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 view_model.sensors_file is None:
            raise LaunchException(
                "Please select sensors file which contains data to import")

        self.logger.debug("Create sensors instance")
        if view_model.sensors_type == SensorsEEG.sensors_type.default:
            sensors_inst = SensorsEEG()
        elif view_model.sensors_type == SensorsMEG.sensors_type.default:
            sensors_inst = SensorsMEG()
        elif view_model.sensors_type == SensorsInternal.sensors_type.default:
            sensors_inst = SensorsInternal()
        else:
            exception_str = "Could not determine sensors type (selected option %s)" % view_model.sensors_type
            raise LaunchException(exception_str)

        locations = self.read_list_data(view_model.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(view_model.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(
                    view_model.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")

        return h5.store_complete(sensors_inst, self.storage_path)
Ejemplo n.º 30
0
                              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

    # model: Generic 2D Oscillator - neural mass has two state variables that
Ejemplo n.º 31
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.º 32
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