Skip to content

Commit

Permalink
Merge pull request #186 from roxyboy/version
Browse files Browse the repository at this point in the history
Linting for a new version release
  • Loading branch information
Takaya Uchida committed Jun 13, 2022
2 parents eb5d330 + 8a38e54 commit d5a89dc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 69 deletions.
16 changes: 8 additions & 8 deletions xrft/tests/test_xrft.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_fft_1d(self, test_data_1d):
dx = float(da.x[1] - da.x[0]) if "x" in da.dims else 1

# defaults with no keyword args
ft = xrft.fft(da, detrend="constant")
ft = xrft.dft(da, detrend="constant")
# check that the frequency dimension was created properly
assert ft.dims == ("freq_x",)
# check that the coords are correct
Expand All @@ -82,12 +82,12 @@ def test_fft_1d(self, test_data_1d):
npt.assert_allclose(ft_data_expected, ft.values, atol=1e-14)

# redo without removing mean
ft = xrft.fft(da)
ft = xrft.dft(da)
ft_data_expected = np.fft.fftshift(np.fft.fft(da))
npt.assert_allclose(ft_data_expected, ft.values)

# redo with detrending linear least-square fit
ft = xrft.fft(da, detrend="linear")
ft = xrft.dft(da, detrend="linear")
da_prime = sps.detrend(da.values)
ft_data_expected = np.fft.fftshift(np.fft.fft(da_prime))
npt.assert_allclose(ft_data_expected, ft.values, atol=1e-14)
Expand All @@ -103,7 +103,7 @@ def test_fft_1d_time(self, time_data):
Nt = len(time)
da = xr.DataArray(np.random.rand(Nt), coords=[time], dims=["time"])

ft = xrft.fft(da, shift=False)
ft = xrft.dft(da, shift=False)

# check that frequencies are correct
if pd.api.types.is_datetime64_dtype(time):
Expand All @@ -119,10 +119,10 @@ def test_fft_2d(self):
da = xr.DataArray(
np.random.rand(N, N), dims=["x", "y"], coords={"x": range(N), "y": range(N)}
)
ft = xrft.fft(da, shift=False)
ft = xrft.dft(da, shift=False)
npt.assert_almost_equal(ft.values, np.fft.fftn(da.values))

ft = xrft.fft(da, shift=False, window="hann", detrend="constant")
ft = xrft.dft(da, shift=False, window="hann", detrend="constant")
dim = da.dims
window = (
sps.windows.hann(N, sym=False)
Expand Down Expand Up @@ -213,7 +213,7 @@ def test_fft_real_1d(self, test_data_1d):
dx = float(da.x[1] - da.x[0]) if "x" in da.dims else 1

# defaults with no keyword args
ft = xrft.fft(da, real_dim="x", detrend="constant")
ft = xrft.dft(da, real_dim="x", detrend="constant")
# check that the frequency dimension was created properly
assert ft.dims == ("freq_x",)
# check that the coords are correct
Expand Down Expand Up @@ -248,7 +248,7 @@ def test_fft_real_2d(self):
dx = float(da.x[1] - da.x[0])
dy = float(da.y[1] - da.y[0])

daft = xrft.fft(da, real_dim="x")
daft = xrft.dft(da, real_dim="x")
npt.assert_almost_equal(
daft.values, np.fft.rfftn(da.transpose("y", "x")).transpose()
)
Expand Down
80 changes: 19 additions & 61 deletions xrft/xrft.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
"isotropize",
"isotropic_power_spectrum",
"isotropic_cross_spectrum",
"isotropic_powerspectrum",
"isotropic_crossspectrum",
"fit_loglog",
]

Expand Down Expand Up @@ -277,8 +275,8 @@ def fft(
shift=True,
detrend=None,
window=None,
true_phase=False,
true_amplitude=False,
true_phase=True,
true_amplitude=True,
chunks_to_segments=False,
prefix="freq_",
**kwargs,
Expand Down Expand Up @@ -333,10 +331,6 @@ def fft(
The output of the Fourier transformation, with appropriate dimensions.
"""

if not true_phase and not true_amplitude:
msg = "Flags true_phase and true_amplitude will be set to True in future versions of xrft.fft to preserve the theoretical phasing and amplitude of Fourier Transform. Consider using xrft.fft to ensure future compatibility with numpy.fft like behavior and to deactivate this warning."
warnings.warn(msg, FutureWarning)

if dim is None:
dim = list(da.dims)
else:
Expand Down Expand Up @@ -474,8 +468,8 @@ def ifft(
dim=None,
real_dim=None,
shift=True,
true_phase=False,
true_amplitude=False,
true_phase=True,
true_amplitude=True,
chunks_to_segments=False,
prefix="freq_",
lag=None,
Expand Down Expand Up @@ -527,10 +521,6 @@ def ifft(
The output of the Inverse Fourier transformation, with appropriate dimensions.
"""

if not true_phase and not true_amplitude:
msg = "Flags true_phase and true_amplitude will be set to True in future versions of xrft.ifft to preserve the theoretical phasing and amplitude of Inverse Fourier Transform. Consider using xrft.ifft to ensure future compatibility with numpy.ifft like behavior and to deactivate this warning."
warnings.warn(msg, FutureWarning)

if dim is None:
dim = list(daft.dims)
else:
Expand Down Expand Up @@ -773,7 +763,7 @@ def cross_spectrum(
real_dim=None,
scaling="density",
window_correction=False,
true_phase=False,
true_phase=True,
**kwargs,
):
"""
Expand Down Expand Up @@ -805,17 +795,13 @@ def cross_spectrum(
If scaling = 'density', correct for the energy (integral) of the spectrum. This ensures, for example, that the power spectral density integrates to the square of the RMS of the signal (ie that Parseval's theorem is satisfied). Note that in most cases, Parseval's theorem will only be approximately satisfied with this correction as it assumes that the signal being windowed is independent of the window. The correction becomes more accurate as the width of the window gets large in comparison with any noticeable period in the signal.
If False, the spectrum gives a representation of the power in the windowed signal.
Note that when True, Parseval's theorem may only be approximately satisfied.
true_phase : boolean
If True, the phase information is retained.
Set explicitly true_phase = False in cross_spectrum arguments list to ensure future compatibility
with numpy-like behavior where the coordinates are disregarded.
kwargs : dict : see xrft.fft for argument list
"""

if not true_phase:
msg = (
"true_phase flag will be set to True in future version of xrft.fft possibly impacting cross_spectrum output. "
+ "Set explicitely true_phase = False in cross_spectrum arguments list to ensure future compatibility "
+ "with numpy-like behavior where the coordinates are disregarded."
)
warnings.warn(msg, FutureWarning)

if "real" in kwargs:
real_dim = kwargs.get("real")
msg = "`real` flag will be deprecated in future version of xrft.cross_spectrum and replaced by `real_dim` flag."
Expand Down Expand Up @@ -885,7 +871,7 @@ def cross_spectrum(
return cs


def cross_phase(da1, da2, dim=None, true_phase=False, **kwargs):
def cross_phase(da1, da2, dim=None, true_phase=True, **kwargs):
"""
Calculates the cross-phase between da1 and da2.
Expand All @@ -902,15 +888,15 @@ def cross_phase(da1, da2, dim=None, true_phase=False, **kwargs):
The data to be transformed
da2 : `xarray.DataArray`
The data to be transformed
dim : str or sequence of str, optional
The dimensions along which to take the transformation. If `None`, all
dimensions will be transformed.
true_phase : boolean
If True, the phase information is retained.
Set explicitly true_phase = False in cross_spectrum arguments list to ensure future compatibility
with numpy-like behavior where the coordinates are disregarded.
kwargs : dict : see xrft.fft for argument list
"""
if not true_phase:
msg = (
"true_phase flag will be set to True in future version of xrft.fft possibly impacting cross_phase output. "
+ "Set explicitely true_phase = False in cross_spectrum arguments list to ensure future compatibility "
+ "with numpy-like behavior where the coordinates are disregarded."
)
warnings.warn(msg, FutureWarning)

cp = xr.ufuncs.angle(
cross_spectrum(da1, da2, dim=dim, true_phase=true_phase, **kwargs)
Expand Down Expand Up @@ -993,7 +979,7 @@ def _groupby_bins_agg(
return result


def isotropize(ps, fftdim, nfactor=4, truncate=False, complx=False):
def isotropize(ps, fftdim, nfactor=4, truncate=True, complx=False):
"""
Isotropize a 2D power spectrum or cross spectrum
by taking an azimuthal average.
Expand Down Expand Up @@ -1035,11 +1021,7 @@ def isotropize(ps, fftdim, nfactor=4, truncate=False, complx=False):
kmax = k.max()
kr = kr.where(kr <= kmax)
else:
msg = (
"The flag `truncate` will be set to True by default in future version "
+ "in order to truncate the isotropic wavenumber larger than the "
+ "Nyquist wavenumber."
)
msg = "Isotropic wavenumber larger than the " + "Nyquist wavenumber may result."
warnings.warn(msg, FutureWarning)

if complx:
Expand All @@ -1061,18 +1043,6 @@ def isotropize(ps, fftdim, nfactor=4, truncate=False, complx=False):
return iso_ps * iso_ps.freq_r


def isotropic_powerspectrum(*args, **kwargs): # pragma: no cover
"""
Deprecated function. See isotropic_power_spectrum doc
"""
msg = (
"This function has been renamed and will disappear in the future."
+ " Please use isotropic_power_spectrum instead"
)
warnings.warn(msg, Warning)
return isotropic_power_spectrum(*args, **kwargs)


def isotropic_power_spectrum(
da,
spacing_tol=1e-3,
Expand Down Expand Up @@ -1158,18 +1128,6 @@ def isotropic_power_spectrum(
return isotropize(ps, fftdim, nfactor=nfactor, truncate=truncate)


def isotropic_crossspectrum(*args, **kwargs): # pragma: no cover
"""
Deprecated function. See isotropic_cross_spectrum doc
"""
msg = (
"This function has been renamed and will disappear in the future."
+ " Please use isotropic_cross_spectrum instead"
)
warnings.warn(msg, Warning)
return isotropic_cross_spectrum(*args, **kwargs)


def isotropic_cross_spectrum(
da1,
da2,
Expand Down

0 comments on commit d5a89dc

Please sign in to comment.