Thanks to visit codestin.com
Credit goes to github.com

Skip to content

DEP: do not deprecate the trapezoidal rule #25586

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

Closed
ntessore opened this issue Jan 15, 2024 · 5 comments · Fixed by #25738
Closed

DEP: do not deprecate the trapezoidal rule #25586

ntessore opened this issue Jan 15, 2024 · 5 comments · Fixed by #25738

Comments

@ntessore
Copy link
Contributor

Proposed new feature or change:

I would like to ask you to reconsider the planned removal of the trapezoidal rule (currently trapz(), but see below about the name) in numpy v2, in favour of the equivalent in SciPy.

While trapz() is perhaps not in the first tier of numpy functions, it is not exactly rare, with np.trapz showing up around 20k times in a GitHub code search. Its implementation is a very simple pure Python function with an exceedingly stable interface, and I cannot imagine it is much of a maintenance burden.

While the functionality could be out-sourced to SciPy, the requirements for SciPy far exceed those of Numpy, and for a project not already using SciPy, it arguably makes more sense to copy and paste Numpy's soon-to-be-deprecated implementation than to add the quite heavy dependency on SciPy.

I understand that there has initially been an objection to the trapz() name, on the basis of derogatory connotations. Not being part of the affected group, I do not have an opinion on the name. But perhaps it would suffice to deprecate the old name in favour of a new one for the existing function, which would still be far less disruptive than complete removal of the functionality.

@rgommers
Copy link
Member

Thanks @ntessore for opening this discussion. Given the usage frequency, I agree this would be somewhat disruptive. The current state is:

  • It's deprecated in 2.0, with removal probably having a long-ish timeline,
  • The recommended replacement is scipy.stats.trapezoid,
  • If a new dependency on scipy wouldn't be feasible, the implementation (which is basic and pure Python) could be vendored fairly easily.

An alternative is to keep it but rename it. For the majority of usage I think it's the same, since most projects and end user code is already going to rely on scipy. For the rest, they'd have more work if they decided to vendor the code to avoid a new dependency.

I'm not opposed to adding trapezoid. I am curious why it is used so much, given that it's somewhat inaccurate "area under curve given by samples" approximation, and for example scipy.integrate.simpson would usually be more accurate. Perhaps because it's good enough in many cases, and also because users see this function more prominently (simpson is not present in numpy after all). Scope wise I think it was a pretty random choice a long time ago to add this one function.

Let's see what others think.

@ntessore
Copy link
Contributor Author

Thanks for your reply @rgommers. Let me address the one point that I can:

I am curious why it is used so much, given that it's somewhat inaccurate "area under curve given by samples" approximation

I use the trapezoidal rule as my go-to because the only generic assumption that I am willing to make about sampled data is that it is sampled finely enough for a piecewise linear approximation to be "good enough". The trapezoidal rule implements this assumption, while anything beyond that is, in some sense, "augmentation" of the data. In my work, I am usually integrating data I have sampled myself, and it is hence usually always possible for me to push the sampling into that "linear is good enough" regime.

There other reasons for choosing the trapezoidal rule (e.g. when the given data needs resampling, and the piecewise linear approximation allows you find additional sampling points without changing the result of the quadrature), and there are reasons when the trapezoidal rule is actually the method of choice (some oscillatory integrals). I don't think these "informed choices" are reasons to keep the function around. Instead, I would argue firmly along the lines of: here is a simple quadrature method that makes the minimal amount of assumptions about the data, and in many (most?) cases its error can be pushed below any desired tolerance.

@charris
Copy link
Member

charris commented Jan 16, 2024

some oscillatory integrals

In particular, the dft :)

@rgommers
Copy link
Member

Discussed in the community meeting today: we'll put it back in as trapezoid. The argument of @ntessore is compelling enough, we don't want to cause too much disruption here by having folks need SciPy for this.

The error message will mention both trapezoid and that scipy.integrate has more methods for this.

@ntessore
Copy link
Contributor Author

Thanks for taking the time to consider this issue!

rgommers added a commit to rgommers/numpy that referenced this issue Jan 31, 2024
Rather than pointing users at ``scipy.interpolate.trapezoid``, we
keep the functionality in NumPy after the discussion in numpygh-25586.

`trapz` stays deprecated, and will be removed in the future. But
this change makes the change for users who don't want to depend on
SciPy a lot easier.

[skip cirrus]
rgommers added a commit to rgommers/numpy that referenced this issue Jan 31, 2024
Rather than pointing users at ``scipy.interpolate.trapezoid``, we
keep the functionality in NumPy after the discussion in numpygh-25586.

`trapz` stays deprecated, and will be removed in the future. But
this change makes the change for users who don't want to depend on
SciPy a lot easier.

Closes numpygh-25586

[skip cirrus]
rgommers added a commit to rgommers/numpy that referenced this issue Feb 1, 2024
Rather than pointing users at ``scipy.interpolate.trapezoid``, we
keep the functionality in NumPy after the discussion in numpygh-25586.

`trapz` stays deprecated, and will be removed in the future. But
this change makes the change for users who don't want to depend on
SciPy a lot easier.

Closes numpygh-25586

[skip cirrus]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants