Download task/single-software-execution/cli/fenics/task-01/eval.py from HongchengGao/EngiWorld-Tasks: direct link, hf CLI and curl.
- Browser
- Download file 3.05 kB
-
https://huggingface.co/datasets/HongchengGao/EngiWorld-Tasks/resolve/main/task/single-software-execution/cli/fenics/task-01/eval.py
- Command line
-
hf download hf://datasets/HongchengGao/EngiWorld-Tasks/task/single-software-execution/cli/fenics/task-01/eval.py
-
curl -L -o eval.py https://huggingface.co/datasets/HongchengGao/EngiWorld-Tasks/resolve/main/task/single-software-execution/cli/fenics/task-01/eval.py
3.05 kB
| #!/usr/bin/env python3 | |
| from __future__ import annotations | |
| import argparse | |
| import ast | |
| import json | |
| import math | |
| import re | |
| import subprocess | |
| import sys | |
| from pathlib import Path | |
| FLOAT_RE = re.compile(r"(?<![A-Za-z0-9_])[-+]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][-+]?\d+)?(?![A-Za-z0-9_])") | |
| def is_result_artifact(path: Path) -> bool: | |
| name = path.name.lower() | |
| return ( | |
| any(k in name for k in ("summary", "result", "report", "diagnosis")) | |
| or path.suffix.lower() in {".txt", ".csv", ".xy", ".result"} | |
| ) | |
| def is_nonempty_file(path: Path) -> bool: | |
| return path.exists() and path.is_file() and path.stat().st_size > 0 | |
| def read_text(path: Path) -> str: | |
| return path.read_text(encoding="utf-8", errors="ignore") | |
| def parse_floats(text: str) -> list[float]: | |
| out: list[float] = [] | |
| for token in FLOAT_RE.findall(text): | |
| try: | |
| out.append(float(token)) | |
| except (TypeError, ValueError): | |
| continue | |
| return out | |
| def write_result(path: Path, value: int) -> None: | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| path.write_text(json.dumps({"result": int(1 if value else 0)}, ensure_ascii=False) + "\n", encoding="utf-8") | |
| def check_task(root: Path) -> bool: | |
| required = ["poisson.py", "summary.txt"] | |
| for rel in required: | |
| if not is_nonempty_file(root / rel): | |
| return False | |
| script = root / "poisson.py" | |
| source = read_text(script) | |
| try: | |
| ast.parse(source, filename=str(script)) | |
| except SyntaxError: | |
| return False | |
| normalized = re.sub(r"\s+", "", source) | |
| required_code = ( | |
| "fromdolfinimport", | |
| "inner(grad(u),grad(v))*dx", | |
| "Constant(-6.0)", | |
| "solve(a==L,u,bc)", | |
| "assemble((u-u_D)**2*dx)", | |
| "assemble(inner(grad(u-u_D),grad(u-u_D))*dx)", | |
| ) | |
| if any(fragment not in normalized for fragment in required_code): | |
| return False | |
| summary = root / "summary.txt" | |
| summary.unlink(missing_ok=True) | |
| try: | |
| completed = subprocess.run( | |
| [sys.executable, str(script)], | |
| cwd=root, | |
| stdout=subprocess.PIPE, | |
| stderr=subprocess.PIPE, | |
| text=True, | |
| timeout=120, | |
| check=False, | |
| ) | |
| except (OSError, subprocess.SubprocessError): | |
| return False | |
| if completed.returncode != 0 or not is_nonempty_file(summary): | |
| return False | |
| vals = parse_floats(read_text(summary)) | |
| if len(vals) != 2 or not all(math.isfinite(value) for value in vals): | |
| return False | |
| err_l2, err_h1 = vals[0], vals[1] | |
| if err_l2 <= 0.0 or err_h1 <= 0.0: | |
| return False | |
| if err_l2 > 0.05 or err_h1 > 0.5: | |
| return False | |
| return True | |
| def evaluate() -> int: | |
| root = Path("/home/user/Desktop") | |
| try: | |
| ok = check_task(root) | |
| except Exception: | |
| ok = False | |
| return 1 if ok else 0 | |
| def main() -> int: | |
| result = evaluate() | |
| print("True" if result == 1 else "False") | |
| return 0 | |
| if __name__ == "__main__": | |
| raise SystemExit(main()) | |