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

Include wavedec_coeff_len from nigma/pywt #85

Closed
aaren opened this issue Jul 30, 2015 · 7 comments
Closed

Include wavedec_coeff_len from nigma/pywt #85

aaren opened this issue Jul 30, 2015 · 7 comments

Comments

@aaren
Copy link
Member

aaren commented Jul 30, 2015

This is an open PR on nigma's repo: nigma/pywt#4

Leaving this here to remove it from #55.

@aaren aaren mentioned this issue Jul 30, 2015
17 tasks
@rgommers
Copy link
Member

The code from the PR, just in case that PRs on that repo won't be accessible in the future (we may close them completely if people keep sending PRs there instead of here):

def wavedec_coeff_len(data_len, filter_len, mode, level=None):
    """
    Total number of coefficients in the output of `wavedec`, given data
    length, filter length, mode and level.
    Returns integer: len(cAn) + len(cDn) + len(cDn-1) + ... + len(cD1)
    data_len   - data length
    filter_len - filter length or Wavelet instance
    level      - decomposition level. If level is None then it will be
                 calculated using `dwt_max_level` function.
    """

    if level is None:
        level = dwt_max_level(data_len, filter_len)
    elif level < 0:
        raise ValueError(
            "Level value of %d is too low . Minimum level is 0." % level)
    total_len = 0

    for i in xrange(level):
        data_len = dwt_coeff_len(data_len, filter_len, mode)
        total_len += data_len 

    return total_len + data_len

@rgommers
Copy link
Member

PR author is @belevtsoff

@belevtsoff
Copy link

Wow, thanks for resurrecting this, it's been 3 years :)

@rgommers
Copy link
Member

time flies:)

@rgommers
Copy link
Member

The motivation given on the original PR:

We used PyWavelets recently in our SpikeSort project. In particular, hstacked ouput from pywt.wavedec was used as a single feature vector. This approach is often used for spike sorting. The function I'm proposing, will be very handy when one wants to allocate a fixed-size numpy array to store wavelet coefficients (obtained from wavedec), before the input data is processed (for example, if one has a huge stack of equal-length independent signals to process and store).

@PyWavelets/core opinions on adding this function?

@grlee77
Copy link
Contributor

grlee77 commented Dec 29, 2016

I think the implementation is correct and it would not be a burden to maintain. However, I'm not sure if there is still much of a use case for it (see below).

For batched 1D transforms, it should now be faster to run them all at once using the axis feature of wavedec (which was not present at the time the original PR was opened).

If wavedecn was used to do the 1D transforms, one could also then use coeffs_to_array to convert to a single array, which I think is the use case stated above?

e.g. to transform 1000 signals, each of length 256 and get the results in a single array:

coeffs = pywt.wavedecn(np.random.randn(256, 1000), 'db2', 'periodization', axes=(0, ))
coeff_array, slices = pywt.coeffs_to_array(coeffs, axes=(0, ))

@rgommers
Copy link
Member

Thanks @grlee77, good points. And if one really wanted to do preallocation for some reason, it wouldn't be much of an issue to run a single transform once and obtain the length from the output of that.

So closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants