Skip to main content

Python

A Python package named pathling is available from PyPI.

View the generated API documentation

Encoders

The Python library features a set of encoders for converting FHIR data into Spark dataframes.

See the Encoders page for more details.

Value set membership

The member_of function can be used to test the membership of a code within a FHIR value set. In this example, we take a list of SNOMED CT diagnosis codes and create a new column which shows which are viral infections.

# << 64572001|Disease| : (
# << 370135005|Pathological process| = << 441862004|Infectious process|,
# << 246075003|Causative agent| = << 49872002|Virus|
# )
result = pc.member_of(csv, to_coding(csv.CODE, 'http://snomed.info/sct'),
'http://snomed.info/sct?fhir_vs=ecl/%3C%3C%2064572001%20%3A%20('
'%3C%3C%20370135005%20%3D%20%3C%3C%20441862004%20%2C%20%3C%3C%2'
'0246075003%20%3D%20%3C%3C%2049872002%20)',
'VIRAL_INFECTION')
result.select('CODE', 'DESCRIPTION', 'VIRAL_INFECTION').show()

Results in:

CODEDESCRIPTIONVIRAL_INFECTION
65363002Otitis mediafalse
16114001Fracture of anklefalse
444814009Viral sinusitistrue
444814009Viral sinusitistrue
43878008Streptococcal sore throatfalse

Concept translation

The translate function can be used to translate codes from one code system to another using maps that are known to the terminology server. In this example, we translate our SNOMED CT diagnosis codes into Read CTV3.

result = pc.translate(csv, to_coding(csv.CODE, 'http://snomed.info/sct'),
'http://snomed.info/sct/900000000000207008?fhir_cm='
'900000000000497000',
output_column_name='READ_CODE')
result = result.withColumn('READ_CODE', result.READ_CODE.code)
result.select('CODE', 'DESCRIPTION', 'READ_CODE').show()

Results in:

CODEDESCRIPTIONREAD_CODE
65363002Otitis mediaX00ik
16114001Fracture of ankleS34..
444814009Viral sinusitisXUjp0
444814009Viral sinusitisXUjp0
43878008Streptococcal sore throatA340.

Subsumption testing

The subsumes function allows us to perform subsumption testing on codes within our data. In hierarchical code systems, a subsumption test determines whether a code is a subtype of another code, e.g. an "ankle fracture" is subsumed by " fracture".

In this example, we first take our codes, cross-join them and then test whether they subsume each other. Then we do another subsumption test against the "ear, nose and throat disorder" concept.

# 232208008 |Ear, nose and throat disorder|
left_coding = Coding('http://snomed.info/sct', '232208008')
right_coding_column = to_coding(csv.CODE, 'http://snomed.info/sct')

result = pc.subsumes(csv, 'SUBSUMES',
left_coding=left_coding,
right_coding_column=right_coding_column)

result.select('CODE', 'DESCRIPTION', 'IS_ENT').show()

Results in:

CODEDESCRIPTIONIS_ENT
65363002Otitis mediatrue
16114001Fracture of anklefalse
444814009Viral sinusitistrue