-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkBasedConnCalc.py
More file actions
186 lines (111 loc) · 6.21 KB
/
NetworkBasedConnCalc.py
File metadata and controls
186 lines (111 loc) · 6.21 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import numpy as np
import pickle
import scipy.stats as sps
import Modules.GRConnPy as GRC
import Modules.GRUniPy as GRU
from Utils import Local
from Utils import Constants
from Utils import SciPlot as SP
from tqdm import tqdm
from Utils.InputVariables import CalculationVars as CV
from Utils.InputVariables import CommonVars as CoV
########################################################### Define Parameters ###########################################################
DataName = CV.NetBaseConnCalc['DataName']
NodeNames = Constants.LocalDataConstants.names[DataName + 'ClusterNames']
group_labels = Constants.LocalDataConstants.Labels['groups']
data_labels = Constants.LocalDataConstants.Labels['data_block']
ConKers = CV.NetBaseConnCalc['ConKers']
overlap_ratio = CV.NetBaseConnCalc['OLR']
win_length = CV.NetBaseConnCalc['WinLen']
NOIs = CV.NetBaseConnCalc['NOIs']
Bands = CV.NetBaseConnCalc['Bands']
CommonEraData = CV.NetBaseConnCalc['CommonEraData']
FilterInKernel = CV.NetBaseConnCalc['FilterInKernel']
OutSource = CV.NetBaseConnCalc['OutSource']
st = CV.NetBaseConnCalc['st']
ft = CV.NetBaseConnCalc['ft']
Fs = CoV.SamplingFrequency
data_name = CV.NetBaseConnCalc['DataName']
confile_dir = Constants.LocalDataConstants.directories['n_confile_dir']
if CommonEraData:
confile_dir = confile_dir + "\\CommonEraData"
NB = CV.NetBaseConnCalc['NB']
TB = CV.NetBaseConnCalc['TB']
########################################################### Load Available Data ###########################################################
BehavioralData, _ = Local.ExperimentDataLoader()
SOI = Local.AvailableSubjects()
################################################## Divide Subject into DEP and CTRL Groups ###########################################################
Sub_G = [[], []] # first element is CTRL Group Members and the Second one the DEP Group
for i, sub_i in enumerate(SOI[0]):
if BehavioralData['BDI'][sub_i] < 10:
Sub_G[0].append([i, sub_i])
else:
Sub_G[1].append([i, sub_i])
########################################################## Generate Connectivity Data ###########################################################
event_numbers = CV.NetBaseConnCalc['Events']
specs = {
'orders_matrix': CV.NetBaseConnCalc['OrderMat'],
'overlap_ratio': overlap_ratio,
'window_length': win_length,
'start time': st,
'end time': ft,
'DecompKern': CV.NetBaseConnCalc['DecompKern'],
'CorrCalcFunct': CV.NetBaseConnCalc['CorrCalcFunct'],
'AmpBand': CV.NetBaseConnCalc['AmpBand'],
'PhaseBand': CV.NetBaseConnCalc['PhaseBand'],
'Spectral_Res': CV.NetBaseConnCalc['Spectral_Res'],
'DyCommogram': CV.NetBaseConnCalc['DyCommogram'],
'SingleTrial': CV.NetBaseConnCalc['SingleTrial'],
}
for event in event_numbers:
event_name = Constants.LocalDataConstants.names['events'][event]
if event_name == 'Actions' or event_name == 'TestActions':
sp = int((st + 1) * Fs)
fp = int((ft + 1) * Fs)
elif event_name == 'TestStim':
sp = int((st + 0.6) * Fs)
fp = int((ft + 0.6) * Fs)
else:
sp = int((st + 0.4) * Fs)
fp = int((ft + 0.4) * Fs)
raw_data, data_lengths = Local.ClusteredEEGLoader(event = event_name, data_name = data_name)
print("The Event is " + event_name)
for NOI in NOIs:
for kernel in ConKers:
specs['Kernel'] = kernel
for Band_i, Band in enumerate(Bands):
if Local.BandAvailable(kernel, Band):
# SaveFileDir = Local.HandleDir(confile_dir + '\\' + data_name + "\\" + event_name + '\\' + NOI + '\\' + kernel + '\\' + Band)
SaveFileDir = Local.SaveDirGen({'MainDir': confile_dir, 'CurrEventName': event_name, 'Network': NOI, 'KernelName': kernel, 'BandName': Band, 'DataName': data_name, 'Specs': specs})
tConDataDict = {}
for i, sub_i in tqdm(enumerate(SOI[0])):
print("subject " + str(i))
dl = int(data_lengths[i])
data = sps.zscore(raw_data[i][:, :, sp : fp], axis = -1)
if NB == 1:
Samples = SP.RandomBlockSampling(dl, NumBlock = NB, NumSample_inBlock = TB)
else:
Samples = SP.DeterminedBlockSampling(dl, NumBlock = NB, NumSample_inBlock = TB)
divData = data[Samples]
if CV.NetBaseConnCalc['SingleTrial']:
BDC_Data = []
sub_Data = []
for dataBlock in divData:
for SingleTrialData in dataBlock:
tmpDC_Data = GRC.DynamicConnectivityMeasure(SingleTrialData, kernel = kernel, Band = Band, OutSource = OutSource, inc_channels = Constants.LocalDataConstants.NetworksOfInterest[DataName][NOI], **specs)
BDC_Data.append(tmpDC_Data)
sub_Data.append(np.mean(np.array(BDC_Data), axis = 0))
else:
divData = np.mean(divData, axis = 1)
if Band != 'All' and not FilterInKernel:
divData = np.squeeze(np.array([GRU.FrequencyBandExt(divData[i], Band = Band) for i in range(divData.shape[0])]))
Band_ = 'All'
else:
Band_ = Band
sub_Data = GRC.DynamicConnectivityMeasure(divData, kernel = kernel, Band = Band_, OutSource = OutSource, inc_channels = Constants.LocalDataConstants.NetworksOfInterest[DataName][NOI], **specs)
tConDataDict[str(SOI[1][i])] = np.array(sub_Data)
SaveFileName, version_number = Local.HandleFileName(SaveFileDir, specs)
with open(SaveFileDir + "\\" + SaveFileName, 'wb') as f:
pickle.dump(tConDataDict, f, protocol=pickle.HIGHEST_PROTOCOL)
print("Version " + str(version_number) + " of File Saved")
SaveFileDir = Local.HandleDir(confile_dir + '\\' + event_name)