I am trying to create an HL7 RU_R01 message with multiple OBX segments. However, I cannot find a way to merge these segments together into the message.
Here's my code:
message = Message("ORU_R01", validation_level=VALIDATION_LEVEL.STRICT)
has_xxx_formatted = '2'
text_formatted = DEFAULT_HL7_TEXT["EN"]["XXXX"]
## MSH Segment
message.MSH.msh_3 = 'XXXXX'
message.msh.msh_4 = 'XXXXX'
message.msh.msh_9 = "ORU^R01^ORU_R01"
message.msh.msh_10 = ""
message.msh.msh_11 = ""
# patient details
message.ORU_R01_PATIENT_RESULT.ORU_R01_PATIENT.PID.pid_2 = "patient_id"
message.ORU_R01_PATIENT_RESULT.ORU_R01_PATIENT.PID.pid_3 = ""
message.ORU_R01_PATIENT_RESULT.ORU_R01_PATIENT.PID.pid_5 = ""
## OBR Segment -- frature details
message.ORU_R01_PATIENT_RESULT.ORU_R01_ORDER_OBSERVATION.OBR.obr_4 = "Observations"
message.ORU_R01_PATIENT_RESULT.ORU_R01_ORDER_OBSERVATION.OBR.obr_7 = f"{datetime.now().strftime('%Y%m%d%H%M%S.%f')[:-2]}"
# obx0
obx0 = Segment('OBX', version='2.5')
obx0.obx_1 = "1"
obx0.obx_2 = "HD"
obx0.obx_3 = "REFInstanceUID"
obx0.obx_5 = "ref_uid"
obx0.obx_11 = "F"
# obx1
obx1 = Segment('OBX', version='2.5')
obx1.obx_1 = "2"
obx1.obx_2 = "TX"
obx1.obx_3 = "Presence"
obx1.obx_5 = "XXXX"
obx1.obx_11 = "F"
# obx2
obx2 = Segment('OBX', version='2.5')
obx2.obx_1 = "3"
obx2.obx_2 = "TX"
obx2.obx_3 = "Result"
obx2.obx_5 = "XXXXX"
obx2.obx_11 = "F"
Now if I add these obx segments using:
message.ORU_R01_PATIENT_RESULT.ORU_R01_ORDER_OBSERVATION.ORU_R01_OBSERVATION.OBX.obx_1 = 1
# adds individual values of obx0
....
message.ORU_R01_PATIENT_RESULT.ORU_R01_ORDER_OBSERVATION.ORU_R01_OBSERVATION.OBX.obx_1 = 2
# adds individual values of obx1
....
message.ORU_R01_PATIENT_RESULT.ORU_R01_ORDER_OBSERVATION.ORU_R01_OBSERVATION.OBX.obx_1 = 3
# adds individual values of obx2
....
It only adds the last OBX segment into the message.
If I try to add the convert the OBX segments into a group as suggested here:
name = 'MDM_T02_OBXNTE_SUPPGRP'
mdm_group = Group(name, version='2.5')
mdm_group.obx = obx0
mdm_group.add(obx1)
mdm_group.add(obx2)
message.ORU_R01_PATIENT_RESULT.ORU_R01_ORDER_OBSERVATION.ORU_R01_ORDER_OBSERVATION = mdm_group
I get the following error:
InvalidName: Invalid name for Group: MDM_T02_OBXNTE_SUPPGRP
I'd like to know how to add these multiple segments into a single message after the OBR. Thank you in advance
It seems that the correct way to add multiple segments is to add each hl7apy.core.Segment
to it's individual hl7apy.core.Group
. This is explained here, and the documentation from the java version of hl7apy is useful.
message = Message("ORU_R01", validation_level=VALIDATION_LEVEL.TOLERATED)
has_xxx_formatted = '2'
text_formatted = DEFAULT_HL7_TEXT["EN"]["XXXX"]
## MSH Segment
message.MSH.msh_3 = 'XXXXX'
message.msh.msh_4 = 'XXXXX'
message.msh.msh_9 = "ORU^R01^ORU_R01"
message.msh.msh_10 = ""
message.msh.msh_11 = ""
# patient details
message.ORU_R01_PATIENT_RESULT.ORU_R01_PATIENT.PID.pid_2 = "patient_id"
message.ORU_R01_PATIENT_RESULT.ORU_R01_PATIENT.PID.pid_3 = ""
message.ORU_R01_PATIENT_RESULT.ORU_R01_PATIENT.PID.pid_5 = ""
## OBR Segment -- frature details
message.ORU_R01_PATIENT_RESULT.ORU_R01_ORDER_OBSERVATION.OBR.obr_4 = "Observations"
message.ORU_R01_PATIENT_RESULT.ORU_R01_ORDER_OBSERVATION.OBR.obr_7 = f"{datetime.now().strftime('%Y%m%d%H%M%S.%f')[:-2]}"
obs_name = 'ORU_R01_OBSERVATION'
# obx0
obx0_group = Group(obs_name, version='2.5')
obx0 = Segment('OBX', version='2.5')
obx0.obx_1 = "1"
obx0.obx_2 = "HD"
obx0.obx_3 = "REFInstanceUID"
obx0.obx_5 = "ref_uid"
obx0.obx_11 = "F"
obx0_group.add(obx0)
message.ORU_R01_PATIENT_RESULT.ORU_R01_ORDER_OBSERVATION.add(obx0_group)
# obx1
name = 'ORU_R01_OBSERVATION'
obx1_group = Group(obs_name, version='2.5')
obx1 = Segment('OBX', version='2.5')
obx1.obx_1 = "2"
obx1.obx_2 = "TX"
obx1.obx_3 = "Presence"
obx1.obx_5 = "XXXX"
obx1.obx_11 = "F"
obx1_group.add(obx1)
message.ORU_R01_PATIENT_RESULT.ORU_R01_ORDER_OBSERVATION.add(obx1_group)
# obx2
name = 'ORU_R01_OBSERVATION'
obx2_group = Group(obs_name, version='2.5')
obx2 = Segment('OBX', version='2.5')
obx2.obx_1 = "3"
obx2.obx_2 = "TX"
obx2.obx_3 = "Result"
obx2.obx_5 = "XXXXX"
obx2.obx_11 = "F"
obx2_group.add(obx2)
message.ORU_R01_PATIENT_RESULT.ORU_R01_ORDER_OBSERVATION.add(obx2_group)
Also note that the validation level is set to tolerated TOLERATED
. STRICT
mode only works with a single OBX segment.
Now the validation works.
assert hl7.validate() is True