Skip to content

Commit

Permalink
ENH Speed up several functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
kwgoodman committed Mar 12, 2012
1 parent 1d80f97 commit ac9551e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
6 changes: 5 additions & 1 deletion RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ la 0.6
- Upgrade numpydoc from 0.3.1 to 0.4 to support Sphinx 1.0.1
- la.farray.ranking() and larry ranking method support `axis=None`
- Generate C code with Cython 0.15.1 instead of Cython 0.11
- larry.astype(), larry.push(), larry.__rdiv__() are faster

**Faster**

- larry methods: merge, nan_replace, push, cumsum, cumprod, astype, __rdiv__
- Numpy array functions: geometric_mean, correlation, covMissing

**Breakage from la 0.5**

Expand Down
24 changes: 12 additions & 12 deletions la/deflarry.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,10 @@ def cumsum(self, axis):
raise ValueError, 'axis cannot be None'
y = self.copy()
idx = np.isnan(y.x)
y[idx] = 0
np.putmask(y.x, idx, 0)
y.x.cumsum(axis, out=y.x)
if idx.any():
y.x[idx] = np.nan
np.putmask(y.x, idx, np.nan)
return y

def cumprod(self, axis):
Expand Down Expand Up @@ -413,10 +413,10 @@ def cumprod(self, axis):
raise ValueError, 'axis cannot be None'
y = self.copy()
idx = np.isnan(y.x)
y[idx] = 1
np.putmask(y.x, idx, 1)
y.x.cumprod(axis, out=y.x)
if idx.any():
y.x[idx] = np.nan
np.putmask(y.x, idx, np.nan)
return y

def clip(self, lo, hi):
Expand Down Expand Up @@ -1047,14 +1047,14 @@ def prod(self, axis=None):
"""
y = self.copy()
idx = np.isnan(y.x)
y.x[idx] = 1
np.putmask(y.x, idx, 1)
y = y.__reduce(np.prod, axis=axis)
idx = idx.all(axis)
if idx.ndim == 0:
if idx:
y = np.nan
else:
y[idx] = np.nan
else:
np.putmask(y.x, idx, np.nan)
return y

def mean(self, axis=None):
Expand Down Expand Up @@ -3062,9 +3062,9 @@ def quantile(self, q, axis=0):
array([-1., -1., 0., 0., 1., 1.])
"""
y = self.copy()
y.x = quantile(y.x, q, axis=axis)
return y
label = self.copylabel()
x = quantile(self.x, q, axis=axis)
return larry(x, label, validate=False)

# Group calc -------------------------------------------------------------

Expand Down Expand Up @@ -3184,7 +3184,7 @@ def morph(self, label, axis):
miss = missing_marker(x)
if miss == NotImplemented:
x = x.astype(float)
miss = missing_marker(x)
miss = missing_marker(x)
x[index] = miss
lab = self.copylabel()
lab[axis] = list(label)
Expand Down Expand Up @@ -3900,7 +3900,7 @@ def nan_replace(self, replace_with=0):
"""
y = self.copy()
y.x[np.isnan(y.x)] = replace_with
np.putmask(y.x, np.isnan(y.x), replace_with)
return y

# Size, shape, type ------------------------------------------------------
Expand Down
10 changes: 5 additions & 5 deletions la/farray/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def geometric_mean(x, axis=-1, check_for_greater_than_zero=True):
raise ValueError, msg
x = x.copy()
m = np.isnan(x)
x[m] = 1.0
np.putmask(x, m, 1.0)
m = np.asarray(~m, np.float64)
m = m.sum(axis)
x = np.log(x).sum(axis)
Expand All @@ -30,7 +30,7 @@ def geometric_mean(x, axis=-1, check_for_greater_than_zero=True):
if m == 0:
idx = np.nan
else:
idx[m == 0] = np.nan
np.putmask(idx, m==0, np.nan)
x = np.multiply(x, idx)
return x

Expand Down Expand Up @@ -97,8 +97,8 @@ def correlation(arr1, arr2, axis=None):
else:
x1 = arr1.copy()
x2 = arr2.copy()
x1[mask] = np.nan
x2[mask] = np.nan
np.putmask(x1, mask, np.nan)
np.putmask(x2, mask, np.nan)
if axis == 0:
x1 = x1 - bn.nanmean(x1, axis)
x2 = x2 - bn.nanmean(x2, axis)
Expand Down Expand Up @@ -152,7 +152,7 @@ def covMissing(R):
"""
mask = np.isnan(R)
R[mask] = 0
np.putmask(R, mask, 0)
mask = np.asarray(mask, np.float64)
mask = 1 - mask # Change meaning of missing matrix to present matrix

Expand Down
6 changes: 3 additions & 3 deletions la/farray/normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def lastrank(x, axis=-1, decay=0.0):
if not np.isfinite(x[indlast2]):
r = np.nan
else:
r[~np.isfinite(x[indlast2])] = np.nan
np.putmask(r, ~np.isfinite(x[indlast2]), np.nan)
return r

def ranking(x, axis=0, norm='-1,1'):
Expand Down Expand Up @@ -195,7 +195,7 @@ def ranking(x, axis=0, norm='-1,1'):
else:
msg = "norm must be '-1,1', '0,N-1', or 'gaussian'."
raise ValueError(msg)
idx[(countnotnan==1)*(~masknan)] = middle
np.putmask(idx, (countnotnan==1)*(~masknan), middle)
return idx

def push(x, n, axis=-1):
Expand Down Expand Up @@ -273,7 +273,7 @@ def quantile(x, q, axis=0):
raise ValueError, 'q must be one or greater.'
elif q == 1:
y = np.zeros(x.shape)
y[np.isnan(x)] = np.nan
np.putmask(y, np.isnan(x), np.nan)
return y
if axis == None:
if q > x.size:
Expand Down

0 comments on commit ac9551e

Please sign in to comment.