File size: 9,750 Bytes
75660bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#--- anomaly detection - unsupervised page
import streamlit as st
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go

import lib.claims as libClaims
import lib.providers as libProviders
import lib.utils as libUtils
import sys

description = "Anomaly Detection - Unsupervised"
m_kblnTraceOn = False                                  #--- enable/disable module level tracing

def run():
    #--- note:  in python, you need to specify global scope for fxns to access module-level variables 
    global m_kblnTraceOn
    print("\nINFO (lit_about.run)  loading ", description, " page ...") 


    try:

        #--- page settings
        if (m_kblnTraceOn):  print("TRACE (litAnomUnSuperv.run):  Initialize Page Settings ...")
        st.header("Claims Anomalies - Unsupervised Approach (KMeans)")


        #--- provide file drag/drop capability
        m_blnDisableDragDrop = False
        if(not m_blnDisableDragDrop): 
            #btnSave = st.button("Save")
            pklDropped = st.file_uploader("Upload a Claims Dataset", type=["pkl"])
            m_blnDisableDragDrop = (pklDropped is None)


        #--- show:  raw claims data analysis
        if (m_kblnTraceOn):  print("TRACE (litAnomUnSuperv.run):  load raw claims data ...")
        if (m_blnDisableDragDrop):
            pdfClaims = libClaims.load_claims(False)
        else:
            pdfClaims = pd.read_pickle(pklDropped)        

        #--- show:  raw claims data analysis
        if (m_kblnTraceOn):  print("TRACE (litAnomUnsuperv.run):  Show Raw Claims Dataframe ...")
        pdfClaims = libClaims.load_claims(False)


        #--- get unsupervised predictions
        #pdfFeatEng = libClaims.do_featEng(pdfClaims)
        pdfPred = libClaims.get_kmeansPredict(pdfClaims)
        pdfSample = pdfPred.sample(100)
        pdfSample['providerId'] = pdfSample['Provider'].str[3:].astype(np.float64)


        #--- save this file locally as a pkl
        #btnSave_testFile(pdfClaims, pdfPred)


        #--- table of claims and clusters, sorted by InscClaimAmt Reimbursed
        pdfTopClaims = pdfSample.sort_values(by=["cluster", "InscClaimAmtReimbursed"], ascending=False)
        if (m_kblnTraceOn):  print("TRACE (litAnomUnsuperv.run):  Show $claims reimbursed by cluster ...")
        st.markdown("(Top) Ins Claim Reimbursed by Cluster")
        st.dataframe(pdfTopClaims)

        
        #--- chart cluster data distribution
        chart_clusterDistr(pdfSample)


        col1, col2, col3 = st.columns(3)


        #--- chart KMeans clusters":  InscClaimAmtReimbursed
        #chart_KMeansClusters(pdfSample, "Age", "InscClaimAmtReimbursed", col1)
        #chart_KMeansClusters(pdfSample, "providerId", "InscClaimAmtReimbursed", col2)        
        
        chart_KMeansClusters(pdfSample, "providerId", "AdmittedDays", col1)
        chart_KMeansClusters(pdfSample, "providerId", "DeductibleAmtPaid", col2)
        chart_KMeansClusters(pdfSample, "providerId", "InscClaimAmtReimbursed", col3)

        chart_KMeansClusters(pdfSample, "providerId", "ChronicCond_KidneyDisease", col1)
        chart_KMeansClusters(pdfSample, "providerId", "ChronicCond_Heartfailure", col2)
        chart_KMeansClusters(pdfSample, "providerId", "ChronicCond_ObstrPulmonary", col3)

        chart_KMeansClusters(pdfSample, "AdmittedDays", "DeductibleAmtPaid", col1)
        chart_KMeansClusters(pdfSample, "AdmittedDays", "InscClaimAmtReimbursed", col2)
        chart_KMeansClusters(pdfSample, "DeductibleAmtPaid", "InscClaimAmtReimbursed", col3)



        #--- chart cluster bars
        #chart_KMeansBars(pdfSample, "cluster", "InscClaimAmtReimbursed", col1)
        #chart_KMeansBars(pdfSample, "cluster", "DeductibleAmtPaid", col2)

        #chart_KMeansBars(pdfSample, "cluster", "IPAnnualReimbursementAmt", col1)
        #chart_KMeansBars(pdfSample, "cluster", "IPAnnualDeductibleAmt", col2)
    
        #chart_KMeansBars(pdfSample, "cluster", "OPAnnualReimbursementAmt", col1)
        #chart_KMeansBars(pdfSample, "cluster", "OPAnnualDeductibleAmt", col2)
    
        #chart_KMeansBars(pdfSample, "cluster", "ChronicCond_Heartfailure", col1)
        #chart_KMeansBars(pdfSample, "cluster", "ChronicCond_KidneyDisease", col2)

    except TypeError as e:
        print("ERROR (litAnomUnsuperv.run_typeError):  ", e)

    except:
        e = sys.exc_info()
        print("ERROR (litAnomUnsuperv.run_genError):  ", e)        



def chart_clusterDistr(pdfSample):
    #pdfClustDistr = pdfSample['cluster'].value_counts()
    pdfBar = pdfSample
    pdfCluster0 = pdfBar[pdfBar['cluster'] == 0] 
    pdfCluster1 = pdfBar[pdfBar['cluster'] == 1] 
    pdfCluster2 = pdfBar[pdfBar['cluster'] == 2] 

    kstrTitle = "(KMeans Clusters) Claims data"
    #--- chart 
    fig = go.Figure(
        layout=dict(
            legend=dict(groupclick="toggleitem"),
            xaxis=dict(title='cluster'),
            yaxis=dict(title='#data points')
        )
    )

    fig.add_trace(
        go.Bar(
            x=pdfCluster0['cluster'],
            y=pdfCluster0['cluster'].value_counts(),
            name='cluster0'
        )
    )

    if (pdfCluster1.shape[0]>0):
        fig.add_trace(
            go.Bar(
                x=pdfCluster1['cluster'],
                y=pdfCluster1['cluster'].value_counts(),
                name='cluster1'
            )) 

    if (pdfCluster2.shape[0]>0):
        fig.add_trace(
            go.Bar(
                x=pdfCluster2['cluster'],
                y=pdfCluster2['cluster'].value_counts(),
                name='cluster2'
            )) 
    st.plotly_chart(fig, use_container_width=True)


def chart_KMeansClusters(pdfSample, strXFeature, strYFeature, stCol):
    pdfScatter = pdfSample
    pdfCluster0 = pdfScatter[pdfScatter['cluster'] == 0] 
    pdfCluster1 = pdfScatter[pdfScatter['cluster'] == 1] 
    pdfCluster2 = pdfScatter[pdfScatter['cluster'] == 2] 

    kstrTitle = "(KMeans Clusters) Claims data"
    #--- chart 
    fig = go.Figure(
        layout=dict(
            legend=dict(groupclick="toggleitem"),
            xaxis=dict(title=strXFeature),
            yaxis=dict(title=strYFeature)
        )
    )

    fig.add_trace(
        go.Scatter(
            x=pdfCluster0[strXFeature],
            y=pdfCluster0[strYFeature],
            text="claimId: " + pdfCluster0['ClaimID'],
            mode='markers',
            name='cluster0'
        )
    )

    if (pdfCluster1.shape[0]>0):
        fig.add_trace(
            go.Scatter(
                x=pdfCluster1[strXFeature],
                y=pdfCluster1[strYFeature],
                mode='markers',
                name='cluster1'
            )) 

    if (pdfCluster2.shape[0]>0):
        fig.add_trace(
            go.Scatter(
                x=pdfCluster2[strXFeature],
                y=pdfCluster2[strYFeature],
                mode='markers',
                name='cluster2'
            )) 
    stCol.plotly_chart(fig, use_container_width=True)


def chart_KMeansBars(pdfSample, strXFeature, strYFeature, stCol):
    pdfBar = pdfSample
    pdfCluster0 = pdfBar[pdfBar['cluster'] == 0] 
    pdfCluster1 = pdfBar[pdfBar['cluster'] == 1] 
    pdfCluster2 = pdfBar[pdfBar['cluster'] == 2] 

    kstrTitle = "(KMeans Clusters) Claims data"
    #--- chart 
    fig = go.Figure(
        layout=dict(
            legend=dict(groupclick="toggleitem"),
            xaxis=dict(title=strXFeature),
            yaxis=dict(title=strYFeature)
        )
    )

    fig.add_trace(
        go.Bar(
            x=pdfCluster0[strXFeature],
            y=pdfCluster0[strYFeature],
            name='cluster0'
        )
    )

    if (pdfCluster1.shape[0]>0):
        fig.add_trace(
            go.Bar(
                x=pdfCluster1[strXFeature],
                y=pdfCluster1[strYFeature],
                name='cluster1'
            )) 

    if (pdfCluster2.shape[0]>0):
        fig.add_trace(
            go.Bar(
                x=pdfCluster2[strXFeature],
                y=pdfCluster2[strYFeature],
                name='cluster2'
            )) 
    stCol.plotly_chart(fig, use_container_width=True)



def btnSave_testFile(pdfClaims, pdfPred):
    #--- get all claims for all anoms
    """     print("TRACE (lit_anom_unsuperv.btnSave_testFile)  query anoms ... ", pdfPred.head(10))
        pdfAnomClaims = pdfPred[pdfPred['hasAnom?'] > 0] 
        #pdfAnomProv = pdfAnomProv['Provider']

        #--- filter claims by anomProviders
        print("TRACE (lit_anom_unsuperv.btnSave_testFile)  filter claims ... ")
        pdfClaimAnom = pdfClaims[pdfClaims['Provider'].isin(pdfAnomProv['Provider'])]
        pdfClaimNoAnom = pdfClaims[~pdfClaims['Provider'].isin(pdfAnomProv['Provider'])]
        lngNumAnoms = len(pdfClaimAnom.index)
        lngNumOk = len(pdfClaimNoAnom.index)
        print("TRACE (lit_anom_unsuperv.btnSave_testFile)  #anoms: ", lngNumAnoms, ",  !anoms: ", lngNumOk)

        #--- get a sample for remaining records
        print("TRACE (lit_anom_unsuperv.btnSave_testFile)  sampling claims ... ")
        pdfSave = pd.concat([pdfClaimAnom.sample(frac=0.6), pdfClaimNoAnom.sample(frac=0.1)]) """

    pdfSave = pdfClaims.sample(frac=0.1)

    print("TRACE (lit_anom_unsuperv.btnSave_testFile)  saving ... ")
    saveProviderTestData(pdfSave)


def saveProviderTestData(pdfTestData):

    #--- save the file
    from datetime import date
    import time
    import pickle
    strDteNow = date.today().strftime('%Y%m%d')
    strTimeNow = time.strftime('%H%M%S')
    strProvTestFile = libUtils.pth_data + strDteNow + strTimeNow + "_claimsTestSample.pkl"
    #pd.to_pickle(pdfClaims.sample(200), strProvTestFile,  protocol=pickle.HIGHEST_PROTOCOL) 
    pdfTestData.to_pickle(strProvTestFile, protocol=pickle.HIGHEST_PROTOCOL)