Skip to content

Fix GPU S2N validation: shape mismatch crash and inverted threshold logic#32

Merged
alexlib merged 2 commits into
masterfrom
copilot/fix-s2n-validation-logic
May 18, 2026
Merged

Fix GPU S2N validation: shape mismatch crash and inverted threshold logic#32
alexlib merged 2 commits into
masterfrom
copilot/fix-s2n-validation-logic

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 18, 2026

gpu_piv(..., validation_method="s2n") crashes with a PyCUDA shape mismatch, and when it doesn't crash (combined validation), it invalidates the wrong vectors — high-S2N instead of low-S2N.

Changes

  • process.py — reshape s2n_ratio to u.shape before passing to validation. self._corr_gpu.s2n_ratio is 1D (flattened); this caused val_locations to be flat, mismatching the 2D velocity field in replace_vectors().

    # Before
    s2n_ratio = self._corr_gpu.s2n_ratio
    # After
    s2n_ratio = self._corr_gpu.s2n_ratio.reshape(u.shape)
  • validation.py — fix inverted threshold in _s2n_validation. The old formula s2n_ratio / s2n_tol > 1 flagged high-S2N vectors as invalid. Replaced with s2n_tol - s2n_ratio > 0, which correctly flags vectors where s2n_ratio < s2n_tol, consistent with the CPU implementation.

    # Before — invalidates HIGH s2n (wrong)
    sig2noise_tol = s2n_ratio / DTYPE_f(s2n_tol)
    self.val_locations = _local_validation(sig2noise_tol, 1, self.val_locations)
    
    # After — invalidates LOW s2n (correct)
    # Mark invalid where s2n_ratio < s2n_tol, i.e. (s2n_tol - s2n_ratio) > 0.
    self.val_locations = _local_validation(
        DTYPE_f(s2n_tol) - s2n_ratio, 0, self.val_locations
    )
  • test_validation.py — update test_validation_s2n_validation to assert the corrected logic.

Copilot AI changed the title [WIP] Fix GPU S2N validation shape mismatch and logic errors Fix GPU S2N validation: shape mismatch crash and inverted threshold logic May 18, 2026
Copilot AI requested a review from alexlib May 18, 2026 21:08
@alexlib alexlib marked this pull request as ready for review May 18, 2026 21:39
Copilot AI review requested due to automatic review settings May 18, 2026 21:39
@alexlib alexlib merged commit 033d680 into master May 18, 2026
0 of 3 checks passed
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes GPU signal-to-noise validation by aligning the S2N array shape with velocity fields and correcting the threshold comparison so low-S2N vectors are marked invalid.

Changes:

  • Reshapes GPU S2N ratios before validation in the PIV pipeline.
  • Reverses S2N validation logic to flag values below the configured threshold.
  • Updates the S2N validation unit test expectation.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
openpiv/gpu/process.py Reshapes flattened correlation S2N output to match velocity field shape before validation.
openpiv/gpu/validation.py Changes S2N validation to invalidate low-S2N vectors instead of high-S2N vectors.
openpiv/test/gpu/test_validation.py Updates the S2N validation test to match the corrected threshold direction.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

tol = log10(validation.S2N_TOL)

val_locations = validation._local_validation(s2n_ratio / tol, 1).get()
val_locations = validation._local_validation(DTYPE_f(tol) - s2n_ratio, 0).get()
Comment thread openpiv/gpu/process.py
return u, v, val_locations
if "s2n" in self.validation_method:
s2n_ratio = self._corr_gpu.s2n_ratio
s2n_ratio = self._corr_gpu.s2n_ratio.reshape(u.shape)
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.

GPU S2N validation has shape mismatch and inverted threshold logic

3 participants