def __init__(self, x0, P0, Q, R, cor, f, h):
self.Q = Q
self.R = R
self.cor = cor
self.fa = lambda col: f(col[0], col[2])
self.ha = lambda col: h(col[0], col[1])
Pxx = P0
Pxv = 0.
self.xa = np.array( ((x0,), (0.,), (0.,), (0.,)) )
self.Pa = np.array( ((Pxx, Pxv , 0. , 0. ),
(Pxv, self.R, 0. , 0. ),
(0. , 0. , self.Q , self.cor),
(0. , 0. , self.cor, self.R )) )
self.lastobservation = np.NAN
self.predictedobservation = np.NAN
self.innov = np.NAN
self.innovcov = np.NAN
self.gain = np.NAN
self.loglikelihood = 0.0
python类NAN的实例源码
def remask(self):
"""Reset the mask based on the seeded connected component.
"""
body = self.to_body()
if not body.is_seed_in_mask():
return False
new_mask_bin, bounds = body.get_seeded_component(CONFIG.postprocessing.closing_shape)
new_mask_bin = new_mask_bin.astype(np.bool)
mask_block = self.mask[map(slice, bounds[0], bounds[1])].copy()
# Clip any values not in the seeded connected component so that they
# cannot not generate moves when rechecking.
mask_block[~new_mask_bin] = np.clip(mask_block[~new_mask_bin], None, 0.9 * CONFIG.model.t_move)
self.mask[:] = np.NAN
self.mask[map(slice, bounds[0], bounds[1])] = mask_block
return True
def MA_RIBBON(df, ma_series):
ma_array = np.zeros([len(df), len(ma_series)])
ema_list = []
for idx, ma_len in enumerate(ma_series):
ema_i = EMA(df, n = ma_len, field = 'close')
ma_array[:, idx] = ema_i
ema_list.append(ema_i)
corr = np.empty([len(df)])
pval = np.empty([len(df)])
dist = np.empty([len(df)])
corr[:] = np.NAN
pval[:] = np.NAN
dist[:] = np.NAN
max_n = max(ma_series)
for idy in range(len(df)):
if idy >= max_n - 1:
corr[idy], pval[idy] = stats.spearmanr(ma_array[idy,:], range(len(ma_series), 0, -1))
dist[idy] = max(ma_array[idy,:]) - min(ma_array[idy,:])
corr_ts = pd.Series(corr*100, index = df.index, name = "MARIBBON_CORR")
pval_ts = pd.Series(pval*100, index = df.index, name = "MARIBBON_PVAL")
dist_ts = pd.Series(dist, index = df.index, name = "MARIBBON_DIST")
return pd.concat([corr_ts, pval_ts, dist_ts] + ema_list, join='outer', axis=1)
def read_csv(filename, skip_lines=0):
csvfile = file(filename, 'rb')
reader = csv.reader(csvfile)
data = np.empty(0, dtype=object)
last_count = np.NAN
for line in reader:
if skip_lines > 0:
skip_lines = skip_lines - 1
continue
if data.size > 0:
if len(line) != last_count:
raise Exception('unequal columes found')
data = np.c_[data, line]
last_count = len(line)
else:
data = np.array(line, dtype=object)
data = data.reshape(len(data), 1)
last_count = len(line)
csvfile.close()
return data.T
def clear_fit(self, **kwargs):
# if no pixel was provided the current pixel is updated
if 'pixel' not in kwargs.keys() or kwargs['pixel'] == -1:
px = self._focus[0]
else:
px = kwargs['pixel'][0]
# clear fit
self._fit_functions[px, :] = numpy.zeros(6)
self._fit_initial_parameters[px, :, :] = numpy.NAN
self._fit_optimized_parameters[px, :, :] = numpy.NAN
# emit signal
if 'emit' not in kwargs or kwargs['emit']:
self._app.fit_changed.emit(self._id)
# TODO: Flip for 1D
def clear_fit(self, **kwargs):
# if no pixel was provided the current pixel is updated
if 'pixel' not in kwargs.keys() or kwargs['pixel'] == -1:
px = self._focus[0]
py = self._focus[1]
else:
px = kwargs['pixel'][0]
py = kwargs['pixel'][1]
# clear fit
self._fit_functions[px, py, :] = numpy.zeros(6)
self._fit_initial_parameters[px, py, :, :] = numpy.NAN
self._fit_optimized_parameters[px, py, :, :] = numpy.NAN
# emit signal
if 'emit' not in kwargs or kwargs['emit']:
self._app.fit_changed.emit(self._id)
input_rec_transform_nengo_plot_figs.py 文件源码
项目:FOLLOW
作者: adityagilra
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def rasterplot(ax,trange,tstart,tend,spikesOut,n_neurons,colors=['r','b'],\
size=2.5,marker='.',sort=False):
spikesPlot = []
for i in n_neurons:
spikesti = trange[spikesOut[:, i] > 0].ravel()
spikesti = spikesti[np.where((spikesti>tstart) & (spikesti<tend))]
if len(spikesti)==0: spikesPlot.append([np.NAN])
else: spikesPlot.append(spikesti)
if sort:
idxs = np.argsort(
[spikesPlot[i][0] for i in range(len(spikesPlot))] )
idxs = idxs[::-1] # reverse sorted in time to first spike
else: idxs = range(len(n_neurons))
for i,idx in enumerate(idxs):
ax.scatter(spikesPlot[idx],[i+1]*len(spikesPlot[idx]),\
marker=marker,s=size,\
facecolor=colors[i%2],lw=0,clip_on=False)
ax.set_ylim((1,len(n_neurons)))
ax.set_xlim((tstart,tend))
ax.get_xaxis().get_major_formatter().set_useOffset(False)
def get_read_reg_events(r_data, interval_start, num_bases):
r_means = nh.get_read_base_means(r_data)
if r_data.start > interval_start:
# handle reads that start in middle of region
start_overlap = interval_start + num_bases - r_data.start
# create region with nan values
region_means = np.empty(num_bases)
region_means[:] = np.NAN
region_means[-start_overlap:] = r_means[:start_overlap]
elif r_data.end < interval_start + num_bases:
# handle reads that end inside region
end_overlap = r_data.end - interval_start
# create region with nan values
region_means = np.empty(num_bases)
region_means[:] = np.NAN
region_means[:end_overlap] = r_means[-end_overlap:]
else:
skipped_bases = interval_start - r_data.start
region_means = r_means[
skipped_bases:skipped_bases + num_bases]
return region_means
def linkage(df, n_groups):
# create the distance matrix based on the forbenius norm: |A-B|_F where A is
# a 24 x N matrix with N the number of timeseries inside the dataframe df
# TODO: We can save have time as we only need the upper triangle once as the
# distance matrix is symmetric
if True:
Y = np.empty((n_groups, n_groups,))
Y[:] = np.NAN
for i in range(len(Y)):
for j in range(len(Y[i,:])):
A = df.loc[i+1].values
B = df.loc[j+1].values
#print('Computing distance of:{},{}'.format(i,j))
Y[i,j] = norm(A-B, ord='fro')
# condensed distance matrix as vector for linkage (upper triangle as a vector)
y = Y[np.triu_indices(n_groups, 1)]
# create linkage matrix with wards algorithm an euclidean norm
Z = hac.linkage(y, method='ward', metric='euclidean')
# R = hac.inconsistent(Z, d=10)
return Z
def fcluster(df, Z, n_groups, n_clusters):
"""
"""
# create flat cluster, i.e. maximal number of clusters...
T = hac.fcluster(Z, criterion='maxclust', depth=2, t=n_clusters)
# add cluster id to original dataframe
df['cluster_id'] = np.NAN
# group is either days (1-365) or weeks (1-52)
#for d in df.index.get_level_values('group').unique():
for g in range(1, n_groups+1):
# T[d-1] because df.index is e.g. 1-365 (d) and T= is 0...364
df.ix[g, 'cluster_id'] = T[g-1]
# add the cluster id to the index
df.set_index(['cluster_id'], append=True, inplace=True)
# set cluster id as first index level for easier looping through cluster_ids
df.index = df.index.swaplevel(0, 'cluster_id')
# just to have datetime at the last level of the multiindex df
df.index = df.index.swaplevel('datetime', 'group')
return df
def set_value(self, column, value):
if column not in self._columns:
if isinstance(value, str):
self._columns.update({
column: np.empty((1, len(self._df)), dtype="object")
})
self._columns[column][:] = ""
elif isinstance(value, bool):
self._columns.update({
column: np.empty((1, len(self._df)), dtype="bool")
})
self._columns[column][:] = False
else:
self._columns.update({
column: np.empty((1, len(self._df)))
})
self._columns[column][:] = np.NAN
self._columns[column][0, self._current_row_index] = value
def linkage(counts_table):
"""
Return the linkage disequilibrium (D) for an arbitrary number of
loci given their contingency table.
"""
probs_table = frequency_to_probability(counts_table)
marginal_probs = get_marginal_probabilities(probs_table)
if either_locus_not_detected(marginal_probs):
return np.NAN
exp_freqs = marginal_probs.prod(axis=0)[0]
observed = probs_table.flat[-1]
if observed == 0:
return np.NAN
return observed - exp_freqs
def generate(self):
self.__validate()
self.__generatenoises()
self.__generatejumps()
processcount = len(self.__data._processnames)
self.__data._processes = np.empty((self.__data._timecount, processcount))
self.__data._processes[:] = np.NAN
for time in range(self.__data._timecount):
for pi, (pn, pf) in enumerate(zip(self.__data._processnames, self.__processfuncs)):
self.__data._processes[time, pi] = pf(time, pn, self.__data)
return self.__data.copy()
def tonumpyarray(self, fill=None, symmetric=False):
import numpy as np
if fill is None: fill = np.NAN
res = np.empty((self.__dim, self.__dim))
idx = 0
for i in range(self.__dim):
for j in range(i+1):
res[i,j] = self._data[idx]
if symmetric: res[j,i] = res[i,j]
idx += 1
if not symmetric: res[i,i+1:self.__dim] = fill
return res
def tonumpyarray(self, fill=None, symmetric=False):
import numpy as np
if fill is None: fill = np.NAN
res = np.empty((self.__dim, self.__dim))
idx = 0
for i in range(self.__dim):
for j in range(i):
res[i,j] = self._data[idx]
if symmetric: res[j,i] = res[i,j]
idx += 1
res[i,i] = fill
if not symmetric: res[i,i+1:self.__dim] = fill
return res
def __init__(self, x0, P0, params):
self._params = params
self.x = x0
self.P = P0
self._constterm = self._params.meanlogvar * (1. - self._params.persistence)
self._cv = self._params.cor * self._params.voloflogvar
self._cv2 = self._cv * self._cv
self._p2 = self._params.persistence * self._params.persistence
self._v2 = self._params.voloflogvar * self._params.voloflogvar
self.predictedobservation = np.NAN
self.lastobservation = None
self.innov = np.NAN
self.innovcov = np.NAN
self.gain = np.NAN
self.loglikelihood = 0.0
def _compose_alpha(img_in, img_layer, opacity: float=1.0):
"""
Calculate alpha composition ratio between two images.
"""
comp_alpha = np.minimum(img_in[:, :, 3], img_layer[:, :, 3]) * opacity
new_alpha = img_in[:, :, 3] + (1.0 - img_in[:, :, 3]) * comp_alpha
np.seterr(divide='ignore', invalid='ignore')
ratio = comp_alpha / new_alpha
ratio[ratio == np.NAN] = 0.0
return ratio
def check_binary_nan(self, name, xp, dtype):
a = xp.array([-3, numpy.NAN, -1, numpy.NAN, 0, numpy.NAN, 2],
dtype=dtype)
b = xp.array([numpy.NAN, numpy.NAN, 1, 0, numpy.NAN, -1, -2],
dtype=dtype)
return getattr(xp, name)(a, b)
def check_unary_nan(self, name, xp, dtype):
a = xp.array(
[-3, numpy.NAN, -1, numpy.NAN, 0, numpy.NAN, dtype('inf')],
dtype=dtype)
return (a,)
def check_binary_nan(self, name, xp, dtype):
a = xp.array([-3, numpy.NAN, -1, numpy.NAN, 0, numpy.NAN, 2],
dtype=dtype)
b = xp.array([numpy.NAN, numpy.NAN, 1, 0, numpy.NAN, -1, -2],
dtype=dtype)
return a, b
def check_unary_nan(self, name, xp, dtype):
a = xp.array(
[-3, numpy.NAN, -1, numpy.NAN, 0, numpy.NAN, numpy.inf],
dtype=dtype)
return getattr(xp, name)(a)
def fill_symbol_value(symbols, valsyms, vals, fill_value=np.NAN):
if valsyms.size == 0:
return np.tile(np.NAN, len(symbols))
values = np.tile(np.NAN, len(symbols)) if len(vals.shape) == 1 else np.tile(np.NAN, (len(symbols),vals.shape[1]))
symbol2pos = ustr.get_str2pos_dict(symbols)
for i in xrange(len(valsyms)):
try:
if len(vals.shape) == 1:
values[symbol2pos[valsyms[i]]] = vals[i]
else:
values[symbol2pos[valsyms[i]], :] = vals[i, :]
except Exception, e:
pass
values[np.isnan(values)] = fill_value
return values
def get_mat_ewma(tsmat, alpha):
# EWMA(t) = alpha * ts(t) + (1-alpha) * EWMA(t-1)
ewma = np.tile(np.NAN, tsmat.shape)
for i in xrange(1, tsmat.shape[0]):
init_selection = np.isnan(ewma[i-1, :])
ewma[i-1, init_selection] = tsmat[i-1, init_selection]
ewma[i, :] = alpha * tsmat[i, :] + (1-alpha) * ewma[i-1, :]
return ewma
def get_mat_movingstd(tsmat, periods):
mstd = np.empty(shape = tsmat.shape)
mstd.fill(np.NAN)
for i in xrange(tsmat.shape[0]):
j = i - periods + 1
if j < 0:
j = 0
mstd[i,:] = np.nanstd(tsmat[j:i+1,:], 0)
return mstd
def get_mat_ma(tsmat, periods):
ma = np.empty(shape = tsmat.shape)
ma.fill(np.NAN)
for i in xrange(tsmat.shape[0]):
j = i - periods + 1
if j < 0:
j = 0
ma[i,:] = np.nanmean(tsmat[j:i+1,:], 0)
return ma
def get_array_ma(ts, periods):
ma = np.empty(shape = len(ts))
ma.fill(np.NAN)
for i in xrange(len(ts)):
j = i - periods + 1
if j < 0:
j = 0
ma[i] = np.nanmean(ts[j:i+1], 0)
return ma
# 0 for add, 1 for multiply
def ret2value(returns):
values = np.empty(shape=len(returns) + 1)
values.fill(np.NAN)
values[0] = 1
for i in xrange(len(returns)):
values[i + 1] = values[i] * (1 + returns[i])
return values
def retmat2valuemat(retmat):
valuemat = np.tile(np.NAN, (retmat.shape[0] + 1, retmat.shape[1]))
for i in xrange(retmat.shape[1]):
valuemat[:, i] = ret2value(retmat[:, i])
return valuemat
def value2ret(values):
if len(values.shape) == 1:
prevalues = np.append(np.NAN, values[0:-1])
returns = (values - prevalues) / prevalues
returns[np.isinf(returns)] = np.NAN
else:
prevalues = np.r_[
np.tile(np.NAN, (1, values.shape[1])), values[0:-1, :]]
returns = (values - prevalues) / prevalues
returns[np.isinf(returns)] = np.NAN
return returns
def get_bstrs_pos_in_astrs(astrs, bstrs, astrs2pos={}):
if not astrs2pos:
astrs2pos = get_str2pos_dict(astrs)
pos = np.zeros(shape=len(bstrs))
count = 0
for s in bstrs:
try:
pos[count] = astrs2pos[s]
except Exception:
pos[count] = np.NAN
count += 1
return pos