| """ |
| Prepare compact visualization data for souls visualizations. |
| Converts enriched Joshua Project data into minimal format for browser use. |
| |
| Output: souls_enhanced_viz_data.json |
| Size target: < 3 MB (compact field names, essential data only) |
| """ |
|
|
| import json |
| import sys |
| from pathlib import Path |
|
|
| |
| COMPACT_FIELDS = { |
| 'name': 'n', |
| 'population': 'p', |
| 'jp_scale': 's', |
| 'percent_evangelical': 'e', |
| 'primary_religion': 'r', |
| 'primary_language': 'l', |
| 'language_code': 'lc', |
| 'country': 'c', |
| 'country_code': 'cc', |
| 'continent': 'cn', |
| 'region': 'rg', |
| 'affinity_bloc': 'ab', |
| 'people_cluster': 'pc', |
| 'bible_status': 'bs', |
| 'has_jesus_film': 'jf', |
| 'lat_lon': 'll', |
| 'least_reached': 'lr' |
| } |
|
|
| def load_enriched_data(): |
| """Load the enriched Joshua Project dataset.""" |
| data_file = Path(__file__).parent / 'joshua_project_enriched.json' |
|
|
| if not data_file.exists(): |
| print(f"Error: {data_file} not found") |
| print("Run create_enriched_datasets.py first!") |
| sys.exit(1) |
|
|
| print(f"Loading {data_file.name}...") |
| with open(data_file, 'r') as f: |
| data = json.load(f) |
|
|
| print(f"Loaded {len(data):,} people groups") |
| return data |
|
|
| def safe_float(value, default=0.0): |
| """Safely convert value to float.""" |
| try: |
| return float(value) if value is not None else default |
| except (ValueError, TypeError): |
| return default |
|
|
| def safe_int(value, default=0): |
| """Safely convert value to int.""" |
| try: |
| return int(value) if value is not None else default |
| except (ValueError, TypeError): |
| return default |
|
|
| def compact_group(group): |
| """Convert a people group record to compact format.""" |
| compact = { |
| 'n': group.get('PeopNameInCountry', 'Unknown'), |
| 'p': safe_int(group.get('Population', 0)), |
| 's': safe_int(group.get('JPScale', 0)), |
| 'e': round(safe_float(group.get('PercentEvangelical', 0)), 1), |
| 'r': group.get('PrimaryReligion', 'Unknown'), |
| 'l': group.get('PrimaryLanguageName', 'Unknown'), |
| 'lc': group.get('ROL3', ''), |
| 'c': group.get('country_data', {}).get('name', 'Unknown') if group.get('country_data') else group.get('Ctry', 'Unknown'), |
| 'cc': group.get('ROG3', ''), |
| 'cn': group.get('Continent', ''), |
| 'rg': group.get('RegionName', ''), |
| 'ab': group.get('AffinityBloc', ''), |
| 'pc': group.get('PeopleCluster', ''), |
| 'bs': safe_int(group.get('BibleStatus', 0)), |
| 'jf': group.get('language_data', {}).get('has_jesus_film', 'N') if group.get('language_data') else 'N', |
| 'lr': group.get('LeastReached', 'N') |
| } |
|
|
| |
| |
| lat = group.get('Latitude') or group.get('PrimaryLanguageLatitude') |
| lon = group.get('Longitude') or group.get('PrimaryLanguageLongitude') |
|
|
| if lat and lon: |
| lat_val = safe_float(lat, None) |
| lon_val = safe_float(lon, None) |
| if lat_val is not None and lon_val is not None and lat_val != 0 and lon_val != 0: |
| compact['ll'] = [lat_val, lon_val] |
|
|
| return compact |
|
|
| def generate_stats(groups): |
| """Generate summary statistics.""" |
| stats = { |
| 'total_groups': len(groups), |
| 'total_population': sum(g['p'] for g in groups), |
| 'unreached_count': sum(1 for g in groups if g['lr'] == 'Y'), |
| 'unreached_population': sum(g['p'] for g in groups if g['lr'] == 'Y'), |
|
|
| |
| 'by_religion': {}, |
|
|
| |
| 'by_continent': {}, |
|
|
| |
| 'by_affinity_bloc': {}, |
|
|
| |
| 'by_jp_scale': {str(i): 0 for i in range(1, 6)}, |
|
|
| |
| 'by_bible_status': {str(i): 0 for i in range(0, 6)} |
| } |
|
|
| for g in groups: |
| |
| if g['r'] not in stats['by_religion']: |
| stats['by_religion'][g['r']] = {'count': 0, 'population': 0, 'unreached': 0} |
| stats['by_religion'][g['r']]['count'] += 1 |
| stats['by_religion'][g['r']]['population'] += g['p'] |
| if g['lr'] == 'Y': |
| stats['by_religion'][g['r']]['unreached'] += g['p'] |
|
|
| |
| if g['cn']: |
| if g['cn'] not in stats['by_continent']: |
| stats['by_continent'][g['cn']] = {'count': 0, 'population': 0} |
| stats['by_continent'][g['cn']]['count'] += 1 |
| stats['by_continent'][g['cn']]['population'] += g['p'] |
|
|
| |
| if g['ab']: |
| if g['ab'] not in stats['by_affinity_bloc']: |
| stats['by_affinity_bloc'][g['ab']] = {'count': 0, 'population': 0} |
| stats['by_affinity_bloc'][g['ab']]['count'] += 1 |
| stats['by_affinity_bloc'][g['ab']]['population'] += g['p'] |
|
|
| |
| if g['s']: |
| stats['by_jp_scale'][str(g['s'])] += 1 |
|
|
| |
| if g['bs'] is not None: |
| stats['by_bible_status'][str(g['bs'])] += 1 |
|
|
| return stats |
|
|
| def main(): |
| print("=" * 70) |
| print("PREPARING SOULS VISUALIZATION DATA") |
| print("=" * 70) |
|
|
| |
| enriched = load_enriched_data() |
|
|
| |
| print("\nConverting to compact format...") |
| compact_groups = [] |
|
|
| for i, group in enumerate(enriched): |
| compact_groups.append(compact_group(group)) |
|
|
| if (i + 1) % 1000 == 0: |
| print(f" Progress: {i+1:,}/{len(enriched):,}") |
|
|
| print(f"\n✅ Converted {len(compact_groups):,} groups") |
|
|
| |
| print("\nGenerating statistics...") |
| stats = generate_stats(compact_groups) |
|
|
| |
| output = { |
| 'groups': compact_groups, |
| 'stats': stats, |
| 'generated': '2025-12-23', |
| 'source': 'Joshua Project API via enriched dataset' |
| } |
|
|
| |
| output_file = Path(__file__).parent.parent.parent / 'poems' / 'souls' / 'souls_enhanced_viz_data.json' |
| output_file.parent.mkdir(parents=True, exist_ok=True) |
|
|
| print(f"\nSaving to {output_file}...") |
| with open(output_file, 'w', encoding='utf-8') as f: |
| json.dump(output, f, separators=(',', ':'), ensure_ascii=False) |
|
|
| |
| size_mb = output_file.stat().st_size / (1024 * 1024) |
|
|
| print("\n" + "=" * 70) |
| print("SUMMARY") |
| print("=" * 70) |
| print(f"Output file: {output_file.name}") |
| print(f"File size: {size_mb:.2f} MB") |
| print(f"People groups: {len(compact_groups):,}") |
| print(f"Total population: {stats['total_population']:,}") |
| print(f"Unreached: {stats['unreached_count']:,} groups ({stats['unreached_population']:,} people)") |
| print(f"\nReligions: {len(stats['by_religion'])}") |
| print(f"Continents: {len(stats['by_continent'])}") |
| print(f"Affinity Blocs: {len(stats['by_affinity_bloc'])}") |
| print("\n" + "=" * 70) |
| print("✅ COMPLETE - Ready for visualization!") |
| print("=" * 70) |
|
|
| if __name__ == '__main__': |
| main() |
|
|