def test_check_array_min_samples_and_features_messages(): # empty list is considered 2D by default: msg = "0 feature(s) (shape=(1, 0)) while a minimum of 1 is required." assert_raise_message(ValueError, msg, check_array, [[]]) # If considered a 1D collection when ensure_2d=False, then the minimum # number of samples will break: msg = "0 sample(s) (shape=(0,)) while a minimum of 1 is required." assert_raise_message(ValueError, msg, check_array, [], ensure_2d=False) # Invalid edge case when checking the default minimum sample of a scalar msg = "Singleton array array(42) cannot be considered a valid collection." assert_raise_message(TypeError, msg, check_array, 42, ensure_2d=False) # But this works if the input data is forced to look like a 2 array with # one sample and one feature: X_checked = assert_warns(DeprecationWarning, check_array, [42], ensure_2d=True) assert_array_equal(np.array([[42]]), X_checked)
def test_get_affinity_matrix(almost_equal_decimals=5): """ test different ways to call get_affinity_matrix """ # 1. (No existing affinity, no passed radius, no self.radius, input_type = data) X = np.random.uniform(size = (10,10)) dist_mat = distance_matrix(X, radius = 1/X.shape[1]) A2 = affinity_matrix(dist_mat, neighbors_radius = 1/X.shape[1]) Geometry = geom.Geometry(X, input_type = 'data') ## Return default Affinity A = Geometry.get_affinity_matrix() assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals) # 2. (No existing affinity, no passed radius, no self.radius, input_type = distance) X = np.random.uniform(size = (10,10)) dist_mat = distance_matrix(X, radius = 1/X.shape[1]) A2 = affinity_matrix(dist_mat, neighbors_radius = 1/X.shape[1]) Geometry = geom.Geometry(dist_mat, input_type = 'distance') ## Return default A = Geometry.get_affinity_matrix() assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals) # 3. (No existing affinity, no passed radius, existing self.radius, input_type = data) X = np.random.uniform(size = (10,10)) radius = 1 dist_mat = distance_matrix(X, radius = radius) A2 = affinity_matrix(dist_mat, neighbors_radius = radius) Geometry = geom.Geometry(X, input_type = 'data', neighborhood_radius = radius, affinity_radius = radius) ## Return with existing radius A = Geometry.get_affinity_matrix() assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals) # 4. (No existing affinity, no passed radius, existing self.radius, input_type = distance) X = np.random.uniform(size = (10,10)) radius = 1 dist_mat = distance_matrix(X, radius = radius) A2 = affinity_matrix(dist_mat, neighbors_radius = radius) Geometry = geom.Geometry(dist_mat, input_type = 'distance', neighborhood_radius = radius, affinity_radius = radius) ## Return with existing radius A = Geometry.get_affinity_matrix() assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals) # 5. (No existing affinity, passed radius, no existing self.radius, input_type = data) X = np.random.uniform(size = (10,10)) radius = 1 dist_mat = distance_matrix(X, radius = radius) A2 = affinity_matrix(dist_mat, neighbors_radius = 2) Geometry = geom.Geometry(X, input_type = 'data', neighborhood_radius = radius) ## Return passed radius affinity, set radius to passed A = Geometry.get_affinity_matrix(affinity_radius=2) assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals) # 6. (No existing affinity, passed radius, no existing self.radius, input_type = distance) X = np.random.uniform(size = (10,10)) radius = 1 dist_mat = distance_matrix(X, radius = radius) A2 = affinity_matrix(dist_mat, neighbors_radius = 2) Geometry = geom.Geometry(dist_mat, input_type = 'distance', neighborhood_radius = radius) ## Return passed radius affinity, set radius to passed A = Geometry.get_affinity_matrix(affinity_radius=2) assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals) # 7. (No existing affinity, passed radius equal to existing self.radius, input_type = data) X = np.random.uniform(size = (10,10)) radius = 1 dist_mat = distance_matrix(X, radius = radius) A2 = affinity_matrix(dist_mat, neighbors_radius = radius) Geometry = geom.Geometry(X, input_type = 'data', neighborhood_radius = radius, affinity_radius = radius) ## Return self.radius affinity A = Geometry.get_affinity_matrix(affinity_radius=radius) assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals) # 8. (No existing affinity, passed radius equal to existing self.radius, input_type = distance) X = np.random.uniform(size = (10,10)) radius = 1 dist_mat = distance_matrix(X, radius = radius) A2 = affinity_matrix(dist_mat, neighbors_radius = radius) Geometry = geom.Geometry(dist_mat, input_type = 'distance', neighborhood_radius = radius, affinity_radius = radius) ## Return self.radius affinity A = Geometry.get_affinity_matrix(affinity_radius=radius) assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals) # 9. (No existing affinity, passed radius not equal to existing self.radius, input_type = data) X = np.random.uniform(size = (10,10)) radius = 1 radius2 = 2 dist_mat = distance_matrix(X, radius = radius) A2 = affinity_matrix(dist_mat, neighbors_radius = radius2) Geometry = geom.Geometry(X, input_type = 'data', neighborhood_radius = radius, affinity_radius = radius) ## Return passed radius affinity, set radius to passed A = Geometry.get_affinity_matrix(affinity_radius=radius2) assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals) assert(Geometry.affinity_radius == radius2) # 10. (No existing affinity, passed radius not equal to existing self.radius, input_type = distance) X = np.random.uniform(size = (10,10)) radius = 1 radius2 = 2 dist_mat = distance_matrix(X, radius = radius) A2 = affinity_matrix(dist_mat, neighbors_radius = radius2) Geometry = geom.Geometry(dist_mat, input_type = 'distance', neighborhood_radius = radius, affinity_radius = radius) ## Return passed radius affinity, set radius to passed A = Geometry.get_affinity_matrix(affinity_radius=radius2) assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals) assert(Geometry.affinity_radius == radius2) # 11. (Existing affinity, no passed radius, no self.radius, input_type = data) X = np.random.uniform(size = (10,10)) radius = 1 dist_mat = distance_matrix(X, radius = radius) A2 = affinity_matrix(dist_mat, neighbors_radius = radius) Geometry = geom.Geometry(X, input_type = 'data') Geometry.assign_affinity_matrix(A2) ## Return existing affinity A = Geometry.get_affinity_matrix() assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals) # 12. (Existing affinity, no passed radius, no self.radius, input_type = distance) X = np.random.uniform(size = (10,10)) radius = 1 dist_mat = distance_matrix(X, radius = radius) A2 = affinity_matrix(dist_mat, neighbors_radius = radius) Geometry = geom.Geometry(dist_mat, input_type = 'distance') Geometry.assign_affinity_matrix(A2) ## Return existing affinity A = Geometry.get_affinity_matrix() assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals) # 13. (Existing affinity, no passed radius, no self.radius, input_type = affinity) X = np.random.uniform(size = (10,10)) radius = 1 dist_mat = distance_matrix(X, radius = radius) A2 = affinity_matrix(dist_mat, neighbors_radius = radius) Geometry = geom.Geometry(A2, input_type = 'affinity') ### Return existing affinity A = Geometry.get_affinity_matrix() assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals) # 14. (Existing affinity, no passed radius, existing self.radius, input_type = data) X = np.random.uniform(size = (10,10)) radius = 1 dist_mat = distance_matrix(X, radius = radius) A2 = affinity_matrix(dist_mat, neighbors_radius = radius) Geometry = geom.Geometry(X, input_type = 'data', neighborhood_radius = radius, affinity_radius = radius) Geometry.assign_affinity_matrix(A2) ### Return existing affinity A = Geometry.get_affinity_matrix() assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals) # 15. (Existing affinity, no passed radius, existing self.radius, input_type = distance) X = np.random.uniform(size = (10,10)) radius = 1 dist_mat = distance_matrix(X, radius = radius) A2 = affinity_matrix(dist_mat, neighbors_radius = radius) Geometry = geom.Geometry(dist_mat, input_type = 'distance', neighborhood_radius = radius, affinity_radius = radius) Geometry.assign_affinity_matrix(A2) ### Return existing affinity A = Geometry.get_affinity_matrix() assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals) # 16. (Existing affinity, no passed radius, existing self.radius, input_type = affinity) X = np.random.uniform(size = (10,10)) radius = 1 dist_mat = distance_matrix(X, radius = radius) A2 = affinity_matrix(dist_mat, neighbors_radius = radius) Geometry = geom.Geometry(A2, input_type = 'affinity', neighborhood_radius = radius, affinity_radius = radius) ## Return existing affinity A = Geometry.get_affinity_matrix() assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals) # 17. (Existing affinity, passed radius, no self.radius, input_type = data) X = np.random.uniform(size = (10,10)) radius = 1 dist_mat = distance_matrix(X, radius = radius) A2 = affinity_matrix(dist_mat, neighbors_radius = 2) Geometry = geom.Geometry(X, input_type = 'data', neighborhood_radius = radius) Geometry.assign_affinity_matrix(A2) ## Compute with passed radius, set self.radius to passed radius A2 = affinity_matrix(dist_mat, neighbors_radius = radius) A = Geometry.get_affinity_matrix(affinity_radius=radius) assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals) # 18. (Existing affinity, passed radius, no self.radius, input_type = distance) X = np.random.uniform(size = (10,10)) radius = 1 dist_mat = distance_matrix(X, radius = radius) A2 = affinity_matrix(dist_mat, neighbors_radius = 2) Geometry = geom.Geometry(dist_mat, input_type = 'distance', neighborhood_radius = radius) Geometry.assign_affinity_matrix(A2) ## Compute with passed radius, set self.radius to passed radius A2 = affinity_matrix(dist_mat, neighbors_radius = radius) A = Geometry.get_affinity_matrix(affinity_radius=radius) assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals) # 19. (Existing affinity, passed radius, no self.radius, input_type = affinity) X = np.random.uniform(size = (10,10)) radius = 1 dist_mat = distance_matrix(X, radius = radius) A2 = affinity_matrix(dist_mat, neighbors_radius = 2) Geometry = geom.Geometry(A2, input_type = 'affinity', neighborhood_radius = radius) ## Raise error, unknown existing radius, passed radius but input type affinity msg = ("Input_method was passed as affinity." "Requested radius was not equal to self.affinity_radius." "Affinity Matrix cannot be recalculated.") assert_raise_message(ValueError, msg, Geometry.get_affinity_matrix, affinity_radius=radius) # 20. (Existing affinity, passed radius equal to self.radius, input_type = data) X = np.random.uniform(size = (10,10)) radius = 1 dist_mat = distance_matrix(X, radius = radius) A2 = affinity_matrix(dist_mat, neighbors_radius = radius) Geometry = geom.Geometry(X, input_type = 'data', neighborhood_radius = radius, affinity_radius = radius) Geometry.assign_affinity_matrix(A2) ## Return existing affinity A = Geometry.get_affinity_matrix(affinity_radius=radius) assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals) # 21. (Existing affinity, passed radius equal to self.radius, input_type = distance) X = np.random.uniform(size = (10,10)) radius = 1 dist_mat = distance_matrix(X, radius = radius) A2 = affinity_matrix(dist_mat, neighbors_radius = radius) Geometry = geom.Geometry(dist_mat, input_type = 'distance', neighborhood_radius = radius, affinity_radius = radius) Geometry.assign_affinity_matrix(A2) ## Return existing affinity A = Geometry.get_affinity_matrix(affinity_radius=radius) assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals) # 22. (Existing affinity, passed radius equal to self.radius, input_type = affinity) X = np.random.uniform(size = (10,10)) radius = 1 dist_mat = distance_matrix(X, radius = radius) A2 = affinity_matrix(dist_mat, neighbors_radius = radius) Geometry = geom.Geometry(A2, input_type = 'affinity', neighborhood_radius = radius, affinity_radius = radius) ## Return existing affinity A = Geometry.get_affinity_matrix(affinity_radius=radius) assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals) # 23. (Existing affinity, passed radius not equal to self.radius, input_type = data) X = np.random.uniform(size = (10,10)) radius = 1 dist_mat = distance_matrix(X, radius = radius) A2 = affinity_matrix(dist_mat, neighbors_radius = radius) Geometry = geom.Geometry(X, input_type = 'data', neighborhood_radius = radius, affinity_radius = radius) Geometry.assign_affinity_matrix(A2) ## Re-calculate with passed radius, set self.radius to passed radius A2 = affinity_matrix(dist_mat, neighbors_radius = 2) A = Geometry.get_affinity_matrix(affinity_radius=2) assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals) assert(Geometry.affinity_radius == 2) # 24. (Existing affinity, passed radius not equal to self.radius, input_type = distance) X = np.random.uniform(size = (10,10)) radius = 1 dist_mat = distance_matrix(X, radius = radius) A2 = affinity_matrix(dist_mat, neighbors_radius = radius) Geometry = geom.Geometry(dist_mat, input_type = 'distance', neighborhood_radius = radius, affinity_radius = radius) Geometry.assign_affinity_matrix(A2) ## Re-calculate with passed radius, set self.radius to passed radius A2 = affinity_matrix(dist_mat, neighbors_radius=2) A = Geometry.get_affinity_matrix(affinity_radius=2) assert_array_almost_equal(A.todense(), A2.todense(), almost_equal_decimals) assert(Geometry.affinity_radius == 2) # 25. (Existing affinity, passed radius not equal to self.radius, input_type = affinity) X = np.random.uniform(size = (10,10)) radius = 1 dist_mat = distance_matrix(X, radius = radius) A2 = affinity_matrix(dist_mat, neighbors_radius = 2) Geometry = geom.Geometry(A2, input_type = 'affinity', neighborhood_radius = radius, affinity_radius = 2) ## Raise error, passed affinity re-calculateion not supported. msg = ("Input_method was passed as affinity." "Requested radius was not equal to self.affinity_radius." "Affinity Matrix cannot be recalculated.") assert_raise_message(ValueError, msg, Geometry.get_affinity_matrix, affinity_radius=radius)
def test_get_distance_matrix(almost_equal_decimals = 5): """ test different ways to call get_distance_matrix """ # 1. (Input type = Affinity) X = np.random.uniform(size = (10,10)) Geometry = geom.Geometry(X, input_type = 'affinity') ## Raise error Affinity matrix passed msg = ( "input_method was passed as affinity. " "Distance matrix cannot be computed.") assert_raise_message(ValueError, msg, Geometry.get_distance_matrix) # 2. (No existing distance, no passed radius, no self.radius, input_type = Data) X = np.random.uniform(size = (10,10)) Geometry = geom.Geometry(X, input_type = 'data') ## Calculate with default radius distance_mat = Geometry.get_distance_matrix() distance_mat2 = distance_matrix(X, radius = 1/X.shape[1]) assert_array_almost_equal(distance_mat.todense(), distance_mat2.todense(), almost_equal_decimals) # 3. (No existing distance, no passed radius, existing self.radius, input_type = Data) X = np.random.uniform(size = (10,10)) Geometry = geom.Geometry(X, input_type = 'data', neighborhood_radius = 1) ## Calculate with existing radius distance_mat = Geometry.get_distance_matrix() distance_mat2 = distance_matrix(X, radius = 1) assert_array_almost_equal(distance_mat.todense(), distance_mat2.todense(), almost_equal_decimals) # 4. (No existing distance, passed radius, no existing self.radius, input_type = Data) X = np.random.uniform(size = (10,10)) Geometry = geom.Geometry(X, input_type = 'data') ## Set current radius to passed, calcualte with passed radius distance_mat = Geometry.get_distance_matrix(neighborhood_radius = 1) distance_mat2 = distance_matrix(X, radius = 1) assert_array_almost_equal(distance_mat.todense(), distance_mat2.todense(), almost_equal_decimals) assert(Geometry.neighborhood_radius == 1) # 5. (No existing distance, passed radius equal to existing self.radius, input_type = Data) X = np.random.uniform(size = (10,10)) Geometry = geom.Geometry(X, input_type = 'data', neighborhood_radius = 1) ## Calculate with passed radius distance_mat = Geometry.get_distance_matrix(neighborhood_radius = 1) distance_mat2 = distance_matrix(X, radius = 1) assert_array_almost_equal(distance_mat.todense(), distance_mat2.todense(), almost_equal_decimals) # 6. (No existing distance, passed radius not equal to existing self.radius, input_type = Data) X = np.random.uniform(size = (10,10)) Geometry = geom.Geometry(X, input_type = 'data', neighborhood_radius = 1) ## Calculate with passed radius, set new self.radius distance_mat = Geometry.get_distance_matrix(neighborhood_radius = 2) distance_mat2 = distance_matrix(X, radius = 2) assert_array_almost_equal(distance_mat.todense(), distance_mat2.todense(), almost_equal_decimals) assert(Geometry.neighborhood_radius == 2) # 7. (Existing distance, no passed radius, no self.radius, input_type = data) X = np.random.uniform(size = (10,10)) Geometry = geom.Geometry(X, input_type = 'data') distance_mat2 = distance_matrix(X, radius = 1) Geometry.assign_distance_matrix(distance_mat2) ## Return existing distance matrix distance_mat = Geometry.get_distance_matrix() assert_array_almost_equal(distance_mat.todense(), distance_mat2.todense(), almost_equal_decimals) # 8. (Existing distance, no passed radius, no self.radius, input_type = distance) X = np.random.uniform(size = (10,10)) distance_mat2 = distance_matrix(X, radius = 1) Geometry = geom.Geometry(distance_mat2, input_type = 'distance') ## Return existing distance matrix distance_mat = Geometry.get_distance_matrix() assert_array_almost_equal(distance_mat.todense(), distance_mat2.todense(), almost_equal_decimals) # 9. (Existing distance, no passed radius, existing self.radius, input_type = data) X = np.random.uniform(size = (10,10)) Geometry = geom.Geometry(X, input_type = 'data', neighborhood_radius = 1) distance_mat2 = distance_matrix(X, radius = 1) Geometry.assign_distance_matrix(distance_mat2) ## Return existing distance Matrix distance_mat = Geometry.get_distance_matrix() assert_array_almost_equal(distance_mat.todense(), distance_mat2.todense(), almost_equal_decimals) # 10. (Existing distance, no passed radius, existing self.radius, input_type = distance) X = np.random.uniform(size = (10,10)) distance_mat2 = distance_matrix(X, radius = 1) Geometry = geom.Geometry(distance_mat2, input_type = 'distance', neighborhood_radius = 1) ## Return existing distance matrix distance_mat = Geometry.get_distance_matrix() assert_array_almost_equal(distance_mat.todense(), distance_mat2.todense(), almost_equal_decimals) # 11. (Existing distance, passed radius, no existing self.radius, input_type = data) X = np.random.uniform(size = (10,10)) distance_mat2 = distance_matrix(X, radius = 1) Geometry = geom.Geometry(X, input_type = 'data') Geometry.assign_distance_matrix(distance_mat2) ## Re-calculate with passed radius, set self.radius to passed radius distance_mat = Geometry.get_distance_matrix(neighborhood_radius=3) distance_mat2 = distance_matrix(X, radius = 3) assert_array_almost_equal(distance_mat.todense(), distance_mat2.todense(), almost_equal_decimals) assert(Geometry.neighborhood_radius == 3) # 12. (Existing distance, passed radius, no existing self.radius, input_type = distance) X = np.random.uniform(size = (10,10)) distance_mat2 = distance_matrix(X, radius = 1) Geometry = geom.Geometry(distance_mat2, input_type = 'distance') ## Raise error, can't re-calculate msg = ("input_method was passed as distance." "requested radius not equal to self.neighborhood_radius." "distance matrix cannot be re-calculated.") assert_raise_message(ValueError, msg, Geometry.get_distance_matrix, neighborhood_radius=3) # 13. (Existing distance, passed radius equal to existing self.radius, input_type = data) X = np.random.uniform(size = (10,10)) distance_mat2 = distance_matrix(X, radius = 1) Geometry = geom.Geometry(X, input_type = 'data', neighborhood_radius=1) Geometry.assign_distance_matrix(distance_mat2) ## Return existing distance matrix distance_mat = Geometry.get_distance_matrix(neighborhood_radius=1) assert_array_almost_equal(distance_mat.todense(), distance_mat2.todense(), almost_equal_decimals) # 14. (Existing distance, passed radius equal to existing self.radius, input_type = distance) X = np.random.uniform(size = (10,10)) distance_mat2 = distance_matrix(X, radius = 1) Geometry = geom.Geometry(distance_mat2, input_type = 'distance', neighborhood_radius = 1) ## Return existing distance matrix distance_mat = Geometry.get_distance_matrix(neighborhood_radius=1) assert_array_almost_equal(distance_mat.todense(), distance_mat2.todense(), almost_equal_decimals) # 15. (Existing distance, passed radius not equal to existing self.radius, input_type = data) X = np.random.uniform(size = (10,10)) distance_mat2 = distance_matrix(X, radius = 1) Geometry = geom.Geometry(X, input_type = 'data', neighborhood_radius=1) Geometry.assign_distance_matrix(distance_mat2) ## Recompute with passed radius, set self.radius to passed radius distance_mat2 = distance_matrix(X, radius = 3) distance_mat = Geometry.get_distance_matrix(neighborhood_radius=3) assert_array_almost_equal(distance_mat.todense(), distance_mat2.todense(), almost_equal_decimals) assert(Geometry.neighborhood_radius == 3) # 16. (Existing distance, passed radius not equal to existing self.radius, input_type = distance) X = np.random.uniform(size = (10,10)) distance_mat2 = distance_matrix(X, radius = 1) Geometry = geom.Geometry(distance_mat2, input_type = 'distance', neighborhood_radius = 1) ## Raise error, can't recalculate msg = ("input_method was passed as distance." "requested radius not equal to self.neighborhood_radius." "distance matrix cannot be re-calculated.") assert_raise_message(ValueError, msg, Geometry.get_distance_matrix, neighborhood_radius=3)