Python numpy.isneginf() Examples

The following are 30 code examples of numpy.isneginf(). 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: Scalers.py    From scattertext with Apache License 2.0 6 votes vote down vote up
def scale_neg_1_to_1_with_zero_mean_log_abs_max(v):
	'''
	!!! not working
	'''
	df = pd.DataFrame({'v':v,
	                   'sign': (v > 0) * 2 - 1})
	df['lg'] = np.log(np.abs(v)) / np.log(1.96)
	df['exclude'] = (np.isinf(df.lg) | np.isneginf(df.lg))
	for mask in [(df['sign'] == -1) & (df['exclude'] == False),
	             (df['sign'] == 1) & (df['exclude'] == False)]:
		df[mask]['lg'] = df[mask]['lg'].max() - df[mask]['lg']
	df['lg'] *= df['sign']
	df['lg'] = df['lg'].fillna(0)
	print(df[df['exclude']]['lg'].values)
	#to_rescale = convention_df['lg'].reindex(v.index)
	df['to_out'] =  scale_neg_1_to_1_with_zero_mean_abs_max(df['lg'])
	print('right')
	print(df.sort_values(by='lg').iloc[:5])
	print(df.sort_values(by='lg').iloc[-5:])
	print('to_out')
	print(df.sort_values(by='to_out').iloc[:5])
	print(df.sort_values(by='to_out').iloc[-5:])
	print(len(df), len(df.dropna()))
	return df['to_out'] 
Example #2
Source File: common.py    From sparse with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def isneginf(x, out=None):
    """
    Test element-wise for negative 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.isneginf(x).todense()
    array([ True])

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

    return elemwise(lambda x, out=None, dtype=None: np.isneginf(x, out=out), x, out=out) 
Example #3
Source File: data_loader.py    From factorized with MIT License 6 votes vote down vote up
def load_covarep(truth_dict):
    for video_index in truth_dict:
        file_name = covarep_path + video_index + '.mat'
        fts = sio.loadmat(file_name)['features']
        #print fts.shape
        for seg_index in truth_dict[video_index]:
            for w in truth_dict[video_index][seg_index]['data']:
                start_frame = int(w['start_time_clip']*100)
                end_frame = int(w['end_time_clip']*100)
                ft = fts[start_frame:end_frame]
                if ft.shape[0] == 0:
                    avg_ft = np.zeros(ft.shape[1])
                else:
                    #print np.array(ft).shape
                    #print ft[0]
                    avg_ft = np.mean(ft,0)
                avg_ft[np.isnan(avg_ft)] = 0
                avg_ft[np.isneginf(avg_ft)] = 0
                w['covarep'] = avg_ft 
Example #4
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 #5
Source File: DNGR.py    From DNGR-Keras with MIT License 6 votes vote down vote up
def PPMI_matrix(M):

	M = scale_sim_mat(M)
	nm_nodes = len(M)

	col_s = np.sum(M, axis=0).reshape(1,nm_nodes)
	row_s = np.sum(M, axis=1).reshape(nm_nodes,1)
	D = np.sum(col_s)
	rowcol_s = np.dot(row_s,col_s)
	PPMI = np.log(np.divide(D*M,rowcol_s))
	PPMI[np.isnan(PPMI)] = 0.0
	PPMI[np.isinf(PPMI)] = 0.0
	PPMI[np.isneginf(PPMI)] = 0.0
	PPMI[PPMI<0] = 0.0

	return PPMI 
Example #6
Source File: data_loader.py    From MFN with MIT License 6 votes vote down vote up
def load_covarep(truth_dict):
    for video_index in truth_dict:
        file_name = covarep_path + video_index + '.mat'
        fts = sio.loadmat(file_name)['features']
        #print fts.shape
        for seg_index in truth_dict[video_index]:
            for w in truth_dict[video_index][seg_index]['data']:
                start_frame = int(w['start_time_clip']*100)
                end_frame = int(w['end_time_clip']*100)
                ft = fts[start_frame:end_frame]
                if ft.shape[0] == 0:
                    avg_ft = np.zeros(ft.shape[1])
                else:
                    #print np.array(ft).shape
                    #print ft[0]
                    avg_ft = np.mean(ft,0)
                avg_ft[np.isnan(avg_ft)] = 0
                avg_ft[np.isneginf(avg_ft)] = 0
                w['covarep'] = avg_ft 
Example #7
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 #8
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 #9
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 #10
Source File: util_lstm_seqlabel.py    From neural_wfst with MIT License 5 votes vote down vote up
def is_invalid(arr):
    return any([f(arr).any() for f in [numpy.isinf, numpy.isnan, numpy.isneginf]]) 
Example #11
Source File: dngr.py    From cogdl with MIT License 5 votes vote down vote up
def get_ppmi_matrix(self, mat):
        # Get Positive Pairwise Mutual Information(PPMI) matrix
        mat = self.random_surfing(mat)
        M = self.scale_matrix(mat)
        col_s = np.sum(M, axis=0).reshape(1, self.num_node)
        row_s = np.sum(M, axis=1).reshape(self.num_node, 1)
        D = np.sum(col_s)
        rowcol_s = np.dot(row_s, col_s)
        PPMI = np.log(np.divide(D * M, rowcol_s))
        
        PPMI[np.isnan(PPMI)] = 0.0
        PPMI[np.isinf(PPMI)] = 0.0
        PPMI[np.isneginf(PPMI)] = 0.0
        PPMI[PPMI < 0] = 0.0
        return PPMI 
Example #12
Source File: test_pdp_script.py    From neural_wfst with MIT License 5 votes vote down vote up
def tolerant_eq(a, b):
    return (True
            if (numpy.isinf(a) and numpy.isinf(b))
            or (numpy.isneginf(a) and numpy.isneginf(b))
            or abs(a-b) < 1e-10
            else False) 
Example #13
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 
Example #14
Source File: distributions.py    From Neuraxle with Apache License 2.0 5 votes vote down vote up
def min(self):
        """
        Calculate minimum value that can be sampled in the quanitzed version of the distribution.

        :return: minimal value return from distribution.
        """
        hd_min = self.hd.min()
        if np.isneginf(hd_min):
            return hd_min
        return round(hd_min) 
Example #15
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_isneginf(self):
        self.check(np.isneginf) 
Example #16
Source File: utils.py    From scvelo with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_mean_var(X, ignore_zeros=False, perc=None):
    data = X.data if issparse(X) else X
    mask_nans = np.isnan(data) | np.isinf(data) | np.isneginf(data)

    n_nonzeros = (X != 0).sum(0)
    n_counts = n_nonzeros if ignore_zeros else X.shape[0]

    if mask_nans.sum() > 0:
        if issparse(X):
            data[np.isnan(data) | np.isinf(data) | np.isneginf(data)] = 0
            n_nans = n_nonzeros - (X != 0).sum(0)
        else:
            X[mask_nans] = 0
            n_nans = mask_nans.sum(0)
        n_counts -= n_nans

    if perc is not None:
        if np.size(perc) < 2:
            perc = [perc, 100] if perc < 50 else [0, perc]
        lb, ub = np.percentile(data, perc)
        data = np.clip(data, lb, ub)

    if issparse(X):
        mean = (X.sum(0) / n_counts).A1
        mean_sq = (X.multiply(X).sum(0) / n_counts).A1
    else:
        mean = X.sum(0) / n_counts
        mean_sq = np.multiply(X, X).sum(0) / n_counts
    n_cells = np.clip(X.shape[0], 2, None)  # to avoid division by zero
    var = (mean_sq - mean ** 2) * (n_cells / (n_cells - 1))

    mean = np.nan_to_num(mean)
    var = np.nan_to_num(var)
    return mean, var 
Example #17
Source File: rank_velocity_genes.py    From scvelo with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_mean_var(X, ignore_zeros=False, perc=None):
    data = X.data if issparse(X) else X
    mask_nans = np.isnan(data) | np.isinf(data) | np.isneginf(data)

    n_nonzeros = (X != 0).sum(0)
    n_counts = n_nonzeros if ignore_zeros else X.shape[0]

    if mask_nans.sum() > 0:
        if issparse(X):
            data[np.isnan(data) | np.isinf(data) | np.isneginf(data)] = 0
            n_nans = n_nonzeros - (X != 0).sum(0)
        else:
            X[mask_nans] = 0
            n_nans = mask_nans.sum(0)
        n_counts -= n_nans

    if perc is not None:
        if np.size(perc) < 2:
            perc = [perc, 100] if perc < 50 else [0, perc]
        lb, ub = np.percentile(data, perc)
        data = np.clip(data, lb, ub)

    if issparse(X):
        mean = (X.sum(0) / n_counts).A1
        mean_sq = (X.multiply(X).sum(0) / n_counts).A1
    else:
        mean = X.sum(0) / n_counts
        mean_sq = np.multiply(X, X).sum(0) / n_counts
    n_cells = np.clip(X.shape[0], 2, None)  # to avoid division by zero
    var = (mean_sq - mean ** 2) * (n_cells / (n_cells - 1))

    mean = np.nan_to_num(mean)
    var = np.nan_to_num(var)
    return mean, var 
Example #18
Source File: utilities.py    From tensorprob with MIT License 5 votes vote down vote up
def set_logp_to_neg_inf(X, logp, bounds):
    """Set `logp` to negative infinity when `X` is outside the allowed bounds.

    # Arguments
        X: tensorflow.Tensor
            The variable to apply the bounds to
        logp: tensorflow.Tensor
            The log probability corrosponding to `X`
        bounds: list of `Region` objects
            The regions corrosponding to allowed regions of `X`

    # Returns
        logp: tensorflow.Tensor
            The newly bounded log probability
    """
    conditions = []
    for l, u in bounds:
        lower_is_neg_inf = not isinstance(l, tf.Tensor) and np.isneginf(l)
        upper_is_pos_inf = not isinstance(u, tf.Tensor) and np.isposinf(u)

        if not lower_is_neg_inf and upper_is_pos_inf:
            conditions.append(tf.greater(X, l))
        elif lower_is_neg_inf and not upper_is_pos_inf:
            conditions.append(tf.less(X, u))
        elif not (lower_is_neg_inf or upper_is_pos_inf):
            conditions.append(tf.logical_and(tf.greater(X, l), tf.less(X, u)))

    if len(conditions) > 0:
        is_inside_bounds = conditions[0]
        for condition in conditions[1:]:
            is_inside_bounds = tf.logical_or(is_inside_bounds, condition)

        logp = tf.select(
            is_inside_bounds,
            logp,
            tf.fill(tf.shape(X), config.dtype(-np.inf))
        )

    return logp 
Example #19
Source File: various.py    From madminer with MIT License 5 votes vote down vote up
def sanitize_array(array, replace_nan=0.0, replace_inf=0.0, replace_neg_inf=0.0, min_value=None, max_value=None):
    array[np.isneginf(array)] = replace_neg_inf
    array[np.isinf(array)] = replace_inf
    array[np.isnan(array)] = replace_nan

    if min_value is not None or max_value is not None:
        array = np.clip(array, min_value, max_value)

    return array 
Example #20
Source File: imputer.py    From gordo with GNU Affero General Public License v3.0 5 votes vote down vote up
def _fill_minmax(self, X: np.ndarray):
        """
        Fill inf/-inf values in features of the array based on their min & max values.
        Compounded by the ``power`` value so long as the result doesn't exceed the
        current array's dtype's max/min. Otherwise it will use those.
        """

        # For each feature fill inf/-inf with pre-calculate fill values
        for feature_idx, (posinf_fill, neginf_fill) in enumerate(
            zip(self._posinf_fill_values, self._neginf_fill_values)
        ):
            X[:, feature_idx][np.isposinf(X[:, feature_idx])] = posinf_fill
            X[:, feature_idx][np.isneginf(X[:, feature_idx])] = neginf_fill
        return X 
Example #21
Source File: test_stattools.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_coint_identical_series():
    nobs = 200
    scale_e = 1
    np.random.seed(123)
    y = scale_e * np.random.randn(nobs)
    warnings.simplefilter('always', ColinearityWarning)
    with warnings.catch_warnings(record=True) as w:
        c = coint(y, y, trend="c", maxlag=0, autolag=None)
    assert_equal(len(w), 1)
    assert_equal(c[1], 0.0)
    assert_(np.isneginf(c[0])) 
Example #22
Source File: test_stattools.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_coint_perfect_collinearity():
    # test uses nearly perfect collinearity
    nobs = 200
    scale_e = 1
    np.random.seed(123)
    x = scale_e * np.random.randn(nobs, 2)
    y = 1 + x.sum(axis=1) + 1e-7 * np.random.randn(nobs)
    warnings.simplefilter('always', ColinearityWarning)
    with warnings.catch_warnings(record=True) as w:
        c = coint(y, x, trend="c", maxlag=0, autolag=None)
    assert_equal(c[1], 0.0)
    assert_(np.isneginf(c[0])) 
Example #23
Source File: test_tost.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def assert_almost_equal_inf(x, y, decimal=6, msg=None):
    x = np.atleast_1d(x)
    y = np.atleast_1d(y)
    assert_equal(np.isposinf(x), np.isposinf(y))
    assert_equal(np.isneginf(x), np.isneginf(y))
    assert_equal(np.isnan(x), np.isnan(y))
    assert_almost_equal(x[np.isfinite(x)], y[np.isfinite(y)]) 
Example #24
Source File: test_transformers.py    From gordo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_infimputer_fill_values():
    """
    InfImputer when fill values are provided
    """
    base_x = np.random.random((100, 10)).astype(np.float32)

    flat_view = base_x.ravel()

    pos_inf_idxs = [1, 2, 3, 4, 5]
    neg_inf_idxs = [6, 7, 8, 9, 10]

    flat_view[pos_inf_idxs] = np.inf
    flat_view[neg_inf_idxs] = -np.inf

    # Our base x should now be littered with pos/neg inf values
    assert np.isposinf(base_x).sum() > 0, "Expected some positive infinity values here"
    assert np.isneginf(base_x).sum() > 0, "Expected some negative infinity values here"

    imputer = InfImputer(inf_fill_value=9999.0, neg_inf_fill_value=-9999.0)
    X = imputer.fit_transform(base_x)
    np.equal(
        X.ravel()[[pos_inf_idxs]], np.array([9999.0, 9999.0, 9999.0, 9999.0, 9999.0])
    )
    np.equal(
        X.ravel()[[neg_inf_idxs]],
        np.array([-9999.0, -9999.0, -9999.0, -9999.0, -9999.0]),
    ) 
Example #25
Source File: imputer.py    From gordo with GNU Affero General Public License v3.0 5 votes vote down vote up
def transform(self, X: Union[pd.DataFrame, np.ndarray], y=None):

        # Ensure we're dealing with numpy array if it's a dataframe or similar
        X = X.values if hasattr(X, "values") else X

        # Apply specific fill values if provided.
        if self.inf_fill_value is not None:
            X[np.isposinf(X)] = self.inf_fill_value
        if self.neg_inf_fill_value is not None:
            X[np.isneginf(X)] = self.neg_inf_fill_value

        # May still be left over infs, if only one fill value was supplied for example
        if self.strategy is not None:
            return getattr(self, f"_fill_{self.strategy}")(X)
        return X 
Example #26
Source File: imputer.py    From gordo with GNU Affero General Public License v3.0 5 votes vote down vote up
def _fill_extremes(self, X: np.ndarray):
        """
        Fill negative and postive infs with their dtype's min/max values
        """
        X[np.isposinf(X)] = np.finfo(X.dtype).max
        X[np.isneginf(X)] = np.finfo(X.dtype).min
        return X 
Example #27
Source File: terminate_on_nan.py    From mljar-supervised with MIT License 5 votes vote down vote up
def on_iteration_end(self, iter_cnt, data):
        loss_train = 0
        if data.get("y_train_predicted") is not None:
            loss_train = self.metric(
                data.get("y_train_true"), data.get("y_train_predicted")
            )
        loss_validation = self.metric(
            data.get("y_validation_true"), data.get("y_validation_predicted")
        )

        for loss in [loss_train, loss_validation]:
            if np.isnan(loss) or np.isinf(loss) or np.isneginf(loss):
                self.learner.stop_training = True
                log.info("Terminating learning, invalid loss value") 
Example #28
Source File: nanops.py    From Computable with MIT License 5 votes vote down vote up
def _has_infs(result):
    if isinstance(result, np.ndarray):
        if result.dtype == 'f8':
            return lib.has_infs_f8(result)
        elif result.dtype == 'f4':
            return lib.has_infs_f4(result)
        return False
    return np.isinf(result) or np.isneginf(result) 
Example #29
Source File: utils.py    From smooth-topk with MIT License 5 votes vote down vote up
def assert_all_close(tensor_1, tensor_2, rtol=1e-4, atol=1e-4):
    tensor_1 = to_numpy(tensor_1).astype(np.float64)
    tensor_2 = to_numpy(tensor_2).astype(np.float64)
    np.testing.assert_equal(np.isposinf(tensor_1),
                            np.isposinf(tensor_2))
    np.testing.assert_equal(np.isneginf(tensor_1),
                            np.isneginf(tensor_2))
    indices = np.isfinite(tensor_1)
    if indices.sum():
        tensor_1 = tensor_1[indices]
        tensor_2 = tensor_2[indices]
        err = np.max(np.abs(tensor_1 - tensor_2))
        err_msg = "Max abs error: {0:.3g}".format(err)
        np.testing.assert_allclose(tensor_1, tensor_2, rtol=rtol, atol=atol,
                                   err_msg=err_msg) 
Example #30
Source File: math_ops.py    From trax with Apache License 2.0 5 votes vote down vote up
def isneginf(x):
  return x == array_ops.full_like(x, -np.inf)