File size: 5,040 Bytes
c913f06
 
 
909cb80
c913f06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
909cb80
51109f7
c913f06
 
909cb80
 
c913f06
 
 
 
 
 
 
 
909cb80
 
 
c913f06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
909cb80
 
 
 
 
c913f06
 
 
 
6e454e9
c913f06
6e454e9
 
384629d
 
 
 
c913f06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
909cb80
c913f06
 
 
 
 
 
 
 
 
909cb80
c913f06
 
 
 
909cb80
c913f06
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
from cmath import pi
from json import load, tool
from os import stat
#from telnetlib import RCP
import streamlit as st
import pandas as pd
import numpy as np
import pydeck as pdk
from typing import Dict, Union
import streamlit.components.v1 as components
#import streamlit_shared_funcs as my

st.title("Live 3D Map")
location = st.checkbox('Location Filter')
queried_zip_code = None
queried_city = None
queried_state = None
queried_age = None
if location:
    queried_zip_code = st.text_input('Zip Code:')
    queried_city = st.text_input('City')
    queried_state = st.selectbox('State:',  ('AL', 'AK', 'AZ', 'AR', 'AS','CA','CO','CT','DE','DC','FL','GA','GU','HI','ID','IL',
    'IN','IA','KS','KY','LA','ME','MD','MA','MI','MN','MS','MO','MT','NE','NV','NH','NJ','NM','NY','NC','ND','CM','OH',
    'OK','OR','PA','PR','RI','SC','SD','TN','TX','UT','VT','VA','VI','WA','WV','WI','WY'))
ageBox = st.checkbox("Age Filter")
if ageBox:
    queried_age = st.slider("Age",0,200,(0,200))

queried_male = st.checkbox("Male",value=True)
queried_female = st.checkbox("Female",value=True)

@st.cache(allow_output_mutation=True)
def gen_load() -> pd.DataFrame:
    #df = my.get_data()
    df = pd.read_csv('US.txt')
    return df

#AI Emotional State Score: Anxiety, Confusion, Trepidation, Fear, Guilt

@st.cache(allow_output_mutation=True)
def gen_load_old() -> pd.DataFrame:
    # file = pd.read_csv('us-zip-code-latitude-and-longitude.txt',names = ['Zip','City','State','lat','lon','Timezone',"Daylight Savings", "Geo point"], skiprows=1, delimiter=";")
    filename = "US.txt"
    file = pd.read_csv(filename,names = ['Country','Zip','City','State','Abb','Name1','Code1','Name2','Code2','lat','lon','Accuracy'], delimiter="\t",usecols=["Zip","City","State","Abb","lat","lon"])
    file['Zip'] = file['Zip'].astype(str).str.zfill(5)
    w = lambda x: (x[-5:])
    filename = "NPIProviders.xlsx"
    df = pd.read_csv('Locations.csv',header = None,names = ['Zip','TIN','Name', 'Services','Age','Gender','StartDate','Questions','Entity'], skiprows=1,converters={'Zip':w },usecols="A,B,C,D,E,F,G,H,I")  
    'Zip','TIN','Name', 'Services','Age','Gender','StartDate','Questions','Entity'
     
    df = df.merge(file, left_on = 'Zip', right_on = 'Zip', how='inner')
    df = df.fillna(" ")
    return df

#queried_ctss = st.multiselect("Ctss",options=gen_load()["Ctss"].value_counts().reset_index())

@st.cache(allow_output_mutation=True)
def load_data(state = None,zip_code = None,city = None,age = None,male  = None,female = None,selected_cohorts = None) -> pd.DataFrame:
    df = gen_load()
    try:
        if (male or female) and not (male and female):
            df = df.loc[df['Gender'] == ("Male" if male else ("Female" if female else ""))]
        elif not (male or female):
            df = df.loc[df['Gender'] == "(null)"]
    except:
        pass
    try:
        df = df[(df['Age'].gt(age[0]) & df['Age'].lt(age[1]))]
    except:
        pass
    try:
        if zip_code or state or city:
            val = "Zip" if zip_code != "" else "City" if city != "" else "Abb"
            df = df.loc[df[val] == (zip_code or city or state)]
    except:
        pass

    try:
        if len(selected_cohorts) > 0:
            df = df.loc[df['Cohort'].isin(selected_ctss)]
                
    except:
        pass

    #value_counts = df["FCName"].value_counts()
    #df2 = pd.DataFrame(value_counts)
    #df2 = df2.reset_index()
    #df2.columns = ['FCName', 'Count']
    #df = df.merge(df2, left_on = 'FCName', right_on = 'FCName',how='left')

    return df

def mapF(data):
    print(data)
    geo = data.iloc[0]
    print(geo)
    #print(geo[7], geo[8])
    #lat = float(geo['lat'])
    #lon = float(geo['lon'])
    lat = float(geo[7])
    lon = float(geo[8])
    max = data['Count'].max()
    view_state = pdk.ViewState(
        pitch = 40.5,
        bearing = -27.36,
        latitude = lat,
        longitude = lon,
        zoom = 4,
    )

    layer = pdk.Layer(
            'ColumnLayer',
            data=data,
            get_position='[lon, lat]',
            pickable=True,
            extruded=True,
            auto_highlight=True,
            get_elevation="Count",
            # cell_size=200,
            radius = 1000,
            elevation_scale=1000000/ max,
            get_fill_color='[255, 255 - (Count/3500) * 255, 0,100]',
            coverage=10
    )                 

    r = pdk.Deck(
        # map_style = x,
        layers=[layer],
        initial_view_state= view_state,
        tooltip = {
            "html": "<b>City,State:</b> {City} </br> <b>Services:</b> {Count} </br> <b>F:</b> {FCName}",
           "style": {
                "backgroundColor": "steelblue",
                "color": "white"
           }
        },
    )
    # st.pydeck_chart(r)
    components.html(r.to_html(as_string=True), height=600)

data = load_data(state=queried_state,zip_code=queried_zip_code,city=queried_city,age=queried_age,male=queried_male,female=queried_female)

if st.checkbox('Display data ?'):
    data

mapF(data)