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/flows.py
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
import requests
|
|
3
|
+
import xml.etree.ElementTree as ET
|
|
4
|
+
import time
|
|
5
|
+
|
|
6
|
+
def list_dataflows(max_retries: int = 3) -> pd.DataFrame:
|
|
7
|
+
"""
|
|
8
|
+
List all available UNICEF SDMX dataflows.
|
|
9
|
+
|
|
10
|
+
Returns:
|
|
11
|
+
DataFrame with columns: id, name, agency, version
|
|
12
|
+
|
|
13
|
+
Example:
|
|
14
|
+
>>> from unicefdata import list_dataflows
|
|
15
|
+
>>> flows = list_dataflows()
|
|
16
|
+
>>> print(flows.head())
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
url = "https://sdmx.data.unicef.org/ws/public/sdmxapi/rest/dataflow/UNICEF?references=none&detail=full"
|
|
20
|
+
|
|
21
|
+
for attempt in range(max_retries):
|
|
22
|
+
try:
|
|
23
|
+
response = requests.get(url, timeout=60)
|
|
24
|
+
response.raise_for_status()
|
|
25
|
+
|
|
26
|
+
# Parse XML response
|
|
27
|
+
root = ET.fromstring(response.content)
|
|
28
|
+
|
|
29
|
+
# Extract dataflows
|
|
30
|
+
ns = {'s': 'http://www.sdmx.org/resources/sdmxml/schemas/v2_1/structure'}
|
|
31
|
+
dataflows = []
|
|
32
|
+
|
|
33
|
+
for df in root.findall('.//s:Dataflow', ns):
|
|
34
|
+
name_elem = df.find('.//s:Name', ns)
|
|
35
|
+
dataflows.append({
|
|
36
|
+
'id': df.get('id'),
|
|
37
|
+
'agency': df.get('agencyID'),
|
|
38
|
+
'version': df.get('version'),
|
|
39
|
+
'name': name_elem.text if name_elem is not None else ''
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
return pd.DataFrame(dataflows)
|
|
43
|
+
|
|
44
|
+
except Exception as e:
|
|
45
|
+
if attempt == max_retries - 1:
|
|
46
|
+
raise
|
|
47
|
+
time.sleep(1)
|
|
48
|
+
|
|
49
|
+
return pd.DataFrame()
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def dataflow_schema(dataflow: str, metadata_dir: str = None) -> dict:
|
|
53
|
+
"""
|
|
54
|
+
Get dataflow schema information (dimensions and attributes).
|
|
55
|
+
|
|
56
|
+
Reads from local YAML schema files in metadata/current/dataflows/.
|
|
57
|
+
|
|
58
|
+
Args:
|
|
59
|
+
dataflow: The dataflow ID (e.g., "CME", "EDUCATION").
|
|
60
|
+
metadata_dir: Optional path to metadata directory. Auto-detected if None.
|
|
61
|
+
|
|
62
|
+
Returns:
|
|
63
|
+
Dictionary with keys: id, name, version, agency, dimensions, attributes,
|
|
64
|
+
time_dimension, primary_measure.
|
|
65
|
+
|
|
66
|
+
Example:
|
|
67
|
+
>>> from unicefdata import dataflow_schema
|
|
68
|
+
>>> schema = dataflow_schema("CME")
|
|
69
|
+
>>> print(schema['dimensions'])
|
|
70
|
+
['REF_AREA', 'INDICATOR', 'SEX', 'WEALTH_QUINTILE']
|
|
71
|
+
>>> print(schema['attributes'])
|
|
72
|
+
['DATA_SOURCE', 'COUNTRY_NOTES', 'REF_PERIOD', ...]
|
|
73
|
+
"""
|
|
74
|
+
import yaml
|
|
75
|
+
from pathlib import Path
|
|
76
|
+
import os
|
|
77
|
+
|
|
78
|
+
df_upper = dataflow.upper()
|
|
79
|
+
|
|
80
|
+
# Find metadata directory
|
|
81
|
+
if metadata_dir is None:
|
|
82
|
+
metadata_dir = _find_metadata_dir()
|
|
83
|
+
|
|
84
|
+
metadata_path = Path(metadata_dir)
|
|
85
|
+
schema_path = metadata_path / "dataflows" / f"{df_upper}.yaml"
|
|
86
|
+
|
|
87
|
+
if not schema_path.exists():
|
|
88
|
+
# Fall back to basic info from _unicefdata_dataflows.yaml
|
|
89
|
+
basic = _get_basic_dataflow_info(df_upper, metadata_path)
|
|
90
|
+
if basic:
|
|
91
|
+
print(f"Note: Detailed schema not available for '{df_upper}'. Showing basic info.")
|
|
92
|
+
return basic
|
|
93
|
+
raise FileNotFoundError(
|
|
94
|
+
f"Dataflow '{df_upper}' not found. Use list_dataflows() to see available dataflows."
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
# Parse YAML schema
|
|
98
|
+
with open(schema_path, 'r', encoding='utf-8') as f:
|
|
99
|
+
schema = yaml.safe_load(f)
|
|
100
|
+
|
|
101
|
+
# Extract dimensions (list of id values)
|
|
102
|
+
dimensions = []
|
|
103
|
+
if schema.get('dimensions'):
|
|
104
|
+
dimensions = [d.get('id', '') for d in schema['dimensions']]
|
|
105
|
+
|
|
106
|
+
# Extract attributes (list of id values)
|
|
107
|
+
attributes = []
|
|
108
|
+
if schema.get('attributes'):
|
|
109
|
+
attributes = [a.get('id', '') for a in schema['attributes']]
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
'id': schema.get('id', df_upper),
|
|
113
|
+
'name': schema.get('name', ''),
|
|
114
|
+
'version': schema.get('version', ''),
|
|
115
|
+
'agency': schema.get('agency', 'UNICEF'),
|
|
116
|
+
'dimensions': dimensions,
|
|
117
|
+
'attributes': attributes,
|
|
118
|
+
'time_dimension': schema.get('time_dimension', 'TIME_PERIOD'),
|
|
119
|
+
'primary_measure': schema.get('primary_measure', 'OBS_VALUE'),
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def print_dataflow_schema(schema: dict) -> None:
|
|
124
|
+
"""
|
|
125
|
+
Pretty-print a dataflow schema.
|
|
126
|
+
|
|
127
|
+
Args:
|
|
128
|
+
schema: Dictionary from dataflow_schema().
|
|
129
|
+
|
|
130
|
+
Example:
|
|
131
|
+
>>> schema = dataflow_schema("CME")
|
|
132
|
+
>>> print_dataflow_schema(schema)
|
|
133
|
+
"""
|
|
134
|
+
print()
|
|
135
|
+
print("-" * 70)
|
|
136
|
+
print(f"Dataflow Schema: {schema['id']}")
|
|
137
|
+
print("-" * 70)
|
|
138
|
+
print()
|
|
139
|
+
|
|
140
|
+
if schema.get('name'):
|
|
141
|
+
print(f"Name: {schema['name']}")
|
|
142
|
+
if schema.get('version'):
|
|
143
|
+
print(f"Version: {schema['version']}")
|
|
144
|
+
if schema.get('agency'):
|
|
145
|
+
print(f"Agency: {schema['agency']}")
|
|
146
|
+
print()
|
|
147
|
+
|
|
148
|
+
dims = schema.get('dimensions', [])
|
|
149
|
+
if dims:
|
|
150
|
+
print(f"Dimensions ({len(dims)}):")
|
|
151
|
+
for d in dims:
|
|
152
|
+
print(f" {d}")
|
|
153
|
+
print()
|
|
154
|
+
|
|
155
|
+
attrs = schema.get('attributes', [])
|
|
156
|
+
if attrs:
|
|
157
|
+
print(f"Attributes ({len(attrs)}):")
|
|
158
|
+
for a in attrs:
|
|
159
|
+
print(f" {a}")
|
|
160
|
+
|
|
161
|
+
print()
|
|
162
|
+
print("-" * 70)
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def _find_metadata_dir() -> str:
|
|
166
|
+
"""Find metadata directory. Returns path as string."""
|
|
167
|
+
from pathlib import Path
|
|
168
|
+
import os
|
|
169
|
+
|
|
170
|
+
# 1. Environment override
|
|
171
|
+
env_home = os.environ.get('UNICEF_DATA_HOME_PYTHON') or os.environ.get('UNICEF_DATA_HOME', '')
|
|
172
|
+
if env_home:
|
|
173
|
+
metadata_dir = Path(env_home) / "metadata" / "current"
|
|
174
|
+
if metadata_dir.exists():
|
|
175
|
+
return str(metadata_dir)
|
|
176
|
+
|
|
177
|
+
# 2. Relative to module location
|
|
178
|
+
module_dir = Path(__file__).parent
|
|
179
|
+
candidates = [
|
|
180
|
+
module_dir / "metadata" / "current", # unicefdata/metadata/current
|
|
181
|
+
module_dir.parent / "metadata" / "current", # python/metadata/current
|
|
182
|
+
]
|
|
183
|
+
for path in candidates:
|
|
184
|
+
if path.exists():
|
|
185
|
+
return str(path.resolve())
|
|
186
|
+
|
|
187
|
+
# 3. User home directory
|
|
188
|
+
home_dir = Path.home() / ".unicef_data" / "python" / "metadata" / "current"
|
|
189
|
+
if home_dir.exists():
|
|
190
|
+
return str(home_dir)
|
|
191
|
+
|
|
192
|
+
raise FileNotFoundError("Could not find metadata directory. Run sync_metadata() first.")
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def _get_basic_dataflow_info(dataflow: str, metadata_path) -> dict:
|
|
196
|
+
"""Get basic dataflow info from _unicefdata_dataflows.yaml."""
|
|
197
|
+
import yaml
|
|
198
|
+
from pathlib import Path
|
|
199
|
+
|
|
200
|
+
df_file = Path(metadata_path) / "_unicefdata_dataflows.yaml"
|
|
201
|
+
if not df_file.exists():
|
|
202
|
+
return None
|
|
203
|
+
|
|
204
|
+
with open(df_file, 'r', encoding='utf-8') as f:
|
|
205
|
+
all_flows = yaml.safe_load(f)
|
|
206
|
+
|
|
207
|
+
if dataflow in all_flows:
|
|
208
|
+
info = all_flows[dataflow]
|
|
209
|
+
return {
|
|
210
|
+
'id': dataflow,
|
|
211
|
+
'name': info.get('name', ''),
|
|
212
|
+
'version': info.get('version', ''),
|
|
213
|
+
'agency': 'UNICEF',
|
|
214
|
+
'dimensions': [],
|
|
215
|
+
'attributes': [],
|
|
216
|
+
}
|
|
217
|
+
return None
|