-
Notifications
You must be signed in to change notification settings - Fork 2
/
niipre.py
executable file
·213 lines (159 loc) · 3.96 KB
/
niipre.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
#!/usr/bin/env python3
# Quick display of a Nifti image
# Packages
import nibabel as nb
import numpy as np
import os
import sys
from pathlib import Path
import matplotlib.pyplot as plt
# Disable Toolbar for plots
plt.rcParams['toolbar'] = 'None'
# Environment and file names
home = str(Path.home())
iFile = sys.argv[1]
oFile=(str(os.path.basename(iFile).replace('.nii.gz','.png').replace('.nii','.png')))
# Set rounding
np.set_printoptions(formatter={'float': lambda x: "{0:0.2f}".format(x)})
### IMPORT DATA ###
# Load data
image=nb.load(iFile)
# 3D data
if image.header['dim'][0]==3:
data=image.get_data()
# 4D data
elif image.header['dim'][0]==4:
data=image.get_data()[:,:,:,0]
# Header
header=image.header
# Set NAN to 0
data[np.isnan(data)] = 0
### PREPARE SOME PARAMETERS ###
# Spacing for Aspect Ratio
sX=header['pixdim'][1]
sY=header['pixdim'][2]
sZ=header['pixdim'][3]
# Size per slice
lX = data.shape[0]
lY = data.shape[1]
lZ = data.shape[2]
# Middle slice number
mX = int(lX/2)
mY = int(lY/2)
mZ = int(lZ/2)
# True middle point
tmX = lX/2.0
tmY = lY/2.0
tmZ = lZ/2.0
### ORIENTATION ###
qfX = image.get_qform()[0,0]
sfX = image.get_sform()[0,0]
if qfX < 0 and (sfX == 0 or sfX < 0):
oL = 'R'
oR = 'L'
elif qfX > 0 and (sfX == 0 or sfX > 0):
oL = 'L'
oR = 'R'
if sfX < 0 and (qfX == 0 or qfX < 0):
oL = 'R'
oR = 'L'
elif sfX > 0 and (qfX == 0 or qfX > 0):
oL = 'L'
oR = 'R'
### PLOTTING ###
# Plot main window
fig = plt.figure(
facecolor='black',
figsize=(5,4),
dpi=200
)
# Black background
plt.style.use('dark_background')
# Set title
fig.canvas.set_window_title(oFile.replace('.png',''))
# Coronal
ax1=fig.add_subplot(2,2,1)
imgplot = plt.imshow(
np.rot90(data[:,mY,:]),
aspect=sZ/sX,
)
imgplot.set_cmap('gray')
ax1.hlines(tmZ, 0, lX, colors='red', linestyles='dotted', linewidth=.5)
ax1.vlines(tmX, 0, lZ, colors='red', linestyles='dotted', linewidth=.5)
plt.axis('off')
# Sagittal
ax2=fig.add_subplot(2,2,2)
imgplot = plt.imshow(
np.rot90(data[mX,:,:]),
aspect=sZ/sY,
)
imgplot.set_cmap('gray')
ax2.hlines(tmZ, 0, lY, colors='red', linestyles='dotted', linewidth=.5)
ax2.vlines(tmY, 0, lZ, colors='red', linestyles='dotted', linewidth=.5)
plt.axis('off')
# Axial
ax3=fig.add_subplot(2,2,3)
imgplot = plt.imshow(
np.rot90(data[:,:,mZ]),
aspect=sY/sX
)
imgplot.set_cmap('gray')
ax3.hlines(tmY, 0, lX, colors='red', linestyles='dotted', linewidth=.5)
ax3.vlines(tmX, 0, lY, colors='red', linestyles='dotted', linewidth=.5)
plt.axis('off')
plt.text(-10, mY+5, oL, fontsize=9, color='red') # Label on left side
# Textual information
# sform code
sform=np.round(image.get_sform(),decimals=2)
sform_txt=str(sform).replace('[',' ').replace(']',' ').replace(' ',' ').replace(' -',' -')
# qform code
qform=np.round(image.get_qform(),decimals=2)
qform_txt=str(qform).replace('[',' ').replace(']',' ').replace(' ',' ').replace(' -',' -')
# Dimensions
dims=str(data.shape).replace(', ',' x ').replace('(','').replace(')','')
dim=("Dimensions: "+dims)
# Spacing
spacing=("Spacing: "
+str(np.round(sX, decimals=2))
+" x "
+str(np.round(sY, decimals=2))
+" x "
+str(np.round(sZ, decimals=2))
+" mm"
)
# Data type
type=image.header.get_data_dtype()
type_str=("Data type: "+str(type))
# Volumes
volumes=("Volumes: "+str(image.header['dim'][4]))
# Range
min=np.round(np.amin(data), decimals=2)
max=np.round(np.amax(data), decimals=2)
range=("Range: "+str(min)+" - "+str(max))
text=(
dim+"\n"
+spacing+"\n"
+volumes+"\n"
+type_str+"\n"
+range+"\n\n"
+"sform code:\n"
+sform_txt+"\n"
+"\nqform code:\n"
+qform_txt
)
# Plot text subplot
ax4=fig.add_subplot(2,2,4)
plt.text(
0.15,
0.95,
text,
horizontalalignment='left',
verticalalignment='top',
size=6,
color='white',
)
plt.axis('off')
# Adjust whitespace
plt.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)
# Display
plt.show()