Skip to content

Commit bfc70e8

Browse files
authored
Merge pull request #33 from mibur1/main
Sliding window changes
2 parents e7d06f1 + 6ba60e6 commit bfc70e8

3 files changed

Lines changed: 24 additions & 29 deletions

File tree

pydfc/dfc_methods/discrete_hmm.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def __init__(self, **params):
6565
"backend",
6666
"n_subj_clstrs",
6767
"W",
68+
"window_std",
6869
"n_overlap",
6970
"n_states",
7071
"normalization",

pydfc/dfc_methods/sliding_window.py

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def __init__(self, **params):
4545
"is_state_based",
4646
"sw_method",
4747
"tapered_window",
48+
"window_std",
4849
"W",
4950
"n_overlap",
5051
"normalization",
@@ -95,7 +96,7 @@ def calc_MI(self, X, Y):
9596
return MI
9697

9798
def FC(self, time_series):
98-
99+
# Graphical Lasso
99100
if self.params["sw_method"] == "GraphLasso":
100101
# Standardize the data (zero mean, unit variance for each feature)
101102
mean = np.mean(time_series, axis=1, keepdims=True)
@@ -105,29 +106,28 @@ def FC(self, time_series):
105106
model.fit(time_series_standardized.T)
106107
# the covariance matrix will equal the correlation matrix
107108
C = model.covariance_
108-
else:
109+
110+
# Mutual information
111+
elif self.params["sw_method"] == "MI":
109112
C = np.zeros((time_series.shape[0], time_series.shape[0]))
113+
110114
for i in range(time_series.shape[0]):
111115
for j in range(i, time_series.shape[0]):
112-
113116
X = time_series[i, :]
114117
Y = time_series[j, :]
118+
C[j, i] = self.calc_MI(X, Y)
115119

116-
if self.params["sw_method"] == "MI":
117-
########### Mutual Information ##############
118-
C[j, i] = self.calc_MI(X, Y)
119-
else:
120-
########### Pearson Correlation ##############
121-
if np.var(X) == 0 or np.var(Y) == 0:
122-
C[j, i] = 0
123-
else:
124-
C[j, i] = np.corrcoef(X, Y)[0, 1]
125-
126-
C[i, j] = C[j, i]
127-
120+
# Pearson correlation
121+
else:
122+
C = np.corrcoef(time_series)
123+
C[np.isnan(C)] = 0
124+
# make the diagonal elements 1 (for nan values on the diagonal)
125+
C[np.diag_indices_from(C)] = 1
128126
return C
129127

130-
def dFC(self, time_series, W=None, n_overlap=None, tapered_window=False):
128+
def dFC(
129+
self, time_series, W=None, n_overlap=None, tapered_window=False, window_std=None
130+
):
131131
# W is in time samples
132132

133133
L = time_series.shape[1]
@@ -141,34 +141,26 @@ def dFC(self, time_series, W=None, n_overlap=None, tapered_window=False):
141141
model.fit(time_series.T)
142142
self.graphical_lasso_alpha_ = model.alpha_
143143

144-
window_taper = signal.windows.gaussian(W, std=3 * W / 22)
145-
# C = DFC(measure=self)
146144
FCSs = list()
147145
TR_array = list()
148146
for l in range(0, L - W + 1, step):
149147

150-
######### creating a rectangel window ############
148+
# Create rectangular window
151149
window = np.zeros((L))
152150
window[l : l + W] = 1
153151

154-
########### tapering the window ##############
152+
# Taper the window
155153
if tapered_window:
154+
std = window_std if window_std is not None else 3 * W / 22
155+
window_taper = signal.windows.gaussian(W, std=std)
156156
window = signal.convolve(window, window_taper, mode="same") / sum(
157157
window_taper
158158
)
159159

160160
window = np.repeat(
161161
np.expand_dims(window, axis=0), time_series.shape[0], axis=0
162162
)
163-
164-
# int(l-W/2):int(l+3*W/2) is the nonzero interval after tapering
165-
FCSs.append(
166-
self.FC(
167-
np.multiply(time_series, window)[
168-
:, max(int(l - W / 2), 0) : min(int(l + 3 * W / 2), L)
169-
]
170-
)
171-
)
163+
FCSs.append(self.FC(np.multiply(time_series, window)[:, l : l + W]))
172164
TR_array.append(int((l + (l + W)) / 2))
173165

174166
return np.array(FCSs), np.array(TR_array)
@@ -200,6 +192,7 @@ def estimate_dFC(self, time_series):
200192
W=int(self.params["W"] * time_series.Fs),
201193
n_overlap=self.params["n_overlap"],
202194
tapered_window=self.params["tapered_window"],
195+
window_std=self.params["window_std"],
203196
)
204197

205198
# record time

pydfc/dfc_methods/sliding_window_clustr.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def __init__(self, clstr_distance="euclidean", **params):
6767
"coi_correction",
6868
"n_subj_clstrs",
6969
"W",
70+
"window_std",
7071
"n_overlap",
7172
"n_states",
7273
"normalization",

0 commit comments

Comments
 (0)