Here are the examples of the python api numpy.diff taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
166 Examples
6
Example 1
def test_diff(self):
# test simple
series = self.create()
d = series.diff()
d2 = self.TEST_CLASS(numpy.diff(series.value),
x0=series.x0 + series.dx)
self.assertArraysEqual(d, d2)
d = series.diff(n=2)
d2 = self.TEST_CLASS(numpy.diff(series.value, n=2),
x0=series.x0 + series.dx * 2)
self.assertArraysEqual(d, d2)
# test irregular xindex
x = numpy.logspace(0, 2, num=self.data.shape[0])
series = self.create(xindex=x)
d = series.diff(n=4)
d2 = self.TEST_CLASS(numpy.diff(series.value, n=4), x0=x[4])
self.assertArraysEqual(d, d2)
3
Example 2
@staticmethod
def from_arrays(x, F):
edf = EDF(None,None)
edf.x = x
edf.F = F
edf.dF = numpy.diff(edf.F)
return edf
3
Example 3
Project: kaggle-heart Source File: utils.py
def cdf_to_pdf(x):
if x.ndim==1:
res = np.diff(x, axis=0)
return np.hstack([x[:1], res])
elif x.ndim==2:
res = np.diff(x, axis=1)
return np.hstack([x[:, :1], res])
else:
return np.apply_along_axis(cdf_to_pdf, axis=-1, arr=x)
3
Example 4
def _validate_data(self):
if not self.x.size == self.y.size:
raise ValueError('x and y must be equal length 1D arrays')
if not all(np.diff(self.xhat)>0):
if np.alltrue(self.xhat == self.x):
raise ValueError('x must be monotonically increasing if a '
'separate xhat is not provided')
else:
raise ValueError('xhat must be monotonically increasing')
3
Example 5
Project: polar2grid Source File: swath.py
def _make_longitude_monotonic(lon_swath):
"""DEPRECATED!
Modify longitude in place to be monotonic -180..180 or 0..360
:param lon_swath: 2D numpy masked_array of longitude data
:return: modified array
"""
rows,cols = lon_swath.shape
shift = False
for r in range(rows):
dif = np.abs(np.diff(lon_swath[r,:].squeeze()))
if np.max(dif) > 180.0:
shift = True
break
if shift:
lon_swath[lon_swath < 0] += 360.0
return lon_swath
3
Example 6
def contiguous_regions(condition):
d = np.diff(condition) # Where the condition changes
idx, = d.nonzero() # Get the indices
idx += 1 # We were lagging behind the condition
if condition[0]: # Handle border conditions
idx = np.r_[0, idx]
if condition[-1]:
idx = np.r_[idx, condition.size]
idx.shape = (-1, 2)
return idx
3
Example 7
Project: hyperspy Source File: egerton_quantification.py
def detect_spike(self):
derivative = np.diff(self.signal())
if self.signal_mask is not None:
derivative[self.signal_mask[:-1]] = 0
if self.argmax is not None:
left, right = self.get_interpolation_range()
self._temp_mask[left:right] = True
derivative[self._temp_mask[:-1]] = 0
if abs(derivative.max()) >= self.threshold:
self.argmax = derivative.argmax()
self.derivmax = abs(derivative.max())
return True
else:
return False
3
Example 8
Project: lfd Source File: move_rope.py
def create_rope(rope_poss, capsule_height=.02):
rope_pos_dists = np.linalg.norm(np.diff(rope_poss, axis=0), axis=1)
xp = np.r_[0, np.cuemsum(rope_pos_dists/capsule_height)]
init_rope_nodes = mu.interp2d(np.arange(xp[-1]+1), xp, rope_poss)
rope_sim_obj = RopeSimulationObject("rope", init_rope_nodes)
return rope_sim_obj
3
Example 9
def get_fps(self):
"""Return the current FPS."""
if len(self.times) >= 2:
dif = np.diff(self.times)
fps = 1. / dif.min()
# if the FPS crosses 500, do not update it
if fps <= 500:
self.fps = fps
return self.fps
else:
return 0.
3
Example 10
Project: xppy Source File: data.py
def getDVDT(data, cols=[0,1]):
'''
Function calculated dV/dt over given data.
'''
return np.diff(data[:,cols[1]])/np.diff(data[:,cols[0]])
3
Example 11
Project: tsfresh Source File: feature_calculators.py
@set_property("fctype", "aggregate")
@not_apply_to_raw_numbers
def mean_change(x):
"""
Returns the mean over the absolute differences between subsequent time series values which is
.. math::
\\frac{1}{n} \\sum_{i=1,\ldots, n-1} x_{i+1} - x_{i}
:param x: the time series to calculate the feature of
:type x: pandas.Series
:return: the value of this feature
:return type: float
"""
return np.mean(np.diff(x))
3
Example 12
Project: OrthoFinder Source File: orthologues_from_recon_trees.py
def GetSpeciesGenesInfo(ogSet):
speciesLabels, nSpAll = util.GetSpeciesToUse(ogSet.speciesIDsFN)
seqsInfo = util.GetSeqsInfo(ogSet.workingDirOF, speciesLabels, nSpAll)
genenumbers = list(np.diff(seqsInfo.seqStartingIndices))
genenumbers.append(seqsInfo.nSeqs - seqsInfo.seqStartingIndices[-1])
return speciesLabels, genenumbers
3
Example 13
Project: aospy Source File: utils.py
def does_coord_increase_w_index(arr):
"""Determine if the array values increase with the index.
Useful, e.g., for pressure, which sometimes is indexed surface to TOA and
sometimes the opposite.
"""
diff = np.diff(arr)
if not np.all(np.abs(np.sign(diff))):
raise ValueError("Array is not monotonic: {}".format(arr))
# Since we know its monotonic, just test the first value.
return bool(diff[0])
3
Example 14
def get_peaks(spectrum, threshold):
sweep = OrderedDict(spectrum[max(spectrum)])
for freq, level in sweep.items():
if level < threshold:
del sweep[freq]
indices = (numpy.diff(numpy.sign(numpy.diff(sweep.values()))) < 0).nonzero()[0] + 1
return sweep, indices
3
Example 15
def _order_points(points):
s = points.sum(axis=1)
diff = np.diff(points, axis=1)
ordered_points = np.zeros((4,2), dtype='float32')
ordered_points[0] = points[np.argmin(s)]
ordered_points[2] = points[np.argmax(s)]
ordered_points[1] = points[np.argmin(diff)]
ordered_points[3] = points[np.argmax(diff)]
return ordered_points
3
Example 16
def show_glass(dataview, pad=10):
'''Create a classic "glass brain" view of the data, with the outline'''
import nibabel
nib = db.get_anat(subject, 'fiducial')
mask = nib.get_data()
left, right = np.nonzero(np.diff(mask.max(0).max(0)))[0][[0,-1]]
front, back = np.nonzero(np.diff(mask.max(0).max(1)))[0][[0,-1]]
top, bottom = np.nonzero(np.diff(mask.max(1).max(1)))[0][[0,-1]]
glass = np.zeros((mask.shape[1], mask.shape[2]*2), dtype=bool)
glass[:, :mask.shape[2]] = mask.max(0)
#this requires a lot of logic to put the image into a canonical orientation
#too much work for something we'll never use
raise NotImplementedError
3
Example 17
def __init__(self, z, vp, vs, n=None):
assert len(z) == len(vp) == len(vs)
if n is not None:
z = _interpolate_n(z, n)
vp = _interpolate_n(vp, n)
vs = _interpolate_n(vs, n)
self.z = z[:-1]
self.dz = np.diff(z)
self.vp = vp[:-1]
self.vs = vs[:-1]
self.t_ref = {}
3
Example 18
def compute_edges(edges):
"""
Computes edges from a number of bin centers,
throwing an exception if the edges are not
evenly spaced.
"""
widths = np.diff(edges)
if np.allclose(widths, widths[0]):
width = widths[0]
else:
raise ValueError('Centered bins have to be of equal width.')
edges -= width/2.
return np.concatenate([edges, [edges[-1]+width]])
3
Example 19
Project: pyqtgraph Source File: PlotDataItem.py
def _fourierTransform(self, x, y):
## Perform fourier transform. If x values are not sampled uniformly,
## then use np.interp to resample before taking fft.
dx = np.diff(x)
uniform = not np.any(np.abs(dx-dx[0]) > (abs(dx[0]) / 1000.))
if not uniform:
x2 = np.linspace(x[0], x[-1], len(x))
y = np.interp(x2, x, y)
x = x2
f = np.fft.fft(y) / len(y)
y = abs(f[1:len(f)/2])
dt = x[-1] - x[0]
x = np.linspace(0, 0.5*len(x)/dt, len(y))
return x, y
3
Example 20
def ISI(data, cols=[0,1], threshold=20, sampleThr=3):
'''
Function calculates ISI from given data and return in the form of an array
of ISI for the given pair. The ISI is calculated for the peak of the spikes.
Threshold value according to (Naundorf et al. 2006).
'''
(spb, spm, spe) = findSpikes(data, cols, threshold, sampleThr)
# ISI can be calculated for at least 2 spikes
if len(spm) < 2:
return None
return np.diff(data[spm,cols[0]])
3
Example 21
def volume(x,y):
d = min(8, np.median(np.diff(x)));
idx = y>0;
x = x[idx];
y = y[idx];
L = np.sum(idx);
if L<3:
return np.nan;
vol = (y[0]+y[-1])/2.0*d;#end slice
for i in xrange(L-1):
vol += (y[i]+y[i+1])*np.abs(x[i+1]-x[i])/2.0;
return vol/1000.0;
3
Example 22
Project: tsfresh Source File: feature_calculators.py
@set_property("fctype", "aggregate")
@not_apply_to_raw_numbers
def mean_abs_change(x):
"""
Returns the mean over the absolute differences between subsequent time series values which is
.. math::
\\frac{1}{n} \\sum_{i=1,\ldots, n-1} | x_{i+1} - x_{i}|
:param x: the time series to calculate the feature of
:type x: pandas.Series
:return: the value of this feature
:return type: float
"""
return np.mean(abs(np.diff(x)))
3
Example 23
def slope(self, xinp):
"""
Find the local slope of the model at location x
(x must be in xax's units)
"""
if hasattr(self, 'model'):
dm = np.diff(self.model)
# convert requested x to pixels
xpix = self.xax.x_to_pix(xinp)
dmx = np.average(dm[xpix-1:xpix+1])
if np.isfinite(dmx):
return dmx
else:
return 0
3
Example 24
Project: scikit-beam Source File: test_utils.py
def _bin_edges_helper(p_dict):
bin_edges = core.bin_edges(**p_dict)
assert_almost_equal(0, np.ptp(np.diff(bin_edges)))
if 'nbins' in p_dict:
nbins = p_dict['nbins']
assert_equal(nbins + 1, len(bin_edges))
if 'step' in p_dict:
step = p_dict['step']
assert_almost_equal(step, np.diff(bin_edges))
if 'range_max' in p_dict:
range_max = p_dict['range_max']
assert_true(np.all(bin_edges <= range_max))
if 'range_min' in p_dict:
range_min = p_dict['range_min']
assert_true(np.all(bin_edges >= range_min))
if 'range_max' in p_dict and 'step' in p_dict:
step = p_dict['step']
range_max = p_dict['range_max']
assert_true((range_max - bin_edges[-1]) < step)
3
Example 25
Project: NeuroM Source File: morphmath.py
def path_distance(points):
"""
Compute the path distance from given set of points
"""
vecs = np.diff(points, axis=0)[:, :3]
d2 = [np.dot(p, p) for p in vecs]
return np.sum(np.sqrt(d2))
3
Example 26
Project: cesium Source File: test_general_features.py
def test_max_slope():
"""Test maximum slope feature, which finds the INDEX of the largest slope."""
times, values, errors = irregular_random()
f = generate_features(times, values, errors, ['max_slope'])
slopes = np.diff(values) / np.diff(times)
npt.assert_allclose(f['max_slope'], np.max(np.abs(slopes)))
3
Example 27
def init_state(data):
close = data
diff = np.diff(data)
diff = np.insert(diff, 0, 0)
#--- Preprocess data
xdata = np.column_stack((close, diff))
xdata = np.nan_to_num(xdata)
scaler = preprocessing.StandardScaler()
xdata = scaler.fit_transform(xdata)
state = xdata[0:1, :]
return state, xdata
3
Example 28
Project: aospy Source File: model.py
@staticmethod
def diff_bounds(bounds, coord):
"""Get grid spacing by subtracting upper and lower bounds."""
try:
return bounds[:, 1] - bounds[:, 0]
except IndexError:
diff = np.diff(bounds, axis=0)
return xr.DataArray(diff, dims=coord.dims, coords=coord.coords)
3
Example 29
Project: polar2grid Source File: test_ll2cr.py
def test_latlong_dateline1(self):
lon_arr = create_test_longitude(165.0, -165.0, (50, 100), twist_factor=0.6)
lat_arr = create_test_latitude(15.0, 30.0, (50, 100), twist_factor=-0.1)
grid_info = dynamic_wgs84.copy()
points_in_grid, lon_res, lat_res = ll2cr.ll2cr(lon_arr, lat_arr, grid_info)
assert points_in_grid == lon_arr.size, "all points should be contained in a dynamic grid"
assert lon_arr is lon_res
assert lat_arr is lat_res
assert lon_arr[0, 0] == 0, "ll2cr returned the wrong result for a dynamic latlong grid over the dateline"
assert lat_arr[-1, 0] == 0, "ll2cr returned the wrong result for a dynamic latlong grid over the dateline"
assert numpy.all(numpy.diff(lon_arr[0]) >= 0), "ll2cr didn't return monotonic columns over the dateline"
3
Example 30
Project: qutip Source File: test_graph.py
def test_column_permutation():
"Graph: Column Permutation"
A = sp.rand(5, 5, 0.25, format='csc')
perm = column_permutation(A)
B = sp_permute(A, [], perm)
counts = np.diff(B.indptr)
assert_equal(np.all(np.argsort(counts) == np.arange(5)), True)
3
Example 31
Project: george Source File: model.py
def generate_data(params, N, rng=(-5, 5)):
gp = george.GP(0.1 * kernels.ExpSquaredKernel(3.3))
t = rng[0] + np.diff(rng) * np.sort(np.random.rand(N))
y = gp.sample(t)
y += model(params, t)
yerr = 0.05 + 0.05 * np.random.rand(N)
y += yerr * np.random.randn(N)
return t, y, yerr
3
Example 32
Project: GromacsWrapper Source File: timeseries.py
def tc_histogrammed_function(t, y, **kwargs):
"""Calculate the correlation time in each bin using :func:`tcorrel`.
.. Warning:: Not well tested and fragile.
"""
dt = numpy.mean(numpy.diff(t))
def get_tcorrel(a):
if len(a) == 0:
return numpy.NAN
t = numpy.cuemsum(dt*numpy.ones_like(a)) - dt
results = tcorrel(t, a, nstep=1)
return results['tc']
return apply_histogrammed_function(get_tcorrel, t, y, **kwargs)
3
Example 33
Project: chaco Source File: base.py
def arg_find_runs(int_array, order='ascending'):
"""
Like find_runs(), but returns a list of tuples indicating the start and
end indices of runs in the input *int_array*.
"""
n_points = len(int_array)
if n_points == 0:
return []
indices = nonzero(diff(int_array) - delta.get(order, 0))[0] + 1
result = empty(shape=(len(indices) + 1, 2), dtype=indices.dtype)
result[0, 0] = 0
result[-1, 1] = n_points
result[1:, 0] = indices
result[:-1, 1] = indices
return result
3
Example 34
Project: PyAbel Source File: direct.py
def is_uniform_sampling(r):
"""
Returns True if the array is uniformly spaced to within 1e-13.
Otherwise False.
"""
dr = np.diff(r)
ddr = np.diff(dr)
return np.allclose(ddr, 0, atol=1e-13)
3
Example 35
Project: gplearn Source File: fixes.py
def _minor_reduce(X, ufunc):
major_index = np.flatnonzero(np.diff(X.indptr))
if X.data.size == 0 and major_index.size == 0:
# Numpy < 1.8.0 don't handle empty arrays in reduceat
value = np.zeros_like(X.data)
else:
value = ufunc.reduceat(X.data, X.indptr[major_index])
return major_index, value
3
Example 36
def freq_from_crossings(self, sig):
"""Estimate frequency by counting zero crossings"""
# From https://gist.github.com/endolith/255291:
fs = self.sampleRate
# Find all indices right before a rising-edge zero crossing
indices = find((sig[1:] >= 0) & (sig[:-1] < 0))
# More accurate, using linear interpolation to find intersample
# zero-crossings (Measures 1000.000129 Hz for 1000 Hz, for instance)
crossings = [i - sig[i] / (sig[i+1] - sig[i]) for i in indices]
# Some other interpolation based on neighboring points might be better. Spline, cubic, whatever
return fs / np.mean(np.diff(crossings))
3
Example 37
def is_regular(self):
"""Determine whether this `Index` contains linearly increasing samples
This also works for linear decrease
"""
if self.size <= 1:
return False
return numpy.isclose(numpy.diff(self.value, n=2), 0).all()
3
Example 38
Project: structural_engineering Source File: postprocess.py
@staticmethod
def determine_shear_force(element, con):
"""
Determines the shear force by differentiating the bending moment.
:param element: (object) of the Element class
"""
dV = np.diff(element.bending_moment)
dx = element.l / (con - 1)
shear_force = dV / dx
# Due to differentiation the first and the last values must be corrected.
correction = shear_force[1] - shear_force[0]
shear_force = np.insert(shear_force, 0, [shear_force[0] - 0.5 * correction])
shear_force = np.insert(shear_force, con, [shear_force[-1] + 0.5 * correction])
element.shear_force = shear_force
3
Example 39
def risk(x, sids, data, penalty=0):
''' Use the covariance matrix from returns '''
returns = np.diff(data[sids], axis=0)
p = np.asarray(returns)
Acov = np.cov(p.T)
return np.dot(x, np.dot(Acov, x)) + penalty
3
Example 40
def _prepare_draw(self, view):
xform = view.get_transform()
tr = view.get_transform('docuement', 'render')
logical_scale = np.diff(tr.map(([0, 1], [1, 0])), axis=0)[0, :2]
tr = view.get_transform('docuement', 'framebuffer')
log_to_phy = np.mean(np.diff(tr.map(([0, 1], [1, 0])), axis=0)[0, :2])
n_pix = (self.font_size / 72.) * 92. # num of pixels tall
# The -2 here is because the char_height has a gap built in
font_scale = max(n_pix / float((self._char_height-2)), 1)
self._resize_buffers(font_scale)
self._do_pending_writes()
self._program['u_origin'] = xform.map((0, 0, 0, 1))
self._program['u_logical_scale'] = font_scale * logical_scale
self._program['u_color'] = self.text_color.rgba
self._program['u_physical_scale'] = font_scale * log_to_phy
self._program['a_position'] = self._position
self._program['a_bytes_012'] = VertexBuffer(self._bytes_012)
self._program['a_bytes_345'] = VertexBuffer(self._bytes_345)
3
Example 41
def is_regular(self, rtol=1.e-5, atol=1.e-8):
"""Whether all bins have the same width.
Parameters
----------
rtol, atol : float
numpy tolerance parameters
Returns
-------
bool
"""
return np.allclose(np.diff(self.bins[1] - self.bins[0]), 0.0, rtol=rtol, atol=atol)
3
Example 42
def test_mask(self):
# time scheme
nt = 100
dt = 1.e-3
t1 = dt
t2 = t1 + (nt-1)*dt
# mute parameters
slope = 0. # units: time/distance
const = t2/2. # units: time
offset = 0. # units: distance
w = signal.mask(slope, const, offset, (nt, dt, t1), length=10)
# check monotonicity
assert(np.all(np.diff(w) >= 0))
3
Example 43
def parse( self, current ):
# Take the derivative of the current first
diff = np.abs( np.diff( current ) )
# Find the places where the derivative is low
tics = np.concatenate( ( [0], np.where( diff < 1e-3 )[0], [ diff.shape[0] ] ) )
# For pieces between these tics, make each point the cuemulative sum of that piece and put it together piecewise
cuemsum = np.concatenate( ( [ np.cuemsum( diff[ tics[i] : tics[i+1] ] ) for i in xrange( tics.shape[0]-1 ) ] ) )
# Find the edges where the cuemulative sum passes a threshold
split_points = np.where( np.abs( np.diff( np.where( cuemsum > self.threshold, 1, 0 ) ) ) == 1 )[0] + 1
# Return segments which do pass the threshold
return [ Segment( current = current[ tics[i]: tics[i+1] ], start = tics[i] ) for i in xrange( 1, tics.shape[0] - 1, 2 ) ]
3
Example 44
Project: neleval Source File: coref_metrics.py
def _vectorized_dice(true_matrix, pred_matrix):
overlap = _vectorized_overlap(true_matrix, pred_matrix).astype(float)
# The following should be no-ops
assert overlap.format == true_matrix.format == pred_matrix.format == 'csr'
true_sizes = np.diff(true_matrix.indptr)
pred_sizes = np.diff(pred_matrix.indptr)
denom = np.repeat(true_sizes, np.diff(overlap.indptr))
denom += pred_sizes.take(overlap.indices)
overlap.data /= denom
return overlap
3
Example 45
Project: director Source File: planplayback.py
def getMovingJointNames(self, msg):
poseTimes, poses = self.getPlanPoses(msg)
diffs = np.diff(poses, axis=0)
jointIds = np.unique(np.where(diffs != 0.0)[1])
jointNames = [robotstate.getDrakePoseJointNames()[jointId] for jointId in jointIds]
return jointNames
3
Example 46
Project: pypot Source File: primitive.py
@property
def recent_update_frequencies(self):
""" Returns the 10 most recent update frequencies.
The given frequencies are computed as short-term frequencies!
The 0th element of the list corresponds to the most recent frequency.
"""
return list(reversed([(1.0 / p) for p in numpy.diff(self._recent_updates)]))
3
Example 47
Project: crosscat Source File: diagnostic_utils.py
def column_chain_to_ratio(column_chain_arr, j, i=0):
chain_i_j = column_chain_arr[[i, j], :]
is_same = numpy.diff(chain_i_j, axis=0)[0] == 0
n_chains = len(is_same)
is_same_count = sum(is_same)
ratio = is_same_count / float(n_chains)
return ratio
3
Example 48
Project: zipline Source File: technical.py
def compute(self, today, assets, out, closes):
diffs = diff(closes, axis=0)
ups = nanmean(clip(diffs, 0, inf), axis=0)
downs = abs(nanmean(clip(diffs, -inf, 0), axis=0))
return evaluate(
"100 - (100 / (1 + (ups / downs)))",
local_dict={'ups': ups, 'downs': downs},
global_dict={},
out=out,
)
3
Example 49
Project: postpic Source File: datahandling.py
def islinear(self, force=False):
"""
Checks if the axis has a linear grid.
"""
if self._linear is None or force:
self._linear = np.var(np.diff(self._grid_node)) < 1e-7
return self._linear
3
Example 50
Project: mir_eval Source File: util.py
def intervals_to_durations(intervals):
"""Converts an array of n intervals to their n durations.
Parameters
----------
intervals : np.ndarray, shape=(n, 2)
An array of time intervals, as returned by
:func:`mir_eval.io.load_intervals()`.
The ``i`` th interval spans time ``intervals[i, 0]`` to
``intervals[i, 1]``.
Returns
-------
durations : np.ndarray, shape=(n,)
Array of the duration of each interval.
"""
validate_intervals(intervals)
return np.abs(np.diff(intervals, axis=-1)).flatten()