def test_segmented_rings(): center = (75, 75) img_dim = (150, 140) first_q = 5 delta_q = 5 num_rings = 4 # number of Q rings slicing = 4 edges = roi.ring_edges(first_q, width=delta_q, spacing=4, num_rings=num_rings) print("edges", edges) label_array = roi.segmented_rings(edges, slicing, center, img_dim, offset_angle=0) print("label_array for segmented_rings", label_array) # Did we draw the right number of ROIs? label_list = np.unique(label_array.ravel()) actual_num_labels = len(label_list) - 1 num_labels = num_rings * slicing assert_equal(actual_num_labels, num_labels) # Did we draw the right ROIs? (1-16 with some zeros around too) assert_array_equal(label_list, np.arange(num_labels + 1)) # A brittle test to make sure the exactly number of pixels per label # is never accidentally changed: # number of pixels per ROI num_pixels = np.bincount(label_array.ravel()) expected_num_pixels = [18372, 59, 59, 59, 59, 129, 129, 129, 129, 200, 200, 200, 200, 269, 269, 269, 269] assert_array_equal(num_pixels, expected_num_pixels)
def test_segmented_rings(): center = (75, 75) img_dim = (150, 140) first_q = 5 delta_q = 5 num_rings = 4 # number of Q rings slicing = 4 edges = roi.ring_edges(first_q, width=delta_q, spacing=4, num_rings=num_rings) print("edges", edges) label_array = roi.segmented_rings(edges, slicing, center, img_dim, offset_angle=0) print("label_array for segmented_rings", label_array) # Did we draw the right number of ROIs? label_list = np.unique(label_array.ravel()) actual_num_labels = len(label_list) - 1 num_labels = num_rings * slicing assert_equal(actual_num_labels, num_labels) # Did we draw the right ROIs? (1-16 with some zeros around too) assert_array_equal(label_list, np.arange(num_labels + 1)) # A brittle test to make sure the exactly number of pixels per label # is never accidentally changed: # number of pixels per ROI num_pixels = np.bincount(label_array.ravel()) expected_num_pixels = [18372, 59, 59, 59, 59, 129, 129, 129, 129, 200, 200, 200, 200, 269, 269, 269, 269] assert_array_equal(num_pixels, expected_num_pixels)
def testCrossCorrelator2d(): ''' Test the 2D case of the cross correlator. With non-binary labels. ''' np.random.seed(123) # test 2D data Npoints2 = 10 x2 = np.linspace(-10, 10, Npoints2) X, Y = np.meshgrid(x2, x2) Z = np.random.random((Npoints2, Npoints2)) np.random.seed(123) sigma = .2 # purposely have sparsely filled values (with lots of zeros) # place peaks in random positions peak_positions = (np.random.random((2, 10)) - .5) * 20 for peak_position in peak_positions: Z += np.exp(-((X - peak_position[0])**2 + (Y - peak_position[1])**2) / 2. / sigma**2) mask_2D = np.ones_like(Z) mask_2D[1:2, 1:2] = 0 mask_2D[7:9, 4:6] = 0 mask_2D[1:2, 9:] = 0 # Compute with segmented rings edges = ring_edges(1, 3, num_rings=2) segments = 5 x0, y0 = np.array(mask_2D.shape) // 2 maskids = segmented_rings(edges, segments, (y0, x0), mask_2D.shape) cc2D_ids = CrossCorrelator(mask_2D.shape, mask=maskids) cc2D_ids_symavg = CrossCorrelator(mask_2D.shape, mask=maskids, normalization='symavg') # 10 ids assert_equal(cc2D_ids.nids, 10) ycorr_ids_2D = cc2D_ids(Z) ycorr_ids_2D_symavg = cc2D_ids_symavg(Z) index = 0 ycorr_ids_2D[index][ycorr_ids_2D[index].shape[0] // 2] assert_array_almost_equal( ycorr_ids_2D[index][ycorr_ids_2D[index].shape[0] // 2], np.array([1.22195059, 1.08685771, 1.43246508, 1.08685771, 1.22195059])) index = 1 ycorr_ids_2D[index][ycorr_ids_2D[index].shape[0] // 2] assert_array_almost_equal( ycorr_ids_2D[index][ycorr_ids_2D[index].shape[0] // 2], np.array([1.24324268, 0.80748997, 1.35790022, 0.80748997, 1.24324268])) index = 0 ycorr_ids_2D_symavg[index][ycorr_ids_2D[index].shape[0] // 2] assert_array_almost_equal( ycorr_ids_2D_symavg[index][ycorr_ids_2D[index].shape[0] // 2], np.array([0.84532695, 1.16405848, 1.43246508, 1.16405848, 0.84532695])) index = 1 ycorr_ids_2D_symavg[index][ycorr_ids_2D[index].shape[0] // 2] assert_array_almost_equal( ycorr_ids_2D_symavg[index][ycorr_ids_2D[index].shape[0] // 2], np.array([0.94823482, 0.8629459, 1.35790022, 0.8629459, 0.94823482]))
def testCrossCorrelator2d(): ''' Test the 2D case of the cross correlator. With non-binary labels. ''' np.random.seed(123) # test 2D data Npoints2 = 10 x2 = np.linspace(-10, 10, Npoints2) X, Y = np.meshgrid(x2, x2) Z = np.random.random((Npoints2, Npoints2)) np.random.seed(123) sigma = .2 # purposely have sparsely filled values (with lots of zeros) # place peaks in random positions peak_positions = (np.random.random((2, 10))-.5)*20 for peak_position in peak_positions: Z += np.exp(-((X - peak_position[0])**2 + (Y - peak_position[1])**2)/2./sigma**2) mask_2D = np.ones_like(Z) mask_2D[1:2, 1:2] = 0 mask_2D[7:9, 4:6] = 0 mask_2D[1:2, 9:] = 0 # Compute with segmented rings edges = ring_edges(1, 3, num_rings=2) segments = 5 x0, y0 = np.array(mask_2D.shape)//2 maskids = segmented_rings(edges, segments, (y0, x0), mask_2D.shape) cc2D_ids = CrossCorrelator(mask_2D.shape, mask=maskids) cc2D_ids_symavg = CrossCorrelator(mask_2D.shape, mask=maskids, normalization='symavg') # 10 ids assert_equal(cc2D_ids.nids, 10) ycorr_ids_2D = cc2D_ids(Z) ycorr_ids_2D_symavg = cc2D_ids_symavg(Z) index = 0 ycorr_ids_2D[index][ycorr_ids_2D[index].shape[0]//2] assert_array_almost_equal(ycorr_ids_2D[index] [ycorr_ids_2D[index].shape[0]//2], np.array([1.22195059, 1.08685771, 1.43246508, 1.08685771, 1.22195059 ]) ) index = 1 ycorr_ids_2D[index][ycorr_ids_2D[index].shape[0]//2] assert_array_almost_equal(ycorr_ids_2D[index] [ycorr_ids_2D[index].shape[0]//2], np.array([1.24324268, 0.80748997, 1.35790022, 0.80748997, 1.24324268 ]) ) index = 0 ycorr_ids_2D_symavg[index][ycorr_ids_2D[index].shape[0]//2] assert_array_almost_equal(ycorr_ids_2D_symavg[index] [ycorr_ids_2D[index].shape[0]//2], np.array([0.84532695, 1.16405848, 1.43246508, 1.16405848, 0.84532695]) ) index = 1 ycorr_ids_2D_symavg[index][ycorr_ids_2D[index].shape[0]//2] assert_array_almost_equal(ycorr_ids_2D_symavg[index] [ycorr_ids_2D[index].shape[0]//2], np.array([0.94823482, 0.8629459, 1.35790022, 0.8629459, 0.94823482]) )