Skip to content

Add Multidimensional FFTs - #2116

Merged
mrocklin merged 9 commits into
dask:masterfrom
jakirkham:add_fftn
Mar 28, 2017
Merged

mrocklin merged 9 commits into
dask:masterfrom
jakirkham:add_fftn

Conversation

@jakirkham

Copy link
Copy Markdown
Member

Fixes #1980

Note: This appears to be functioning correctly. That said, this can probably benefit from more refactoring, cleanup, and maybe more tests. At this point, I think it just needs more eyes on it to figure out how it should progress.

This extends the fft_wrap function and chunk generating functions to support 2-D and N-D FFTs in addition to the existing 1-D support. Have also wrapped relevant NumPy FFT functions for these cases. Added a bunch of tests for the wrapped functions and also tested wrapping equivalent functions from SciPy.

@jakirkham jakirkham mentioned this pull request Mar 22, 2017

@mrocklin mrocklin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just general comments at this stage. Thanks for taking this on.

Comment thread dask/array/fft.py
'irfft2': _irfft_out_chunks,
'irfftn': _irfft_out_chunks,
'hfft': _hfft_out_chunks,
'ihfft': _ihfft_out_chunks}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we consistently just strip off '2n' here?

chunk_fn = _out_chunk_fns[name.strip('2n')]

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's a little less clear, but sure we could. Do you think these will actually change that much though?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No, I don't think that they will change. My comment is trying to expose the structure that the choice of chunk function depends only on the prefix to fft, not the suffix.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done.

assert_eq(irfftn(darr3, axes=(1, 0)), np.fft.irfftn(nparr, axes=(1, 0)))
assert_eq(irfftn(darr3, (8, 9), axes=(1, 0)),
np.fft.irfftn(nparr, (8, 9), axes=(1, 0)))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Any thoughts on pulling in all of these tests to a large parametrized test that was more comprehensive?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Focusing our energy on the test at the bottom might yield more confidence with less work.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We can consolidate them a bit, but I think there is some value in testing the wrapped copies we expose in the API independently from testing the process of rolling our own.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Have consolidated these a bit.

Comment thread dask/array/fft.py Outdated
return a.map_blocks(fft_func, n=n, axis=axis, dtype=dtype,
chunks=chunks)
return a.map_blocks(fft_func, n=n, axis=axis, dtype=dtype,
chunks=chunks)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it feasible to make the single-dimensional case a special case of the multi-dimensional case? This might make this easier to maintain in the future.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, I was thinking about that as well. Ran out of time to pursue it yesterday. So tabled it. Might try again today. The main thing is the arguments have different names. So we have to handle this somehow (maybe a second wrapper). I don't know if there are any other consequences of having a second wrapper like that.

Other than that we will have to move some of the branches into the function. Not sure whether it will be easier or harder to reason about what it is doing. At a bare minimum, branches for the 1-D and N-D cases next to each other should make it clear what the difference is, but there will be a few more of them. Some cases like guessing dtype and raising chunk errors can just use the N-D case for 1-D one.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Have combined the two wrapping functions together. However, as noted, still need a wrapper around that to map 1-D arguments to something that can be handled by the N-D wrapper.

@jakirkham
jakirkham force-pushed the add_fftn branch 4 times, most recently from 01ae721 to 49d3e18 Compare March 23, 2017 16:28
Comment thread dask/array/tests/test_fft.py Outdated
assert_eq(da_fft(darr3, axes=(1, 0)),
np_fft(nparr, axes=(1, 0)))
assert_eq(da_fft(darr3, (8, 9), axes=(1, 0)),
np_fft(nparr, (8, 9), axes=(1, 0)))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Are the bodies of test_irfft2n, test_rfft2n, test_ifft2n, and test_fft2n all identical? if so then perhaps we can make them one function and merge their parameters?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'd really like to start testing with complex data in conjunction with [i]rfft* for instance and verifying that they are giving us a correctly typed result. That will ultimately require some separation of these. Though I suppose we could do that as new tests. What do you think?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you provide an example of a test that couldn't be done in this way? Almost all dask.array tests are of the form:

  1. Do something with numpy
  2. Do the equivalent thing with dask.array
  3. Call assert_eq on the results (which should check many things)

This usually works, but you're right that sometimes we need to break this pattern. I think that the question you're posing is if this is one of those cases. I don't yet understand what would break about the pattern above for ifft functions.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This will probably amount to just writing it, but yeah I can give it some thought. In the interim, I think you are right that we should consolidate these. We can always add new tests for other cases that don't fit the existing motif.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

While we are at it, we can probably do the same thing with 1D tests.

xref: #2118

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Going to just go ahead and rebase this on top of PR ( #2118 ).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Have combined all the mentioned 2D and ND tests into one test.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The only really interesting things this function is doing at this point are allowing all default arguments and specifying a size smaller than the data. The axes case is covered in the test below so will drop that line.

@jakirkham
jakirkham force-pushed the add_fftn branch 5 times, most recently from 9d1f785 to e922e4b Compare March 24, 2017 20:17
@jakirkham

Copy link
Copy Markdown
Member Author

Alright have made a few changes. Would appreciate another review.

nparr = np.arange(100).reshape(10, 10)
darr = da.from_array(nparr, chunks=(1, 10))
darr2 = da.from_array(nparr, chunks=(10, 1))
darr3 = da.from_array(nparr, chunks=(10, 10))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For fftn computations should we be using larger dimensional arrays?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We could, but we can also just reuse this. Depends how many axes we want to try and such. Though we can also repeat axes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

FWIW in NumPy's implementation 2-D transforms either proceed through the same code path as N-D transforms or they actually call the N-D transform. So they really are just a special case of the N-D transform.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Understood. I'm curious if we should test a case where there is at least one dimension on which we're not performing an FFT.

This gave me some confidence that everything was running correctly:

@pytest.mark.parametrize('modname', ['numpy.fft', 'scipy.fftpack'])
@pytest.mark.parametrize('funcname', ['fftn', 'ifftn', 'irfftn', 'rfftn'])
@pytest.mark.parametrize('shape,chunks', [((8, 8, 8, 8), (4, 4, 4, 4))])
@pytest.mark.parametrize('axes', [(0,), (1,), (0, 1), (1, 3), (1, 2, 3), (3, 1)])
@pytest.mark.parametrize('norm', [None, 'ortho'])
def test_multi_dimensional(modname, funcname, shape, chunks, axes, norm):
    fft_mod = pytest.importorskip(modname)
    try:
        func = getattr(fft_mod, funcname)
    except AttributeError:
        pytest.skip("`%s` missing function `%s`." % (modname, funcname))
    x = np.arange(np.prod(shape)).reshape(shape)
    y = da.from_array(x, chunks=chunks)
    y = y.rechunk(tuple(shape[i] if i in axes else None  # single chunks
                        for i in range(len(shape))))  # for fft dimensions

    dfunc = fft_wrap(func)

    assert_eq(func(x, axes=axes),
              dfunc(y, axes=axes))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That's a nice test. How long did it take to run?

We should probably also test at least one case of a repeated axis. IMHO I wouldn't want to combine this with the wrapper test and just test the exposed NumPy interface.

Side note: We don't currently support norm in the wrapper. So we should probably just drop that from the test.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It took .7 seconds on my laptop. If you remove norm then it takes 0.36 seconds

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added a test that should exercise the data as described above.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There appears to be a shape mismatch with the irfft[2n]/rfft[2n] functions that I would like to understand.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed a bug with the irfft[2n] case.

However, I'm still seeing something strange with the rfft[2n] case. FWICT this is a bug or at a bare minimum an inconsistency between the transform's and inverse transform's shape. Have opened an issue with NumPy. More details in that issue.

xref: numpy/numpy#8855

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I guess the moral of the story is weird things happen when one repeats FFTs on the same axis. Something NumPy permits and documents. Though that is a weird thing to do. So maybe we should just raise an error if someone does this.

@jakirkham
jakirkham force-pushed the add_fftn branch 2 times, most recently from b96465a to a8ee7e2 Compare March 27, 2017 14:42
@jakirkham

jakirkham commented Mar 27, 2017 •

Copy link
Copy Markdown
Member Author

Seeing a test failure in one of the matrix items, but it is unrelated to the changes here. Seems to be some tolerance issue with a random array test.

Edit: Opened issue ( #2125 ) to document this failure.

@mrocklin

Copy link
Copy Markdown
Member

Yeah. I'll have to handle that later (or feel free to if you have time). Restarted the test.

@jakirkham

Copy link
Copy Markdown
Member Author

Thanks.

We'll see if I have time. Might need to switch gears and do some analysis soon. At least it is documented in an issue for now.

This expands on `fft_wrap` to allow support for wrapping functions of
the form `*fft2` and `*fftn`. To provide this support separate chunk
generating functions were added to handle both the 2-D and N-D cases
together.

In addition to providing this support, the NumPy functions for 2-D and
N-D cases that match the supported API were also wrapped. Though it
should be possible to wrap other FFT functions from external libraries
(as long as the support the NumPy API) should that be desired.
To simplify 1-D and N-D FFT support, this combines the chunk generating
functions for both cases and makes some tweaks to the 1-D case to treat
it as a special case of N-D. Should make the code a little more compact
and hopefully a bit easier to test.
Instead of having separate wrappers for 1-D and N-D functions, add one
wrapper function for both cases. To resolve occasional discrepancies
between the functions add a few branches to handle the different
dimension cases. Also add a small additional wrapper for the 1-D case to
map and format arguments from the 1-D case into something the N-D case
can handle.
@jakirkham
jakirkham force-pushed the add_fftn branch 3 times, most recently from 38cd82d to c7e272c Compare March 27, 2017 17:57
Adds various tests to exercise 2-D and N-D FFT functions. Also tests
wrapping of functions from NumPy and SciPy with 2-D and N-D variants.
Comment thread dask/array/fft.py Outdated
docstring += (fft_preamble % (2 * (func_fullname,)))
docstring += fft_func.__doc__

return docstring

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

These functions are only used once. It might be better to just inline them.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The reason I was pulling them out is the fft_wrap function is exceeding one page in an IDE or terminal. So was thinking it would improve readability to pull out a few smaller functions even if they are only used once.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As a maintainer I prefer not having to jump around in code to understand what's going on. I'd prefer a long linear read to a non-linear read any day.

@jakirkham jakirkham Mar 27, 2017 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Would you prefer I not? Can add them back easily by tossing the latest commit.

Edit: Sorry hadn't seen your comment yet when I posted this. Will drop them.

@jakirkham

Copy link
Copy Markdown
Member Author

So I think this is in a pretty good place. Is there anything else you would like to see before this is ready to merge?

@jakirkham jakirkham changed the title WIP: Add Multidimensional FFTs Add Multidimensional FFTs Mar 27, 2017
As it is sort of strange for someone to take an FFT of an FFT, simply
disallow this behavior by raising an exception. Also check that this
exception is raised.
This is just one less thing to track.  Instead we inline the kind check.
@mrocklin

Copy link
Copy Markdown
Member

Any objection to the recent commit @jakirkham ?

@jakirkham

Copy link
Copy Markdown
Member Author

The reason I had added _dtype was I had some issues getting the closure to bind to the dtype argument from the outer scope and allow assignment in PR ( #2093 ). See this related SO answer on this topic. If you would rather workaround this by just using _dtype, I don't have any issues. The same strategy is definitely not needed with dim.

@jakirkham

Copy link
Copy Markdown
Member Author

Made a small tweak to fix the flake8 error.

@jakirkham

Copy link
Copy Markdown
Member Author

Any objection to the recent commit @jakirkham ?

To clearly and succinctly answer this question, looks fine to me. Just tweaked to make CIs happy.

@mrocklin
mrocklin merged commit 26bca8a into dask:master Mar 28, 2017
@mrocklin

Copy link
Copy Markdown
Member

Thanks @jakirkham . This is in.

@jakirkham

Copy link
Copy Markdown
Member Author

Awesome, thanks @mrocklin.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants