def test_2_old():
    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(event_file=e_path, eliminate_events_with_no_eeg=True, use_ptsa_events_class=False)

    base_e_reader.read()

    base_events = base_e_reader.get_output()

    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(tal_filename=tal_path)
    monopolar_channels = tal_reader.get_monopolar_channels()
    bipolar_pairs = tal_reader.get_bipolar_pairs()

    print 'bipolar_pairs=', bipolar_pairs

    from ptsa.data.experimental.TimeSeriesEEGReader import TimeSeriesEEGReader

    time_series_reader = TimeSeriesEEGReader(events=base_events, start_time=0.0,
                                             end_time=1.6, buffer_time=1.0, keep_buffer=True)

    base_eegs = time_series_reader.read(channels=monopolar_channels)

    # base_eegs = base_eegs[:, 0:10, :]
    # bipolar_pairs = bipolar_pairs[0:10]


    wf = MorletWaveletFilter(time_series=base_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 'total time = ', time.time() - start

    res_start = time.time()

    # from ptsa.data.filters.ResampleFilter import ResampleFilter
    # rsf = ResampleFilter (resamplerate=50.0)
    # rsf.set_input(pow_wavelet)
    # pow_wavelet = rsf.filter()



    print 'resample_time=', time.time() - res_start
    return pow_wavelet
    def run(self):
        events = None
        if self.params.include_fr1:
            try:
                e_path = os.path.join(self.pipeline.mount_point , 'data/events/RAM_FR1', self.pipeline.subject + '_math.mat')
                e_reader = BaseEventReader(filename=e_path, eliminate_events_with_no_eeg=True)
                events = e_reader.read()
                print "Got FR1 events"
            except IOError:
                pass

        if self.params.include_catfr1:
            try:
                e_path = os.path.join(self.pipeline.mount_point , 'data/events/RAM_CatFR1', self.pipeline.subject + '_math.mat')
                e_reader = BaseEventReader(filename=e_path, eliminate_events_with_no_eeg=True)
                catfr1_events = e_reader.read()
                print "Got CatFR1 events"
                if events is None:
                    events = catfr1_events
                else:
                    print "Joining FR1 and CatFR1"
                    catfr1_events.session = -catfr1_events.session-1
                    fields = list(set(events.dtype.names).intersection(catfr1_events.dtype.names))
                    events = np.hstack((events[fields],catfr1_events[fields])).view(np.recarray)
            except IOError:
                pass

        events = events[events.type == 'PROB']

        print len(events), 'PROB events'

        self.pass_object('control_events', events)
    def run(self):
        events = None
        if self.params.include_fr1:
            try:
                e_path = os.path.join(self.pipeline.mount_point , 'data/events/RAM_FR1', self.pipeline.subject + '_events.mat')
                e_reader = BaseEventReader(filename=e_path, eliminate_events_with_no_eeg=True)
                events = e_reader.read()
                ev_order = np.argsort(events, order=('session','list','mstime'))
                events = events[ev_order]
            except IOError:
                pass

        if self.params.include_catfr1:
            try:
                e_path = os.path.join(self.pipeline.mount_point , 'data/events/RAM_CatFR1', self.pipeline.subject + '_events.mat')
                e_reader = BaseEventReader(filename=e_path, eliminate_events_with_no_eeg=True)
                catfr1_events = e_reader.read()
                ev_order = np.argsort(catfr1_events, order=('session','list','mstime'))
                catfr1_events = catfr1_events[ev_order]
                if events is None:
                    events = catfr1_events
                else:
                    catfr1_events.session += 100
                    fields = list(set(events.dtype.names).intersection(catfr1_events.dtype.names))
                    events = np.hstack((events[fields],catfr1_events[fields])).view(np.recarray)
            except IOError:
                pass

        events = events[events.type == 'WORD']

        print len(events), 'WORD events'

        self.pass_object('FR_events', events)
def test_2():
    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(event_file=e_path,
                                    eliminate_events_with_no_eeg=True,
                                    use_ptsa_events_class=False)

    base_e_reader.read()

    base_events = base_e_reader.get_output()

    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(tal_filename=tal_path)
    monopolar_channels = tal_reader.get_monopolar_channels()
    bipolar_pairs = tal_reader.get_bipolar_pairs()

    print 'bipolar_pairs=', bipolar_pairs

    from ptsa.data.experimental.TimeSeriesEEGReader import TimeSeriesEEGReader

    time_series_reader = TimeSeriesEEGReader(events=base_events,
                                             start_time=0.0,
                                             end_time=1.6,
                                             buffer_time=1.0,
                                             keep_buffer=True)

    base_eegs = time_series_reader.read(channels=monopolar_channels)

    # base_eegs = base_eegs[:, 0:10, :]
    # bipolar_pairs = bipolar_pairs[0:10]

    wf = MorletWaveletFilter(
        time_series=base_eegs,
        # bipolar_pairs=bipolar_pairs,
        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 'total time = ', time.time() - start

    return pow_wavelet
Exemple #5
0
 def read_base_events(self):
     base_e_reader = BaseEventReader(event_file=self.e_path,
                                     eliminate_events_with_no_eeg=True,
                                     use_ptsa_events_class=False)
     base_e_reader.read()
     base_events = base_e_reader.get_output()
     base_events = base_events[base_events.type == 'WORD']
     base_ev_order = np.argsort(base_events,
                                order=('session', 'list', 'mstime'))
     base_events = base_events[base_ev_order]
     base_events = base_events[self.event_range]
     return base_events
Exemple #6
0
    def read_events(self, task, subject):
        e_path = join(self.prefix, 'data/events/%s/%s_events.mat' % (task, subject))

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

        base_events = base_e_reader.read()
        return base_events
def get_ltp_subject_sessions_by_experiment(experiment):
    events_dir = os.path.join(paths.data_root, 'scalp', 'ltp', experiment, 'behavioral', 'events')
    events_files = sorted(glob.glob(os.path.join(events_dir, 'events_all_LTP*.mat')),
                          key=lambda f: f.split('_')[:-1])
    seen_experiments = defaultdict(list)
    for events_file in events_files:
        print(events_file)
        subject = os.path.basename(events_file)[11:-4]  # Subject number is the basename with events_all_, .mat removed
        subject_no_year = subject.split('_')[0]
        if '_' in subject:
            continue
        mat_events_reader = BaseEventReader(filename=events_file, common_root=paths.data_root)
        logger.debug('Loading matlab events {exp}: {subj}'.format(exp=experiment, subj=subject))
        try:
            mat_events = mat_events_reader.read()
            sessions = np.unique(mat_events['session']) - 1  # MATLAB events start counting sessions at 1 instead of 0
            version = 0.
            for i, session in enumerate(sessions):
                if 'experiment' in mat_events.dtype.names:
                    experiments = np.unique(mat_events[mat_events['session'] == session]['experiment'])
                else:
                    experiments = [experiment]
                for this_experiment in experiments:
                    n_sessions = seen_experiments[subject_no_year].count(this_experiment)
                    yield subject_no_year, subject, n_sessions, session, this_experiment, version
                    seen_experiments[subject_no_year].append(this_experiment)
        except IndexError or AttributeError:
            traceback.print_exc()
            logger.error('Could not get session from {}'.format(events_file))
Exemple #8
0
 def test_missing_data_read(self):
     self.e_path = '/Volumes/rhino_root/data/events/RAM_PS/R1104D_events.mat'
     if sys.platform.startswith('win'):
         self.e_path = 'D:/data/events/RAM_PS/R1104D_events.mat'
     base_e_reader = BaseEventReader(filename=self.e_path)
     base_events = base_e_reader.read()
     print('base_events=', base_events)
Exemple #9
0
 def test_missing_data_read(self):
     self.e_path = '/Volumes/rhino_root/data/events/RAM_PS/R1104D_events.mat'
     if sys.platform.startswith('win'):
         self.e_path = 'D:/data/events/RAM_PS/R1104D_events.mat'
     base_e_reader = BaseEventReader(filename=self.e_path)
     base_events = base_e_reader.read()
     print('base_events=',base_events)
Exemple #10
0
    def setUp(self):
        self.e_path = '/Users/m/data/events/RAM_FR1/R1060M_events.mat'
        tal_path = '/Users/m/data/eeg/R1060M/tal/R1060M_talLocs_database_bipol.mat'

        # ---------------- NEW STYLE PTSA -------------------
        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()
Exemple #11
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()
Exemple #12
0
    def run(self):
        task = self.pipeline.task

        e_path = os.path.join(self.pipeline.mount_point, 'data', 'events',
                              task, self.pipeline.subject + '_events.mat')
        e_reader = BaseEventReader(filename=e_path,
                                   eliminate_events_with_no_eeg=True)

        events = e_reader.read()
        ev_order = np.argsort(events, order=('session', 'list', 'mstime'))
        events = events[ev_order]

        self.pass_object(self.pipeline.task + '_all_events', events)

        intr_events = events[(events.intrusion != -999)
                             & (events.intrusion != 0)]

        rec_events = events[events.type == 'REC_WORD']

        events = events[events.type == 'WORD']

        print len(events), task, 'WORD events'

        self.pass_object(task + '_events', events)
        self.pass_object(self.pipeline.task + '_intr_events', intr_events)
        self.pass_object(self.pipeline.task + '_rec_events', rec_events)
Exemple #13
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()
def get_word_events(sub, task, montage=0):
    from ptsa.data.readers import BaseEventReader
    from ptsa.data.events import Events
    from ptsa.data.readers.IndexReader import JsonIndexReader
    reader = JsonIndexReader('/protocols/r1.json')

    #Get events
    evfiles = list(
        reader.aggregate_values('task_events',
                                subject=sub,
                                experiment=task,
                                montage=montage))

    evs_on = np.array([])
    for ef in evfiles:
        try:
            base_e_reader = BaseEventReader(filename=ef,
                                            eliminate_events_with_no_eeg=True)
            base_events = base_e_reader.read()
            if len(evs_on) == 0:
                evs_on = base_events[base_events.type == 'WORD']
            else:
                evs_on = np.concatenate(
                    (evs_on, base_events[base_events.type == 'WORD']), axis=0)
        except:
            continue
    evs = Events(evs_on)

    return evs
    def read_base_events(self):

        base_e_reader = BaseEventReader(event_file=self.e_path, eliminate_events_with_no_eeg=True, use_ptsa_events_class=False)


        base_e_reader.read()

        base_events = base_e_reader.get_output()

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

        base_ev_order = np.argsort(base_events, order=('session','list','mstime'))
        base_events = base_events[base_ev_order]

        base_events = base_events[self.event_range]

        return base_events
Exemple #16
0
def get_events():
    # ---------------- 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 == 'WORD']
    return base_events
def get_stim_events(sub, task, montage=0):

    from ptsa.data.readers import BaseEventReader
    from ptsa.data.events import Events
    from ptsa.data.readers.IndexReader import JsonIndexReader
    reader = JsonIndexReader('/protocols/r1.json')

    #Get events
    evfiles = list(
        reader.aggregate_values(
            'task_events', subject=sub, experiment=task,
            montage=montage))  #This is supposed to work but often does not
    if len(evfiles) < 1:
        from glob import glob
        sessions = [
            s.split('/')[-1]
            for s in glob('/protocols/r1/subjects/' + sub + '/experiments/' +
                          task + '/sessions/*')
        ]
        evfiles = []
        for sess in sessions:
            evfiles.append('/protocols/r1/subjects/' + sub + '/experiments/' +
                           task + '/sessions/' + sess +
                           '/behavioral/current_processed/task_events.json')

    evs_on = np.array([])
    for ef in evfiles:
        try:
            base_e_reader = BaseEventReader(filename=ef,
                                            eliminate_events_with_no_eeg=True)
            base_events = base_e_reader.read()
            if len(evs_on) == 0:
                evs_on = base_events[base_events.type == 'STIM_ON']
            else:
                evs_on = np.concatenate(
                    (evs_on, base_events[base_events.type == 'STIM_ON']),
                    axis=0)
        except:
            continue
    evs_on = Events(evs_on)

    #Reorganize sessions around stim sites
    sessions = reader.sessions(subject=sub, experiment=task, montage=montage)
    sess_tags = []
    for i in range(len(evs_on)):
        stimtag = str(evs_on[i]['stim_params']['anode_number']) + '-' + str(
            evs_on[i]['stim_params']['cathode_number'])
        sess_tags.append(stimtag)
    sess_tags = np.array(sess_tags)
    if len(np.unique(sess_tags)) <= len(sessions):
        session_array = evs_on['session']
    else:
        session_array = np.empty(len(evs_on))
        for idx, s in enumerate(np.unique(sess_tags)):
            session_array[sess_tags == s] = int(idx)
    sessions = np.unique(session_array)

    return evs_on, session_array
Exemple #18
0
    def run(self):
        events = None
        if self.params.include_fr1:
            try:
                e_path = os.path.join(self.pipeline.mount_point,
                                      'data/events/RAM_FR1',
                                      self.pipeline.subject + '_events.mat')
                e_reader = BaseEventReader(filename=e_path,
                                           eliminate_events_with_no_eeg=True)
                events = e_reader.read()
                ev_order = np.argsort(events,
                                      order=('session', 'list', 'mstime'))
                events = events[ev_order]
            except IOError:
                pass

        if self.params.include_catfr1:
            try:
                e_path = os.path.join(self.pipeline.mount_point,
                                      'data/events/RAM_CatFR1',
                                      self.pipeline.subject + '_events.mat')
                e_reader = BaseEventReader(filename=e_path,
                                           eliminate_events_with_no_eeg=True)
                catfr1_events = e_reader.read()
                ev_order = np.argsort(catfr1_events,
                                      order=('session', 'list', 'mstime'))
                catfr1_events = catfr1_events[ev_order]
                if events is None:
                    events = catfr1_events
                else:
                    catfr1_events.session += 100
                    fields = list(
                        set(events.dtype.names).intersection(
                            catfr1_events.dtype.names))
                    events = np.hstack(
                        (events[fields],
                         catfr1_events[fields])).view(np.recarray)
            except IOError:
                pass

        events = events[events.type == 'WORD']

        print len(events), 'WORD events'

        self.pass_object('FR_events', events)
Exemple #19
0
    def setUp(self):
        self.event_range = range(0, 30, 1)
        self.e_path = '/Users/m/data/events/RAM_FR1/R1060M_events.mat'
        self.tal_path = '/Users/m/data/eeg/R1060M/tal/R1060M_talLocs_database_bipol.mat'

        if sys.platform.startswith('win'):
            self.e_path = 'D:/data/events/RAM_FR1/R1060M_events.mat'
            self.tal_path = 'D:/data/eeg/R1060M/tal/R1060M_talLocs_database_bipol.mat'

        # --------------- TAL STRUCTS READ
        tal_reader = TalReader(filename=self.tal_path)
        self.monopolar_channels = tal_reader.get_monopolar_channels()


        # ---------------- ORIG PTSA -------------------
        e_reader = PTSAEventReader(filename=self.e_path, eliminate_events_with_no_eeg=True)
        events = e_reader.read()

        events = events[events.type == 'WORD']

        events = events[self.event_range]

        ev_order = np.argsort(events, order=('session','list','mstime'))
        self.events = events[ev_order]

        # self.events = self.read_ptsa_events()

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

        start_time = 0.0
        end_time = 1.6
        buffer_time = 1.0

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

        # ---------------- NEW STYLE PTSA -------------------
        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'))
        base_events = base_events[base_ev_order]

        self.base_events = base_events[self.event_range]

        # self.base_events = self.read_base_events()

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

        self.base_eegs = eeg_reader.read()
def main_fcn():
    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)

    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()

    print('bipolar_pairs=', bipolar_pairs)


    from ptsa.data.readers.EEGReader import EEGReader

    sessions = np.unique(base_events.session)
    dataroot = base_events[0].eegfile

    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()





    time_series_reader = EEGReader(events=base_events, channels=monopolar_channels, start_time=0.0,
                                             end_time=1.6, buffer_time=1.0)


    base_eegs = time_series_reader.read()


    m2b = MonopolarToBipolarMapper(time_series=base_eegs, bipolar_pairs=bipolar_pairs)
    ts_filtered = m2b.filter()

    del base_eegs
    del time_series_reader

    print()

    pass
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
Exemple #22
0
def main_fcn():
    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)

    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()

    print 'bipolar_pairs=', bipolar_pairs

    from ptsa.data.readers.EEGReader import EEGReader

    sessions = np.unique(base_events.session)
    dataroot = base_events[0].eegfile

    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()

    time_series_reader = EEGReader(events=base_events,
                                   channels=monopolar_channels,
                                   start_time=0.0,
                                   end_time=1.6,
                                   buffer_time=1.0)

    base_eegs = time_series_reader.read()

    m2b = MonopolarToBipolarMapper(time_series=base_eegs,
                                   bipolar_pairs=bipolar_pairs)
    ts_filtered = m2b.filter()

    del base_eegs
    del time_series_reader

    print

    pass
def get_suffixes(subject):
    events_file= os.path.join(DATA_ROOT, '..', 'events', 'RAM_PS', '{}_events.mat'.format(subject))
    mat_events_reader = BaseEventReader(filename=events_file, common_root=DATA_ROOT)
    mat_events = mat_events_reader.read()
    sessions = np.unique(mat_events.session)
    suffixes = {}
    for session in sessions:
        sess_events = mat_events[mat_events.session == session]
        eegfile = sess_events[10].eegfile
        suffixes[session] =  '_'.join(eegfile.split('_')[-2:])
    return suffixes
    def setUp(self):
        self.event_range = range(0, 30, 1)
        self.e_path = '/Users/m/data/events/RAM_FR1/R1060M_events.mat'
        self.tal_path = '/Users/m/data/eeg/R1060M/tal/R1060M_talLocs_database_bipol.mat'

        # --------------- TAL STRUCTS READ
        tal_reader = TalReader(filename=self.tal_path)
        self.monopolar_channels = tal_reader.get_monopolar_channels()


        # ---------------- ORIG PTSA -------------------
        e_reader = PTSAEventReader(filename=self.e_path, eliminate_events_with_no_eeg=True)
        events = e_reader.read()

        events = events[events.type == 'WORD']

        events = events[self.event_range]

        ev_order = np.argsort(events, order=('session','list','mstime'))
        self.events = events[ev_order]

        # self.events = self.read_ptsa_events()

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

        start_time = 0.0
        end_time = 1.6
        buffer_time = 1.0

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

        # ---------------- NEW STYLE PTSA -------------------
        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'))
        base_events = base_events[base_ev_order]

        self.base_events = base_events[self.event_range]

        # self.base_events = self.read_base_events()

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

        self.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
Exemple #26
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()
Exemple #27
0
def get_suffixes(subject):
    events_file = os.path.join(DATA_ROOT, '..', 'events', 'RAM_PS',
                               '{}_events.mat'.format(subject))
    mat_events_reader = BaseEventReader(filename=events_file,
                                        common_root=DATA_ROOT)
    mat_events = mat_events_reader.read()
    sessions = np.unique(mat_events.session)
    suffixes = {}
    for session in sessions:
        sess_events = mat_events[mat_events.session == session]
        eegfile = sess_events[10].eegfile
        suffixes[session] = '_'.join(eegfile.split('_')[-2:])
    return suffixes
Exemple #28
0
    def run(self):
        subject = self.pipeline.subject
        experiment = self.pipeline.experiment

        from ptsa.data.readers import BaseEventReader
        e_path = os.path.join(self.pipeline.mount_point , 'data', 'events', 'RAM_PS', self.pipeline.subject + '_events.mat')
        e_reader = BaseEventReader(filename=e_path, eliminate_events_with_no_eeg=True)

        events = e_reader.read()
        ev_order = np.argsort(events, order=('session','mstime'))
        events = events[ev_order]

        # try:
        #     events = Events(get_events(subject=subject, task='RAM_PS', path_prefix=self.pipeline.mount_point))
        # except IOError:
        #     raise Exception('No parameter search for subject %s' % subject)
        #

        events = events[events.experiment == experiment]
        
        if len(events) == 0:
            raise Exception('No %s events for subject %s' % (experiment,subject))

        sessions = np.unique(events.session)
        print experiment, 'sessions:', sessions

        events = pd.DataFrame.from_records(events)

        events = compute_isi(events)

        is_stim_event_type_vec = np.vectorize(is_stim_event_type)
        stim_mask = is_stim_event_type_vec(events.type)
        if experiment == 'PS3':
            stim_inds = np.where(stim_mask)[0]
            stim_events = pd.DataFrame(events[stim_mask])
            last_burst_inds = stim_inds + stim_events['nBursts'].values
            last_bursts = events.ix[last_burst_inds]
            events = stim_events
            events['train_duration'] = last_bursts['mstime'].values - events['mstime'].values + last_bursts['pulse_duration'].values
        else:
            events = events[stim_mask]

        events = events.to_records(index=False)

        print len(events), 'stim', experiment, 'events'

        # events = Events(events.to_records(index=False))
        #
        # joblib.dump(events, self.get_path_to_resource_in_workspace(subject+'-'+experiment+'-ps_events.pkl'))
        self.pass_object(experiment+'_events', events)
    def run(self):
        task = self.pipeline.task

        from ptsa.data.readers import BaseEventReader
        e_path = os.path.join(self.pipeline.mount_point , 'data', 'events', task, self.pipeline.subject + '_events.mat')
        e_reader = BaseEventReader(filename=e_path, eliminate_events_with_no_eeg=True)

        events = e_reader.read()

        events = events[events.type == 'WORD']

        print len(events), task, 'WORD events'

        self.pass_object(task+'_events', events)
    def read_base_events(self):

        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'))
        base_events = base_events[base_ev_order]

        base_events = base_events[self.event_range]

        return base_events
Exemple #31
0
    def test_event_read(self):

        ptsa_reader = PTSAEventReader(filename=self.e_path)

        events = ptsa_reader.read()

        base_event_reader = BaseEventReader(filename=self.e_path)

        base_events = base_event_reader.read()

        for base_event, event in zip(base_events, events):
            self.assertEqual(base_event['item'], event['item'])

        for base_event, event in zip(base_events, events):
            self.assertEqual(base_event.eegoffset, event.eegoffset)
Exemple #32
0
    def read_base_events(self):

        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'))
        base_events = base_events[base_ev_order]

        base_events = base_events[self.event_range]

        return base_events
Exemple #33
0
    def run(self):
        task = self.pipeline.task

        from ptsa.data.readers import BaseEventReader
        e_path = os.path.join(self.pipeline.mount_point, 'data', 'events',
                              task, self.pipeline.subject + '_events.mat')
        e_reader = BaseEventReader(filename=e_path,
                                   eliminate_events_with_no_eeg=True)

        events = e_reader.read()

        events = events[events.type == 'WORD']

        print len(events), task, 'WORD events'

        self.pass_object(task + '_events', events)
Exemple #34
0
    def run(self):
        try:
            e_path = os.path.join(self.pipeline.mount_point, 'data/events', self.pipeline.task, self.pipeline.subject+'_math.mat')
            e_reader = BaseEventReader(filename=e_path, eliminate_events_with_no_eeg=False)

            events = e_reader.read()
            ev_order = np.argsort(events, order=('session','mstime'))
            events = events[ev_order]

            events = events[events.type == 'PROB']

            self.pass_object(self.pipeline.task+'_math_events', events)

        except IOError:

            self.pass_object(self.pipeline.task+'_math_events', None)
Exemple #35
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)
Exemple #36
0
    def read_events_debug(self):

        # event_files = ['/Users/m/data/dboy_ev.mat']
        event_files = [
            '/Volumes/rhino_root/data/eeg/FR438/behavioral/session_0/events.mat'
        ]
        for event_file in event_files:
            e_path = event_file

            base_e_reader = BaseEventReader(filename=e_path,
                                            eliminate_events_with_no_eeg=False,
                                            normalize_eeg_path=False)

            base_events = base_e_reader.read()
            print base_events['store'][447]

            return base_events
    def run(self):
        subject = self.pipeline.subject
        experiment = self.pipeline.experiment

        from ptsa.data.readers import BaseEventReader
        e_path = os.path.join(self.pipeline.mount_point, 'data', 'events',
                              'RAM_PS', self.pipeline.subject + '_events.mat')
        e_reader = BaseEventReader(filename=e_path,
                                   eliminate_events_with_no_eeg=True)

        events = e_reader.read()

        # try:
        #     events = Events(get_events(subject=subject, task='RAM_PS', path_prefix=self.pipeline.mount_point))
        # except IOError:
        #     raise Exception('No parameter search for subject %s' % subject)
        #

        events = events[events.experiment == experiment]

        if len(events) == 0:
            raise Exception('No %s events for subject %s' %
                            (experiment, subject))

        #
        # events = correct_eegfile_field(events)
        # events = self.attach_raw_bin_wrappers(events)

        sessions = np.unique(events.session)
        print experiment, 'sessions:', sessions

        events = pd.DataFrame.from_records(events)

        events = compute_isi(events)

        is_stim_event_type_vec = np.vectorize(is_stim_event_type)
        events = events[is_stim_event_type_vec(events.type)]

        print len(events), 'stim', experiment, 'events'

        events = events.to_records(index=False)
        # events = Events(events.to_records(index=False))
        #
        # joblib.dump(events, self.get_path_to_resource_in_workspace(subject+'-'+experiment+'-ps_events.pkl'))
        self.pass_object(experiment + '_events', events)
Exemple #38
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])
    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()
    def run(self):
        subject = self.pipeline.subject
        experiment = self.pipeline.experiment

        from ptsa.data.readers import BaseEventReader
        e_path = os.path.join(self.pipeline.mount_point , 'data', 'events', 'RAM_PS', self.pipeline.subject + '_events.mat')
        e_reader = BaseEventReader(filename=e_path, eliminate_events_with_no_eeg=True)

        events = e_reader.read()

        # try:
        #     events = Events(get_events(subject=subject, task='RAM_PS', path_prefix=self.pipeline.mount_point))
        # except IOError:
        #     raise Exception('No parameter search for subject %s' % subject)
        #

        events = events[events.experiment == experiment]
        
        if len(events) == 0:
            raise Exception('No %s events for subject %s' % (experiment,subject))

        #
        # events = correct_eegfile_field(events)
        # events = self.attach_raw_bin_wrappers(events)

        sessions = np.unique(events.session)
        print experiment, 'sessions:', sessions

        events = pd.DataFrame.from_records(events)

        events = compute_isi(events)

        is_stim_event_type_vec = np.vectorize(is_stim_event_type)
        events = events[is_stim_event_type_vec(events.type)]

        print len(events), 'stim', experiment, 'events'

        events = events.to_records(index=False)
        # events = Events(events.to_records(index=False))
        #
        # joblib.dump(events, self.get_path_to_resource_in_workspace(subject+'-'+experiment+'-ps_events.pkl'))
        self.pass_object(experiment+'_events', events)
def get_ps4_events(fn):

    from ptsa.data.readers import BaseEventReader
    from ptsa.data.events import Events

    #Get events
    evfiles = [fn]

    evs_on = np.array([])
    for ef in evfiles:
        try:
            base_e_reader = BaseEventReader(filename=ef,
                                            eliminate_events_with_no_eeg=True)
            base_events = base_e_reader.read()
            if len(evs_on) == 0:
                evs_on = base_events[base_events.type == 'STIM_ON']
            else:
                evs_on = np.concatenate(
                    (evs_on, base_events[base_events.type == 'STIM_ON']),
                    axis=0)
        except:
            continue
    evs_on = Events(evs_on)

    #Reorganize sessions around stim sites
    sessions = np.unique(evs_on['session'])
    sess_tags = []
    for i in range(len(evs_on)):
        stimtag = str(evs_on[i]['anode_num']) + '-' + str(
            evs_on[i]['cathode_num'])
        sess_tags.append(stimtag)
    sess_tags = np.array(sess_tags)
    if len(np.unique(sess_tags)) <= len(sessions):
        session_array = evs_on['session']
    else:
        session_array = np.empty(len(evs_on))
        for idx, s in enumerate(np.unique(sess_tags)):
            session_array[sess_tags == s] = int(idx)
    sessions = np.unique(session_array)

    return evs_on, session_array
    def run(self):
        task = self.pipeline.task

        e_path = os.path.join(self.pipeline.mount_point, 'data/events', task, self.pipeline.subject + '_events.mat')
        e_reader = BaseEventReader(filename=e_path, eliminate_events_with_no_eeg=False)

        events = e_reader.read()

        self.pass_object(self.pipeline.task+'_all_events', events)

        intr_events = events[(events.intrusion!=-999) & (events.intrusion!=0)]

        rec_events = events[events.type == 'REC_WORD']

        events = events[events.type == 'WORD']

        print len(events), task, 'WORD events'

        self.pass_object(task+'_events', events)
        self.pass_object(self.pipeline.task+'_intr_events', intr_events)
        self.pass_object(self.pipeline.task+'_rec_events', rec_events)
Exemple #43
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)
        self.e_path = '/Users/m/data/events/RAM_FR1/R1060M_events.mat'

        tal_path = '/Users/m/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()

        # ---------------- NEW STYLE PTSA -------------------
        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]

        # self.base_events = base_events[self.event_range]

        # self.base_events = self.read_base_events()

        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()
    def run(self):
        task = self.pipeline.task

        from ptsa.data.readers import BaseEventReader
        e_path = os.path.join(self.pipeline.mount_point , 'data', 'events', task, self.pipeline.subject + '_math.mat')
        e_reader = BaseEventReader(filename=e_path, eliminate_events_with_no_eeg=True)

        events = e_reader.read()


        # events = Events(get_events(subject=self.pipeline.subject, task=task, path_prefix=self.pipeline.mount_point))

        # events = correct_eegfile_field(events)
        # ev_order = np.argsort(events, order=('session','list','mstime'))
        # events = events[ev_order]
        #
        # events = self.attach_raw_bin_wrappers(events)

        events = events[events.type == 'PROB']

        print len(events), task, 'PROB events'

        self.pass_object(task+'_control_events', events)
    def run(self):
        task = self.pipeline.task

        e_path = os.path.join(self.pipeline.mount_point , 'data', 'events', task, self.pipeline.subject + '_events.mat')
        e_reader = BaseEventReader(filename=e_path, eliminate_events_with_no_eeg=True)

        events = e_reader.read()
        ev_order = np.argsort(events, order=('session','list','mstime'))
        events = events[ev_order]

        self.pass_object(self.pipeline.task+'_all_events', events)

        intr_events = events[(events.intrusion!=-999) & (events.intrusion!=0)]

        rec_events = events[events.type == 'REC_WORD']

        events = events[events.type == 'WORD']

        print len(events), task, 'WORD events'

        self.pass_object(task+'_events', events)
        self.pass_object(self.pipeline.task+'_intr_events', intr_events)
        self.pass_object(self.pipeline.task+'_rec_events', rec_events)
Exemple #46
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)
Exemple #47
0
    def run(self):
        task = self.pipeline.task

        e_path = os.path.join(self.pipeline.mount_point, 'data/events', task,
                              self.pipeline.subject + '_events.mat')
        e_reader = BaseEventReader(filename=e_path,
                                   eliminate_events_with_no_eeg=False)

        events = e_reader.read()

        self.pass_object(self.pipeline.task + '_all_events', events)

        intr_events = events[(events.intrusion != -999)
                             & (events.intrusion != 0)]

        rec_events = events[events.type == 'REC_WORD']

        events = events[events.type == 'WORD']

        print len(events), task, 'WORD events'

        self.pass_object(task + '_events', events)
        self.pass_object(self.pipeline.task + '_intr_events', intr_events)
        self.pass_object(self.pipeline.task + '_rec_events', rec_events)
 def test_missing_data_read(self):
     self.e_path = '/Volumes/rhino_root/data/events/RAM_PS/R1104D_events.mat'
     base_e_reader = BaseEventReader(filename=self.e_path)
     base_events = base_e_reader.read()
     print 'base_events=',base_events
def get_subject_sessions_by_experiment(experiment, protocol='r1', include_montage_changes=False):
    """

    :param experiment:
    :param protocol:
    :param include_montage_changes:
    :return: subject, subject_code,  session, original_session, experiment, version
    """
    json_reader = JsonIndexReader(os.path.join(paths.rhino_root,'protocols','%s.json'%protocol))
    if experiment in json_reader.experiments():
        subjects = json_reader.subjects(experiment=experiment)
        for subject_no_montage in subjects:
            for montage in json_reader.montages(subject=subject_no_montage, experiment=experiment):
                subject = subject_no_montage if montage == '0' else '%s_%s' % (subject_no_montage, montage)
                sessions = json_reader.sessions(subject=subject_no_montage, montage=montage, experiment=experiment)
                for session in sessions:
                    try:
                        original_session =  json_reader.get_value('original_session',
                                                                  subject=subject_no_montage,experiment=experiment,
                                                                  session=session)
                    except ValueError:
                        original_session = session # not necessarily robust
                    yield subject_no_montage, subject,session, original_session,  experiment, '0'
    else:
        if re.match('catFR[0-4]', experiment):
            ram_exp = 'RAM_{}'.format(experiment[0].capitalize() + experiment[1:])
        else:
            ram_exp = 'RAM_{}'.format(experiment)
        events_dir = os.path.join(paths.data_root,'events',ram_exp)
        events_files = sorted(glob.glob(os.path.join(events_dir, '{}*_events.mat'.format(protocol.upper()))),
                              key=lambda f: f.split('_')[:-1])
        seen_experiments = defaultdict(list)
        for events_file in events_files:
            subject = '_'.join(os.path.basename(events_file).split('_')[:-1])
            subject_no_montage = subject.split('_')[0]
            if '_' in subject:
                if not include_montage_changes:
                    continue
            mat_events_reader = BaseEventReader(filename=events_file, common_root=paths.data_root)
            logger.debug('Loading matlab events {exp}: {subj}'.format(exp=experiment, subj=subject))
            try:
                mat_events = mat_events_reader.read()
                sessions = np.unique(mat_events['session'])
                version_str = mat_events[-5]['expVersion'] if 'expVersion' in mat_events.dtype.names else '0'
                version = -1
                try:
                    version = float(version_str.split('_')[-1])
                except:
                    try:
                        version = float(version_str.split('v')[-1])
                    except:
                        pass

                for i, session in enumerate(sessions):
                    if 'experiment' in mat_events.dtype.names:
                        experiments = np.unique(mat_events[mat_events['session'] == session]['experiment'])
                    else:
                        experiments = [experiment]
                    for this_experiment in experiments:
                        n_sessions = seen_experiments[subject_no_montage].count(this_experiment)
                        yield subject_no_montage, subject, n_sessions, session, this_experiment, version
                        seen_experiments[subject_no_montage].append(this_experiment)
            except AttributeError:
                traceback.print_exc()
                logger.error('Could not get session from {}'.format(events_file))
def test_1_old():
    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(event_file=e_path, eliminate_events_with_no_eeg=True, use_ptsa_events_class=False)

    base_e_reader.read()

    base_events = base_e_reader.get_output()

    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(tal_filename=tal_path)
    monopolar_channels = tal_reader.get_monopolar_channels()
    bipolar_pairs = tal_reader.get_bipolar_pairs()

    print 'bipolar_pairs=', bipolar_pairs

    from ptsa.data.experimental.TimeSeriesSessionEEGReader import TimeSeriesSessionEEGReader

    # time_series_reader = TimeSeriesSessionEEGReader(events=base_events, channels = ['002', '003', '004', '005'])
    time_series_reader = TimeSeriesSessionEEGReader(events=base_events, channels=monopolar_channels)
    ts_dict = time_series_reader.read()

    first_session_data = ts_dict.items()[0][1]

    print first_session_data

    wavelet_start = time.time()

    wf = MorletWaveletFilter(time_series=first_session_data,
                             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.experimental.EventDataChopper import EventDataChopper
    edcw = EventDataChopper(events=base_events, event_duration=1.6, buffer=1.0,
                            data_dict={base_events[0].eegfile: pow_wavelet})

    chopped_wavelets = edcw.filter()

    chopped_wavelets = chopped_wavelets.items()[0][1]  # getting first item of return dictionary

    print 'total time = ', time.time() - start
    #
    # from ptsa.data.filters.ResampleFilter import ResampleFilter
    # rsf = ResampleFilter (resamplerate=50.0)
    # rsf.set_input(chopped_wavelets)
    # chopped_wavelets_resampled = rsf.filter()
    #
    # return chopped_wavelets_resampled
    return chopped_wavelets