Ejemplo n.º 1
0
    def setUp(self):
        self.start_time = -0.5
        self.end_time = 1.6
        self.buffer_time = 0.5

        self.event_range = range(0, 30, 1)

        # here = osp.abspath(osp.dirname(__file__))
        here = get_rhino_root()
        self.e_path = osp.join(here, 'data', 'events', 'RAM_FR1', 'R1060M_events.mat')
        tal_path = osp.join(here, 'data', 'eeg', 'R1060M', 'tal', 'R1060M_talLocs_database_bipol.mat')

        tal_reader = TalReader(filename=tal_path)
        self.monopolar_channels = tal_reader.get_monopolar_channels()
        self.bipolar_pairs = tal_reader.get_bipolar_pairs()

        base_e_reader = BaseEventReader(filename=self.e_path, eliminate_events_with_no_eeg=True)
        base_events = base_e_reader.read()
        base_events = base_events[base_events.type == 'WORD']
        base_ev_order = np.argsort(base_events, order=('session', 'list', 'mstime'))
        self.base_events = base_events[base_ev_order]

        # retaining first session
        dataroot = self.base_events[0].eegfile
        self.base_events = self.base_events[self.base_events.eegfile == dataroot]
        self.base_events = self.base_events[self.event_range]

        eeg_reader = EEGReader(events=self.base_events, channels=self.monopolar_channels,
                               start_time=self.start_time, end_time=self.end_time, buffer_time=self.buffer_time)

        self.base_eegs = eeg_reader.read()

        session_reader = EEGReader(session_dataroot=dataroot, channels=self.monopolar_channels)
        self.session_eegs = session_reader.read()
Ejemplo n.º 2
0
def read_session(subject, root, experiment, rhino_root):
    jr = JsonIndexReader(rhino_root + '/protocols/r1.json')  # Build JSON reader
    pairs_path = jr.get_value('pairs', subject=subject, experiment=experiment)
    tal_reader = TalReader(filename=pairs_path)
    monopolar_channels = tal_reader.get_monopolar_channels()
    bipolar_channels = tal_reader.get_bipolar_pairs()

    jr = JsonIndexReader(rhino_root + '/protocols/r1.json')  # Build JSON reader
    pairs_path = jr.get_value('pairs', subject=subject, experiment=experiment)
    tal_reader = TalReader(filename=pairs_path)
    monopolar_channels = tal_reader.get_monopolar_channels()
    p_reader = ParamsReader(dataroot=root)
    params = p_reader.read()
    samplerate = params['samplerate']
    print samplerate
    try:
        raw_eeg = EEGReader(session_dataroot=root, channels = monopolar_channels)
        raw_eeg = raw_eeg.read()
        print raw_eeg.shape

    except:
        with h5py.File(root, 'r') as hfile:
            raw_eeg = H5RawReader.H5RawReader.read_h5file(hfile, monopolar_channels,[0])[0]
            print raw_eeg.shape
            raw_eeg = TimeSeriesX.TimeSeriesX(raw_eeg,
                                  dims=['channels', 'start_offsets', 'time'],
                                  coords={
                                      'channels': monopolar_channels,
                                      'start_offsets': [0],
                                      'time': np.arange(raw_eeg.shape[2])*1000/samplerate,
                                      'offsets': ('time',np.arange(raw_eeg.shape[2])),
                                      'samplerate': samplerate})
    return raw_eeg
Ejemplo n.º 3
0
    def setUp(self):
        root = get_rhino_root()
        self.e_path = osp.join(root, 'data', 'events', 'RAM_FR1',
                               'R1060M_events.mat')
        tal_path = osp.join(root, 'data', 'eeg', 'R1060M', 'tal',
                            'R1060M_talLocs_database_bipol.mat')

        base_e_reader = BaseEventReader(filename=self.e_path,
                                        eliminate_events_with_no_eeg=True)

        self.base_events = base_e_reader.read()

        tal_reader = TalReader(filename=tal_path)
        self.monopolar_channels = tal_reader.get_monopolar_channels()
        self.bipolar_pairs = tal_reader.get_bipolar_pairs()

        self.base_events = self.base_events[self.base_events.type == 'WORD']

        # retaining first session
        dataroot = self.base_events[0].eegfile
        self.base_events = self.base_events[self.base_events.eegfile == dataroot]

        eeg_reader = EEGReader(events=self.base_events, channels=self.monopolar_channels,
                               start_time=0.0, end_time=1.6, buffer_time=1.0)

        self.base_eegs = eeg_reader.read()
Ejemplo n.º 4
0
def get_bp_data(evs):
    eeg_reader = EEGReader(events=evs, channels=monopolar_channels,
                           start_time=start_time, end_time=end_time, buffer_time=buffer_time)

    base_eegs = eeg_reader.read()

    m2b = MonopolarToBipolarMapper(time_series=base_eegs, bipolar_pairs=bipolar_pairs)
    bp_eegs = m2b.filter()
    return bp_eegs
def test_1():
    import time
    start = time.time()

    e_path = '/Users/m/data/events/RAM_FR1/R1060M_events.mat'

    from ptsa.data.readers import BaseEventReader

    base_e_reader = BaseEventReader(filename=e_path,
                                    eliminate_events_with_no_eeg=True,
                                    use_ptsa_events_class=False)

    base_events = base_e_reader.read()

    base_events = base_events[base_events.type == 'WORD']

    # selecting only one session
    base_events = base_events[base_events.eegfile == base_events[0].eegfile]

    from ptsa.data.readers.TalReader import TalReader
    tal_path = '/Users/m/data/eeg/R1060M/tal/R1060M_talLocs_database_bipol.mat'
    tal_reader = TalReader(filename=tal_path)
    monopolar_channels = tal_reader.get_monopolar_channels()
    bipolar_pairs = tal_reader.get_bipolar_pairs()

    dataroot = base_events[0].eegfile
    from ptsa.data.readers import EEGReader
    session_reader = EEGReader(session_dataroot=dataroot,
                               channels=monopolar_channels)
    session_eegs = session_reader.read()

    wavelet_start = time.time()

    wf = MorletWaveletFilterSimple(
        time_series=session_eegs,
        freqs=np.logspace(np.log10(3), np.log10(180), 2),
        # freqs=np.array([3.]),
        output='power',
        # resamplerate=50.0
    )

    pow_wavelet, phase_wavelet = wf.filter()
    print('wavelet total time = ', time.time() - wavelet_start)
    # return pow_wavelet

    from ptsa.data.filters import EventDataChopper
    sedc = EventDataChopper(events=base_events,
                            session_data=pow_wavelet,
                            start_time=0.0,
                            end_time=1.6,
                            buffer_time=1.0)
    chopped_wavelets = sedc.filter()

    print('total time = ', time.time() - start)

    return chopped_wavelets
Ejemplo n.º 6
0
    def test_R1070T_read(self):
        e_path = '/Volumes/rhino_root/data/events/RAM_FR1/R1070T_events.mat'
        base_event_reader = BaseEventReader(filename=e_path)

        start_time = 0.0
        end_time = 1.366
        buffer_time = 1.365

        base_events = base_event_reader.read()
        eeg_reader = EEGReader(events=base_events, channels=np.array(['042', '043']),
                               start_time=start_time, end_time=end_time, buffer_time=buffer_time)
        base_eegs = eeg_reader.read()
def test_1():
    import time

    start = time.time()

    e_path = "/Users/m/data/events/RAM_FR1/R1060M_events.mat"

    from ptsa.data.readers import BaseEventReader

    base_e_reader = BaseEventReader(filename=e_path, eliminate_events_with_no_eeg=True, use_ptsa_events_class=False)

    base_events = base_e_reader.read()

    base_events = base_events[base_events.type == "WORD"]

    # selecting only one session
    base_events = base_events[base_events.eegfile == base_events[0].eegfile]

    from ptsa.data.readers.TalReader import TalReader

    tal_path = "/Users/m/data/eeg/R1060M/tal/R1060M_talLocs_database_bipol.mat"
    tal_reader = TalReader(filename=tal_path)
    monopolar_channels = tal_reader.get_monopolar_channels()
    bipolar_pairs = tal_reader.get_bipolar_pairs()

    dataroot = base_events[0].eegfile
    from ptsa.data.readers import EEGReader

    session_reader = EEGReader(session_dataroot=dataroot, channels=monopolar_channels)
    session_eegs = session_reader.read()

    wavelet_start = time.time()

    wf = MorletWaveletFilterSimple(
        time_series=session_eegs,
        freqs=np.logspace(np.log10(3), np.log10(180), 2),
        # freqs=np.array([3.]),
        output="power",
        # resamplerate=50.0
    )

    pow_wavelet, phase_wavelet = wf.filter()
    print "wavelet total time = ", time.time() - wavelet_start
    # return pow_wavelet

    from ptsa.data.filters import EventDataChopper

    sedc = EventDataChopper(events=base_events, session_data=pow_wavelet, start_time=0.0, end_time=1.6, buffer_time=1.0)
    chopped_wavelets = sedc.filter()

    print "total time = ", time.time() - start

    return chopped_wavelets
Ejemplo n.º 8
0
    def test_event_data_chopper(self):
        dataroot = self.base_events[0].eegfile
        session_reader = EEGReader(session_dataroot=dataroot, channels=self.monopolar_channels)
        session_eegs = session_reader.read()

        sedc = DataChopper(events=self.base_events, session_data=session_eegs,
                           start_time=self.start_time, end_time=self.end_time, buffer_time=self.buffer_time)
        chopped_session = sedc.filter()
        assert_array_equal(chopped_session, self.base_eegs)

        sedc = DataChopper(start_offsets=self.base_events.eegoffset, session_data=session_eegs,
                           start_time=self.start_time, end_time=self.end_time, buffer_time=self.buffer_time)
        chopped_session = sedc.filter()
        assert_array_equal(chopped_session, self.base_eegs)
 def _test_eeg_with_tal_struct(self, subject, experiment, session, struct_type='mono'):
     from ptsa.data.readers import JsonIndexReader,TalReader,EEGReader,BaseEventReader
     import os
     tal_file = 'contacts' if struct_type=='mono' else 'pairs'
     jr = JsonIndexReader(os.path.join(get_rhino_root(),'protocols','r1.json'))
     events = BaseEventReader(filename=jr.get_value('task_events',subject=subject,experiment=experiment,session=session)).read()
     tal_struct = TalReader(filename=jr.get_value(tal_file,subject=subject),struct_type=struct_type).read()
     eeg_reader = EEGReader(events=events[:10],channels=tal_struct[:10],start_time=0.0,end_time=0.1)
     eeg = eeg_reader.read()
     print(eeg)
     channel_name = eeg_reader.channel_name
     for col in tal_struct.dtype.names:
         if col != 'atlases': # Because I don't want to deal with NaNs at the moment
             assert_array_equal(eeg[channel_name].data[col],tal_struct[:10][col])
Ejemplo n.º 10
0
def compute_continuous_wavelets(dataroot):
    session_reader = EEGReader(session_dataroot=dataroot, channels=monopolar_channels)
    session_eegs = session_reader.read()

    m2b = MonopolarToBipolarMapper(time_series=session_eegs, bipolar_pairs=bipolar_pairs)
    session_bp_eegs = m2b.filter()

    wf = MorletWaveletFilter(time_series=session_bp_eegs,
                             freqs=np.logspace(np.log10(3), np.log10(180), 8),
                             output='power',
                             frequency_dim_pos=0,
                             verbose=True
                             )

    pow_wavelet_session, phase_wavelet_session = wf.filter()
    return pow_wavelet_session
Ejemplo n.º 11
0
    def test_eeg_read_incomplete_data(self):
        e_path_incomplete = '/Volumes/rhino_root/data/events/RAM_PS/R1104D_events.mat'
        if sys.platform.startswith('win'):
            e_path_incomplete = 'D:/data/events/RAM_PS/R1104D_events.mat'


        base_event_reader = BaseEventReader(filename=e_path_incomplete)

        base_events = base_event_reader.read()

        sess_1 = base_events[base_events.session == 1]
        sess_3 = base_events[base_events.session == 3]
        sess_5 = base_events[base_events.session == 5]
        sess_7 = base_events[base_events.session == 7]

        sess_1[440].eegoffset = 2000000000000
        sess_1[444].eegoffset = 2000000000000
        sess_1[434].eegoffset = 2000000000000

        shuffled_sess_events = np.hstack((sess_3, sess_7, sess_1, sess_5)).view(np.recarray)

        eeg_reader = EEGReader(events=shuffled_sess_events, channels=np.array(['002', '003']),
                               start_time=self.start_time, end_time=self.end_time, buffer_time=self.buffer_time)
        base_eegs = eeg_reader.read()

        if eeg_reader.removed_bad_data():
            print('REMOVED BAD DATA !!!!!!!!!!!!!')

        events = base_eegs['events'].data.view(np.recarray)

        if not isinstance(events, Events):
            events = Events(events)

        from ptsa.data.rawbinwrapper import RawBinWrapper
        events = events.add_fields(esrc=np.dtype(RawBinWrapper))

        # ------------- using PTSAevent reader functions to prepare events for reading old-ptsa-style

        ptsa_event_reader = PTSAEventReader()

        ptsa_event_reader.attach_rawbinwrapper_groupped(events)

        eegs = events.get_data(channels=['002', '003'], start_time=self.start_time, end_time=self.end_time,
                               buffer_time=self.buffer_time, eoffset='eegoffset', keep_buffer=True,
                               eoffset_in_time=False, verbose=True)

        npt.assert_array_equal(eegs[:, :, :-1], base_eegs.data)
Ejemplo n.º 12
0
 def test_h5reader_full_session(self):
     dataroot = self.dataroot_template.format(
         subject='R1354E',
         experiment='FR1',
         session=0,
         filename='R1354E_FR1_0_26Oct17_2006.h5'
     )
     EEGReader(session_dataroot=dataroot,channels=np.array([])).read()
Ejemplo n.º 13
0
    def test_base_raw_reader(self):
        e_path = '/Users/m/data/events/RAM_FR1/R1060M_events.mat'
        base_e_reader = BaseEventReader(filename=e_path, eliminate_events_with_no_eeg=True)
        base_events = base_e_reader.read()
        base_events = base_events[base_events.type == 'WORD']

        dataroot = base_events[0].eegfile

        brr_session = BaseRawReader(dataroot=dataroot, channels=np.array(['002', '003']))
        array_session, read_ok_mask = brr_session.read()

        eeg_reader = EEGReader(events=base_events, channels=np.array(['002', '003']),
                               start_time=self.start_time, end_time=self.end_time, buffer_time=0.0)
        base_eegs = eeg_reader.read()

        for i in xrange(100):
            offset = base_events[i].eegoffset
            npt.assert_array_equal(array_session[:, 0, offset:offset + 100], base_eegs[:, i, :100])
Ejemplo n.º 14
0
    def test_monopolar_to_bipolar_filter_and_data_chopper(self):
        dataroot = self.base_events[0].eegfile

        session_reader = EEGReader(session_dataroot=dataroot, channels=self.monopolar_channels)
        session_eegs = session_reader.read()

        m2b = MonopolarToBipolarMapper(time_series=session_eegs, bipolar_pairs=self.bipolar_pairs)
        bp_session_eegs = m2b.filter()

        sedc = DataChopper(events=self.base_events, session_data=bp_session_eegs, start_time=self.start_time,
                           end_time=self.end_time, buffer_time=self.buffer_time)

        bp_session_eegs_chopped = sedc.filter()

        m2b = MonopolarToBipolarMapper(time_series=self.base_eegs, bipolar_pairs=self.bipolar_pairs)
        bp_base_eegs = m2b.filter()

        assert_array_equal(bp_session_eegs_chopped, bp_base_eegs)
Ejemplo n.º 15
0
 def test_with_events(self):
     dataroot_format = osp.join(get_rhino_root(), 'scratch', 'ptsa_test',
                                'R1308T', 'experiments', 'FR1', 'sessions',
                                '3', 'behavioral', 'current_processed',
                                'task_events.json')
     events = CMLEventReader(filename=dataroot_format).read()[:20]
     EEGReader(events=events,
               channels=np.array(['%.03d' % i for i in range(1, 10)]),
               start_time=0.0,
               end_time=0.5).read()
Ejemplo n.º 16
0
 def test_h5reader_empty_channels(self):
     dataroot = self.task_events_template.format(
         subject='R1358T',
         experiment='FR1',
         session=0
     )
     events = CMLEventReader(filename=dataroot).read()[:20]
     eeg = EEGReader(events=events, channels=np.array([]),
                     start_time=0.0, end_time=0.5).read()
     assert len(eeg['channels']) > 0
Ejemplo n.º 17
0
 def test_h5reader_empty_channels(self):
     dataroot_format = osp.join(get_rhino_root(), 'scratch', 'ptsa_test',
                                'R1308T', 'experiments', 'FR1', 'sessions',
                                '3', 'behavioral', 'current_processed',
                                'task_events.json')
     events = CMLEventReader(filename=dataroot_format).read()[:20]
     eeg = EEGReader(events=events,
                     channels=np.array([]),
                     start_time=0.0,
                     end_time=0.5).read()
     assert len(eeg.bipolar_pairs) > 0
Ejemplo n.º 18
0
 def test_with_events(self):
     dataroot = self.task_events_template.format(
         subject='R1358T',
         experiment='FR1',
         session=0
     )
     events = CMLEventReader(filename=dataroot).read()[:20]
     EEGReader(events=events,
               channels=np.array(['{:03d}'.format(i).encode() for i in range(1, 10)]),
               start_time=0.0,
               end_time=0.5).read()
    def test_event_data_chopper(self):
        dataroot = self.base_events[0].eegfile
        session_reader = EEGReader(session_dataroot=dataroot,
                                   channels=self.monopolar_channels)
        session_eegs = session_reader.read()

        sedc = DataChopper(events=self.base_events,
                           timeseries=session_eegs,
                           start_time=self.start_time,
                           end_time=self.end_time,
                           buffer_time=self.buffer_time)
        chopped_session = sedc.filter()
        assert_array_equal(chopped_session, self.base_eegs)

        sedc = DataChopper(start_offsets=self.base_events.eegoffset,
                           timeseries=session_eegs,
                           start_time=self.start_time,
                           end_time=self.end_time,
                           buffer_time=self.buffer_time)
        chopped_session = sedc.filter()
        assert_array_equal(chopped_session, self.base_eegs)
Ejemplo n.º 20
0
def get_stim_eeg(evs_on,
                 tal_struct,
                 bipolar_pairs,
                 monopolar_channels,
                 chans=np.array([]),
                 start_time=-1.0,
                 end_time=1.5):

    from ptsa.data.readers import EEGReader
    from ptsa.data.filters import MonopolarToBipolarMapper

    #Get EEG clips
    eeg_reader = EEGReader(events=evs_on,
                           channels=chans,
                           start_time=start_time,
                           end_time=end_time,
                           buffer_time=0.0)
    eegs = eeg_reader.read()
    eegs = eegs.baseline_corrected((-1.0, 2.5))

    if tal_struct is np.nan:
        return eegs, np.nan

    if ('bipolar_pairs'
            in list(eegs.coords)):  #Gotta do this for bipolar ENS subjects
        bps_eeg = [list(i) for i in np.array(eegs['bipolar_pairs'])]
        bps_tal = [list(i) for i in np.array(tal_struct['channel'])]
        bps_eeg_good = []
        bps_tal_good = []
        for iidx, i in enumerate(bps_eeg):
            for jidx, j in enumerate(bps_tal):
                if i == j:
                    bps_eeg_good.append(iidx)
                    bps_tal_good.append(jidx)
        bps_eeg_good = np.array(bps_eeg_good)
        bps_tal_good = np.array(bps_tal_good)

        eegs_raw = eegs[bps_eeg_good]
        tal_struct = tal_struct[bps_tal_good]
    else:
        try:
            m2b = MonopolarToBipolarMapper(time_series=eegs,
                                           bipolar_pairs=bipolar_pairs)
            eegs_raw = m2b.filter()
        except KeyError:  #we've got to do this subject with specified channels
            eeg_reader = EEGReader(events=evs_on,
                                   channels=monopolar_channels,
                                   start_time=start_time,
                                   end_time=end_time,
                                   buffer_time=0.0)
            eegs = eeg_reader.read()
            eegs = eegs.baseline_corrected((-1.0, 2.5))

            #Bipolar rereference
            m2b = MonopolarToBipolarMapper(time_series=eegs,
                                           bipolar_pairs=bipolar_pairs)
            eegs_raw = m2b.filter()

    return eegs_raw, tal_struct
Ejemplo n.º 21
0
    def test_scalp_eeg_readers(self):

        subject_list = ['LTP001', 'LTP002', 'LTP040']
        task = 'catFR'

        monopolar_channels = np.array(map(lambda x: str(x).zfill(3), range(1,129)))

        for subject in subject_list:
            e_path = e_path_matlab_scalp(self.prefix, subject, task)

            cml_evs = read_events(e_path=e_path,
                                  reader_class=CMLEventReader,
                                     common_root='data/scalp_events',
                                     normalize_eeg_path=True
                                  )



            eeg_reader = EEGReader(events=cml_evs[:30], channels = monopolar_channels,
                       start_time=self.start_time, end_time=self.end_time, buffer_time=self.buffer_time)

            eegs = eeg_reader.read()
            print eegs
    def test_monopolar_to_bipolar_filter_and_data_chopper(self):
        dataroot = self.base_events[0].eegfile

        session_reader = EEGReader(session_dataroot=dataroot,
                                   channels=self.monopolar_channels)
        session_eegs = session_reader.read()

        m2b = MonopolarToBipolarMapper(timeseries=session_eegs,
                                       bipolar_pairs=self.bipolar_pairs)
        bp_session_eegs = m2b.filter()

        sedc = DataChopper(events=self.base_events,
                           timeseries=bp_session_eegs,
                           start_time=self.start_time,
                           end_time=self.end_time,
                           buffer_time=self.buffer_time)

        bp_session_eegs_chopped = sedc.filter()

        m2b = MonopolarToBipolarMapper(timeseries=self.base_eegs,
                                       bipolar_pairs=self.bipolar_pairs)
        bp_base_eegs = m2b.filter()

        assert_array_equal(bp_session_eegs_chopped, bp_base_eegs)
Ejemplo n.º 23
0
    def test_eeg_read(self):

        ptsa_reader = PTSAEventReader(filename=self.e_path)

        events = ptsa_reader.read()

        # in case fancy indexing looses Eventness of events we need to create Events object explicitely
        if not isinstance(events, Events):
            events = Events(events)

        eegs = events.get_data(channels=['002', '003'], start_time=self.start_time, end_time=self.end_time,
                               buffer_time=self.buffer_time, eoffset='eegoffset', keep_buffer=True,
                               eoffset_in_time=False, verbose=True)

        base_event_reader = BaseEventReader(filename=self.e_path)

        base_events = base_event_reader.read()

        eeg_reader = EEGReader(events=base_events, channels=np.array(['002', '003']),
                               start_time=self.start_time, end_time=self.end_time, buffer_time=self.buffer_time)

        base_eegs = eeg_reader.read()

        npt.assert_array_equal(eegs[:, :, :-1], base_eegs.data)
Ejemplo n.º 24
0
    def test_eeg_reader(self, subject, session):
        jr = JsonIndexReader(
            osp.join(get_rhino_root(), 'protocols', 'ltp.json'))
        events = BaseEventReader(filename=jr.get_value(
            'task_events', subject=subject, session=session)).read()

        # hack to make the scalp events work with Rhino mounted
        events['eegfile'] = get_rhino_root() + events[0]['eegfile']

        channels = np.array([0, 1])
        eeg = EEGReader(events=events[:10],
                        channels=channels,
                        start_time=0.0,
                        end_time=0.1).read()
        assert (eeg['channels'].values['index'] == channels).all()
        assert (eeg['channels'].values['label'] == np.array(
            ['A1', 'A2'], dtype=bytes)).all()
    def setUp(self):
        self.start_time = -0.5
        self.end_time = 1.6
        self.buffer_time = 0.5

        self.event_range = range(0, 30, 1)

        # here = osp.abspath(osp.dirname(__file__))
        here = get_rhino_root()
        self.e_path = osp.join(here, 'data', 'events', 'RAM_FR1',
                               'R1060M_events.mat')
        tal_path = osp.join(here, 'data', 'eeg', 'R1060M', 'tal',
                            'R1060M_talLocs_database_bipol.mat')

        tal_reader = TalReader(filename=tal_path)
        self.monopolar_channels = tal_reader.get_monopolar_channels()
        self.bipolar_pairs = tal_reader.get_bipolar_pairs()

        base_e_reader = BaseEventReader(filename=self.e_path,
                                        eliminate_events_with_no_eeg=True)
        base_events = base_e_reader.read()
        base_events = base_events[base_events.type == 'WORD']
        base_ev_order = np.argsort(base_events,
                                   order=('session', 'list', 'mstime'))
        self.base_events = base_events[base_ev_order]

        # retaining first session
        dataroot = self.base_events[0].eegfile
        self.base_events = self.base_events[self.base_events.eegfile ==
                                            dataroot]
        self.base_events = self.base_events[self.event_range]

        eeg_reader = EEGReader(events=self.base_events,
                               channels=self.monopolar_channels,
                               start_time=self.start_time,
                               end_time=self.end_time,
                               buffer_time=self.buffer_time)

        self.base_eegs = eeg_reader.read()

        session_reader = EEGReader(session_dataroot=dataroot,
                                   channels=self.monopolar_channels)
        self.session_eegs = session_reader.read()
Ejemplo n.º 26
0
if __name__ == '__main__':
    for subject in bipolar_subjects:
        logger.set_subject(subject, 'R1')
        sucess, _ = make_bipolar_montage(subject)
        if sucess:
            tal = TalReader(filename=pairs_path_template.format(
                protocols_root=test_index_root, subject=subject))
            new_bipolar_pairs = tal.get_bipolar_pairs()
            jr = JsonIndexReader(
                osp.join(reference_index_root, 'protocols', 'r1.json'))
            task_event_files = list(
                jr.aggregate_values('task_events', subject=subject, montage=0))
            events = CMLEventReader(filename=task_event_files[0]).read()
            eeg = EEGReader(events=events[:1],
                            channels=np.array([]),
                            start_time=0.0,
                            end_time=0.1).read()
            hdf_bipolar_pairs = eeg.bipolar_pairs.values
            new_bipolar_pairs = new_bipolar_pairs.astype(
                hdf_bipolar_pairs.dtype)
            if not np.in1d(hdf_bipolar_pairs, new_bipolar_pairs).all():
                logger.info(
                    '\n\n%s missing from new_bipolar_pairs\n\n' %
                    str(hdf_bipolar_pairs[
                        ~np.in1d(hdf_bipolar_pairs, new_bipolar_pairs)]))
            if not np.in1d(new_bipolar_pairs, hdf_bipolar_pairs).all():
                logger.info(
                    '\n\n%s missing from new_bipolar_pairs\n\n' %
                    str(new_bipolar_pairs[
                        ~np.in1d(new_bipolar_pairs, hdf_bipolar_pairs)]))
Ejemplo n.º 27
0
    def compute_ps_powers(self, events, sessions, monopolar_channels, bipolar_pairs, experiment):
        n_freqs = len(self.params.freqs)
        n_bps = len(bipolar_pairs)

        pow_mat_pre = pow_mat_post = None

        pow_ev = None
        samplerate = winsize = bufsize = None

        monopolar_channels_list = list(monopolar_channels)
        for sess in sessions:
            sess_events = events[events.session == sess]
            # print type(sess_events)

            n_events = len(sess_events)

            print 'Loading EEG for', n_events, 'events of session', sess

            pre_start_time = self.params.ps_start_time - self.params.ps_offset
            pre_end_time = self.params.ps_end_time - self.params.ps_offset

            # eegs_pre = Events(sess_events).get_data(channels=channels, start_time=pre_start_time, end_time=pre_end_time,
            #             buffer_time=self.params.ps_buf, eoffset='eegoffset', keep_buffer=True, eoffset_in_time=False)


            eeg_pre_reader = EEGReader(events=sess_events, channels=np.array(monopolar_channels_list),
                                   start_time=pre_start_time,
                                   end_time=pre_end_time, buffer_time=self.params.ps_buf)

            eegs_pre = eeg_pre_reader.read()


            if samplerate is None:
                # samplerate = round(eegs_pre.samplerate)
                # samplerate = eegs_pre.attrs['samplerate']

                samplerate = float(eegs_pre['samplerate'])

                winsize = int(round(samplerate*(pre_end_time-pre_start_time+2*self.params.ps_buf)))
                bufsize = int(round(samplerate*self.params.ps_buf))
                print 'samplerate =', samplerate, 'winsize =', winsize, 'bufsize =', bufsize
                pow_ev = np.empty(shape=n_freqs*winsize, dtype=float)
                self.wavelet_transform.init(self.params.width, self.params.freqs[0], self.params.freqs[-1], n_freqs, samplerate, winsize)

            # mirroring
            nb_ = int(round(samplerate*(self.params.ps_buf)))
            eegs_pre[...,-nb_:] = eegs_pre[...,-nb_-2:-2*nb_-2:-1]

            dim3_pre = eegs_pre.shape[2]  # because post-stim time inreval does not align for all stim events (stims have different duration)
                                          # we have to take care of aligning eegs_post ourselves time dim to dim3

            # eegs_post = np.zeros_like(eegs_pre)

            from ptsa.data.TimeSeriesX import TimeSeriesX
            eegs_post = TimeSeriesX(np.zeros_like(eegs_pre),dims=eegs_pre.dims,coords=eegs_pre.coords)


            post_start_time = self.params.ps_offset
            post_end_time = self.params.ps_offset + (self.params.ps_end_time - self.params.ps_start_time)
            for i_ev in xrange(n_events):
                ev_offset = sess_events[i_ev].pulse_duration
                if ev_offset > 0:
                    if experiment == 'PS3' and sess_events[i_ev].nBursts > 0:
                        ev_offset *= sess_events[i_ev].nBursts + 1
                    ev_offset *= 0.001
                else:
                    ev_offset = 0.0

                # eeg_post = Events(sess_events[i_ev:i_ev+1]).get_data(channels=channels, start_time=post_start_time+ev_offset,
                #             end_time=post_end_time+ev_offset, buffer_time=self.params.ps_buf,
                #             eoffset='eegoffset', keep_buffer=True, eoffset_in_time=False)

                eeg_post_reader = EEGReader(events=sess_events[i_ev:i_ev+1], channels=np.array(monopolar_channels_list),
                                       start_time=post_start_time+ev_offset,
                                       end_time=post_end_time+ev_offset, buffer_time=self.params.ps_buf)

                eeg_post = eeg_post_reader.read()

                dim3_post = eeg_post.shape[2]
                # here we take care of possible mismatch of time dim length
                if dim3_pre == dim3_post:
                    eegs_post[:,i_ev:i_ev+1,:] = eeg_post
                elif dim3_pre < dim3_post:
                    eegs_post[:,i_ev:i_ev+1,:] = eeg_post[:,:,:-1]
                else:
                    eegs_post[:,i_ev:i_ev+1,:-1] = eeg_post

            # mirroring
            eegs_post[...,:nb_] = eegs_post[...,2*nb_:nb_:-1]

            print 'Computing', experiment, 'powers'

            sess_pow_mat_pre = np.empty(shape=(n_events, n_bps, n_freqs), dtype=np.float)
            sess_pow_mat_post = np.empty_like(sess_pow_mat_pre)

            for i,ti in enumerate(bipolar_pairs):
                bp = ti['channel_str']
                print 'Computing powers for bipolar pair', bp
                elec1 = np.where(monopolar_channels == bp[0])[0][0]
                elec2 = np.where(monopolar_channels == bp[1])[0][0]

            #
            # for i,ti in enumerate(tal_info):
            #     bp = ti['channel_str']
            #     print 'Computing powers for bipolar pair', bp
            #     elec1 = np.where(channels == bp[0])[0][0]
            #     elec2 = np.where(channels == bp[1])[0][0]


                bp_data_pre = eegs_pre[elec1] - eegs_pre[elec2]
                # bp_data_pre.attrs['samplerate'] = samplerate

                bp_data_pre = bp_data_pre.filtered([58,62], filt_type='stop', order=self.params.filt_order)
                for ev in xrange(n_events):
                    #pow_pre_ev = phase_pow_multi(self.params.freqs, bp_data_pre[ev], to_return='power')
                    self.wavelet_transform.multiphasevec(bp_data_pre[ev][0:winsize], pow_ev)
                    #sess_pow_mat_pre[ev,i,:] = np.mean(pow_pre_ev[:,nb_:-nb_], axis=1)
                    pow_ev_stripped = np.reshape(pow_ev, (n_freqs,winsize))[:,bufsize:winsize-bufsize]
                    if self.params.log_powers:
                        np.log10(pow_ev_stripped, out=pow_ev_stripped)
                    sess_pow_mat_pre[ev,i,:] = np.nanmean(pow_ev_stripped, axis=1)

                bp_data_post = eegs_post[elec1] - eegs_post[elec2]
                # bp_data_post.attrs['samplerate'] = samplerate

                bp_data_post = bp_data_post.filtered([58,62], filt_type='stop', order=self.params.filt_order)
                for ev in xrange(n_events):
                    #pow_post_ev = phase_pow_multi(self.params.freqs, bp_data_post[ev], to_return='power')
                    self.wavelet_transform.multiphasevec(bp_data_post[ev][0:winsize], pow_ev)
                    #sess_pow_mat_post[ev,i,:] = np.mean(pow_post_ev[:,nb_:-nb_], axis=1)
                    pow_ev_stripped = np.reshape(pow_ev, (n_freqs,winsize))[:,bufsize:winsize-bufsize]
                    if self.params.log_powers:
                        np.log10(pow_ev_stripped, out=pow_ev_stripped)
                    sess_pow_mat_post[ev,i,:] = np.nanmean(pow_ev_stripped, axis=1)

            sess_pow_mat_pre = sess_pow_mat_pre.reshape((n_events, n_bps*n_freqs))
            #sess_pow_mat_pre = zscore(sess_pow_mat_pre, axis=0, ddof=1)

            sess_pow_mat_post = sess_pow_mat_post.reshape((n_events, n_bps*n_freqs))
            #sess_pow_mat_post = zscore(sess_pow_mat_post, axis=0, ddof=1)

            sess_pow_mat_joint = zscore(np.vstack((sess_pow_mat_pre,sess_pow_mat_post)), axis=0, ddof=1)
            sess_pow_mat_pre = sess_pow_mat_joint[:n_events,...]
            sess_pow_mat_post = sess_pow_mat_joint[n_events:,...]

            pow_mat_pre = np.vstack((pow_mat_pre,sess_pow_mat_pre)) if pow_mat_pre is not None else sess_pow_mat_pre
            pow_mat_post = np.vstack((pow_mat_post,sess_pow_mat_post)) if pow_mat_post is not None else sess_pow_mat_post

        return pow_mat_pre, pow_mat_post
Ejemplo n.º 28
0
    def compute_powers(self, events, sessions, monopolar_channels, bipolar_pairs, start_time, end_time, mirror_front, mirror_back):
        n_freqs = len(self.params.freqs)
        n_bps = len(bipolar_pairs)

        self.pow_mat = None

        pow_ev = None
        winsize = bufsize = None
        for sess in sessions:
            sess_events = events[events.session == sess]
            n_events = len(sess_events)

            print 'Loading EEG for', n_events, 'events of session', sess

            # eegs = Events(sess_events).get_data(channels=channels, start_time=self.params.control_start_time, end_time=self.params.control_end_time,
            #                             buffer_time=self.params.control_buf, eoffset='eegoffset', keep_buffer=True, eoffset_in_time=False)

            # from ptsa.data.readers import TimeSeriesEEGReader
            # time_series_reader = TimeSeriesEEGReader(events=sess_events, start_time=self.params.control_start_time,
            #                                  end_time=self.params.control_end_time, buffer_time=self.params.control_buf, keep_buffer=True)
            #
            # eegs = time_series_reader.read(monopolar_channels)

            eeg_reader = EEGReader(events=sess_events, channels = monopolar_channels,
                                   start_time=start_time, end_time=end_time, buffer_time=self.params.control_buf)

            eegs = eeg_reader.read()

            # print 'eegs=',eegs.values[0,0,:2],eegs.values[0,0,-2:]
            # sys.exit()
            #
            # a = eegs[0]-eegs[1]

            #eegs[...,:1365] = eegs[...,2730:1365:-1]
            #eegs[...,2731:4096] = eegs[...,2729:1364:-1]

            if self.samplerate is None:
                self.samplerate = float(eegs.samplerate)
                winsize = int(round(self.samplerate*(self.params.control_end_time-self.params.control_start_time+2*self.params.control_buf)))
                bufsize = int(round(self.samplerate*self.params.control_buf))
                print 'samplerate =', self.samplerate, 'winsize =', winsize, 'bufsize =', bufsize
                pow_ev = np.empty(shape=n_freqs*winsize, dtype=float)
                self.wavelet_transform.init(self.params.width, self.params.freqs[0], self.params.freqs[-1], n_freqs, self.samplerate, winsize)

            # mirroring
            nb_ = int(round(self.samplerate*(self.params.control_buf)))
            if mirror_front:
                eegs[...,:nb_] = eegs[...,2*nb_-1:nb_-1:-1]
            if mirror_back:
                eegs[...,-nb_:] = eegs[...,-nb_-1:-2*nb_-1:-1]

            print 'Computing control powers'

            sess_pow_mat = np.empty(shape=(n_events, n_bps, n_freqs), dtype=np.float)

            #monopolar_channels_np = np.array(monopolar_channels)
            for i,ti in enumerate(bipolar_pairs):
                # print bp
                # print monopolar_channels

                # print np.where(monopolar_channels == bp[0])
                # print np.where(monopolar_channels == bp[1])
                bp = ti['channel_str']
                print 'Computing powers for bipolar pair', bp
                elec1 = np.where(monopolar_channels == bp[0])[0][0]
                elec2 = np.where(monopolar_channels == bp[1])[0][0]
                # print 'elec1=',elec1
                # print 'elec2=',elec2
                # eegs_elec1 = eegs[elec1]
                # eegs_elec2 = eegs[elec2]
                # print 'eegs_elec1=',eegs_elec1
                # print 'eegs_elec2=',eegs_elec2
                # eegs_elec1.reset_coords('channels')
                # eegs_elec2.reset_coords('channels')

                bp_data = eegs[elec1] - eegs[elec2]
                bp_data.attrs['samplerate'] = self.samplerate

                # bp_data = eegs[elec1] - eegs[elec2]
                # bp_data = eegs[elec1] - eegs[elec2]
                # bp_data = eegs.values[elec1] - eegs.values[elec2]

                bp_data = bp_data.filtered([58,62], filt_type='stop', order=self.params.filt_order)
                for ev in xrange(n_events):
                    self.wavelet_transform.multiphasevec(bp_data[ev][0:winsize], pow_ev)
                    #if np.min(pow_ev) < 0.0:
                    #    print ev, events[ev]
                    #    joblib.dump(bp_data[ev], 'bad_bp_ev%d'%ev)
                    #    joblib.dump(eegs[elec1][ev], 'bad_elec1_ev%d'%ev)
                    #    joblib.dump(eegs[elec2][ev], 'bad_elec2_ev%d'%ev)
                    #    print 'Negative powers detected'
                    #    import sys
                    #    sys.exit(1)
                    pow_ev_stripped = np.reshape(pow_ev, (n_freqs,winsize))[:,bufsize:winsize-bufsize]
                    if self.params.log_powers:
                        np.log10(pow_ev_stripped, out=pow_ev_stripped)
                    sess_pow_mat[ev,i,:] = np.nanmean(pow_ev_stripped, axis=1)

            sess_pow_mat = zscore(sess_pow_mat, axis=0, ddof=1)
            self.pow_mat = np.concatenate((self.pow_mat,sess_pow_mat), axis=0) if self.pow_mat is not None else sess_pow_mat

        self.pow_mat = np.reshape(self.pow_mat, (len(events), n_bps*n_freqs))
    filename=rhino_root +
    '/data/eeg/scalp/ltp/ltpFR2/behavioral/events/events_all_LTP377.mat',
    use_reref_eeg=True,
    common_root='data').read()
words = events[events.type == 'WORD']

# select all available channels
eegfile = words[0].eegfile
eegfile_reref = str.split(str(eegfile), '/')
day = eegfile_reref[-1]
eegfile_reref = '/'.join(eegfile_reref[0:-1])
channels = glob.glob(eegfile_reref + '/' + day + "*.[0-9]*")
channels = np.array([str.split(x, '.')[-1] for x in channels])
#channels = np.array(['{:03}'.format(x) for x in range(0,132)])

eeg = EEGReader(events=words, channels=channels, start_time=0.3,
                end_time=1.6).read()

b_filter = ButterworthFilter(time_series=eeg, order=4, freq_range=[58, 62])
eeg_filtered = b_filter.filter()
eeg_buffered = eeg_filtered.add_mirror_buffer(1.3)
eeg_buffered.data = np.ascontiguousarray(eeg_buffered.data)

import time
pt = time.time()
freqs = np.logspace(np.log10(3), np.log10(180), 50)
pow_ev, _ = MorletWaveletFilterCpp(time_series=eeg_buffered,
                                   freqs=freqs[0:1],
                                   output='power',
                                   cpus=20,
                                   verbose=False).filter()
run_time = time.time() - pt
Ejemplo n.º 30
0
    monopolar_channels = tal_reader.get_monopolar_channels()
    bipolar_pairs = tal_reader.get_bipolar_pairs()

    # ---------------- NEW STYLE PTSA -------------------
    base_e_reader = BaseEventReader(filename=e_path,
                                    eliminate_events_with_no_eeg=True)

    base_events = base_e_reader.read()

    base_events = base_events[(base_events.type == 'STIMULATING')]
    base_events = base_events[base_events.session == 2]

    from ptsa.data.readers.EEGReader import EEGReader
    eeg_reader = EEGReader(events=base_events,
                           channels=monopolar_channels[0:3],
                           start_time=-1.1,
                           end_time=-0.1,
                           buffer_time=1.0)

    base_eegs = eeg_reader.read()

    bw_base_eegs = base_eegs.filtered(freq_range=[58., 62.],
                                      filt_type='stop',
                                      order=4)

    b, a = butter_bandpass(58., 62., float(base_eegs['samplerate']), 4)
    y_in = base_eegs[0, 0, :].data

    y_out = butter_bandpass_filter(y_in, 58., 62.,
                                   float(base_eegs['samplerate']), 4)
Ejemplo n.º 31
0
        try:
            return str(''.join(c for c in unicodedata.normalize('NFD', unicode(s))
                               if unicodedata.category(c) != 'Mn'))
        except UnicodeError:  # If accents can't be converted, just remove them
            return str(re.sub(r'[^A-Za-z0-9 -_.]', '', s))


if __name__ == '__main__':

    e_path = '/Volumes/db_root/protocols/r1/subjects/R1001P/experiments/FR1/sessions/0/behavioral/current_processed/task_events.json'
    e_reader = BaseEventReader(filename=e_path)
    events = e_reader.read()

    from ptsa.data.readers import EEGReader

    eeg_reader = EEGReader(events=events, channels=np.array(['006']), start_time = 0., end_time=1.6, buffer_time=1.0)
    base_eeg = eeg_reader.read()
    print base_eeg

    #
    # from ptsa.data.MatlabIO import *
    #
    # # d = deserialize_objects_from_matlab_format('/Volumes/rhino_root/home2/yezzyat/R1108J_1_sess2_rawEEG_chans1_2.mat', 'ye')
    # d = deserialize_objects_from_matlab_format('/Volumes/rhino_root/home2/yezzyat/R1060M_FR1_sess0_rawEEG_chans2_3.mat', 'ye')
    #
    # print d
    #
    # from BaseEventReader import BaseEventReader
    # # e_path = join('/Volumes/rhino_root', 'data/events/RAM_FR1/R1060M_math.mat')
    # e_path = '/Volumes/rhino_root/data/events/RAM_PS/R1108J_1_events.mat'
    # e_path = '/Volumes/rhino_root/data/events/RAM_PS/R1108J_1_events.mat'
Ejemplo n.º 32
0
    def compute_powers(self, events, sessions,monopolar_channels , bipolar_pairs ):
        n_freqs = len(self.params.freqs)
        n_bps = len(bipolar_pairs)

        self.pow_mat = None

        pow_ev = None
        winsize = bufsize = None
        for sess in sessions:
            sess_events = events[events.session == sess]
            n_events = len(sess_events)

            print 'Loading EEG for', n_events, 'events of session', sess

            # eegs = Events(sess_events).get_data(channels=channels, start_time=self.params.fr1_start_time, end_time=self.params.fr1_end_time,
            #                             buffer_time=self.params.fr1_buf, eoffset='eegoffset', keep_buffer=True, eoffset_in_time=False)

            # from ptsa.data.readers import TimeSeriesEEGReader
            # time_series_reader = TimeSeriesEEGReader(events=sess_events, start_time=self.params.fr1_start_time,
            #                                  end_time=self.params.fr1_end_time, buffer_time=self.params.fr1_buf, keep_buffer=True)
            #
            # eegs = time_series_reader.read(monopolar_channels)

            # VERSION 2/22/2016
            # eeg_reader = EEGReader(events=sess_events, channels=monopolar_channels,
            #                        start_time=self.params.fr1_start_time,
            #                        end_time=self.params.fr1_end_time, buffer_time=self.params.fr1_buf)

            # VERSION WITH MIRRORING
            eeg_reader = EEGReader(events=sess_events, channels=monopolar_channels,
                                   start_time=self.params.fr1_start_time,
                                   end_time=self.params.fr1_end_time, buffer_time=0.0)


            eegs = eeg_reader.read()
            if eeg_reader.removed_bad_data():
                print 'REMOVED SOME BAD EVENTS !!!'
                sess_events = eegs['events'].values.view(np.recarray)
                n_events = len(sess_events)
                events = np.hstack((events[events.session!=sess],sess_events)).view(np.recarray)
                ev_order = np.argsort(events, order=('session','list','mstime'))
                events = events[ev_order]
                self.pass_object(self.pipeline.task+'_events', events)


            # mirroring
            #eegs[...,:1365] = eegs[...,2730:1365:-1]
            #eegs[...,2731:4096] = eegs[...,2729:1364:-1]

            eegs = eegs.add_mirror_buffer(duration=self.params.fr1_buf)


            if self.samplerate is None:
                self.samplerate = float(eegs.samplerate)
                winsize = int(round(self.samplerate*(self.params.fr1_end_time-self.params.fr1_start_time+2*self.params.fr1_buf)))
                bufsize = int(round(self.samplerate*self.params.fr1_buf))
                print 'samplerate =', self.samplerate, 'winsize =', winsize, 'bufsize =', bufsize
                pow_ev = np.empty(shape=n_freqs*winsize, dtype=float)
                self.wavelet_transform.init(self.params.width, self.params.freqs[0], self.params.freqs[-1], n_freqs, self.samplerate, winsize)

            print 'Computing FR1 powers'

            sess_pow_mat = np.empty(shape=(n_events, n_bps, n_freqs), dtype=np.float)

            #monopolar_channels_np = np.array(monopolar_channels)
            for i,ti in enumerate(bipolar_pairs):
                # print bp
                # print monopolar_channels

                # print np.where(monopolar_channels == bp[0])
                # print np.where(monopolar_channels == bp[1])
                bp = ti['channel_str']
                print 'Computing powers for bipolar pair', bp
                elec1 = np.where(monopolar_channels == bp[0])[0][0]
                elec2 = np.where(monopolar_channels == bp[1])[0][0]
                # print 'elec1=',elec1
                # print 'elec2=',elec2
                # eegs_elec1 = eegs[elec1]
                # eegs_elec2 = eegs[elec2]
                # print 'eegs_elec1=',eegs_elec1
                # print 'eegs_elec2=',eegs_elec2
                # eegs_elec1.reset_coords('channels')
                # eegs_elec2.reset_coords('channels')

                bp_data = eegs[elec1] - eegs[elec2]
                bp_data.attrs['samplerate'] = self.samplerate

                # bp_data = eegs[elec1] - eegs[elec2]
                # bp_data = eegs[elec1] - eegs[elec2]
                # bp_data = eegs.values[elec1] - eegs.values[elec2]

                bp_data = bp_data.filtered([58,62], filt_type='stop', order=self.params.filt_order)
                for ev in xrange(n_events):
                    self.wavelet_transform.multiphasevec(bp_data[ev][0:winsize], pow_ev)
                    #if np.min(pow_ev) < 0.0:
                    #    print ev, events[ev]
                    #    joblib.dump(bp_data[ev], 'bad_bp_ev%d'%ev)
                    #    joblib.dump(eegs[elec1][ev], 'bad_elec1_ev%d'%ev)
                    #    joblib.dump(eegs[elec2][ev], 'bad_elec2_ev%d'%ev)
                    #    print 'Negative powers detected'
                    #    import sys
                    #    sys.exit(1)
                    pow_ev_stripped = np.reshape(pow_ev, (n_freqs,winsize))[:,bufsize:winsize-bufsize]
                    if self.params.log_powers:
                        np.log10(pow_ev_stripped, out=pow_ev_stripped)
                    sess_pow_mat[ev,i,:] = np.nanmean(pow_ev_stripped, axis=1)

            self.pow_mat = np.concatenate((self.pow_mat,sess_pow_mat), axis=0) if self.pow_mat is not None else sess_pow_mat

        self.pow_mat = np.reshape(self.pow_mat, (len(events), n_bps*n_freqs))
Ejemplo n.º 33
0
    tal_path = '/Volumes/rhino_root/data/eeg/R1108J_1/tal/R1108J_1_talLocs_database_bipol.mat'
    tal_reader = TalReader(filename=tal_path)
    monopolar_channels = tal_reader.get_monopolar_channels()
    bipolar_pairs = tal_reader.get_bipolar_pairs()

    # ---------------- NEW STYLE PTSA -------------------
    base_e_reader = BaseEventReader(filename=e_path, eliminate_events_with_no_eeg=True)

    base_events = base_e_reader.read()

    base_events = base_events[(base_events.type == 'STIMULATING') ]
    base_events = base_events[base_events.session == 2]


    from ptsa.data.readers.EEGReader import EEGReader
    eeg_reader = EEGReader(events=base_events, channels=monopolar_channels[0:3],
                           start_time=-1.1, end_time=-0.1, buffer_time=1.0)

    base_eegs = eeg_reader.read()

    bw_base_eegs = base_eegs.filtered(freq_range=[58.,62.], filt_type='stop', order=4)


    b, a = butter_bandpass(58.,62.,float(base_eegs['samplerate']), 4)
    y_in = base_eegs[0,0,:].data


    y_out = butter_bandpass_filter(y_in, 58.,62.,float(base_eegs['samplerate']), 4)


    import matplotlib;
    matplotlib.use('Qt4Agg')
Ejemplo n.º 34
0
    def compute_ps_powers(self, events, sessions, monopolar_channels,
                          bipolar_pairs, experiment):
        n_freqs = len(self.params.freqs)
        n_bps = len(bipolar_pairs)

        pow_mat_pre = pow_mat_post = None

        pow_ev = None
        samplerate = winsize = bufsize = None

        monopolar_channels_list = list(monopolar_channels)
        for sess in sessions:
            sess_events = events[events.session == sess]
            # print type(sess_events)

            n_events = len(sess_events)

            print 'Loading EEG for', n_events, 'events of session', sess

            pre_start_time = self.params.ps_start_time - self.params.ps_offset
            pre_end_time = self.params.ps_end_time - self.params.ps_offset

            # eegs_pre = Events(sess_events).get_data(channels=channels, start_time=pre_start_time, end_time=pre_end_time,
            #             buffer_time=self.params.ps_buf, eoffset='eegoffset', keep_buffer=True, eoffset_in_time=False)

            eeg_pre_reader = EEGReader(
                events=sess_events,
                channels=np.array(monopolar_channels_list),
                start_time=pre_start_time,
                end_time=pre_end_time,
                buffer_time=self.params.ps_buf)

            eegs_pre = eeg_pre_reader.read()

            if samplerate is None:
                # samplerate = round(eegs_pre.samplerate)
                # samplerate = eegs_pre.attrs['samplerate']

                samplerate = float(eegs_pre['samplerate'])

                winsize = int(
                    round(samplerate * (pre_end_time - pre_start_time +
                                        2 * self.params.ps_buf)))
                bufsize = int(round(samplerate * self.params.ps_buf))
                print 'samplerate =', samplerate, 'winsize =', winsize, 'bufsize =', bufsize
                pow_ev = np.empty(shape=n_freqs * winsize, dtype=float)
                self.wavelet_transform.init(self.params.width,
                                            self.params.freqs[0],
                                            self.params.freqs[-1], n_freqs,
                                            samplerate, winsize)

            # mirroring
            nb_ = int(round(samplerate * (self.params.ps_buf)))
            eegs_pre[..., -nb_:] = eegs_pre[..., -nb_ - 2:-2 * nb_ - 2:-1]

            dim3_pre = eegs_pre.shape[
                2]  # because post-stim time inreval does not align for all stim events (stims have different duration)
            # we have to take care of aligning eegs_post ourselves time dim to dim3

            # eegs_post = np.zeros_like(eegs_pre)

            from ptsa.data.TimeSeriesX import TimeSeriesX
            eegs_post = TimeSeriesX(np.zeros_like(eegs_pre),
                                    dims=eegs_pre.dims,
                                    coords=eegs_pre.coords)

            post_start_time = self.params.ps_offset
            post_end_time = self.params.ps_offset + (self.params.ps_end_time -
                                                     self.params.ps_start_time)
            for i_ev in xrange(n_events):
                ev_offset = sess_events[i_ev].pulse_duration
                if ev_offset > 0:
                    if experiment == 'PS3' and sess_events[i_ev].nBursts > 0:
                        ev_offset *= sess_events[i_ev].nBursts + 1
                    ev_offset *= 0.001
                else:
                    ev_offset = 0.0

                # eeg_post = Events(sess_events[i_ev:i_ev+1]).get_data(channels=channels, start_time=post_start_time+ev_offset,
                #             end_time=post_end_time+ev_offset, buffer_time=self.params.ps_buf,
                #             eoffset='eegoffset', keep_buffer=True, eoffset_in_time=False)

                eeg_post_reader = EEGReader(
                    events=sess_events[i_ev:i_ev + 1],
                    channels=np.array(monopolar_channels_list),
                    start_time=post_start_time + ev_offset,
                    end_time=post_end_time + ev_offset,
                    buffer_time=self.params.ps_buf)

                eeg_post = eeg_post_reader.read()

                dim3_post = eeg_post.shape[2]
                # here we take care of possible mismatch of time dim length
                if dim3_pre == dim3_post:
                    eegs_post[:, i_ev:i_ev + 1, :] = eeg_post
                elif dim3_pre < dim3_post:
                    eegs_post[:, i_ev:i_ev + 1, :] = eeg_post[:, :, :-1]
                else:
                    eegs_post[:, i_ev:i_ev + 1, :-1] = eeg_post

            # mirroring
            eegs_post[..., :nb_] = eegs_post[..., 2 * nb_:nb_:-1]

            print 'Computing', experiment, 'powers'

            sess_pow_mat_pre = np.empty(shape=(n_events, n_bps, n_freqs),
                                        dtype=np.float)
            sess_pow_mat_post = np.empty_like(sess_pow_mat_pre)

            for i, ti in enumerate(bipolar_pairs):
                bp = ti['channel_str']
                print 'Computing powers for bipolar pair', bp
                elec1 = np.where(monopolar_channels == bp[0])[0][0]
                elec2 = np.where(monopolar_channels == bp[1])[0][0]

                #
                # for i,ti in enumerate(tal_info):
                #     bp = ti['channel_str']
                #     print 'Computing powers for bipolar pair', bp
                #     elec1 = np.where(channels == bp[0])[0][0]
                #     elec2 = np.where(channels == bp[1])[0][0]

                bp_data_pre = eegs_pre[elec1] - eegs_pre[elec2]
                # bp_data_pre.attrs['samplerate'] = samplerate

                bp_data_pre = bp_data_pre.filtered(
                    [58, 62], filt_type='stop', order=self.params.filt_order)
                for ev in xrange(n_events):
                    #pow_pre_ev = phase_pow_multi(self.params.freqs, bp_data_pre[ev], to_return='power')
                    self.wavelet_transform.multiphasevec(
                        bp_data_pre[ev][0:winsize], pow_ev)
                    #sess_pow_mat_pre[ev,i,:] = np.mean(pow_pre_ev[:,nb_:-nb_], axis=1)
                    pow_ev_stripped = np.reshape(
                        pow_ev, (n_freqs, winsize))[:,
                                                    bufsize:winsize - bufsize]
                    if self.params.log_powers:
                        np.log10(pow_ev_stripped, out=pow_ev_stripped)
                    sess_pow_mat_pre[ev, i, :] = np.nanmean(pow_ev_stripped,
                                                            axis=1)

                bp_data_post = eegs_post[elec1] - eegs_post[elec2]
                # bp_data_post.attrs['samplerate'] = samplerate

                bp_data_post = bp_data_post.filtered(
                    [58, 62], filt_type='stop', order=self.params.filt_order)
                for ev in xrange(n_events):
                    #pow_post_ev = phase_pow_multi(self.params.freqs, bp_data_post[ev], to_return='power')
                    self.wavelet_transform.multiphasevec(
                        bp_data_post[ev][0:winsize], pow_ev)
                    #sess_pow_mat_post[ev,i,:] = np.mean(pow_post_ev[:,nb_:-nb_], axis=1)
                    pow_ev_stripped = np.reshape(
                        pow_ev, (n_freqs, winsize))[:,
                                                    bufsize:winsize - bufsize]
                    if self.params.log_powers:
                        np.log10(pow_ev_stripped, out=pow_ev_stripped)
                    sess_pow_mat_post[ev, i, :] = np.nanmean(pow_ev_stripped,
                                                             axis=1)

            sess_pow_mat_pre = sess_pow_mat_pre.reshape(
                (n_events, n_bps * n_freqs))
            #sess_pow_mat_pre = zscore(sess_pow_mat_pre, axis=0, ddof=1)

            sess_pow_mat_post = sess_pow_mat_post.reshape(
                (n_events, n_bps * n_freqs))
            #sess_pow_mat_post = zscore(sess_pow_mat_post, axis=0, ddof=1)

            sess_pow_mat_joint = zscore(np.vstack(
                (sess_pow_mat_pre, sess_pow_mat_post)),
                                        axis=0,
                                        ddof=1)
            sess_pow_mat_pre = sess_pow_mat_joint[:n_events, ...]
            sess_pow_mat_post = sess_pow_mat_joint[n_events:, ...]

            pow_mat_pre = np.vstack(
                (pow_mat_pre, sess_pow_mat_pre
                 )) if pow_mat_pre is not None else sess_pow_mat_pre
            pow_mat_post = np.vstack(
                (pow_mat_post, sess_pow_mat_post
                 )) if pow_mat_post is not None else sess_pow_mat_post

        return pow_mat_pre, pow_mat_post
Ejemplo n.º 35
0
    def compute_powers(self, events, sessions, monopolar_channels,
                       bipolar_pairs):
        n_freqs = len(self.params.freqs)
        n_bps = len(bipolar_pairs)

        self.pow_mat = None

        pow_ev = None
        winsize = bufsize = None
        for sess in sessions:
            sess_events = events[events.session == sess]
            n_events = len(sess_events)

            print 'Loading EEG for', n_events, 'events of session', sess

            # eegs = Events(sess_events).get_data(channels=channels, start_time=self.params.fr1_start_time, end_time=self.params.fr1_end_time,
            #                             buffer_time=self.params.fr1_buf, eoffset='eegoffset', keep_buffer=True, eoffset_in_time=False)

            # from ptsa.data.readers import TimeSeriesEEGReader
            # time_series_reader = TimeSeriesEEGReader(events=sess_events, start_time=self.params.fr1_start_time,
            #                                  end_time=self.params.fr1_end_time, buffer_time=self.params.fr1_buf, keep_buffer=True)
            #
            # eegs = time_series_reader.read(monopolar_channels)

            eeg_reader = EEGReader(events=sess_events,
                                   channels=monopolar_channels,
                                   start_time=self.params.fr1_start_time,
                                   end_time=self.params.fr1_end_time,
                                   buffer_time=self.params.fr1_buf)

            eegs = eeg_reader.read()

            # print 'eegs=',eegs.values[0,0,:2],eegs.values[0,0,-2:]
            # sys.exit()
            #
            # a = eegs[0]-eegs[1]

            # mirroring
            #eegs[...,:1365] = eegs[...,2730:1365:-1]
            #eegs[...,2731:4096] = eegs[...,2729:1364:-1]

            if self.samplerate is None:
                self.samplerate = float(eegs.samplerate)
                winsize = int(
                    round(
                        self.samplerate *
                        (self.params.fr1_end_time - self.params.fr1_start_time
                         + 2 * self.params.fr1_buf)))
                bufsize = int(round(self.samplerate * self.params.fr1_buf))
                print 'samplerate =', self.samplerate, 'winsize =', winsize, 'bufsize =', bufsize
                pow_ev = np.empty(shape=n_freqs * winsize, dtype=float)
                self.wavelet_transform.init(self.params.width,
                                            self.params.freqs[0],
                                            self.params.freqs[-1], n_freqs,
                                            self.samplerate, winsize)

            print 'Computing FR1 powers'

            sess_pow_mat = np.empty(shape=(n_events, n_bps, n_freqs),
                                    dtype=np.float)

            #monopolar_channels_np = np.array(monopolar_channels)
            for i, ti in enumerate(bipolar_pairs):
                # print bp
                # print monopolar_channels

                # print np.where(monopolar_channels == bp[0])
                # print np.where(monopolar_channels == bp[1])
                bp = ti['channel_str']
                print 'Computing powers for bipolar pair', bp
                elec1 = np.where(monopolar_channels == bp[0])[0][0]
                elec2 = np.where(monopolar_channels == bp[1])[0][0]
                # print 'elec1=',elec1
                # print 'elec2=',elec2
                # eegs_elec1 = eegs[elec1]
                # eegs_elec2 = eegs[elec2]
                # print 'eegs_elec1=',eegs_elec1
                # print 'eegs_elec2=',eegs_elec2
                # eegs_elec1.reset_coords('channels')
                # eegs_elec2.reset_coords('channels')

                bp_data = eegs[elec1] - eegs[elec2]
                bp_data.attrs['samplerate'] = self.samplerate

                # bp_data = eegs[elec1] - eegs[elec2]
                # bp_data = eegs[elec1] - eegs[elec2]
                # bp_data = eegs.values[elec1] - eegs.values[elec2]

                bp_data = bp_data.filtered([58, 62],
                                           filt_type='stop',
                                           order=self.params.filt_order)
                for ev in xrange(n_events):
                    self.wavelet_transform.multiphasevec(
                        bp_data[ev][0:winsize], pow_ev)
                    #if np.min(pow_ev) < 0.0:
                    #    print ev, events[ev]
                    #    joblib.dump(bp_data[ev], 'bad_bp_ev%d'%ev)
                    #    joblib.dump(eegs[elec1][ev], 'bad_elec1_ev%d'%ev)
                    #    joblib.dump(eegs[elec2][ev], 'bad_elec2_ev%d'%ev)
                    #    print 'Negative powers detected'
                    #    import sys
                    #    sys.exit(1)
                    pow_ev_stripped = np.reshape(
                        pow_ev, (n_freqs, winsize))[:,
                                                    bufsize:winsize - bufsize]
                    if self.params.log_powers:
                        np.log10(pow_ev_stripped, out=pow_ev_stripped)
                    sess_pow_mat[ev, i, :] = np.nanmean(pow_ev_stripped,
                                                        axis=1)

            self.pow_mat = np.concatenate(
                (self.pow_mat, sess_pow_mat),
                axis=0) if self.pow_mat is not None else sess_pow_mat

        self.pow_mat = np.reshape(self.pow_mat, (len(events), n_bps * n_freqs))