Conversation
Add implementation of Matthews Correlation Coefficient (MCC)-based loss Add tests for MCC loss Add entry for MCC loss in documentation Signed-off-by: Kumar Abhishek <7644965+kakumarabhishek@users.noreply.github.com>
📝 WalkthroughWalkthroughThis PR introduces Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment Tip CodeRabbit can approve the review once all CodeRabbit's comments are resolved.Enable the |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
tests/losses/test_mcc_loss.py (1)
116-150: Add short docstrings to the test class/methods.This keeps new definitions aligned with the repository’s docstring requirement.
As per coding guidelines, "Docstrings should be present for all definition which describe each variable, return value, and raised exception in the appropriate section of the Google-style of docstrings."
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@tests/losses/test_mcc_loss.py` around lines 116 - 150, Add concise Google-style docstrings to the TestMCCLoss test class and each test method (TestMCCLoss, test_shape, test_ill_shape, test_ill_opts, test_input_warnings, test_script) describing what the test verifies, the key inputs, expected outcome, and any exceptions/warnings asserted; place a short summary line followed by brief sections for Args (inputs used, if needed), Raises (expected exceptions or warnings), and Returns (none) where appropriate to satisfy the repository docstring requirement.monai/losses/mcc_loss.py (1)
111-129: Add aReturnssection toforwarddocstring.
Args/Raises/Exampleare present, but returned tensor semantics and shape are undocumented.Proposed docstring patch
def forward(self, input: torch.Tensor, target: torch.Tensor) -> torch.Tensor: """ Args: input: the shape should be BNH[WD], where N is the number of classes. target: the shape should be BNH[WD] or B1H[WD], where N is the number of classes. + + Returns: + torch.Tensor: MCC loss tensor. Shape depends on ``reduction``: + - ``"mean"``/``"sum"``: scalar + - ``"none"``: per-item/per-class loss tensor after spatial (and optional batch) reduction. Raises: AssertionError: When input and target (after one hot transform if set) have different shapes. ValueError: When ``self.reduction`` is not one of ["mean", "sum", "none"].As per coding guidelines, "Docstrings should be present for all definition which describe each variable, return value, and raised exception in the appropriate section of the Google-style of docstrings."
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@monai/losses/mcc_loss.py` around lines 111 - 129, The forward docstring of MCCLoss is missing a Returns section; update the MCCLoss.forward docstring to add a Returns block that describes the returned object (a torch.Tensor), its dtype and device semantics, and the output shape depending on self.reduction (e.g., if reduction=='none' return per-sample/per-voxel loss with shape matching input/target spatial dims, otherwise scalar loss for 'mean' or 'sum'), and mention any special cases (e.g., broadcasting from B1H[WD] target). Ensure the section uses Google-style “Returns:” heading and succinctly references reduction behavior and shape semantics so readers can understand what MCCLoss.forward returns.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@monai/losses/mcc_loss.py`:
- Around line 111-129: The forward docstring of MCCLoss is missing a Returns
section; update the MCCLoss.forward docstring to add a Returns block that
describes the returned object (a torch.Tensor), its dtype and device semantics,
and the output shape depending on self.reduction (e.g., if reduction=='none'
return per-sample/per-voxel loss with shape matching input/target spatial dims,
otherwise scalar loss for 'mean' or 'sum'), and mention any special cases (e.g.,
broadcasting from B1H[WD] target). Ensure the section uses Google-style
“Returns:” heading and succinctly references reduction behavior and shape
semantics so readers can understand what MCCLoss.forward returns.
In `@tests/losses/test_mcc_loss.py`:
- Around line 116-150: Add concise Google-style docstrings to the TestMCCLoss
test class and each test method (TestMCCLoss, test_shape, test_ill_shape,
test_ill_opts, test_input_warnings, test_script) describing what the test
verifies, the key inputs, expected outcome, and any exceptions/warnings
asserted; place a short summary line followed by brief sections for Args (inputs
used, if needed), Raises (expected exceptions or warnings), and Returns (none)
where appropriate to satisfy the repository docstring requirement.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 1e2b4ea9-a8d2-4eb7-8709-c4400ee7c062
📒 Files selected for processing (4)
docs/source/losses.rstmonai/losses/__init__.pymonai/losses/mcc_loss.pytests/losses/test_mcc_loss.py
Add Matthews Correlation Coefficient (MCC) Lossa
Fixes #8784 .
Description
Add the Matthews Correlation Coefficient (MCC) loss function to monai.losses. Unlike Dice and Tversky losses which only use TP, FP, and FN, the MCC loss considers all four entries of the confusion matrix (TP, TN, FP, FN), making it effective for class-imbalanced segmentation tasks. The loss was proposed in Abhishek & Hamarneh (IEEE ISBI 2021), has been cited 75 times, and has been adopted by Segmentation Models PyTorch (smp). The implementation follows MONAI conventions, supporting sigmoid, softmax, to_onehot_y, include_background, batch, and reduction parameters.
Types of changes
./runtests.sh -f -u --net --coverage../runtests.sh --quick --unittests --disttests.make htmlcommand in thedocs/folder.