本文整理汇总了Python中numpy.isnan函数的典型用法代码示例。如果您正苦于以下问题:Python isnan函数的具体用法?Python isnan怎么用?Python isnan使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了isnan函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _crop_out_special_values
def _crop_out_special_values(self, ws):
if ws.getNumberHistograms() != 1:
# Strip zeros is only possible on 1D workspaces
return
y_vals = ws.readY(0)
length = len(y_vals)
# Find the first non-zero value
start = 0
for i in range(0, length):
if not np.isnan(y_vals[i]) and not np.isinf(y_vals[i]):
start = i
break
# Now find the last non-zero value
stop = 0
length -= 1
for j in range(length, 0, -1):
if not np.isnan(y_vals[j]) and not np.isinf(y_vals[j]):
stop = j
break
# Find the appropriate X values and call CropWorkspace
x_vals = ws.readX(0)
start_x = x_vals[start]
# Make sure we're inside the bin that we want to crop
end_x = x_vals[stop + 1]
return self._crop_to_x_range(ws=ws,x_min=start_x, x_max=end_x)
示例2: reconstruct_coincidence
def reconstruct_coincidence(self, coincidence_events, station_numbers=None,
offsets=None, initial=None):
"""Reconstruct a single coincidence
:param coincidence_events: a coincidence list consisting of one
or more (station_number, event) tuples.
:param station_numbers: list of station numbers, to only use
events from those stations.
:param offsets: dictionary with detector offsets for each station.
These detector offsets should be relative to one
detector from a specific station.
:param initial: dictionary with already fitted shower parameters.
:return: list of theta, phi, and station numbers.
"""
if len(coincidence_events) < 1:
return nan, nan, []
if offsets is None:
offsets = {}
if initial is None:
initial = {}
# Subtract base timestamp to prevent loss of precision
ts0 = int(coincidence_events[0][1]['timestamp'])
ets0 = ts0 * int(1e9)
self.cluster.set_timestamp(ts0)
t, x, y, z, nums = ([], [], [], [], [])
offsets = self.get_station_offsets(coincidence_events, station_numbers,
offsets, ts0)
for station_number, event in coincidence_events:
if station_numbers is not None:
if station_number not in station_numbers:
continue
t_off = offsets.get(station_number, NO_OFFSET)
station = self.cluster.get_station(station_number)
t_detectors = relative_detector_arrival_times(event, ets0,
offsets=t_off,
station=station)
for t_detector, detector in zip(t_detectors, station.detectors):
if not isnan(t_detector):
dx, dy, dz = detector.get_coordinates()
t.append(t_detector)
x.append(dx)
y.append(dy)
z.append(dz)
if not all(isnan(t_detectors)):
nums.append(station_number)
if len(t) >= 3 and 'core_x' in initial and 'core_y' in initial:
theta, phi = self.curved.reconstruct_common(t, x, y, z, initial)
elif len(t) == 3:
theta, phi = self.direct.reconstruct_common(t, x, y, z, initial)
elif len(t) > 3:
theta, phi = self.fit.reconstruct_common(t, x, y, z, initial)
else:
theta, phi = (nan, nan)
return theta, phi, nums
示例3: responsetime
def responsetime(conn):
""" Determine the average response time for tasks in bins """
c = conn.cursor()
results = c.execute("""
select finished.time, event.time, finished.time - event.time as responsetime
from event
left join (select time, task_id from event where type_id=""" + taskid("run_task") + """) as finished
on event.task_id = finished.task_id
where event.type_id=""" + taskid("add_task")).fetchall()
results = np.matrix(results, dtype=float)
runtimes = results[:,2]
nones = runtimes == np.array(None)
(finished, nofinish) = (runtimes[~np.isnan(runtimes).all(axis=1)], runtimes[np.isnan(runtimes).any(axis=1)])
return {
"completion":{
"finished":finished.size,
"dnf":nofinish.size,
},
"response_times":{
"min":np.min(finished),
"mean":np.mean(finished),
"max":np.max(finished),
"std":np.std(finished)
}
}
示例4: test_align
def test_align(self):
left = create_test_data()
right = left.copy(deep=True)
right['dim3'] = ('dim3', list('cdefghijkl'))
right['var3'][:-2] = right['var3'][2:]
right['var3'][-2:] = np.random.randn(*right['var3'][-2:].shape)
intersection = list('cdefghij')
union = list('abcdefghijkl')
left2, right2 = align(left, right, join='inner')
self.assertArrayEqual(left2['dim3'], intersection)
self.assertDatasetIdentical(left2, right2)
left2, right2 = align(left, right, join='outer')
self.assertVariableEqual(left2['dim3'], right2['dim3'])
self.assertArrayEqual(left2['dim3'], union)
self.assertDatasetIdentical(left2.labeled(dim3=intersection),
right2.labeled(dim3=intersection))
self.assertTrue(np.isnan(left2['var3'][-2:]).all())
self.assertTrue(np.isnan(right2['var3'][:2]).all())
left2, right2 = align(left, right, join='left')
self.assertVariableEqual(left2['dim3'], right2['dim3'])
self.assertVariableEqual(left2['dim3'], left['dim3'])
self.assertDatasetIdentical(left2.labeled(dim3=intersection),
right2.labeled(dim3=intersection))
self.assertTrue(np.isnan(right2['var3'][:2]).all())
left2, right2 = align(left, right, join='right')
self.assertVariableEqual(left2['dim3'], right2['dim3'])
self.assertVariableEqual(left2['dim3'], right['dim3'])
self.assertDatasetIdentical(left2.labeled(dim3=intersection),
right2.labeled(dim3=intersection))
self.assertTrue(np.isnan(left2['var3'][-2:]).all())
示例5: __set_static_gaus_pmfs
def __set_static_gaus_pmfs(self):
if np.logical_not(self.off_buff.is_full()):
print "The long term buffer is not yet full. This may give undesirable results"
# median RSS of off-state buffer
cal_med = self.off_buff.get_no_nan_median()
if (np.sum(cal_med == 127) > 0) | (np.sum(np.isnan(cal_med)) > 0):
sys.stderr.write('At least one link has a median of 127 or is nan\n\n')
quit()
if (np.sum(np.isnan(self.off_buff.get_nanvar())) > 0):
sys.stderr.write('the long term buffer has a nan')
quit()
cal_med_mat = np.tile(cal_med,(self.V_mat.shape[1],1)).T
# variance of RSS during calibration
cal_var = np.maximum(self.off_buff.get_nanvar(),self.omega) #3.0
cal_var_mat = np.tile(cal_var,(self.V_mat.shape[1],1)).T
# Compute the off_link emission probabilities for each link
x = np.exp(- (self.V_mat - cal_med_mat)**2/(2*cal_var_mat/1.0)) # 1.0
self.off_links = self.__normalize_pmf(x)
# Compute the on_link emission probabilities for each link
x = np.exp(- (self.V_mat - (cal_med_mat-self.Delta))**2/(self.eta*2*cal_var_mat)) # 3
self.on_links = self.__normalize_pmf(x)
示例6: calcForces_and_potentialE
def calcForces_and_potentialE(F_x, F_y, old_or_new, x_positions, y_positions, V_atoms):
"""calculates x and y forces and potential energy per atom as summed over
all contributions due to all neighbors, as functions of position and the
parameters of the LJ potential"""
for atom in xrange(Natoms):
for i in xrange(Natoms):
if i != atom:
delx = x_positions[atom,old_or_new]-x_positions[i,old_or_new]
dely = y_positions[atom,old_or_new]-y_positions[i,old_or_new]
r_ij = np.sqrt( (x_positions[atom,old_or_new]-x_positions[i,old_or_new])**2\
+ (y_positions[atom,old_or_new]-y_positions[i,old_or_new])**2 )
F_x[atom,old_or_new] = F_x[atom,old_or_new] - 24.0 *epsilon * sigma**6 \
* delx * ( 1 - 2.0*(sigma/r_ij)**6 ) / r_ij**8
F_y[atom,old_or_new] = F_y[atom,old_or_new] - 24.0 *epsilon * sigma**6 * \
dely * ( 1 - 2.0*(sigma/r_ij)**6 ) / r_ij**8
V_atoms[atom] = V_atoms[atom] + 4.0 * epsilon \
* ( (sigma/r_ij)**12-(sigma/r_ij)**6 )
if np.isnan(F_x[atom,old_or_new]) or np.isinf(F_x[atom,old_or_new]):
F_x[atom,old_or_new]=0
if np.isnan(F_y[atom,old_or_new]) or np.isinf(F_y[atom,old_or_new]):
F_y[atom,0]=0
if np.isnan(V_atoms[atom]) or np.isinf(V_atoms[atom]):
V_atoms[atom]=0
return F_x, F_y, V_atoms
示例7: estimateBIsochrone
def estimateBIsochrone(R,z,pot=None):
"""
NAME:
estimateBIsochrone
PURPOSE:
Estimate a good value for the scale of the isochrone potential by matching the slope of the rotation curve
INPUT:
R,z = coordinates (if these are arrays, the median estimated delta is returned, i.e., if this is an orbit)
pot= Potential instance or list thereof
OUTPUT:
b if 1 R,Z given
bmin,bmedian,bmax if multiple R given
HISTORY:
2013-09-12 - Written - Bovy (IAS)
"""
if pot is None: #pragma: no cover
raise IOError("pot= needs to be set to a Potential instance or list thereof")
if isinstance(R,nu.ndarray):
bs= nu.array([estimateBIsochrone(R[ii],z[ii],pot=pot) for ii in range(len(R))])
return (nu.amin(bs[True-nu.isnan(bs)]),
nu.median(bs[True-nu.isnan(bs)]),
nu.amax(bs[True-nu.isnan(bs)]))
else:
r2= R**2.+z**2
r= math.sqrt(r2)
dlvcdlr= dvcircdR(pot,r)/vcirc(pot,r)*r
try:
b= optimize.brentq(lambda x: dlvcdlr-(x/math.sqrt(r2+x**2.)-0.5*r2/(r2+x**2.)),
0.01,100.)
except: #pragma: no cover
b= nu.nan
return b
示例8: test_float_modulus_corner_cases
def test_float_modulus_corner_cases(self):
# Check remainder magnitude.
for dt in np.typecodes['Float']:
b = np.array(1.0, dtype=dt)
a = np.nextafter(np.array(0.0, dtype=dt), -b)
rem = self.mod(a, b)
assert_(rem <= b, 'dt: %s' % dt)
rem = self.mod(-a, -b)
assert_(rem >= -b, 'dt: %s' % dt)
# Check nans, inf
with suppress_warnings() as sup:
sup.filter(RuntimeWarning, "invalid value encountered in remainder")
for dt in np.typecodes['Float']:
fone = np.array(1.0, dtype=dt)
fzer = np.array(0.0, dtype=dt)
finf = np.array(np.inf, dtype=dt)
fnan = np.array(np.nan, dtype=dt)
rem = self.mod(fone, fzer)
assert_(np.isnan(rem), 'dt: %s' % dt)
# MSVC 2008 returns NaN here, so disable the check.
#rem = self.mod(fone, finf)
#assert_(rem == fone, 'dt: %s' % dt)
rem = self.mod(fone, fnan)
assert_(np.isnan(rem), 'dt: %s' % dt)
rem = self.mod(finf, fone)
assert_(np.isnan(rem), 'dt: %s' % dt)
示例9: test_autocorr
def test_autocorr(self):
# Just run the function
corr1 = self.ts.autocorr()
# Now run it with the lag parameter
corr2 = self.ts.autocorr(lag=1)
# corr() with lag needs Series of at least length 2
if len(self.ts) <= 2:
self.assertTrue(np.isnan(corr1))
self.assertTrue(np.isnan(corr2))
else:
self.assertEqual(corr1, corr2)
# Choose a random lag between 1 and length of Series - 2
# and compare the result with the Series corr() function
n = 1 + np.random.randint(max(1, len(self.ts) - 2))
corr1 = self.ts.corr(self.ts.shift(n))
corr2 = self.ts.autocorr(lag=n)
# corr() with lag needs Series of at least length 2
if len(self.ts) <= 2:
self.assertTrue(np.isnan(corr1))
self.assertTrue(np.isnan(corr2))
else:
self.assertEqual(corr1, corr2)
示例10: exact_roc
def exact_roc(actuals, controls):
"""
computes the area under the roc curve for separating to sets. Uses all
possibl thresholds and trapezoidal interpolation. Also returns arrays of
the true positive rate and the false positive rate.
"""
actuals = np.ravel(actuals)
controls = np.ravel(controls)
if np.isnan(actuals).any():
raise RuntimeError('NaN found in actuals')
if np.isnan(controls).any():
raise RuntimeError('NaN found in controls')
thresholds = np.hstack([-np.inf,
np.unique(np.concatenate((actuals,controls))), np.inf])[::-1]
true_pos_rate = np.empty(thresholds.size)
false_pos_rate = np.empty(thresholds.size)
num_act = float(len(actuals))
num_ctr = float(len(controls))
for i, value in enumerate(thresholds):
true_pos_rate[i] = (actuals >= value).sum() / num_act
false_pos_rate[i] = (controls >= value).sum() / num_ctr
auc = np.dot(np.diff(false_pos_rate),
(true_pos_rate[0:-1]+true_pos_rate[1:])/2)
return(auc, true_pos_rate, false_pos_rate)
示例11: __init__
def __init__(self, x, y):
assert np.ndim(x)==2 and np.ndim(y)==2 and np.shape(x)==np.shape(y), \
'x and y must be 2D arrays of the same size.'
if np.any(np.isnan(x)) or np.any(np.isnan(y)):
x = np.ma.masked_where( (isnan(x)) | (isnan(y)) , x)
y = np.ma.masked_where( (isnan(x)) | (isnan(y)) , y)
self.x_vert = x
self.y_vert = y
mask_shape = tuple([n-1 for n in self.x_vert.shape])
self.mask_rho = np.ones(mask_shape, dtype='d')
# If maskedarray is given for verticies, modify the mask such that
# non-existant grid points are masked. A cell requires all four
# verticies to be defined as a water point.
if isinstance(self.x_vert, np.ma.MaskedArray):
mask = (self.x_vert.mask[:-1,:-1] | self.x_vert.mask[1:,:-1] | \
self.x_vert.mask[:-1,1:] | self.x_vert.mask[1:,1:])
self.mask_rho = np.asarray(~(~np.bool_(self.mask_rho) | mask), dtype='d')
if isinstance(self.y_vert, np.ma.MaskedArray):
mask = (self.y_vert.mask[:-1,:-1] | self.y_vert.mask[1:,:-1] | \
self.y_vert.mask[:-1,1:] | self.y_vert.mask[1:,1:])
self.mask_rho = np.asarray(~(~np.bool_(self.mask_rho) | mask), dtype='d')
self._calculate_subgrids()
self._calculate_metrics()
示例12: test_nan_arithmetic
def test_nan_arithmetic(ctx_getter):
context = ctx_getter()
queue = cl.CommandQueue(context)
def make_nan_contaminated_vector(size):
shape = (size,)
a = numpy.random.randn(*shape).astype(numpy.float32)
#for i in range(0, shape[0], 3):
#a[i] = float('nan')
from random import randrange
for i in range(size//10):
a[randrange(0, size)] = float('nan')
return a
size = 1 << 20
a = make_nan_contaminated_vector(size)
a_gpu = cl_array.to_device(context, queue, a)
b = make_nan_contaminated_vector(size)
b_gpu = cl_array.to_device(context, queue, b)
ab = a*b
ab_gpu = (a_gpu*b_gpu).get()
for i in range(size):
assert numpy.isnan(ab[i]) == numpy.isnan(ab_gpu[i])
示例13: update
def update(self, tick):
security = tick['security']
quote_time = datetime.datetime.fromtimestamp(int(tick['data']['timestamp']))
last_price = tick['data']['last']
log.debug("tick update security %s with tick %s, price %s" % (security.symbol, quote_time, last_price))
# update sma
# appending new row to df is not efficient
data = tick['data']
row = [quote_time, float(data['volume']), float(data['bid']), float(data['ask']), float(data['last']), float(data['high']), float(data['low'])]
new_serie = pd.Series(row, index=['datetime', 'volume', 'bid', 'ask', 'last', 'high', 'low'])
self.quotes = self.quotes.append(new_serie, ignore_index=True)
self.sma_short = SMA(self.quotes, timeperiod=10, key='last')
self.sma_mid = SMA(self.quotes, timeperiod=60, key='last')
self.sma_long = SMA(self.quotes, timeperiod=200, key='last')
if np.isnan(self.sma_long.iloc[-1]) or np.isnan(self.sma_mid.iloc[-1]) or np.isnan(self.sma_short.iloc[-1]):
log.info('not enough data, skip to reduce risk')
return None
action = None
if security.symbol not in self.account.holdings:
action = self.check_buy(security)
# already have some holdings
else:
action = self.check_sell(security)
log.info('strategy action {0}'.format(action))
return action
示例14: norm_range
def norm_range(data, mins, maxs, lowbound, highbound):
""" Normalizing the data with range normalization between lowbound and highbound
Keyword parameters:
data
the data to be normalized, numpy.ndarray, each row is a sample
mins, maxs
arrays of minimum and maximum values that each feature can take
lowbound, highbound
the bounds of the normalization
"""
denom = maxs - mins
diff = highbound - lowbound
addit = numpy.ndarray([data.shape[0],1])
addit.fill(lowbound)
for i in range(data.shape[0]): # for each feature vector
data[i] = diff * (data[i] - mins) / denom + lowbound
nanCounter = numpy.isnan(data[i])
#If all data was nan, maitain nan,
if(sum(nanCounter)!=data.shape[1]):
data[i][numpy.isnan(data[i])] = (lowbound + highbound) / 2
return data
示例15: _evaluate_projection
def _evaluate_projection(self, x, y):
"""
kNNEvaluate - evaluate class separation in the given projection using a k-NN method
Parameters
----------
x - variables to evaluate
y - class
Returns
-------
scores
"""
if self.percent_data_used != 100:
rand = np.random.choice(len(x), int(len(x) * self.percent_data_used / 100),
replace=False)
x = x[rand]
y = y[rand]
neigh = KNeighborsClassifier(n_neighbors=3) if self.attr_color.is_discrete else \
KNeighborsRegressor(n_neighbors=3)
assert ~(np.isnan(x).any(axis=None) | np.isnan(x).any(axis=None))
neigh.fit(x, y)
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=UserWarning)
scores = cross_val_score(neigh, x, y, cv=3)
return scores.mean()