Python numpy.isneginf() 使用实例

The following are code examples for showing how to use . They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don’t like. You can also save this page to your account.

Example 1

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 2

def heatmap (d, bins=(100, 100), smoothing=1.3, cmap='jet'):
  def getx (pt):
    return pt.coords[0][0]

  def gety (pt):
    return pt.coords[0][1]

  x = list(d.geometry.apply(getx))
  y = list(d.geometry.apply(gety))
  heatmap, xedges, yedges = np.histogram2d(y, x, bins=bins)
  extent = [yedges[0], yedges[-1], xedges[-1], xedges[0]]  # bin edges along the x and y dimensions, ordered

  # why are we taking log?
  logheatmap = np.log(heatmap)
  logheatmap[np.isneginf(logheatmap)] = 0
  logheatmap = ndimage.filters.gaussian_filter(logheatmap, smoothing, mode='nearest')


  return (logheatmap, extent) 

Example 3

def test_lnprob_does_not_raise_on_LinAlgError(datasets_db):
    """lnprob() should catch LinAlgError raised by equilibrium and return -np.inf"""
    datasets_db.insert(zpf_json)
    res = lnprob([10], comps=['CU','MG', 'VA'], dbf=dbf,
                 phases=['LIQUID', 'FCC_A1', 'HCP_A3', 'LAVES_C15', 'CUMG2'],
                 datasets=datasets_db, symbols_to_fit=['VV0001'], phase_models=None, scheduler=None)
    assert np.isneginf(res) 

Example 4

def test_lnprob_does_not_raise_on_ValueError(datasets_db):
    """lnprob() should catch ValueError raised by equilibrium and return -np.inf"""
    datasets_db.insert(zpf_json)
    res = lnprob([10], comps=['CU','MG', 'VA'], dbf=dbf,
                 phases=['LIQUID', 'FCC_A1', 'HCP_A3', 'LAVES_C15', 'CUMG2'],
                 datasets=datasets_db, symbols_to_fit=['VV0001'], phase_models=None, scheduler=None)
    assert np.isneginf(res) 

Example 5

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 6

def is_invalid(arr):
    return any([f(arr).any() for f in [numpy.isinf, numpy.isnan, numpy.isneginf]]) 

Example 7

def get_mask_invalid(matrix):
    mask = np.isposinf(matrix) + np.isneginf(matrix) + np.isnan(matrix)
    return mask 

Example 8

def test_known_value_0_0(self):
        x = lin2db(0.0)
        self.assertTrue(np.isneginf(x)) 

Example 9

def test_known_value_0_0(self):
        x = pow2db(0.0)
        self.assertTrue(np.isneginf(x)) 

Example 10

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 11

def modified_topology_features(ds, seg_id, use_2d_edges):
    modified_features = ds.topology_features(seg_id, use_2d_edges)
    if not ds.defect_slices:
        return modified_features

    skip_edges   = get_skip_edges(  ds, seg_id)
    skip_ranges  = get_skip_ranges( ds, seg_id)
    skip_starts  = get_skip_starts( ds, seg_id)
    delete_edge_ids = get_delete_edge_ids(ds, seg_id)

    # delete all features corresponding to delete - edges
    modified_features = np.delete(modified_features, delete_edge_ids, axis = 0)

    # get topo features for the new skip edges
    seg = ds.seg(seg_id)
    lower_slices  = np.unique(skip_starts)
    skip_edge_pairs_to_slice = {z : skip_edges[skip_starts == z] for z in lower_slices}
    skip_edge_indices_to_slice = {z : np.where(skip_starts == z)[0] for z in lower_slices}
    skip_edge_ranges_to_slice  = {z : skip_ranges[skip_starts == z] for z in lower_slices}

    n_feats = modified_features.shape[1]
    skip_topo_features = np.zeros( (skip_edges.shape[0], n_feats) )

    for z in lower_slices:
        _get_skip_topo_features_for_slices(
                z,
                seg,
                skip_edge_pairs_to_slice[z],
                skip_edge_ranges_to_slice[z],
                skip_edge_indices_to_slice[z],
                use_2d_edges,
                skip_topo_features)

    skip_topo_features[np.isinf(skip_topo_features)] = 0.
    skip_topo_features[np.isneginf(skip_topo_features)] = 0.
    skip_topo_features = np.nan_to_num(skip_topo_features)
    assert skip_topo_features.shape[1] == modified_features.shape[1]
    return np.concatenate([modified_features, skip_topo_features],axis = 0)


# the last argument is only for caching results with different features correctly 

Example 12

def _numpy(self, data, weights, shape):
        q = self.quantity(data)
        self._checkNPQuantity(q, shape)
        self._checkNPWeights(weights, shape)
        weights = self._makeNPWeights(weights, shape)
        newentries = weights.sum()

        import numpy

        selection = numpy.isnan(q)
        numpy.bitwise_not(selection, selection)
        subweights = weights.copy()
        subweights[selection] = 0.0
        self.nanflow._numpy(data, subweights, shape)

        # switch to float here like in bin.py else numpy throws
        # TypeError on trivial integer cases such as:
        # >>> q = numpy.array([1,2,3,4])
        # >>> np.divide(q,1,q)
        # >>> np.floor(q,q)
        q = numpy.array(q, dtype=numpy.float64)
        neginfs = numpy.isneginf(q)
        posinfs = numpy.isposinf(q)

        numpy.subtract(q, self.origin, q)
        numpy.divide(q, self.binWidth, q)
        numpy.floor(q, q)
        q = numpy.array(q, dtype=numpy.int64)
        q[neginfs] = LONG_MINUSINF
        q[posinfs] = LONG_PLUSINF

        selected = q[weights > 0.0]

        selection = numpy.empty(q.shape, dtype=numpy.bool)
        for index in numpy.unique(selected):
            if index != LONG_NAN:
                bin = self.bins.get(index)
                if bin is None:
                    bin = self.value.zero()
                    self.bins[index] = bin

                numpy.not_equal(q, index, selection)
                subweights[:] = weights
                subweights[selection] = 0.0
                bin._numpy(data, subweights, shape)

        # no possibility of exception from here on out (for rollback)
        self.entries += float(newentries) 

Example 13

def _propose_anchors(self, num_anchors):
        if num_anchors != 2:
            raise Exception('PointInformedSplitMergeSetupKernel only works for 2 anchors')

        anchor_1 = np.random.randint(0, self.num_data_points)

        if anchor_1 not in self.data_to_clusters:
            self._set_data_to_clusters(anchor_1)

        log_p_anchor = self.data_to_clusters[anchor_1].copy()

        u = np.random.random()

        alpha = np.random.beta(1, 9) * 100

        if u <= 0.5:
            x = np.percentile(log_p_anchor, alpha)

            log_p_anchor[log_p_anchor > x] = float('-inf')

            log_p_anchor[log_p_anchor <= x] = 0

        else:
            x = np.percentile(log_p_anchor, 100 - alpha)

            log_p_anchor[log_p_anchor > x] = 0

            log_p_anchor[log_p_anchor <= x] = float('-inf')

        log_p_anchor[anchor_1] = float('-inf')

        if np.isneginf(np.max(log_p_anchor)):
            idx = np.arange(self.num_data_points)

            idx = list(idx)

            idx.remove(anchor_1)

            anchor_2 = np.random.choice(idx)

        else:
            idx = np.where(~np.isneginf(log_p_anchor))[0].flatten()

            anchor_2 = np.random.choice(idx)

        return anchor_1, anchor_2 

Example 14

def elliptical_slice(xx, log_like_fn, prior_chol, prior_mean, *log_like_args, **sampler_args):
    cur_log_like = sampler_args.get('cur_log_like', None)
    angle_range = sampler_args.get('angle_range', 0)

    if cur_log_like is None:
        cur_log_like = log_like_fn(xx, *log_like_args)

    if np.isneginf(cur_log_like):
        raise Exception("Elliptical Slice Sampler: initial logprob is -inf for inputs %s" % xx)
    if np.isnan(cur_log_like):
        raise Exception("Elliptical Slice Sampler: initial logprob is NaN for inputs %s" % xx)

    nu = np.dot(prior_chol, npr.randn(xx.shape[0])) # don't bother adding mean here, would just subtract it at update step
    hh = np.log(npr.rand()) + cur_log_like  
    # log likelihood threshold -- LESS THAN THE INITIAL LOG LIKELIHOOD

    # Set up a bracket of angles and pick a first proposal.
    # "phi = (theta'-theta)" is a change in angle.
    if angle_range <= 0:
        # Bracket whole ellipse with both edges at first proposed point
        phi = npr.rand()*2*math.pi
        phi_min = phi - 2*math.pi
        phi_max = phi
    else:
        # Randomly center bracket on current point
        phi_min = -angle_range*npr.rand();
        phi_max = phi_min + angle_range;
        phi = npr.rand()*(phi_max - phi_min) + phi_min;

    # Slice sampling loop
    while True:
        # Compute xx for proposed angle difference 
        # and check if it's on the slice
        xx_prop = (xx-prior_mean)*np.cos(phi) + nu*np.sin(phi) + prior_mean

        cur_log_like = log_like_fn(xx_prop, *log_like_args)
        
        if cur_log_like > hh:
            # New point is on slice, ** EXIT LOOP **
            return xx_prop, cur_log_like

        # Shrink slice to rejected point
        if phi > 0:
            phi_max = phi
        elif phi < 0:
            phi_min = phi
        else:
            sys.stderr.write('Initial x: %s\n' % xx)
            # sys.stderr.write('initial log like = %f\n' % initial_log_like)
            sys.stderr.write('Proposed x: %s\n' % xx_prop)
            sys.stderr.write('ESS log lik = %f\n' % cur_log_like)
            raise Exception('BUG DETECTED: Shrunk to current position '
                            'and still not acceptable.');

        # Propose new angle difference
        phi = npr.rand()*(phi_max - phi_min) + phi_min 

Example 15

def elliptical_slice(xx, log_like_fn, prior_chol, prior_mean, *log_like_args, **sampler_args):
    cur_log_like = sampler_args.get('cur_log_like', None)
    angle_range = sampler_args.get('angle_range', 0)

    if cur_log_like is None:
        cur_log_like = log_like_fn(xx, *log_like_args)

    if np.isneginf(cur_log_like):
        raise Exception("Elliptical Slice Sampler: initial logprob is -inf for inputs %s" % xx)
    if np.isnan(cur_log_like):
        raise Exception("Elliptical Slice Sampler: initial logprob is NaN for inputs %s" % xx)

    nu = np.dot(prior_chol, npr.randn(xx.shape[0])) # don't bother adding mean here, would just subtract it at update step
    hh = np.log(npr.rand()) + cur_log_like  
    # log likelihood threshold -- LESS THAN THE INITIAL LOG LIKELIHOOD

    # Set up a bracket of angles and pick a first proposal.
    # "phi = (theta'-theta)" is a change in angle.
    if angle_range <= 0:
        # Bracket whole ellipse with both edges at first proposed point
        phi = npr.rand()*2*math.pi
        phi_min = phi - 2*math.pi
        phi_max = phi
    else:
        # Randomly center bracket on current point
        phi_min = -angle_range*npr.rand();
        phi_max = phi_min + angle_range;
        phi = npr.rand()*(phi_max - phi_min) + phi_min;

    # Slice sampling loop
    while True:
        # Compute xx for proposed angle difference 
        # and check if it's on the slice
        xx_prop = (xx-prior_mean)*np.cos(phi) + nu*np.sin(phi) + prior_mean

        cur_log_like = log_like_fn(xx_prop, *log_like_args)
        
        if cur_log_like > hh:
            # New point is on slice, ** EXIT LOOP **
            return xx_prop, cur_log_like

        # Shrink slice to rejected point
        if phi > 0:
            phi_max = phi
        elif phi < 0:
            phi_min = phi
        else:
            sys.stderr.write('Initial x: %s\n' % xx)
            # sys.stderr.write('initial log like = %f\n' % initial_log_like)
            sys.stderr.write('Proposed x: %s\n' % xx_prop)
            sys.stderr.write('ESS log lik = %f\n' % cur_log_like)
            raise Exception('BUG DETECTED: Shrunk to current position '
                            'and still not acceptable.');

        # Propose new angle difference
        phi = npr.rand()*(phi_max - phi_min) + phi_min 

Example 16

def initnw(layer):
    """
    Nguyen-Widrow initialization function

    """
    ci = layer.ci
    cn = layer.cn
    w_fix = 0.7 * cn ** (1. / ci)
    w_rand = np.random.rand(cn, ci) * 2 - 1
    # Normalize
    if ci == 1:
        w_rand = w_rand / np.abs(w_rand)
    else:
        w_rand = np.sqrt(1. / np.square(w_rand).sum(axis=1).reshape(cn, 1)) * w_rand

    w = w_fix * w_rand
    b = np.array([0]) if cn == 1 else w_fix * np.linspace(-1, 1, cn) * np.sign(w[:, 0])

    # Scaleble to inp_active
    amin, amax  = layer.transf.inp_active
    amin = -1 if amin == -np.Inf else amin
    amax = 1 if amax == np.Inf else amax

    x = 0.5 * (amax - amin)
    y = 0.5 * (amax + amin)
    w = x * w
    b = x * b + y

    # Scaleble to inp_minmax
    minmax = layer.inp_minmax.copy()
    minmax[np.isneginf(minmax)] = -1
    minmax[np.isinf(minmax)] = 1

    x = 2. / (minmax[:, 1] - minmax[:, 0])
    y = 1. - minmax[:, 1] * x
    w = w * x
    b = np.dot(w, y) + b

    layer.np['w'][:] = w
    layer.np['b'][:] = b

    return 
点赞