Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 9,508 Bytes
896453f | 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 | #!/usr/bin/env python3
"""
Split gold parquet files by state for easier distribution and access.
This script splits large monolithic parquet files into state-specific files:
- nonprofits_organizations.parquet β nonprofits_organizations_AL.parquet, nonprofits_organizations_AK.parquet, etc.
- nonprofits_locations.parquet β nonprofits_locations_AL.parquet, nonprofits_locations_AK.parquet, etc.
- jurisdictions_*.parquet β jurisdictions_*_AL.parquet, etc.
- domains_gsa_domains.parquet β domains_gsa_domains_AL.parquet, etc.
Benefits:
- Smaller file sizes (easier downloads)
- Faster queries (load only needed states)
- Better HuggingFace upload (avoid file size limits)
Usage:
# Split all files
python scripts/split_gold_by_state.py --all
# Split specific file
python scripts/split_gold_by_state.py --file nonprofits_organizations.parquet
# Dry run (see what would happen)
python scripts/split_gold_by_state.py --all --dry-run
"""
import argparse
from pathlib import Path
import pandas as pd
from loguru import logger
from typing import Dict, List
class GoldFileSplitter:
"""Split gold parquet files by state."""
# Files that have direct 'state' column
STATE_COLUMN_FILES = {
'nonprofits_organizations.parquet': 'state',
'nonprofits_locations.parquet': 'state',
'nonprofits_financials.parquet': 'state',
'nonprofits_programs.parquet': 'state',
'nonprofits_tuscaloosa_form990.parquet': 'state',
}
# Files that have 'State' column (capitalized)
STATE_UPPER_FILES = {
'domains_gsa_domains.parquet': 'State',
}
# Files that have 'USPS' column (state abbreviation)
USPS_FILES = {
'jurisdictions_cities.parquet': 'USPS',
'jurisdictions_counties.parquet': 'USPS',
'jurisdictions_school_districts.parquet': 'USPS',
'jurisdictions_townships.parquet': 'USPS',
}
def __init__(self, gold_dir: str = "data/gold", output_dir: str = None):
"""
Initialize splitter.
DEPRECATED: Use create_partitioned_datasets.py instead for better performance.
Args:
gold_dir: Directory containing gold parquet files
output_dir: Directory to write split files (defaults to gold_dir/by_state/)
"""
self.gold_dir = Path(gold_dir)
self.output_dir = Path(output_dir) if output_dir else self.gold_dir / "by_state"
# Create output directory
self.output_dir.mkdir(parents=True, exist_ok=True)
# Combined mapping of all files to split
self.all_files = {
**self.STATE_COLUMN_FILES,
**self.STATE_UPPER_FILES,
**self.USPS_FILES,
}
def get_state_column(self, filename: str) -> str:
"""Get the state column name for a file."""
if filename in self.STATE_COLUMN_FILES:
return self.STATE_COLUMN_FILES[filename]
elif filename in self.STATE_UPPER_FILES:
return self.STATE_UPPER_FILES[filename]
elif filename in self.USPS_FILES:
return self.USPS_FILES[filename]
else:
raise ValueError(f"Unknown file: {filename}")
def split_file(self, filename: str, dry_run: bool = False) -> Dict[str, int]:
"""
Split a single parquet file by state.
Args:
filename: Name of file to split (e.g., 'nonprofits_organizations.parquet')
dry_run: If True, only report what would be done
Returns:
Dict mapping state abbreviation to record count
"""
input_path = self.gold_dir / filename
if not input_path.exists():
logger.warning(f"File not found: {input_path}")
return {}
logger.info(f"π Processing: {filename}")
# Read the file
df = pd.read_parquet(input_path)
logger.info(f" Total records: {len(df):,}")
# Get state column
state_col = self.get_state_column(filename)
if state_col not in df.columns:
logger.error(f" β Column '{state_col}' not found in {filename}")
logger.error(f" Available columns: {df.columns.tolist()}")
return {}
# Get unique states
unique_states = sorted(df[state_col].dropna().unique())
logger.info(f" Unique states: {len(unique_states)}")
# Split by state
state_counts = {}
base_name = filename.replace('.parquet', '')
for state in unique_states:
state_df = df[df[state_col] == state]
count = len(state_df)
state_counts[state] = count
# Create output filename
output_filename = f"{base_name}_{state}.parquet"
output_path = self.output_dir / output_filename
if dry_run:
logger.info(f" [DRY RUN] Would create: {output_filename} ({count:,} records)")
else:
# Write state-specific file
state_df.to_parquet(output_path, index=False, engine='pyarrow')
size_mb = output_path.stat().st_size / 1024 / 1024
logger.success(f" β
Created: {output_filename} ({count:,} records, {size_mb:.2f} MB)")
return state_counts
def split_all(self, dry_run: bool = False) -> None:
"""
Split all configured files by state.
Args:
dry_run: If True, only report what would be done
"""
logger.info("π Splitting all gold files by state...")
logger.info(f" Input directory: {self.gold_dir}")
logger.info(f" Output directory: {self.output_dir}")
logger.info("")
total_files = 0
total_states = 0
for filename in self.all_files.keys():
try:
state_counts = self.split_file(filename, dry_run=dry_run)
if state_counts:
total_files += 1
total_states += len(state_counts)
logger.info("")
except Exception as e:
logger.error(f"β Error processing {filename}: {e}")
logger.info("")
logger.success("=" * 60)
logger.success(f"β
Split {total_files} files into {total_states} state-specific files")
logger.success(f"π Output directory: {self.output_dir}")
logger.success("=" * 60)
def list_split_files(self) -> List[Path]:
"""List all split files in the output directory."""
return sorted(self.output_dir.glob("*.parquet"))
def get_split_stats(self) -> pd.DataFrame:
"""Get statistics about split files."""
files = self.list_split_files()
stats = []
for f in files:
df = pd.read_parquet(f)
stats.append({
'filename': f.name,
'records': len(df),
'size_mb': f.stat().st_size / 1024 / 1024,
'columns': len(df.columns)
})
return pd.DataFrame(stats)
def main():
parser = argparse.ArgumentParser(
description="Split gold parquet files by state",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Split all files
python scripts/split_gold_by_state.py --all
# Split specific file
python scripts/split_gold_by_state.py --file nonprofits_organizations.parquet
# Dry run (see what would happen)
python scripts/split_gold_by_state.py --all --dry-run
# View statistics
python scripts/split_gold_by_state.py --stats
"""
)
parser.add_argument('--all', action='store_true',
help='Split all configured files')
parser.add_argument('--file', type=str,
help='Split a specific file')
parser.add_argument('--dry-run', action='store_true',
help='Show what would be done without actually splitting')
parser.add_argument('--stats', action='store_true',
help='Show statistics about split files')
parser.add_argument('--gold-dir', type=str, default='data/gold',
help='Directory containing gold parquet files (default: data/gold)')
parser.add_argument('--output-dir', type=str,
help='Output directory for split files (default: data/gold/by_state)')
args = parser.parse_args()
# Initialize splitter
splitter = GoldFileSplitter(
gold_dir=args.gold_dir,
output_dir=args.output_dir
)
# Handle commands
if args.stats:
logger.info("π Split file statistics:")
stats_df = splitter.get_split_stats()
if len(stats_df) == 0:
logger.warning("No split files found. Run with --all first.")
else:
print(stats_df.to_string(index=False))
print(f"\nTotal files: {len(stats_df)}")
print(f"Total records: {stats_df['records'].sum():,}")
print(f"Total size: {stats_df['size_mb'].sum():.2f} MB")
elif args.all:
splitter.split_all(dry_run=args.dry_run)
elif args.file:
splitter.split_file(args.file, dry_run=args.dry_run)
else:
parser.print_help()
if __name__ == "__main__":
main()
|