Download predict.py from Dharini24/MEDA: direct link, hf CLI and curl.
- Browser
- Download file 1.44 kB
-
https://huggingface.co/Dharini24/MEDA/resolve/main/predict.py
- Command line
-
hf download hf://Dharini24/MEDA/predict.py
-
curl -L -o predict.py https://huggingface.co/Dharini24/MEDA/resolve/main/predict.py
1.44 kB
| """Score CT embeddings with the MEDA head of one finding. | |
| python predict.py --encoder merlin --finding ascites --embeddings embeddings.npy | |
| ``embeddings.npy`` holds one row per CT study: 2048 columns for Merlin or 1120 for CT-NEXUS. | |
| Prints the score and the positive/negative call for every study. | |
| """ | |
| import argparse | |
| import joblib | |
| import numpy as np | |
| from huggingface_hub import hf_hub_download | |
| REPO = "Dharini24/MEDA" | |
| def load_head(encoder, finding, repo=REPO): | |
| path = hf_hub_download(repo, f"models/{encoder}/{finding}.joblib") | |
| return joblib.load(path) | |
| def predict(head, embeddings): | |
| scores = head["model"].predict_proba(np.asarray(embeddings, dtype=np.float32))[:, 1] | |
| return scores, (scores >= head["threshold"]).astype(int) | |
| if __name__ == "__main__": | |
| ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) | |
| ap.add_argument("--encoder", required=True, choices=["merlin", "ctnexus"]) | |
| ap.add_argument("--finding", required=True) | |
| ap.add_argument("--embeddings", required=True) | |
| args = ap.parse_args() | |
| head = load_head(args.encoder, args.finding) | |
| scores, calls = predict(head, np.load(args.embeddings)) | |
| print(f"{args.finding} ({args.encoder}): {head['name']}, threshold {head['threshold']:.4f}") | |
| for i, (s, c) in enumerate(zip(scores, calls)): | |
| print(f"study {i}: score {s:.4f} -> {'positive' if c else 'negative'}") | |