Download hmc/reference/scripts/patch_nifreeze.py from neurodeskorg/webapps: direct link, hf CLI and curl.
- Browser
- Download file 2.35 kB
-
https://huggingface.co/datasets/neurodeskorg/webapps/resolve/main/hmc/reference/scripts/patch_nifreeze.py
- Command line
-
hf download hf://datasets/neurodeskorg/webapps/hmc/reference/scripts/patch_nifreeze.py
-
curl -L -o patch_nifreeze.py https://huggingface.co/datasets/neurodeskorg/webapps/resolve/main/hmc/reference/scripts/patch_nifreeze.py
2.35 kB
| """Patch the NiFreeze venv for the M0 reference runs. | |
| - NIFREEZE_KEEP_DIR: process in this directory instead of a TemporaryDirectory, | |
| so sample_*, predicted_*, cmd-*.sh and ants-*0GenericAffine.mat stay. | |
| The visiting order of volumes goes to order.txt in that directory. | |
| - NIFREEZE_ANTS_FLOAT=1: add --float 1 to antsRegistration (nipype `float`). | |
| Originals are kept as *.orig for the diff. | |
| """ | |
| import shutil | |
| import sys | |
| from pathlib import Path | |
| PKG = Path("/var/tmp/dwi-hmc/nifreeze-venv/lib/python3.12/site-packages/nifreeze") | |
| def patch(path: Path, old: str, new: str) -> None: | |
| orig = path.with_suffix(path.suffix + ".orig") | |
| if not orig.exists(): | |
| shutil.copy2(path, orig) | |
| text = orig.read_text() | |
| if text.count(old) != 1: | |
| sys.exit(f"pattern not found once in {path}: {old!r}") | |
| path.write_text(text.replace(old, new)) | |
| est = PKG / "estimator.py" | |
| est_old = """ with TemporaryDirectory() as tmp_dir: | |
| print(f"Processing in <{tmp_dir}>") | |
| ptmp_dir = Path(tmp_dir) | |
| """ | |
| est_new = """ import contextlib | |
| import os | |
| _keep = os.environ.get("NIFREEZE_KEEP_DIR") | |
| if _keep: | |
| Path(_keep).mkdir(parents=True, exist_ok=True) | |
| with ( | |
| contextlib.nullcontext(_keep) if _keep else TemporaryDirectory() | |
| ) as tmp_dir: | |
| print(f"Processing in <{tmp_dir}>") | |
| ptmp_dir = Path(tmp_dir) | |
| """ | |
| patch(est, est_old, est_new) | |
| text = est.read_text() | |
| order_old = """ # update | |
| dataset.set_transform(i, xform.matrix) | |
| """ | |
| order_new = """ # update | |
| dataset.set_transform(i, xform.matrix) | |
| with open(ptmp_dir / "order.txt", "a") as _f: | |
| _f.write(f"{i}\\n") | |
| """ | |
| if text.count(order_old) != 1: | |
| sys.exit("order pattern not found") | |
| est.write_text(text.replace(order_old, order_new)) | |
| ants = PKG / "registration" / "ants.py" | |
| ants_old = """ if num_threads: | |
| reg_iface.inputs.num_threads = num_threads | |
| return reg_iface | |
| """ | |
| ants_new = """ if num_threads: | |
| reg_iface.inputs.num_threads = num_threads | |
| import os | |
| if os.environ.get("NIFREEZE_ANTS_FLOAT") == "1": | |
| reg_iface.inputs.float = True | |
| return reg_iface | |
| """ | |
| patch(ants, ants_old, ants_new) | |
| print("patched") | |