Skip to content

Commit f833f65

Browse files
authored
Update overview_plot_dlc.py
1 parent 304ab27 commit f833f65

File tree

1 file changed

+148
-91
lines changed

1 file changed

+148
-91
lines changed

dlc/overview_plot_dlc.py

Lines changed: 148 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@
2222
# https://github.com/lindermanlab/ssm
2323

2424

25+
26+
def get_all_sess_with_ME():
27+
one = ONE()
28+
# get all bwm sessions with dlc
29+
all_sess = one.alyx.rest('sessions', 'list',
30+
project='ibl_neuropixel_brainwide_01',
31+
task_protocol="ephys",
32+
dataset_types='camera.ROIMotionEnergy')
33+
34+
eids = [s['url'].split('/')[-1] for s in all_sess]
35+
36+
return eids
37+
38+
39+
2540
def get_repeated_sites():
2641
one = ONE()
2742
STR_QUERY = 'probe_insertion__session__project__name__icontains,ibl_neuropixel_brainwide_01,' \
@@ -36,6 +51,27 @@ def get_repeated_sites():
3651
return eids
3752

3853

54+
def check_progress():
55+
56+
one = ONE()
57+
eids = get_repeated_sites()
58+
59+
s = {}
60+
comp = []
61+
for eid in eids:
62+
63+
task = one.alyx.rest('tasks', 'list', session=eid, name='EphysDLC')[0]
64+
try:
65+
s[eid] = task['version']
66+
print(task['version'])
67+
if task['version'][:3] != '1.1':
68+
comp.append(eid)
69+
except:
70+
print(eid, 'has no task version')
71+
72+
return s
73+
74+
3975
def download_all_dlc():
4076

4177
eids = get_repeated_sites()
@@ -127,7 +163,7 @@ def get_dlc_XYs(eid, video_type):
127163
# return
128164

129165

130-
one.load(eid, dataset_types = dataset_types)
166+
one.load(eid, dataset_types = dataset_types) #clobber=True # force download
131167
local_path = one.path_from_eid(eid)
132168
alf_path = local_path / 'alf'
133169

@@ -529,16 +565,20 @@ def plot_licks(eid, combine=False):
529565

530566
def lick_raster(licks_pos):
531567

532-
plt.imshow(licks_pos,extent=[-0.5,1.5,-0.5,1.5], cmap='gray_r')
568+
#plt.figure(figsize=(4,4))
569+
570+
y_dims, x_dims = len(licks_pos), len(licks_pos[0])
571+
plt.imshow(licks_pos,aspect='auto', extent=[-0.5,1.5,y_dims,0],
572+
cmap='gray_r')
533573

534574
ax = plt.gca()
535-
ax.set_yticklabels([])
536575
ax.set_xticks([-0.5,0,0.5,1,1.5])
537576
ax.set_xticklabels([-0.5,0,0.5,1,1.5])
538577
plt.ylabel('trials')
539578
plt.xlabel('time [sec]')
540579
ax.axvline(x=0, label='feedback time', linestyle = '--', c='r')
541580
plt.title('lick events per correct trial')
581+
plt.tight_layout()
542582

543583

544584

@@ -840,30 +880,30 @@ def non_uniform_savgol(x, y, window, polynom):
840880
return y_smoothed
841881

842882

843-
def align_left_right_pupil(eid):
883+
#def align_left_right_pupil(eid):
844884

845-
timesL, XYsL = get_dlc_XYs(eid, 'left')
846-
dL=get_pupil_diameter(XYsL)
847-
timesR, XYsR = get_dlc_XYs(eid, 'right')
848-
dR=get_pupil_diameter(XYsR)
885+
# timesL, XYsL = get_dlc_XYs(eid, 'left')
886+
# dL=get_pupil_diameter(XYsL)
887+
# timesR, XYsR = get_dlc_XYs(eid, 'right')
888+
# dR=get_pupil_diameter(XYsR)
849889

850-
# align time series left/right
851-
interpolater = interp1d(
852-
timesR,
853-
np.arange(len(timesR)),
854-
kind="cubic",
855-
fill_value="extrapolate",)
856-
857-
idx_aligned = np.round(interpolater(timesL)).astype(np.int)
858-
dRa = dR[idx_aligned]
859-
860-
plt.plot(timesL,zscore(dL),label='left')
861-
plt.plot(timesL,zscore(dRa),label='right')
862-
plt.title('smoothed, aligned, z-scored right/left pupil diameter \n'
863-
f'{eid}')
864-
plt.legend(loc='lower right')
865-
plt.ylabel('pupil diameter')
866-
plt.xlabel('time [sec]')
890+
# # align time series left/right
891+
# interpolater = interp1d(
892+
# timesR,
893+
# np.arange(len(timesR)),
894+
# kind="cubic",
895+
# fill_value="extrapolate",)
896+
897+
# idx_aligned = np.round(interpolater(timesL)).astype(np.int)
898+
# dRa = dR[idx_aligned]
899+
#
900+
# plt.plot(timesL,zscore(dL),label='left')
901+
# plt.plot(timesL,zscore(dRa),label='right')
902+
# plt.title('smoothed, aligned, z-scored right/left pupil diameter \n'
903+
# f'{eid}')
904+
# plt.legend(loc='lower right')
905+
# plt.ylabel('pupil diameter')
906+
# plt.xlabel('time [sec]')
867907

868908

869909

@@ -874,7 +914,7 @@ def pupil_diameter_PSTH(eid, s=None, times=None):
874914
eid = 'd0ea3148-948d-4817-94f8-dcaf2342bbbe' is good
875915
'''
876916

877-
rt = 4 # duration of window
917+
rt = 2 # duration of window
878918
st = -0.5 # lag of window wrt to stype
879919

880920
if (s is None) or (times is None):
@@ -930,7 +970,7 @@ def motion_energy_PSTH(eid):
930970
eid = '15f742e1-1043-45c9-9504-f1e8a53c1744'
931971
'''
932972

933-
rt = 4 # duration of window
973+
rt = 2 # duration of window
934974
st = -0.5 # lag of window wrt to stype
935975
stype = 'stimOn_times'
936976

@@ -940,73 +980,78 @@ def motion_energy_PSTH(eid):
940980
ts = trials.intervals[0][0]
941981
te = trials.intervals[-1][1]
942982

943-
for video_type in ['left','right','body']:
944-
t,m = get_ME(eid, video_type)
945-
m = zscore(m,nan_policy='omit')
983+
try:
984+
for video_type in ['left','right','body']:
985+
t,m = get_ME(eid, video_type)
986+
m = zscore(m,nan_policy='omit')
946987

947-
sta, end = find_nearest(t,ts), find_nearest(t,te)
948-
t = t[sta:end]
949-
m = m[sta:end]
988+
sta, end = find_nearest(t,ts), find_nearest(t,te)
989+
t = t[sta:end]
990+
m = m[sta:end]
950991

951-
ME[video_type] = [t,m]
992+
ME[video_type] = [t,m]
952993

953-
# align to body cam
954-
for video_type in ['left','right']:
994+
# align to body cam
995+
for video_type in ['left','right']:
955996

956-
# align time series camera/neural
957-
interpolater = interp1d(
958-
ME[video_type ][0],
959-
np.arange(len(ME[video_type ][0])),
960-
kind="cubic",
961-
fill_value="extrapolate")
997+
# align time series camera/neural
998+
interpolater = interp1d(
999+
ME[video_type ][0],
1000+
np.arange(len(ME[video_type ][0])),
1001+
kind="cubic",
1002+
fill_value="extrapolate")
9621003

963-
idx_aligned = np.round(interpolater(ME['body'][0])).astype(int)
964-
ME[video_type] = [ME['body'][0], ME[video_type][1][idx_aligned]]
1004+
idx_aligned = np.round(interpolater(ME['body'][0])).astype(int)
1005+
ME[video_type] = [ME['body'][0], ME[video_type][1][idx_aligned]]
9651006

966-
967-
D = {}
968-
969-
fs = 30
970-
xs = np.arange(rt*fs) # number of frames
971-
xs = np.concatenate([-1*np.array(list(reversed(xs[:int(abs(st)*fs)]))),
972-
np.arange(rt*fs)[1:1+len(xs[int(abs(st)*fs):])]])
973-
xs = xs /float(fs)
974-
975-
cols = {'left':'r','right':'b','body':'g'}
976-
977-
for video_type in ME:
978-
# that's centered at feedback time
979-
980-
981-
D[video_type] = []
1007+
1008+
D = {}
1009+
1010+
fs = 30
1011+
xs = np.arange(rt*fs) # number of frames
1012+
xs = np.concatenate([-1*np.array(list(reversed(xs[:int(abs(st)*fs)]))),
1013+
np.arange(rt*fs)[1:1+len(xs[int(abs(st)*fs):])]])
1014+
xs = xs /float(fs)
9821015

983-
times,s = ME[video_type]
984-
985-
trs = trials[stype][20:-20]
986-
for i in trs:
1016+
cols = {'left':'r','right':'b','body':'g'}
1017+
1018+
for video_type in ME:
1019+
# that's centered at feedback time
1020+
1021+
1022+
D[video_type] = []
1023+
1024+
times,s = ME[video_type]
9871025

988-
start_idx = int(find_nearest(times,i) + st*30)
989-
end_idx = int(start_idx + rt*30)
1026+
trs = trials[stype][20:-20]
1027+
for i in trs:
9901028

991-
D[video_type].append(s[start_idx:end_idx])
1029+
start_idx = int(find_nearest(times,i) + st*30)
1030+
end_idx = int(start_idx + rt*30)
9921031

993-
994-
MEAN = np.mean(D[video_type],axis=0)
995-
STD = np.std(D[video_type],axis=0)/np.sqrt(len(trs))
996-
1032+
D[video_type].append(s[start_idx:end_idx])
9971033

998-
plt.plot(xs, MEAN, label=video_type,
999-
color = cols[video_type], linewidth = 2)
1000-
plt.fill_between(xs, MEAN + STD, MEAN - STD, color = cols[video_type],
1001-
alpha=0.2)
10021034

1003-
ax = plt.gca()
1004-
ax.axvline(x=0, label='stimOn', linestyle = '--', c='k')
1005-
plt.title('Motion Energy PSTH')
1006-
plt.xlabel('time [sec]')
1007-
plt.ylabel('z-scored motion energy [a.u.]')
1008-
plt.legend(loc='lower right')
1035+
MEAN = np.mean(D[video_type],axis=0)
1036+
STD = np.std(D[video_type],axis=0)/np.sqrt(len(trs))
1037+
1038+
1039+
plt.plot(xs, MEAN, label=video_type,
1040+
color = cols[video_type], linewidth = 2)
1041+
plt.fill_between(xs, MEAN + STD, MEAN - STD, color = cols[video_type],
1042+
alpha=0.2)
1043+
1044+
ax = plt.gca()
1045+
ax.axvline(x=0, label='stimOn', linestyle = '--', c='k')
1046+
plt.title('Motion Energy PSTH')
1047+
plt.xlabel('time [sec]')
1048+
plt.ylabel('z-scored motion energy [a.u.]')
1049+
plt.legend(loc='lower right')
1050+
1051+
except:
1052+
plt.title('No motion energy available!')
10091053

1054+
10101055

10111056
def interp_nans(y):
10121057

@@ -1030,15 +1075,15 @@ def add_panel_letter(k):
10301075

10311076

10321077
def plot_all(eid):
1033-
matplotlib.rcParams.update({'font.size': 15})
1078+
matplotlib.rcParams.update({'font.size': 10})
10341079
# report eid = '4a45c8ba-db6f-4f11-9403-56e06a33dfa4'
10351080

10361081
nrows = 2
10371082
ncols = 4
10381083

1039-
plt.ion()
1084+
plt.ioff()
10401085
one = ONE()
1041-
plt.figure(figsize=(10,10))
1086+
plt.figure(figsize=(15,10))
10421087
plt.subplot(nrows,ncols,1)
10431088
add_panel_letter(1)
10441089
plot_paw_on_image(eid, 'left')
@@ -1069,20 +1114,32 @@ def plot_all(eid):
10691114
add_panel_letter(8)
10701115
motion_energy_PSTH(eid)
10711116

1072-
1073-
# plt.subplot(3,3,7)
1074-
# plot_wheel_position(eid)
1075-
# plt.subplot(3,3,8)
1076-
#paw_speed_PSTH(eid)
10771117
#plt.subplot(3,3,9)
10781118
#plot_sniffPSTH(eid)
10791119

10801120
plt.tight_layout()
10811121
p = one.path_from_eid(eid)
10821122
plt.suptitle(' '.join([str(p).split('/')[i] for i in [4,6,7,8]]),
10831123
backgroundcolor= 'white')
1084-
# plt.savefig(f'/home/mic/reproducible_dlc/{eid}.png')
1085-
# plt.close()
1124+
plt.tight_layout()
1125+
plt.tight_layout()
1126+
plt.savefig(f'/home/mic/reproducible_dlc/overviewJune/{eid}.png')
1127+
plt.close()
1128+
1129+
1130+
#BWM sessions that errored out when plotting
1131+
#['7b26ce84-07f9-43d1-957f-bc72aeb730a3',
1132+
# '4b7fbad4-f6de-43b4-9b15-c7c7ef44db4b',
1133+
# 'ecb5520d-1358-434c-95ec-93687ecd1396',
1134+
# 'a52f5a1b-7f45-4f2c-89a9-fb199d2a0d63',
1135+
# '17602713-0ac6-49c1-b134-ee580de84729',
1136+
# 'b03fbc44-3d8e-4a6c-8a50-5ea3498568e0',
1137+
# '064a7252-8e10-4ad6-b3fd-7a88a2db5463',
1138+
# '71e55bfe-5a3a-4cba-bdc7-f085140d798e',
1139+
# '15763234-d21e-491f-a01b-1238eb96d389',
1140+
# 'd9f0c293-df4c-410a-846d-842e47c6b502',
1141+
# 'ebe090af-5922-4fcd-8fc6-17b8ba7bad6d',
1142+
# '7f6b86f9-879a-4ea2-8531-294a221af5d0']
10861143

10871144

10881145

0 commit comments

Comments
 (0)