-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapply_kesh_pipeline.py
194 lines (140 loc) · 6.33 KB
/
apply_kesh_pipeline.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
import pandas as pd
import os
import dipy as dp
from dipy.tracking.utils import length
from dipy.tracking import utils
from glob import glob
import nibabel as nib
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import nilearn.plotting as nip
from dipy.viz import window, actor, ui
from IPython.display import Image
from dipy.align.streamlinear import whole_brain_slr, slr_with_qb, transform_streamlines
from dipy.segment.clustering import qbx_and_merge
from dipy.segment.bundles import RecoBundles
track_of_interest = 'Left ARCUATE'
template_basepath = './qb_templates/Base_CTRL'
wb_template = os.path.join(
template_basepath, 'Whole_Brain_long_resaved_newapi.trk')
template_boi = '/Users/kesshijordan/Desktop/IU_Bloomington/qb_templates/Arcuate_template.trk'
putdir = './pyAFQuicobundles_freesurfer_test_outputs'
if not os.path.exists(putdir):
os.mkdir(putdir)
roi_matrix_path = 'WM_pathways_freesurfer_based_seg_EC_KMJmod.xlsx'
pathpath = './pyafquicobundles_freesurfer_test_basepath.txt'
# Load files
apac_path = glob(os.path.join(case_basepath, 'aparc+aseg_di*.nii*'))[0]
fa_path = glob(os.path.join(case_basepath, '*_fa.nii*'))[0]
md_path = glob(os.path.join(case_basepath, '*_md.nii*'))[0]
wb_path = glob(os.path.join(case_basepath, 'Whole_Brain*newapi.trk'))[0]
with open(pathpath, 'r') as myfile:
case_basepath = myfile.read().replace('\n', '').replace('\'', '')
wb_template_tg, wb_template_hdr = loadtgm_newapi(wb_template)
wb_template_sls = wb_template_tg.streamlines
template_arcuate_tg, template_arcuate_hdr = loadtgm_newapi(template_arcuate)
template_arcuate_sls = template_arcuate_tg.streamlines
def loadnii(niipath):
im = nib.load(niipath)
return im, im.get_data(), im.affine
def loadtgm_newapi(trkpath):
trkloaded = nib.streamlines.trk.TrkFile.load(trkpath)
hdrloaded = trkloaded.header
tg = trkloaded.tractogram
return tg, hdrloaded
def filter_length(streamlines, minlen=40):
print("calc lengths")
lengths = list(length(streamlines))
print("filter")
long_sls = []
for i, sl in enumerate(streamlines):
if lengths[i] > minlen:
long_sls.append(sl)
return long_sls
def filter_freesurf(sls, apac_data, aff, code):
return list(utils.target(sls, apac_data == code, affine=aff))
apac_im, apac_data, apac_aff = loadnii(apac_path)
fa_im, fa_data, fa_aff = loadnii(fa_path)
md_im, md_data, md_aff = loadnii(md_path)
roi_matrix = pd.read_excel(roi_matrix_path)
def build_dict(track_name, roi_matrix):
rois = roi_matrix[track_name]
roi_groups = list(rois.value_counts().index)
roi_dict = {}
roi_dict['include'] = {}
roi_dict['exclude'] = {}
for i in roi_groups:
if i > 0:
roitype = 'include'
elif i < 0:
roitype = 'exclude'
else:
raise('UHOH')
setname = 'set'+str(int(i))
roi_dict[roitype][setname] = {}
temp = roi_matrix[rois == i]
for j, name in enumerate(temp['VOIS']):
roi_dict[roitype][setname][name] = temp['aparc+aseg'].values[j]
return roi_dict
def combine_rois(mydict, apac):
x, y, z = apac.shape
include = np.zeros([x, y, z, len(mydict['include'])])
exclude = np.zeros([x, y, z, len(mydict['exclude'])])
for i, iset in enumerate(mydict['include'].keys()):
for item in mydict['include'][iset].items():
include[:, :, :, i] += 1*(apac == item[-1])
for j, jset in enumerate(mydict['exclude'].keys()):
for item in mydict['exclude'][jset].items():
exclude[:, :, :, j] += 1*(apac == item[-1])
return include, exclude
mydict = build_dict(track_of_interest, roi_matrix=roi_matrix)
include, exclude = combine_rois(mydict, apac_data)
def targetme(sls, include, exclude, aff):
for i in range(include.shape[-1]):
sls = list(utils.target(sls, include[:, :, :, i], affine=aff))
for i in range(exclude.shape[-1]):
sls = list(utils.target(
sls, exclude[:, :, :, i], affine=aff, include=False))
return sls
bundle = targetme(long_sls, include, exclude, fa_aff)
def rough_reg(sub_fixed, temp_moving):
# template moves to the subject space
# qb_thr=5 errored
moved, transform, qb_centroids1, qb_centroids2 = whole_brain_slr(sub_fixed, temp_moving,
verbose=True,
progressive=True)
return moved, transform, qb_centroids1, qb_centroids2
def run_rb(templatesls, bucketosls, cluster_map=None, pruning_thr=10):
# try pruning thresh 10 if not specific drop to 5
if cluster_map is None:
cluster_map = qbx_and_merge(bucketosls, thresholds=[40, 25, 20, 10])
else:
print("Loading provided cluster map")
rb = RecoBundles(bucketosls, cluster_map=cluster_map, clust_thr=5)
recognized_atlassp, rec_labels, recognized_ptsp = rb.recognize(model_bundle=templatesls,
model_clust_thr=5.,
reduction_thr=10, pruning_thr=pruning_thr)
'''rb = RecoBundles(bucketosls, cluster_map=cluster_map, clust_thr=10)
recognized, rec_labels, rec_trans = rb.recognize(model_bundle=templatesls,
model_clust_thr=1.)'''
#D = bundles_distances_mam(templatesls, recognized)
return recognized_ptsp, cluster_map
from dipy.align.streamlinear import StreamlineLinearRegistration
from dipy.tracking.streamline import set_number_of_points
def runslr(fixed, moving, npts=20):
fixed_subsamp = set_number_of_points(fixed, npts)
moving_subsamp = set_number_of_points(moving, npts)
srr = StreamlineLinearRegistration()
srm = srr.optimize(static=fixed_subsamp, moving=moving_subsamp)
aligned = srm.transform(moving)
return aligned
moved_temp2case, xfm_temp2case, qbc1_temp2pcase, qbc2_temp2case = rough_reg(
long_sls[::10], wb_template_sls)
template_boi_sls_xfmd = template_boi_tg.copy(
).apply_affine(xfm_temp2case).streamlines
template_boi_sls_xfmd_slr = runslr(arc, template_boi_sls_xfmd, npts=10)
template_arrayseq = nib.streamlines.array_sequence.ArraySequence(
template_boi_sls_xfmd)
bucket_arrayseq = nib.streamlines.array_sequence.ArraySequence(boi)
rb_boi, ig_ip = run_rb(template_arrayseq, bucket_arrayseq)