Skip to content

Commit

Permalink
Fixed procedure dates (#14)
Browse files Browse the repository at this point in the history
# Fixing procedure dates

## ♻️ Current situation & Problem
Not all procedures had dates being extracted from the JSON resources.
Without all of the dates being sent to the LLM, it cannot understand the
full context and timeline of the patients procedures.


## ⚙️ Release Notes 
The only change in this PR was made in
[FHIRResource.swift](2354244).
The modification ensures that the end date of the procedure is taken in
each of the resources (which is consistent between DSTU2 and R4).

```
if case let .period(period) = procedure.performed {
    return try? period.end?.value?.asNSDate()
}
```
## 📝 Code of Conduct & Contributing Guidelines 

By submitting creating this pull request, you agree to follow our [Code
of
Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md):
- [x] I agree to follow the [Code of
Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md).

---------

Co-authored-by: Paul Schmiedmayer <[email protected]>
  • Loading branch information
AdritRao and PSchmiedmayer authored Dec 11, 2023
1 parent d60882b commit 07c2e56
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 22 deletions.
28 changes: 28 additions & 0 deletions Sources/SpeziFHIR/Extensions/ConditionOnsetXR4+Date.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// This source file is part of the Stanford Spezi open source project
//
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md)
//
// SPDX-License-Identifier: MIT
//

import Foundation
import ModelsR4


extension Condition.OnsetX {
var date: Date? {
switch self {
case let .dateTime(date):
return try? date.value?.asNSDate()
case let .period(period):
return period.date
case .age:
return nil
case .range:
return nil
case .string:
return nil
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// This source file is part of the Stanford Spezi open source project
//
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md)
//
// SPDX-License-Identifier: MIT
//

import Foundation
import ModelsR4


extension DiagnosticReport.EffectiveX {
var date: Date? {
switch self {
case let .dateTime(date):
return try? date.value?.asNSDate()
case let .period(period):
return period.date
}
}
}
22 changes: 22 additions & 0 deletions Sources/SpeziFHIR/Extensions/ImmunizationOccurrenceXR4+Date.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// This source file is part of the Stanford Spezi open source project
//
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md)
//
// SPDX-License-Identifier: MIT
//

import Foundation
import ModelsR4


extension Immunization.OccurrenceX {
var date: Date? {
switch self {
case let .dateTime(date):
return try? date.value?.asNSDate()
case .string:
return nil
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// This source file is part of the Stanford Spezi open source project
//
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md)
//
// SPDX-License-Identifier: MIT
//

import Foundation
import ModelsR4


extension Period {
var date: Date? {
if let endDate = try? end?.value?.asNSDate() {
return endDate
}
if let startDate = try? start?.value?.asNSDate() {
return startDate
}
return nil
}
}
22 changes: 22 additions & 0 deletions Sources/SpeziFHIR/Extensions/PeriodR4+Date.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// This source file is part of the Stanford Spezi open source project
//
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md)
//
// SPDX-License-Identifier: MIT
//

import Foundation
import ModelsR4


extension MedicationAdministration.EffectiveX {
var date: Date? {
switch self {
case let .dateTime(date):
return try? date.value?.asNSDate()
case let .period(period):
return period.date
}
}
}
28 changes: 28 additions & 0 deletions Sources/SpeziFHIR/Extensions/ProcedurePerformedXR4+Date.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// This source file is part of the Stanford Spezi open source project
//
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md)
//
// SPDX-License-Identifier: MIT
//

import Foundation
import ModelsR4


extension Procedure.PerformedX {
var date: Date? {
switch self {
case let .dateTime(date):
return try? date.value?.asNSDate()
case let .period(period):
return period.date
case .age:
return nil
case .range:
return nil
case .string:
return nil
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// This source file is part of the Stanford Spezi open source project
//
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md)
//
// SPDX-License-Identifier: MIT
//

import Foundation
import ModelsR4


extension SupplyDelivery.OccurrenceX {
var date: Date? {
switch self {
case let .dateTime(date):
return try? date.value?.asNSDate()
case let .period(period):
return period.date
case .timing:
return nil
}
}
}
63 changes: 42 additions & 21 deletions Sources/SpeziFHIR/FHIRResource/FHIRResource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,34 +55,40 @@ public struct FHIRResource: Sendable, Identifiable, Hashable {
switch versionedResource {
case let .r4(resource):
switch resource {
case let carePlan as ModelsR4.CarePlan:
return carePlan.period?.date
case let careTeam as ModelsR4.CareTeam:
return careTeam.period?.date
case let claim as ModelsR4.Claim:
return try? claim.billablePeriod?.end?.value?.asNSDate()
case let condition as ModelsR4.Condition:
guard case let .dateTime(date) = condition.onset else {
return nil
}
return try? date.value?.asNSDate()
return condition.onset?.date
case let device as ModelsR4.Device:
return try? device.manufactureDate?.value?.asNSDate()
case let diagnosticReport as ModelsR4.DiagnosticReport:
guard case let .dateTime(date) = diagnosticReport.effective else {
return nil
}
return try? date.value?.asNSDate()
return diagnosticReport.effective?.date
case let documentReference as ModelsR4.DocumentReference:
return try? documentReference.date?.value?.asNSDate()
case let encounter as ModelsR4.Encounter:
return try? encounter.period?.end?.value?.asNSDate()
case let explanationOfBenefit as ModelsR4.ExplanationOfBenefit:
return try? explanationOfBenefit.billablePeriod?.end?.value?.asNSDate()
case let immunization as ModelsR4.Immunization:
guard case let .dateTime(date) = immunization.occurrence else {
return nil
}
return try? date.value?.asNSDate()
return immunization.occurrence.date
case let medicationRequest as ModelsR4.MedicationRequest:
return try? medicationRequest.authoredOn?.value?.asNSDate()
case let medicationAdministration as ModelsR4.MedicationAdministration:
return medicationAdministration.effective.date
case let observation as ModelsR4.Observation:
return try? observation.issued?.value?.asNSDate()
case let procedure as ModelsR4.Procedure:
guard case let .dateTime(date) = procedure.performed else {
return nil
}
return try? date.value?.asNSDate()
case is ModelsR4.Patient:
return .now
return procedure.performed?.date
case let patient as ModelsR4.Patient:
return try? patient.birthDate?.value?.asNSDate()
case let provenance as ModelsR4.Provenance:
return try? provenance.recorded.value?.asNSDate()
case let supplyDelivery as ModelsR4.SupplyDelivery:
return supplyDelivery.occurrence?.date
default:
return nil
}
Expand All @@ -92,16 +98,31 @@ public struct FHIRResource: Sendable, Identifiable, Hashable {
return try? observation.issued?.value?.asNSDate()
case let medicationOrder as ModelsDSTU2.MedicationOrder:
return try? medicationOrder.dateWritten?.value?.asNSDate()
case let medicationStatement as ModelsDSTU2.MedicationStatement:
guard case let .dateTime(date) = medicationStatement.effective else {
return nil
}
return try? date.value?.asNSDate()
case let condition as ModelsDSTU2.Condition:
guard case let .dateTime(date) = condition.onset else {
return nil
}
return try? date.value?.asNSDate()
case let procedure as ModelsDSTU2.Procedure:
guard case let .dateTime(date) = procedure.performed else {
return nil
switch procedure.performed {
case let .dateTime(date):
if let date = try? date.value?.asNSDate() {
return date
}
case let .period(period):
if let date = try? period.end?.value?.asNSDate() {
return date
}
default:
break
}
return try? date.value?.asNSDate()

return nil
default:
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Your task is to private a title and compact summary for an FHIR resource from the user's clinical record. You should provide the title and summary in the following locale: {{LOCALE}}.\n\nYour response should contain two lines without headings, markdown formatting, or any other structure beyond two lines. Directly provide the content without any additional structure or an introduction. Another computer program will parse the output.\n\n1. Line: A 1-5 Word summary of the FHIR resource that immediately identifies the resource and provides the essential information at a glance. Do NOT use a complete sentence; instead, use a formatting typically used for titles in computer systems.\n\n2. Line: Provide a short one-sentence summary of the resource in less than 40 words. Ensure that the summary only focuses on the essential information that a patient would need and, e.g., excludes the person who prescribed a medication or similar metadata. Ensure that all clinically relevant data is included, which allows us to use the summary in additional prompts where using the complete JSON might not be feasible.\n\nThe following JSON representation defines the FHIR resource that you should provide a title and summary for:\n\n{{FHIR_RESOURCE}}"
"value" : "Your task is to private a title and compact summary for an FHIR resource from the user's clinical record. You should provide the title and summary in the following locale: {{LOCALE}}.\n\nYour response should contain two lines without headings, markdown formatting, or any other structure beyond two lines. Directly provide the content without any additional structure or an introduction. Another computer program will parse the output.\n\n1. Line: A 1-5 Word summary of the FHIR resource that immediately identifies the resource and provides the essential information at a glance. Do NOT use a complete sentence; instead, use a formatting typically used for titles in computer systems.\n\n2. Line: Provide a short one-sentence summary of the resource in less than 100 words. Ensure that the summary only focuses on the essential information that a patient would need and, e.g., excludes the person who prescribed a medication or similar metadata. Ensure that all clinically relevant data is included, which allows us to use the summary in additional prompts where using the complete JSON might not be feasible.\n\nThe following JSON representation defines the FHIR resource that you should provide a title and summary for:\n\n{{FHIR_RESOURCE}}"
}
}
}
Expand Down

0 comments on commit 07c2e56

Please sign in to comment.