File: nan_ops_for_numeric.py

package info (click to toggle)
python-enthoughtbase 3.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 960 kB
  • ctags: 1,034
  • sloc: python: 6,104; makefile: 9; sh: 5
file content (77 lines) | stat: -rw-r--r-- 2,236 bytes parent folder | download | duplicates (2)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

# Since this module is meant to offer safety wrappers around some Numeric
# functions, if we can't import Numeric or scipy, then gracefully handle
# the import error and define stubs.

try:
    import Numeric as _nx
    from Numeric import asarray, reshape, argmin, argmax, compress
    from scipy.stats import mean, median
    from scipy import isnan, amin, amax, inf, 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 nansum(x,axis=-1):
        """Sum the array over the given axis treating nans as missing values.
        """
        x = _asarray1d(x).copy()
        _nx.putmask(x,isnan(x),0)
        return _nx.sum(x,axis)

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

    def nanargmin(x,axis=-1):
        """Find the indices of the minimium over the given axis ignoring nans.
        """
        x = _asarray1d(x).copy()
        _nx.putmask(x,isnan(x),inf)
        return argmin(x,axis)
        

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

    def nanargmax(x,axis=-1):
        """Find the maximum over the given axis ignoring nans.
        """
        x = _asarray1d(x).copy()
        _nx.putmask(x,isnan(x),-inf)
        return argmax(x,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)

except ImportError:
    _asarray1d = nansum = nanmin = nanargmin = nanmax = nanargmax = \
            nanmean = nanmedian = None