def set_ufunc(self, scalar_op):
# This is probably a speed up of the implementation
if isinstance(scalar_op, theano.scalar.basic.Add):
self.ufunc = numpy.add
elif isinstance(scalar_op, theano.scalar.basic.Mul):
self.ufunc = numpy.multiply
elif isinstance(scalar_op, theano.scalar.basic.Maximum):
self.ufunc = numpy.maximum
elif isinstance(scalar_op, theano.scalar.basic.Minimum):
self.ufunc = numpy.minimum
elif isinstance(scalar_op, theano.scalar.basic.AND):
self.ufunc = numpy.bitwise_and
elif isinstance(scalar_op, theano.scalar.basic.OR):
self.ufunc = numpy.bitwise_or
elif isinstance(scalar_op, theano.scalar.basic.XOR):
self.ufunc = numpy.bitwise_xor
else:
self.ufunc = numpy.frompyfunc(scalar_op.impl, 2, 1)
python类frompyfunc()的实例源码
def _KeenerMatrix(self, A, C, regularization, func, epsilon):
"""func is a regularization function imposed on every element of matrix.
"""
# Apply Laplace Law
B = A+A.T+2;
A = A+1
A = A/B
# Regularization
if func is not None:
h = np.frompyfunc(func, 1, 1)
A = np.require(h(A), dtype=np.float32)
# divide by contest number
C = C+C.T
c = np.sum(C, axis=1)
if regularization:
A = A/np.expand_dims(c, axis=1)
A[C==0]=0
if epsilon is not None:
A += epsilon*np.ones(A.shape, A.dtype)
return A
def ztnb_pmf(y, mu, alpha):
r = 1.0 / alpha
if y <= 0:
raise Exception('y must be larger than 0.')
p = mu/(mu+r+0.0)
ztnbin_mpmath = lambda y, p, r: mpmath.gamma(y + r)/(mpmath.gamma(y+1)*mpmath.gamma(r))*np.power(1-p, r)*np.power(p, y)/(1-np.power(1-p, r))
ztnbin = np.frompyfunc(ztnbin_mpmath, 3, 1)
return float(ztnbin(y, p, r))
def test_frompyfunc_endian(self, level=rlevel):
# Ticket #503
from math import radians
uradians = np.frompyfunc(radians, 1, 1)
big_endian = np.array([83.4, 83.5], dtype='>f8')
little_endian = np.array([83.4, 83.5], dtype='<f8')
assert_almost_equal(uradians(big_endian).astype(float),
uradians(little_endian).astype(float))
def test_frompyfunc_many_args(self):
# gh-5672
def passer(*args):
pass
assert_raises(ValueError, np.frompyfunc, passer, 32, 1)
def test_frompyfunc_nout_0(self):
# gh-2014
def f(x):
x[0], x[-1] = x[-1], x[0]
uf = np.frompyfunc(f, 1, 0)
a = np.array([[1, 2, 3], [4, 5], [6, 7, 8, 9]])
assert_equal(uf(a), ())
assert_array_equal(a, [[3, 2, 1], [5, 4], [9, 7, 8, 6]])
def test_frompyfunc_endian(self, level=rlevel):
# Ticket #503
from math import radians
uradians = np.frompyfunc(radians, 1, 1)
big_endian = np.array([83.4, 83.5], dtype='>f8')
little_endian = np.array([83.4, 83.5], dtype='<f8')
assert_almost_equal(uradians(big_endian).astype(float),
uradians(little_endian).astype(float))
def test_frompyfunc_many_args(self):
# gh-5672
def passer(*args):
pass
assert_raises(ValueError, np.frompyfunc, passer, 32, 1)
def test_frompyfunc_nout_0(self):
# gh-2014
def f(x):
x[0], x[-1] = x[-1], x[0]
uf = np.frompyfunc(f, 1, 0)
a = np.array([[1, 2, 3], [4, 5], [6, 7, 8, 9]])
assert_equal(uf(a), ())
assert_array_equal(a, [[3, 2, 1], [5, 4], [9, 7, 8, 6]])
pytables.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def _maybe_convert(values, val_kind, encoding):
if _need_convert(val_kind):
conv = _get_converter(val_kind, encoding)
# conv = np.frompyfunc(conv, 1, 1)
values = conv(values)
return values
test_regression.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 43
收藏 0
点赞 0
评论 0
def test_frompyfunc_endian(self, level=rlevel):
# Ticket #503
from math import radians
uradians = np.frompyfunc(radians, 1, 1)
big_endian = np.array([83.4, 83.5], dtype='>f8')
little_endian = np.array([83.4, 83.5], dtype='<f8')
assert_almost_equal(uradians(big_endian).astype(float),
uradians(little_endian).astype(float))
test_regression.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 29
收藏 0
点赞 0
评论 0
def test_frompyfunc_many_args(self):
# gh-5672
def passer(*args):
pass
assert_raises(ValueError, np.frompyfunc, passer, 32, 1)
test_regression.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 46
收藏 0
点赞 0
评论 0
def test_frompyfunc_nout_0(self):
# gh-2014
def f(x):
x[0], x[-1] = x[-1], x[0]
uf = np.frompyfunc(f, 1, 0)
a = np.array([[1, 2, 3], [4, 5], [6, 7, 8, 9]])
assert_equal(uf(a), ())
assert_array_equal(a, [[3, 2, 1], [5, 4], [9, 7, 8, 6]])
def draw_mandelbrot(cx, cy, d):
"""
???(cx, cy)????d????Mandelbrot
"""
x0, x1, y0, y1 = cx-d, cx+d, cy-d, cy+d
y, x = np.ogrid[y0:y1:200j, x0:x1:200j]
c = x + y*1j
start = time.clock()
mandelbrot = np.frompyfunc(iter_point,1,1)(c).astype(np.float)
print("time=",time.clock() - start)
pl.imshow(mandelbrot, cmap=cm.jet, extent=[x0,x1,y0,y1])
#pl.gca().set_axis_off()
def test_frompyfunc_endian(self, level=rlevel):
# Ticket #503
from math import radians
uradians = np.frompyfunc(radians, 1, 1)
big_endian = np.array([83.4, 83.5], dtype='>f8')
little_endian = np.array([83.4, 83.5], dtype='<f8')
assert_almost_equal(uradians(big_endian).astype(float),
uradians(little_endian).astype(float))
def test_frompyfunc_many_args(self):
# gh-5672
def passer(*args):
pass
assert_raises(ValueError, np.frompyfunc, passer, 32, 1)
def test_frompyfunc_nout_0(self):
# gh-2014
def f(x):
x[0], x[-1] = x[-1], x[0]
uf = np.frompyfunc(f, 1, 0)
a = np.array([[1, 2, 3], [4, 5], [6, 7, 8, 9]])
assert_equal(uf(a), ())
assert_array_equal(a, [[3, 2, 1], [5, 4], [9, 7, 8, 6]])
def test_frompyfunc_endian(self, level=rlevel):
# Ticket #503
from math import radians
uradians = np.frompyfunc(radians, 1, 1)
big_endian = np.array([83.4, 83.5], dtype='>f8')
little_endian = np.array([83.4, 83.5], dtype='<f8')
assert_almost_equal(uradians(big_endian).astype(float),
uradians(little_endian).astype(float))
def test_frompyfunc_many_args(self):
# gh-5672
def passer(*args):
pass
assert_raises(ValueError, np.frompyfunc, passer, 32, 1)
def test_frompyfunc_nout_0(self):
# gh-2014
def f(x):
x[0], x[-1] = x[-1], x[0]
uf = np.frompyfunc(f, 1, 0)
a = np.array([[1, 2, 3], [4, 5], [6, 7, 8, 9]])
assert_equal(uf(a), ())
assert_array_equal(a, [[3, 2, 1], [5, 4], [9, 7, 8, 6]])
def test_frompyfunc_endian(self, level=rlevel):
# Ticket #503
from math import radians
uradians = np.frompyfunc(radians, 1, 1)
big_endian = np.array([83.4, 83.5], dtype='>f8')
little_endian = np.array([83.4, 83.5], dtype='<f8')
assert_almost_equal(uradians(big_endian).astype(float),
uradians(little_endian).astype(float))
def test_frompyfunc_many_args(self):
# gh-5672
def passer(*args):
pass
assert_raises(ValueError, np.frompyfunc, passer, 32, 1)
def test_frompyfunc_nout_0(self):
# gh-2014
def f(x):
x[0], x[-1] = x[-1], x[0]
uf = np.frompyfunc(f, 1, 0)
a = np.array([[1, 2, 3], [4, 5], [6, 7, 8, 9]])
assert_equal(uf(a), ())
assert_array_equal(a, [[3, 2, 1], [5, 4], [9, 7, 8, 6]])
def __setstate__(self, d):
super(Elemwise, self).__setstate__(d)
self.ufunc = None
self.nfunc = None
if getattr(self, 'nfunc_spec', None):
self.nfunc = getattr(numpy, self.nfunc_spec[0])
elif 0 < self.scalar_op.nin < 32:
self.ufunc = numpy.frompyfunc(self.scalar_op.impl,
self.scalar_op.nin,
self.scalar_op.nout)
self._rehash()
def compute_optimal_scales(self):
"""Form a set of scales to use in the wavelet transform.
For non-orthogonal wavelet analysis, one can use an
arbitrary set of scales.
It is convenient to write the scales as fractional powers of
two:
s_j = s_0 * 2 ** (j * dj), j = 0, 1, ..., J
J = (1 / dj) * log2(N * dt / s_0)
s0 - smallest resolvable scale
J - largest scale
choose s0 so that the equivalent Fourier period is 2 * dt.
The choice of dj depends on the width in spectral space of
the wavelet function. For the morlet, dj=0.5 is the largest
that still adequately samples scale. Smaller dj gives finer
scale resolution.
"""
dt = self.dt
# resolution
dj = self.dj
# smallest resolvable scale, chosen so that the equivalent
# fourier period is approximately 2dt
s0 = self.s0
# Largest scale
J = int((1 / dj) * np.log2(self.N * dt / s0))
sj = s0 * 2 ** (dj * np.arange(0, J + 1))
return sj
# TODO: use np.frompyfunc on this
# TODO: can we just replace it with fftfreqs?
def compute_optimal_scales(self):
"""Form a set of scales to use in the wavelet transform.
For non-orthogonal wavelet analysis, one can use an
arbitrary set of scales.
It is convenient to write the scales as fractional powers of
two:
s_j = s_0 * 2 ** (j * dj), j = 0, 1, ..., J
J = (1 / dj) * log2(N * dt / s_0)
s0 - smallest resolvable scale
J - largest scale
choose s0 so that the equivalent Fourier period is 2 * dt.
The choice of dj depends on the width in spectral space of
the wavelet function. For the morlet, dj=0.5 is the largest
that still adequately samples scale. Smaller dj gives finer
scale resolution.
"""
dt = self.dt
# resolution
dj = self.dj
# smallest resolvable scale, chosen so that the equivalent
# fourier period is approximately 2dt
s0 = self.s0
# Largest scale
J = int((1 / dj) * np.log2(self.N * dt / s0))
sj = s0 * 2 ** (dj * np.arange(0, J + 1))
return sj
# TODO: use np.frompyfunc on this
# TODO: can we just replace it with fftfreqs?
def test_frompyfunc_endian(self, level=rlevel):
# Ticket #503
from math import radians
uradians = np.frompyfunc(radians, 1, 1)
big_endian = np.array([83.4, 83.5], dtype='>f8')
little_endian = np.array([83.4, 83.5], dtype='<f8')
assert_almost_equal(uradians(big_endian).astype(float),
uradians(little_endian).astype(float))
def test_frompyfunc_many_args(self):
# gh-5672
def passer(*args):
pass
assert_raises(ValueError, np.frompyfunc, passer, 32, 1)
def test_frompyfunc_nout_0(self):
# gh-2014
def f(x):
x[0], x[-1] = x[-1], x[0]
uf = np.frompyfunc(f, 1, 0)
a = np.array([[1, 2, 3], [4, 5], [6, 7, 8, 9]])
assert_equal(uf(a), ())
assert_array_equal(a, [[3, 2, 1], [5, 4], [9, 7, 8, 6]])
def __init__(self, p = 0.1, r = 10):
nbin_mpmath = lambda k, p, r: mpmath.gamma(k + r)/(mpmath.gamma(k+1)\
*mpmath.gamma(r))*np.power(1-p, r)*np.power(p, k)
self.nbin = np.frompyfunc(nbin_mpmath, 3, 1)
self.p = p
self.r = r