forked from AraiKensuke/LOST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createOscARMT.py
133 lines (117 loc) · 5.34 KB
/
createOscARMT.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
import scipy.optimize as _sco
from shutil import copyfile
from utildirs import setFN
from mcmcARpPlot import plotWFandSpks
import matplotlib.pyplot as _plt
from kflib import createDataPPl2, savesetMT, savesetMTnosc, createDataAR, createFlucOsc
from LOSTdirs import resFN, datFN
import numpy as _N
import pickle as _pk
import warnings
import numpy.polynomial.polynomial as _Npp
import utilities as _U
from kstat import percentile
TR = None; N = None; dt = 0.001
trim = 50;
nzs = None; nRhythms = None;
rs = None; ths = None; alfa = None;
lambda2 = None; psth = None
lowQpc = 0; lowQs = None
isis = None; rpsth = None
us = None
csTR = None; # coupling strength trend
etme = None;
absrefr = 0
bGenOscUsingAR = True; bGenOscUsingSines = False;
# These params if osc. not generated by AR but by Sines
f0VAR = None; f0 = None; Bf = None; Ba = None; amp = 1; amp_nz = 0;
dSA = 5; dSF = 5;
def create(setname):
# _plt.ioff()
copyfile("%s.py" % setname, "%(s)s/%(s)s.py" % {"s" : setname, "to" : setFN("%s.py" % setname, dir=setname, create=True)})
global dt, lambda2, rpsth, isis, us, csTR, etme, bGenOscUsingAR, f0VAR, f0, Bf, Ba, amp, amp_nz, dSA, dSF, psth
if bGenOscUsingAR:
ARcoeff = _N.empty((nRhythms, 2))
for n in xrange(nRhythms):
ARcoeff[n] = (-1*_Npp.polyfromroots(alfa[n])[::-1][1:]).real
stNzs = _N.empty((TR, nRhythms))
for tr in xrange(TR):
if _N.random.rand() < lowQpc:
lowQs.append(tr)
stNzs[tr] = nzs[:, 0]
else:
stNzs[tr] = nzs[:, 1] # high
elif bGenOscUsingSines:
if f0VAR is None:
f0VAR = _N.zeros(TR)
sig = 0.1
x, y = createDataAR(100000, Bf, sig, sig)
stdf = _N.std(x) # choice of 4 std devs to keep phase monotonically increasing
x, y = createDataAR(100000, Ba, sig, sig)
stda = _N.std(x) # choice of 4 std devs to keep phase monotonically increasing
# x, prbs, spks 3 columns
nColumns = 3
alldat = _N.empty((N, TR*nColumns))
probNOsc = _N.empty((N, TR))
spksPT = _N.empty(TR)
lowQs = []
isis = []
rpsth = []
if csTR is None:
csTR = _N.ones(TR)
if etme is None:
etme = _N.ones((TR, N))
if us is None:
us = _N.zeros(TR)
elif (type(us) is float) or (type(us) is int):
us = _N.zeros(TR) * us
for tr in xrange(TR):
if bGenOscUsingAR:
#x, dN, prbs, fs, prbsNOsc = createDataPPl2(TR, N, dt, ARcoeff, psth + us[tr], stNzs[tr], lambda2=lambda2, p=1, nRhythms=nRhythms, cs=csTR[tr], etme=etme[tr])
# psth is None. Turn it off for now
x, dN, prbs, fs, prbsNOsc = createDataPPl2(TR, N, dt, ARcoeff, us[tr], stNzs[tr], lambda2=lambda2, p=1, nRhythms=nRhythms, cs=csTR[tr], etme=etme[tr], offset=psth)
else:
xosc = createFlucOsc(f0, _N.array([f0VAR[tr]]), N, dt, 1, Bf=Bf, Ba=Ba, amp=amp, amp_nz=amp_nz, stdf=stdf, stda=stda, sig=sig, smoothKer=5, dSA=dSA, dSF=dSF) * etme[tr] # sig is arbitrary, but we need to keep it same as when stdf, stda measured
#x, dN, prbs, fs, prbsNOsc = createDataPPl2(TR, N, dt, None, psth + us[tr], None, lambda2=lambda2, p=1, nRhythms=1, cs=csTR[tr], etme=etme[tr], x=xosc[0])
x, dN, prbs, fs, prbsNOsc = createDataPPl2(TR, N, dt, None, us[tr], None, lambda2=lambda2, p=1, nRhythms=1, cs=csTR[tr], etme=etme[tr], x=xosc, offset=psth)
spksPT[tr] = _N.sum(dN)
rpsth.extend(_N.where(dN == 1)[0])
alldat[:, nColumns*tr] = _N.sum(x, axis=0).T*etme[tr]*csTR[tr]
alldat[:, nColumns*tr+1] = prbs
alldat[:, nColumns*tr+2] = dN
probNOsc[:, tr] = prbsNOsc
isis.extend(_U.toISI([_N.where(dN == 1)[0].tolist()])[0])
savesetMT(TR, alldat, model, setname)
savesetMTnosc(TR, probNOsc, setname)
arfs = ""
xlst = []
if bGenOscUsingAR:
for nr in xrange(nRhythms):
arfs += "%.1fHz " % (500*ths[nr]/_N.pi)
xlst.append(x[nr])
else:
xlst.append(x[0])
sTitle = "AR2 freq %(fs)s spk Hz %(spkf).1fHz TR=%(tr)d N=%(N)d" % {"spkf" : (_N.sum(spksPT) / (N*TR*0.001)), "tr" : TR, "N" : N, "fs" : arfs}
plotWFandSpks(N-1, dN, xlst, sTitle=sTitle, sFilename=resFN("generative", dir=setname))
fig = _plt.figure(figsize=(8, 4))
_plt.hist(isis, bins=range(100), color="black")
_plt.grid()
_plt.savefig(resFN("ISIhist", dir=setname))
_plt.close()
fig = _plt.figure(figsize=(13, 4))
_plt.plot(spksPT, marker=".", color="black", ms=8)
_plt.ylim(0, max(spksPT)*1.1)
_plt.grid()
_plt.suptitle("avg. Hz %.1f" % (_N.mean(spksPT) / (N*0.001)))
_plt.savefig(resFN("spksPT", dir=setname))
_plt.close()
if (lambda2 is None) and (absrefr > 0):
lambda2 = _N.array([0.0001] * absrefr)
if lambda2 is not None:
_N.savetxt(resFN("lambda2.dat", dir=setname), lambda2, fmt="%.7f")
# if we want to double bin size
#lambda2db = 0.5*(lambda2[1::2] + lambda2[::2])
#_N.savetxt(resFN("lambda2db.dat", dir=setname), lambda2db, fmt="%.7f")
#_plt.ion()
if lowQpc > 0:
_N.savetxt(resFN("lowQtrials", dir=setname), lowQs, fmt="%d")