Exemplo n.º 1
0
    def test_ptsa_event_ordering(self):
        # --------------------OROG PTSA - one raw bin wrapper per event
        e_reader = PTSAEventReader(filename=self.e_path, eliminate_events_with_no_eeg=True, use_groupped_rawbinwrapper=False)
        events = e_reader.read()

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

        events = events[self.event_range]

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

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

        eegs = eegs[:,:,:-1]
        words_ptsa = eegs['events']['item']
        print 'words_ptsa=',words_ptsa


        words = self.base_eegs['events'].data['item']


        print words

        assert_array_equal(np.array(words_ptsa),words)

        assert_array_equal(eegs, self.base_eegs)
Exemplo n.º 2
0
    def test_eeg_read_keep_buffer(self):

        # OLD READERS
        events = self.read_ptsa_events()

        # 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=0.0, end_time=1.6,
                               buffer_time=1.0, eoffset='eegoffset', keep_buffer=True,
                               eoffset_in_time=False, verbose=True)


        # NEW READERS
        base_events = self.read_base_events()

        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)

        time_series_reader.read(channels=['002', '003'])

        base_eegs = time_series_reader.get_output()

        # testing
        assert_array_equal(eegs, base_eegs.data)
Exemplo n.º 3
0
    def test_ptsa_event_ordering(self):
        # --------------------OROG PTSA - one raw bin wrapper per event
        e_reader = PTSAEventReader(filename=self.e_path,
                                   eliminate_events_with_no_eeg=True,
                                   use_groupped_rawbinwrapper=False)
        events = e_reader.read()

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

        events = events[self.event_range]

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

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

        eegs = eegs[:, :, :-1]
        words_ptsa = eegs['events']['item']
        print('words_ptsa=', words_ptsa)

        words = self.base_eegs['events'].data['item']

        print(words)

        assert_array_equal(np.array(words_ptsa), words)

        assert_array_equal(eegs, self.base_eegs)
Exemplo n.º 4
0
    def run(self):
        subject = self.pipeline.subject
        experiment = self.pipeline.experiment

        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 events.size == 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(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)
Exemplo n.º 5
0
    def test_eeg_read_keep_buffer(self):

        # OLD READERS
        events = self.read_ptsa_events()

        # 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=0.0,
                               end_time=1.6,
                               buffer_time=1.0,
                               eoffset='eegoffset',
                               keep_buffer=True,
                               eoffset_in_time=False,
                               verbose=True)

        # NEW READERS
        base_events = self.read_base_events()

        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)

        time_series_reader.read(channels=['002', '003'])

        base_eegs = time_series_reader.get_output()

        # testing
        assert_array_equal(eegs, base_eegs.data)
Exemplo n.º 6
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'

        # --------------- 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()
Exemplo n.º 7
0
    def test_full_session_read(self):

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

        eegs = self.events.get_data(channels=self.monopolar_channels,
                                    start_time=0.0,
                                    end_time=1.6,
                                    buffer_time=1.0,
                                    eoffset='eegoffset',
                                    keep_buffer=True,
                                    eoffset_in_time=False,
                                    verbose=True)

        # removing last entry to match dimensions - ptsa adds one extra element at the end of time axis
        eegs = eegs[:, :, :-1]
        eeg_reader = EEGReader(events=self.base_events,
                               channels=self.monopolar_channels,
                               start_time=0.0,
                               end_time=1.6,
                               buffer_time=1.0)

        base_eegs = eeg_reader.read()

        assert_array_equal(x=eegs, y=base_eegs)
        # checking if axes match
        assert_array_equal(np.array(eegs['channels']['name']),
                           base_eegs['channels'].data)
        assert_array_almost_equal(np.array(eegs['time']),
                                  base_eegs['time'].data,
                                  decimal=3)
        assert_array_equal(np.array(eegs['events']['item']),
                           base_eegs['events'].data['item'])

        print()
Exemplo n.º 8
0
    def test_eeg_read_keep_buffer_butterworth_filtered(self):

        # old readers
        events = self.read_ptsa_events()

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

        base_events = self.read_base_events()

        eegs = events.get_data(channels=['002', '003'],
                               start_time=0.0,
                               end_time=1.6,
                               buffer_time=1.0,
                               eoffset='eegoffset',
                               keep_buffer=True,
                               eoffset_in_time=False,
                               verbose=True)
        # filtering
        eegs_filtered = eegs.filtered([58, 62], filt_type='stop', order=4)

        # New style reading
        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)

        time_series_reader.read(channels=['002', '003'])

        base_eegs = time_series_reader.get_output()

        from ptsa.data.filters.ButterworthFilter import ButterworthFilter
        b_filter = ButterworthFilter(time_series=base_eegs,
                                     samplerate=base_eegs.attrs['samplerate'],
                                     freq_range=[58, 62],
                                     filt_type='stop',
                                     order=4)

        base_eegs_filtered = b_filter.filter()

        # testing
        assert_array_equal(eegs_filtered, base_eegs_filtered.data)

        assert_array_equal(eegs, base_eegs.data)
Exemplo n.º 9
0
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
Exemplo n.º 10
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)
Exemplo n.º 11
0
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
Exemplo n.º 12
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()
Exemplo n.º 13
0
    def test_eeg_resample(self):

        # old readers
        events = self.read_ptsa_events()

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

        base_events = self.read_base_events()

        eegs = events.get_data(channels=['002', '003'],
                               start_time=0.0,
                               end_time=1.6,
                               buffer_time=1.0,
                               eoffset='eegoffset',
                               keep_buffer=True,
                               eoffset_in_time=False,
                               verbose=True)
        # filtering
        eegs_resampled = eegs.resampled(50.0)

        # New style reading
        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)

        time_series_reader.read(channels=['002', '003'])

        base_eegs = time_series_reader.get_output()

        from ptsa.data.filters.ResampleFilter import ResampleFilter
        resample_filter = ResampleFilter(time_series=base_eegs,
                                         resamplerate=50.0)

        base_eegs_resampled = resample_filter.filter()

        # testing
        assert_array_equal(eegs_resampled, base_eegs_resampled.data)
Exemplo n.º 14
0
    def test_eeg_read_keep_buffer_butterworth_filtered(self):

        # old readers
        events = self.read_ptsa_events()

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

        base_events = self.read_base_events()

        eegs = events.get_data(channels=['002', '003'], start_time=0.0, end_time=1.6,
                               buffer_time=1.0, eoffset='eegoffset', keep_buffer=True,
                               eoffset_in_time=False, verbose=True)
        # filtering
        eegs_filtered = eegs.filtered([58, 62], filt_type='stop', order=4)


        # New style reading
        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)

        time_series_reader.read(channels=['002', '003'])

        base_eegs = time_series_reader.get_output()

        from ptsa.data.filters.ButterworthFilter import ButterworthFilter
        b_filter = ButterworthFilter(time_series=base_eegs, samplerate=base_eegs.attrs['samplerate'],
                                     freq_range=[58, 62], filt_type='stop',
                                     order=4)


        base_eegs_filtered = b_filter.filter()



        # testing
        assert_array_equal(eegs_filtered, base_eegs_filtered.data)

        assert_array_equal(eegs, base_eegs.data)
Exemplo n.º 15
0
    def read(self):
        """
        Reads Matlab event file , converts it to np.recarray and attaches rawbinwrappers (if appropriate flags indicate so)
        :return: Events object. depending on flagg settings the rawbinwrappers may be attached as well
        """

        # calling base class read fcn
        evs = BaseEventReader.read(self)

        # in case evs is simply recarray
        if not isinstance(evs, Events):
            evs = Events(evs)

        if self.attach_rawbinwrapper:
            evs = evs.add_fields(esrc=np.dtype(RawBinWrapper))

            if self.use_groupped_rawbinwrapper:  # this should be default choice - much faster execution
                self.attach_rawbinwrapper_groupped(evs)
            else:  # used for debugging purposes
                self.attach_rawbinwrapper_individual(evs)

        return evs
Exemplo n.º 16
0
    def read(self):
        '''
        Reads Matlab event file , converts it to np.recarray and attaches rawbinwrappers (if appropriate flags indicate so)
        :return: Events object. depending on flagg settings the rawbinwrappers may be attached as well
        '''

        # calling base class read fcn
        evs = BaseEventReader.read(self)

        # in case evs is simply recarray
        if not isinstance(evs, Events):
            evs = Events(evs)

        if self.attach_rawbinwrapper:
            evs = evs.add_fields(esrc=np.dtype(RawBinWrapper))

            if self.use_groupped_rawbinwrapper:  # this should be default choice - much faster execution
                self.attach_rawbinwrapper_groupped(evs)
            else:  # used for debugging purposes
                self.attach_rawbinwrapper_individual(evs)

        return evs
Exemplo n.º 17
0
    def test_eeg_resample(self):



        # old readers
        events = self.read_ptsa_events()

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

        base_events = self.read_base_events()

        eegs = events.get_data(channels=['002', '003'], start_time=0.0, end_time=1.6,
                               buffer_time=1.0, eoffset='eegoffset', keep_buffer=True,
                               eoffset_in_time=False, verbose=True)
        # filtering
        eegs_resampled = eegs.resampled(50.0)


        # New style reading
        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)

        time_series_reader.read(channels=['002', '003'])

        base_eegs = time_series_reader.get_output()

        from ptsa.data.filters.ResampleFilter import ResampleFilter
        resample_filter = ResampleFilter(time_series=base_eegs, resamplerate=50.0)

        base_eegs_resampled = resample_filter.filter()


        # testing
        assert_array_equal(eegs_resampled, base_eegs_resampled.data)
Exemplo n.º 18
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)
Exemplo n.º 19
0
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
Exemplo n.º 20
0
    def test_full_session_read(self):


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

        eegs = self.events.get_data(channels=self.monopolar_channels, start_time=0.0, end_time=1.6,
                                         buffer_time=1.0, eoffset='eegoffset', keep_buffer=True,
                                         eoffset_in_time=False, verbose=True)

        # removing last entry to match dimensions - ptsa adds one extra element at the end of time axis
        eegs = eegs[:,:,:-1]
        eeg_reader = EEGReader(events=self.base_events, channels=self.monopolar_channels,
                               start_time=0.0, end_time=1.6, buffer_time=1.0)


        base_eegs = eeg_reader.read()

        assert_array_equal(x=eegs, y=base_eegs)
        # checking if axes match
        assert_array_equal(np.array(eegs['channels']['name']), base_eegs['channels'].data)
        assert_array_almost_equal(np.array(eegs['time']), base_eegs['time'].data, decimal=3)
        assert_array_equal(np.array(eegs['events']['item']), base_eegs['events'].data['item'])


        print
Exemplo n.º 21
0
    def read(self):
        from MatlabIO import read_single_matlab_matrix_as_numpy_structured_array

        # extract matlab matrix (called 'events') as numpy structured array
        struct_array = read_single_matlab_matrix_as_numpy_structured_array(
            self.__event_file, 'events')

        evs = Events(struct_array)

        if self.eliminate_events_with_no_eeg:

            # eliminating events that have no eeg file
            indicator = np.empty(len(evs), dtype=bool)
            indicator[:] = False
            for i, ev in enumerate(evs):
                indicator[i] = (type(
                    evs[i].eegfile).__name__.startswith('unicode')) & (len(
                        str(evs[i].eegfile)) > 3)

            evs = evs[indicator]

        self.__events = evs
        return self.__events
Exemplo n.º 22
0
class TestRegressionPTSA(unittest.TestCase):
    # class TestRegressionPTSA(unittest.TestCase):
    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_time_series_mirror(self):
        duration = 1.0
        mirrored_buf_eegs = self.base_eegs.add_mirror_buffer(duration=duration)

        samplerate = float(self.base_eegs['samplerate'])
        nb_ = int(np.ceil(samplerate * duration))

        assert_array_equal(self.base_eegs[...,1:nb_+1], mirrored_buf_eegs[...,:nb_][...,::-1])
        print mirrored_buf_eegs

    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 test_full_session_read(self):


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

        eegs = self.events.get_data(channels=self.monopolar_channels, start_time=0.0, end_time=1.6,
                                         buffer_time=1.0, eoffset='eegoffset', keep_buffer=True,
                                         eoffset_in_time=False, verbose=True)

        # removing last entry to match dimensions - ptsa adds one extra element at the end of time axis
        eegs = eegs[:,:,:-1]
        eeg_reader = EEGReader(events=self.base_events, channels=self.monopolar_channels,
                               start_time=0.0, end_time=1.6, buffer_time=1.0)


        base_eegs = eeg_reader.read()

        assert_array_equal(x=eegs, y=base_eegs)
        # checking if axes match
        assert_array_equal(np.array(eegs['channels']['name']), base_eegs['channels'].data)
        assert_array_almost_equal(np.array(eegs['time']), base_eegs['time'].data, decimal=3)
        assert_array_equal(np.array(eegs['events']['item']), base_eegs['events'].data['item'])


        print



    def test_ptsa_event_ordering(self):
        # --------------------OROG PTSA - one raw bin wrapper per event
        e_reader = PTSAEventReader(filename=self.e_path, eliminate_events_with_no_eeg=True, use_groupped_rawbinwrapper=False)
        events = e_reader.read()

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

        events = events[self.event_range]

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

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

        eegs = eegs[:,:,:-1]
        words_ptsa = eegs['events']['item']
        print 'words_ptsa=',words_ptsa


        words = self.base_eegs['events'].data['item']


        print words

        assert_array_equal(np.array(words_ptsa),words)

        assert_array_equal(eegs, self.base_eegs)


    def test_eeg_read(self):
        # base_eegs = self.base_eegs.remove_buffer(duration=1.0)
        # eegs = self.eegs.remove_buffer(duration=1.0)

        base_eegs = self.base_eegs
        eegs = self.eegs


        # orig ptsa returns extra stime point that's why eegs[:,:,:-1]
        # assert_array_equal(eegs[:, :, :-1], base_eegs.data)
        assert_array_equal(eegs[:, :, :-1], base_eegs.data)



    def test_eeg_read_keep_buffer(self):
        # orig ptsa returns extra stime point that's why eegs[:,:,:-1]
        eegs = self.eegs
        base_eegs = self.base_eegs

        assert_array_equal(eegs[:, :, :-1], base_eegs.data)

    def test_eeg_read_keep_buffer_butterworth_filtered(self):
        # filtering
        # # orig ptsa returns extra time point that's why eegs[:,:,:-1]
        eegs = self.eegs[:, :, :-1]
        eegs_filtered = eegs.filtered([58, 62], filt_type='stop', order=4)

        # -------------------TimeSeriesX
        base_eegs = self.base_eegs
        base_eegs_filtered_direct = self.base_eegs.filtered([58, 62], filt_type='stop', order=4)

        b_filter = ButterworthFilter(time_series=base_eegs, freq_range=[58, 62], filt_type='stop', order=4)

        base_eegs_filtered = b_filter.filter()

        # ---------------- testing
        # assert_array_almost_equal(eegs_filtered[:,:,:], base_eegs_filtered.data, decimal=2)
        assert_array_equal(eegs_filtered[:, :, :], base_eegs_filtered.data)
        assert_array_equal(eegs_filtered[:, :, :], base_eegs_filtered_direct.data)

        # checking filtering of just single time series
        eegs_0_0 = eegs[0,0]
        eegs_filtered_0_0 = eegs_0_0.filtered([58, 62], filt_type='stop', order=4)

        assert_array_equal(eegs_filtered_0_0, eegs_filtered[0,0])

        base_eegs[0:1,0:1].filtered([58, 62], filt_type='stop', order=4)





    def test_eeg_resample(self):
        # # orig ptsa returns extra stime point that's why eegs[:,:,:-1]
        eegs = self.eegs[:, :, :-1]

        # filtering
        eegs_resampled = eegs.resampled(100.0)

        # -------------------TimeSeriesX
        base_eegs = self.base_eegs

        resample_filter = ResampleFilter(time_series=base_eegs, resamplerate=100.0)

        base_eegs_resampled = resample_filter.filter()

        base_eegs_resampled_direct = base_eegs.resampled(resampled_rate=100.0)

        # -------------- testing
        assert_array_equal(eegs_resampled, base_eegs_resampled.data)
        assert_array_equal(eegs_resampled, base_eegs_resampled_direct.data)

        base_eegs_resampled_direct_0_0 = base_eegs[0,0].resampled(resampled_rate=100.0)

    def test_wavelets_resample(self):
        eegs = self.eegs[:, :, :-1]
        base_eegs = self.base_eegs

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

        pow_wavelet, phase_wavelet = wf.filter()

        resample_filter = ResampleFilter(time_series=pow_wavelet, resamplerate=100.0)
        pow_wavelet_resampled = resample_filter.filter()

        print pow_wavelet_resampled

        pow_wavelet_resampled_direct = pow_wavelet.resampled(resampled_rate=100.0)

        print pow_wavelet_resampled_direct

        # from ptsa.wavelet import phase_pow_multi
        # pow_wavelet_ptsa_orig = phase_pow_multi(freqs=np.logspace(np.log10(3), np.log10(180), 8), dat=eegs,to_return='power')
        # print 'pow_wavelets_ptsa_orig=',pow_wavelet_ptsa_orig


    def test_ts_convenience_fcns(self):
        # # orig ptsa returns extra stime point that's why eegs[:,:,:-1]
        eegs = self.eegs[:, :, :-1]
        base_eegs = self.base_eegs

        mean_eegs = eegs.mean(axis='time')
        mean_base_eegs = base_eegs.mean(dim='time')
        assert_array_equal(mean_eegs, mean_base_eegs)

        min_eegs = eegs.min(axis='time')
        min_base_eegs = base_eegs.min(dim='time')
        assert_array_equal(min_eegs, min_base_eegs)

        max_eegs = eegs.max(axis='time')
        max_base_eegs = base_eegs.max(dim='time')
        assert_array_equal(max_eegs, max_base_eegs)


        cumsum_eegs = eegs.cumsum(axis='time')
        cumsum_base_eegs = np.cumsum(base_eegs,axis=base_eegs.get_axis_num('time'))
        assert_array_equal(cumsum_eegs, cumsum_base_eegs)

        cumprod_eegs = eegs.cumprod(axis='time')
        cumprod_base_eegs = np.cumprod(base_eegs,axis=base_eegs.get_axis_num('time'))
        assert_array_equal(cumprod_eegs, cumprod_base_eegs)

        std_eegs = eegs.std(axis='time')
        std_base_eegs = base_eegs.std(dim='time')
        assert_array_equal(std_eegs, std_base_eegs)

        sum_eegs = eegs.sum(axis='time')
        sum_base_eegs = base_eegs.sum(dim='time')
        assert_array_equal(sum_eegs, sum_base_eegs)

        argmin_eegs = eegs.argmin(axis='time')
        argmin_base_eegs = base_eegs.argmin(dim='time')
        assert_array_equal(argmin_eegs, argmin_base_eegs)

        argmax_eegs = eegs.argmax(axis='time')
        argmax_base_eegs = base_eegs.argmax(dim='time')
        assert_array_equal(argmax_eegs, argmax_base_eegs)

        argsort_eegs = eegs.argsort(axis='time')
        argsort_base_eegs = np.argsort(base_eegs,axis=base_eegs.get_axis_num('time'))
        assert_array_equal(argsort_eegs, argsort_base_eegs)
                
        # could not get compress to work using timeseries.compress method 
        # compress_eegs = eegs.compress(condition=[0,2], axis='time')
        # compress_base_eegs = np.compress(condition=[0,1], a=base_eegs.data, axis=base_eegs.get_axis_num('channels'))
        # assert_array_equal(compress_eegs, compress_base_eegs)

        diagonal_eegs = eegs.diagonal(offset=0, axis1=1, axis2=2)
        diagonal_base_eegs = np.diagonal(base_eegs,offset=0, axis1=1,axis2=2)
        assert_array_equal(diagonal_eegs, diagonal_base_eegs)

        flatten_eegs = eegs.flatten()
        flatten_base_eegs = base_eegs.data.flatten()
        assert_array_equal(flatten_eegs, flatten_base_eegs)

        prod_eegs = eegs.prod(axis='channels')
        prod_base_eegs = base_eegs.prod(dim = 'channels')
        assert_array_equal(prod_eegs, prod_base_eegs)

        ptp_eegs = eegs.ptp(axis='channels')
        ptp_base_eegs = np.ptp(base_eegs.data, axis = base_eegs.get_axis_num('channels'))
        assert_array_equal(ptp_eegs, ptp_base_eegs)

        ravel_eegs = eegs.ravel()
        ravel_base_eegs = np.ravel(base_eegs)
        assert_array_equal(ravel_eegs, ravel_base_eegs)

        repeat_eegs = eegs.repeat(repeats=2, axis='channels')
        repeat_base_eegs = np.repeat(base_eegs.data, repeats=2, axis=base_eegs.get_axis_num('channels'))
        assert_array_equal(repeat_eegs, repeat_base_eegs)

        swapaxes_eegs = eegs.swapaxes(axis1='channels',axis2='events')
        swapaxes_base_eegs = base_eegs.transpose('events','channels','time')
        assert_array_equal(swapaxes_eegs , swapaxes_base_eegs)

        take_eegs = eegs.take(indices=[2,4,6,8],axis='events')
        take_base_eegs = base_eegs.isel(events=[2,4,6,8])
        assert_array_equal(take_eegs , take_base_eegs)
        
        trace_eegs = eegs.trace(offset=0, axis1=0,axis2=2)
        trace_base_eegs = np.trace(base_eegs, offset=0, axis1=base_eegs.get_axis_num('channels'),
                                   axis2=base_eegs.get_axis_num('time'))
        assert_array_equal(trace_eegs , trace_base_eegs)
        
        var_eegs = eegs.var(axis='time')
        var_base_eegs = base_eegs.var(dim='time')
        assert_array_equal(var_eegs, var_base_eegs)
        
    def test_wavelets(self):
        eegs = self.eegs[:, :, :-1]
        base_eegs = self.base_eegs

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

                                 )

        pow_wavelet, phase_wavelet = wf.filter()
        print 'pow_wavelet=',pow_wavelet

        from ptsa.wavelet import phase_pow_multi
        pow_wavelet_ptsa_orig = phase_pow_multi(freqs=np.logspace(np.log10(3), np.log10(180), 8), dat=eegs,to_return='power')
        print 'pow_wavelets_ptsa_orig=',pow_wavelet_ptsa_orig


        # import matplotlib;
        # matplotlib.use('Qt4Agg')
        #
        #
        # import matplotlib.pyplot as plt
        # plt.get_current_fig_manager().window.raise_()
        #
        wavelet_1 = pow_wavelet[0,0,0,500:-500]
        wavelet_2 = pow_wavelet_ptsa_orig[0,0,0,500:-500]
        #
        # plt.plot(np.arange(wavelet_1.shape[0])-1,wavelet_1,'k')
        # plt.plot(np.arange(wavelet_2.shape[0])-1,wavelet_2,'r--')
        #
        # plt.show()

        assert_array_equal(eegs, base_eegs.data)
        # assert_array_equal(wavelet_1, wavelet_2)

        # assert_array_almost_equal((wavelet_1-wavelet_2)/wavelet_1, np.zeros_like(wavelet_1), decimal=4)
        # assert_array_almost_equal((pow_wavelet_ptsa_orig-pow_wavelet)/pow_wavelet_ptsa_orig, np.zeros_like(pow_wavelet), decimal=4)


        assert_array_almost_equal(
            (pow_wavelet_ptsa_orig-pow_wavelet)/pow_wavelet_ptsa_orig,
            np.zeros_like(pow_wavelet), decimal=6)


        freq_num = 7
        assert_array_equal(
            (pow_wavelet_ptsa_orig[freq_num,:,:,500:-500]-pow_wavelet[freq_num,:,:,500:-500])/pow_wavelet_ptsa_orig[freq_num,:,:,500:-500],
            np.zeros_like(pow_wavelet[freq_num,:,:,500:-500]))

    def test_wavelets_python_cpp(self):
        from ptsa.data.filters import MorletWaveletFilterCpp
        print 'hello'

        wf = MorletWaveletFilter(time_series=self.base_eegs,
                                 freqs=np.logspace(np.log10(3), np.log10(180), 8),
                                 output='power',
                                 )

        pow_wavelet, phase_wavelet = wf.filter()

        wf_cpp = MorletWaveletFilterCpp(time_series=self.base_eegs,
                                 freqs=np.logspace(np.log10(3), np.log10(180), 8),
                                 output='power',
                                 )

        pow_wavelet_cpp, phase_wavelet_cpp = wf_cpp.filter()

        decimal = 2
        freq_num=0

        from scipy.stats import describe

        desc_cpp = describe(pow_wavelet_cpp[freq_num,:,:,500:-500])
        desc_py = describe(pow_wavelet[freq_num,:,:,500:-500])

        try:
            assert_array_almost_equal(
                (pow_wavelet_cpp[freq_num,:,:,500:-500]-pow_wavelet[freq_num,:,:,500:-500])/pow_wavelet_cpp[freq_num,:,:,500:-500],
                np.zeros_like(pow_wavelet_cpp[freq_num,:,:,500:-500]), decimal=decimal)
        except AssertionError:
            print 'WARNING: Cpp and Python wavelets are not within 1%. Will try weaker test '

            mean_min = np.min((desc_cpp.mean-desc_py.mean)/desc_cpp.mean)
            mean_max = np.max((desc_cpp.mean-desc_py.mean)/desc_cpp.mean)
            print 'mean_max=',mean_max
            print 'mean_min=',mean_min


            self.assertTrue(np.abs(mean_max)<0.05)
            self.assertTrue(np.abs(mean_min)<0.05)


    def test_wavelets_multicore(self):
        from ptsa.data.filters import MorletWaveletFilterCpp
        print 'hello'
        print sys.path

        wf_cpp_multi = MorletWaveletFilterCpp(time_series=self.base_eegs,
                                 freqs=np.logspace(np.log10(3), np.log10(180), 8),
                                 output='both',
                                 cpus=8
                                 )

        pow_wavelet_multi, phase_wavelet_multi = wf_cpp_multi.filter()

        wf_cpp = MorletWaveletFilterCpp(time_series=self.base_eegs,
                                 freqs=np.logspace(np.log10(3), np.log10(180), 8),
                                 output='both',
                                 cpus=1
                                 )

        pow_wavelet, phase_wavelet= wf_cpp.filter()

        assert_array_equal(pow_wavelet_multi,pow_wavelet)
        assert_array_equal(phase_wavelet_multi,phase_wavelet)



    def test_wavelets_cpp(self):


        eegs = self.eegs[:, :, :-1]
        base_eegs = self.base_eegs


        sys.path.append('/Users/m/src/morlet_git_install')
        import morlet
        num_freqs = 8
        f_min = 3.0
        f_max = 180.0
        signal_length = base_eegs.shape[-1]
        morlet_transform = morlet.MorletWaveletTransform()
        samplerate = float(base_eegs['samplerate'])
        morlet_transform.init(5, f_min, f_max, num_freqs, samplerate , signal_length)

        signal = base_eegs[0:1,0:1,:]
        signal_orig_eegs = eegs[0:1,0:1,:]

        pow_wavelets_cpp = np.empty(shape=(base_eegs.shape[-1]*num_freqs,), dtype=np.float)

        # for i in xrange(num_of_iterations):
        #     morlet_transform.multiphasevec(signal,powers)
        morlet_transform.multiphasevec(signal.data.flatten(),pow_wavelets_cpp)

        pow_wavelets_cpp = pow_wavelets_cpp.reshape(8,pow_wavelets_cpp.shape[0]/8)



        wf = MorletWaveletFilter(time_series=signal,
                                 freqs=np.logspace(np.log10(f_min), np.log10(f_max), num_freqs),
                                 output='power',
                                 frequency_dim_pos=0,

                                 )

        pow_wavelet, phase_wavelet = wf.filter()



        from ptsa.wavelet import phase_pow_multi
        pow_wavelet_ptsa_orig = phase_pow_multi(freqs=np.logspace(np.log10(3), np.log10(180), 8), dat=signal_orig_eegs,to_return='power')


        freq_num = 0

        decimal = 1

        assert_array_almost_equal(
            (np.squeeze(pow_wavelet[freq_num,:,:,500:-500])-np.squeeze(pow_wavelet_ptsa_orig[freq_num,:,:,500:-500]))/np.squeeze(pow_wavelet_ptsa_orig[freq_num,:,:,500:-500]),
            np.zeros_like(np.squeeze(pow_wavelet_ptsa_orig[freq_num,:,:,500:-500])), decimal=decimal)


        assert_array_almost_equal(
            (pow_wavelets_cpp[freq_num,500:-500]-np.squeeze(pow_wavelet[freq_num,:,:,500:-500]))/pow_wavelets_cpp[freq_num,500:-500],
            np.zeros_like(pow_wavelets_cpp[freq_num,500:-500]), decimal=decimal)

        #
        assert_array_almost_equal(
            (pow_wavelets_cpp[freq_num,500:-500]-np.squeeze(pow_wavelet_ptsa_orig[freq_num,:,:,500:-500]))/pow_wavelets_cpp[freq_num,500:-500],
            np.zeros_like(np.squeeze(pow_wavelet_ptsa_orig[freq_num,:,:,500:-500])), decimal=decimal)




        from ptsa.wavelet import phase_pow_multi

    def test_event_data_chopper(self):


        dataroot=self.base_events[0].eegfile
        from ptsa.data.readers import EEGReader
        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=0.0, end_time=1.6, buffer_time=1.0)
        chopped_session = sedc.filter()

        sedc_so = DataChopper(start_offsets=self.base_events.eegoffset, session_data=session_eegs, start_time=0.0, end_time=1.6, buffer_time=1.0)
        chopped_session_so = sedc_so.filter()

        assert_array_equal(chopped_session,chopped_session_so)


    def test_wavelets_synthetic_data(self):
        samplerate = 1000.
        frequency = 180.0
        modulation_frequency = 80.0

        duration = 1.0

        n_points = int(np.round(duration*samplerate))
        x = np.arange(n_points, dtype=np.float)
        y = np.sin(x*(2*np.pi*frequency/n_points))
        y_mod = np.sin(x*(2*np.pi*frequency/n_points))* np.sin(x*(2*np.pi*modulation_frequency/n_points))

        ts = TimeSeriesX(y, dims=['time'], coords=[x])
        ts['samplerate']=samplerate
        ts.attrs['samplerate'] = samplerate

        frequencies = [ 10.0, 30.0, 50.0, 80., 120., 180., 250.0 , 300.0, 500.]
        for frequency  in frequencies:
            wf = MorletWaveletFilter(time_series=ts,
                                     freqs=np.array([frequency]),
                                     output='both',
                                     frequency_dim_pos=0,
                                     verbose=True
                                     )

            pow_wavelet, phase_wavelet = wf.filter()

            from ptsa.wavelet import phase_pow_multi


            pow_wavelet_ptsa_orig = phase_pow_multi(freqs=[frequency],samplerates=samplerate, dat=ts.data,to_return='power')


            assert_array_almost_equal(
                (pow_wavelet_ptsa_orig-pow_wavelet)/pow_wavelet_ptsa_orig,
                np.zeros_like(pow_wavelet), decimal=6)
Exemplo n.º 23
0
    def run(self):

        # PTSA EVENT  READER

        # e_path = join(self.pipeline.mount_point, 'data/events', self.pipeline.task,self.pipeline.subject+'_events.mat')
        e_path = '/Users/m/data/events/RAM_FR1/R1060M_events.mat'
        # e_path = '/Users/m/data/events/RAM_FR1/R1056M_events.mat'
        e_reader = PTSAEventReader(event_file=e_path,
                                   eliminate_events_with_no_eeg=True)

        e_reader.read()

        events = e_reader.get_output()

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

        events = events[0:30]

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

        events = Events(events)  # necessary workaround for new numpy
        print 'events=', events

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

        print eegs

        # BASE READER

        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']

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

        base_events = base_events[0:30]

        print 'base_events=', base_events

        from ptsa.data.readers.TimeSeriesEEGReader import TimeSeriesEEGReader

        time_series_reader = TimeSeriesEEGReader(base_events)

        time_series_reader.start_time = 0.0
        time_series_reader.end_time = 1.6
        time_series_reader.buffer_time = 1.0
        time_series_reader.keep_buffer = False

        time_series_reader.read(channels=['002', '003'])

        base_eegs = time_series_reader.get_output()
        print
Exemplo n.º 24
0
class TestRegressionPTSA(unittest.TestCase):
    # class TestRegressionPTSA(unittest.TestCase):
    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 test_time_series_mirror(self):
        duration = 1.0
        mirrored_buf_eegs = self.base_eegs.add_mirror_buffer(duration=duration)

        samplerate = float(self.base_eegs['samplerate'])
        nb_ = int(np.ceil(samplerate * duration))

        assert_array_equal(self.base_eegs[..., 1:nb_ + 1],
                           mirrored_buf_eegs[..., :nb_][..., ::-1])
        print(mirrored_buf_eegs)

    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)

    def test_full_session_read(self):

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

        eegs = self.events.get_data(channels=self.monopolar_channels,
                                    start_time=0.0,
                                    end_time=1.6,
                                    buffer_time=1.0,
                                    eoffset='eegoffset',
                                    keep_buffer=True,
                                    eoffset_in_time=False,
                                    verbose=True)

        # removing last entry to match dimensions - ptsa adds one extra element at the end of time axis
        eegs = eegs[:, :, :-1]
        eeg_reader = EEGReader(events=self.base_events,
                               channels=self.monopolar_channels,
                               start_time=0.0,
                               end_time=1.6,
                               buffer_time=1.0)

        base_eegs = eeg_reader.read()

        assert_array_equal(x=eegs, y=base_eegs)
        # checking if axes match
        assert_array_equal(np.array(eegs['channels']['name']),
                           base_eegs['channels'].data)
        assert_array_almost_equal(np.array(eegs['time']),
                                  base_eegs['time'].data,
                                  decimal=3)
        assert_array_equal(np.array(eegs['events']['item']),
                           base_eegs['events'].data['item'])

        print()

    def test_ptsa_event_ordering(self):
        # --------------------OROG PTSA - one raw bin wrapper per event
        e_reader = PTSAEventReader(filename=self.e_path,
                                   eliminate_events_with_no_eeg=True,
                                   use_groupped_rawbinwrapper=False)
        events = e_reader.read()

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

        events = events[self.event_range]

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

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

        eegs = eegs[:, :, :-1]
        words_ptsa = eegs['events']['item']
        print('words_ptsa=', words_ptsa)

        words = self.base_eegs['events'].data['item']

        print(words)

        assert_array_equal(np.array(words_ptsa), words)

        assert_array_equal(eegs, self.base_eegs)

    def test_eeg_read(self):
        # base_eegs = self.base_eegs.remove_buffer(duration=1.0)
        # eegs = self.eegs.remove_buffer(duration=1.0)

        base_eegs = self.base_eegs
        eegs = self.eegs

        # orig ptsa returns extra stime point that's why eegs[:,:,:-1]
        # assert_array_equal(eegs[:, :, :-1], base_eegs.data)
        assert_array_equal(eegs[:, :, :-1], base_eegs.data)

    def test_eeg_read_keep_buffer(self):
        # orig ptsa returns extra stime point that's why eegs[:,:,:-1]
        eegs = self.eegs
        base_eegs = self.base_eegs

        assert_array_equal(eegs[:, :, :-1], base_eegs.data)

    def test_eeg_read_keep_buffer_butterworth_filtered(self):
        # filtering
        # # orig ptsa returns extra time point that's why eegs[:,:,:-1]
        eegs = self.eegs[:, :, :-1]
        eegs_filtered = eegs.filtered([58, 62], filt_type='stop', order=4)

        # -------------------TimeSeries
        base_eegs = self.base_eegs
        base_eegs_filtered_direct = self.base_eegs.filtered([58, 62],
                                                            filt_type='stop',
                                                            order=4)

        b_filter = ButterworthFilter(time_series=base_eegs,
                                     freq_range=[58, 62],
                                     filt_type='stop',
                                     order=4)

        base_eegs_filtered = b_filter.filter()

        # ---------------- testing
        # assert_array_almost_equal(eegs_filtered[:,:,:], base_eegs_filtered.data, decimal=2)
        assert_array_equal(eegs_filtered[:, :, :], base_eegs_filtered.data)
        assert_array_equal(eegs_filtered[:, :, :],
                           base_eegs_filtered_direct.data)

        # checking filtering of just single time series
        eegs_0_0 = eegs[0, 0]
        eegs_filtered_0_0 = eegs_0_0.filtered([58, 62],
                                              filt_type='stop',
                                              order=4)

        assert_array_equal(eegs_filtered_0_0, eegs_filtered[0, 0])

        base_eegs[0:1, 0:1].filtered([58, 62], filt_type='stop', order=4)

    def test_eeg_resample(self):
        # # orig ptsa returns extra stime point that's why eegs[:,:,:-1]
        eegs = self.eegs[:, :, :-1]

        # filtering
        eegs_resampled = eegs.resampled(100.0)

        # -------------------TimeSeries
        base_eegs = self.base_eegs

        resample_filter = ResampleFilter(time_series=base_eegs,
                                         resamplerate=100.0)

        base_eegs_resampled = resample_filter.filter()

        base_eegs_resampled_direct = base_eegs.resampled(resampled_rate=100.0)

        # -------------- testing
        assert_array_equal(eegs_resampled, base_eegs_resampled.data)
        assert_array_equal(eegs_resampled, base_eegs_resampled_direct.data)

        base_eegs_resampled_direct_0_0 = base_eegs[0, 0].resampled(
            resampled_rate=100.0)

    def test_wavelets_resample(self):
        eegs = self.eegs[:, :, :-1]
        base_eegs = self.base_eegs

        wf = MorletWaveletFilter(
            timeseries=base_eegs,
            freqs=np.logspace(np.log10(3), np.log10(180), 8),
            output='power',
            frequency_dim_pos=0,
        )

        pow_wavelet, phase_wavelet = wf.filter()

        resample_filter = ResampleFilter(time_series=pow_wavelet,
                                         resamplerate=100.0)
        pow_wavelet_resampled = resample_filter.filter()

        print(pow_wavelet_resampled)

        pow_wavelet_resampled_direct = pow_wavelet.resampled(
            resampled_rate=100.0)

        print(pow_wavelet_resampled_direct)

        # from ptsa.wavelet import phase_pow_multi
        # pow_wavelet_ptsa_orig = phase_pow_multi(freqs=np.logspace(np.log10(3), np.log10(180), 8), dat=eegs,to_return='power')
        # print 'pow_wavelets_ptsa_orig=',pow_wavelet_ptsa_orig

    def test_ts_convenience_fcns(self):
        # # orig ptsa returns extra stime point that's why eegs[:,:,:-1]
        eegs = self.eegs[:, :, :-1]
        base_eegs = self.base_eegs

        mean_eegs = eegs.mean(axis='time')
        mean_base_eegs = base_eegs.mean(dim='time')
        assert_array_equal(mean_eegs, mean_base_eegs)

        min_eegs = eegs.min(axis='time')
        min_base_eegs = base_eegs.min(dim='time')
        assert_array_equal(min_eegs, min_base_eegs)

        max_eegs = eegs.max(axis='time')
        max_base_eegs = base_eegs.max(dim='time')
        assert_array_equal(max_eegs, max_base_eegs)

        cumsum_eegs = eegs.cumsum(axis='time')
        cumsum_base_eegs = np.cumsum(base_eegs,
                                     axis=base_eegs.get_axis_num('time'))
        assert_array_equal(cumsum_eegs, cumsum_base_eegs)

        cumprod_eegs = eegs.cumprod(axis='time')
        cumprod_base_eegs = np.cumprod(base_eegs,
                                       axis=base_eegs.get_axis_num('time'))
        assert_array_equal(cumprod_eegs, cumprod_base_eegs)

        std_eegs = eegs.std(axis='time')
        std_base_eegs = base_eegs.std(dim='time')
        assert_array_equal(std_eegs, std_base_eegs)

        sum_eegs = eegs.sum(axis='time')
        sum_base_eegs = base_eegs.sum(dim='time')
        assert_array_equal(sum_eegs, sum_base_eegs)

        argmin_eegs = eegs.argmin(axis='time')
        argmin_base_eegs = base_eegs.argmin(dim='time')
        assert_array_equal(argmin_eegs, argmin_base_eegs)

        argmax_eegs = eegs.argmax(axis='time')
        argmax_base_eegs = base_eegs.argmax(dim='time')
        assert_array_equal(argmax_eegs, argmax_base_eegs)

        argsort_eegs = eegs.argsort(axis='time')
        argsort_base_eegs = np.argsort(base_eegs,
                                       axis=base_eegs.get_axis_num('time'))
        assert_array_equal(argsort_eegs, argsort_base_eegs)

        # could not get compress to work using timeseries.compress method
        # compress_eegs = eegs.compress(condition=[0,2], axis='time')
        # compress_base_eegs = np.compress(condition=[0,1], a=base_eegs.data, axis=base_eegs.get_axis_num('channels'))
        # assert_array_equal(compress_eegs, compress_base_eegs)

        diagonal_eegs = eegs.diagonal(offset=0, axis1=1, axis2=2)
        diagonal_base_eegs = np.diagonal(base_eegs, offset=0, axis1=1, axis2=2)
        assert_array_equal(diagonal_eegs, diagonal_base_eegs)

        flatten_eegs = eegs.flatten()
        flatten_base_eegs = base_eegs.data.flatten()
        assert_array_equal(flatten_eegs, flatten_base_eegs)

        prod_eegs = eegs.prod(axis='channels')
        prod_base_eegs = base_eegs.prod(dim='channels')
        assert_array_equal(prod_eegs, prod_base_eegs)

        ptp_eegs = eegs.ptp(axis='channels')
        ptp_base_eegs = np.ptp(base_eegs.data,
                               axis=base_eegs.get_axis_num('channels'))
        assert_array_equal(ptp_eegs, ptp_base_eegs)

        ravel_eegs = eegs.ravel()
        ravel_base_eegs = np.ravel(base_eegs)
        assert_array_equal(ravel_eegs, ravel_base_eegs)

        repeat_eegs = eegs.repeat(repeats=2, axis='channels')
        repeat_base_eegs = np.repeat(base_eegs.data,
                                     repeats=2,
                                     axis=base_eegs.get_axis_num('channels'))
        assert_array_equal(repeat_eegs, repeat_base_eegs)

        swapaxes_eegs = eegs.swapaxes(axis1='channels', axis2='events')
        swapaxes_base_eegs = base_eegs.transpose('events', 'channels', 'time')
        assert_array_equal(swapaxes_eegs, swapaxes_base_eegs)

        take_eegs = eegs.take(indices=[2, 4, 6, 8], axis='events')
        take_base_eegs = base_eegs.isel(events=[2, 4, 6, 8])
        assert_array_equal(take_eegs, take_base_eegs)

        trace_eegs = eegs.trace(offset=0, axis1=0, axis2=2)
        trace_base_eegs = np.trace(base_eegs,
                                   offset=0,
                                   axis1=base_eegs.get_axis_num('channels'),
                                   axis2=base_eegs.get_axis_num('time'))
        assert_array_equal(trace_eegs, trace_base_eegs)

        var_eegs = eegs.var(axis='time')
        var_base_eegs = base_eegs.var(dim='time')
        assert_array_equal(var_eegs, var_base_eegs)

    def test_wavelets(self):
        eegs = self.eegs[:, :, :-1]
        base_eegs = self.base_eegs

        wf = MorletWaveletFilter(
            timeseries=base_eegs,
            freqs=np.logspace(np.log10(3), np.log10(180), 8),
            output='power',
            frequency_dim_pos=0,
        )

        pow_wavelet, phase_wavelet = wf.filter()
        print('pow_wavelet=', pow_wavelet)

        from ptsa.wavelet import phase_pow_multi
        pow_wavelet_ptsa_orig = phase_pow_multi(freqs=np.logspace(
            np.log10(3), np.log10(180), 8),
                                                dat=eegs,
                                                to_return='power')
        print('pow_wavelets_ptsa_orig=', pow_wavelet_ptsa_orig)

        # import matplotlib;
        # matplotlib.use('Qt4Agg')
        #
        #
        # import matplotlib.pyplot as plt
        # plt.get_current_fig_manager().window.raise_()
        #
        wavelet_1 = pow_wavelet[0, 0, 0, 500:-500]
        wavelet_2 = pow_wavelet_ptsa_orig[0, 0, 0, 500:-500]
        #
        # plt.plot(np.arange(wavelet_1.shape[0])-1,wavelet_1,'k')
        # plt.plot(np.arange(wavelet_2.shape[0])-1,wavelet_2,'r--')
        #
        # plt.show()

        assert_array_equal(eegs, base_eegs.data)
        # assert_array_equal(wavelet_1, wavelet_2)

        # assert_array_almost_equal((wavelet_1-wavelet_2)/wavelet_1, np.zeros_like(wavelet_1), decimal=4)
        # assert_array_almost_equal((pow_wavelet_ptsa_orig-pow_wavelet)/pow_wavelet_ptsa_orig, np.zeros_like(pow_wavelet), decimal=4)

        assert_array_almost_equal(
            (pow_wavelet_ptsa_orig - pow_wavelet) / pow_wavelet_ptsa_orig,
            np.zeros_like(pow_wavelet),
            decimal=6)

        freq_num = 7
        assert_array_equal((pow_wavelet_ptsa_orig[freq_num, :, :, 500:-500] -
                            pow_wavelet[freq_num, :, :, 500:-500]) /
                           pow_wavelet_ptsa_orig[freq_num, :, :, 500:-500],
                           np.zeros_like(pow_wavelet[freq_num, :, :,
                                                     500:-500]))

    def test_wavelets_python_cpp(self):
        from ptsa.data.filters import MorletWaveletFilter

        wf = MorletWaveletFilter(
            timeseries=self.base_eegs,
            freqs=np.logspace(np.log10(3), np.log10(180), 8),
            output='power',
        )

        pow_wavelet, phase_wavelet = wf.filter()

        wf_cpp = MorletWaveletFilter(timeseries=self.base_eegs,
                                     freqs=np.logspace(np.log10(3),
                                                       np.log10(180), 8),
                                     output='power',
                                     cpus=4)

        pow_wavelet_cpp, phase_wavelet_cpp = wf_cpp.filter()

        decimal = 2
        freq_num = 0

        from scipy.stats import describe

        desc_cpp = describe(pow_wavelet_cpp[freq_num, :, :, 500:-500])
        desc_py = describe(pow_wavelet[freq_num, :, :, 500:-500])

        try:
            assert_array_almost_equal(
                (pow_wavelet_cpp[freq_num, :, :, 500:-500] -
                 pow_wavelet[freq_num, :, :, 500:-500]) /
                pow_wavelet_cpp[freq_num, :, :, 500:-500],
                np.zeros_like(pow_wavelet_cpp[freq_num, :, :, 500:-500]),
                decimal=decimal)
        except AssertionError:
            print(
                'WARNING: Cpp and Python wavelets are not within 1%. Will try weaker test '
            )

            mean_min = np.min((desc_cpp.mean - desc_py.mean) / desc_cpp.mean)
            mean_max = np.max((desc_cpp.mean - desc_py.mean) / desc_cpp.mean)
            print('mean_max=', mean_max)
            print('mean_min=', mean_min)

            self.assertTrue(np.abs(mean_max) < 0.05)
            self.assertTrue(np.abs(mean_min) < 0.05)

    def test_wavelets_multicore(self):
        from ptsa.data.filters import MorletWaveletFilter
        print('hello')
        print(sys.path)

        wf_cpp_multi = MorletWaveletFilter(timeseries=self.base_eegs,
                                           freqs=np.logspace(
                                               np.log10(3), np.log10(180), 8),
                                           output='both',
                                           cpus=8)

        pow_wavelet_multi, phase_wavelet_multi = wf_cpp_multi.filter()

        wf_cpp = MorletWaveletFilter(timeseries=self.base_eegs,
                                     freqs=np.logspace(np.log10(3),
                                                       np.log10(180), 8),
                                     output='both',
                                     cpus=1)

        pow_wavelet, phase_wavelet = wf_cpp.filter()

        assert_array_equal(pow_wavelet_multi, pow_wavelet)
        assert_array_equal(phase_wavelet_multi, phase_wavelet)

    def test_wavelets_cpp(self):
        eegs = self.eegs[:, :, :-1]
        base_eegs = self.base_eegs

        # if not sys.platform.startswith('win'):
        #     sys.path.append('/Users/m/src/morlet_git_install')
        import ptsa.extensions.morlet as morlet
        num_freqs = 8
        f_min = 3.0
        f_max = 180.0
        signal_length = base_eegs.shape[-1]
        morlet_transform = morlet.MorletWaveletTransform()
        samplerate = float(base_eegs['samplerate'])
        morlet_transform.init(5, f_min, f_max, num_freqs, samplerate,
                              signal_length)

        signal = base_eegs[0:1, 0:1, :]
        signal_orig_eegs = eegs[0:1, 0:1, :]

        pow_wavelets_cpp = np.empty(shape=(base_eegs.shape[-1] * num_freqs, ),
                                    dtype=np.float)

        # for i in xrange(num_of_iterations):
        #     morlet_transform.multiphasevec(signal,powers)
        morlet_transform.multiphasevec(signal.data.flatten(), pow_wavelets_cpp)

        pow_wavelets_cpp = pow_wavelets_cpp.reshape(
            8, pow_wavelets_cpp.shape[0] / 8)

        wf = MorletWaveletFilter(
            timeseries=signal,
            freqs=np.logspace(np.log10(f_min), np.log10(f_max), num_freqs),
            output='power',
            frequency_dim_pos=0,
        )

        pow_wavelet, phase_wavelet = wf.filter()

        from ptsa.wavelet import phase_pow_multi
        pow_wavelet_ptsa_orig = phase_pow_multi(freqs=np.logspace(
            np.log10(3), np.log10(180), 8),
                                                dat=signal_orig_eegs,
                                                to_return='power')

        freq_num = 0

        decimal = 1

        assert_array_almost_equal(
            (np.squeeze(pow_wavelet[freq_num, :, :, 500:-500]) -
             np.squeeze(pow_wavelet_ptsa_orig[freq_num, :, :, 500:-500])) /
            np.squeeze(pow_wavelet_ptsa_orig[freq_num, :, :, 500:-500]),
            np.zeros_like(
                np.squeeze(pow_wavelet_ptsa_orig[freq_num, :, :, 500:-500])),
            decimal=decimal)

        assert_array_almost_equal(
            (pow_wavelets_cpp[freq_num, 500:-500] -
             np.squeeze(pow_wavelet[freq_num, :, :, 500:-500])) /
            pow_wavelets_cpp[freq_num, 500:-500],
            np.zeros_like(pow_wavelets_cpp[freq_num, 500:-500]),
            decimal=decimal)

        #
        assert_array_almost_equal(
            (pow_wavelets_cpp[freq_num, 500:-500] -
             np.squeeze(pow_wavelet_ptsa_orig[freq_num, :, :, 500:-500])) /
            pow_wavelets_cpp[freq_num, 500:-500],
            np.zeros_like(
                np.squeeze(pow_wavelet_ptsa_orig[freq_num, :, :, 500:-500])),
            decimal=decimal)

        from ptsa.wavelet import phase_pow_multi

    def test_event_data_chopper(self):

        dataroot = self.base_events[0].eegfile
        from ptsa.data.readers import EEGReader
        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=0.0,
                           end_time=1.6,
                           buffer_time=1.0)
        chopped_session = sedc.filter()

        sedc_so = DataChopper(start_offsets=self.base_events.eegoffset,
                              session_data=session_eegs,
                              start_time=0.0,
                              end_time=1.6,
                              buffer_time=1.0)
        chopped_session_so = sedc_so.filter()

        assert_array_equal(chopped_session, chopped_session_so)

    def test_wavelets_synthetic_data(self):
        samplerate = 1000.
        frequency = 180.0
        modulation_frequency = 80.0

        duration = 1.0

        n_points = int(np.round(duration * samplerate))
        x = np.arange(n_points, dtype=np.float)
        y = np.sin(x * (2 * np.pi * frequency / n_points))
        y_mod = np.sin(x * (2 * np.pi * frequency / n_points)) * np.sin(
            x * (2 * np.pi * modulation_frequency / n_points))

        ts = TimeSeries(y, dims=['time'], coords=[x])
        ts['samplerate'] = samplerate
        ts.attrs['samplerate'] = samplerate

        frequencies = [10.0, 30.0, 50.0, 80., 120., 180., 250.0, 300.0, 500.]
        for frequency in frequencies:
            wf = MorletWaveletFilter(timeseries=ts,
                                     freqs=np.array([frequency]),
                                     output='both',
                                     frequency_dim_pos=0,
                                     verbose=True)

            pow_wavelet, phase_wavelet = wf.filter()

            from ptsa.wavelet import phase_pow_multi

            pow_wavelet_ptsa_orig = phase_pow_multi(freqs=[frequency],
                                                    samplerates=samplerate,
                                                    dat=ts.data,
                                                    to_return='power')

            assert_array_almost_equal(
                (pow_wavelet_ptsa_orig - pow_wavelet) / pow_wavelet_ptsa_orig,
                np.zeros_like(pow_wavelet),
                decimal=6)
Exemplo n.º 25
0
newevs_stimParams = np.rec.recarray(len(matevs),dtype=dtypes_stimParams)

# sys.exit()

for field in matevs.dtype.names:
    try:
        newevs[field] = matevs[field]
    except ValueError:
        if ((field == 'stimAmp') and
            (np.all(np.array(matevs[field],np.str)=='X'))):
            matevs[field].fill(np.nan)
            newevs[field] = matevs[field]
        else: # not sure what's going on, so raise the same error as above
            newevs[field] = matevs[field]

events = Events(newevs)
events = events.add_fields(esrc=np.dtype(RawBinWrapper))


good_indices = np.ones(len(events),np.bool)
for e,event in enumerate(events):
    try:
        event['esrc'] = RawBinWrapper(event['eegfile'])
    except IOError:
        print('No EEG files for',event['subject'],event['session'],event['eegfile'])
        good_indices[e] = False
        
events = events[good_indices]

start_time = -0.6
end_time = 1.6
Exemplo n.º 26
0
e_path = '/Users/m/data/events/RAM_FR1/R1060M_events.mat'
base_e_reader = BaseEventReader(event_file=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']

base_e_reader_ptsa = BaseEventReader(event_file=e_path,
                                     eliminate_events_with_no_eeg=True,
                                     use_ptsa_events_class=False)
base_events_ptsa = base_e_reader_ptsa.read()

base_events_ptsa = base_events_ptsa[base_events_ptsa.type == 'WORD']
base_events_ptsa = Events(base_events_ptsa)

base_events_ptsa = attach_rawbinwrappers(base_events_ptsa)

print base_events_ptsa

base_ev_data_ptsa, base_ev_data_xray = base_events_ptsa.get_data(
    channels=channels,
    start_time=0.0,
    end_time=1.6,
    buffer_time=1.0,
    eoffset='eegoffset',
    keep_buffer=True,
    eoffset_in_time=False,
    verbose=True,
    return_both=True)
Exemplo n.º 27
0
newevs_stimParams = np.rec.recarray(len(matevs), dtype=dtypes_stimParams)

# sys.exit()

for field in matevs.dtype.names:
    try:
        newevs[field] = matevs[field]
    except ValueError:
        if ((field == 'stimAmp')
                and (np.all(np.array(matevs[field], np.str) == 'X'))):
            matevs[field].fill(np.nan)
            newevs[field] = matevs[field]
        else:  # not sure what's going on, so raise the same error as above
            newevs[field] = matevs[field]

events = Events(newevs)
events = events.add_fields(esrc=np.dtype(RawBinWrapper))

good_indices = np.ones(len(events), np.bool)
for e, event in enumerate(events):
    try:
        event['esrc'] = RawBinWrapper(event['eegfile'])
    except IOError:
        print('No EEG files for', event['subject'], event['session'],
              event['eegfile'])
        good_indices[e] = False

events = events[good_indices]

start_time = -0.6
end_time = 1.6
Exemplo n.º 28
0
    },
    'catFR2':{
        'output_path': rhino_mount+'/data10/scratch/cweidema/RAM/RAM_catFR/RAM_catFR2_power/hdf5_files_sess',
        'ev_file': rhino_mount+'/home1/cweidema/Christoph/Analyses/RAM/RAM_catFR/data/RAM_catFR2_events20151106.npy'
    },
    'FR1':{
        'output_path': rhino_mount+'/data10/scratch/cweidema/RAM/RAM_FR/RAM_FR1_power/hdf5_files_sess',
        'ev_file': rhino_mount+'/home1/cweidema/Christoph/Analyses/RAM/RAM_FR/data/RAM_FR1_events20151106.npy'
    },
    'FR2':{
        'output_path': rhino_mount+'/data10/scratch/cweidema/RAM/RAM_FR/RAM_FR2_power/hdf5_files_sess',
        'ev_file': rhino_mount+'/home1/cweidema/Christoph/Analyses/RAM/RAM_FR/data/RAM_FR2_events20151106.npy'
    }
}

ev = Events(np.load(pow_params[exp]['ev_file']))
ev = ev.add_fields(esrc=np.dtype(RawBinWrapper))
ev = ev[ev['type']=='WORD']
good_indices = np.ones(len(ev),np.bool)
for e,event in enumerate(ev):
    try:
        event['esrc'] = RawBinWrapper(rhino_mount+event['eegfile'])
    except IOError:
        print('No EEG files for',event['subject'],event['session'],event['eegfile'])
        good_indices[e] = False
        
ev = ev[good_indices]

start_time = -0.6
end_time = 1.6
buf = 1
Exemplo n.º 29
0
channels = ['002', '003', '004', '005']

from ptsa.data.readers import BaseEventReader

e_path = '/Users/m/data/events/RAM_FR1/R1060M_events.mat'
base_e_reader = BaseEventReader(event_file=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']

base_e_reader_ptsa = BaseEventReader(event_file=e_path, eliminate_events_with_no_eeg=True, use_ptsa_events_class=False)
base_events_ptsa = base_e_reader_ptsa.read()

base_events_ptsa = base_events_ptsa[base_events_ptsa.type == 'WORD']
base_events_ptsa = Events(base_events_ptsa)

base_events_ptsa = attach_rawbinwrappers(base_events_ptsa)

print base_events_ptsa

base_ev_data_ptsa, base_ev_data_xray = base_events_ptsa.get_data(channels=channels, start_time=0.0, end_time=1.6,
                                                                 buffer_time=1.0, eoffset='eegoffset', keep_buffer=True,
                                                                 eoffset_in_time=False, verbose=True, return_both=True)


# casting data DataArray into TimeSeriesXray
base_ev_data_xray = TimeSeriesXray(base_ev_data_xray)


# baseline_corrected test
Exemplo n.º 30
0
def get_sub_events(task, sub, eliminate_events_with_no_eeg=True, rhino_prefix='/', ignore_no_params=False):
    """Returns all events for a given task and subject identifier

    Keyword arguments:
    task -- string of task name
    sub -- string of subject identifier
    eliminate_events_with_no_eeg -- (default: True) Do not return events for which no corresponding eeg file exists.
    rhino_prefix -- (default: '/') To get data if rhino is mounted as an external volume from a local computer.
    ignore_no_params -- (default: False) Automatically handles IOError when no params.txt file is found for a subject.

    """
    from MatlabIO import read_single_matlab_matrix_as_numpy_structured_array
    
    e_path = rhino_prefix+'/data/events/'+task+'/'+sub+'_events.mat'    
    
    # extract matlab matrix (called 'events') as numpy structured array
    struct_array = read_single_matlab_matrix_as_numpy_structured_array(e_path, 'events')

    evs = Events(struct_array)

    if eliminate_events_with_no_eeg:

        # eliminating events that have no eeg file
        indicator = np.empty(len(evs), dtype=bool)
        indicator[:] = False
        for i, ev in enumerate(evs):
            indicator[i] = (type(evs[i].eegfile).__name__.startswith('unicode')) & (len(str(evs[i].eegfile)) > 3)

        #Due to numpy upgrade
        evs = Events(evs[indicator])
    
    evs = evs.add_fields(esrc=np.dtype(RawBinWrapper))

    import pathlib

    #This may throw and error for a few subjects who have no params file. Should be written to handle this elegantly.

    if ignore_no_params:
        try:
            for ev in evs:
                try:
                    eeg_file_path = join(rhino_prefix, str(pathlib.Path(str(ev.eegfile)).parts[1:]))
                    ev.esrc = RawBinWrapper(eeg_file_path)
                    #self.raw_data_root=str(eeg_file_path)
                except TypeError:
                    print 'skipping event with eegfile=',ev.eegfile
                    pass
        except IOError:
            print('No params.txt or .params file found for '+sub)

    else:
        for ev in evs:
            try:
                eeg_file_path = join(rhino_prefix, str(pathlib.Path(str(ev.eegfile)).parts[1:]))
                ev.esrc = RawBinWrapper(eeg_file_path)
                #self.raw_data_root=str(eeg_file_path)
            except TypeError:
                print 'skipping event with eegfile=',ev.eegfile
                pass

    return(evs)
Exemplo n.º 31
0
    def run(self):


        # PTSA EVENT  READER

        # e_path = join(self.pipeline.mount_point, 'data/events', self.pipeline.task,self.pipeline.subject+'_events.mat')
        e_path = '/Users/m/data/events/RAM_FR1/R1060M_events.mat'
        # e_path = '/Users/m/data/events/RAM_FR1/R1056M_events.mat'
        e_reader = PTSAEventReader(event_file=e_path, eliminate_events_with_no_eeg=True)

        e_reader.read()

        events = e_reader.get_output()

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

        events = events[0:30]

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



        events = Events(events) # necessary workaround for new numpy
        print 'events=',events

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

        print eegs

        # BASE READER


        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']

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

        base_events = base_events[0:30]


        print 'base_events=',base_events


        from ptsa.data.readers.TimeSeriesEEGReader import TimeSeriesEEGReader

        time_series_reader = TimeSeriesEEGReader(base_events)

        time_series_reader.start_time = 0.0
        time_series_reader.end_time = 1.6
        time_series_reader.buffer_time = 1.0
        time_series_reader.keep_buffer = False

        time_series_reader.read(channels=['002','003'])


        base_eegs = time_series_reader.get_output()
        print