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.

See https://www.snomed.org/.

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: cardinal_pythonlib.snomed.SnomedConcept, value: Union[SnomedConcept, SnomedExpression, int, float, str])[source]

Represents a SNOMED-CT attribute, being a name/value pair.

Parameters:
as_string(longform: bool = True) → str[source]

Returns the string form.

Parameters:longform – print SNOMED-CT concepts in long form?
class cardinal_pythonlib.snomed.SnomedAttributeGroup(attribute_set: Union[Dict[SnomedConcept, Union[SnomedConcept, SnomedExpression, int, float, str]], cardinal_pythonlib.snomed.SnomedAttributeSet])[source]

Represents a collected group of attribute/value pairs.

Parameters:attribute_set – a SnomedAttributeSet to group
as_string(longform: bool = True) → str[source]

Returns the string form.

Parameters:longform – print SNOMED-CT concepts in long form?
class cardinal_pythonlib.snomed.SnomedAttributeSet(attributes: Union[Dict[SnomedConcept, Union[SnomedConcept, SnomedExpression, int, float, str]], Iterable[cardinal_pythonlib.snomed.SnomedAttribute]])[source]

Represents an attribute set.

Parameters:attributes – the attributes
as_string(longform: bool = True) → str[source]

Returns the string form.

Parameters:longform – print SNOMED-CT concepts in long form?
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?
shortform

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).

Parameters:
  • identifier – SNOMED-CT identifier (code)
  • term – associated term (description)
as_string(longform: bool = True) → str[source]

Returns the string form.

Parameters:longform – print SNOMED-CT concepts in long form?
concept_reference(longform: bool = True) → str[source]

Returns one of the string representations.

Parameters:longform – in long form, with the description (associated term)?
class cardinal_pythonlib.snomed.SnomedExpression(focus_concept: Union[cardinal_pythonlib.snomed.SnomedConcept, cardinal_pythonlib.snomed.SnomedFocusConcept], refinement: Union[cardinal_pythonlib.snomed.SnomedRefinement, Dict[SnomedConcept, Union[SnomedConcept, SnomedExpression, int, float, str]], List[Union[cardinal_pythonlib.snomed.SnomedAttributeSet, cardinal_pythonlib.snomed.SnomedAttributeGroup]]] = 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
as_string(longform: bool = True) → str[source]

Returns the string form.

Parameters:longform – print SNOMED-CT concepts in long form?
class cardinal_pythonlib.snomed.SnomedFocusConcept(concept: Union[cardinal_pythonlib.snomed.SnomedConcept, Iterable[cardinal_pythonlib.snomed.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
as_string(longform: bool = True) → str[source]

Returns the string form.

Parameters:longform – print SNOMED-CT concepts in long form?
class cardinal_pythonlib.snomed.SnomedRefinement(refinements: Union[Dict[SnomedConcept, Union[SnomedConcept, SnomedExpression, int, float, str]], Iterable[Union[cardinal_pythonlib.snomed.SnomedAttributeSet, cardinal_pythonlib.snomed.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) and SnomedAttributeGroup objects
as_string(longform: bool = True) → str[source]

Returns the string form.

Parameters:longform – print SNOMED-CT concepts in long form?
class cardinal_pythonlib.snomed.SnomedValue(value: Union[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
as_string(longform: bool = True) → str[source]

Returns the string form.

Parameters:longform – print SNOMED-CT concepts in long form?
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:

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')