-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndividualWatershed.py
167 lines (131 loc) · 5.07 KB
/
IndividualWatershed.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
#! /usr/bin/env python
from hcp_class import hcp_subj
import numpy as np
from functools import reduce
import nibabel as nib
import pickle
import sys
import seaborn as sn
import subprocess as sp
import os
from utils import save_gifti
from nilearn.plotting import view_surf
from statistics import mode
SubjectIn=sys.argv[1]
##### this is the watershed function we call
def cortiGradWS(W,D,m,method):
##### adapted from eyal soreq's matlab code
#### W is masked metric file to use in watershed
#### D is masked distance matrix for a previously specified radius
m=m
#### get the order of the elements of local minima or the maximum depending on method provided
idx=np.argsort(W)
if method =='min':
idx=idx
elif method =='max':
idx=idx[::-1]
# #### need to go in order from most to least not on the distL alone
# #### so reorder the distL to go by idx
N= len(idx)
C=np.zeros(W.shape)
for ii in idx:
##### find where the nodes in your ROI are
subNodes=np.where(D[ii]>0)[0]
c=C[subNodes]
c=c[c>0]
##### next part conditional
if c.size == 0:
C[ii]=m
m=m+1
# comment back in. this is to see if you can plot just the watershed itself
elif ~np.any(np.diff(c)):
C[ii]=c[0]
else:
C[ii]=mode(c)
return C
def getSensory(label):
data=nib.load(label).darrays[0].data
A1=np.hstack([np.where(data==33)[0],np.where(data==75)[0]])
S1=np.hstack([np.where(data==28)[0],np.where(data==46)[0]])
V1=np.hstack([np.where(data==45)[0],np.where(data==43)[0]])
return A1,V1,S1
def SensVals(subj,distArr,hemi):
data=np.round(distArr)
if hemi == 'L':
A1,V1,S1=getSensory(subj.Laparc)
elif hemi =='R':
A1,V1,S1=getSensory(subj.Raparc)
A1Vals=data[A1]
S1Vals=data[S1]
V1Vals=data[V1]
# print(A1Vals.shape,S1Vals.shape,V1Vals.shape)
equi=reduce(np.intersect1d,[A1Vals,S1Vals,V1Vals])
distVals=np.hstack([A1Vals,S1Vals,V1Vals])
# equi=np.intersect1d(A1Vals,S1Vals)
# equi=np.intersect1d(equi,V1Vals)
# print(equi)
if equi.shape[0] <1:
print(f'subject {subj.subj} has no equidistant value from gradient mask to the {hemi} sensory cortex')
val = 0
highest=0
valid=False
return np.nan,np.nan,np.nan,np.nan
else:
equi=list(equi)
val=[np.count_nonzero(distVals==val) for val in equi ]
valid=True
common=equi[np.argmax(val)]
highest=np.max(equi)
lowest=np.min(equi)
med=np.median(equi)
# print(f'the common is {common},max is {highest},min is {lowest}, median ins {np.round(med)}')
return common,highest,lowest,np.round(med)
def runWS(subject):
subj_str=subject
inst=hcp_subj(subj_str,4)
Ldist=np.round(np.load(f'/well/margulies/projects/pkReliability/Dist2SensoryBorder/{subj_str}/L.top10toCort.npy'))
Rdist=np.round(np.load(f'/well/margulies/projects/pkReliability/Dist2SensoryBorder/{subj_str}/R.top10t0Cort.npy'))
keys=['Common','MaxEq','MinEq','MedEq']
L=SensVals(inst,Ldist,'L')
R=SensVals(inst,Rdist,'R')
if np.isnan(L[0])==True or np.isnan(R[0])==True:
pass
else:
L=dict(zip(keys,L))
print(L)
R=dict(zip(keys,R))
print(R)
outpath=f'tmp.{inst.subj}/'
sp.run(f'mkdir -p {outpath}',shell=True)
WS_outPath=f'/well/margulies/projects/pkReliability/Dist2SensoryBorder/{inst.subj}/WS_seg'
sp.run(f'mkdir -p {WS_outPath}',shell=True)
for key in L:
cifti_out=f'{outpath}/{key}.L.0{int(L[key])}.dconn.nii'
cmd=f'wb_command -surface-geodesic-distance-all-to-all {inst.Lsrf} {cifti_out} -limit {L[key]}'
sp.run(cmd,shell=True)
dist=nib.load(cifti_out).get_fdata()
dist=dist[inst.Lfill,:][:,inst.Lfill]
grad=inst.Lgrad[0][inst.Lfill]
WS=cortiGradWS(grad,dist,1,'max')
del dist
cmd=f'rm {cifti_out}'
sp.run(cmd,shell=True)
out=np.zeros(32492)
out[inst.Lfill]=WS
save_gifti(out,f'{WS_outPath}/L.{key}.0{int(L[key])}')
for key in R:
cifti_out=f'{outpath}/{key}.R.0{int(R[key])}.dconn.nii'
cmd=f'wb_command -surface-geodesic-distance-all-to-all {inst.Rsrf} {cifti_out} -limit {R[key]}'
sp.run(cmd,shell=True)
dist=nib.load(cifti_out).get_fdata()
dist=dist[inst.Rfill,:][:,inst.Rfill]
grad=inst.Rgrad[0][inst.Rfill]
WS=cortiGradWS(grad,dist,1,'max')
del dist
cmd=f'rm {cifti_out}'
sp.run(cmd,shell=True)
sp.run(f'rm -rf {outpath}',shell=True)
out=np.zeros(32492)
out[inst.Rfill]=WS
save_gifti(out,f'{WS_outPath}/R.{key}.0{int(R[key])}')
runWS(SubjectIn)