Ejemplo n.º 1
0
    def test_pipeline(self):
        rnds = np.random.RandomState(0)
        R12 = rnds.rand(50, 30)
        R13 = rnds.rand(50, 40)
        R23 = rnds.rand(30, 40)

        t1 = ObjectType('type1', 30)
        t2 = ObjectType('type2', 40)
        t3 = ObjectType('type3', 40)
        relations = [
            Relation(R12, t1, t2),
            Relation(R13, t1, t3),
            Relation(R23, t2, t3)
        ]
        fusion_graph = FusionGraph()
        fusion_graph.add_relations_from(relations)

        fuser = Dfmf(random_state=rnds).fuse(fusion_graph)
        self.assertEqual(fuser.factor(t1).shape, (50, 30))
        self.assertEqual(fuser.factor(t2).shape, (30, 40))
        self.assertEqual(fuser.factor(t3).shape, (40, 40))
        self.assertEqual(fuser.backbone(relations[0]).shape, (30, 40))
        self.assertEqual(fuser.backbone(relations[1]).shape, (30, 40))
        self.assertEqual(fuser.backbone(relations[2]).shape, (40, 40))

        new_R12 = rnds.rand(15, 30)
        new_R13 = rnds.rand(15, 40)

        new_relations = [Relation(new_R12, t1, t2), Relation(new_R13, t1, t3)]
        new_graph = FusionGraph(new_relations)

        transformer = DfmfTransform(random_state=rnds).transform(
            t1, new_graph, fuser)
        self.assertEqual(transformer.factor(t1).shape, (15, 30))
Ejemplo n.º 2
0
    def test_dfmf(self):
        rnds = np.random.RandomState(0)
        R12 = rnds.rand(30, 30)
        R13 = rnds.rand(30, 30)

        t1 = ObjectType('type1', 50)
        t2 = ObjectType('type2', 30)
        t3 = ObjectType('type3', 10)
        fusion_graph = FusionGraph()
        relations = [Relation(R12, t1, t2), Relation(R13, t1, t3)]
        fusion_graph.add_relations_from(relations)

        fuser = Dfmf(init_type='random', random_state=rnds, n_run=3
                     ).fuse(fusion_graph)
        self.assertEqual(len(list(fuser.factor(t1))), 3)
        self.assertEqual(len(list(fuser.factor(t2))), 3)
        self.assertEqual(len(list(fuser.factor(t3))), 3)
        self.assertEqual(len(list(fuser.backbone(relations[0]))), 3)
        self.assertEqual(len(list(fuser.backbone(relations[1]))), 3)
        for object_type in [t1, t2, t3]:
            for factor in fuser.factor(object_type):
                self.assertEqual(factor.shape, (30, object_type.rank))

        G1 = fuser.factor(t1, run=1)
        S13 = fuser.backbone(relations[1], run=1)
        G3 = fuser.factor(t3, run=1)
        R13_hat = np.dot(G1, np.dot(S13, G3.T))
        completed = fuser.complete(relations[1], run=1)
        np.testing.assert_almost_equal(completed, R13_hat)
Ejemplo n.º 3
0
    def test_dfmf(self):
        rnds = np.random.RandomState(0)
        R12 = rnds.rand(30, 30)
        R13 = rnds.rand(30, 30)

        t1 = ObjectType('type1', 50)
        t2 = ObjectType('type2', 30)
        t3 = ObjectType('type3', 10)
        fusion_graph = FusionGraph()
        relations = [Relation(R12, t1, t2), Relation(R13, t1, t3)]
        fusion_graph.add_relations_from(relations)

        fuser = Dfmf(init_type='random', random_state=rnds,
                     n_run=3).fuse(fusion_graph)
        self.assertEqual(len(list(fuser.factor(t1))), 3)
        self.assertEqual(len(list(fuser.factor(t2))), 3)
        self.assertEqual(len(list(fuser.factor(t3))), 3)
        self.assertEqual(len(list(fuser.backbone(relations[0]))), 3)
        self.assertEqual(len(list(fuser.backbone(relations[1]))), 3)
        for object_type in [t1, t2, t3]:
            for factor in fuser.factor(object_type):
                self.assertEqual(factor.shape, (30, object_type.rank))

        G1 = fuser.factor(t1, run=1)
        S13 = fuser.backbone(relations[1], run=1)
        G3 = fuser.factor(t3, run=1)
        R13_hat = np.dot(G1, np.dot(S13, G3.T))
        completed = fuser.complete(relations[1], run=1)
        np.testing.assert_almost_equal(completed, R13_hat)
    def test_dfmf(self):
        rnds = np.random.RandomState(0)
        R12_1 = np.random.rand(30, 30)
        R12_2 = np.random.rand(30, 30)
        R13 = np.random.rand(30, 20)

        t1 = ObjectType('type1', 30)
        t2 = ObjectType('type2', 30)
        t3 = ObjectType('type3', 20)
        relations = [
            Relation(R12_1, t1, t2),
            Relation(R12_2, t1, t2),
            Relation(R13, t1, t3)
        ]
        fusion_graph = FusionGraph()
        fusion_graph.add_relations_from(relations)
        self.assertEqual(len(fusion_graph.relations), 3)
        self.assertEqual(len(fusion_graph.object_types), 3)

        fuser = Dfmf(init_type='random', random_state=rnds).fuse(fusion_graph)
        self.assertEqual(fuser.backbone(relations[0]).shape, (30, 30))
        self.assertEqual(fuser.backbone(relations[1]).shape, (30, 30))
        self.assertEqual(fuser.backbone(relations[2]).shape, (30, 20))
        G1 = fuser.factor(t1)
        G2 = fuser.factor(t2)
        S12_1 = fuser.backbone(relations[0])
        S12_2 = fuser.backbone(relations[1])
        R12_1_hat = np.dot(G1, np.dot(S12_1, G2.T))
        R12_2_hat = np.dot(G1, np.dot(S12_2, G2.T))
        np.testing.assert_almost_equal(fuser.complete(relations[0]), R12_1_hat)
        np.testing.assert_almost_equal(fuser.complete(relations[1]), R12_2_hat)
    def test_dfmf(self):
        rnds = np.random.RandomState(0)
        R12_1 = np.random.rand(30, 30)
        R12_2 = np.random.rand(30, 30)
        R13 = np.random.rand(30, 20)

        t1 = ObjectType('type1', 30)
        t2 = ObjectType('type2', 30)
        t3 = ObjectType('type3', 20)
        relations = [Relation(R12_1, t1, t2), Relation(R12_2, t1, t2),
                     Relation(R13, t1, t3)]
        fusion_graph = FusionGraph()
        fusion_graph.add_relations_from(relations)
        self.assertEqual(len(fusion_graph.relations), 3)
        self.assertEqual(len(fusion_graph.object_types), 3)

        fuser = Dfmf(init_type='random', random_state=rnds).fuse(fusion_graph)
        self.assertEqual(fuser.backbone(relations[0]).shape, (30, 30))
        self.assertEqual(fuser.backbone(relations[1]).shape, (30, 30))
        self.assertEqual(fuser.backbone(relations[2]).shape, (30, 20))
        G1 = fuser.factor(t1)
        G2 = fuser.factor(t2)
        S12_1 = fuser.backbone(relations[0])
        S12_2 = fuser.backbone(relations[1])
        R12_1_hat = np.dot(G1, np.dot(S12_1, G2.T))
        R12_2_hat = np.dot(G1, np.dot(S12_2, G2.T))
        np.testing.assert_almost_equal(fuser.complete(relations[0]), R12_1_hat)
        np.testing.assert_almost_equal(fuser.complete(relations[1]), R12_2_hat)
Ejemplo n.º 6
0
    def test_pipeline(self):
        rnds = np.random.RandomState(0)
        R12 = rnds.rand(50, 30)
        R13 = rnds.rand(50, 40)
        R23 = rnds.rand(30, 40)

        t1 = ObjectType('type1', 30)
        t2 = ObjectType('type2', 40)
        t3 = ObjectType('type3', 40)
        relations = [Relation(R12, t1, t2),
                     Relation(R13, t1, t3), Relation(R23, t2, t3)]
        fusion_graph = FusionGraph()
        fusion_graph.add_relations_from(relations)

        fuser = Dfmf(random_state=rnds).fuse(fusion_graph)
        self.assertEqual(fuser.factor(t1).shape, (50, 30))
        self.assertEqual(fuser.factor(t2).shape, (30, 40))
        self.assertEqual(fuser.factor(t3).shape, (40, 40))
        self.assertEqual(fuser.backbone(relations[0]).shape, (30, 40))
        self.assertEqual(fuser.backbone(relations[1]).shape, (30, 40))
        self.assertEqual(fuser.backbone(relations[2]).shape, (40, 40))

        new_R12 = rnds.rand(15, 30)
        new_R13 = rnds.rand(15, 40)

        new_relations = [Relation(new_R12, t1, t2), Relation(new_R13, t1, t3)]
        new_graph = FusionGraph(new_relations)

        transformer = DfmfTransform(random_state=rnds).transform(t1, new_graph, fuser)
        self.assertEqual(transformer.factor(t1).shape, (15, 30))
Ejemplo n.º 7
0
    def test_manipulation(self):
        fusion_graph = FusionGraph()
        fusion_graph.add_relations_from(self.relations2)

        self.assertEqual(fusion_graph.n_object_types, 5)
        self.assertEqual(fusion_graph.n_relations, 10)

        fusion_graph.remove_relation(self.relations2[6])
        self.assertEqual(fusion_graph.n_object_types, 5)
        self.assertEqual(fusion_graph.n_relations, 9)

        fusion_graph.remove_relations_from([
            self.relations2[9], self.relations2[4], self.relations2[5]])
        self.assertEqual(fusion_graph.n_object_types, 4)
        self.assertEqual(fusion_graph.n_relations, 6)
Ejemplo n.º 8
0
    def test_manipulation(self):
        fusion_graph = FusionGraph()
        fusion_graph.add_relations_from(self.relations2)

        self.assertEqual(fusion_graph['Test2'], self.relations2[0])
        self.assertEqual(fusion_graph['Test3'], self.relations2[8])

        self.assertEqual(fusion_graph.n_object_types, 5)
        self.assertEqual(fusion_graph.n_relations, 10)

        fusion_graph.remove_relation(self.relations2[6])
        self.assertEqual(fusion_graph.n_object_types, 5)
        self.assertEqual(fusion_graph.n_relations, 9)

        fusion_graph.remove_relations_from(
            [self.relations2[9], self.relations2[4], self.relations2[5]])
        self.assertEqual(fusion_graph.n_object_types, 4)
        self.assertEqual(fusion_graph.n_relations, 6)
Ejemplo n.º 9
0
 def test_drawing(self):
     fusion_graph = FusionGraph()
     fusion_graph.add_relations_from(self.relations1)
Ejemplo n.º 10
0
 def test_drawing(self):
     fusion_graph = FusionGraph()
     fusion_graph.add_relations_from(self.relations1)