Skip to content

Commit

Permalink
replace usage of deprecated np.product
Browse files Browse the repository at this point in the history
  • Loading branch information
zachjweiner committed Aug 6, 2023
1 parent f0c7fa7 commit 27448e1
Show file tree
Hide file tree
Showing 14 changed files with 31 additions and 31 deletions.
4 changes: 2 additions & 2 deletions examples/scalar_preheating.py
Expand Up @@ -69,12 +69,12 @@ def main():
p = parser.parse_args()
# process args
p.grid_shape = tuple(p.grid_shape)
p.grid_size = np.product(p.grid_shape)
p.grid_size = np.prod(p.grid_shape)
p.proc_shape = tuple(p.proc_shape)
p.rank_shape = tuple(Ni // pi for Ni, pi in zip(p.grid_shape, p.proc_shape))
p.pencil_shape = tuple(ni + 2 * p.halo_shape for ni in p.rank_shape)
p.box_dim = tuple(p.box_dim)
p.volume = np.product(p.box_dim)
p.volume = np.prod(p.box_dim)
p.dx = tuple(Li / Ni for Li, Ni in zip(p.box_dim, p.grid_shape))
p.dk = tuple(2 * np.pi / Li for Li in p.box_dim)
dt = p.kappa * min(p.dx)
Expand Down
4 changes: 2 additions & 2 deletions pystella/decomp.py
Expand Up @@ -270,8 +270,8 @@ def _bind_kernels(self, queue_or_context):
def get_displs_and_counts(self, full_shape, x_slice):
NX, NY, NZ = full_shape

displs = np.ones(np.product(self.proc_shape[:2]), dtype="int")
counts = np.ones(np.product(self.proc_shape[:2]), dtype="int")
displs = np.ones(np.prod(self.proc_shape[:2]), dtype="int")
counts = np.ones(np.prod(self.proc_shape[:2]), dtype="int")
for ri in range(self.proc_shape[0]):
for rj in range(self.proc_shape[1]):
(Nx, Ny, Nz), (sx, sy, sz) = self.get_rank_shape_start(
Expand Down
2 changes: 1 addition & 1 deletion pystella/fourier/spectra.py
Expand Up @@ -71,7 +71,7 @@ def __init__(self, decomp, fft, dk, volume, **kwargs):
self.dk = dk
self.bin_width = kwargs.pop("bin_width", min(dk))

d3x = volume / np.product(self.grid_shape)
d3x = volume / np.prod(self.grid_shape)
self.norm = (1 / 2 / np.pi**2 / volume) * d3x**2

sub_k = list(x.get() for x in self.fft.sub_k.values())
Expand Down
4 changes: 2 additions & 2 deletions pystella/multigrid/relax.py
Expand Up @@ -257,7 +257,7 @@ def get_error(self, queue, **kwargs):

padded_shape = kwargs.get(self.unknown_args[0].name).shape
rank_shape = tuple(i - 2 * self.halo_shape for i in padded_shape)
grid_size = np.product(self.decomp.proc_shape) * np.product(rank_shape)
grid_size = np.prod(self.decomp.proc_shape) * np.prod(rank_shape)
errs = self.resid_stats(queue, **kwargs, filter_args=True,
rank_shape=rank_shape, grid_size=grid_size)
for k, v in errs.items():
Expand Down Expand Up @@ -285,7 +285,7 @@ def eval_constraint(self, queue, shifts, scales, **kwargs):

padded_shape = kwargs.get(self.unknown_args[0].name).shape
rank_shape = tuple(i - 2 * self.halo_shape for i in padded_shape)
grid_size = np.product(self.decomp.proc_shape) * np.product(rank_shape)
grid_size = np.prod(self.decomp.proc_shape) * np.prod(rank_shape)

args_to_avg_resid = kwargs.copy()
for arg in self.unknown_args:
Expand Down
2 changes: 1 addition & 1 deletion pystella/reduction.py
Expand Up @@ -245,7 +245,7 @@ def __call__(self, queue=None, filter_args=False, **kwargs):
if op == "avg":
if self.grid_size is None:
Nx = output["Nx_"].get()
sub_grid_size = Nx * np.product(tmp[j].shape)
sub_grid_size = Nx * np.prod(tmp[j].shape)
grid_size = self.decomp.allreduce(sub_grid_size)
else:
grid_size = self.grid_size
Expand Down
6 changes: 3 additions & 3 deletions test/test_dft.py
Expand Up @@ -36,7 +36,7 @@
@pytest.mark.parametrize("dtype", ["float64", "complex128"])
@pytest.mark.parametrize("backend", ["fftw", "clfft", "vkfft"])
def test_dft(ctx_factory, grid_shape, proc_shape, dtype, backend, timing=False):
if backend != "fftw" and np.product(proc_shape) > 1:
if backend != "fftw" and np.prod(proc_shape) > 1:
pytest.skip("Must use mpi4py-fft on more than one rank.")

ctx = ctx_factory()
Expand All @@ -48,7 +48,7 @@ def test_dft(ctx_factory, grid_shape, proc_shape, dtype, backend, timing=False):
rank_shape, _ = mpi.get_rank_shape_start(grid_shape)

fft = ps.DFT(mpi, ctx, queue, grid_shape, dtype, backend=backend)
grid_size = np.product(grid_shape)
grid_size = np.prod(grid_shape)
rdtype = fft.rdtype

if fft.is_real:
Expand Down Expand Up @@ -146,7 +146,7 @@ def test_dft(ctx_factory, grid_shape, proc_shape, dtype, backend, timing=False):
from common import parser
parser.add_argument("--backend", type=str, default="vkfft")
args = parser.parse_args()
if np.product(args.proc_shape) > 1:
if np.prod(args.proc_shape) > 1:
args.backend = "fftw"

test_dft(
Expand Down
2 changes: 1 addition & 1 deletion test/test_energy.py
Expand Up @@ -41,7 +41,7 @@ def test_scalar_energy(ctx_factory, grid_shape, proc_shape, h, dtype, timing=Fal
mpi = ps.DomainDecomposition(proc_shape, h, grid_shape=grid_shape)
rank_shape, _ = mpi.get_rank_shape_start(grid_shape)

grid_size = np.product(grid_shape)
grid_size = np.prod(grid_shape)

nscalars = 2

Expand Down
4 changes: 2 additions & 2 deletions test/test_histogram.py
Expand Up @@ -57,7 +57,7 @@ def test_trivial_histogram(ctx_factory, grid_shape, proc_shape, dtype,
for key, (_b, weight) in histograms.items():
res = result[key]
b = int(np.floor(_b))
expected = weight * np.product(grid_shape)
expected = weight * np.prod(grid_shape)
assert res[b] == expected, \
f"{key}: result={res[b]}, {expected=}, ratio={res[b]/expected}"
assert np.all(res[res != res[b]] == 0.)
Expand Down Expand Up @@ -94,7 +94,7 @@ def test_histogram(ctx_factory, grid_shape, proc_shape, dtype, num_bins,
result = hist(queue, fx=fx)

res = result["count"]
assert np.sum(res.astype("int64")) == np.product(grid_shape), \
assert np.sum(res.astype("int64")) == np.prod(grid_shape), \
f"Count histogram doesn't sum to grid_size ({np.sum(res)})"

bins = np.linspace(0, 1, num_bins+1).astype(dtype)
Expand Down
2 changes: 1 addition & 1 deletion test/test_multigrid.py
Expand Up @@ -51,7 +51,7 @@ def test_multigrid(ctx_factory, grid_shape, proc_shape, h, dtype, Solver, MG,
dx = L / grid_shape[0]

statistics = ps.FieldStatistics(mpi, h, rank_shape=rank_shape,
grid_size=np.product(grid_shape))
grid_size=np.prod(grid_shape))

def get_laplacian(f):
from pystella.derivs import _lap_coefs, centered_diff
Expand Down
2 changes: 1 addition & 1 deletion test/test_poisson.py
Expand Up @@ -63,7 +63,7 @@ def get_evals_2(k, dx):
pencil_shape = tuple(ni + 2*h for ni in rank_shape)

statistics = ps.FieldStatistics(mpi, 0, rank_shape=rank_shape,
grid_size=np.product(grid_shape))
grid_size=np.prod(grid_shape))

fx = cla.empty(queue, pencil_shape, dtype)
rho = clr.rand(queue, rank_shape, dtype)
Expand Down
8 changes: 4 additions & 4 deletions test/test_rayleigh.py
Expand Up @@ -45,7 +45,7 @@ def test_generate_WKB(ctx_factory, grid_shape, proc_shape, dtype, random,
fft = ps.DFT(mpi, ctx, queue, grid_shape, dtype)

L = (10,)*3
volume = np.product(L)
volume = np.prod(L)
dk = tuple(2 * np.pi / Li for Li in L)
modes = ps.RayleighGenerator(ctx, fft, dk, volume)

Expand Down Expand Up @@ -73,13 +73,13 @@ def test_generate(ctx_factory, grid_shape, proc_shape, dtype, random, timing=Fal

num_bins = int(sum(Ni**2 for Ni in grid_shape)**.5 / 2 + .5) + 1
L = (10,)*3
volume = np.product(L)
volume = np.prod(L)
dk = tuple(2 * np.pi / Li for Li in L)
spectra = ps.PowerSpectra(mpi, fft, dk, volume)
modes = ps.RayleighGenerator(ctx, fft, dk, volume, seed=5123)

kbins = min(dk) * np.arange(0, num_bins)
test_norm = 1 / 2 / np.pi**2 / np.product(grid_shape)**2
test_norm = 1 / 2 / np.pi**2 / np.prod(grid_shape)**2

for exp in [-1, -2, -3]:
def power(k):
Expand All @@ -101,7 +101,7 @@ def power(k):
if isinstance(fx, cla.Array):
fx = fx.get()

grid_size = np.product(grid_shape)
grid_size = np.prod(grid_shape)

avg = mpi.allreduce(np.sum(fx)) / grid_size
var = mpi.allreduce(np.sum(fx**2)) / grid_size - avg**2
Expand Down
14 changes: 7 additions & 7 deletions test/test_reduction.py
Expand Up @@ -55,7 +55,7 @@ def test_reduction(ctx_factory, grid_shape, proc_shape, dtype, op,
if pass_grid_dims:
reducer = ps.Reduction(mpi, reducers, rank_shape=rank_shape,
tmp_instructions=tmp_insns,
grid_size=np.product(grid_shape))
grid_size=np.prod(grid_shape))
else:
reducer = ps.Reduction(mpi, reducers, tmp_instructions=tmp_insns)

Expand All @@ -69,7 +69,7 @@ def test_reduction(ctx_factory, grid_shape, proc_shape, dtype, op,

avg_test = reducer.reduce_array(f / 2 + .31, op)
if op == "avg":
avg_test /= np.product(grid_shape)
avg_test /= np.prod(grid_shape)

rtol = 5e-14 if dtype == np.float64 else 1e-5
assert np.allclose(avg, avg_test, rtol=rtol, atol=0), \
Expand Down Expand Up @@ -109,7 +109,7 @@ def test_reduction_with_new_shape(ctx_factory, grid_shape, proc_shape, dtype, op

avg_test = reducer.reduce_array(f, op)
if op == "avg":
avg_test /= np.product(grid_shape)
avg_test /= np.prod(grid_shape)

rtol = 5e-14 if dtype == np.float64 else 1e-5
assert np.allclose(avg, avg_test, rtol=rtol, atol=0), \
Expand All @@ -124,7 +124,7 @@ def test_reduction_with_new_shape(ctx_factory, grid_shape, proc_shape, dtype, op

avg_test = reducer.reduce_array(f, op)
if op == "avg":
avg_test /= np.product(grid_shape)
avg_test /= np.prod(grid_shape)

rtol = 5e-14 if dtype == np.float64 else 1e-5
assert np.allclose(avg, avg_test, rtol=rtol, atol=0), \
Expand All @@ -150,7 +150,7 @@ def test_field_statistics(ctx_factory, grid_shape, proc_shape, dtype, _grid_shap

if pass_grid_dims:
statistics = ps.FieldStatistics(mpi, h, rank_shape=rank_shape,
grid_size=np.product(grid_shape))
grid_size=np.prod(grid_shape))
else:
statistics = ps.FieldStatistics(mpi, h)

Expand All @@ -163,10 +163,10 @@ def test_field_statistics(ctx_factory, grid_shape, proc_shape, dtype, _grid_shap

f_h = f.get()
rank_sum = np.sum(f_h[..., h:-h, h:-h, h:-h], axis=(-3, -2, -1))
avg_test = mpi.allreduce(rank_sum) / np.product(grid_shape)
avg_test = mpi.allreduce(rank_sum) / np.prod(grid_shape)

rank_sum = np.sum(f_h[..., h:-h, h:-h, h:-h]**2, axis=(-3, -2, -1))
var_test = mpi.allreduce(rank_sum) / np.product(grid_shape) - avg_test**2
var_test = mpi.allreduce(rank_sum) / np.prod(grid_shape) - avg_test**2

rtol = 5e-14 if dtype == np.float64 else 1e-5

Expand Down
2 changes: 1 addition & 1 deletion test/test_relax.py
Expand Up @@ -54,7 +54,7 @@ def test_relax(ctx_factory, grid_shape, proc_shape, h, dtype, Solver, timing=Fal
fft = ps.DFT(mpi, ctx, queue, grid_shape, dtype)
spectra = ps.PowerSpectra(mpi, fft, (dk,)*3, L**3)
statistics = ps.FieldStatistics(mpi, h, rank_shape=rank_shape,
grid_size=np.product(grid_shape))
grid_size=np.prod(grid_shape))

def get_laplacian(f):
from pystella.derivs import _lap_coefs, centered_diff
Expand Down
6 changes: 3 additions & 3 deletions test/test_spectra.py
Expand Up @@ -61,10 +61,10 @@ def test_spectra(ctx_factory, grid_shape, proc_shape, dtype, L, timing=False):
L = L or (3, 5, 7)
dk = tuple(2 * np.pi / Li for Li in L)
cdtype = fft.cdtype
spec = ps.PowerSpectra(mpi, fft, dk, np.product(L), bin_width=min(dk)+.001)
spec = ps.PowerSpectra(mpi, fft, dk, np.prod(L), bin_width=min(dk)+.001)
# FIXME: bin_width=min(dk) sometimes disagrees to O(.1%) with numpy...

assert int(np.sum(spec.bin_counts)) == np.product(grid_shape), \
assert int(np.sum(spec.bin_counts)) == np.prod(grid_shape), \
"bin counts don't sum to total number of points/modes"

k_power = 2.
Expand Down Expand Up @@ -132,7 +132,7 @@ def test_pol_spectra(ctx_factory, grid_shape, proc_shape, dtype, timing=False):
dk = tuple(2 * np.pi / Li for Li in L)
dx = tuple(Li / Ni for Li, Ni in zip(L, grid_shape))
cdtype = fft.cdtype
spec = ps.PowerSpectra(mpi, fft, dk, np.product(L))
spec = ps.PowerSpectra(mpi, fft, dk, np.prod(L))

k_power = 2.

Expand Down

0 comments on commit 27448e1

Please sign in to comment.