Skip to content

Fix im3DNORM returning None for supported norm orders and improve input validation (fixes #797) - #805

Merged
AnderBiguri merged 1 commit into
CERN:masterfrom
bhumikabiyani:fix/im3dnorm-norm-orders
Nov 10, 2026
Merged

AnderBiguri merged 1 commit into
CERN:masterfrom
bhumikabiyani:fix/im3dnorm-norm-orders

Conversation

@bhumikabiyani

Copy link
Copy Markdown
Contributor

Description

Fixes #797.

This PR fixes im3DNORM silently returning None for supported norm orders (np.inf, -np.inf, floats, and NumPy integer types) and improves input validation across the function.

Root Cause

  1. Identity comparison bug: Line 66 used if normind is [np.inf, -np.inf, "fro", "nuc"]:, testing identity against a new list literal, which always evaluated to False.
  2. Strict type check: if type(normind) is int: excluded NumPy integers (np.int64, np.int32) and floating-point orders (float, np.floating).
  3. Silent fall-through: Unmatched orders fell off the end of im3DNORM without a return statement, returning None.
  4. Semantics of fro and nuc: im3DNORM flattens the input array into a 1D vector (img.ravel()). In NumPy, Frobenius and nuclear norms are defined only for 2D matrices, so vectors raise ValueError. Passing fro or nuc now explicitly raises ValueError consistent with NumPy norm semantics.

Changes Made

  • Input validation: Explicitly validate normind and reject None, booleans, NaNs, and non-scalar structures with ValueError.
  • L2 norm optimization: Preserved existing l2norm(img) optimized float32 accumulator for all order-2 representations (2, np.int64(2), 2.0, np.float64(2.0)).
  • TV norm: Preserved and aligned TV norm with gradient options (forward, backward, central), supporting 3D volumes with zero-padding matching MATLAB's implementation.
  • General vector norms: Correctly compute vector p-norms for positive/negative infinity (np.inf, -np.inf), integer orders, floating-point orders, and NumPy numeric types.
  • Unsupported inputs: Explicitly raise ValueError for unsupported norm orders (fro, nuc, unknown, L2, etc.).
  • Docstring: Clarified documentation regarding supported scalar orders and notes on unsupported matrix norms.

Testing

Added comprehensive unit and regression tests in Python/tests/test_im3dnorm.py covering:

  • np.inf and -np.inf (and float("inf"), float("-inf"))
  • Integer and floating-point norm orders (1, 3, 4, -1, -2, 1.5, 0.5, 2.5, 0)
  • np.int64 and np.int32 norm orders
  • L2 norm consistency, dtype preservation (float32 vs float64), and equivalence across types
  • Frobenius and nuclear norm orders raising ValueError
  • Unsupported norm values (None, True, False, unknown, L2, lists, dicts) raising ValueError
  • TV norm functionality across forward, backward, and central gradient options

@AnderBiguri
AnderBiguri merged commit c9b2945 into CERN:master Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

im3DNORM returns None for np.inf, 'fro' and non-int orders

2 participants