forked from yu02019/BEN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualization.py
220 lines (183 loc) · 8.06 KB
/
visualization.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
import os
import numpy as np
from glob import glob
from utils.load_data import get_itk_array
from utils.transparent_imshow import transp_imshow
import matplotlib.pyplot as plt
def load_slice_cross_species(raw_path='', zeroshot_path='', ft_path='', DA_path='', gt_path='',
scans_num=0, disp_num=6):
"""
:param raw_path:
:param zeroshot_path:
:param ft_path:
:param DA_path:
:param gt_path:
:param scans_num:
:param disp_num:
:return:
"""
# os.chdir(r'G:\\2020_01_17\\G\\gmycode\\unet-BET_pm2.5\\unet-github') # for debug only
raw = glob(raw_path + '/*');
raw.sort()
zeroshot = glob(zeroshot_path + '/*');
zeroshot.sort()
ft = glob(ft_path + '/*');
ft.sort()
DA = glob(DA_path + '/*');
DA.sort()
gt = glob(gt_path + '/*');
gt.sort()
raw = get_itk_array(raw[scans_num])
zeroshot = get_itk_array(zeroshot[scans_num])
ft = get_itk_array(ft[scans_num])
DA = get_itk_array(DA[scans_num])
gt = get_itk_array(gt[scans_num])
disp_num_list = list(range(0, raw.shape[0], raw.shape[0] // (disp_num)))
# extract slices
raw = raw[disp_num_list]
zeroshot = zeroshot[disp_num_list]
ft = ft[disp_num_list]
DA = DA[disp_num_list]
gt = gt[disp_num_list]
return raw, zeroshot, ft, DA, gt
def load_slice(raw_path='', zeroshot_path='', DA_path='', scans_num=0, disp_num=6):
"""
:param raw_path:
:param zeroshot_path:
:param DA_path:
:param scans_num:
:param disp_num:
:return:
"""
# os.chdir(r'G:\\2020_01_17\\G\\gmycode\\unet-BET_pm2.5\\unet-github') # for debug only
raw = glob(raw_path + '/*');
raw.sort()
zeroshot = glob(zeroshot_path + '/*');
zeroshot.sort()
DA = glob(DA_path + '/*');
DA.sort()
raw = get_itk_array(raw[scans_num])
zeroshot = get_itk_array(zeroshot[scans_num])
DA = get_itk_array(DA[scans_num])
disp_num_list = list(range(0, raw.shape[0], raw.shape[0] // (disp_num)))
# extract slices
raw = raw[disp_num_list]
zeroshot = zeroshot[disp_num_list]
DA = DA[disp_num_list]
return raw, zeroshot, DA
def plot_segmentation_cross_species(raw, zeroshot, ft, DA, gt, task, hspace=-0.6, wspace=0.0, figsize=None):
"""
:param raw:
:param zeroshot:
:param ft:
:param DA:
:param gt:
:param task:
:param hspace: float, optional. The height of the padding between subplots, as a fraction of the average axes height.
:return:
"""
fig = plt.figure(
figsize=figsize
)
timepoint_count = raw.shape[0]
for i in range(timepoint_count):
plt.subplot(5, timepoint_count, i + 1)
plt.imshow(raw[i], cmap='gray')
# plt.title('No.{} scan\n'.format(i + 1), fontsize=8)
plt.xticks([]), plt.yticks([])
for i in range(timepoint_count):
plt.subplot(5, timepoint_count, timepoint_count + i + 1)
plt.imshow(raw[i], cmap='gray')
transp_imshow(zeroshot[i], cmap='Reds', alpha=0.6)
# plt.title('No.{} scan lung\n'.format(i + 1), fontsize=8)
plt.xticks([]), plt.yticks([])
for i in range(timepoint_count):
plt.subplot(5, timepoint_count, timepoint_count * 2 + i + 1)
plt.imshow(raw[i], cmap='gray')
transp_imshow(ft[i], cmap='Reds', alpha=0.6)
# plt.title('No.{} scan lung\n'.format(i + 1), fontsize=16)
plt.xticks([]), plt.yticks([])
for i in range(timepoint_count):
plt.subplot(5, timepoint_count, timepoint_count * 3 + i + 1)
plt.imshow(raw[i], cmap='gray')
transp_imshow(DA[i], cmap='Reds', alpha=0.6)
# plt.title('No.{} scan lesion\n'.format(i + 1), fontsize=16)
plt.xticks([]), plt.yticks([])
# gt
for i in range(timepoint_count):
plt.subplot(5, timepoint_count, timepoint_count * 4 + i + 1)
plt.imshow(raw[i], cmap='gray')
transp_imshow(gt[i], cmap='Oranges', alpha=0.6)
# plt.title('No.{} scan lesion\n'.format(i + 1), fontsize=16)
plt.xticks([]), plt.yticks([])
plt.subplots_adjust(hspace=hspace, wspace=wspace) # wspace=0.0
# plt.tight_layout()
fig.suptitle('Plot of different methods on cross-{} task'.format(task), fontsize=14)
# plt.savefig('result.png', dpi=300)
plt.show()
def plot_segmentation(raw, zeroshot, DA, task, hspace=-0.7, wspace=0.0, figsize=None):
"""
Each 'methods' need a set of slices for plot
Displays the segmentation results.
Parameters
----------
raw:
lung: green
lesion: red (color_map)
color_map:
task:
hspace: float, optional. The height of the padding between subplots, as a fraction of the average axes height.
"""
fig = plt.figure(
figsize=figsize
)
timepoint_count = raw.shape[0]
for i in range(timepoint_count):
plt.subplot(3, timepoint_count, i + 1)
plt.imshow(raw[i], cmap='gray')
# plt.title('No.{} scan\n'.format(i + 1), fontsize=8)
plt.xticks([]), plt.yticks([])
for i in range(timepoint_count):
plt.subplot(3, timepoint_count, timepoint_count + i + 1)
plt.imshow(raw[i], cmap='gray')
transp_imshow(zeroshot[i], cmap='Reds', alpha=0.6)
# plt.title('No.{} scan lung\n'.format(i + 1), fontsize=8)
plt.xticks([]), plt.yticks([])
for i in range(timepoint_count):
plt.subplot(3, timepoint_count, timepoint_count * 2 + i + 1)
plt.imshow(raw[i], cmap='gray')
transp_imshow(DA[i], cmap='Reds', alpha=0.6)
# plt.title('No.{} scan lesion\n'.format(i + 1), fontsize=16)
plt.xticks([]), plt.yticks([])
plt.subplots_adjust(hspace=hspace, wspace=0.0)
# plt.tight_layout()
fig.suptitle('Plot of different methods on cross-{} task'.format(task), fontsize=14)
# plt.savefig('result.png', dpi=300)
plt.show()
if __name__ == '__main__':
''' cross species '''
# raw, zeroshot, ft, DA, gt = load_slice_cross_species(raw_path=r'cross_domain\rat\src',
# zeroshot_path=r'cross_domain\\rat\\pred-Rat-42d-2022-%0-ft',
# ft_path=r'cross_domain\\rat\\pred-Rat-42d-2022-%1-ft',
# DA_path=r'cross_domain\\rat\\pred-Rat-42d-2022-%1-DA',
# gt_path=r'cross_domain\\rat\\gt', scans_num=2)
# plot_segmentation_cross_species(raw, zeroshot, ft, DA, gt, task='species')
''' cross species - postprocessing'''
# raw, zeroshot, ft, DA, gt = load_slice_cross_species(raw_path=r'cross_domain\rat\src',
# zeroshot_path=r'cross_domain\\rat\\pred-Rat-42d-2022-%0-ft',
# ft_path=r'cross_domain\\rat\\pred-Rat-42d-2022-%1-ft-post',
# DA_path=r'cross_domain\\rat\\pred-Rat-42d-2022-%1-DA-post',
# gt_path=r'cross_domain\\rat\\gt', scans_num=2)
# plot_segmentation_cross_species(raw, zeroshot, ft, DA, gt, task='species')
''' cross modality / field strengh '''
# raw, zeroshot, DA = load_slice(raw_path=r'cross_domain\epi\src',
# zeroshot_path=r'cross_domain\\epi\\pred-zeroshot',
# DA_path=r'cross_domain\\epi\\pred-02162151',
# scans_num=1)
# plot_segmentation(raw, zeroshot, DA, task='modality', hspace=-0.7)
#
# raw, zeroshot, DA = load_slice(raw_path=r'cross_domain\7T\src',
# zeroshot_path=r'cross_domain\\7T\\pred-zeroshot',
# DA_path=r'cross_domain\\7T\\pred-02162151',
# scans_num=1)
# plot_segmentation(raw, zeroshot, DA, task='field strength', )