-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathld_probe.py
459 lines (362 loc) · 21.9 KB
/
ld_probe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
96
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
137
138
139
140
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
from setup import *
def get_last_day_difficulty(df):
"""
This function gets the last day of each difficulty for each LD Probe block. Since each block is 4 days by default,
this function will return every 2nd and 4th day. If there are multiple blocks being passed in, the function will
accurately assign which block the animal's row belongs to.
:param df: A dataframe that represents all the cleaned LD Probe data.
"""
df['Day'] = df.groupby('ID').cumcount() + 1
df['Day'] = df['Day'].astype(float)
df['Block'] = np.ceil(df['Day'] / 4)
df.drop(df.loc[df['Day'] % 2 == 1].index, inplace=True)
df.sort_values(by=['Block', 'Type', 'ID'], inplace=True)
def get_first_day_difficulty(df):
"""
This function gets the first day of each difficulty for each LD Probe block. Since each block is 4 days by default,
this function will return every 1st and 3rd day. If there are multiple blocks being passed in, the function will
accurately assign which block the animal's row belongs to.
:param df: A dataframe that represents all the cleaned LD Probe data.
"""
df['Day'] = df.groupby('ID').cumcount() + 1
df['Day'] = df['Day'].astype(float)
df['Block'] = np.ceil(df['Day'] / 4)
df.drop(df.loc[df['Day'] % 2 == 0].index, inplace=True)
df.sort_values(by=['Block', 'Type', 'ID'], inplace=True)
def ld_probe_delete_other_difficulties(df):
"""
This function deletes the other location discrimination difficulties that are not used in LD Probe. Since both LD
Train and LD Probe run on the same ABET schedule, we separate LD Train and LD Probe by LD difficulty. In LD Probe,
we want all the easy and hard rows. This function will drop all the intermediate and undetermined rows from the
working dataframe
:param df: A dataframe that represents all the cleaned LD Probe data.
"""
# for ld train, we want only intermediate, delete all others
df.drop(df.loc[df['Type'] == 'intermediate'].index, inplace=True)
df.drop(df.loc[df['Type'] == 'undetermined'].index, inplace=True)
df.sort_values(['Date', 'ID'], ascending=[1, 1], inplace=True)
df['Day'] = df.groupby('ID').cumcount() + 1
def ld_probe_last_day_difficulty():
"""
This function creates a csv file for the LD Probe test. Each row will be the last day of each difficulty within each
block. Afterward, the function will ask the user to save the newly created csv file in a directory.
"""
df = data_setup('LD Probe')
if df is not None:
ld_probe_delete_other_difficulties(df)
get_last_day_difficulty(df)
save_file_message(df)
def ld_probe_first_day_difficulty():
"""
This function creates a csv file for the LD Probe test. Each row will be the last day of each difficulty within each
block. Afterward, the function will ask the user to save the newly created csv file in a directory.
"""
df = data_setup('LD Probe')
if df is not None:
ld_probe_delete_other_difficulties(df)
get_first_day_difficulty(df)
save_file_message(df)
def ld_probe_widget_check(widget):
"""
This function checks the LD Probe select id and select day widgets. It checks if the value given is a valid numeric
value, otherwise it print an error message and return None.
:param widget: An entry widget that contains the value of the selected id/day.
:return: None: If the function doesn't pass the widget check, it will stop and return None.
"""
try:
widget_value = int(widget.get())
except ValueError:
mb.showerror('LD Probe Error', 'ld_probe_widget_check() error: The selected day/id is invalid or empty!')
print('ld_probe_widget_check() error: The selected day/id is invalid or empty!')
return None
return widget_value
def ld_probe_select_day(enter_day):
"""
This function creates a csv file for the LD Probe test. Each row will be the selected day for each animal.
Afterward, the function will ask the user to save the newly created csv file in a directory.
:param enter_day: An entry widget that contains the value for the selected day.
:return: None: If the function doesn't pass the widget check, it will stop and return None.
"""
if ld_probe_widget_check(enter_day) is not None:
selected_day = ld_probe_widget_check(enter_day)
else:
mb.showerror('LD Probe Error', 'ld_probe_select_id() error: The selected day criteria is empty or invalid!')
print('ld_probe_select_id() error: The selected day criteria is empty or invalid!')
return None
df = data_setup('LD Probe')
if df is not None:
ld_probe_delete_other_difficulties(df)
df = df.loc[df['Day'] == selected_day]
save_file_message(df)
def ld_probe_select_id(enter_id):
"""
This function creates a csv file for the LD Probe test. Each row will be all the rows for the selected animal id.
Afterward, the function will ask the user to save the newly created csv file in a directory.
:param enter_id: An entry widget that contains the value for the selected id.
:return: None: If the function doesn't pass the widget check, it will stop and return None.
"""
if ld_probe_widget_check(enter_id) is not None:
selected_id = ld_probe_widget_check(enter_id)
else:
mb.showerror('LD Probe Error', 'ld_probe_select_id() error: The selected id criteria is empty or invalid!')
print('ld_probe_select_id() error: The selected id criteria is empty or invalid!')
return None
df = data_setup('LD Probe')
if df is not None:
ld_probe_delete_other_difficulties(df)
df = df.loc[df['ID'] == selected_id]
save_file_message(df)
def ld_probe_select_block(block_number):
"""
This function creates a csv file for the LD Probe test. Each row will be the last days of a specific block. This
function works better if there are multiple blocks that are being entered. For single blocks, please use the Last
Day Difficulty All button! Afterward, the function will ask the user to save the newly created csv file in a
directory.
:param block_number: An entry widget that contains the value for the block number.
:return: None: If the function doesn't pass the widget check, it will stop and return None.
"""
if ld_probe_widget_check(block_number) is not None:
selected_block = ld_probe_widget_check(block_number)
else:
mb.showerror('LD Probe Error', 'ld_probe_select_block() error: The block number criteria is empty or invalid!')
print('ld_probe_select_block() error: The block number criteria is empty or invalid!')
return None
block_day_range_max = 4 * selected_block
block_day_range_min = block_day_range_max - 3
block_day_total_range = [*range(block_day_range_min, block_day_range_max + 1, 1)]
df = data_setup('LD Probe')
if df is not None:
ld_probe_delete_other_difficulties(df)
df = df.loc[(df['Day'] == block_day_total_range[1]) | (df['Day'] == block_day_total_range[3])]
save_file_message(df)
def averaging_process(df):
"""
This function does inter-difficulty averages. It will calculate the averages for the two easy days and the two hard
days and return them as separate rows. If the animal reaches a criteria on one day and does not reach the same
criteria the next day, the average will automatically be the result of the day that it actually reached the
criteria.
:param df: A dataframe that contains all the cleaned LD Probe data with only easy and hard rows.
:return: new_df: A dataframe that contains all the inter-difficulty averages.
"""
new_df = pd.DataFrame()
row_index = 0
while row_index < df.shape[0] - 1:
rows_to_avg = list()
# average two rows at a time
for avg_index in range(2):
row_to_add = df.loc[avg_index + row_index]
rows_to_avg.append(row_to_add)
# check that the ids are same and the days are 1 day apart and then does the averaging
if rows_to_avg[0]['Day'] + 1 == rows_to_avg[1]['Day'] and rows_to_avg[0]['ID'] == rows_to_avg[1]['ID']:
temp_row = (rows_to_avg[0][3:18] + rows_to_avg[1][3:18]) / 2
temp_row['Date'] = rows_to_avg[1]['Date']
temp_row['Day'] = rows_to_avg[1]['Day']
temp_row['Type'] = rows_to_avg[1]['Type']
temp_row['ID'] = rows_to_avg[1]['ID']
if np.isnan(temp_row['SessionLengthTo2ndReversalDuration']) and not np.isnan(
rows_to_avg[0]['SessionLengthTo2ndReversalDuration']):
temp_row['SessionLengthTo2ndReversalDuration'] = rows_to_avg[0]['SessionLengthTo2ndReversalDuration']
if np.isnan(temp_row['SessionLengthTo2ndReversalDuration']) and not np.isnan(
rows_to_avg[1]['SessionLengthTo2ndReversalDuration']):
temp_row['SessionLengthTo2ndReversalDuration'] = rows_to_avg[1]['SessionLengthTo2ndReversalDuration']
if np.isnan(temp_row['NumberOfTrialTo2ndReversal']) and not np.isnan(
rows_to_avg[0]['NumberOfTrialTo2ndReversal']):
temp_row['NumberOfTrialTo2ndReversal'] = rows_to_avg[0]['NumberOfTrialTo2ndReversal']
if np.isnan(temp_row['NumberOfTrialTo2ndReversal']) and not np.isnan(
rows_to_avg[1]['NumberOfTrialTo2ndReversal']):
temp_row['NumberOfTrialTo2ndReversal'] = rows_to_avg[1]['NumberOfTrialTo2ndReversal']
if np.isnan(temp_row['PercentCorrectTo2ndReversal']) and not np.isnan(
rows_to_avg[0]['PercentCorrectTo2ndReversal']):
temp_row['PercentCorrectTo2ndReversal'] = rows_to_avg[0]['PercentCorrectTo2ndReversal']
if np.isnan(temp_row['PercentCorrectTo2ndReversal']) and not np.isnan(
rows_to_avg[1]['PercentCorrectTo2ndReversal']):
temp_row['PercentCorrectTo2ndReversal'] = rows_to_avg[1]['PercentCorrectTo2ndReversal']
new_df = new_df.append(temp_row, ignore_index=True)
row_index += 2
else:
row_index += 1
return new_df
def ld_probe_last_day_avg():
"""
This function creates a csv file for the LD Probe test. Each row will be the inter-difficulty averages for each
animal. Afterward, the function will ask the user to save the newly created csv file in a directory.
"""
df = data_setup('LD Probe')
if df is not None:
ld_probe_delete_other_difficulties(df)
df.sort_values(['ID', 'Day'], ascending=[1, 1], inplace=True)
df.reset_index(drop=True, inplace=True)
col_names = ['Date', 'ID', 'Type', 'Day', 'SessionLength', 'NumberOfTrial', 'PercentCorrect',
'NumberOfReversal',
'TotalITITouches', 'TotalBlankTouches', 'MeanRewardCollectionLatency', 'MeanCorrectTouchLatency',
'MeanIncorrectTouchLatency', 'SessionLengthTo1stReversalDuration',
'SessionLengthTo2ndReversalDuration',
'NumberOfTrialTo1stReversal', 'NumberOfTrialTo2ndReversal', 'PercentCorrectTo1stReversal',
'PercentCorrectTo2ndReversal']
new_df = averaging_process(df)
new_df = new_df[col_names]
save_file_message(new_df)
def ld_probe_block_average(block_number):
"""
This function creates a csv file for the LD Probe test. Each row will be the inter-difficulty averages for each
animal for a specific block. Afterward, the function will ask the user to save the newly created csv file
in a directory.
:param block_number: An entry widget that contains the value of the block number.
:return: None: If the function doesn't pass the widget check, it will stop and return None.
"""
if ld_probe_widget_check(block_number) is not None:
selected_block = ld_probe_widget_check(block_number)
else:
mb.showerror('LD Probe Error', 'ld_probe_select_block() error: The block number criteria is empty or invalid!')
print('ld_probe_select_block() error: The block number criteria is empty or invalid!')
return None
block_day_range_max = 4 * selected_block
block_day_range_min = block_day_range_max - 3
block_day_total_range = [*range(block_day_range_min, block_day_range_max + 1, 1)]
df = data_setup('LD Probe')
if df is not None:
ld_probe_delete_other_difficulties(df)
df = df.loc[df['Day'].isin(block_day_total_range)]
df.sort_values(['ID', 'Day'], ascending=[1, 1], inplace=True)
df.reset_index(drop=True, inplace=True)
col_names = ['Date', 'ID', 'Type', 'Day', 'SessionLength', 'NumberOfTrial', 'PercentCorrect',
'NumberOfReversal',
'TotalITITouches', 'TotalBlankTouches', 'MeanRewardCollectionLatency', 'MeanCorrectTouchLatency',
'MeanIncorrectTouchLatency', 'SessionLengthTo1stReversalDuration',
'SessionLengthTo2ndReversalDuration',
'NumberOfTrialTo1stReversal', 'NumberOfTrialTo2ndReversal', 'PercentCorrectTo1stReversal',
'PercentCorrectTo2ndReversal']
new_df = averaging_process(df)
new_df = new_df[col_names]
save_file_message(new_df)
def ld_probe_id_average(animal_id, difficulty_type):
"""
This function creates a csv file for the LD Probe test. It will return a row with the average performance of all the
test for a selected animal. Afterward, the function will ask the user to save the newly created csv file in a
directory.
:param animal_id: An entry widget that contains the value of the selected id.
:param difficulty_type: A string that determines which determines which difficulty to average.
"""
df = data_setup('LD Probe')
if df is not None:
ld_probe_delete_other_difficulties(df)
selected_id = int(animal_id.get())
df = df.loc[(df['ID'] == selected_id) & (df['Type'] == difficulty_type)]
df.sort_values(['ID', 'Day'], ascending=[1, 1], inplace=True)
df.reset_index(drop=True, inplace=True)
col_names = ['Date', 'ID', 'Type', 'Day', 'SessionLength', 'NumberOfTrial', 'PercentCorrect',
'NumberOfReversal',
'TotalITITouches', 'TotalBlankTouches', 'MeanRewardCollectionLatency', 'MeanCorrectTouchLatency',
'MeanIncorrectTouchLatency', 'SessionLengthTo1stReversalDuration',
'SessionLengthTo2ndReversalDuration',
'NumberOfTrialTo1stReversal', 'NumberOfTrialTo2ndReversal', 'PercentCorrectTo1stReversal',
'PercentCorrectTo2ndReversal']
avg_col = ['SessionLength', 'NumberOfTrial', 'PercentCorrect',
'NumberOfReversal',
'TotalITITouches', 'TotalBlankTouches', 'MeanRewardCollectionLatency', 'MeanCorrectTouchLatency',
'MeanIncorrectTouchLatency', 'SessionLengthTo1stReversalDuration',
'SessionLengthTo2ndReversalDuration',
'NumberOfTrialTo1stReversal', 'NumberOfTrialTo2ndReversal', 'PercentCorrectTo1stReversal',
'PercentCorrectTo2ndReversal']
new_df = df.copy(deep=True)
new_df = new_df[col_names]
for col in avg_col:
new_df['Avg' + col] = df[col].mean(axis=0)
new_df.drop(col, axis=1, inplace=True)
new_df.drop_duplicates(subset='ID', keep='last', inplace=True)
save_file_message(new_df)
def ld_probe_type_average(difficulty_type):
"""
This function creates a csv file for the LD Probe test. The resulting csv file will have the average performance
for all animals based on difficulty type. Afterward, the function will ask the user to save the newly created csv
file in a directory.
:param difficulty_type: A string that determines which determines which difficulty to average.
"""
print('You have selected the LD Probe(Select ID Avg) button!')
df = data_setup('LD Probe')
if df is not None:
ld_probe_delete_other_difficulties(df)
df = df.loc[(df['Type'] == difficulty_type)]
df.sort_values(['ID', 'Day'], ascending=[1, 1], inplace=True)
df.reset_index(drop=True, inplace=True)
col_names = ['ID', 'SessionLength', 'NumberOfTrial', 'PercentCorrect',
'NumberOfReversal',
'TotalITITouches', 'TotalBlankTouches', 'MeanRewardCollectionLatency', 'MeanCorrectTouchLatency',
'MeanIncorrectTouchLatency', 'SessionLengthTo1stReversalDuration',
'SessionLengthTo2ndReversalDuration',
'NumberOfTrialTo1stReversal', 'NumberOfTrialTo2ndReversal', 'PercentCorrectTo1stReversal',
'PercentCorrectTo2ndReversal']
avg_col = ['SessionLength', 'NumberOfTrial', 'PercentCorrect',
'NumberOfReversal',
'TotalITITouches', 'TotalBlankTouches', 'MeanRewardCollectionLatency', 'MeanCorrectTouchLatency',
'MeanIncorrectTouchLatency', 'SessionLengthTo1stReversalDuration',
'SessionLengthTo2ndReversalDuration',
'NumberOfTrialTo1stReversal', 'NumberOfTrialTo2ndReversal', 'PercentCorrectTo1stReversal',
'PercentCorrectTo2ndReversal']
new_df = pd.DataFrame(columns=col_names)
df.reset_index(drop=True, inplace=True)
for col in avg_col:
new_df[col] = df.groupby('ID')[col].mean()
new_df['ID'] = df['ID'].unique()
save_file_message(new_df)
def make_ld_probe_buttons(tk, root):
"""
This function creates all the location discrimination probe buttons found on the LD Probe sub-menu.
:param tk: The TKinter library
:param root: A specific frame where all the buttons will live on.
"""
# creates all ld probe buttons
ld_probe_first_day_all = tk.Button(root, text='LD Probe (First Day Difficulty All)',
command=ld_probe_first_day_difficulty, width=30)
ld_probe_first_day_all.grid(row=0, column=0)
ld_probe_button_last_day_all = tk.Button(root, text='LD Probe (Last Day Difficulty All)',
command=ld_probe_last_day_difficulty, width=30)
ld_probe_button_last_day_all.grid(row=1, column=0)
ld_probe_enter_day = tk.Entry(root, width=30, justify='center')
ld_probe_enter_day.grid(row=2, column=1)
ld_probe_button_select_day = tk.Button(root, text='LD Probe (Select Day)',
command=lambda: ld_probe_select_day(ld_probe_enter_day), width=30)
ld_probe_button_select_day.grid(row=2, column=0)
ld_probe_enter_id = tk.Entry(root, width=30, justify='center')
ld_probe_enter_id.grid(row=3, column=1)
ld_probe_button_select_id = tk.Button(root, text='LD Probe (Select ID)',
command=lambda: ld_probe_select_id(ld_probe_enter_id), width=30)
ld_probe_button_select_id.grid(row=3, column=0)
ld_probe_block_number = tk.Entry(root, width=30, justify='center')
ld_probe_block_number.grid(row=4, column=1)
ld_probe_button_select_block = tk.Button(root, text='LD Probe (Select Block)',
command=lambda: ld_probe_select_block(
ld_probe_block_number), width=30)
ld_probe_button_select_block.grid(row=4, column=0)
spacer_btn = tk.Label(root, text='', width=57, bg='#D6D6D6')
spacer_btn.grid(row=5, columnspan=2)
ld_probe_button_last_day_avg = tk.Button(root, text='LD Probe (Last Day All Avg)', command=ld_probe_last_day_avg,
width=30
)
ld_probe_button_last_day_avg.grid(row=6, column=0)
ld_probe_button_block_avg_text = tk.Entry(root, width=30, justify='center')
ld_probe_button_block_avg_text.grid(row=7, column=1)
ld_probe_button_block_avg = tk.Button(root, text='LD Probe (Block Avg)',
command=lambda: ld_probe_block_average(ld_probe_button_block_avg_text),
width=30)
ld_probe_button_block_avg.grid(row=7, column=0)
ld_probe_button_id_avg_text = tk.Entry(root, width=30, justify='center')
ld_probe_button_id_avg_text.grid(row=8, column=1)
ld_probe_button_id_avg = tk.Button(root, text='LD Probe (ID Avg Easy)',
command=lambda: ld_probe_id_average(ld_probe_button_id_avg_text, 'easy'),
width=30)
ld_probe_button_id_avg.grid(row=8, column=0)
ld_probe_button_id_avg_text_two = tk.Entry(root, width=30, justify='center')
ld_probe_button_id_avg_text_two.grid(row=9, column=1)
ld_probe_button_id_avg_two = tk.Button(root, text='LD Probe (ID Avg Hard)',
command=lambda: ld_probe_id_average(ld_probe_button_id_avg_text, 'hard'),
width=30)
ld_probe_button_id_avg_two.grid(row=9, column=0)
ld_probe_button_avg_easy = tk.Button(root, text='LD Probe (Avg Easy)',
command=lambda: ld_probe_type_average('easy'),
width=30)
ld_probe_button_avg_easy.grid(row=10, column=0)
ld_probe_button_avg_hard = tk.Button(root, text='LD Probe (Avg Hard)',
command=lambda: ld_probe_type_average('hard'),
width=30)
ld_probe_button_avg_hard.grid(row=11, column=0)