SciPy
The SciPy pack has 30 rules, and xpd can fix 18 of them.
The SciPy rules report code that raises or warns on SciPy 1.18.1, code that loses digits, and
SciPy calls made once per value, in each file that imports scipy. The examples assume
import numpy as np.
When a message uses a word from the table, the word means the result of a check on SciPy 1.18.1 with NumPy 2.5.5.
| Word in the message | What was measured |
|---|---|
| removed, deprecated | The old form raises under python -W error. |
| inexact | The old form lost at least 3 decimal digits against mpmath at 50 digits, and the form to write lost fewer than 1. |
| faster | The range over repeated runs on one core, at 1,000,000 values unless the rule says otherwise. The faster form was not slower at 1,000 values. |
| legacy, preferred | SciPy's own source calls the old form legacy or prefers the new one. Such rules have severity info and claim nothing about speed or accuracy. |
A rule reports a name only when the dotted name starts with scipy, or when the file's own
imports bind it from scipy. A form made only of NumPy names, such as np.linalg.inv(a) @ b, belongs to the
NumPy rules. A rule that asks for an API newer than the project's SciPy is skipped.
| Fix | What it promises |
|---|---|
| safe | The same object, or a value of the same type that is equal bit for bit. xpd offers no safe fix where the file binds the name again or the call spreads *args or **kwargs. |
| unsafe | The message names what can differ. |
Names that are gone or that warn
Section titled “Names that are gone or that warn”Replace each name that SciPy removed or deprecated with the name the rule gives, because a removed name raises on import or on the call.
| Rule | Severity | Fix | What it finds |
|---|---|---|---|
| removed-in-scipy | error | unsafe | These names were removed from SciPy and raise AttributeError on SciPy 1.18.1. |
| deprecated-in-scipy | warning | unsafe | These names warn on SciPy 1.18.1, often because they come from a private module. |
Arguments, defaults and legacy functions
Section titled “Arguments, defaults and legacy functions”Change each argument that SciPy removed, deprecated or ignores, and move each legacy function to the interface that SciPy's documentation names as its replacement.
| Rule | Severity | Fix | What it finds |
|---|---|---|---|
| removed-keyword | error | unsafe | These keywords and argument values were removed from SciPy and raise TypeError on SciPy 1.18.1. |
| deprecated-argument | warning | unsafe | These arguments warn on SciPy 1.18.1, and SciPy will remove them. |
| default-that-warns | warning | unsafe | These calls rely on a default that warns on SciPy 1.18.1 because it will change. |
| minimize-unknown-option | error | none | minimize warns and ignores an option that the chosen method does not know, so the setting has no effect. |
| minimize-unused-argument | warning | none | minimize ignores an argument such as jac= when the chosen method does not use it, and warns. |
| rng-keyword | info | unsafe | Since SciPy 1.15, these functions take rng=, and SciPy's docs say new code should use it in place of random_state= or seed=. |
| legacy-optimize | info | none | SciPy lists these optimize functions as legacy and not recommended for new scripts. |
| legacy-odeint | info | none | SciPy's docs say new code should use solve_ivp instead of odeint. |
| legacy-interpolate | info | none | SciPy marks interp1d and these other interpolation classes as legacy. |
| legacy-signal | info | safe | SciPy marks signal.stft, spectrogram and similar functions as legacy. |
| legacy-api | info | safe | SciPy keeps these names only for old code. |
Forms that keep digits
Section titled “Forms that keep digits”Use the SciPy function that computes the quantity directly, because the two-step form loses digits or overflows where the direct function doesn't.
| Rule | Severity | Fix | What it finds |
|---|---|---|---|
| log-of-special | warning | unsafe | A scipy.special function such as ndtr underflows to 0 far in a tail, so its log is -inf there. |
| log-of-dist-method | warning | unsafe | A distribution's pdf, cdf or sf underflows to 0 far in a tail, so np.log of it gives -inf there. |
| cdf-complement | warning | unsafe | 1 - dist.cdf(x) drops the digits of a small right tail and gives 0 once the cdf rounds to 1. |
| special-complement | warning | unsafe | 1 - special.f(x) drops the digits of a small result and gives 0 once the function rounds to 1. |
| ppf-of-complement | warning | unsafe | ppf(1 - p) rounds away the digits of a small p, so it loses precision far in the tail. |
| comb-by-factorials | warning | unsafe | factorial overflows to inf above 170, so a binomial coefficient built from factorials gives inf or nan for larger numbers. |
| log-of-scipy-det | warning | unsafe | linalg.det overflows or underflows for a large matrix, so its log is inf or -inf. |
SciPy calls made once per value or once per pair
Section titled “SciPy calls made once per value or once per pair”Replace each SciPy call that Python makes once per value, once per pair of points or once per pass of a loop with one call on whole arrays, which does the same work in compiled code. Each speedup is a median of 7 runs on one core with SciPy 1.18.1, on float64 and float32, at 1,000,000 values unless the rule names another size.
| Rule | Severity | Fix | What it finds |
|---|---|---|---|
| distance-per-pair | warning | none | Calling a distance function once for every pair of points runs Python for each pair. |
| callable-metric | warning | unsafe | Passing a metric function such as euclidean to cdist or pdist calls it once for every pair in Python. |
| per-value-scipy-call | warning | none | Calling a SciPy function that takes whole arrays once for every value was 7 to 23 times slower. |
| query-per-point | warning | none | Querying a KD tree once for every point makes one call for each point. |
| solve-in-loop | warning | none | spsolve or solve inside a loop factors the same matrix again on every pass. |
| sparse-inv-then-multiply | warning | unsafe | The inverse of most sparse matrices is dense, so inv(A) @ b builds a huge array. |
Sparse matrices
Section titled “Sparse matrices”Build a sparse matrix in a format made for writes, and use the sparse array classes in place of the matrix classes.
| Rule | Severity | Fix | What it finds |
|---|---|---|---|
| empty-csr-write | warning | none | Writing into a CSR or CSC matrix that started empty warns, raises under python -W error, and copies the whole matrix each time. |
| sparse-matrix-api | info | none | SciPy expects to deprecate the sparse matrix classes. |
The API that SciPy's docs prefer on SciPy 1.18
Section titled “The API that SciPy's docs prefer on SciPy 1.18”Use the class or module that SciPy's documentation prefers, because the older name stays only for old code.
| Rule | Severity | Fix | What it finds |
|---|---|---|---|
| ckdtree | info | unsafe | cKDTree is the same as KDTree, and SciPy keeps the name only for old code. |
| legacy-fftpack | info | none | scipy.fftpack is legacy, and SciPy says new code should use scipy.fft. |