from __future__ import annotations import argparse from pathlib import Path import numpy as np def average_expert_masks(mask_paths: list[str | Path], output_path: str | Path) -> Path: """Average binary expert masks into the soft supervision mask used by Stage A.""" if not mask_paths: raise ValueError("At least one expert mask is required") arrays = [] for path in mask_paths: data = np.load(path) arrays.append(np.asarray(data["mask"] if "mask" in data else data[data.files[0]], dtype=np.float32)) shape = arrays[0].shape if any(arr.shape != shape for arr in arrays): raise ValueError("All expert masks must have the same shape before averaging") soft = np.mean(np.stack(arrays, axis=0), axis=0) output_path = Path(output_path) output_path.parent.mkdir(parents=True, exist_ok=True) np.savez_compressed(output_path, mask=soft.astype(np.float32)) return output_path def main() -> None: parser = argparse.ArgumentParser(description="Average LIDC expert mask NPZ files.") parser.add_argument("--masks", nargs="+", required=True) parser.add_argument("--out", required=True) args = parser.parse_args() average_expert_masks(args.masks, args.out) if __name__ == "__main__": main()