Ejemplo n.º 1
0
def load_complete_data(data_path):
    """
    Load the complete data containing both i-contour and o-contour.

    :param data_path: Path to the folder with the DICOM data and labels.
    :type data_path: str
    :return: images, o_contours, i_contours
    :rtype: np.ndarray, np.ndarray, np.ndarray
    """
    def correspondig_i_contour_path(o_contour, i_contour_dir):
        """
        Get i-contour path that corresponds to the given o-contour path.

        :param o_contour: Path to the o-countour file.
        :type o_contour: str
        :param i_contour_dir: Path to the i-contour folder.
        :type i_contour_dir: str
        :return: Path to the i-contour file.
        :rtype: str
        """
        i_contour_basename = os.path.basename(o_contour).replace(
            'ocontour', 'icontour')
        return os.path.join(i_contour_dir, i_contour_basename)

    images = []
    o_contours = []
    i_contours = []
    ids_mapping = parse_csv_mapping_file(data_path)
    for dicom, contour in ids_mapping.iteritems():
        o_contour_files_path = os.path.join(data_path, COUNTOURS_FOLDER_NAME,
                                            contour, O_CONTOURS_FOLDER_NAME)
        i_contour_files_path = os.path.join(data_path, COUNTOURS_FOLDER_NAME,
                                            contour, I_CONTOURS_FOLDER_NAME)
        dicom_files_path = os.path.join(data_path, DICOMS_FOLDER_NAME, dicom)
        for contour_file_path in glob.glob(
                os.path.join(o_contour_files_path,
                             CONTOURS_EXTENSION_PATTERN)):
            o_contour_list = parse_contour_file(contour_file_path)
            i_contour_path = correspondig_i_contour_path(
                contour_file_path, i_contour_files_path)
            i_contour_list = parse_contour_file(i_contour_path)
            dicom_file_name = contour_name_to_dicom_name(
                os.path.basename(contour_file_path))
            dicom_pixels = parse_dicom_file(
                os.path.join(dicom_files_path, dicom_file_name))['pixel_data']
            height, width = dicom_pixels.shape
            images.append(dicom_pixels)
            o_contours.append(poly_to_mask(o_contour_list, width, height))
            i_contours.append(poly_to_mask(i_contour_list, width, height))
    images = np.array(images)
    o_contours = np.array(o_contours)
    i_contours = np.array(i_contours)
    assert images.shape[0] == o_contours.shape[0], \
        'There must be exactly the same number of DICOMS and o-contours.'
    assert images.shape[0] == i_contours.shape[0], \
        'There must be exactly the same number of DICOMS and i-contours.'
    return images, o_contours, i_contours
Ejemplo n.º 2
0
 def test_contour_file_to_mask(self):
     """
     Test parse contour file.
     """
     file_path = os.path.join(LOCAL_PATH, 'test_input',
                              'IM-0001-0048-icontour-manual.txt')
     result = poly_to_mask(parse_contour_file(file_path), 256, 256)
     expected = np.load(
         os.path.join(LOCAL_PATH, 'test_output',
                      'IM-0001-0048-icontour-manual.npy'))
     np.testing.assert_equal(result, expected)
Ejemplo n.º 3
0
def visualize_contour_masks_diff(o_contour_path,
                                 i_contour_path,
                                 width=256,
                                 height=256):
    """
    Visualize diff of i_contour and o_contour.

    :param o_contour_path: Path to the contour file.
    :type o_contour_path: str
    :param i_contour_path: Path to the contour file.
    :type i_contour_path: str
    :param width: Width of the contour.
    :type width: int
    :param height: Height of the contour.
    :type height: int
    """
    i_image = poly_to_mask(parse_contour_file(i_contour_path), width, height)
    o_image = poly_to_mask(parse_contour_file(o_contour_path), width, height)
    image = o_image ^ i_image
    plt.imshow(image)
    plt.show()
Ejemplo n.º 4
0
def visualize_contour_mask(contour_path, width=256, height=256):
    """
    Visualize the contour converted to mask.

    :param contour_path: Path to the contour file.
    :type contour_path: str
    :param width: Width of the contour.
    :type width: int
    :param height: Height of the contour.
    :type height: int
    """
    image = poly_to_mask(parse_contour_file(contour_path), width, height)
    plt.imshow(image)
    plt.show()
Ejemplo n.º 5
0
def visualize_dicom_with_contour(dicom_path, contour_path):
    """
    Visualize the DICOM and contour mask.

    :param dicom_path: Path to the DICOM image.
    :type dicom_path: str
    :param contour_path: Path to the corresponding contour file.
    :type contour_path: str
    """
    dicom_image = parse_dicom_file(dicom_path)['pixel_data']
    height, width = dicom_image.shape
    contour_image = poly_to_mask(parse_contour_file(contour_path), width,
                                 height)
    fig = plt.figure(figsize=(8, 8))
    fig.add_subplot(1, 2, 1)
    plt.imshow(dicom_image)
    fig.add_subplot(1, 2, 2)
    plt.imshow(contour_image)
    plt.show()
Ejemplo n.º 6
0
def load_data_as_dataset(data_path):
    """
    Load the data.

    NOTE: assumes that all the data can fit to the RAM memory.
    NOTE: for the purpose of the challenge only the i-contour data is used
    NOTE: only DICOM files having the corresponding i-contour file are loaded

    :param data_path: Path to the folder with the DICOM data and labels.
    :type data_path: str
    :return: images, labels
    :rtype: np.ndarray.Dataset, np.ndarray.Dataset
    """
    images = []
    labels = []
    ids_mapping = parse_csv_mapping_file(data_path)
    for dicom, contour in ids_mapping.iteritems():
        contour_files_path = os.path.join(data_path, COUNTOURS_FOLDER_NAME,
                                          contour, I_CONTOURS_FOLDER_NAME)
        dicom_files_path = os.path.join(data_path, DICOMS_FOLDER_NAME, dicom)
        for contour_file_path in glob.glob(
                os.path.join(contour_files_path, CONTOURS_EXTENSION_PATTERN)):
            contour_list = parse_contour_file(contour_file_path)
            dicom_file_name = contour_name_to_dicom_name(
                os.path.basename(contour_file_path))
            dicom_pixels = parse_dicom_file(
                os.path.join(dicom_files_path, dicom_file_name))['pixel_data']
            height, width = dicom_pixels.shape
            images.append(dicom_pixels)
            labels.append(poly_to_mask(contour_list, width, height))
    images = np.array(images)
    images = np.expand_dims(images, -1)
    labels = np.array(labels)
    assert images.shape[0] == labels.shape[0], \
        'There must be exactly the same number of DICOMS and contours.'
    return images, labels