Пример #1
0
    def test_transformation(self):
        R12 = np.random.rand(5, 3)

        t1 = ObjectType('type1', 2)
        t2 = ObjectType('type2', 2)
        relation = Relation(R12, t1, t2)
        fusion_graph = FusionGraph()
        fusion_graph.add_relation(relation)

        rnds = np.random.RandomState(0)
        fuser = Dfmf(init_type='random', random_state=rnds, max_iter=100
                     ).fuse(fusion_graph)

        new_R12 = R12[:2].copy()
        new_graph = FusionGraph([Relation(new_R12, t1, t2)])

        new_rnds = np.random.RandomState(0)
        transformer = DfmfTransform(random_state=new_rnds).transform(
            t1, new_graph, fuser)

        new_G1 = transformer.factor(t1)
        G1 = fuser.factor(t1)
        G2 = fuser.factor(t2)
        S12 = fuser.backbone(relation)
        new_R12_hat = np.dot(new_G1, np.dot(S12, G2.T))
        R12_hat = np.dot(G1, np.dot(S12, G2.T))

        diff_G1 = new_G1 - G1[:2]
        diff_hat = new_R12_hat - R12_hat[:2]
        self.assertLess(np.sum(diff_G1 ** 2) / diff_G1.size, 1e-5)
        self.assertLess(np.sum(diff_hat ** 2) / diff_hat.size, 1e-5)
Пример #2
0
    def test_get_names_by_object_type(self):
        rnds = np.random.RandomState(0)
        X = rnds.rand(10, 10)
        t1_names = list('ABCDEFGHIJ')
        t2_names = list('KLMNOPQRST')

        rel = Relation(X,
                       name='Test',
                       row_type=self.t1,
                       row_names=t1_names,
                       col_type=self.t2,
                       col_names=t2_names)
        rel2 = Relation(X,
                        name='Test2',
                        row_type=self.t2,
                        row_names=t2_names,
                        col_type=self.t3)
        fusion_graph = FusionGraph()
        fusion_graph.add_relation(rel)
        fusion_graph.add_relation(rel2)

        self.assertEqual(fusion_graph.get_names(self.t1), t1_names)
        self.assertEqual(fusion_graph.get_names(self.t2), t2_names)
        t3_names = fusion_graph.get_names(self.t3)
        self.assertEqual(len(t3_names), 10)
Пример #3
0
    def test_get_object_type_metadata(self):
        rnds = np.random.RandomState(0)
        X = rnds.rand(10, 10)
        a, b, c = list('ABCDEFGHIJ'), list('0123456789'), list('KLMNOPQRST')
        t1_metadata = [{'a': x} for x in a]
        t2_metadata = [{'b': x} for x in b]
        t2_metadata2 = [{'d': x} for x in b]

        rel = Relation(X, name='Test',
                       row_type=self.t1, row_metadata=t1_metadata,
                       col_type=self.t2, col_metadata=t2_metadata)
        rel2 = Relation(X, name='Test2',
                        row_type=self.t2, row_metadata=t2_metadata2,
                        col_type=self.t3)
        fusion_graph = FusionGraph()
        fusion_graph.add_relation(rel)
        fusion_graph.add_relation(rel2)

        def merge(d1, d2):
            d = {}
            d.update(d1)
            d.update(d2)
            return d

        self.assertEqual(fusion_graph.get_metadata(self.t1), t1_metadata)
        self.assertEqual(fusion_graph.get_metadata(self.t2), list(map(merge, t2_metadata, t2_metadata2)))
        t3_metadata = fusion_graph.get_metadata(self.t3)
        self.assertEqual(len(t3_metadata), 10)
        for md in t3_metadata:
            self.assertFalse(md)
Пример #4
0
    def test_transformation(self):
        R12 = np.random.rand(5, 3)

        t1 = ObjectType('type1', 2)
        t2 = ObjectType('type2', 2)
        relation = Relation(R12, t1, t2)
        fusion_graph = FusionGraph()
        fusion_graph.add_relation(relation)

        rnds = np.random.RandomState(0)
        fuser = Dfmf(init_type='random', random_state=rnds,
                     max_iter=100).fuse(fusion_graph)

        new_R12 = R12[:2].copy()
        new_graph = FusionGraph([Relation(new_R12, t1, t2)])

        new_rnds = np.random.RandomState(0)
        transformer = DfmfTransform(random_state=new_rnds).transform(
            t1, new_graph, fuser)

        new_G1 = transformer.factor(t1)
        G1 = fuser.factor(t1)
        G2 = fuser.factor(t2)
        S12 = fuser.backbone(relation)
        new_R12_hat = np.dot(new_G1, np.dot(S12, G2.T))
        R12_hat = np.dot(G1, np.dot(S12, G2.T))

        diff_G1 = new_G1 - G1[:2]
        diff_hat = new_R12_hat - R12_hat[:2]
        self.assertLess(np.sum(diff_G1**2) / diff_G1.size, 1e-5)
        self.assertLess(np.sum(diff_hat**2) / diff_hat.size, 1e-5)
Пример #5
0
 def test_removal_of_loops(self):
     fusion_graph = FusionGraph()
     fusion_graph.add_relation(self.relations2[-1])
     self.assertEqual(fusion_graph.n_relations, 1)
     self.assertEqual(fusion_graph.n_object_types, 1)
     fusion_graph.remove_relation(self.relations2[-1])
     self.assertEqual(fusion_graph.n_relations, 0)
     self.assertEqual(fusion_graph.n_object_types, 0)
Пример #6
0
 def test_removal_single_relation(self):
     fusion_graph = FusionGraph()
     fusion_graph.add_relation(self.relations1[0])
     self.assertEqual(fusion_graph.n_relations, 1)
     self.assertEqual(fusion_graph.n_object_types, 2)
     fusion_graph.remove_relation(self.relations1[0])
     self.assertEqual(fusion_graph.n_relations, 0)
     self.assertEqual(fusion_graph.n_object_types, 0)
Пример #7
0
 def test_removal_of_loops(self):
     fusion_graph = FusionGraph()
     fusion_graph.add_relation(self.relations2[-1])
     self.assertEqual(fusion_graph.n_relations, 1)
     self.assertEqual(fusion_graph.n_object_types, 1)
     fusion_graph.remove_relation(self.relations2[-1])
     self.assertEqual(fusion_graph.n_relations, 0)
     self.assertEqual(fusion_graph.n_object_types, 0)
Пример #8
0
 def test_removal_single_relation(self):
     fusion_graph = FusionGraph()
     fusion_graph.add_relation(self.relations1[0])
     self.assertEqual(fusion_graph.n_relations, 1)
     self.assertEqual(fusion_graph.n_object_types, 2)
     fusion_graph.remove_relation(self.relations1[0])
     self.assertEqual(fusion_graph.n_relations, 0)
     self.assertEqual(fusion_graph.n_object_types, 0)
Пример #9
0
    def test_dfmc(self):
        rnds = np.random.RandomState(0)
        R12 = rnds.rand(50, 30)

        t1 = ObjectType('type1', 50)
        t2 = ObjectType('type2', 30)
        relation = Relation(R12, t1, t2)
        fusion_graph = FusionGraph()
        fusion_graph.add_relation(relation)

        fuser = Dfmc(init_type='random', random_state=rnds).fuse(fusion_graph)
        self.assertEqual(fuser.backbone(relation).shape, (50, 30))
        self.assertEqual(fuser.factor(t1).shape, (50, 50))
        self.assertEqual(fuser.factor(t2).shape, (30, 30))
        np.testing.assert_almost_equal(fuser.complete(relation), relation.data)
Пример #10
0
    def test_dfmc(self):
        rnds = np.random.RandomState(0)
        R12 = rnds.rand(50, 30)

        t1 = ObjectType('type1', 50)
        t2 = ObjectType('type2', 30)
        relation = Relation(R12, t1, t2)
        fusion_graph = FusionGraph()
        fusion_graph.add_relation(relation)

        fuser = Dfmc(init_type='random', random_state=rnds).fuse(fusion_graph)
        self.assertEqual(fuser.backbone(relation).shape, (50, 30))
        self.assertEqual(fuser.factor(t1).shape, (50, 50))
        self.assertEqual(fuser.factor(t2).shape, (30, 30))
        np.testing.assert_almost_equal(fuser.complete(relation), relation.data)
Пример #11
0
    def test_preprocessors(self):
        rnds = np.random.RandomState(0)
        R12 = rnds.rand(50, 30)

        t1 = ObjectType('type1', 50)
        t2 = ObjectType('type2', 30)

        def preprocessor(data):
            return np.ones_like(data)

        relation = Relation(R12, t1, t2, preprocessor=preprocessor)
        fusion_graph = FusionGraph()
        fusion_graph.add_relation(relation)

        fuser = Dfmf(init_type='random', random_state=rnds).fuse(fusion_graph)
        self.assertEqual(fuser.backbone(relation).shape, (50, 30))
        self.assertEqual(fuser.factor(t1).shape, (50, 50))
        self.assertEqual(fuser.factor(t2).shape, (30, 30))
        trnf = np.ones_like(relation.data)
        np.testing.assert_almost_equal(fuser.complete(relation), trnf)
Пример #12
0
    def test_get_names_by_object_type(self):
        rnds = np.random.RandomState(0)
        X = rnds.rand(10, 10)
        t1_names = list('ABCDEFGHIJ')
        t2_names = list('KLMNOPQRST')

        rel = Relation(X, name='Test',
                       row_type=self.t1, row_names=t1_names,
                       col_type=self.t2, col_names=t2_names)
        rel2 = Relation(X, name='Test2',
                        row_type=self.t2, row_names=t2_names,
                        col_type=self.t3)
        fusion_graph = FusionGraph()
        fusion_graph.add_relation(rel)
        fusion_graph.add_relation(rel2)

        self.assertEqual(fusion_graph.get_names(self.t1), t1_names)
        self.assertEqual(fusion_graph.get_names(self.t2), t2_names)
        t3_names = fusion_graph.get_names(self.t3)
        self.assertEqual(len(t3_names), 10)
Пример #13
0
    def test_preprocessors(self):
        rnds = np.random.RandomState(0)
        R12 = rnds.rand(50, 30)

        t1 = ObjectType('type1', 50)
        t2 = ObjectType('type2', 30)

        def preprocessor(data):
            return np.ones_like(data)

        relation = Relation(R12, t1, t2, preprocessor=preprocessor)
        fusion_graph = FusionGraph()
        fusion_graph.add_relation(relation)

        fuser = Dfmf(init_type='random', random_state=rnds).fuse(fusion_graph)
        self.assertEqual(fuser.backbone(relation).shape, (50, 30))
        self.assertEqual(fuser.factor(t1).shape, (50, 50))
        self.assertEqual(fuser.factor(t2).shape, (30, 30))
        trnf = np.ones_like(relation.data)
        np.testing.assert_almost_equal(fuser.complete(relation), trnf)
Пример #14
0
    def test_postprocessors(self):
        rnds = np.random.RandomState(0)
        R12 = rnds.rand(50, 30)
        R12 = np.ma.masked_greater(R12, 0.7)

        t1 = ObjectType('type1', 50)
        t2 = ObjectType('type2', 30)

        def postprocessor(data):
            return data - 10

        relation = Relation(R12, t1, t2, name='R', postprocessor=postprocessor)
        fusion_graph = FusionGraph()
        fusion_graph.add_relation(relation)

        fuser = Dfmc(init_type='random', random_state=rnds).fuse(fusion_graph)
        self.assertEqual(fuser.backbone(relation).shape, (50, 30))
        self.assertEqual(fuser.factor(t1).shape, (50, 50))
        self.assertEqual(fuser.factor(t2).shape, (30, 30))
        trnf = relation.data - 10
        np.testing.assert_almost_equal(fuser.complete(relation), trnf)
        np.testing.assert_equal(fusion_graph.get_relation('R').data, R12)
Пример #15
0
    def test_postprocessors(self):
        rnds = np.random.RandomState(0)
        R12 = rnds.rand(50, 30)
        R12 = np.ma.masked_greater(R12, 0.7)

        t1 = ObjectType('type1', 50)
        t2 = ObjectType('type2', 30)

        def postprocessor(data):
            return data - 10

        relation = Relation(R12, t1, t2, name='R', postprocessor=postprocessor)
        fusion_graph = FusionGraph()
        fusion_graph.add_relation(relation)

        fuser = Dfmc(init_type='random', random_state=rnds).fuse(fusion_graph)
        self.assertEqual(fuser.backbone(relation).shape, (50, 30))
        self.assertEqual(fuser.factor(t1).shape, (50, 50))
        self.assertEqual(fuser.factor(t2).shape, (30, 30))
        trnf = relation.data - 10
        np.testing.assert_almost_equal(fuser.complete(relation), trnf)
        np.testing.assert_equal(fusion_graph.get_relation('R').data, R12)
Пример #16
0
    def test_get_object_type_metadata(self):
        rnds = np.random.RandomState(0)
        X = rnds.rand(10, 10)
        a, b, c = list('ABCDEFGHIJ'), list('0123456789'), list('KLMNOPQRST')
        t1_metadata = [{'a': x} for x in a]
        t2_metadata = [{'b': x} for x in b]
        t2_metadata2 = [{'d': x} for x in b]

        rel = Relation(X,
                       name='Test',
                       row_type=self.t1,
                       row_metadata=t1_metadata,
                       col_type=self.t2,
                       col_metadata=t2_metadata)
        rel2 = Relation(X,
                        name='Test2',
                        row_type=self.t2,
                        row_metadata=t2_metadata2,
                        col_type=self.t3)
        fusion_graph = FusionGraph()
        fusion_graph.add_relation(rel)
        fusion_graph.add_relation(rel2)

        def merge(d1, d2):
            d = {}
            d.update(d1)
            d.update(d2)
            return d

        self.assertEqual(fusion_graph.get_metadata(self.t1), t1_metadata)
        self.assertEqual(fusion_graph.get_metadata(self.t2),
                         list(map(merge, t2_metadata, t2_metadata2)))
        t3_metadata = fusion_graph.get_metadata(self.t3)
        self.assertEqual(len(t3_metadata), 10)
        for md in t3_metadata:
            self.assertFalse(md)