Download reader.py from RRRong/GeoReader-MindCube: direct link, hf CLI and curl.
- Browser
- Download file 4.81 kB
-
https://huggingface.co/datasets/RRRong/GeoReader-MindCube/resolve/main/reader.py
- Command line
-
hf download hf://datasets/RRRong/GeoReader-MindCube/reader.py
-
curl -L -o reader.py https://huggingface.co/datasets/RRRong/GeoReader-MindCube/resolve/main/reader.py
4.81 kB
| """自动读取 GeoReader 相对路径数据包,不生成恢复副本或修改下载文件。""" | |
| from functools import lru_cache | |
| import json | |
| import os | |
| from pathlib import Path | |
| import numpy as np | |
| _ROOTS = {} | |
| _IMAGE_FIELDS = ('image_input', 'images', 'media_paths') | |
| _GEOMETRY_RGB_FIELDS = ('geometry_image_input', 'geometry_rgb_paths', 'qa_rgb_paths', | |
| 'qa_rgb_path', 'geometry_rgb_path') | |
| def bundle_root(data_file): | |
| """从数据文件向上找到发布包;普通历史数据明确沿用原读取合同。""" | |
| path = Path(os.path.abspath(os.path.expanduser(str(data_file)))) | |
| for root in _ROOTS: | |
| if path.is_relative_to(root): | |
| return root | |
| for parent in path.parents: | |
| manifest = parent / 'manifest.json' | |
| if manifest.is_file(): | |
| values = json.loads(manifest.read_text()) | |
| if values.get('format') in {'georeader-sixbench', 'georeader-mindcube'}: | |
| _ROOTS[parent] = values | |
| return parent | |
| return None | |
| def _absolute(root, value): | |
| """只解析包内路径,禁止相对路径越界或引用其它机器的运行文件。""" | |
| path = Path(value) | |
| if path.is_absolute(): | |
| if not path.is_relative_to(root): | |
| raise ValueError(f'数据包运行路径指向包外: {path}') | |
| return str(path) | |
| if '..' in path.parts: | |
| raise ValueError(f'数据包相对路径越界: {path}') | |
| return str(root / path) | |
| def _mapped(value, root): | |
| """保持标量路径和有序路径列表的原类型。""" | |
| if isinstance(value, str): | |
| return _absolute(root, value) | |
| if isinstance(value, list) and all(isinstance(item, str) for item in value): | |
| return [_absolute(root, item) for item in value] | |
| raise TypeError(f'路径不是字符串或字符串列表: {type(value)}') | |
| def resolve_record_paths(record, data_file): | |
| """只在内存解析运行路径;问题、答案和描述不变。""" | |
| root = bundle_root(str(data_file)) | |
| if root is None: | |
| return record | |
| relative = Path(os.path.abspath(str(data_file))).relative_to(root) | |
| phase = relative.parts[:2] | |
| if phase not in {('train', 'stage1'), ('train', 'stage2')} and relative.parts[0] != 'eval': | |
| raise ValueError(f'未知数据包清单位置: {data_file}') | |
| result = dict(record) | |
| for name in _IMAGE_FIELDS: | |
| if name in record: | |
| result[name] = _mapped(record[name], root) | |
| for flag, name in (('has_pose', 'pose_path'), ('has_depth', 'depth_path')): | |
| if record.get(flag) and record.get(name): | |
| result[name] = _absolute(root, record[name]) | |
| # Stage 2 的 depth_paths 可能是旧GT审计;只有 Stage 1 实际消费逐帧深度。 | |
| if phase == ('train', 'stage1') and record.get('depth_paths'): | |
| result['depth_paths'] = _mapped(record['depth_paths'], root) | |
| if record.get('geometry_provenance'): | |
| provenance = dict(record['geometry_provenance']) | |
| for name in _GEOMETRY_RGB_FIELDS: | |
| if provenance.get(name): | |
| provenance[name] = _mapped(provenance[name], root) | |
| result['geometry_provenance'] = provenance | |
| return result | |
| def resolve_records(records, data_file): | |
| """供现有 Stage 1 / Stage 2 读取器接入,原绝对路径数据保持原行为。""" | |
| if bundle_root(str(data_file)) is None: | |
| return records | |
| return [resolve_record_paths(row, data_file) for row in records] | |
| def resolve_npz_paths(payload, path): | |
| """从NPZ自身读取包内图序;数值数组沿用原对象,不读取外部映射表。""" | |
| root = bundle_root(str(path)) | |
| if root is None: | |
| return payload | |
| result = dict(payload) | |
| for name in ('image_input', 'geometry_image_input'): | |
| if name not in payload: | |
| continue | |
| stored = np.asarray(payload[name]) | |
| if stored.ndim != 1 or stored.dtype.kind not in {'U', 'S'}: | |
| raise ValueError(f'NPZ图序不是一维字符串: {path}:{name}') | |
| result[name] = np.asarray([_absolute(root, value) for value in stored.astype(str).tolist()]) | |
| return result | |
| def iter_jsonl(path): | |
| """供外部使用者直接读取下载的JSONL,返回本机可访问的路径。""" | |
| with Path(path).open() as handle: | |
| for line in handle: | |
| if not line.strip(): | |
| raise ValueError(f'JSONL含空行: {path}') | |
| yield resolve_record_paths(json.loads(line), path) | |
| def load_npz(path): | |
| """供外部使用者读取几何,几何数值不变、路径元数据自动解析。""" | |
| bundle_root(str(path)) | |
| with np.load(path, allow_pickle=False) as archive: | |
| payload = {name: archive[name] for name in archive.files} | |
| return resolve_npz_paths(payload, path) | |