Exemplo n.º 1
0
    def fit_normalization(self, num_sample=None, show_progress=False):
        """
        Calculate the voxel-wise mean and std across the dataset for normalization.

        Args:
            num_sample (int or None): If None (default), calculate the values across the complete dataset,
                                      otherwise sample a number of images.
            show_progress (bool): Show a progress bar during the calculation."
        """

        if num_sample is None:
            num_sample = len(self)

        image_shape = self.image_shape()
        all_struct_arr = np.zeros(
            (num_sample, image_shape[0], image_shape[1], image_shape[2]))

        sampled_filenames = np.random.choice(self.filenames,
                                             num_sample,
                                             replace=False)
        if show_progress:
            sampled_filenames = tqdm_notebook(sampled_filenames)

        for i, filename in enumerate(sampled_filenames):
            struct_arr = utils.load_nifti(filename, mask=mask)
            all_struct_arr[i] = struct_arr

        self.mean = all_struct_arr.mean(0)
        self.std = all_struct_arr.std(0)
Exemplo n.º 2
0
    def __getitem__(self, idx):
        """Return the image as a numpy array and the label."""
        label = self.labels[idx]

        struct_arr = utils.load_nifti(self.filenames[idx], mask=self.mask)
        # TDOO: Try normalizing each image to mean 0 and std 1 here.
        # struct_arr = (struct_arr - struct_arr.mean()) / (struct_arr.std() + 1e-10)
        struct_arr = (struct_arr - self.mean) / (
            self.std + 1e-10)  # prevent 0 division by adding small factor
        struct_arr = struct_arr[None]  # add (empty) channel dimension
        struct_arr = torch.FloatTensor(struct_arr)

        if self.transform is not None:
            struct_arr = self.transform(struct_arr)

        return struct_arr, label
Exemplo n.º 3
0
 def get_raw_image(self, idx):
     """Return the raw image at index idx (i.e. not normalized, no color channel, no transform."""
     return utils.load_nifti(self.filenames[idx], mask=self.mask)
Exemplo n.º 4
0
 def image_shape(self):
     """The shape of the MRI images."""
     return utils.load_nifti(self.filenames[0], mask=mask).shape
Exemplo n.º 5
0
import numpy as np
import pandas as pd
import os
from jrieke import utils
from tqdm import tqdm_notebook
import multiprocessing
from settings import settings

import torch
from torch.utils.data import Dataset, DataLoader

from tabulate import tabulate

# Binary brain mask used to cut out the skull.
mask = utils.load_nifti(settings["binary_brain_mask"])

# ------------------------- ADNI data tables -----------------------------------

# Ritter/Haynes lab file system at BCCN Berlin.
ADNI_DIR = settings["ADNI_DIR"]

# Filepaths for 1.5 Tesla scans.
table_15T = None  #os.path.join(ADNI_DIR, settings["1.5T_table"])
image_dir_15T = None  #os.path.join(ADNI_DIR, settings["1.5T_image_dir"])
corrupt_images_15T = ['067_S_0077/Screening']


# TODO: Maybe rename to load_table or load_adni_table
def load_data_table(table, image_dir, corrupt_images=None):
    """Read data table, find corresponding images, filter out corrupt,
    missing and MCI images, and return the samples as a pandas dataframe."""