File size: 2,381 Bytes
2e208c6 | 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 | # build_kline_index.py
import json
from pathlib import Path
ROOT = Path("/workspace/data/finmultitime_hf")
# HS300
HS300_IMG_ROOT = ROOT / "image" / "HS300_image" / "HS300_image"
HS300_TS_ROOT = ROOT / "time_series" / "HS300_time_series" / "HS300_time_series"
# S&P500 图片:image/S&P500_image_a .../b.../c...
SP500_IMG_ROOT = ROOT / "image" / "image"
SP500_TS_ROOT = ROOT / "time_series" / "S&P500_time_series" / "S&P500_time_series"
OUT_PATH = ROOT / "finmultitime_kline_index.jsonl"
def collect_hs300():
for code_dir in sorted(HS300_IMG_ROOT.glob("*")):
if not code_dir.is_dir():
continue
stock_id = code_dir.name # 例如 000001.SZ
image_files = sorted(str(p) for p in code_dir.glob("*.png"))
if not image_files:
continue
ts_file = HS300_TS_ROOT / f"{stock_id}.csv"
rec = {
"market": "HS300",
"stock_id": stock_id,
"image_files": image_files,
"time_series_files": [str(ts_file)] if ts_file.is_file() else [],
}
yield rec
def collect_sp500():
# 遍历 S&P500_image_a ~ S&P500_image_h 等大目录
for big_dir in sorted(SP500_IMG_ROOT.glob("S&P500_image_*")):
if not big_dir.is_dir():
continue
# big_dir 下是按首字母分的子目录:aa, aapl, ...
for code_dir in sorted(big_dir.glob("*")):
if not code_dir.is_dir():
continue
stock_id = code_dir.name # 例如 aa, aapl
image_files = sorted(str(p) for p in code_dir.glob("*.png"))
if not image_files:
continue
ts_file = SP500_TS_ROOT / f"{stock_id}.csv"
rec = {
"market": "SP500",
"stock_id": stock_id,
"image_files": image_files,
"time_series_files": [str(ts_file)] if ts_file.is_file() else [],
}
yield rec
def main():
count = 0
with OUT_PATH.open("w", encoding="utf-8") as f:
for rec in collect_hs300():
f.write(json.dumps(rec, ensure_ascii=False) + "\n")
count += 1
for rec in collect_sp500():
f.write(json.dumps(rec, ensure_ascii=False) + "\n")
count += 1
print(f"[OK] wrote {count} records -> {OUT_PATH}")
if __name__ == "__main__":
main()
|