forked from FBK-NILab/tractome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
guillotine.py
216 lines (176 loc) · 5.98 KB
/
guillotine.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
# -*- coding: utf-8 -*-
"""This is the Volume Slicer Actor, to visualize a 3D volumetric image
of the head as slices.
Copyright (c) 2012-2014, Emanuele Olivetti and Eleftherios Garyfallidis
Distributed under the BSD 3-clause license. See COPYING.txt.
"""
import numpy as np
from fos import Window, Scene
from fos.actor.slicer import Slicer
from pyglet.gl import *
from fos.coords import rotation_matrix, from_matvec
from fos import Init, Run
from PySide.QtCore import Qt
import copy
class Guillotine(Slicer):
""" Volume Slicer Actor
Notes
------
Coordinate Systems
http://www.grahamwideman.com/gw/brain/orientation/orientterms.htm
http://www.slicer.org/slicerWiki/index.php/Coordinate_systems
http://eeg.sourceforge.net/mri_orientation_notes.html
"""
def __init__(self, name, data, affine,
convention='RAS', look='anteriorz+'):
""" Volume Slicer that supports medical conventions
Parameters
----------
name : str
data : array, shape (X, Y, Z) or (X, Y, Z, 3) or (X, Y, Z, 4)
affine : array, shape (4, 4)
convention : str,
'RAS' for neurological,
'LAS' for radiological (default)
look : str,
'anteriorz+' look in the subject from the front
"""
data[np.isnan(data)] = 0
data = np.interp(data, [data.min(), data.max()], [0, 255])
data = data.astype(np.ubyte)
"""
if convention == 'RAS' and look == 'anteriorz+':
axis = np.array([1, 0, 0.])
theta = -90.
post_mat = from_matvec(rotation_matrix(axis, theta))
axis = np.array([0, 0, 1.])
theta = -90.
post_mat = np.dot(
from_matvec(rotation_matrix(axis, theta)),
post_mat)
"""
post_mat = np.eye(4)
super(Guillotine, self).__init__(name, data, affine, convention, post_mat)
def right2left(self, step):
if self.i + step < self.I:
self.slice_i(self.i + step)
else:
self.slice_i(self.I - 1)
def left2right(self, step):
if self.i - step >= 0:
self.slice_i(self.i - step)
else:
self.slice_i(0)
def inferior2superior(self, step):
if self.k + step < self.K:
self.slice_k(self.k + step)
else:
self.slice_k(self.K - 1)
def superior2inferior(self, step):
if self.k - step >= 0:
self.slice_k(self.k - step)
else:
self.slice_k(0)
def anterior2posterior(self, step):
if self.j + step < self.J:
self.slice_j(self.j + step)
else:
self.slice_j(self.J - 1)
def posterior2anterior(self, step):
if self.j - step >= 0:
self.slice_j(self.j - step)
else:
self.slice_j(0)
def reset_slices(self):
self.slice_i(self.I / 2)
self.slice_j(self.J / 2)
self.slice_k(self.K / 2)
def slices_ijk(self, i, j, k):
self.slice_i(i)
self.slice_j(j)
self.slice_k(k)
def show_coronal(self, bool=True):
self.show_k = bool
def show_axial(self, bool=True):
self.show_i = bool
def show_saggital(self, bool=True):
self.show_j = bool
def show_all(self, bool=True):
self.show_i = bool
self.show_j = bool
self.show_k = bool
def process_messages(self, messages):
msg=messages['key_pressed']
#print 'Processing messages in actor', self.name,
#' key_press message ', msg
if msg!=None:
self.process_keys(msg,None)
def process_keys(self, symbol, modifiers):
"""Bind actions to key press.
"""
if symbol == Qt.Key_Left:
print 'Left'
if self.i < self.data.shape[0]:
self.slice_i(self.i+1)
else:
self.slice_i(0)
if symbol == Qt.Key_Left:
print 'Left'
if self.i < self.data.shape[0]:
self.slice_i(self.i+1)
else:
self.slice_i(0)
if symbol == Qt.Key_Right:
print 'Right'
if self.i >=0:
self.slice_i(self.i-1)
else:
self.slice_i(self.data.shape[0]-1)
if symbol == Qt.Key_Up:
print 'Superior'
if self.k < self.data.shape[2]:
self.slice_k(self.k+1)
else:
self.slice_k(0)
if symbol == Qt.Key_Down:
print 'Interior'
if self.k >= 0:
self.slice_k(self.k-1)
else:
self.slice_k(self.data.shape[2]-1)
if symbol == Qt.Key_PageUp:
print 'Anterior'
if self.j < self.data.shape[1]:
self.slice_j(self.j+1)
else:
self.slice_j(0)
if symbol == Qt.Key_PageDown:
print 'Posterior'
if self.j >= 0:
self.slice_j(self.j-1)
else:
self.slice_j(self.data.shape[1]-1)
if symbol == Qt.Key_0:
self.show_i = not self.show_i
self.show_j = not self.show_j
self.show_k = not self.show_k
if symbol == Qt.Key_1:
self.show_i = not self.show_i
if symbol == Qt.Key_2:
self.show_j = not self.show_j
if symbol == Qt.Key_3:
self.show_k = not self.show_k
# if symbol == Qt.Key_R:
# self.slice_i(self.I / 2)
# self.slice_j(self.J / 2)
# self.slice_k(self.K / 2)
def anteriorzplus(xyz):
axis = np.array([1, 0, 0.])
theta = -90.
post_mat = from_matvec(rotation_matrix(axis, theta))
axis = np.array([0, 1, 0])
theta = 180.
post_mat = np.dot(
from_matvec(rotation_matrix(axis, theta)),
post_mat)
return np.dot(post_mat[:3, :3], xyz.T).T