-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkSlicePlots.py
137 lines (89 loc) · 6.44 KB
/
NetworkSlicePlots.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
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
import numpy as np
from Modules import BasicNetwork as BNet
import pickle
from matplotlib.gridspec import GridSpec
import matplotlib.pyplot as plt
import scipy.stats as sps
from Utils import Local, Constants, SciPlot
from Utils import PlotVariables as PV
from Utils.Constants import LocalDataConstants as LDC
########################################################## Define Constants #############################################################
confile_dir = Constants.LocalDataConstants.directories['n_confile_dir'] + "\\CommonEraData"
PlotSave_dir = Constants.LocalDataConstants.directories['plotSave_dir'] + "\\CommonEraPlots"
BlockLabel = ['Block 1', 'Block 2']
theta = PV.SliceNetworkPlots.Theta
phi = PV.SliceNetworkPlots.Phi
Bands = PV.SliceNetworkPlots.Bands
NOIs = PV.SliceNetworkPlots.NOIs
event_names = PV.SliceNetworkPlots.Events
kernel = PV.SliceNetworkPlots.Kernels
VisThresh = PV.SliceNetworkPlots.VisualThreshold
DataName = PV.SliceNetworkPlots.DataName
########################################################### Load Available Data ###########################################################
BehavioralData, Performance_data = 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])
############################################################## Win Arrays ###########################################################################
win_step = PV.SliceNetworkPlots.WindowSteps
for event_name in event_names:
for NOI in NOIs:
for Band in Bands:
DataDir = Local.DataDirExt({'MainDir': confile_dir, 'CurrEventName': event_name, 'Network': NOI, 'KernelName': kernel, 'BandName': Band, 'DataName': DataName, 'Specs': {'CommonEraData': True, 'SingleTrial': False}})
Data, VersionSpecs = Local.HandleDataLoad(DataDir, version_number = None)
StT = VersionSpecs['Start Time'] * 1000
EnT = VersionSpecs['End Time'] * 1000
WiLe = VersionSpecs['Window_Length'] / PV.SliceNetworkPlots.Fs * 1000
win_arrays = [[int(win), int(win + win_step)] for win in np.arange(StT + WiLe / 2, EnT - WiLe / 2, win_step)]
Data_Ctrl = [Data[str(SOI[1][sub_i[0]])] for sub_i in Sub_G[0]]
CtrlDataMean = (np.transpose(np.mean(np.array(Data_Ctrl), axis = 0), (0, 2, 3, 1)) - 0.5) * 5
CtrlData = np.transpose(np.array(Data_Ctrl), (0, 1, 3, 4, 2))
NodeNumbers = CtrlData.shape[2]
pairs = Local.PairGen(NodeNumbers)
PP = len(pairs)
NetB = []
for i in range(2):
NetB.append(BNet.NetworkGraph(CtrlDataMean[i]))
for NetBB in NetB:
coords = Constants.LocalDataConstants.DefaulValues['NodeLocations'][NOI]
NetBB.SetCoords(coords)
NetBB.SetLabels(Constants.LocalDataConstants.names['NetworkNames'][NOI])
for win_array in win_arrays:
STP, FTP = Local.TimeToSample(win_array, specs = {'ZeroPoint': 12, 'Fs': 125})
fig = plt.figure(figsize = (40, 30), dpi = 500)
gs = GridSpec(PP, 5, figure = fig)
axs = []
axs.append(fig.add_subplot(gs[:int(PP / 2), 1:4]))
axs.append(fig.add_subplot(gs[int(PP / 2):, 1:4]))
for Pairment in range(PP):
axs.append(fig.add_subplot(gs[Pairment, 0]))
axs.append(fig.add_subplot(gs[Pairment, 4]))
hist_axs = np.arange(2, 2 * PP + 2, 2)
trnd_axs = np.arange(3, 2 * PP + 2, 2)
for Bi, NetBB in enumerate(NetB):
NetBB.DrawStaticGraph(VisThresh = VisThresh, window = win_array, xlims = LDC.DefaulValues['xAxisLimits'][NOI], ylims = LDC.DefaulValues['yAxisLimits'][NOI], show_labels = True, ax = axs[Bi], DirectionBias = 0)
axs[Bi].set_title(BlockLabel[Bi])
for i in range(len(hist_axs)):
axs[hist_axs[i]].hist(np.mean(CtrlData[:, Bi, :, :, STP : FTP], axis = 3)[:, pairs[i][0], pairs[i][1]], alpha = 0.5, label = BlockLabel[Bi])
axs[hist_axs[i]].set_xlim([0, 1])
axs[hist_axs[i]].set_title(Constants.LocalDataConstants.names[DataName + 'ClusterNames'][Constants.LocalDataConstants.NetworksOfInterest[DataName][NOI][pairs[i][0]]] + ' - ' + Constants.LocalDataConstants.names[DataName + 'ClusterNames'][Constants.LocalDataConstants.NetworksOfInterest[DataName][NOI][pairs[i][1]]], fontsize = 10)
axs[hist_axs[0]].legend()
for i in range(len(trnd_axs)):
Data2Trnd = np.mean(CtrlData[:, :, pairs[i][0], pairs[i][1], STP : FTP], axis = -1)
pVal = sps.ttest_ind(Data2Trnd[:, 0], Data2Trnd[:, 1]).pvalue
y_U, y_L = SciPlot.ConfidenceBoundsGen(Data2Trnd)
axs[trnd_axs[i]].plot(np.mean(Data2Trnd, axis = 0))
axs[trnd_axs[i]].fill_between(np.arange(2), y_U, y_L, alpha = 0.2)
axs[trnd_axs[i]].set_title(Constants.LocalDataConstants.names[DataName + 'ClusterNames'][Constants.LocalDataConstants.NetworksOfInterest[DataName][NOI][pairs[i][0]]] + ' - ' + Constants.LocalDataConstants.names[DataName + 'ClusterNames'][Constants.LocalDataConstants.NetworksOfInterest[DataName][NOI][pairs[i][1]]] + ' (p-Value = ' + str(int(10000 * pVal / 2) / 10000) + ' )', fontsize = 10)
axs[trnd_axs[i]].set_xlim([0, 1])
fig.suptitle(NOI + ' Network During Learning in Control Subjects in Band ' + Band + ' (dPLI) Locked on ' + event_name + ' Onset\nTime windows ' + str(win_array[0]) + 'ms to ' + str(win_array[1]) + 'ms', fontsize = 15)
# plt.show()
SavePlotsLoc = Local.HandleDir(PlotSave_dir + '\\' + DataName + "\\AveragedTrialBased\\" + event_name + '\\NetworkAnalyses\\' + NOI + '\\' + kernel + '\\' + Band)
fig.savefig(SavePlotsLoc + '\\' + 'ControlGroup_LockedOn_' + event_name + '_' + str(win_array[0]) + 'msTo' + str(win_array[1]) + 'ms' + '.jpg')
plt.close()
print('Figure Saved')