def test_truncate_range(): """Test range truncation.""" # Given data = np.random.random(100) data.ravel()[np.random.choice(data.size, 10, replace=False)] = 0 data.ravel()[np.random.choice(data.size, 5, replace=False)] = np.nan p_min, p_max = 2.5, 97.5 expected = np.nanpercentile(data, [p_min, p_max]) # When output, _, _ = truncate_range(data, percMin=p_min, percMax=p_max, discard_zeros=False) # Then assert all(np.nanpercentile(output, [0, 100]) == expected)
tmp2 = np.zeros((tmp.shape)) for ind2, val in enumerate(uniqueVals): tmp2[tmp == val] = newVals[ind2] counter = counter + nrUniqueVals ncut_labels[:, :, ind] = tmp2 lMax = np.max(ncut_labels) orig_ncut_labels = ncut_labels.copy() ima_ncut_labels = ncut_labels.copy() # """Data Processing""" orig, dims = check_data(nii.get_data(), cfg.force_original_precision) # Save min and max truncation thresholds to be used in axis labels orig, pMin, pMax = truncate_range(orig, percMin=cfg.perc_min, percMax=cfg.perc_max) # Continue with scaling the original truncated image and recomputing gradient orig = scale_range(orig, scale_factor=cfg.scale, delta=0.0001) gra = set_gradient_magnitude(orig, cfg.gramag) if cfg.export_gramag: export_gradient_magnitude_image(gra, nii.get_filename(), nii.affine) # Reshape ima (more intuitive for voxel-wise operations) ima = np.ndarray.flatten(orig) gra = np.ndarray.flatten(gra) # """Plots""" print("Preparing GUI...") # Plot 2D histogram fig = plt.figure(facecolor='0.775')
from nibabel import load from segmentator.utils import map_ima_to_2D_hist, prep_2D_hist from segmentator.utils import truncate_range, scale_range, check_data from segmentator.utils import set_gradient_magnitude from segmentator.utils import export_gradient_magnitude_image from segmentator.gui_utils import sector_mask, responsiveObj from segmentator.config_gui import palette, axcolor, hovcolor # """Data Processing""" nii = load(cfg.filename) orig, dims = check_data(nii.get_data(), cfg.force_original_precision) # Save min and max truncation thresholds to be used in axis labels if np.isnan(cfg.valmin) or np.isnan(cfg.valmax): orig, pMin, pMax = truncate_range(orig, percMin=cfg.perc_min, percMax=cfg.perc_max) else: # TODO: integrate this into truncate range function orig[orig < cfg.valmin] = cfg.valmin orig[orig > cfg.valmax] = cfg.valmax pMin, pMax = cfg.valmin, cfg.valmax # Continue with scaling the original truncated image and recomputing gradient orig = scale_range(orig, scale_factor=cfg.scale, delta=0.0001) gra = set_gradient_magnitude(orig, cfg.gramag) if cfg.export_gramag: export_gradient_magnitude_image(gra, nii.get_filename(), cfg.gramag, nii.affine) # Reshape for voxel-wise operations ima = np.copy(orig.flatten()) gra = gra.flatten()