Exemplo n.º 1
0
 def test_from_dict_error(self):
     d = dict(
         literal="pneumonia",
         category="CONDITION",
         invalid="this is an invalid key",
     )
     with pytest.raises(ValueError):
         TargetRule.from_dict(d)
Exemplo n.º 2
0
def load():
    import medspacy
    nlp = medspacy.load(enable=["sentencizer", "tokenizer"])

    # Add components
    from medspacy.target_matcher import TargetMatcher, TargetRule
    target_matcher = TargetMatcher(nlp)
    target_filepath = path.join(RESOURCES_DIR, "target_rules.json")
    target_rules = TargetRule.from_json(target_filepath)
    target_matcher.add(target_rules)
    nlp.add_pipe(target_matcher)

    from medspacy.context import ConTextComponent, ConTextRule
    context_filepath = path.join(RESOURCES_DIR, "context_rules.json")
    context = ConTextComponent(nlp, rules=None, add_attrs=CONTEXT_ATTRS)
    context_rules = ConTextRule.from_json(context_filepath)
    context.add(context_rules)
    nlp.add_pipe(context)

    from medspacy.section_detection import Sectionizer
    # TODO: Add radiology section rules
    sectionizer = Sectionizer(nlp)
    nlp.add_pipe(sectionizer)

    clf = DocumentClassifier(nlp)
    nlp.add_pipe(clf)

    return nlp
Exemplo n.º 3
0
    def test_to_json(self):
        import json, os

        dname = os.path.join(tmpdirname.name, "test_target_rules.json")

        literal = "pneumonia"
        category = "CONDITION"
        item = TargetRule(literal, category)
        TargetRule.to_json([item], dname)

        with open(dname) as f:
            data = json.load(f)
        assert "target_rules" in data
        assert len(data["target_rules"]) == 1
        rule_dict = data["target_rules"][0]
        for key in ["literal", "category"]:
            assert key in rule_dict
Exemplo n.º 4
0
 def test_add_rule_pattern(self):
     matcher = TargetMatcher(nlp)
     matcher.add([TargetRule("PE", "CONDITION", pattern=[{"LOWER": "pe"}])])
     doc = nlp("Past Medical History: Pe")
     matcher(doc)
     assert len(doc.ents) == 1
     assert (doc.ents[0].start, doc.ents[0].end) == (4, 5)
     assert doc.ents[0].label_ == "CONDITION"
Exemplo n.º 5
0
 def test_basic_rule(self):
     matcher = TargetMatcher(nlp)
     matcher.add([TargetRule("PE", "CONDITION")])
     doc = nlp("Past Medical History: PE")
     matcher(doc)
     assert len(doc.ents) == 1
     assert (doc.ents[0].start, doc.ents[0].end) == (4, 5)
     assert doc.ents[0].label_ == "CONDITION"
Exemplo n.º 6
0
 def test_add_rule_regex(self):
     matcher = TargetMatcher(nlp)
     matcher.add(
         [TargetRule("PE", "CONDITION", pattern="pulmonary embolisms?")])
     doc = nlp("Past Medical History: Pulmonary embolism")
     matcher(doc)
     assert len(doc.ents) == 1
     assert (doc.ents[0].start, doc.ents[0].end) == (4, 6)
     assert doc.ents[0].label_ == "CONDITION"
Exemplo n.º 7
0
    def test_add_ents_false(self):
        matcher = TargetMatcher(nlp, add_ents=False)
        matcher.add([TargetRule("PE", "CONDITION")])
        doc = nlp("Past Medical History: PE")
        spans = matcher(doc)

        assert len(doc.ents) == 0

        assert len(spans) == 1
        assert isinstance(spans[0], spacy.tokens.Span)
Exemplo n.º 8
0
import pytest
import os
import tempfile

from medspacy.io.db_connect import DbConnect
import sqlite3

import medspacy
from medspacy.target_matcher import TargetRule
from medspacy.io import DocConsumer

tmpdirname = tempfile.TemporaryDirectory()
db = os.path.join(tmpdirname.name, "test")

nlp = medspacy.load(enable=["sentencizer", "target_matcher", "context", "sectionizer"])
nlp.get_pipe("target_matcher").add(TargetRule("pneumonia", "CONDITION"))
doc = nlp("There is no evidence of pneumonia.")

doc_consumer = DocConsumer(nlp)
doc_consumer(doc)



class TestDbWriter:

    def test_init_from_sqlite3_conn_defaults(self):
        """Test writing with default values for ent attributes."""
        sq_conn = sqlite3.connect(db)
        cursor = sq_conn.cursor()
        db_conn = DbConnect(conn=sq_conn)
        from medspacy.io.db_writer import DbWriter
Exemplo n.º 9
0
import tempfile

from medspacy.io.db_connect import DbConnect
import sqlite3

import medspacy
from medspacy.target_matcher import TargetRule
from medspacy.io import DocConsumer

tmpdirname = tempfile.TemporaryDirectory()
db = os.path.join(tmpdirname.name, "test")

nlp = medspacy.load(
    enable=["pyrush", "target_matcher", "context", "sectionizer"])
nlp.get_pipe("medspacy_target_matcher").add(
    TargetRule("pneumonia", "CONDITION"))
doc = nlp("There is no evidence of pneumonia.")

doc_consumer = DocConsumer(nlp)
doc_consumer(doc)


class TestDbWriter:
    def test_init_from_sqlite3_conn_defaults(self):
        """Test writing with default values for ent attributes."""
        sq_conn = sqlite3.connect(db)
        cursor = sq_conn.cursor()
        db_conn = DbConnect(conn=sq_conn)
        from medspacy.io.db_writer import DbWriter

        writer = DbWriter(db_conn,
Exemplo n.º 10
0
 def test_from_json(self, from_json_file):
     assert TargetRule.from_json(from_json_file)
Exemplo n.º 11
0
 def test_from_dict(self):
     d = dict(literal="pneumonia", category="CONDITION")
     assert TargetRule.from_dict(d)
Exemplo n.º 12
0
 def test_inherits_from_base_rule(self):
     rule = TargetRule("pneumonia", "CONDITION")
     assert isinstance(rule, BaseRule)
Exemplo n.º 13
0
 def test_initiate(self):
     assert TargetRule("pneumonia", "CONDITION")
Exemplo n.º 14
0
import pytest
import os
import tempfile

import medspacy
from medspacy.target_matcher import TargetRule
from medspacy.io import DocConsumer

tmpdirname = tempfile.TemporaryDirectory()
db = os.path.join(tmpdirname.name, "test.db")

# Set up a simple pipeline which will allow us to write results
nlp = medspacy.load(
    enable=["pyrush", "target_matcher", "context", "sectionizer"])
nlp.get_pipe("medspacy_target_matcher").add([
    TargetRule("pneumonia", "CONDITION"),
    TargetRule("breast ca", "CONDITION")
])
doc = nlp("There is no evidence of pneumonia.")

doc_consumer = DocConsumer(
    nlp,
    dtype_attrs={"ent": ["text", "label_", "is_negated", "section_category"]})
nlp.add_pipe("medspacy_doc_consumer",
             config={
                 "dtype_attrs": {
                     "ent":
                     ["text", "label_", "is_negated", "section_category"]
                 }
             })
Exemplo n.º 15
0
 def test_add(self):
     matcher = TargetMatcher(nlp)
     matcher.add([TargetRule("my direction", "RULE")])
     assert matcher._rules