-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcenter_of_mass.py
executable file
·178 lines (150 loc) · 4.16 KB
/
center_of_mass.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
#!/usr/bin/env python
from sys import argv
import numpy as np
from matplotlib import pyplot as plt
import os
from iutils import *
from call_log import *
from tracking_processing import whl_to_pos, whl_to_speed
import matplotlib.animation as animation
from scipy import stats
def plot_kde(kernel):
xmin=0
ymin=0
xmax=400
ymax=200
X, Y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = np.vstack([X.ravel(), Y.ravel()])
Z = np.reshape(kernel(positions).T, X.shape)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(np.rot90(Z), cmap=plt.cm.gist_earth_r, extent=[xmin, xmax, ymin, ymax])
# ax.plot(m1, m2, 'k.', markersize=2)
ax.set_xlim([xmin, xmax])
ax.set_ylim([ymin, ymax])
# plt.colorbar()
plt.show()
if len(argv) < 3:
print 'USAGE: (1)<base for whl, clu, res> (2)<speed threshold>'
exit(0)
argv = resolve_vars(argv)
log_call(argv)
base = argv[1]
ST = float(argv[2])
EBORD = 180
whl = whl_to_pos(open(base + 'whl'), False)
speed = whl_to_speed(whl)
clu = read_int_list(base + 'clu')
res = read_int_list(base + 'res')
# for kde, speed-filtered
# whl_s = np.vstack([[whl[i][0] for i in range(len(whl)) if speed[i] > ST],[[whl[i][1] for i in range(len(whl)) if speed[i] > ST]]])
NCELL = max(clu)
coms1 = [] # cumulative coordinates for each cell [x, y]
coms2 = [] # cumulative coordinates for each cell [x, y]i
coms = [coms1, coms2]
ns1 = [0] * NCELL
ns2 = [0] * NCELL
ns = [ns1, ns2]
for c in range(NCELL):
coms[0].append([0, 0])
coms[1].append([0, 0])
intervals = []
# load intervals according to session: learning, post-probe, post-learning
pth = os.getcwd()
if 'post' in pth:
intervals = [[0, 100000000]]
else:
# read trials
ftr = open(base + 'whl.trials')
trials = []
for line in ftr:
trials.append([int(t) for t in line.split(' ')])
if '9l' in pth:
if '1123' in pth:
intervals.append([0, trials[21][1]])
else:
# use first 10 trials
intervals.append([0, trials[10][1]])
elif '16l' in pth:
intervals.append(trials[0])
if '1123' in pth:
intervals.append(trials[10])
elif '1129' in pth:
intervals.append(trials[3])
elif '0106' in pth:
intervals.append(trials[1])
else:
intervals.append(trials[5])
else:
print 'ERROR: uknown session'
exit(1)
print 'Intervals:', intervals
iind = 0
#plt.hist([s for s in speed if s < 10])
#plt.show()
whl_s = []
for i in range(len(whl)):
t = i * 480
sp = speed[i]
if whl[i][0] < 0.01:
continue
if t > intervals[iind][1]:
# go to next interval or exit if out of intervals
if iind == len(intervals) - 1:
break
else:
iind += 1
while i < len(whl) and i*480 < intervals[iind][0]:
i += 1
continue
if sp > ST:
whl_s.append(whl[i])
whl_s = np.vstack([[w[0] for w in whl_s], [w[1] for w in whl_s]])
kernel = stats.gaussian_kde(whl_s)
# DEBUG plot pos kde
# plot_kde(kernel)
for i in range(len(clu)):
c = clu[i] - 1
t = res[i]
twhl = res[i] / 480
sp = speed[twhl]
if t > intervals[iind][1]:
# go to next interval or exit if out of intervals
if iind == len(intervals) - 1:
break
else:
iind += 1
while i < len(res) and res[i] < intervals[iind][0]:
i += 1
continue
if sp > ST and whl[twhl][0] > 0.001:
x = whl[twhl][0]
e = int(x > EBORD)
k = kernel([whl[twhl]])[0]
if k > 1e-5:
# INVERSE ?
coms[e][c][0] += whl[twhl][0] / (k*100000)
coms[e][c][1] += whl[twhl][1] / (k*100000)
ns[e][c] += 1/(k*100000)
SC = 6.0
for c in range(NCELL):
if ns[0][c] > 0:
coms[0][c][0] /= float(ns[0][c]) * SC
coms[0][c][1] /= float(ns[0][c]) * SC
if ns[1][c] > 0:
coms[1][c][0] /= float(ns[1][c]) * SC
coms[1][c][1] /= float(ns[1][c]) * SC
print 'Cell %d centers of mass' % c, coms[0][c], coms[1][c]
else:
print 'No spikes of cell', c
# write celter of mass
outpath = base + 'rev2.com'
if os.path.isfile(outpath):
print 'The destination file exists:', base + 'com'
exit(1)
if os.path.isfile(outpath):
print 'ERROR: output file exists!'
exit(1)
fo = open(outpath, 'w')
for c in range(NCELL):
fo.write((4*'%.2f '+'\n') % (coms1[c][0], coms1[c][1], coms2[c][0], coms2[c][1]))