def _create_prices(t):
last_average = 100 if t==0 else source.data['average'][-1]
returns = asarray(lognormal(mean.value, stddev.value, 1))
average = last_average * cumprod(returns)
high = average * exp(abs(gamma(1, 0.03, size=1)))
low = average / exp(abs(gamma(1, 0.03, size=1)))
delta = high - low
open = low + delta * uniform(0.05, 0.95, size=1)
close = low + delta * uniform(0.05, 0.95, size=1)
return open[0], high[0], low[0], close[0], average[0]
python类cumprod()的实例源码
def _ema(prices, days=10):
if len(prices) < days or days < 2: return [prices[-1]]
a = 2.0 / (days+1)
kernel = ones(days, dtype=float)
kernel[1:] = 1 - a
kernel = a * cumprod(kernel)
# The 0.8647 normalizes out that we stop the EMA after a finite number of terms
return convolve(prices[-days:], kernel, mode="valid") / (0.8647)
def _create_prices(t):
last_average = 100 if t==0 else source.data['average'][-1]
returns = asarray(lognormal(mean.value, stddev.value, 1))
average = last_average * cumprod(returns)
high = average * exp(abs(gamma(1, 0.03, size=1)))
low = average / exp(abs(gamma(1, 0.03, size=1)))
delta = high - low
open = low + delta * uniform(0.05, 0.95, size=1)
close = low + delta * uniform(0.05, 0.95, size=1)
return open[0], high[0], low[0], close[0], average[0]
def _grid_distance(self, index):
"""
Calculate the distance grid for a single index position.
This is pre-calculated for fast neighborhood calculations
later on (see _calc_influence).
"""
# Take every dimension but the first in reverse
# then reverse that list again.
dimensions = np.cumprod(self.map_dimensions[1::][::-1])[::-1]
coord = []
for idx, dim in enumerate(dimensions):
if idx != 0:
value = (index % dimensions[idx-1]) // dim
else:
value = index // dim
coord.append(value)
coord.append(index % self.map_dimensions[-1])
for idx, (width, row) in enumerate(zip(self.map_dimensions, coord)):
x = np.abs(np.arange(width) - row) ** 2
dims = self.map_dimensions[::-1]
if idx:
dims = dims[:-idx]
x = np.broadcast_to(x, dims).T
if idx == 0:
distance = np.copy(x)
else:
distance += x.T
return distance
test_analytics.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 30
收藏 0
点赞 0
评论 0
def test_cumprod(self):
self._check_accum_op('cumprod')
test_analytics.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 32
收藏 0
点赞 0
评论 0
def test_cummethods_bool(self):
# GH 6270
# looks like a buggy np.maximum.accumulate for numpy 1.6.1, py 3.2
def cummin(x):
return np.minimum.accumulate(x)
def cummax(x):
return np.maximum.accumulate(x)
a = pd.Series([False, False, False, True, True, False, False])
b = ~a
c = pd.Series([False] * len(b))
d = ~c
methods = {'cumsum': np.cumsum,
'cumprod': np.cumprod,
'cummin': cummin,
'cummax': cummax}
args = product((a, b, c, d), methods)
for s, method in args:
expected = Series(methods[method](s.values))
result = getattr(s, method)()
assert_series_equal(result, expected)
e = pd.Series([False, True, nan, False])
cse = pd.Series([0, 1, nan, 1], dtype=object)
cpe = pd.Series([False, 0, nan, 0])
cmin = pd.Series([False, False, nan, False])
cmax = pd.Series([False, True, nan, True])
expecteds = {'cumsum': cse,
'cumprod': cpe,
'cummin': cmin,
'cummax': cmax}
for method in methods:
res = getattr(e, method)()
assert_series_equal(res, expecteds[method])
fromnumeric.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
def cumproduct(a, axis=None, dtype=None, out=None):
"""
Return the cumulative product over the given axis.
See Also
--------
cumprod : equivalent function; see for details.
"""
try:
cumprod = a.cumprod
except AttributeError:
return _wrapit(a, 'cumprod', axis, dtype, out)
return cumprod(axis, dtype, out)
def p2(x, coef):
X = np.empty(len(coef))
X[0] = 1
X[1:] = x
y = np.cumprod(X) # y = [1, x, x**2,...]
return np.dot(coef, y)
def cumproduct(a, axis=None, dtype=None, out=None):
"""
Return the cumulative product over the given axis.
See Also
--------
cumprod : equivalent function; see for details.
"""
try:
cumprod = a.cumprod
except AttributeError:
return _wrapit(a, 'cumprod', axis, dtype, out)
return cumprod(axis, dtype, out)
def get_idx_from_arg(a, arg, axis):
shp = a.shape
cp = np.cumprod(shp[::-1])[::-1]
if axis == len(shp) - 1:
m = 1
else:
m = cp[axis + 1]
n = cp[0] // cp[axis]
if m == 1:
return np.arange(n) * cp[axis] + arg.ravel()
return np.repeat(np.arange(n) * cp[axis], m) + np.tile(np.arange(m), n) + arg.ravel() * m
def read_feature(filename, keep_shape=False):
"""Read feature (a.k.a blob) dump by C3D.
Parameters
----------
filename : str
Fullpath of file to read.
keep_shape : bool
Reshape feature to the shape reported.
Outputs
-------
feature : ndarray
numpy array of features
s : tuple
shape of original feature
Note: It accomplishes the same purpose of this code:
C3D/examples/c3d_feature_extraction/script/read_binary_blob.m
"""
s_parr, d_parr = array.array('i'), array.array('f')
with open(filename, 'rb') as f:
s_parr.fromfile(f, 5)
s = np.array(s_parr)
m = np.cumprod(s)[-1]
d_parr.fromfile(f, m)
feature = np.array(d_parr)
if keep_shape:
feature = feature.reshape(s)
return feature, s
def __init__(self, prefix, children, suffix='.dat', root='root', createdirs=True):
'''
Parameters
----------
prefix : string
All paths are prefixed with this string.
children: sequence of ints
Creates a directory tree rooted at path given by `root` with levels
specified by the `children` array: level ``i`` had ``children[i]``
children. ``children[-1]`` specifies the arity of the leaves.
suffix : string
The suffix of the leafs (i.e. files) of the tree.
root : path
The path to the root of the tree.
createdirs : bool.
If True, actually create the directories. Note: this is not thread-safe.
'''
if len(children) == 0:
raise ValueError("need at least one level")
self.children = np.asarray(children)
self.root = root
self.prefix = prefix
self.suffix = suffix
self.depth = len(children)
self._cap = np.cumprod(self.children[::-1])[::-1]
self.capacity = self._cap[0]
self._den = self._cap / self.children
self.width = int(np.ceil(np.log10(self.capacity)))
if createdirs:
self._mktree()
def neighbors(shape, conn=1):
dim = len(shape)
block = generate_binary_structure(dim, conn)
block[tuple([1]*dim)] = 0
idx = np.where(block>0)
idx = np.array(idx, dtype=np.uint8).T
idx = np.array(idx-[1]*dim)
acc = np.cumprod((1,)+shape[::-1][:-1])
return np.dot(idx, acc[::-1])
def neighbors(shape):
dim = len(shape)
block = np.ones([3]*dim)
block[tuple([1]*dim)] = 0
idx = np.where(block>0)
idx = np.array(idx, dtype=np.uint8).T
idx = np.array(idx-[1]*dim)
acc = np.cumprod((1,)+shape[::-1][:-1])
return np.dot(idx, acc[::-1])
def filter(img, msk, idx, bur, tor, mode):
nbs = neighbors(img.shape)
acc = np.cumprod((1,)+img.shape[::-1][:-1])[::-1]
img = img.ravel()
msk = msk.ravel()
arg = np.argsort(img[idx])[::-1 if mode else 1]
for i in arg:
if msk[idx[i]]!=3:
idx[i] = 0
continue
cur = 0; s = 1;
bur[0] = idx[i]
while cur<s:
p = bur[cur]
if msk[p] == 2:
idx[i]=0
break
for dp in nbs:
cp = p+dp
if msk[cp]==0 or cp==idx[i] or msk[cp] == 4: continue
if mode and img[cp] < img[idx[i]]-tor: continue
if not mode and img[cp] > img[idx[i]]+tor: continue
bur[s] = cp
s += 1
if s==msk.size//3:
cut = cur//2
msk[bur[:cut]] = 2
bur[:s-cut] = bur[cut:]
cur -= cut
s -= cut
if msk[cp]!=2:msk[cp] = 4
cur += 1
msk[bur[:s]] = 2
return idx2rc(idx[idx>0], acc)
def ridge(img, mark, up=True):
oimg, omark = img, mark
ndim = img.ndim
mark[[0,-1],:] = 4
mark[:,[0,-1]] = 4
nb4 = nbs4(*img.shape)
nb8 = nbs8(*img.shape)
acc = np.cumprod((1,)+img.shape[::-1][:-1])[::-1]
img = img.ravel()
mark = mark.ravel()
pts = np.zeros(131072, dtype=np.int64)
s, bins = collect(img, mark, nb4, pts)
#print(bins)
aaa=0
for level in range(len(bins))[::1 if up else -1]:
if bins[level]==0:continue
aaa+=1
s, c = clear(mark, pts, s, 0)
s = step(img, mark, pts, s, level, up, nb4, nb8)
'''
if level>250:
plt.imshow(omark, cmap='gray')
plt.show()
'''
for i in range(len(mark)):
if mark[i] == 3:mark[i] = 255
else: mark[i] = 0
def neighbors(shape):
dim = len(shape)
block = generate_binary_structure(dim, 1)
block[tuple([1]*dim)] = 0
idx = np.where(block>0)
idx = np.array(idx, dtype=np.uint8).T
idx = np.array(idx-[1]*dim)
acc = np.cumprod((1,)+shape[::-1][:-1])
return np.dot(idx, acc[::-1])
def draw_graph(img, graph, cn=255, ce=128):
acc = np.cumprod((1,)+img.shape[::-1][:-1])[::-1]
img = img.ravel()
for idx in graph.nodes():
pts = graph.node[idx]['pts']
img[np.dot(pts, acc)] = cn
for (s, e) in graph.edges():
eds = graph[s][e]
for i in eds:
pts = eds[i]['pts']
img[np.dot(pts, acc)] = ce
def run(self, ips, snap, img, para = None):
ips.lut = self.buflut
k, unit = ips.unit
lev, ds, step = para['thr'], para['ds'], para['step']
scube = np.cumprod(ips.imgs.shape)[-1] * k**3
sfront = (ips.imgs[::ds,::ds,::ds]>lev).sum() * ds ** 3 * k**3
sback = scube - sfront
print(scube, sfront, sback)
vts, fs, ns, cs = marching_cubes_lewiner(ips.imgs[::ds,::ds,::ds], lev, step_size=step)
area = mesh_surface_area(vts, fs) * (ds**2 * k **2)
rst = [round(i,3) for i in [scube, sfront, sback, sfront/scube, area, area/sfront]]
titles = ['Cube Volume', 'Volume', 'Blank', 'Volume/Cube', 'Surface', 'Volume/Surface']
IPy.table('Volume Measure', [rst], cols=titles)
def _map_global_to_filtered(self, k):
"""
map global (unfiltered) ND key to local (filtered) 2D key
Parameters
----------
k: tuple
Labels associated with the modified element of the non-filtered array.
Returns
-------
tuple
Positional index (row, column) of the modified data cell.
"""
assert isinstance(k, tuple) and len(k) == self.la_data.ndim
dkey = {axis_id: axis_key for axis_key, axis_id in zip(k, self.la_data.axes.ids)}
# transform global dictionary key to "local" (filtered) key by removing
# the parts of the key which are redundant with the filter
for axis_id, axis_filter in self.current_filter.items():
axis_key = dkey[axis_id]
if np.isscalar(axis_filter) and axis_key == axis_filter:
del dkey[axis_id]
elif not np.isscalar(axis_filter) and axis_key in axis_filter:
pass
else:
# that key is invalid for/outside the current filter
return None
# transform (axis:label) dict key to positional ND key
try:
index_key = self.filtered_data._translated_key(dkey)
except ValueError:
return None
# transform positional ND key to positional 2D key
strides = np.append(1, np.cumprod(self.filtered_data.shape[1:-1][::-1], dtype=int))[::-1]
return (index_key[:-1] * strides).sum(), index_key[-1]