예제 #1
0
    def test_vstack_with_single_vector(self):
        expected = SparseMatrix.from_list([[1, 0, 2, 0, 3, 4, 0, 5]])
        result = SparseMatrix.vstack(
            [SparseVector.from_list([1, 0, 2, 0, 3, 4, 0, 5])])

        self.assertEqual(result.shape, (1, 8))
        self.assertEqual(result, expected)
예제 #2
0
    def test_identity_with_multiple_vectors(self):
        expected = SparseMatrix.from_list([[1, 0, 0, 0, 0], [0, 1, 0, 0, 0],
                                           [0, 0, 1, 0, 0], [0, 0, 0, 1, 0]])
        result = SparseMatrix.identity((4, 5))

        self.assertEqual(result.shape, (4, 5))
        self.assertEqual(result, expected)
예제 #3
0
 def test_from_list_with_several_unique_elements(self):
     mat = SparseMatrix.from_list([[0, 1, 0, 0], [0, 0, 0, 2], [4, 0, 0, 0],
                                   [0, 0, 3, 0]])
     self.assertEqual(len(mat), 4)
     self.assertTrue(np.array_equal(mat.cols, np.array([1, 3, 0, 2])))
     self.assertTrue(np.array_equal(mat.data, np.array([1, 2, 4, 3])))
     self.assertTrue(np.array_equal(mat.rows, np.array([0, 1, 2, 3])))
예제 #4
0
    def test_to_dense_with_random(self):
        array = np.random.randint(0, 5, (10, 10), dtype=np.uint16)
        mat = SparseMatrix.from_list(array)

        expected = np.copy(array)
        result = mat.to_dense()

        self.assertTrue(np.array_equal(result, expected))
예제 #5
0
    def test_vstack_with_multiple_vectors(self):
        arrays = [np.random.randint(0, 10, 30) for _ in range(20)]
        vectors = [SparseVector.from_list(arrays[i]) for i in range(20)]

        expected = SparseMatrix.from_list(arrays)
        result = SparseMatrix.vstack(vectors)

        self.assertEqual(result, expected)
예제 #6
0
 def test_from_list_with_no_unique_elements(self):
     mat = SparseMatrix.from_list([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0],
                                   [0, 0, 0, 0]])
     self.assertEqual(len(mat), 0)
     self.assertEqual(mat, SparseMatrix.zero((4, 4)))
     self.assertTrue(np.array_equal(mat.cols, np.array([])))
     self.assertTrue(np.array_equal(mat.data, np.array([])))
     self.assertTrue(np.array_equal(mat.rows, np.array([])))
예제 #7
0
    def test_get_rows_using_simple_matrix(self):
        mat = SparseMatrix.from_list([[0, 2, 0], [0, 0, 3], [1, 0, 0]])

        expected = [
            SparseVector.from_list([0, 2, 0]),
            SparseVector.from_list([0, 0, 3]),
            SparseVector.from_list([1, 0, 0])
        ]
        result = mat.get_rows()

        for ex, res in zip(expected, result):
            self.assertEqual(res, ex)
예제 #8
0
    def test_get_row_using_simple_matrix(self):
        mat = SparseMatrix.from_list([[0, 2, 0], [0, 0, 3], [1, 0, 0]])

        expected0 = SparseVector.from_list([0, 2, 0])
        expected1 = SparseVector.from_list([0, 0, 3])
        expected2 = SparseVector.from_list([1, 0, 0])

        result0 = mat.get_row(0)
        result1 = mat.get_row(1)
        result2 = mat.get_row(2)

        self.assertEqual(expected0, result0)
        self.assertEqual(expected1, result1)
        self.assertEqual(expected2, result2)
예제 #9
0
    def test_read_database_with_single_row(self):
        src = "0,0,1,0,2,0,3,4,0,5,1"

        with Pool(processes=4) as pool, StringIO(src) as stream:
            expected = TrainingDatabase(
                np.ones(1, dtype=np.uint8),
                SparseMatrix.from_list([
                    [0, 1, 0, 2, 0, 3, 4, 0, 5],
                ]))
            expected.counts.cols = expected.counts.cols + 1
            expected.counts.shape = (expected.counts.shape[0],
                                     expected.counts.shape[1] + 1)

            result = CsvIO.read_database(pool, stream)

            self.assertEqual(result, expected)
예제 #10
0
    def test_read_database_with_multiple_rows(self):
        src = "0,0,1,0,2,0,3,4,0,5,1\n" +\
              "1,6,0,7,0,8,9,0,10,0,2\n" +\
              "2,0,11,0,12,0,13,14,0,15,1\n"

        with Pool(processes=4) as pool, StringIO(src) as stream:
            expected = TrainingDatabase(
                np.array([1, 2, 1], dtype=np.uint8),
                SparseMatrix.from_list([[0, 1, 0, 2, 0, 3, 4, 0, 5],
                                        [6, 0, 7, 0, 8, 9, 0, 10, 0],
                                        [0, 11, 0, 12, 0, 13, 14, 0, 15]]))
            expected.counts.cols = expected.counts.cols + 1
            expected.counts.shape = (expected.counts.shape[0],
                                     expected.counts.shape[1] + 1)

            result = CsvIO.read_database(pool, stream)

            self.assertEqual(result, expected)
예제 #11
0
    def test_identity_with_single_vector(self):
        expected = SparseMatrix.from_list([[1, 0, 0, 0, 0]])
        result = SparseMatrix.identity((1, 5))

        self.assertEqual(result.shape, (1, 5))
        self.assertEqual(result, expected)