def do_adfuller(path, srv, p_values):
filename = os.path.join(path, srv["filename"])
df = load_timeseries(filename, srv)
columns = []
for c in df.columns:
if (not df[c].isnull().all()) and df[c].var() != 0:
columns.append(c)
df = df[columns]
if len(columns) == 0: return []
for i, col in enumerate(df.columns):
serie = df[col].dropna()
if is_monotonic(serie):
serie = serie.diff()[1:]
for reg in p_values:
v = adfuller(serie, regression=reg)[1]
if math.isnan(v): # uncertain
p_values[reg].append(-0.1)
else:
p_values[reg].append(v)
return p_values
python类isnan()的实例源码
def draw(path, srv):
filename = os.path.join(path, srv["preprocessed_filename"])
df = pd.read_csv(filename, sep="\t", index_col='time', parse_dates=True)
bins = defaultdict(list)
for i, col in enumerate(df.columns):
serie = df[col].dropna()
if pd.algos.is_monotonic_float64(serie.values, False)[0]:
serie = serie.diff()[1:]
p_value = adfuller(serie, autolag='AIC')[1]
if math.isnan(p_value): continue
nearest = 0.05 * round(p_value/0.05)
bins[nearest].append(serie)
for bin, members in bins.items():
series = [serie.name for serie in members]
if len(members) <= 10:
columns = series
else:
columns = random.sample(series, 10)
subset = df[columns]
name = "%s_adf_confidence_%.2f.png" % (srv["name"], bin)
print(name)
axes = subset.plot(subplots=True)
plt.savefig(os.path.join(path, name))
plt.close("all")
def validate(self, val):
if not isinstance(val, numbers.Real):
raise ValidationError('expected real number, got %s' %
generic_type_name(val))
if not isinstance(val, float):
# This checks for the case where a number is passed in with a
# magnitude larger than supported by float64.
try:
val = float(val)
except OverflowError:
raise ValidationError('too large for float')
if math.isnan(val) or math.isinf(val):
raise ValidationError('%f values are not supported' % val)
if self.minimum is not None and val < self.minimum:
raise ValidationError('%f is not greater than %f' %
(val, self.minimum))
if self.maximum is not None and val > self.maximum:
raise ValidationError('%f is not less than %f' %
(val, self.maximum))
return val
def __eq__(a, b):
"""a == b"""
if isinstance(b, Rational):
return (a._numerator == b.numerator and
a._denominator == b.denominator)
if isinstance(b, numbers.Complex) and b.imag == 0:
b = b.real
if isinstance(b, float):
if math.isnan(b) or math.isinf(b):
# comparisons with an infinity or nan should behave in
# the same way for any finite a, so treat a as zero.
return 0.0 == b
else:
return a == a.from_float(b)
else:
# Since a doesn't know how to compare with b, let's give b
# a chance to compare itself with a.
return NotImplemented
def _richcmp(self, other, op):
"""Helper for comparison operators, for internal use only.
Implement comparison between a Rational instance `self`, and
either another Rational instance or a float `other`. If
`other` is not a Rational instance or a float, return
NotImplemented. `op` should be one of the six standard
comparison operators.
"""
# convert other to a Rational instance where reasonable.
if isinstance(other, Rational):
return op(self._numerator * other.denominator,
self._denominator * other.numerator)
# comparisons with complex should raise a TypeError, for consistency
# with int<->complex, float<->complex, and complex<->complex comparisons.
if isinstance(other, complex):
raise TypeError("no ordering relation is defined for complex numbers")
if isinstance(other, float):
if math.isnan(other) or math.isinf(other):
return op(0.0, other)
else:
return op(self, self.from_float(other))
else:
return NotImplemented
def validate(self, val):
if not isinstance(val, numbers.Real):
raise ValidationError('expected real number, got %s' %
generic_type_name(val))
if not isinstance(val, float):
# This checks for the case where a number is passed in with a
# magnitude larger than supported by float64.
try:
val = float(val)
except OverflowError:
raise ValidationError('too large for float')
if math.isnan(val) or math.isinf(val):
raise ValidationError('%f values are not supported' % val)
if self.minimum is not None and val < self.minimum:
raise ValidationError('%f is not greater than %f' %
(val, self.minimum))
if self.maximum is not None and val > self.maximum:
raise ValidationError('%f is not less than %f' %
(val, self.maximum))
return val
def _modbusRead(self, key):
if self.PM_cacheEnabled is False:
# W/O cache
val = self.mb.readRegistersAndDecode(self.modbusmap[key][0],self.modbusmap[key][1],self.modbusmap[key][2])
else:
# with cache
val = self.mb.cachedRead(self.modbusmap[key][0], self.modbusmap[key][1], self.modbusmap[key][2])
log.debug('"%s" Modbus: (%s,%s,%s) = %s' % (key, self.modbusmap[key][0],
self.modbusmap[key][1], self.modbusmap[key][2], val))
if self.modbusmap[key][2].startswith("float"):
try:
if math.isnan(val):
log.debug("NaN regs %s => 0" % self.modbusmap[key][0])
val = 0
except TypeError:
val = 0
return val
def test_nan(self):
output = convert(
'table',
{
'format': 'csv',
'url': 'file://' + os.path.join('data', 'RadiomicsData.csv')
},
{'format': 'rows.json'}
)
data = json.loads(output['data'])
self.assertEqual(len(data['fields']), 454)
self.assertEqual(data['fields'][:3], [
'GLCM_autocorr', 'GLCM_clusProm', 'GLCM_clusShade'
])
self.assertEqual(len(data['rows']), 99)
for row in data['rows']:
for field in row:
if isinstance(row[field], float):
self.assertFalse(math.isnan(row[field]))
self.assertFalse(math.isinf(row[field]))
def csv_to_rows(input):
reader = get_csv_reader(input)
rows = [d for d in reader]
fields = reader.fieldnames
output = {'fields': fields, 'rows': rows}
# Attempt numeric conversion
for row in output['rows']:
for col in row:
try:
row[col] = int(row[col])
except Exception:
try:
orig = row[col]
row[col] = float(row[col])
# Disallow NaN, Inf, -Inf since this does not
# pass through JSON converters cleanly
if math.isnan(row[col]) or math.isinf(row[col]):
row[col] = orig
except Exception:
pass
return output
def update_qz(self, left = None, right = None):
if left == None:
left = 0
right = self.lc.n
for index in range(left, right):
#if index % 100 == 0:
# print index
# sys.stdout.flush()
qz_1 = np.log(self.theta)
qz_0 = np.log(1 - self.theta)
for (label, worker) in zip(*self.lc.crowd_labels[index]):
if label >=0 and worker in self.quv:
qz_1 += expectation_z(self.quv[worker][0], self.quv[worker][1], label)
qz_0 += expectation_z(self.quv[worker][2], self.quv[worker][3], label)
qz_1 = np.exp(qz_1)
qz_0 = np.exp(qz_0)
temp = qz_1 * 1.0 / (qz_0 + qz_1)
if not math.isnan(temp):
self.total_changes += np.abs(self.qz[index] - temp)
self.qz[index] = temp
def add(self, t, data, mindt):
# update previous timestamps based on downtime
if self.points and math.isnan(self.points[0][1]):
dt = time.time() - t - self.timeoff
self.timeoff = False
for i in range(len(self.points)):
point = self.points[i]
self.points[i] = point[0]-dt, point[1]
if not self.timeoff or self.timeoff < time.time() - t or self.timeoff > time.time() - t + 1:
self.timeoff = time.time() - t
elif self.points and t-self.points[0][0]<mindt:
return False
self.points.insert(0, (t, data))
return True
def tracevertexes(self, time, plot, gldrawtype):
# remove datapoints after the first one that is off the screen
for i in range(len(self.points)):
if self.points[i][0] < time - plot.disptime:
self.points = self.points[:i+1]
break
glBegin(gldrawtype)
for point in self.points:
if math.isnan(point[1]):
glEnd()
glBegin(gldrawtype)
else:
y = point[1] - self.offset
if self.directional:
if y >= 180:
y -= 360
elif y < - 180:
y += 360
glVertex2d(point[0]-time, y)
glEnd()
def __eq__(a, b):
"""a == b"""
if isinstance(b, Rational):
return (a._numerator == b.numerator and
a._denominator == b.denominator)
if isinstance(b, numbers.Complex) and b.imag == 0:
b = b.real
if isinstance(b, float):
if math.isnan(b) or math.isinf(b):
# comparisons with an infinity or nan should behave in
# the same way for any finite a, so treat a as zero.
return 0.0 == b
else:
return a == a.from_float(b)
else:
# Since a doesn't know how to compare with b, let's give b
# a chance to compare itself with a.
return NotImplemented
def _richcmp(self, other, op):
"""Helper for comparison operators, for internal use only.
Implement comparison between a Rational instance `self`, and
either another Rational instance or a float `other`. If
`other` is not a Rational instance or a float, return
NotImplemented. `op` should be one of the six standard
comparison operators.
"""
# convert other to a Rational instance where reasonable.
if isinstance(other, Rational):
return op(self._numerator * other.denominator,
self._denominator * other.numerator)
# comparisons with complex should raise a TypeError, for consistency
# with int<->complex, float<->complex, and complex<->complex comparisons.
if isinstance(other, complex):
raise TypeError("no ordering relation is defined for complex numbers")
if isinstance(other, float):
if math.isnan(other) or math.isinf(other):
return op(0.0, other)
else:
return op(self, self.from_float(other))
else:
return NotImplemented
def test_floating_voint_ranges(self):
arr = np.array([[[0.0, 0.0, 0.0, 0.0],
[1.0, 1.0, 1.0, 1.0],
[1.5, 1.5, 1.5, 1.5],
[2.0, 2.0, 2.0, 2.0]]], dtype=float)
tile = Tile(arr, 'FLOAT', float('nan'))
rdd = BaseTestClass.pysc.parallelize([(self.projected_extent, tile)])
raster_rdd = RasterLayer.from_numpy_rdd(LayerType.SPATIAL, rdd)
value_map = {2.0: 5.0}
result = raster_rdd.reclassify(value_map, float,
ClassificationStrategy.LESS_THAN).to_numpy_rdd().first()[1].cells
expected = np.array([[[5.0, 5.0, 5.0, 5.0],
[5.0, 5.0, 5.0, 5.0],
[5.0, 5.0, 5.0, 5.0]]], dtype=float)
self.assertTrue((result[0, 2, ] == expected).all())
for x in result[0, 3, ]:
self.assertTrue(math.isnan(x))
def main(args):
num, den = args
try:
num, den = float(num), float(den)
except ValueError as e:
logging.error('Invalid input')
return INVALID_INPUT
if den == 0:
# this is a run-time error but not a type error
# can be considered a warning or an error based on use case
# written here as mere warning.
logging.warn('Invalid denominator input!')
return DIV_BY_ZERO_EXIT
if math.isnan(num) or math.isnan(den):
return INVALID_INPUT_NAN
if math.isinf(num) or math.isinf(den):
return INVALID_INPUT_INF
print('Answer: ' + str(num / den))
return 0
def process(self, **kwargs):
"""Process module."""
self._times = kwargs[self.key('dense_times')]
self._alpha = kwargs[self.key('alpha')]
self._beta = kwargs[self.key('beta')]
self._t_peak = kwargs[self.key('tpeak')]
self._lum_scale = kwargs[self.key('lumscale')]
self._rest_t_explosion = kwargs[self.key('resttexplosion')]
ts = [
np.inf
if self._rest_t_explosion > x else (x - self._rest_t_explosion)
for x in self._times
]
luminosities = [
self._lum_scale * (1.0 - np.exp(-t / self._t_peak)) **
self._alpha * (t / self._t_peak) ** (-self._beta) for t in ts
]
luminosities = [0.0 if isnan(x) else x for x in luminosities]
return {self.dense_key('luminosities'): luminosities}
def process(self, **kwargs):
"""Process module."""
self._times = kwargs[self.key('dense_times')]
self._mnickel = kwargs[self.key('fnickel')] * kwargs[
self.key('mejecta')]
self._rest_t_explosion = kwargs[self.key('resttexplosion')]
# From 1994ApJS...92..527N
ts = [
np.inf
if self._rest_t_explosion > x else (x - self._rest_t_explosion)
for x in self._times
]
luminosities = [
self._mnickel * (self.NI56_LUM * np.exp(-t / self.NI56_LIFE) +
self.CO56_LUM * np.exp(-t / self.CO56_LIFE))
for t in ts
]
luminosities = [0.0 if isnan(x) else x for x in luminosities]
return {self.dense_key('luminosities'): luminosities}
def calFinalResult(self, testSetNum):
self.loadResultFiles(testSetNum)
res = []
for key, value in self.actualDict.iteritems():
actual = value
if actual == 0:
print "record {} is 0, not included in final calculation".format(key)
continue
prediction = self.predictonDict[key]
temp = (actual - prediction)/float(actual)
if math.isnan(temp):
print temp
res.append(abs(temp))
res = np.array(res)
pd.DataFrame(res).to_csv('result.csv')
print "final result: {}".format(res.mean())
return np.mean(res)
def def_itemgetter(attr, default=0, _type=None):
# like operator.itemgetter but fills in missing keys with a default value
def keyfunc(item):
value = item.get(attr, default)
casted = cast(value, _type) if _type else value
try:
is_nan = isnan(casted)
except TypeError:
is_nan = False
return default if is_nan else casted
return keyfunc
# TODO: move this to meza.process.group
def test_iter_discrete_traces_nan(enum_discrete, trace_graph):
pyro.clear_param_store()
def model():
p = Variable(torch.Tensor([0.0, 0.5, 1.0]))
pyro.sample("z", dist.Bernoulli(p))
def guide():
p = pyro.param("p", Variable(torch.Tensor([0.0, 0.5, 1.0]), requires_grad=True))
pyro.sample("z", dist.Bernoulli(p))
Elbo = TraceGraph_ELBO if trace_graph else Trace_ELBO
elbo = Elbo(enum_discrete=enum_discrete)
with xfail_if_not_implemented():
loss = elbo.loss(model, guide)
assert isinstance(loss, float) and not math.isnan(loss), loss
loss = elbo.loss_and_grads(model, guide)
assert isinstance(loss, float) and not math.isnan(loss), loss
# A simple Gaussian mixture model, with no vectorization.
run_pipeline_lib_test.py 文件源码
项目:healthcare-deid
作者: GoogleCloudPlatform
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
def testCalculateStats(self):
stats = results_pb2.Stats()
stats.true_positives = 12
stats.false_positives = 8
stats.false_negatives = 3
run_pipeline_lib.calculate_stats(stats)
self.assertAlmostEqual(.6, stats.precision)
self.assertAlmostEqual(.8, stats.recall)
self.assertAlmostEqual(.6857142857142856, stats.f_score)
stats = results_pb2.Stats()
run_pipeline_lib.calculate_stats(stats)
self.assertTrue(math.isnan(stats.precision))
self.assertTrue(math.isnan(stats.recall))
self.assertTrue(math.isnan(stats.f_score))
self.assertEqual(
'Precision has denominator of zero. Recall has denominator of zero. '
'f-score is NaN',
stats.error_message)
run_pipeline_lib_test.py 文件源码
项目:healthcare-deid
作者: GoogleCloudPlatform
项目源码
文件源码
阅读 19
收藏 0
点赞 0
评论 0
def testMacroStats(self):
macro_stats = run_pipeline_lib._MacroStats()
macro_stats.count = 50
macro_stats.precision_sum = 40
macro_stats.recall_sum = 45
stats = macro_stats.calculate_stats()
self.assertAlmostEqual(.8, stats.precision)
self.assertAlmostEqual(.9, stats.recall)
self.assertAlmostEqual(.8470588235294118, stats.f_score)
macro_stats = run_pipeline_lib._MacroStats()
stats = macro_stats.calculate_stats()
self.assertTrue(math.isnan(stats.precision))
self.assertTrue(math.isnan(stats.recall))
self.assertTrue(math.isnan(stats.f_score))
def calculate_stats(stats):
"""Calculate derived stats and put them into the given results_pb2.Stats."""
stats.error_message = ''
if stats.true_positives + stats.false_positives:
stats.precision = (float(stats.true_positives) /
(stats.true_positives + stats.false_positives))
else:
stats.precision = float('NaN')
stats.error_message += 'Precision has denominator of zero. '
if stats.true_positives + stats.false_negatives:
stats.recall = (float(stats.true_positives) /
(stats.true_positives + stats.false_negatives))
else:
stats.recall = float('NaN')
stats.error_message += 'Recall has denominator of zero. '
stats.f_score = hmean(stats.precision, stats.recall)
if math.isnan(stats.f_score):
stats.error_message += 'f-score is NaN'
return stats
def isConverged(self,iter):
from math import isnan
if isnan(self.loss):
print 'Loss = NaN or Infinity: current settings does not fit the recommender! Change the settings and try again!'
exit(-1)
measure = self.performance()
value = [item.strip()for item in measure]
#with open(self.algorName+' iteration.txt')
deltaLoss = (self.lastLoss-self.loss)
print '%s %s iteration %d: loss = %.4f, delta_loss = %.5f learning_Rate = %.5f %s %s' %(self.algorName,self.foldInfo,iter,self.loss,deltaLoss,self.lRate,measure[0][:11],measure[1][:12])
#check if converged
cond = abs(deltaLoss) < 1e-3
converged = cond
if not converged:
self.updateLearningRate(iter)
self.lastLoss = self.loss
shuffle(self.dao.trainingData)
return converged
def GetInfoFromTable(fitstable,indice):
'''read salvatore table and return info corresponding to the source at the place indice
Parameters
---------
fitstable : pyfits object : table to be browsed
indice : place of the source in the table
'''
data = fitstable[1].data[indice]
sourcename = data[0]
ra = data[1]
dec = data[2]
z = data[4]
if math.isnan(z):
z=0
hemisphere = data[6]
observation_type = data[8]
if hemisphere =='S':
hemisphere ='South'
if hemisphere =='N':
hemisphere ='North'
return sourcename,ra,dec,z,hemisphere,observation_type
def pearson(A, B):
# Pearson
coeff = 0.0
try:
coeff = scipy.stats.pearsonr(A, B)[0]
if math.isnan(coeff):
coeff = 0.0
except:
coeff = 0.0
return (coeff)
##
# Calculate the hamming weight of all values
# within the given array.
#
# @param A 1-D byte array
# @return Hamming weight
#
def maybe_create_close_position_transaction(self, asset, dt, data_portal):
if not self.positions.get(asset):
return None
amount = self.positions.get(asset).amount
price = data_portal.get_spot_value(
asset, 'price', dt, self.data_frequency)
# Get the last traded price if price is no longer available
if isnan(price):
price = self.positions.get(asset).last_sale_price
txn = Transaction(
asset=asset,
amount=(-1 * amount),
dt=dt,
price=price,
commission=0,
order_id=None,
)
return txn
def sync_last_sale_prices(self, dt, handle_non_market_minutes,
data_portal):
if not handle_non_market_minutes:
for asset, position in iteritems(self.positions):
last_sale_price = data_portal.get_spot_value(
asset, 'price', dt, self.data_frequency
)
if not np.isnan(last_sale_price):
position.last_sale_price = last_sale_price
else:
for asset, position in iteritems(self.positions):
last_sale_price = data_portal.get_adjusted_value(
asset,
'price',
data_portal.trading_calendar.previous_minute(dt),
dt,
self.data_frequency
)
if not np.isnan(last_sale_price):
position.last_sale_price = last_sale_price
def set_metric(self, key, value):
# This method sets a numeric tag value for the given key. It acts
# like `set_meta()` and it simply add a tag without further processing.
# FIXME[matt] we could push this check to serialization time as well.
# only permit types that are commonly serializable (don't use
# isinstance so that we convert unserializable types like numpy
# numbers)
if type(value) not in numeric_types:
try:
value = float(value)
except (ValueError, TypeError):
log.debug("ignoring not number metric %s:%s", key, value)
return
# don't allow nan or inf
if math.isnan(value) or math.isinf(value):
log.debug("ignoring not real metric %s:%s", key, value)
return
self.metrics[key] = value