AMAZONCM.jpg
profile picture Pradhyumna N Holla
6 min read Aug 10, 2022

AWS Machine Learning - Extract Medical Insights with Amazon Comprehend Medical

Have you ever stared at a doctor’s prescription and wondered if it was shorter? Have you ever seen a lengthy medical report and thought it would be easier if you got to know the essential parts without reading through all the pages?

Well, Amazon Comprehend Medical service is the answer for you.

INTERESTING, WHAT IS IT ACTUALLY?

Amazon Comprehend Medical(ACM) is a HIPAA-eligible Natural Language Processing (NLP) service that uses machine learning to extract health data from the medical text – no machine learning experience is required.

ACM detects and returns valuable information in unstructured clinical text such as physician’s notes, discharge summaries, test results, and case notes. This service (ACM) uses natural language processing (NLP) models to detect entities, which are textual references to medical information such as medical conditions, medications, or Protected Health Information(PHI).

So all you need to do is to enter the text as input, and BOOM! You’ll get the results you’re looking for in a simplified way.

WHAT ARE ITS USE CASES?

Patient Case Management and Outcome– Doctors and healthcare providers can manage and easily access medical information that doesn’t fit into traditional forms. By analysing case notes, providers can identify candidates for early screening of medical conditions before the condition becomes more difficult and expensive to treat.

Clinical Research– Life sciences and research organisations can optimise the matching process for enrolling patients into clinical trials. For instance, it can be easier and more effective to monitor how patients respond to certain therapies by analysing their narratives.

Medical Billing and Healthcare Revenue Cycle Management– Customers can expand their analytics to include unstructured documents such as clinical notes. ACM uses the latest advances in NLP to analyse clinical text, help decrease time to revenue and improve reimbursement accuracy.

Ontology Linking– Ontology linking features detect entities from clinical text and link those entities to standardised concepts in common medical ontologies.

SERVICES OFFERED:

The InferICD10CM, InferSNOMEDCT, and InferRxNorm operations detect potential medical conditions and medications and link them to codes in the knowledge bases. You can use ontology linking batch analysis to analyse a collection of documents or a single large document.

detect_entities_v2- Inspects the clinical text for various medical entities and returns specific information about them, such as entity category, location, and confidence score on that information.

infer_icd10_cm - InferICD10CM detects medical conditions as entities listed in a patient record and links those entities to normalised concept identifiers in the ICD-10-CM knowledge base.

infer_rx_norm - InferRxNorm detects medications as entities listed in a patient record and links to the normalised concept identifiers in the RxNorm database from the National Library of Medicine.

infer_snomedct - InferSNOMEDCT detects possible medical concepts as entities. Links them to codes from the Systematized Nomenclature of Medicine, Clinical Terms (SNOMED-CT) ontology

detect_phi - Inspects the clinical text for protected health information (PHI) entities. Returns the entity category, location, and confidence score for each entity.

Okay, enough of the boring part already! How do I actually use it?!

USING IT ON CONSOLE:

Screenshot 2022-07-18 at 12.58.42 PM.png

Once the text is entered, the keywords are highlighted and shown like this:

Screenshot 2022-07-18 at 1.29.39 PM.png Screenshot 2022-07-18 at 3.21.30 PM.png Some results can even be expanded: Screenshot 2022-07-18 at 3.23.49 PM.png

You can also select a different service:

Screenshot 2022-07-21 at 5.34.25 PM.png

The respective medical codes are matched with a confidence score:

Screenshot 2022-07-21 at 5.51.35 PM.png

USING ACM WITH LAMBDA:

Services Used:

  • API Gateway
  • AWS Lambda
  • ACM(As API)

CREATE AN API AND A LAMBDA FUNCTION:

  • Go to API Gateway on AWS Console and create an HTTP or a REST API.
  • Create a POST method.
  • Create a Lambda Function with Python as its runtime.
  • Go to the Lambda Function’s permissions and attach ComprehendMedicalFullAccess policy to the execution role.
  • Add the API that is created as a trigger to the Lambda.

The code snippet for the Lambda Function is given below.

import boto3
import json

client = boto3.client('comprehendmedical')

def lambda_handler(event, context):
    
    data = event["body"]
    data = json.loads(data)
    
    option = data["choice"]
    text = data["body"]

    if option == 'Detect Entities':
        result = client.detect_entities_v2(Text=text)
        entities = result['Entities']
        return entities

    elif option == 'RXNorm':
        result = client.infer_rx_norm(Text=text)
        entities = result['Entities']
        return entities

    elif option == 'ICD-10-CM':
        result = client.infer_icd10_cm(Text=text)
        entities = result['Entities']
        return entities

    elif option == 'SNOMED CT':
        result = client.infer_snomedct(Text=text)
        entities = result['Entities']
        return entities
        
    elif option == 'Detect PHI':
        result = client.detect_phi(Text=text)
        entities = result['Entities']
        return entities

Okay, but how do I send the inputs? In what format am I supposed to send it?

It’s quite simple. Inputs should be sent in JSON to the API Endpoint created using API Gateway.

{
   "body":"Rahul Kumar is a 24 year old male with fever. He has headaches, nausea and joint pain. He has been prescribed with Dolo 650 twice a day after consuming food.",
   "choice":"Detect PHI"
}

Response:

[
   {
       "Id": 1,
       "BeginOffset": 0,
       "EndOffset": 11,
       "Score": 0.9994426369667053,
       "Text": "Rahul Kumar",
       "Category": "PROTECTED_HEALTH_INFORMATION",
       "Type": "NAME",
       "Traits": []
   },
   {
       "Id": 2,
       "BeginOffset": 17,
       "EndOffset": 19,
       "Score": 0.99980229139328,
       "Text": "24",
       "Category": "PROTECTED_HEALTH_INFORMATION",
       "Type": "AGE",
       "Traits": []
   }
]

Here’s another response if the RXNorm choice is selected:

[
   {
       "Id": 0,
       "Text": "Dolo",
       "Category": "MEDICATION",
       "Type": "BRAND_NAME",
       "Score": 0.9879279732704163,
       "BeginOffset": 115,
       "EndOffset": 119,
       "Attributes": [
           {
               "Type": "DOSAGE",
               "Score": 0.8067280054092407,
               "RelationshipScore": 0.9999293088912964,
               "Id": 1,
               "BeginOffset": 120,
               "EndOffset": 123,
               "Text": "650",
               "Traits": []
           },
           {
               "Type": "FREQUENCY",
               "Score": 0.968130886554718,
               "RelationshipScore": 0.9999997615814209,
               "Id": 2,
               "BeginOffset": 124,
               "EndOffset": 135,
               "Text": "twice a day",
               "Traits": []
           }
       ],
       "Traits": [],
       "RxNormConcepts": [
           {
               "Description": "Doxy",
               "Code": "544837",
               "Score": 0.7806317806243896
           },
           {
               "Description": "Qdolo",
               "Code": "2395809",
               "Score": 0.37000608444213867
           },
           {
               "Description": "24 HR diltiazem hydrochloride 120 MG Extended Release Oral Capsule [Dilt]",
               "Code": "831215",
               "Score": 0.12268374115228653
           },
           {
               "Description": "hydrocortisone 250 MG Injection [Solu-Cortef]",
               "Code": "1738592",
               "Score": 0.10417662560939789
           },
           {
               "Description": "levonorgestrel 1.5 MG Oral Tablet [Fallback Solo]",
               "Code": "1543735",
               "Score": 0.10047975927591324
           }
       ]
   }
]

Once the request is sent, Comprehend Medical returns the entities it detects based on the method that is called. Later this data (Which is in a JSON Form) can be parsed and presented to the user or stored in a database.

PRICING

ACM offers a free tier covering 85k units of text (8.5M characters, or ~1000 5-page 1700-character per page documents) for the first month you start using the service for any of the APIs.

Screenshot 2022-08-10 at 5.48.22 PM.png

CONCLUSION:

With very reasonable pricing and having a required little to no practical experience in Machine Learning, ACM is inarguably one of the most beneficial services offered by AWS.

I hope this blog provided you with a good idea about ACM. I’ve also built a basic Web Application around this service. Please go to the attached repo link for the code and execution details.

That’s a wrap guys! Cheers for reading! You can connect with me on Twitter or LinkedIn if you have any feedback!

LINKS:

Application Modernization Icon

Innovate faster, and go farther with serverless-native application development. Explore limitless possibilities with AntStack's serverless solutions. Empowering your business to achieve your most audacious goals.

Talk to us

Author(s)

Tags

Your Digital Journey deserves a great story.

Build one with us.

Recommended Blogs

Cookies Icon

These cookies are used to collect information about how you interact with this website and allow us to remember you. We use this information in order to improve and customize your browsing experience and for analytics and metrics about our visitors on this website.

If you decline, your information won’t be tracked when you visit this website. A single cookie will be used in your browser to remember your preference not to be tracked.

Talk to us