Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC: better document how to handle omitted coefficients in multilevel DWT reconstructions #303

Merged
merged 4 commits into from Dec 3, 2017

Conversation

grlee77
Copy link
Contributor

@grlee77 grlee77 commented Mar 16, 2017

Related to the discussion in #302, this PR is intended to clarify issues of running waverecn (as well as waverec and waverec2) with some coefficients omitted. The only truly safe way to do this currently seems to be to replace the undesired coefficient arrays with corresponding arrays of zeros. In general, None cannot be allowed (as it is for the single-level idwtn), as the shapes of the arrays are needed at various places to ensure any extra padding was removed properly:

pywt/pywt/_multilevel.py

Lines 148 to 149 in dd7b793

if a.shape[axis] == d.shape[axis] + 1:
a = a[[slice(s) for s in d.shape]]

pywt/pywt/_multilevel.py

Lines 296 to 297 in dd7b793

idxs = tuple(slice(None, -1 if a_len == d_len + 1 else None)
for a_len, d_len in zip(a.shape, d_shape))

a = _match_coeff_dims(a, d)

Here the error message on None coefficients is updated to suggest the appropriate workaround and some text is added to the Notes section of the function docstrings.

…fficients

are None.

Coefficients as None is allowed by idwtn, but for the multilevel waverecn, the
shape of the detail coefficients arrays are used to make sure the final shape
is correct.  This information is lost if the detail coefficients are None.

The error message in _fix_coeffs is updated to suggest to the user, the
appropriate workaround of using an array of zeros instead.
omitting specific coefficients from the reconstruction
@grlee77 grlee77 added this to the v1.0 milestone Mar 16, 2017
@codecov-io
Copy link

codecov-io commented Mar 16, 2017

Codecov Report

Merging #303 into master will decrease coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #303      +/-   ##
==========================================
- Coverage   86.22%   86.22%   -0.01%     
==========================================
  Files          21       21              
  Lines        3092     3099       +7     
  Branches      535      562      +27     
==========================================
+ Hits         2666     2672       +6     
- Misses        373      374       +1     
  Partials       53       53
Impacted Files Coverage Δ
pywt/_multilevel.py 92.6% <ø> (ø) ⬆️
pywt/_multidim.py 97.77% <100%> (ø) ⬆️
pywt/_extensions/_dwt.pyx 74.41% <0%> (-0.47%) ⬇️
pywt/_extensions/_pywt.pyx 70.34% <0%> (-0.23%) ⬇️
pywt/_extensions/c/wavelets.c 85.74% <0%> (-0.04%) ⬇️
pywt/_thresholding.py 100% <0%> (ø) ⬆️
pywt/_extensions/c/wt.template.c 87.38% <0%> (+0.45%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update dd7b793...e623c44. Read the comment docs.

@grlee77
Copy link
Contributor Author

grlee77 commented Nov 28, 2017

@PyWavelets/core can someone take a quick look at this PR as well as #309? They should both be quick to review.

Copy link
Member

@rgommers rgommers left a comment

Choose a reason for hiding this comment

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

I've commented on a few minor doc things; my main comment is about the example. I tried to follow the wavedecn one given:

coeffs = pywt.wavedecn(np.ones((4, 4, 4)), 'db1')  # from Examples section
coeffs[-2]= {k: numpy.zeros_like(v) for k, v in coeffs[-2].items()}

the output doesn't look quite right

@@ -122,6 +122,17 @@ def waverec(coeffs, wavelet, mode='symmetric', axis=-1):
Axis over which to compute the inverse DWT. If not given, the
last axis is used.

Notes
-----
It may sometimes desired to run ``waverec`` with some sets of
Copy link
Member

Choose a reason for hiding this comment

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

"sometimes be desired"

@@ -251,6 +262,17 @@ def waverec2(coeffs, wavelet, mode='symmetric', axes=(-2, -1)):
-------
2D array of reconstructed data.

Notes
-----
It may sometimes desired to run ``waverec2`` with some sets of
Copy link
Member

Choose a reason for hiding this comment

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

"sometimes be desired"

Copy link
Member

Choose a reason for hiding this comment

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

and same for waverecn


Specifically, to ignore all detail coefficients at level 2, one could do::

codffs[-2] = {k: numpy.zeros_like(v) for k, v in coeffs[-2].items()}
Copy link
Member

Choose a reason for hiding this comment

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

typo: codffs --> coeffs


Specifically, to ignore detail coefficients at level 2, one could do::

coeffs[-2] == numpy.zeros_like(coeffs[-2])
Copy link
Member

Choose a reason for hiding this comment

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

numpy --> np (in all 3 docstrings)

@grlee77
Copy link
Contributor Author

grlee77 commented Nov 29, 2017

the output doesn't look quite right

Well, for that example with np.ones as the input, all detail coefficients are already zero, so setting to zero has no effect. You should still get an array of all ones after waverecn.

The output of the example below looks as I would expect for discarding some detail coefficients:

import pywt
import numpy as np
import matplotlib.pyplot as plt
img = pywt.data.camera()
coeffs = pywt.wavedecn(img, 'db1')
coeffs[-2]= {k: np.zeros_like(v) for k, v in coeffs[-2].items()}
imgr = pywt.waverecn(coeffs, 'db1')

fig, axes = plt.subplots(1, 2)
axes[0].imshow(img)
axes[1].imshow(imgr)

figure_5

@rgommers
Copy link
Member

rgommers commented Dec 3, 2017

Thanks, that example looks more useful (and correct). Guess we should get around to fixing up the trivial examples with some like the one you provide here at some point.

@rgommers rgommers merged commit b56e8ba into PyWavelets:master Dec 3, 2017
@grlee77 grlee77 deleted the waverecn_none branch July 20, 2018 21:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants