Ejemplo n.º 1
0
def build_hierarchy():
    r"""Return hierarchy with this structure:

           a
          / \
         b   c
        / \ / \
       d   e   f
      / \ / \ / \
     g   h   i   j
    """

    edges = [
        ("a", "b"),
        ("a", "c"),
        ("b", "d"),
        ("b", "e"),
        ("c", "e"),
        ("c", "f"),
        ("d", "g"),
        ("d", "h"),
        ("e", "h"),
        ("e", "i"),
        ("f", "i"),
        ("f", "j"),
    ]

    return Hierarchy("a", edges)
Ejemplo n.º 2
0
def test_roundtrip_examples(subtests):
    # tree has this structure:
    #
    #      ┌--0--┐
    #      |     |
    #   ┌--1--┌--2--┐
    #   |     |     |
    # ┌-3-┐ ┌-4-┐ ┌-5-┐
    # |   | |   | |   |
    # 6   7 8   9 10 11

    edges = [
        ("0", "1"),
        ("0", "2"),
        ("1", "3"),
        ("1", "4"),
        ("2", "4"),
        ("2", "5"),
        ("3", "6"),
        ("3", "7"),
        ("4", "8"),
        ("4", "9"),
        ("5", "10"),
        ("5", "11"),
    ]
    hierarchy = Hierarchy("0", edges)

    for codes, query in [
            # the root element
        ({"0"}, ["0"]),
            # an intermediate element
        ({"2"}, ["2"]),
            # a leaf element
        ({"8"}, ["8"]),
            # everything under intermediate element, inclusive
        ({"2", "4", "5", "8", "9", "10", "11"}, ["2<"]),
            # everything under intermediate element, exclusive
        ({"4", "5", "8", "9", "10", "11"}, ["4<", "5<"]),
            # everything under intermediate element, except another intermediate element
        ({"2", "5", "8", "9", "10", "11"}, ["2<", "~4"]),
            # everything under intermediate element, except leaf element
        ({"2", "4", "5", "9", "10", "11"}, ["2<", "~8"]),
            # everything under root element, except intermediate element and its children (i)
        ({"0", "1", "2", "3", "4", "6", "7", "8", "9"}, ["0", "1<", "2"]),
            # everything under root element, except intermediate element and its children (ii)
        ({"0", "1", "2", "3", "5", "6", "7", "10",
          "11"}, ["0", "1", "2", "3<", "5<"]),
    ]:
        with subtests.test(codes=codes, query=query):
            defn1 = Definition.from_codes(codes, hierarchy)
            assert sorted(str(r) for r in defn1.rules) == sorted(query)
            assert defn1.codes(hierarchy) == codes

            defn2 = Definition.from_query(query)
            assert sorted(str(r) for r in defn2.rules) == sorted(query)
            assert defn2.codes(hierarchy) == codes
Ejemplo n.º 3
0
def hierarchies(draw, size):
    """Build a Hierarchy with `size` nodes.

    Based on an indea by @Zac-HD at https://github.com/HypothesisWorks/hypothesis/issues/2464.
    """

    edges = []
    for child_id in range(1, size):
        for parent_id in draw(
                st.sets(st.sampled_from(range(child_id)), min_size=1)):
            edges.append((parent_id, child_id))
    return Hierarchy("0", edges)
Ejemplo n.º 4
0
def update_code_statuses(*, codelist, updates):
    code_to_status = dict(codelist.codes.values_list("code", "status"))
    h = Hierarchy.from_codes(codelist.coding_system, list(code_to_status))
    new_code_to_status = h.update_node_to_status(code_to_status, updates)

    status_to_new_code = defaultdict(list)
    for code, status in new_code_to_status.items():
        status_to_new_code[status].append(code)

    for status, codes in status_to_new_code.items():
        codelist.codes.filter(code__in=codes).update(status=status)

    logger.info("Updated Codelist Statuses", codelist_pk=codelist.pk)
Ejemplo n.º 5
0
def test_build_definition_rows():
    # hierarchy has this structure:
    #
    #      ┌--0--┐
    #      |     |
    #   ┌--1--┌--2--┐
    #   |     |     |
    # ┌-3-┐ ┌-4-┐ ┌-5-┐
    # |   | |   | |   |
    # 6   7 8   9 10 11

    edges = [
        ("0", "1"),
        ("0", "2"),
        ("1", "3"),
        ("1", "4"),
        ("2", "4"),
        ("2", "5"),
        ("3", "6"),
        ("3", "7"),
        ("4", "8"),
        ("4", "9"),
        ("5", "10"),
        ("5", "11"),
    ]
    hierarchy = Hierarchy("0", edges)

    # construct a hierarchy with 2, and all its descendants except 8
    definition = Definition.from_codes({"2", "4", "5", "9", "10", "11"}, hierarchy)

    # make a dummy coding system so we can do name lookup
    dummy_coding_system = DummyCodingSystem()

    rows = presenters.build_definition_rows(dummy_coding_system, hierarchy, definition)

    # we expect only one row (with an exclusion for 8)
    assert len(rows) == 1

    row = rows[0]
    assert row["code"] == "2"
    assert row["all_descendants"]

    # the only excluded code should be 8
    assert len(row["excluded_descendants"]) == 1
    excluded = row["excluded_descendants"][0]
    assert excluded["code"] == "8"
Ejemplo n.º 6
0
def build_small_hierarchy():
    r"""Return hierarchy with this structure:

        a
       / \
      b   c
     / \ / \
    d   e   f
    """

    edges = [
        ("a", "b"),
        ("a", "c"),
        ("b", "d"),
        ("b", "e"),
        ("c", "e"),
        ("c", "f"),
    ]

    return Hierarchy("a", edges)
Ejemplo n.º 7
0
def test_tree_tables(tennis_elbow_codelist):
    cl = tennis_elbow_codelist
    clv = cl.versions.get()

    hierarchy = Hierarchy.from_codes(cl.coding_system, clv.codes)
    ancestor_codes = hierarchy.filter_to_ultimate_ancestors(set(clv.codes))
    codes_by_type = snomed.codes_by_type(ancestor_codes, hierarchy)
    code_to_term = snomed.code_to_term(hierarchy.nodes)

    # 128133004 (Disorder of elbow)
    #   ├  429554009 (Arthropathy of elbow)
    #   │     └  439656005 (Arthritis of elbow)
    #   │           └  202855006 (Lateral epicondylitis)
    #   ├  35185008 (Enthesopathy of elbow region)
    #   │     └  73583000 (Epicondylitis)
    #   │           └  202855006 (Lateral epicondylitis)
    #   └  239964003 (Soft tissue lesion of elbow region)

    assert presenters.tree_tables(codes_by_type, hierarchy, code_to_term) == [
        {
            "heading": "Disorder",
            "rows": [
                {
                    "code": "128133004",
                    "term": "Disorder of elbow",
                    "pipes": [],
                },
                {
                    "code": "429554009",
                    "term": "Arthropathy of elbow",
                    "pipes": ["├"],
                },
                {
                    "code": "439656005",
                    "term": "Arthritis of elbow",
                    "pipes": ["│", "└"],
                },
                {
                    "code": "202855006",
                    "term": "Lateral epicondylitis",
                    "pipes": ["│", " ", "└"],
                },
                {
                    "code": "35185008",
                    "term": "Enthesopathy of elbow region",
                    "pipes": ["├"],
                },
                {
                    "code": "73583000",
                    "term": "Epicondylitis",
                    "pipes": ["│", "└"],
                },
                {
                    "code": "202855006",
                    "term": "Lateral epicondylitis",
                    "pipes": ["│", " ", "└"],
                },
                {
                    "code": "239964003",
                    "term": "Soft tissue lesion of elbow region",
                    "pipes": ["└"],
                },
            ],
        }
    ]