-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp1_calcVolunteers.py
266 lines (212 loc) · 9.04 KB
/
p1_calcVolunteers.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
"""Calculates BOLD dynamics metric space, per volunteer.
Argument list:
apath: Reads list of volunteers from here
(default: '/keilholz-lab/SharedFiles/SomeBrainMaps/HCPSaves_Compact/').
poolsize: Size of cpu pool (default: 14).
nVols: Number of volunteer datasets to run (default: -1 "all").
display: plot and display some intermediate results (default: False).
"""
from os.path import join as OSjoin
from os.path import isfile as OSisfile
from os import makedirs as makedirs
from multiprocessing import Pool, cpu_count, Process
import argparse
import random
import pickle
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import scipy.sparse as sp
from scipy.spatial.distance import squareform
from scipy.signal import butter, sosfiltfilt
from pycwt.helpers import fft, fft_kwargs, rect
from tqdm import tqdm
import datetime as dt
import itertools
from functools import partial
import p_utils
import plotters
maxdim = p_utils.maxdim
truncDim = p_utils.truncDim
print([truncDim,maxdim])
TR = p_utils.TR
TARGS = p_utils.TARGS
TARGS_CEIL = p_utils.TARGS_CEIL
frq_edges = p_utils.frq_edges
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--apath', type=str, default = '/keilholz-lab/Jacob/TDABrains_00/data/FCStateClassif/anat/')
parser.add_argument('--poolSize', type=int, default=14)
parser.add_argument('--nVols', type=int, default=-1)
parser.add_argument('--display', type=bool, default=False)
args = parser.parse_args()
apath_rest = args.apath
apath_task = args.apath
vloc = args.apath
volunteers, restDirs, taskDirs, EVtxt, Blocks, Blockstxt = p_utils.getLists(vloc=vloc)
random.shuffle(volunteers)
if args.nVols == -1:
nVols = len(volunteers)
else:
nVols = args.nVols
print('nVols is {}'.format(nVols))
# Choose a spatial parcellation
knn = p_utils.knn
lensPath = '/keilholz-lab/Jacob/TDABrains_00/data/groupICA_3T_HCP1200_MSMAll/groupICA/groupICA_3T_HCP1200_MSMAll_d{}.ica'.format(knn)
lensName = 'melodic_IC.dscalar.nii'
nICA = knn
knn_dist_len = int(knn*(knn-1)/2)
print(knn)
buffLen = p_utils.buffLen
mixLen = p_utils.mixLen
trimLen = p_utils.trimLen
# saveloc
saveloc = './results/'
# Loop through volunteers. Does pipeline.
t0 = dt.datetime.now()
def doStack(voln):
"""Perform calculations of inter-volunteer dynamics"""
curloc = saveloc + '/train0table/'
makedirs(curloc ,exist_ok=True)
curfile = (curloc + str(voln) + '.npz')
np.savez(curfile,'')
adata = np.load(vloc + voln ).T
adata = np.delete(adata, p_utils.missingVoxels, axis=0)
nvox, nT = nP, nT = np.shape(adata)
print(nP,nT)
print('Data single time histogram')
print(np.histogram(adata[:,10].ravel()))
'''
testData = np.sum(adata,axis=1)
if sum(testData==0)>0:
print('skipping volunteer {} with {} zero valued parcels'.format(voln, sum(testData==0)))
return
'''
sj, frequencies, coi, mother, s0, dj, J, s1, TR = p_utils.getCWT_auxInfoPlus(adata[0,:])
pool = Pool(args.poolSize)
'''
W = pool.map(p_utils.getCWT_coeff,tqdm(adata))
W = np.array(W)
#W = W[:10,:]
print('shape W is ' + str(W.shape))
print('W histogram')
print(np.histogram(W.ravel()))
_, m, n = nP, nW, nT = W.shape
k = 2 * np.pi * fft.fftfreq(fft_kwargs(W[0, 0, :])['n'])
k2 = k ** 2
scales1 = np.ones([1, n]) * sj[:, None]
snorm = sj / TR
F = np.exp(-0.5 * (snorm[:, np.newaxis] ** 2) * k2) # Outer product
wsize = mother.deltaj0 / dj * 2
win = rect(np.int(np.round(wsize)), normalize=True)
print('Smooth wavelets')
mapFun = partial(p_utils.cwt2smooth_local, n=n, F=F, win=win)
S = pool.map(mapFun,tqdm( list(np.abs(W[ic,:,:].squeeze())**2/scales1 for ic in range(nP)) ))
print('One smoothed values histogram')
print(np.histogram(S[0].ravel()))
samps = np.arange(nP)
mapCross = partial(p_utils.cross)
W12 = list(pool.imap(mapCross,tqdm(list([W[sma,:,:].squeeze(),W[smb,:,:].squeeze()] for sma, smb in itertools.combinations(samps,2)), desc='Cross wavelets')))
mapCoher = partial(p_utils.coher_postCross,n=n,F=F,win=win,scales1=scales1)
wct = list(pool.imap(mapCoher,tqdm(list([W12[i],S[sma],S[smb]] for i,[sma,smb] in enumerate(itertools.combinations(samps,2))), desc='Wavelet coherence' )))
mapNorms = partial(p_utils.crossNorms, frq_edges=frq_edges)
nW12 = list(pool.imap(mapNorms,tqdm(W12)))
print('one W12 norms histogram, having shape {}'.format(nW12[0].shape))
print(np.histogram(nW12[0]))
pool.close()
pool.join()
pool.terminate
# Trim input data
temp = np.array(wct)
temp = temp[:,:,trimLen:-trimLen]
tnW12 = np.array(nW12)[:,:,trimLen:-trimLen]
aa, bb, cc = temp.shape
wct = np.zeros((aa, 1, cc))
[uu0, uu1] = frq_edges
wct[:,0,:] = np.sum(np.multiply(temp[:,uu0:uu1,:], tnW12[:,uu0:uu1,:]),axis=1)
_, m, n = _, nW, nT = wct.shape
print('wct is shape {}.'.format(wct.shape))
print('One wct histogram')
print(np.histogram(wct[0].ravel()))
#Select time points, random
#inds = p_utils.doAgg(voln, adf)
inds = np.arange(nT) #np.random.randint(nT,size=75)
nI = len(inds)
indloc = saveloc + '/ind0/'
makedirs(indloc ,exist_ok=True)
indfile = (indloc + str(voln) + '.npy')
np.save(indfile,inds)
wctloc = saveloc + '/wctTraining/'
makedirs(wctloc ,exist_ok=True)
wctfile = (wctloc + str(voln) + '.pkl')
with open(wctfile, 'wb') as file:
pickle.dump(wct[:,:,inds],file, pickle.HIGHEST_PROTOCOL)
'''
wctfile = ('../z18_GonzCast_WeightedCWT/results/wctTesting/' + str(voln) + '.pkl')
with open(wctfile, 'rb') as file:
wct = pickle.load(file)
_, m, n = wct.shape
nP = p_utils.knn
nW = 1
nI = n
inds = np.arange(nI)
# Build background connectivity
print('Building background connectivity')
Wcoh1 = np.ones((nP*nW,nP*nW)) # setting background to infinate (=1) distance
Wcoh0 = np.zeros((nP*nW,nP*nW))
Wcoh = [1]*nI
GG = {}
inc = 0
for ee, ii in enumerate(tqdm(inds)):
temp = Wcoh0.copy()
inc = 0
for nw in range(nW):
temp[inc:inc+nP:,inc:inc+nP:] = 1-squareform(wct[:,nw,ii].ravel())
inc += nP
rows, cols = np.nonzero(temp)
temp[np.diag_indices(nP*nW)] = 0
Wcoh[ee] = Wcoh1.copy()
Wcoh[ee][rows,cols] = temp[rows,cols]
print('Wcoh is len {}'.format(len(Wcoh)))
'''
pool = Pool(min(nI,args.poolSize), p_utils.getMultiscaleIndexer, (nP, nW, ))
bd_fun = partial(p_utils.calcSimplices)
bdMat = list(pool.imap(bd_fun, tqdm([[tt, Wcoh[tt]] for tt in range(nI)], desc='calc connected simplices'), chunksize=1))
#bdMat = list(map(bd_fun, tqdm([[tt, Wcoh[tt]] for tt in range(nI)], desc='calc connected simplices')))
loc = saveloc + '/simplexTraining/'
makedirs(loc ,exist_ok=True)
name = (loc + str(voln) + '.pkl')
with open(name, 'wb') as file:
pickle.dump(bdMat,file)
'''
pool = Pool(min(nI,args.poolSize))
ripFun = partial(p_utils.getRipped,
maxdim=1,
makeSquare = False,
fixInf = True,
do_cocycles=True,
threshold=p_utils.TARGS_CEIL,
doLandscape=False)
Rip = list(pool.imap(ripFun, tqdm([Wcoh[tt] for tt in range(nI)], desc='Rips fun'), chunksize=1))
riploc = saveloc + '/rippedTraining/'
makedirs(riploc ,exist_ok=True)
ripfile = (riploc + str(voln) + '.pkl')
with open(ripfile, 'wb') as file:
pickle.dump(Rip,file, pickle.HIGHEST_PROTOCOL)
for r in range(maxdim+1):
print('One lifetimes histogram in dimension {}'.format(r))
print(np.histogram(np.diff(Rip[0]['dgms'][r],axis=1)))
pool.close()
pool.join()
pool.terminate
#if args.display:
# padmFun = partial(plotters.plotCoherMetrics, display=args.display)
# Process(target=padmFun)
return
for voln in volunteers[:nVols]:
if OSisfile(saveloc + '/train0table/' + str(voln) + '.npz'):
pass
else:
doStack(voln)
if __name__ == '__main__':
main()