本文整理汇总了Python中numpy.gradient方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.gradient方法的具体用法?Python numpy.gradient怎么用?Python numpy.gradient使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.gradient方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_log_interp_diff
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import gradient [as 别名]
def test_log_interp_diff(self):
""" Test the differentiation facility from the class log_interp_c """
#import matplotlib.pyplot as plt
rr,pp = funct_log_mesh(1024, 0.001, 20.0)
logi = log_interp_c(rr)
gc = 1.2030
ff = np.array([np.exp(-gc*r**2) for r in rr])
ffd_ref = np.array([np.exp(-gc*r**2)*(-2.0*gc*r) for r in rr])
ffd = logi.diff(ff)
ffd_np = np.gradient(ff, rr)
s = 3
for r,d,dref,dnp in zip(rr[s:],ffd[s:],ffd_ref[s:],ffd_np[s:]):
self.assertAlmostEqual(d,dref)
#plt.plot(rr, ff, '-', label='ff')
#plt.plot(rr, ffd, '--', label='ffd')
#plt.legend()
#plt.show()
示例2: _ecg_delineator_peak_P_onset
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import gradient [as 别名]
def _ecg_delineator_peak_P_onset(rpeak, heartbeat, R, P):
if P is None:
return np.nan
segment = heartbeat.iloc[:P] # Select left of P wave
try:
signal = signal_smooth(segment["Signal"].values, size=R / 10)
except TypeError:
signal = segment["Signal"]
if len(signal) < 2:
return np.nan
signal = np.gradient(np.gradient(signal))
P_onset = np.argmax(signal)
from_R = R - P_onset # Relative to R
return rpeak - from_R
示例3: _ecg_delineator_peak_T_offset
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import gradient [as 别名]
def _ecg_delineator_peak_T_offset(rpeak, heartbeat, R, T):
if T is None:
return np.nan
segment = heartbeat.iloc[R + T :] # Select left of P wave
try:
signal = signal_smooth(segment["Signal"].values, size=R / 10)
except TypeError:
signal = segment["Signal"]
if len(signal) < 2:
return np.nan
signal = np.gradient(np.gradient(signal))
T_offset = np.argmax(signal)
return rpeak + T + T_offset
# =============================================================================
# Internals
# =============================================================================
示例4: check_segmentation
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import gradient [as 别名]
def check_segmentation(fn_subject):
from scipy import ndimage
import matplotlib.pylab as pl
from matplotlib.colors import ListedColormap
files = simnibs.SubjectFiles(fn_subject + '.msh')
T1 = nib.load(files.T1)
masks = nib.load(files.final_contr).get_data()
lines = np.linalg.norm(np.gradient(masks), axis=0) > 0
print(lines.shape)
viewer = NiftiViewer(T1.get_data(), T1.affine)
cmap = pl.cm.jet
my_cmap = cmap(np.arange(cmap.N))
my_cmap[:,-1] = np.linspace(0, 1, cmap.N)
my_cmap = ListedColormap(my_cmap)
viewer.add_overlay(lines, cmap=my_cmap)
viewer.show()
示例5: test_args
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import gradient [as 别名]
def test_args(self):
dx = np.cumsum(np.ones(5))
dx_uneven = [1., 2., 5., 9., 11.]
f_2d = np.arange(25).reshape(5, 5)
# distances must be scalars or have size equal to gradient[axis]
gradient(np.arange(5), 3.)
gradient(np.arange(5), np.array(3.))
gradient(np.arange(5), dx)
# dy is set equal to dx because scalar
gradient(f_2d, 1.5)
gradient(f_2d, np.array(1.5))
gradient(f_2d, dx_uneven, dx_uneven)
# mix between even and uneven spaces and
# mix between scalar and vector
gradient(f_2d, dx, 2)
# 2D but axis specified
gradient(f_2d, dx, axis=1)
# 2d coordinate arguments are not yet allowed
assert_raises_regex(ValueError, '.*scalars or 1d',
gradient, f_2d, np.stack([dx]*2, axis=-1), 1)
示例6: test_specific_axes
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import gradient [as 别名]
def test_specific_axes(self):
# Testing that gradient can work on a given axis only
v = [[1, 1], [3, 4]]
x = np.array(v)
dx = [np.array([[2., 3.], [2., 3.]]),
np.array([[0., 0.], [1., 1.]])]
assert_array_equal(gradient(x, axis=0), dx[0])
assert_array_equal(gradient(x, axis=1), dx[1])
assert_array_equal(gradient(x, axis=-1), dx[1])
assert_array_equal(gradient(x, axis=(1, 0)), [dx[1], dx[0]])
# test axis=None which means all axes
assert_almost_equal(gradient(x, axis=None), [dx[0], dx[1]])
# and is the same as no axis keyword given
assert_almost_equal(gradient(x, axis=None), gradient(x))
# test vararg order
assert_array_equal(gradient(x, 2, 3, axis=(1, 0)),
[dx[1]/2.0, dx[0]/3.0])
# test maximal number of varargs
assert_raises(TypeError, gradient, x, 1, 2, axis=1)
assert_raises(np.AxisError, gradient, x, axis=3)
assert_raises(np.AxisError, gradient, x, axis=-3)
# assert_raises(TypeError, gradient, x, axis=[1,])
示例7: bullen
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import gradient [as 别名]
def bullen(self, depth):
"""
Returns the Bullen parameter only for significant arrays
"""
assert(len(depth) > 3)
v_phi = self.v_phi(depth)
density = self.density(depth)
phi = v_phi * v_phi
kappa = phi * density
try:
dkappadP = np.gradient(kappa, edge_order=2) / \
np.gradient(self.pressure(depth), edge_order=2)
dphidz = np.gradient(phi,
edge_order=2) / np.gradient(depth,
edge_order=2) / self.gravity(depth)
except:
dkappadP = np.gradient(kappa) / np.gradient(self.pressure(depth))
dphidz = np.gradient(phi) / np.gradient(depth) / self.gravity(depth)
bullen = dkappadP - dphidz
return bullen
示例8: bullen
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import gradient [as 别名]
def bullen(self):
"""
Returns the Bullen parameter across the layer.
The Bullen parameter assess if compression as a function of pressure is
like homogeneous, adiabatic compression.
Bullen parameter =1 , homogeneous, adiabatic compression
Bullen parameter > 1 , more compressed with pressure, e.g. across phase transitions
Bullen parameter < 1, less compressed with pressure, e.g. across a boundary layer
"""
kappa = self.bulk_sound_velocity * self.bulk_sound_velocity * self.density
phi = self.bulk_sound_velocity * self.bulk_sound_velocity
try:
dkappadP = np.gradient(kappa, edge_order=2) / \
np.gradient(self.pressures, edge_order=2)
dphidr = np.gradient(phi,edge_order=2) / np.gradient(self.radii,edge_order=2) / self.gravity
except:
dkappadP = np.gradient(kappa) / \
np.gradient(self.pressures)
dphidr = np.gradient(phi) / np.gradient(self.radii) / self.gravity
bullen = dkappadP + dphidr
return bullen
示例9: Hillshade
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import gradient [as 别名]
def Hillshade(raster_file, azimuth, angle_altitude):
array = ReadRasterArrayBlocks(raster_file,raster_band=1)
x, y = np.gradient(array)
slope = np.pi/2. - np.arctan(np.sqrt(x*x + y*y))
aspect = np.arctan2(-x, y)
azimuthrad = np.azimuth*np.pi / 180.
altituderad = np.angle_altitude*np.pi / 180.
shaded = np.sin(altituderad) * np.sin(slope)\
+ np.cos(altituderad) * np.cos(slope)\
* np.cos(azimuthrad - aspect)
return 255*(shaded + 1)/2
#==============================================================================
示例10: test_specific_axes
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import gradient [as 别名]
def test_specific_axes(self):
# Testing that gradient can work on a given axis only
v = [[1, 1], [3, 4]]
x = np.array(v)
dx = [np.array([[2., 3.], [2., 3.]]),
np.array([[0., 0.], [1., 1.]])]
assert_array_equal(gradient(x, axis=0), dx[0])
assert_array_equal(gradient(x, axis=1), dx[1])
assert_array_equal(gradient(x, axis=-1), dx[1])
assert_array_equal(gradient(x, axis=(1, 0)), [dx[1], dx[0]])
# test axis=None which means all axes
assert_almost_equal(gradient(x, axis=None), [dx[0], dx[1]])
# and is the same as no axis keyword given
assert_almost_equal(gradient(x, axis=None), gradient(x))
# test vararg order
assert_array_equal(gradient(x, 2, 3, axis=(1, 0)), [dx[1]/2.0, dx[0]/3.0])
# test maximal number of varargs
assert_raises(SyntaxError, gradient, x, 1, 2, axis=1)
assert_raises(ValueError, gradient, x, axis=3)
assert_raises(ValueError, gradient, x, axis=-3)
assert_raises(TypeError, gradient, x, axis=[1,])
示例11: get_external_state
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import gradient [as 别名]
def get_external_state(self):
"""
Attempt to include avg decomp. of original normalised spread
"""
x_sma = np.stack(
[
feature.get(size=self.p.time_dim) for feature in self.data.features
],
axis=-1
)
scale = 1 / np.clip(self.data.std[0], 1e-10, None)
x_sma *= scale # <-- more or less ok
# Gradient along features axis:
dx = np.gradient(x_sma, axis=-1)
# TODO: different conv. encoders for these two types of features:
x = np.concatenate([x_sma, dx], axis=-1)
# Crop outliers:
x = np.clip(x, -10, 10)
return x[:, None, :]
示例12: get_data_model_state
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import gradient [as 别名]
def get_data_model_state(self):
"""
Spread stochastic model parameters.
"""
state = self.data_model.s.process.get_state()
cross_corr = cov2corr(state.filtered.covariance)[[0, 0, 1], [1, 2, 2]]
update = np.concatenate(
[
state.filtered.mean.flatten(),
state.filtered.variance.flatten(),
cross_corr,
]
)
self.external_model_state = np.concatenate(
[
self.external_model_state[1:, :, :],
update[None, None, :]
],
axis=0
)
# self.external_model_state = np.gradient(self.external_model_state, axis=-1)
return self.external_model_state
示例13: get_external_state
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import gradient [as 别名]
def get_external_state(self):
x = np.stack(
[
np.frombuffer(self.data.open.get(size=self.time_dim)),
np.frombuffer(self.data.sma_4.get(size=self.time_dim)),
np.frombuffer(self.data.sma_8.get(size=self.time_dim)),
np.frombuffer(self.data.sma_16.get(size=self.time_dim)),
np.frombuffer(self.data.sma_32.get(size=self.time_dim)),
np.frombuffer(self.data.sma_64.get(size=self.time_dim)),
np.frombuffer(self.data.sma_128.get(size=self.time_dim)),
np.frombuffer(self.data.sma_256.get(size=self.time_dim)),
],
axis=-1
)
# Gradient along features axis:
x = np.gradient(x, axis=1) * self.p.state_ext_scale
# Log-scale:
x = log_transform(x)
return x[:, None, :]
示例14: get_external_state
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import gradient [as 别名]
def get_external_state(self):
# Use Hi-Low median as signal:
x = (
np.frombuffer(self.data.high.get(size=self.time_dim)) +
np.frombuffer(self.data.low.get(size=self.time_dim))
) / 2
# Differences along time dimension:
d_x = np.gradient(x, axis=0) * self.p.cwt_signal_scale
# Compute continuous wavelet transform using Ricker wavelet:
cwt_x = signal.cwt(d_x, signal.ricker, self.cwt_width).T
# Note: differences taken once again along channels axis,
# apply weighted scaling to normalize channels
norm_x = np.gradient(cwt_x, axis=-1)
norm_x = zscore(norm_x, axis=0) * self.p.state_ext_scale
#out_x = tanh(norm_x)
out_x = np.clip(norm_x, -10, 10)
return out_x[:, None, :]
示例15: get_hessian
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import gradient [as 别名]
def get_hessian(ccm, hes_norm=True, hes_smth=False, **kwargs):
""" Find Hessian of the input cross correlation matrix <ccm>
Parameters
----------
ccm : 2D numpy array, cross-correlation matrix
hes_norm : bool, normalize Hessian by AVG and STD?
hes_smth : bool, smooth Hessian?
"""
if hes_smth:
ccm2 = nd.filters.gaussian_filter(ccm, 1)
else:
ccm2 = ccm
# Jacobian components
dcc_dy, dcc_dx = np.gradient(ccm2)
# Hessian components
d2cc_dx2 = np.gradient(dcc_dx)[1]
d2cc_dy2 = np.gradient(dcc_dy)[0]
hes = np.hypot(d2cc_dx2, d2cc_dy2)
if hes_norm:
hes = (hes - np.median(hes)) / np.std(hes)
return hes