Skip to content

Commit c212f47

Browse files
committed
Fix three malformed error messages
Adjacent string literals concatenate in Python, and in two places the join produces text the author clearly did not intend: monai/networks/utils.py:443 pixelunshuffle() "...divisible by factor 2. , spatial shape is: [7, 8]" The second literal opens with ", " while the first already closed with ". ", so the rendered message carries a stray ". ,". monai/inferers/inferer.py:1776 LatentDiffusionInferer.__init__() "...autoencoder_latent_shape must be Noneand vice versa." No trailing space on the first literal, so two words run together. The third is a plain typo in the same family, a missing comma in a list of valid options, appearing in both the raised message and the docstring that documents it: monai/metrics/utils.py:104,144 do_metric_reduction() '[..., "mean_channel", "sum_channel" "none"].' Every sibling message in monai/losses/ writes this list fully comma-separated. All three are user-visible text only; no behaviour changes. Adds a regression test for the pixelunshuffle() path, which had no coverage of its ValueError at all. The test fails against the unmodified source with: AssertionError: "divisible by factor 2, spatial shape is: \[7, 8\]" does not match "All spatial dimensions must be divisible by factor 2. , spatial shape is: [7, 8]" Verified locally: tests/networks/utils (57 tests), tests/metrics (477), tests.inferers.test_latent_diffusion_inferer (45) and tests.inferers.test_controlnet_inferers (58) all pass, as do runtests.sh --codeformat and pre-commit run --all-files. tests/metrics has one pre-existing failure, test_compute_fid_metric, from a scipy sqrtm(disp=...) signature change; it fails identically on unmodified dev and is unrelated to this change. Signed-off-by: Hans Johnson <hans-johnson@uiowa.edu>
1 parent c1240a2 commit c212f47

4 files changed

Lines changed: 9 additions & 4 deletions

File tree

monai/inferers/inferer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1773,7 +1773,7 @@ def __init__(
17731773
super().__init__(scheduler=scheduler)
17741774
self.scale_factor = scale_factor
17751775
if (ldm_latent_shape is None) ^ (autoencoder_latent_shape is None):
1776-
raise ValueError("If ldm_latent_shape is None, autoencoder_latent_shape must be None" "and vice versa.")
1776+
raise ValueError("If ldm_latent_shape is None, autoencoder_latent_shape must be None and vice versa.")
17771777
self.ldm_latent_shape = ldm_latent_shape
17781778
self.autoencoder_latent_shape = autoencoder_latent_shape
17791779
if self.ldm_latent_shape is not None and self.autoencoder_latent_shape is not None:

monai/metrics/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def do_metric_reduction(
101101
102102
Raises:
103103
ValueError: When ``reduction`` is not one of
104-
["mean", "sum", "mean_batch", "sum_batch", "mean_channel", "sum_channel" "none"].
104+
["mean", "sum", "mean_batch", "sum_batch", "mean_channel", "sum_channel", "none"].
105105
"""
106106

107107
# some elements might be Nan (if ground truth y was missing (zeros))
@@ -141,7 +141,7 @@ def do_metric_reduction(
141141
elif reduction != MetricReduction.NONE:
142142
raise ValueError(
143143
f"Unsupported reduction: {reduction}, available options are "
144-
'["mean", "sum", "mean_batch", "sum_batch", "mean_channel", "sum_channel" "none"].'
144+
'["mean", "sum", "mean_batch", "sum_batch", "mean_channel", "sum_channel", "none"].'
145145
)
146146
return f, not_nans
147147

monai/networks/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ def pixelunshuffle(x: torch.Tensor, spatial_dims: int, scale_factor: int) -> tor
440440

441441
if any(d % factor != 0 for d in input_size[2:]):
442442
raise ValueError(
443-
f"All spatial dimensions must be divisible by factor {factor}. " f", spatial shape is: {input_size[2:]}"
443+
f"All spatial dimensions must be divisible by factor {factor}, spatial shape is: {input_size[2:]}"
444444
)
445445
output_size = [batch_size, new_channels] + [d // factor for d in input_size[2:]]
446446
reshaped_size = [batch_size, channels] + sum([[d // factor, factor] for d in input_size[2:]], [])

tests/networks/utils/test_pixelunshuffle.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ def test_different_scale_factor(self):
4040
out = pixelunshuffle(x, spatial_dims=2, scale_factor=3)
4141
torch.testing.assert_close(out, torch.pixel_unshuffle(x, 3))
4242

43+
def test_indivisible_spatial_dims(self):
44+
x = torch.randn(1, 2, 7, 8)
45+
with self.assertRaisesRegex(ValueError, r"divisible by factor 2, spatial shape is: \[7, 8\]"):
46+
pixelunshuffle(x, spatial_dims=2, scale_factor=2)
47+
4348
def test_inverse_operation(self):
4449
x = torch.arange(4096).reshape(1, 8, 8, 8, 8)
4550
shuffled = pixelshuffle(x, spatial_dims=3, scale_factor=2)

0 commit comments

Comments
 (0)