crs4 / hl7apy

Python library to parse, create and handle HL7 v2 messages.
http://crs4.github.io/hl7apy/
MIT License
215 stars 85 forks source link

Child limit exceeded ORU_R01_OBSERVATION.OBX #90

Open kwiechen opened 3 years ago

kwiechen commented 3 years ago

I have tried adding an additional OBX segment to an ORU_R01 message using a slightly modified code from Michael Sarfati

from hl7apy import core

hl7 = core.Message("ORU_R01", version="2.2")

hl7.msh.msh_3 = "SendingApp" hl7.msh.msh_4 = "SendingFac" hl7.msh.msh_5 = "ReceivingApp" hl7.msh.msh_6 = "ReceivingFac" hl7.msh.msh_9 = "ORU^R01" hl7.msh.msh_10 = "168715" hl7.msh.msh_11 = "P"

hl7.ORU_R01_PATIENT_RESULT.ORU_R01_PATIENT.PID.pid_3 = "1" hl7.ORU_R01_PATIENT_RESULT.ORU_R01_PATIENT.PID.pid_5 = "Patient^Test"

hl7.ORU_R01_PATIENT_RESULT.ORU_R01_ORDER_OBSERVATION.OBR.obr_4 = "10000"

hl7.ORU_R01_PATIENT_RESULT.ORU_R01_ORDER_OBSERVATION.ORU_R01_OBSERVATION.OBX.obx_1 = "1" hl7.ORU_R01_PATIENT_RESULT.ORU_R01_ORDER_OBSERVATION.ORU_R01_OBSERVATION.OBX.obx_2 = "ST" hl7.ORU_R01_PATIENT_RESULT.ORU_R01_ORDER_OBSERVATION.ORU_R01_OBSERVATION.OBX.obx_3 = "PDF" hl7.ORU_R01_PATIENT_RESULT.ORU_R01_ORDER_OBSERVATION.ORU_R01_OBSERVATION.OBX.obx_5 = "Test" hl7.ORU_R01_PATIENT_RESULT.ORU_R01_ORDER_OBSERVATION.ORU_R01_OBSERVATION.OBX.obx_11 = "F" # Observ Result Status -- "F" meaning 'Final result'

obx = core.Segment("OBX", version="2.2") obx.obx_1 = "2" obx.obx_2 = "ST" obx.obx_5 = "Test2" obx.obx_11 = "F"

hl7.ORU_R01_PATIENT_RESULT.ORU_R01_ORDER_OBSERVATION.ORU_R01_OBSERVATION.add(obx)

assert hl7.validate() is True

-> gives exception: Child limit exceeded ORU_R01_OBSERVATION.OBX

As far as I understand the specification one or more OBX segments are allowed?

Best regards,

Kai

rileyschack commented 3 years ago

The specification allows for 1 OBX segment per OBSERVATION group. You'll need to add a new OBSERVATION group for each OBX segment you would like to add.

perjer4675 commented 2 years ago

@kwiechen @rileyschack Do you have an code example? I do not understand.

svituz commented 2 years ago

@kwiechen @perjer4675 As @rileyschack suggested, OBX segment is part of the ORU_R01_OBSERVATION group which, in turn, is part of ORU_R01_ORDER_OBSERVATION group. It means that you need to create both groups and attach them to ORU_R01_PATIENT_RESULT group.

This will add the OBX:

from hl7apy import core

hl7 = core.Message("ORU_R01", version="2.2")

hl7.msh.msh_3 = "SendingApp"
hl7.msh.msh_4 = "SendingFac"
hl7.msh.msh_5 = "ReceivingApp"
hl7.msh.msh_6 = "ReceivingFac"
hl7.msh.msh_9 = "ORU^R01"
hl7.msh.msh_10 = "168715"
hl7.msh.msh_11 = "P"

hl7.ORU_R01_PATIENT_RESULT.ORU_R01_PATIENT.PID.pid_3 = "1"
hl7.ORU_R01_PATIENT_RESULT.ORU_R01_PATIENT.PID.pid_5 = "Patient^Test"

hl7.ORU_R01_PATIENT_RESULT.ORU_R01_ORDER_OBSERVATION.OBR.obr_4 = "10000"

hl7.ORU_R01_PATIENT_RESULT.ORU_R01_ORDER_OBSERVATION.ORU_R01_OBSERVATION.OBX.obx_1 = "1"
hl7.ORU_R01_PATIENT_RESULT.ORU_R01_ORDER_OBSERVATION.ORU_R01_OBSERVATION.OBX.obx_2 = "ST"
hl7.ORU_R01_PATIENT_RESULT.ORU_R01_ORDER_OBSERVATION.ORU_R01_OBSERVATION.OBX.obx_3 = "PDF"
hl7.ORU_R01_PATIENT_RESULT.ORU_R01_ORDER_OBSERVATION.ORU_R01_OBSERVATION.OBX.obx_5 = "Test"
hl7.ORU_R01_PATIENT_RESULT.ORU_R01_ORDER_OBSERVATION.ORU_R01_OBSERVATION.OBX.obx_11 = "F" # Observ Result Status -- "F" meaning 'Final result'

obx = core.Segment("OBX", version="2.2")
obx.obx_1 = "2"
obx.obx_2 = "ST"
obx.obx_5 = "Test2"
obx.obx_11 = "F"

order_observation_group = core.Group("ORU_R01_ORDER_OBSERVATION", version="2.2")
observation_group = core.Group("ORU_R01_OBSERVATION", version="2.2")
observation_group.add(obx)
order_observation_group.add(observation_group)

hl7.ORU_R01_PATIENT_RESULT.add(order_observation_group)
print(repr(hl7.to_er7()))

Resulting in

'MSH|^~\\&|SendingApp|SendingFac|ReceivingApp|ReceivingFac|20210910120522||ORU^R01|168715|P|2.2\rPID|||1||Patient^Test\rOBR||||10000\rOBX|1|ST|PDF||Test||||||F\rOBX|2|ST|||Test2||||||F'

You can access the second instance by index.

hl7.ORU_R01_PATIENT_RESULT.ORU_R01_ORDER_OBSERVATION[1].ORU_R01_OBSERVATION.OBX

More about accessing elements here

Notice that validation still fails due to your ORU_R01_OBSERVATIONs missing ORU segment but this is one step forward :)

perjer4675 commented 2 years ago

@svituz thank you very much. ^_^