-
Notifications
You must be signed in to change notification settings - Fork 1
/
plotreceptivefields.py
169 lines (136 loc) · 4.31 KB
/
plotreceptivefields.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 17 14:24:09 2018
@author: ycan
"""
import warnings
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from matplotlib.patches import Ellipse
import numpy as np
import gaussfitter as gfit
import iofuncs as iof
import analysis_scripts as asc
import miscfuncs as msc
exp = '20180710_kilosorted'
sorted_stimuli = asc.stimulisorter(exp)
#checker = sorted_stimuli['frozennoise'][0]
checker = sorted_stimuli['checkerflicker'][0]
data = iof.load(exp, checker)
parameters = asc.read_parameters(exp, checker)
stas = data['stas']
max_inds = data['max_inds']
i = 0
sta = stas[i]
max_i = max_inds[i]
bound = 1.5
#%%
def fitgaussian(sta, f_size=10):
max_i = np.unravel_index(np.argmax(np.abs(sta)), sta.shape)
try:
sta, max_i_cut = msc.cut_around_center(sta, max_i, f_size)
except ValueError as e:
if str(e).startswith('Frame is out'):
raise ValueError('Fit failed.')
fit_frame = sta[..., max_i_cut[-1]]
# Parameters are in the format:
# (height,amplitude,center_x,center_y,width_x,width_y,rota)
pars = gfit.gaussfit(fit_frame)
# f = gfit.twodgaussian(pars)
pars_out = pars
pars_out[2:4] = pars[2:4] - [f_size, f_size] + max_i[:2]
return pars_out
def mahalonobis_convert(Z, pars):
with warnings.catch_warnings():
warnings.filterwarnings('ignore', '.*divide by zero*.', RuntimeWarning)
Zm = np.log((Z-pars[0])/pars[1])
Zm[np.isinf(Zm)] = np.nan
Zm = np.sqrt(Zm*-2)
return Zm
#%%
def drawellipse(pars, bound, ax=None):
if ax is None:
ax = plt.gca()
# Flip it for correct x-y axis
center = pars[2:4][::-1]
width_x = pars[5]*2 # HINT: magic number, possibly due to diameter vs radius
width_y = pars[4]*2 # difference in the arguments accepted by Ellipse and gaussfit
width_x *= bound
width_y *= bound
angle = pars[-1]
rf = Ellipse(tuple(center), width_x, width_y, angle,
fill=False, edgecolor='green', lw=1)
ax.add_artist(rf)
return rf
def label_ellipse(pars, text, ax=None):
if ax is None:
ax = plt.gca()
center = pars[2:4][::-1]
ax.text(*center, text, va='center', ha='center')
#%%
stixelw, stixelh = parameters['stixelwidth'],parameters['stixelheight']
if stixelw != stixelh:
ValueError('Stimulus is not checkerflicker.')
Y, X = np.meshgrid(np.arange(sta.shape[0]),
np.arange(sta.shape[1]), indexing='xy')
plt.figure()
ax = plt.subplot(111)
ax.axis('equal')
all_pars = np.zeros((len(stas), 7))
for i, _ in enumerate(data['clusters']):
sta = stas[i]
max_i = max_inds[i]
try:
pars = fitgaussian(sta)
except ValueError as e:
if str(e).startswith('Fit failed'):
continue
all_pars[i, :] = pars
f = gfit.twodgaussian(pars)
Z = f(X, Y)
Zm = mahalonobis_convert(Z, pars)
drawellipse(pars, bound, ax)
label_ellipse(pars, i, ax)
with warnings.catch_warnings():
warnings.filterwarnings('ignore', category=UserWarning)
# ax.contour(X, Y, Zm, [bound])
plt.axis([0, sta.shape[1], 0, sta.shape[0]])
#image = mpimg.imread('/media/ycan/datadrive/data/Erol_20180207/microscope_images/afterexperiment_grid.tif')
#ax.imshow(image)
#plt.show()
#plt.savefig(f'/home/ycan/Downloads/TAC_outgoingfiles/RFs_{exp}.svg')
#%%
import plotfuncs as plf
stas = np.array(stas)
fig, sl = plf.multistabrowser(stas)
for i in range(stas.shape[0]):
ax = fig.axes[i]
drawellipse(all_pars[i], 1.5, ax)
#%%
import pandas as pd
import seaborn as sns
pars_filtered = all_pars[:, (4, 5)]
#pars_filtered = pars_filtered[pars_filtered[:, 0]<5]
#pars_filtered = pars_filtered[pars_filtered[:, 1]<5]
sizes = pd.DataFrame(data=pars_filtered, columns=['x', 'y'])
#sns.set(style='darkgrid')
g = sns.jointplot('x','y', sizes, 'scatter',
# shade_lowest = False,
# , xlim=[0, 6], ylim=[0, 6]
)
#%%
import plotfuncs as plf
plf.absmax() # stop the script
for i in range(len(stas)):
sta = stas[i]
plt.imshow(sta[..., max_inds[i][-1]], cmap='RdBu_r',
vmax=asc.absmax(sta), vmin=asc.absmin(sta))
drawellipse(all_pars[i])
plt.title(f'{i}')
plt.show()
#%%
fig, sl = plf.multistabrowser(stas)
for i in range(len(stas)):
ax = fig.axes[i]
drawellipse(all_pars[i], ax)