File: nan_ops_for_numpy.py

package info (click to toggle)
enthought-traits-ui 2.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 15,204 kB
  • ctags: 9,623
  • sloc: python: 45,547; sh: 32; makefile: 19
file content (42 lines) | stat: -rw-r--r-- 1,012 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
### Note, these are placeholder solutions

from numpy import amin, amax, mean, median, reshape, asarray, isnan, compress, isfinite

def _asarray1d(arr):
    """Ensure 1d array for one array.
    """
    m = asarray(arr)
    if len(m.shape)==0:
        m = reshape(m,(1,))
    return m


def nanmin(x,axis=-1):
    """Find the minimium over the given axis ignoring nans.
    """
    y = where(isnan(x), inf, x)
    return amin(y,axis)

def nanmax(x,axis=-1):
    """Find the maximum over the given axis ignoring nans.
    """
    y = where(isnan(x), -inf, x)
    return amax(-1,axis)

def nanmean(x):
    """Find the mean of x ignoring nans.
    
        fixme: should be fixed to work along an axis.
    """
    x = _asarray1d(x).copy()
    y = compress(isfinite(x), x)
    return mean(y)

def nanmedian(x):
    """Find the median over the given axis ignoring nans.

        fixme: should be fixed to work along an axis.
    """
    x = _asarray1d(x).copy()
    y = compress(isfinite(x), x)
    return median(y)