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_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_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)
Esempio n. 4
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)
Esempio n. 5
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)
def test_walk_through_graph_shortest_path():
    """Test the routine to go through the graph using shortest path."""

    # 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 = [10] * 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
    heatmap_inv_exp = np.exp(img_as_float(1. - (heatmap / np.max(heatmap))))
    graph = StandardTimeNormalization._build_graph(heatmap_inv_exp, .99)

    start_end_tuple = ((0, 6), (3, 6))

    # Call the algorithm to walk through the graph
    path = StandardTimeNormalization._walk_through_graph(graph,
                                                         heatmap_inv_exp,
                                                         start_end_tuple,
                                                         'shortest-path')

    gt_path = np.array([[0, 6], [1, 6], [2, 6], [3, 6]])
    assert_array_equal(path, gt_path)
    # 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])
    print 'Data and label extracted for the current patient ...'

n_jobs = 48
config = [{
    'classifier_str': 'random-forest',
    'n_estimators': 100,
    'gs_n_jobs': n_jobs
}]

result_config = []
for c in config:
    print 'The following configuration will be used: {}'.format(c)
    # 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(' ', '_') +
                      '_hoffmann.npy')

    # Concatenate the training data
    # Select the parameter kep
    data.append(np.load(os.path.join(path_hoffmann, filename_hoffmann))[:, 2])
    # 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])
    print 'Data and label extracted for the current patient ...'

n_jobs = 48
config = [{'classifier_str': 'random-forest', 'n_estimators': 100,
           'gs_n_jobs': n_jobs}]

result_config = []
for c in config:
    print 'The following configuration will be used: {}'.format(c)

    result_cv = []
    # Go for LOPO cross-validation
Esempio n. 9
0
        # 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)

    # Get the roi corresponding to the ground truth
    roi_idx = gt_mod.extract_gt_data(label_gt[0])

    # Append the minimum
    list_min.append(np.min(t2w_mod.data_[roi_idx]))
    list_max.append(np.max(t2w_mod.data_[roi_idx]))

print 'The min and max to consider for the whole dataset is {} - {}'.format(
    min(list_min), max(list_max))
Esempio n. 10
0
path_gt = [
    '/data/prostate/experiments/Patient 1041/GT_inv/prostate',
    '/data/prostate/experiments/Patient 1041/GT_inv/pz',
    '/data/prostate/experiments/Patient 1041/GT_inv/cg',
    '/data/prostate/experiments/Patient 1041/GT_inv/cap'
]
label_gt = ['prostate', 'pz', 'cg', 'cap']
gt_mod = GTModality()
gt_mod.read_data_from_path(label_gt, path_gt)

# Create an empty volume of the size of the modality data
prob_vol = np.zeros(t2w_mod.data_.shape)
prob_ca = np.zeros(t2w_mod.data_.shape)

# Extract the index of the prostate index
prostate_idx = np.array(gt_mod.extract_gt_data('prostate'))
ca_idx = np.array(gt_mod.extract_gt_data('cap'))

for ii in range(ca_idx.shape[1]):
    coord = ca_idx[:, ii]
    prob_ca[coord[0], coord[1], coord[2]] = 1

# Assign the value in the volume
for ii in range(prob_cancer.size):
    coord = prostate_idx[:, ii]
    prob_vol[coord[0], coord[1], coord[2]] = prob_cancer[ii]

idx_sl = 30

plt.figure()
plt.imshow(t2w_mod.data_[:, :, idx_sl])
Esempio n. 11
0
# Load the ground truth
# Define the path of the ground for the prostate
path_gt = ['/data/prostate/experiments/Patient 1041/GT_inv/prostate',
           '/data/prostate/experiments/Patient 1041/GT_inv/pz',
           '/data/prostate/experiments/Patient 1041/GT_inv/cg',
           '/data/prostate/experiments/Patient 1041/GT_inv/cap']
label_gt = ['prostate', 'pz', 'cg', 'cap']
gt_mod = GTModality()
gt_mod.read_data_from_path(label_gt, path_gt)

# Create an empty volume of the size of the modality data
prob_vol = np.zeros(t2w_mod.data_.shape)
prob_ca = np.zeros(t2w_mod.data_.shape)

# Extract the index of the prostate index
prostate_idx = np.array(gt_mod.extract_gt_data('prostate'))
ca_idx = np.array(gt_mod.extract_gt_data('cap'))

for ii in range(ca_idx.shape[1]):
    coord = ca_idx[:, ii]
    prob_ca[coord[0], coord[1], coord[2]] = 1

# Assign the value in the volume
for ii in range(prob_cancer.size):
    coord = prostate_idx[:, ii]
    prob_vol[coord[0], coord[1], coord[2]] = prob_cancer[ii]

idx_sl = 30

plt.figure()
plt.imshow(t2w_mod.data_[:, :, idx_sl])