From 36de15afd35615be84e16a1891b9e9f3a4337f57 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 25 Sep 2026 19:18:31 +0000 Subject: [PATCH] Generalise the Fiji comparison to any Zenodo dataset; add HUVEC compare_with_fiji.py replaces time_full_movie.py. It runs the original Fiji macro and Fast4DRegPy with the same settings on a registered dataset (synthetic-large-drift or huvec) and compares drift tables, outputs, the archived Fiji result and run times. It reuses PeakRss from process_metrics.py and keeps the strict checks: one results directory, one of each Fiji output, one 'Time taken' entry and matching output shapes. The fiji comparison workflow is manual-only. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKqXa6YXedjwYhQT9vT5WN --- .github/workflows/fiji-comparison.yml | 59 +++ .github/workflows/fiji-full-movie-timing.yml | 49 --- CHANGELOG.md | 4 + docs/validation.md | 2 +- validation/README.md | 9 +- validation/fiji/compare_with_fiji.py | 403 +++++++++++++++++++ validation/fiji/time_full_movie.py | 344 ---------------- 7 files changed, 472 insertions(+), 398 deletions(-) create mode 100644 .github/workflows/fiji-comparison.yml delete mode 100644 .github/workflows/fiji-full-movie-timing.yml create mode 100644 validation/fiji/compare_with_fiji.py delete mode 100644 validation/fiji/time_full_movie.py diff --git a/.github/workflows/fiji-comparison.yml b/.github/workflows/fiji-comparison.yml new file mode 100644 index 0000000..060adad --- /dev/null +++ b/.github/workflows/fiji-comparison.yml @@ -0,0 +1,59 @@ +name: fiji comparison + +# Runs the original Fast4DReg Fiji macro (v2.1, without a screen under Xvfb) +# and Fast4DRegPy with the same settings on the same runner, on a dataset +# from the Fast4DReg Zenodo archive. Compares drift tables, corrected stacks, +# the archived Fiji result, run time and memory. Manual only. +on: + workflow_dispatch: + inputs: + dataset: + description: "Dataset" + default: huvec + type: choice + options: [huvec, synthetic-large-drift] + +jobs: + compare: + runs-on: ubuntu-latest + timeout-minutes: 360 + env: + DATASET: ${{ inputs.dataset || 'huvec' }} + steps: + - uses: actions/checkout@v7 + - uses: actions/setup-python@v7 + with: + python-version: "3.12" + cache: pip + - run: python -m pip install --upgrade pip + - run: python -m pip install -e ".[validation]" + - run: sudo apt-get update && sudo apt-get install -y xvfb + - run: java -version && nproc && free -g + - name: Fetch the Fast4DReg Fiji sources (commit 0483ace) + run: | + git clone https://github.com/CellMigrationLab/Fast4DReg ../Fast4DReg + git -C ../Fast4DReg checkout 0483ace1ac5b8ee0e3938d00c303104109897803 + - run: validation/fiji/setup_imagej.sh "$RUNNER_TEMP/ImageJ" ../Fast4DReg + - name: Cache the Zenodo archive + uses: actions/cache@v4 + with: + path: .validation-data/*.zip + key: zenodo-7514913-${{ env.DATASET }}-v1 + - name: Compare Fiji and Fast4DRegPy + env: + FIJI_XMX: 13g + run: > + python validation/fiji/compare_with_fiji.py "$RUNNER_TEMP/ImageJ" + "../Fast4DReg/Fast4DReg/macros/time_estimate+apply.ijm" + --dataset "$DATASET" --download --workers 4 + --output-dir "validation-results/fiji-comparison-$DATASET" + - if: always() + run: cat validation-results/fiji-comparison-*/summary.json validation-results/fiji-comparison-*/per_frame.csv validation-results/fiji-comparison-*/fiji_log.txt || true + - if: always() + uses: actions/upload-artifact@v4 + with: + name: fiji-comparison-${{ env.DATASET }} + path: | + validation-results/fiji-comparison-*/*.json + validation-results/fiji-comparison-*/*.csv + validation-results/fiji-comparison-*/*.txt diff --git a/.github/workflows/fiji-full-movie-timing.yml b/.github/workflows/fiji-full-movie-timing.yml deleted file mode 100644 index 664148b..0000000 --- a/.github/workflows/fiji-full-movie-timing.yml +++ /dev/null @@ -1,49 +0,0 @@ -name: fiji full-movie timing - -# Runs the original Fast4DReg Fiji macro (v2.1, headless under Xvfb) and -# Fast4DRegPy on the complete published synthetic movie on the same runner, -# records runtime and memory for both, and compares Fiji's drift tables and -# corrected stack with Fast4DRegPy and the archived Fiji result. Manual only. -on: - workflow_dispatch: - -jobs: - time: - runs-on: ubuntu-latest - timeout-minutes: 360 - steps: - - uses: actions/checkout@v7 - - uses: actions/setup-python@v7 - with: - python-version: "3.12" - cache: pip - - run: python -m pip install --upgrade pip - - run: python -m pip install -e ".[validation]" - - run: sudo apt-get update && sudo apt-get install -y xvfb - - run: java -version && javac -version && nproc && free -g - - name: Fetch the Fast4DReg Fiji sources (commit 0483ace) - run: | - git clone https://github.com/CellMigrationLab/Fast4DReg ../Fast4DReg - git -C ../Fast4DReg checkout 0483ace1ac5b8ee0e3938d00c303104109897803 - - run: validation/fiji/setup_imagej.sh "$RUNNER_TEMP/ImageJ" ../Fast4DReg - - name: Cache the verified Zenodo archive - uses: actions/cache@v4 - with: - path: .validation-data/2-Synthetic-drift-dataset.zip - key: zenodo-7514913-2-Synthetic-drift-dataset-27e3623f58d23d7c50674edc9432fe1b - - name: Time Fiji and Fast4DRegPy - env: - FIJI_XMX: 12g - run: > - python validation/fiji/time_full_movie.py "$RUNNER_TEMP/ImageJ" - "../Fast4DReg/Fast4DReg/macros/time_estimate+apply.ijm" - --download --workers 4 - - if: always() - run: cat validation-results/fiji-timing/timing_and_parity.json validation-results/fiji-timing/fiji_log.txt || true - - if: always() - uses: actions/upload-artifact@v4 - with: - name: fiji-full-movie-timing - path: | - validation-results/fiji-timing/*.json - validation-results/fiji-timing/*.txt diff --git a/CHANGELOG.md b/CHANGELOG.md index abb18e4..fd026c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,10 @@ setting was previously accepted but ignored. - Full-movie validation now fails if reference fitting, commit identification, Fiji timing extraction or output-shape comparison is incomplete. +- `validation/fiji/compare_with_fiji.py` replaces `time_full_movie.py`. It + runs Fiji and Fast4DRegPy on any registered Zenodo dataset + (`synthetic-large-drift` or `huvec`) and is available as the manual + **fiji comparison** workflow. ## 0.5.0 diff --git a/docs/validation.md b/docs/validation.md index 06b3923..7ca35f5 100644 --- a/docs/validation.md +++ b/docs/validation.md @@ -121,7 +121,7 @@ The default strict gate requires: - serial and parallel results to be identical. The repository also has manual **full-movie validation** and -**fiji full-movie timing** Actions workflows. Details for reproducing the Fiji +**fiji comparison** Actions workflows. Details for reproducing the Fiji run locally are in [validation/README.md](../validation/README.md). ## Data licence diff --git a/validation/README.md b/validation/README.md index 519bd32..9eb8799 100644 --- a/validation/README.md +++ b/validation/README.md @@ -58,10 +58,11 @@ the patched macro on a virtual display. Scripts: -- `time_full_movie.py` runs Fiji and Fast4DRegPy on the full movie on the same - machine and compares their drift tables, outputs and run times. It is also - available as the **fiji full-movie timing** workflow; results are in - `results/fiji-full-movie-timing/`. +- `compare_with_fiji.py --dataset NAME` runs Fiji and Fast4DRegPy with the + same settings on a dataset from the Zenodo archive (`synthetic-large-drift` + or `huvec`) and compares their drift tables, outputs, the archived Fiji + result and run times. It is also available as the **fiji comparison** + workflow. Earlier timing results are in `results/fiji-full-movie-timing/`. - `make_macro_fixtures.py` regenerates `tests/data/fiji_macro/`, and `make_imagej_interpolation_fixture.py` regenerates `tests/data/imagej_interpolation/`: diff --git a/validation/fiji/compare_with_fiji.py b/validation/fiji/compare_with_fiji.py new file mode 100644 index 0000000..2ba365d --- /dev/null +++ b/validation/fiji/compare_with_fiji.py @@ -0,0 +1,403 @@ +"""Compare Fast4DRegPy with a fresh run of the Fiji macro on a Zenodo dataset. + +For the chosen dataset, the script downloads and checks the Zenodo archive, +runs the original ``time_estimate+apply.ijm`` in ImageJ (see +``patch_macro.py``), runs Fast4DRegPy with the same settings on the same +machine, and compares: + +- the drift tables, frame by frame; +- the corrected stacks; +- the fresh Fiji output with the Fiji result stored in the archive; +- run time and peak memory. + +Usage:: + + python validation/fiji/compare_with_fiji.py IMAGEJ_DIR MACRO \\ + --dataset huvec --download --output-dir validation-results/huvec +""" + +from __future__ import annotations + +import os +import sys + +os.environ.setdefault("OMP_NUM_THREADS", "1") + +import argparse +import csv +import hashlib +import json +import resource +import shutil +import subprocess +import time +import urllib.request +import zipfile +from pathlib import Path + +import numpy as np +import tifffile + +HERE = Path(__file__).resolve().parent +sys.path.insert(0, str(HERE)) + +from make_macro_fixtures import FIRST, PREVIOUS, read_njt # noqa: E402 +from process_metrics import PeakRss # noqa: E402 + +from fast4dreg import apply_drift, estimate_time_drift # noqa: E402 + +ZENODO_RECORD = "7514913" +BASE_SETTINGS = { + "exp_nro": 1, + "XY_registration": True, + "projection_type_xy": "Max Intensity", + "time_xy": 1, + "max_xy": 0, + "reference_xy": FIRST, + "crop_output": False, + "z_registration": True, + "projection_type_z": "Max Intensity", + "reslice_mode": "Top", + "time_z": 1, + "max_z": 0, + "reference_z": FIRST, + "extend_stack_to_fit": False, + "ram_conservative_mode": False, +} +DATASETS = { + "synthetic-large-drift": { + "archive": "2-Synthetic-drift-dataset.zip", + "md5": "27e3623f58d23d7c50674edc9432fe1b", + "raw": "2-Synthetic-drift-dataset/2.1-Synthetic-drift-files/" + "2.1.2-LargeDrift_original.tif", + "archived_fiji": "2-Synthetic-drift-dataset/" + "2.2-Synthetic-drift-expected-results/2.2.1-LargeDrift_F4DReg.tif", + "axes": "TZYX", + "settings": {**BASE_SETTINGS, "crop_output": True}, + }, + "huvec": { + "archive": "4-HUVEC-monolayer-dataset.zip", + "md5": "8fa5f595ed267d1c66b3f6b1b1237ef5", + "raw": "4-HUVEC-monolayer-dataset/4.1-Image11_original.tif", + "archived_fiji": "4-HUVEC-monolayer-dataset/4.2-Image11_Fast4D.tif", + "axes": "TZYX", + "settings": dict(BASE_SETTINGS), + }, +} +PROJECTIONS = {"Max Intensity": "max", "Average Intensity": "mean"} + + +# ------------------------------------------------------------------ data +def md5sum(path: Path) -> str: + digest = hashlib.md5() + with path.open("rb") as handle: + for block in iter(lambda: handle.read(1 << 24), b""): + digest.update(block) + return digest.hexdigest() + + +def fetch(dataset: dict, data_dir: Path, *, download: bool) -> tuple[Path, Path]: + """Return verified paths to the raw and archived-Fiji TIFFs.""" + data_dir.mkdir(parents=True, exist_ok=True) + archive = data_dir / dataset["archive"] + if not archive.exists(): + if not download: + raise FileNotFoundError(f"{archive} not found; pass --download") + url = ( + f"https://zenodo.org/api/records/{ZENODO_RECORD}/files/" + f"{dataset['archive']}/content" + ) + partial = archive.with_suffix(".partial") + urllib.request.urlretrieve(url, partial) + partial.rename(archive) + if md5sum(archive) != dataset["md5"]: + raise RuntimeError(f"{archive} does not have MD5 {dataset['md5']}") + with zipfile.ZipFile(archive) as bundle: + for member in (dataset["raw"], dataset["archived_fiji"]): + target = data_dir / member + if ( + not target.exists() + or target.stat().st_size != bundle.getinfo(member).file_size + ): + bundle.extract(member, data_dir) + return data_dir / dataset["raw"], data_dir / dataset["archived_fiji"] + + +def python_options(settings: dict, axes: str) -> dict: + """Translate Fiji macro settings into Fast4DRegPy keyword arguments.""" + for xy, z in ( + ("projection_type_xy", "projection_type_z"), + ("reference_xy", "reference_z"), + ("time_xy", "time_z"), + ("max_xy", "max_z"), + ): + if settings[xy] != settings[z]: + raise ValueError(f"Fast4DRegPy uses one value for {xy} and {z}") + if settings["ram_conservative_mode"]: + raise ValueError("Fiji 'Save RAM' is not reproduced by Fast4DRegPy") + return { + "axes": axes, + "projection": PROJECTIONS[settings["projection_type_xy"]], + "reference": {FIRST: "first", PREVIOUS: "previous"}[settings["reference_xy"]], + "correct_xy": settings["XY_registration"], + "correct_z": settings["z_registration"], + "axial_projection_axis": {"Top": "Y", "Left": "X"}[settings["reslice_mode"]], + "time_averaging": settings["time_xy"], + "max_expected_drift": settings["max_xy"], + } + + +# ------------------------------------------------------------------- Fiji +def _one_match(directory: Path, pattern: str) -> Path: + matches = list(directory.glob(pattern)) + if len(matches) != 1: + raise RuntimeError( + f"expected one {pattern!r} in {directory}, found {len(matches)}" + ) + return matches[0] + + +def _macro_seconds(log: str) -> float: + values = [ + float(line.split("Time taken:")[1].split("s")[0]) + for line in log.splitlines() + if "Time taken:" in line + ] + if len(values) != 1: + raise RuntimeError( + f"expected one Fiji 'Time taken:' entry, found {len(values)}" + ) + return values[0] + + +def run_fiji( + imagej: Path, macro: Path, raw_path: Path, settings: dict, work: Path +) -> dict: + work.mkdir(parents=True, exist_ok=True) + (work / "settings.json").write_text( + json.dumps({**settings, "files": str(raw_path)}), encoding="utf-8" + ) + # The macro writes to _-; remove a stale folder first. + for stale in raw_path.parent.glob(raw_path.stem + "_*"): + if stale.is_dir(): + shutil.rmtree(stale) + before = {p for p in raw_path.parent.iterdir() if p.is_dir()} + start = time.perf_counter() + with PeakRss() as rss: + completed = subprocess.run( + [ + HERE / "run_fiji_macro.sh", + imagej, + macro, + work / "settings.json", + work / "run", + "14400", + ], + capture_output=True, + text=True, + ) + seconds = time.perf_counter() - start + if completed.returncode != 0: + raise RuntimeError(f"Fiji failed:\n{completed.stdout}\n{completed.stderr}") + new = [p for p in raw_path.parent.iterdir() if p.is_dir() and p not in before] + if len(new) != 1: + raise RuntimeError(f"Fiji should create one results directory, found {new}") + results = new[0] + log = (work / "run" / "fiji_log.txt").read_text() + macro_seconds = _macro_seconds(log) + xy = read_njt(_one_match(results, "*_xy_DriftTable.njt")) + z = read_njt(_one_match(results, "*_z_DriftTable.njt")) + shifts = np.column_stack( + [z["Y-Drift (pixels)"], xy["Y-Drift (pixels)"], xy["X-Drift (pixels)"]] + ) + return { + "wall_seconds_including_jvm_start": seconds, + "macro_reported_seconds": macro_seconds, + "peak_java_rss_gb": rss.peak / 1e9, + "shifts": shifts, + "output_path": _one_match(results, "*_xyzCorrected.tif"), + "log": log, + } + + +# ---------------------------------------------------------------- Python +def run_python(raw: np.ndarray, options: dict, settings: dict, workers: int) -> dict: + start = time.perf_counter() + shifts = estimate_time_drift( + raw, crop_output=settings["crop_output"], workers=workers, **options + ) + estimated = time.perf_counter() + corrected = apply_drift( + raw, + shifts, + axes=options["axes"], + axial_projection_axis=options["axial_projection_axis"], + crop_output=settings["crop_output"], + extend_z=settings["extend_stack_to_fit"], + workers=workers, + ) + done = time.perf_counter() + return { + "shifts": shifts, + "corrected": corrected, + "estimate_seconds": estimated - start, + "apply_seconds": done - estimated, + "total_seconds": done - start, + } + + +def pearson(a: np.ndarray, b: np.ndarray) -> float: + return float(np.corrcoef(np.ravel(a), np.ravel(b))[0, 1]) + + +def rounded(volume: np.ndarray, dtype: np.dtype) -> np.ndarray: + info = np.iinfo(dtype) + return np.clip(np.floor(np.asarray(volume, np.float64) + 0.5), info.min, info.max) + + +def open_stack(path: Path) -> np.ndarray: + """Memory-map an uncompressed TIFF when possible, else read it.""" + try: + return tifffile.memmap(path, mode="r") + except ValueError: + return tifffile.imread(path) + + +# ------------------------------------------------------------------ main +def main() -> int: + parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) + parser.add_argument("imagej_dir", type=Path) + parser.add_argument("macro", type=Path, help="original time_estimate+apply.ijm") + parser.add_argument("--dataset", choices=sorted(DATASETS), required=True) + parser.add_argument("--data-dir", type=Path, default=Path(".validation-data")) + parser.add_argument("--output-dir", type=Path, required=True) + parser.add_argument("--download", action="store_true") + parser.add_argument("--workers", type=int, default=4) + args = parser.parse_args() + args.output_dir.mkdir(parents=True, exist_ok=True) + + dataset = DATASETS[args.dataset] + settings = dataset["settings"] + options = python_options(settings, dataset["axes"]) + raw_path, archived_path = fetch(dataset, args.data_dir, download=args.download) + + fiji = run_fiji( + args.imagej_dir, args.macro, raw_path, settings, args.output_dir / "fiji-work" + ) + print( + f"Fiji: {fiji['macro_reported_seconds']:.1f} s, " + f"peak {fiji['peak_java_rss_gb']:.1f} GB" + ) + + raw = tifffile.imread(raw_path) + ours = run_python(raw, options, settings, workers=1) + print(f"Fast4DRegPy workers=1: {ours['total_seconds']:.1f} s") + parallel = run_python(raw, options, settings, workers=args.workers) + print(f"Fast4DRegPy workers={args.workers}: {parallel['total_seconds']:.1f} s") + identical = bool( + np.array_equal(parallel["shifts"], ours["shifts"]) + and np.array_equal(parallel["corrected"], ours["corrected"]) + ) + timing_parallel = { + k: parallel[k] for k in ("estimate_seconds", "apply_seconds", "total_seconds") + } + del parallel + python_peak_gb = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1e6 + + fresh = open_stack(fiji["output_path"]) + archived = open_stack(archived_path) + if ours["corrected"].shape != fresh.shape: + raise RuntimeError( + "Fast4DRegPy and fresh Fiji output shapes differ: " + f"{ours['corrected'].shape} vs {fresh.shape}" + ) + frames = raw.shape[0] + rows = [] + for t in range(frames): + row = {"frame": t} + for axis, name in enumerate(("dz", "dy", "dx")): + row[f"fiji_{name}"] = float(fiji["shifts"][t, axis]) + row[f"fast4dregpy_{name}"] = float(ours["shifts"][t, axis]) + row[f"difference_{name}"] = float( + ours["shifts"][t, axis] - fiji["shifts"][t, axis] + ) + row["pearson_vs_fresh_fiji"] = pearson(ours["corrected"][t], fresh[t]) + row["identical_voxels_vs_fresh_fiji"] = float( + np.mean( + rounded(ours["corrected"][t], raw.dtype) == rounded(fresh[t], raw.dtype) + ) + ) + if archived.shape == fresh.shape: + row["fresh_fiji_identical_to_archive"] = float( + np.mean(rounded(fresh[t], archived.dtype) == archived[t]) + ) + row["pearson_fresh_fiji_vs_archive"] = pearson(fresh[t], archived[t]) + row["pearson_vs_archive"] = ( + pearson(ours["corrected"][t], archived[t]) + if ours["corrected"].shape == archived.shape + else None + ) + rows.append(row) + with (args.output_dir / "per_frame.csv").open( + "w", newline="", encoding="utf-8" + ) as handle: + writer = csv.DictWriter(handle, fieldnames=list(rows[0])) + writer.writeheader() + writer.writerows(rows) + + difference = np.abs(ours["shifts"] - fiji["shifts"]) + + def minimum(key: str): + values = [r[key] for r in rows if r.get(key) is not None] + return min(values) if values else None + + result = { + "dataset": {k: v for k, v in dataset.items() if k != "settings"}, + "raw_shape": list(raw.shape), + "raw_dtype": str(raw.dtype), + "fiji_settings": settings, + "fast4dregpy_options": { + **options, + "crop_output": settings["crop_output"], + "extend_z": settings["extend_stack_to_fit"], + }, + "machine": { + "cpus": os.cpu_count(), + "omp_num_threads": os.environ["OMP_NUM_THREADS"], + }, + "fiji": { + k: v for k, v in fiji.items() if k not in ("shifts", "output_path", "log") + }, + "fast4dregpy": { + "1": { + k: ours[k] + for k in ("estimate_seconds", "apply_seconds", "total_seconds") + }, + str(args.workers): timing_parallel, + "peak_rss_gb": python_peak_gb, + }, + "serial_equals_parallel": identical, + "fresh_fiji_shape": list(fresh.shape), + "fast4dregpy_shape": list(ours["corrected"].shape), + "archived_shape": list(archived.shape), + "max_abs_difference_dzdydx": difference.max(axis=0).tolist(), + "mean_abs_difference_dzdydx": difference.mean(axis=0).tolist(), + "max_abs_fiji_drift_dzdydx": np.abs(fiji["shifts"]).max(axis=0).tolist(), + "min_pearson_vs_fresh_fiji": minimum("pearson_vs_fresh_fiji"), + "min_identical_voxels_vs_fresh_fiji": minimum("identical_voxels_vs_fresh_fiji"), + "min_fresh_fiji_identical_to_archive": minimum( + "fresh_fiji_identical_to_archive" + ), + "min_pearson_fresh_fiji_vs_archive": minimum("pearson_fresh_fiji_vs_archive"), + "min_pearson_vs_archive": minimum("pearson_vs_archive"), + } + (args.output_dir / "summary.json").write_text( + json.dumps(result, indent=2), encoding="utf-8" + ) + (args.output_dir / "fiji_log.txt").write_text(fiji["log"], encoding="utf-8") + print(json.dumps(result, indent=2)) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/validation/fiji/time_full_movie.py b/validation/fiji/time_full_movie.py deleted file mode 100644 index 544065e..0000000 --- a/validation/fiji/time_full_movie.py +++ /dev/null @@ -1,344 +0,0 @@ -"""Time Fiji Fast4DReg and Fast4DRegPy on the published full movie.""" - -from __future__ import annotations - -import os -import sys - -os.environ.setdefault("OMP_NUM_THREADS", "1") - -import argparse -import json -import resource -import shutil -import subprocess -import time -from pathlib import Path - -import numpy as np -import tifffile - -HERE = Path(__file__).resolve().parent -sys.path.insert(0, str(HERE.parent / "full_movie")) -sys.path.insert(0, str(HERE)) - -from data import extract, fetch_archive # noqa: E402 -from make_macro_fixtures import FIRST, read_njt # noqa: E402 -from metrics import pearson # noqa: E402 -from process_metrics import PeakRss # noqa: E402 - -from fast4dreg import apply_drift, estimate_time_drift # noqa: E402 - -SETTINGS = { - "exp_nro": 1, - "XY_registration": True, - "projection_type_xy": "Max Intensity", - "time_xy": 1, - "max_xy": 0, - "reference_xy": FIRST, - "crop_output": True, - "z_registration": True, - "projection_type_z": "Max Intensity", - "reslice_mode": "Top", - "time_z": 1, - "max_z": 0, - "reference_z": FIRST, - "extend_stack_to_fit": False, - "ram_conservative_mode": False, -} - - -def _one_match(directory: Path, pattern: str) -> Path: - matches = list(directory.glob(pattern)) - if len(matches) != 1: - raise RuntimeError( - f"expected one {pattern!r} in {directory}, found {len(matches)}" - ) - return matches[0] - - -def _macro_seconds(log: str) -> float: - values = [] - for line in log.splitlines(): - if "Time taken:" in line: - values.append(float(line.split("Time taken:")[1].split("s")[0])) - if len(values) != 1: - raise RuntimeError( - f"expected one Fiji 'Time taken:' entry, found {len(values)}" - ) - return values[0] - - -def run_fiji(imagej: Path, macro: Path, raw_path: Path, work: Path) -> dict: - work.mkdir(parents=True, exist_ok=True) - settings = {**SETTINGS, "files": str(raw_path)} - (work / "settings.json").write_text(json.dumps(settings), encoding="utf-8") - - for stale in raw_path.parent.glob(raw_path.stem + "_*"): - if stale.is_dir(): - shutil.rmtree(stale) - before = {path for path in raw_path.parent.iterdir() if path.is_dir()} - - start = time.perf_counter() - with PeakRss() as rss: - completed = subprocess.run( - [ - HERE / "run_fiji_macro.sh", - imagej, - macro, - work / "settings.json", - work / "run", - "10800", - ], - capture_output=True, - text=True, - ) - wall_seconds = time.perf_counter() - start - if completed.returncode != 0: - raise RuntimeError(f"Fiji failed:\n{completed.stdout}\n{completed.stderr}") - - new_directories = [ - path - for path in raw_path.parent.iterdir() - if path.is_dir() and path not in before - ] - if len(new_directories) != 1: - raise RuntimeError( - "Fiji should create exactly one results directory; " - f"found {new_directories}" - ) - - log = (work / "run" / "fiji_log.txt").read_text(encoding="utf-8") - return { - "wall_seconds_including_jvm_start": wall_seconds, - "macro_reported_seconds": _macro_seconds(log), - "peak_java_rss_gb": rss.peak / 1e9, - "results_dir": new_directories[0], - "log": log, - } - - -def run_python(raw: np.ndarray, workers: int) -> dict: - options = { - "axes": "TZYX", - "projection": "max", - "reference": "first", - "correct_xy": True, - "correct_z": True, - "axial_projection_axis": "Y", - "time_averaging": 1, - "max_expected_drift": 0, - "crop_output": True, - "workers": workers, - } - start = time.perf_counter() - shifts = estimate_time_drift(raw, **options) - estimated = time.perf_counter() - corrected = apply_drift( - raw, - shifts, - axes="TZYX", - crop_output=True, - workers=workers, - ) - finished = time.perf_counter() - return { - "shifts": shifts, - "corrected": corrected, - "estimate_seconds": estimated - start, - "apply_seconds": finished - estimated, - "total_seconds": finished - start, - } - - -def _read_fiji_outputs(results_dir: Path) -> tuple[np.ndarray, np.ndarray]: - xy = read_njt(_one_match(results_dir, "*_xy_DriftTable.njt")) - z = read_njt(_one_match(results_dir, "*_z_DriftTable.njt")) - shifts = np.column_stack( - [z["Y-Drift (pixels)"], xy["Y-Drift (pixels)"], xy["X-Drift (pixels)"]] - ) - corrected = tifffile.imread(_one_match(results_dir, "*_xyzCorrected.tif")) - return shifts, corrected - - -def _run_python_variants(raw: np.ndarray, workers: int) -> dict[int, dict]: - runs = { - count: run_python(raw, count) - for count in sorted({1, workers}) - } - for count, run in runs.items(): - print(f"Fast4DRegPy workers={count}: {run['total_seconds']:.1f} s") - return runs - - -def _compare_outputs( - *, - raw: np.ndarray, - archived: np.ndarray, - fiji_output: np.ndarray, - fiji_shifts: np.ndarray, - python: dict[int, dict], -) -> dict: - ours = python[1] - if ours["corrected"].shape != fiji_output.shape: - raise RuntimeError( - "Fast4DRegPy and fresh Fiji output shapes differ: " - f"{ours['corrected'].shape} vs {fiji_output.shape}" - ) - - height = min(fiji_output.shape[2], archived.shape[2]) - width = min(fiji_output.shape[3], archived.shape[3]) - fresh_vs_archive = [ - pearson( - fiji_output[frame, :, :height, :width], - archived[frame, :, :height, :width], - ) - for frame in range(raw.shape[0]) - ] - fresh_rounded = np.clip( - np.floor(fiji_output[:, :, :height, :width] + 0.5), - 0, - 65535, - ) - identical_to_archive = float( - np.mean(fresh_rounded == archived[:, :, :height, :width]) - ) - ours_vs_fresh = [ - pearson(ours["corrected"][frame], fiji_output[frame]) - for frame in range(raw.shape[0]) - ] - difference = np.abs(ours["shifts"] - fiji_shifts) - - return { - "fiji_output_shape": list(fiji_output.shape), - "fiji_output_dtype": str(fiji_output.dtype), - "fast4dregpy_output_shape": list(ours["corrected"].shape), - "archived_shape": list(archived.shape), - "max_abs_fast4dregpy_minus_fiji_dzdydx": difference.max(axis=0).tolist(), - "mean_abs_fast4dregpy_minus_fiji_dzdydx": difference.mean(axis=0).tolist(), - "fresh_fiji_vs_archive_pearson_min": min(fresh_vs_archive), - "fresh_fiji_vs_archive_identical_voxels_after_uint16": identical_to_archive, - "fast4dregpy_vs_fresh_fiji_pearson_min": min(ours_vs_fresh), - } - - -def _result( - *, - fiji: dict, - python: dict[int, dict], - parity: dict, - fiji_shifts: np.ndarray, -) -> dict: - ours = python[1] - return { - "machine": { - "cpus": os.cpu_count(), - "omp_num_threads": os.environ["OMP_NUM_THREADS"], - }, - "settings": SETTINGS, - "fiji": { - key: value - for key, value in fiji.items() - if key not in ("results_dir", "log") - }, - "fast4dregpy": { - str(workers): { - key: run[key] - for key in ("estimate_seconds", "apply_seconds", "total_seconds") - } - for workers, run in python.items() - }, - "fast4dregpy_process_peak_rss_gb": resource.getrusage( - resource.RUSAGE_SELF - ).ru_maxrss - / 1e6, - "serial_equals_parallel": bool( - all( - np.array_equal(run["shifts"], ours["shifts"]) - for run in python.values() - ) - and all( - np.array_equal(run["corrected"], ours["corrected"]) - for run in python.values() - ) - ), - **parity, - "fiji_drift_table_dzdydx": fiji_shifts.tolist(), - "fast4dregpy_dzdydx": ours["shifts"].tolist(), - } - - -def _parser() -> argparse.ArgumentParser: - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("imagej_dir", type=Path) - parser.add_argument("macro", type=Path, help="original time_estimate+apply.ijm") - parser.add_argument("--data-dir", type=Path, default=Path(".validation-data")) - parser.add_argument( - "--output-dir", - type=Path, - default=Path("validation-results/fiji-timing"), - ) - parser.add_argument("--download", action="store_true") - parser.add_argument("--workers", type=int, default=4) - return parser - - -def main() -> int: - args = _parser().parse_args() - args.output_dir.mkdir(parents=True, exist_ok=True) - - archive = fetch_archive(args.data_dir, download=args.download) - raw_path, fiji_path, _ = extract(archive, args.data_dir) - raw = tifffile.imread(raw_path) - archived = tifffile.imread(fiji_path) - - fiji = run_fiji( - args.imagej_dir, - args.macro, - raw_path, - args.output_dir / "fiji-work", - ) - print( - f"Fiji: {fiji['wall_seconds_including_jvm_start']:.1f} s wall, " - f"{fiji['macro_reported_seconds']:.1f} s reported, " - f"peak {fiji['peak_java_rss_gb']:.2f} GB" - ) - fiji_shifts, fiji_output = _read_fiji_outputs(fiji["results_dir"]) - python = _run_python_variants(raw, args.workers) - parity = _compare_outputs( - raw=raw, - archived=archived, - fiji_output=fiji_output, - fiji_shifts=fiji_shifts, - python=python, - ) - result = _result( - fiji=fiji, - python=python, - parity=parity, - fiji_shifts=fiji_shifts, - ) - - (args.output_dir / "timing_and_parity.json").write_text( - json.dumps(result, indent=2), - encoding="utf-8", - ) - (args.output_dir / "fiji_log.txt").write_text( - fiji["log"], - encoding="utf-8", - ) - print( - json.dumps( - { - key: value - for key, value in result.items() - if not key.endswith("dzdydx") - }, - indent=2, - ) - ) - return 0 - - -if __name__ == "__main__": - raise SystemExit(main())