fix(signal_detrend): correct D_2 matrix in tarvainen2002 method - #1198
Open
LensHRV wants to merge 3 commits into
Open
fix(signal_detrend): correct D_2 matrix in tarvainen2002 method#1198LensHRV wants to merge 3 commits into
LensHRV wants to merge 3 commits into
Conversation
Fixes boundary truncation in D_2 matrix using scipy.sparse.diags and improves numerical stability and performance using np.linalg.solve.
Contributor
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1198 +/- ##
==========================================
- Coverage 57.78% 57.77% -0.01%
==========================================
Files 310 310
Lines 15680 15678 -2
==========================================
- Hits 9060 9058 -2
Misses 6620 6620 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes an issue in$D_2$ was being truncated at the boundaries due to the usage of $(N-2) \times N$ . This caused the last entries of the detrended residual to artificially diverge or evaluate to zero.
_signal_detrend_tarvainen2002where the second-difference matrixscipy.sparse.dia_matrixwith rectangular shapesSummary of Improvements:
scipy.sparse.dia_matrixwithscipy.sparse.diags([1, -2, 1], [0, 1, 2], shape=(N - 2, N)). This ensures the[1, -2, 1]coefficients across all rows without truncating edge elements.np.linalg.invwithnp.linalg.solve(A, signal). Following the official NumPy and SciPy documentation recommendations, using a direct linear solver avoids the computational overhead and numerical instability of explicit matrix inversion. It also eliminates unnecessary intermediate 2D array allocations (B,.T), improving execution speed and memory footprint.Validation:
atol < 1e-9).References:
linalg.invDocumentation: "Calculating the inverse of a matrix is computationally expensive and numerically unstable. To solve a linear system $Ax = b$, usenumpy.linalg.solve(A, b)instead."