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)
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)
def test_from_dict_error(self): d = dict( literal="reason for examination", category="INDICATION", rule="FORWARD", invalid="this is an invalid key", ) with pytest.raises(ValueError): ConTextItem.from_dict(d)
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)
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)
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
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
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
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
def context_graph(self): doc = nlp("There is no evidence of pneumonia but there is chf.") 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
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)
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"
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"
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"}
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
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()
def test_to_json(self): import json, os dname = os.path.join(tmpdirname.name, "test_modifiers.json") literal = "no evidence of" category = "definite_negated_existence" rule = "forward" item = ConTextItem(literal, category, rule) ConTextItem.to_json([item], dname) with open(dname) as f: data = json.load(f) assert "item_data" in data assert len(data["item_data"]) == 1 item = data["item_data"][0] for key in ["literal", "category", "rule"]: assert key in item
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"
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
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
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
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]
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
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
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")
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"}
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)
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
def test_set_scope_fails_no_sentences(self): """Test that setting the scope fails if sentence boundaries haven't been set.""" nlp = spacy.blank("en") assert nlp.pipeline == [] doc = nlp("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. " "Add an upstream component such as the dependency parser, Sentencizer, or PyRuSH to detect sentence boundaries." )
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