def test_1d_coordinates(self): img_size = (21, 1, 1, 1, 1) win_size = (15, 1, 1) sampling_map = np.zeros(img_size) coords = balanced_spatial_coordinates(10, img_size, win_size, sampling_map) self.assertAllEqual(coords.shape, (10, N_SPATIAL)) self.assertCoordinatesAreValid(coords, sampling_map) sampling_map[9, 0, 0, 0, 0] = 1.0 coords = balanced_spatial_coordinates(500, img_size, win_size, sampling_map) # better test? self.assertTrue(np.sum(np.all(coords == [9, 0, 0], axis=1)) >= 200)
def test_classes_balances(self): # Set the random state to prevent false positive np.random.seed(0) # Setting these too high inflats the run time number_of_repetitions = 1000 samples_per_repetition = 10 num_classes = 3 # Create a map with almost all background, one pixel of each # other label img_size = (50, 25, 10, 1, 1) win_size = (8, 7, 2, 1, 1) sampling_map = np.zeros(img_size) sampling_map[6, 5, 2:, 0, 0] = 1 sampling_map[11, 13:, 3, 0, 0] = 2 # Accumulate the number of times each class is sampled accum = np.zeros((num_classes)) for _ in range(number_of_repetitions): coords = balanced_spatial_coordinates(samples_per_repetition, img_size, win_size, sampling_map) # Be sure to sample the correct number self.assertAllEqual(coords.shape, (samples_per_repetition, N_SPATIAL)) # Convert to np.ndarry indexable for coord in coords.astype(int): x, y, z = coord label = int(sampling_map[x, y, z]) accum[label] = accum[label] + 1 # Each class should be within 2 decimal places of 1.0/num_classes accum = np.divide(accum, accum.sum()) self.assertAllClose(accum, np.ones((num_classes)) * 1.0 / num_classes, rtol=1e-2, atol=1e-2)