forked from ycanerol/pymer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkersvdsurround.py
143 lines (116 loc) · 3.24 KB
/
checkersvdsurround.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 29 11:54:47 2018
@author: ycan
"""
import iofuncs as iof
import plotfuncs as plf
import numpy as np
import miscfuncs as msc
import matplotlib.pyplot as plt
import gaussfitter as gfit
#%%
def getfit(frame):
if np.max(frame) != np.max(np.abs(frame)):
onoroff = -1
else:
onoroff = 1
pars = gfit.gaussfit(frame*onoroff)
f = gfit.twodgaussian(pars)
return f, pars, onoroff
def mahalonobis_convert(Z, pars):
Zm = np.log((Z-pars[0])/pars[1])
Zm[np.isinf(Zm)] = np.nan
Zm = np.sqrt(Zm*-2)
return Zm
#%%
exp_name = 'V'
stim_nr = 10
data = iof.load(exp_name, stim_nr)
clusters = data['clusters']
max_inds = data['max_inds']
stas = data['stas']
stx_h = data['stx_h']
exp_name = data['exp_name']
stimname = data['stimname']
max_inds = data['max_inds']
frame_duration = data['frame_duration']
filter_length = data['filter_length']
quals = data['quals'][-1, :]
spikenrs = np.array([a.sum() for a in data['all_spiketimes']])
clusterids = plf.clusters_to_ids(clusters)
# Determine frame size so that the total frame covers
# an area large enough i.e. 2*700um
px_size = 7.5
f_size = int(700/(stx_h*px_size))
i = 63
#%%
sta = stas[i]
max_i = max_inds[i]
fit_frame = sta[:, :, max_i[2]]
stac, max_i = msc.cut_around_center(sta, max_i, f_size+2)
sp1, sp2, t1, t2, _, _ = msc.svd(stac)
#%%
plt.figure(figsize=(12, 10))
vmax = np.max(np.abs([sp1, sp2]))
vmin = -vmax
plt.subplot(131)
plt.imshow(sp1, cmap=iof.config('colormap'), vmin=vmin, vmax=vmax)
plt.subplot(132)
plt.imshow(sp2, cmap=iof.config('colormap'), vmin=vmin, vmax=vmax)
plt.subplot(133)
im = plt.imshow(fit_frame, cmap=iof.config('colormap'), vmin=vmin, vmax=vmax)
plf.colorbar(im, size='2%', ticks=[vmin, vmax], format='%.2f')
plt.show()
sp1c, maxic = msc.cut_around_center(sp1, max_i, f_size)
sp2c, maxic = msc.cut_around_center(sp2, max_i, f_size)
fit_framec, maxic = msc.cut_around_center(fit_frame, max_i, f_size)
plt.figure(figsize=(12, 10))
vmax = np.max(np.abs([sp1c, sp2c]))
vmin = -vmax
plt.subplot(131)
plt.imshow(sp1c, cmap=iof.config('colormap'), vmin=vmin, vmax=vmax)
plt.subplot(132)
plt.imshow(sp2c, cmap=iof.config('colormap'), vmin=vmin, vmax=vmax)
plt.subplot(133)
im = plt.imshow(fit_framec, cmap=iof.config('colormap'), vmin=vmin, vmax=vmax)
plf.colorbar(im, size='2%', ticks=[vmin, vmax], format='%.2f')
plt.show()
#%%
sp = sp1c
rows = 2
columns = 2
plt.figure(figsize=(12, 10))
f, pars0, pol0 = getfit(sp)
X, Y = np.meshgrid(np.arange(sp.shape[0]),
np.arange(sp.shape[1]))
Z0 = f(Y, X)
Z0m = mahalonobis_convert(Z0, pars0)
ax0 = plt.subplot(rows, columns, 1)
plf.stashow(sp, ax0)
ax0.contour(X, Y, Z0m, [2])
d1 = sp - Z0*pol0
f1, pars1, pol1 = getfit(d1)
Z1 = f1(Y, X)
Z1m = mahalonobis_convert(Z1, pars1)
ax1 = plt.subplot(rows, columns, 2)
plf.stashow(d1, ax1)
ax1.contour(X, Y, Z1m, [1.4])
d2 = sp - Z1*pol1
f2, pars2, pol2 = getfit(d2)
Z2 = f2(Y, X)
Z2m = mahalonobis_convert(Z2, pars2)
ax2 = plt.subplot(rows, columns, 3)
plf.stashow(d2, ax2)
ax2.contour(X, Y, Z2m, [2])
plt.show()
#%%
plt.figure(figsize=(20, 15))
rec = Z0*pol0+Z1*pol1*2
ax3 = plt.subplot(221)
plf.stashow(rec, ax3)
ax4 = plt.subplot(222)
plf.stashow(sp, ax4)
ax5 = plt.subplot(223)
plf.stashow(sp-rec, ax5)