Exemplo n.º 1
0
 def log_image(self, print_calling_method=False):
     """
     Log basic stats for this ImagePair.
     """
     self.logger(
         "%s\n"
         "--- shape:      %s\n"
         "--- real shape: %s\n"
         "--- pixdim:     %s" %
         (self.id, self.shape, np.round(get_real_image_size(self),
                                        3), np.round(get_pix_dim(self), 3)),
         print_calling_method=print_calling_method)
Exemplo n.º 2
0
    def audit(self):
        shapes = []
        channels = []
        real_sizes = []
        pixdims = []
        memory = []

        for im_path in self.nii_paths:
            # Load the nii file without loading image data
            im = nib.load(im_path)

            # Get image voxel shape
            shape = im.shape
            shapes.append(shape[:3])

            try:
                c = shape[3]
            except IndexError:
                c = 1
            channels.append(c)

            # Get image real shape
            real_sizes.append(get_real_image_size(im))

            # Get pixel dims
            pixdims.append(get_pix_dim(im))

            # Calculate memory in bytes to store image
            memory.append(im.get_data_dtype().itemsize * np.prod(shape))

        n_classes = None
        if self.nii_lab_paths is not None:
            # Labels exists, thus we need the n_classes attribute
            if self.hparms is not None:
                # Attempt to get it from a potentially specified hparams obj
                n_classes = self.hparms.get_from_anywhere("n_classes")
            if n_classes is None:
                # If still none, infer it
                n_classes = _audit_classes(self.nii_lab_paths, self.logger)

        info = {
            "shapes": shapes,
            "real_sizes": real_sizes,
            "pixdims": pixdims,
            "memory_bytes": memory,
            "n_channels": channels,
            "n_classes": n_classes,
        }
        return info
Exemplo n.º 3
0
    def audit(self):
        shapes = []
        channels = []
        real_sizes = []
        pixdims = []
        memory = []

        for im_path in self.nii_paths:
            # Load the nii file without loading image data
            im = nib.load(im_path)

            # Get image voxel shape
            shape = im.shape
            shapes.append(shape[:3])

            try:
                c = shape[3]
            except IndexError:
                c = 1
            channels.append(c)

            # Get image real shape
            real_sizes.append(get_real_image_size(im))

            # Get pixel dims
            pixdims.append(get_pix_dim(im))

            # Calculate memory in bytes to store image
            memory.append(im.get_data_dtype().itemsize * np.prod(shape))

        if self.nii_lab_paths is not None:
            self.logger("Auditing number of target classes. This may take "
                        "a while as data must be read from disk."
                        "\n-- Note: avoid this by manually setting the "
                        "n_classes attribute in train_hparams.yaml.")
            # Select up to 50 random images and find the unique classes
            lab_paths = np.random.choice(self.nii_lab_paths,
                                         min(50, len(self.nii_lab_paths)),
                                         replace=False)
            classes = []
            for l in lab_paths:
                classes.append(np.unique(nib.load(l).get_data()))
            classes = np.unique(classes)
            n_classes = classes.shape[0]

            # Make sure the classes start from 0 and step continuously by 1
            c_min, c_max = np.min(classes), np.max(classes)
            if c_min != 0:
                raise ValueError(
                    "Invalid class audit - Class integers should"
                    " start from 0, found %i (classes found: %s)" %
                    (c_min, classes))
            if n_classes != max(classes) + 1:
                raise ValueError("Invalid class audit - Found %i classes, but"
                                 " expected %i, as the largest class value"
                                 " found was %i. Classes found: %s" %
                                 (n_classes, c_max + 1, c_max, classes))
        else:
            n_classes = None
            classes = None

        info = {
            "shapes": shapes,
            "real_sizes": real_sizes,
            "pixdims": pixdims,
            "memory_bytes": memory,
            "n_channels": channels,
            "n_classes": n_classes,
            "classes": classes
        }
        return info
Exemplo n.º 4
0
 def real_shape(self):
     """
     Returns:
         The real (physical, scanner-space span) shape of the image
     """
     return get_real_image_size(self.image_obj)