def test_gn_fit_fix_mu_sigma():
    """ Test the fitting routine with fixed mean and std. """

    # Create a T2WModality object
    currdir = os.path.dirname(os.path.abspath(__file__))
    path_data_t2w = os.path.join(currdir, 'data', 't2w')
    t2w_mod = T2WModality(path_data_t2w)
    t2w_mod.read_data_from_path()

    # Create the GTModality object
    path_data_gt = os.path.join(currdir, 'data', 'gt_folders')
    path_data_gt_list = [os.path.join(path_data_gt, 'prostate'),
                         os.path.join(path_data_gt, 'pz'),
                         os.path.join(path_data_gt, 'cg'),
                         os.path.join(path_data_gt, 'cap')]
    label_gt = ['prostate', 'pz', 'cg', 'cap']
    gt_mod = GTModality()
    gt_mod.read_data_from_path(cat_gt=label_gt, path_data=path_data_gt_list)

    params = {'mu': 200., 'sigma': 70.}
    gaussian_norm = GaussianNormalization(T2WModality(), params=params)
    gaussian_norm.fit(t2w_mod, gt_mod, label_gt[0])
    assert_almost_equal(gaussian_norm.fit_params_['mu'], 245.90,
                        decimal=DECIMAL_PRECISON)
    assert_almost_equal(gaussian_norm.fit_params_['sigma'], 74.31,
                        decimal=DECIMAL_PRECISON)
def test_gn_fit_wt_gt():
    """ Test the fitting routine without ground-truth. """

    # Create a T2WModality object
    currdir = os.path.dirname(os.path.abspath(__file__))
    path_data_t2w = os.path.join(currdir, 'data', 't2w')
    t2w_mod = T2WModality(path_data_t2w)
    t2w_mod.read_data_from_path()

    gaussian_norm = GaussianNormalization(T2WModality())
    gaussian_norm.fit(t2w_mod)
    assert_almost_equal(gaussian_norm.fit_params_['mu'], -8.13,
                        decimal=DECIMAL_PRECISON)
    assert_almost_equal(gaussian_norm.fit_params_['sigma'], 11.35,
                        decimal=DECIMAL_PRECISON)
def find_normalization_params(pat_t2w, pat_gt, label):
    # Create the normalization object and load the model
    t2w_norm = GaussianNormalization(T2WModality())

    # Read the T2W
    t2w_mod = T2WModality()
    t2w_mod.read_data_from_path(pat_t2w)

    # Read the GT
    gt_mod = GTModality()
    gt_mod.read_data_from_path(label, pat_gt)

    # Find the normalization parameters
    t2w_norm.fit(t2w_mod, ground_truth=gt_mod, cat=label[0])

    return t2w_norm
def test_gn_normalize():
    """ Test the normalize function. """

    # Create a T2WModality object
    currdir = os.path.dirname(os.path.abspath(__file__))
    path_data_t2w = os.path.join(currdir, 'data', 't2w')
    t2w_mod = T2WModality(path_data_t2w)
    t2w_mod.read_data_from_path()

    # Create the GTModality object
    path_data_gt = os.path.join(currdir, 'data', 'gt_folders')
    path_data_gt_list = [os.path.join(path_data_gt, 'prostate'),
                         os.path.join(path_data_gt, 'pz'),
                         os.path.join(path_data_gt, 'cg'),
                         os.path.join(path_data_gt, 'cap')]
    label_gt = ['prostate', 'pz', 'cg', 'cap']
    gt_mod = GTModality()
    gt_mod.read_data_from_path(cat_gt=label_gt, path_data=path_data_gt_list)

    # Store the data before the normalization
    pdf_copy = t2w_mod.pdf_.copy()
    data_copy = t2w_mod.data_.copy()

    # Normalize the data
    gaussian_norm = GaussianNormalization(T2WModality())
    gaussian_norm.fit(t2w_mod, gt_mod, 'prostate')

    t2w_mod = gaussian_norm.normalize(t2w_mod)

    # Check that the data are equal to what they should be
    assert_array_almost_equal(t2w_mod.data_, (data_copy - 245.90) / 74.31,
                              decimal=DECIMAL_PRECISON)

    # Denormalize the data
    t2w_mod = gaussian_norm.denormalize(t2w_mod)

    # Check that the data are equal to the original data
    data = np.load(os.path.join(currdir, 'data', 'data_denormalize.npy'))
    assert_array_equal(t2w_mod.data_, data)
    data = np.load(os.path.join(currdir, 'data', 'pdf_denormalize.npy'))
    assert_array_equal(t2w_mod.pdf_, data)
def test_gn_fit_wrong_params():
    """ Test either if an error is raised when the params change just before
    fitting. """

    # Create a T2WModality object
    currdir = os.path.dirname(os.path.abspath(__file__))
    path_data_t2w = os.path.join(currdir, 'data', 't2w')
    t2w_mod = T2WModality(path_data_t2w)
    t2w_mod.read_data_from_path()

    # Create the GTModality object
    path_data_gt = os.path.join(currdir, 'data', 'gt_folders')
    path_data_gt_list = [os.path.join(path_data_gt, 'prostate'),
                         os.path.join(path_data_gt, 'pz'),
                         os.path.join(path_data_gt, 'cg'),
                         os.path.join(path_data_gt, 'cap')]
    label_gt = ['prostate', 'pz', 'cg', 'cap']
    gt_mod = GTModality()
    gt_mod.read_data_from_path(cat_gt=label_gt, path_data=path_data_gt_list)

    gaussian_norm = GaussianNormalization(T2WModality())
    gaussian_norm.params = 'None'
    assert_raises(ValueError, gaussian_norm.fit, t2w_mod, gt_mod, label_gt[0])
def test_gn_save_load():
    """ Test the save and load function. """

    # Create a T2WModality object
    currdir = os.path.dirname(os.path.abspath(__file__))
    path_data_t2w = os.path.join(currdir, 'data', 't2w')
    t2w_mod = T2WModality(path_data_t2w)
    t2w_mod.read_data_from_path()

    # Create the GTModality object
    path_data_gt = os.path.join(currdir, 'data', 'gt_folders')
    path_data_gt_list = [os.path.join(path_data_gt, 'prostate'),
                         os.path.join(path_data_gt, 'pz'),
                         os.path.join(path_data_gt, 'cg'),
                         os.path.join(path_data_gt, 'cap')]
    label_gt = ['prostate', 'pz', 'cg', 'cap']
    gt_mod = GTModality()
    gt_mod.read_data_from_path(cat_gt=label_gt, path_data=path_data_gt_list)

    # Normalize the data
    gaussian_norm = GaussianNormalization(T2WModality())
    gaussian_norm.fit(t2w_mod, gt_mod, 'prostate')

    # Store the normalization object
    filename = os.path.join(currdir, 'data', 'gn_obj.p')
    gaussian_norm.save_to_pickles(filename)

    # Load the object
    gn_2 = GaussianNormalization.load_from_pickles(filename)

    # Check that the different variables are the same
    assert_equal(type(gn_2.base_modality_), type(gaussian_norm.base_modality_))
    assert_equal(gn_2.fit_params_['mu'], gaussian_norm.fit_params_['mu'])
    assert_equal(gn_2.fit_params_['sigma'], gaussian_norm.fit_params_['sigma'])
    assert_equal(gn_2.is_fitted_, gaussian_norm.is_fitted_)
    assert_array_equal(gn_2.roi_data_, gaussian_norm.roi_data_)
예제 #7
0
        # Read the normalization information
        pat_chg = id_patient_list[id_p].lower().replace(' ', '_') + '_norm.p'
        filename = os.path.join(path_rician, pat_chg)
        t2w_norm = RicianNormalization.load_from_pickles(filename)

        # Normalize the data
        t2w_mod = t2w_norm.normalize(t2w_mod)

    else:
        # Gaussian Normalization

        # Read the normalization information
        pat_chg = id_patient_list[id_p].lower().replace(' ', '_') + '_norm.p'
        filename = os.path.join(path_gaussian, pat_chg)
        t2w_norm = GaussianNormalization.load_from_pickles(filename)

        # Normalize the data
        t2w_mod = t2w_norm.normalize(t2w_mod)

    # Rescale the data on 8 bits
    t2w_mod.data_ = ((t2w_mod.data_ - EXTREM[0]) * (255. /
                                                    (EXTREM[1] - EXTREM[0])))

    # Update the histogram
    t2w_mod.update_histogram()

    # Create the different parameters for the filter bank
    frequencies = np.linspace(0.05, 0.25, num=4, endpoint=True)
    alphas = np.linspace(0., np.pi, num=4, endpoint=True)
    gammas = np.linspace(0., 2. * np.pi, num=8, endpoint=True)