BUG: fix pywt.pad for a zero pad width in 'smooth' and 'antisymmetric' (gh-589) - #854
Open
Hrafz wants to merge 1 commit into
Open
BUG: fix pywt.pad for a zero pad width in 'smooth' and 'antisymmetric' (gh-589)#854Hrafz wants to merge 1 commit into
Hrafz wants to merge 1 commit into
Conversation
PyWaveletsgh-589) The two modes that numpy.pad cannot express directly are implemented with callbacks that index the vector from the end using the trailing pad width. When that width is 0, `vector[-pad_width[1]:]` selects the whole vector instead of an empty slice, and `vector[pad_width[0]:-pad_width[-1]]` selects an empty one, so pad() raised a ValueError instead of returning the array unchanged. Every other extension mode already treats a zero pad width as a no-op. Index from the start of the vector instead, and correct the message of the negative-pad-width check, which reported that 0 is invalid.
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.
Fixes #589.
A pad width of 0 should leave the array unchanged, but raises in the
'smooth'and
'antisymmetric'extension modes:Six of the nine extension modes return the array untouched for a zero pad
width.
'periodization'is the other exception, and a benign one:pywt.pad(x, 0, 'periodization')gives[1., 2., 3., 3.], since it promotesan odd-length axis to even length.
Both failing modes are implemented as
numpy.padcallbacks, and both index thevector from the end using the trailing pad width:
vector[-pad_width[1]:]inpad_smoothandvector[pad_width[0]:-pad_width[-1]]inpad_antisymmetric.With a trailing width of 0 the first selects the whole vector instead of an
empty slice and the second selects an empty one, hence the two messages above.
The leading width is unaffected because
vector[:0]is already empty.The fix measures both indices from the start of the vector.
pad_smoothalsoread its right-hand slope with a second negative index (
-pad_width[1] - 2),now expressed relative to the same start-based index. Results are unchanged for
everything that worked before: comparing old against new over 1-D inputs of
length 1 to 5 and both pad widths from 0 to 4, in both modes, the only cases
that differ are ones that raised: a trailing width of 0, plus a length-1 axis
with
(0, k)in'smooth', which raisedIndexError.The message of the pad-width check is also corrected; it read
pad_widths must be > 0while the check is< 0and 0 is legal. Happy to dropthat line if you would rather keep the diff to the two callbacks.
Two regression tests are added to
pywt/tests/test_dwt_idwt.py:test_pad_zero_widthasserts the no-op property in 1, 2 and 3 dimensions forevery mode except
'periodization', andtest_pad_one_sidedchecks fixedexpected values plus the property that one-sided padding equals the
corresponding slice of two-sided padding, again for every mode except
'periodization'. Both tests fail on main and pass with this change.