def __add__(self, other):
assert isinstance(other, ShapeFunction), "Can only add other shape function"
assert self.name == other.name, "Cannot add shapes of different features"
new_splits = self.splits.copy()
new_vals = self.values.copy()
for split, val in zip(other.splits, other.values):
idx = np.searchsorted(new_splits, split, side='right')
new_val = val
if split in new_splits:
idx_2 = np.argwhere(new_splits == split)
new_vals[idx_2] = new_vals[idx_2] + new_val
elif idx == len(new_splits) and (~np.isposinf(split)):
new_splits = np.append(new_splits, split)
new_vals = np.append(new_vals, new_val)
elif np.isposinf(split):
new_vals[-1] = new_vals[-1] + new_val
else:
new_splits = np.insert(new_splits, idx, split)
new_vals = np.insert(new_vals, idx, new_val)
return ShapeFunction(new_splits, new_vals, self.name)
python类isposinf()的实例源码
def get_mask_invalid(matrix):
mask = np.isposinf(matrix) + np.isneginf(matrix) + np.isnan(matrix)
return mask
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
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)