explore
Module for exploring data
from fastai.vision.all import *
from fastai.medical.imaging import *
from torchvision.utils import save_image
matplotlib.rcParams['image.cmap'] = 'bone'
from fmi.explore import *
from fmi.pipeline import *
system_info
is a quick and convenient way of relevant information about what version of fastai
, fastcore
, torch
, pydicom
, kornia
cuda
and what gpu you are using
system_info()
Many dicom datasets have multiple slices of images for the same patient. For example with this dataset each folder represents a single patient and each folder can have multiple slices.
s = 'D:/Datasets/osic-pulmonary-fibrosis-progression/train/'
sort_items = get_dicom_files(s, folders='ID00007637202177411956430')
sort_items
In this example there are 30 images within the folder. To view them:
imgs = []
for filename in sort_items:
file = dcmread(filename).pixel_array
img = TensorDicom(file)
imgs.append(img)
show_images(imgs, nrows=3)
We notice that the images are not in sequence. Dicoms typically have an InstanceNumber
which can be used to display the images in sequence.
def instance_sort(folder:(Path, L)):
"Helper to sort files by instance number"
if isinstance(folder, Path): folder = get_dicom_files(folder)
if isinstance(folder, L): folder = folder
sorted_files = []
for file in folder:
instance = dcmread(file)[0x20, 0x13].value
sorted_files.append([instance, file])
return L(sorted(sorted_files))
def instance_dcmread(folder:(L)):
"instance dcmread"
file = [dcmread(o[1]) for o in folder]
return file
def instance_show(folder: (L), nrows=1):
"Helper to display sorted files by instance number"
f_list = []; t_list = []
for file in instance_sort(folder):
f = TensorDicom(dcmread(file[1]).pixel_array)
f_list.append(f); t_list.append(file[0])
return show_images(f_list, titles=t_list, nrows=nrows)
We can simply view the images in sequence by using instance_show
instance_show(sort_items, nrows=3)
fastai
has a handy method from.dicoms
that can access dicom metadata and display this in a dataframe.
m_items = get_dicom_files('D:/Datasets/osic-pulmonary-fibrosis-progression/train/ID00007637202177411956430')
source = 'D:/Datasets/osic-pulmonary-fibrosis-progression/train/'
dicom_dataframe = pd.DataFrame.from_dicoms(m_items)
dicom_dataframe[:2]
def get_dicom_image(df, key, nrows=3, source=None):
"Helper to view images by key"
imgs=[]
title=[]
for i in df.index:
file_path = f"{source}/{df.iloc[i]['PatientID']}/{df.iloc[i]['InstanceNumber']}.dcm"
dcc = dcmread(file_path).pixel_array
imgs.append(dcc)
pct = df.iloc[i][key]
title.append(pct)
return show_images(imgs, titles=title, nrows=nrows)
get_dicom_image
easily allows to view images based on the chosen dataframe key. For example we can reduce the dataframe created earlier.
pct = dicom_dataframe[['PatientID', 'InstanceNumber', 'img_pct_window', 'img_mean', 'img_std']].sort_values(by=['img_pct_window'], ascending=False).reset_index(drop=True)
pct[:5]
View images by img_pct_window
get_dicom_image(pct[:30], 'img_pct_window', source=source)
View by img_mean
mean = dicom_dataframe[['PatientID', 'InstanceNumber', 'img_pct_window', 'img_mean', 'img_std']].sort_values(by=['img_mean'], ascending=False).reset_index(drop=True)
mean[:5]
get_dicom_image(mean[:30], 'img_mean', source=source)