"""Predict native center256 HEMIT numerical fluorescence channels from H&E.""" from __future__ import annotations import argparse import json from pathlib import Path import numpy as np from PIL import Image, ImageDraw from inference import load_model, load_rgb_center_crop, predict_tensor def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--input", type=Path, required=True, help="Stored uint8 RGB H&E image, at least256x256") parser.add_argument("--output", type=Path, required=True, help="Output.npy: HWC float32 DAPI/CD3/panCK in[0,1]") parser.add_argument("--preview", type=Path, help="Optional three-panel grayscale PNG, no per-image intensity fit") parser.add_argument("--device", default="cpu") args = parser.parse_args() if args.output.suffix.lower() != ".npy": parser.error("--output must end in.npy") model = load_model(device=args.device) rgb, crop = load_rgb_center_crop(args.input) raw = predict_tensor(model, rgb)[0].permute(1, 2, 0).cpu().numpy() prediction = np.clip(raw, 0, 1).astype(np.float32, copy=False) args.output.parent.mkdir(parents=True, exist_ok=True) np.save(args.output, prediction, allow_pickle=False) raw_path = args.output.with_name(args.output.stem + ".raw.npy") np.save(raw_path, raw, allow_pickle=False) metadata = {"axes": "HWC", "channels": ["DAPI", "CD3", "panCK"], "dtype": "float32", "normalization": "released uint8/255 numerical intensity; no physical calibration", "output": "forward result clamped to[0,1], as used in benchmark metrics", "raw_output": raw_path.name, "raw_output_semantics": "unclamped forward result", "source_crop": crop, "resized": False, "clinical_score": False} args.output.with_suffix(".json").write_text(json.dumps(metadata, indent=2) + "\n") if args.preview: canvas = Image.new("RGB", (256 * 3, 280), "white") draw = ImageDraw.Draw(canvas) for index, name in enumerate(metadata["channels"]): plane = Image.fromarray(np.round(prediction[:, :, index] * 255).astype(np.uint8)) canvas.paste(plane.convert("RGB"), (256 * index, 24)) draw.text((256 * index + 8, 6), name + " [0,1]", fill="black") args.preview.parent.mkdir(parents=True, exist_ok=True) canvas.save(args.preview) print(json.dumps({"output": str(args.output), "raw_output": str(raw_path), "channels": metadata["channels"]})) if __name__ == "__main__": main()