Skip to content

Commit ea41ff6

Browse files
Final optimizations from CodeRabbit review
- Optimize radial calculation using sum() instead of stack().sum() - Remove redundant empty check in RadialFourierFeatures3D - All suggestions implemented, ready for merge - Formatting fixes from pre-commit hooks
1 parent aea3856 commit ea41ff6

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

monai/transforms/signal/radial_fourier.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def _compute_radial_coordinates(self, shape: tuple[int, ...], device: torch.devi
104104

105105
# Create meshgrid and compute radial distance
106106
mesh = torch.meshgrid(coords, indexing="ij")
107-
radial = torch.sqrt(torch.stack([c**2 for c in mesh]).sum(dim=0))
107+
radial = torch.sqrt(sum(c**2 for c in mesh))
108108

109109
return radial
110110

@@ -340,18 +340,14 @@ def __call__(self, img: NdarrayOrTensor) -> NdarrayOrTensor:
340340
feat = transform(img)
341341
features.append(feat)
342342

343-
# Concatenate along last dimension
344-
if features:
345-
# Convert all features to tensors if any are numpy arrays
346-
features_tensors = []
347-
for feat in features:
348-
if isinstance(feat, np.ndarray):
349-
features_tensors.append(torch.from_numpy(feat))
350-
else:
351-
features_tensors.append(feat)
352-
output = torch.cat(features_tensors, dim=-1)
353-
else:
354-
raise ValueError("No features extracted. This should not happen with validated parameters.")
343+
# Convert all features to tensors if any are numpy arrays
344+
features_tensors = []
345+
for feat in features:
346+
if isinstance(feat, np.ndarray):
347+
features_tensors.append(torch.from_numpy(feat))
348+
else:
349+
features_tensors.append(feat)
350+
output = torch.cat(features_tensors, dim=-1)
355351

356352
# Convert to original type if needed
357353
if isinstance(img, np.ndarray):

0 commit comments

Comments
 (0)