File size: 4,877 Bytes
ca302ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# standard libraries
import os

# third party libraries
import gradio as gr
import validators
from validators import ValidationFailure
import gspread
from google.oauth2.service_account import Credentials 
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

import etl
from generic import matched_super_generic

cred_filename = 'heliumhealth-a05d595e5991.json'
# cred_file_path = os.path.join('credentials', cred_filename)

# google auth scopes
scopes = ['https://www.googleapis.com/auth/spreadsheets',
          'https://www.googleapis.com/auth/drive']

# create credentials 
credentials = Credentials.from_service_account_file(
    cred_filename, scopes=scopes )

# authorize google spreadsheet
gc = gspread.authorize(credentials)

gauth = GoogleAuth()
drive = GoogleDrive(gauth)


def prescription_quality_check(gs, sheet_type):
    """
    - Loads, transforms prescription data 
    - Performs RX Norm check for prescription data 

    Args:
        gs (Object): authenticated google sheet instance
        sheet_type (str): sheet type always: prescription
    """
    # load data from sheet
    pres_data, super_data = etl.load_data(gs, sheet_type)

    # process data for use
    pres_data, super_data = etl.preprocess_data(pres_data, super_data, sheet_type)

    # run the matching algorithm
    pres_data = pres_data.apply(matched_super_generic, 
                                axis=1, 
                                args=[
                                    super_data, 
                                    'RX Norm [Super Generic]',
                                    'SUPER_GENERIC', 
                                    'match'])

    # post process data
    pres_data = pres_data[['Unstructured Name', 'RX Norm [Super Generic]', 'match', 'Number']]
    
    # write output to sheet 
    etl.output_data(gs, pres_data, sheet_type)


def diagnosis_quality_check(gs, sheet_type):
    """
    - Loads, transforms prescription data 
    - Performs RX Norm check for prescription data 

    Args:
        gs (Object): google-sheet instance 
        sheet_type (str): sheet type always diagnosis
    """
    # load data from sheet
    diag_data, super_data = etl.load_data(gs, sheet_type)
    # process data for use
    diag_data, super_data = etl.preprocess_data(diag_data, super_data, sheet_type)
    
    
    # run the matching algorithm
    diag_data = diag_data.apply(matched_super_generic, 
                                axis=1, 
                                args=[
                                    super_data, 
                                    'ICD10 Diagnosis',
                                    'Name', 
                                    'match'])

    # post processing 
    diag_data = diag_data[['Unstructured Name', 'ICD10 Diagnosis', 'match', 'Number']]

    # write output to sheet 
    etl.output_data(gs, diag_data, sheet_type)

def quality_check_main(sheet_type, sheet_url):
    """
   main check function

    Args:
        sheet_type (str): sheet type either: prescription or diagnosis 
        sheet_url (url): url to data sheet

    Raises:
        gr.exceptions.Error: Value error for invalid urls 

    Returns:
        message (str): success or failure messages.
    """
    result = validators.url(sheet_url)

    # open the google sheet for reading 
    gs = gc.open_by_url(sheet_url)
    
    # Check if the input is valid url
    # ToDO: Error message displayed is not explanatory - Fix it 
    if isinstance(result, ValidationFailure):
        raise gr.exceptions.Error(message="Please enter a valid URL")
    
    if sheet_type == 'diagnosis':
        diagnosis_quality_check(gs, sheet_type)
    elif sheet_type == 'prescription':
        prescription_quality_check(gs, sheet_type)

    output_text = f"{sheet_type} Q/A successfully. Please check the result sheet for output"
    return output_text



with gr.Blocks() as demo:
  gr.Markdown(
      """
      # Quality Assurance App

      ## Instructions

      * Add IAM User to sheet you want to test
      * Gsheet tabs required for diagnosis:  
        * Diagnosis, ICD10 Diagnosis, resultd
      * Gsheet tabs required for prescription:  
        * Prescriptions, Super_generic_Prescriptions, result
      * Data headers required for diagnosis
        * Unstructured Name, ICD10 Diagnosis, Number
      * Data headers required for prescription
        * Unstructured Name, RX Norm [Super Generic], Number
      """
  )
  # inputs
  sheet_type = gr.Dropdown(['prescription', 'diagnosis'], label="QA Type")
  url = gr.Textbox(label="URL", placeholder="Enter sheet url ...")
  # outputs
  output = gr.Textbox(label="Output Box")
  
  run_btn = gr.Button("Run")
  run_btn.click(
      fn=quality_check_main,
      inputs=[
          sheet_type,
          url
          ],
      outputs=output, 
      api_name="quality_check"
  )

demo.launch()