API: Metrics & helpers
The metrics take probabilities of shape (B, C, *spatial) and integer labels of
shape (B, *spatial), or floating class distributions with the same shape as
the probabilities. mask selects valid voxels for either target kind;
ignore_index applies to hard labels. See multi-rater calibration.
negative_log_likelihood
negative_log_likelihood(probs: Any, targets: Any, mask: Any | None = None, ignore_index: int = -100) -> float
Mean negative log-likelihood (cross-entropy) over valid voxels.
Zero probabilities are floored at 1e-12 by :func:fiducio.utils.safe_log,
capping the NLL at ~27.63 instead of inf.
Source code in src/fiducio/metrics/calibration.py
expected_calibration_error
expected_calibration_error(probs: Any, targets: Any, mask: Any | None = None, ignore_index: int = -100, n_bins: int = 15, *, class_index: int | None = None) -> float
Top-1 expected calibration error (ECE) with uniform binning.
Pass class_index for class-specific calibration against hard or soft
targets. This measures ECE against the supplied targets, not mean rater ECE.
The confidence is the maximum predicted probability and the accuracy is
whether the argmax matches the label. Bins partition [0, 1] uniformly
and each bin's gap is weighted by its share of voxels — see
:func:unweighted_calibration_error for the unweighted variant.
n_bins defaults to 15. The paper reports ECE with n_bins=50 (ACE with
n_bins=15) and averages metrics per image before pooling; pass
n_bins=50 and compute per case to follow that convention. Boundary-aware
ECE is not included.
Source code in src/fiducio/metrics/calibration.py
unweighted_calibration_error
unweighted_calibration_error(probs: Any, targets: Any, mask: Any | None = None, ignore_index: int = -100, n_bins: int = 15, *, class_index: int | None = None) -> float
Top-1 unweighted calibration error (ACE) with uniform binning.
class_index selects a class instead of the default top-1 confidence.
The unweighted mean of the per-bin |confidence - accuracy| gap over
non-empty uniform bins. This is not the adaptive calibration error of
other work, which uses data-dependent (equal-mass) bins.
Uses the same uniform confidence bins as :func:expected_calibration_error,
but weights each non-empty bin equally instead of by its share of voxels; a
confidence region visited by only a handful of voxels therefore counts as
much as a densely populated one. n_bins=15 matches the paper's ACE; the
paper computes ACE on the pooled test voxels rather than per image. Kept as
average_calibration_error for backwards compatibility.
Source code in src/fiducio/metrics/calibration.py
Alias
average_calibration_error is a backwards-compatible alias of
unweighted_calibration_error — the same function, not a separate
estimator.
brier_score
Mean sum of squared class residuals against labels or soft distributions.
Against an MHR, this differs from the mean per-rater Brier by the rater disagreement term. Binary two-channel Brier is twice positive-channel MSE. Residuals accumulate in float64 to retain errors near perfect predictions.
Source code in src/fiducio/metrics/calibration.py
reliability_curve
reliability_curve(probs: Any, targets: Any, mask: Any | None = None, ignore_index: int = -100, n_bins: int = 15, *, class_index: int | None = None) -> ReliabilityCurve
Compute top-1 or class-specific reliability with uniform binning.
The confidence is the maximum predicted probability and the accuracy is
whether the argmax matches the label, or q[argmax(p)] for soft targets.
With class_index=c, compare p_c with 1[y=c] or q_c.
ECE against MHR differs from mean per-rater ECE; retain the individual votes
to compute the latter. Useful both for reporting ECE/ACE and
for drawing reliability diagrams (see
:func:fiducio.plots.reliability_diagram).
Source code in src/fiducio/metrics/calibration.py
ReliabilityCurve
dataclass
ReliabilityCurve(bin_edges: Tensor, bin_confidence: Tensor, bin_accuracy: Tensor, bin_counts: Tensor, ece: float, ace: float)
Per-bin top-1 or class-specific reliability statistics.
Attributes:
| Name | Type | Description |
|---|---|---|
bin_edges |
Tensor
|
|
bin_confidence |
Tensor
|
|
bin_accuracy |
Tensor
|
|
bin_counts |
Tensor
|
|
ece |
float
|
Expected calibration error (count-weighted mean |
ace |
float
|
Unweighted calibration error (unweighted mean |
Diagnostics
diagnose
diagnose(logits: Any, labels: Any, *, input_type: str = 'logits', mask: Any | None = None, ignore_index: int = -100, device: Any | None = None, n_bins: int = 15, class_names: list[str] | None = None, image_ids: list[str] | None = None, background_class: int | None = None, dice_failure_threshold: float = 0.1, n_worst: int = 5, n_boot: int = 200, seed: int = 0, min_images: int = 20, min_class_fraction: float = 0.01, min_class_voxels: int = 1000) -> ReliabilityReport
Diagnose the reliability of a segmenter on a calibration set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logits
|
Any
|
|
required |
labels
|
Any
|
|
required |
mask
|
Any | None
|
Voxel selection, as in the metrics. For soft targets only |
None
|
ignore_index
|
Any | None
|
Voxel selection, as in the metrics. For soft targets only |
None
|
device
|
Any | None
|
Device for the DataLoader path's batches (default: the device of the model's first parameter, otherwise CPU). Ignored for tensor inputs. |
None
|
class_names
|
list[str] | None
|
Optional display names (default: indices). |
None
|
image_ids
|
list[str] | None
|
Optional display names (default: indices). |
None
|
background_class
|
int | None
|
Class excluded from the per-image mean Dice, whose value near 1 would hide a
failure on the classes that matter. |
None
|
dice_failure_threshold
|
float
|
A class fails on an image when it is in the labels and its Dice is below this. An image fails when its mean Dice over the classes present is below it. |
0.1
|
n_worst
|
int
|
Number of lowest-Dice images listed. |
5
|
n_boot
|
int
|
Bootstrap resamples over images for the 95% intervals ( |
200
|
seed
|
int
|
Bootstrap resamples over images for the 95% intervals ( |
200
|
min_images
|
int
|
Thresholds of the warnings. They are heuristics, not statistical guarantees. |
20
|
min_class_fraction
|
int
|
Thresholds of the warnings. They are heuristics, not statistical guarantees. |
20
|
min_class_voxels
|
int
|
Thresholds of the warnings. They are heuristics, not statistical guarantees. |
20
|
Notes
Soft targets. Per-class ECE compares p_c with the target mass q_c and the
top-1/routed ECE compares the top-1 confidence with q[argmax(p)]; NLL and
Brier follow :mod:fiducio.metrics.calibration. Hard-label Dice and the failure
statistics are undefined: every Dice field, the image Dice matrix and the
per-image failure counts are NaN/0, and image_dice is all NaN. Class balance
still counts the expected label mass sum(q_c).
Boundary bins. report.boundary describes the first [0, 1/n_bins] and last
[(n_bins-1)/n_bins, 1] top-1 confidence bins: their voxel mass, their mean
confidence minus accuracy, and their exact contribution to ece. It is
additive reporting only; ece keeps its definition.
The ECE interval is a percentile bootstrap. ECE has a finite-sample noise floor that
resampling raises further, so for a well-calibrated model the interval can sit
slightly above the estimate (it missed it in 15 of 100 simulated calibrated models
of 24 images). Read it as an order of magnitude. The bias line uses the
voxel-weighted confidence-accuracy gap, which has no such noise floor, but its
intervals are slightly narrow: it flagged 9 of those 100 calibrated models, against
5 expected (reproducibility/diagnose_interval_check.py reruns the simulation).
Cost is 2C + 1 reliability curves per image (global, per-class and routed),
plus a few passes for NLL, Brier and the confusion matrix. With a DataLoader the
model forward pass dominates; the accumulator keeps O(n_images) summaries
(O(n_images * C * n_bins)), not the voxels. Classes absent from both labels and
prediction have an undefined Dice and are never counted as failures.
Source code in src/fiducio/diagnostics.py
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 | |
ReliabilityReport
dataclass
ReliabilityReport(n_images: int, n_valid: int, n_bins: int, saturated_fraction: float, boundary: BoundaryStats, metrics: dict[str, tuple[float, float, float]], classes: list[ClassStats], failures: FailureStats, curve: ReliabilityCurve, image_dice: Tensor, warnings: list[str], soft_targets: bool = False)
Result of :func:diagnose. print(report) shows :meth:summary.
metrics maps ece, nll, brier and gap (mean confidence minus
accuracy) to (value, ci_low, ci_high); the interval is NaN when it could not
be computed. image_dice is the (n_images, n_classes) Dice matrix, NaN where
the class is absent from both labels and prediction, or everywhere for soft
targets (hard-label Dice is undefined there). boundary reports the first and
last confidence bins. soft_targets records which target kind was diagnosed.
summary
Plain-text report for a terminal or a notebook cell.
Source code in src/fiducio/diagnostics.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
to_dict
plot
Reliability, confidence histogram, per-class ECE and per-image Dice (2x2).
Source code in src/fiducio/diagnostics.py
Helpers
mean_human_response
mean_human_response(annotations: ArrayLike, *, num_classes: int, ignore_index: int = DEFAULT_IGNORE_INDEX) -> tuple[torch.Tensor, torch.Tensor]
Return empirical class proportions and an annotated-voxel mask.
annotations has shape (B, R, *spatial) with rater axis 1.
Missing votes equal ignore_index. Unannotated voxels have zero mass;
pass the returned mask to fit and metrics. Each valid voxel receives
equal weight, regardless of how many available raters it has.
Source code in src/fiducio/utils/tensors.py
two_channel_from_binary
Convert single-channel binary outputs to the two-channel form Fiducio uses.
Fiducio represents binary segmentation as two channels (C = 2). Use this
helper to convert a single-channel sigmoid output, whose class axis at
dimension 1 has size 1 (shape (B, 1, *spatial)), into (B, 2, *spatial).
Class 1 is the positive class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scores
|
ArrayLike
|
|
required |
input_type
|
str
|
|
'logits'
|