-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscrimination.py
205 lines (170 loc) · 7.8 KB
/
discrimination.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
import session_directory as sd
import placefield_stability as pfs
import numpy as np
import Placefields as pf
import helpers as hlp
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import er_plot_functions as er
import scipy as sp
from os import path
import scipy.io as sio
from session_directory import find_eraser_directory as get_dir
def get_DI_scores(mouse, arena1, day1, arena2, day2, **kwargs):
"""
Gets neural discrimination index between sessions.
:param mouse:
:param arena1:
:param day1:
:param arena2:
:param day2:
"""
# Get mapping between sessions
neuron_map = pfs.get_neuronmap(mouse, arena1, day1, arena2, day2, **kwargs)
reg_session = sd.find_eraser_session(mouse, arena2, day2)
good_map_bool, silent_ind, new_ind = pfs.classify_cells(neuron_map, reg_session)
good_map = neuron_map[good_map_bool].astype(np.int64)
# Identify neurons with proper mapping between sessions
try:
good_sesh1_ind, _ = np.where(good_map_bool)
except ValueError:
good_sesh1_ind = np.where(good_map_bool)
ngood = len(good_sesh1_ind)
# load in PF objects for between sessions
try:
PF1 = pf.load_pf(mouse, arena1, day1, pf_file='placefields_cm1_manlims_1000shuf.pkl')
PF2 = pf.load_pf(mouse, arena2, day2, pf_file='placefields_cm1_manlims_1000shuf.pkl')
# load in event and sampling rates
fps1 = hlp.get_sampling_rate(PF1)
ER1, _ = hlp.get_eventrate(PF1.PSAbool_align, fps1)
fps2 = hlp.get_sampling_rate(PF2)
ER2, _ = hlp.get_eventrate(PF2.PSAbool_align, fps2)
# calculate the discrimination index
DIscores = np.zeros(ngood)
DIscores = (ER1[good_sesh1_ind] - ER2[good_map]) / (ER1[good_sesh1_ind] + ER2[good_map])
return DIscores
except FileNotFoundError: # If pf files are missing, return NaN
return np.nan
def get_matched_event_rates(mouse, arena1, day1, arena2, day2, **kwargs):
"""
Gets event rates for each neuron active across both sessions.
:param mouse:
:param arena1:
:param day1:
:param arena2:
:param day2:
"""
# Get mapping between sessions
neuron_map = pfs.get_neuronmap(mouse, arena1, day1, arena2, day2, **kwargs)
reg_session = sd.find_eraser_session(mouse, arena2, day2)
good_map_bool, silent_ind, new_ind = pfs.classify_cells(neuron_map, reg_session)
good_map = neuron_map[good_map_bool].astype(np.int64)
# Identify neurons with proper mapping between sessions
try:
good_sesh1_ind, _ = np.where(good_map_bool)
except ValueError:
good_sesh1_ind = np.where(good_map_bool)
ngood = len(good_sesh1_ind)
# load in PF objects for between sessions
try:
PF1 = pf.load_pf(mouse, arena1, day1, pf_file='placefields_cm1_manlims_1000shuf.pkl')
PF2 = pf.load_pf(mouse, arena2, day2, pf_file='placefields_cm1_manlims_1000shuf.pkl')
# load in event and sampling rates
fps1 = hlp.get_sampling_rate(PF1)
ER1, _ = hlp.get_eventrate(PF1.PSAbool_align, fps1)
fps2 = hlp.get_sampling_rate(PF2)
ER2, _ = hlp.get_eventrate(PF2.PSAbool_align, fps2)
except FileNotFoundError:
PSAboth, srboth = [], []
for arena, day in zip([arena1, arena2], [day1, day2]):
dir_use1 = get_dir(mouse, arena, day)
im_data_file = path.join(dir_use1, 'FinalOutput.mat')
im_data = sio.loadmat(im_data_file)
PSAboth.append(im_data['PSAbool'])
srboth.append(im_data['SampleRate'].squeeze())
ER1, _ = hlp.get_eventrate(PSAboth[0], srboth[0])
ER2, _ = hlp.get_eventrate(PSAboth[1], srboth[1])
return np.array([ER1[good_sesh1_ind], ER2[good_map]])
# except FileNotFoundError: # If pf files are missing, return NaN
# return np.nan
def get_matched_peak_heights(mouse, arena1, day1, arena2, day2, raw_type='RawTrace', **kwargs):
# Get mapping between sessions
neuron_map = pfs.get_neuronmap(mouse, arena1, day1, arena2, day2, **kwargs)
reg_session = sd.find_eraser_session(mouse, arena2, day2)
good_map_bool, silent_ind, new_ind = pfs.classify_cells(neuron_map, reg_session)
good_map = neuron_map[good_map_bool].astype(np.int64)
# Identify neurons with proper mapping between sessions
try:
good_sesh1_ind, _ = np.where(good_map_bool)
except ValueError:
good_sesh1_ind = np.where(good_map_bool)
ngood = len(good_sesh1_ind)
peak_means_all = []
for arena, day in zip([arena1, arena2], [day1, day2]):
dir_use1 = get_dir(mouse, arena, day)
im_data_file = path.join(dir_use1, 'FinalOutput.mat')
im_data = sio.loadmat(im_data_file)
psabool = im_data['PSAbool']
rawtrace = im_data['NeuronTraces'][0][raw_type][0]
peaks = []
for raw, psa in zip(rawtrace, psabool):
peaks.append(hlp.get_transient_peaks(raw, psa))
peak_means = np.array([np.mean(peak) for peak in peaks])
peak_means_all.append(peak_means)
return peak_means_all[0][good_sesh1_ind], peak_means_all[1][good_map]
def get_on_off_cells(mouse, arena1, day1, arena2, day2):
# Get mapping between sessions and use Nat's classify cells function to return good_map_bool, silent_ind, and new_ind
neuron_map = pfs.get_neuronmap(mouse, arena1, day1, arena2, day2)
reg_session = sd.find_eraser_session(mouse, arena2, day2)
good_map_bool, silent_ind, new_ind = pfs.classify_cells(neuron_map, reg_session)
both_cells = int(sum(good_map_bool == True)) # good_map_bool == true sum will yield total number of neurons that stayed on in both sessions
on_cells = len(new_ind) # len of new_ind corresponds to the number of new neurons appearing in session 2
off_cells = len(silent_ind) # len of silent_ind corresponds to the number of new neurons appearing in session 2
return both_cells, on_cells, off_cells
def DI_CC_scatter(mice):
nmice = len(mice)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, facecolor="1.0")
DIarray_total = []
on_off_norm_total = np.empty((nmice, 3))
idc = 0
idr = 0
days = [1, 2, 7]
for mouse in mice:
for day in days:
DIarray =[]
# this should spit out the mouse with issues, then you can zero in after that via debugging
# (make sure to comment out try/except statement first)
#try:
DIval = er.DIFreeze(mouse, [day])
DIarray += DIval
DIarray_total += DIval
cc_both, cc_on, cc_off = get_on_off_cells(mouse, "Open", day, "Shock", day)
on_off_norm = np.divide((cc_on + cc_off), (cc_on + cc_off + cc_both))
on_off_norm_total[idr, idc] = on_off_norm
ax.scatter(DIarray, on_off_norm)
#except ValueError:
#print('Error in ' + mouse)
idc += 1
idc = 0
idr += 1
title = input("What is your title?")
plt.title(title)
plt.xlabel("DI_Freeze")
plt.ylabel("Normalized cell count for on and off cells")
try:
on_off_norm_total = on_off_norm_total.reshape(-1)
DIarray_total = np.array(DIarray_total)
good_pts_bool = np.bitwise_and(~np.isnan(DIarray_total), ~np.isnan(on_off_norm_total.reshape(-1)))
z = np.polyfit(DIarray_total[good_pts_bool], on_off_norm_total[good_pts_bool], 1)
p = np.poly1d(z)
plt.plot(ax.get_xlim(), p(ax.get_xlim()), "r--")
except ValueError:
print('Error in trend line')
Corrcoef = sp.stats.pearsonr(DIarray_total[good_pts_bool], on_off_norm_total[good_pts_bool])
plt.annotate("Pearson Correlation Coefficients: R = " + str(round(Corrcoef[0],3)) + "; P = " + str(round(Corrcoef[1],3)), xy=(0.15, 0.95), xycoords='axes fraction')
plt.show()
return Corrcoef
if __name__ == '__main__':
DIneurons = get_DI_scores('Marble06', 'Open', 1, 'Shock', 1)
pass