Skip to content

Commit bf3ac32

Browse files
committed
style: apply black formatting
Signed-off-by: Rusheel Sharma <rusheelhere@gmail.com>
1 parent db5cec0 commit bf3ac32

7 files changed

Lines changed: 27 additions & 26 deletions

File tree

monai/losses/focal_loss.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,7 @@ def forward(self, input: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
246246
pass
247247

248248
else:
249-
raise ValueError(
250-
f"Unsupported reduction: {self.reduction}, available options are [\"mean\", \"sum\", \"none\"]."
251-
)
249+
raise ValueError(f'Unsupported reduction: {self.reduction}, available options are ["mean", "sum", "none"].')
252250

253251
return loss
254252

monai/losses/unified_focal_loss.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ def forward(self, y_pred: torch.Tensor, y_true: torch.Tensor) -> torch.Tensor:
6969
if n_pred_ch == 1:
7070
warnings.warn("single channel prediction, `to_onehot_y=True` ignored.")
7171
else:
72-
if self.ignore_index is not None and (
73-
self.ignore_index < 0 or self.ignore_index >= n_pred_ch
74-
):
72+
if self.ignore_index is not None and (self.ignore_index < 0 or self.ignore_index >= n_pred_ch):
7573
# Replace sentinel ignore_index with a valid class before one_hot
7674
y_true = torch.where(y_true == self.ignore_index, torch.tensor(0, device=y_true.device), y_true)
7775
y_true = one_hot(y_true, num_classes=n_pred_ch)
@@ -148,9 +146,7 @@ def forward(self, y_pred: torch.Tensor, y_true: torch.Tensor) -> torch.Tensor:
148146
if n_pred_ch == 1:
149147
warnings.warn("single channel prediction, `to_onehot_y=True` ignored.")
150148
else:
151-
if self.ignore_index is not None and (
152-
self.ignore_index < 0 or self.ignore_index >= n_pred_ch
153-
):
149+
if self.ignore_index is not None and (self.ignore_index < 0 or self.ignore_index >= n_pred_ch):
154150
# Replace sentinel ignore_index with a valid class before one_hot
155151
y_true = torch.where(y_true == self.ignore_index, torch.tensor(0, device=y_true.device), y_true)
156152
y_true = one_hot(y_true, num_classes=n_pred_ch)
@@ -278,14 +274,10 @@ def forward(self, y_pred: torch.Tensor, y_true: torch.Tensor) -> torch.Tensor:
278274
if not (self.to_onehot_y and orig_pred_ch != 1):
279275
if not (orig_pred_ch == 1 and y_true.shape[1] == 1):
280276
if y_true.shape != y_pred.shape:
281-
raise ValueError(
282-
f"ground truth has different shape ({y_true.shape}) from input ({y_pred.shape})"
283-
)
277+
raise ValueError(f"ground truth has different shape ({y_true.shape}) from input ({y_pred.shape})")
284278

285279
if self.to_onehot_y and orig_pred_ch != 1:
286-
if self.ignore_index is not None and (
287-
self.ignore_index < 0 or self.ignore_index >= self.num_classes
288-
):
280+
if self.ignore_index is not None and (self.ignore_index < 0 or self.ignore_index >= self.num_classes):
289281
# Replace sentinel ignore_index with a valid class before one_hot
290282
y_true = torch.where(y_true == self.ignore_index, torch.tensor(0, device=y_true.device), y_true)
291283
y_true = one_hot(y_true, num_classes=self.num_classes)
@@ -296,9 +288,7 @@ def forward(self, y_pred: torch.Tensor, y_true: torch.Tensor) -> torch.Tensor:
296288

297289
mask = create_ignore_mask(original_y_true, self.ignore_index)
298290

299-
use_mask = self.ignore_index is not None and (
300-
self.ignore_index < 0 or self.ignore_index >= self.num_classes
301-
)
291+
use_mask = self.ignore_index is not None and (self.ignore_index < 0 or self.ignore_index >= self.num_classes)
302292
if use_mask:
303293
y_pred_masked, y_true_masked = mask_loss_inputs(y_pred, y_true, self.ignore_index, mask=mask)
304294
else:

monai/losses/utils.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ def compute_tp_fp_fn(
7171

7272

7373
def mask_loss_inputs(
74-
input: torch.Tensor,
75-
target: torch.Tensor,
76-
ignore_index: int | None,
77-
mask: torch.Tensor | None = None,
74+
input: torch.Tensor, target: torch.Tensor, ignore_index: int | None, mask: torch.Tensor | None = None
7875
) -> tuple[torch.Tensor, torch.Tensor]:
7976
"""Apply ignore_index masking to loss inputs."""
8077
if mask is None and ignore_index is not None:

monai/metrics/confusion_matrix.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,12 @@ class range, ignored regions are inferred from spatial locations where all label
184184
mask = None
185185
else:
186186
mask = create_ignore_mask(original_y if ignore_index is not None else y, ignore_index)
187-
if mask is not None and not include_background and mask.shape[1] != y_pred.shape[1] and mask.shape[1] > y_pred.shape[1]:
187+
if (
188+
mask is not None
189+
and not include_background
190+
and mask.shape[1] != y_pred.shape[1]
191+
and mask.shape[1] > y_pred.shape[1]
192+
):
188193
mask = mask[:, 1:]
189194

190195
# convert to [BNS], where S is the number of pixels for one sample.

monai/metrics/meaniou.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,12 @@ def compute_iou(
162162
mask = None
163163
else:
164164
mask = create_ignore_mask(original_y if ignore_index is not None else y, ignore_index)
165-
if mask is not None and not include_background and mask.shape[1] != y_pred.shape[1] and mask.shape[1] > y_pred.shape[1]:
165+
if (
166+
mask is not None
167+
and not include_background
168+
and mask.shape[1] != y_pred.shape[1]
169+
and mask.shape[1] > y_pred.shape[1]
170+
):
166171
mask = mask[:, 1:]
167172
if mask is not None:
168173
if mask.shape != y_pred.shape:

tests/losses/test_ignore_index_losses.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ def test_no_ignore_behavior(self, loss_class, kwargs):
7676
output = loss_func(input_data, target)
7777
self.assertFalse(torch.isnan(output))
7878

79-
@parameterized.expand([(loss_class, kwargs, ignore_index) for loss_class, kwargs in CLASS_INDEX_TEST_CASES for ignore_index in (0, 1)])
79+
@parameterized.expand(
80+
[(loss_class, kwargs, ignore_index) for loss_class, kwargs in CLASS_INDEX_TEST_CASES for ignore_index in (0, 1)]
81+
)
8082
def test_loss_ignore_class_index(self, loss_class, kwargs, ignore_index):
8183
loss_func = loss_class(ignore_index=ignore_index, **kwargs)
8284

tests/metrics/test_ignore_index_metrics.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ def test_metric_ignore_consistency(self, metric_class, kwargs):
9292
torch.testing.assert_close(res1, res2, msg=f"Failed for {metric_class.__name__}")
9393

9494
@parameterized.expand(
95-
[(metric_class, kwargs, ignore_index) for metric_class, kwargs in TEST_METRICS + SCIPY_METRICS for ignore_index in (0, 1)]
95+
[
96+
(metric_class, kwargs, ignore_index)
97+
for metric_class, kwargs in TEST_METRICS + SCIPY_METRICS
98+
for ignore_index in (0, 1)
99+
]
96100
)
97101
def test_metric_ignore_class_index(self, metric_class, kwargs, ignore_index):
98102
metric = metric_class(ignore_index=ignore_index, **kwargs)

0 commit comments

Comments
 (0)