Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from cmath import pi
|
| 2 |
+
from json import load, tool
|
| 3 |
+
from os import stat
|
| 4 |
+
from telnetlib import RCP
|
| 5 |
+
import streamlit as st
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import numpy as np
|
| 8 |
+
import pydeck as pdk
|
| 9 |
+
from typing import Dict, Union
|
| 10 |
+
import streamlit.components.v1 as components
|
| 11 |
+
#import streamlit_shared_funcs as my
|
| 12 |
+
|
| 13 |
+
st.title("Live 3D Map")
|
| 14 |
+
location = st.checkbox('Location Filter')
|
| 15 |
+
queried_zip_code = None
|
| 16 |
+
queried_city = None
|
| 17 |
+
queried_state = None
|
| 18 |
+
queried_age = None
|
| 19 |
+
if location:
|
| 20 |
+
queried_zip_code = st.text_input('Zip Code:')
|
| 21 |
+
queried_city = st.text_input('City')
|
| 22 |
+
queried_state = st.selectbox('State:', ('AL', 'AK', 'AZ', 'AR', 'AS','CA','CO','CT','DE','DC','FL','GA','GU','HI','ID','IL',
|
| 23 |
+
'IN','IA','KS','KY','LA','ME','MD','MA','MI','MN','MS','MO','MT','NE','NV','NH','NJ','NM','NY','NC','ND','CM','OH',
|
| 24 |
+
'OK','OR','PA','PR','RI','SC','SD','TN','TX','UT','VT','VA','VI','WA','WV','WI','WY'))
|
| 25 |
+
ageBox = st.checkbox("Age Filter")
|
| 26 |
+
if ageBox:
|
| 27 |
+
queried_age = st.slider("Age",0,200,(0,200))
|
| 28 |
+
|
| 29 |
+
queried_male = st.checkbox("Male",value=True)
|
| 30 |
+
queried_female = st.checkbox("Female",value=True)
|
| 31 |
+
|
| 32 |
+
@st.cache(allow_output_mutation=True)
|
| 33 |
+
def gen_load() -> pd.DataFrame:
|
| 34 |
+
df = my.get_data()
|
| 35 |
+
return df
|
| 36 |
+
|
| 37 |
+
@st.cache(allow_output_mutation=True)
|
| 38 |
+
def gen_load_old() -> pd.DataFrame:
|
| 39 |
+
# 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=";")
|
| 40 |
+
filename = "US.txt"
|
| 41 |
+
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"])
|
| 42 |
+
#file = pd.read_csv('US.txt',names = ['Country','Zip','City','State','Abb','Name1','Code1','Name2','Code2','lat','lon','Accuracy'], delimiter="\t",usecols=["Zip","City","State","Abb","lat","lon"])
|
| 43 |
+
file['Zip'] = file['Zip'].astype(str).str.zfill(5)
|
| 44 |
+
w = lambda x: (x[-5:])
|
| 45 |
+
filename = "NPIProviders.xlsx"
|
| 46 |
+
df = pd.read_excel('...xlsx',header = None,names = ['Zip','FCTIN','FCName', 'Procedures','Age','Gender','Admitted','Form','Cohort'], skiprows=1,converters={'Zip':w },usecols="A,B,C,D,E,F,G,H,I")
|
| 47 |
+
df = df.merge(file, left_on = 'Zip', right_on = 'Zip', how='inner')
|
| 48 |
+
df = df.fillna(" ")
|
| 49 |
+
return df
|
| 50 |
+
|
| 51 |
+
#queried_ctss = st.multiselect("Ctss",options=gen_load()["Ctss"].value_counts().reset_index())
|
| 52 |
+
|
| 53 |
+
@st.cache(allow_output_mutation=True)
|
| 54 |
+
def load_data(state = None,zip_code = None,city = None,age = None,male = None,female = None,selected_cohorts = None) -> pd.DataFrame:
|
| 55 |
+
df = gen_load()
|
| 56 |
+
try:
|
| 57 |
+
if (male or female) and not (male and female):
|
| 58 |
+
df = df.loc[df['Gender'] == ("Male" if male else ("Female" if female else ""))]
|
| 59 |
+
elif not (male or female):
|
| 60 |
+
df = df.loc[df['Gender'] == "(null)"]
|
| 61 |
+
except:
|
| 62 |
+
pass
|
| 63 |
+
try:
|
| 64 |
+
df = df[(df['Age'].gt(age[0]) & df['Age'].lt(age[1]))]
|
| 65 |
+
except:
|
| 66 |
+
pass
|
| 67 |
+
try:
|
| 68 |
+
if zip_code or state or city:
|
| 69 |
+
val = "Zip" if zip_code != "" else "City" if city != "" else "Abb"
|
| 70 |
+
df = df.loc[df[val] == (zip_code or city or state)]
|
| 71 |
+
except:
|
| 72 |
+
pass
|
| 73 |
+
|
| 74 |
+
try:
|
| 75 |
+
if len(selected_cohorts) > 0:
|
| 76 |
+
df = df.loc[df['Cohort'].isin(selected_ctss)]
|
| 77 |
+
|
| 78 |
+
except:
|
| 79 |
+
pass
|
| 80 |
+
|
| 81 |
+
value_counts = df["FCName"].value_counts()
|
| 82 |
+
df2 = pd.DataFrame(value_counts)
|
| 83 |
+
df2 = df2.reset_index()
|
| 84 |
+
df2.columns = ['FCName', 'Count']
|
| 85 |
+
df = df.merge(df2, left_on = 'FCName', right_on = 'FCName',how='left')
|
| 86 |
+
|
| 87 |
+
return df
|
| 88 |
+
|
| 89 |
+
def mapF(data):
|
| 90 |
+
geo = data.iloc[0]
|
| 91 |
+
lat = float(geo['lat'])
|
| 92 |
+
lon = float(geo['lon'])
|
| 93 |
+
max = data['Count'].max()
|
| 94 |
+
view_state = pdk.ViewState(
|
| 95 |
+
pitch = 40.5,
|
| 96 |
+
bearing = -27.36,
|
| 97 |
+
latitude = lat,
|
| 98 |
+
longitude = lon,
|
| 99 |
+
zoom = 4,
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
layer = pdk.Layer(
|
| 103 |
+
'ColumnLayer',
|
| 104 |
+
data=data,
|
| 105 |
+
get_position='[lon, lat]',
|
| 106 |
+
pickable=True,
|
| 107 |
+
extruded=True,
|
| 108 |
+
auto_highlight=True,
|
| 109 |
+
get_elevation="Count",
|
| 110 |
+
# cell_size=200,
|
| 111 |
+
radius = 1000,
|
| 112 |
+
elevation_scale=1000000/ max,
|
| 113 |
+
get_fill_color='[255, 255 - (Count/3500) * 255, 0,100]',
|
| 114 |
+
coverage=10
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
r = pdk.Deck(
|
| 118 |
+
# map_style = x,
|
| 119 |
+
layers=[layer],
|
| 120 |
+
initial_view_state= view_state,
|
| 121 |
+
tooltip = {
|
| 122 |
+
"html": "<b>City,State:</b> {City} </br> <b>Procedures:</b> {Count} </br> <b>F:</b> {FCName}",
|
| 123 |
+
"style": {
|
| 124 |
+
"backgroundColor": "steelblue",
|
| 125 |
+
"color": "white"
|
| 126 |
+
}
|
| 127 |
+
},
|
| 128 |
+
)
|
| 129 |
+
# st.pydeck_chart(r)
|
| 130 |
+
components.html(r.to_html(as_string=True), height=600)
|
| 131 |
+
|
| 132 |
+
data = load_data(state=queried_state,zip_code=queried_zip_code,city=queried_city,age=queried_age,male=queried_male,female=queried_female,selected_cohorts=queried_cohorts)
|
| 133 |
+
|
| 134 |
+
if st.checkbox('Display data ?'):
|
| 135 |
+
data
|
| 136 |
+
|
| 137 |
+
mapFs(data)
|
| 138 |
+
|