Spaces:
Sleeping
Sleeping
File size: 10,027 Bytes
1aa7583 a47ab28 45a8c07 a47ab28 45a8c07 1aa7583 1c76a96 2236dd4 1c76a96 a47ab28 45a8c07 cc8b36c a47ab28 cc8b36c a47ab28 cc8b36c a47ab28 1c76a96 a47ab28 263f645 a47ab28 1c76a96 a47ab28 46725fa 2059c4a a47ab28 36961ed a47ab28 6284181 a47ab28 d328343 a47ab28 6284181 a47ab28 36961ed a47ab28 36961ed a47ab28 2059c4a 45d9637 a47ab28 36961ed a47ab28 2059c4a 45d9637 a47ab28 36961ed a47ab28 45a8c07 a47ab28 45a8c07 a47ab28 45a8c07 a47ab28 8424379 46bc3f8 a47ab28 45a8c07 6284181 45a8c07 6284181 45a8c07 a47ab28 45a8c07 6284181 45a8c07 a47ab28 45a8c07 a47ab28 45a8c07 a47ab28 45a8c07 a47ab28 45a8c07 6284181 45a8c07 6284181 a47ab28 45a8c07 6284181 45a8c07 a47ab28 45a8c07 a47ab28 45a8c07 a47ab28 45a8c07 a47ab28 45a8c07 a47ab28 45a8c07 6284181 45d9637 6284181 2059c4a 45d9637 2059c4a a47ab28 6284181 7d06004 2059c4a 45a8c07 6284181 a47ab28 7d06004 a47ab28 6284181 a47ab28 45a8c07 6284181 a47ab28 656f25c 36961ed a47ab28 2059c4a 6284181 46725fa 2059c4a 6284181 |
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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
import ibis
from ibis import _
import streamlit as st
import altair as alt
import os
import pandas as pd
import matplotlib.pyplot as plt
from pandasai.llm.openai import OpenAI
from pandasai import Agent
from pandasai.responses.streamlit_response import StreamlitResponse
import leafmap.maplibregl as leafmap
st.set_page_config(layout="wide",
page_title="TPL LandVote",
page_icon=":globe:")
'''
# LandVote Prototype
An experimental platform for visualizing data on ballot measures for conservation, based on data from <https://landvote.org/> curated by the Trust for Public Land.
'''
COLORS = {
"dark_orange": "#ab5601",
"light_orange": "#f3d3b1",
"grey": "#d3d3d3",
"light_green": "#c3dbc3",
"dark_green": "#417d41",
"dem_blue": "#1b46c2",
"rep_red": "#E81B23"
}
## chatbot
llm = OpenAI(api_token=st.secrets["OPENAI_API_KEY"])
df1 = pd.read_csv("data.csv")
agent = Agent([df1], config={"verbose": True, "response_parser": StreamlitResponse, "llm": llm})
year = st.slider("Select a year", 1988, 2024, 2022, 1)
votes_pmtiles = "https://huggingface.co/datasets/boettiger-lab/landvote/resolve/main/votes.pmtiles"
votes_parquet = "https://huggingface.co/datasets/boettiger-lab/landvote/resolve/main/votes.parquet"
# get parquet data for charts
con = ibis.duckdb.connect(extensions=["spatial"])
votes = (con
.read_parquet(votes_parquet)
.cast({"geometry": "geometry"})
)
# generate altair chart with df
def create_chart(df, y_column, ylab, title, color):
# color encoding - color is a list or single value
color_encoding = (
alt.Color('party:N', scale=alt.Scale(domain=["DEMOCRAT", "REPUBLICAN"], range=color))
if isinstance(color, list) else alt.value(color)
)
return alt.Chart(df).mark_line(strokeWidth=3).encode(
x=alt.X('year:N', title='Year'),
y=alt.Y(f'{y_column}:Q', title=ylab),
color=color_encoding
).properties(
title=title
)
# percentage of measures passing, per party
def get_passes(votes):
return (votes
# .filter(_.year >= 2000)
.group_by("year", "party")
.aggregate(total=_.count(), passes=_.Status.isin(["Pass", "Pass*"]).sum())
.mutate(percent_passed=(_.passes / _.total).round(2),
color=ibis.case().when(_.party == "DEMOCRAT", COLORS["dem_blue"]).else_(COLORS["rep_red"]).end())
.to_pandas())
# cumulative funding over time
def funding_chart(votes):
return (votes
# .filter(_.year >= 2000)
.mutate(amount=_.amount.replace('$', '')
.replace(',', '')
.cast('float64'))
.filter(_.Status.isin(["Pass", "Pass*"]))
.group_by("year")
.aggregate(total_funding=_.amount.sum())
.order_by("year")
.mutate(cumulative_funding=_.total_funding.cumsum()/1e9)
.to_pandas()
)
#color fill for measure status
paint_fill = {
"fill-color": [
"case",
["==", ["get", "Status"], "Pass"],
[
"interpolate", ["linear"], [
"to-number", ["slice", ["get", "yes"], 0, -1] # convert 'yes' string to number
],
50, COLORS["grey"],
55, COLORS["light_green"],
100, COLORS["dark_green"] # higher yes % -> darker green
],
["==", ["get", "Status"], "Fail"],
[
"interpolate", ["linear"], [
"to-number", ["slice", ["get", "yes"], 0, -1]
],
0, COLORS["dark_orange"],
50, COLORS["light_orange"], # lower yes % -> darker orange
67, COLORS["grey"] # 67 is max in our data
],
COLORS["grey"]
]
}
# for status, height depends on funding
paint_extrusion = {
"fill-extrusion-color": paint_fill["fill-color"],
"fill-extrusion-height": ["*", ["to-number", ["get", "log_amount"]], 5000]
}
# pmtiles style for status
def get_style_status(jurisdiction):
if jurisdiction == "State":
name = "state"
label = "States"
paint_type = paint_fill
layer_type = "fill"
elif jurisdiction == "County":
name = "county"
label = "Counties"
paint_type = paint_extrusion
layer_type = "fill-extrusion"
else: # Municipal
name = "municipal"
label = "Cities"
paint_type = paint_extrusion
layer_type = "fill-extrusion"
return {
"layers": [
{
"id": label,
"source": name,
"source-layer": name,
"type": layer_type,
"filter": ["==", ["get", "year"], year],
"paint": paint_type
}
]
}
# pmtiles style for party
def get_style_party(jurisdiction):
if jurisdiction == "State":
name = "state"
label = "States"
elif jurisdiction == "County":
name = "county"
label = "Counties"
else: # Municipal
name = "municipal"
label = "Cities"
# Return style dictionary for political party
return {
"layers": [
{
"id": label,
"source": name,
"source-layer": name,
"type": "fill",
"filter": [
"==", ["get", "year"], year
],
"paint": {
"fill-color": {
"property": "party",
"type": "categorical",
"stops": [
["DEMOCRAT", COLORS["dem_blue"]],
["REPUBLICAN", COLORS["rep_red"]]
]
}
}
}
]
}
sv_pmtiles = "https://data.source.coop/cboettig/social-vulnerability/svi2020_us_county.pmtiles"
sv_style = {
"layers": [
{
"id": "SVI",
"source": "Social Vulnerability Index",
"source-layer": "SVI2020_US_county",
"type": "fill",
"paint": {
"fill-color":
["interpolate", ["linear"], ["get", "RPL_THEMES"],
0, "#FFE6EE",
1, "#850101"]
}
}
]
}
party_pmtiles = "https://huggingface.co/datasets/boettiger-lab/landvote/resolve/main/party_polygons_all.pmtiles"
recent_election_year = year - year%4
party_style = {
"layers": [
{
"id": "Party",
"source": "Political Parties",
"source-layer": "county",
"type": "fill",
"filter": [
"==", ["get", "year"], recent_election_year
],
"paint": {
"fill-color": {
"property": "party",
"type": "categorical",
"stops": [
["DEMOCRAT", COLORS["dem_blue"]],
["REPUBLICAN", COLORS["rep_red"]]
]
}
}
}
]
}
with st.sidebar:
color_choice = st.radio("Color by:", ["Measure Status", "Political Party"])
st.divider()
social_toggle = st.toggle("Social Vulnerability Index")
party_toggle = st.toggle("Political Parties")
st.divider()
'''
## Data Assistant (experimental)
Ask questions about the landvote data, like:
- What are the top states for approved conservation funds?
- Plot the total funds spent in conservation each year.
- What city has approved the most funds in a single measure? What was the description of that vote?
- Which state has had largest number measures fail? What is that as a fraction of it's total measures?
'''
prompt = st.chat_input("Ask about the data")
if prompt:
with st.spinner():
resp = agent.chat(prompt)
if os.path.isfile('exports/charts/temp_chart.png'):
im = plt.imread('exports/charts/temp_chart.png')
st.image(im)
os.remove('exports/charts/temp_chart.png')
st.write(resp)
m = leafmap.Map(style="positron", center=(-100, 40), zoom=3)
if social_toggle:
m.add_pmtiles(sv_pmtiles, style = sv_style ,visible=True, opacity=0.3, tooltip=True)
if party_toggle:
m.add_pmtiles(party_pmtiles, style = party_style ,visible=True, opacity=0.3, tooltip=True)
passed = votes.filter(_.Status.isin(["Pass","Pass*"])).count().execute()
total = votes.count().execute()
overall_passed = (passed/total*100).round(2)
f"{overall_passed}% Measures Passed"
if color_choice == "Measure Status":
m.add_pmtiles(votes_pmtiles, style=get_style_status("State"), visible=True, opacity=0.8, tooltip=True)
m.add_pmtiles(votes_pmtiles, style=get_style_status("County"), visible=True, opacity=1.0, tooltip=True)
m.add_pmtiles(votes_pmtiles, style=get_style_status("Municipal"), visible=True, opacity=1.0, tooltip=True)
elif color_choice == "Political Party":
m.add_pmtiles(votes_pmtiles, style=get_style_party("State"), visible=True, opacity=0.8, tooltip=True)
m.add_pmtiles(votes_pmtiles, style=get_style_party("County"), visible=True, opacity=1.0, tooltip=True)
m.add_pmtiles(votes_pmtiles, style=get_style_party("Municipal"), visible=True, opacity=1.0, tooltip=True)
m.add_layer_control()
m.to_streamlit()
# display charts
df_passes = get_passes(votes)
st.altair_chart(create_chart(df_passes, "percent_passed", "Percent Passed","% of Measures Passed", [COLORS["dem_blue"], COLORS["rep_red"]]), use_container_width=True)
df_funding = funding_chart(votes)
st.altair_chart(create_chart(df_funding, "cumulative_funding", "Billions of Dollars", "Cumulative Funding", COLORS["dark_green"]), use_container_width=True)
|