From 034b3ce187cc6d9ede2fd474ed641dbc226ca89b Mon Sep 17 00:00:00 2001 From: Simon Perkins Date: Tue, 30 Jan 2024 16:54:27 +0200 Subject: [PATCH] Address warnings (#292) --- .github/workflows/ci.yml | 15 +++--- HISTORY.rst | 1 + .../tests/test_time_and_channel_averaging.py | 8 +-- africanus/experimental/rime/fused/core.py | 2 +- .../policies/baseline_transform_policies.py | 2 +- .../policies/convolution_policies.py | 2 +- .../policies/phase_transform_policies.py | 2 +- .../policies/stokes_conversion_policies.py | 6 +-- .../model/coherency/tests/test_convert.py | 4 +- africanus/model/shape/shapelets.py | 53 +++++++++---------- africanus/rime/zernike.py | 2 +- africanus/util/nvcc.py | 6 +-- setup.cfg | 2 +- 13 files changed, 52 insertions(+), 53 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index eeb138567..0b26cd4ef 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -41,11 +41,17 @@ jobs: with: fetch-depth: 1 + - name: Create a .env file + run: | + touch .env + echo "NUMBA_NRT_STATS=1" >> .env + echo "NUMBA_CAPTURED_ERRORS='new_style'" >> .env + - name: Install base codex-africanus run: pipenv install .[testing] - name: Run base test suite - run: pipenv run NUMBA_NRT_STATS=1 py.test -s -vvv africanus/ + run: pipenv run py.test -s -vvv africanus/ - name: List the measures directory run: curl ftp://ftp.astron.nl/outgoing/Measures/ > measures_dir.txt @@ -67,19 +73,16 @@ jobs: echo "measures.directory: ~/measures" > ~/.casarc - name: Install complete codex-africanus - # Need pyyaml for dask 2.17.0 - # See https://github.com/dask/dask/issues/6221 run: > pipenv install .[complete] - pyyaml git+https://gitlab.mpcdf.mpg.de/ift/nifty_gridder.git#egg=nifty-gridder - name: Log installed package versions run: pipenv graph - name: Run complete test suite - run: pipenv run NUMBA_NRT_STATS=1 py.test -s -vvv africanus/ + run: pipenv run py.test -s -vvv africanus/ deploy: needs: [test] @@ -94,7 +97,7 @@ jobs: - name: Set up Python 3.10 uses: actions/setup-python@v5.0.0 with: - python-version: 3.10 + python-version: '3.10' - name: Install latest setuptools, wheel, pip run: python3 -m pip install -U pip setuptools wheel diff --git a/HISTORY.rst b/HISTORY.rst index 5961793db..fa3d7091d 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -4,6 +4,7 @@ History X.Y.Z (YYYY-MM-DD) ------------------ +* Address warnings (:pr:`292`) * Update github action versions (:pr:`291`) * Deprecate python 3.8 support (:pr:`290`) * Use pre-commit hooks for auto-linting (:pr:`290`) diff --git a/africanus/averaging/tests/test_time_and_channel_averaging.py b/africanus/averaging/tests/test_time_and_channel_averaging.py index c8ebf53cb..dd8c9f45c 100644 --- a/africanus/averaging/tests/test_time_and_channel_averaging.py +++ b/africanus/averaging/tests/test_time_and_channel_averaging.py @@ -55,25 +55,25 @@ def interval(): @pytest.fixture def weight(time): shape = (time.shape[0], ncorr) - return np.arange(np.product(shape), dtype=np.float64).reshape(shape) + return np.arange(np.prod(shape), dtype=np.float64).reshape(shape) @pytest.fixture def sigma(time): shape = (time.shape[0], ncorr) - return np.arange(np.product(shape), dtype=np.float64).reshape(shape) + return np.arange(np.prod(shape), dtype=np.float64).reshape(shape) @pytest.fixture def weight_spectrum(time): shape = (time.shape[0], nchan, ncorr) - return np.arange(np.product(shape), dtype=np.float64).reshape(shape) + return np.arange(np.prod(shape), dtype=np.float64).reshape(shape) @pytest.fixture def sigma_spectrum(time): shape = (time.shape[0], nchan, ncorr) - return np.arange(np.product(shape), dtype=np.float64).reshape(shape) + return np.arange(np.prod(shape), dtype=np.float64).reshape(shape) @pytest.fixture diff --git a/africanus/experimental/rime/fused/core.py b/africanus/experimental/rime/fused/core.py index ed4b9c1ca..661dcfccf 100644 --- a/africanus/experimental/rime/fused/core.py +++ b/africanus/experimental/rime/fused/core.py @@ -37,7 +37,7 @@ def rime(*args): def rime_impl(*args): raise NotImplementedError - @overload(rime_impl, jit_options=JIT_OPTIONS) + @overload(rime_impl, jit_options=JIT_OPTIONS, prefer_literal=True) def nb_rime(*args): if not len(args) > 0: raise TypeError( diff --git a/africanus/gridding/perleypolyhedron/policies/baseline_transform_policies.py b/africanus/gridding/perleypolyhedron/policies/baseline_transform_policies.py index 0ea9d1642..57f17c8be 100644 --- a/africanus/gridding/perleypolyhedron/policies/baseline_transform_policies.py +++ b/africanus/gridding/perleypolyhedron/policies/baseline_transform_policies.py @@ -83,7 +83,7 @@ def policy(uvw, ra0, dec0, ra, dec, policy_type): pass -@overload(policy, inline="always") +@overload(policy, inline="always", prefer_literal=True) def policy_impl(uvw, ra0, dec0, ra, dec, policy_type): if policy_type.literal_value == "None": return uvw_norotate diff --git a/africanus/gridding/perleypolyhedron/policies/convolution_policies.py b/africanus/gridding/perleypolyhedron/policies/convolution_policies.py index 2093be11c..4a1152640 100644 --- a/africanus/gridding/perleypolyhedron/policies/convolution_policies.py +++ b/africanus/gridding/perleypolyhedron/policies/convolution_policies.py @@ -349,7 +349,7 @@ def policy( pass -@overload(policy, inline="always") +@overload(policy, inline="always", prefer_literal=True) def policy_impl( scaled_u, scaled_v, diff --git a/africanus/gridding/perleypolyhedron/policies/phase_transform_policies.py b/africanus/gridding/perleypolyhedron/policies/phase_transform_policies.py index b42e490d1..1b6ab6175 100644 --- a/africanus/gridding/perleypolyhedron/policies/phase_transform_policies.py +++ b/africanus/gridding/perleypolyhedron/policies/phase_transform_policies.py @@ -38,7 +38,7 @@ def policy(vis, uvw, lambdas, ra0, dec0, ra, dec, policy_type, phasesign=1.0): pass -@overload(policy, inline="always") +@overload(policy, inline="always", prefer_literal=True) def policy_impl(vis, uvw, lambdas, ra0, dec0, ra, dec, policy_type, phasesign=1.0): if policy_type.literal_value == "None" or policy_type.literal_value is None: return phase_norotate diff --git a/africanus/gridding/perleypolyhedron/policies/stokes_conversion_policies.py b/africanus/gridding/perleypolyhedron/policies/stokes_conversion_policies.py index 873fd744d..c1e9d94c9 100644 --- a/africanus/gridding/perleypolyhedron/policies/stokes_conversion_policies.py +++ b/africanus/gridding/perleypolyhedron/policies/stokes_conversion_policies.py @@ -5,7 +5,7 @@ def stokes2corr(vis_in, vis_out, policy_type): pass -@overload(stokes2corr, inline="always") +@overload(stokes2corr, inline="always", prefer_literal=True) def stokes2corrimpl(vis_in, vis_out, policy_type): if policy_type.literal_value == "XXYY_FROM_I": @@ -143,7 +143,7 @@ def corr2stokes(vis_in, policy_type): pass -@overload(corr2stokes, inline="always") +@overload(corr2stokes, inline="always", prefer_literal=True) def corr2stokesimpl(vis_in, policy_type): if policy_type.literal_value == "I_FROM_XXYY": return lambda vis_in, policy_type: (vis_in[0] + vis_in[1]) * 0.5 @@ -183,7 +183,7 @@ def ncorr_out(policy_type): pass -@overload(ncorr_out, inline="always") +@overload(ncorr_out, inline="always", prefer_literal=True) def ncorr_outimpl(policy_type): if policy_type.literal_value == "XXYY_FROM_I": return lambda policy_type: 2 diff --git a/africanus/model/coherency/tests/test_convert.py b/africanus/model/coherency/tests/test_convert.py index 490d45231..f509c236d 100644 --- a/africanus/model/coherency/tests/test_convert.py +++ b/africanus/model/coherency/tests/test_convert.py @@ -41,11 +41,11 @@ def visibility_factory(vis_shape, input_shape, in_type, backend="numpy", **kwarg shape = vis_shape + input_shape if backend == "numpy": - vis = np.arange(1.0, np.product(shape) + 1.0) + vis = np.arange(1.0, np.prod(shape) + 1.0) vis = vis.reshape(shape) elif backend == "dask": da = pytest.importorskip("dask.array") - vis = da.arange(1.0, np.product(shape) + 1.0, chunks=np.product(shape)) + vis = da.arange(1.0, np.prod(shape) + 1.0, chunks=np.prod(shape)) vis = vis.reshape(shape) vis = vis.rechunk(kwargs["vis_chunks"] + input_shape) else: diff --git a/africanus/model/shape/shapelets.py b/africanus/model/shape/shapelets.py index 3a7881f2e..72599120f 100644 --- a/africanus/model/shape/shapelets.py +++ b/africanus/model/shape/shapelets.py @@ -27,25 +27,21 @@ def factorial(n): @numba.jit(nogil=True, nopython=True, cache=True) -def basis_function(n, xx, beta, fourier=False, delta_x=-1): - if fourier: - x = 2 * np.pi * xx - scale = 1.0 / beta - else: - x = xx - scale = beta +def real_basis_function(n, xx, beta, delta_x=-1): + basis_component = 1.0 / np.sqrt(2.0**n * np.sqrt(np.pi) * factorial(n) * beta) + exponential_component = hermite(n, xx / beta) * np.exp(-(xx**2) / (2.0 * beta**2)) + return basis_component * exponential_component + + +@numba.jit(nogil=True, nopython=True, cache=True) +def complex_basis_function(n, xx, beta, delta_x=-1): + x = 2 * np.pi * xx + scale = 1.0 / beta basis_component = 1.0 / np.sqrt(2.0**n * np.sqrt(np.pi) * factorial(n) * scale) exponential_component = hermite(n, x / scale) * np.exp(-(x**2) / (2.0 * scale**2)) - if fourier: - return ( - 1.0j**n - * basis_component - * exponential_component - * np.sqrt(2 * np.pi) - / delta_x - ) - else: - return basis_component * exponential_component + return ( + 1.0j**n * basis_component * exponential_component * np.sqrt(2 * np.pi) / delta_x + ) @numba.jit(nogil=True, nopython=True, cache=True) @@ -96,8 +92,8 @@ def shapelet(coords, frequency, coeffs, beta, delta_lm, dtype=np.complex128): 0 if coeffs[src][n1, n2] == 0 else coeffs[src][n1, n2] - * basis_function(n1, fu, beta_u, True, delta_x=delta_l) - * basis_function(n2, fv, beta_v, True, delta_x=delta_m) + * complex_basis_function(n1, fu, beta_u, delta_x=delta_l) + * complex_basis_function(n2, fv, beta_v, delta_x=delta_m) ) out_shapelets[row, chan, src] = tmp_shapelet return out_shapelets @@ -145,8 +141,8 @@ def shapelet_with_w_term( 0 if coeffs[src][n1, n2] == 0 else coeffs[src][n1, n2] - * basis_function(n1, fu, beta_u, True, delta_x=delta_l) - * basis_function(n2, fv, beta_v, True, delta_x=delta_m) + * complex_basis_function(n1, fu, beta_u, delta_x=delta_l) + * complex_basis_function(n2, fv, beta_v, delta_x=delta_m) ) w_term = phase_steer_and_w_correct((u, v, w), (l, m), frequency[chan]) out_shapelets[row, chan, src] = tmp_shapelet * w_term @@ -183,17 +179,16 @@ def shapelet_1d(u, coeffs, fourier, delta_x=1, beta=1.0): if delta_x is None: raise ValueError("You have to pass in a value for delta_x in Fourier mode") out = np.zeros(nrow, dtype=np.complex128) + for row, ui in enumerate(u): + for n, c in enumerate(coeffs): + out[row] += c * complex_basis_function(n, ui, beta, delta_x=delta_x) else: out = np.zeros(nrow, dtype=np.float64) - for row, ui in enumerate(u): - for n, c in enumerate(coeffs): - out[row] += c * basis_function( - n, ui, beta, fourier=fourier, delta_x=delta_x - ) - return out + for row, ui in enumerate(u): + for n, c in enumerate(coeffs): + out[row] += c * real_basis_function(n, ui, beta, delta_x=delta_x) - -# @numba.jit(nogil=True, nopython=True, cache=True) + return out def shapelet_2d(u, v, coeffs_l, fourier, delta_x=None, delta_y=None, beta=1.0): diff --git a/africanus/rime/zernike.py b/africanus/rime/zernike.py index 3a88a6c67..c3def23bf 100644 --- a/africanus/rime/zernike.py +++ b/africanus/rime/zernike.py @@ -123,7 +123,7 @@ def zernike_dde( npoly = coeffs.shape[-1] # Flatten correlation dimensions for numba function - fcorrs = np.product(corr_shape) + fcorrs = np.prod(corr_shape) ddes = np.empty((sources, times, ants, chans, fcorrs), coeffs.dtype) coeffs = coeffs.reshape((ants, chans, fcorrs, npoly)) diff --git a/africanus/util/nvcc.py b/africanus/util/nvcc.py index 638c2dc7b..6b996b85b 100644 --- a/africanus/util/nvcc.py +++ b/africanus/util/nvcc.py @@ -3,6 +3,7 @@ import ast import contextlib +import importlib.resources import logging import os import re @@ -12,7 +13,6 @@ import tempfile from os.path import join as pjoin -from pkg_resources import resource_filename import distutils from distutils import errors @@ -486,8 +486,8 @@ def compile_using_nvcc(source, options=None, arch=None, filename="kern.cu"): options += ["-cubin"] - cupy_path = resource_filename("cupy", pjoin("core", "include")) - settings["include_dirs"].append(cupy_path) + cupy_path = importlib.resources.files("cupy") / "core" / "include" + settings["include_dirs"].append(str(cupy_path)) with _tempdir() as tmpdir: tmpfile = pjoin(tmpdir, filename) diff --git a/setup.cfg b/setup.cfg index 44ee33bda..e205cc7e2 100644 --- a/setup.cfg +++ b/setup.cfg @@ -19,7 +19,7 @@ exclude = docs test = pytest [tool:pytest] -collect_ignore = ['setup.py'] +addopts = --ignore=setup.py [pycodestyle] ignore = E121,E123,E126,E133,E226,E241,E242,E704,W503,W504,E741