Skip to content

NumPy rfft2/rfftn return original shape with repeated axes #8855

Open

Description

@jakirkham

Normally NumPy's rfft2/rfftn returns an array whose shape is largely the same as that of the input (as long as s is unspecified) with one exception. Namely the last axis in axes will specify the dimension whose length will be reduced by l // 2 + 1, where l was the original length of that dimension. This is even noted in the docs. However, it seems if the last axis is repeated this no longer holds. Now I could be wrong, but this is not what I would naively expect. If this does make sense for some reason, then the docs probably should be updated.

Details
In [1]: import numpy

In [2]: import numpy.fft

In [3]: a = numpy.random.random((6, 7, 8, 9))

In [4]: numpy.fft.rfft2(a, axes=(0,)).shape
Out[4]: (4, 7, 8, 9)

In [5]: numpy.fft.rfft2(a, axes=(1, 0)).shape
Out[5]: (4, 7, 8, 9)

In [6]: numpy.fft.rfft2(a, axes=(0, 0)).shape
Out[6]: (6, 7, 8, 9)

In [7]: numpy.fft.rfftn(a, axes=(0,)).shape
Out[7]: (4, 7, 8, 9)

In [8]: numpy.fft.rfftn(a, axes=(1, 0)).shape
Out[8]: (4, 7, 8, 9)

In [9]: numpy.fft.rfftn(a, axes=(0, 0)).shape
Out[9]: (6, 7, 8, 9)

Exported environment details from conda:

Details
name: npenv
channels: !!python/tuple
- !!python/unicode
  'conda-forge'
- !!python/unicode
  'defaults'
dependencies:
- conda-forge::appnope=0.1.0=py27_0
- conda-forge::backports.shutil_get_terminal_size=1.0.0=py27_1
- conda-forge::blas=1.1=openblas
- conda-forge::ca-certificates=2017.1.23=0
- conda-forge::certifi=2017.1.23=py27_0
- conda-forge::decorator=4.0.11=py27_0
- conda-forge::enum34=1.1.6=py27_1
- conda-forge::ipython=5.3.0=py27_0
- conda-forge::ipython_genutils=0.2.0=py27_0
- conda-forge::libgfortran=3.0.0=0
- conda-forge::ncurses=5.9=10
- conda-forge::numpy=1.12.1=py27_blas_openblas_200
- conda-forge::openblas=0.2.19=1
- conda-forge::openssl=1.0.2h=3
- conda-forge::pathlib2=2.2.1=py27_0
- conda-forge::pexpect=4.2.1=py27_0
- conda-forge::pickleshare=0.7.3=py27_0
- conda-forge::prompt_toolkit=1.0.13=py27_0
- conda-forge::ptyprocess=0.5.1=py27_0
- conda-forge::pygments=2.2.0=py27_0
- conda-forge::python=2.7.13=0
- conda-forge::readline=6.2=0
- conda-forge::scandir=1.5=py27_1
- conda-forge::setuptools=33.1.1=py27_0
- conda-forge::simplegeneric=0.8.1=py27_0
- conda-forge::six=1.10.0=py27_1
- conda-forge::sqlite=3.13.0=1
- conda-forge::tk=8.5.19=1
- conda-forge::traitlets=4.3.2=py27_0
- conda-forge::wcwidth=0.1.7=py27_0
- conda-forge::zlib=1.2.11=0

ref: https://docs.scipy.org/doc/numpy-1.12.0/reference/generated/numpy.fft.rfftn.html

Activity

  1. jakirkham commented on Mar 27, 2017

    @jakirkham
    ContributorAuthor

    FWIW I don't see the same behavior with irfft2/irfftn, which makes me more inclined to believe this is a bug.

    Details
    In [1]: import numpy
    
    In [2]: import numpy.fft
    
    In [3]: a = numpy.random.random((6, 7, 8, 9))
    
    In [4]: numpy.fft.irfft2(a, axes=(0,)).shape
    Out[4]: (10, 7, 8, 9)
    
    In [5]: numpy.fft.irfft2(a, axes=(1, 0)).shape
    Out[5]: (10, 7, 8, 9)
    
    In [6]: numpy.fft.irfft2(a, axes=(0, 0)).shape
    Out[6]: (10, 7, 8, 9)
    
    In [7]: numpy.fft.irfftn(a, axes=(0,)).shape
    Out[7]: (10, 7, 8, 9)
    
    In [8]: numpy.fft.irfftn(a, axes=(1, 0)).shape
    Out[8]: (10, 7, 8, 9)
    
    In [9]: numpy.fft.irfftn(a, axes=(0, 0)).shape
    Out[9]: (10, 7, 8, 9)
  2. jakirkham commented on Mar 27, 2017

    @jakirkham
    ContributorAuthor

    Tried to update the examples above to make it more obvious what is happening with the repeated axis case by keeping the same axis, 0, last.

  3. eric-wieser commented on Mar 27, 2017

    @eric-wieser
    Member

    Do repeated axes even make any sense?

  4. charris commented on Mar 27, 2017

    @charris
    Member

    Do repeated axes even make any sense?

    That would be my first question also: why are repeated axis allowed?

  5. eric-wieser commented on Mar 27, 2017

    @eric-wieser
    Member

    It looks like the whole function is basically apply_over_axes(some_fft, axes...) - so repeating an axis amounts to doing something like fft(fft(data)) - which IIRC is just some scaled/shifted version of data, as fft is almost its own inverse? Doesn't seem desirable to me.

    Also, ffts are associative, so fft(fft(fft(data, axis=0), axis=1), axis=0))) is approximately fft(fft(fft(data, axis=0), axis=0), axis=1) - which again contains the meaningless expression

  6. jakirkham commented on Mar 27, 2017

    @jakirkham
    ContributorAuthor

    Was wondering the same thing too. Is there any value to repeating an FFT transform to already transformed data?

    FWIW in the fttn case this is documented behavior, which is what rfftn (and thus rfft2) are using.

    Repeated indices in axes means that the transform over that axis is performed multiple times.

  7. eric-wieser commented on Mar 27, 2017

    @eric-wieser
    Member

    From this page

    Applying the DFT twice results in a scaled, time reversed version of the original series.

    So no, this doesn't really seem desirable, and is a weird enough thing to do that I think the user should have to write it explicitly

  8. jakirkham commented on Mar 27, 2017

    @jakirkham
    ContributorAuthor

    Just thinking through this, I guess repeated FFTs would teach you something about the frequency distribution itself, but I agree this doesn't seem like a common use case. Certainly not something I have encountered before.

  9. eric-wieser commented on Mar 27, 2017

    @eric-wieser
    Member

    teach you something about the frequency distribution itself

    yes, but you already had that to begin with from the data you passed in.

    I guess you can use it to -> fft -> pad -> fft -> pad -> through the s argument, which is less trivial than -> fft -> fft -> (which decays to -> reverse_and_scale ->)

  10. jakirkham commented on Mar 27, 2017

    @jakirkham
    ContributorAuthor

    Though if you are padding it before each FFT, that is already more complicated than simply repeating an axis and one would need to actually do it in multiple function applications. So that is another good argument for deprecation of this behavior.

    FWIW I'm not defending the current behavior. Just trying to decide whether it is worth supporting in a downstream library. Thus far it seems that we are thinking it shouldn't even be in NumPy. So I guess I won't bother supporting it.

  11. eric-wieser commented on Mar 27, 2017

    @eric-wieser
    Member

    Though if you are padding it before each FFT, that is already more complicated than simply repeating an axis and one would need to actually do it in multiple function applications

    Nope, I'm pretty sure that the s parameter allows padding (or at least cropping) on each successive axis.

    I think an important argument against repeated axes - the order of s and axes start to matter, and depends on the (undocumented) order in which they are applied.

    I'd be +1 on deprecating this, and maybe even immediately removing this

  12. jakirkham commented on Mar 27, 2017

    @jakirkham
    ContributorAuthor

    I'd be +1 on deprecating this, and maybe even immediately removing this

    Me too.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions