File size: 9,775 Bytes
2044f69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3

from __future__ import annotations

import argparse
import csv
import errno
import json
import os
import shutil
from pathlib import Path
from typing import Any, Dict, Iterable, List


DEFAULT_TARGET_COLUMNS = [
    "lcd",
    "pld",
    "void_fraction",
    "surface_area_m2g",
    "surface_area_m2cm3",
]


def parse_target_columns(raw: str) -> List[str]:
    cols = [c.strip() for c in raw.split(",") if c.strip()]
    if not cols:
        raise ValueError("Список target-колонок пуст.")
    return cols


def get_nested_value(data: Any, path: str) -> Any:
    current = data
    for part in path.split("."):
        if isinstance(current, dict):
            if part not in current:
                return None
            current = current[part]
            continue

        if isinstance(current, list) and part.isdigit():
            idx = int(part)
            if idx < 0 or idx >= len(current):
                return None
            current = current[idx]
            continue

        return None

    return current


def normalize_scalar(value: Any) -> Any:
    if value is None:
        return ""
    if isinstance(value, (str, int, float, bool)):
        return value
    return json.dumps(value, ensure_ascii=False)


def try_parse_float(value: Any) -> float | None:
    if isinstance(value, bool):
        return None
    if isinstance(value, (int, float)):
        return float(value)
    if isinstance(value, str):
        raw = value.strip()
        if not raw:
            return None
        try:
            return float(raw)
        except ValueError:
            return None
    return None


def all_targets_are_zero(row: Dict[str, Any], target_columns: List[str]) -> bool:
    for col in target_columns:
        num = try_parse_float(row.get(col))
        if num is None or num != 0.0:
            return False
    return True


def safe_link_or_copy(src: Path, dst: Path) -> bool:
    try:
        os.link(src, dst)
        return False
    except OSError as exc:
        if exc.errno not in {errno.EXDEV, errno.EPERM, errno.EOPNOTSUPP, errno.EACCES}:
            raise
        shutil.copy2(src, dst)
        return True


def place_cif_file(src: Path, dst: Path, file_op: str) -> bool:
    """
    Returns True if fallback to copy happened (only for hardlink mode), else False.
    """
    if file_op == "copy":
        shutil.copy2(src, dst)
        return False
    if file_op == "move":
        shutil.move(str(src), str(dst))
        return False
    if file_op == "symlink":
        os.symlink(src.resolve(), dst)
        return False
    if file_op == "hardlink":
        return safe_link_or_copy(src, dst)
    raise ValueError(f"Неизвестный file-op: {file_op}")


def iter_cif_files(hmof_dir: Path) -> Iterable[Path]:
    return sorted(p for p in hmof_dir.iterdir() if p.is_file() and p.suffix.lower() == ".cif")


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        description=(
            "Создать структуру датасета для hMOF в стиле CoreMof/ASR: "
            "<out-dir>/raw/*.cif + <out-dir>/id_prop.csv"
        )
    )
    parser.add_argument(
        "--hmof-dir",
        type=Path,
        default=Path("hMOF"),
        help="Папка с исходными файлами *.cif и *.json (по умолчанию: hMOF)",
    )
    parser.add_argument(
        "--out-dir",
        type=Path,
        default=None,
        help="Куда собрать датасет (по умолчанию: <hmof-dir>/ASR)",
    )
    parser.add_argument(
        "--target-columns",
        default=",".join(DEFAULT_TARGET_COLUMNS),
        help=(
            "Target-колонки из JSON (через запятую). "
            "Поддерживаются и вложенные пути, например: adsorbent.id"
        ),
    )
    parser.add_argument(
        "--file-op",
        choices=["hardlink", "copy", "move", "symlink"],
        default="hardlink",
        help=(
            "Как поместить CIF в raw: hardlink/copy/move/symlink "
            "(по умолчанию: hardlink)"
        ),
    )
    parser.add_argument(
        "--overwrite-files",
        action="store_true",
        help="Перезаписывать уже существующие CIF в raw",
    )
    parser.add_argument(
        "--max-files",
        type=int,
        default=None,
        help="Ограничить количество обрабатываемых CIF (для теста)",
    )
    parser.add_argument(
        "--dry-run",
        action="store_true",
        help="Проверка без записи файлов",
    )
    parser.add_argument(
        "--progress-every",
        type=int,
        default=5000,
        help="Как часто печатать прогресс (по умолчанию: 5000)",
    )
    return parser.parse_args()


def main() -> None:
    args = parse_args()

    hmof_dir = args.hmof_dir
    out_dir = args.out_dir or (hmof_dir / "ASR")
    raw_dir = out_dir / "raw"
    id_prop_path = out_dir / "id_prop.csv"
    target_columns = parse_target_columns(args.target_columns)

    if not hmof_dir.is_dir():
        raise FileNotFoundError(f"Папка не найдена: {hmof_dir}")

    cif_files = list(iter_cif_files(hmof_dir))
    if args.max_files is not None:
        cif_files = cif_files[: args.max_files]

    total = len(cif_files)
    if total == 0:
        raise RuntimeError(f"В {hmof_dir} не найдено ни одного .cif файла.")

    if not args.dry_run:
        raw_dir.mkdir(parents=True, exist_ok=True)
        out_dir.mkdir(parents=True, exist_ok=True)

    stats: Dict[str, int] = {
        "total_cif": total,
        "written_rows": 0,
        "missing_json": 0,
        "bad_json": 0,
        "skipped_all_zero_targets": 0,
        "removed_all_zero_from_raw": 0,
        "placed_files": 0,
        "already_present": 0,
        "hardlink_fallback_copy": 0,
    }
    missing_targets: Dict[str, int] = {col: 0 for col in target_columns}

    if args.dry_run:
        sink = open(os.devnull, "w", newline="", encoding="utf-8")
    else:
        sink = id_prop_path.open("w", newline="", encoding="utf-8")

    try:
        writer = csv.DictWriter(sink, fieldnames=["mof_id", *target_columns])
        writer.writeheader()

        for idx, cif_path in enumerate(cif_files, start=1):
            mof_id = cif_path.stem
            json_path = hmof_dir / f"{mof_id}.json"

            if not json_path.is_file():
                stats["missing_json"] += 1
                continue

            try:
                with json_path.open("r", encoding="utf-8") as f:
                    payload = json.load(f)
            except json.JSONDecodeError:
                stats["bad_json"] += 1
                continue

            row: Dict[str, Any] = {"mof_id": mof_id}
            for col in target_columns:
                value = get_nested_value(payload, col)
                if value is None:
                    missing_targets[col] += 1
                row[col] = normalize_scalar(value)

            raw_cif_path = raw_dir / cif_path.name

            # Пропускаем структуры, где все target-значения равны 0.0.
            if all_targets_are_zero(row, target_columns):
                if not args.dry_run and raw_cif_path.exists():
                    raw_cif_path.unlink()
                    stats["removed_all_zero_from_raw"] += 1
                stats["skipped_all_zero_targets"] += 1
                continue

            if not args.dry_run:
                if raw_cif_path.exists():
                    if args.overwrite_files:
                        raw_cif_path.unlink()
                        fallback_copy = place_cif_file(cif_path, raw_cif_path, args.file_op)
                        if fallback_copy:
                            stats["hardlink_fallback_copy"] += 1
                        stats["placed_files"] += 1
                    else:
                        stats["already_present"] += 1
                else:
                    fallback_copy = place_cif_file(cif_path, raw_cif_path, args.file_op)
                    if fallback_copy:
                        stats["hardlink_fallback_copy"] += 1
                    stats["placed_files"] += 1

            writer.writerow(row)
            stats["written_rows"] += 1

            if args.progress_every > 0 and idx % args.progress_every == 0:
                print(
                    f"[{idx}/{total}] rows={stats['written_rows']} "
                    f"missing_json={stats['missing_json']} bad_json={stats['bad_json']}"
                )
    finally:
        sink.close()

    print("=== hMOF preprocess done ===")
    print(f"hmof dir: {hmof_dir}")
    print(f"out dir: {out_dir}")
    print(f"raw dir: {raw_dir}")
    print(f"id_prop: {id_prop_path}")
    print(f"dry run: {args.dry_run}")
    print(f"file op: {args.file_op}")
    print(f"total cif: {stats['total_cif']}")
    print(f"id_prop rows written: {stats['written_rows']}")
    print(f"missing json: {stats['missing_json']}")
    print(f"bad json: {stats['bad_json']}")
    print(f"skipped all-zero targets: {stats['skipped_all_zero_targets']}")
    print(f"removed all-zero from raw: {stats['removed_all_zero_from_raw']}")
    print(f"placed cif files: {stats['placed_files']}")
    print(f"already present in raw: {stats['already_present']}")
    print(f"hardlink fallback copy: {stats['hardlink_fallback_copy']}")
    print(f"target columns: {', '.join(target_columns)}")

    for col in target_columns:
        print(f"missing target '{col}': {missing_targets[col]}")


if __name__ == "__main__":
    main()