-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverview2.py
97 lines (77 loc) · 3.56 KB
/
overview2.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
from util import *
from setting import *
det_class_path = os.path.join(DS_dir, 'stage_1_detailed_class_info.csv')
bbox_path = os.path.join(DS_dir, 'stage_1_train_labels.csv')
dicom_dir = os.path.join(DS_dir, 'stage_1_train_images/')
# detailed_class_info:{
# index
# patientId
# class(labels)
# }
# train_labels:{
# index
# patientId
# x
# y
# width
# height
# Target
# }
det_class_df = pd.read_csv(det_class_path)
det_class_df.groupby('class').size().plot.bar()
bbox_df = pd.read_csv(bbox_path)
comb_bbox_df = pd.merge(bbox_df, det_class_df, how='inner', on='patientId')
comb_bbox_df = pd.concat([bbox_df, det_class_df.drop('patientId', 1)], 1)
box_df = comb_bbox_df.groupby('patientId').size().reset_index(name='boxes')
comb_bbox_df = pd.merge(comb_bbox_df, box_df, on='patientId')
box_df.groupby('boxes').size().reset_index(name='patients')
comb_bbox_df.groupby(['class', 'Target']).size().reset_index(name='Patient Count')
print(glob(os.path.join(dicom_dir, '*.dcm')))
image_df = pd.DataFrame({'path': glob(os.path.join(dicom_dir, '*.dcm'))})
image_df['patientId'] = image_df['path'].map(\
lambda x: os.path.splitext(os.path.basename(x))[0])
img_pat_ids = set(image_df['patientId'].values.tolist())
box_pat_ids = set(comb_bbox_df['patientId'].values.tolist())
# assert img_pat_ids.union(box_pat_ids) == img_pat_ids, 'Patient IDs should be the same'
DCM_TAG_LIST = ['PatientAge', 'BodyPartExamined', 'ViewPosition', 'PatientSex']
def get_tags(in_path):
c_dicom = pydicom.read_file(in_path, stop_before_pixels=True)
tag_dict = {c_tag: getattr(c_dicom, c_tag, '') for c_tag in DCM_TAG_LIST}
tag_dict['path'] = in_path
return pd.Series(tag_dict)
image_meta_df = image_df.apply(lambda x: get_tags(x['path']), 1)
image_meta_df['PatientAge'] = image_meta_df['PatientAge'].map(int)
# image_meta_df['PatientAge'].hist()
image_meta_df.drop('path', 1).describe(exclude=np.number)
image_full_df = pd.merge(image_df, image_meta_df, on='path')
image_bbox_df = pd.merge(comb_bbox_df, image_full_df, on='patientId')
sample_df = image_bbox_df.groupby(['Target', 'class', 'boxes']).apply(\
lambda x: x[x['patientId']==x.sample(1)['patientId'].values[0]]).reset_index(drop=True)
fig, m_axs = plt.subplots(2, 3, figsize=(20, 10))
for c_ax, (c_path, c_rows) in zip(m_axs.flatten(), sample_df.groupby(['path'])):
c_dicom = pydicom.read_file(c_path)
c_ax.imshow(c_dicom.pixel_array, cmap='bone')
c_ax.set_title('{class}'.format(**c_rows.iloc[0,:]))
for i, (_, c_row) in enumerate(c_rows.dropna().iterrows()):
c_ax.plot(c_row['x'], c_row['y'], 's', label='{class}'.format(**c_row))
c_ax.add_patch(Rectangle(xy=(c_row['x'], c_row['y']), width=c_row['width'],\
height=c_row['height'], alpha=0.5))
if i==0:
c_ax.legend()
pos_bbox = image_bbox_df.query('Target==1')
pos_bbox.plot.scatter(x='x', y='y')
fig, ax1 = plt.subplots(1, 1, figsize=(10, 10))
ax1.set_xlim(0, 1024)
ax1.set_ylim(0, 1024)
for _, c_row in pos_bbox.sample(1000).iterrows():
ax1.add_patch(Rectangle(xy=(c_row['x'], c_row['y']), width=c_row['width'], height=c_row['height'], alpha=5e-3))
X_STEPS, Y_STEPS = 1024, 1024
xx, yy = np.meshgrid(np.linespace(0, 1024, X_STEPS), np.linespace(0, 1024, Y_STEPS), indexing='xy')
prob_image = np.zeros_like(xx)
for _, c_row in pos_bbox.sample(5000).iterrows():
c_mask = (xx >= c_row['x']) & (xx <= (c_row['x'] + c_row['width']))
c_mask &= (yy >= c_row['y']) & (yy <= c_row['y'] + c_row['height'])
prob_image += c_mask
fig, ax1 = plt.subplots(1, 1, figsize=(10, 10))
ax1.imshow(prob_image, cmap='hot')
plt.show()