|
a |
|
b/sandbox/test_data.py |
|
|
1 |
import matplotlib.pyplot as plt |
|
|
2 |
import numpy as np |
|
|
3 |
import scipy |
|
|
4 |
import scipy.ndimage |
|
|
5 |
import data_transforms |
|
|
6 |
import pathfinder |
|
|
7 |
import utils |
|
|
8 |
import utils_lung |
|
|
9 |
import logger |
|
|
10 |
import sys |
|
|
11 |
import collections |
|
|
12 |
|
|
|
13 |
|
|
|
14 |
def resample(image, spacing, new_spacing=[1, 1, 1]): |
|
|
15 |
# Determine current pixel spacing |
|
|
16 |
spacing = np.array(spacing) |
|
|
17 |
resize_factor = spacing / new_spacing |
|
|
18 |
new_real_shape = image.shape * resize_factor |
|
|
19 |
new_shape = np.round(new_real_shape) |
|
|
20 |
real_resize_factor = new_shape / image.shape |
|
|
21 |
new_spacing = spacing / real_resize_factor |
|
|
22 |
image = scipy.ndimage.interpolation.zoom(image, real_resize_factor) |
|
|
23 |
return image, new_spacing |
|
|
24 |
|
|
|
25 |
|
|
|
26 |
def plot_2d(image3d, axis, pid, img_dir): |
|
|
27 |
fig = plt.figure() |
|
|
28 |
fig.canvas.set_window_title(pid) |
|
|
29 |
ax = fig.add_subplot(111) |
|
|
30 |
idx = image3d.shape[axis] / 2 |
|
|
31 |
if axis == 0: # sax |
|
|
32 |
ax.imshow(image3d[idx, :, :], cmap=plt.cm.gray) |
|
|
33 |
if axis == 1: # 2 lungs |
|
|
34 |
ax.imshow(image3d[:, idx, :], cmap=plt.cm.gray) |
|
|
35 |
if axis == 2: # side view |
|
|
36 |
ax.imshow(image3d[:, :, idx], cmap=plt.cm.gray) |
|
|
37 |
plt.show() |
|
|
38 |
fig.savefig(img_dir + '/%s.png' % pid, bbox_inches='tight') |
|
|
39 |
fig.clf() |
|
|
40 |
plt.close('all') |
|
|
41 |
|
|
|
42 |
|
|
|
43 |
def test1(): |
|
|
44 |
image_dir = utils.get_dir_path('analysis', pathfinder.METADATA_PATH) |
|
|
45 |
image_dir = image_dir + '/test_1/' |
|
|
46 |
utils.auto_make_dir(image_dir) |
|
|
47 |
|
|
|
48 |
sys.stdout = logger.Logger(image_dir + '/%s.log' % 'test1_log') |
|
|
49 |
sys.stderr = sys.stdout |
|
|
50 |
|
|
|
51 |
patient_data_paths = utils_lung.get_patient_data_paths(pathfinder.DATA_PATH) |
|
|
52 |
print len(patient_data_paths) |
|
|
53 |
|
|
|
54 |
for k, p in enumerate(patient_data_paths): |
|
|
55 |
pid = utils_lung.extract_pid_dir(p) |
|
|
56 |
try: |
|
|
57 |
sid2data, sid2metadata = utils_lung.get_patient_data(p) |
|
|
58 |
sids_sorted = utils_lung.sort_sids_by_position(sid2metadata) |
|
|
59 |
sids_sorted_jonas = utils_lung.sort_slices_jonas(sid2metadata) |
|
|
60 |
sid2position = utils_lung.slice_location_finder(sid2metadata) |
|
|
61 |
|
|
|
62 |
try: |
|
|
63 |
slice_thickness_pos = np.abs(sid2metadata[sids_sorted[0]]['ImagePositionPatient'][2] - |
|
|
64 |
sid2metadata[sids_sorted[1]]['ImagePositionPatient'][2]) |
|
|
65 |
except: |
|
|
66 |
print 'This patient has no ImagePosition!' |
|
|
67 |
slice_thickness_pos = 0. |
|
|
68 |
try: |
|
|
69 |
slice_thickness_loc = np.abs( |
|
|
70 |
sid2metadata[sids_sorted[0]]['SliceLocation'] - sid2metadata[sids_sorted[1]]['SliceLocation']) |
|
|
71 |
except: |
|
|
72 |
print 'This patient has no SliceLocation!' |
|
|
73 |
slice_thickness_loc = 0. |
|
|
74 |
|
|
|
75 |
jonas_slicethick = [] |
|
|
76 |
for i in xrange(len(sids_sorted_jonas) - 1): |
|
|
77 |
s = np.abs(sid2position[sids_sorted_jonas[i + 1]] - sid2position[sids_sorted_jonas[i]]) |
|
|
78 |
jonas_slicethick.append(s) |
|
|
79 |
|
|
|
80 |
full_img = np.stack([data_transforms.ct2normHU(sid2data[sid], sid2metadata[sid]) for sid in sids_sorted]) |
|
|
81 |
del sid2data, sid2metadata |
|
|
82 |
print np.min(full_img), np.max(full_img) |
|
|
83 |
# spacing = sid2metadata[sids_sorted[0]]['PixelSpacing'] |
|
|
84 |
# spacing = [slice_thickness, spacing[0], spacing[1]] |
|
|
85 |
# resampled_image, _ = resample(full_img, spacing) |
|
|
86 |
plot_2d(full_img, axis=0, pid=pid + 'ax0', img_dir=image_dir) |
|
|
87 |
plot_2d(full_img, axis=1, pid=pid + 'ax1', img_dir=image_dir) |
|
|
88 |
plot_2d(full_img, axis=2, pid=pid + 'ax2', img_dir=image_dir) |
|
|
89 |
print k, pid, full_img.shape, slice_thickness_pos, slice_thickness_loc, set(jonas_slicethick) |
|
|
90 |
del full_img |
|
|
91 |
except: |
|
|
92 |
print 'exception!!!', pid |
|
|
93 |
|
|
|
94 |
|
|
|
95 |
def test2(): |
|
|
96 |
patient_data_paths = utils_lung.get_patient_data_paths(pathfinder.DATA_PATH) |
|
|
97 |
print len(patient_data_paths) |
|
|
98 |
pixel_spacings_xy = [] |
|
|
99 |
n_slices = [] |
|
|
100 |
|
|
|
101 |
for k, p in enumerate(patient_data_paths): |
|
|
102 |
pid = utils_lung.extract_pid_dir(p) |
|
|
103 |
sid2data, sid2metadata = utils_lung.get_patient_data(p) |
|
|
104 |
mtd = sid2metadata.itervalues().next() |
|
|
105 |
|
|
|
106 |
assert mtd['PixelSpacing'][0] == mtd['PixelSpacing'][1] |
|
|
107 |
pixel_spacings_xy.append(mtd['PixelSpacing'][0]) |
|
|
108 |
n_slices.append(len(sid2metadata)) |
|
|
109 |
print pid, pixel_spacings_xy[-1], n_slices[-1] |
|
|
110 |
|
|
|
111 |
print 'nslices', np.max(n_slices), np.min(n_slices), np.mean(n_slices) |
|
|
112 |
counts = collections.Counter(pixel_spacings_xy) |
|
|
113 |
new_list = sorted(pixel_spacings_xy, key=counts.get, reverse=True) |
|
|
114 |
print 'spacing', new_list |
|
|
115 |
|
|
|
116 |
|
|
|
117 |
if __name__ == '__main__': |
|
|
118 |
test1() |