File: sample_match_trials_structured_coded_elements.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (125 lines) | stat: -rw-r--r-- 6,402 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import os
import datetime

from azure.core.credentials import AzureKeyCredential
from azure.healthinsights.clinicalmatching import ClinicalMatchingClient, models

"""
FILE: sample_match_trials_structured_coded_elements.py

DESCRIPTION:
    Finding potential eligible trials for a patient, based on patient’s structured medical information.
    It uses **SYNC** function unlike other samples that uses async function.

    Trial Matcher model matches a single patient to a set of relevant clinical trials,
    that this patient appears to be qualified for. This use case will demonstrate:
    a. How to use the trial matcher when patient clinical health information is provided to the
    Trial Matcher in a key-value structure with coded elements.
    b. How to use the clinical trial configuration to narrow down the trial condition,
    recruitment status, location and other criteria that the service users may choose to prioritize.


USAGE:
    python sample_match_trials_structured_coded_elements.py

    Set the environment variables with your own values before running the sample:
    1) HEALTHINSIGHTS_KEY - your source from Health Insights API key.
    2) HEALTHINSIGHTS_ENDPOINT - the endpoint to your source Health Insights resource.
"""


class HealthInsightsSamples:
    def match_trials(self):
        KEY = os.environ["HEALTHINSIGHTS_KEY"]
        ENDPOINT = os.environ["HEALTHINSIGHTS_ENDPOINT"]

        # Create a Trial Matcher client
        # <client>
        trial_matcher_client = ClinicalMatchingClient(endpoint=ENDPOINT,
                                                      credential=AzureKeyCredential(KEY))
        # </client>

        # Create clinical info list
        # <clinicalInfo>
        clinical_info_list = [models.ClinicalCodedElement(system="http://www.nlm.nih.gov/research/umls",
                                                          code="C0032181",
                                                          name="Platelet count",
                                                          value="250000"),
                              models.ClinicalCodedElement(system="http://www.nlm.nih.gov/research/umls",
                                                          code="C0002965",
                                                          name="Unstable Angina",
                                                          value="true"),
                              models.ClinicalCodedElement(system="http://www.nlm.nih.gov/research/umls",
                                                          code="C1522449",
                                                          name="Radiotherapy",
                                                          value="false"),
                              models.ClinicalCodedElement(system="http://www.nlm.nih.gov/research/umls",
                                                          code="C0242957",
                                                          name="GeneOrProtein-Expression",
                                                          value="Negative;EntityType:GENEORPROTEIN-EXPRESSION"),
                              models.ClinicalCodedElement(system="http://www.nlm.nih.gov/research/umls",
                                                          code="C1300072",
                                                          name="cancer stage",
                                                          value="2")]

        # </clinicalInfo>

        # Construct Patient
        # <PatientConstructor>
        patient_info = models.PatientInfo(sex=models.PatientInfoSex.MALE, birth_date=datetime.date(1965, 12, 26),
                                          clinical_info=clinical_info_list)
        patient1 = models.PatientRecord(id="patient_id", info=patient_info)
        # </PatientConstructor>

        # Create registry filter
        registry_filters = models.ClinicalTrialRegistryFilter()
        # Limit the trial to a specific patient condition ("Non-small cell lung cancer")
        registry_filters.conditions = ["non small cell lung cancer (nsclc)"]
        # Specify the clinical trial registry source as ClinicalTrials.Gov
        registry_filters.sources = [models.ClinicalTrialSource.CLINICALTRIALS_GOV]
        # Limit the clinical trial to a certain location, in this case California, USA
        registry_filters.facility_locations = [
            models.GeographicLocation(country_or_region="United States", city="Gilbert", state="Arizona")]
        # Limit the trial to a specific recruitment status
        registry_filters.recruitment_statuses = [models.ClinicalTrialRecruitmentStatus.RECRUITING]

        # Construct ClinicalTrial instance and attach the registry filter to it.
        clinical_trials = models.ClinicalTrials(registry_filters=[registry_filters])

        # Create TrialMatcherRequest
        configuration = models.TrialMatcherModelConfiguration(clinical_trials=clinical_trials)
        trial_matcher_data = models.TrialMatcherData(patients=[patient1], configuration=configuration)

        # Health Insights Trial match trials
        try:
            poller = trial_matcher_client.begin_match_trials(trial_matcher_data)
            trial_matcher_result = poller.result()
            self.print_results(trial_matcher_result)
        except Exception as ex:
            print(str(ex))
            return

    # print match trials (eligible/ineligible)
    @staticmethod
    def print_results(trial_matcher_result):
        if trial_matcher_result.status == models.JobStatus.SUCCEEDED:
            tm_results = trial_matcher_result.results
            for patient_result in tm_results.patients:
                print(f"Inferences of Patient {patient_result.id}")
                for tm_inferences in patient_result.inferences:
                    print(f"Trial Id {tm_inferences.id}")
                    print(f"Type: {str(tm_inferences.type)}  Value: {tm_inferences.value}")
                    print(f"Description {tm_inferences.description}")
        else:
            tm_errors = trial_matcher_result.errors
            if tm_errors is not None:
                for error in tm_errors:
                    print(f"{error.code} : {error.message}")


if __name__ == "__main__":
    sample = HealthInsightsSamples()
    sample.match_trials()