Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 19,527 Bytes
d15f07c | 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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 | ---
sidebar_position: 5
sidebar_label: Legislative Tracking Maps
---
# Creating Legislative Tracking Maps
Learn how to download state legislation data and create choropleth maps showing legislative activity across multiple social issues.
## What You'll Create
Interactive maps similar to legislative tracking visualizations that show:
- **Type of Legislation:** Outright Ban, Restriction, Protection
- **Status:** Introduced (pending), Enacted (passed), Failed (defeated)
- **Geographic Distribution:** State-by-state view of legislative activity
Example use cases:
- Water fluoridation legislation tracker
- Abortion access legislation map
- Marijuana legalization progress
- Voting rights legislation
- LGBTQ+ protection laws
- Education policy (CRT, book bans, etc.)
---
## Prerequisites
### Choose Your Data Source
** RECOMMENDED: Bulk Downloads (Faster & Easier)**
Plural Policy offers bulk downloads of ALL state legislation - no API key needed!
```bash
# Download all 2024 legislation for all 50 states (CSV)
python scripts/bulk_legislative_download.py --year 2024 --format csv --merge
# Output: data/cache/legislation_bulk/all_states_2024.csv
# Contains: ALL bills from ALL states in one file!
```
**Advantages:**
- Yes **No API key required** - Public bulk data
- Yes **No rate limits** - Download encouraged
- Yes **Faster** - One download vs thousands of API calls
- Yes **Complete** - Entire legislative sessions
- Yes **Offline** - Process locally without internet
**Alternative: Open States API (Real-time)**
For real-time bill tracking and search:
```bash
# Sign up: https://openstates.org/accounts/signup/
# Add to .env:
OPENSTATES_API_KEY=your-key-here
```
Use API when you need:
- Latest bill status (same-day updates)
- Search by specific keywords
- Subset of bills (not entire sessions)
---
### 1. Install Required Packages
```bash
# Core dependencies
pip install httpx pandas loguru python-dotenv
# Visualization libraries
pip install plotly matplotlib
```
### 2. Get Open States API Key (Free)
Open States provides state legislation data for all 50 states.
1. **Sign up:** https://openstates.org/accounts/signup/
2. **Get API key:** https://openstates.org/accounts/profile/
3. **Add to `.env`:**
```bash
OPENSTATES_API_KEY=your-key-here
```
**Free Tier:**
- 50,000 requests per month
- Access to all 50 states
- Bill text, voting records, legislators
---
## Quick Start
### Method 1: Bulk Download (Recommended)
**Step 1: Download all 2024 legislation**
```bash
# Download CSV files for all 50 states
python scripts/bulk_legislative_download.py --year 2024 --format csv --merge
```
**Step 2: Process and categorize**
```python
from scripts.legislative_tracker import LegislativeTracker
import pandas as pd
tracker = LegislativeTracker()
# Load bulk downloaded data
df = pd.read_csv('data/cache/legislation_bulk/all_states_2024.csv')
# Categorize bills for fluoridation tracking
categorized = []
for _, bill in df.iterrows():
cat = tracker.categorize_bill(bill.to_dict(), 'fluoridation')
categorized.append(cat)
df_categorized = pd.DataFrame(categorized)
# Generate map
tracker.create_choropleth_map(df_categorized, "fluoridation")
```
**Output:**
- **Downloaded:** All 50 states in minutes (not hours)
- **Processed:** Categorized by issue type
- **Map:** `data/visualizations/fluoridation_map.html`
---
### Method 2: Open States API (Real-time)
**Step 1: Get API key**
```bash
# Sign up at https://openstates.org/accounts/signup/
# Add to .env:
OPENSTATES_API_KEY=your-key-here
```
**Step 2: Track issue**
```bash
# Track fluoridation legislation in 2024
python scripts/legislative_tracker.py \
--issue fluoridation \
--year 2024 \
--visualize
```
**Output:**
- `data/cache/legislation/fluoridation_2024.csv` - Raw bill data
- `data/visualizations/fluoridation_map.html` - Interactive map
- `data/visualizations/fluoridation_legend.png` - Color legend
### Track Multiple Issues
```python
from scripts.legislative_tracker import LegislativeTracker
import asyncio
async def track_all_issues():
tracker = LegislativeTracker()
issues = ["fluoridation", "abortion", "marijuana", "voting", "lgbtq", "education"]
for issue in issues:
print(f"\n Tracking {issue} legislation...")
df = await tracker.track_issue(issue, year=2024)
tracker.create_choropleth_map(df, issue)
print(f"Yes {issue}: {len(df)} bills tracked")
asyncio.run(track_all_issues())
```
---
## How It Works
### 1. Data Collection (Open States API)
The `LegislativeTracker` class searches Open States API for bills matching issue keywords:
```python
tracker = LegislativeTracker()
# Search for fluoridation bills in all states
bills = await tracker.search_bills("fluoridation", year=2024)
# Returns bill metadata:
# - Title, summary, bill number
# - State, session, sponsors
# - Actions, votes, committee assignments
```
### 2. Bill Categorization
Each bill is categorized by **type** and **status**:
#### Bill Types
Determined by keyword matching in title/summary:
| Type | Keywords | Example |
|------|----------|---------|
| **Ban** | "prohibit", "ban", "criminalize" | "Prohibit Water Fluoridation Act" |
| **Restriction** | "limit", "restrict", "regulate", "parental consent" | "Fluoride Disclosure Requirements" |
| **Protection** | "require", "mandate", "protect", "expand" | "Community Water Fluoridation Mandate" |
#### Bill Status
Determined by latest legislative action:
| Status | Action Keywords | Meaning |
|--------|----------------|---------|
| **Enacted** | "signed", "enacted", "passed", "approved" | Bill became law |
| **Introduced** | "introduced", "referred", "committee" | Bill is pending |
| **Failed** | "failed", "defeated", "vetoed", "withdrawn" | Bill did not pass |
**Categorization Logic:**
```python
def categorize_bill(bill, issue):
text = f"{bill['title']} {bill['summary']}".lower()
# Check for ban keywords
if any(kw in text for kw in ["prohibit fluoride", "ban fluoridation"]):
bill_type = "ban"
# Check for restriction keywords
elif any(kw in text for kw in ["restrict fluoride", "limit fluoridation"]):
bill_type = "restriction"
# Check for protection keywords
elif any(kw in text for kw in ["require fluoride", "mandate fluoridation"]):
bill_type = "protection"
return bill_type
```
### 3. State-Level Aggregation
Bills are grouped by state to determine:
- **Dominant legislation type** (ban/restriction/protection)
- **Dominant status** (enacted/introduced/failed)
- **Bill counts** per category
```python
# Example state summary
{
"state_code": "AL",
"dominant_type": "ban",
"dominant_status": "introduced",
"total_bills": 3,
"ban_count": 2,
"restriction_count": 1,
"protection_count": 0
}
```
### 4. Map Visualization
**Color Coding:**
| Color | Bill Type | Status | Example |
|-------|-----------|--------|---------|
| Dark Brown | Ban | Enacted | Fluoridation ban passed into law |
| Orange | Ban | Introduced | Fluoridation ban pending |
| Light Yellow | Ban | Failed | Fluoridation ban defeated |
| Goldenrod | Restriction | Enacted | Disclosure law passed |
| Gold | Restriction | Introduced | Restriction pending |
| Pale Yellow | Restriction | Failed | Restriction defeated |
| Dark Blue | Protection | Enacted | Fluoridation mandate passed |
| Royal Blue | Protection | Introduced | Protection bill pending |
| Sky Blue | Protection | Failed | Protection defeated |
**Visual Patterns:**
- Solid colors = bills passed into law
- Lighter shades = bills introduced but pending
- Palest shades = bills failed/defeated
---
## Example: Fluoridation Legislation Map
### Step 1: Search for Bills
```python
import asyncio
from scripts.legislative_tracker import LegislativeTracker
async def main():
tracker = LegislativeTracker()
# Track fluoridation bills in 2024
df = await tracker.track_issue("fluoridation", year=2024)
# View results
print(df[['state_code', 'title', 'type', 'status']])
asyncio.run(main())
```
**Output:**
```
state_code title type status
AL Prohibit Water Fluoridation ban introduced
CA Community Fluoridation Mandate protection enacted
TX Fluoride Disclosure Requirements restriction introduced
```
### Step 2: Generate Map
```python
# Create interactive choropleth map
tracker.create_choropleth_map(df, "fluoridation")
```
**Output:** `data/visualizations/fluoridation_map.html`
### Step 3: View Map
Open `fluoridation_map.html` in a browser to see:
- Color-coded states by legislation type/status
- Hover over states for bill details
- Interactive zoom and pan
- Legend showing color meanings
---
## Customizing Issue Keywords
Add your own issue keywords to track new topics:
```python
tracker = LegislativeTracker()
# Add custom issue keywords
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"]
}
# Track the new issue
df = await tracker.track_issue("gun_control", year=2024)
```
---
## Data Sources
### Plural Policy Bulk Downloads **Recommended**
**What it provides:**
- **Complete legislative sessions** - All bills, votes, sponsors
- **CSV format** - Easy to process with pandas
- **JSON format** - Includes full bill text
- **PostgreSQL dumps** - Entire database (monthly snapshots)
- **No rate limits** - Bulk downloads encouraged
- **No API key** - Public domain data
**Coverage:**
- Yes All 50 states + DC, PR
- Yes Historical sessions (2010+)
- Yes Current sessions (monthly updates)
- Yes Complete bill lifecycle
**Bulk Download URLs:**
- **CSV per session:** https://data.openstates.org/session/csv/{state}/{session_id}.csv
- **JSON per session:** https://data.openstates.org/session/json/{state}/{session_id}.json.zip
- **PostgreSQL dump:** https://data.openstates.org/postgres/monthly/YYYY-MM-public.pgdump
- **Documentation:** https://open.pluralpolicy.com/data/
**Download Options:**
```bash
# Option 1: All states as CSV (fast, ~500MB total)
python scripts/bulk_legislative_download.py --year 2024 --format csv --merge
# Option 2: Specific states as JSON (includes full text)
python scripts/bulk_legislative_download.py --year 2024 --states CA,TX,NY --format json
# Option 3: PostgreSQL dump (complete database, ~5GB)
python scripts/bulk_legislative_download.py --postgres --month 2026-04
```
**Why use bulk downloads?**
| Feature | Bulk Download | API |
|---------|--------------|-----|
| **Speed** | Yes Minutes for all states | No Hours (50K API calls) |
| **Rate Limits** | Yes None | Warning 50K/month |
| **API Key** | Yes Not required | No Required |
| **Offline** | Yes Process locally | No Needs internet |
| **Complete Sessions** | Yes All bills at once | Warning Must paginate |
| **Real-time** | Warning Monthly updates | Yes Same-day updates |
**Recommendation:** Use bulk downloads for historical analysis and map generation. Use API for real-time bill tracking.
---
### Open States API (Real-time Updates)
**What it provides:**
- 100,000+ state bills from all 50 states
- Bill text, summaries, sponsors
- Legislative actions and votes
- Committee assignments
- Real-time updates
**Coverage:**
- Yes All 50 states + DC, PR, territories
- Yes Current and historical sessions
- Yes Multiple bill types (HB, SB, HR, SR, etc.)
- Yes Standardized OCD-ID format
**API Documentation:** https://docs.openstates.org/api-v3/
**Free Tier:**
- 50,000 requests/month
- No credit card required
- Commercial and non-commercial use
### Ballotpedia (Optional)
For ballot measures and referendums (not just legislation):
```python
from discovery.ballotpedia_integration import BallotpediaDiscovery
discovery = BallotpediaDiscovery()
measures = await discovery.get_ballot_measures("Alabama", year=2024)
```
**Note:** Ballotpedia API is paid. Web scraping fallback available.
---
## Output Files
### CSV Data Files
**Location:** `data/cache/legislation/{issue}_{year}.csv`
**Columns:**
- `bill_id` - State bill number (e.g., "HB 123")
- `state` - State name (e.g., "Alabama")
- `state_code` - Two-letter code (e.g., "AL")
- `title` - Bill title
- `type` - Categorization (ban/restriction/protection)
- `status` - Legislative status (introduced/enacted/failed)
- `url` - Open States bill page URL
- `session` - Legislative session
- `latest_action` - Most recent action
- `latest_action_date` - Date of action
**Example:**
```csv
bill_id,state,state_code,title,type,status,url
HB 123,Alabama,AL,Prohibit Water Fluoridation,ban,introduced,https://openstates.org/al/bills/2024/HB123/
SB 456,California,CA,Community Fluoridation Mandate,protection,enacted,https://openstates.org/ca/bills/2024/SB456/
```
### HTML Map Files
**Location:** `data/visualizations/{issue}_map.html`
**Features:**
- Interactive choropleth map
- Hover tooltips with bill details
- Zoom/pan controls
- Responsive design
- Embeddable in websites
### Legend Images
**Location:** `data/visualizations/{issue}_legend.png`
**Contains:**
- Color key for bill types
- Status indicators
- Pattern explanations
---
## Advanced Usage
### Track Specific States Only
```python
# Track only southern states
southern_states = ["AL", "AR", "FL", "GA", "KY", "LA", "MS", "NC", "SC", "TN", "TX", "VA", "WV"]
df = await tracker.track_issue("fluoridation", year=2024, states=southern_states)
```
### Multi-Year Trends
```python
import pandas as pd
dfs = []
for year in range(2020, 2025):
df = await tracker.track_issue("fluoridation", year=year)
df['year'] = year
dfs.append(df)
# Combine all years
all_years = pd.concat(dfs, ignore_index=True)
# Analyze trends
trend = all_years.groupby(['year', 'type']).size().reset_index(name='count')
print(trend)
```
**Output:**
```
year type count
2020 ban 12
2020 restriction 8
2020 protection 5
2021 ban 15
2021 restriction 10
...
```
### Export for Further Analysis
```python
# Export to Excel with multiple sheets
with pd.ExcelWriter('fluoridation_analysis.xlsx') as writer:
df.to_excel(writer, sheet_name='All Bills', index=False)
# Summary by state
state_summary = tracker.generate_state_summary(df)
state_summary.to_excel(writer, sheet_name='State Summary', index=False)
# Enacted bills only
enacted = df[df['status'] == 'enacted']
enacted.to_excel(writer, sheet_name='Enacted Bills', index=False)
```
---
## Use Cases
### 1. Advocacy Campaign Planning
**Goal:** Identify states with active legislation for targeted campaigns
```python
# Find states with pending ban legislation
df = await tracker.track_issue("fluoridation", year=2024)
pending_bans = df[(df['type'] == 'ban') & (df['status'] == 'introduced')]
print(f"States with pending fluoridation bans: {pending_bans['state_code'].unique()}")
# Output: ['AL', 'TX', 'FL', ...]
```
**Use for:**
- Email campaigns to state legislators
- Social media targeting by state
- Coalition building in key states
### 2. Policy Research
**Goal:** Track legislative trends over time
```python
# Compare ban vs protection bills over 5 years
for year in range(2020, 2025):
df = await tracker.track_issue("fluoridation", year=year)
bans = len(df[df['type'] == 'ban'])
protections = len(df[df['type'] == 'protection'])
print(f"{year}: {bans} bans, {protections} protections")
```
### 3. Media and Journalism
**Goal:** Create data-driven stories on legislative activity
```python
# Generate map for publication
df = await tracker.track_issue("fluoridation", year=2024)
tracker.create_choropleth_map(df, "fluoridation", output_file="public/fluoride_map.html")
# Embed in article with <iframe>
```
### 4. Academic Research
**Goal:** Analyze correlation between legislation and demographics
```python
# Merge with Census data
import geopandas as gpd
df = await tracker.track_issue("fluoridation", year=2024)
state_summary = tracker.generate_state_summary(df)
# Join with state-level Census data
census = pd.read_csv("census_state_demographics.csv")
merged = state_summary.merge(census, on='state_code')
# Analyze correlations
correlation = merged[['ban_count', 'median_income', 'college_educated_pct']].corr()
```
---
## Next Steps
### Integrate with Knowledge Graph
Add legislation to the jurisdiction knowledge graph:
```python
from neo4j import GraphDatabase
driver = GraphDatabase.driver("bolt://localhost:7687")
with driver.session() as session:
for _, bill in df.iterrows():
session.run("""
MATCH (j:Jurisdiction {state_code: $state_code})
CREATE (b:Bill {
bill_id: $bill_id,
title: $title,
type: $type,
status: $status,
url: $url
})
CREATE (j)-[:HAS_LEGISLATION]->(b)
""", bill.to_dict())
```
### Add to Dashboard
Embed map in React frontend:
```tsx
// frontend/src/pages/LegislationTracker.tsx
import React from 'react';
export function LegislationMap({ issue }: { issue: string }) {
return (
<iframe
src={`/data/visualizations/${issue}_map.html`}
width="100%"
height="600px"
frameBorder="0"
/>
);
}
```
### Automate Daily Updates
Create cron job to update data:
```bash
# crontab -e
# Run daily at 6am
0 6 * * * cd /path/to/project && python scripts/legislative_tracker.py --issue fluoridation --year 2024 --visualize
```
---
## Related Documentation
- [Open States API Documentation](https://docs.openstates.org/)
- [Ballotpedia Integration](../data-sources/ballot-election-sources.md)
- [Data Model ERD](../data-sources/data-model-erd.md)
- [Jurisdiction Discovery](../data-sources/jurisdiction-discovery.md)
---
## Troubleshooting
### Error: "OPENSTATES_API_KEY not found"
**Solution:** Add API key to `.env`:
```bash
OPENSTATES_API_KEY=your-key-here
```
### Error: "Plotly not installed"
**Solution:** Install visualization libraries:
```bash
pip install plotly matplotlib
```
### Error: "No bills found for issue"
**Possible causes:**
1. **Issue keyword too specific** - Broaden search terms
2. **No legislation in that year** - Try different year
3. **API quota exceeded** - Wait for next month or upgrade
**Solution:** Customize keywords:
```python
tracker.issue_keywords["fluoridation"]["ban"].append("anti-fluoride")
```
### Map shows no data
**Check:**
1. CSV file was created: `data/cache/legislation/{issue}_{year}.csv`
2. CSV has rows with state_code values
3. Categorization logic matched bills correctly
---
## Tips & Best Practices
1. **Start broad, then refine** - Use general keywords first, then add specific terms
2. **Cache aggressively** - API calls are rate-limited, save results locally
3. **Update regularly** - Legislation changes daily during session
4. **Verify categorization** - Review sample bills to ensure accuracy
5. **Document keywords** - Keep track of which keywords work best
6. **Share visualizations** - Export maps as images for social media
---
## Color Scheme Reference
### Default Colors (Customizable)
```python
color_map = {
('ban', 'enacted'): '#D2691E', # Brown
('ban', 'introduced'): '#FFA500', # Orange
('ban', 'failed'): '#FFE4B5', # Moccasin
('restriction', 'enacted'): '#DAA520', # Goldenrod
('restriction', 'introduced'): '#FFD700', # Gold
('restriction', 'failed'): '#FFFFE0', # Light Yellow
('protection', 'enacted'): '#00008B', # Dark Blue
('protection', 'introduced'): '#4169E1', # Royal Blue
('protection', 'failed'): '#87CEEB', # Sky Blue
}
```
### Custom Color Scheme
```python
# Update colors for your brand
tracker.color_map = {
('ban', 'enacted'): '#FF0000', # Red for enacted bans
('protection', 'enacted'): '#00FF00', # Green for enacted protections
}
```
|