Repository navigation
Add Multidimensional FFTs - #2116
Conversation
mrocklin
left a comment
There was a problem hiding this comment.
Just general comments at this stage. Thanks for taking this on.
| 'irfft2': _irfft_out_chunks, | ||
| 'irfftn': _irfft_out_chunks, | ||
| 'hfft': _hfft_out_chunks, | ||
| 'ihfft': _ihfft_out_chunks} |
There was a problem hiding this comment.
Can we consistently just strip off '2n' here?
chunk_fn = _out_chunk_fns[name.strip('2n')]
There was a problem hiding this comment.
It's a little less clear, but sure we could. Do you think these will actually change that much though?
There was a problem hiding this comment.
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.
| 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))) | ||
|
|
There was a problem hiding this comment.
Any thoughts on pulling in all of these tests to a large parametrized test that was more comprehensive?
There was a problem hiding this comment.
Focusing our energy on the test at the bottom might yield more confidence with less work.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Have consolidated these a bit.
| 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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
01ae721 to
49d3e18
Compare
| 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))) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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:
- Do something with numpy
- Do the equivalent thing with dask.array
- 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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
While we are at it, we can probably do the same thing with 1D tests.
xref: #2118
There was a problem hiding this comment.
Going to just go ahead and rebase this on top of PR ( #2118 ).
There was a problem hiding this comment.
Have combined all the mentioned 2D and ND tests into one test.
There was a problem hiding this comment.
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.
9d1f785 to
e922e4b
Compare
|
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)) |
There was a problem hiding this comment.
For fftn computations should we be using larger dimensional arrays?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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))There was a problem hiding this comment.
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.
There was a problem hiding this comment.
It took .7 seconds on my laptop. If you remove norm then it takes 0.36 seconds
There was a problem hiding this comment.
Added a test that should exercise the data as described above.
There was a problem hiding this comment.
There appears to be a shape mismatch with the irfft[2n]/rfft[2n] functions that I would like to understand.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
b96465a to
a8ee7e2
Compare
|
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. |
|
Yeah. I'll have to handle that later (or feel free to if you have time). Restarted the test. |
|
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.
38cd82d to
c7e272c
Compare
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.
| docstring += (fft_preamble % (2 * (func_fullname,))) | ||
| docstring += fft_func.__doc__ | ||
|
|
||
| return docstring |
There was a problem hiding this comment.
These functions are only used once. It might be better to just inline them.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
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? |
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.
|
Any objection to the recent commit @jakirkham ? |
|
The reason I had added |
|
Made a small tweak to fix the flake8 error. |
To clearly and succinctly answer this question, looks fine to me. Just tweaked to make CIs happy. |
|
Thanks @jakirkham . This is in. |
|
Awesome, thanks @mrocklin. |
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_wrapfunction 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.