CoolFace
Apppublic

hpiatrht/report-analyzer

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes
data_parser.cpython-311.pyc79 linesDownload Raw Back to __pycache__
1�

2@�ui� ���dZddlZddlZddlmZddlmZmZddlm	Z	e	e3��Zdedefd�Z
d	edefd4�Zdedefd�ZdS)
z&5Medical data parsing and enhancement6�N)�Dict)�clean_json_response�validate_parsed_data)�setup_logger�text�returnc�6�d|�d�}d}t|��D�]r}	|�|��}t|j��}t�d|dz�d���t
j|��}t|��r-t�d��t|��}|cSt�7d|dz�d�����#tj$rM}t�d|dz�d	|����||dz8krt|��cYd9}~cSYd10}~��d11}~wt$rM}t�d|dz�d|����||dz12krt|��cYd13}~cSYd14}~��ld15}~wwxYwt|��S)z3Parse extracted text to identify medical parametersz�16    Analyze this medical report text and extract structured information. This appears to be a comprehensive lab report.17 18    Text to analyze:19    a�20 21    Please provide a JSON response with exactly this structure:22    {23        "patient_info": {24            "name": "patient name if available, otherwise 'Not specified'",25            "age": "age if available, otherwise 'Not specified'",26            "gender": "gender if available, otherwise 'Not specified'",27            "report_date": "date if available, otherwise 'Not specified'",28            "lab_number": "lab number if available, otherwise 'Not specified'"29        },30        "test_categories": [31            {32                "category": "test category name (e.g., 'Complete Blood Count', 'Liver Function', 'Lipid Profile')",33                "tests": [34                    {35                        "test_name": "name of test",36                        "value": "measured value",37                        "unit": "unit of measurement",38                        "reference_range": "normal range",39                        "status": "normal/abnormal/high/low/borderline"40                    }41                ]42            }43        ],44        "abnormal_findings": [45            "list of abnormal test results with brief description"46        ],47        "critical_values": [48            "list of any critical or extremely abnormal values"49        ]50    }51 52    Important guidelines:53    - Look for common lab categories like CBC, Liver Panel, Kidney Panel, Lipid Screen, Thyroid Profile, HbA1c, Vitamins54    - Pay attention to numerical values and their units55    - Compare values to reference ranges when provided56    - If no test results are found, include an empty array for test_categories57    - Always include all required fields even if empty or "Not specified"58    - Only return valid JSON, no additional text or explanations59    �zAttempt �z - Parsing medical data...z.Successfully parsed and validated medical dataz& - Invalid data structure, retrying...z - JSON decode error: Nz - Error parsing medical data: )�range�generate_contentrr�logger�info�json�loadsr�enhance_test_status�warning�JSONDecodeError�error�create_fallback_structure�	Exception)	r�model�prompt�max_retries�attempt�response�	json_text�parsed_data�es	         �?C:\Users\HP\PycharmProjects\PythonProject16\core\data_parser.py�parse_medical_datar!
s��,�60�	,�,�,�F�\�K���%�%�7�7��	7��-�-�f�5�5�H�+�H�M�:�:�I��K�K�J�7�Q�;�J�J�J�K�K�K��*�Y�/�/�K�$�K�0�0�
_����L�M�M�M�1�+�>�>��"�"�"�"����]�'�A�+�]�]�]�^�^�^�^���#�	7�	7�	7��L�L�J�G�a�K�J�J�q�J�J�K�K�K��+��/�)�)�0��6�6�6�6�6�6�6�6�6�6�*�)�)�)�)������	7�	7�	7��L�L�S�G�a�K�S�S�PQ�S�S�T�T�T��+��/�)�)�0��6�6�6�6�6�6�6�6�6�6�*�)�)�)�)�����	7����61%�T�*�*�*s6�BC�6!C�F	�':D/�!F	�/
F	�<:F�6F	�F	rc��|�dg��D�]k}|�dg��D�]P}|�dd�����}|�dd��}	t|ttf��rt	|��}n;tjdt|����}|rt	|d��}n��d|vsd	|vr(|d62krd|d<d
|d<nv|dkrd|d<d|d<ned|d<n_d|vr,d|vr(|dkrd|d<d|d<nF|dkrd|d<d|d<n5d|d<n/d|vr+d|vr'|dkrd|d<d|d<n|dkrd|d<d|d<nd|d<��:#ttf$rY��NwxYw��m|S)z2Enhance test results with improved status analysis�test_categories�tests�	test_name��valuez[\d.]+r�hba1czglycosylated hemoglobing@�high�statuszSuggests diabetes�clinical_significanceg������@�63borderlinezPrediabetic range�normal�glucose�fasting�~zDiabetic range�dzImpaired fasting glucose�cholesterol�total��zHigh cardiovascular risk��zBorderline high)64�get�lower�65isinstance�int�float�re�findall�str�66ValueError�	TypeError)r�category�testr%�	value_strr'�numberss       r rr]s&���O�O�$5�r�:�:�1�1���L�L��"�-�-�0	�0	�D�����b�1�1�7�7�9�9�I�����"�-�-�I�+
��i�#�u��6�6�!�!�)�,�,�E�E�!�j��C�	�N�N�C�C�G��!� %�g�a�j� 1� 1��� ��i�'�'�+D�	�+Q�+Q���|�|�)/��X��8K��4�5�5��#���)5��X��8K��4�5�5�)1��X����)�+�+�	�Y�0F�0F���|�|�)/��X��8H��4�5�5��#���)5��X��8R��4�5�5�)1��X���"�i�/�/�G�y�4H�4H���|�|�)/��X��8R��4�5�5��#���)5��X��8I��4�5�5�)1��X�����	�*�
�
�
���
����_0	�d�s�2A&E*�BE*�*E?�>E?�
original_textc	��t�d��|�d��}g}d}d}d}|dd�D]�}d|vsd|vsd|vr>tjd	|��}|r'|�d67�����}d|vsd|vr/tjd
|��}|r|�d��dz}d|vsd|vr,tjd|��}	|	r|	�d��}��|D]e}d}68tj|69|��}|rJ|���\}}
}}|�|���|
|pd|pddd����f|||ddd�|rd|dd�d�gngdggd�S)z4Create a basic fallback structure when parsing failsz,Creating fallback structure for medical data�70z
Not specifiedN�zMr.zMrs.zMs.z!(Mr\.|Mrs\.|Ms\.)\s+([A-Za-z\s]+)r�Years�yearsz(\d+)\s+[Yy]earsrz years�Male�Femalez
(Male|Female)uR([A-Za-z\s]+):\s*([0-9.]+)\s*([A-Za-z/%]*)\s*\(?([0-9.-]+\s*[-–]\s*[0-9.-]+)?\)?r&�unknown)r%r'�unit�reference_ranger*)�name�age�gender�report_date�71lab_numberz
General Tests�)r@r$zLUnable to automatically detect abnormal findings - manual review recommended)�patient_infor#�abnormal_findings�critical_values)	rr�splitr;�search�group�strip�groups�append)rD�lines�potential_tests�patient_name�patient_age�patient_gender�line�72name_match�	age_match�gender_match�test_pattern�matchr%r'rM�	ref_ranges                r rr�s��73�K�K�>�?�?�?�
����%�%�E��O�"�L�!�K�$�N��c�r�c�74�7�7���D�=�=�F�d�N�N�e�t�m�m���#G��N�N�J��
;�)�/�/��2�2�8�8�:�:���d�?�?�g��o�o��	�"5�t�<�<�I��
<�'�o�o�a�0�0�8�;���T�>�>�X��-�-��9�%5�t�<�<�L��
7�!-�!3�!3�A�!6�!6�������l���	�,��-�-���	�05�����-�I�u�d�I��"�"�&�_�_�.�.���75��#,�#?��#�$�$�
�
�
��!��$�*�)�76�77��&�'�$�S�b�S�)�78�79���$&�l�m�����)�__doc__rr;�typingr�utils.json_parserrr�utils.loggerr�__name__rr=r!rr�rjr �<module>rqs���������	�	�	�	�������G�G�G�G�G�G�G�G�%�%�%�%�%�%�	��h�	�	��M+�S�M+�D�M+�M+�M+�M+�`5�T�5�d�5�5�5�5�p9�S�9�T�9�9�9�9�9�9rj