Download make_fixtures.py from idiouf/African-Conservation-Human-Development-Observatory: direct link, hf CLI and curl.
- Browser
- Download file 5.69 kB
-
https://huggingface.co/spaces/idiouf/African-Conservation-Human-Development-Observatory/resolve/main/make_fixtures.py
- Command line
-
hf download hf://spaces/idiouf/African-Conservation-Human-Development-Observatory/make_fixtures.py
-
curl -L -o make_fixtures.py https://huggingface.co/spaces/idiouf/African-Conservation-Human-Development-Observatory/resolve/main/make_fixtures.py
5.69 kB
| """Generate offline test doubles so the UI can be exercised without network. | |
| These files are TEST FIXTURES. The numbers are generated, not observed, and the | |
| dashboard never loads them unless CONSERVATION_HD_FIXTURES points here. | |
| """ | |
| from __future__ import annotations | |
| import json | |
| from pathlib import Path | |
| import numpy as np | |
| import pandas as pd | |
| import sys | |
| sys.path.insert(0, str(Path(__file__).resolve().parent)) | |
| import sources as src # noqa: E402 | |
| # Everything lives in one flat folder; the prefix keeps the test doubles obvious. | |
| OUT = Path(__file__).resolve().parent | |
| PREFIX = "fixture_" | |
| rng = np.random.default_rng(11) | |
| COUNTRIES = { | |
| "SEN": ("Senegal", 14.5, -14.5), "KEN": ("Kenya", 0.5, 37.9), | |
| "TZA": ("Tanzania", -6.4, 34.9), "ETH": ("Ethiopia", 9.1, 40.5), | |
| "ZAF": ("South Africa", -30.6, 22.9), "NGA": ("Nigeria", 9.1, 8.7), | |
| "ZMB": ("Zambia", -13.1, 27.8), "MOZ": ("Mozambique", -18.7, 35.5), | |
| "GHA": ("Ghana", 7.9, -1.0), "MAR": ("Morocco", 31.8, -7.1), | |
| "COD": ("Democratic Republic of the Congo", -4.0, 21.8), | |
| "BWA": ("Botswana", -22.3, 24.7), | |
| } | |
| IUCN = ["Ia", "II", "IV", "V", "VI", ""] | |
| # --- protected areas ------------------------------------------------------- | |
| rows = [] | |
| for iso3, (name, lat0, lon0) in COUNTRIES.items(): | |
| for i in range(rng.integers(12, 30)): | |
| rows.append({ | |
| "pa_id": f"Q{rng.integers(100000, 999999)}", | |
| "name": f"{name} Reserve {i + 1}", | |
| "iso3": iso3, | |
| "country": name, | |
| "lat": float(lat0 + rng.normal(0, 2.2)), | |
| "lon": float(lon0 + rng.normal(0, 2.2)), | |
| "area_km2": float(np.exp(rng.normal(6.0, 1.6))), | |
| "established": int(rng.integers(1930, 2020)), | |
| "iucn_category": str(rng.choice(IUCN)), | |
| "source": "fixture", | |
| }) | |
| pd.DataFrame(rows).to_parquet(OUT / (PREFIX + "protected_areas.parquet"), index=False) | |
| # --- world bank style indicator panel ------------------------------------- | |
| years = list(range(1990, 2025)) | |
| records = [] | |
| for iso3, (name, _, _) in COUNTRIES.items(): | |
| for code, meta in src.INDICATORS.items(): | |
| base = {"Education": 45, "Health": 58, "Income": 30, | |
| "Population": 60, "Conservation": 14}[meta["group"]] | |
| level = base * rng.uniform(0.5, 1.8) | |
| drift = np.linspace(1.0, rng.uniform(1.1, 1.9), len(years)) | |
| noise = 1 + rng.normal(0, 0.012, len(years)).cumsum() | |
| series = level * drift * noise | |
| if code == "SP.POP.TOTL": | |
| series = series * 5e5 | |
| for y, v in zip(years, series): | |
| if rng.random() < 0.08: # realistic gaps | |
| continue | |
| records.append({"iso3": iso3, "country": name, "indicator": code, | |
| "year": y, "value": float(v)}) | |
| pd.DataFrame(records).to_parquet(OUT / (PREFIX + "indicators_1990_2024.parquet"), index=False) | |
| # --- UN World Population Prospects style panel, with projections ----------- | |
| un = [] | |
| un_years = list(range(1950, 2101)) | |
| for iso3, (name, _, _) in COUNTRIES.items(): | |
| for code, meta in src.UN_INDICATORS.items(): | |
| base = {"UN:49": 2e7, "UN:51": 2.4, "UN:54": 70, "UN:67": 19, "UN:46": 3e6, | |
| "UN:41": 6e6, "UN:83": 72, "UN:84": 6, "UN:85": 15, "UN:86": 78}[code] | |
| level = base * rng.uniform(0.6, 1.5) | |
| drift = np.linspace(1.0, rng.uniform(0.7, 2.0), len(un_years)) | |
| series = level * drift * (1 + rng.normal(0, 0.008, len(un_years)).cumsum()) | |
| for y, v in zip(un_years, series): | |
| un.append({"iso3": iso3, "country": name, "indicator": code, "year": y, | |
| "value": float(abs(v)), | |
| "series": "Estimate" if y <= src.UN_LAST_ESTIMATE_YEAR else "Projection"}) | |
| pd.DataFrame(un).to_parquet(OUT / (PREFIX + "un_population.parquet"), index=False) | |
| # --- subnational hdi ------------------------------------------------------- | |
| sub = [] | |
| for iso3, (name, _, _) in COUNTRIES.items(): | |
| for r in range(4): | |
| for y in range(1995, 2023, 3): | |
| sub.append({"iso3": iso3, "country": name, "region": f"{name} region {r + 1}", | |
| "gdl_code": f"{iso3}r{r + 1}", "level": "Subnat", "year": y, | |
| "shdi": float(rng.uniform(0.3, 0.72)), | |
| "health_index": float(rng.uniform(0.3, 0.8)), | |
| "income_index": float(rng.uniform(0.25, 0.7)), | |
| "education_index": float(rng.uniform(0.2, 0.65))}) | |
| pd.DataFrame(sub).to_parquet(OUT / (PREFIX + "subnational_hdi.parquet"), index=False) | |
| # --- simple ADM0 polygons -------------------------------------------------- | |
| features = [] | |
| for iso3, (name, lat0, lon0) in COUNTRIES.items(): | |
| d = 3.0 | |
| features.append({ | |
| "type": "Feature", | |
| "properties": {"iso3": iso3, "name": name, "shapeName": name}, | |
| "geometry": {"type": "Polygon", "coordinates": [[ | |
| [lon0 - d, lat0 - d], [lon0 + d, lat0 - d], | |
| [lon0 + d, lat0 + d], [lon0 - d, lat0 + d], [lon0 - d, lat0 - d], | |
| ]]}, | |
| }) | |
| (OUT / (PREFIX + "boundaries_adm0.json")).write_text( | |
| json.dumps({"type": "FeatureCollection", "features": features})) | |
| # --- an effectiveness upload to exercise the join -------------------------- | |
| pas = pd.read_parquet(OUT / (PREFIX + "protected_areas.parquet")) | |
| sample = pas.sample(60, random_state=3) | |
| pd.DataFrame({ | |
| "name": sample["name"].values, | |
| "iso3": sample["iso3"].values, | |
| "score": np.round(rng.uniform(1.5, 4.8, len(sample)), 1), | |
| }).to_csv(OUT / (PREFIX + "effectiveness_upload.csv"), index=False) | |
| print("fixtures written to", OUT) | |
| for f in sorted(OUT.glob(PREFIX + "*")): | |
| print(" ", f.name, f.stat().st_size, "bytes") | |