cardinal_pythonlib.snomed
Original code copyright (C) 2009-2022 Rudolf Cardinal (rudolf@pobox.com).
This file is part of cardinal_pythonlib.
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Functions to assist with SNOMED-CT.
Note that the licensing arrangements for SNOMED-CT mean that the actual codes must be separate (and not part of this code).
A full SNOMED CT download is about 1.1 Gb; see
https://digital.nhs.uk/services/terminology-and-classifications/snomed-ct.
Within a file such as uk_sct2cl_26.0.2_20181107000001.zip
, relevant files
include:
# Files with "Amoxicillin" in include two snapshots and two full files:
SnomedCT_UKClinicalRF2_PRODUCTION_20181031T000001Z/Full/Terminology/sct2_Description_Full-en-GB_GB1000000_20181031.txt
# ... 234,755 lines
SnomedCT_InternationalRF2_PRODUCTION_20180731T120000Z/Full/Terminology/sct2_Description_Full-en_INT_20180731.txt
# ... 2,513,953 lines; this is the main file.
Note grammar:
Test basic expressions:
import logging
from cardinal_pythonlib.logs import main_only_quicksetup_rootlogger
from cardinal_pythonlib.snomed import *
main_only_quicksetup_rootlogger(level=logging.DEBUG)
# ---------------------------------------------------------------------
# From the SNOMED-CT examples (https://www.snomed.org/scg), with some
# values fixed from the term browser:
# ---------------------------------------------------------------------
diabetes = SnomedConcept(73211009, "Diabetes mellitus (disorder)")
diabetes_expr = SnomedExpression(diabetes)
print(diabetes_expr.longform)
print(diabetes_expr.shortform)
pain = SnomedConcept(22253000, "Pain (finding)")
finding_site = SnomedConcept(36369800, "Finding site")
foot = SnomedConcept(56459004, "Foot")
pain_in_foot = SnomedExpression(pain, {finding_site: foot})
print(pain_in_foot.longform)
print(pain_in_foot.shortform)
amoxicillin_medicine = SnomedConcept(27658006, "Product containing amoxicillin (medicinal product)")
amoxicillin_substance = SnomedConcept(372687004, "Amoxicillin (substance)")
has_dose_form = SnomedConcept(411116001, "Has manufactured dose form (attribute)")
capsule = SnomedConcept(385049006, "Capsule (basic dose form)")
has_active_ingredient = SnomedConcept(127489000, "Has active ingredient (attribute)")
has_basis_of_strength_substance = SnomedConcept(732943007, "Has basis of strength substance (attribute)")
mass = SnomedConcept(118538004, "Mass, a measure of quantity of matter (property) (qualifier value)")
unit_of_measure = SnomedConcept(767524001, "Unit of measure (qualifier value)")
milligrams = SnomedConcept(258684004, "milligram (qualifier value)")
amoxicillin_500mg_capsule = SnomedExpression(
amoxicillin_medicine, [
SnomedAttributeSet({has_dose_form: capsule}),
SnomedAttributeGroup({
has_active_ingredient: amoxicillin_substance,
has_basis_of_strength_substance: SnomedExpression(
amoxicillin_substance, {
mass: 500,
unit_of_measure: milligrams,
}
),
}),
]
)
print(amoxicillin_500mg_capsule.longform)
print(amoxicillin_500mg_capsule.shortform)
- class cardinal_pythonlib.snomed.SnomedAttribute(name: SnomedConcept, value: SnomedConcept | SnomedExpression | int | float | str)[source]
Represents a SNOMED-CT attribute, being a name/value pair.
- Parameters:
name¶ – a
SnomedConcept
(attribute name)value¶ – an attribute value (
SnomedConcept
, number, or string)
- class cardinal_pythonlib.snomed.SnomedAttributeGroup(attribute_set: Dict[SnomedConcept, SnomedConcept | SnomedExpression | int | float | str] | SnomedAttributeSet)[source]
Represents a collected group of attribute/value pairs.
- Parameters:
attribute_set¶ – a
SnomedAttributeSet
to group
- class cardinal_pythonlib.snomed.SnomedAttributeSet(attributes: Dict[SnomedConcept, SnomedConcept | SnomedExpression | int | float | str] | Iterable[SnomedAttribute])[source]
Represents an attribute set.
- Parameters:
attributes¶ – the attributes
- class cardinal_pythonlib.snomed.SnomedBase[source]
Common functions for SNOMED-CT classes
- as_string(longform: bool = True) str [source]
Returns the string form.
- Parameters:
longform¶ – print SNOMED-CT concepts in long form?
- property shortform: str
Returns the short form, without terms.
- class cardinal_pythonlib.snomed.SnomedConcept(identifier: int, term: str)[source]
Represents a SNOMED concept with its description (associated term).
- class cardinal_pythonlib.snomed.SnomedExpression(focus_concept: SnomedConcept | SnomedFocusConcept, refinement: SnomedRefinement | Dict[SnomedConcept, SnomedConcept | SnomedExpression | int | float | str] | List[SnomedAttributeSet | SnomedAttributeGroup] | None = None)[source]
An expression containing several SNOMED-CT codes in relationships.
- Parameters:
focus_concept¶ – the core concept(s); a
SnomedFocusConcept
refinement¶ – optional additional information; a
SnomedRefinement
or a dictionary or list that can be converted to one
- class cardinal_pythonlib.snomed.SnomedFocusConcept(concept: SnomedConcept | Iterable[SnomedConcept])[source]
Represents a SNOMED-CT focus concept, which is one or more concepts.
- Parameters:
concept¶ – the core concept(s); a
SnomedCode
or an iterable of them
- class cardinal_pythonlib.snomed.SnomedRefinement(refinements: Dict[SnomedConcept, SnomedConcept | SnomedExpression | int | float | str] | Iterable[SnomedAttributeSet | SnomedAttributeGroup])[source]
Implements a SNOMED-CT “refinement”, which is an attribute set +/- some attribute groups.
- Parameters:
refinements¶ – iterable of
SnomedAttributeSet
(but only zero or one) andSnomedAttributeGroup
objects
- class cardinal_pythonlib.snomed.SnomedValue(value: SnomedConcept | SnomedExpression | int | float | str)[source]
Represents a value: either a concrete value (e.g. int, float, str), or a SNOMED-CT concept/expression.
Implements the grammar elements: attributeValue, expressionValue, stringValue, numericValue, integerValue, decimalValue.
- Parameters:
value¶ – the value
- cardinal_pythonlib.snomed.double_quoted(s: str) str [source]
Returns a representation of the string argument with double quotes and escaped characters.
- Parameters:
s¶ – the argument
See:
https://code.activestate.com/lists/python-list/272714/ – does not work as null values get escaped in different ways in modern Python, and in a slightly unpredictable way
https://mail.python.org/pipermail/python-list/2003-April/236940.html – won’t deal with repr() using triple-quotes
https://stackoverflow.com/questions/1675181/get-str-repr-with-double-quotes-python – probably the right general approach
Test code:
from cardinal_pythonlib.snomed import double_quoted def test(s): print(f"double_quoted({s!r}) -> {double_quoted(s)}") test("ab'cd") test("ab'c\"d") test('ab"cd')