def test_list(self): """ Accept Python list as input. """ a = [(0, 0), (0, 0)] np.testing.assert_almost_equal([0, 0], geometric_median(a, method='minimize')) np.testing.assert_almost_equal([0, 0], geometric_median(a, method='weiszfeld'))
def test_square(self): """ Simple square, median point obviously in the middle. """ a = np.array([(0, 0), (1, 0), (1, 1), (0, 1)]) np.testing.assert_almost_equal([0.5, 0.5], geometric_median(a, method='minimize')) np.testing.assert_almost_equal([0.5, 0.5], geometric_median(a, method='weiszfeld'))
def test_1d(self): """ Geometric median should be median in 1D. """ a = np.array([[8, 6, 7, 5, 3, 0, 9]]).T np.testing.assert_almost_equal(np.median(a), geometric_median(a, method='minimize')) np.testing.assert_almost_equal(np.median(a), geometric_median(a, method='weiszfeld'))
def test_require2d(self): """ Accepting 1D input has too much error potential. 1 row of N dims or N rows of 1? """ a = [1, 2] with self.assertRaises(ValueError): np.testing.assert_almost_equal( a, geometric_median(a, method='minimize')) with self.assertRaises(ValueError): np.testing.assert_almost_equal( a, geometric_median(a, method='weiszfeld'))
def test_collinear(self): """ Similar to the 1D case. """ a = np.array([8, 6, 7, 5, 3, 0, 9]) median = np.median(a) expected = [median, median] a = np.array(zip(a, a)) np.testing.assert_almost_equal(expected, geometric_median(a, method='minimize')) np.testing.assert_almost_equal(expected, geometric_median(a, method='weiszfeld'))
def main(): # create a bunch of point datasets for testing n_tests = 10 n_points = 20 n_dims = 10 np.random.seed(0) test_data = np.random.uniform(size=(n_tests, n_points, n_dims)) methods = [ 'minimize', 'weiszfeld', 'auto' ] test_results = [] for points in test_data: results = [] for method in methods: start_t = time() m = geometric_median(points, method=method) end_t = time() t = end_t - start_t d = total_distance([m], points) results.append((t, d)) test_results.append(results) test_results = np.array(test_results) width = 0.90 / test_results.shape[1] bar_positions = np.arange(test_results.shape[0]) colors = ['b', 'r', 'g'] # Display computation time for method_i, method in enumerate(methods): method_times = test_results[:,method_i,0] plt.bar( bar_positions + (width * method_i), method_times, width, color=colors[method_i], label=method ) plt.xticks(bar_positions) plt.ylabel("Computation time") plt.xlabel("Trials") plt.legend() plt.show() # Display result quality (closeness) '''
def _test_4coplanar(self): """ From Wikipedia: If 1 of the 4 points is inside the triangle formed by the other 3 points, then the geometric median is that point. This is failing. """ a = [(0, 0), (1, 0), (1, 1)] # TODO: assert that b is inside a b = [0.90, 0.90] a = np.r_[a, [b]] np.testing.assert_almost_equal(b, geometric_median(a, method='minimize')) np.testing.assert_almost_equal(b, geometric_median(a, method='weiszfeld'))
def main(): # create a bunch of point datasets for testing n_tests = 10 n_points = 20 n_dims = 10 np.random.seed(0) test_data = np.random.uniform(size=(n_tests, n_points, n_dims)) methods = ['minimize', 'weiszfeld', 'auto'] test_results = [] for points in test_data: results = [] for method in methods: start_t = time() m = geometric_median(points, method=method) end_t = time() t = end_t - start_t d = total_distance([m], points) results.append((t, d)) test_results.append(results) test_results = np.array(test_results) width = 0.90 / test_results.shape[1] bar_positions = np.arange(test_results.shape[0]) colors = ['b', 'r', 'g'] # Display computation time for method_i, method in enumerate(methods): method_times = test_results[:, method_i, 0] plt.bar(bar_positions + (width * method_i), method_times, width, color=colors[method_i], label=method) plt.xticks(bar_positions) plt.ylabel("Computation time") plt.xlabel("Trials") plt.legend() plt.show() # Display result quality (closeness) '''
def test_1point(self): a = [(5, 5)] np.testing.assert_almost_equal(a[0], geometric_median(a, method='minimize')) np.testing.assert_almost_equal(a[0], geometric_median(a, method='weiszfeld'))