Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 6,704 Bytes
61d29fc | 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 | """
Quick Demo: Legislative Tracking Maps
This script demonstrates how to create legislative tracking maps
for multiple social issues with minimal code.
Run:
python examples/legislative_map_demo.py
"""
import asyncio
import sys
from pathlib import Path
# Add project root to path
sys.path.insert(0, str(Path(__file__).parent.parent))
from scripts.legislative_tracker import LegislativeTracker
from loguru import logger
async def demo_single_issue():
"""Demo: Track fluoridation legislation."""
logger.info("=" * 80)
logger.info("DEMO 1: Track Fluoridation Legislation")
logger.info("=" * 80)
tracker = LegislativeTracker()
# Track fluoridation bills in 2024
logger.info("\nπ Searching for fluoridation bills...")
df = await tracker.track_issue("fluoridation", year=2024)
# Print summary
logger.info(f"\nβ
Found {len(df)} bills")
logger.info(f" Bans: {len(df[df['type'] == 'ban'])}")
logger.info(f" Restrictions: {len(df[df['type'] == 'restriction'])}")
logger.info(f" Protections: {len(df[df['type'] == 'protection'])}")
# Show first 5 bills
logger.info("\nπ Sample Bills:")
for _, bill in df.head(5).iterrows():
logger.info(f" β’ {bill['state_code']}: {bill['title']} ({bill['type']}, {bill['status']})")
# Generate map
logger.info("\nπΊοΈ Generating map visualization...")
tracker.create_choropleth_map(df, "fluoridation")
logger.info("β
Map saved to data/visualizations/fluoridation_map.html")
async def demo_multiple_issues():
"""Demo: Track multiple issues at once."""
logger.info("\n" + "=" * 80)
logger.info("DEMO 2: Track Multiple Issues")
logger.info("=" * 80)
tracker = LegislativeTracker()
# Track multiple issues
issues = ["abortion", "marijuana", "voting"]
for issue in issues:
logger.info(f"\nπ Tracking {issue} legislation...")
df = await tracker.track_issue(issue, year=2024)
logger.info(f" β
{len(df)} bills found")
# Generate map
tracker.create_choropleth_map(df, issue)
logger.info(f" πΊοΈ Map: data/visualizations/{issue}_map.html")
async def demo_state_filtering():
"""Demo: Track legislation in specific states only."""
logger.info("\n" + "=" * 80)
logger.info("DEMO 3: Track Specific States")
logger.info("=" * 80)
tracker = LegislativeTracker()
# Track only southern states
southern_states = ["AL", "AR", "FL", "GA", "KY", "LA", "MS", "NC", "SC", "TN", "TX", "VA", "WV"]
logger.info(f"\nπ Tracking abortion legislation in southern states...")
df = await tracker.track_issue("abortion", year=2024, states=southern_states)
logger.info(f" β
{len(df)} bills found in {len(df['state_code'].unique())} states")
# Show state breakdown
state_counts = df.groupby('state_code').size().sort_values(ascending=False)
logger.info("\nπ Bills by State:")
for state, count in state_counts.items():
logger.info(f" {state}: {count} bills")
async def demo_custom_keywords():
"""Demo: Add custom issue with your own keywords."""
logger.info("\n" + "=" * 80)
logger.info("DEMO 4: Custom Issue Keywords")
logger.info("=" * 80)
tracker = LegislativeTracker()
# Add custom issue: Gun Control
tracker.issue_keywords["gun_control"] = {
"ban": ["ban assault weapons", "prohibit firearms", "gun ban"],
"restriction": ["background check", "waiting period", "permit requirement"],
"protection": ["constitutional carry", "second amendment protection", "gun rights"]
}
logger.info("\nπ Tracking gun control legislation with custom keywords...")
df = await tracker.track_issue("gun_control", year=2024)
logger.info(f" β
{len(df)} bills found")
logger.info(f" Bans: {len(df[df['type'] == 'ban'])}")
logger.info(f" Restrictions: {len(df[df['type'] == 'restriction'])}")
logger.info(f" Protections: {len(df[df['type'] == 'protection'])}")
async def demo_data_export():
"""Demo: Export data for further analysis."""
logger.info("\n" + "=" * 80)
logger.info("DEMO 5: Data Export")
logger.info("=" * 80)
tracker = LegislativeTracker()
# Track fluoridation
df = await tracker.track_issue("fluoridation", year=2024)
# Export to CSV (already done automatically)
logger.info(f"\nπΎ CSV saved to: data/cache/legislation/fluoridation_2024.csv")
# Export state summary
state_summary = tracker.generate_state_summary(df)
output_file = "data/cache/legislation/fluoridation_2024_summary.csv"
state_summary.to_csv(output_file, index=False)
logger.info(f"πΎ Summary saved to: {output_file}")
# Show summary
logger.info("\nπ State Summary (first 10):")
print(state_summary.head(10).to_string(index=False))
async def main():
"""Run all demos."""
logger.info("\n" + "=" * 80)
logger.info("LEGISLATIVE TRACKING MAPS - DEMO")
logger.info("=" * 80)
logger.info("\nThis demo shows how to:")
logger.info(" 1. Track legislation for specific issues")
logger.info(" 2. Track multiple issues at once")
logger.info(" 3. Filter by specific states")
logger.info(" 4. Add custom issue keywords")
logger.info(" 5. Export data for analysis")
logger.info("\n" + "=" * 80)
try:
# Run all demos
await demo_single_issue()
await demo_multiple_issues()
await demo_state_filtering()
await demo_custom_keywords()
await demo_data_export()
logger.info("\n" + "=" * 80)
logger.info("β
ALL DEMOS COMPLETE")
logger.info("=" * 80)
logger.info("\nView generated maps:")
logger.info(" - data/visualizations/fluoridation_map.html")
logger.info(" - data/visualizations/abortion_map.html")
logger.info(" - data/visualizations/marijuana_map.html")
logger.info(" - data/visualizations/voting_map.html")
logger.info(" - data/visualizations/gun_control_map.html")
logger.info("\nView exported data:")
logger.info(" - data/cache/legislation/*.csv")
except Exception as e:
logger.error(f"\nβ Error running demos: {e}")
logger.error("\nMake sure you have:")
logger.error(" 1. OPENSTATES_API_KEY in .env file")
logger.error(" 2. Installed requirements: pip install plotly matplotlib")
logger.error("\nGet API key at: https://openstates.org/accounts/signup/")
if __name__ == "__main__":
asyncio.run(main())
|