Exemple #1
0
    def test_init_svd(self):
        test_cases = [(self.space2, self.us, self.us2, self.x, self.row3)]
        red1 = Svd(2)
        red2 = Svd(1)

        for in_s, expected_mat, expected_mat2, data, rows in test_cases:
            in_s = in_s.apply(red1)
            per_s = PeripheralSpace(in_s, DenseMatrix(data), rows)

            np.testing.assert_array_almost_equal(expected_mat,
                                                 per_s.cooccurrence_matrix.mat,
                                                 2)
            self.assertListEqual(per_s.id2row, in_s.id2row)
            self.assertListEqual(per_s.id2column, [])
            self.assertDictEqual(per_s.row2id, in_s.row2id)
            self.assertDictEqual(per_s.column2id, {})
            self.assertEqual(1, len(per_s.operations))

            in_s = in_s.apply(red2)
            per_s = PeripheralSpace(in_s, DenseMatrix(data), rows)

            np.testing.assert_array_almost_equal(expected_mat2,
                                                 per_s.cooccurrence_matrix.mat,
                                                 2)
            self.assertListEqual(per_s.id2row, in_s.id2row)
            self.assertListEqual(per_s.id2column, [])
            self.assertDictEqual(per_s.row2id, in_s.row2id)
            self.assertDictEqual(per_s.column2id, {})
            self.assertEqual(2, len(per_s.operations))
Exemple #2
0
    def test_init(self):
        test_cases = [(self.space1, self.m2, self.row2, np.array([[2, 0.5,
                                                                   1]]),
                       np.array([[0.69314718, 0, 0]]))]

        w1 = EpmiWeighting()
        w2 = PlogWeighting()

        for core_s, per_mat, per_row, per_mat_out1, per_mat_out2 in test_cases:
            tmp_mat = per_mat.copy()
            tmp_core_mat = core_s.cooccurrence_matrix.mat
            per_s1 = PeripheralSpace(core_s, DenseMatrix(per_mat), per_row)

            np.testing.assert_array_equal(per_s1.cooccurrence_matrix.mat,
                                          tmp_mat)
            self.assert_column_identical(per_s1, core_s)
            self.assertListEqual(per_s1.id2row, per_row)
            self.assertListEqual(per_s1.operations, core_s.operations)

            core_s1 = core_s.apply(w1)
            per_s2 = PeripheralSpace(core_s1, DenseMatrix(per_mat), per_row)
            np.testing.assert_array_almost_equal(
                per_s2.cooccurrence_matrix.mat, per_mat_out1)
            self.assert_column_identical(per_s2, core_s1)
            self.assertListEqual(per_s2.id2row, per_row)
            self.assertListEqual(per_s2.operations, core_s1.operations)
            self.assertEqual(len(per_s2.operations), 1)

            core_s2 = core_s1.apply(w2)
            per_s3 = PeripheralSpace(core_s2, DenseMatrix(per_mat), per_row)
            np.testing.assert_array_almost_equal(
                per_s3.cooccurrence_matrix.mat, per_mat_out2)
            self.assert_column_identical(per_s3, core_s2)
            self.assertListEqual(per_s3.id2row, per_row)
            self.assertListEqual(per_s3.operations, core_s2.operations)
            self.assertEqual(len(per_s3.operations), 2)

            np.testing.assert_array_equal(tmp_core_mat,
                                          core_s.cooccurrence_matrix.mat)

            core_s3 = core_s2
            per_s4 = PeripheralSpace(core_s3, DenseMatrix(per_mat), per_row)
            np.testing.assert_array_almost_equal(
                per_s4.cooccurrence_matrix.mat, per_mat_out2)
            self.assert_column_identical(per_s4, core_s2)
            self.assertListEqual(per_s4.id2row, per_row)
            self.assertListEqual(per_s4.operations, core_s3.operations)
            self.assertEqual(len(per_s4.operations), 2)

            np.testing.assert_array_equal(tmp_core_mat,
                                          core_s.cooccurrence_matrix.mat)
Exemple #3
0
    def test_add_rows(self):

        test_cases = [(self.space1, self.m2, self.row2, np.array([[4, 2,
                                                                   6]]), ["c"],
                       np.array([[4, 2, 6], [4, 2, 6]]),
                       np.array([[0.69314718, 0, 0], [0.69314718, 0, 0]]), {
                           "b": 0,
                           "c": 1
                       }, ["b", "c"])]

        for (core_sp, per_mat1, id2row1, per_mat2, id2row2, per_exp_mat1,
             per_exp_mat2, per_exp_row2id, per_exp_id2row) in test_cases:

            per_sp = PeripheralSpace(core_sp, DenseMatrix(per_mat1), id2row1)
            per_sp.add_rows(DenseMatrix(per_mat2), id2row2)
            np.testing.assert_array_almost_equal(
                per_sp.cooccurrence_matrix.mat, per_exp_mat1, 7)

            self.assertDictEqual(per_sp.row2id, per_exp_row2id)
            self.assertListEqual(per_sp.id2row, per_exp_id2row)

            self.assertDictEqual(per_sp.column2id, core_sp.column2id)
            self.assertListEqual(per_sp.id2column, core_sp.id2column)

            core_sp2 = core_sp.apply(PpmiWeighting())
            per_sp2 = PeripheralSpace(core_sp2, DenseMatrix(per_mat1), id2row1)
            per_sp2.add_rows(DenseMatrix(per_mat2), id2row2)
            np.testing.assert_array_almost_equal(
                per_sp2.cooccurrence_matrix.mat, per_exp_mat2, 7)

            self.assertRaises(ValueError, per_sp2.add_rows,
                              DenseMatrix(per_mat2), id2row1)

            self.assertRaises(ValueError, per_sp2.add_rows,
                              DenseMatrix(per_mat2), id2row2)

            self.assertRaises(ValueError, per_sp2.add_rows,
                              DenseMatrix(per_mat2), ["d", "e"])
Exemple #4
0
def transform_raw_per_space(raw_per_space, in_file_prefix, out_dir, out_format,
                            core_space_file):

    in_file_descr = "PER_SS." + in_file_prefix.split("/")[-1]
    core_space = io_utils.load(core_space_file, Space)
    core_descr = ".".join(core_space_file.split("/")[-1].split(".")[0:-1])

    space = PeripheralSpace(core_space, raw_per_space.cooccurrence_matrix,
                            raw_per_space.id2row, raw_per_space.row2id)

    print("Printing...")
    out_file_prefix = "%s/%s.%s" % (out_dir, in_file_descr, core_descr)
    io_utils.save(space, out_file_prefix + ".pkl")
    if not out_format is None:
        space.export(out_file_prefix, format=out_format)
Exemple #5
0
    def test_add_rows_svd(self):
        test_cases = [(self.space2, np.vstack([self.us2[0], self.us2[0]]),
                       self.m1, ["e"], ["f"], {
                           "e": 0,
                           "f": 1
                       })]
        red1 = Svd(2)
        red2 = Svd(1)

        for in_s, expected_mat, data, id2row1, id2row2, row2id in test_cases:
            in_s = in_s.apply(red1)
            in_s = in_s.apply(red2)
            per_s = PeripheralSpace(in_s, DenseMatrix(data), id2row1)
            per_s.add_rows(DenseMatrix(data), id2row2)

            np.testing.assert_array_almost_equal(expected_mat,
                                                 per_s.cooccurrence_matrix.mat,
                                                 2)
            self.assertListEqual(per_s.id2row, id2row1 + id2row2)
            self.assertListEqual(per_s.id2column, [])
            self.assertDictEqual(per_s.row2id, row2id)
            self.assertDictEqual(per_s.column2id, {})
            self.assertEqual(2, len(per_s.operations))
Exemple #6
0
    def test_per_space_top_feat_selection(self):

        test_cases = [(self.space_d, 1, ["f3"], {
            "f3": 0
        }, np.mat([[3], [5]])),
                      (self.space_d, 2, ["f3", "f1"], {
                          "f3": 0,
                          "f1": 1
                      }, np.mat([[3, 1], [5, 4]])),
                      (self.space_d, 4, ["f3", "f1", "f2"], {
                          "f3": 0,
                          "f1": 1,
                          "f2": 2
                      }, np.mat([[3, 1, 2], [5, 4, 0]]))]

        for space_d, no_dim, id2col, col2id, mat in test_cases:

            trans = TopFeatureSelection(no_dim)
            new_space = space_d.apply(trans)

            #peripheral test simple test
            per_sp = PeripheralSpace(new_space, DenseMatrix(self.a),
                                     ["c", "d"])

            self.assertListEqual(per_sp.id2row, ["c", "d"])
            self.assertListEqual(per_sp.id2column, id2col)
            self.assertDictEqual(per_sp.column2id, col2id)

            np.testing.assert_array_equal(per_sp.cooccurrence_matrix.mat, mat)

            #peripheral test with add rows
            per_sp = PeripheralSpace(new_space, DenseMatrix(self.a[0, :]),
                                     ["c"])
            per_sp.add_rows(DenseMatrix(self.a[1, :]), ["d"])

            self.assertListEqual(per_sp.id2row, ["c", "d"])
            self.assertListEqual(per_sp.id2column, id2col)
            self.assertDictEqual(per_sp.column2id, col2id)

            np.testing.assert_array_equal(per_sp.cooccurrence_matrix.mat, mat)

            #peripheral test, with plog applied to core BEFORE feat selection
            plogmat = mat.copy()
            plogmat[plogmat == 0] = 1
            plogmat = np.log(plogmat)

            new_space = space_d.apply(PlogWeighting())
            trans = TopFeatureSelection(no_dim)
            new_space = new_space.apply(trans)

            per_sp = PeripheralSpace(new_space, DenseMatrix(self.a),
                                     ["c", "d"])

            self.assertListEqual(per_sp.id2row, ["c", "d"])
            self.assertListEqual(per_sp.id2column, id2col)
            self.assertDictEqual(per_sp.column2id, col2id)

            np.testing.assert_array_almost_equal(
                per_sp.cooccurrence_matrix.mat, plogmat, 7)

            #peripheral test, with plog applied to core AFTER feat selection
            trans = TopFeatureSelection(no_dim)
            new_space = space_d.apply(trans)
            new_space = new_space.apply(PlogWeighting())

            per_sp = PeripheralSpace(new_space, DenseMatrix(self.a),
                                     ["c", "d"])

            self.assertListEqual(per_sp.id2row, ["c", "d"])
            self.assertListEqual(per_sp.id2column, id2col)
            self.assertDictEqual(per_sp.column2id, col2id)

            np.testing.assert_array_almost_equal(
                per_sp.cooccurrence_matrix.mat, plogmat, 7)