Appendix B: Testing the API - AI-Powered Health Data Masking

Appendix B: Testing the API

The AI-Powered Health Data Masking API receives images and text as input. Before testing the API, you must complete Step 2: Create an IAM Policy to Access the API and attach the policy to an IAM role.

Use the following procedures to use the API image and text masking capabilities:

Note

Verify that the IAM role you assume has an attached IAM policy that restricts the image and text masking functions.

Mask Text

Use the following procedure to test the solution API text masking capability:

  1. In the AWS CloudFormation console, select the deployed solution stack.

  2. In the Outputs tab, copy the ApiGatewayId and the TextMaskResourceId.

    Using a portion of a fictional medical note provided by the Amazon Comprehend Medical team, you can test the solution API for masking text using AWS CLI or your AWS SDK of choice.

    PERSON INFORMATION Name: SALAZAR, CARLOS MRN: RQ36114734 ED Arrival Time: 11/12/2011 18:15 Sex: Male DOB: 2/11/1961

    The example POST request using the example note above, will de-identify the request message and look like the following: {"text": "PERSON INFORMATION\nName: SALAZAR, CARLOS\nMRN: RQ36114734\nED Arrival Time: 11/12/2011 18:15\nSex: Male\nDOB: 2/11/1961"}

  3. Open a Python terminal and paste in the following code sample. You must update the api_id and resource_id you copied from the Outputs tab above.

    import boto3 import json client = boto3.client('apigateway') api_id = YOUR_API_ID resource_id = YOUR_RESOURCE_ID payload = {"text": "PERSON INFORMATION\nName: SALAZAR, CARLOS\nMRN: RQ36114734\nED Arrival Time: 11/12/2011 18:15\nSex: Male\nDOB: 2/11/1961"} response = client.test_invoke_method( restApiId=api_id, resourceId=resource_id, httpMethod='POST', headers={"Content-Type": "application/json"}, body=json.dumps(payload) ) print(response['body'])

    If the masking was completed successfully, you will see the following message:

    {"maskedText": "PERSON INFORMATION\nName: NAME\nMRN: ID\nADDRESS Arrival Time: DATE 18:15\nSex: Male\nDOB: DATE"}

Mask Image

Use the following procedure to test the solution API image masking capability.

  1. Download the example chest x-ray image from a dataset made available by the NIH Clinical Center, and upload the image into the created Amazon S3 bucket.

  2. Copy the Amazon S3 bucket name and key.

  3. In the Outputs tab, copy the ApiGatewayId and the ImageMaskResourceId.

  4. Open a Python terminal and paste in the following code sample. Note that you must update the api_id and resource_id you copied from the Outputs tab above, and the S3 bucket name and key.

    import boto3 import json client = boto3.client('apigateway') api_id = YOUR_API_ID resource_id = YOUR_RESOURCE_ID s3_bucket = YOUR_S3_IMAGE_BUCKET s3_key = YOUR_S3_IMAGE_KEY destination_key = 'masked/' + s3_key payload = {"s3Bucket": s3_bucket, "s3Key": s3_key, "destinationBucket": s3_bucket, "destinationKey": destination_key} response = client.test_invoke_method( restApiId=api_id, resourceId=resource_id, httpMethod='POST', headers={"Content-Type": "application/json"}, body=json.dumps(payload) ) print(response['body'])
  5. Once the image is successfully masked, navigate to the response path to view the image. The path will be returned in the JSON document (response['body']).