Exemple #1
0
def test_entity_mapper(get_test_graph):
    """'Femur' keyword should resolve to the correct resource"""
    g = get_test_graph

    matched_resource_1 = entity_mapper.match_entity("Femur", g)
    matched_resource_2 = entity_mapper.match_entity("Long bone", g)

    assert matched_resource_1
    assert matched_resource_2
Exemple #2
0
def test_r_predecessors(get_test_graph):
    """Should return correct r_predecessors"""
    g = get_test_graph

    femur = entity_mapper.match_entity("Femur", g)
    lower_limb = entity_mapper.match_entity("Lower limb", g)
    edges = [("Femur", "Lower limb", {'relation': 'partOf'})]
    edge_type = "r-predecessor"

    r_predecessors = create_edges.get_r_predecessors(femur, g)

    assert lower_limb in r_predecessors["uris"]
    assert "Lower limb" in r_predecessors["short_names"]
    assert utils.same_edge_lists(edges, r_predecessors["edges"])
    assert edge_type == r_predecessors["edge_type"]
def main():
    """
    CLI to extract ontology modules

    """
    parser = argparse.ArgumentParser()
    parser.add_argument('-i',
                        '--input-ontology',
                        default=config_test.config["msh_test_onto"])
    parser.add_argument('-s', '--signature')
    parser.add_argument('-f', '--format-name', default=None)
    parser.add_argument('-o', '--output-file', default="ontology/output.owl")
    parser.add_argument('-d', '--max-depth', default=10)
    parser.add_argument('-l', '--locality', default='top')

    args = parser.parse_args()

    g = Graph().parse(args.input_ontology, format=args.format_name)
    resource = entity_mapper.match_entity(args.signature, g)
    ontomodule = extract_module.extract_module([resource],
                                               g,
                                               locality=args.locality,
                                               max_depth=args.max_depth)

    with open(args.output_file, "w") as f:
        ontomodule.serialize(f)
Exemple #4
0
def test_entity_mapper_fma(get_fma_graph):
    """Compute qname is known to break on FMA ontology"""
    g = get_fma_graph

    matched_resource_1 = entity_mapper.match_entity("Femur", g)
    print(matched_resource_1)

    assert matched_resource_1
def test_graph_bottom_locality(get_test_graph):
    """Should contain Lower limb when extracting Femur in the graph"""
    g = get_test_graph

    femur = entity_mapper.match_entity("Femur", g)
    locality = "bottom"

    nx_graph = create_graph.extract_subgraph([femur], g, locality=locality)

    assert nx_graph.has_edge('Femur', 'Lower limb')
Exemple #6
0
def test_r_successors(get_test_graph):
    """Should return correct r_successors"""
    g = get_test_graph

    joint_stiffness = entity_mapper.match_entity("Joint stiffness", g)
    alteration_in_gait_pattern = entity_mapper.match_entity(
        "Alteration in gait pattern", g)
    expected_short_names = [
        "Alteration in gait pattern",
        "Cartilage thinning",
        "Cartilage fissure",
        "Bone erosion"]

    edge_type = "r-successor"

    r_successors = create_edges.get_r_successors(joint_stiffness, g)

    assert alteration_in_gait_pattern in r_successors["uris"]
    assert set(expected_short_names) == set(r_successors["short_names"])
    assert edge_type == r_successors["edge_type"]
def test_graph_iterative_bfs_bottom_locality(get_test_graph):
    """Should return full subgraph, extracted iteratively"""
    g = get_test_graph

    femur = entity_mapper.match_entity("Femur", g)
    locality = "bottom"

    nx_graph = create_graph.extract_subgraph([femur], g, locality=locality)

    print(nx_graph.nodes())

    assert nx.algorithms.shortest_paths.generic.has_path(
        nx_graph, 'Femur', 'Anatomical entity')
Exemple #8
0
def test_direct_subclasses(get_test_graph):
    """Should return correct direct subclasses"""
    g = get_test_graph

    femur = entity_mapper.match_entity("Femur", g)
    long_bone = entity_mapper.match_entity("Long bone", g)

    # axioms for OWL
    triples = [(femur, RDFS.subClassOf, long_bone),
               (femur, RDF.type, OWL.Class)]

    # edges for Graph
    edges = [("Long bone", "Femur",  {'relation': 'subClassOf'})]
    edge_type = "direct subclass"

    subclasses = create_edges.get_direct_subclasses(long_bone, g)

    assert femur in subclasses["uris"]
    assert set(triples).issubset(subclasses["triples"])
    assert "Femur" in subclasses["short_names"]
    assert utils.same_edge_lists(edges, subclasses["edges"])
    assert edge_type == subclasses["edge_type"]
def test_extraction_r_successor_triples(get_test_graph):
    """Check if all triples are extracted"""
    g = get_test_graph
    locality = "top"

    joint_stiffness = entity_mapper.match_entity("Joint stiffness", g)
    # lower_limb = entity_mapper.match_entity("Lower limb", g)

    # ontomodule is rdflib.Graph
    ontomodule = extract_module.extract_module([joint_stiffness],
                                               g,
                                               locality=locality)

    # assert that the r-predecessor axioms are there
    restrictions = (
        restriction
        for restriction in ontomodule.subjects(RDF.type, OWL.Restriction)
        if isinstance(restriction, BNode) and (restriction, OWL.someValuesFrom,
                                               joint_stiffness) in g)

    assert len(list(restrictions)) != 0

    # Now assert all the R-predecessors as well as the object properties
    for restriction in restrictions:
        # there can only be one restriction on one property
        obj_property = next(g.objects(restriction, OWL.onProperty))

        # we assume only atomic concepts in the filler of the restriction
        r_successor = next(g.subbjects(RDFS.subClassOf, restriction))

        # add r-predecessor axiom
        triples = [(r_successor, RDFS.subClassOf, restriction),
                   (restriction, RDF.type, OWL.Restriction),
                   (restriction, OWL.onProperty, obj_property),
                   (restriction, OWL.someValuesFrom, joint_stiffness)]

        for triple in triples:
            assert triple in ontomodule
def test_compute_shortname():
    con_matched = entity_mapper.match_entity('continuant', g)
    assert entity_mapper.compute_short_name(con_matched, g) == 'Continuant'

    occ_matched = entity_mapper.match_entity('Occurrent', g)
    assert entity_mapper.compute_short_name(occ_matched, g) == 'Occurent'
def test_simple_entity():
    con_matched = entity_mapper.match_entity('Continuant', g)
    assert con_matched == con.identifier

    occ_matched = entity_mapper.match_entity('Occurrent', g)
    assert occ_matched == occ.identifier