<commit_before>import unittest
import numpy as np
from numpy.testing import assert_array_almost_equal
from scipy.sparse import csr_matrix
from sklearn.decomposition import PCA
from sklearn.manifold import locally_linear_embedding
from graphs import Graph
from graphs.construction import neighbor_graph


class TestEmbeddings(unittest.TestCase):
  def test_isomap(self):
    expected = [-np.sqrt(8), -np.sqrt(2), 0, np.sqrt(2), np.sqrt(8)]
    G = Graph.from_adj_matrix([[0, np.sqrt(2), 2.82842712, 0, 0],
                               [np.sqrt(2), 0, np.sqrt(2), 0, 0],
                               [0, np.sqrt(2), 0, np.sqrt(2), 0],
                               [0, 0, np.sqrt(2), 0, np.sqrt(2)],
                               [0, 0, 2.82842712, np.sqrt(2), 0]])
    Y = G.isomap(num_vecs=1)
    self.assertEqual(Y.shape, (5, 1))
    assert_array_almost_equal(Y[:,0], expected)

  def test_laplacian_eigenmaps(self):
    # Test a simple chain graph
    expected = np.array([0.5, 0.5, 0., -0.5, -0.5])
    W = np.zeros((5,5)) + np.diag(np.ones(4), k=1) + np.diag(np.ones(4), k=-1)
    G = Graph.from_adj_matrix(W)
    Y = G.laplacian_eigenmaps(num_vecs=1)
    self.assertEqual(Y.shape, (5, 1))
    assert_array_almost_equal(Y[:,0], expected)
    # Test num_vecs=None case
    Y = G.laplacian_eigenmaps()
    self.assertEqual(Y.shape, (5, 4))
    assert_array_almost_equal(Y[:,0], expected)
    # Test sparse case
    G = Graph.from_adj_matrix(csr_matrix(W))
    Y = G.laplacian_eigenmaps(num_vecs=1)
    self.assertEqual(Y.shape, (5, 1))
    assert_array_almost_equal(Y[:,0], expected)

  def test_locality_preserving_projections(self):
    X = np.array([[1,2],[2,1],[3,1.5],[4,0.5],[5,1]])
    G = Graph.from_adj_matrix([[0, 1, 1, 0, 0],
                               [1, 0, 1, 0, 0],
                               [1, 1, 0, 1, 1],
                               [0, 0, 1, 0, 1],
                               [0, 0, 1, 1, 0]])
    proj = G.locality_preserving_projections(X, num_vecs=1)
    assert_array_almost_equal(proj, np.array([[-0.95479113],[0.29727749]]))
    # test case with bigger d than n
    X = np.hstack((X, X))[:3]
    G = Graph.from_adj_matrix(G.matrix()[:3,:3])
    proj = G.locality_preserving_projections(X, num_vecs=1)<commit_msg>Account for eigenvector sign flipping<commit_after>import unittest
import numpy as np
from numpy.testing import assert_array_almost_equal
from scipy.sparse import csr_matrix
from sklearn.decomposition import PCA
from sklearn.manifold import locally_linear_embedding
from graphs import Graph
from graphs.construction import neighbor_graph


def assert_signless_array_almost_equal(a, b):
  if (a < 0 and b > 0) or (a > 0 and b < 0):
    assert_array_almost_equal(a, -b)
  else:
    assert_array_almost_equal(a, b)


class TestEmbeddings(unittest.TestCase):
  def test_isomap(self):
    expected = [-np.sqrt(8), -np.sqrt(2), 0, np.sqrt(2), np.sqrt(8)]
    G = Graph.from_adj_matrix([[0, np.sqrt(2), 2.82842712, 0, 0],
                               [np.sqrt(2), 0, np.sqrt(2), 0, 0],
                               [0, np.sqrt(2), 0, np.sqrt(2), 0],
                               [0, 0, np.sqrt(2), 0, np.sqrt(2)],
                               [0, 0, 2.82842712, np.sqrt(2), 0]])
    Y = G.isomap(num_vecs=1)
    self.assertEqual(Y.shape, (5, 1))
    assert_array_almost_equal(Y[:,0], expected)

  def test_laplacian_eigenmaps(self):
    # Test a simple chain graph
    expected = np.array([0.5, 0.5, 0., -0.5, -0.5])
    W = np.zeros((5,5)) + np.diag(np.ones(4), k=1) + np.diag(np.ones(4), k=-1)
    G = Graph.from_adj_matrix(W)
    Y = G.laplacian_eigenmaps(num_vecs=1)
    self.assertEqual(Y.shape, (5, 1))
    assert_signless_array_almost_equal(Y[:,0], expected)
    # Test num_vecs=None case
    Y = G.laplacian_eigenmaps()
    self.assertEqual(Y.shape, (5, 4))
    assert_signless_array_almost_equal(Y[:,0], expected)
    # Test sparse case
    G = Graph.from_adj_matrix(csr_matrix(W))
    Y = G.laplacian_eigenmaps(num_vecs=1)
    self.assertEqual(Y.shape, (5, 1))
    assert_signless_array_almost_equal(Y[:,0], expected)

  def test_locality_preserving_projections(self):
    X = np.array([[1,2],[2,1],[3,1.5],[4,0.5],[5,1]])
    G = Graph.from_adj_matrix([[0, 1, 1, 0, 0],
                               [1, 0, 1, 0, 0],
                               [1, 1, 0, 1, 1],
                               [0, 0, 1, 0, 1],
                               [0, 0, 1, 1, 0]])
    proj = G.locality_preserving_projections(X, num_vecs=1)
    assert_array_almost_equal(proj, np.array([[-0.95479113],[0.29727749]]))
    # test case with bigger d than n
    X = np.hstack((X, X))[:3]
    G = Graph.from_adj_matrix(G.matrix()[:3,:3])
    proj = G.locality_preserving_projections(X, num_vecs=1)
{'fraction_non_alphanumeric': 0.15694122603749736, 'fraction_numerical': 0.058141984411207076, 'mean_word_length': 2.6299694189602447, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '798905', 'n_tokens_mistral': 1973, 'n_tokens_neox': 1790, 'n_words': 369}