~inkscape.dev/inkscape-devlibs/trunk

« back to all changes in this revision

Viewing changes to python/Lib/site-packages/numpy/lib/nanfunctions.py

  • Committer: Eduard Braun
  • Date: 2016-10-22 16:54:41 UTC
  • Revision ID: eduard.braun2@gmx.de-20161022165441-gfp6agtut9nh4p22
Update Python to version 2.7.12

Included modules:
  coverage 4.2
  lxml 3.6.4
  numpy 1.11.2
  scour 0.35
  six 1.10.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
import numpy as np
24
24
from numpy.lib.function_base import _ureduce as _ureduce
25
25
 
 
26
 
26
27
__all__ = [
27
28
    'nansum', 'nanmax', 'nanmin', 'nanargmax', 'nanargmin', 'nanmean',
28
29
    'nanmedian', 'nanpercentile', 'nanvar', 'nanstd', 'nanprod',
141
142
                return np.divide(a, b, out=out, casting='unsafe')
142
143
 
143
144
 
144
 
def nanmin(a, axis=None, out=None, keepdims=False):
 
145
def nanmin(a, axis=None, out=None, keepdims=np._NoValue):
145
146
    """
146
147
    Return minimum of an array or minimum along an axis, ignoring any NaNs.
147
148
    When all-NaN slices are encountered a ``RuntimeWarning`` is raised and
163
164
 
164
165
        .. versionadded:: 1.8.0
165
166
    keepdims : bool, optional
166
 
        If this is set to True, the axes which are reduced are left in the
167
 
        result as dimensions with size one. With this option, the result
168
 
        will broadcast correctly against the original `a`.
 
167
        If this is set to True, the axes which are reduced are left
 
168
        in the result as dimensions with size one. With this option,
 
169
        the result will broadcast correctly against the original `a`.
 
170
 
 
171
        If the value is anything but the default, then
 
172
        `keepdims` will be passed through to the `min` method
 
173
        of sub-classes of `ndarray`.  If the sub-classes methods
 
174
        does not implement `keepdims` any exceptions will be raised.
169
175
 
170
176
        .. versionadded:: 1.8.0
171
177
 
220
226
    -inf
221
227
 
222
228
    """
 
229
    kwargs = {}
 
230
    if keepdims is not np._NoValue:
 
231
        kwargs['keepdims'] = keepdims
223
232
    if not isinstance(a, np.ndarray) or type(a) is np.ndarray:
224
233
        # Fast, but not safe for subclasses of ndarray
225
 
        res = np.fmin.reduce(a, axis=axis, out=out, keepdims=keepdims)
 
234
        res = np.fmin.reduce(a, axis=axis, out=out, **kwargs)
226
235
        if np.isnan(res).any():
227
236
            warnings.warn("All-NaN axis encountered", RuntimeWarning)
228
237
    else:
229
238
        # Slow, but safe for subclasses of ndarray
230
239
        a, mask = _replace_nan(a, +np.inf)
231
 
        res = np.amin(a, axis=axis, out=out, keepdims=keepdims)
 
240
        res = np.amin(a, axis=axis, out=out, **kwargs)
232
241
        if mask is None:
233
242
            return res
234
243
 
235
244
        # Check for all-NaN axis
236
 
        mask = np.all(mask, axis=axis, keepdims=keepdims)
 
245
        mask = np.all(mask, axis=axis, **kwargs)
237
246
        if np.any(mask):
238
247
            res = _copyto(res, np.nan, mask)
239
248
            warnings.warn("All-NaN axis encountered", RuntimeWarning)
240
249
    return res
241
250
 
242
251
 
243
 
def nanmax(a, axis=None, out=None, keepdims=False):
 
252
def nanmax(a, axis=None, out=None, keepdims=np._NoValue):
244
253
    """
245
254
    Return the maximum of an array or maximum along an axis, ignoring any
246
255
    NaNs.  When all-NaN slices are encountered a ``RuntimeWarning`` is
262
271
 
263
272
        .. versionadded:: 1.8.0
264
273
    keepdims : bool, optional
265
 
        If this is set to True, the axes which are reduced are left in the
266
 
        result as dimensions with size one. With this option, the result
267
 
        will broadcast correctly against the original `a`.
 
274
        If this is set to True, the axes which are reduced are left
 
275
        in the result as dimensions with size one. With this option,
 
276
        the result will broadcast correctly against the original `a`.
 
277
 
 
278
        If the value is anything but the default, then
 
279
        `keepdims` will be passed through to the `max` method
 
280
        of sub-classes of `ndarray`.  If the sub-classes methods
 
281
        does not implement `keepdims` any exceptions will be raised.
268
282
 
269
283
        .. versionadded:: 1.8.0
270
284
 
319
333
    inf
320
334
 
321
335
    """
 
336
    kwargs = {}
 
337
    if keepdims is not np._NoValue:
 
338
        kwargs['keepdims'] = keepdims
322
339
    if not isinstance(a, np.ndarray) or type(a) is np.ndarray:
323
340
        # Fast, but not safe for subclasses of ndarray
324
 
        res = np.fmax.reduce(a, axis=axis, out=out, keepdims=keepdims)
 
341
        res = np.fmax.reduce(a, axis=axis, out=out, **kwargs)
325
342
        if np.isnan(res).any():
326
343
            warnings.warn("All-NaN slice encountered", RuntimeWarning)
327
344
    else:
328
345
        # Slow, but safe for subclasses of ndarray
329
346
        a, mask = _replace_nan(a, -np.inf)
330
 
        res = np.amax(a, axis=axis, out=out, keepdims=keepdims)
 
347
        res = np.amax(a, axis=axis, out=out, **kwargs)
331
348
        if mask is None:
332
349
            return res
333
350
 
334
351
        # Check for all-NaN axis
335
 
        mask = np.all(mask, axis=axis, keepdims=keepdims)
 
352
        mask = np.all(mask, axis=axis, **kwargs)
336
353
        if np.any(mask):
337
354
            res = _copyto(res, np.nan, mask)
338
355
            warnings.warn("All-NaN axis encountered", RuntimeWarning)
428
445
    return res
429
446
 
430
447
 
431
 
def nansum(a, axis=None, dtype=None, out=None, keepdims=0):
 
448
def nansum(a, axis=None, dtype=None, out=None, keepdims=np._NoValue):
432
449
    """
433
450
    Return the sum of array elements over a given axis treating Not a
434
451
    Numbers (NaNs) as zero.
462
479
 
463
480
        .. versionadded:: 1.8.0
464
481
    keepdims : bool, optional
465
 
        If True, the axes which are reduced are left in the result as
466
 
        dimensions with size one. With this option, the result will
467
 
        broadcast correctly against the original `arr`.
 
482
        If this is set to True, the axes which are reduced are left
 
483
        in the result as dimensions with size one. With this option,
 
484
        the result will broadcast correctly against the original `a`.
 
485
 
 
486
 
 
487
        If the value is anything but the default, then
 
488
        `keepdims` will be passed through to the `mean` or `sum` methods
 
489
        of sub-classes of `ndarray`.  If the sub-classes methods
 
490
        does not implement `keepdims` any exceptions will be raised.
468
491
 
469
492
        .. versionadded:: 1.8.0
470
493
 
513
536
    return np.sum(a, axis=axis, dtype=dtype, out=out, keepdims=keepdims)
514
537
 
515
538
 
516
 
def nanprod(a, axis=None, dtype=None, out=None, keepdims=0):
 
539
def nanprod(a, axis=None, dtype=None, out=None, keepdims=np._NoValue):
517
540
    """
518
541
    Return the product of array elements over a given axis treating Not a
519
542
    Numbers (NaNs) as zero.
583
606
    return np.prod(a, axis=axis, dtype=dtype, out=out, keepdims=keepdims)
584
607
 
585
608
 
586
 
def nanmean(a, axis=None, dtype=None, out=None, keepdims=False):
 
609
def nanmean(a, axis=None, dtype=None, out=None, keepdims=np._NoValue):
587
610
    """
588
611
    Compute the arithmetic mean along the specified axis, ignoring NaNs.
589
612
 
613
636
        expected output, but the type will be cast if necessary.  See
614
637
        `doc.ufuncs` for details.
615
638
    keepdims : bool, optional
616
 
        If this is set to True, the axes which are reduced are left in the
617
 
        result as dimensions with size one. With this option, the result
618
 
        will broadcast correctly against the original `arr`.
 
639
        If this is set to True, the axes which are reduced are left
 
640
        in the result as dimensions with size one. With this option,
 
641
        the result will broadcast correctly against the original `a`.
 
642
 
 
643
        If the value is anything but the default, then
 
644
        `keepdims` will be passed through to the `mean` or `sum` methods
 
645
        of sub-classes of `ndarray`.  If the sub-classes methods
 
646
        does not implement `keepdims` any exceptions will be raised.
619
647
 
620
648
    Returns
621
649
    -------
727
755
            out[...] = result
728
756
        return result
729
757
 
 
758
 
730
759
def _nanmedian_small(a, axis=None, out=None, overwrite_input=False):
731
760
    """
732
761
    sort + indexing median, faster for small medians along multiple
743
772
        return out
744
773
    return m.filled(np.nan)
745
774
 
746
 
def nanmedian(a, axis=None, out=None, overwrite_input=False, keepdims=False):
 
775
 
 
776
def nanmedian(a, axis=None, out=None, overwrite_input=False, keepdims=np._NoValue):
747
777
    """
748
778
    Compute the median along the specified axis, while ignoring NaNs.
749
779
 
772
802
       False. If `overwrite_input` is ``True`` and `a` is not already an
773
803
       `ndarray`, an error will be raised.
774
804
    keepdims : bool, optional
775
 
        If this is set to True, the axes which are reduced are left in
776
 
        the result as dimensions with size one. With this option, the
777
 
        result will broadcast correctly against the original `arr`.
 
805
        If this is set to True, the axes which are reduced are left
 
806
        in the result as dimensions with size one. With this option,
 
807
        the result will broadcast correctly against the original `a`.
 
808
 
 
809
        If this is anything but the default value it will be passed
 
810
        through (in the special case of an empty array) to the
 
811
        `mean` function of the underlying array.  If the array is
 
812
        a sub-class and `mean` does not have the kwarg `keepdims` this
 
813
        will raise a RuntimeError.
778
814
 
779
815
    Returns
780
816
    -------
829
865
 
830
866
    r, k = _ureduce(a, func=_nanmedian, axis=axis, out=out,
831
867
                    overwrite_input=overwrite_input)
832
 
    if keepdims:
 
868
    if keepdims and keepdims is not np._NoValue:
833
869
        return r.reshape(k)
834
870
    else:
835
871
        return r
836
872
 
837
873
 
838
874
def nanpercentile(a, q, axis=None, out=None, overwrite_input=False,
839
 
                  interpolation='linear', keepdims=False):
 
875
                  interpolation='linear', keepdims=np._NoValue):
840
876
    """
841
877
    Compute the qth percentile of the data along the specified axis,
842
878
    while ignoring nan values.
883
919
            * nearest: ``i`` or ``j``, whichever is nearest.
884
920
            * midpoint: ``(i + j) / 2``.
885
921
    keepdims : bool, optional
886
 
        If this is set to True, the axes which are reduced are left in
887
 
        the result as dimensions with size one. With this option, the
888
 
        result will broadcast correctly against the original array `a`.
 
922
        If this is set to True, the axes which are reduced are left
 
923
        in the result as dimensions with size one. With this option,
 
924
        the result will broadcast correctly against the original `a`.
 
925
 
 
926
        If this is anything but the default value it will be passed
 
927
        through (in the special case of an empty array) to the
 
928
        `mean` function of the underlying array.  If the array is
 
929
        a sub-class and `mean` does not have the kwarg `keepdims` this
 
930
        will raise a RuntimeError.
889
931
 
890
932
    Returns
891
933
    -------
893
935
        If `q` is a single percentile and `axis=None`, then the result
894
936
        is a scalar. If multiple percentiles are given, first axis of
895
937
        the result corresponds to the percentiles. The other axes are
896
 
        the axes that remain after the reduction of `a`. If the input 
 
938
        the axes that remain after the reduction of `a`. If the input
897
939
        contains integers or floats smaller than ``float64``, the output
898
940
        data-type is ``float64``. Otherwise, the output data-type is the
899
941
        same as that of the input. If `out` is specified, that array is
954
996
    r, k = _ureduce(a, func=_nanpercentile, q=q, axis=axis, out=out,
955
997
                    overwrite_input=overwrite_input,
956
998
                    interpolation=interpolation)
957
 
    if keepdims:
 
999
    if keepdims and keepdims is not np._NoValue:
958
1000
        if q.ndim == 0:
959
1001
            return r.reshape(k)
960
1002
        else:
964
1006
 
965
1007
 
966
1008
def _nanpercentile(a, q, axis=None, out=None, overwrite_input=False,
967
 
                   interpolation='linear', keepdims=False):
 
1009
                   interpolation='linear'):
968
1010
    """
969
1011
    Private function that doesn't support extended axis or keepdims.
970
1012
    These methods are extended to this function using _ureduce
981
1023
        # Move that axis to the beginning to match percentile's
982
1024
        # convention.
983
1025
        if q.ndim != 0:
984
 
            result = np.rollaxis(result, axis)   
 
1026
            result = np.rollaxis(result, axis)
985
1027
 
986
1028
    if out is not None:
987
1029
        out[...] = result
1020
1062
                             interpolation=interpolation)
1021
1063
 
1022
1064
 
1023
 
def nanvar(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False):
 
1065
def nanvar(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue):
1024
1066
    """
1025
1067
    Compute the variance along the specified axis, while ignoring NaNs.
1026
1068
 
1056
1098
    keepdims : bool, optional
1057
1099
        If this is set to True, the axes which are reduced are left
1058
1100
        in the result as dimensions with size one. With this option,
1059
 
        the result will broadcast correctly against the original `arr`.
 
1101
        the result will broadcast correctly against the original `a`.
 
1102
 
1060
1103
 
1061
1104
    Returns
1062
1105
    -------
1095
1138
    below).  Specifying a higher-accuracy accumulator using the ``dtype``
1096
1139
    keyword can alleviate this issue.
1097
1140
 
 
1141
    For this function to work on sub-classes of ndarray, they must define
 
1142
    `sum` with the kwarg `keepdims`
 
1143
 
1098
1144
    Examples
1099
1145
    --------
1100
1146
    >>> a = np.array([[1, np.nan], [3, 4]])
1122
1168
        warnings.simplefilter('ignore')
1123
1169
 
1124
1170
        # Compute mean
1125
 
        cnt = np.sum(~mask, axis=axis, dtype=np.intp, keepdims=True)
1126
 
        avg = np.sum(arr, axis=axis, dtype=dtype, keepdims=True)
 
1171
        if type(arr) is np.matrix:
 
1172
            _keepdims = np._NoValue
 
1173
        else:
 
1174
            _keepdims = True
 
1175
        # we need to special case matrix for reverse compatibility
 
1176
        # in order for this to work, these sums need to be called with
 
1177
        # keepdims=True, however matrix now raises an error in this case, but
 
1178
        # the reason that it drops the keepdims kwarg is to force keepdims=True
 
1179
        # so this used to work by serendipity.
 
1180
        cnt = np.sum(~mask, axis=axis, dtype=np.intp, keepdims=_keepdims)
 
1181
        avg = np.sum(arr, axis=axis, dtype=dtype, keepdims=_keepdims)
1127
1182
        avg = _divide_by_count(avg, cnt)
1128
1183
 
1129
1184
        # Compute squared deviation from mean.
1151
1206
    return var
1152
1207
 
1153
1208
 
1154
 
def nanstd(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False):
 
1209
def nanstd(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue):
1155
1210
    """
1156
1211
    Compute the standard deviation along the specified axis, while
1157
1212
    ignoring NaNs.
1185
1240
        Means Delta Degrees of Freedom.  The divisor used in calculations
1186
1241
        is ``N - ddof``, where ``N`` represents the number of non-NaN
1187
1242
        elements.  By default `ddof` is zero.
 
1243
 
1188
1244
    keepdims : bool, optional
1189
1245
        If this is set to True, the axes which are reduced are left
1190
1246
        in the result as dimensions with size one. With this option,
1191
 
        the result will broadcast correctly against the original `arr`.
 
1247
        the result will broadcast correctly against the original `a`.
 
1248
 
 
1249
        If this value is anything but the default it is passed through
 
1250
        as-is to the relevant functions of the sub-classes.  If these
 
1251
        functions do not have a `keepdims` kwarg, a RuntimeError will
 
1252
        be raised.
1192
1253
 
1193
1254
    Returns
1194
1255
    -------