-
Notifications
You must be signed in to change notification settings - Fork 0
/
_simdata.py
102 lines (72 loc) · 2.48 KB
/
_simdata.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
from scipy.stats import multivariate_normal
import numpy as np
from _util import calc_K
def _gen_rand_Cd(n_neurons,nDims):
"""
Generate random matrices of baseline firing rates, d, and
loadings from latent states, C
"""
C = np.random.normal(loc=0,scale=.5,size=(n_neurons,nDims))/2
d = np.random.randint(0,4,size=(n_neurons))/2
return C,d
def _gen_sample_traj(t,l,pretty,n_timePoints):
"""
Generate sample trajectory from GP
Arguments:
_______________________________________
t: array
array of size (n_timePoints,) with linear spacing
l: float
length scale of the GP
pretty: bool
if true, trajectories have on and off ramps, looks kinda more pretty
"""
Ki = calc_K(x=t,y=t,l=l)
Ki /= np.max(Ki)
mvn1 = multivariate_normal(mean=[np.random.randint(1,5)-3]*n_timePoints,cov=Ki)
if pretty:
traj = mvn1.rvs()*np.concatenate([np.zeros(5),
np.cos(np.linspace(-np.pi/2,0,num=15)),
np.ones(n_timePoints-35),
np.cos(np.linspace(0,.5*np.pi,num=10)),
np.zeros(5)])
else:
traj = mvn1.rvs()
return traj
def genSim_data_static(n_neurons=80,nDims=3,n_timePoints=67,pretty=False,nTrials=5):
"""
Generate simulated data for testing the algorithm
Arguments:
_______________________________________
n_neurons: int
number of neurons to simulate
nDims: int
number of latent dimensions
n_timePoints: int
number of timepoints comrpising a trial
"""
t = np.linspace(-33,33,num=n_timePoints)/10
C,d =_gen_rand_Cd(n_neurons,nDims)
length_scales_GP = [10**(1 if i== 0 else -i*.2) for i in range(nDims)]
latent_trajs = [];
CIFs = []
ys = []
for trl_idx in range(nTrials):
x = np.zeros([nDims,n_timePoints]) #the latent trajectories
for i in range(nDims):
x[i] = _gen_sample_traj(t,l=length_scales_GP[i],pretty=pretty,n_timePoints=n_timePoints) #latent states
cifs = np.exp(C.dot(x) + d[:,None])
y = np.random.poisson(cifs +
np.abs(np.random.normal(loc=0,scale=1,size=cifs.shape))) #spike trains
CIFs.append(cifs)
ys.append(y)
latent_trajs.append(x)
ground_truth_dict = {'C':C,
'd': d,
'l':length_scales_GP,
'latent_traj': latent_trajs,
'CIFs': CIFs,
't': t,
'nTrials':nTrials
}
return ys, ground_truth_dict