forked from issalab/Mapping
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataMapModel.py
More file actions
166 lines (131 loc) · 6.26 KB
/
DataMapModel.py
File metadata and controls
166 lines (131 loc) · 6.26 KB
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
resultdir = '/home/tahereh/Documents/Research/Results/Mapping_unit_test/'
neuralfeaturesdir = '/home/tahereh/Documents/Research/features/neural_features/'
datadir = '/home/tahereh/Documents/Research/Data/DiCarlo/'
import numpy as np
import h5py
import importlib
import MappingV36
importlib.reload(MappingV36)
from MappingV36 import MappingV36 as Mapping
Mapping = Mapping()
from ReadMeta import ReadMeta
from ReadData import ReadData
class DataMapModel:
def __init__(self, ni, nf, nt):
self.ni = ni
self.nf = nf
self.nt = nt
def get_syntheic(self, sds, splitfract, Collinearity, noise_dist, stats_from_data):
D = np.zeros((self.ni, self.nf, self.nt))
if stats_from_data:
# import HvM mean and stds
hf = h5py.File(resultdir + 'HvM_stats.h5', 'r')
mu_HvM = np.array(hf.get('mu')).T
sds_HvM = np.array(hf.get('sd')).T
hf.close()
#
mu = mu_HvM[:self.ni, :self.nf]
else:
mu = np.random.rand(self.ni, self.nf)
Dtruth = mu[:self.ni, :self.nf] # np.random.rand(ni, nf) # model M: nf feat x ni images
if Collinearity:
collinearity_r = np.random.uniform(low=0.8, high=1, size=self.nf - 1)
# collinearity_r = np.random.uniform(low=0., high=0.1, size=nf-1)
for ir, r in enumerate(collinearity_r):
Dtruth[ir + 1] = Dtruth[0] * r + Dtruth[ir + 1] * np.sqrt(1 - r ** 2)
# print(r, ss.pearsonr(M[0], M[ir+1])[0], end="")
for tr in range(self.nt):
D[:, :, tr] = Dtruth
noise1 = np.zeros((self.ni, self.nf, int(self.nt * splitfract)))
noise2 = np.zeros((self.ni, self.nf, int(self.nt * splitfract)))
for i in range(self.ni):
if noise_dist == 'normal':
n = np.random.rand()
n1 = np.array([np.random.normal(0, sdf, size=int(self.nt * splitfract)) for sdf in sds])
n2 = np.array([np.random.normal(0, sdf, size=int(self.nt * splitfract)) for sdf in sds])
noise1[i] = n1 # (n1 - n1.min()) / (n1.max() - n1.min())
noise2[i] = n2 # (n2 - n2.min()) / (n2.max() - n2.min())
elif noise_dist == 'poisson':
n = np.random.rand()
n1 = np.array([np.random.poisson(sdf, size=int(self.nt * splitfract)) for sdf in mu[i]])
n2 = np.array([np.random.poisson(sdf, size=int(self.nt * splitfract)) for sdf in mu[i]])
noise1[i] = n1 # (n1-n1.min())/(n1.max()-n1.min())
noise2[i] = n2 # (n2-n2.min())/(n2.max()-n2.min())
elif noise_dist == 'HvM_normal':
n1 = np.array([np.random.normal(0, sdf, size=int(self.nt * splitfract)) for sdf in sds_HvM[i]])
n2 = np.array([np.random.normal(0, sdf, size=int(self.nt * splitfract)) for sdf in sds_HvM[i]])
noise1[i] = n1 # (n1-n1.min())/(n1.max()-n1.min())
noise2[i] = n2 # (n2-n2.min())/(n2.max()-n2.min())
elif noise_dist == 'HvM_poisson':
n1 = np.array([np.random.poisson(abs(sdf), size=int(self.nt * splitfract)) for sdf in mu[i]])
n2 = np.array([np.random.poisson(abs(sdf), size=int(self.nt * splitfract)) for sdf in mu[i]])
noise1[i] = n1 # (n1-n1.min())/(n1.max()-n1.min())
noise2[i] = n2 # (n2-n2.min())/(n2.max()-n2.min())
D[:, :, :int(self.nt * splitfract)] = D[:, :, :int(self.nt * splitfract)] + noise1
D[:, :, int(self.nt * splitfract):] = D[:, :, int(self.nt * splitfract):] + noise2
return D, Dtruth
def get_HvM(self, ):
# Read Meta
Meta = ReadMeta(neuralfeaturesdir)
DF_img = Meta.get_DF_img()
DF_neu = Meta.get_DF_neu()
times = Meta.get_times()
# Read Neural data
Data = ReadData(datadir, DF_neu)
IT, V4 = Data.get_data()
D = Mapping.get_Neu_trial_V36(IT[1:], [70, 170], times)
image_indices = np.random.randint(low=0, high=D.shape[1], size=ni)
D = D[:, image_indices, :]
D = np.swapaxes(D, 0, 1)
nf = D.shape[1]
nt = D.shape[2]
mu = np.zeros((self.nf, self.ni))
sd = np.zeros((self.nf, self.ni))
for f in range(self.nf):
for i in range(self.ni):
mu[f, i] = D[i, f, :].mean()
sd[f, i] = D[i, f, :].std()
hf = h5py.File(resultdir + 'HvM_stats.h5', 'w')
hf.create_dataset('mu', data=mu)
hf.create_dataset('sd', data=sd)
hf.close()
# #test synthetic as HvM
# nf = 168
# nt = 46
# noise_dist = 'poisson'
# sds = np.logspace(-1, 1, num=int(nf))
# D = np.zeros((ni, nf, nt))
# D_mean = np.random.rand(ni, nf)
# for tr in range(nt):
# D[:, :, tr] = D_mean
#
# noise1 = np.zeros((ni, nf, int(nt * splitfract)))
# noise2 = np.zeros((ni, nf, int(nt * splitfract)))
# for i in range(ni):
# if noise_dist == 'normal':
# n = np.random.rand()
# noise1[i] = np.array([np.random.normal(0, sd + n, size=int(nt * splitfract)) for sd in sds])
# noise2[i] = np.array([np.random.normal(0, sd + n, size=int(nt * splitfract)) for sd in sds])
# elif noise_dist == 'poisson':
# n = np.random.rand()
# noise1[i] = np.array([np.random.poisson(sd + n, size=int(nt * splitfract)) for sd in sds])
# noise2[i] = np.array([np.random.poisson(sd + n, size=int(nt * splitfract)) for sd in sds])
#
# D[:, :, :int(nt * splitfract)] = D[:, :, :int(nt * splitfract)] + noise1
# D[:, :, int(nt * splitfract):] = D[:, :, int(nt * splitfract):] + noise2
# to test HvM as syntheic
# hf = h5py.File(resultdir+'D.h5', 'w')
# hf.create_dataset('D', data=D)
# hf.close()
sds = []
Collinearity = 'HvM'
noise_dist = 'HvM'
return D
def get_model(self, D, A, PCA_ncomponents):
M = np.matmul(D, A) # D_mean
if PCA_ncomponents:
from sklearn.decomposition import PCA
pca = PCA(n_components=PCA_ncomponents)
pca.fit(M)
M = pca.transform(M)
return M