-
Notifications
You must be signed in to change notification settings - Fork 1
/
subject.py
195 lines (163 loc) · 9.52 KB
/
subject.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
from os.path import join
import numpy as np
import nibabel as nb
import os
from xml.dom import minidom
from collections import Counter
import itertools
import pandas as pd
import re
from matplotlib.colors import from_levels_and_colors
class mniT1Data:
def __init__(self):
self.mni = join(os.environ['FSLDIR'], 'data/standard/MNI152_T1_1mm_brain.nii.gz')
self.mni_img = nb.load(self.mni)
self.mni_data = self.mni_img.get_data()
self.mni_mask = join(os.environ['FSLDIR'], 'data/standard/MNI152_T1_1mm_brain_mask.nii.gz')
self.mni_mask_img = nb.load(self.mni_mask)
self.mni_mask_data = self.mni_mask_img.get_data()
class havardOxfordThalamusData:
def __init__(self):
self.mni_subcortex = join(os.environ['FSLDIR'],
'data/atlases',
'HarvardOxford',
'HarvardOxford-sub-maxprob-thr25-1mm.nii.gz')
self.mni_subcortex_img = nb.load(self.mni_subcortex)
self.mni_subcortex_data = self.mni_subcortex_img.get_data()
self.prob_img = nb.load(join(os.environ['FSLDIR'],
'data/atlases',
'HarvardOxford',
'HarvardOxford-cort-prob-1mm.nii.gz'))
self.prob_data = self.prob_img.get_data()
self.mni_subcortex_data_masked = np.ma.masked_where(~np.isin(self.mni_subcortex_data, [15, 4]), self.mni_subcortex_data)
class talairachData:
def __init__(self):
self.talairach = join(os.environ['FSLDIR'],
'data/atlases/Talairach/Talairach-labels-1mm.nii.gz')
self.talairach_img = nb.load(self.talairach)
self.talairach_data = self.talairach_img.get_data()
class talairachLabels:
def __init__(self):
self.xmldoc = minidom.parse(join(os.environ['FSLDIR'],
'data/atlases/Talairach.xml'))
self.itemlist = self.xmldoc.getElementsByTagName('label')
self.talairach_dict = {}
for s in self.itemlist:
label_number = int(s.attributes['index'].value)
region_name = s.childNodes[0].data
self.talairach_dict[label_number] = region_name
self.talairach_df = pd.DataFrame.from_dict(self.talairach_dict, orient='index')
self.talairach_df.index.name='label number'
class talairachThalamusLabels(talairachLabels):
def __init__(self):
super().__init__()
# select thalamus labels
self.talairach_thalamus_df = self.talairach_df[self.talairach_df[0].str.contains('Sub-lobar.Thalamus')]
# side and name extracted to columns
self.talairach_thalamus_df['side'] = self.talairach_thalamus_df[0].str.split(' ').str[0]
self.talairach_thalamus_df['name'] = self.talairach_thalamus_df[0].str.split('.').str[-1]
self.remove_nucleus_from_name = lambda x: re.sub(' Nucleus', '', x) if x.endswith('Nucleus') else x
self.talairach_thalamus_df['name'] = self.talairach_thalamus_df.name.map(self.remove_nucleus_from_name)
# re-number nuclei
self.nuclei_list = self.talairach_thalamus_df['name'].unique()
self.name_num_dict = dict(zip(self.nuclei_list, range(1, len(self.nuclei_list)+1)))
self.talairach_thalamus_df['roi_number'] = self.talairach_thalamus_df['name'].map(self.name_num_dict)
class talirachThalamusLabelsColor(talairach_thalamus_labels):
def __init__(self):
super().__init__()
self.colors = np.array([[0.80526917, 0.76246004, 0.71236253],
[0.73105507, 0.82655098, 0.89263817],
[0.69931578, 0.6526525 , 0.5823189 ],
[0.74857169, 0.81418701, 0.65601533],
[0.86296798, 0.90864072, 0.64738471],
[0.68078472, 0.57431632, 0.63717782],
[0.93066726, 0.93946834, 0.59296891],
[0.79797146, 0.55516047, 0.78058996],
[0.82512093, 0.58636063, 0.63810387],
[0.73687016, 0.86102455, 0.59222474],
[0.56751623, 0.81363333, 0.75638909],
[0.92659012, 0.56316328, 0.67795806]])
self.levels = np.arange(12 + 1) - 0.5
self.cmap, self.norm = from_levels_and_colors(self.levels, self.colors)
self.cmap.set_bad('white', 0)
# talairach_thalamus_df.loc[:, 'colors'] = list(colors)
self.color_df = pd.DataFrame({'number':np.arange(1,len(self.colors)+1),
'colors':list(self.colors)})
self.talairach_thalamus_df = pd.merge(self.talairach_thalamus_df.reset_index(),
self.color_df,
left_on = 'roi_number',
right_on = 'number', how='left')
self.number_to_colors_dict = self.color_df.set_index('number').to_dict()['colors']
self.talairach_label_to_color_dict = self.talairach_thalamus_df.set_index('name').to_dict()['colors']
class talairachThalamusImage(talirachThalamusLabelsColor, talairachData):
def __init__(self):
talairachData.__init__(self)
super().__init__()
self.orig_num_to_new_dict = self.talairach_thalamus_df['roi_number'].to_dict()
# print(self.talairach_data)
self.talairach_thalamus_data = self.talairach_data.copy()
# zero other areas apart from the thalamus
self.talairach_thalamus_data[~np.isin(self.talairach_data,
self.talairach_thalamus_df['label number'].values)] = 0
# change the values of the labels
for origNum, newNum in self.talairach_thalamus_df.set_index('label number').roi_number.to_dict().items():
np.place(self.talairach_thalamus_data, self.talairach_thalamus_data==origNum, newNum)
def estimate_overlap_count_with(self, nibabel_img):
'''
Returns dict overlap voxel count
'''
overlap_img = np.ma.masked_where(nibabel_img, self.talairach_thalamus_data)
voxel_count = Counter(np.array(overlap_img.filled()).ravel())
return voxel_count
class behrensMask():
def __init__(self):
self.behrens_mask_loc = join(os.environ['FSLDIR'],
'data/atlases/Thalamus',
'Thalamus-maxprob-thr25-1mm.nii.gz')
self.behrens_mask_img = nb.load(self.behrens_mask_loc)
self.behrens_mask_data = self.behrens_mask_img.get_data()
self.behrens_mask_data_masked = np.ma.masked_where(self.behrens_mask_data == 0, self.behrens_mask_data)
class behrensMaskColor():
def __init__(self):
self.behrensRoiNumDict = {"Primary motor":1,
"Sensory":2,
"Occipital":3,
"Pre-frontal":4,
"Pre-motor":5,
"Posterior parietal":6,
"Temporal":7}
self.behrensNumRoiDict = {value : key for key,value in self.behrensRoiNumDict.items()}
self.behrens_colors = np.array([[0.87192607, 0.66041753, 0.91142589],
[0.80917289, 0.88120141, 0.88958628],
[0.6416239 , 0.86516164, 0.71139534],
[0.56606377, 0.61487396, 0.73786088],
[0.86076639, 0.63772511, 0.67666386],
[0.66172476, 0.85218288, 0.62232168],
[0.75858755, 0.83385981, 0.81687487]])
self.behrens_levels = np.arange(7 + 1) - 0.5
self.behrens_cmap, self.behrens_norm = from_levels_and_colors(self.behrens_levels, self.behrens_colors)
self.behrens_cmap.set_bad('white', 0)
self.behrens_color_df = pd.DataFrame({'number':np.arange(1, 8),
'colors':list(self.behrens_colors)})
self.behrens_color_df['name'] = self.behrens_color_df['number'].map(self.behrensNumRoiDict)
self.behrens_label_to_color_dict = self.behrens_color_df.set_index('name').to_dict()['colors']
get_long_side=lambda x: 'left' if x=='lh' else 'right'
def get_seed_map_fsl_thr_loc_mni(subject_loc, side, cortex, thrP=90):
side_long = get_long_side(side)
seed_map_loc = join(subject_loc, 'segmentation', side_long, str(thrP)+'thrP', 'mni_{}_seeds_to_{}_{}.nii.gz'.format(thrP, side, cortex))
return seed_map_loc
def get_seed_map_fsl_thr_loc(subject_loc, side, cortex, thrP=90):
side_long = get_long_side(side)
seed_map_loc = join(subject_loc, 'segmentation', side_long, str(thrP)+'thrP', '{}_seeds_to_{}_{}.nii.gz'.format(thrP, side, cortex))
return seed_map_loc
def get_seed_map_loc(subject_loc, side, cortex):
side_long = get_long_side(side)
seed_map_loc = join(subject_loc, 'segmentation', side_long, 'mni_seeds_to_{}_{}.nii.gz'.format(side, cortex))
return seed_map_loc
def get_tract_map_loc(subject_loc, side, cortex):
side_long = get_long_side(side)
tract_map_loc = join(subject_loc, 'segmentation', side_long, 'fig', cortex, 'mni_fdt_paths.nii.gz')
return tract_map_loc
def get_dice_coeff(data1_region, data2_region):
diceC = np.sum(data1_region * data2_region) * 2.0 / (np.sum(data1_region) + np.sum(data2_region))
return diceC