unicefdata 2.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- unicefdata/__init__.py +83 -0
- unicefdata/config.py +156 -0
- unicefdata/config_loader.py +270 -0
- unicefdata/flows.py +217 -0
- unicefdata/indicator_registry.py +775 -0
- unicefdata/metadata/_unicefdata_sync_history.yaml +115 -0
- unicefdata/metadata/current/_dataflow_fallback_sequences.yaml +278 -0
- unicefdata/metadata/current/_unicefdata_codelists.yaml +641 -0
- unicefdata/metadata/current/_unicefdata_countries.yaml +464 -0
- unicefdata/metadata/current/_unicefdata_dataflows.yaml +639 -0
- unicefdata/metadata/current/_unicefdata_indicators.yaml +7270 -0
- unicefdata/metadata/current/_unicefdata_indicators_metadata.yaml +12860 -0
- unicefdata/metadata/current/_unicefdata_regions.yaml +123 -0
- unicefdata/metadata/current/dataflow_index.yaml +356 -0
- unicefdata/metadata/current/dataflows/CAUSE_OF_DEATH.yaml +52 -0
- unicefdata/metadata/current/dataflows/CCRI.yaml +137 -0
- unicefdata/metadata/current/dataflows/CHILD_RELATED_SDG.yaml +144 -0
- unicefdata/metadata/current/dataflows/CHLD_PVTY.yaml +77 -0
- unicefdata/metadata/current/dataflows/CME.yaml +142 -0
- unicefdata/metadata/current/dataflows/CME_CAUSE_OF_DEATH.yaml +96 -0
- unicefdata/metadata/current/dataflows/CME_COUNTRY_PROFILES_DATA.yaml +100 -0
- unicefdata/metadata/current/dataflows/CME_DF_2021_WQ.yaml +280 -0
- unicefdata/metadata/current/dataflows/CME_SUBNATIONAL.yaml +202 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_AGO.yaml +179 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_BDI.yaml +179 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_BEN.yaml +179 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_BGD.yaml +179 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_CMR.yaml +179 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_ETH.yaml +179 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_GHA.yaml +179 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_GIN.yaml +179 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_HTI.yaml +179 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_KEN.yaml +179 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_LAO.yaml +179 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_LBR.yaml +179 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_LSO.yaml +179 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_MDG.yaml +185 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_MLI.yaml +179 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_MMR.yaml +179 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_MRT.yaml +179 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_MWI.yaml +179 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_NAM.yaml +179 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_NGA.yaml +179 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_NPL.yaml +179 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_PAK.yaml +179 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_RWA.yaml +179 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_SEN.yaml +179 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_SLE.yaml +179 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_TCD.yaml +179 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_TGO.yaml +174 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_TZA.yaml +179 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_UGA.yaml +178 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_ZMB.yaml +178 -0
- unicefdata/metadata/current/dataflows/CME_SUBNAT_ZWE.yaml +179 -0
- unicefdata/metadata/current/dataflows/COVID.yaml +145 -0
- unicefdata/metadata/current/dataflows/COVID_CASES.yaml +89 -0
- unicefdata/metadata/current/dataflows/DM.yaml +121 -0
- unicefdata/metadata/current/dataflows/DM_PROJECTIONS.yaml +118 -0
- unicefdata/metadata/current/dataflows/ECD.yaml +272 -0
- unicefdata/metadata/current/dataflows/ECONOMIC.yaml +103 -0
- unicefdata/metadata/current/dataflows/EDUCATION.yaml +156 -0
- unicefdata/metadata/current/dataflows/EDUCATION_FLS.yaml +66 -0
- unicefdata/metadata/current/dataflows/EDUCATION_UIS_SDG.yaml +119 -0
- unicefdata/metadata/current/dataflows/FUNCTIONAL_DIFF.yaml +250 -0
- unicefdata/metadata/current/dataflows/GENDER.yaml +138 -0
- unicefdata/metadata/current/dataflows/GLOBAL_DATAFLOW.yaml +550 -0
- unicefdata/metadata/current/dataflows/HIV_AIDS.yaml +205 -0
- unicefdata/metadata/current/dataflows/IMMUNISATION.yaml +155 -0
- unicefdata/metadata/current/dataflows/MG.yaml +147 -0
- unicefdata/metadata/current/dataflows/MNCH.yaml +285 -0
- unicefdata/metadata/current/dataflows/NUTRITION.yaml +293 -0
- unicefdata/metadata/current/dataflows/PT.yaml +320 -0
- unicefdata/metadata/current/dataflows/PT_CM.yaml +297 -0
- unicefdata/metadata/current/dataflows/PT_CM_SUBNATIONAL.yaml +228 -0
- unicefdata/metadata/current/dataflows/PT_CONFLICT.yaml +116 -0
- unicefdata/metadata/current/dataflows/PT_FGM.yaml +292 -0
- unicefdata/metadata/current/dataflows/SDG_PROG_ASSESSMENT.yaml +124 -0
- unicefdata/metadata/current/dataflows/SOC_PROTECTION.yaml +140 -0
- unicefdata/metadata/current/dataflows/WASH_HEALTHCARE_FACILITY.yaml +139 -0
- unicefdata/metadata/current/dataflows/WASH_HOUSEHOLDS.yaml +146 -0
- unicefdata/metadata/current/dataflows/WASH_HOUSEHOLD_MH.yaml +122 -0
- unicefdata/metadata/current/dataflows/WASH_HOUSEHOLD_SUBNAT.yaml +219 -0
- unicefdata/metadata/current/dataflows/WASH_SCHOOLS.yaml +123 -0
- unicefdata/metadata/current/dataflows/WT.yaml +237 -0
- unicefdata/metadata/current/unicef_indicators_metadata.yaml +4419 -0
- unicefdata/metadata/vintages/2025-12-03/codelists.yaml +574 -0
- unicefdata/metadata/vintages/2025-12-03/dataflows.yaml +626 -0
- unicefdata/metadata/vintages/2025-12-03/indicators.yaml +230 -0
- unicefdata/metadata/vintages/2025-12-03/summary.yaml +5 -0
- unicefdata/metadata/vintages/2025-12-05/_unicefdata_codelists.yaml +628 -0
- unicefdata/metadata/vintages/2025-12-05/_unicefdata_countries.yaml +462 -0
- unicefdata/metadata/vintages/2025-12-05/_unicefdata_dataflows.yaml +630 -0
- unicefdata/metadata/vintages/2025-12-05/_unicefdata_indicators.yaml +248 -0
- unicefdata/metadata/vintages/2025-12-05/_unicefdata_regions.yaml +121 -0
- unicefdata/metadata/vintages/2025-12-05/summary.yaml +7 -0
- unicefdata/metadata/vintages/2025-12-07/_unicefdata_codelists.yaml +633 -0
- unicefdata/metadata/vintages/2025-12-07/_unicefdata_countries.yaml +464 -0
- unicefdata/metadata/vintages/2025-12-07/_unicefdata_dataflows.yaml +630 -0
- unicefdata/metadata/vintages/2025-12-07/_unicefdata_indicators.yaml +249 -0
- unicefdata/metadata/vintages/2025-12-07/_unicefdata_regions.yaml +123 -0
- unicefdata/metadata/vintages/2025-12-07/summary.yaml +7 -0
- unicefdata/metadata/vintages/2026-01-21/_unicefdata_codelists.yaml +641 -0
- unicefdata/metadata/vintages/2026-01-21/_unicefdata_countries.yaml +464 -0
- unicefdata/metadata/vintages/2026-01-21/_unicefdata_dataflows.yaml +639 -0
- unicefdata/metadata/vintages/2026-01-21/_unicefdata_indicators.yaml +9674 -0
- unicefdata/metadata/vintages/2026-01-21/_unicefdata_regions.yaml +123 -0
- unicefdata/metadata/vintages/2026-01-21/summary.yaml +7 -0
- unicefdata/metadata/vintages/2026-01-25/_unicefdata_codelists.yaml +641 -0
- unicefdata/metadata/vintages/2026-01-25/_unicefdata_countries.yaml +464 -0
- unicefdata/metadata/vintages/2026-01-25/_unicefdata_dataflows.yaml +639 -0
- unicefdata/metadata/vintages/2026-01-25/_unicefdata_indicators.yaml +9674 -0
- unicefdata/metadata/vintages/2026-01-25/_unicefdata_regions.yaml +123 -0
- unicefdata/metadata/vintages/2026-01-25/summary.yaml +7 -0
- unicefdata/metadata.py +1084 -0
- unicefdata/metadata_manager.py +281 -0
- unicefdata/run_sync.py +20 -0
- unicefdata/schema_cache.py +283 -0
- unicefdata/schema_sync.py +561 -0
- unicefdata/sdmx.py +166 -0
- unicefdata/sdmx_client.py +1388 -0
- unicefdata/unicefdata.py +1192 -0
- unicefdata/utils.py +616 -0
- unicefdata/yaml_formatter.py +333 -0
- unicefdata-2.1.0.dist-info/METADATA +358 -0
- unicefdata-2.1.0.dist-info/RECORD +128 -0
- unicefdata-2.1.0.dist-info/WHEEL +5 -0
- unicefdata-2.1.0.dist-info/licenses/LICENSE +21 -0
- unicefdata-2.1.0.dist-info/top_level.txt +1 -0
unicefdata/__init__.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"""
|
|
2
|
+
unicefdata: Python library for downloading UNICEF indicators via SDMX API
|
|
3
|
+
|
|
4
|
+
This library provides a simplified interface for fetching child welfare and development
|
|
5
|
+
indicators from UNICEF's SDMX data repository.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
__version__ = "2.1.0"
|
|
9
|
+
|
|
10
|
+
def build_user_agent() -> str:
|
|
11
|
+
"""Build a User-Agent string including package version and Python runtime."""
|
|
12
|
+
import platform
|
|
13
|
+
py_ver = platform.python_version()
|
|
14
|
+
system = platform.system()
|
|
15
|
+
return f"unicefData-Python/{__version__} (Python/{py_ver}; {system}) (+https://github.com/unicef-drp/unicefData)"
|
|
16
|
+
__author__ = "Joao Pedro Azevedo"
|
|
17
|
+
__email__ = "jpazevedo@unicef.org"
|
|
18
|
+
__url__ = "https://jpazvd.github.io/"
|
|
19
|
+
|
|
20
|
+
from unicefdata.unicefdata import unicefData, unicefdata, parse_year, clear_cache
|
|
21
|
+
from unicefdata.sdmx import get_sdmx
|
|
22
|
+
from unicefdata.flows import list_dataflows, dataflow_schema, print_dataflow_schema
|
|
23
|
+
from unicefdata.indicator_registry import (
|
|
24
|
+
get_dataflow_for_indicator,
|
|
25
|
+
get_indicator_info,
|
|
26
|
+
list_indicators,
|
|
27
|
+
search_indicators,
|
|
28
|
+
list_categories,
|
|
29
|
+
)
|
|
30
|
+
from unicefdata.utils import (
|
|
31
|
+
validate_country_codes,
|
|
32
|
+
load_country_codes,
|
|
33
|
+
clean_dataframe,
|
|
34
|
+
)
|
|
35
|
+
from unicefdata.sdmx_client import (
|
|
36
|
+
UNICEFSDMXClient,
|
|
37
|
+
SDMXError,
|
|
38
|
+
SDMXBadRequestError,
|
|
39
|
+
SDMXNotFoundError,
|
|
40
|
+
SDMXServerError,
|
|
41
|
+
SDMXTimeoutError,
|
|
42
|
+
)
|
|
43
|
+
# Note: COMMON_INDICATORS and UNICEF_DATAFLOWS removed
|
|
44
|
+
# Use UNICEFSDMXClient._indicators_metadata instead
|
|
45
|
+
from unicefdata.metadata import (
|
|
46
|
+
list_vintages,
|
|
47
|
+
compare_vintages,
|
|
48
|
+
sync_metadata,
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
__all__ = [
|
|
52
|
+
# Primary functions
|
|
53
|
+
"unicefData",
|
|
54
|
+
"unicefdata", # lowercase alias for Stata compatibility
|
|
55
|
+
"parse_year", # year parameter parser
|
|
56
|
+
"clear_cache", # unified cache clearing
|
|
57
|
+
"get_sdmx",
|
|
58
|
+
# Discovery functions
|
|
59
|
+
"list_dataflows",
|
|
60
|
+
"dataflow_schema",
|
|
61
|
+
"print_dataflow_schema",
|
|
62
|
+
"list_indicators",
|
|
63
|
+
"search_indicators",
|
|
64
|
+
"list_categories",
|
|
65
|
+
# Utility functions
|
|
66
|
+
"get_dataflow_for_indicator",
|
|
67
|
+
"get_indicator_info",
|
|
68
|
+
"validate_country_codes",
|
|
69
|
+
"load_country_codes",
|
|
70
|
+
"clean_dataframe",
|
|
71
|
+
# Classes
|
|
72
|
+
"UNICEFSDMXClient",
|
|
73
|
+
# Exceptions
|
|
74
|
+
"SDMXError",
|
|
75
|
+
"SDMXBadRequestError",
|
|
76
|
+
"SDMXNotFoundError",
|
|
77
|
+
"SDMXServerError",
|
|
78
|
+
"SDMXTimeoutError",
|
|
79
|
+
# Metadata
|
|
80
|
+
"list_vintages",
|
|
81
|
+
"compare_vintages",
|
|
82
|
+
"sync_metadata",
|
|
83
|
+
]
|
unicefdata/config.py
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Configuration for UNICEF SDMX API
|
|
3
|
+
==================================
|
|
4
|
+
|
|
5
|
+
Dataflow definitions, indicator mappings, and API endpoint configurations.
|
|
6
|
+
|
|
7
|
+
This module provides both:
|
|
8
|
+
1. Hardcoded fallback indicator/dataflow definitions
|
|
9
|
+
2. Support for loading from shared config/indicators.yaml
|
|
10
|
+
|
|
11
|
+
The shared YAML config is the source of truth - hardcoded values are fallbacks.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from typing import Dict, List, Optional
|
|
15
|
+
import os
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
# ============================================================================
|
|
19
|
+
# API Configuration
|
|
20
|
+
# ============================================================================
|
|
21
|
+
|
|
22
|
+
UNICEF_API_BASE_URL = "https://sdmx.data.unicef.org/ws/public/sdmxapi/rest"
|
|
23
|
+
UNICEF_AGENCY = "UNICEF"
|
|
24
|
+
DEFAULT_VERSION = "1.0"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
# ============================================================================
|
|
28
|
+
# Shared Config Loading
|
|
29
|
+
# ============================================================================
|
|
30
|
+
|
|
31
|
+
def _try_load_shared_config():
|
|
32
|
+
"""Try to load indicators from shared YAML config file.
|
|
33
|
+
|
|
34
|
+
Returns:
|
|
35
|
+
Tuple of (indicators_dict, dataflows_dict) or (None, None) if not found
|
|
36
|
+
"""
|
|
37
|
+
try:
|
|
38
|
+
from unicefdata.config_loader import load_indicators, load_dataflows
|
|
39
|
+
return load_indicators(), load_dataflows()
|
|
40
|
+
except (ImportError, FileNotFoundError):
|
|
41
|
+
return None, None
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
# ============================================================================
|
|
45
|
+
# UNICEF Dataflows
|
|
46
|
+
# ============================================================================
|
|
47
|
+
# REMOVED: Hardcoded UNICEF_DATAFLOWS dictionary
|
|
48
|
+
# All dataflow information now loaded from:
|
|
49
|
+
# - _unicefdata_indicators_metadata.yaml (comprehensive indicators metadata)
|
|
50
|
+
# - _dataflow_fallback_sequences.yaml (prefix-based fallback sequences)
|
|
51
|
+
#
|
|
52
|
+
# This ensures all platforms (Stata, Python, R) use identical canonical metadata.
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
# ============================================================================
|
|
56
|
+
# Common Indicators (SDG-related)
|
|
57
|
+
# ============================================================================
|
|
58
|
+
# REMOVED: Hardcoded COMMON_INDICATORS dictionary
|
|
59
|
+
# All indicator metadata now loaded from:
|
|
60
|
+
# - _unicefdata_indicators_metadata.yaml (733 indicators with full metadata)
|
|
61
|
+
#
|
|
62
|
+
# To access indicator metadata, use UNICEFSDMXClient._indicators_metadata
|
|
63
|
+
# loaded at client initialization from canonical YAML source.
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
# ============================================================================
|
|
67
|
+
# Helper Functions
|
|
68
|
+
# ============================================================================
|
|
69
|
+
|
|
70
|
+
# REMOVED: get_dataflow_for_indicator() function
|
|
71
|
+
# Dataflow resolution now handled by UNICEFSDMXClient._get_fallback_dataflows()
|
|
72
|
+
# which uses direct O(1) lookup from _indicators_metadata dictionary.
|
|
73
|
+
#
|
|
74
|
+
# For backward compatibility with external code that may import this function,
|
|
75
|
+
# use the sdmx_client instead:
|
|
76
|
+
# from unicefdata.sdmx_client import UNICEFSDMXClient
|
|
77
|
+
# client = UNICEFSDMXClient()
|
|
78
|
+
# dataflows = client._get_fallback_dataflows(indicator_code, 'GLOBAL_DATAFLOW')
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
# REMOVED: get_indicator_metadata() function
|
|
82
|
+
# Indicator metadata now accessed via UNICEFSDMXClient._indicators_metadata
|
|
83
|
+
#
|
|
84
|
+
# For backward compatibility:
|
|
85
|
+
# from unicefdata.sdmx_client import UNICEFSDMXClient
|
|
86
|
+
# client = UNICEFSDMXClient()
|
|
87
|
+
# metadata = client._indicators_metadata.get(indicator_code)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
# REMOVED: list_indicators_by_sdg() function
|
|
91
|
+
# SDG filtering now available via comprehensive metadata:
|
|
92
|
+
# client._indicators_metadata with sdg_target field
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
# REMOVED: list_indicators_by_dataflow() function
|
|
96
|
+
# Dataflow filtering now available via comprehensive metadata:
|
|
97
|
+
# [code for code, meta in client._indicators_metadata.items() if meta.get('dataflow') == dataflow]
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
# REMOVED: get_all_sdg_targets() function
|
|
101
|
+
# SDG target enumeration now available via comprehensive metadata:
|
|
102
|
+
# sorted(set(meta.get('sdg_target') for meta in client._indicators_metadata.values() if meta.get('sdg_target')))
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
# ============================================================================
|
|
106
|
+
# Dynamic Config Loading (with fallback to hardcoded values)
|
|
107
|
+
# ============================================================================
|
|
108
|
+
|
|
109
|
+
# REMOVED: get_indicators() function
|
|
110
|
+
# Indicator definitions now loaded via UNICEFSDMXClient._load_indicators_metadata()
|
|
111
|
+
# All 733 indicators available from canonical YAML at client initialization.
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
# REMOVED: get_dataflows() function
|
|
115
|
+
# Dataflow information now embedded in indicators metadata.
|
|
116
|
+
# Access via: client._indicators_metadata[code]['dataflow']
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def get_indicator_codes(
|
|
120
|
+
category: Optional[str] = None,
|
|
121
|
+
sdg_goal: Optional[str] = None,
|
|
122
|
+
dataflow: Optional[str] = None,
|
|
123
|
+
use_shared_config: bool = True
|
|
124
|
+
) -> List[str]:
|
|
125
|
+
"""Get list of indicator codes with optional filtering.
|
|
126
|
+
|
|
127
|
+
Args:
|
|
128
|
+
category: Filter by category (e.g., 'mortality', 'nutrition')
|
|
129
|
+
sdg_goal: Filter by SDG goal (e.g., '3', '4')
|
|
130
|
+
dataflow: Filter by dataflow (e.g., 'CME', 'NUTRITION')
|
|
131
|
+
use_shared_config: If True, try to load from shared YAML config first
|
|
132
|
+
|
|
133
|
+
Returns:
|
|
134
|
+
List of indicator codes matching the filters
|
|
135
|
+
|
|
136
|
+
Example:
|
|
137
|
+
>>> codes = get_indicator_codes(category='mortality')
|
|
138
|
+
['CME_MRM0', 'CME_MRY0T4']
|
|
139
|
+
|
|
140
|
+
>>> codes = get_indicator_codes(sdg_goal='3')
|
|
141
|
+
['CME_MRM0', 'CME_MRY0T4', 'IM_DTP3', 'IM_MCV1', ...]
|
|
142
|
+
"""
|
|
143
|
+
indicators = get_indicators(use_shared_config)
|
|
144
|
+
|
|
145
|
+
result = []
|
|
146
|
+
for code, info in indicators.items():
|
|
147
|
+
# Apply filters
|
|
148
|
+
if category and info.get('category') != category:
|
|
149
|
+
continue
|
|
150
|
+
if sdg_goal and not info.get('sdg', '').startswith(f"{sdg_goal}."):
|
|
151
|
+
continue
|
|
152
|
+
if dataflow and info.get('dataflow') != dataflow:
|
|
153
|
+
continue
|
|
154
|
+
result.append(code)
|
|
155
|
+
|
|
156
|
+
return sorted(result)
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Shared Configuration Loader
|
|
3
|
+
============================
|
|
4
|
+
|
|
5
|
+
Loads indicator and dataflow configurations from the shared YAML config file.
|
|
6
|
+
This ensures R and Python packages use identical indicator definitions.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import os
|
|
10
|
+
import yaml
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from typing import Dict, Any, Optional, List
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def get_shared_indicators_path() -> Path:
|
|
16
|
+
"""Get path to the shared common_indicators.yaml config file.
|
|
17
|
+
|
|
18
|
+
Searches in order:
|
|
19
|
+
1. UNICEF_SHARED_CONFIG_PATH environment variable
|
|
20
|
+
2. ../../config/common_indicators.yaml relative to this file
|
|
21
|
+
3. ./config/common_indicators.yaml relative to current working directory
|
|
22
|
+
|
|
23
|
+
Returns:
|
|
24
|
+
Path to common_indicators.yaml
|
|
25
|
+
|
|
26
|
+
Raises:
|
|
27
|
+
FileNotFoundError: If config file not found
|
|
28
|
+
"""
|
|
29
|
+
# Check environment variable first
|
|
30
|
+
env_path = os.environ.get('UNICEF_SHARED_CONFIG_PATH')
|
|
31
|
+
if env_path:
|
|
32
|
+
path = Path(env_path)
|
|
33
|
+
if path.exists():
|
|
34
|
+
return path
|
|
35
|
+
|
|
36
|
+
# Check relative to this module
|
|
37
|
+
module_dir = Path(__file__).parent
|
|
38
|
+
relative_path = module_dir.parent.parent / 'config' / 'common_indicators.yaml'
|
|
39
|
+
if relative_path.exists():
|
|
40
|
+
return relative_path
|
|
41
|
+
|
|
42
|
+
# Check relative to cwd
|
|
43
|
+
cwd_path = Path.cwd() / 'config' / 'common_indicators.yaml'
|
|
44
|
+
if cwd_path.exists():
|
|
45
|
+
return cwd_path
|
|
46
|
+
|
|
47
|
+
raise FileNotFoundError(
|
|
48
|
+
"Could not find common_indicators.yaml config file. "
|
|
49
|
+
"Set UNICEF_SHARED_CONFIG_PATH environment variable or ensure config/common_indicators.yaml exists."
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def load_shared_indicators() -> Dict[str, Dict[str, Any]]:
|
|
54
|
+
"""Load indicator definitions from shared common_indicators.yaml.
|
|
55
|
+
|
|
56
|
+
This ensures Python, R, and Stata use identical indicator definitions.
|
|
57
|
+
|
|
58
|
+
Returns dictionary compatible with COMMON_INDICATORS format:
|
|
59
|
+
{
|
|
60
|
+
"CME_MRY0T4": {
|
|
61
|
+
"code": "CME_MRY0T4",
|
|
62
|
+
"name": "Under-5 mortality rate",
|
|
63
|
+
"dataflow": "CME",
|
|
64
|
+
"sdg": "3.2.1",
|
|
65
|
+
"unit": "Deaths per 1,000 live births",
|
|
66
|
+
},
|
|
67
|
+
...
|
|
68
|
+
}
|
|
69
|
+
"""
|
|
70
|
+
config_path = get_shared_indicators_path()
|
|
71
|
+
|
|
72
|
+
with open(config_path, 'r', encoding='utf-8') as f:
|
|
73
|
+
config = yaml.safe_load(f)
|
|
74
|
+
|
|
75
|
+
indicators = config.get('COMMON_INDICATORS', {})
|
|
76
|
+
|
|
77
|
+
# Transform to match expected format (rename sdg_target to sdg if needed)
|
|
78
|
+
result = {}
|
|
79
|
+
for code, info in indicators.items():
|
|
80
|
+
result[code] = {
|
|
81
|
+
'code': info.get('code', code),
|
|
82
|
+
'name': info.get('name', code),
|
|
83
|
+
'dataflow': info.get('dataflow'),
|
|
84
|
+
'sdg': info.get('sdg'), # Already named 'sdg' in common_indicators.yaml
|
|
85
|
+
'unit': info.get('unit'),
|
|
86
|
+
'description': info.get('description'),
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return result
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def get_config_path() -> Path:
|
|
93
|
+
"""Get path to the shared indicators.yaml config file.
|
|
94
|
+
|
|
95
|
+
Searches in order:
|
|
96
|
+
1. UNICEF_CONFIG_PATH environment variable
|
|
97
|
+
2. ../../config/indicators.yaml relative to this file
|
|
98
|
+
3. ./config/indicators.yaml relative to current working directory
|
|
99
|
+
|
|
100
|
+
Returns:
|
|
101
|
+
Path to indicators.yaml
|
|
102
|
+
|
|
103
|
+
Raises:
|
|
104
|
+
FileNotFoundError: If config file not found
|
|
105
|
+
"""
|
|
106
|
+
# Check environment variable first
|
|
107
|
+
env_path = os.environ.get('UNICEF_CONFIG_PATH')
|
|
108
|
+
if env_path:
|
|
109
|
+
path = Path(env_path)
|
|
110
|
+
if path.exists():
|
|
111
|
+
return path
|
|
112
|
+
|
|
113
|
+
# Check relative to this module
|
|
114
|
+
module_dir = Path(__file__).parent
|
|
115
|
+
relative_path = module_dir.parent.parent / 'config' / 'indicators.yaml'
|
|
116
|
+
if relative_path.exists():
|
|
117
|
+
return relative_path
|
|
118
|
+
|
|
119
|
+
# Check relative to cwd
|
|
120
|
+
cwd_path = Path.cwd() / 'config' / 'indicators.yaml'
|
|
121
|
+
if cwd_path.exists():
|
|
122
|
+
return cwd_path
|
|
123
|
+
|
|
124
|
+
raise FileNotFoundError(
|
|
125
|
+
"Could not find indicators.yaml config file. "
|
|
126
|
+
"Set UNICEF_CONFIG_PATH environment variable or ensure config/indicators.yaml exists."
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def load_config(config_path: Optional[Path] = None) -> Dict[str, Any]:
|
|
131
|
+
"""Load the full configuration from YAML.
|
|
132
|
+
|
|
133
|
+
Args:
|
|
134
|
+
config_path: Optional explicit path to config file
|
|
135
|
+
|
|
136
|
+
Returns:
|
|
137
|
+
Full configuration dictionary
|
|
138
|
+
"""
|
|
139
|
+
if config_path is None:
|
|
140
|
+
config_path = get_config_path()
|
|
141
|
+
|
|
142
|
+
with open(config_path, 'r', encoding='utf-8') as f:
|
|
143
|
+
return yaml.safe_load(f)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def load_indicators(config_path: Optional[Path] = None) -> Dict[str, Dict[str, Any]]:
|
|
147
|
+
"""Load indicator definitions from shared config.
|
|
148
|
+
|
|
149
|
+
Returns dictionary compatible with COMMON_INDICATORS format:
|
|
150
|
+
{
|
|
151
|
+
"CME_MRY0T4": {
|
|
152
|
+
"code": "CME_MRY0T4",
|
|
153
|
+
"name": "Under-5 mortality rate",
|
|
154
|
+
"dataflow": "CME",
|
|
155
|
+
"sdg": "3.2.1",
|
|
156
|
+
"unit": "Deaths per 1,000 live births",
|
|
157
|
+
},
|
|
158
|
+
...
|
|
159
|
+
}
|
|
160
|
+
"""
|
|
161
|
+
config = load_config(config_path)
|
|
162
|
+
indicators = config.get('indicators', {})
|
|
163
|
+
|
|
164
|
+
# Transform to COMMON_INDICATORS format (rename sdg_target to sdg)
|
|
165
|
+
result = {}
|
|
166
|
+
for code, info in indicators.items():
|
|
167
|
+
result[code] = {
|
|
168
|
+
'code': info.get('code', code),
|
|
169
|
+
'name': info.get('name', code),
|
|
170
|
+
'dataflow': info.get('dataflow'),
|
|
171
|
+
'sdg': info.get('sdg_target'), # Rename for compatibility
|
|
172
|
+
'unit': info.get('unit'),
|
|
173
|
+
'category': info.get('category'),
|
|
174
|
+
'description': info.get('description'),
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return result
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def load_dataflows(config_path: Optional[Path] = None) -> Dict[str, Dict[str, Any]]:
|
|
181
|
+
"""Load dataflow definitions from shared config."""
|
|
182
|
+
config = load_config(config_path)
|
|
183
|
+
return config.get('dataflows', {})
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def load_categories(config_path: Optional[Path] = None) -> Dict[str, Dict[str, Any]]:
|
|
187
|
+
"""Load category definitions from shared config."""
|
|
188
|
+
config = load_config(config_path)
|
|
189
|
+
return config.get('categories', {})
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def get_indicators_by_category(
|
|
193
|
+
category: str,
|
|
194
|
+
config_path: Optional[Path] = None
|
|
195
|
+
) -> List[str]:
|
|
196
|
+
"""Get list of indicator codes for a given category.
|
|
197
|
+
|
|
198
|
+
Args:
|
|
199
|
+
category: Category name (e.g., 'mortality', 'nutrition')
|
|
200
|
+
|
|
201
|
+
Returns:
|
|
202
|
+
List of indicator codes in that category
|
|
203
|
+
"""
|
|
204
|
+
config = load_config(config_path)
|
|
205
|
+
indicators = config.get('indicators', {})
|
|
206
|
+
|
|
207
|
+
return [
|
|
208
|
+
code for code, info in indicators.items()
|
|
209
|
+
if info.get('category') == category
|
|
210
|
+
]
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def get_indicators_by_sdg(
|
|
214
|
+
sdg_goal: str,
|
|
215
|
+
config_path: Optional[Path] = None
|
|
216
|
+
) -> List[str]:
|
|
217
|
+
"""Get list of indicator codes for a given SDG goal.
|
|
218
|
+
|
|
219
|
+
Args:
|
|
220
|
+
sdg_goal: SDG goal number (e.g., '3', '4')
|
|
221
|
+
|
|
222
|
+
Returns:
|
|
223
|
+
List of indicator codes for that SDG
|
|
224
|
+
"""
|
|
225
|
+
config = load_config(config_path)
|
|
226
|
+
indicators = config.get('indicators', {})
|
|
227
|
+
|
|
228
|
+
return [
|
|
229
|
+
code for code, info in indicators.items()
|
|
230
|
+
if info.get('sdg_target', '').startswith(f"{sdg_goal}.")
|
|
231
|
+
]
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def get_indicators_by_dataflow(
|
|
235
|
+
dataflow: str,
|
|
236
|
+
config_path: Optional[Path] = None
|
|
237
|
+
) -> List[str]:
|
|
238
|
+
"""Get list of indicator codes for a given dataflow.
|
|
239
|
+
|
|
240
|
+
Args:
|
|
241
|
+
dataflow: Dataflow name (e.g., 'CME', 'NUTRITION')
|
|
242
|
+
|
|
243
|
+
Returns:
|
|
244
|
+
List of indicator codes in that dataflow
|
|
245
|
+
"""
|
|
246
|
+
config = load_config(config_path)
|
|
247
|
+
indicators = config.get('indicators', {})
|
|
248
|
+
|
|
249
|
+
return [
|
|
250
|
+
code for code, info in indicators.items()
|
|
251
|
+
if info.get('dataflow') == dataflow
|
|
252
|
+
]
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
# Module-level cached config
|
|
256
|
+
_cached_config = None
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def get_cached_config(config_path: Optional[Path] = None) -> Dict[str, Any]:
|
|
260
|
+
"""Get cached configuration (loads once, reuses thereafter)."""
|
|
261
|
+
global _cached_config
|
|
262
|
+
if _cached_config is None:
|
|
263
|
+
_cached_config = load_config(config_path)
|
|
264
|
+
return _cached_config
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
def clear_config_cache():
|
|
268
|
+
"""Clear the cached configuration."""
|
|
269
|
+
global _cached_config
|
|
270
|
+
_cached_config = None
|