def test_partial_fit_model_dict_wrong_type():
    """Test either if an error is raised when a parameters is a wrong
     type in the dictionary."""

    # Load the data with only a single serie
    currdir = os.path.dirname(os.path.abspath(__file__))
    path_data = os.path.join(currdir, 'data', 'dce')
    # Create an object to handle the data
    dce_mod = DCEModality()

    # Read the data
    dce_mod.read_data_from_path(path_data)

    # Load the GT data
    path_gt = [os.path.join(currdir, 'data', 'gt_folders', 'prostate')]
    label_gt = ['prostate']
    gt_mod = GTModality()
    gt_mod.read_data_from_path(label_gt, path_gt)

    # Create the object to make the normalization
    stn = StandardTimeNormalization(dce_mod)
    params = {'std': 50., 'exp': 25., 'alpha': .9, 'max_iter': 5.}
    assert_raises(ValueError, stn.partial_fit_model, dce_mod,
                  ground_truth=gt_mod, cat=label_gt[0], params=params)

    params = {'std': 50., 'exp': 25, 'alpha': .9, 'max_iter': 5}
    assert_raises(ValueError, stn.partial_fit_model, dce_mod,
                  ground_truth=gt_mod, cat=label_gt[0], params=params)
Example #2
0
def test_read_gt_data_path_list_constructor():
    """ Test if we can read gt series. """

    # Load the data with only a single serie
    currdir = os.path.dirname(os.path.abspath(__file__))
    path_data = os.path.join(currdir, 'data', 'gt_folders')
    path_data_list = [os.path.join(path_data, 'prostate'),
                      os.path.join(path_data, 'cg'),
                      os.path.join(path_data, 'pz'),
                      os.path.join(path_data, 'cap')]
    # Give the list for the ground_truth
    label = ['prostate', 'cg', 'pz', 'cap']
    # Create an object to handle the data
    gt_mod = GTModality(path_data_list)

    # Check that the data have been read
    assert_true(not gt_mod.is_read())

    gt_mod.read_data_from_path(label)

    # Check that the data have been read
    assert_true(gt_mod.is_read())

    # Check the data here
    data = np.load(os.path.join(currdir, 'data', 'gt_path_list.npy'))
    assert_array_equal(gt_mod.data_, data)
    assert_equal(gt_mod.n_serie_, 4)
Example #3
0
def test_extract_index():
    """ Test if the indexes of a GT will be well extracted. """

    # Load the data with only a single serie
    currdir = os.path.dirname(os.path.abspath(__file__))
    path_data = os.path.join(currdir, 'data', 'gt_folders')
    path_data_list = [os.path.join(path_data, 'prostate'),
                      os.path.join(path_data, 'cg'),
                      os.path.join(path_data, 'pz'),
                      os.path.join(path_data, 'cap')]
    # Give the list for the ground_truth
    label = ['prostate', 'cg', 'pz', 'cap']
    # Create an object to handle the data
    gt_mod = GTModality()

    # Read the data
    gt_mod.read_data_from_path(label, path_data=path_data_list)

    # Extract the prostate indexes
    label_extr = 'prostate'
    idx_prostate = gt_mod.extract_gt_data(label_extr, 'index')
    data = np.load(os.path.join(currdir, 'data', 'extract_gt_index.npy'))
    # Check each table
    for idx_arr, test_arr in zip(idx_prostate, data):
        assert_array_equal(idx_arr, test_arr)
def test_denormalize_wt_fitting():
    """Test either an error is raised if the data are not fitted first."""
    # 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())
    assert_raises(ValueError, gaussian_norm.denormalize, t2w_mod)
def test_partial_fit_model_2():
    """Test the routine to fit two models."""

    # Load the data with only a single serie
    currdir = os.path.dirname(os.path.abspath(__file__))
    path_data = os.path.join(currdir, 'data', 'full_dce')
    # Create an object to handle the data
    dce_mod = DCEModality()

    # Read the data
    dce_mod.read_data_from_path(path_data)

    # Load the GT data
    path_gt = [os.path.join(currdir, 'data', 'full_gt', 'prostate')]
    label_gt = ['prostate']
    gt_mod = GTModality()
    gt_mod.read_data_from_path(label_gt, path_gt)

    # Create the object to make the normalization
    stn = StandardTimeNormalization(dce_mod)
    stn.partial_fit_model(dce_mod, gt_mod, label_gt[0])
    stn.partial_fit_model(dce_mod, gt_mod, label_gt[0])

    # Check the model computed
    model_gt = np.array([22.26479174, 22.51070962, 24.66027277, 23.43488237,
                         23.75601817, 22.56173871, 26.86244505, 45.06227804,
                         62.34273874, 71.35327656])
    assert_array_almost_equal(stn.model_, model_gt, decimal=PRECISION_DECIMAL)
    assert_true(stn.is_model_fitted_)
def test_build_graph():
    """Test the method to build a graph from the heatmap."""

    # Load the data with only a single serie
    currdir = os.path.dirname(os.path.abspath(__file__))
    path_data = os.path.join(currdir, 'data', 'dce')
    # Create an object to handle the data
    dce_mod = DCEModality()

    # Read the data
    dce_mod.read_data_from_path(path_data)

    # Load the GT data
    path_gt = [os.path.join(currdir, 'data', 'gt_folders', 'prostate')]
    label_gt = ['prostate']
    gt_mod = GTModality()
    gt_mod.read_data_from_path(label_gt, path_gt)

    # Build a heatmap from the dce data
    # Reduce the number of bins to enforce low memory consumption
    nb_bins = [100] * dce_mod.n_serie_
    heatmap, bins_heatmap = dce_mod.build_heatmap(gt_mod.extract_gt_data(
        label_gt[0]), nb_bins=nb_bins)

    # Build the graph by taking the inverse exponential of the heatmap
    graph = StandardTimeNormalization._build_graph(heatmap, .5)
    graph_dense = graph.toarray()

    data = np.load(os.path.join(currdir, 'data', 'graph.npy'))
    assert_array_equal(graph_dense, data)
def test_shift_heatmap():
    """Test the routine which shift the heatmap."""

    # Load the data with only a single serie
    currdir = os.path.dirname(os.path.abspath(__file__))
    path_data = os.path.join(currdir, 'data', 'dce')
    # Create an object to handle the data
    dce_mod = DCEModality()

    # Read the data
    dce_mod.read_data_from_path(path_data)

    # Load the GT data
    path_gt = [os.path.join(currdir, 'data', 'gt_folders', 'prostate')]
    label_gt = ['prostate']
    gt_mod = GTModality()
    gt_mod.read_data_from_path(label_gt, path_gt)

    # Build a heatmap from the dce data
    # Reduce the number of bins to enforce low memory consumption
    nb_bins = [100] * dce_mod.n_serie_
    heatmap, bins_heatmap = dce_mod.build_heatmap(gt_mod.extract_gt_data(
        label_gt[0]), nb_bins=nb_bins)

    # Create a list of shift which do not have the same number of entries
    # than the heatmap - There is 4 series, let's create only 2
    shift_arr = np.array([10] * 4)

    heatmap_shifted = StandardTimeNormalization._shift_heatmap(heatmap,
                                                               shift_arr)

    data = np.load(os.path.join(currdir, 'data', 'heatmap_shifted.npy'))
    assert_array_equal(heatmap_shifted, data)
def test_ese_transform_gt_cat():
    """Test the transform routine with a given ground-truth."""

    # Create the normalization object with the right modality
    dce_ese = EnhancementSignalExtraction(DCEModality())

    # Try to fit an object with another modality
    currdir = os.path.dirname(os.path.abspath(__file__))
    path_data = os.path.join(currdir, 'data', 'dce')
    # Create an object to handle the data
    dce_mod = DCEModality()
    dce_mod.read_data_from_path(path_data)

    # Load the GT data
    path_gt = [os.path.join(currdir, 'data', 'gt_folders', 'prostate')]
    label_gt = ['prostate']
    gt_mod = GTModality()
    gt_mod.read_data_from_path(label_gt, path_gt)

    # Fit and raise the error
    data = dce_ese.transform(dce_mod, gt_mod, label_gt[0])

    # Check the size of the data
    assert_equal(data.shape, (12899, 4))
    # Check the hash of the data
    data.flags.writeable = False
    assert_equal(hash(data.data), -3808597525488161265)
def test_gn_wrong_size_gt():
    """ Test either if an error is raised when the size of the ground-truth
    is different from the size of the base modality. """

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

    # Change the size of the data of the modality
    t2w_mod.data_ = t2w_mod.data_[:-1, :, :]

    gaussian_norm = GaussianNormalization(T2WModality())
    assert_raises(ValueError, gaussian_norm.fit, t2w_mod, gt_mod, label_gt[0])
def test_qte_transform_regular():
    """Test the transform function for regular model."""

    # Try to fit an object with another modality
    currdir = os.path.dirname(os.path.abspath(__file__))
    path_data = os.path.join(currdir,
                             '../../preprocessing/tests/data/full_dce')
    # Create an object to handle the data
    dce_mod = DCEModality()
    dce_mod.read_data_from_path(path_data)
    # Create the gt data
    gt_mod = GTModality()
    gt_cat = ['cap']
    path_data = [os.path.join(
        currdir,
        '../../preprocessing/tests/data/full_gt/cap')]
    gt_mod.read_data_from_path(gt_cat, path_data)

    # Create the object for the Tofts extraction
    tqe = ToftsQuantificationExtraction(DCEModality(), 1.6, 3.5,
                                        random_state=RND_SEED)
    tqe.fit(dce_mod)
    data = tqe.transform(dce_mod, gt_mod, gt_cat[0], kind='regular')

    data_gt = np.load(os.path.join(currdir, 'data/tofts_reg_data.npy'))
    assert_array_almost_equal(data, data_gt, decimal=DECIMAL_PRECISION)
def test_fit():
    """Test the routine to fit the parameters of the dce normalization."""

    # Load the data with only a single serie
    currdir = os.path.dirname(os.path.abspath(__file__))
    path_data = os.path.join(currdir, 'data', 'full_dce')
    # Create an object to handle the data
    dce_mod = DCEModality()

    # Read the data
    dce_mod.read_data_from_path(path_data)

    # Load the GT data
    path_gt = [os.path.join(currdir, 'data', 'full_gt', 'prostate')]
    label_gt = ['prostate']
    gt_mod = GTModality()
    gt_mod.read_data_from_path(label_gt, path_gt)

    # Create the object to make the normalization
    stn = StandardTimeNormalization(dce_mod)

    # Create a synthetic model to fit on
    stn.model_ = np.array([30., 30., 32., 31., 31., 30., 35., 55., 70., 80.])
    stn.is_model_fitted_ = True

    # Fit the parameters on the model
    stn.fit(dce_mod, gt_mod, label_gt[0])

    assert_almost_equal(stn.fit_params_['scale-int'], 1.2296657327848537,
                        decimal=PRECISION_DECIMAL)
    assert_equal(stn.fit_params_['shift-time'], 0.0)
    data = np.array([191.29, 193.28, 195.28, 195.28, 195.28, 197.28, 213.25,
                     249.18, 283.12, 298.10])
    assert_array_almost_equal(stn.fit_params_['shift-int'], data,
                              decimal=PRECISION_DECIMAL)
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)
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_save_model_wrong_ext():
    """Test either if an error is raised if the filename as a wrong
    extension while storing the model."""

    # Load the data with only a single serie
    currdir = os.path.dirname(os.path.abspath(__file__))
    path_data = os.path.join(currdir, 'data', 'full_dce')
    # Create an object to handle the data
    dce_mod = DCEModality()

    # Read the data
    dce_mod.read_data_from_path(path_data)

    # Load the GT data
    path_gt = [os.path.join(currdir, 'data', 'full_gt', 'prostate')]
    label_gt = ['prostate']
    gt_mod = GTModality()
    gt_mod.read_data_from_path(label_gt, path_gt)

    # Create the object to make the normalization
    stn = StandardTimeNormalization(dce_mod)
    stn.partial_fit_model(dce_mod, gt_mod, label_gt[0])

    # Try to store the file not with an npy file
    assert_raises(ValueError, stn.save_model, os.path.join(currdir, 'data',
                                                           'model.rnd'))
def test_shift_heatmap_wrong_shift():
    """Test if an error is raised when the shidt provided is not consistent."""

    # Load the data with only a single serie
    currdir = os.path.dirname(os.path.abspath(__file__))
    path_data = os.path.join(currdir, 'data', 'dce')
    # Create an object to handle the data
    dce_mod = DCEModality()

    # Read the data
    dce_mod.read_data_from_path(path_data)

    # Load the GT data
    path_gt = [os.path.join(currdir, 'data', 'gt_folders', 'prostate')]
    label_gt = ['prostate']
    gt_mod = GTModality()
    gt_mod.read_data_from_path(label_gt, path_gt)

    # Build a heatmap from the dce data
    # Reduce the number of bins to enforce low memory consumption
    nb_bins = [100] * dce_mod.n_serie_
    heatmap, bins_heatmap = dce_mod.build_heatmap(gt_mod.extract_gt_data(
        label_gt[0]), nb_bins=nb_bins)

    # Create a list of shift which do not have the same number of entries
    # than the heatmap - There is 4 series, let's create only 2
    shift_arr = np.array([10] * 2)

    assert_raises(ValueError, StandardTimeNormalization._shift_heatmap,
                  heatmap, shift_arr)
Example #16
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
Example #17
0
def test_check_modality_gt_not_read():
    """Test if an error is raised when the gt is not read."""

    # Try to fit an object with another modality
    currdir = os.path.dirname(os.path.abspath(__file__))
    path_data = os.path.join(currdir, 'data', 't2w')
    # Create an object to handle the data
    t2w_mod = T2WModality()

    # Load the GT data
    path_gt = [os.path.join(currdir, 'data', 'full_gt', 'prostate')]
    label_gt = ['prostate']
    gt_mod = GTModality()
    gt_mod.read_data_from_path(label_gt, path_gt)

    assert_raises(ValueError, check_modality_gt, t2w_mod, gt_mod, label_gt[0])
def test_normalize_denormalize_3():
    """Test the data normalization and denormalization with shift > 0."""

    # Load the data with only a single serie
    currdir = os.path.dirname(os.path.abspath(__file__))
    path_data = os.path.join(currdir, 'data', 'full_dce')
    # Create an object to handle the data
    dce_mod = DCEModality()

    # Read the data
    dce_mod.read_data_from_path(path_data)

    # Load the GT data
    path_gt = [os.path.join(currdir, 'data', 'full_gt', 'prostate')]
    label_gt = ['prostate']
    gt_mod = GTModality()
    gt_mod.read_data_from_path(label_gt, path_gt)

    # Create the object to make the normalization
    stn = StandardTimeNormalization(dce_mod)

    # Simulate that we fitted the data
    stn.model_ = np.array([30., 30., 32., 31., 31., 30., 35., 55., 70., 80.])
    stn.is_model_fitted_ = True
    stn.fit_params_ = {'scale-int': 1.2296657327848537,
                       'shift-time': 3.0,
                       'shift-int': np.array([191.29, 193.28, 195.28, 195.28,
                                              195.28, 197.28, 213.25, 249.18,
                                              283.12, 298.10])}
    stn.is_fitted_ = True

    # Store the data somewhere
    data_gt_cp = dce_mod.data_.copy()

    # Normalize the data
    dce_mod_norm = stn.normalize(dce_mod)

    # Check if the data are properly normalized
    dce_mod_norm.data_.flags.writeable = False
    data = np.load(os.path.join(currdir, 'data', 'data_normalized_dce_3.npy'))
    assert_equal(hash(dce_mod_norm.data_.data), data)

    dce_mod_norm.data_.flags.writeable = True

    dce_mod_2 = stn.denormalize(dce_mod_norm)
    dce_mod_2.data_.flags.writeable = False
    assert_equal(hash(dce_mod_2.data_.data), -3781160829709175881)
def find_normalization_params(pat_dce, pat_gt, label, pat_model):
    # Create the normalization object and load the model
    dce_norm = StandardTimeNormalization(DCEModality())
    dce_norm.load_model(pat_model)

    # Read the DCE
    dce_mod = DCEModality()
    dce_mod.read_data_from_path(pat_dce)

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

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

    return dce_norm
Example #20
0
def find_normalization_params(pat_dce, pat_gt, label, pat_model):
    # Create the normalization object and load the model
    dce_norm = StandardTimeNormalization(DCEModality())
    dce_norm.load_model(pat_model)

    # Read the DCE
    dce_mod = DCEModality()
    dce_mod.read_data_from_path(pat_dce)

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

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

    return dce_norm
def find_normalization_params(pat_adc, pat_gt, label, pat_model):
    # Create the normalization object and load the model
    adc_norm = PiecewiseLinearNormalization(ADCModality())
    adc_norm.load_model(pat_model)

    # Read the ADC
    adc_mod = ADCModality()
    adc_mod.read_data_from_path(pat_adc)

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

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

    return adc_norm
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)
Example #23
0
def test_read_gt_path():
    """ Test if we can read data from the same folder organized with
    different serie ID. """

    # Load the data with only a single serie
    currdir = os.path.dirname(os.path.abspath(__file__))
    path_data = os.path.join(currdir, 'data', 'dce')

    # Give the list for the ground_truth
    label = ['prostate', 'pz']
    # Create an object to handle the data
    gt_mod = GTModality()

    # Read the data
    gt_mod.read_data_from_path(label, path_data=path_data)

    # Check the data here
    data = np.load(os.path.join(currdir, 'data', 'gt_path.npy'))
    assert_array_equal(gt_mod.data_, data)
    assert_equal(gt_mod.n_serie_, 2)
Example #24
0
def test_extract_wrong_output_type():
    """ Test either if an error is raised if the type of output is unknown. """

    # Load the data with only a single serie
    currdir = os.path.dirname(os.path.abspath(__file__))
    path_data = os.path.join(currdir, 'data', 'gt_folders')
    path_data_list = [os.path.join(path_data, 'prostate'),
                      os.path.join(path_data, 'cg'),
                      os.path.join(path_data, 'pz'),
                      os.path.join(path_data, 'cap')]
    # Give the list for the ground_truth
    label = ['prostate', 'cg', 'pz', 'cap']
    # Create an object to handle the data
    gt_mod = GTModality()

    # Read the data
    gt_mod.read_data_from_path(label, path_data=path_data_list)

    # Extract the prostate indexes
    label_extr = 'prostate'
    assert_raises(ValueError, gt_mod.extract_gt_data, label_extr, 'rnd')
def test_rn_fit_wrong_gt():
    """ Test either if an error is raised when the wrong class is provided for
    the 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()

    # 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())
    assert_raises(ValueError, rician_norm.fit, t2w_mod, t2w_mod, label_gt[0])
def test_partial_fit_model_wrong_string():
    """Test either if an error is raised when the string is unknown."""

    # Load the data with only a single serie
    currdir = os.path.dirname(os.path.abspath(__file__))
    path_data = os.path.join(currdir, 'data', 'dce')
    # Create an object to handle the data
    dce_mod = DCEModality()

    # Read the data
    dce_mod.read_data_from_path(path_data)

    # Load the GT data
    path_gt = [os.path.join(currdir, 'data', 'gt_folders', 'prostate')]
    label_gt = ['prostate']
    gt_mod = GTModality()
    gt_mod.read_data_from_path(label_gt, path_gt)

    # Create the object to make the normalization
    stn = StandardTimeNormalization(dce_mod)
    assert_raises(ValueError, stn.partial_fit_model, dce_mod,
                  ground_truth=gt_mod, cat=label_gt[0], params='rnd')
def test_ese_wrong_gt_size():
    """Test if an error is raised when the size of gt is not consistent
    with the modality."""

    # Create the normalization object with the right modality
    dce_ese = EnhancementSignalExtraction(DCEModality())

    # Try to fit an object with another modality
    currdir = os.path.dirname(os.path.abspath(__file__))
    path_data = os.path.join(currdir, 'data', 'dce')
    # Create an object to handle the data
    dce_mod = DCEModality()
    dce_mod.read_data_from_path(path_data)

    # Load the GT data
    path_gt = [os.path.join(currdir, 'data', 'full_gt', 'prostate')]
    label_gt = ['prostate']
    gt_mod = GTModality()
    gt_mod.read_data_from_path(label_gt, path_gt)

    # Fit and raise the error
    assert_raises(ValueError, dce_ese.transform, dce_mod, gt_mod, label_gt[0])
def test_gn_wrong_label_gt():
    """ Test either if an error is raised when the category label asked was
    not load by the GTModality. """

    # 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())
    assert_raises(ValueError, gaussian_norm.fit, t2w_mod, gt_mod, 'None')
def test_ese_transform_gt_no_cat():
    """Test eihter if an error is raised when no category for GT is
    provided."""

    # Create the normalization object with the right modality
    dce_ese = EnhancementSignalExtraction(DCEModality())

    # Try to fit an object with another modality
    currdir = os.path.dirname(os.path.abspath(__file__))
    path_data = os.path.join(currdir, 'data', 'dce')
    # Create an object to handle the data
    dce_mod = DCEModality()
    dce_mod.read_data_from_path(path_data)

    # Load the GT data
    path_gt = [os.path.join(currdir, 'data', 'gt_folders', 'prostate')]
    label_gt = ['prostate']
    gt_mod = GTModality()
    gt_mod.read_data_from_path(label_gt, path_gt)

    # Fit and raise the error
    assert_raises(ValueError, dce_ese.transform, dce_mod, gt_mod)
def test_fit_not_read_mod():
    """Test either if an error is raised when the modality was
    not opened."""

    # Load the data with only a single serie
    currdir = os.path.dirname(os.path.abspath(__file__))
    path_data = os.path.join(currdir, 'data', 'full_dce')
    # Create an object to handle the data
    dce_mod = DCEModality()

    # Load the GT data
    path_gt = [os.path.join(currdir, 'data', 'full_gt', 'prostate')]
    label_gt = ['prostate']
    gt_mod = GTModality()

    # Read the ground-truth
    gt_mod.read_data_from_path(label_gt, path_gt)

    # Create the object to make the normalization
    stn = StandardTimeNormalization(dce_mod)
    assert_raises(ValueError, stn.partial_fit_model, dce_mod,
                  gt_mod, label_gt[0])
def test_partial_fit_model_wt_label_gt():
    """Test either if a warning is raised when a gt is not provided
    and a cat is."""

    # Load the data with only a single serie
    currdir = os.path.dirname(os.path.abspath(__file__))
    path_data = os.path.join(currdir, 'data', 'full_dce')
    # Create an object to handle the data
    dce_mod = DCEModality()

    # Read the data
    dce_mod.read_data_from_path(path_data)

    # Load the GT data
    path_gt = [os.path.join(currdir, 'data', 'full_gt', 'prostate')]
    label_gt = ['prostate']
    gt_mod = GTModality()
    gt_mod.read_data_from_path(label_gt, path_gt)

    # Create the object to make the normalization
    stn = StandardTimeNormalization(dce_mod)
    assert_raises(ValueError, stn.partial_fit_model, dce_mod, gt_mod)
Example #32
0
def test_extract_no_label_known():
    """Test either if an error is raised when no corresponding gt category
    is found."""

    # Load the data with only a single serie
    currdir = os.path.dirname(os.path.abspath(__file__))
    path_data = os.path.join(currdir, 'data', 'gt_folders')
    path_data_list = [os.path.join(path_data, 'prostate'),
                      os.path.join(path_data, 'cg'),
                      os.path.join(path_data, 'pz'),
                      os.path.join(path_data, 'cap')]
    # Give the list for the ground_truth
    label = ['prostate', 'cg', 'pz', 'cap']
    # Create an object to handle the data
    gt_mod = GTModality()

    # Read the data
    gt_mod.read_data_from_path(label, path_data=path_data_list)

    # Try to extract a wrong label
    label_extr = 'rnd'
    assert_raises(ValueError, gt_mod.extract_gt_data, label_extr)
Example #33
0
def test_get_pdf_roi():
    """Test the routine to get pdf and bins with a given roi."""

    # Load the data with only a single serie
    currdir = os.path.dirname(os.path.abspath(__file__))
    path_data = os.path.join(currdir, 'data', 't2w')
    # Create an object to handle the data
    t2w_mod = T2WModality()

    t2w_mod.read_data_from_path(path_data)

    path_data = os.path.join(currdir, 'data', 'gt_folders')
    path_data_list = [os.path.join(path_data, 'prostate'),
                      os.path.join(path_data, 'cg'),
                      os.path.join(path_data, 'pz'),
                      os.path.join(path_data, 'cap')]
    # Give the list for the ground_truth
    label = ['prostate', 'cg', 'pz', 'cap']
    # Create an object to handle the data
    gt_mod = GTModality()

    # Read the data
    gt_mod.read_data_from_path(label, path_data=path_data_list)

    # Extract the prostate indexes
    label_extr = 'prostate'
    data_prostate = gt_mod.extract_gt_data(label_extr, 'index')

    # Compute the hisogram with a wrong argument as string
    pdf_data, bin_data = t2w_mod.get_pdf(roi_data=data_prostate)

    # Check that the data correspond to the one save inside the the test
    data = np.load(os.path.join(currdir, 'data',
                                'bin_t2w_get_pdf_roi.npy'))
    assert_array_equal(bin_data, data)
    data = np.load(os.path.join(currdir, 'data',
                                'pdf_t2w_get_pdf_roi.npy'))
    assert_array_equal(pdf_data, data)
Example #34
0
# Define the path for the DCE
path_dce = '/data/prostate/experiments/Patient 383/DCE'

# Define the list of path for the GT
path_gt = ['/data/prostate/experiments/Patient 383/GT_inv/prostate']
# Define the associated list of label for the GT
label_gt = ['prostate']

# Read the DCE
dce_mod = DCEModality()
dce_mod.read_data_from_path(path_dce)

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

# Create the object to normalize the DCE data
dce_norm = StandardTimeNormalization(dce_mod)
# Fit the data to get the normalization parameters
dce_norm.partial_fit_model(dce_mod, ground_truth=gt_mod,
                           cat='prostate')

print dce_norm.model_

# Define the path for the DCE
path_dce = '/data/prostate/experiments/Patient 387/DCE'

# Define the list of path for the GT
path_gt = ['/data/prostate/experiments/Patient 387/GT_inv/prostate']
# Define the associated list of label for the GT
]
for id_patient in id_patient_list:
    # Append for the GT data - Note that we need a list of gt path
    path_patients_list_gt.append(
        [os.path.join(path_patients, id_patient, gt) for gt in path_gt])

# Load all the data once. Splitting into training and testing will be done at
# the cross-validation time
data = []
label = []
for idx_pat in range(len(id_patient_list)):
    print 'Read patient {}'.format(id_patient_list[idx_pat])

    # Create the corresponding ground-truth
    gt_mod = GTModality()
    gt_mod.read_data_from_path(label_gt, path_patients_list_gt[idx_pat])
    print 'Read the GT data for the current patient ...'

    # Load the approproate normalization object
    filename_hoffmann = (id_patient_list[idx_pat].lower().replace(' ', '_') +
                         '_semi.npy')

    # Concatenate the training data
    # Select only the wash-in parameter
    data.append(np.load(os.path.join(path_hoffmann, filename_hoffmann))[:, 0])
    # Extract the corresponding ground-truth for the testing data
    # Get the index corresponding to the ground-truth
    roi_prostate = gt_mod.extract_gt_data('prostate', output_type='index')
    # Get the label of the gt only for the prostate ROI
    gt_cap = gt_mod.extract_gt_data('cap', output_type='data')
    label.append(gt_cap[roi_prostate])