Exemplo n.º 1
0
 def test_limit_scope3(self):
     """Test that two modifiers of the same type limit the scope of the first modifier."""
     doc = nlp("no evidence of CHF, neg for pneumonia")
     item = ConTextItem("no evidence of", "DEFINITE_NEGATED_EXISTENCE",
                        "FORWARD")
     item2 = ConTextItem("neg for", "DEFINITE_NEGATED_EXISTENCE", "FORWARD")
     tag_object = TagObject(item, 0, 3, doc)
     tag_object2 = TagObject(item2, 5, 7, doc)
     assert tag_object.limit_scope(tag_object2)
Exemplo n.º 2
0
    def test_terminate_limit_scope_backward(self):
        """Test that a 'TERMINATE' modifier will limit the scope of a 'BACKWARD' modifier.
        """
        doc = nlp("Pt has chf but pneumonia is ruled out")
        item = ConTextItem("is ruled out", "NEGATED_EXISTENCE", "BACKWARD")
        tag_object = TagObject(item, 6, 8, doc)

        item2 = ConTextItem("but", "TERMINATE", "TERMINATE")
        tag_object2 = TagObject(item2, 3, 4, doc)
        assert tag_object.limit_scope(tag_object2)
Exemplo n.º 3
0
    def test_terminate_stops_forward_modifier(self):
        context = ConTextComponent(nlp, rules=None)

        item = ConTextItem("no evidence of", "NEGATED_EXISTENCE", "FORWARD")
        item2 = ConTextItem("but", "TERMINATE", "TERMINATE")
        context.add([item, item2])
        doc = nlp("No evidence of chf but she has pneumonia.")
        doc.ents = (Span(doc, 3, 4, "PROBLEM"), Span(doc, 7, 8, "PROBLEM"))
        context(doc)
        chf, pneumonia = doc.ents
        assert len(chf._.modifiers) > 0
        assert len(pneumonia._.modifiers) == 0
Exemplo n.º 4
0
    def test_terminate_stops_backward_modifier(self):
        context = ConTextComponent(nlp, rules=None)

        item = ConTextItem("is ruled out", "NEGATED_EXISTENCE", "BACKWARD")
        item2 = ConTextItem("but", "CONJ", "TERMINATE")
        context.add([item, item2])
        doc = nlp("Pt has chf but pneumonia is ruled out")
        doc.ents = (Span(doc, 2, 3, "PROBLEM"), Span(doc, 4, 5, "PROBLEM"))
        context(doc)
        chf, pneumonia = doc.ents
        assert len(chf._.modifiers) == 0
        assert len(pneumonia._.modifiers) > 0
Exemplo n.º 5
0
 def test_terminate_limit_scope_custom2(self):
     """Test that a modifier will be explicitly terminated by a modifier with a category
     in terminated_by."""
     doc = nlp("flu is negative, pneumonia is positive.")
     item = ConTextItem("negative", "NEGATED_EXISTENCE", rule="BACKWARD")
     item2 = ConTextItem("positive",
                         "POSITIVE_EXISTENCE",
                         rule="BACKWARD",
                         terminated_by={"NEGATED_EXISTENCE"})
     tag_object = TagObject(item, 2, 3, doc)
     tag_object2 = TagObject(item2, 6, 7, doc)
     assert tag_object2.limit_scope(tag_object)
Exemplo n.º 6
0
 def test_terminate_limit_scope_custom(self):
     """Test that a modifier will be explicitly terminated by a modifier with a category
     in terminated_by."""
     doc = nlp("negative for flu, positive for pneumonia.")
     item = ConTextItem("negative for",
                        "NEGATED_EXISTENCE",
                        rule="FORWARD",
                        terminated_by={"POSITIVE_EXISTENCE"})
     item2 = ConTextItem("positive for",
                         "POSITIVE_EXISTENCE",
                         rule="FORWARD")
     tag_object = TagObject(item, 0, 2, doc)
     tag_object2 = TagObject(item2, 4, 6, doc)
     assert tag_object.limit_scope(tag_object2)
Exemplo n.º 7
0
 def test_metadata(self):
     literal = "no evidence of"
     category = "definite_negated_existence"
     rule = "forward"
     meta = {"comment": "This is a comment."}
     item = ConTextItem(literal, category, rule, metadata=meta)
     assert item.metadata
Exemplo n.º 8
0
 def test_rule_value_error(self):
     """Test that ConTextItem raises a ValueError if an invalid rule is passed in."""
     literal = "no evidence of"
     category = "definite_negated_existence"
     rule = "asdf"
     with pytest.raises(ValueError):
         ConTextItem(literal, category, rule)
Exemplo n.º 9
0
 def test_context_item_category_upper(self):
     """Test that a ConTextItem category is always upper"""
     literal = "no evidence of"
     category = "definite_negated_existence"
     rule = "forward"
     item = ConTextItem(literal, category, rule)
     assert item.category == "DEFINITE_NEGATED_EXISTENCE"
Exemplo n.º 10
0
 def test_deprecated_context_item_throws_error(self):
     with pytest.raises(NotImplementedError) as exception_info:
         # This should fail because context_item throws a NotImplementedError
         from medspacy.context import ConTextItem
         ConTextItem()
     exception_info.match(
         "ConTextItem has been deprecated and replaced with ConTextRule.")
Exemplo n.º 11
0
 def test_context_item_rule_upper(self):
     """Test that a ConTextItem rule is always upper"""
     literal = "no evidence of"
     category = "definite_negated_existence"
     rule = "forward"
     item = ConTextItem(literal, category, rule)
     assert item.rule == "FORWARD"
Exemplo n.º 12
0
    def test_custom_terminate_stops_forward_modifier(self):
        doc = nlp("negative for flu, positive for pneumonia.")
        context = ConTextComponent(nlp, rules=None)

        item = ConTextItem("negative for",
                           "NEGATED_EXISTENCE",
                           rule="FORWARD",
                           terminated_by={"POSITIVE_EXISTENCE"})
        item2 = ConTextItem("positive for",
                            "POSITIVE_EXISTENCE",
                            rule="FORWARD")
        context.add([item, item2])
        doc.ents = (Span(doc, 2, 3, "PROBLEM"), Span(doc, 6, 7))
        flu, pneumonia = doc.ents
        context(doc)
        assert len(flu._.modifiers) == 1
        assert len(pneumonia._.modifiers) == 1
Exemplo n.º 13
0
 def test_item_modifier_termination(self):
     context = ConTextComponent(nlp, rules=None, terminations=None)
     item = ConTextItem("no evidence of",
                        "NEGATED_EXISTENCE",
                        "FORWARD",
                        terminated_by={"POSITIVE_EXISTENCE", "UNCERTAIN"})
     context.add([item])
     assert item.terminated_by == {"POSITIVE_EXISTENCE", "UNCERTAIN"}
Exemplo n.º 14
0
 def test_null_modifier_termination(self):
     context = ConTextComponent(nlp, rules=None, terminations=None)
     item = ConTextItem("no evidence of",
                        "NEGATED_EXISTENCE",
                        "FORWARD",
                        terminated_by=None)
     context.add([item])
     assert item.terminated_by == set()
Exemplo n.º 15
0
 def create_objects(self):
     doc = nlp(
         "family history of breast cancer but no diabetes. She has afib.")
     item = ConTextItem("family history of",
                        "FAMILY_HISTORY",
                        rule="FORWARD")
     tag_object = TagObject(item, 0, 3, doc)
     return doc, item, tag_object
Exemplo n.º 16
0
    def test_is_historical(self):
        doc = nlp("History of pneumonia.")
        context = ConTextComponent(nlp, add_attrs=True, rules=None)
        item_data = [ConTextItem("history of", "HISTORICAL", rule="forward")]
        context.add(item_data)
        doc.ents = (doc[-2:-1], )
        context(doc)

        assert doc.ents[0]._.is_historical is True
Exemplo n.º 17
0
    def test_pseudo_modifier(self):
        item_data = [
            ConTextItem("negative", "NEGATED_EXISTENCE"),
            ConTextItem("negative attitude",
                        "PSEUDO_NEGATED_EXISTENCE",
                        rule="PSEUDO"),
        ]
        context = ConTextComponent(nlp, rules=None)
        context.add(item_data)

        doc = nlp("She has a negative attitude about her treatment.")
        doc.ents = (doc[-2:-1], )
        context(doc)

        assert len(doc.ents[0]._.modifiers) == 0
        assert len(doc._.context_graph.modifiers) == 1
        assert doc._.context_graph.modifiers[
            0].category == "PSEUDO_NEGATED_EXISTENCE"
Exemplo n.º 18
0
 def test_set_scope_context_window_no_sentences(self):
     """Test that setting the scope succeeds if sentence boundaries haven't been set but _use_context_window is True."""
     doc = nlp.tokenizer(
         "family history of breast cancer but no diabetes. She has afib.")
     item = ConTextItem("family history of",
                        "FAMILY_HISTORY",
                        rule="FORWARD",
                        max_scope=2)
     tag_object = TagObject(item, 0, 3, doc, _use_context_window=True)
     assert tag_object.scope == doc[3:5]
Exemplo n.º 19
0
    def test_overlapping_target(self):
        """Test that a modifier will not modify a target if it is
        in the same span as the modifier.
        """
        doc = nlp("Pt presents for r/o of pneumonia.")
        item = ConTextItem("r/o", "UNCERTAIN", rule="BIDIRECTIONAL")
        tag_object = TagObject(item, 3, 4, doc)
        target = Span(doc, 3, 4, "TEST")

        assert tag_object.modifies(target) is False
Exemplo n.º 20
0
    def test_is_negated(self):
        doc = nlp("There is no evidence of pneumonia.")
        context = ConTextComponent(nlp, add_attrs=True, rules=None)
        item_data = [
            ConTextItem("no evidence of", "NEGATED_EXISTENCE", rule="forward")
        ]
        context.add(item_data)
        doc.ents = (doc[-2:-1], )
        context(doc)

        assert doc.ents[0]._.is_negated is True
Exemplo n.º 21
0
    def test_is_family(self):
        doc = nlp("Family history of breast cancer.")
        context = ConTextComponent(nlp, add_attrs=True, rules=None)
        item_data = [
            ConTextItem("family history of", "FAMILY", rule="forward")
        ]
        context.add(item_data)
        doc.ents = (doc[-3:-1], )
        context(doc)

        assert doc.ents[0]._.is_family is True
Exemplo n.º 22
0
    def context_graph(self):
        doc = nlp.tokenizer(
            "There is no evidence of pneumonia but there is chf.")
        doc[0].is_sent_start = True
        for token in doc[1:]:
            token.is_sent_start = False
        item_data1 = ConTextItem("no evidence of",
                                 "DEFINITE_NEGATED_EXISTENCE", "forward")
        tag_object1 = TagObject(item_data1, 2, 5, doc)

        item_data2 = ConTextItem("evidence of", "DEFINITE_EXISTENCE",
                                 "forward")
        tag_object2 = TagObject(item_data2, 3, 5, doc)

        item_data3 = ConTextItem("but", "TERMINATE", "TERMINATE")
        tag_object3 = TagObject(item_data3, 6, 7, doc)

        graph = ConTextGraph()
        graph.modifiers = [tag_object1, tag_object2, tag_object3]
        return doc, graph
Exemplo n.º 23
0
 def test_set_scope_fails_no_sentences(self):
     """Test that setting the scope fails if sentence boundaries haven't been set."""
     doc = nlp.tokenizer(
         "family history of breast cancer but no diabetes. She has afib.")
     item = ConTextItem("family history of",
                        "FAMILY_HISTORY",
                        rule="FORWARD")
     with pytest.raises(ValueError) as exception_info:
         # This should fail because doc.sents are None
         TagObject(item, 0, 3, doc)
     exception_info.match(
         "ConText failed because sentence boundaries have not been set")
Exemplo n.º 24
0
    def test_no_limit_scope_same_category_different_allowed_types(self):
        """Test that a two TagObjects of the same type but with different
         allowed types does not limits the scope of the tag object.
         """
        doc = nlp("no history of travel to Puerto Rico, neg for pneumonia")

        item = ConTextItem(
            "no history of",
            "DEFINITE_NEGATED_EXISTENCE",
            "FORWARD",
            allowed_types={"TRAVEL"},
        )
        item2 = ConTextItem(
            "neg for",
            "DEFINITE_NEGATED_EXISTENCE",
            "FORWARD",
            allowed_types={"CONDITION"},
        )
        tag_object = TagObject(item, 0, 3, doc)
        tag_object2 = TagObject(item2, 8, 10, doc)
        assert not tag_object.limit_scope(tag_object2)
Exemplo n.º 25
0
 def test_global_allowed_types2(self):
     """Check that if the ConTextComponent does not have allowed_types defined
     and a ConTextItem does, the ConTextItem will not receive the component's
     value.
     """
     context = ConTextComponent(nlp, rules=None, allowed_types=None)
     item = ConTextItem("no evidence of",
                        "NEGATED_EXISTENCE",
                        "FORWARD",
                        allowed_types={"PROBLEM"})
     context.add([item])
     assert item.allowed_types == {"PROBLEM"}
Exemplo n.º 26
0
    def test_on_modifies_false(self):
        def on_modifies(target, modifier, span_between):
            return False

        item = ConTextItem("no evidence of",
                           "NEGATED_EXISTENCE",
                           on_modifies=on_modifies)
        doc = nlp("There is no evidence of pneumonia or chf.")
        doc.ents = (doc[5:6], doc[7:8])
        tag = TagObject(item, 2, 5, doc)

        assert tag.modifies(doc.ents[0]) is False
Exemplo n.º 27
0
 def test_no_types(self):
     """Test that not specifying allowed_types or excluded_types will modify all targets."""
     doc = self.create_target_type_examples()
     item = ConTextItem(
         "no history of travel to",
         category="DEFINITE_NEGATED_EXISTENCE",
         rule="FORWARD",
     )
     tag_object = TagObject(item, 0, 5, doc)
     tag_object.set_scope()
     travel, condition = doc.ents  # "puerto rico", "pneumonia"
     assert tag_object.modifies(travel) is True
     assert tag_object.modifies(condition) is True
Exemplo n.º 28
0
    def test_not_remove_modifiers_overlap_target(self):
        """Test that a modifier which overlaps with a target is not pruned but does not modify itself."""
        doc = nlp("The patient has heart failure.")
        doc.ents = (Span(doc, 3, 5, "CONDITION"), )
        context_item = ConTextItem("failure", "MODIFIER")
        tag_object = TagObject(context_item, 4, 5, doc)
        graph = ConTextGraph(remove_overlapping_modifiers=False)

        graph.modifiers = [tag_object]
        graph.targets = doc.ents
        graph.prune_modifiers()
        graph.apply_modifiers()

        assert overlap_target_modifiers(tag_object.span, doc.ents[0])
        assert len(graph.modifiers) == 1
Exemplo n.º 29
0
    def test_on_modifies_false(self):
        def on_modifies(target, modifier, span_between):
            return False

        context = ConTextComponent(nlp, rules=None)
        item = ConTextItem("no evidence of",
                           "NEGATED_EXISTENCE",
                           on_modifies=on_modifies)
        context.add([item])
        doc = nlp("There is no evidence of pneumonia or chf.")
        doc.ents = (doc[5:6], doc[7:8])
        context(doc)

        for ent in doc.ents:
            assert len(ent._.modifiers) == 0
Exemplo n.º 30
0
    def test_on_modifies_arg_types(self):
        def check_arg_types(target, modifier, span_between):
            for arg in (target, modifier, span_between):
                if not isinstance(arg, spacy.tokens.Span):
                    return False
            return True

        item = ConTextItem("no evidence of",
                           "NEGATED_EXISTENCE",
                           on_modifies=check_arg_types)
        doc = nlp("There is no evidence of pneumonia or chf.")
        doc.ents = (doc[5:6], doc[7:8])
        tag = TagObject(item, 2, 5, doc)

        assert tag.modifies(doc.ents[0]) is True