Spaces:
Sleeping
Sleeping
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) | |
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 | |
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()) | |
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) | |