Comparing two codes where one is erroneously set as a SRT will throw KeyError
erikogabrielsson opened this issue · comments
Erik O Gabrielsson commented
Describe the bug
The comparision of two codes does mapping from SRT
codes to SCT
. If comparision is done on a code that has SRT
as the scheme designator but is not in the dictionary of SRT
to SCT
mappings, the comparision will throw a KeyError
.
Expected behavior
If the mapping from SRT
to SCT
is not possible I would excpect the comparision to return False
if the codes are not equal.
Steps To Reproduce
wrong_srt_code = Code("111729", "SRT", "Specimen storage") # Is actually a DCM code
sct_code = Code("127790008", "SCT", "Staining")
wrong_srt_code == sct_code
Throws:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Cell In[9], [line 4](vscode-notebook-cell:?execution_count=9&line=4)
[1](vscode-notebook-cell:?execution_count=9&line=1) wrong_srt_code = Code("111729", "SRT", "Specimen storage") # Is actually a DCM code
[2](vscode-notebook-cell:?execution_count=9&line=2) sct_code = Code("127790008", "SCT", "Staining")
----> [4](vscode-notebook-cell:?execution_count=9&line=4) wrong_srt_code == sct_code
File [c:\work\notebooks\.venv311\Lib\site-packages\pydicom\sr\coding.py:26](file:///C:/work/notebooks/.venv311/Lib/site-packages/pydicom/sr/coding.py:26), in Code.__eq__(self, other)
[23](file:///C:/work/notebooks/.venv311/Lib/site-packages/pydicom/sr/coding.py:23) def __eq__(self, other: Any) -> Any:
[24](file:///C:/work/notebooks/.venv311/Lib/site-packages/pydicom/sr/coding.py:24) if self.scheme_designator == "SRT":
[25](file:///C:/work/notebooks/.venv311/Lib/site-packages/pydicom/sr/coding.py:25) self_mapped = Code(
---> [26](file:///C:/work/notebooks/.venv311/Lib/site-packages/pydicom/sr/coding.py:26) value=snomed_mapping["SRT"][self.value],
[27](file:///C:/work/notebooks/.venv311/Lib/site-packages/pydicom/sr/coding.py:27) meaning="",
[28](file:///C:/work/notebooks/.venv311/Lib/site-packages/pydicom/sr/coding.py:28) scheme_designator="SCT",
[29](file:///C:/work/notebooks/.venv311/Lib/site-packages/pydicom/sr/coding.py:29) scheme_version=self.scheme_version,
[30](file:///C:/work/notebooks/.venv311/Lib/site-packages/pydicom/sr/coding.py:30) )
[31](file:///C:/work/notebooks/.venv311/Lib/site-packages/pydicom/sr/coding.py:31) else:
[32](file:///C:/work/notebooks/.venv311/Lib/site-packages/pydicom/sr/coding.py:32) self_mapped = Code(
[33](file:///C:/work/notebooks/.venv311/Lib/site-packages/pydicom/sr/coding.py:33) value=self.value,
[34](file:///C:/work/notebooks/.venv311/Lib/site-packages/pydicom/sr/coding.py:34) meaning="",
[35](file:///C:/work/notebooks/.venv311/Lib/site-packages/pydicom/sr/coding.py:35) scheme_designator=self.scheme_designator,
[36](file:///C:/work/notebooks/.venv311/Lib/site-packages/pydicom/sr/coding.py:36) scheme_version=self.scheme_version,
[37](file:///C:/work/notebooks/.venv311/Lib/site-packages/pydicom/sr/coding.py:37) )
KeyError: '111729'
Your environment
Please use the pydicom.env_info
module (available in pydicom 2 or later)
to gather information about your environment and paste it into the issue:
module | version
------ | -------
platform | Windows-10-10.0.22631-SP0
Python | 3.11.6 (tags/v3.11.6:8b6ee5b, Oct 2 2023, 14:57:12) [MSC v.1935 64 bit (AMD64)]
pydicom | 3.0.0.dev0
gdcm | 3.0.22
jpeg_ls | **cannot determine version**
numpy | 1.26.2
PIL | 10.1.0
pylibjpeg | 1.4.0
openjpeg | 1.3.2
libjpeg | _module not found_