def test_avg(): # label image # ftm: off label_field = cp.asarray( [[1, 1, 1, 2], [1, 2, 2, 2], [3, 3, 4, 4]], dtype=np.uint8 ) # color image r = cp.asarray( [[1.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 0.0, 0.0]] ) g = cp.asarray( [[0.0, 0.0, 0.0, 1.0], [1.0, 1.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0]] ) b = cp.asarray( [[0.0, 0.0, 0.0, 1.0], [0.0, 1.0, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0]] ) image = cp.dstack((r, g, b)) # reference label-colored image rout = cp.asarray( [[0.5, 0.5, 0.5, 0.5], [0.5, 0.5, 0.5, 0.5], [0.0, 0.0, 0.0, 0.0]] ) gout = cp.asarray( [ [0.25, 0.25, 0.25, 0.75], [0.25, 0.75, 0.75, 0.75], [0.0, 0.0, 0.0, 0.0], ] ) bout = cp.asarray( [[0.0, 0.0, 0.0, 1.0], [0.0, 1.0, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0]] ) expected_out = cp.dstack((rout, gout, bout)) # ftm: on # test standard averaging out = label2rgb(label_field, image, kind="avg", bg_label=-1) assert_array_equal(out, expected_out) # test averaging with custom background value out_bg = label2rgb( label_field, image, bg_label=2, bg_color=(0, 0, 0), kind="avg" ) expected_out_bg = expected_out.copy() expected_out_bg[label_field == 2] = 0 assert_array_equal(out_bg, expected_out_bg) # test default background color out_bg = label2rgb(label_field, image, bg_label=2, kind="avg") assert_array_equal(out_bg, expected_out_bg)
def test_3d_cropped_camera_image(): a_black = crop(cp.asarray(camera()), ((206, 206), (206, 206))) a_black = cp.dstack([a_black, a_black, a_black]) a_white = invert(a_black) zeros = cp.zeros((100, 100, 3)) ones = cp.ones((100, 100, 3)) assert_allclose( meijering(a_black, black_ridges=True), meijering(a_white, black_ridges=False), ) assert_allclose( sato(a_black, black_ridges=True, mode="reflect"), sato(a_white, black_ridges=False, mode="reflect"), ) assert_allclose(frangi(a_black, black_ridges=True), zeros, atol=1e-3) assert_allclose(frangi(a_white, black_ridges=False), zeros, atol=1e-3) assert_allclose( hessian(a_black, black_ridges=True, mode="reflect"), ones, atol=1 - 1e-7 ) assert_allclose( hessian(a_white, black_ridges=False, mode="reflect"), ones, atol=1 - 1e-7, )
def curvature_to_height(image, h2, iterations=2000): f = image[..., 0] A = image[..., 3] u = cup.ones_like(f) * 0.5 k = 1 t = np.empty_like(u, dtype=np.float32) # periodic gauss seidel iteration for ic in range(iterations): if ic % 100 == 0: print(ic) # roll k, axis=0 t[:-k, :] = u[k:, :] t[-k:, :] = u[:k, :] # roll -k, axis=0 t[k:, :] += u[:-k, :] t[:k, :] += u[-k:, :] # roll k, axis=1 t[:, :-k] += u[:, k:] t[:, -k:] += u[:, :k] # roll -k, axis=1 t[:, k:] += u[:, :-k] t[:, :k] += u[:, -k:] t -= h2 * f t *= 0.25 u = t * A u = -u u -= cup.min(u) u /= cup.max(u) return cup.dstack([u, u, u, image[..., 3]])
def test_lab_full_gamut(self): a, b = cp.meshgrid(cp.arange(-100, 100), cp.arange(-100, 100)) L = cp.ones(a.shape) lab = cp.dstack((L, a, b)) for value in [0, 10, 20]: lab[:, :, 0] = value with expected_warnings(['Color data out of range']): lab2xyz(lab)
def test_adapthist_grayscale(): """Test a grayscale float image""" img = util.img_as_float(cp.array(data.astronaut())) img = rgb2gray(img) img = cp.dstack((img, img, img)) adapted = exposure.equalize_adapthist(img, kernel_size=(57, 51), clip_limit=0.01, nbins=128) assert img.shape == adapted.shape assert_almost_equal(float(peak_snr(img, adapted)), 100.140, 3) assert_almost_equal(float(norm_brightness_err(img, adapted)), 0.0529, 3)
def test_adapthist_alpha(): """Test an RGBA color image""" img = util.img_as_float(cp.array(data.astronaut())) alpha = cp.ones((img.shape[0], img.shape[1]), dtype=float) img = cp.dstack((img, alpha)) adapted = exposure.equalize_adapthist(img) assert adapted.shape != img.shape img = img[:, :, :3] full_scale = exposure.rescale_intensity(img) assert img.shape == adapted.shape assert_almost_equal(float(peak_snr(full_scale, adapted)), 109.393, 2) assert_almost_equal(float(norm_brightness_err(full_scale, adapted)), 0.0248, 3)
def normals_to_height(image, grid_steps, iterations=2000, intensity=1.0): # A = image[..., 3] ih, iw = image.shape[0], image.shape[1] u = cup.ones((ih, iw), dtype=np.float32) * 0.5 vectors = nmap_to_vectors(image) # vectors[..., 0] = 0.5 - image[..., 0] # vectors[..., 1] = image[..., 1] - 0.5 vectors *= intensity t = np.empty_like(u, dtype=np.float32) for k in range(grid_steps, -1, -1): # multigrid k = 2**k print("grid step:", k) n = cup.roll(vectors[..., 0], k, axis=1) n -= cup.roll(vectors[..., 0], -k, axis=1) n += cup.roll(vectors[..., 1], k, axis=0) n -= cup.roll(vectors[..., 1], -k, axis=0) n *= 0.125 for ic in range(iterations): if ic % 100 == 0: print(ic) # roll k, axis=0 t[:-k, :] = u[k:, :] t[-k:, :] = u[:k, :] # roll -k, axis=0 t[k:, :] += u[:-k, :] t[:k, :] += u[-k:, :] # roll k, axis=1 t[:, :-k] += u[:, k:] t[:, -k:] += u[:, :k] # roll -k, axis=1 t[:, k:] += u[:, :-k] t[:, :k] += u[:, -k:] t *= 0.25 u = t + n # zero alpha = zero height # u = u * A + cup.max(u) * (1 - A) u = -u u -= cup.min(u) u /= cup.max(u) return cup.dstack([u, u, u, image[..., 3]])
def delight_simple(image, dd, iterations=500): A = image[..., 3] u = cup.ones_like(image[..., 0]) grads = cup.zeros((image.shape[0], image.shape[1], 2), dtype=cup.float32) grads[..., 0] = (cup.roll(image[..., 0], 1, axis=0) - image[..., 0]) * dd grads[..., 1] = (image[..., 0] - cup.roll(image[..., 0], 1, axis=1)) * dd # grads[..., 0] = (image[..., 0] - 0.5) * (dd) # grads[..., 1] = (image[..., 0] - 0.5) * (dd) for k in range(5, -1, -1): # multigrid k = 2**k print("grid step:", k) n = cup.roll(grads[..., 0], k, axis=1) n -= cup.roll(grads[..., 0], -k, axis=1) n += cup.roll(grads[..., 1], k, axis=0) n -= cup.roll(grads[..., 1], -k, axis=0) n *= 0.125 * image[..., 3] for ic in range(iterations): if ic % 100 == 0: print(ic) t = cup.roll(u, -k, axis=0) t += cup.roll(u, k, axis=0) t += cup.roll(u, -k, axis=1) t += cup.roll(u, k, axis=1) t *= 0.25 # zero alpha = zero height u = t + n u = u * A + cup.max(u) * (1 - A) u = -u u -= cup.min(u) u /= cup.max(u) # u *= image[..., 3] # u -= cup.mean(u) # u /= max(abs(cup.min(u)), abs(cup.max(u))) # u *= 0.5 # u += 0.5 # u = 1.0 - u # return cup.dstack([(u - image[..., 0]) * 0.5 + 0.5, u, u, image[..., 3]]) u = (image[..., 0] - u) * 0.5 + 0.5 return cup.dstack([u, u, u, image[..., 3]])
def test_3d_cropped_camera_image(): a_black = crop(cp.asarray(camera()), ((200, 212), (100, 312))) a_black = cp.dstack([a_black, a_black, a_black]) a_white = invert(a_black) zeros = cp.zeros((100, 100, 3)) ones = cp.ones((100, 100, 3)) # TODO: determine why the following allclose checks occassionally fail assert_allclose(meijering(a_black, black_ridges=True), meijering(a_white, black_ridges=False)) assert_allclose(sato(a_black, black_ridges=True, mode='mirror'), sato(a_white, black_ridges=False, mode='mirror')) assert_allclose(frangi(a_black, black_ridges=True), zeros, atol=1e-3) assert_allclose(frangi(a_white, black_ridges=False), zeros, atol=1e-3) assert_allclose(hessian(a_black, black_ridges=True, mode='mirror'), ones, atol=1 - 1e-7) assert_allclose(hessian(a_white, black_ridges=False, mode='mirror'), ones, atol=1 - 1e-7)
<<<<<<< HEAD ======= >>>>>>> 024791b60731bd81bf57a6c52f3f58c77cab4579 # Compute inter-squares proba transition matrix self.coords_squares, self.square_ids_cells = squarify(xcoords, ycoords) self.set_attractivities(attractivities) # the first cells in parameter `cells`must be home cell, otherwise modify here self.agent_squares = self.square_ids_cells[self.home_cell_ids] cp.cuda.Stream.null.synchronize() # Re-order transitions by ids order = cp.argsort(self.transitions_ids) self.transitions_ids = self.transitions_ids[order] self.transitions = cp.dstack(self.transitions) self.transitions = self.transitions[:,:, order] cp.cuda.Stream.null.synchronize() # Compute upfront cumulated sum self.transitions = cp.cumsum(self.transitions, axis=1) # Compute probas_move for agent selection # Define variable for monitoring the propagation (r factor, contagion chain) self.n_contaminated_period = 0 # number of agent contaminated during current period self.n_diseased_period = self.get_n_diseased() self.r_factors = cp.array([]) # TODO: Contagion chains # Define arrays for agents state transitions self.infecting_agents, self.infected_agents, self.infected_periods = cp.array([]), cp.array([]), cp.array([])
def time_dstack_l(self): np.dstack(self.l)
def explicit_cross(a, b): x = a[..., 1] * b[..., 2] - a[..., 2] * b[..., 1] y = a[..., 2] * b[..., 0] - a[..., 0] * b[..., 2] z = a[..., 0] * b[..., 1] - a[..., 1] * b[..., 0] return cup.dstack([x, y, z])
def __init__(self, cell_ids, attractivities, unsafeties, xcoords, ycoords, unique_state_ids, unique_contagiousities, unique_sensitivities, unique_severities, transitions, agent_ids, home_cell_ids, p_moves, least_state_ids, current_state_ids, current_state_durations, durations, transitions_ids, dscale=1, current_period=0, verbose=0): """ A map contains a list of `cells`, `agents` and an implementation of the way agents can move from a cell to another. `possible_states` must be distinct. We let each the possibility for each agent to have its own least severe state to make the model more flexible. Default parameter set to None in order to be able to create an empty map and load it from disk `dcale` allows to weight the importance of the distance vs. attractivity for the moves to cells """ self.current_period = current_period self.verbose = verbose self.dscale = dscale self.n_infected_period = 0 # For cells self.cell_ids = cell_ids self.attractivities = attractivities self.unsafeties = unsafeties self.xcoords = xcoords self.ycoords = ycoords # For states self.unique_state_ids = unique_state_ids self.unique_contagiousities = unique_contagiousities self.unique_sensitivities = unique_sensitivities self.unique_severities = unique_severities self.transitions = transitions # For agents self.agent_ids = agent_ids self.home_cell_ids = home_cell_ids self.p_moves = p_moves self.least_state_ids = least_state_ids self.current_state_ids = current_state_ids self.current_state_durations = current_state_durations # how long the agents are already in their current state self.durations = cp.squeeze(durations) # 2d, one row for each agent self.transitions_ids = transitions_ids # for cells: cell_ids, attractivities, unsafeties, xcoords, ycoords # for states: unique_contagiousities, unique_sensitivities, unique_severities, transitions # for agents: home_cell_ids, p_moves, least_state_ids, current_state_ids, current_state_durations, durations (3d) # Compute inter-squares proba transition matrix self.coords_squares, self.square_ids_cells = squarify(xcoords, ycoords) self.set_attractivities(attractivities) # the first cells in parameter `cells`must be home cell, otherwise modify here self.agent_squares = self.square_ids_cells[self.home_cell_ids] cp.cuda.Stream.null.synchronize() # Re-order transitions by ids order = cp.argsort(self.transitions_ids) self.transitions_ids = self.transitions_ids[order] self.transitions = cp.dstack(self.transitions) self.transitions = self.transitions[:, :, order] cp.cuda.Stream.null.synchronize() # Compute upfront cumulated sum self.transitions = cp.cumsum(self.transitions, axis=1) # Compute probas_move for agent selection # Define variable for monitoring the propagation (r factor, contagion chain) self.n_contaminated_period = 0 # number of agent contaminated during current period self.n_diseased_period = self.get_n_diseased() self.r_factors = cp.array([]) # TODO: Contagion chains # Define arrays for agents state transitions self.infecting_agents, self.infected_agents, self.infected_periods = cp.array( []), cp.array([]), cp.array([])
def test_feret_diameter_max_3d(): img = cp.zeros((20, 20), dtype=cp.uint8) img[2:-2, 2:-2] = 1 img_3d = cp.dstack((img, ) * 3) feret_diameter_max = regionprops(img_3d)[0].feret_diameter_max assert cp.abs(feret_diameter_max - 16 * math.sqrt(2)) < 1