Repository navigation
Expand file tree
/
Copy pathdata.py
More file actions
394 lines (333 loc) · 13 KB
/
Copy pathdata.py
File metadata and controls
394 lines (333 loc) · 13 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import os, io
import requests
import polars as pl
import json
from target_format import clean_text, format_target, format_target_cn
# Dataset info ----
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
REPO = "MGFPKU/target_dataset"
LOCAL_DATA: bool = os.getenv("LOCAL_DATA", "FALSE").upper() == "TRUE"
# GitHub release asset name per language
_ASSET_NAME = {"CN": "China_Climate_Target_Tracker_cn.xlsx", "EN": "China_Climate_Target_Tracker_en.xlsx"}
DISPLAY_COLS = [
"Metric",
"Announced",
"Target",
"Target_Category",
]
WANTED_COLS = [
"Announcement_Year",
"Metric",
"Direction",
"Target_Magnitude",
"Baseline",
"Target_Year_or_Period",
"Target_Category",
"Accountability",
"Sentence",
"Document",
"Topic_Label",
]
# Chinese local file support ------------------------------------------------
CN_LOCAL_FILE = "../中国国家气候目标数据库.xlsx"
# Chinese source column name → internal English name
CN_COLUMN_MAP: dict[str, str] = {
"公布年份": "Announcement_Year",
"指标": "Metric",
"方向": "Direction",
"目标值": "Target_Magnitude",
"基线": "Baseline",
"目标年份/时期": "Target_Year_or_Period",
"计数": "Count",
"目标类别": "Target_Category",
"责任主体": "Accountability",
"政策原文": "Sentence",
"文件": "Document",
"主题标签": "Topic_Label",
}
# Reverse: internal English name → Chinese source name
_EN_TO_CN = {en: cn for cn, en in CN_COLUMN_MAP.items()}
# Chinese display labels for DISPLAY_COLS.
# Derived from _EN_TO_CN; "Announced" is aliased from Announcement_Year at
# load time, and "Target" is a computed column with no source equivalent.
CN_HEADER_MAP: dict[str, str] = {
col: "目标" if col == "Target"
else _EN_TO_CN.get("Announcement_Year" if col == "Announced" else col, col)
for col in DISPLAY_COLS
}
# Per-language data cache — loaded once on first request per language
_data_cache: dict[str, pl.DataFrame] = {}
def _resolve_lang(lang: str | None) -> str:
"""Normalise a language string to CN or EN, falling back to env var."""
if lang is None:
lang = os.getenv("LANGUAGE", "CN")
lang = lang.upper()
if lang not in ("CN", "EN"):
lang = "CN"
return lang
def fetch_raw_data(lang: str | None = None) -> io.BytesIO:
lang = _resolve_lang(lang)
if LOCAL_DATA:
if lang == "CN":
file_path = CN_LOCAL_FILE
else:
file_path = "../CHINA'S NATIONAL CLIMATE TARGETS DATABASE.xlsx"
with open(file_path, "rb") as f:
print(f"Using local data ({file_path})...")
return io.BytesIO(f.read())
headers = {
"Authorization": f"Bearer {GITHUB_TOKEN}",
"Accept": "application/vnd.github+json",
}
# 1️⃣ Get latest release metadata
latest_url = f"https://api.github.com/repos/{REPO}/releases/latest"
res = requests.get(latest_url, headers=headers)
if res.status_code != 200:
raise RuntimeError(f"Failed to fetch file: {res.status_code}\n{res.text}")
release = res.json()
# 2️⃣ Find the language-appropriate asset
asset_name = _ASSET_NAME.get(lang, _ASSET_NAME["CN"])
asset = next((a for a in release["assets"] if a["name"] == asset_name), None)
if asset is None:
raise RuntimeError(f"{asset_name} not found in latest release.")
asset_id = asset["id"]
# 3️⃣ Download asset binary
download_headers = {
"Authorization": f"Bearer {GITHUB_TOKEN}",
"Accept": "application/octet-stream",
}
download_url = f"https://api.github.com/repos/{REPO}/releases/assets/{asset_id}"
file_res = requests.get(download_url, headers=download_headers)
if file_res.status_code != 200:
raise RuntimeError(f"Failed to download file:\n{file_res.text}")
# 4️⃣ Load Excel into Polars
return io.BytesIO(file_res.content)
def get_sheet_names(lang: str | None = None) -> list[str]:
lang = _resolve_lang(lang)
with open("sheets.json", "r", encoding="utf-8") as f:
data: dict = json.load(f)
sheets = data.get("sheets", {})
# Support both nested {CN: [...], EN: [...]} and legacy [[...]] formats
if isinstance(sheets, dict):
sheet_names: list[str] = sheets.get(lang, sheets.get("CN", []))
else:
# Legacy format: sheets is a list of lists
sheet_names: list[str] = sheets[0] if sheets else []
if not sheet_names:
raise RuntimeError("No sheet names found in sheets.json")
return sheet_names
def _rename_cn_columns(df: pl.DataFrame) -> pl.DataFrame:
"""Rename Chinese source column names to internal English names."""
rename_map = {cn: en for cn, en in CN_COLUMN_MAP.items() if cn in df.columns}
return df.rename(rename_map)
def _build_doc_title_map(raw_xlsx: io.BytesIO, lang: str) -> dict[str, str]:
"""Read the Sources sheet and return a mapping from document code → title.
The Sources sheet (``来源`` / ``Sources``) has multi-row headers and
interspersed category-label rows. We locate the real header row (col 0
is ``编号`` or ``code``), then gather every data row whose first column
looks like a document code (e.g. ``HL2104``).
Returns a dict mapping the code to the document title in the current
language.
"""
import re
sheet_name = "来源" if lang == "CN" else "Sources"
raw_xlsx.seek(0)
src = pl.read_excel(raw_xlsx, sheet_name=sheet_name, has_header=False)
# Find the header row — the row whose first cell is "编号" or "code"
header_row_idx: int | None = None
for i in range(len(src)):
cell = str(src.row(i)[0])
if cell in ("编号", "code"):
header_row_idx = i
break
if header_row_idx is None:
raise RuntimeError(
f"Could not find header row in '{sheet_name}' sheet"
)
# Build the mapping: row is data if col 0 looks like a doc code
code_re = re.compile(r"^[A-Z]+\d+")
doc_map: dict[str, str] = {}
for i in range(header_row_idx + 1, len(src)):
row = src.row(i)
code = str(row[0]) if row[0] is not None else ""
if not code_re.match(code):
continue # category label, blank, or other non-data row
# Pick the title column for the current language
title = row[2] # Chinese title
if lang == "EN":
title = row[3] # English title
if title is not None and str(title).strip():
doc_map[code] = str(title).strip()
return doc_map
def _load_cn_data(raw_xlsx: io.BytesIO, lang: str) -> pl.DataFrame:
"""Load and process data from the Chinese Excel file.
Differences from _load_en_data():
- Chinese source column names are renamed to internal English names
- Filters Count != "重申目标" (equivalent to English Count != "r")
- Uses format_target_cn() for Chinese target display
- Uses fill_null("无") instead of fill_null("N/A")
"""
sheet_names = get_sheet_names(lang)
combined_sheet: pl.DataFrame | None = None
for sheet_name in sheet_names:
raw_xlsx.seek(0)
sheet = (
pl.read_excel(raw_xlsx, sheet_name=sheet_name)
.with_columns(pl.all().cast(pl.Utf8))
)
# Rename Chinese columns → internal English names
sheet = _rename_cn_columns(sheet).filter(
pl.col("Count") != "重申目标"
)
available_cols = [col for col in WANTED_COLS if col in sheet.columns]
missing_cols = [col for col in WANTED_COLS if col not in sheet.columns]
if missing_cols:
raise RuntimeError(
f"Sheet '{sheet_name}' is missing required columns: {', '.join(missing_cols)}"
)
sheet = sheet.select(available_cols).with_columns(
pl.col("Announcement_Year").alias("Announced"),
pl.col("Metric")
.map_elements(clean_text, return_dtype=pl.Utf8)
.alias("Metric"),
pl.struct(
"Direction",
"Target_Magnitude",
"Baseline",
"Target_Year_or_Period",
"Announcement_Year",
)
.map_elements(format_target_cn, return_dtype=pl.Utf8)
.alias("Target"),
)
combined_sheet = (
sheet if combined_sheet is None else pl.concat([combined_sheet, sheet])
)
if combined_sheet is None:
raise RuntimeError(
"No sheets were processed. Check the Chinese Excel file."
)
# Build document-code → title lookup from the Sources sheet
doc_map = _build_doc_title_map(raw_xlsx, lang)
return (
combined_sheet.fill_null("无")
.with_columns(
pl.col("Target_Year_or_Period")
.str.extract(r"(\d{4})")
.cast(pl.Int32, strict=False)
.alias("_sort_target_year"),
pl.col("Metric").alias("_sort_metric"),
)
.sort(
by=[
"Target_Category",
"_sort_metric",
"Announced",
"_sort_target_year",
"Target_Year_or_Period",
"Target",
],
descending=[False, False, False, False, False, False],
nulls_last=True,
)
.drop(["_sort_target_year", "_sort_metric"])
.with_columns(
pl.col("Document")
.str.replace(r"\.(pdf|htm|html|docx?|PDF|HTM|HTML|DOCX?)$", "")
.map_elements(
lambda code: f"来源:{doc_map[code]}" if code in doc_map else None,
return_dtype=pl.Utf8,
)
.alias("Doc_Title"),
)
)
def _load_en_data(raw_xlsx: io.BytesIO, lang: str) -> pl.DataFrame:
"""Load and process data from the English Excel file."""
sheet_names = get_sheet_names(lang)
combined_sheet: pl.DataFrame | None = None
for sheet_name in sheet_names:
raw_xlsx.seek(0)
sheet = (
pl.read_excel(raw_xlsx, sheet_name=sheet_name)
.with_columns(pl.all().cast(pl.Utf8))
.filter(pl.col("Count") != "r")
)
available_cols = [col for col in WANTED_COLS if col in sheet.columns]
missing_cols = [col for col in WANTED_COLS if col not in sheet.columns]
if missing_cols:
raise RuntimeError(
f"Sheet '{sheet_name}' is missing required columns: {', '.join(missing_cols)}"
)
sheet = sheet.select(available_cols).with_columns(
pl.col("Announcement_Year").alias("Announced"),
pl.col("Metric")
.map_elements(clean_text, return_dtype=pl.Utf8)
.alias("Metric"),
pl.struct(
"Direction",
"Target_Magnitude",
"Baseline",
"Target_Year_or_Period",
"Announcement_Year",
)
.map_elements(format_target, return_dtype=pl.Utf8)
.alias("Target"),
)
combined_sheet = (
sheet if combined_sheet is None else pl.concat([combined_sheet, sheet])
)
if combined_sheet is None:
raise RuntimeError(
"No sheets were processed. Check sheets.json and China_Climate_Target_Tracker_cn.xlsx / China_Climate_Target_Tracker_en.xlsx"
)
# Build document-code → title lookup from the Sources sheet
doc_map = _build_doc_title_map(raw_xlsx, lang)
return (
combined_sheet.fill_null("N/A")
.with_columns(
pl.col("Target_Year_or_Period")
.str.extract(r"(\d{4})")
.cast(pl.Int32, strict=False)
.alias("_sort_target_year"),
pl.col("Metric").alias("_sort_metric"),
)
.sort(
by=[
"Target_Category",
"_sort_metric",
"Announced",
"_sort_target_year",
"Target_Year_or_Period",
"Target",
],
descending=[False, False, False, False, False, False],
nulls_last=True,
)
.drop(["_sort_target_year", "_sort_metric"])
.with_columns(
pl.col("Document")
.str.replace(r"\.(pdf|htm|html|docx?|PDF|HTM|HTML|DOCX?)$", "")
.map_elements(
lambda code: f"Source: {doc_map[code]}" if code in doc_map else None,
return_dtype=pl.Utf8,
)
.alias("Doc_Title"),
)
)
def get_data(lang: str | None = None) -> pl.DataFrame:
"""Return the full dataset for *lang*, caching it in memory.
Call this from inside a Shiny session so ``lang`` can be driven by a
query parameter (``?lang=cn`` / ``?lang=en``). The first call per
language fetches and processes the Excel file; subsequent calls hit an
in-memory cache.
"""
lang = _resolve_lang(lang)
if lang in _data_cache:
return _data_cache[lang]
raw_xlsx = fetch_raw_data(lang)
if lang == "CN":
df = _load_cn_data(raw_xlsx, lang)
else:
df = _load_en_data(raw_xlsx, lang)
_data_cache[lang] = df
return df