-
Notifications
You must be signed in to change notification settings - Fork 0
/
createData3.py
326 lines (269 loc) · 10.4 KB
/
createData3.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# position dependent firing rate
# discontinuous change of place field params
import os
import utilities as _U
from utils import createSmoothedPath, createSmoothedPathK
import numpy as _N
import matplotlib.pyplot as _plt
import pickle
import time as _tm
UNIF = 0
NUNIF = 1
APPEAR=0
DISAPPEAR=1
NEITHER=-1
#_N.array([[0, 0.02], [0.1, 0.03], [0.2, 0.04], [0.5, 0.05], [0.7, 0.06]]
def chpts(npts, yL, yH, t0=0, ad=NEITHER):
# [tn, dy]
cpts = _N.empty((npts, 2))
ts = _N.random.rand(npts)
ts[0] = 0
for i in xrange(1, npts):
ts[i] += ts[i-1]
ts /= _N.sum(ts[-1])
cpts[:, 0] = ts
F = 0.9999
x = _N.random.randn()
cpts[0, 1] = x
for i in xrange(1, npts):
x = F*x + 0.1*_N.random.randn()
cpts[i, 1] = x
miny = _N.min(cpts[:, 1])
cpts[:, 1] -= miny # min value is now 0
maxy = _N.max(cpts[:, 1])
cpts[:, 1] /= maxy # max value is now 1
cpts[:, 1] *= (yH-yL) # max value is now 1
cpts[:, 1] += yL
if ad != NEITHER:
if ad == APPEAR:
cpts[0:t0, 1] = 0
else:
cpts[t0:, 1] = 0
return cpts
def makeCovs(nNrns, K, wvfmsz):
Covs = _N.empty((nNrns, K, K))
for n in xrange(nNrns):
for k1 in xrange(K):
#Covs[n, k1, k1] = (LoHisMk[n, k1, 1] - LoHisMk[n, k1, 0])*(0.1+0.1*_N.random.rand())
Covs[n, k1, k1] = (wvfmsz[n, k1, 0] + (wvfmsz[n, k1, 1] - wvfmsz[n, k1, 0])*_N.random.rand())**2
for k1 in xrange(K):
for k2 in xrange(k1+1, K):
Covs[n, k1, k2] = (0.4+0.4*_N.random.rand()) * _N.sqrt(Covs[n, k1, k1]*Covs[n, k2, k2])#(0.5 + 0.3*_N.random.rand()) * _N.sqrt(Covs[n, k1, k1]*Covs[n, k2, k2])
Covs[n, k2, k1] = Covs[n, k1, k2]
return Covs
def create(Lx, Hx, N, mvPat, RTs, frqmx, Amx, pT, l_sx_chpts, l_l0_chpts, l_ctr_chpts, mk_chpts, Covs, LoHis, km, bckgrdLam=None, script="no info", addShortStops=False, stops=10, stopDur=500, thresh=None):
"""
km tells me neuron N gives rise to clusters km[N] (list)
bckgrd is background spike rate (Hz)
"""
global UNIF, NUNIF
##### First check that the number of neurons and PFs all consistent.
nNrnsSX = len(l_sx_chpts)
nNrnsL0 = len(l_l0_chpts)
nNrnsCT = len(l_ctr_chpts)
nNrnsMK = len(mk_chpts)
nNrnsMKA= LoHis.shape[0]
nNrnsMKC= Covs.shape[0]
if not (nNrnsSX == nNrnsL0 == nNrnsCT == nNrnsMK == nNrnsMKA == nNrnsMKC):
print "Number of neurons not consistent"
return None
nNrns = nNrnsSX
if not (LoHis.shape[1] == Covs.shape[1] == Covs.shape[2]):
print "Covariance of LoHis not correct"
return None
K = LoHis.shape[1]
PFsPerNrn = _N.zeros(nNrns, dtype=_N.int)
sx_chpts = []
l0_chpts = []
ctr_chpts = []
M = 0
nrnNum = []
for nrn in xrange(nNrns):
# # of place fields for neuron nrn
nPFsSX = len(l_sx_chpts[nrn])
nPFsL0 = len(l_l0_chpts[nrn])
nPFsCT = len(l_ctr_chpts[nrn])
sx_chpts.extend(l_sx_chpts[nrn])
l0_chpts.extend(l_l0_chpts[nrn])
ctr_chpts.extend(l_ctr_chpts[nrn])
if not (nPFsSX == nPFsL0 == nPFsCT):
print "Number of PFs for neuron %d not consistent" % nrn
return None
M += len(l_ctr_chpts[nrn])
nrnNum += [nrn]*nPFsSX
PFsPerNrn[nrn] = nPFsSX
# M = # of clusters (in mark + pos space)
# nNrns = # of neurons
#### build data
Ns = _N.empty(RTs, dtype=_N.int)
if mvPat == NUNIF:
for rt in xrange(RTs):
Ns[rt] = N*((1-pT) + pT*_N.random.rand())
else:
Ns[:] = N
NT = _N.sum(Ns) # total time we have data
pths = _N.empty(NT)
x01 = _N.linspace(0, 1, len(pths))
x01 = x01.reshape((1, NT))
plastic = False
########## nonstationary center width
# sxt should be (M x NT)
sxt = _N.empty((M, NT))
for m in xrange(M): # sxts time scale
sxt[m] = createSmoothedPath(sx_chpts[m], NT)
if len(sx_chpts[m]) > 1: plastic = True
sx = sxt**2 # var of firing rate function
########## nonstationary center height l0
# f is NT x M
l0 = _N.empty((M, NT))
for m in xrange(M):
l0[m] = createSmoothedPath(l0_chpts[m], NT)
if len(l0_chpts[m]) > 1: plastic = True
f = l0/_N.sqrt(2*_N.pi*sx) # f*dt
########## nonstationary center location
ctr = _N.empty((M, NT))
for m in xrange(M):
ctr[m] = createSmoothedPath(ctr_chpts[m], NT)
if len(ctr_chpts[m]) > 1: plastic = True
if K > 0:
########## nonstationary marks
mk_MU = _N.empty((nNrns, NT, K))
for n in xrange(nNrns):
mk_MU[n] = createSmoothedPathK(mk_chpts[n], NT, K, LoHis[n])
if len(mk_chpts[n]) > 1: plastic = True
if mvPat == NUNIF:
now = 0
for rt in xrange(RTs):
N = Ns[rt] # each traverse slightly different duration
rp = _N.random.rand(N/100)
x = _N.linspace(Lx, Hx, N)
xp = _N.linspace(Lx, Hx, N/100)
r = _N.interp(x, xp, rp) # creates a velocity vector
# create movement without regard for place field
frqmxR = _N.abs(frqmx*(1+0.25*_N.random.randn()))
_N.linspace(0, 1, N, endpoint=False)
rscld_t = _N.random.rand(N) # rscld_t
rscld_t /= (_N.max(rscld_t)*1.01)
rscld_t.sort()
phi0 = _N.random.rand()*2*_N.pi
r += _N.exp(Amx*_N.sin(2*_N.pi*rscld_t*frqmxR + phi0))
pth = _N.zeros(N+1)
for n in xrange(1, N+1):
pth[n] = pth[n-1] + r[n-1]
pth /= (pth[-1] - pth[0])
pth *= (Hx-Lx)
pth += Lx
pths[now:now+N] = pth[0:N]
now += N
else:
now = 0
x = _N.linspace(Lx, Hx, N)
for rt in xrange(RTs):
N = Ns[rt]
pths[now:now+N] = x
now += N
if addShortStops:
for ist in xrange(stops):
done = False
while not done:
t0 = int(_N.random.rand()*NT)
t1 = t0 + int(stopDur*(1+0.1*_N.random.randn()))
if _N.abs(_N.max(_N.diff(pths[t0:t1]))) < 0.05*(Hx-Lx):
done = True # not crossing origin
pths[t0:t1] = _N.mean(pths[t0:t1])
### now calculate firing rates
dt = 0.001
fdt = f*dt
# change place field location
Lam = f*dt*_N.exp(-0.5*(pths-ctr)**2 / sx)
#_N.savetxt("lam", Lam.T)
#_N.savetxt("pths", pths)
rnds = _N.random.rand(M, NT)
#dat = _N.zeros((NT, 2 + K))
dat = _N.zeros((NT, 2 + K))
dat[:, 0] = pths
datNghbr = _N.zeros((NT, 2 + K)) ## if spk in bin already exists, nxt door
datNghbr[:, 0] = pths
for m in xrange(M):
sts = _N.where(rnds[m] < Lam[m])[0] # spikes from this neuron
alrdyXst = _N.where(dat[:, 1] == 1)[0] # places where synchronous
ndToMove = _N.intersect1d(alrdyXst, sts)
dntMove = _N.setdiff1d(sts, ndToMove) # empty slots
nonsynch = _N.empty(len(sts))
datNghbr[dntMove, 1] = 1
nonsynch[0:len(dntMove)] = dntMove
print len(ndToMove)
iStart = len(dntMove) # in nonsynch
for iOcpd in ndToMove: # occupied
bDone = False
while not bDone:
iOcpd += 1
if datNghbr[iOcpd, 1] == 0:
datNghbr[iOcpd, 1] = 1
nonsynch[iStart] = iOcpd
iStart += 1
bDone = True
snonsynch = _N.sort(nonsynch)
dat[sts, 1] = 1
nrn = nrnNum[m]
if K > 0:
for t in xrange(len(sts)):
obsMrk = _N.random.multivariate_normal(mk_MU[nrn, sts[t]], Covs[nrn], size=1)
dat[sts[t], 2:] = obsMrk
datNghbr[nonsynch[t], 2:] = obsMrk
# now noise spikes
if bckgrdLam is not None:
nzsts = _N.where(rnds[m] < (bckgrdLam*dt)/float(M))[0]
dat[nzsts, 1] = 1
nrn = nrnNum[m]
if K > 0:
for t in xrange(len(nzsts)):
dat[nzsts[t], 2:] = _N.random.multivariate_normal(mk_MU[nrn, nzsts[t]], Covs[nrn], size=1)
if thresh is not None:
sts = _N.where(dat[:, 1] == 1)[0]
nID, nC = _N.where(dat[sts, 2:] < thresh)
swtchs = _N.zeros((len(sts), K))
swtchs[nID, nC] = 1 # for all cells, all components below hash == 1
swtchsK = _N.sum(swtchs, axis=1)
blw_thrsh_all_chs = _N.where(swtchsK == K)[0]
abv_thr = _N.setdiff1d(_N.arange(len(sts)), blw_thrsh_all_chs)
print "below thresh in all channels %(1)d / %(2)d" % {"1" : len(blw_thrsh_all_chs), "2" : len(sts)}
dat[sts[blw_thrsh_all_chs], 1:] = 0
bFnd = False
## us un uniform sampling of space, stationary or non-stationary place field
## ns nn non-uni sampling of space, stationary or non-stationary place field
## bs bb biased and non-uni sampling of space
bfn = "" if (M == 1) else ("%d" % M)
if mvPat == UNIF:
bfn += "u"
else:
bfn += "b" if (Amx > 0) else "n"
bfn += "n" if plastic else "s"
iInd = 0
while not bFnd:
iInd += 1
dd = os.getenv("__EnDeDataDir__")
fn = "%(dd)s/%(bfn)s%(iI)d.dat" % {"bfn" : bfn, "iI" : iInd, "dd" : dd}
fnocc="%(dd)s/%(bfn)s%(iI)docc.png" % {"bfn" : bfn, "iI" : iInd, "dd" : dd}
fnprm = "%(dd)s/%(bfn)s%(iI)d_prms.pkl" % {"bfn" : bfn, "iI" : iInd, "dd" : dd}
if not os.access(fn, os.F_OK): # file exists
bFnd = True
smk = " %.4f" * K
_U.savetxtWCom("%s" % fn, dat, fmt=("%.4f %d" + smk), delimiter=" ", com="# script=%s.py" % script)
_U.savetxtWCom("%s_NS.dat" % fn[:-4], datNghbr, fmt=("%.4f %d" + smk), delimiter=" ", com="# script=%s.py" % script)
pcklme = {}
pcklme["l0"] = l0[:, ::100]
pcklme["f"] = ctr[:, ::100]
pcklme["sq2"] = sx[:, ::100]
pcklme["u"] = mk_MU[:, ::100]
pcklme["covs"]= Covs
pcklme["intv"]= 100
pcklme["km"] = km
dmp = open(fnprm, "wb")
pickle.dump(pcklme, dmp, -1)
dmp.close()
print "created %s" % fn
fig = _plt.figure()
_plt.hist(dat[:, 0], bins=_N.linspace(Lx, Hx, 101), color="black")
_plt.savefig(fnocc)
_plt.close()