Python numpy.isposinf() Examples

The following are 30 code examples of numpy.isposinf(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module numpy , or try the search function .
Example #1
Source File: util.py    From gluon-ts with Apache License 2.0 6 votes vote down vote up
def jsonify_floats(json_object):
    """
    Traverses through the JSON object and converts non JSON-spec compliant
    floats(nan, -inf, inf) to their string representations.

    Parameters
    ----------
    json_object
        JSON object
    """
    if isinstance(json_object, dict):
        return {k: jsonify_floats(v) for k, v in json_object.items()}
    elif isinstance(json_object, list):
        return [jsonify_floats(item) for item in json_object]
    elif isinstance(json_object, float):
        if np.isnan(json_object):
            return "NaN"
        elif np.isposinf(json_object):
            return "Infinity"
        elif np.isneginf(json_object):
            return "-Infinity"
        return json_object
    return json_object 
Example #2
Source File: discretization.py    From nevergrad with MIT License 6 votes vote down vote up
def probabilities(self) -> np.ndarray:
        """Creates the probability matrix from the weights
        """
        axis = 1
        maxv = np.max(self.weights, axis=1, keepdims=True)
        hasposinf = np.isposinf(maxv)
        maxv[np.isinf(maxv)] = 0  # avoid indeterminations
        exp: np.ndarray = np.exp(self.weights - maxv)
        # deal with infinite positives special case
        # by ignoring (0 proba) non-infinte on same row
        if np.any(hasposinf):
            is_inf = np.isposinf(self.weights)
            is_ignored = np.logical_and(np.logical_not(is_inf), hasposinf)
            exp[is_inf] = 1
            exp[is_ignored] = 0
        # random choice if sums to 0
        sums0 = np.sum(exp, axis=axis) == 0
        exp[sums0, :] = 1
        exp /= np.sum(exp, axis=axis, keepdims=True)  # normalize
        return exp 
Example #3
Source File: test_node.py    From onnx-tensorflow with Apache License 2.0 6 votes vote down vote up
def test_is_inf(self):
    if legacy_opset_pre_ver(10):
      raise unittest.SkipTest("ONNX version {} doesn't support IsInf.".format(
          defs.onnx_opset_version()))
    input = np.array([-1.2, np.nan, np.inf, 2.8, np.NINF, np.inf],
                     dtype=np.float32)
    expected_output = {
        "node_def": np.isinf(input),
        "node_def_neg_false": np.isposinf(input),
        "node_def_pos_false": np.isneginf(input)
    }
    node_defs = {
        "node_def":
            helper.make_node("IsInf", ["X"], ["Y"]),
        "node_def_neg_false":
            helper.make_node("IsInf", ["X"], ["Y"], detect_negative=0),
        "node_def_pos_false":
            helper.make_node("IsInf", ["X"], ["Y"], detect_positive=0)
    }
    for key in node_defs:
      output = run_node(node_defs[key], [input])
      np.testing.assert_equal(output["Y"], expected_output[key]) 
Example #4
Source File: test_ufunclike.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_scalar(self):
        x = np.inf
        actual = np.isposinf(x)
        expected = np.True_
        assert_equal(actual, expected)
        assert_equal(type(actual), type(expected))

        x = -3.4
        actual = np.fix(x)
        expected = np.float64(-3.0)
        assert_equal(actual, expected)
        assert_equal(type(actual), type(expected))

        out = np.array(0.0)
        actual = np.fix(x, out=out)
        assert_(actual is out) 
Example #5
Source File: test_ufunclike.py    From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License 6 votes vote down vote up
def test_scalar(self):
        x = np.inf
        actual = np.isposinf(x)
        expected = np.True_
        assert_equal(actual, expected)
        assert_equal(type(actual), type(expected))

        x = -3.4
        actual = np.fix(x)
        expected = np.float64(-3.0)
        assert_equal(actual, expected)
        assert_equal(type(actual), type(expected))

        out = np.array(0.0)
        actual = np.fix(x, out=out)
        assert_(actual is out) 
Example #6
Source File: test_ufunclike.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_scalar(self):
        x = np.inf
        actual = np.isposinf(x)
        expected = np.True_
        assert_equal(actual, expected)
        assert_equal(type(actual), type(expected))

        x = -3.4
        actual = np.fix(x)
        expected = np.float64(-3.0)
        assert_equal(actual, expected)
        assert_equal(type(actual), type(expected))

        out = np.array(0.0)
        actual = np.fix(x, out=out)
        assert_(actual is out) 
Example #7
Source File: converters.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def output(self, value, mask):
        if mask:
            return self._null_output
        if np.isfinite(value):
            if not np.isscalar(value):
                value = value.dtype.type(value)
            result = self._output_format.format(value)
            if result.startswith('array'):
                raise RuntimeError()
            if (self._output_format[2] == 'r' and
                result.endswith('.0')):
                result = result[:-2]
            return result
        elif np.isnan(value):
            return 'NaN'
        elif np.isposinf(value):
            return '+InF'
        elif np.isneginf(value):
            return '-InF'
        # Should never raise
        vo_raise(f"Invalid floating point value '{value}'") 
Example #8
Source File: test_ufunclike.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_scalar(self):
        x = np.inf
        actual = np.isposinf(x)
        expected = np.True_
        assert_equal(actual, expected)
        assert_equal(type(actual), type(expected))

        x = -3.4
        actual = np.fix(x)
        expected = np.float64(-3.0)
        assert_equal(actual, expected)
        assert_equal(type(actual), type(expected))

        out = np.array(0.0)
        actual = np.fix(x, out=out)
        assert_(actual is out) 
Example #9
Source File: test_ufunclike.py    From coffeegrindsize with MIT License 6 votes vote down vote up
def test_scalar(self):
        x = np.inf
        actual = np.isposinf(x)
        expected = np.True_
        assert_equal(actual, expected)
        assert_equal(type(actual), type(expected))

        x = -3.4
        actual = np.fix(x)
        expected = np.float64(-3.0)
        assert_equal(actual, expected)
        assert_equal(type(actual), type(expected))

        out = np.array(0.0)
        actual = np.fix(x, out=out)
        assert_(actual is out) 
Example #10
Source File: test_ufunclike.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def test_scalar(self):
        x = np.inf
        actual = np.isposinf(x)
        expected = np.True_
        assert_equal(actual, expected)
        assert_equal(type(actual), type(expected))

        x = -3.4
        actual = np.fix(x)
        expected = np.float64(-3.0)
        assert_equal(actual, expected)
        assert_equal(type(actual), type(expected))

        out = np.array(0.0)
        actual = np.fix(x, out=out)
        assert_(actual is out) 
Example #11
Source File: test_ufunclike.py    From Mastering-Elasticsearch-7.0 with MIT License 6 votes vote down vote up
def test_scalar(self):
        x = np.inf
        actual = np.isposinf(x)
        expected = np.True_
        assert_equal(actual, expected)
        assert_equal(type(actual), type(expected))

        x = -3.4
        actual = np.fix(x)
        expected = np.float64(-3.0)
        assert_equal(actual, expected)
        assert_equal(type(actual), type(expected))

        out = np.array(0.0)
        actual = np.fix(x, out=out)
        assert_(actual is out) 
Example #12
Source File: test_ufunclike.py    From GraphicDesignPatternByPython with MIT License 6 votes vote down vote up
def test_scalar(self):
        x = np.inf
        actual = np.isposinf(x)
        expected = np.True_
        assert_equal(actual, expected)
        assert_equal(type(actual), type(expected))

        x = -3.4
        actual = np.fix(x)
        expected = np.float64(-3.0)
        assert_equal(actual, expected)
        assert_equal(type(actual), type(expected))

        out = np.array(0.0)
        actual = np.fix(x, out=out)
        assert_(actual is out) 
Example #13
Source File: test_basic.py    From GraphicDesignPatternByPython with MIT License 6 votes vote down vote up
def test_kl_div():
    def xfunc(x, y):
        if x < 0 or y < 0 or (y == 0 and x != 0):
            # extension of natural domain to preserve convexity
            return np.inf
        elif np.isposinf(x) or np.isposinf(y):
            # limits within the natural domain
            return np.inf
        elif x == 0:
            return y
        else:
            return special.xlogy(x, x/y) - x + y
    values = (0, 0.5, 1.0)
    signs = [-1, 1]
    arr = []
    for sgna, va, sgnb, vb in itertools.product(signs, values, signs, values):
        arr.append((sgna*va, sgnb*vb))
    z = np.array(arr, dtype=float)
    w = np.vectorize(xfunc, otypes=[np.float64])(z[:,0], z[:,1])
    assert_func_equal(special.kl_div, w, z, rtol=1e-13, atol=1e-13) 
Example #14
Source File: numutils.py    From cooltools with MIT License 6 votes vote down vote up
def fill_inf(arr, pos_value=0, neg_value=0, copy=True):
    """Replaces positive and negative infinity entries in an array with the
    provided values.

    Parameters
    ----------
    arr : np.array

    pos_value : float
        Fill value for np.inf

    neg_value : float
        Fill value for -np.inf

    copy : bool, optional
        If True, creates a copy of x, otherwise replaces values in-place.
        By default, True.

    """
    if copy:
        arr = arr.copy()
    arr[np.isposinf(arr)] = pos_value
    arr[np.isneginf(arr)] = neg_value
    return arr 
Example #15
Source File: common.py    From sparse with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def isposinf(x, out=None):
    """
    Test element-wise for positive infinity, return result as sparse ``bool`` array.

    Parameters
    ----------
    x
        Input
    out, optional
        Output array

    Examples
    --------
    >>> import sparse
    >>> x = sparse.as_coo(np.array([np.inf]))
    >>> sparse.isposinf(x).todense()
    array([ True])

    See Also
    --------
    numpy.isposinf : The NumPy equivalent
    """
    from .core import elemwise

    return elemwise(lambda x, out=None, dtype=None: np.isposinf(x, out=out), x, out=out) 
Example #16
Source File: test_ufunclike.py    From mxnet-lambda with Apache License 2.0 6 votes vote down vote up
def test_scalar(self):
        x = np.inf
        actual = np.isposinf(x)
        expected = np.True_
        assert_equal(actual, expected)
        assert_equal(type(actual), type(expected))

        x = -3.4
        actual = np.fix(x)
        expected = np.float64(-3.0)
        assert_equal(actual, expected)
        assert_equal(type(actual), type(expected))

        out = np.array(0.0)
        actual = np.fix(x, out=out)
        assert_(actual is out) 
Example #17
Source File: test_ufunclike.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_scalar(self):
        x = np.inf
        actual = np.isposinf(x)
        expected = np.True_
        assert_equal(actual, expected)
        assert_equal(type(actual), type(expected))

        x = -3.4
        actual = np.fix(x)
        expected = np.float64(-3.0)
        assert_equal(actual, expected)
        assert_equal(type(actual), type(expected))

        out = np.array(0.0)
        actual = np.fix(x, out=out)
        assert_(actual is out) 
Example #18
Source File: test_ufunclike.py    From pySINDy with MIT License 6 votes vote down vote up
def test_scalar(self):
        x = np.inf
        actual = np.isposinf(x)
        expected = np.True_
        assert_equal(actual, expected)
        assert_equal(type(actual), type(expected))

        x = -3.4
        actual = np.fix(x)
        expected = np.float64(-3.0)
        assert_equal(actual, expected)
        assert_equal(type(actual), type(expected))

        out = np.array(0.0)
        actual = np.fix(x, out=out)
        assert_(actual is out) 
Example #19
Source File: test_quantity_non_ufuncs.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_isposinf(self):
        self.check(np.isposinf) 
Example #20
Source File: test_ufunclike.py    From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License 5 votes vote down vote up
def test_isposinf(self):
        a = nx.array([nx.inf, -nx.inf, nx.nan, 0.0, 3.0, -3.0])
        out = nx.zeros(a.shape, bool)
        tgt = nx.array([True, False, False, False, False, False])

        res = ufl.isposinf(a)
        assert_equal(res, tgt)
        res = ufl.isposinf(a, out)
        assert_equal(res, tgt)
        assert_equal(out, tgt) 
Example #21
Source File: test_ufunclike.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def test_isposinf(self):
        a = nx.array([nx.inf, -nx.inf, nx.nan, 0.0, 3.0, -3.0])
        out = nx.zeros(a.shape, bool)
        tgt = nx.array([True, False, False, False, False, False])

        res = ufl.isposinf(a)
        assert_equal(res, tgt)
        res = ufl.isposinf(a, out)
        assert_equal(res, tgt)
        assert_equal(out, tgt)

        a = a.astype(np.complex)
        with assert_raises(TypeError):
            ufl.isposinf(a) 
Example #22
Source File: test_ufunclike.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_deprecated(self):
        # NumPy 1.13.0, 2017-04-26
        assert_warns(DeprecationWarning, ufl.fix, [1, 2], y=nx.empty(2))
        assert_warns(DeprecationWarning, ufl.isposinf, [1, 2], y=nx.empty(2))
        assert_warns(DeprecationWarning, ufl.isneginf, [1, 2], y=nx.empty(2)) 
Example #23
Source File: test_ufunclike.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def test_deprecated(self):
        # NumPy 1.13.0, 2017-04-26
        assert_warns(DeprecationWarning, ufl.fix, [1, 2], y=nx.empty(2))
        assert_warns(DeprecationWarning, ufl.isposinf, [1, 2], y=nx.empty(2))
        assert_warns(DeprecationWarning, ufl.isneginf, [1, 2], y=nx.empty(2)) 
Example #24
Source File: test_ufunclike.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_isposinf(self):
        a = nx.array([nx.inf, -nx.inf, nx.nan, 0.0, 3.0, -3.0])
        out = nx.zeros(a.shape, bool)
        tgt = nx.array([True, False, False, False, False, False])

        res = ufl.isposinf(a)
        assert_equal(res, tgt)
        res = ufl.isposinf(a, out)
        assert_equal(res, tgt)
        assert_equal(out, tgt) 
Example #25
Source File: test_ufunclike.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_deprecated(self):
        # NumPy 1.13.0, 2017-04-26
        assert_warns(DeprecationWarning, ufl.fix, [1, 2], y=nx.empty(2))
        assert_warns(DeprecationWarning, ufl.isposinf, [1, 2], y=nx.empty(2))
        assert_warns(DeprecationWarning, ufl.isneginf, [1, 2], y=nx.empty(2)) 
Example #26
Source File: test_ufunclike.py    From mxnet-lambda with Apache License 2.0 5 votes vote down vote up
def test_deprecated(self):
        # NumPy 1.13.0, 2017-04-26
        assert_warns(DeprecationWarning, ufl.fix, [1, 2], y=nx.empty(2))
        assert_warns(DeprecationWarning, ufl.isposinf, [1, 2], y=nx.empty(2))
        assert_warns(DeprecationWarning, ufl.isneginf, [1, 2], y=nx.empty(2)) 
Example #27
Source File: test_ufunclike.py    From mxnet-lambda with Apache License 2.0 5 votes vote down vote up
def test_isposinf(self):
        a = nx.array([nx.inf, -nx.inf, nx.nan, 0.0, 3.0, -3.0])
        out = nx.zeros(a.shape, bool)
        tgt = nx.array([True, False, False, False, False, False])

        res = ufl.isposinf(a)
        assert_equal(res, tgt)
        res = ufl.isposinf(a, out)
        assert_equal(res, tgt)
        assert_equal(out, tgt) 
Example #28
Source File: test_erf.py    From cupy with MIT License 5 votes vote down vote up
def test_erfcinv_behavior(self, dtype):
        a = cupy.empty((1,), dtype=dtype)

        a[:] = 2.0 + 1E-6
        a = cupyx.scipy.special.erfcinv(a)
        assert cupy.isnan(a)
        a[:] = 0.0 - 1E-6
        a = cupyx.scipy.special.erfcinv(a)
        assert cupy.isnan(a)
        a[:] = 0.0
        a = cupyx.scipy.special.erfcinv(a)
        assert numpy.isposinf(cupy.asnumpy(a))
        a[:] = 2.0
        a = cupyx.scipy.special.erfcinv(a)
        assert numpy.isneginf(cupy.asnumpy(a)) 
Example #29
Source File: test_ufunclike.py    From pySINDy with MIT License 5 votes vote down vote up
def test_deprecated(self):
        # NumPy 1.13.0, 2017-04-26
        assert_warns(DeprecationWarning, ufl.fix, [1, 2], y=nx.empty(2))
        assert_warns(DeprecationWarning, ufl.isposinf, [1, 2], y=nx.empty(2))
        assert_warns(DeprecationWarning, ufl.isneginf, [1, 2], y=nx.empty(2)) 
Example #30
Source File: distributions.py    From Neuraxle with Apache License 2.0 5 votes vote down vote up
def _get_sum_starting_info(limits):
    if np.isinf(limits[0]) and np.isinf(limits[1]):
        raise ValueError("Cannot calculate a sum on infinite terms.")
    if np.isposinf(limits[0]):
        starting_value = limits[1]
        stop_value = limits[0]
        method = "increasing"
    elif np.isposinf(limits[1]):
        starting_value = limits[0]
        stop_value = limits[1]
        method = "increasing"
    elif np.isneginf(limits[0]):
        starting_value = limits[1]
        stop_value = limits[0]
        method = "decreasing"
    elif np.isneginf(limits[1]):
        starting_value = limits[0]
        stop_value = limits[1]
        method = "decreasing"
    elif np.greater(limits[1], limits[0]):
        starting_value = limits[0]
        stop_value = limits[1]
        method = "increasing"
    else:
        starting_value = limits[1]
        stop_value = limits[0]
        method = "increasing"
    return method, starting_value, stop_value