本文整理汇总了Python中numpy.trapz方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.trapz方法的具体用法?Python numpy.trapz怎么用?Python numpy.trapz使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.trapz方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_gamma
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import trapz [as 别名]
def test_gamma():
tsample = 0.005 / 1000
with pytest.raises(ValueError):
t, g = gamma(0, 0.1, tsample)
with pytest.raises(ValueError):
t, g = gamma(2, -0.1, tsample)
with pytest.raises(ValueError):
t, g = gamma(2, 0.1, -tsample)
for tau in [0.001, 0.01, 0.1]:
for n in [1, 2, 5]:
t, g = gamma(n, tau, tsample)
npt.assert_equal(np.arange(0, t[-1] + tsample / 2.0, tsample), t)
if n > 1:
npt.assert_equal(g[0], 0.0)
# Make sure area under the curve is normalized
npt.assert_almost_equal(np.trapz(np.abs(g), dx=tsample), 1.0,
decimal=2)
# Make sure peak sits correctly
npt.assert_almost_equal(g.argmax() * tsample, tau * (n - 1))
示例2: posterior_mean
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import trapz [as 别名]
def posterior_mean(self, x):
r"""
Computes the posterior mean by computing the first moment of the
estimated posterior CDF.
Arguments:
x(np.array): Array of shape `(n, m)` containing `n` inputs for which
to predict the posterior mean.
Returns:
Array containing the posterior means for the provided inputs.
"""
y_pred, qs = self.cdf(x)
mus = y_pred[-1] - np.trapz(qs, x=y_pred)
return mus
示例3: integrate_column
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import trapz [as 别名]
def integrate_column(y, x=None, axis=0):
"""Integrate array along an arbitrary axis.
Note:
This function is just a wrapper for :func:`numpy.trapz`.
Parameters:
y (ndarray): Data array.
x (ndarray): Coordinate array.
axis (int): Axis to integrate along for multidimensional input.
Returns:
float or ndarray: Column integral.
Examples:
>>> import numpy as np
>>> x = np.linspace(0, 1, 5)
>>> y = np.arange(5)
>>> integrate_column(y)
8.0
>>> integrate_column(y, x)
2.0
"""
return np.trapz(y, x, axis=axis)
示例4: test_resample
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import trapz [as 别名]
def test_resample(collection):
coll = collection.clone()
transform.ToDense(coll, 'parametric gain', output='pg_dense')
pg = coll.variables['pg_dense']
old_shape = pg.values.shape
old_auc = np.trapz(np.abs(pg.values.values.squeeze()), dx=0.1)
transform.Resample(coll, 'pg_dense', 1)
pg = coll.variables['pg_dense']
new_shape = pg.values.shape
# Spacing (dx) is 10* larger when downsampled fro 10hz to 1hz
new_auc = np.trapz(np.abs(pg.values.values.squeeze()), dx=1)
# Shape from 10hz to 1hz
assert new_shape[0] == old_shape[0] / 10
# Assert that the auc is more or less the same (not exact, rounding error)
# Values are around 0.25
assert np.allclose(old_auc, new_auc, rtol=0.05)
示例5: calc_spec
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import trapz [as 别名]
def calc_spec(self, zi=None, loc='near', npad=1, estimate_ph_sp_dens=1):
field = self.rad_field(zi=zi, loc=loc)
axis = field.ndim - 1
spec = np.abs(np.fft.fft(field, axis=axis))**2
spec = np.fft.fftshift(spec, axes=axis)
scale_ev = h_eV_s * speed_of_light * (np.fft.fftfreq(self.nSlices, d=self.s[1]-self.s[0]) + 1 / self.lambdaref)
scale_ev = np.fft.fftshift(scale_ev)
if estimate_ph_sp_dens:
tt=np.trapz(spec, scale_ev, axis=axis)
if axis==1:
tt[tt==0] = np.inf
spec *= (self.n_photons / tt)[:, np.newaxis]
else:
if tt==0:
tt = np.inf
spec *= (self.n_photons[zi] / tt)
return scale_ev, spec
示例6: AUC
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import trapz [as 别名]
def AUC(x, y):
'''
AUC - area under curve
Note: area under curve wich had been computed by standard trapezial
method (np.trapz)
Args:
x (numpy.ndarray): array of one metric rate (1D)
y (numpy.ndarray): array of another metric rate (1D)
Returns:
float - area under curve
'''
return np.trapz(y, x)
示例7: test_tlcorrection
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import trapz [as 别名]
def test_tlcorrection(self):
# testing long correction function
x_for_long = np.array([20.,21.,22.,23.,24.,25.])
y_for_long = np.array([1.0,1.0,1.0,1.0,1.0,1.0])
nu0 = 1.0/(514.532)*1e9 #laser wavenumber at 514.532
nu = 100.0*x_for_long # cm-1 to m-1
T = 23.0+273.15 # the temperature in K
x_long,long_res,eselong = rp.tlcorrection(x_for_long, y_for_long,23.0,514.532,correction = 'long',normalisation='area') # using the function
t0 = nu0**3.0*nu/((nu0-nu)**4)
t1= 1.0 - np.exp(-h*c*nu/(k*T)) # c in m/s : t1 dimensionless
long_calc= y_for_long*t0*t1 # pour les y
long_calc = long_calc/np.trapz(long_calc,x_for_long) # area normalisation
np.testing.assert_equal(long_res,long_calc)
np.testing.assert_equal(x_for_long,x_long)
x_long,long_res,eselong = rp.tlcorrection(x_for_long, y_for_long,23.0,514.532,correction = 'long',normalisation='no') # using the function
t0 = nu0**3.0*nu/((nu0-nu)**4)
t1= 1.0 - np.exp(-h*c*nu/(k*T)) # c in m/s : t1 dimensionless
long_calc= y_for_long*t0*t1 # pour les y
np.testing.assert_equal(long_res,long_calc)
np.testing.assert_equal(x_for_long,x_long)
示例8: interpolate_basis
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import trapz [as 别名]
def interpolate_basis(self, basis, dt, dt_max,
norm=True):
# Interpolate basis at the resolution of the data
L,B = basis.shape
t_int = np.arange(0.0, dt_max, step=dt)
t_bas = np.linspace(0.0, dt_max, L)
ibasis = np.zeros((len(t_int), B))
for b in np.arange(B):
ibasis[:,b] = np.interp(t_int, t_bas, basis[:,b])
# Normalize so that the interpolated basis has volume 1
if norm:
# ibasis /= np.trapz(ibasis,t_int,axis=0)
ibasis /= (dt * np.sum(ibasis, axis=0))
if not self.allow_instantaneous:
# Typically, the impulse responses are applied to times
# (t+1:t+R). That means we need to prepend a row of zeros to make
# sure the basis remains causal
ibasis = np.vstack((np.zeros((1,B)), ibasis))
return ibasis
示例9: test_compute_rate
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import trapz [as 别名]
def test_compute_rate():
K = 1
T = 100
dt = 1.0
network_hypers = {'c': np.zeros(K, dtype=np.int), 'p': 1.0, 'kappa': 10.0, 'v': 10*5.0}
true_model = DiscreteTimeNetworkHawkesModelSpikeAndSlab(K=K, dt=dt,
network_hypers=network_hypers)
S,R = true_model.generate(T=T)
print("Expected number of events: ", np.trapz(R, dt * np.arange(T), axis=0))
print("Actual number of events: ", S.sum(axis=0))
print("Lambda0: ", true_model.bias_model.lambda0)
print("W: ", true_model.weight_model.W)
print("")
R_test = true_model.compute_rate()
assert np.allclose(R, R_test)
示例10: test_generate_statistics
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import trapz [as 别名]
def test_generate_statistics():
K = 1
T = 100
dt = 1.0
network_hypers = {'c': np.zeros(K, dtype=np.int), 'p': 1.0, 'kappa': 10.0, 'v': 10*5.0}
true_model = DiscreteTimeNetworkHawkesModelSpikeAndSlab(K=K, dt=dt,
network_hypers=network_hypers)
S,R = true_model.generate(T=T)
E_N = np.trapz(R, dt * np.arange(T), axis=0)
std_N = np.sqrt(E_N)
N = S.sum(axis=0)
assert np.all(N >= E_N - 3*std_N), "N less than 3std below mean"
assert np.all(N <= E_N + 3*std_N), "N more than 3std above mean"
print("Expected number of events: ", E_N)
print("Actual number of events: ", S.sum(axis=0))
示例11: test_RTAP_to_diameter_callaghan
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import trapz [as 别名]
def test_RTAP_to_diameter_callaghan(samples=10000):
mu = [0, 0]
lambda_par = 1.7
diameter = 10e-6
delta = np.tile(1e-10, samples) # delta towards zero
Delta = np.tile(1e10, samples) # Delta towards infinity
qvals_perp = np.linspace(0, 10e6, samples)
n_perp = np.tile(np.r_[1., 0., 0.], (samples, 1))
scheme = acquisition_scheme_from_qvalues(qvals_perp, n_perp, delta, Delta)
callaghan = cylinder_models.C3CylinderCallaghanApproximation(
mu=mu, lambda_par=lambda_par, diameter=diameter)
E_callaghan = callaghan(scheme)
rtap_callaghan = 2 * np.pi * np.trapz(
E_callaghan * qvals_perp, x=qvals_perp
)
diameter_callaghan = 2 / np.sqrt(np.pi * rtap_callaghan)
assert_almost_equal(diameter_callaghan, diameter, 7)
示例12: test_RTPP_to_length_callaghan
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import trapz [as 别名]
def test_RTPP_to_length_callaghan(samples=1000):
length = 10e-6
delta = 1e-10 # delta towards zero
Delta = 1e10 # Delta towards infinity
qvals_perp = np.linspace(0, 10e6, samples)
n_perp = np.tile(np.r_[1., 0., 0.], (samples, 1))
scheme = acquisition_scheme_from_qvalues(qvals_perp, n_perp, delta, Delta)
plane = plane_models.P3PlaneCallaghanApproximation(diameter=length)
E_callaghan = plane(scheme)
rtpp_callaghan = 2 * np.trapz(E_callaghan, x=qvals_perp)
length_callaghan = 1 / rtpp_callaghan
assert_almost_equal(length_callaghan, length, 7)
示例13: test_RTPP_to_diameter_soderman
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import trapz [as 别名]
def test_RTPP_to_diameter_soderman(samples=1000):
"""This tests if the RTPP of the plane relates correctly to the diameter
of the plane."""
diameter = 10e-6
delta = np.tile(1e-10, samples) # delta towards zero
Delta = np.tile(1e10, samples) # Delta towards infinity
qvals_perp = np.linspace(0, 10e6, samples)
n_perp = np.tile(np.r_[1., 0., 0.], (samples, 1))
scheme = acquisition_scheme_from_qvalues(
qvals_perp, n_perp, delta, Delta)
soderman = plane_models.P2PlaneStejskalTannerApproximation(
diameter=diameter)
E_soderman = soderman(scheme)
rtpp_soderman = 2 * np.trapz(
E_soderman, x=qvals_perp
)
diameter_soderman = 1 / rtpp_soderman
assert_almost_equal(diameter_soderman, diameter, 7)
示例14: test_RTOP_to_diameter_soderman
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import trapz [as 别名]
def test_RTOP_to_diameter_soderman(samples=1000):
"""This tests if the RTAP of the sphere relates correctly to the diameter
of the sphere."""
diameter = 10e-6
delta = np.tile(1e-10, samples) # delta towards zero
Delta = np.tile(1e10, samples) # Delta towards infinity
qvals_perp = np.linspace(0, 10e6, samples)
n_perp = np.tile(np.r_[1., 0., 0.], (samples, 1))
scheme = acquisition_scheme_from_qvalues(
qvals_perp, n_perp, delta, Delta)
soderman = sphere_models.S2SphereStejskalTannerApproximation(
diameter=diameter)
E_soderman = soderman(scheme)
rtop_soderman = 4 * np.pi * np.trapz(
E_soderman * qvals_perp ** 2, x=qvals_perp
)
sphere_volume = 1. / rtop_soderman
diameter_soderman = 2 * (sphere_volume / ((4. / 3.) * np.pi)) ** (1. / 3.)
assert_almost_equal(diameter_soderman, diameter, 7)
示例15: _normalize_functions
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import trapz [as 别名]
def _normalize_functions(y_values_list, t_values):
"""Normalize list of functions by their integral value
Parameters
----------
y_values_list : `list` of np.ndarray
y values of the list of function we want to normalize
t_values : `np.ndarray`
t values shared by all functions given with y_values_list
Returns
-------
normalized_y_values_list : `list` of np.ndarray
Normalized y values of the given list of function
normalizations : `np.ndarray`
Normalization factors that have been used
"""
y_values_list = np.array(y_values_list)
normalizations = [
1. / np.trapz(y_values, t_values) for y_values in y_values_list
]
normalized_y_values_list = (y_values_list.T * normalizations).T
return normalized_y_values_list, normalizations