-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- directly mentioned and linked values are handled, some indirectly defined values are not handled yet
- Loading branch information
1 parent
9118c37
commit 4b23b82
Showing
14 changed files
with
344 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from typing import Callable, Optional, Dict | ||
|
||
from pydicom.valuerep import VR, INT_VR, STR_VR | ||
|
||
try: | ||
import lxml.etree as ElementTree | ||
except ImportError: | ||
import xml.etree.ElementTree as ElementTree | ||
|
||
from dicom_validator.spec_reader.condition import ValuesType | ||
|
||
|
||
class EnumParser: | ||
"""Parses enumerated values for a tag.""" | ||
|
||
docbook_ns = "{http://docbook.org/ns/docbook}" | ||
|
||
def __init__( | ||
self, find_section: Callable[[str], Optional[ElementTree.Element]] | ||
) -> None: | ||
self._find_section = find_section | ||
self._enum_cache: Dict[str, ValuesType] = {} | ||
|
||
def parse(self, node: ElementTree.Element, vr: VR) -> ValuesType: | ||
"""Searches for enumerated values in the tag description and in linked sections. | ||
Returns a list of the allowed values, or an empty list if none found. | ||
""" | ||
var_list = node.find(self.docbook_ns + "variablelist") | ||
if var_list is not None: | ||
enums = self.parse_variable_list(var_list) | ||
else: | ||
enums = self.parse_linked_variablelist(node) | ||
if enums: | ||
if vr == VR.AT: | ||
return [] # this is included in INT_VRs, but won't work | ||
if vr in INT_VR: | ||
int_enums: ValuesType = [] | ||
for e in enums: | ||
assert isinstance(e, str) | ||
if e.endswith("H"): | ||
int_enums.append(int(e[:-1], 16)) | ||
else: | ||
int_enums.append(int(e)) | ||
return int_enums | ||
if vr in STR_VR: | ||
return enums | ||
# any other VR does not make sense here | ||
print( | ||
f"Ignoring enum values: " | ||
f"{', '.join([str(e) for e in enums])} with VR {vr}" | ||
) | ||
return [] | ||
|
||
def parse_variable_list(self, var_list) -> ValuesType: | ||
# we assume that a variablelist contains enumerated values or defined terms | ||
# we ignore defined terms, as they do not limit the possible values | ||
title = var_list.find(self.docbook_ns + "title") | ||
# TODO: handle cases with conditions | ||
if title is None or title.text.lower() not in ( | ||
"enumerated values", | ||
"enumerated values:", | ||
): | ||
return [] | ||
terms = [] | ||
for item in var_list.findall(self.docbook_ns + "varlistentry"): | ||
term = item.find(self.docbook_ns + "term") | ||
if term is not None: | ||
terms.append(term.text) | ||
return terms | ||
|
||
def parse_linked_variablelist(self, node) -> ValuesType: | ||
for xref in node.findall(f"{self.docbook_ns}para/{self.docbook_ns}xref"): | ||
link = xref.attrib.get("linkend") | ||
if link and link.startswith("sect_"): | ||
if link in self._enum_cache: | ||
return self._enum_cache[link] | ||
section = self._find_section(link[5:]) | ||
if section is not None: | ||
var_list = section.find(f"{self.docbook_ns}variablelist") | ||
if var_list is not None: | ||
self._enum_cache[link] = self.parse_variable_list(var_list) | ||
return self._enum_cache[link] | ||
return [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
from typing import Optional, Generator | ||
from xml.etree import ElementTree | ||
|
||
import pytest | ||
from pydicom.valuerep import VR | ||
|
||
from dicom_validator.spec_reader.enum_parser import EnumParser | ||
from dicom_validator.spec_reader.part3_reader import Part3Reader | ||
|
||
|
||
@pytest.fixture | ||
def chapter_c(dict_reader, spec_path): | ||
yield Part3Reader(spec_path, dict_reader.data_elements()).get_doc_root().find( | ||
['chapter[@label="C"]'] | ||
) | ||
|
||
|
||
def find_chapter(name: str): | ||
return None | ||
|
||
|
||
@pytest.fixture | ||
def parser() -> Generator[EnumParser, None, None]: | ||
yield EnumParser(find_chapter) | ||
|
||
|
||
def section(contents, label="C.42.3.5") -> Optional[ElementTree.Element]: | ||
xml = f"""<?xml version="1.0" encoding="utf-8"?> | ||
<book xmlns="http://docbook.org/ns/docbook" xmlns:xl="http://www.w3.org/1999/xlink"> | ||
<chapter label="C"><section label="{label}" xml:id="sect_{label}"> | ||
{contents} | ||
</section></chapter> | ||
</book>""" | ||
doc = ElementTree.fromstring(xml) | ||
return doc.find(f".//{{http://docbook.org/ns/docbook}}section[@label='{label}']") | ||
|
||
|
||
class TestEmptyEnumParser: | ||
@pytest.mark.section_content("") | ||
def test_empty_description(self, parser): | ||
assert parser.parse(section(""), VR.SH) == [] | ||
|
||
def test_incorrect_tag(self, parser): | ||
content = """<variablelist> | ||
<div>Enumerated Values:</div> | ||
<varlistentry> | ||
<term>NO</term> | ||
</varlistentry> | ||
</variablelist>""" | ||
assert parser.parse(section(content), VR.SS) == [] | ||
|
||
def test_empty_list(self, parser): | ||
content = """<variablelist> | ||
<title>Enumerated Values:</title> | ||
</variablelist>""" | ||
assert parser.parse(section(content), VR.LO) == [] | ||
|
||
|
||
class TestEnumParser: | ||
@pytest.mark.section_content() | ||
def test_single_enum(self, parser): | ||
content = """<variablelist> | ||
<title>Enumerated Values:</title> | ||
<varlistentry> | ||
<term>NO</term> | ||
</varlistentry> | ||
</variablelist>""" | ||
assert parser.parse(section(content), VR.SH) == ["NO"] | ||
|
||
def test_single_enum_with_extra_tag(self, parser): | ||
content = """<variablelist> | ||
<title>Enumerated Values:</title> | ||
<varlistentry> | ||
<listitem> | ||
<para xml:id="para_f6578fe2-d628-412e-8314-af2c8961b633"/> | ||
</listitem> | ||
<term>NO</term> | ||
</varlistentry> | ||
</variablelist>""" | ||
assert parser.parse(section(content), VR.SH) == ["NO"] | ||
|
||
def test_two_enums(self, parser): | ||
content = """<variablelist> | ||
<title>Enumerated Values:</title> | ||
<varlistentry> | ||
<term>YES</term> | ||
</varlistentry> | ||
<varlistentry> | ||
<term>NO</term> | ||
</varlistentry> | ||
</variablelist>""" | ||
assert parser.parse(section(content), VR.SH) == ["YES", "NO"] | ||
|
||
def test_int_enums(self, parser): | ||
content = """<variablelist> | ||
<title>Enumerated Values:</title> | ||
<varlistentry> | ||
<term>0000</term> | ||
</varlistentry> | ||
<varlistentry> | ||
<term>0001</term> | ||
</varlistentry> | ||
</variablelist>""" | ||
assert parser.parse(section(content), VR.US) == [0, 1] | ||
|
||
def test_hex_enums(self, parser): | ||
content = """<variablelist> | ||
<title>Enumerated Values:</title> | ||
<varlistentry> | ||
<term>0010H</term> | ||
</varlistentry> | ||
<varlistentry> | ||
<term>0011H</term> | ||
</varlistentry> | ||
</variablelist>""" | ||
assert parser.parse(section(content), VR.US) == [16, 17] | ||
|
||
def test_linked_enum(self): | ||
content = """<para>Bla blah, see | ||
<xref linkend="sect_10.7.1.2" xrefstyle="select: label"/>.</para>" | ||
""" | ||
linked = """ | ||
<title>Pixel Spacing Calibration Type</title> | ||
<para xml:id="para_f24c1">Pixel Spacing Calibration Type.</para> | ||
<variablelist spacing="compact"> | ||
<title>Enumerated Values:</title> | ||
<varlistentry> | ||
<term>GEOMETRY</term> | ||
<listitem> | ||
<para xml:id="para_a79e2faf">Description...</para> | ||
</listitem> | ||
</varlistentry> | ||
<varlistentry> | ||
<term>FIDUCIAL</term> | ||
<listitem> | ||
<para xml:id="para_10de0799">Another...</para> | ||
</listitem> | ||
</varlistentry> | ||
</variablelist> | ||
""" | ||
parser = EnumParser(lambda s: section(linked, s)) | ||
assert parser.parse(section(content), VR.SH) == ["GEOMETRY", "FIDUCIAL"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.