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

trouble with real=True in dft #83

Closed
navidcy opened this issue Nov 15, 2019 · 9 comments
Closed

trouble with real=True in dft #83

navidcy opened this issue Nov 15, 2019 · 9 comments

Comments

@navidcy
Copy link
Contributor

navidcy commented Nov 15, 2019

I am having some trouble using the real fft. I have a time series of temperature data:

da = surf_temp_1selection.copy()
months = np.arange(da.time.size)+1
da.time.values = months
da = da.chunk({'time':None})
da = da.mean(dim={'xt_ocean', 'yt_ocean'})
da

<xarray.DataArray 'surface_temp' (time: 2520)>
dask.array<mean_agg-aggregate, shape=(2520,), dtype=float32, chunksize=(2520,), chunktype=numpy.ndarray>
Coordinates:
  * time     (time) int64 1 2 3 4 5 6 7 8 ... 2514 2515 2516 2517 2518 2519 2520

When I use dft without the real=True flag all seems good:

dahat = xrft.dft(da, dim=['time'], window=True)
dahat

<xarray.DataArray 'rechunk-merge-43b3f58ebe71665aef5d349f10b36e8c' (freq_time: 2520)>
dask.array<rechunk-merge, shape=(2520,), dtype=complex128, chunksize=(2520,), chunktype=numpy.ndarray>
Coordinates:
  * freq_time          (freq_time) float64 -0.5 -0.4996 ... 0.4992 0.4996
    freq_time_spacing  float64 0.0003968

But when I give real=True I get:

dahat = xrft.dft(da, dim=['time'], window=True, real=True)
dahat


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-89-2e819cfb9956> in <module>
      6 
      7 print(da)
----> 8 dahat = xrft.dft(da, dim=['time'], window=True, real=True)
      9 print(" ");print(" ");print(" ");
     10 print(dahat)

/g/data3/hh5/public/apps/miniconda3/envs/analysis3-19.10/lib/python3.6/site-packages/xrft/xrft.py in dft(da, spacing_tol, dim, real, shift, detrend, window, chunks_to_segments)
    250 
    251     rawdims = da.dims
--> 252     da, trans = _transpose(da, real)
    253     if dim is None:
    254         dim = list(da.dims)

/g/data3/hh5/public/apps/miniconda3/envs/analysis3-19.10/lib/python3.6/site-packages/xrft/xrft.py in _transpose(da, real, trans)
    194         transdim = list(da.dims)
    195         if real not in transdim:
--> 196             raise ValueError("The dimension along real FT is taken must "
    197                             "be one of the existing dimensions.")
    198         elif real != transdim[-1]:

ValueError: The dimension along real FT is taken must be one of the existing dimensions.

Am I doing something wrong?

@roxyboy
Copy link
Member

roxyboy commented Nov 15, 2019

real takes the dimension to take the real FFT over so in your case, dahat = xrft.dft(da, window=True, real='time') should do the trick.

@roxyboy
Copy link
Member

roxyboy commented Nov 15, 2019

Did it work @navidcy ? If so, I'm gonna close this :)

@navidcy
Copy link
Contributor Author

navidcy commented Nov 15, 2019

@roxyboy, I got a different error now

xrft.dft(da, dim=['time'], real='time', window=True)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-17-3ee90d6aade1> in <module>
----> 1 xrft.dft(da, dim=['time'], real='time', window=True)

/g/data3/hh5/public/apps/miniconda3/envs/analysis3-19.07/lib/python3.6/site-packages/xrft/xrft.py in dft(da, spacing_tol, dim, real, shift, detrend, window, chunks_to_segments)
    350     if trans:
    351         enddims = [prefix + d for d in rawdims if d in dim]
--> 352         return daft.transpose(*enddims)
    353     else:
    354         return daft

/g/data3/hh5/public/apps/miniconda3/envs/analysis3-19.07/lib/python3.6/site-packages/xarray/core/dataarray.py in transpose(self, transpose_coords, *dims)
   1850                 raise ValueError(
   1851                     "arguments to transpose (%s) must be "
-> 1852                     "permuted array dimensions (%s)" % (dims, tuple(self.dims))
   1853                 )
   1854 

ValueError: arguments to transpose (('freq_time',)) must be permuted array dimensions (('yt_ocean', 'xt_ocean', 'freq_time'))

But you are right. This doesn't seem to be an issue of the package so I'm closing it and we can continue discussion over email.

@navidcy navidcy closed this as completed Nov 15, 2019
@roxyboy
Copy link
Member

roxyboy commented Nov 19, 2019

This is probably the same issue as #78 . It seems that xarray did some updates which we may have to update xrft...

@navidcy navidcy reopened this Nov 19, 2019
@navidcy
Copy link
Contributor Author

navidcy commented Nov 19, 2019

OK, I'm reopening this then.
I don't really understand the solution @TomNicholas suggests in #78.

How come the tests didn't break when xarray updated? Perhaps you need to make stricter tests? (I'd happily do it but I'm not very good with python...)

@roxyboy
Copy link
Member

roxyboy commented Nov 19, 2019

That's probably because our test environment is Python 3.6.7 while as the new xarray features are in Python 3.7

@roxyboy
Copy link
Member

roxyboy commented Nov 20, 2019

@navidcy Do you wanna give PR #88 a try to see if it works for your needs? I think it should.

@rabernat
Copy link
Collaborator

Xarray is the same in Python 3.6 and 3.7

@roxyboy
Copy link
Member

roxyboy commented Nov 20, 2019

Hmm, then I'm not sure why real DFT generated errors in Python 3.7 and not in 3.6. In any case, the tests for real DFT in Python 3.7 all pass in PR #88.

@roxyboy roxyboy closed this as completed Feb 15, 2020
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

No branches or pull requests

3 participants