crs4 / hl7apy

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

Feature request: Hl7 2.1 support #41

Closed tschnibo closed 5 years ago

tschnibo commented 6 years ago

Good day and thanks for sharing this package!

Do you know what sort of changes there would be needed to use the package with Hl7 v2.1? It seems our software is using this quite an old version.

Kind regards and many thanks four your suggestions.

svituz commented 6 years ago

Hello tschinibo, to support version 2.1 we should have the schemes of all the messages of that version and create the internal representation of that messages as in other versions (e.g., here there is the internal representation of v2.2 structures). We have a script that parses the official XML and creates the structures but we haven't found the v2.1.

tschnibo commented 6 years ago

Dear svituz, thanks for the quick reply!

I guess you did look for it and therefore there is probably no such XML. Too bad. I'll think about how to solve this problem.

Maybe I'll consult you again. Thank you!

svituz commented 6 years ago

Well, of course the other way is to write all the structures manually without generating them. An idea is to copy the version 2.2 files and change the structures according to the 2.1 specification. If you look at HAPI structures for 2.1 and 2.2 it should be easy to check the differences and reply them on hl7apy. Kinda boring actually, but easy :)

tschnibo commented 6 years ago

Ok, thats at least a possibility! I'll have a look at this tomorrow.

Many thanks!

svituz commented 5 years ago

Hi @tschnibo, I created the structures for version 2.1 in the develop branch. Probably some structures are not completely correct, for example the CM datatype. That said, can you try that version or send some example messages before I push a release? Thank you, Vittorio

tschnibo commented 5 years ago

Hi @svituz thank you! That's very kind! I was parsing messages with just copying the 2.2 folder, and no problems so far. But I just parsed some data-fields, for testing, not quantitatively. Can it be, that the parsing is completely downward compatible? Until now I am not sending any messages, I am just building a 'consuming' application. Can the parsing of messages be helpful for the tests too? Sorry for the confusion, I'm totally new to hl7. Kind regards, Julian

svituz commented 5 years ago

The parsing, just like the message creation from scratch, follow the structures of the messages taken from the correct version. If the message doesn't adhere to the structure, the parsing can fail. It can happen that just changing the version of the message in the MSH doesn't break the parsing, but in this way you are not using the correct structure. For example, the version 2.2 may have different segments for a message or different fields. So, the parsing is totally helpful.

tschnibo commented 5 years ago

Dear Vittorio, sorry for the delay. Then I am going to test the new version. I'll be able to work on the project in two days. I'll keep you posted!

tschnibo commented 5 years ago

Dear Vittorio, already in the first file I tried I get the following error:

What do you think causes this?

###########

` File "...lib\site-packages\hl7apy\parser.py", line 88, in parse_message children = parse_segments(message, m.version, encoding_chars, validation_level, m.reference, find_groups) File "...\lib\site-packages\hl7apy\core.py", line 842, in getattr raise AttributeError(name) AttributeError: reference

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "...\lib\site-packages\hl7apy\v2_1__init__.py", line 41, in get return ELEMENTS[element_type][name] KeyError: 'ID_1'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "...\lib\site-packages\hl7apy\core.py", line 546, in get_structure reference = load_reference(element.name, element.classname, element.version)

File "...\lib\site-packages\hl7apy__init__.py", line 280, in load_reference ref = lib.get(name, element_type) File "...\lib\site-packages\hl7apy\v2_1__init__.py", line 43, in get raise ChildNotFound(name) hl7apy.exceptions.ChildNotFound: No child named ID_1

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "c:\virtualizedpython\Lib\runpy.py", line 183, in _run_module_as_main mod_name, mod_spec, code = _get_module_details(mod_name, _Error) File "c:\virtualizedpython\Lib\runpy.py", line 109, in _get_module_details import(pkg_name) File "...\hl7_parser.py", line 49, in u = parser.parse_message(hl7) File "...\site-packages\hl7apy\parser.py", line 90, in parse_message children = parse_segments(message, m.version, encoding_chars, validation_lev el, find_groups=False) File "...\site-packages\hl7apy\parser.py", line 152, in parse_segments segment = parse_segment(s.strip(), version, encoding_chars, validation_level) File "C:\award_env\lib\site-packages\hl7apy\parser.py", line 241, in parse_segment segment.structure_by_name, segment.allow_infinite_children) File "C:\award_env\lib\site-packages\hl7apy\parser.py", line 315, in parse_fields reference, force_varies)) File "...\site-packages\hl7apy\parser.py", line 389, in parse_field field.structure_by_name) File "...\site-packages\hl7apy\parser.py", line 462, in parse_components version, encoding_chars, validation_level, reference)) File "...\site-packages\hl7apy\parser.py", line 522, in parse_component children = parse_subcomponents(text, component.datatype, version, encoding_c hars, validation_level) File "...\site-packages\hl7apy\parser.py", line 587, in parse_subcomponents version, validation_level)) File "...\site-packages\hl7apy\parser.py", line 620, in parse_subcomponent validation_level=validation_level) File "...\site-packages\hl7apy\core.py", line 1116, in init version, validation_level, traversal_parent) File "...\site-packages\hl7apy\core.py", line 1042, in init validation_level, traversal_parent) File "...\site-packages\hl7apy\core.py", line 638, in init self._find_structure(reference) File "...\site-packages\hl7apy\core.py", line 1065, in _find_structure structure = ElementFinder.get_structure(self, reference) File "...\site-packages\hl7apy\core.py", line 548, in get_structure raise InvalidName(element.classname, element.name) hl7apy.exceptions.InvalidName: Invalid name for SubComponent: ID_1`

The shortened message is: `MSH|^~\&|LDS-LAB|LAB1|SC-MED|MED1|2011332112001||ORU|4554543|P|2.1||XLAB PID|1|4554d43|4554543|002344234455|test^michii||4554543|M||||||||W|RK

`

svituz commented 5 years ago

It was a problem with the ID datatypes that I've just fixed. I tried to parse the message but I noticed that it is missing the escape character (): is it a problem with your example or version 2.1 is actually missing it? (I hope for the first option :) )

tschnibo commented 5 years ago

Perfect thanks! I'll retest. I had to do something like .replace( "\r\n" , "\r" ) before parsing the files, in order to make the parsing work. Do you mean this "segment" escape?

svituz commented 5 years ago

The encoding characters are the MSH.1, which is usually |, and MSH.2 which are the first 4 characters after that and usually are ^~\&. In your message the \ char is missing. I wonder it is because 2.1 doesn't have that character. You can find some info here

EDIT: I noticed it is probably a problem with GitHub using the same encoding char :relaxed:

tschnibo commented 5 years ago

OK, yes, I looked at the first line again. And voila, there is a "^~\&" contained :) - so seemst to be ok then, right?

I parsed some informations out of 7000 files now. It works smooth. When I just copied the 2.2 folder earlier I run into some errors, so until now your 2.1 version works better than the 2.2 version for parsing 2.1 files ;)

Maybe I'll find more issues proceeding, till now my test-case is not very thorough.

tuckeryatesjvion commented 5 years ago

@svituz - Are there any plans to release this officially via pypi? We integrate with some very old clients and might have to grab what you all have been working on for 2_1 from the develop branch

svituz commented 5 years ago

@tuckeryatesjvion I'm preparing a new release, it should be ready today

svituz commented 5 years ago

Release 1.3.3 with version 2.1 support has been released

tuckeryatesjvion commented 5 years ago

Thank you so much!