Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

API: adjust nD fft s param to array API #25495

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

Merged
merged 1 commit into from
Jan 8, 2024
Merged

Conversation

lucascolley
Copy link
Contributor

@lucascolley lucascolley commented Dec 26, 2023

Towards gh-25076, closes gh-14179, closes scipy/scipy#10569

scipy/scipy#10569 reported that the s parameter of NumPy's nD fft functions behaves differently to the equivalent in scipy.fft. I checked, and there are some changes needed for array API compatibility. This PR adjusts for the following lines of the spec:

If s[i] is -1, the whole input array along the axis i is used (no padding/trimming).

If s is not None, axes must not be None.


To be deprecated:

"If s is not None, axes must not be None."

  • Current behaviour: axes is allowed to be None

s and axes should be of type Sequence[Int]

New features:

"If s[i] is -1, the whole input array along the axis i is used (no padding/trimming)."

  • Current behaviour: ValueError: Invalid number of FFT data points (-1) specified.

@ngoldbaum
Copy link
Member

If we want to do this, it needs a release note. It should likely also be mentioned in the array API compat notes in the docs. Also it sounds like the array API tests don't cover this unless our test suite has an exception for it already.

I searched a little and found one real-world usage this would break in a hydrodynamics solver: https://github.com/pencil-code/pencil-code/blob/91ad3991c339fa62cdf5546cea64fac854c2ba9f/python/pencil/math/Helmholtz.py#L99

Perhaps this could be a deprecation?

@lucascolley
Copy link
Contributor Author

lucascolley commented Dec 29, 2023

Also it sounds like the array API tests don't cover this unless our test suite has an exception for it already.

https://github.com/numpy/numpy/blob/main/tools/ci/array-api-skips.txt#L36-L37

Apparently the fft tests are all skipped right now due to them being buggy. I assume this is why there aren't any points on gh-25076 for fft yet.

Perhaps this could be a deprecation?

I thought the point was to use 2.0 to make the necessary breaking changes for ~100% compat, but forgive me if I'm out of the loop.

If we want to do this, it needs a release note. It should likely also be mentioned in the array API compat notes in the docs.

Happy to add these if we want to continue here.

@lucascolley
Copy link
Contributor Author

Also, I didn't look thoroughly at whether these functions need any other changes. If it turns out that you are going to be touching these functions anyway @mtsokol, feel free to take over my PR. I thought this might have just been something which was missed, which would be understandable considering the buggy tests.

@ngoldbaum ngoldbaum added the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Dec 30, 2023
@rgommers rgommers added this to the 2.0.0 release milestone Jan 3, 2024
@lucascolley
Copy link
Contributor Author

From community meeting, to-do here:

  • add the additional doc lines as per comment above
  • For breaking changes, deprecate instead
  • For new behaviour which used to give errors, fine to introduce
  • also deal with undocumented None behaviour
  • add release note

@lucascolley
Copy link
Contributor Author

What should the deprecation messages say? Mark for removal in 2 release cycles or...?

@ngoldbaum
Copy link
Member

The warning should describe the behavior that's deprecated (e.g. "specifying the s parameter without also specifying the axes parameter is deprecated") and suggest an alternative that isn't deprecated if that's possible. You should also leave a comment next to the deprecation saying when it happened and which numpy version it's in. A few numpy deprecations list a specific version the deprecation will expire but it leaves a little more flexibility if you don't specify it. Also make sure the stacklevel is correct given where in the call stack the warning comes from.

An example I found via grep:

fromnumeric.py
2341-
2342-    >>> np.sum([10], initial=5)
2343-    15
2344-    """
2345-    if isinstance(a, _gentype):
2346-        # 2018-02-25, 1.15.0
2347-        warnings.warn(
2348-            "Calling np.sum(generator) is deprecated, and in the future will "
2349-            "give a different result. Use np.sum(np.fromiter(generator)) or "
2350-            "the python sum builtin instead.",
2351:            DeprecationWarning, stacklevel=2
2352-        )

@seberg
Copy link
Member

seberg commented Jan 3, 2024

Please make sure to add when the warning was added, like (Deprecated in NumPy 2.0). We don't have a habit of promising an expiration version, since it is hard to predict the future.

@lucascolley
Copy link
Contributor Author

To be deprecated:

"If s is not None, axes must not be None."

  • Current behaviour: axes is allowed to be None

s and axes should be of type Sequence[Int]

New features:

"If s[i] is -1, the whole input array along the axis i is used (no padding/trimming)."

  • Current behaviour: ValueError: Invalid number of FFT data points (-1) specified.

@lucascolley
Copy link
Contributor Author

Looks like CI is happy, ready for a look @ngoldbaum !

@lucascolley
Copy link
Contributor Author

I think the doc changes look good, although I need to add deprecation notes for s containing None values.

image

Comment on lines 4 to 6
`numpy.fft.fftn`, `numpy.fft.ifftn`, `numpy.fft.rfftn` and `numpy.fft.irfftn`
are deprecated in line with the following part of the array API
specification: "If ``s`` is not ``None``, ``axes`` must not be ``None``".
Copy link
Member

@ngoldbaum ngoldbaum Jan 4, 2024

Choose a reason for hiding this comment

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

Suggested wording change to the above:

Using `numpy.fft.fftn`, `numpy.fft.ifftn`, `numpy.fft.rfftn` and `numpy.fft.irfftn` with the ``s`` parameter set to value that is not `None` and the ``axes`` set to ``None`` has been deprecated, to match the array API standard.

Right now a reader might mistakenly read that the whole function is deprecated, not just this one usage.

If a user has s set to None or passes an array containing None, what should they do now to get the same behavior back?

EDIT: Ah, I see you mention the fix in the warning, just need to also mention it here.

@ngoldbaum ngoldbaum removed the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Jan 4, 2024
@lucascolley
Copy link
Contributor Author

I've updated the release notes for your suggestion and also documented the deprecations for (i)fft2, as well as adding tests for those functions.

Comment on lines +203 to +205
@pytest.mark.parametrize("op", [np.fft.fftn, np.fft.ifftn,
np.fft.fft2, np.fft.ifft2])
def test_s_negative_1(self, op):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

real transforms aren't included since the shapes aren't the same

@charris charris changed the title API: adjust nD fft s param to array API API: adjust nD fft s param to array API Jan 4, 2024
@ngoldbaum
Copy link
Member

Thanks @lucascolley!

@ngoldbaum ngoldbaum merged commit ea56f1c into numpy:main Jan 8, 2024
@lucascolley lucascolley deleted the nd-fft-s branch January 8, 2024 15:20
@lucascolley
Copy link
Contributor Author

thanks for the review Nathan and Marten!

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

Successfully merging this pull request may close these issues.

numpy.fft: Undocumented behavior of s argument API: s argument different in scipy.fft and numpy.fft
5 participants