def test_rn_fit_fix_params():
    """ Test the fitting routine with fixed parameters. """

    # 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 = {'b': 200., 'off': 7., 'sigma': 80.}
    rician_norm = RicianNormalization(T2WModality(), params=params)
    rician_norm.fit(t2w_mod, gt_mod, label_gt[0])
    assert_almost_equal(rician_norm.fit_params_['b'], 1.4463929678319398,
                        decimal=DECIMAL_PRECISON)
    assert_almost_equal(rician_norm.fit_params_['off'], 0.12668917318976813,
                        decimal=DECIMAL_PRECISON)
    assert_almost_equal(rician_norm.fit_params_['sigma'], 0.10331905081688209,
                        decimal=DECIMAL_PRECISON)
예제 #2
0
def find_normalization_params(pat_t2w, pat_gt, label):
    # Create the normalization object and load the model
    t2w_norm = RicianNormalization(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_rn_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()

    rician_norm = RicianNormalization(T2WModality())
    rician_norm.fit(t2w_mod)
    assert_almost_equal(rician_norm.fit_params_['b'], 0.17064341621605977,
                        decimal=DECIMAL_PRECISON)
    assert_almost_equal(rician_norm.fit_params_['off'], 0.0,
                        decimal=DECIMAL_PRECISON)
    assert_almost_equal(rician_norm.fit_params_['sigma'], 0.15609648503515802,
                        decimal=DECIMAL_PRECISON)
def test_rn_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
    rician_norm = RicianNormalization(T2WModality())
    rician_norm.fit(t2w_mod, gt_mod, 'prostate')

    t2w_mod = rician_norm.normalize(t2w_mod)

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

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

    # Check that the data are equal to the original data
    data = np.load(os.path.join(currdir, 'data',
                                'data_rician_denormalize.npy'))
    assert_array_equal(t2w_mod.data_, data)
    data = np.load(os.path.join(currdir, 'data', 'pdf_rician_denormalize.npy'))
    assert_array_equal(t2w_mod.pdf_, data)
def test_rn_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)

    rician_norm = RicianNormalization(T2WModality())
    rician_norm.params = 'None'
    assert_raises(ValueError, rician_norm.fit, t2w_mod, gt_mod, label_gt[0])
def test_rn_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
    rician_norm = RicianNormalization(T2WModality())
    rician_norm.fit(t2w_mod, gt_mod, 'prostate')

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

    # Load the object
    rn_2 = RicianNormalization.load_from_pickles(filename)

    # Check that the different variables are the same
    assert_equal(type(rn_2.base_modality_), type(rician_norm.base_modality_))
    assert_equal(rn_2.fit_params_['b'], rician_norm.fit_params_['b'])
    assert_equal(rn_2.fit_params_['off'], rician_norm.fit_params_['off'])
    assert_equal(rn_2.fit_params_['sigma'], rician_norm.fit_params_['sigma'])
    assert_equal(rn_2.is_fitted_, rician_norm.is_fitted_)
    assert_array_equal(rn_2.roi_data_, rician_norm.roi_data_)
예제 #7
0
    # Read the image data
    t2w_mod = T2WModality()
    t2w_mod.read_data_from_path(p_t2w)

    # Read the GT
    gt_mod = GTModality()
    gt_mod.read_data_from_path(label_gt, p_gt)

    if not nb_patient == ID_GAUSSIAN:
        # Rician Normalization

        # 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)