-
Notifications
You must be signed in to change notification settings - Fork 35
feat: add ClinicalImpression helper and enhance Observation categorization #194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DinhLucent
wants to merge
2
commits into
dotimplement:main
Choose a base branch
from
DinhLucent:feat/clinical-impression-and-obs-category
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -145,6 +145,7 @@ def create_value_quantity_observation( | |
| unit: str, | ||
| status: str = "final", | ||
| subject: Optional[str] = None, | ||
| category: Optional[str] = None, | ||
| system: str = "http://loinc.org", | ||
| display: Optional[str] = None, | ||
| effective_datetime: Optional[str] = None, | ||
|
|
@@ -159,6 +160,7 @@ def create_value_quantity_observation( | |
| code: REQUIRED. The observation code (e.g., LOINC code for the measurement) | ||
| value: The numeric value of the observation | ||
| unit: The unit of measure (e.g., "beats/min", "mg/dL") | ||
| category: Optional category code (e.g., "vital-signs", "laboratory") | ||
| system: The code system for the observation code (default: LOINC) | ||
| display: The display name for the observation code | ||
| effective_datetime: When the observation was made (ISO format). Uses current time if not provided. | ||
|
|
@@ -168,22 +170,35 @@ def create_value_quantity_observation( | |
| Observation: A FHIR Observation resource with an auto-generated ID prefixed with 'hc-' | ||
| """ | ||
| if not effective_datetime: | ||
| effective_datetime = datetime.datetime.now(datetime.timezone.utc).strftime( | ||
| "%Y-%m-%dT%H:%M:%S%z" | ||
| ) | ||
| effective_datetime = datetime.datetime.now(datetime.timezone.utc).isoformat() | ||
|
|
||
| subject_ref = Reference(reference=subject) if subject is not None else None | ||
| subject_ref = None | ||
| if subject is not None: | ||
| subject_ref = ReferenceClass(reference=subject) | ||
|
|
||
| return Observation( | ||
| id=_generate_id(), | ||
| status=status, | ||
| code=create_single_codeable_concept(code, display, system), | ||
| subject=subject_ref, | ||
| effectiveDateTime=effective_datetime, | ||
| valueQuantity=Quantity( | ||
| observation_data = { | ||
| "id": _generate_id(), | ||
| "status": status, | ||
| "code": create_single_codeable_concept(code, display, system, version), | ||
| "subject": subject_ref, | ||
| "effectiveDateTime": effective_datetime, | ||
| "valueQuantity": Quantity( | ||
| value=value, unit=unit, system="http://unitsofmeasure.org", code=unit | ||
| ), | ||
| ) | ||
| } | ||
|
|
||
| if category: | ||
| observation_data["category"] = [ | ||
| create_single_codeable_concept( | ||
| code=category, | ||
| display=category.replace("-", " ").capitalize(), | ||
| system="http://terminology.hl7.org/CodeSystem/observation-category", | ||
| version=version, | ||
| ) | ||
| ] | ||
|
|
||
| observation = Observation(**observation_data) | ||
|
|
||
|
|
||
|
|
||
| def create_patient( | ||
|
|
@@ -567,3 +582,90 @@ def add_coding_to_codeable_concept( | |
| codeable_concept.coding.append(CodingCls(system=system, code=code, display=display)) | ||
|
|
||
| return codeable_concept | ||
|
|
||
|
|
||
| def create_clinical_impression( | ||
| subject: str, | ||
| status: str = "completed", | ||
| description: Optional[str] = None, | ||
| date: Optional[str] = None, | ||
| version: Optional[Union["FHIRVersion", str]] = None, | ||
| ) -> Any: | ||
| """ | ||
| Create a minimal FHIR ClinicalImpression resource. | ||
| Useful for recording an AI agent's assessment or diagnostic conclusion. | ||
| https://hl7.org/fhir/clinicalimpression.html | ||
|
|
||
| Args: | ||
| subject: REQUIRED. Reference to the patient (e.g. "Patient/123") | ||
| status: REQUIRED. Status of the impression (default: "completed") | ||
| description: Optional text describing the assessment | ||
| date: When the assessment was made. Uses current time if not provided. | ||
| version: FHIR version to use (e.g., "R4B", "STU3"). Defaults to current default. | ||
|
|
||
| Returns: | ||
| ClinicalImpression: A FHIR ClinicalImpression resource with an auto-generated ID. | ||
| """ | ||
| from healthchain.fhir.version import get_fhir_resource | ||
|
|
||
| ClinicalImpression = get_fhir_resource("ClinicalImpression", version) | ||
| ReferenceClass = get_fhir_resource("Reference", version) | ||
|
|
||
| if not date: | ||
| date = datetime.datetime.now(datetime.timezone.utc).isoformat() | ||
|
|
||
|
|
||
| impression_data = { | ||
| "id": _generate_id(), | ||
| "status": status, | ||
| "subject": ReferenceClass(reference=subject), | ||
| "date": date, | ||
| } | ||
|
|
||
| if description: | ||
| impression_data["description"] = description | ||
|
|
||
| return ClinicalImpression(**impression_data) | ||
|
|
||
|
|
||
| def add_finding_to_clinical_impression( | ||
| impression: Any, | ||
| item_code: str, | ||
| item_display: Optional[str] = None, | ||
| item_system: str = "http://snomed.info/sct", | ||
| basis: Optional[str] = None, | ||
| version: Optional[Union["FHIRVersion", str]] = None, | ||
| ) -> Any: | ||
| """Add a finding to an existing ClinicalImpression. | ||
|
|
||
| Args: | ||
| impression: The ClinicalImpression resource to modify | ||
| item_code: Code for the finding | ||
| item_display: Display name for the finding | ||
| item_system: Code system (default: SNOMED CT) | ||
| basis: Optional text describing the basis for the finding | ||
| version: FHIR version to use. | ||
|
|
||
| Returns: | ||
| ClinicalImpression: The modified resource | ||
| """ | ||
| from healthchain.fhir.version import get_resource_version | ||
|
|
||
| if version is None: | ||
| version = get_resource_version(impression) | ||
|
|
||
| finding = { | ||
| "itemCodeableConcept": create_single_codeable_concept( | ||
| item_code, item_display, item_system, version | ||
| ) | ||
| } | ||
| if basis: | ||
| finding["basis"] = basis | ||
|
|
||
| if not impression.finding: | ||
| impression.finding = [] | ||
|
|
||
| impression.finding.append(finding) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we are appending a raw dict, it would be better to import from Fhir.resources and use ClinicalImpressionFinding(**finding). |
||
| return impression | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Union isn't imported earlier in the file.