Skip to content

Commit 15a35dc

Browse files
committed
Fix to is_scalar plus additional functions in cbook.py:
less_simple_linear_interpolation isvector vector_lengths distances_along_curve path_length is_closed_polygon svn path=/trunk/matplotlib/; revision=5824
1 parent c1ce194 commit 15a35dc

File tree

1 file changed

+110
-1
lines changed

1 file changed

+110
-1
lines changed

lib/matplotlib/cbook.py

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ def is_writable_file_like(obj):
287287

288288
def is_scalar(obj):
289289
'return true if *obj* is not string like and is not iterable'
290-
return not is_string_like(obj) or not iterable(obj)
290+
return not is_string_like(obj) and not iterable(obj)
291291

292292
def is_numlike(obj):
293293
'return true if *obj* looks like a number'
@@ -1156,6 +1156,46 @@ def simple_linear_interpolation(a, steps):
11561156

11571157
return result
11581158

1159+
def less_simple_linear_interpolation( x, y, xi, extrap=False ):
1160+
"""
1161+
This function provides simple (but somewhat less so than
1162+
simple_linear_interpolation) linear interpolation. This is very
1163+
inefficient linear interpolation meant to be used only for a small
1164+
number of points in relatively non-intensive use cases.
1165+
1166+
Call signature::
1167+
1168+
yi = less_simple_linear_interpolation(x,y,xi)
1169+
"""
1170+
if is_scalar(xi): xi = [xi]
1171+
1172+
x = np.asarray(x)
1173+
y = np.asarray(y)
1174+
xi = np.asarray(xi)
1175+
1176+
s = list(y.shape)
1177+
s[0] = len(xi)
1178+
yi = np.tile( np.nan, s )
1179+
1180+
for ii,xx in enumerate(xi):
1181+
bb = x == xx
1182+
if np.any(bb):
1183+
jj, = np.nonzero(bb)
1184+
yi[ii] = y[jj[0]]
1185+
elif xx<x[0]:
1186+
if extrap:
1187+
yi[ii] = y[0]
1188+
elif xx>x[-1]:
1189+
if extrap:
1190+
yi[ii] = y[-1]
1191+
else:
1192+
jj, = np.nonzero(x<xx)
1193+
jj = max(jj)
1194+
1195+
yi[ii] = y[jj] + (xx-x[jj])/(x[jj+1]-x[jj]) * (y[jj+1]-y[jj])
1196+
1197+
return yi
1198+
11591199
def recursive_remove(path):
11601200
if os.path.isdir(path):
11611201
for fname in glob.glob(os.path.join(path, '*')) + glob.glob(os.path.join(path, '.*')):
@@ -1294,6 +1334,75 @@ def unmasked_index_ranges(mask, compressed = True):
12941334
ic1 = breakpoints
12951335
return np.concatenate((ic0[:, np.newaxis], ic1[:, np.newaxis]), axis=1)
12961336

1337+
def isvector(X):
1338+
"""
1339+
Like the Matlab (TM) function with the same name, returns true if
1340+
the supplied numpy array or matrix looks like a vector, meaning it
1341+
has a one non-singleton axis (i.e., it can have multiple axes, but
1342+
all must have length 1, except for one of them).
1343+
1344+
If you just want to see if the array has 1 axis, use X.ndim==1
1345+
1346+
Call signature::
1347+
isvector(X)
1348+
"""
1349+
return np.prod(X.shape)==np.max(X.shape)
1350+
1351+
def vector_lengths( X, P=2., axis=None ):
1352+
"""
1353+
Finds the length of a set of vectors in n dimensions. This is
1354+
like the mlab.norm function for vectors, but has the ability to
1355+
work over a particular axis of the supplied array or matrix.
1356+
1357+
Call signature::
1358+
1359+
vector_lengths( X, P=2., axis=None )
1360+
1361+
Computes (sum((x_i)^P))^(1/P) for each {x_i} being the elements of X along
1362+
the given axis. If *axis* is *None*, compute over all elements of X.
1363+
"""
1364+
X = np.asarray(X)
1365+
return (np.sum(X**(P),axis=axis))**(1./P)
1366+
1367+
def distances_along_curve( X ):
1368+
"""
1369+
Computes the distance between a set of successive points in N dimensions.
1370+
1371+
Call signature::
1372+
1373+
distances_along_curve(X)
1374+
1375+
where X is an MxN array or matrix. The distances between successive rows
1376+
is computed. Distance is the standard Euclidean distance.
1377+
"""
1378+
X = np.diff( X, axis=0 )
1379+
return vector_lengths(X,axis=1)
1380+
1381+
def path_length(X):
1382+
"""
1383+
Computes the distance travelled along a polygonal curve in N dimensions.
1384+
1385+
Call signature::
1386+
1387+
path_length(X)
1388+
1389+
where X is an MxN array or matrix. Returns an array of length M consisting
1390+
of the distance along the curve at each point (i.e., the rows of X).
1391+
"""
1392+
X = distances_along_curve(X)
1393+
return np.concatenate( (np.zeros(1), np.cumsum(X)) )
1394+
1395+
def is_closed_polygon(X):
1396+
"""
1397+
Tests whether first and last object in a sequence are the same. These are
1398+
presumably coordinates on a polygonal curve, in which case this function
1399+
tests if that curve is closed.
1400+
1401+
Call signature::
1402+
1403+
is_closed_polygon(X)
1404+
"""
1405+
return np.all(X[0] == X[-1])
12971406

12981407
# a dict to cross-map linestyle arguments
12991408
_linestyles = [('-', 'solid'),

0 commit comments

Comments
 (0)