|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import argparse |
| 4 | +from statistics import median |
| 5 | +from time import perf_counter |
| 6 | +from typing import Callable |
| 7 | + |
| 8 | +import numpy as np |
| 9 | + |
| 10 | + |
| 11 | +def parser(description: str) -> argparse.ArgumentParser: |
| 12 | + arg_parser = argparse.ArgumentParser(description=description) |
| 13 | + arg_parser.add_argument( |
| 14 | + "--path", |
| 15 | + default=None, |
| 16 | + help="Path to the external multicut problem. Defaults to the package cache.", |
| 17 | + ) |
| 18 | + arg_parser.add_argument( |
| 19 | + "--repeats", |
| 20 | + type=int, |
| 21 | + default=3, |
| 22 | + help="Number of timed repeats per implementation.", |
| 23 | + ) |
| 24 | + arg_parser.add_argument( |
| 25 | + "--threads", |
| 26 | + type=int, |
| 27 | + default=1, |
| 28 | + help="Number of threads for solvers that support it.", |
| 29 | + ) |
| 30 | + arg_parser.add_argument( |
| 31 | + "--timeout", |
| 32 | + type=float, |
| 33 | + default=30.0, |
| 34 | + help="Download timeout in seconds if the external problem is not cached.", |
| 35 | + ) |
| 36 | + arg_parser.add_argument( |
| 37 | + "--energy-bound", |
| 38 | + type=float, |
| 39 | + default=-76900.0, |
| 40 | + help="Maximum accepted energy for both implementations.", |
| 41 | + ) |
| 42 | + return arg_parser |
| 43 | + |
| 44 | + |
| 45 | +def load_problem(path: str | None, *, timeout: float): |
| 46 | + import bioimage_cpp as bic |
| 47 | + import nifty |
| 48 | + |
| 49 | + uv_ids, costs = bic.graph.load_external_multicut_problem_data( |
| 50 | + path, |
| 51 | + timeout=timeout, |
| 52 | + ) |
| 53 | + bic_graph = bic.graph.UndirectedGraph.from_edges(int(uv_ids.max()) + 1, uv_ids) |
| 54 | + nifty_graph = nifty.graph.undirectedGraph(int(uv_ids.max()) + 1) |
| 55 | + nifty_graph.insertEdges(uv_ids) |
| 56 | + return bic_graph, nifty_graph, costs |
| 57 | + |
| 58 | + |
| 59 | +def time_call(function: Callable[[], np.ndarray], repeats: int): |
| 60 | + timings = [] |
| 61 | + result = None |
| 62 | + for _ in range(repeats): |
| 63 | + start = perf_counter() |
| 64 | + result = function() |
| 65 | + timings.append(perf_counter() - start) |
| 66 | + assert result is not None |
| 67 | + return timings, result |
| 68 | + |
| 69 | + |
| 70 | +def optimize_bic_solver(make_bic_solver, objective): |
| 71 | + objective.reset_labels() |
| 72 | + return make_bic_solver().optimize(objective) |
| 73 | + |
| 74 | + |
| 75 | +def bic_energy(graph, costs: np.ndarray, labels: np.ndarray) -> float: |
| 76 | + import bioimage_cpp as bic |
| 77 | + |
| 78 | + return bic.graph.MulticutObjective(graph, costs).energy(labels) |
| 79 | + |
| 80 | + |
| 81 | +def nifty_energy(graph, costs: np.ndarray, labels: np.ndarray) -> float: |
| 82 | + import nifty.graph.opt.multicut as nmc |
| 83 | + |
| 84 | + return float(nmc.multicutObjective(graph, costs).evalNodeLabels(labels)) |
| 85 | + |
| 86 | + |
| 87 | +def run_comparison( |
| 88 | + name: str, |
| 89 | + make_bic_solver, |
| 90 | + make_nifty_solver, |
| 91 | + args: argparse.Namespace, |
| 92 | +) -> dict[str, float]: |
| 93 | + import bioimage_cpp as bic |
| 94 | + import nifty.graph.opt.multicut as nmc |
| 95 | + |
| 96 | + bic_graph, nifty_graph, costs = load_problem(args.path, timeout=args.timeout) |
| 97 | + bic_objective = bic.graph.MulticutObjective(bic_graph, costs) |
| 98 | + nifty_objective = nmc.multicutObjective(nifty_graph, costs) |
| 99 | + |
| 100 | + bic_timings, bic_labels = time_call( |
| 101 | + lambda: optimize_bic_solver(make_bic_solver, bic_objective), |
| 102 | + args.repeats, |
| 103 | + ) |
| 104 | + nifty_timings, nifty_labels = time_call( |
| 105 | + lambda: make_nifty_solver(nifty_objective).create(nifty_objective).optimize(), |
| 106 | + args.repeats, |
| 107 | + ) |
| 108 | + |
| 109 | + bic_score = bic_energy(bic_graph, costs, bic_labels) |
| 110 | + nifty_score = nifty_energy(nifty_graph, costs, nifty_labels) |
| 111 | + if bic_score > args.energy_bound: |
| 112 | + raise AssertionError( |
| 113 | + f"bioimage-cpp {name} energy {bic_score:.6f} exceeds bound {args.energy_bound:.6f}" |
| 114 | + ) |
| 115 | + if nifty_score > args.energy_bound: |
| 116 | + raise AssertionError( |
| 117 | + f"nifty {name} energy {nifty_score:.6f} exceeds bound {args.energy_bound:.6f}" |
| 118 | + ) |
| 119 | + |
| 120 | + result = { |
| 121 | + "bioimage_cpp_energy": bic_score, |
| 122 | + "nifty_energy": nifty_score, |
| 123 | + "energy_difference": bic_score - nifty_score, |
| 124 | + "bioimage_cpp_median_runtime": median(bic_timings), |
| 125 | + "nifty_median_runtime": median(nifty_timings), |
| 126 | + } |
| 127 | + print(f"solver: {name}") |
| 128 | + print(f"nodes: {bic_graph.number_of_nodes}, edges: {bic_graph.number_of_edges}") |
| 129 | + print(f"bioimage-cpp energy: {bic_score:.6f}") |
| 130 | + print(f"nifty energy: {nifty_score:.6f}") |
| 131 | + print(f"energy difference: {bic_score - nifty_score:.6f}") |
| 132 | + print(f"bioimage-cpp median runtime [s]: {median(bic_timings):.6f}") |
| 133 | + print(f"nifty median runtime [s]: {median(nifty_timings):.6f}") |
| 134 | + return result |
0 commit comments