本文整理汇总了Python中pylab.isnan函数的典型用法代码示例。如果您正苦于以下问题:Python isnan函数的具体用法?Python isnan怎么用?Python isnan使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了isnan函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: resample
def resample(data):
if len(data) == 0:
return data
delta_true = .1
p = data['mu_pred']+1.e-6
# TODO: abstract this block of code into rate_model.py; it is also called in data_model.py
## ensure that all data has uncertainty quantified appropriately
# first replace all missing se from ci
missing_se = pl.isnan(data['standard_error']) | (data['standard_error'] <= 0)
data['standard_error'][missing_se] = (data['upper_ci'][missing_se] - data['lower_ci'][missing_se]) / (2*1.96)
# then replace all missing ess with se
missing_ess = pl.isnan(data['effective_sample_size'])
data['effective_sample_size'][missing_ess] = data['value'][missing_ess]*(1-data['value'][missing_ess])/data['standard_error'][missing_ess]**2
# warn and drop data that doesn't have effective sample size quantified, or is is non-positive
missing_ess = pl.isnan(data['effective_sample_size']) | (data['effective_sample_size'] < 0)
if sum(missing_ess) > 0:
print 'WARNING: %d rows of data has invalid quantification of uncertainty.' % sum(missing_ess)
data['effective_sample_size'][missing_ess] = 1.0
n = data['effective_sample_size']
data['true'] = p
data['value'] = (1.0 * mc.rnegative_binomial(n*p, delta_true*n*p)) / n
# uncomment below to test the effect of having very wrong data
#data['value'] = 0.
#data['effective_sample_size'] = 1.e6
return data
示例2: process
def process(self):
"""rearranges the ping data into a matrix of max amplitude of
dimensions corrisponding to the power, gain and beam sections."""
MINSAMPLES = 5
datadim = self.pingdata.shape
self.pingmax = pl.zeros((len(self.settings['power']), len(self.settings['gain']), datadim[2]))
for i, power in enumerate(self.settings['power']):
for j, gain in enumerate(self.settings['gain']):
for k in xrange(datadim[2]):
sampleindx = pl.find((self.pingdata[:, 1, k] == power) & (self.pingdata[:, 2, k] == gain))
if len(sampleindx) > MINSAMPLES:
temp = self.pingdata[sampleindx[-MINSAMPLES:], 0, k]
tempmax = temp.max()
if tempmax == 0:
self.pingmax[i, j, k] = pl.NaN
else:
self.pingmax[i, j, k] = temp.max()
else:
self.pingmax[i, j, k] = pl.NaN
#The following section removes settings that were collected erroniously.
#gain settings first
null = pl.zeros((len(self.settings['gain']), datadim[2]))
powershortlist = []
self.havedata = True # this is an ugly workaround...
for i, power in enumerate(self.settings['power']):
test = pl.isnan(self.pingmax[i, :, :] )
if test.all():
powershortlist.append(i)
print 'removing ' + str(power) + ' power setting.'
for i in powershortlist:
try:
self.settings['power'].pop(i)
except IndexError:
self.havedata = False
if self.havedata:
self.pingmax = pl.delete(self.pingmax, powershortlist, 0)
#then power settings
null = pl.zeros((len(self.settings['power']), datadim[2]))
gainshortlist = []
for i, gain in enumerate(self.settings['gain']):
test = pl.isnan(self.pingmax[:, i, :])
if test.all():
gainshortlist.append(i)
print 'removing ' + str(gain) + ' gain setting.'
for i in gainshortlist:
try:
self.settings['gain'].pop(i)
except IndexError:
self.havedata = False
if self.havedata:
self.pingmax = pl.delete(self.pingmax, gainshortlist, 1)
#remove the power and gain to normalize
self.pingmax = 20*pl.log10(self.pingmax)
for i, power in enumerate(self.settings['power']):
for j, gain in enumerate(self.settings['gain']):
self.pingmax[i, j, :] = self.pingmax[i, j, :] - power - gain
示例3: evaluate_model
def evaluate_model(mod, comment='', data_fname='missing_noisy_data.csv', truth_fname='data.csv'):
""" Run specified model on existing data (data.csv / missing_noisy_data.csv) and save results in dev_log.csv
Existing models: %s """ % data_run_models
if mod not in data_run_models.split(' '):
raise TypeError, 'Unrecognized model "%s"; must be one of %s' % (mod, data_run_models)
import model
reload(model)
print 'loading data'
data = pl.csv2rec(data_fname)
truth = pl.csv2rec(truth_fname)
t0 = time.time()
print 'generating model'
mod_mc = eval('model.%s(data)' % mod)
print 'fitting model with mcmc'
mod_mc.sample(10000, 5000, 50, verbose=1)
t1 = time.time()
print 'summarizing results'
import graphics
reload(graphics)
pl.figure(figsize=(22, 17), dpi=300)
pl.clf()
graphics.plot_all_predictions_over_time(data, mod_mc.predicted, more_data=truth)
data_stats = mod_mc.data_predicted.stats()
i_out = [i for i in range(len(data)) if pl.isnan(data.y[i])]
rmse_abs_out = pl.rms_flat(truth.y[i_out] - data_stats['mean'][i_out])
rmse_rel_out = 100*pl.rms_flat(1. - data_stats['mean'][i_out]/truth.y[i_out])
i_in = [i for i in range(len(data)) if not pl.isnan(data.y[i])]
rmse_abs_in = pl.rms_flat(truth.y[i_in] - data_stats['mean'][i_in])
rmse_rel_in = 100*pl.rms_flat(1. - data_stats['mean'][i_in]/truth.y[i_in])
param_stats = mod_mc.param_predicted.stats()
coverage = 100*pl.sum((truth.y[i_out] >= param_stats['95% HPD interval'][i_out, 0]) & (truth.y[i_out] <= param_stats['95% HPD interval'][i_out, 1])) / float(len(i_out))
import md5
data_hash = md5.md5(data).hexdigest()
results = [mod, t1-t0, rmse_abs_out, rmse_rel_out, rmse_abs_in, rmse_rel_in, coverage,
len(data), len(pl.unique(data.region)), len(pl.unique(data.country)), len(pl.unique(data.year)), len(pl.unique(data.age)), data_hash,
t0, comment]
print '%s: time: %.0fs out-of-samp rmse abs=%.1f rel=%.0f in-samp rmse abs=%.1f rel=%.0f coverage=%.0f\ndata: %d rows; %d regions, %d countries %d years %d ages [data hash: %s]\n(run conducted at %f)\n%s' % tuple(results)
pl.savefig('/home/j/Project/Models/space-time-smoothing/images/%s.png' % t0) # FIXME: don't hardcode path for saving images
import csv
f = open('dev_log.csv', 'a')
f_csv = csv.writer(f)
f_csv.writerow(results)
f.close()
return mod_mc
示例4: create_uncertainty
def create_uncertainty(model, rate_type):
'''data without valid uncertainty is given the 10% uncertainty of the data set
Parameters
----------
model : data.ModelData
dismod model
rate_type : str
a rate model
'neg_binom', 'binom', 'normal', 'log_norm', 'poisson', 'beta'
Results
-------
model : data.ModelData
dismod model with measurements of uncertainty for all data
'''
# fill any missing covariate data with 0s
for cv in list(model.input_data.filter(like='x_').columns):
model.input_data[cv] = model.input_data[cv].fillna([0])
# find indices that are negative for standard error and
# calculate standard error from effective sample size
missing_se = pl.isnan(model.input_data['standard_error']) | (model.input_data['standard_error'] < 0)
if True in set(missing_se):
model.input_data['standard_error'][missing_se] = (model.input_data['upper_ci'][missing_se] - model.input_data['lower_ci'][missing_se]) / (2*1.96)
missing_se_still = pl.isnan(model.input_data['standard_error']) | (model.input_data['standard_error'] < 0)
if True in set(missing_se_still):
model.input_data['standard_error'][missing_se_still] = pl.sqrt(model.input_data['value'][missing_se_still]*(1-model.input_data['value'][missing_se_still])/model.input_data['effective_sample_size'][missing_se_still])
# find indices that contain nan for effective sample size
missing_ess = pl.isnan(model.input_data['effective_sample_size'])==1
# calculate effective sample size from standard error
model.input_data['effective_sample_size'][missing_ess] = model.input_data['value'][missing_ess]*(1-model.input_data['value'][missing_ess])/(model.input_data['standard_error'][missing_ess])**2
# find effective sample size of entire dataset
non_missing_ess_still = pl.isnan(model.input_data['effective_sample_size'])==0 # finds all real numbers
if False in non_missing_ess_still:
percent = pl.percentile(model.input_data['effective_sample_size'][non_missing_ess_still], 10.)
missing_ess_still = pl.isnan(model.input_data['effective_sample_size'])==1 # finds all nan
# replace nan effective sample size with 10th percentile
model.input_data['effective_sample_size'][missing_ess_still] = percent
# change values of 0 in lognormal model to 1 observation
if rate_type == 'log_normal':
# find indices where values are 0
zero_val = (model.input_data['value'] == 0)
# add 1 observation so no values are zero, also change effective sample size
model.input_data['effective_sample_size'][zero_val] = model.input_data['effective_sample_size'][zero_val] + 1
model.input_data['value'][zero_val] = 1.0/model.input_data['effective_sample_size'][zero_val]
# update standard error
model.input_data['standard_error'][zero_val] = pl.sqrt(model.input_data['value'][zero_val]*(1-model.input_data['value'][zero_val])/model.input_data['effective_sample_size'][zero_val])
return model
示例5: test_from_gbd_json
def test_from_gbd_json():
d = data.ModelData.from_gbd_json('tests/dismoditis.json')
assert len(d.input_data) > 17, 'dismoditis model has more than 17 data points'
for field in 'data_type value area sex age_start age_end year_start year_end standard_error effective_sample_size lower_ci upper_ci age_weights'.split():
assert field in d.input_data.columns, 'Input data CSV should have field "%s"' % field
#assert len(d.input_data.filter(regex='x_').columns) == 1, 'should have added country-level covariates to input data'
#assert len(d.input_data['x_LDI_id_Updated_7July2011'].dropna().index) > 0
assert len(d.output_template) > 100
for field in 'area sex year pop'.split():
assert field in d.output_template.columns, 'Output template CSV should have field "%s"' % field
#assert len(d.output_template.filter(regex='x_').columns) == 1, 'should have added country-level covariates to output template'
#assert len(d.output_template['x_LDI_id_Updated_7July2011'].dropna().index) > 0
for data_type in 'i p r f rr X'.split():
for prior in 'smoothness heterogeneity level_value level_bounds increasing decreasing'.split():
assert prior in d.parameters[data_type], 'Parameters for %s should include prior on %s' % (data_type, prior)
assert 'CHN' in d.hierarchy.successors('asia_east')
assert pl.isnan(d.hierarchy['asia_east']['CHN'].get('weight'))
#assert set(d.hierarchy.node['asia_east'].keys()) == set('area sex year_start year_end pop'.split())
#assert len(d.nodes_to_fit) == 21*3*2 + 1
import dismod3
import simplejson as json
model = data.ModelData.from_gbd_jsons(json.loads(dismod3.disease_json.DiseaseJson().to_json()))
示例6: sample
def sample(self, model, evidence):
z = evidence['z']
T, g, h, sigma_g = [evidence[var] for var in ['T', 'g', 'h', 'sigma_g']]
sigma_z_g = model.known_params['sigma_z_g']
sigma_z_h = model.known_params['sigma_z_h']
prior_mu_g, prior_cov_g = [model.hyper_params[var] for var in ['prior_mu_g', 'prior_cov_g']]
n = len(g)
# Must be a more concise way to deal with scalar vs vector
g = g.copy().reshape((n,1))
h = h.copy().reshape((n,1))
z_g = ma.asarray(z.copy().reshape((n,1)))
obs_cov = sigma_z_g**2*ones((n,1,1))
if sum(T == 0) > 0:
z_g[T == 0] = nan
if sum(T == 2) > 0:
z_g[T == 2] -= h[T == 2]
obs_cov[T == 2] = sigma_z_h**2
z_g[isnan(z_g)] = ma.masked
kalman = self._kalman
kalman.initial_state_mean = array([prior_mu_g[0],])
kalman.initial_state_covariance = array([prior_cov_g[0,0],])
kalman.transition_matrices = eye(1)
kalman.transition_covariance = array([sigma_g**2,])
kalman.observation_matrices = eye(1)
kalman.observation_covariance = obs_cov
sampled_g = forward_filter_backward_sample(kalman, z_g)
return sampled_g.reshape((n,))
示例7: find_unfeasible_concentrations
def find_unfeasible_concentrations(S, dG0_f, c_range, c_mid=1e-4, T=default_T, bounds=None, log_stream=None):
"""
Almost the same as find_pCr, but adds a global restriction on the concentrations (for compounds
that don't have specific bounds in 'bounds').
After the solution which optimizes the pCr is found, any concentration which does not confer
to the limits of c_range will be truncated to the closes allowed concentration.
If at least one concentration needs to be adjusted, then pCr looses its meaning
and therefore is returned with the value None.
"""
dG_f, concentrations, pCr = find_pCr(S, dG0_f, c_mid=c_mid, bounds=bounds, log_stream=log_stream)
for c in xrange(dG0_f.shape[0]):
if (pylab.isnan(dG0_f[c, 0])):
continue # unknown dG0_f - therefore the concentration of this compounds is meaningless
if ((bounds == None or bounds[c][0] == None) and concentrations[c, 0] < c_range[0]):
concentrations[c, 0] = c_range[0]
dG_f[c, 0] = dG0_f[c, 0] + R * T * c_range[0]
pCr = None
elif ((bounds == None or bounds[c][1] == None) and concentrations[c, 0] > c_range[1]):
concentrations[c, 0] = c_range[1]
dG_f[c, 0] = dG0_f[c, 0] + R * T * c_range[1]
pCr = None
return (dG_f, concentrations, pCr)
示例8: add_thermodynamic_constraints
def add_thermodynamic_constraints(cpl, dG0_f, c_range=(1e-6, 1e-2), T=default_T, bounds=None):
"""
For any compound that does not have an explicit bound set by the 'bounds' argument,
create a bound using the 'margin' variables (the last to columns of A).
"""
Nc = dG0_f.shape[0]
if bounds != None and len(bounds) != Nc:
raise Exception("The concentration bounds list must be the same length as the number of compounds")
if bounds == None:
bounds = [(None, None)] * Nc
for c in xrange(Nc):
if pylab.isnan(dG0_f[c, 0]):
continue # unknown dG0_f - cannot bound this compound's concentration at all
b_low = bounds[c][0] or c_range[0]
b_high = bounds[c][1] or c_range[1]
# lower bound: dG0_f + R*T*ln(Cmin) <= x_i
cpl.variables.set_lower_bounds('c%d' % c, dG0_f[c, 0] + R*T*pylab.log(b_low))
# upper bound: x_i <= dG0_f + R*T*ln(Cmax)
cpl.variables.set_upper_bounds('c%d' % c, dG0_f[c, 0] + R*T*pylab.log(b_high))
示例9: metric_heat
def metric_heat(group):
if all(pylab.isnan(group[metric])):
#print metric
#describe_group(group)
pass
return groupfunc(group[metric])
示例10: getAngle
def getAngle(t1,c1,t2,c2):
'''
Get angle between two celestials at t1 and t2
Verify if ignoring the k-cordinate makes any sense
timeit 240 microseconds
'''
if type(t2) == numpy.ndarray:
t2 = t2[0]
elif isnan(t2):
print "ERROR, t2 is nan!"
return t2
p1 = c1.eph(t1)[0]
p1[2] = 0.0
p1l = norm(p1)
p1 /= p1l
p2 = c2.eph(t2)[0]
p2[2] = 0.0
p2l = norm(p2)
p2 /= p2l
#if p1l > p2l:
return p1.dot(p2)
#else:
# return p1.dot(p2)
'''
示例11: load_new_model
def load_new_model(disease, country='all', sex=['total', 'male', 'female'], cov='no'):
'''create disease model with relavtive data
cov : str
method to handle covariates
default is nothing ('no')
options include,
- 'drop' : drop all covartiates
- 'zero' : missing values replaced with 0
- 'average' : missing values replaced with average of column
'''
model = dismod3.data.load('/home/j/Project/dismod/output/dm-%s'%disease)
# keep relative data
if (type(sex)==str) & (sex != 'total'): model.keep(areas=[country], sexes=[sex, 'total'])
else: model.keep(areas=[country], sexes=sex)
if (True in pl.isnan(pl.array(model.output_template.filter(like='x_')))) | (True in pl.isnan(pl.array(model.input_data.filter(like='x_')))):
print 'Covariates missing, %s method used'%(cov)
col = model.input_data.filter(like='x_').columns
for i in col:
if cov == 'drop':
model.input_data = model.input_data.drop(i,1)
model.output_template = model.output_template.drop(i,1)
elif cov == 'zero':
model.input_data[i] = model.input_data[i].fillna([0])
model.output_template[i] = model.output_template[i].fillna([0])
elif cov == 'average':
model.input_data[i] = model.input_data[i].fillna([model.input_data[i].mean()])
model.output_template[i] = model.output_template[i].fillna(model.output_template[i].mean())
return model
示例12: get_cod_data_all_causes
def get_cod_data_all_causes(iso3='USA', age_group='1_4', sex='F'):
""" TODO: write doc string for this function"""
print 'loading', iso3, age_group, sex
import glob
cause_list = []
fpath = '/home/j/Project/Causes of Death/Under Five Deaths/CoD Correct Input Data/v02_prep_%s/%s+*+%s+%s.csv' % (iso3, iso3, age_group, sex)
#fpath = '/home/j/Project/GBD/dalynator/data/cod_correct_input_pos/run_9_cause_*.csv' # use Mike's validation data
fnames = glob.glob(fpath)
# initialize input distribution array
N = 990 # TODO: get this from the data files
T = 32 # TODO: get this from the data files
J = len(fnames)
F = pl.zeros((N, T, J))
# fill input distribution array with data from files
for j, fname in enumerate(sorted(fnames)):
cause = fname.split('+')[1] # TODO: make this less brittle and clearer
#cause = str(j) # use Mike's validation data causes
print 'loading cause', cause
F_j = pl.csv2rec(fname)
for n in range(N):
F[n, :, j] = F_j['ensemble_d%d'%(n+1)]/F_j['envelope']
#F[n, :, j] = F_j['d%d'%(n+1)]/F_j['envelope'] # use Mike's validation data
assert not pl.any(pl.isnan(F)), '%s should have no missing values' % fname
cause_list.append(cause)
print 'loading complete'
return F, cause_list
示例13: sample
def sample(self, model, evidence):
z = evidence['z']
T, surfaces, sigma_g, sigma_h = [evidence[var] for var in ['T', 'surfaces', 'sigma_g', 'sigma_h']]
mu_h, phi, sigma_z_g, sigma_z_h = [model.known_params[var] for var in ['mu_h', 'phi', 'sigma_z_g', 'sigma_z_h']]
prior_mu_g, prior_cov_g = [model.hyper_params[var] for var in ['prior_mu_g', 'prior_cov_g']]
prior_mu_h, prior_cov_h = [model.hyper_params[var] for var in ['prior_mu_h', 'prior_cov_h']]
n = len(g)
y = ma.asarray(ones((n, 2))*nan)
if sum(T==1) > 0:
y[T==1, 0] = z[T==1]
if sum(T==2) > 0:
y[T==2, 1] = z[T==2]
y[isnan(y)] = ma.masked
kalman = self._kalman
kalman.initial_state_mean=[prior_mu_g[0], prior_mu_h[0]]
kalman.initial_state_covariance=diag([prior_cov_g[0,0], prior_cov_h[0,0]])
kalman.transition_matrices=[[1, 0], [0, phi]]
kalman.transition_offsets =ones((n, 2))*[0, mu_h*(1-phi)]
kalman.transition_covariance=[[sigma_g**2, 0], [0, sigma_h**2]]
kalman.observation_matrices=[[1, 0], [1, 1]]
kalman.observation_covariance=[[sigma_z_g**2, 0], [0, sigma_z_h**2]]
sampled_surfaces = forward_filter_backward_sample(kalman, y)
return sampled_surfaces
示例14: setup
def setup(dm, key, data_list, rate_stoch):
""" Generate the PyMC variables for a log-normal model of
a function of age
Parameters
----------
dm : dismod3.DiseaseModel
the object containing all the data, priors, and additional
information (like input and output age-mesh)
key : str
the name of the key for everything about this model (priors,
initial values, estimations)
data_list : list of data dicts
the observed data to use in the beta-binomial liklihood function
rate_stoch : pymc.Stochastic
a PyMC stochastic (or deterministic) object, with
len(rate_stoch.value) == len(dm.get_estimation_age_mesh()).
Results
-------
vars : dict
Return a dictionary of all the relevant PyMC objects for the
log-normal model. vars['rate_stoch'] is of particular
relevance, for details see the beta_binomial_model
"""
vars = {}
est_mesh = dm.get_estimate_age_mesh()
vars['rate_stoch'] = rate_stoch
# set up priors and observed data
prior_str = dm.get_priors(key)
dismod3.utils.generate_prior_potentials(vars, prior_str, est_mesh)
vars['observed_rates'] = []
for d in data_list:
age_indices = dismod3.utils.indices_for_range(est_mesh, d['age_start'], d['age_end'])
age_weights = d.get('age_weights', pl.ones(len(age_indices)) / len(age_indices))
lb, ub = dm.bounds_per_1(d)
se = (pl.log(ub) - pl.log(lb)) / (2. * 1.96)
if pl.isnan(se) or se <= 0.:
se = 1.
print 'data %d: log(value) = %f, se = %f' % (d['id'], pl.log(dm.value_per_1(d)), se)
@mc.observed
@mc.stochastic(name='obs_%d' % d['id'])
def obs(f=vars['rate_stoch'],
age_indices=age_indices,
age_weights=age_weights,
value=pl.log(dm.value_per_1(d)),
tau=se**-2, data=d):
f_i = dismod3.utils.rate_for_range(f, age_indices, age_weights)
return mc.normal_like(value, pl.log(f_i), tau)
vars['observed_rates'].append(obs)
return vars
示例15: _get_angles
def _get_angles(steps,track_length):
angles = pl.zeros(track_length-2)
polar = pl.zeros(pl.shape(steps))
for i in range(track_length-1):
polar[i,0] = pl.norm(steps[i,:])
polar[i,1] = pl.arctan(steps[i,0]/steps[i,1])
if pl.isnan( polar[i,1]):
polar[i,1] = 0
if (steps[i,0] >= 0):
if (steps[i,1] >= 0):
pass
elif (steps[i,1] < 0):
polar[i,1] += 2.*pl.pi
elif (steps[i,0] < 0):
if (steps[i,1] >= 0):
polar[i,1] += pl.pi
elif (steps[i,1] < 0):
polar[i,1] += pl.pi
for i in range(track_length-2):
angles[i] = polar[i+1,1] - polar[i,1]
return angles