Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cf5c05a

Browse files
committed
Added tests to stats. Added nanXXXX functions. Fixed kurtosis and skew to handle biased and unbiased estimates to match MATLAB.
1 parent 7e15158 commit cf5c05a

File tree

4 files changed

+123
-7
lines changed

4 files changed

+123
-7
lines changed

scipy_base/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
Useful functions
3838
==================
3939
select -- Extension of where to multiple conditions and choices
40+
unique -- Pull out unique entries in a sequence
4041
linspace -- Evenly spaced samples in linear space
4142
logspace -- Evenly spaced samples in logarithmic space
4243
fix -- Round x to nearest integer towards zero
@@ -55,6 +56,9 @@
5556
5657
Shape manipulation
5758
===================
59+
apply_over_axes -- Apply a function over multiple axes, output is same shape
60+
apply_along_axis -- Apply a function for 1d arrays repeatedly for Nd array
61+
expand_dims -- Return array with NewAxis applied before given axis
5862
squeeze -- Return a with length-one dimensions removed.
5963
atleast_1d -- Force arrays to be > 1D
6064
atleast_2d -- Force arrays to be > 2D

scipy_base/function_base.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
N = Numeric
44
from Numeric import *
55
from scipy_base.fastumath import *
6+
inf = PINF
67
import _compiled_base
78
from type_check import ScalarType, isscalar
89

910
__all__ = ['round','any','all','logspace','linspace','fix','mod',
1011
'select','trim_zeros','amax','amin','ptp','cumsum',
1112
'prod','cumprod','diff','angle','unwrap','sort_complex',
12-
'disp','unique']
13+
'disp','unique','takemask','nansum','nanmax','nanargmax',
14+
'nanargmin','nanmin']
1315

1416
round = Numeric.around
1517
any = Numeric.sometrue
@@ -264,6 +266,47 @@ def unique(inseq):
264266
set[item] = None
265267
return asarray(set.keys())
266268

269+
def takemask(arr,mask):
270+
"""1D array of those elements of ravel(arr) where ravel(mask) is true.
271+
"""
272+
return N.take(ravel(arr), nonzero(ravel(mask)))
273+
274+
def nansum(x,axis=-1):
275+
"""Sum the array over the given axis treating nans as missing values.
276+
"""
277+
x = N.asarray(x).copy()
278+
N.putmask(x,isnan(x),0)
279+
return N.sum(x,axis)
280+
281+
def nanmin(x,axis=-1):
282+
"""Find the minimium over the given axis ignoring nans.
283+
"""
284+
x = N.asarray(x).copy()
285+
N.putmask(x,isnan(x),inf)
286+
return amin(x,axis)
287+
288+
def nanargmin(x,axis=-1):
289+
"""Find the indices of the minimium over the given axis ignoring nans.
290+
"""
291+
x = N.asarray(x).copy()
292+
N.putmask(x,isnan(x),inf)
293+
return argmin(x,axis)
294+
295+
296+
def nanmax(x,axis=-1):
297+
"""Find the maximum over the given axis ignoring nans.
298+
"""
299+
x = asarray(x).copy()
300+
putmask(x,isnan(x),-inf)
301+
return amax(x,axis)
302+
303+
def nanargmax(x,axis=-1):
304+
"""Find the maximum over the given axis ignoring nans.
305+
"""
306+
x = asarray(x).copy()
307+
putmask(x,isnan(x),-inf)
308+
return argmax(x,axis)
309+
267310
import sys
268311
def disp(mesg, device=None, linefeed=1):
269312
"""Display a message to device (default is sys.stdout) with(out) linefeed.

scipy_base/shape_base.py

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,73 @@
11
import Numeric
22
from Numeric import *
3+
from type_check import isscalar
34

45
__all__ = ['atleast_1d','atleast_2d','atleast_3d','vstack','hstack',
56
'column_stack','dstack','array_split','split','hsplit',
6-
'vsplit','dsplit','squeeze','apply_over_axes','expand_dims']
7-
7+
'vsplit','dsplit','squeeze','apply_over_axes','expand_dims',
8+
'apply_along_axis']
89

10+
def apply_along_axis(func1d,axis,arr,*args):
11+
""" Execute func1d(arr[i],*args) where func1d takes 1-D arrays
12+
and arr is an N-d array. i varies so as to apply the function
13+
along the given axis for each 1-d subarray in arr.
14+
"""
15+
nd = Numeric.rank(arr)
16+
if axis < 0: axis += nd
17+
if (axis >= nd):
18+
raise ValueError, "axis must be less than the rank; "+\
19+
"axis=%d, rank=%d." % (axis,)
20+
ind = [0]*(nd-1)
21+
dims = Numeric.shape(arr)
22+
i = zeros(nd,'O')
23+
indlist = range(nd)
24+
indlist.remove(axis)
25+
i[axis] = slice(None,None)
26+
outshape = take(shape(arr),indlist)
27+
put(i,indlist,ind)
28+
res = func1d(arr[i],*args)
29+
# if res is a number, then we have a smaller output array
30+
if isscalar(res):
31+
outarr = zeros(outshape,asarray(res).typecode())
32+
outarr[ind] = res
33+
Ntot = product(outshape)
34+
k = 1
35+
while k < Ntot:
36+
# increment the index
37+
ind[-1] += 1
38+
n = -1
39+
while (ind[n] >= outshape[n]) and (n > (1-nd)):
40+
ind[n-1] += 1
41+
ind[n] = 0
42+
n -= 1
43+
put(i,indlist,ind)
44+
res = func1d(arr[i],*args)
45+
outarr[ind] = res
46+
k += 1
47+
return outarr
48+
else:
49+
Ntot = product(outshape)
50+
holdshape = outshape
51+
outshape = list(shape(arr))
52+
outshape[axis] = len(res)
53+
outarr = zeros(outshape,asarray(res).typecode())
54+
outarr[i] = res
55+
k = 1
56+
while k < Ntot:
57+
# increment the index
58+
ind[-1] += 1
59+
n = -1
60+
while (ind[n] >= holdshape[n]) and (n > (1-nd)):
61+
ind[n-1] += 1
62+
ind[n] = 0
63+
n -= 1
64+
put(i,indlist,ind)
65+
res = func1d(arr[i],*args)
66+
outarr[i] = res
67+
k += 1
68+
return outarr
69+
70+
971
def apply_over_axes(func, a, axes):
1072
"""Apply a function over multiple axes, keeping the same shape
1173
for the resulting array.
@@ -21,8 +83,7 @@ def apply_over_axes(func, a, axes):
2183
return val
2284

2385
def expand_dims(a, axis):
24-
"""Expand the shape of a to include a length 1 dimension before the given
25-
axis.
86+
"""Expand the shape of a by including NewAxis before given axis.
2687
"""
2788
a = asarray(a)
2889
shape = a.shape

scipy_test/testing.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,16 @@ def assert_approx_equal(actual,desired,significant=7,err_msg='',verbose=1):
343343
"""
344344
msg = '\nItems are not equal to %d significant digits:\n' % significant
345345
msg += err_msg
346-
sc_desired = desired/pow(10,math.floor(math.log10(abs(desired))))
347-
sc_actual = actual/pow(10,math.floor(math.log10(abs(actual))))
346+
actual, desired = map(float, (actual, desired))
347+
# Normalized the numbers to be in range (-10.0,10.0)
348+
try:
349+
sc_desired = desired/pow(10,math.floor(math.log10(abs(desired))))
350+
except ZeroDivisionError:
351+
sc_desired = 0.0
352+
try:
353+
sc_actual = actual/pow(10,math.floor(math.log10(abs(actual))))
354+
except ZeroDivisionError:
355+
sc_actual = 0.0
348356
try:
349357
if ( verbose and len(repr(desired)) < 100 and len(repr(actual)) ):
350358
msg = msg \

0 commit comments

Comments
 (0)