forked from razvanmarinescu/brain-coloring
-
Notifications
You must be signed in to change notification settings - Fork 1
/
blendCreateSnapshot.py
226 lines (181 loc) · 8.39 KB
/
blendCreateSnapshot.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
#!BPY
import sys
print('blender python:', sys.exec_prefix)
import bpy
import numpy as np
import os
import pandas as pd
# filename = 'blendCreateSnapshot.py'
# exec(compile(open('blendCreateSnapshot.py').read(), 'blendCreateSnapshot.py', 'exec'))
blendFullPath = os.path.abspath('.')
os.chdir(blendFullPath)
sys.path.append(blendFullPath)
from blendHelper import *
from fileFormatChecker import *
# if environment variable configFile is set, read the path from there. Otherwise, load ./config.py
configFile = os.getenv('configFile', './config.py')
import importlib.util
spec = importlib.util.spec_from_file_location("module.name", configFile)
config = importlib.util.module_from_spec(spec)
spec.loader.exec_module(config)
INPUT_FILE = config.INPUT_FILE
print(INPUT_FILE)
OUT_FOLDER = config.OUTPUT_FOLDER
COLORS_RGB = config.COLORS_RGB
COLOR_POINTS = [np.array(x) for x in COLORS_RGB]
NR_SIGN_LEVELS = len(COLOR_POINTS)-1
IMG_TYPE = config.IMG_TYPE
BRAIN_TYPE = config.BRAIN_TYPE
ATLAS = config.ATLAS
if ATLAS == 'DK':
cortAreasIndexMap = config.cortAreasIndexMapDK
subcortAreasIndexMap = config.subcortAreasIndexMap
elif ATLAS == 'Destrieux':
cortAreasIndexMap = config.cortAreasIndexMapDestrieux
subcortAreasIndexMap = config.subcortAreasIndexMap
elif ATLAS == 'Mice':
cortAreasIndexMap = config.cortAreasIndexMapMice
subcortAreasIndexMap = config.subcortMouseAreasIndexMap
elif ATLAS == 'Tourville':
cortAreasIndexMap = config.cortAreasIndexMapTourville
subcortAreasIndexMap = config.subcortAreasIndexMap
ATLAS = 'DKT' # actually 3D models are labelled as DKT
elif ATLAS == 'Dorr':
cortAreasIndexMap = config.cortAreasIndexMapDorr
subcortAreasIndexMap = config.subcortDorrAreasIndexMap
elif ATLAS == 'Dsurque':
cortAreasIndexMap = config.cortAreasIndexMapDsurque
subcortAreasIndexMap = config.subcortDsurqueAreasIndexMap
elif ATLAS == 'Custom':
cortAreasIndexMap = config.cortAreasIndexMapCustom
subcortAreasIndexMap = config.subcortAreasIndexMapCustom
else:
raise ValueError('ATLAS has to be either \'DK\', \'Destrieux\', \'Tourville\' or \'Custom\' ')
cortAreasList = cortAreasIndexMap.keys()
subcortAreasShort = subcortAreasIndexMap.keys()
cortAreas = []
cortRegionsThatShouldBeInTemplate = cortAreasIndexMap.values()
subcortRegionsThatShouldBeInTemplate = subcortAreasIndexMap.values()
for mesh in cortAreasList:
if 'Right-' in mesh:
cortAreas.append(mesh.replace('Right-', ''))
elif 'Left-' in mesh:
cortAreas.append(mesh.replace('Left-', ''))
cortAreas = list(set(cortAreas)) # remove duplicated items
cortFilesRight = ['models/%s_atlas_%s/rh.%s.%s.%s.ply' % (ATLAS, BRAIN_TYPE, BRAIN_TYPE, ATLAS, x) for x in cortAreas]
cortFilesLeft = ['models/%s_atlas_%s/lh.%s.%s.%s.ply' % (ATLAS, BRAIN_TYPE, BRAIN_TYPE, ATLAS, x) for x in cortAreas]
cortFilesAll = list(set(cortFilesLeft + cortFilesRight))
cortAreasNamesFull = [x.split("/")[-1][:-4] for x in cortFilesAll]
cortAreasNamesMatching = [] # for the new mesh order
for index, mesh in enumerate(cortAreasNamesFull):
if 'rh.' in mesh:
cortAreasNamesMatching.append(cortAreasIndexMap.get('Right-' + mesh.split(".")[-1]))
elif 'lh.' in mesh:
cortAreasNamesMatching.append(cortAreasIndexMap.get('Left-' + mesh.split(".")[-1]))
cortAreasIndexMap = dict(zip(cortAreasNamesFull, cortAreasNamesMatching))
if ATLAS == 'Mice':
subcortMiceAreas = [x[4:] for x in subcortAreasIndexMap.keys()]
subcortMiceAreasIndexMap = dict(zip(subcortMiceAreas, subcortAreasIndexMap.values()))
subcortAreas = [x for x in subcortAreasIndexMap.keys() if subcortAreasIndexMap[x] != -1]
subcortFiles = ['./models/mouse_subcortical_ply/%s.ply' % x for x in subcortAreas]
elif ATLAS == 'Dorr':
subcortMiceAreas = [x[4:] for x in subcortAreasIndexMap.keys()]
subcortMiceAreasIndexMap = dict(zip(subcortMiceAreas, subcortAreasIndexMap.values()))
subcortAreas = [x for x in subcortAreasIndexMap.keys() if subcortAreasIndexMap[x] != -1]
subcortFiles = ['./models/Dorr_subcortical_ply/%s.ply' % x for x in subcortAreas]
elif ATLAS == 'Dsurque':
subcortMiceAreas = [x[4:] for x in subcortAreasIndexMap.keys()]
subcortMiceAreasIndexMap = dict(zip(subcortMiceAreas, subcortAreasIndexMap.values()))
subcortAreas = [x for x in subcortAreasIndexMap.keys() if subcortAreasIndexMap[x] != -1]
subcortFiles = ['./models/Dsurque_subcortical_ply/%s.ply' % x for x in subcortAreas]
else:
subcortRightAreas = [x[4:] for x in subcortAreasIndexMap.keys()]
subcortRightAreasIndexMap = dict(zip(subcortRightAreas, subcortRegionsThatShouldBeInTemplate))
subcortAreas = [x for x in subcortAreasIndexMap.keys() if subcortAreasIndexMap[x] != -1]
subcortFiles = ['./models/subcortical_ply/%s.ply' % x for x in subcortAreas]
nrSubcortRegions = len(subcortAreas)
nrCortRegions = len(cortFilesAll)
# merge cort and subcort index maps
cortIndexMap = cortAreasIndexMap.copy()
cortIndexMap.update(subcortAreasIndexMap)
fullIndexMap = cortIndexMap # for mouse brain and cross section view
if IMG_TYPE == 'subcortical-outer-right-hemisphere' or IMG_TYPE == 'subcortical':
painter = SubcorticalPainter(cortFilesRight,subcortFiles)
indexMap = subcortAreasIndexMap
areasShort = subcortAreasShort
regionsThatShouldBeInTemplate = subcortRegionsThatShouldBeInTemplate
elif IMG_TYPE == 'subcortical-outer-left-hemisphere':
painter = SubcorticalPainterLeft(cortFilesLeft,subcortFiles)
indexMap = subcortAreasIndexMap
areasShort = subcortAreasShort
regionsThatShouldBeInTemplate = subcortRegionsThatShouldBeInTemplate
elif IMG_TYPE == 'subcortical-top':
painter = SubcorticalPainterTop(cortFilesLeft,subcortFiles)
indexMap = subcortAreasIndexMap
areasShort = subcortAreasShort
regionsThatShouldBeInTemplate = subcortRegionsThatShouldBeInTemplate
elif IMG_TYPE == 'subcortical-bottom':
painter = SubcorticalPainterBottom(cortFilesLeft,subcortFiles)
indexMap = subcortAreasIndexMap
areasShort = subcortAreasShort
regionsThatShouldBeInTemplate = subcortRegionsThatShouldBeInTemplate
# cortical painters
# right side painter
elif IMG_TYPE == 'cortical-outer-right-hemisphere':
# loadCortical(cortFilesAll)
if ATLAS == 'Mice': painter = CorticalPainter(cortFilesAll, subcortFiles)
else: painter = CorticalPainter(cortFilesRight)
indexMap = fullIndexMap
areasShort = cortAreas
regionsThatShouldBeInTemplate = cortRegionsThatShouldBeInTemplate
elif IMG_TYPE == 'cortical-inner-right-hemisphere':
painter = CorticalPainterInnerRight(cortFilesRight, subcortFiles)
indexMap = fullIndexMap
areasShort = cortAreas
regionsThatShouldBeInTemplate = cortRegionsThatShouldBeInTemplate
# left side painter
elif IMG_TYPE == 'cortical-outer-left-hemisphere':
painter = CorticalPainterLeft(cortFilesLeft)
if ATLAS == 'Mice': painter = CorticalPainterLeft(cortFilesLeft, subcortFiles)
indexMap = fullIndexMap
areasShort = cortAreas
regionsThatShouldBeInTemplate = cortRegionsThatShouldBeInTemplate
elif IMG_TYPE == 'cortical-inner-left-hemisphere':
painter = CorticalPainterInnerLeft(cortFilesLeft, subcortFiles)
indexMap = fullIndexMap
areasShort = cortAreas
regionsThatShouldBeInTemplate = cortRegionsThatShouldBeInTemplate
elif IMG_TYPE == 'cortical-top' or IMG_TYPE == 'top':
painter = CorticalPainterTop(cortFilesAll, subcortFiles)
indexMap = fullIndexMap
areasShort = cortAreas
regionsThatShouldBeInTemplate = cortRegionsThatShouldBeInTemplate
elif IMG_TYPE == 'cortical-bottom' or IMG_TYPE == 'bottom':
painter = CorticalPainterBottom(cortFilesAll, subcortFiles)
indexMap = fullIndexMap
areasShort = cortAreas
regionsThatShouldBeInTemplate = cortRegionsThatShouldBeInTemplate
else:
raise ValueError('mode has to be either cortical-outer, cortical-inner or subcortical')
fov = 50.0
if BRAIN_TYPE == 'inflated':
ortho_scale = 280
elif IMG_TYPE in ['top', 'bottom']:
ortho_scale = 190
else:
ortho_scale = 180
matDf = pd.read_csv(INPUT_FILE)
labels = matDf.columns.to_list()
checkInputDf(matDf, regionsThatShouldBeInTemplate)
painter.prepareScene(resolution=config.RESOLUTION, bckColor = config.BACKGROUND_COLOR, fov=fov, ortho_scale=ortho_scale, BRAIN_TYPE=BRAIN_TYPE)
painter.loadMeshes()
print('-------------%s-------------' % INPUT_FILE)
colorRegionsAndRender(indexMap, matDf, COLOR_POINTS, OUT_FOLDER, IMG_TYPE)
# outFolderCurrMat = '%s' % (OUT_FOLDER.rsplit('/', 1)[0])
outFolderCurrMat = '%s' % OUT_FOLDER
text = genLaTex(INPUT_FILE, OUT_FOLDER, COLORS_RGB)
os.system('mkdir -p %s' % outFolderCurrMat)
out = open('%s/report.tex' % outFolderCurrMat, 'w')
out.write(text)
print("")
out.close()