Skip to content

Fix structural_similarity win_size / gaussian_weights conflict - #8162

Merged
blink1073 merged 5 commits into
scikit-image:mainfrom
stefanv:fix/7231-ssim-win-size-gaussian-weights
May 15, 2026
Merged

blink1073 merged 5 commits into
scikit-image:mainfrom
stefanv:fix/7231-ssim-win-size-gaussian-weights

Conversation

@stefanv

@stefanv stefanv commented May 14, 2026

Copy link
Copy Markdown
Member

When gaussian_weights=True, the window size should be calculated
automatically based on sigma. However, in skimage1 we allowed
win_size to override it. Raise a ValueError in v2, and warn in v1
when these are used together.

I also corrected an awkward phrasing in the docs: it previously said
that the mean and variance are "spatially weighted by" a Gaussian
kernel, whereas they are are computed using the Gaussian window.

The sigma parameter description is updated to document its default
value (1.5).

Closes #7231

Assisted-by: GLM:glm-5.1
@stefanv reviewed each line of code.

stefanv added 2 commits May 13, 2026 17:24
When gaussian_weights=True, win_size was only derived from sigma when
win_size was None. If the user passed an explicit win_size, it silently
affected padding and covariance normalization, contradicting the docstring.

Changes:
- _skimage2: Raise ValueError when both gaussian_weights=True and
  win_size are given. The window size is determined by sigma.
- skimage v1: Emit FutureWarning when both are given, and set
  win_size=None so _skimage2 derives it from sigma. This preserves
  existing behavior while alerting users to the upcoming change.
- Fix gaussian_weights docstring: describe the computation correctly
  (means/variances are computed using the Gaussian window, not
  "spatially weighted by" it).
- Fix sigma docstring: document the default value (1.5).
- Add tests for ValueError in skimage2 and FutureWarning in skimage.
- Add TODO.txt entry for completing the deprecation.

Closes: scikit-image#7231
Replace manual warnings.warn with stacklevel=2 by warn_external which
auto-determines the correct stacklevel by skipping internal frames.
@greptile-apps

greptile-apps Bot commented May 14, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "Fix structural_similarity win_size / gau..." | Re-trigger Greptile

@greptile-apps

greptile-apps Bot commented May 14, 2026

Copy link
Copy Markdown
Contributor

Reviews (2): Last reviewed commit: "Use warn_external for FutureWarning in s..." | Re-trigger Greptile

@stefanv stefanv added the 🩹 type: Bug fix Fixes unexpected or incorrect behavior label May 14, 2026
stefanv added 2 commits May 14, 2026 00:53
Remove explicit win_size from test calls that also set
gaussian_weights=True, and add FutureWarning filter to pyproject.toml
so the test suite doesn't fail on the new deprecation warning.
@greptile-apps

greptile-apps Bot commented May 14, 2026

Copy link
Copy Markdown
Contributor

Reviews (3): Last reviewed commit: "Fix test regex and win_size values for g..." | Re-trigger Greptile

r = int(truncate * sigma + 0.5) # radius as in ndimage
win_size = 2 * r + 1
elif win_size is None:
win_size = 7 # backwards compatibility

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this value be called out in the docstring as the default gaussian_weights is not set?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good idea.

@greptile-apps

greptile-apps Bot commented May 14, 2026 •

Copy link
Copy Markdown
Contributor

Comments Outside Diff (1)

  1. src/_skimage2/metrics/_structural_similarity.py, line 186-196 (link)

    P1 Misleading error advice when gaussian_weights=True derives a window too large for the image. The message tells the user to "pass win_size explicitly", but that now raises a ValueError when gaussian_weights=True. A user following this advice would immediately hit a second, contradictory error.

Reviews (4): Last reviewed commit: "Tweak docstring: win_size is 7 by defaul..." | Re-trigger Greptile

@blink1073
blink1073 merged commit e370be4 into scikit-image:main May 15, 2026
27 checks passed
@stefanv stefanv added this to the 0.27 milestone May 15, 2026
matthew-brett added a commit that referenced this pull request May 19, 2026
* origin/main:
  Use standard C99 static inline instead of `__inline` (#8159)
  Fix structural_similarity win_size / gaussian_weights conflict (#8162)
  Unwrap seed refactor (#8142)
  ci: Remove llvm build and add ccache support on windows (#8164)
  Document warn_external in contributing guide (#8163)
  CI: Fix emscripten dependencies and clean up wheel test commands (#8147)
  Test only changed subpackages (#8103)
  Fix nightlies: update deprecated scipy call, fix underdetermined test
  Fix minkowski distanced invocation for SciPy >= 1.18 (#8157)
  Port `structural_similarly` to ski2 (#8104)
  make spin build requirement a lower bound (#8158)
matthew-brett added a commit to matthew-brett/scikit-image that referenced this pull request May 21, 2026
* origin/main: (254 commits)
  Refactor, document migration utilities (scikit-image#8184)
  ci: improve handling of pr labels (scikit-image#8169)
  Doctest migration in PRs & on all platforms (scikit-image#8141)
  Update SKIP 4 (import hierarchy, backporting, testing) (scikit-image#8097)
  One page per function / class in HTML docs for skimage2 (scikit-image#8154)
  Pin minimum pytest to 9 (scikit-image#8174)
  docs: make source install instructions more discoverable and add build requirements to top level requirements file (scikit-image#8165)
  Drop `--maxfail=5` from local pytest config (scikit-image#8170)
  Use standard C99 static inline instead of `__inline` (scikit-image#8159)
  Fix structural_similarity win_size / gaussian_weights conflict (scikit-image#8162)
  Unwrap seed refactor (scikit-image#8142)
  ci: Remove llvm build and add ccache support on windows (scikit-image#8164)
  Document warn_external in contributing guide (scikit-image#8163)
  CI: Fix emscripten dependencies and clean up wheel test commands (scikit-image#8147)
  Test only changed subpackages (scikit-image#8103)
  Fix nightlies: update deprecated scipy call, fix underdetermined test
  Fix minkowski distanced invocation for SciPy >= 1.18 (scikit-image#8157)
  Port `structural_similarly` to ski2 (scikit-image#8104)
  make spin build requirement a lower bound (scikit-image#8158)
  Drop Python 3.11 Support (scikit-image#8153)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🩹 type: Bug fix Fixes unexpected or incorrect behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

metrics.structural_similarity: win_size is not ignored if gaussian_weights is set to True

2 participants