100 lines
4.4 KiB
Python
100 lines
4.4 KiB
Python
from datetime import date
|
|
|
|
import pytest
|
|
|
|
from extraction.testing import scanned_pdf, text_pdf
|
|
from extraction import DocumentExtractor, ExtractionError, FieldKind, FieldSpec, make_segmenter, read_document
|
|
from extraction.readers import extract_pages, reader_for
|
|
|
|
EXPEDIENTE = FieldSpec("pac_id_hospital", FieldKind.IDENTIFIER, labels=("expediente",))
|
|
NACIMIENTO = FieldSpec("pac_fecha_nacimiento", FieldKind.DATE, labels=("fecha de nacimiento",))
|
|
VPH = FieldSpec(
|
|
"cit_vph_resultado",
|
|
FieldKind.CHOICE,
|
|
labels=("vph alto riesgo", "resultado vph"),
|
|
choices={"POSITIVO": "POSITIVO", "NEGATIVO": "NEGATIVO"},
|
|
scan_free_text=True,
|
|
)
|
|
SPECS = [EXPEDIENTE, NACIMIENTO, VPH]
|
|
|
|
|
|
def test_text_layer_pages_are_read_without_ocr():
|
|
document = read_document("r.pdf", text_pdf([["Expediente: EXP-0001", "VPH alto riesgo: POSITIVO"]]))
|
|
assert document.pages[0].source == "text"
|
|
assert "EXP-0001" in document.pages[0].text
|
|
|
|
|
|
def test_scanned_pages_are_ocred():
|
|
document = read_document("s.pdf", scanned_pdf([["Expediente: EXP-0002", "Resultado VPH: NEGATIVO"]]))
|
|
page = document.pages[0]
|
|
assert page.source == "ocr"
|
|
assert page.images and page.images[0].kind == "scan"
|
|
|
|
|
|
def test_embedded_figures_are_located():
|
|
document = read_document("f.pdf", text_pdf([["Expediente: EXP-0003"], ["Imagen colposcópica"]], image_on_page=2))
|
|
assert document.pages[0].images == []
|
|
[figure] = document.pages[1].images
|
|
assert figure.kind == "figure"
|
|
assert 0.2 < figure.bbox.x0 < 0.6
|
|
|
|
|
|
def test_one_document_with_several_patients_and_continuation_pages():
|
|
raw = text_pdf(
|
|
[
|
|
["Expediente: EXP-0010", "Fecha de nacimiento: 01/02/1980"],
|
|
["Hoja 2", "VPH alto riesgo: POSITIVO"],
|
|
["Expediente: EXP-0011", "Fecha de nacimiento: 03/04/1975", "VPH alto riesgo: NEGATIVO"],
|
|
]
|
|
)
|
|
records = DocumentExtractor(SPECS, key_field="pac_id_hospital").extract([read_document("lote.pdf", raw)])
|
|
assert [r.key for r in records] == ["EXP-0010", "EXP-0011"]
|
|
first, second = records
|
|
assert first.sources[0].page_numbers == [1, 2]
|
|
assert first.row() == {"pac_id_hospital": "EXP-0010", "pac_fecha_nacimiento": date(1980, 2, 1), "cit_vph_resultado": "POSITIVO"}
|
|
assert second.row()["cit_vph_resultado"] == "NEGATIVO"
|
|
|
|
|
|
def test_patient_scattered_across_documents_is_merged():
|
|
demographics = read_document("a.pdf", text_pdf([["Expediente: EXP-0020", "Fecha de nacimiento: 05/06/1970"]]))
|
|
lab = read_document("b.pdf", scanned_pdf([["Expediente: EXP-0020", "Resultado VPH: POSITIVO"]]))
|
|
[record] = DocumentExtractor(SPECS, key_field="pac_id_hospital").extract([demographics, lab])
|
|
assert record.row()["pac_fecha_nacimiento"] == date(1970, 6, 5)
|
|
assert record.row()["cit_vph_resultado"] == "POSITIVO"
|
|
assert {source.document for source in record.sources} == {"a.pdf", "b.pdf"}
|
|
|
|
|
|
def test_confident_disagreement_is_reported_as_conflict():
|
|
one = read_document("a.pdf", text_pdf([["Expediente: EXP-0030", "VPH alto riesgo: POSITIVO"]]))
|
|
two = read_document("b.pdf", text_pdf([["Expediente: EXP-0030", "VPH alto riesgo: NEGATIVO"]]))
|
|
[record] = DocumentExtractor(SPECS, key_field="pac_id_hospital").extract([one, two])
|
|
assert [conflict.field for conflict in record.conflicts] == ["cit_vph_resultado"]
|
|
|
|
|
|
def test_fixed_page_segmentation():
|
|
raw = text_pdf([["Expediente: EXP-1"], ["x"], ["Expediente: EXP-2"], ["y"]])
|
|
extractor = DocumentExtractor(SPECS, key_field="pac_id_hospital", segmenter=make_segmenter("pages", pages_per_record=2), merger=None)
|
|
assert [r.sources[0].page_numbers for r in extractor.extract([read_document("d.pdf", raw)])] == [[1, 2], [3, 4]]
|
|
|
|
|
|
def test_extract_pages_builds_a_smaller_pdf():
|
|
raw = text_pdf([["Expediente: EXP-A"], ["Expediente: EXP-B"], ["Expediente: EXP-C"]])
|
|
subset = read_document("sub.pdf", extract_pages(raw, [2, 3]))
|
|
assert subset.page_count == 2
|
|
assert "EXP-B" in subset.pages[0].text
|
|
|
|
|
|
def test_unknown_format_is_rejected():
|
|
with pytest.raises(ExtractionError):
|
|
reader_for("notes.docx", b"PK\x03\x04")
|
|
|
|
|
|
def test_combine_pages_from_several_pdfs():
|
|
from extraction.readers import combine_pages
|
|
|
|
one = text_pdf([["Expediente: EXP-A"], ["Expediente: EXP-B"]])
|
|
two = scanned_pdf([["Expediente: EXP-B", "Resultado VPH: NEGATIVO"]])
|
|
combined = read_document("c.pdf", combine_pages([(one, [2]), (two, [1])]))
|
|
assert combined.page_count == 2
|
|
assert "EXP-B" in combined.pages[0].text and "NEGATIVO" in combined.pages[1].text
|