Skip to content

Commit 032344d

Browse files
committed
added masking | improved printing to terminal
1 parent 11c3b0f commit 032344d

File tree

1 file changed

+39
-28
lines changed

1 file changed

+39
-28
lines changed

code/python/cni_toolbox/PEA.py

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def set_direction(self, direction):
8888
self.direction = int(direction)
8989

9090

91-
def fitting(self, data):
91+
def fitting(self, data, mask = [], threshold = 100):
9292
'''
9393
Parameters
9494
----------
@@ -107,11 +107,21 @@ def fitting(self, data):
107107
F = np.exp(self.direction * 2j * np.pi * self.f_stim * (self.time-self.delay))
108108
X = zscore(np.array([np.real(F), np.imag(F)]).transpose())
109109

110-
data = zscore(np.reshape(
110+
data = np.reshape(
111111
data.astype(float),
112112
(self.n_samples,
113-
self.n_total)),
114-
axis = 0)
113+
self.n_total))
114+
115+
mean_signal = np.mean(data, axis = 0)
116+
117+
if np.size(mask)==0:
118+
mask = mean_signal >= threshold
119+
120+
mask = np.reshape(mask,self.n_total)
121+
voxel_index = np.where(mask)[0]
122+
data = zscore(data[:, mask], axis = 0)
123+
n_voxels = voxel_index.size
124+
115125
beta = regress(data, X)
116126
Y_ = np.matmul(X, beta)
117127
residuals = data - Y_
@@ -122,34 +132,35 @@ def fitting(self, data):
122132
'p_value': np.zeros(self.n_total)}
123133
df1 = 1.0
124134
df2 = self.n_samples-2.0
125-
for v in range(self.n_total):
126-
if (std_signal[v]>0):
135+
print('\nperforming analysis')
136+
for m in range(n_voxels):
137+
v = voxel_index[m]
127138

128139

129-
# estimate and correct for autocorrelation
130-
T = np.array([np.append(0,residuals[0:-1, v]),
131-
np.append(np.zeros(2), residuals[0:-2, v])]).transpose()
132-
W = np.append(1, -regress(residuals[:, v], T))
140+
# estimate and correct for autocorrelation
141+
T = np.array([np.append(0,residuals[0:-1, m]),
142+
np.append(np.zeros(2), residuals[0:-2, m])]).transpose()
143+
W = np.append(1, -regress(residuals[:, m], T))
133144

134-
X_corrected = correct_autocorr(X, W)
135-
D_corrected = correct_autocorr(data[:,v], W)
136-
#---------------------------------------------------------------------
145+
X_corrected = correct_autocorr(X, W)
146+
D_corrected = correct_autocorr(data[:,m], W)
147+
#---------------------------------------------------------------------
137148

138-
beta_corrected = regress(D_corrected, X_corrected)
139-
Y_ = np.matmul(X_corrected, beta_corrected)
140-
mu = np.mean(D_corrected, axis = 0);
141-
MSM = np.dot((Y_ - mu).transpose(), Y_ - mu) / df1
142-
MSE = np.dot((Y_ - D_corrected).transpose(), Y_ - D_corrected) / df2
143-
beta_complex = beta_corrected[0] + beta_corrected[1] * 1j
144-
results['phase'][v] = np.angle(beta_complex)
145-
results['amplitude'][v] = np.abs(beta_complex)
146-
results['f_stat'][v] = MSM / MSE;
147-
results['p_value'][v] = max(1-f.cdf(MSM / MSE, df1, df2), 1e-20)
149+
beta_corrected = regress(D_corrected, X_corrected)
150+
Y_ = np.matmul(X_corrected, beta_corrected)
151+
mu = np.mean(D_corrected, axis = 0);
152+
MSM = np.dot((Y_ - mu).transpose(), Y_ - mu) / df1
153+
MSE = np.dot((Y_ - D_corrected).transpose(), Y_ - D_corrected) / df2
154+
beta_complex = beta_corrected[0] + beta_corrected[1] * 1j
155+
results['phase'][v] = np.angle(beta_complex)
156+
results['amplitude'][v] = np.abs(beta_complex)
157+
results['f_stat'][v] = MSM / MSE;
158+
results['p_value'][v] = max(1-f.cdf(MSM / MSE, df1, df2), 1e-20)
148159

149160

150-
i = int(v / self.n_total * 21)
151-
sys.stdout.write('\r')
152-
sys.stdout.write("performing analysis [%-20s] %d%%"
161+
i = int(m / n_voxels * 21)
162+
sys.stdout.write('\r')
163+
sys.stdout.write("[%-20s] %d%%"
153164
% ('='*i, 5*i))
154165

155166
for key in results:
@@ -158,8 +169,8 @@ def fitting(self, data):
158169
(self.n_rows,
159170
self.n_cols,
160171
self.n_slices)))
161-
172+
162173
return results
163174

164175

165-
176+

0 commit comments

Comments
 (0)