From 2cafe5bdff5adca41d4a211d71adf54619015dc5 Mon Sep 17 00:00:00 2001 From: ljgray Date: Thu, 29 Sep 2022 14:51:04 -0700 Subject: [PATCH] feat(_fast_tools): move _invert_no_zero to caput and add deprecation warning --- draco/util/_fast_tools.pyx | 23 ----------------------- draco/util/tools.py | 33 +++++++++------------------------ 2 files changed, 9 insertions(+), 47 deletions(-) diff --git a/draco/util/_fast_tools.pyx b/draco/util/_fast_tools.pyx index 37772528f..cc6c46830 100644 --- a/draco/util/_fast_tools.pyx +++ b/draco/util/_fast_tools.pyx @@ -262,26 +262,3 @@ ctypedef fused real_or_complex: double complex float float complex - -@cython.wraparound(False) -@cython.boundscheck(False) -@cython.cdivision(True) -cpdef _invert_no_zero(real_or_complex [:] array, real_or_complex [:] out): - - cdef bint cond - cdef Py_ssize_t i = 0 - cdef Py_ssize_t n = array.shape[0] - cdef double thresh, ar, ai - if (real_or_complex is cython.doublecomplex) or (real_or_complex is cython.double): - thresh = 1.0 / DBL_MAX - else: - thresh = 1.0 / FLT_MAX - - if (real_or_complex is cython.doublecomplex) or (real_or_complex is cython.floatcomplex): - for i in prange(n, nogil=True): - cond = (fabs(array[i].real) < thresh) and (fabs(array[i].imag) < thresh) - out[i] = 0.0 if cond else 1.0 / array[i] - else: - for i in prange(n, nogil=True): - cond = fabs(array[i]) < thresh - out[i] = 0.0 if cond else 1.0 / array[i] \ No newline at end of file diff --git a/draco/util/tools.py b/draco/util/tools.py index 990c3b290..feb4b0166 100644 --- a/draco/util/tools.py +++ b/draco/util/tools.py @@ -6,7 +6,7 @@ import numpy as np from numpy.lib.recfunctions import structured_to_unstructured -from ._fast_tools import _calc_redundancy, _invert_no_zero +from ._fast_tools import _calc_redundancy def cmap(i, j, n): @@ -223,7 +223,7 @@ def apply_gain(vis, gain, axis=1, out=None, prod_map=None): return out -def invert_no_zero(x, out=None): +def invert_no_zero(*args, **kwargs): """Return the reciprocal, but ignoring zeros. Where `x != 0` return 1/x, or just return 0. Importantly this routine does @@ -240,29 +240,14 @@ def invert_no_zero(x, out=None): Return the reciprocal of x. Where possible the output has the same memory layout as the input, if this cannot be preserved the output is C-contiguous. """ - if not isinstance(x, (np.generic, np.ndarray)) or np.issubdtype( - x.dtype, np.integer - ): - with np.errstate(divide="ignore", invalid="ignore", over="ignore"): - return np.where(x == 0, 0.0, 1.0 / x) - - if out is not None: - if x.shape != out.shape: - raise ValueError( - f"Input and output arrays don't have same shape: {x.shape} != {out.shape}." - ) - else: - # This works even for MPIArrays, producing a correctly shaped MPIArray - out = np.empty_like(x, order="A") - - # In order to be able to flatten the arrays to do element by element operations, we - # need to ensure the inputs are numpy arrays, and so we take a view which will work - # even if `x` (and thus `out`) are MPIArray's - _invert_no_zero( - x.view(np.ndarray).ravel(order="A"), out.view(np.ndarray).ravel(order="A") - ) + from caput import tools + import warnings - return out + warnings.warn( + "Function invert_no_zero is deprecated - use 'caput.tools.invert_no_zero'", + category=DeprecationWarning, + ) + return tools.invert_no_zero(*args, **kwargs) def extract_diagonal(utmat, axis=1):