Skip to content

Commit

Permalink
feat(_fast_tools): move _invert_no_zero to caput and add deprecation …
Browse files Browse the repository at this point in the history
…warning
  • Loading branch information
ljgray committed Nov 8, 2022
1 parent 1603553 commit 2cafe5b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 47 deletions.
23 changes: 0 additions & 23 deletions draco/util/_fast_tools.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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]
33 changes: 9 additions & 24 deletions draco/util/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down

0 comments on commit 2cafe5b

Please sign in to comment.