-
Notifications
You must be signed in to change notification settings - Fork 0
/
matplotlib_main.py
507 lines (403 loc) · 20.4 KB
/
matplotlib_main.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 10 23:14:26 2019
@author: majaa
"""
import sys
import numpy as np
import nibabel as nib
#import matplotlib.pyplot as plt
import os
import vtk.util.colors as colors
from PyQt5 import QtCore, QtGui
from PyQt5 import Qt
from fury import window,actor
#from dipy.viz import window, actor
from PyQt5.QtWidgets import QLabel,QRadioButton,QComboBox,QPushButton, QAction, QMenu, QApplication, QFileDialog, QWidget, QSplitter, QGroupBox
from PyQt5.QtGui import QIcon
import PyQt5.QtGui
from dipy.data import fetch_bundles_2_subjects, read_bundles_2_subjects
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from dipy.tracking.streamline import transform_streamlines, length
from shutil import copyfile
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FingureCanvas
from matplotlib.figure import Figure
import pickle
from nibabel import trackvis
from dipy.tracking import utils
import time
from dipy.tracking.streamline import set_number_of_points
from sklearn import svm
import nibabel as nib
from joblib import Parallel, delayed
from dipy.tracking.vox2track import streamline_mapping
from pykdtree.kdtree import KDTree
from kd_tree_segmentation import kd2
from one_class_svm import ocsvm
from statistical_analysis import stat
from preprocessing import preprocessing as pre
import tkinter.font as tkFont
import tkinter as tk
#from kd_tree_segmentation.kd2 import
interactive = False # set to True to show the interactive display window
class MainWindow(Qt.QMainWindow):
def load_streamline2(self,whole_brain_tractogram,fileName2):
from fury import window
scene = window.Scene()
self.no_of_points=12
self.leafsize=10
# whole_brain_id = "100307"
# #model_tracts =[ "124422", "111312", "100408", "100307", "856766"]
# self.tract = "_af.left.trk"
test_data= pre.load(self, whole_brain_tractogram)
# print("Ki shomossha! ",whole_brain_tractogram," 2 ",fileName2)
if self.method == 1:
print(fileName2)
segmented_tract_positive, segmented_tract_negative = ocsvm.oneClassSVM(self, whole_brain_tractogram, fileName2)
color_tract = colors.blue
else:
segmented_tract_positive, segmented_tract_negative = kd2.segmentation_with_NN(self, whole_brain_tractogram, fileName2, self.no_of_points, self.leafsize)
color_tract = colors.green
affine=utils.affine_for_trackvis(voxel_size=np.array([1.25,1.25,1.25]))
########### Statistical Analysis ################
whole_brain_length_info= np.array(stat.length_info (self, test_data))
max_label = "\nMaximum length of tracts: " + str(whole_brain_length_info[0])
min_label = "\nMinimum length of tracts: "+ str(whole_brain_length_info[1])
streamline_count_label = "\nNo of streamlines: "+ str(len(test_data))
voxel_count_label = "\nNo of voxels:: "+ str(stat.voxel_count(self, test_data))
#dev_streamline_count_label = "\nNo of streamlines: "+ str(len(test_data))
#dev_voxel_count_label = "\nNo of voxels:: "+ str(self.voxel_count(test_data))
self.max_length_label.setText(max_label)
self.min_length_label.setText(min_label)
self.no_streamlines_label.setText(streamline_count_label)
#self.dev_no_streamlines_label.setText(dev_streamline_count_label)
self.no_voxels_label.setText(voxel_count_label)
#self.dev_no_voxels_label.setText(dev_voxel_count_label)
#tract_transform = transform_streamlines(test_data, np.linalg.inv(affine))
#stream_actor = actor.line(tract_transform,(1., 0.5, 0))
tract_transform2 = transform_streamlines(segmented_tract_positive, np.linalg.inv(affine))
stream_actor2 = actor.line(tract_transform2,
colors=color_tract, linewidth=0.1)
bundle_nativeNeg = transform_streamlines(segmented_tract_negative, np.linalg.inv(affine))
stream_actorNeg = actor.line(bundle_nativeNeg, colors=colors.red,
opacity=0.01, linewidth=0.1)
#scene.add(stream_actor)
scene.add(stream_actor2)
scene.add(stream_actorNeg)
self.vtkWidget2.GetRenderWindow().AddRenderer(scene)
self.iren = self.vtkWidget2.GetRenderWindow().GetInteractor()
self.iren.Initialize()
self.iren.Start()
def load_whole_brain(self,fileName):
from fury import window
scene = window.Scene()
affine=utils.affine_for_trackvis(voxel_size=np.array([1.25,1.25,1.25]))
wholeTract= nib.streamlines.load(fileName)
wholeTract = wholeTract.streamlines
wholeTract_transform = transform_streamlines(wholeTract, np.linalg.inv(affine))
stream_actor = actor.line(wholeTract_transform, colors=colors.white,
opacity=0.02, linewidth=0.1)
# scene.set_camera(position=(-176.42, 118.52, 128.20),
# focal_point=(113.30, 128.31, 76.56),
# view_up=(0.18, 0.00, 0.98))
scene.add(stream_actor)
self.vtkWidget.GetRenderWindow().AddRenderer(scene)
self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()
self.iren.Initialize()
self.iren.Start()
def openTractFilesDialog(self):
# global Tract_fileName
# options = QFileDialog.Options()
# options |= QFileDialog.DontUseNativeDialog
# fileWithPath, _ = QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","Track Files (*.trk)", options=options)
# Tract_fileName=os.path.basename(fileWithPath)
# splitFile=Tract_fileName.split(".")[0]
# print(splitFile)
# dest="AllPickles/"+Tract_fileName
# #copyfile(fileWithPath, dest)
self.combo.clear()
self.tract_name2 = []
root_dir = 'G:\\Thesis\\ThesisIm plementations\\Tract Segmentation Visualizer\\Tract-Segmentation-Visualizer\\Tracts\\AF\\Left'
for root, dirs, files in os.walk(root_dir, topdown=False):
for name in files:
self.tract_name2.append(os.path.join(root, name))
print(os.path.join(root, name))
self.combo.addItems(self.tract_name2)
print("done...")
def openFileNameDialog(self):
# global fileName
# options = QFileDialog.Options()
# options |= QFileDialog.DontUseNativeDialog
# fileWithPath, _ = QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","Track Files (*.trk)", options=options)
# fileName=os.path.basename(fileWithPath)
# print(fileName)
# splitFile=fileName.split(".")[0]
# print(splitFile)
#dest="WholeBrains/"+fileName
#copyfile(fileWithPath, dest)
self.combo_box1.clear()
whole_brain = []
root_dir = 'G:\\Thesis\\ThesisIm plementations\\Tract Segmentation Visualizer\\Tract-Segmentation-Visualizer\\WholeBrains'
for root, dirs, files in os.walk(root_dir, topdown=False):
for name in files:
whole_brain.append(os.path.join(root, name))
print(os.path.join(root, name))
#print(dirs,type(dirs))
# for (dirpath, dirnames, filenames) in os.walk("."):
# print("File ki morse", filenames)
# self.whole_brain.extend(str("G:\\Thesis\\ThesisIm plementations\\Tract Segmentation Visualizer\\Tract-Segmentation-Visualizer\\WholeBrains" + filenames))
# break
print("Combo te bombo", whole_brain)
self.combo_box1.addItems(whole_brain)
print("done...")
def checkFile(self,fileName):
whole_brain = []
for (dirpath, dirnames, filenames) in os.walk("WholeBrains"):
whole_brain.extend(filenames)
break
if fileName in whole_brain:
return 1
else:
return 0
def clickMethod(self):
whole_brain = str(self.combo_box1.currentText())
print(whole_brain)
self.load_whole_brain(whole_brain)
def clickMethod2(self):
#AllPickles = []
#for (dirpath, dirnames, filenames) in os.walk("AllPickles"):
# AllPickles.extend(filenames)
# break
side = ""
if self.radio1.isChecked():
side = "Left"
elif self.radio2.isChecked():
side = "Right"
elif self.radio3.isChecked():
side = "Left"
choose_pickle = self.combo.currentText() + side + ".pickle"
Title = self.combo.currentText() + side
choosed_pickle = self.tract_name2
whole_brain = str(self.combo_box1.currentText())
self.method = 1
if self.ocsvm_radio.isChecked():
self.method = 1
elif self.kd_radio.isChecked():
self.method = 2
self.load_streamline2(whole_brain,choosed_pickle)
#if choose_pickle in AllPickles:
# choosed_pickle = "AllPickles/" + choose_pickle
# whole_brain = "WholeBrains/" + str(self.combo_box1.currentText())
# print("choosed_pickle",choosed_pickle)
# print("whole_brain", whole_brain)
def HorizontalLayout(self):
self.groupBox2 = QGroupBox("Method Visuals")
self.groupBox2.setStyleSheet("background-color: #006868")
self.vbox2 = Qt.QHBoxLayout()
self.wb_box = Qt.QVBoxLayout()
self.whole_brain_label = QLabel()
self.whole_brain_label.setText("\nWhole Brain Tractogram")
self.wb_box.addWidget(self.whole_brain_label)
self.vtkWidget = QVTKRenderWindowInteractor(self.frame)
self.wb_box.addWidget(self.vtkWidget)
self.vbox2.addLayout(self.wb_box)
# self.vl.addWidget(self.frame)
############################################
self.seg_box = Qt.QVBoxLayout()
self.vtkWidget2 = QVTKRenderWindowInteractor(self.frame)
self.segmented_label = QLabel()
self.segmented_label.setText("\nTract Segmentation")
self.seg_box.addWidget(self.segmented_label)
self.seg_box.addWidget(self.vtkWidget2)
self.vbox2.addLayout(self.seg_box)
self.groupBox2.setLayout(self.vbox2)
#verbox.addLayout(self.vbox2)
### Adding statistical bar
self.statBox = QGroupBox("Tract Insights")
self.statBox.setStyleSheet("QGroupBox {font-size: 15pt; font-weight: bold; font-family: Courier; color: white}")
self.predBox = Qt.QVBoxLayout()
# ## Creating font with QtFont
# font = QtGui.QFont()
# font.setFamily("Helvetica")
# font.setBold(True)
self.max_length_label = QLabel()
self.max_length_label.setText("\nMaximum length of tracts: ")
self.max_length_label.setStyleSheet("QLabel {font-size: 10pt; font-family: Courier; color: white}")
self.min_length_label = QLabel()
self.min_length_label.setText("\nMinimum length of tracts: ")
self.min_length_label.setStyleSheet("QLabel {font-size: 10pt; font-family: Courier; color: white}")
self.fa_label = QLabel()
self.fa_label.setText("\nFA value: ")
self.fa_label.setStyleSheet("QLabel {font-size: 10pt; font-family: Courier; color: white}")
self.dev_fa_label = QLabel()
self.dev_fa_label.setText("\nDeviation FA value: ")
self.dev_fa_label.setStyleSheet("QLabel {font-size: 10pt; font-family: Courier; color: white}")
self.no_streamlines_label = QLabel()
self.no_streamlines_label.setText("\nNo of streamlines: ")
self.no_streamlines_label.setStyleSheet("QLabel {font-size: 10pt; font-family: Courier; color: white}")
self.dev_no_streamlines_label = QLabel()
self.dev_no_streamlines_label.setText("\nDeviation No of streamlines: ")
self.dev_no_streamlines_label.setStyleSheet("QLabel {font-size: 10pt; font-family: Courier; color: white}")
self.no_voxels_label = QLabel()
self.no_voxels_label.setText("\nNo of voxels: ")
self.no_voxels_label.setStyleSheet("QLabel {font-size: 10pt; font-family: Courier; color: white}")
self.dev_no_voxels_label = QLabel()
self.dev_no_voxels_label.setText("\nDeviation No of voxels: ")
self.dev_no_voxels_label.setStyleSheet("QLabel {font-size: 10pt; font-family: Courier; color: white}")
self.predBox.addWidget(self.max_length_label)
self.predBox.addWidget(self.min_length_label)
self.predBox.addWidget(self.fa_label)
self.predBox.addWidget(self.dev_fa_label)
self.predBox.addWidget(self.no_streamlines_label)
self.predBox.addWidget(self.dev_no_streamlines_label)
self.predBox.addWidget(self.no_voxels_label)
self.predBox.addWidget(self.dev_no_voxels_label)
self.predBox.addStretch(2)
self.statBox.setLayout(self.predBox)
def VerticalLayout(self):
# root = tk.Tk()
# helv36 = tkFont.Font(root, family = "Helvetica",size = 36,weight = "bold")
# tkFont.families()
self.vMain = Qt.QHBoxLayout() ## Added horizontal box layout
self.groupBox1 = QGroupBox("Select Whole Brain")
whole_brain = []
for (dirpath, dirnames, filenames) in os.walk("WholeBrains"):
whole_brain.extend(filenames)
break
self.combo_box1 = QComboBox()
self.combo_box1.addItems(whole_brain)
button_box1 = QPushButton('Choose Directory')
## Creating font with QtFont
# font = QtGui.QFont()
# font.setFamily("Helvetica")
# font.setBold(True)
# button_box1.setFont(font)
button_box2 = QPushButton("Load The Whole Brain")
button_box2.clicked.connect(self.clickMethod)
button_box1.clicked.connect(self.openFileNameDialog)
##### Adjusting Color and font
self.groupBox1.setStyleSheet("background-color: #006868; font-size: 13pt; font-weight: bold; font-family: Arial; color: white")
self.combo_box1.setStyleSheet("QComboBox {background-color: #005151; font-size: 12pt; font-family: Courier; color: white}")
button_box1.setStyleSheet("background-color: #00A0A0; font-size: 13pt; font-family: Arial; color: white")
button_box2.setStyleSheet("background-color: #72d874; font-size: 13pt; font-family: Arial; color: white")
vbox = Qt.QVBoxLayout()
vbox.addWidget(self.combo_box1)
vbox.addWidget(button_box1)
vbox.addWidget(button_box2)
vbox.addStretch(1)
self.groupBox1.setLayout(vbox)
#### Trying to add another GroupBox
self.createTrainingBox = QGroupBox("Make Your Own Training Set")
tract_files = []
for (dirpath, dirnames, filenames) in os.walk("Tracts/AF/Left"):
tract_files.extend(filenames)
break
self.training_tract_combo = QComboBox()
self.training_tract_combo.addItems(tract_files)
training_tract_button = QPushButton('Choose Your Tract')
training_button = QPushButton('Train Me!')
self.createTrainingBox.setStyleSheet("background-color: #006868; font-size: 13pt; font-weight: bold; font-family: Arial; color: white")
self.training_tract_combo.setStyleSheet("QComboBox {background-color: #005151; font-size: 12pt; font-family: Courier; color: white}")
training_tract_button.setStyleSheet("background-color: #00A0A0; font-size: 13pt; font-family: Arial; color: white")
training_button.setStyleSheet("background-color: #00A0AA; font-size: 13pt; font-family: Arial; color: white")
#### Method Radio Button
method_label = QLabel()
method_label.setText("\nSelect a method: ")
self.ocsvm_radio = QRadioButton("One Class SVM")
self.ocsvm_radio.setChecked(True)
self.kd_radio = QRadioButton("KD Tree")
button = QPushButton("Finish!")
button.setStyleSheet("background-color: #20a949; font-size: 13pt; font-family: Arial; color: white")
button.clicked.connect(self.clickMethod2)
training_tract_box = Qt.QVBoxLayout()
training_tract_box.addWidget(self.training_tract_combo)
training_tract_box.addWidget(training_tract_button)
training_tract_box.addWidget(training_button)
training_tract_box.addWidget(method_label)
training_tract_box.addWidget(self.ocsvm_radio)
training_tract_box.addWidget(self.kd_radio)
training_tract_box.addWidget(button)
training_tract_box.addStretch(1)
##################################
self.createTrainingBox.setLayout(training_tract_box)
####################################################
groupBox3 = QGroupBox("Use Pre-Trained Tracts")
groupBox3.setStyleSheet("background-color: #006868")
self.tractsName = ["AF", "CG", "UF"]
self.tract_box = QPushButton("Done!")
self.tract_box.clicked.connect(self.openTractFilesDialog)
self.combo = QComboBox()
self.combo.addItems(self.tractsName)
self.radio1 = QRadioButton("Left tract")
self.radio1.setChecked(True)
self.radio2 = QRadioButton("Right tract")
self.radio3 = QRadioButton("None")
groupBox3.setStyleSheet("background-color: #006868; font-size: 13pt; font-weight:bold; font-family: Arial; color: white")
self.tract_box.setStyleSheet("background-color: #00A82d; font-size: 13pt; font-family: Arial; color: white")
self.combo.setStyleSheet("background-color: #005151; font-size: 13pt; font-family: Arial; color: white")
self.radio1.setStyleSheet("background-color: #006868; font-size: 12pt; font-family: Arial; color: white")
self.radio2.setStyleSheet("background-color: #006868; font-size: 12pt; font-family: Arial; color: white")
self.radio3.setStyleSheet("background-color: #006868; font-size: 12pt; font-family: Arial; color: white")
vbox3 = Qt.QVBoxLayout()
vbox3.addWidget(self.combo)
vbox3.addWidget(self.radio1)
vbox3.addWidget(self.radio2)
vbox3.addWidget(self.radio3)
vbox3.addWidget(self.tract_box)
vbox3.addStretch(1)
groupBox3.setLayout(vbox3)
self.vMain.addWidget(self.groupBox1)
self.vMain.addWidget(groupBox3)
self.vMain.addWidget(self.createTrainingBox)
#verbox.addLayout(self.vMain)
def __init__(self, parent = None):
Qt.QMainWindow.__init__(self, parent)
####################### menubar###############
title = "FastTractSeg"
########## Took screen width and height to adjust in any screen ##########
root = tk.Tk()
width = root.winfo_screenwidth()
height = root.winfo_screenheight()
#print(width, height)
top = width // 6
left = height // 6
width = (width // 6) * 4
height = (height // 6) * 4
# menubar = self.menuBar()
# fileMenu = menubar.addMenu('File ')
# newAct = QAction('New', self)
# fileMenu.addAction(newAct)
##### Set your icon
icon_path = './Tract-Segmentation-Visualizer/icon.png'
if (QtCore.QFile.exists( icon_path )):
self.setWindowIcon(QIcon(icon_path))
#self.setIconSize(QtCore.QSize(0,0))
self.setWindowTitle(title)
self.setGeometry(top,left, width, height)
# Set window background color
self.setAutoFillBackground(True)
p = self.palette()
print(QtCore.Qt.black)
p.setColor(self.backgroundRole(), QtCore.Qt.darkCyan) ## Set the background color
self.setPalette(p)
####################### Pyqt###############
########## frame##################
self.frame = Qt.QFrame()
self.VerticalLayout()
self.HorizontalLayout()
self.vl = Qt.QVBoxLayout()
self.vl.addLayout(self.vMain)
self.vl.addWidget(self.groupBox2)
###### Adding Horizontal Box
self.vbox = Qt.QHBoxLayout()
self.vbox.addLayout(self.vl)
self.vbox.addWidget(self.statBox)
self.frame.setLayout(self.vbox)
self.setCentralWidget(self.frame)
self.setLayout(self.vl)
self.show()
if __name__ == "__main__":
app = Qt.QApplication(sys.argv)
window = MainWindow()
app.exec()
#sys.exit(app.exec_())