I2T2.io loads and saves common file formats used in medical imaging.
# test
try:
import gdcm
print('Successfully imported GDCM')
except ImportError:
print('Could not import GDCM')
Usage
dicom_dataframe
takes a path to a folder containing the dicom files of interest.
It then constructs a pandas dataframe whose first column (DS) is the result of pydicom.dcmread for each file encountered.
import pkg_resources
DATA_ROOT = pkg_resources.resource_filename('I2T2', 'data')
data_path = os.path.join(DATA_ROOT,'knee')
# to read a single, random dicom file from the entire directory
dcm_df = dicom_dataframe(path_to_dicom_dir=data_path, dicom_extension='DCM', read_single_random_dcm=True)
dcm_df.dataframe.head(4)
# to read all the dicoms and save them to a pandas dataframe
dcm_df = dicom_dataframe(path_to_dicom_dir=data_path)
dcm_df.dataframe.head(4)
The dicom dataframe comes pre-populated with a few values of interest. We can add additional keys by using the populate_dataframe
function:
dcm_df.populate_dataframe(['SOPClassUID'])
dcm_df.dataframe.head(4)
We can also filter the dataframe by a key. For instance, below we select only the first Series found:
series_list = dcm_df.dataframe['SeriesInstanceUID'].unique()
ID_of_first_series = series_list[0]
dcm_df.filter_dataframe_by_column_value(column='SeriesInstanceUID', value=ID_of_first_series)
Finally, we can also get pixel data sorted by ImagePositionPatient
pixel_data = dcm_df.get_pixel_data()
plt.imshow(pixel_data[:, :, 6])
plt.show()
I2T2 can also load X-rays. A warning will tell us that ImagePositionPatient
was not found but I2T2 will just ignore it since no slice sorting is needed.
chest_xray_path = os.path.join(DATA_ROOT,'chest-xray')
dcm_df = dicom_dataframe(path_to_dicom_dir=chest_xray_path, dicom_extension='dcm')
pixel_data = dcm_df.get_pixel_data()
plt.title('Chest X-Ray example from TCIA')
plt.imshow(pixel_data[:,:,0])
plt.yticks([])
plt.xticks([])
plt.show()
#test if Xray is loading correctly
dcm_df = dicom_dataframe(path_to_dicom_dir=chest_xray_path, dicom_extension='dcm')
pixel_data = dcm_df.get_pixel_data()
assert(pixel_data.shape == (2022, 2022, 1))
#test
#make sure dataframe is ordered correctly
data_path = os.path.join(DATA_ROOT,'knee')
dcm_df = dicom_dataframe(path_to_dicom_dir=data_path, dicom_extension='dcm')
dcm_df.sort_dataframe_by_IPP_normal()
for i in range(len(dcm_df.dataframe) - 1):
assert(dcm_df.dataframe['ImagePositionPatient_normal'].iloc[i+1] > dcm_df.dataframe['ImagePositionPatient_normal'].iloc[i])
#test
np.testing.assert_equal(_get_normal_from_dicom_slice(dcm_df.dataframe['DS'].iloc[0]), np.array([-1., -0., 0.]))
#tests
data_path = os.path.join(DATA_ROOT,'knee')
dcm_df = dicom_dataframe(path_to_dicom_dir=data_path, dicom_extension='dcm')
assert(is_fat_suppressed(dcm_df.dataframe['DS'].iloc[0]) == True)
assert(is_sagittal(dcm_df.dataframe['DS'].iloc[0]) == True)
assert(is_axial(dcm_df.dataframe['DS'].iloc[0]) == False)
assert(is_coronal(dcm_df.dataframe['DS'].iloc[0]) == False)
segmentation_path = os.path.join(DATA_ROOT,'knee-segmentation','knee_segmentation.h5')
h5_file = load_h5(
path_to_h5_file=segmentation_path)
# Plot
plt.figure(figsize=(10, 10))
plt.subplot(121)
plt.title('Image')
plt.imshow(h5_file['img'][:, :, 30])
plt.subplot(122)
plt.title('Segmentation image')
plt.imshow(h5_file['seg'][:, :, 30])
plt.show()