コード例 #1
0
    def test_query_made_as_expected(self):
        graph = nx.MultiDiGraph()

        graph.add_node(0, concept=Thing('V123', 'person', 'entity'), probabilities=np.array([1.0, 0.0, 0.0]),
                       prediction=0)
        graph.add_node(1, concept=Thing('V1235', 'disease', 'entity'), probabilities=np.array([1.0, 0.0, 0.0]),
                       prediction=0)
        graph.add_node(2, concept=Thing('V6543', 'diagnosis', 'relation'), probabilities=np.array([0.0, 0.0071, 0.9927]),
                       prediction=2)

        graph.add_edge(2, 0)
        graph.add_edge(2, 1)

        graphs = [graph]
        tx = MagicMock(grakn.client.Transaction)

        tx.commit = MagicMock()
        tx.query = MagicMock()

        write_predictions_to_grakn(graphs, tx)

        expected_query = (f'match'
                          f'$p id V123;'
                          f'$d id V1235;'
                          f'$kgcn isa kgcn;'
                          f'insert'
                          f'$pd(patient: $p, diagnosed-disease: $d, diagnoser: $kgcn) isa diagnosis,'
                          f'has probability-exists 0.993,'
                          f'has probability-non-exists 0.007,'
                          f'has probability-preexists 0.000;')

        tx.query.assert_called_with(expected_query)

        tx.commit.assert_called()
コード例 #2
0
    def test_query_made_only_if_relation_wins(self):
        graph = nx.MultiDiGraph()

        graph.add_node(0,
                       concept=Thing('V123', 'person', 'entity'),
                       probabilities=np.array([1.0, 0.0, 0.0]),
                       prediction=0)
        graph.add_node(1,
                       concept=Thing('V1235', 'disease', 'entity'),
                       probabilities=np.array([1.0, 0.0, 0.0]),
                       prediction=0)
        graph.add_node(2,
                       concept=Thing('V6543', 'diagnosis', 'relation'),
                       probabilities=np.array([0.0, 0.0, 1.0]),
                       prediction=1)

        graph.add_edge(2, 0)
        graph.add_edge(2, 1)

        graphs = [graph]
        tx = MagicMock(grakn.client.Transaction)

        tx.commit = MagicMock()
        tx.query = MagicMock()

        write_predictions_to_grakn(graphs, tx)

        tx.query.assert_not_called()

        tx.commit.assert_called()
コード例 #3
0
    def test_variable_graph_properties_are_transferred_to_graph(self):
        variable_graph = nx.MultiDiGraph()
        variable_graph.add_node('x', input=1, solution=1)
        variable_graph.add_node('y', input=1, solution=1)
        variable_graph.add_edge('y', 'x', type='employee', input=0, solution=1)

        person = Thing('V123', 'person', 'entity')
        employment = Thing('V123', 'employment', 'relation')
        concept_dict = {'x': person, 'y': employment}

        grakn_graph = concept_dict_to_graph(concept_dict, variable_graph)
        expected_grakn_graph = nx.MultiDiGraph()
        expected_grakn_graph.add_node(person,
                                      type='person',
                                      input=1,
                                      solution=1)
        expected_grakn_graph.add_node(employment,
                                      type='employment',
                                      input=1,
                                      solution=1)
        expected_grakn_graph.add_edge(employment,
                                      person,
                                      type='employee',
                                      input=0,
                                      solution=1)

        self.assertGraphsEqual(expected_grakn_graph, grakn_graph)
コード例 #4
0
    def test_edges_are_duplicated_as_expected(self):
        graph = nx.MultiDiGraph(name=0)

        p0 = Thing('V123', 'person', 'entity')
        p1 = Thing('V456', 'person', 'entity')
        par0 = Thing('V789', 'parentship', 'relation')

        # people
        graph.add_node(p0, type='person', solution=1)
        graph.add_node(p1, type='person', solution=1)

        # parentships
        graph.add_node(par0, type='parentship', solution=1)
        graph.add_edge(par0, p0, type='parent', solution=1)
        graph.add_edge(par0, p1, type='child', solution=1)

        duplicate_edges_in_reverse(graph)

        expected_graph = nx.MultiDiGraph(name=0)

        # people
        expected_graph.add_node(p0, type='person', solution=1)
        expected_graph.add_node(p1, type='person', solution=1)

        # parentships
        expected_graph.add_node(par0, type='parentship', solution=1)
        expected_graph.add_edge(par0, p0, type='parent', solution=1)
        expected_graph.add_edge(par0, p1, type='child', solution=1)

        # Duplicates
        expected_graph.add_edge(p0, par0, type='parent', solution=1)
        expected_graph.add_edge(p1, par0, type='child', solution=1)
        self.assertGraphsEqual(expected_graph, graph)
コード例 #5
0
    def test_graph_is_built_as_expected(self):
        g1 = nx.MultiDiGraph()
        g1.add_node('x')

        g2 = nx.MultiDiGraph()
        g2.add_node('x')
        g2.add_node('n')
        g2.add_edge('x', 'n', type='has')

        g3 = nx.MultiDiGraph()
        g3.add_node('x')
        g3.add_node('r')
        g3.add_node('y')
        g3.add_edge('r', 'x', type='child')
        g3.add_edge('r', 'y', type='parent')

        query_sampler_variable_graph_tuples = [
            ('match $x id V123; get;', mock_sampler, g1),
            ('match $x id V123, has name $n; get;', mock_sampler, g2),
            ('match $x id V123; $r(child: $x, parent: $y); get;', mock_sampler,
             g3),
            # TODO Add functionality for loading schema at a later date
            # ('match $x sub person; $x sub $type; get;', g4),
            # ('match $x sub $y; get;', g5),
        ]

        mock_tx = MockTransaction()

        combined_graph = build_graph_from_queries(
            query_sampler_variable_graph_tuples, mock_tx)

        person_exp = Thing('V123', 'person', 'entity')
        parentship_exp = Thing('V567', 'parentship', 'relation')
        name_exp = Thing('V987',
                         'name',
                         'attribute',
                         value_type='string',
                         value='Bob')
        expected_combined_graph = nx.MultiDiGraph()
        expected_combined_graph.add_node(person_exp, type='person')
        expected_combined_graph.add_node(name_exp,
                                         type='name',
                                         value_type='string',
                                         value='Bob')
        expected_combined_graph.add_node(parentship_exp, type='parentship')
        expected_combined_graph.add_edge(parentship_exp,
                                         person_exp,
                                         type='child')
        expected_combined_graph.add_edge(parentship_exp,
                                         person_exp,
                                         type='parent')
        expected_combined_graph.add_edge(person_exp, name_exp, type='has')

        self.assertGraphsEqual(expected_combined_graph, combined_graph)
コード例 #6
0
    def test_same_thing_occurs_in_two_different_variables(self):
        variable_graph = nx.MultiDiGraph()
        variable_graph.add_node('x')
        variable_graph.add_node('y')

        person = Thing('V123', 'person', 'entity')
        person2 = Thing('V123', 'person', 'entity')
        concept_dict = {'x': person, 'y': person2}

        grakn_graph = concept_dict_to_graph(concept_dict, variable_graph)
        expected_grakn_graph = nx.MultiDiGraph()
        expected_grakn_graph.add_node(person, type='person')

        self.assertGraphsEqual(expected_grakn_graph, grakn_graph)
コード例 #7
0
    def test_concept_dicts_are_built_as_expected_with_2_concepts(self):
        concept_map = MockConceptMap({
            'x': MockThing('V123', MockType('V456', 'person', 'ENTITY')),
            'y': MockThing('V789', MockType('V765', 'employment', 'RELATION')),
        })

        concept_dicts = concept_dict_from_concept_map(concept_map)

        expected_concept_dict = {
            'x': Thing('V123', 'person', 'entity'),
            'y': Thing('V789', 'employment', 'relation'),
        }

        self.assertEqual(expected_concept_dict, concept_dicts)
コード例 #8
0
    def test_edge_starting_from_entity_throws_exception(self):
        variable_graph = nx.MultiDiGraph()
        variable_graph.add_node('x')
        variable_graph.add_node('y')
        variable_graph.add_edge('x', 'y', type='employee')

        person = Thing('V123', 'person', 'entity')
        employment = Thing('V123', 'employment', 'relation')
        concept_dict = {'x': person, 'y': employment}

        with self.assertRaises(ValueError) as context:
            _ = concept_dict_to_graph(concept_dict, variable_graph)

        self.assertEqual(
            'An edge in the variable_graph originates from a non-relation, check the variable_graph!',
            str(context.exception))
コード例 #9
0
    def test_concept_dicts_are_built_as_expected(self):
        concept_map = MockConceptMap({'x': MockThing('V123', MockType('V456', 'person', 'ENTITY'))})
        concept_dicts = concept_dict_from_concept_map(concept_map)

        expected_concept_dicts = {'x': Thing('V123', 'person', 'entity')}

        self.assertEqual(expected_concept_dicts, concept_dicts)
コード例 #10
0
    def test_single_entity_single_relation_graph_is_as_expected(self):
        variable_graph = nx.MultiDiGraph()
        variable_graph.add_node('x')
        variable_graph.add_node('y')
        variable_graph.add_edge('y', 'x', type='employee')

        person = Thing('V123', 'person', 'entity')
        employment = Thing('V123', 'employment', 'relation')
        concept_dict = {'x': person, 'y': employment}

        grakn_graph = concept_dict_to_graph(concept_dict, variable_graph)
        expected_grakn_graph = nx.MultiDiGraph()
        expected_grakn_graph.add_node(person, type='person')
        expected_grakn_graph.add_node(employment, type='employment')
        expected_grakn_graph.add_edge(employment, person, type='employee')

        self.assertGraphsEqual(expected_grakn_graph, grakn_graph)
コード例 #11
0
    def test_graph_combined_as_expected(self):

        person = Thing('V123', 'person', 'entity')
        employment = Thing('V567', 'employment', 'relation')
        grakn_graph_a = nx.MultiDiGraph()
        grakn_graph_a.add_node(person)
        grakn_graph_a.add_node(employment)
        grakn_graph_a.add_edge(employment, person, type='employee')

        person_b = Thing('V123', 'person', 'entity')
        name = Thing('V1234', 'name', 'attribute', value_type='string', value='Bob')
        grakn_graph_b = nx.MultiDiGraph()
        grakn_graph_b.add_node(person_b)
        grakn_graph_b.add_node(name)
        grakn_graph_b.add_edge(person_b, name, type='has')

        combined_graph = combine_graphs_single_pass([grakn_graph_a, grakn_graph_b])

        person_ex = Thing('V123', 'person', 'entity')
        employment_ex = Thing('V567', 'employment', 'relation')
        name_ex = Thing('V1234', 'name', 'attribute', value_type='string', value='Bob')
        expected_combined_graph = nx.MultiDiGraph()
        expected_combined_graph.add_node(person_ex)
        expected_combined_graph.add_node(name_ex)
        expected_combined_graph.add_node(employment_ex)
        expected_combined_graph.add_edge(employment_ex, person_ex, type='employee')
        expected_combined_graph.add_edge(person_ex, name_ex, type='has')

        self.assertGraphsEqual(expected_combined_graph, combined_graph)
コード例 #12
0
    def test_edge_starting_from_attribute_throws_exception(self):
        variable_graph = nx.MultiDiGraph()
        variable_graph.add_node('x')
        variable_graph.add_node('y')
        variable_graph.add_edge('x', 'y', type='employee')

        name = Thing('V123',
                     'name',
                     'attribute',
                     data_type='string',
                     value='Bob')
        employment = Thing('V123', 'employment', 'relation')
        concept_dict = {'x': name, 'y': employment}

        with self.assertRaises(ValueError) as context:
            _ = concept_dict_to_graph(concept_dict, variable_graph)

        self.assertEqual(
            'An edge in the variable_graph originates from a non-relation, check the variable_graph!',
            str(context.exception))
コード例 #13
0
ファイル: queries_to_graph_test.py プロジェクト: syyunn/kglib
    def test_when_graph_node_properties_are_mismatched_exception_is_raised(
            self):
        person_a = Thing('V123', 'person', 'entity')
        name_a = Thing('V1234',
                       'name',
                       'attribute',
                       data_type='string',
                       value='Bob')
        grakn_graph_a = nx.MultiDiGraph(name='a')
        grakn_graph_a.add_node(person_a, input=1, solution=1)
        grakn_graph_a.add_node(name_a, input=1, solution=1)
        grakn_graph_a.add_edge(person_a,
                               name_a,
                               type='has',
                               input=0,
                               solution=1)

        person_b = Thing('V123', 'person', 'entity')
        name_b = Thing('V1234',
                       'name',
                       'attribute',
                       data_type='string',
                       value='Bob')
        grakn_graph_b = nx.MultiDiGraph(name='b')
        grakn_graph_b.add_node(person_b, input=1, solution=1)
        grakn_graph_b.add_node(name_b, input=0, solution=1)
        grakn_graph_b.add_edge(person_b,
                               name_b,
                               type='has',
                               input=0,
                               solution=1)

        with self.assertRaises(ValueError) as context:
            combine_2_graphs(grakn_graph_a, grakn_graph_b)

        self.assertEqual(
            ('Found non-matching node properties for node <name, V1234: Bob> '
             'between graphs a and b:\n'
             'In graph a: {\'input\': 1, \'solution\': 1}\n'
             'In graph b: {\'input\': 0, \'solution\': 1}'),
            str(context.exception))
コード例 #14
0
    def test_single_entity_graph_is_as_expected(self):
        variable_graph = nx.MultiDiGraph()
        variable_graph.add_node('x')

        person = Thing('V123', 'person', 'entity')
        concept_dict = {'x': person}

        grakn_graph = concept_dict_to_graph(concept_dict, variable_graph)
        expected_grakn_graph = nx.MultiDiGraph()
        expected_grakn_graph.add_node(person, type='person')

        self.assertGraphsEqual(expected_grakn_graph, grakn_graph)
コード例 #15
0
    def test_exception_if_sets_of_variables_differ(self):
        variable_graph = nx.MultiDiGraph()
        variable_graph.add_node('x')
        variable_graph.add_node('y')
        variable_graph.add_node('z')

        thing = Thing('V123', 'person', 'entity')
        concept_dict = {'x': thing, 'y': thing, 'a': thing}

        with self.assertRaises(ValueError) as context:
            _ = concept_dict_to_graph(concept_dict, variable_graph)

        self.assertEqual(
            'The variables in the variable_graph must match those in the concept_dict\n'
            'In the variable graph but not in the concept dict: {\'z\'}\n'
            'In the concept dict but not in the variable graph: {\'a\'}',
            str(context.exception))
コード例 #16
0
    def test_single_attribute_graph_is_as_expected(self):
        variable_graph = nx.MultiDiGraph()
        variable_graph.add_node('x')

        name = Thing('V123',
                     'name',
                     'attribute',
                     value_type='string',
                     value='Bob')
        concept_dict = {'x': name}

        grakn_graph = concept_dict_to_graph(concept_dict, variable_graph)
        expected_grakn_graph = nx.MultiDiGraph()
        expected_grakn_graph.add_node(name,
                                      type='name',
                                      value_type='string',
                                      value='Bob')

        self.assertGraphsEqual(expected_grakn_graph, grakn_graph)