Ejemplo n.º 1
0
    def test_equality(self):
        x = 10
        y = 10
        z = 3
        pixel_id = sitk.sitkUInt8
        size = (x, y, z)
        direction = (0, 1, 0, 1, 0, 0, 0, 0, 1)
        image = sitk.Image([x, y, z], pixel_id)
        image.SetOrigin(size)
        image.SetSpacing(size)
        image.SetDirection(direction)
        dut1 = img.ImageProperties(image)
        dut2 = img.ImageProperties(image)

        self.assertTrue(dut1 == dut2)
        self.assertFalse(dut1 != dut2)

        image = sitk.Image([x, y, z], sitk.sitkInt8)
        image.SetOrigin(size)
        image.SetSpacing(size)
        image.SetDirection(direction)
        dut1 = img.ImageProperties(image)

        self.assertTrue(dut1 == dut2)
        self.assertFalse(dut1 != dut2)

        image = sitk.Image([x, y, z], sitk.sitkVectorUInt8, 2)
        image.SetOrigin(size)
        image.SetSpacing(size)
        image.SetDirection(direction)
        dut1 = img.ImageProperties(image)

        self.assertTrue(dut1 == dut2)
        self.assertFalse(dut1 != dut2)
Ejemplo n.º 2
0
    def test_non_equality(self):
        x = 10
        y = 10
        z = 3
        pixel_id = sitk.sitkUInt8
        size = (x, y, z)
        direction = (0, 1, 0, 1, 0, 0, 0, 0, 1)
        image = sitk.Image([x, y, z], pixel_id)
        image.SetOrigin(size)
        image.SetSpacing(size)
        image.SetDirection(direction)
        dut1 = img.ImageProperties(image)

        different_size = (x, y, 2)

        # non-equal size
        image = sitk.Image(different_size, pixel_id)
        image.SetOrigin(size)
        image.SetSpacing(size)
        image.SetDirection(direction)
        dut2 = img.ImageProperties(image)

        self.assertFalse(dut1 == dut2)
        self.assertTrue(dut1 != dut2)

        # non-equal origin
        image = sitk.Image(size, pixel_id)
        image.SetOrigin(different_size)
        image.SetSpacing(size)
        image.SetDirection(direction)
        dut2 = img.ImageProperties(image)

        self.assertFalse(dut1 == dut2)
        self.assertTrue(dut1 != dut2)

        # non-equal spacing
        different_size = (x, y, 2)
        image = sitk.Image(size, pixel_id)
        image.SetOrigin(size)
        image.SetSpacing(different_size)
        image.SetDirection(direction)
        dut2 = img.ImageProperties(image)

        self.assertFalse(dut1 == dut2)
        self.assertTrue(dut1 != dut2)

        # non-equal direction
        different_size = (x, y, 2)
        image = sitk.Image(size, pixel_id)
        image.SetOrigin(size)
        image.SetSpacing(size)
        image.SetDirection((1, 0, 0, 0, 1, 0, 0, 0, 1))
        dut2 = img.ImageProperties(image)

        self.assertFalse(dut1 == dut2)
        self.assertTrue(dut1 != dut2)
    def __call__(self, file_name: str, id_: str, category: str, subject_id: str) -> \
            typing.Tuple[np.ndarray, typing.Union[conv.ImageProperties, None]]:

        if category == 'images':
            img = sitk.ReadImage(
                file_name,
                sitk.sitkFloat32)  # load as float32 (otherwise is int)
            return sitk.GetArrayFromImage(img), conv.ImageProperties(img)
        else:
            img = sitk.ReadImage(file_name, sitk.sitkUInt8)
            return sitk.GetArrayFromImage(img), conv.ImageProperties(img)
Ejemplo n.º 4
0
def load_atlas_images(directory: str):
    """Loads the T1 and T2 atlas images.

    Args:
        directory (str): The atlas data directory.
    """

    global atlas_t1
    global atlas_t2
    atlas_t1 = sitk.ReadImage(os.path.join(directory, 'mni_icbm152_t1_tal_nlin_sym_09a_mask.nii.gz'))
    atlas_t2 = sitk.ReadImage(os.path.join(directory, 'mni_icbm152_t2_tal_nlin_sym_09a.nii.gz'))
    if not conversion.ImageProperties(atlas_t1) == conversion.ImageProperties(atlas_t2):
        raise ValueError('T1w and T2w atlas images have not the same image properties')
Ejemplo n.º 5
0
    def extract(self, reader: rd.Reader, params: dict,
                extracted: dict) -> None:
        """see :meth:`.Extractor.extract`"""
        subject_index_expr = expr.IndexExpression(
            params[defs.KEY_SUBJECT_INDEX])

        shape = reader.read(defs.LOC_IMGPROP_SHAPE,
                            subject_index_expr).tolist()
        direction = reader.read(defs.LOC_IMGPROP_DIRECTION,
                                subject_index_expr).tolist()
        spacing = reader.read(defs.LOC_IMGPROP_SPACING,
                              subject_index_expr).tolist()
        origin = reader.read(defs.LOC_IMGPROP_ORIGIN,
                             subject_index_expr).tolist()

        # todo: everything in memory?
        image = sitk.Image(shape, sitk.sitkUInt8)
        image.SetDirection(direction)
        image.SetSpacing(spacing)
        image.SetOrigin(origin)
        # todo number_of_components_per_pixel and pixel_id

        img_properties = conv.ImageProperties(image)
        if self.do_pickle:
            # pickle to prevent from problems since own class
            img_properties = pickle.dumps(img_properties)
        extracted[defs.KEY_PROPERTIES] = img_properties
Ejemplo n.º 6
0
    def test_is_two_dimensional(self):
        x = 10
        y = 10
        image = sitk.Image([x, y], sitk.sitkUInt8)
        dut = img.ImageProperties(image)

        self.assertEqual(dut.is_two_dimensional(), True)
        self.assertEqual(dut.is_three_dimensional(), False)
        self.assertEqual(dut.is_vector_image(), False)
Ejemplo n.º 7
0
    def test_is_vector_image(self):
        x = 10
        y = 10
        number_of_components_per_pixel = 3
        image = sitk.Image([x, y], sitk.sitkVectorUInt8,
                           number_of_components_per_pixel)
        dut = img.ImageProperties(image)

        self.assertEqual(dut.is_two_dimensional(), True)
        self.assertEqual(dut.is_three_dimensional(), False)
        self.assertEqual(dut.is_vector_image(), True)
    def __call__(self, file_name: str, id_: str, category: str, subject_id: str) -> \
            typing.Tuple[np.ndarray, typing.Union[conv.ImageProperties, None]]:
        # note that this function works only if the COORDINATE-case is executed before all others

        if id_ == data.FileTypes.COORDINATE.name:
            # note that the "---" is an ugly hack to be able to pass two paths...
            probability_file, gt_file = file_name.split('---')

            # get properties
            img_gt = sitk.ReadImage(gt_file)
            self.properties = conv.ImageProperties(img_gt)

            self.gt_arr = sitk.GetArrayFromImage(img_gt)

            # get probabilities
            img_proba = sitk.ReadImage(probability_file)
            proba_arr = sitk.GetArrayFromImage(img_proba)
            proba_arr = proba_arr[..., 1]  # get probabilities of foreground class

            # get indices of probabilites larger some value
            # meaning construct the point cloud
            proba_indices = np.nonzero(proba_arr > self.probability_threshold)
            proba_indices = np.array(proba_indices[::-1])
            proba_indices = np.transpose(proba_indices)  # shape is (N, 3)
            self.indices = proba_indices

            self.probabilities = np.zeros((self.indices.shape[0], 1), np.float32)  # shape is (N, 1)
            self.labels = np.zeros((self.indices.shape[0], 1), np.uint8)  # shape is (N, 1)

            for idx, (x, y, z) in enumerate(self.indices):
                self.probabilities[idx] = proba_arr[z, y, x]
                self.labels[idx] = self.gt_arr[z, y, x]

            return self.indices, self.properties
        elif id_ == data.FileTypes.INDICES.name:  # todo: needed
            return self.indices, self.properties
        elif id_ == data.FileTypes.LABEL.name:
            return self.labels, self.properties
        elif id_ == data.FileTypes.GTM.name:
            return self.gt_arr, self.properties
        elif id_ == data.FileTypes.IMAGE_INFORMATION.name:
            img = sitk.ReadImage(file_name)
            img = sitk.GetArrayFromImage(img)
            img = img[..., 1]  # get probabilities of foreground class
            img_arr = img.copy()
            img_arr = np.expand_dims(img_arr, -1)

            cubes = self._extract_cubes(img_arr)
            return cubes.astype(np.float32), self.properties
        else:
            raise ValueError('id "{}" unknown'.format(id_))
Ejemplo n.º 9
0
    def setUp(self):
        dim_x = 5
        dim_y = 10
        dim_z = 3
        self.no_vector_components = 4

        # some unordinary origins, spacings and directions
        self.origin_spacing_2d = (dim_x, dim_y)
        self.direction_2d = (0, 1, 1, 0)
        self.origin_spacing_3d = (dim_x, dim_y, dim_z)
        self.direction_3d = (1, 0, 0, 0, 1, 0, 0, 0, 1)

        # set up images
        image = sitk.Image((dim_x, dim_y, dim_z), sitk.sitkUInt8)
        image.SetOrigin(self.origin_spacing_3d)
        image.SetSpacing(self.origin_spacing_3d)
        image.SetDirection(self.direction_3d)
        self.properties_3d = img.ImageProperties(image)

        image = sitk.Image((dim_x, dim_y), sitk.sitkUInt8)
        image.SetOrigin(self.origin_spacing_2d)
        image.SetSpacing(self.origin_spacing_2d)
        image.SetDirection(self.direction_2d)
        self.properties_2d = img.ImageProperties(image)

        # set up numpy arrays (note the inverted order of the dimension)
        self.array_image_shape_2d = np.zeros((dim_y, dim_x), np.uint8)
        self.array_2d_vector = np.zeros(
            (dim_y * dim_x, self.no_vector_components), np.uint8)
        self.array_image_shape_2d_vector = np.zeros(
            (dim_y, dim_x, self.no_vector_components), np.uint8)

        self.array_image_shape_3d = np.zeros((dim_z, dim_y, dim_x), np.uint8)
        self.array_3d_vector = np.zeros(
            (dim_z * dim_y * dim_x, self.no_vector_components), np.uint8)
        self.array_image_shape_3d_vector = np.zeros(
            (dim_z, dim_y, dim_x, self.no_vector_components), np.uint8)
Ejemplo n.º 10
0
def load_atlas_images(directory: str):
    """Loads the T1 and T2 atlas images.

    Args:
        directory (str): The atlas data directory.
    """

    global atlas_t1
    global atlas_t2
    global reference_img
    atlas_t1 = sitk.ReadImage(
        os.path.join(directory, 'mni_icbm152_t1_tal_nlin_sym_09a_mask.nii.gz'))
    atlas_t2 = sitk.ReadImage(
        os.path.join(directory, 'mni_icbm152_t2_tal_nlin_sym_09a.nii.gz'))
    reference_img = sitk.ReadImage(
        '/Users/leo/Documents/MBE/Semester_3/Medical_image_analysis_lab/MIALab/data/train/100307/T1native.nii.gz'
    )

    #reference_img = sitk.Resample(atlas_t2, reference_img)

    if not conversion.ImageProperties(atlas_t1) == conversion.ImageProperties(
            atlas_t2):
        raise ValueError(
            'T1w and T2w atlas images have not the same image properties')
Ejemplo n.º 11
0
    def extract(self, reader: rd.Reader, params: dict, extracted: dict) -> None:
        subject_index_expr = expr.IndexExpression(params['subject_index'])

        shape = reader.read(df.INFO_SHAPE, subject_index_expr).tolist()
        direction = reader.read(df.INFO_DIRECTION, subject_index_expr).tolist()
        spacing = reader.read(df.INFO_SPACING, subject_index_expr).tolist()
        origin = reader.read(df.INFO_ORIGIN, subject_index_expr).tolist()

        # todo: everything in memory?
        image = sitk.Image(shape, sitk.sitkUInt8)
        image.SetDirection(direction)
        image.SetSpacing(spacing)
        image.SetOrigin(origin)
        # todo number_of_components_per_pixel and pixel_id

        img_properties = conv.ImageProperties(image)
        if self.do_pickle:
            # pickle to prevent from problems since own class
            img_properties = pickle.dumps(img_properties)
        extracted['properties'] = img_properties
Ejemplo n.º 12
0
    def test_properties(self):
        x = 10
        y = 10
        z = 3
        pixel_id = sitk.sitkUInt8
        size = (x, y, z)
        direction = (0, 1, 0, 1, 0, 0, 0, 0, 1)
        image = sitk.Image([x, y, z], pixel_id)
        image.SetOrigin(size)
        image.SetSpacing(size)
        image.SetDirection(direction)
        dut = img.ImageProperties(image)

        self.assertEqual(dut.size, size)
        self.assertEqual(dut.origin, size)
        self.assertEqual(dut.spacing, size)
        self.assertEqual(dut.direction, direction)
        self.assertEqual(dut.dimensions, z)
        self.assertEqual(dut.number_of_components_per_pixel, 1)
        self.assertEqual(dut.pixel_id, pixel_id)
Ejemplo n.º 13
0
    def __init__(self, id_: str, path: str, images: dict):
        """Initializes a new instance of the BrainImage class.

        Args:
            id_ (str): An identifier.
            path (str): Full path to the image directory.
            images (dict): The images, where the key is a :py:class:`BrainImageTypes` and the value is a SimpleITK image.
        """

        self.id_ = id_
        self.path = path
        self.images = images

        # ensure we have an image to get the image properties
        if len(images) == 0:
            raise ValueError('No images provided')

        self.image_properties = conversion.ImageProperties(self.images[list(
            self.images.keys())[0]])
        self.feature_images = {}
        self.feature_matrix = None  # a tuple (features, labels),
Ejemplo n.º 14
0
    def __call__(self, file_name: str, id_: str, category: str, subject_id: str) -> \
            typing.Tuple[np.ndarray, typing.Union[conv.ImageProperties, None]]:
        if id_ == FileTypes.AGE.name:
            with open(file_name, 'r') as f:
                value = np.asarray([int(f.readline().split(':')[1].strip())])
                return value, None
        if id_ == FileTypes.GPA.name:
            with open(file_name, 'r') as f:
                value = np.asarray(
                    [float(f.readlines()[1].split(':')[1].strip())])
                return value, None
        if id_ == FileTypes.SEX.name:
            with open(file_name, 'r') as f:
                value = np.asarray([f.readlines()[2].split(':')[1].strip()])
                return value, None

        if category == 'images':
            img = sitk.ReadImage(file_name, sitk.sitkFloat32)
        else:
            img = sitk.ReadImage(file_name, sitk.sitkUInt8)

        return sitk.GetArrayFromImage(img), conv.ImageProperties(img)
Ejemplo n.º 15
0
    def __call__(self, file_name: str, id_: str, category: str, subject_id: str) -> \
            typing.Tuple[np.ndarray, typing.Union[conv.ImageProperties, None]]:
        if id_ == FileTypes.AGE.name:
            with open(file_name, 'r') as f:
                value = np.asarray([int(f.readline().split(':')[1].strip())])
                return value, None
        if id_ == FileTypes.GPA.name:
            with open(file_name, 'r') as f:
                value = np.asarray([float(f.readlines()[1].split(':')[1].strip())])
                return value, None
        if id_ == FileTypes.GENDER.name:
            with open(file_name, 'r') as f:
                value = np.array(f.readlines()[2].split(':')[1].strip())
                return value, None

        if category == defs.KEY_IMAGES:
            img = sitk.ReadImage(file_name, sitk.sitkFloat32)
        else:
            # this is the ground truth (defs.KEY_LABELS), which will be loaded as unsigned integer
            img = sitk.ReadImage(file_name, sitk.sitkUInt8)

        return sitk.GetArrayFromImage(img), conv.ImageProperties(img)
Ejemplo n.º 16
0
 def __call__(self, file_name: str, id_: str, category: str, subject_id: str) -> \
         typing.Tuple[np.ndarray, typing.Union[conv.ImageProperties, None]]:
     img = sitk.ReadImage(file_name)
     return sitk.GetArrayFromImage(img), conv.ImageProperties(img)
Ejemplo n.º 17
0
def pre_process(id_: str, paths: dict, **kwargs) -> structure.BrainImage:
    """Loads and processes an image.

    The processing includes:

    - Registration
    - Pre-processing
    - Feature extraction

    Args:
        id_ (str): An image identifier.
        paths (dict): A dict, where the keys are an image identifier of type structure.BrainImageTypes
            and the values are paths to the images.

    Returns:
        (structure.BrainImage):
    """

    print('-' * 10, 'Processing', id_)

    # load image
    path = paths.pop(
        id_, '')  # the value with key id_ is the root directory of the image
    img = {img_key: sitk.ReadImage(path) for img_key, path in paths.items()}
    img = structure.BrainImage(id_, path, img)

    # construct T1 pipeline
    pipeline_t1 = fltr.FilterPipeline()
    if kwargs.get('zscore_pre', False):
        pipeline_t1.add_filter(fltr_prep.NormalizeZScore())
    if kwargs.get('registration_pre', False):
        pipeline_t1.add_filter(fltr_reg.MultiModalRegistration())
        pipeline_t1.set_param(fltr_reg.MultiModalRegistrationParams(atlas_t1),
                              1)

    # execute pipeline on T1 image
    img.images[structure.BrainImageTypes.T1] = pipeline_t1.execute(
        img.images[structure.BrainImageTypes.T1])

    # construct T2 pipeline
    pipeline_t2 = fltr.FilterPipeline()
    if kwargs.get('zscore_pre', False):
        pipeline_t2.add_filter(fltr_prep.NormalizeZScore())

    # execute pipeline on T2 image
    img.images[structure.BrainImageTypes.T2] = pipeline_t2.execute(
        img.images[structure.BrainImageTypes.T2])

    if kwargs.get('registration_pre', False):
        # get transformation
        transform = pipeline_t1.filters[1].transform

        # apply transformation of T1 image registration to T2 image
        image_t2 = img.images[structure.BrainImageTypes.T2]
        image_t2 = sitk.Resample(image_t2, atlas_t1, transform,
                                 sitk.sitkLinear, 0.0,
                                 image_t2.GetPixelIDValue())
        img.images[structure.BrainImageTypes.T2] = image_t2

        # apply transformation of T1 image registration to ground truth
        image_ground_truth = img.images[structure.BrainImageTypes.GroundTruth]
        image_ground_truth = sitk.Resample(
            image_ground_truth, atlas_t1, transform, sitk.sitkNearestNeighbor,
            0, image_ground_truth.GetPixelIDValue())
        img.images[structure.BrainImageTypes.GroundTruth] = image_ground_truth

        # update image properties to atlas image properties after registration
        img.image_properties = conversion.ImageProperties(atlas_t1)

    # extract the features
    feature_extractor = FeatureExtractor(img, **kwargs)
    img = feature_extractor.execute()

    img.feature_images = {}

    return img
Ejemplo n.º 18
0
def pre_process(id_: str, paths: dict, **kwargs) -> structure.BrainImage:
    """Loads and processes an image.

    The processing includes:

    - Registration
    - Pre-processing
    - Feature extraction

    Args:
        id_ (str): An image identifier.
        paths (dict): A dict, where the keys are an image identifier of type structure.BrainImageTypes
            and the values are paths to the images.

    Returns:
        (structure.BrainImage):
    """

    print('-' * 10, 'Processing', id_)

    # load image
    path = paths.pop(
        id_, '')  # the value with key id_ is the root directory of the image
    path_to_transform = paths.pop(
        structure.BrainImageTypes.RegistrationTransform, '')
    img = {img_key: sitk.ReadImage(path) for img_key, path in paths.items()}
    transform = sitk.ReadTransform(path_to_transform)
    img = structure.BrainImage(id_, path, img, transform)

    # construct pipeline for brain mask registration
    # we need to perform this before the T1w and T2w pipeline because the registered mask is used for skull-stripping
    pipeline_brain_mask = fltr.FilterPipeline()
    if kwargs.get('registration_pre', False):
        pipeline_brain_mask.add_filter(fltr_prep.ImageRegistration())
        pipeline_brain_mask.set_param(
            fltr_prep.ImageRegistrationParameters(atlas_t1, img.transformation,
                                                  True),
            len(pipeline_brain_mask.filters) - 1)

    # execute pipeline on the brain mask image
    img.images[
        structure.BrainImageTypes.BrainMask] = pipeline_brain_mask.execute(
            img.images[structure.BrainImageTypes.BrainMask])

    # construct pipeline for T1w image pre-processing
    pipeline_t1 = fltr.FilterPipeline()
    if kwargs.get('registration_pre', False):
        pipeline_t1.add_filter(fltr_prep.ImageRegistration())
        pipeline_t1.set_param(
            fltr_prep.ImageRegistrationParameters(atlas_t1,
                                                  img.transformation),
            len(pipeline_t1.filters) - 1)
    if kwargs.get('skullstrip_pre', False):
        pipeline_t1.add_filter(fltr_prep.SkullStripping())
        pipeline_t1.set_param(
            fltr_prep.SkullStrippingParameters(
                img.images[structure.BrainImageTypes.BrainMask]),
            len(pipeline_t1.filters) - 1)
    if kwargs.get('normalization_pre', False):
        pipeline_t1.add_filter(fltr_prep.ImageNormalization())

    # execute pipeline on the T1w image
    img.images[structure.BrainImageTypes.T1w] = pipeline_t1.execute(
        img.images[structure.BrainImageTypes.T1w])

    # construct pipeline for T2w image pre-processing
    pipeline_t2 = fltr.FilterPipeline()
    if kwargs.get('registration_pre', False):
        pipeline_t2.add_filter(fltr_prep.ImageRegistration())
        pipeline_t2.set_param(
            fltr_prep.ImageRegistrationParameters(atlas_t2,
                                                  img.transformation),
            len(pipeline_t2.filters) - 1)
    if kwargs.get('skullstrip_pre', False):
        pipeline_t2.add_filter(fltr_prep.SkullStripping())
        pipeline_t2.set_param(
            fltr_prep.SkullStrippingParameters(
                img.images[structure.BrainImageTypes.BrainMask]),
            len(pipeline_t2.filters) - 1)
    if kwargs.get('normalization_pre', False):
        pipeline_t2.add_filter(fltr_prep.ImageNormalization())

    # execute pipeline on the T2w image
    img.images[structure.BrainImageTypes.T2w] = pipeline_t2.execute(
        img.images[structure.BrainImageTypes.T2w])

    # construct pipeline for ground truth image pre-processing
    pipeline_gt = fltr.FilterPipeline()
    if kwargs.get('registration_pre', False):
        pipeline_gt.add_filter(fltr_prep.ImageRegistration())
        pipeline_gt.set_param(
            fltr_prep.ImageRegistrationParameters(atlas_t1, img.transformation,
                                                  True),
            len(pipeline_gt.filters) - 1)

    # execute pipeline on the ground truth image
    img.images[structure.BrainImageTypes.GroundTruth] = pipeline_gt.execute(
        img.images[structure.BrainImageTypes.GroundTruth])

    # update image properties to atlas image properties after registration
    img.image_properties = conversion.ImageProperties(
        img.images[structure.BrainImageTypes.T1w])

    # extract the features
    feature_extractor = FeatureExtractor(img, **kwargs)

    img = feature_extractor.execute()

    img.feature_images = {
    }  # we free up memory because we only need the img.feature_matrix
    # for training of the classifier

    return img
Ejemplo n.º 19
0
 def _read_map(self, file_name):
     img = sitk.ReadImage(file_name)
     np_data = sitk.GetArrayFromImage(img)
     # shape=(5, 350, 350) and dtype=float32
     properties = pymia_conv.ImageProperties(img)
     return np_data, properties
Ejemplo n.º 20
0
def main(hdf_file: str):
    extractor = extr.ComposeExtractor([
        extr.NamesExtractor(),
        extr.DataExtractor(),
        extr.SelectiveDataExtractor(),
        extr.DataExtractor(('numerical', ), ignore_indexing=True),
        extr.DataExtractor(('gender', ), ignore_indexing=True),
        extr.DataExtractor(('mask', ), ignore_indexing=False),
        extr.SubjectExtractor(),
        extr.FilesExtractor(categories=(defs.KEY_IMAGES, defs.KEY_LABELS,
                                        'mask', 'numerical', 'gender')),
        extr.IndexingExtractor(),
        extr.ImagePropertiesExtractor()
    ])
    dataset = extr.PymiaDatasource(hdf_file, extr.SliceIndexing(), extractor)

    for i in range(len(dataset)):
        item = dataset[i]

        index_expr = item[defs.KEY_INDEX_EXPR]  # type: data.IndexExpression
        root = item[defs.KEY_FILE_ROOT]

        image = None  # type: sitk.Image
        for i, file in enumerate(
                item[defs.KEY_PLACEHOLDER_FILES.format('images')]):
            image = sitk.ReadImage(os.path.join(root, file))
            np_img = sitk.GetArrayFromImage(image).astype(np.float32)
            np_img = (np_img - np_img.mean()) / np_img.std()
            np_slice = np_img[index_expr.expression]
            if (np_slice != item[defs.KEY_IMAGES][..., i]).any():
                raise ValueError('slice not equal')

        # for any image
        image_properties = conv.ImageProperties(image)

        if image_properties != item[defs.KEY_PROPERTIES]:
            raise ValueError('image properties not equal')

        for file in item[defs.KEY_PLACEHOLDER_FILES.format('labels')]:
            image = sitk.ReadImage(os.path.join(root, file))
            np_img = sitk.GetArrayFromImage(image)
            np_img = np.expand_dims(
                np_img, axis=-1
            )  # due to the convention of having the last dim as number of channels
            np_slice = np_img[index_expr.expression]
            if (np_slice != item[defs.KEY_LABELS]).any():
                raise ValueError('slice not equal')

        for file in item[defs.KEY_PLACEHOLDER_FILES.format('mask')]:
            image = sitk.ReadImage(os.path.join(root, file))
            np_img = sitk.GetArrayFromImage(image)
            np_img = np.expand_dims(
                np_img, axis=-1
            )  # due to the convention of having the last dim as number of channels
            np_slice = np_img[index_expr.expression]
            if (np_slice != item['mask']).any():
                raise ValueError('slice not equal')

        for file in item[defs.KEY_PLACEHOLDER_FILES.format('numerical')]:
            with open(os.path.join(root, file), 'r') as f:
                lines = f.readlines()
            age = float(lines[0].split(':')[1].strip())
            gpa = float(lines[1].split(':')[1].strip())
            if age != item['numerical'][0][0] or gpa != item['numerical'][0][1]:
                raise ValueError('value not equal')

        for file in item[defs.KEY_PLACEHOLDER_FILES.format('gender')]:
            with open(os.path.join(root, file), 'r') as f:
                gender = f.readlines()[2].split(':')[1].strip()
            if gender != str(item['gender'][0]):
                raise ValueError('value not equal')

    print('All test passed!')
Ejemplo n.º 21
0
def main(hdf_file: str):
    extractor = extr.ComposeExtractor([
        extr.NamesExtractor(),
        extr.DataExtractor(),
        extr.SelectiveDataExtractor(),
        extr.DataExtractor(('numerical', ), ignore_indexing=True),
        extr.DataExtractor(('sex', ), ignore_indexing=True),
        extr.DataExtractor(('mask', ), ignore_indexing=False),
        extr.SubjectExtractor(),
        extr.FilesExtractor(categories=('images', 'labels', 'mask',
                                        'numerical', 'sex')),
        extr.IndexingExtractor(),
        extr.ImagePropertiesExtractor()
    ])
    dataset = extr.ParameterizableDataset(hdf_file, extr.SliceIndexing(),
                                          extractor)

    for i in range(len(dataset)):
        item = dataset[i]

        index_expr = item['index_expr']  # type: pymia_data.IndexExpression
        root = item['file_root']

        image = None  # type: sitk.Image
        for i, file in enumerate(item['images_files']):
            image = sitk.ReadImage(os.path.join(root, file))
            np_img = sitk.GetArrayFromImage(image).astype(np.float32)
            np_img = (np_img - np_img.mean()) / np_img.std()
            np_slice = np_img[index_expr.expression]
            if (np_slice != item['images'][..., i]).any():
                raise ValueError('slice not equal')

        # for any image
        image_properties = conv.ImageProperties(image)

        if image_properties != item['properties']:
            raise ValueError('image properties not equal')

        for file in item['labels_files']:
            image = sitk.ReadImage(os.path.join(root, file))
            np_img = sitk.GetArrayFromImage(image)
            np_img = np.expand_dims(
                np_img, axis=-1
            )  # due to the convention of having the last dim as number of channels
            np_slice = np_img[index_expr.expression]
            if (np_slice != item['labels']).any():
                raise ValueError('slice not equal')

        for file in item['mask_files']:
            image = sitk.ReadImage(os.path.join(root, file))
            np_img = sitk.GetArrayFromImage(image)
            np_img = np.expand_dims(
                np_img, axis=-1
            )  # due to the convention of having the last dim as number of channels
            np_slice = np_img[index_expr.expression]
            if (np_slice != item['mask']).any():
                raise ValueError('slice not equal')

        for file in item['numerical_files']:
            with open(os.path.join(root, file), 'r') as f:
                lines = f.readlines()
            age = float(lines[0].split(':')[1].strip())
            gpa = float(lines[1].split(':')[1].strip())
            if age != item['numerical'][0][0] or gpa != item['numerical'][0][1]:
                raise ValueError('value not equal')

        for file in item['sex_files']:
            with open(os.path.join(root, file), 'r') as f:
                sex = f.readlines()[2].split(':')[1].strip()
            if sex != str(item['sex'][0]):
                raise ValueError('value not equal')

    print('All test passed!')