def test_translate_array(): r"""Test _translate_array with random array.""" array_translated = np.array([[2, 4, 6, 10], [1, 3, 7, 0], [3, 6, 9, 4]]) # Find the means over each dimension column_means_translated = np.zeros(4) for i in range(4): column_means_translated[i] = np.mean(array_translated[:, i]) # Confirm that these means are not all zero assert (abs(column_means_translated) > 1.e-8).all() # Compute the origin-centred array origin_centred_array, _ = _translate_array(array_translated) # Confirm that the column means of the origin-centred array are all zero column_means_centred = np.ones(4) for i in range(4): column_means_centred[i] = np.mean(origin_centred_array[:, i]) assert (abs(column_means_centred) < 1.e-10).all() # translating a centered array does not do anything centred_sphere = 25.25 * np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [-1, 0, 0], [0, -1, 0], [0, 0, -1]]) predicted, _ = _translate_array(centred_sphere) expected = centred_sphere assert (abs(predicted - expected) < 1.e-8).all() # centering a translated unit sphere dose not do anything shift = np.array([[1, 4, 5], [1, 4, 5], [1, 4, 5], [1, 4, 5], [1, 4, 5], [1, 4, 5]]) translated_sphere = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [-1, 0, 0], [0, -1, 0], [0, 0, -1]]) + shift predicted, _ = _translate_array(translated_sphere) expected = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [-1, 0, 0], [0, -1, 0], [0, 0, -1]]) assert (abs(predicted - expected) < 1.e-8).all() # If an arbitrary array is centroid translated, the analysis applied to the original array # and the translated array should give identical results # Define an arbitrary array array_a = np.array([[1, 5, 7], [8, 4, 6]]) # Define an arbitrary translation translate = np.array([[5, 8, 9], [5, 8, 9]]) # Define the translated original array array_translated = array_a + translate # Begin translation analysis centroid_a_to_b, _ = _translate_array(array_a, array_translated) assert (abs(centroid_a_to_b - array_translated) < 1.e-10).all()
def test_scale_array(): r"""Test _scale_array with random array.""" # Rescale arbitrary array array_a = np.array([[6, 2, 1], [5, 2, 9], [8, 6, 4]]) # Confirm Frobenius normaliation has transformed the array to lie on the unit sphere in # the R^(mxn) vector space. We must centre the array about the origin before proceeding array_a, _ = _translate_array(array_a) # Confirm proper centering column_means_centred = np.zeros(3) for i in range(3): column_means_centred[i] = np.mean(array_a[:, i]) assert (abs(column_means_centred) < 1.e-10).all() # Proceed with Frobenius normalization scaled_array, _ = _scale_array(array_a) # Confirm array has unit norm assert abs(np.sqrt((scaled_array**2.).sum()) - 1.) < 1.e-10 # This test verifies that when _scale_array is applied to two scaled unit spheres, # the Frobenius norm of each new sphere is unity. # Rescale spheres to unitary scale # Define arbitrarily scaled unit spheres unit_sphere = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [-1, 0, 0], [0, -1, 0], [0, 0, -1]]) sphere_1 = 230.15 * unit_sphere sphere_2 = .06 * unit_sphere # Proceed with scaling procedure scaled1, _ = _scale_array(sphere_1) scaled2, _ = _scale_array(sphere_2) # Confirm each scaled array has unit Frobenius norm assert abs(np.sqrt((scaled1**2.).sum()) - 1.) < 1.e-10 assert abs(np.sqrt((scaled2**2.).sum()) - 1.) < 1.e-10 # If an arbitrary array is scaled, the scaling analysis should be able to recreate the scaled # array from the original # applied to the original array and the scaled array should give identical results. # Define an arbitrary array array_a = np.array([[1, 5, 7], [8, 4, 6]]) # Define an arbitrary scaling factor scale = 6.3 # Define the scaled original array array_scaled = scale * array_a # Begin scaling analysis scaled_a, _ = _scale_array(array_a, array_scaled) assert (abs(scaled_a - array_scaled) < 1.e-10).all() # Define an arbitrary array array_a = np.array([[6., 12., 16., 7.], [4., 16., 17., 33.], [5., 17., 12., 16.]]) # Define the scaled original array array_scale = 123.45 * array_a # Verify the validity of the translate_scale analysis # Proceed with analysis, matching array_trans_scale to array predicted, _ = _scale_array(array_scale, array_a) # array_trans_scale should be identical to array after the above analysis expected = array_a assert (abs(predicted - expected) < 1.e-10).all()
def test_translate_weight(): r"""Test _translate_array with weighted array.""" rng = np.random.RandomState(789) arr = rng.randint(0, 10, (4, 3)) weight = np.arange(1, 5) # center the data points to mass of center, the way in the notes arr_weighted = np.dot(np.diag(weight), arr) col_sum = np.dot(np.ones((arr_weighted.shape[0], arr_weighted.shape[0])), arr_weighted) # center the data points to mass of center arr_centered = arr - col_sum / weight.sum() array_a_centered, _ = _translate_array(array_a=arr, array_b=None, weight=weight) assert_almost_equal(arr_centered, array_a_centered)