forked from esutlie/behavior_code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_plots.py
More file actions
1061 lines (923 loc) · 48.8 KB
/
simple_plots.py
File metadata and controls
1061 lines (923 loc) · 48.8 KB
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from datetime import date
import os
from tkinter import *
import time
from os import walk
import pandas as pd
from csv import DictReader, reader
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.collections import PatchCollection
from matplotlib.patches import Rectangle
import matplotlib.gridspec as gridspec
from upload_cloud_backup import behavior_data_dir
from user_info import get_user_info
import shutil
from datetime import datetime
import pdb
info_dict = get_user_info()
initials = info_dict['initials']
start_date = info_dict['start_date']
data_dir = behavior_data_dir
def get_today_filepaths(days_back=0):
file_paths = []
for root, dirs, filenames in walk(data_dir):
if len(dirs) == 0 and os.path.basename(root)[:2] in initials:
mouse = os.path.basename(root)
for f in filenames:
if f == 'desktop.ini':
continue
file_date = datetime.strptime(f[5:-4], '%Y-%m-%d_%H-%M-%S')
# file_date = date(int(f[5:9]), int(f[10:12]), int(f[13:15]))
dif = datetime.today() - file_date
if dif.days <= days_back:
# if f[5:15] == time.strftime("%Y-%m-%d"):
file_paths.append(os.path.join(mouse, f))
return file_paths
def min_dif(a, b, tolerance=0, return_index=False, rev=False):
if type(a) == pd.core.series.Series:
a = a.values
if type(b) == pd.core.series.Series:
b = b.values
if rev:
outer = -1 * np.subtract.outer(a, b)
outer[outer <= tolerance] = np.nan
else:
outer = np.subtract.outer(b, a)
outer[outer <= tolerance] = np.nan
# noinspection PyBroadException
mins = np.nanmin(outer, axis=0)
if return_index:
index = np.nanargmin(outer, axis=0)
return index, mins
return mins
def read_pi_meta(pi_dir):
with open(pi_dir, 'r') as file: # Read meta data from first two lines into a dictionary
line1 = file.readline()[:-1]
line2 = file.readline()[:-1]
pieces = line2.split(',')
if '{' in line2:
curly_start = np.where(np.array([p[0] for p in pieces]) == '{')[0]
curly_end = np.where(np.array([p[-1] for p in pieces]) == '}')[0]
pieces_list = []
sub_piece = []
for i in range(len(pieces)):
if curly_start[0] <= i <= curly_end[0] or curly_start[1] <= i <= curly_end[1]:
sub_piece.append(pieces[i])
else:
pieces_list.append(pieces[i])
if i in curly_end:
string = ','.join(sub_piece)
try:
s, e = string.index('<'), string.index('>')
string = string[:s] + "'exp_decreasing'" + string[e + 1:]
except Exception as e:
pass
pieces_list.append(eval(string))
sub_piece = []
else:
pieces_list = line2.split(',')
info = dict(zip(line1.split(','), pieces_list))
return info
def gen_data(file_paths, select_mouse=None, return_info=False):
d = {}
for f in file_paths:
mouse = os.path.dirname(f)
if select_mouse is not None and mouse not in select_mouse:
continue
path = os.path.join(data_dir, f)
meta_data = read_pi_meta(path)
if return_info:
data = meta_data
else:
try:
data = pd.read_csv(path, na_values=['None'], skiprows=3)
except pd.errors.EmptyDataError:
print(f'empty file at {path}')
continue
try:
data = data_reduction(data)
#port info as a row
port_info_row = [{
'key': meta_data['port1_info']['distribution'],
'port': meta_data['port1_info']['port_num']
# 'phase': last_row_phase,
},
{'key': meta_data['port2_info']['distribution'],
'port': meta_data['port2_info']['port_num']
# 'phase': last_row_phase,
}
]
#Added port info at the end of the dataframe
portinfo_rows_df = pd.DataFrame(port_info_row).reindex(columns=data.columns)
data = pd.concat([data, portinfo_rows_df], ignore_index=True)
except ValueError:
file_name = f[6:]
half_session_path = os.path.join(data_dir, 'half_sessions', file_name)
if data.session_time.max() < 800:
print(f'moving {f} to half sessions, session time: {data.session_time.max():.2f} seconds')
shutil.move(path, half_session_path)
else:
ans = input(f'remove broken file? (y/n)\n{path}\n???')
if ans == 'y':
shutil.move(path, half_session_path)
continue
if mouse in d.keys():
d[mouse].append(data)
else:
d[mouse] = [data]
return d
def remove(df, key, tolerance, port):
on_times = df[(df.key == key) & (df.value == 1) & (df.port == port)].session_time.to_numpy()
off_times = df[(df.key == key) & (df.value == 0) & (df.port == port)].session_time.to_numpy()
if (on_times.size > 0) & (off_times.size > 0):
forward = min_dif(on_times, off_times)
forward_off = min_dif(on_times, off_times, rev=True)
forward[np.isnan(forward)] = tolerance
forward_off[np.isnan(forward_off)] = tolerance
on_times = on_times[forward >= tolerance]
off_times = off_times[forward_off >= tolerance]
back = min_dif(off_times, on_times, rev=True)
back_off = min_dif(off_times, on_times)
back[np.isnan(back)] = tolerance
back_off[np.isnan(back_off)] = tolerance
on_times = on_times[back >= tolerance]
off_times = off_times[back_off >= tolerance]
df = df[((df.key != key) | (df.value != 1) | (df.port != port)) | (df.session_time.isin(on_times))]
df = df[((df.key != key) | (df.value != 0) | (df.port != port)) | (df.session_time.isin(off_times))]
return df
def data_reduction(df, lick_tol=.01, head_tol=.2):
df = df[df.key != 'camera']
df = df[df.phase != 'setup']
df = remove(df, 'head', head_tol, port=1)
df = remove(df, 'head', head_tol, port=2)
df = remove(df, 'lick', lick_tol, port=1)
df = remove(df, 'lick', lick_tol, port=2)
return df
def consumption_time(df):
bgportassignment = df.loc[df['key'] == 'background', 'port'].iloc[-1]
expportassignment = df.loc[df['key'] == 'exp_decreasing', 'port'].iloc[-1]
bg_end_times = df[(df.key == 'LED') & (df.port == bgportassignment) & (df.value == 1)]
exp_entries = df[(df.key == 'head') & (df.port == expportassignment) & (df.value == 1)]
dif = min_dif(bg_end_times.session_time, exp_entries.session_time)
bg_consumption = dif[~np.isnan(dif)]
if df.task.iloc[10] != 'single_reward':
consumption_df = pd.DataFrame()
consumption_df['consumption time'] = bg_consumption
consumption_df['port'] = ['bg'] * len(bg_consumption)
return consumption_df
exp_end_times = df[(df.key == 'LED') & (df.port == expportassignment) & (df.value == 1)]
bg_entries = df[(df.key == 'head') & (df.port == bgportassignment) & (df.value == 1)]
dif = min_dif(exp_end_times.session_time, bg_entries.session_time)
exp_consumption = dif[~np.isnan(dif)]
consumption_df = pd.DataFrame()
consumption_df['consumption time'] = np.concatenate([bg_consumption, exp_consumption])
consumption_df['port'] = ['bg'] * len(bg_consumption) + ['exp'] * len(exp_consumption)
return consumption_df
def calculate_premature_leave(df, threshold=1.0): #trials with premature leave
bgportassignment = df.loc[df['key'] == 'background', 'port'].iloc[-1]
premature_leave_trials = 0 # Counter for trials with premature leave
blocks = df.phase.dropna().unique()
blocks.sort()
results = []
for block in blocks:
block_data = df[df.phase == block]
block_total_trials = len(block_data.trial.unique())
for trial in block_data.trial.unique():
if pd.isna(trial):
continue # Skip invalid trials
trial_data = df[df.trial == trial] # Filter data for the current trial
bg_on_times = trial_data[(trial_data['key'] == 'trial') &
(trial_data['value'] == 1)].session_time.values
if len(bg_on_times) == 0:
continue
bg_start = bg_on_times[0]
bg_off_times = trial_data[(trial_data['key'] == 'LED') &
(trial_data['port'] == bgportassignment) &
(trial_data['value'] == 1)].session_time.values
if len(bg_off_times) == 0: # Check if no BG ON recorded
continue
bg_end = bg_off_times[0]
bg_head_out_times = trial_data[(trial_data['key'] == 'head') & # Extract head-out and head-in times while BG port is active
(trial_data['port'] == bgportassignment) &
(trial_data['value'] == 0) &
(trial_data['session_time'] >= bg_start) &
(trial_data['session_time'] < bg_end)].session_time.values
bg_head_in_times = trial_data[(trial_data['key'] == 'head') &
(trial_data['port'] == bgportassignment) &
(trial_data['value'] == 1) &
(trial_data['session_time'] >= bg_start) &
(trial_data['session_time'] < bg_end)].session_time.values
for head_out in bg_head_out_times: # Check for premature leave: head-out without a valid head-in within threshold
if not any((head_in > head_out) and (head_in - head_out) <= threshold for head_in in bg_head_in_times):
premature_leave_trials += 1 # Flag this trial as a premature leave
break # Stop checking further head-outs in this trial
premature_leave_rate = (premature_leave_trials / block_total_trials)
results.append({"block": block,"premature leave numbers":premature_leave_trials, "premature leave rate": premature_leave_rate})
premature_leave_df = pd.DataFrame(results)
return premature_leave_df
def block_leave_times(df):
bgportassignment = df.loc[df['key'] == 'background', 'port'].iloc[-1]
expportassignment = df.loc[df['key'] == 'exp_decreasing', 'port'].iloc[-1]
reward_trials = df[(df.key == 'reward_initiate')].trial.to_numpy()
non_reward = ~df.trial.isin(reward_trials)
bg_end_times = df[(df.key == 'LED') & (df.port == bgportassignment) & (df.value == 1) & non_reward]
exp_entries = df[(df.key == 'head') & (df.value == 1) & (df.port == expportassignment) & non_reward]
exp_exits = df[(df.key == 'head') & (df.value == 0) & (df.port == expportassignment) & non_reward]
bg_end_times = bg_end_times[bg_end_times.session_time < exp_entries.session_time.max()]
ind, dif = min_dif(bg_end_times.session_time, exp_entries.session_time, return_index=True)
exp_entries = exp_entries.iloc[np.unique(ind)]
exp_entries = exp_entries.groupby('trial').session_time.max()
exp_exits = exp_exits.groupby('trial').session_time.max()
valid_trials = np.intersect1d(exp_exits.index.values, exp_entries.index.values)
valid_trials = np.intersect1d(valid_trials, bg_end_times.trial.values)
exp_exits = exp_exits.loc[valid_trials]
exp_entries = exp_entries.loc[valid_trials]
if len(exp_exits.to_numpy()) != len(exp_entries.to_numpy()):
print(
f"Mismatch in exp_entries and exp_exits. "
f"exp_entries: {exp_entries}, exp_exits: {exp_exits}. "
"Using clean_entries_exits to resolve."
)
exp_entries, exp_exits = clean_entries_exits(exp_entries, exp_exits)
leave_times = exp_exits.to_numpy() - exp_entries.to_numpy()
trial_blocks = bg_end_times[bg_end_times.trial.isin(exp_entries.index.values)].phase.to_numpy()
block_leaves_df = pd.DataFrame()
block_leaves_df['leave time'] = leave_times
block_leaves_df['block'] = trial_blocks
return block_leaves_df
def get_entry_exit(df, trial):
is_trial = df.trial == trial
start = df.value == 1
end = df.value == 0
bgport = df.port == df.loc[df['key'] == 'background', 'port'].iloc[-1]
expport = df.port == df.loc[df['key'] == 'exp_decreasing', 'port'].iloc[-1]
# port1 = df.port == 1
# port2 = df.port == 2
trial_start = df[is_trial & start & (df.key == 'trial')].session_time.values[0]
trial_middle = df[is_trial & end & (df.key == 'LED') & bgport].session_time.values[0] #head in to EXP, bg LED off
trial_end = df[is_trial & end & (df.key == 'trial')].session_time.values[0]
bg_entries = df[is_trial & bgport & start & (df.key == 'head')].session_time.to_numpy()
bg_exits = df[is_trial & bgport & end & (df.key == 'head')].session_time.to_numpy()
if len(bg_entries) == 0 or len(bg_exits) == 0 or bg_entries[0] > bg_exits[0]:
bg_entries = np.concatenate([[trial_start], bg_entries])
if trial_end - bg_entries[-1] < .1:
bg_entries = bg_entries[:-1]
if len(bg_exits) == 0 or bg_entries[-1] > bg_exits[-1]:
bg_exits = np.concatenate([bg_exits, [trial_middle]])
exp_entries = df[is_trial & expport & start & (df.key == 'head') &
(df.session_time > trial_middle)].session_time.to_numpy()
exp_exits = df[is_trial & expport & end & (df.key == 'head') &
(df.session_time > trial_middle)].session_time.to_numpy()
if not (len(exp_entries) == 0 and len(exp_exits) == 0):
if len(exp_entries) == 0: #only exp out
exp_entries = np.concatenate([[trial_middle], exp_entries])
if len(exp_exits) == 0: #only exp in
exp_exits = np.concatenate([exp_exits, [trial_end]])
if exp_entries[0] > exp_exits[0]:
exp_entries = np.concatenate([[trial_middle], exp_entries])
if exp_entries[-1] > exp_exits[-1]:
exp_exits = np.concatenate([exp_exits, [trial_end]])
early_exp_entries = df[is_trial & expport & start & (df.key == 'head') &
(df.session_time < trial_middle)].session_time.to_numpy()
early_exp_exits = df[is_trial & expport & end & (df.key == 'head') &
(df.session_time < trial_middle)].session_time.to_numpy()
if not (len(early_exp_entries) == 0 and len(early_exp_exits) == 0): #any early exp in/out
if len(early_exp_entries) == 0:
early_exp_entries = np.concatenate([[trial_start], early_exp_entries])
if len(early_exp_exits) == 0:
early_exp_exits = np.concatenate([early_exp_exits, [trial_middle]])
if early_exp_entries[0] > early_exp_exits[0]:
early_exp_entries = np.concatenate([[trial_start], early_exp_entries])
if early_exp_entries[-1] > early_exp_exits[-1]:
early_exp_exits = np.concatenate([early_exp_exits, [trial_middle]])
if len(bg_entries) != len(bg_exits):
print()
if len(exp_entries) != len(exp_exits):
print()
if len(early_exp_entries) != len(early_exp_exits):
print()
if len(exp_entries):
if len(exp_exits) != len(exp_entries):
print(
f"Mismatch in exp_entries and exp_exits in trial {trial}. "
f"exp_entries: {exp_entries}, exp_exits: {exp_exits}. "
"Using clean_entries_exits to resolve."
)
exp_entries, exp_exits = clean_entries_exits(exp_entries, exp_exits)
if len(bg_entries) != len(bg_exits):
print(
f"Mismatch in bg_entries and bg_exits in trial {trial}. "
f"bg_entries: {bg_entries}, bg_exits: {bg_exits}. "
"Using clean_entries_exits to resolve."
)
bg_entries, bg_exits = clean_entries_exits(bg_entries, bg_exits)
return bg_entries, bg_exits, exp_entries, exp_exits, early_exp_entries, early_exp_exits
def clean_entries_exits(entries, exits):
"""
Cleans mismatched entries and exits such that each entry is paired with the nearest valid exit.
"""
valid_entries = []
valid_exits = []
e_idx, x_idx = 0, 0
while e_idx < len(entries) and x_idx < len(exits):
if entries[e_idx] < exits[x_idx]: # Valid entry-exit pair
valid_entries.append(entries[e_idx])
valid_exits.append(exits[x_idx])
e_idx += 1
x_idx += 1 # Move to the next entry and exit
else:
x_idx += 1 # Skip unmatched exits
return valid_entries, valid_exits
def percent_engaged(df):
try:
travel_time = .5
blocks = df.phase.dropna().unique()
blocks.sort()
time_engaged = []
block_time = []
block_rewards = []
for block in blocks:
engaged = []
all_time = []
rewards = []
block_trials = df[(df.value == 0) & (df.key == 'trial') & (df.phase == block)].trial
for trial in block_trials:
bg_entries, bg_exits, exp_entries, exp_exits, _, _ = get_entry_exit(df, trial)
is_trial = df.trial == trial
start = df.value == 1
end = df.value == 0
# port1 = df.port == 1
# port2 = df.port == 2
#
trial_start = df[is_trial & start & (df.key == 'trial')].session_time.values[0]
# trial_middle = df[is_trial & start & (df.key == 'LED') & port2].session_time.values[0]
trial_end = df[is_trial & end & (df.key == 'trial')].session_time.values[0]
#
# bg_entries = df[is_trial & port2 & start & (df.key == 'head')].session_time.to_numpy()
# bg_exits = df[is_trial & port2 & end & (df.key == 'head')].session_time.to_numpy()
#
# if len(bg_entries) == 0 or bg_entries[0] > bg_exits[0]:
# bg_entries = np.concatenate([[trial_start], bg_entries])
# if trial_end - bg_entries[-1] < .1:
# bg_entries = bg_entries[:-1]
# if len(bg_exits) == 0 or bg_entries[-1] > bg_exits[-1]:
# bg_entries = np.concatenate([bg_exits, [trial_middle]])
#
# if not (len(bg_entries) == len(bg_exits) and np.all(bg_exits - bg_entries > 0)):
# print('stop')
# bg_engaged = sum(bg_exits - bg_entries)
#
# exp_entries = df[is_trial & port1 & start & (df.key == 'head') &
# (df.session_time > trial_middle)].session_time.to_numpy()
# exp_exits = df[is_trial & port1 & end & (df.key == 'head') &
# (df.session_time > trial_middle)].session_time.to_numpy()
#
# if len(exp_entries) == 0 and len(exp_exits) == 0:
# exp_engaged = 0
# else:
# if len(exp_entries) == 0:
# exp_entries = np.concatenate([[trial_middle], exp_entries])
# if len(exp_exits) == 0:
# exp_exits = np.concatenate([exp_exits, [trial_end]])
#
# if exp_entries[0] > exp_exits[0]:
# exp_entries = np.concatenate([[trial_middle], exp_entries])
# if exp_entries[-1] > exp_exits[-1]:
# exp_exits = np.concatenate([exp_exits, [trial_end]])
# exp_engaged = sum(exp_exits - exp_entries)
#
# # if not len(exp_entries) == len(exp_exits) and len(exp_entries):
# # print('stop')
# # if len(exp_entries):
if len(exp_entries):
exp_engaged = sum([exit - entry for entry, exit in zip(exp_entries, exp_exits)])
else:
exp_engaged = 0
bg_engaged = sum([exit - entry for entry, exit in zip(bg_entries, bg_exits)])
all_time.append(trial_end - trial_start)
engaged.append(bg_engaged + exp_engaged)
rewards.append(len(df[is_trial & start & (df.key == 'reward')]))
time_engaged.append(sum(engaged) + travel_time * 2 * len(block_trials))
block_time.append(sum(all_time))
block_rewards.append(sum(rewards))
engaged_df = pd.DataFrame()
engaged_df['percent engaged'] = np.array(time_engaged) / np.array(block_time)
engaged_df['block'] = blocks
engaged_df['time engaged'] = time_engaged
engaged_df['rewards earned'] = block_rewards
engaged_df['reward rate'] = np.array(block_rewards) / np.array(time_engaged)
return engaged_df
except Exception as e:
print ("Error in function.")
raise
def reentry_index(df):
bgportassignment = df.loc[df['key'] == 'background', 'port'].iloc[-1]
expportassignment = df.loc[df['key'] == 'exp_decreasing', 'port'].iloc[-1]
is_bg_exit = (df.port == bgportassignment) & (df.key == 'head') & (df.value == 0)
phase_mode = df.groupby('trial').phase.agg(
lambda s: s.iloc[-1] if (s.value_counts().get('0.4', 0) == 1 and s.value_counts().get('0.8', 0) == 1)
else pd.Series.mode(s).iloc[-1]
)
is_low_block = (phase_mode == '0.4')
is_high_block = (phase_mode == '0.8')
num_ideal_bg_entry_low = len(np.unique(df.trial.dropna())[is_low_block]) # gets number of low block trials
num_bg_entry_low = len(df.index[is_bg_exit & df.trial.isin(
np.unique(df.trial.dropna())[is_low_block])])
num_ideal_bg_entry_high = len(np.unique(df.trial.dropna())[is_high_block]) # gets number of high block trials
num_bg_entry_high = len(df.index[is_bg_exit & df.trial.isin(
np.unique(df.trial.dropna())[is_high_block])])
reentry_index_low = num_bg_entry_low / num_ideal_bg_entry_low if num_ideal_bg_entry_low > 0 else 0
reentry_index_high = num_bg_entry_high / num_ideal_bg_entry_high if num_ideal_bg_entry_high > 0 else 0
reentry_df = pd.DataFrame()
reentry_df['block'] = ['0.4', '0.8']
reentry_df['bg_reentry_index'] = [reentry_index_low, reentry_index_high]
return reentry_df
def add_h_lines(data=None, x=None, y=None, hue=None, ax=None, palette=None, estimator='mean'):
days_back = 4
palette = sns.color_palette(palette)
for i, hue_key in enumerate(data[hue].unique()):
df = data[data[hue] == hue_key]
if df[x].max() > days_back:
if estimator == 'median':
hue_mean = df[(df[x] > df[x].max() - days_back)][y].median()
else:
hue_mean = df[(df[x] > df[x].max() - days_back)][y].mean()
ax.hlines(hue_mean, df[x].max() - days_back, df[x].max(), palette[i], alpha=.5)
def merge_old_trials(session):
print()
return session
def simple_plots(select_mouse=None):
plot_single_mouse_plots = True
save_folder = "C:\\Users\\Shichen\\OneDrive - Johns Hopkins\\ShulerLab\\Rie_behavior\\summary_graphs"
if select_mouse is None:
dif = date.today() - start_date
data = gen_data(get_today_filepaths(days_back=dif.days), select_mouse=select_mouse)
info = gen_data(get_today_filepaths(days_back=dif.days), select_mouse=select_mouse, return_info=True)
else:
data = gen_data(get_today_filepaths(days_back=1000), select_mouse=select_mouse)
info = gen_data(get_today_filepaths(days_back=1000), select_mouse=select_mouse, return_info=True)
block_leaves_last10 = pd.DataFrame()
for mouse in data.keys():
if select_mouse is not None and mouse not in select_mouse:
continue
engaged = pd.DataFrame()
consumption = pd.DataFrame()
block_leaves = pd.DataFrame()
reentry = pd.DataFrame()
premature_leave = pd.DataFrame()
for i, session in enumerate(data[mouse]):
# if info[mouse][i]['task'] == 'multi_reward':
# continue
try:
session = merge_old_trials(session)
engaged_df = percent_engaged(session)
engaged_df['session'] = [i] * len(engaged_df)
engaged = pd.concat([engaged, engaged_df])
consumption_df = consumption_time(session)
consumption_df['session'] = [i] * len(consumption_df)
consumption = pd.concat([consumption, consumption_df])
block_leaves_df = block_leave_times(session)
block_leaves_df['session'] = [i] * len(block_leaves_df)
block_leaves = pd.concat([block_leaves, block_leaves_df])
reentry_df = reentry_index(session)
reentry_df['session'] = [i] * len(reentry_df)
reentry = pd.concat([reentry, reentry_df])
premature_leave_df= calculate_premature_leave(session)
premature_leave_df['session'] = [i] * len(premature_leave_df)
premature_leave = pd.concat([premature_leave,premature_leave_df])
except Exception as e:
print(f"Error processing session {i} for mouse {mouse}: {e}")
raise
engaged.sort_values('block', inplace=True)
block_leaves.sort_values('block', inplace=True)
if plot_single_mouse_plots:
fig, axes = plt.subplots(3, 2, figsize=[11, 8], layout="constrained")
sns.lineplot(data=block_leaves.reset_index(), x='session', y='leave time', hue='block', style = 'block',markers=True, ax=axes[0, 0],
palette='Set2')
add_h_lines(data=block_leaves.reset_index(), x='session', y='leave time', hue='block', ax=axes[0, 0],
palette='Set2')
sns.lineplot(data=consumption.reset_index(), x='session', y='consumption time', hue='port', style = 'port', markers=True, ax=axes[0, 1],
palette='Set1', estimator=np.median)
add_h_lines(data=consumption.reset_index(), x='session', y='consumption time', hue='port', ax=axes[0, 1],
palette='Set1', estimator='median')
sns.lineplot(data=engaged.reset_index(), x='session', y='reward rate', hue='block', style = 'block', markers=True,ax=axes[1, 0],
palette='Set2')
add_h_lines(data=engaged.reset_index(), x='session', y='reward rate', hue='block', ax=axes[1, 0],
palette='Set2')
sns.lineplot(data=engaged.reset_index(), x='session', y='percent engaged', hue='block',style = 'block', markers=True, ax=axes[1, 1],
palette='Set2')
add_h_lines(data=engaged.reset_index(), x='session', y='percent engaged', hue='block', ax=axes[1, 1],
palette='Set2')
sns.lineplot(data=premature_leave.reset_index(), x='session', y='premature leave rate', hue='block', style = 'block',markers=True,ax=axes[2, 0],
palette='Set2')
add_h_lines(data=premature_leave.reset_index(), x='session', y='premature leave rate', hue='block', ax=axes[2, 0],
palette='Set2')
axes[2, 0].axhline(y=0.2, color='red', linestyle='--', linewidth=1.5, label='Threshold = 0.2')
# Add legend to show the line label
axes[2, 0].legend()
axes[0, 0].set_title('Leave Time by Block')
axes[0, 1].set_title('Consumption Time by Port')
axes[1, 0].set_title('Reward Rate by Block')
axes[1, 1].set_title('Percent Time Engaged by Block')
axes[2, 0].set_title('Premature leave from BG port by Block')
axes[0, 0].set_ylim([0, 20])
axes[0, 1].set_ylim([0, 20])
axes[1, 0].set_ylim([0, .65])
axes[1, 1].set_ylim([0, 1])
axes[2, 0].set_ylim([0, 1])
plt.suptitle(mouse, fontsize=20)
os.makedirs(save_folder, exist_ok=True)
# Construct the filename
filename = f'{mouse}_session_summary.png'
save_path = os.path.join(save_folder, filename)
plt.savefig(save_path, dpi=300, bbox_inches='tight')
print(f"Graph saved to: {save_path}")
plt.show()
block_leaves_last10_df = block_leaves[(block_leaves.session >= block_leaves.session.max() - 10)].groupby('block')[
'leave time'].mean().reset_index()
block_leaves_last10_df['animal'] = mouse
block_leaves_last10 = pd.concat([block_leaves_last10, block_leaves_last10_df])
fig, axes = plt.subplots(1, 1)
sns.boxplot(data=block_leaves_last10.reset_index(), x='block', y='leave time')
plt.show()
def single_session(select_mouse=None, num_back=2):
if select_mouse is None:
dif = date.today() - start_date
data = gen_data(get_today_filepaths(days_back=dif.days), select_mouse=select_mouse)
info = gen_data(get_today_filepaths(days_back=dif.days), select_mouse=select_mouse, return_info=True)
else:
data = gen_data(get_today_filepaths(days_back=1000), select_mouse=select_mouse)
info = gen_data(get_today_filepaths(days_back=1000), select_mouse=select_mouse, return_info=True)
for mouse in data.keys():
if select_mouse is not None and mouse not in select_mouse:
continue
for i in range(1, num_back + 1):
last_session = data[mouse][-i]
last_info = info[mouse][-i]
session_summary(last_session, mouse, last_info)
# def session_summary(data, mouse, info):
# base_save_folder= "C:\\Users\\riepo\\Experiment1\\graphs\\each_session"
# save_folder = os.path.join(base_save_folder, mouse)
# os.makedirs(save_folder, exist_ok=True)
# fig, [ax1, ax2] = plt.subplots(1, 2, figsize=[10, 10])
# port_palette = sns.color_palette('Set1')
# block_palette = sns.color_palette('Set2')
# start = data.value == 1
# end = data.value == 0
# head = data.key == 'head'
# lick = data.key == 'lick'
# reward = data.key == 'reward'
# bgport = data.port == data.loc[data['key'] == 'background', 'port'].iloc[-1]
# expport = data.port == data.loc[data['key'] == 'exp_decreasing', 'port'].iloc[-1]
# # port1 = data.port == 1
# # port2 = data.port == 2
# # print(f"port2 is {port2}")
# # print(f"bgport is {bgport}")
# max_trial = data.trial.max()
#
# bg_rectangles = []
# exp_rectangles_in_bg = []
# exp_rectangles = []
# block1_rectangles = []
# block2_rectangles = []
# bg_reward_events = []
# exp_reward_events = []
# bg_lick_events = []
# exp_lick_events = []
# bg_lengths = []
# exp_lengths = []
# trial_blocks = data.groupby(['trial'])['phase'].agg(pd.Series.mode)
# blocks = data.phase.dropna().unique()
# blocks.sort()
# for trial in data.trial.unique():
# if np.isnan(trial):
# continue
# is_trial = data.trial == trial
# try:
# trial_start = data[is_trial & start & (data.key == 'trial')].session_time.values[0]
# trial_middle = data[is_trial & end & (data.key == 'LED') & bgport].session_time.values[0]
# trial_end = data[is_trial & end & (data.key == 'trial')].session_time.values[0]
# except IndexError:
# continue
#
# bg_rewards = data[is_trial & start & bgport & reward].session_time.values
# exp_rewards = data[is_trial & start & expport & reward].session_time.values
# bg_licks = data[is_trial & start & lick & (data.session_time < trial_middle)].session_time.values
# exp_licks = data[is_trial & start & lick & (data.session_time > trial_middle)].session_time.values
#
# bg_lengths.append(trial_middle - trial_start)
# exp_lengths.append(trial_end - trial_middle)
#
# bg_entries, bg_exits, exp_entries, exp_exits, early_exp_entries, early_exp_exits = get_entry_exit(data, trial)
# bg_intervals = list(zip(bg_entries, bg_exits))
# exp_intervals = list(zip(exp_entries, exp_exits))
# early_exp_intervals = list(zip(early_exp_entries, early_exp_exits))
# for [s, e] in bg_intervals:
# bg_rectangles.append(Rectangle((s - trial_start, trial), e - s, .7))
# for [s, e] in early_exp_intervals:
# exp_rectangles_in_bg.append(Rectangle((s - trial_start, trial), e - s, .7))
# for [s, e] in exp_intervals:
# exp_rectangles.append(Rectangle((s - trial_middle, trial), e - s, .7))
# if np.where(blocks == trial_blocks.loc[trial])[0][0] == 0:
# block1_rectangles.append(Rectangle((0, trial), 100, 1))
# else:
# block2_rectangles.append(Rectangle((0, trial), 100, 1))
# bg_reward_events.append(bg_rewards - trial_start)
# exp_reward_events.append(exp_rewards - trial_middle)
# bg_lick_events.append(bg_licks - trial_start)
# exp_lick_events.append(exp_licks - trial_middle)
#
# alpha = .5
# pc_b1 = PatchCollection(block1_rectangles, facecolors=block_palette[0], alpha=alpha)
# pc_b2 = PatchCollection(block2_rectangles, facecolors=block_palette[1], alpha=alpha)
# ax1.add_collection(pc_b1)
# ax1.add_collection(pc_b2)
# pc_b12 = PatchCollection(block1_rectangles, facecolors=block_palette[0], alpha=alpha)
# pc_b22 = PatchCollection(block2_rectangles, facecolors=block_palette[1], alpha=alpha)
# ax2.add_collection(pc_b12)
# ax2.add_collection(pc_b22)
#
# pc_bg = PatchCollection(bg_rectangles, edgecolor=port_palette[0], facecolor='w', alpha=1)
# ax1.add_collection(pc_bg)
#
# pc_exp_bg = PatchCollection(exp_rectangles_in_bg, edgecolor=port_palette[1], facecolor='w', alpha=1)
# ax1.add_collection(pc_exp_bg)
#
# pc_exp = PatchCollection(exp_rectangles, edgecolor=port_palette[1], facecolor='w', alpha=1)
# ax2.add_collection(pc_exp)
#
# offsets = np.array(list(range(len(bg_reward_events)))) + 1.4
# ax1.eventplot(bg_reward_events, color='purple', linelengths=.62, lineoffsets=offsets)
# offsets = np.array(list(range(len(exp_reward_events)))) + 1.4
# ax2.eventplot(exp_reward_events, color='purple', linelengths=.62, lineoffsets=offsets)
#
# light = [.8, .7, .8]
# dark = [.2, .2, .2]
# offsets = np.array(list(range(len(bg_lick_events)))) + 1.4
# ax1.eventplot(bg_lick_events, color=light, linelengths=.25, lineoffsets=offsets)
# offsets = np.array(list(range(len(exp_lick_events)))) + 1.4
# ax2.eventplot(exp_lick_events, color=light, linelengths=.25, lineoffsets=offsets)
#
# session_summary_axis_settings([ax1, ax2], max_trial)
# plt.suptitle(f'{mouse}: {info["date"]} {info["time"]}')
#
# # Construct the filename
# filename = f'{mouse}_{info["date"]}_{info["time"]}.png'
# save_path = os.path.join(save_folder, filename)
#
# # Save the plot
# plt.savefig(save_path, dpi=300, bbox_inches='tight')
# print(f"Graph saved to: {save_path}")
# plt.show()
def session_summary(data, mouse, info):
base_save_folder= save_folder = "C:\\Users\\Shichen\\OneDrive - Johns Hopkins\\ShulerLab\\Rie_behavior\\each_session"
save_folder = os.path.join(base_save_folder, mouse)
os.makedirs(save_folder, exist_ok=True)
fig, [ax1, ax2] = plt.subplots(1, 2, figsize=[10, 10])
port_palette = sns.color_palette('Set1')
block_palette = sns.color_palette('Set2')
start = data.value == 1
end = data.value == 0
head = data.key == 'head'
lick = data.key == 'lick'
reward = data.key == 'reward'
bgport = data.port == data.loc[data['key'] == 'background', 'port'].iloc[-1]
expport = data.port == data.loc[data['key'] == 'exp_decreasing', 'port'].iloc[-1]
max_trial = data.trial.max()
bg_rectangles = []
exp_rectangles_in_bg = []
exp_rectangles = []
block1_rectangles = []
block2_rectangles = []
bg_reward_events = []
exp_reward_events = []
bg_lick_events = []
exp_lick_events = []
bg_lengths = []
exp_lengths = []
trial_blocks = data.groupby(['trial'])['phase'].agg(pd.Series.mode)
blocks = data.phase.dropna().unique()
blocks.sort()
for trial in data.trial.unique():
if np.isnan(trial):
continue
is_trial = data.trial == trial
try:
trial_start = data[is_trial & start & (data.key == 'trial')].session_time.values[0]
trial_middle = data[is_trial & end & (data.key == 'LED') & bgport].session_time.values[0]
trial_end = data[is_trial & end & (data.key == 'trial')].session_time.values[0]
except IndexError:
continue
bg_rewards = data[is_trial & start & bgport & reward].session_time.values
exp_rewards = data[is_trial & start & expport & reward].session_time.values
bg_licks = data[is_trial & start & lick & (data.session_time < trial_middle)].session_time.values
exp_licks = data[is_trial & start & lick & (data.session_time > trial_middle)].session_time.values
bg_lengths.append(trial_middle - trial_start)
exp_lengths.append(trial_end - trial_middle)
bg_entries, bg_exits, exp_entries, exp_exits, early_exp_entries, early_exp_exits = get_entry_exit(data, trial)
bg_intervals = list(zip(bg_entries, bg_exits))
exp_intervals = list(zip(exp_entries, exp_exits))
early_exp_intervals = list(zip(early_exp_entries, early_exp_exits))
for [s, e] in bg_intervals:
bg_rectangles.append(Rectangle((s - trial_start, trial), e - s, .7))
for [s, e] in early_exp_intervals:
exp_rectangles_in_bg.append(Rectangle((s - trial_start, trial), e - s, .7))
for [s, e] in exp_intervals:
exp_rectangles.append(Rectangle((s - trial_middle, trial), e - s, .7))
block_value = trial_blocks.loc[trial]
if block_value == "0.4":
block1_rectangles.append(Rectangle((0, trial), 100, 1))
else:
block2_rectangles.append(Rectangle((0, trial), 100, 1))
bg_reward_events.append(bg_rewards - trial_start)
exp_reward_events.append(exp_rewards - trial_middle)
bg_lick_events.append(bg_licks - trial_start)
exp_lick_events.append(exp_licks - trial_middle)
alpha = .5
pc_b1 = PatchCollection(block1_rectangles, facecolors=block_palette[0], alpha=alpha)
pc_b2 = PatchCollection(block2_rectangles, facecolors=block_palette[1], alpha=alpha)
ax1.add_collection(pc_b1)
ax1.add_collection(pc_b2)
pc_b12 = PatchCollection(block1_rectangles, facecolors=block_palette[0], alpha=alpha)
pc_b22 = PatchCollection(block2_rectangles, facecolors=block_palette[1], alpha=alpha)
ax2.add_collection(pc_b12)
ax2.add_collection(pc_b22)
pc_bg = PatchCollection(bg_rectangles, edgecolor=port_palette[0], facecolor='w', alpha=1)
ax1.add_collection(pc_bg)
pc_exp_bg = PatchCollection(exp_rectangles_in_bg, edgecolor=port_palette[1], facecolor='w', alpha=1)
ax1.add_collection(pc_exp_bg)
pc_exp = PatchCollection(exp_rectangles, edgecolor=port_palette[1], facecolor='w', alpha=1)
ax2.add_collection(pc_exp)
offsets = np.array(list(range(len(bg_reward_events)))) + 1.4
ax1.eventplot(bg_reward_events, color='purple', linelengths=.62, lineoffsets=offsets)
offsets = np.array(list(range(len(exp_reward_events)))) + 1.4
ax2.eventplot(exp_reward_events, color='purple', linelengths=.62, lineoffsets=offsets)
light = [.8, .7, .8]
dark = [.2, .2, .2]
offsets = np.array(list(range(len(bg_lick_events)))) + 1.4
ax1.eventplot(bg_lick_events, color=light, linelengths=.25, lineoffsets=offsets)
offsets = np.array(list(range(len(exp_lick_events)))) + 1.4
ax2.eventplot(exp_lick_events, color=light, linelengths=.25, lineoffsets=offsets)
session_summary_axis_settings([ax1, ax2], max_trial)
plt.suptitle(f'{mouse}: {info["date"]} {info["time"]}')
# Construct the filename
filename = f'{mouse}_{info["date"]}_{info["time"]}.png'
save_path = os.path.join(save_folder, filename)
# Save the plot
plt.savefig(save_path, dpi=300, bbox_inches='tight')
print(f"Graph saved to: {save_path}")
plt.show()
def session_summary_axis_settings(axes, max_trial):
for ax in axes:
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(True)
ax.get_yaxis().set_visible(False)
ax.set_ylim([-1, max_trial + 1])
ax.set_xlim([0, 30])
ax.invert_yaxis()
ax.set_ylabel('Trial')
ax.set_xlabel('Time (sec)')
def simple_plots2(select_mouse=None):
plot_single_mouse_plots = True
save_folder = "C:\\Users\\Shichen\\OneDrive - Johns Hopkins\\ShulerLab\\Rie_behavior\\summary_graphs"
#insert vertical line between stages or pre/post surgery
stage_changes = {
# "RK001": {"1->2": "2025-01-22", "2->3": "2025-01-31"},
# "RK002": {"1->2": "2025-01-03", "2->3": "2025-03-14"},
# "RK003": {"1->2": "2025-01-03", "2->3": "2025-01-22"},
# "RK004": {"1->2": "2025-02-24", "2->3": "2025-03-05"},
# "RK005": {"1->2": "2025-01-27", "2->3": "2025-01-31"},
# "RK006": {"1->2": "2024-12-20", "2->3": "2025-02-26"},
# "ES039": {"1->2": "2024-02-14", "2->3": None},
# "ES045": {"1->2": "2024-04-24", "2->3": None},
# "ES046": {"1->2": "2024-05-02", "2->3": None},
# "ES047": {"1->2": "2024-04-24", "2->3": None},
"RK001": {"1->2": "2025-04-13", "2->3": None}, #surgery date
"RK002": {"1->2": "2025-04-13", "2->3": None},
"RK003": {"1->2": "2025-04-13", "2->3": None},
"RK004": {"1->2": "2025-03-30", "2->3": None},
"RK005": {"1->2": "2025-03-30", "2->3": None},
"RK006": {"1->2": "2025-03-30", "2->3": None}
}
if select_mouse is None:
dif = date.today() - start_date
data = gen_data(get_today_filepaths(days_back=dif.days), select_mouse=select_mouse)
info = gen_data(get_today_filepaths(days_back=dif.days), select_mouse=select_mouse, return_info=True)
else:
data = gen_data(get_today_filepaths(days_back=1000), select_mouse=select_mouse)
info = gen_data(get_today_filepaths(days_back=1000), select_mouse=select_mouse, return_info=True)
block_leaves_last10 = pd.DataFrame()
for mouse in data.keys():
if select_mouse is not None and mouse not in select_mouse:
continue
# Initialize empty dataframes for aggregated data.
engaged = pd.DataFrame()
consumption = pd.DataFrame()
block_leaves = pd.DataFrame()
reentry = pd.DataFrame()
premature_leave = pd.DataFrame()
# Extract session dates
session_dates = []
for sess_info in info[mouse]:
try:
dt = datetime.strptime(sess_info['date'], "%Y-%m-%d")
session_dates.append(dt)
except Exception as e:
print(f"Error parsing date in session info: {sess_info.get('date', None)}: {e}")
for i, session in enumerate(data[mouse]):
# if info[mouse][i]['task'] == 'multi_reward':
# continue
try:
session = merge_old_trials(session)
engaged_df = percent_engaged(session)
engaged_df['session'] = [i] * len(engaged_df)
engaged = pd.concat([engaged, engaged_df])
consumption_df = consumption_time(session)
consumption_df['session'] = [i] * len(consumption_df)
consumption = pd.concat([consumption, consumption_df])
block_leaves_df = block_leave_times(session)
block_leaves_df['session'] = [i] * len(block_leaves_df)
block_leaves = pd.concat([block_leaves, block_leaves_df])
reentry_df = reentry_index(session)
reentry_df['session'] = [i] * len(reentry_df)
reentry = pd.concat([reentry, reentry_df])
premature_leave_df = calculate_premature_leave(session)
premature_leave_df['session'] = [i] * len(premature_leave_df)
premature_leave = pd.concat([premature_leave, premature_leave_df])
except Exception as e:
print(f"Error processing session {i} for mouse {mouse}: {e}")
raise
engaged.sort_values('block', inplace=True)
block_leaves.sort_values('block', inplace=True)
if plot_single_mouse_plots:
fig, axes = plt.subplots(3, 2, figsize=[11, 8], layout="constrained")
sns.lineplot(data=block_leaves.reset_index(), x='session', y='leave time',
hue='block', style='block', markers=True, ax=axes[0, 0], palette='Set2')
add_h_lines(data=block_leaves.reset_index(), x='session', y='leave time',
hue='block', ax=axes[0, 0], palette='Set2')
sns.lineplot(data=consumption.reset_index(), x='session', y='consumption time',
hue='port', style='port', markers=True, ax=axes[0, 1],
palette='Set1', estimator=np.median)
add_h_lines(data=consumption.reset_index(), x='session', y='consumption time',
hue='port', ax=axes[0, 1], palette='Set1', estimator='median')
sns.lineplot(data=engaged.reset_index(), x='session', y='reward rate',
hue='block', style='block', markers=True, ax=axes[1, 0], palette='Set2')
add_h_lines(data=engaged.reset_index(), x='session', y='reward rate',
hue='block', ax=axes[1, 0], palette='Set2')
sns.lineplot(data=engaged.reset_index(), x='session', y='percent engaged',
hue='block', style='block', markers=True, ax=axes[1, 1], palette='Set2')