tasks.py 16.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from data_preparation.preparator import Preparator
from preparation_config import preparation_config as config
import numpy as np


def left_right_task_data_preparation():
    # We use the antisaccade dataset to extract the data for left and right benchmark task.
    saccade = config['saccade_trigger']
    fixation = config['fixation_trigger']
    cue = config['antisaccade']['cue_trigger']

    preparator = Preparator(load_directory=config['LOAD_ANTISACCADE_PATH'], save_directory=config['SAVE_ANTISACCADE_PATH'],
                            load_file_pattern=config['ANTISACCADE_FILE_PATTERN'], save_file_name='LeftRightTask_data.npz', verbose=True)

    preparator.extract_data_at_events(extract_pattern=[cue], name_start_time='Beginning of cue', start_time=lambda events: events['latency'],
                                                             name_length_time='Size blocks of 500', length_time=500,
                                                             start_channel=1, end_channel=129, padding=False)
    preparator.blocks(on_blocks=['20'], off_blocks=['30']) # take only blocks of pro-saccade
    preparator.addFilter(name='Keep immediate saccade', f=lambda events: events['type'].isin(cue) & events['type'].shift(-1).isin(saccade))
    preparator.addFilter(name='Keep right direction', f=lambda events: (events['type'].isin(['10']) & events['type'].shift(-1).isin(saccade) & (events['end_x'].shift(-1) < 400))
                                                                     | (events['type'].isin(['11']) & events['type'].shift(-1).isin(saccade) & (events['end_x'].shift(-1) > 400)))
Martynka's avatar
Martynka committed
22
    preparator.addFilter(name='Keep saccade if it comes after a reasonable reaction time', f=lambda events: events['latency'].shift(-1) - events['latency'] > 50)
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    preparator.addFilter(name='Keep only the ones with big enough amplitude', f=lambda events: events['amplitude'].shift(-1) > 2)
    preparator.addLabel(name='Giving label 0 for left and 1 for right', f=lambda events: events['type'].apply(lambda x: 0 if x == '10' else 1))
    preparator.run()


def left_right_task_feature_extraction_data_preparation():
    # We use the antisaccade dataset to extract the data for left and right benchmark task.
    saccade = config['saccade_trigger']
    cue = config['antisaccade']['cue_trigger']
    fixation = config['fixation_trigger']
    preparator = Preparator(load_directory=config['LOAD_ANTISACCADE_PATH'], save_directory=config['SAVE_ANTISACCADE_PATH'],
                            load_file_pattern=config['ANTISACCADE_FILE_PATTERN'], save_file_name='LeftRightTask_data_feature_extracted.npz', verbose=True)

    preparator.extract_data_at_events(extract_pattern=[saccade], name_start_time='Beginning of saccade', start_time=lambda events: events['latency'],
                                                                 name_length_time='Fixed blocks of 1', length_time=1,
                                                                 start_channel=1, end_channel=129, padding=False)  # we are interested only on the saccade events where the feature were extracted (hilbert trafo)
    preparator.blocks(on_blocks=['20'], off_blocks=['30'])  # take only blocks of pro-saccade
    preparator.addFilter(name='Keep saccade that has previously a cue', f=lambda events: events['type'].isin(saccade) & events['type'].shift(1).isin(cue))
    preparator.addFilter(name='Keep right direction', f=lambda events: (events['type'].shift(1).isin(['10']) & events['type'].isin(saccade) & (events['end_x'] < 400))
                                                                     | (events['type'].shift(1).isin(['11']) & events['type'].isin(saccade) & (events['end_x'] > 400)))
Martynka's avatar
Martynka committed
43
    preparator.addFilter(name='Keep saccade if it comes after a reasonable reaction time', f=lambda events: events['latency'] - events['latency'].shift(1) > 50)
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
    preparator.addFilter(name='Keep only the ones with big enough amplitude', f=lambda events: events['amplitude'] > 2)
    preparator.addLabel(name='Giving label 0 for left and 1 for right', f=lambda events: events['type'].shift(1).apply(lambda x: 0 if x == '10' else 1))
    preparator.run()


def direction_task_data_preparation():
    # We use the 'dots' dataset for direction task
    fixation = config['fixation_trigger']
    saccade = config['saccade_trigger']
    cue = config['dots']['cue_trigger']
    end_cue = config['dots']['end_cue_trigger']

    preparator = Preparator(load_directory=config['LOAD_DOTS_PATH'], save_directory=config['SAVE_DOTS_PATH'],
                            load_file_pattern=config['DOTS_FILE_PATTERN'], save_file_name='DirectionTask_data.npz', verbose=True)
    # no padding, but cut 500 somewhere in between
    # we are interested only on the 5-triggers (fixation 41 cue saccade fixation) and we cut 500 data points in the middle
    preparator.extract_data_at_events(extract_pattern=[fixation, end_cue, cue, saccade, fixation], name_start_time='250 timepoints before the saccade',
                                                                                                   start_time=lambda events: (events['latency'].shift(-3) - 250),
                                                                                                   name_length_time='Fixed blocks of 500', length_time=500,
                                                                                                   start_channel=1, end_channel=129, padding=False)
    preparator.blocks(on_blocks=['55'], off_blocks=['56'])  # take only blocks 55
    preparator.ignoreEvent(name='Ignore microsaccades', f=(lambda events: events['type'].isin(saccade) & (events['amplitude'] < 2)))
    preparator.ignoreEvent(name='Ignore microfixations', f=(lambda events: events['type'].isin(fixation) & (events['duration'] < 150)))
Martynka's avatar
Martynka committed
67
    preparator.addFilter(name='Keep saccade if it comes after a reasonable RT', f=lambda events: events['latency'].shift(-3) - events['latency'].shift(-2) > 50)
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    preparator.addLabel(name='amplitude', f=lambda events: np.sqrt((events['end_x'].shift(-3) - events['start_x'].shift(-3))**2 + (events['end_y'].shift(-3) - events['start_y'].shift(-3))**2))
    preparator.addLabel(name='angle', f=lambda events: np.arctan2(events['end_y'].shift(-3) - events['start_y'].shift(-3), events['end_x'].shift(-3) - events['start_x'].shift(-3)))
    preparator.run()


def direction_task_feature_extraction_data_preparation():
    # We use the 'dots' dataset for direction task
    fixation = config['fixation_trigger']
    saccade = config['saccade_trigger']
    end_trigger = ['41']
    cue = config['dots']['cue_trigger']

    # fixation, 41, cue, saccade, fixation
    # no padding, but cut 500 somewhere in between
    preparator = Preparator(load_directory=config['LOAD_DOTS_PATH'], save_directory=config['SAVE_DOTS_PATH'],
                            load_file_pattern=config['DOTS_FILE_PATTERN'], save_file_name='DirectionTask_data_feature_extracted.npz', verbose=True)
    preparator.extract_data_at_events(extract_pattern=[saccade], name_start_time='At saccade on-set',
                                                                 start_time=lambda events: (events['latency']),
                                                                 name_length_time='Fixed blocks of 1', length_time=1,
                                                                 start_channel=1, end_channel=129, padding=False)  # we are interested only on the saccade on-set (where the features are extracted)
    preparator.blocks(on_blocks=['55'], off_blocks=['56'])  # take only blocks 55
    preparator.ignoreEvent(name='Ignore microsaccades', f=lambda events: events['type'].isin(saccade) & (events['amplitude'] < 2))
    preparator.ignoreEvent(name='Ignore microfixations', f=lambda events: events['type'].isin(fixation) & (events['duration'] < 150))
    preparator.addFilter(name='Keep only the saccades inside a tuple (fixation, 41, cue, saccade, fixation)', f=lambda events: events['type'].shift(3).isin(fixation)
                                                                                                                              & events['type'].shift(2).isin(end_trigger)
                                                                                                                              & events['type'].shift(1).isin(cue)
                                                                                                                              & events['type'].isin(saccade)
                                                                                                                              & events['type'].shift(-1).isin(fixation))
Martynka's avatar
Martynka committed
96
    preparator.addFilter(name='Keep saccade if it comes after a reasonable RT', f=lambda events: (events['latency'] - events['latency'].shift(1) > 50))
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
    preparator.addLabel(name='amplitude', f=lambda events: np.sqrt((events['end_x'] - events['start_x'])**2 + (events['end_y'] - events['start_y']) ** 2))
    preparator.addLabel(name='angle', f=lambda events: np.arctan2(events['end_y'] - events['start_y'], events['end_x'] - events['start_x']))
    preparator.run()

def direction_with_processing_speed_dataset():
    # fixation, saccade, fixation
    # saccade should be bigger than 0.5 (amplitude) (Repetition)

    # We use the 'processing_speed' dataset for this direction task
    fixation = config['fixation_trigger']
    saccade = config['saccade_trigger']

    preparator = Preparator(load_directory=config['LOAD_PROCESSING_SPEED_PATH'], save_directory=config['SAVE_PROCESSING_SPEED_PATH'],
                            load_file_pattern=config['PROCESSING_SPEED_FILE_PATTERN'], save_file_name='DirectionTask_pretraining_data.npz', verbose=True)

    preparator.extract_data_at_events(extract_pattern=[fixation, saccade, fixation], name_start_time='at the fixation start',
                                                                                     start_time=lambda events: (events['latency']),
                                                                                     name_length_time='Fixed blocks of 500', length_time=500,
                                                                                     start_channel=1, end_channel=129, padding=True)  # we are interested only on the triplets, fixation saccade fixation
    preparator.addFilter(name='Keep only long enough fixations1', f=lambda events:events['duration'] > 50)
    preparator.addFilter(name='Keep only big enough saccade', f=lambda events: events['amplitude'].shift(-1) > 0.5)
    preparator.addFilter(name='Keep only long enough fixations2', f=lambda events: events['duration'].shift(-2) > 50)
    preparator.addLabel(name='angle', f=lambda events: np.sqrt((events['end_x'].shift(-1) - events['start_x'].shift(-1)) ** 2 + (events['end_y'].shift(-1) - events['start_y'].shift(-1)) ** 2))
    preparator.addLabel(name='amplitude', f=lambda events: np.arctan2(events['end_y'].shift(-1) - events['start_y'].shift(-1), events['end_x'].shift(-1) - events['start_x'].shift(-1)))
    preparator.run()

def direction_with_processing_speed_feature_extraction_dataset():
    # fixation, saccade, fixation
    # length should be 500
    # the fixations should be at least 50 (100 ms)
    # and if together they are more than 500 cut it, if they are less pad it (with 0s)
    # saccade should be bigger than 0.5 (amplitude) (Repetition)

    # We use the 'processing_speed' dataset for this direction task
    fixation = config['fixation_trigger']
    saccade = config['saccade_trigger']

    preparator = Preparator(load_directory=config['LOAD_PROCESSING_SPEED_PATH'], save_directory=config['SAVE_PROCESSING_SPEED_PATH'],
                            load_file_pattern=config['PROCESSING_SPEED_FILE_PATTERN'], save_file_name='DirectionTask_pretraining_data_feature_extracted.npz')

Ard Kastrati's avatar
Ard Kastrati committed
137
138
139
140
    preparator.extract_data_at_events(extract_pattern=[fixation, saccade, fixation], name_start_time='at saccade onset',
                                                                                     start_time=lambda events: (events['latency'].shift(-1)),
                                                                                     name_length_time='Size 1', length_time=1,
                                                                                     start_channel=1, end_channel=129, padding=False)  # we are interested only on saccade on-set (where the data is feature extracted by hilbert trafo)
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
    preparator.addFilter(name='Keep only saccades around fixations', f=lambda events: events['type'].shift(1).isin(fixation) & events['type'].shift(1).isin(fixation))
    preparator.addFilter(name='Keep only long enough fixations1', f=lambda events: events['duration'].shift(1) > 50)
    preparator.addFilter(name='Keep only big enough saccade', f=lambda events: events['amplitude'] > 0.5)
    preparator.addFilter(name='Keep only long enough fixations2', f=lambda events: events['duration'].shift(-1) > 50)
    preparator.addLabel(name='angle', f=lambda events: np.sqrt((events['end_x'] - events['start_x']) ** 2 + (events['end_y'] - events['start_y']) ** 2))
    preparator.addLabel(name='amplitude', f=lambda events: np.arctan2(events['end_y'] - events['start_y'], events['end_x'] - events['start_x']))
    preparator.run()

def position_task_data_preparation():
    # We use the 'dots' dataset for position task
    fixation = config['fixation_trigger']

    preparator = Preparator(load_directory=config['LOAD_DOTS_PATH'], save_directory=config['SAVE_DOTS_PATH'],
                            load_file_pattern=config['DOTS_FILE_PATTERN'], save_file_name='PositionTask_data.npz')
    # no padding, but cut 500 somewhere in between
    preparator.extract_data_at_events(extract_pattern=[fixation], fixed_length=500)  # we are interested only on the fixations (at a dot)
    preparator.blocks(on_blocks=['55'], off_blocks=['56'])  # take only blocks 55
    preparator.addFilter(name='Keep fixation that are long enough', f=lambda events: events['duration'] >= 500)
    preparator.addLabel(name='x_position', f=lambda events: events['avgpos_x'])
    preparator.addLabel(name='y_position', f=lambda events: events['avgpos_y'])


def position_task_feature_extraction_data_preparation():
    # We use the 'dots' dataset for position task
    fixation = config['fixation_trigger']

    preparator = Preparator(load_directory=config['LOAD_DOTS_PATH'], save_directory=config['SAVE_DOTS_PATH'],
                            load_file_pattern=config['DOTS_FILE_PATTERN'], save_file_name='PositionTask_data.npz')
    # no padding, but cut 500 somewhere in between
    preparator.extract_data_at_events(extract_pattern=[fixation])  # we are interested only on the fixations (at a dot)
    preparator.blocks(on_blocks=['55'], off_blocks=['56'])  # take only blocks 55
    preparator.addFilter(name='Keep fixation that are long enough', f=lambda events: events['duration'] >= 150)
    preparator.addLabel(name='x_position', f=lambda events: events['avgpos_x'])
    preparator.addLabel(name='y_position', f=lambda events: events['avgpos_y'])

def segmentation_task_data_preparation():
    raise NotImplementedError()

def segmentation_task_feature_extraction_data_preparation():
    raise NotImplementedError

def main():

    if config['task'] == 'LR_task' and config['feature_extraction'] == False:
        left_right_task_data_preparation()
    elif config['task'] == 'LR_task' and config['feature_extraction'] == True:
        left_right_task_feature_extraction_data_preparation()
    elif config['task'] == 'Direction_task' and config['feature_extraction'] == False:
        if config['dataset'] == 'dots':
            direction_task_data_preparation()
        elif config['dataset'] == 'processing_speed':
            direction_with_processing_speed_dataset()
        else:
            raise ValueError("This task cannot be prepared with the given dataset.")
    elif config['task'] == 'Direction_task' and config['feature_extraction'] == True:
        if config['dataset'] == 'dots':
            direction_task_feature_extraction_data_preparation()
        elif config['dataset'] == 'processing_speed':
            direction_with_processing_speed_feature_extraction_dataset()
        else:
            raise ValueError("This task cannot be prepared with the given dataset.")
    elif config['task'] == 'Position_task' and config['feature_extraction'] == False:
        position_task_data_preparation()
    elif config['task'] == 'Position' and config['feature_extraction'] == True:
        position_task_feature_extraction_data_preparation()
    elif config['task'] == 'Segmentation_task' and config['feature_extraction'] == False:
        segmentation_task_data_preparation()
    elif config['task'] == 'Segmentation_task' and config['feature_extraction'] == True:
        segmentation_task_feature_extraction_data_preparation()
    else:
        raise ValueError("Task not implemented yet.")

main()