Exemplo n.º 1
0
 def test_add_role_edge_adds_role_as_expected(self):
     g = QueryGraph()
     g.add_vars('a', 'b')
     g.add_role_edge('a', 'b', 'role')
     edges = [edge for edge in g.edges]
     self.assertEqual(1, len(edges))
     self.assertEqual('role', g.edges['a', 'b', 0]['type'])
Exemplo n.º 2
0
 def test_add_role_edge_adds_role_as_expected(self):
     g = QueryGraph()
     g.add_vars('a', 'b')
     g.add_role_edge('a', 'b', 'role_label', 1)
     edges = [edge for edge in g.edges]
     self.assertEqual(1, len(edges))
     self.assertDictEqual({
         'type': 'role_label',
         'solution': 1
     }, g.edges['a', 'b', 0])
Exemplo n.º 3
0
def get_query_handles(scenario_idx):

    # Contains Schema-Specific queries that retrive sub-graphs from grakn!
    """
    Creates an iterable, each element containing a Graql query, a function to sample the answers, and a QueryGraph
    object which must be the Grakn graph representation of the query. This tuple is termed a "query_handle"

    Args:
        scenario_idx: A uniquely identifiable attribute value used to anchor the results of the queries to a specific
                    subgraph
        ===>>> SINGLE SCENARIO_ID

    Returns:
        query handles
    """
    # === Convergence ===
    conv, scn, ray, nray, src, dsrc, seg, dseg, l, s, srcp, bathy, bt, ssp, loc, ses,\
    sspval, dsspmax, speed, dssp, dct, ddct, gd, duct, nod = 'conv','scn','ray', 'nray',\
    'src', 'dsrc', 'seg', 'dseg','l','s','srcp','bathy','bt','ssp','loc','ses',\
    'sspval','dsspmax','speed','dssp','dct','ddct','gd','duct','nod'

    #dt,'dt',
    convergence_query = inspect.cleandoc(
        f'''match 
        $scn isa sound-propagation-scenario, has scenario_id {scenario_idx};'''
        '''$ray isa ray-input, has num_rays $nray; 
        $src isa source, has depth $dsrc; 
        $seg isa bottom-segment, has depth $dseg, has length $l, has slope $s;
        $conv(converged_scenario: $scn, minimum_resolution: $ray) isa convergence;
        $srcp(defined_by_src: $scn, define_src: $src) isa src-position;
        $bathy(defined_by_bathy: $scn, define_bathy: $seg) isa bathymetry, has bottom_type $bt;
        $ssp isa SSP-vec, has location $loc, has season $ses, has SSP_value $sspval, has depth $dsspmax;
        $dct isa duct, has depth $ddct, has grad $gd;
        $speed(defined_by_SSP: $scn, define_SSP: $ssp) isa sound-speed;
        $duct(find_channel: $ssp, channel_exists: $dct) isa SSP-channel, has number_of_ducts $nod; 
        $sspval has depth $dssp;
        {$dssp == $dsrc;} or {$dssp == $dseg;} or {$dssp == $ddct;} or {$dssp == $dsspmax;}; 
        get;''')  # has duct_type $dt,
    '''
    get $scn, $sid, $ray, $nray, $conv,
    $src, $dsrc, $seg, $dseg, $l, $s, $srcp, $bathy, $bt,
    $ssp, $loc, $ses,  $dsspmax, $speed, $sspval, $dssp,
    $dct, $ddct, $gd, $dt, $duct, $nod;
    '''

    convergence_query_graph = (QueryGraph()
                             .add_vars([conv], TO_INFER)
                             .add_vars([scn, ray, nray, src, dsrc, seg, dseg, \
                                        l, s, srcp, bathy, bt, ssp, loc, ses, \
                                        sspval, dsspmax, speed, dssp, dct, ddct,\
                                         gd, duct, nod], PREEXISTS) #dt,
                             .add_has_edge(ray, nray, PREEXISTS)
                             .add_has_edge(src, dsrc, PREEXISTS)
                             .add_has_edge(seg, dseg, PREEXISTS)
                             .add_has_edge(seg, l, PREEXISTS)
                             .add_has_edge(seg, s, PREEXISTS)
                             .add_has_edge(ssp, loc, PREEXISTS)
                             .add_has_edge(ssp, ses, PREEXISTS)
                             .add_has_edge(ssp, sspval, PREEXISTS)
                             .add_has_edge(ssp, dsspmax, PREEXISTS)
                             .add_has_edge(dct, ddct, PREEXISTS)
                             #.add_has_edge(dct, dt, PREEXISTS)
                             .add_has_edge(dct, gd, PREEXISTS)
                             .add_has_edge(bathy, bt, PREEXISTS)
                             .add_has_edge(duct, nod, PREEXISTS)
                             .add_has_edge(sspval, dssp, PREEXISTS)
                             .add_role_edge(conv, scn, 'converged_scenario', TO_INFER) #TO_INFER VS CANDIDATE BELOW
                             .add_role_edge(conv, ray, 'minimum_resolution', TO_INFER)
                             .add_role_edge(srcp, scn, 'defined_by_src', PREEXISTS)
                             .add_role_edge(srcp, src, 'define_src', PREEXISTS)
                             .add_role_edge(bathy, scn, 'defined_by_bathy', PREEXISTS)
                             .add_role_edge(bathy, seg, 'define_bathy', PREEXISTS)
                             .add_role_edge(speed, scn, 'defined_by_SSP', PREEXISTS)
                             .add_role_edge(speed, ssp, 'define_SSP', PREEXISTS)
                             .add_role_edge(duct, ssp, 'find_channel', PREEXISTS)
                             .add_role_edge(duct, dct, 'channel_exists', PREEXISTS)
                               )

    # === Candidate Convergence ===
    candidate_convergence_query = inspect.cleandoc(f'''match
           $scn isa sound-propagation-scenario, has scenario_id {scenario_idx};
           $ray isa ray-input, has num_rays $nray;
           $conv(candidate_scenario: $scn, candidate_resolution: $ray) isa candidate-convergence; 
           get;''')

    candidate_convergence_query_graph = (QueryGraph().add_vars(
        [conv], CANDIDATE).add_vars([scn, ray, nray], PREEXISTS).add_has_edge(
            ray, nray,
            PREEXISTS).add_role_edge(conv, scn, 'candidate_scenario',
                                     CANDIDATE).add_role_edge(
                                         conv, ray, 'candidate_resolution',
                                         CANDIDATE))

    return [(convergence_query, lambda x: x, convergence_query_graph),
            (candidate_convergence_query, lambda x: x,
             candidate_convergence_query_graph)]
Exemplo n.º 4
0
 def test_add_vars_adds_variable_nodes_as_expected(self):
     g = QueryGraph()
     g.add_vars(['a', 'b'], 0)
     nodes = {node for node in g.nodes}
     self.assertSetEqual({'a', 'b'}, nodes)
Exemplo n.º 5
0
 def test_add_single_var_adds_variable_node_as_expected(self):
     g = QueryGraph()
     g.add_vars(['a'], 0)
     self.assertDictEqual({'solution': 0}, g.nodes['a'])
Exemplo n.º 6
0
def get_query_handles(example_id):
    """
    Creates an iterable, each element containing a Graql query, a function to sample the answers, and a QueryGraph
    object which must be the TypeDB graph representation of the query. This tuple is termed a "query_handle"

    Args:
        example_id: A uniquely identifiable attribute value used to anchor the results of the queries to a specific
                    subgraph

    Returns:
        query handles
    """

    # === Hereditary Feature ===
    hereditary_query = inspect.cleandoc(f'''match
           $p isa person, has example-id {example_id};
           $par isa person;
           $ps(child: $p, parent: $par) isa parentship;
           $diag(patient:$par, diagnosed-disease: $d) isa diagnosis;
           $d isa disease, has name $n;
          ''')

    vars = p, par, ps, d, diag, n = 'p', 'par', 'ps', 'd', 'diag', 'n'
    hereditary_query_graph = (QueryGraph().add_vars(
        vars,
        PREEXISTS).add_role_edge(ps, p, 'child', PREEXISTS).add_role_edge(
            ps, par, 'parent', PREEXISTS).add_role_edge(
                diag, par, 'patient', PREEXISTS).add_role_edge(
                    diag, d, 'diagnosed-disease',
                    PREEXISTS).add_has_edge(d, n, PREEXISTS))

    # === Consumption Feature ===
    consumption_query = inspect.cleandoc(f'''match
           $p isa person, has example-id {example_id};
           $s isa substance, has name $n;
           $c(consumer: $p, consumed-substance: $s) isa consumption, 
           has units-per-week $u;''')

    vars = p, s, n, c, u = 'p', 's', 'n', 'c', 'u'
    consumption_query_graph = (QueryGraph().add_vars(
        vars, PREEXISTS).add_has_edge(s, n, PREEXISTS).add_role_edge(
            c, p, 'consumer',
            PREEXISTS).add_role_edge(c, s, 'consumed-substance',
                                     PREEXISTS).add_has_edge(c, u, PREEXISTS))

    # === Age Feature ===
    person_age_query = inspect.cleandoc(f'''match 
            $p isa person, has example-id {example_id}, has age $a; 
           ''')

    vars = p, a = 'p', 'a'
    person_age_query_graph = (QueryGraph().add_vars(vars,
                                                    PREEXISTS).add_has_edge(
                                                        p, a, PREEXISTS))

    # === Risk Factors Feature ===
    risk_factor_query = inspect.cleandoc(f'''match 
            $d isa disease; 
            $p isa person, has example-id {example_id}; 
            $r(person-at-risk: $p, risked-disease: $d) isa risk-factor; 
           ''')

    vars = p, d, r = 'p', 'd', 'r'
    risk_factor_query_graph = (QueryGraph().add_vars(
        vars, PREEXISTS).add_role_edge(r, p, 'person-at-risk',
                                       PREEXISTS).add_role_edge(
                                           r, d, 'risked-disease', PREEXISTS))

    # === Symptom ===
    vars = p, s, sn, d, dn, sp, sev, c = 'p', 's', 'sn', 'd', 'dn', 'sp', 'sev', 'c'

    symptom_query = inspect.cleandoc(f'''match
           $p isa person, has example-id {example_id};
           $s isa symptom, has name $sn;
           $d isa disease, has name $dn;
           $sp(presented-symptom: $s, symptomatic-patient: $p) isa symptom-presentation, has severity $sev;
           $c(cause: $d, effect: $s) isa causality;
          ''')

    symptom_query_graph = (QueryGraph().add_vars(vars, PREEXISTS).add_has_edge(
        s, sn, PREEXISTS).add_has_edge(d, dn, PREEXISTS).add_role_edge(
            sp, s, 'presented-symptom',
            PREEXISTS).add_has_edge(sp, sev, PREEXISTS).add_role_edge(
                sp, p, 'symptomatic-patient', PREEXISTS).add_role_edge(
                    c, s, 'effect',
                    PREEXISTS).add_role_edge(c, d, 'cause', PREEXISTS))

    # === Diagnosis ===

    diag, d, p, dn = 'diag', 'd', 'p', 'dn'

    diagnosis_query = inspect.cleandoc(f'''match
           $p isa person, has example-id {example_id};
           $d isa disease, has name $dn;
           $diag(patient: $p, diagnosed-disease: $d) isa diagnosis;
          ''')

    diagnosis_query_graph = (QueryGraph().add_vars([diag], TO_INFER).add_vars(
        [d, p, dn], PREEXISTS).add_role_edge(diag, d, 'diagnosed-disease',
                                             TO_INFER).add_role_edge(
                                                 diag, p, 'patient', TO_INFER))

    # === Candidate Diagnosis ===
    candidate_diagnosis_query = inspect.cleandoc(f'''match
           $p isa person, has example-id {example_id};
           $d isa disease, has name $dn;
           $diag(candidate-patient: $p, candidate-diagnosed-disease: $d) isa candidate-diagnosis; 
          ''')

    candidate_diagnosis_query_graph = (QueryGraph().add_vars(
        [diag], CANDIDATE).add_vars([d, p, dn], PREEXISTS).add_role_edge(
            diag, d, 'candidate-diagnosed-disease',
            CANDIDATE).add_role_edge(diag, p, 'candidate-patient', CANDIDATE))

    return [(symptom_query, lambda x: x, symptom_query_graph),
            (diagnosis_query, lambda x: x, diagnosis_query_graph),
            (candidate_diagnosis_query, lambda x: x,
             candidate_diagnosis_query_graph),
            (risk_factor_query, lambda x: x, risk_factor_query_graph),
            (person_age_query, lambda x: x, person_age_query_graph),
            (consumption_query, lambda x: x, consumption_query_graph),
            (hereditary_query, lambda x: x, hereditary_query_graph)]
Exemplo n.º 7
0
    def base_query_graph(self):
        p, s, sn, d, dn, sp, c = 'p', 's', 'sn', 'd', 'dn', 'sp', 'c'
        g = QueryGraph()
        g.add_vars(p, s, sn, d, dn, sp, c, **PREEXISTS)
        g.add_has_edge(s, sn, **PREEXISTS)
        g.add_has_edge(d, dn, **PREEXISTS)
        g.add_role_edge(sp, s, 'presented-symptom', **PREEXISTS)
        g.add_role_edge(sp, p, 'symptomatic-patient', **PREEXISTS)
        g.add_role_edge(c, s, 'effect', **PREEXISTS)
        g.add_role_edge(c, d, 'cause', **PREEXISTS)

        return g