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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
|
from numpy import allclose as numpy_allclose
from numpy import amax as numpy_amax
from numpy import amin as numpy_amin
from numpy import any as numpy_any
from numpy import array as numpy_array
from numpy import asanyarray as numpy_asanyarray
from numpy import average as numpy_average
from numpy import bool_ as numpy_bool_
from numpy import copy as numpy_copy
from numpy import empty as numpy_empty
from numpy import expand_dims as numpy_expand_dims
from numpy import integer as numpy_integer
#from numpy import isclose as numpy_isclose
from numpy import maximum as numpy_maximum
from numpy import minimum as numpy_minimum
from numpy import ndim as numpy_ndim
from numpy import sum as numpy_sum
from numpy import where as numpy_where
from numpy import zeros as numpy_zeros
import numpy
from numpy.ma import array as numpy_ma_array
from numpy.ma import average as numpy_ma_average
from numpy.ma import expand_dims as numpy_ma_expand_dims
from numpy.ma import isMA as numpy_ma_isMA
from numpy.ma import masked as numpy_ma_masked
from numpy.ma import masked_less as numpy_ma_masked_less
from numpy.ma import masked_where as numpy_ma_masked_where
from numpy.ma import nomask as numpy_ma_nomask
from numpy.ma import where as numpy_ma_where
from functools import partial
from itertools import izip
from operator import mul as mul
from ..functions import broadcast_array
def psum(x, y):
'''
Add two arrays element-wise.
If either or both of the arrays are masked then the output array is
masked only where both input arrays are masked.
:Parameters:
x : numpy array-like
*Might be updated in place*.
y : numpy array-like
Will not be updated in place.
:Returns:
out : numpy array
:Examples:
>>> c = psum(a, b)
'''
if numpy_ma_isMA(x):
if numpy_ma_isMA(y):
# x and y are both masked
x_mask = x.mask
x = x.filled(0)
x += y.filled(0)
x = numpy_ma_array(x, mask=x_mask & y.mask, copy=False)
else:
# Only x is masked
x = x.filled(0)
x += y
elif numpy_ma_isMA(y):
# Only y is masked
x += y.filled(0)
else:
# x and y are both unmasked
x += y
return x
#--- End: def
def pmax(x, y):
'''
:Parameters:
x : array-like
May be updated in place and should not be used again.
y : array-like
Will not be updated in place.
:Returns:
out : numpy array
'''
if numpy_ma_isMA(x):
if numpy_ma_isMA(y):
# x and y are both masked
z = numpy_maximum(x, y)
z = numpy_ma_where(x.mask & -y.mask, y, z)
x = numpy_ma_where(y.mask & -x.mask, x, z)
if x.mask is numpy_ma_nomask: #not numpy_any(x.mask):
x = numpy_array(x)
else:
# Only x is masked
z = numpy_maximum(x, y)
x = numpy_ma_where(x.mask, y, z)
if x.mask is numpy_ma_nomask: #not numpy_any(x.mask):
x = numpy_array(x)
elif numpy_ma_isMA(y):
# Only y is masked
z = numpy_maximum(x, y)
x = numpy_ma_where(y.mask, x, z)
if x.mask is numpy_ma_nomask: #not numpy_any(x.mask):
x = numpy_array(x)
else:
# x and y are both unmasked
if not numpy_ndim(x):
# Make sure that we have a numpy array (as opposed to,
# e.g. a numpy.float64)
x = numpy_asanyarray(x)
numpy_maximum(x, y, out=x)
return x
#--- End: def
def pmin(x, y):
'''
:Parameters:
x : numpy array
May be updated in place and should not be used again.
y : numpy array
Will not be updated in place.
:Returns:
out : numpy array
'''
if numpy_ma_isMA(x):
if numpy_ma_isMA(y):
# x and y are both masked
z = numpy_minimum(x, y)
z = numpy_ma_where(x.mask & -y.mask, y, z)
x = numpy_ma_where(y.mask & -x.mask, x, z)
if x.mask is numpy_ma_nomask:
x = numpy_array(x)
else:
# Only x is masked
z = numpy_minimum(x, y)
x = numpy_ma_where(x.mask, y, z)
if x.mask is numpy_ma_nomask:
x = numpy_array(x)
elif numpy_ma_isMA(y):
# Only y is masked
z = numpy_minimum(x, y)
x = numpy_ma_where(y.mask, x, z)
if x.mask is numpy_ma_nomask:
x = numpy_array(x)
else:
# x and y are both unmasked
if not numpy_ndim(x):
# Make sure that we have a numpy array (as opposed to,
# e.g. a numpy.float64)
x = numpy_asanyarray(x)
numpy_minimum(x, y, out=x)
return x
#--- End: def
def mask_where_too_few_values(Nmin, N, x):
'''Mask elements of N and x where N is strictly less than Nmin.
:Parameters:
Nmin: `int`
N: `numpy.ndarray`
x: `numpy.ndarray`
:Returns:
out: (`numpy.ndarray`, `numpy.ndarray`)
A tuple containing *N* and *x*, both masked where *N* is
strictly less than *Nmin*.
'''
if N.min() < Nmin:
mask = N < Nmin
N = numpy_ma_array(N, mask=mask, copy=False, shrink=False)
x = numpy_ma_array(x, mask=mask, copy=False, shrink=True)
return N, x
#--- End: def
#---------------------------------------------------------------------
# Maximum
#---------------------------------------------------------------------
def max_f(a, axis=None, masked=False):
'''
Return the maximum of an array, or the maxima of an array along an
axis.
:Parameters:
a : numpy array_like
Input array
axis : int, optional
Axis along which to operate. By default, flattened input is
used.
masked : bool
:Returns:
out : 2-tuple of numpy arrays
The sample sizes and the maxima.
'''
N = sample_size_f(a, axis=axis, masked=masked)
amax = numpy_amax(a, axis=axis)
if not numpy_ndim(amax):
# Make sure that we have a numpy array (as opposed to, e.g. a
# numpy.float64)
amax = numpy_asanyarray(amax)
return N, amax
#--- End: def
def max_fpartial(out, out1=None):
N, amax = out
if out1 is not None:
N1, amax1 = out1
N = psum(N, N1)
amax = pmax(amax, amax1)
#--- End: if
return N, amax
#--- End: def
def max_ffinalise(out, sub_samples=None):
'''
sub_samples : *optional*
Ignored.
'''
return mask_where_too_few_values(1, *out)
#--- End: def
#---------------------------------------------------------------------
# Minimum
#---------------------------------------------------------------------
def min_f(a, axis=None, masked=False):
'''
Return the minimum of an array, or the minima of an array along an
axis.
:Parameters:
a : numpy array_like
Input array
axis : int, optional
Axis along which to operate. By default, flattened input is
used.
masked : bool
:Returns:
out : 2-tuple of numpy arrays
The sample sizes and the minima.
'''
N = sample_size_f(a, axis=axis, masked=masked)
amin = numpy_amin(a, axis=axis)
if not numpy_ndim(amin):
# Make sure that we have a numpy array (as opposed to, e.g. a
# numpy.float64)
amin = numpy_asanyarray(amin)
return N, amin
#--- End: def
def min_fpartial(out, out1=None):
'''
'''
N, amin = out
if out1 is not None:
N1, amin1 = out1
N = psum(N, N1)
amin = pmin(amin, amin1)
#--- End: if
return N, amin
#--- End: def
def min_ffinalise(out, sub_samples=None):
'''
sub_samples : *optional*
Ignored.
'''
return mask_where_too_few_values(1, *out)
#--- End: def
#---------------------------------------------------------------------
# Mean
#---------------------------------------------------------------------
def mean_f(a, axis=None, weights=None, masked=False):
'''
The weighted average along the specified axes.
:Parameters:
a : array-like
Input array. Not all missing data
axis : int, optional
Axis along which to operate. By default, flattened input is
used.
weights : array-like, optional
masked : bool, optional
kwargs : ignored
:Returns:
out : tuple
3-tuple.
'''
if masked:
average = numpy_ma_average
else:
average = numpy_average
avg, sw = average(a, axis=axis, weights=weights, returned=True)
if not numpy_ndim(avg):
avg = numpy_asanyarray(avg)
sw = numpy_asanyarray(sw)
if weights is None:
N = sw.copy()
else:
N = sample_size_f(a, axis=axis, masked=masked)
return N, avg, sw
#--- End: def
def mean_fpartial(out, out1=None):
'''
:Returns:
out: `numpy.ndarray`, `numpy.ndarray`, `numpy.ndarray`
'''
N, avg, sw = out
if out1 is None:
avg *= sw
else:
N1, avg1, sw1 = out1
avg1 *= sw1
N = psum(N, N1)
avg = psum(avg, avg1)
sw = psum(sw, sw1)
#--- End: if
return N, avg, sw
#--- End: def
def mean_ffinalise(out, sub_samples=None):
'''
sub_samples: optional
Ignored.
:Returns:
out: `numpy.ndarray`, `numpy.ndarray`
'''
N, avg, sw = out
if sub_samples:
avg /= sw
return mask_where_too_few_values(1, N, avg)
#--- End: def
#---------------------------------------------------------------------
# Mid range: Average of maximum and minimum
#---------------------------------------------------------------------
def mid_range_f(a, axis=None, masked=False):
'''
Return the minimum and maximum of an array or the minima and maxima
along an axis.
``mid_range_f(a, axis=axis)`` is equivalent to ``(numpy.amin(a,
axis=axis), numpy.amax(a, axis=axis))``
:Parameters:
a : numpy array_like
Input array
axis : int, optional
Axis along which to operate. By default, flattened input is
used.
kwargs : ignored
:Returns:
out : tuple
The minimum and maximum inside a 2-tuple.
'''
N = sample_size_f(a, axis=axis, masked=masked)
amin = numpy_amin(a, axis=axis)
amax = numpy_amax(a, axis=axis)
if not numpy_ndim(amin):
# Make sure that we have a numpy array (as opposed to, e.g. a
# numpy.float64)
amin = numpy_asanyarray(amin)
amax = numpy_asanyarray(amax)
return N, amin, amax
#--- End: def
def mid_range_fpartial(out, out1=None):
'''
'''
N, amin, amax = out
if out1 is not None:
N1, amin1, amax1 = out1
N = psum(N, N1)
amin = pmin(amin, amin1)
amax = pmax(amax, amax1)
#--- End: if
return N, amin, amax
#--- End: def
def mid_range_ffinalise(out, sub_samples=None):
'''
:Parameters:
out : ordered sequence
amin : numpy.ndarray
amax : numpy.ndarray
sub_samples : *optional*
Ignored.
'''
N, amin, amax = out
# Cast bool, unsigned int, and int to float64
if issubclass(amax.dtype.type, (numpy_integer, numpy_bool_)):
amax = amax.astype(float)
amax += amin
amax *= 0.5
return mask_where_too_few_values(1, N, amax)
#--- End: def
#---------------------------------------------------------------------
# Range: Absolute difference between maximum and minimum
#---------------------------------------------------------------------
range_f = mid_range_f
range_fpartial = mid_range_fpartial
def range_ffinalise(out, sub_samples=None):
'''
Absolute difference between maximum and minimum
:Parameters:
out : ordered sequence
sub_samples : *optional*
Ignored.
'''
N, amin, amax = out
amax -= amin
return mask_where_too_few_values(1, N, amax)
#--- End: def
#---------------------------------------------------------------------
# Sample size
#---------------------------------------------------------------------
def sample_size_f(a, axis=None, masked=False):
'''
axis : int, optional
non-negative
'''
if masked:
N = numpy_sum(-a.mask, axis=axis, dtype=float)
if not numpy_ndim(N):
N = numpy_asanyarray(N)
else:
if axis is None:
N = numpy_array(a.size, dtype=float)
else:
shape = a.shape
N = numpy_empty(shape[:axis]+shape[axis+1:], dtype=float)
N[...] = shape[axis]
#--- End: if
return N
#--- End: def
def sample_size_fpartial(N, out1=None):
'''
:Parameters:
N : numpy array
:Returns:
out : numpy array
'''
if out1 is not None:
N1 = out1
N = psum(N, N1)
return N
#--- End: def
def sample_size_ffinalise(N, sub_samples=None):
'''
:Parameters:
N : numpy array
sub_samples : *optional*
Ignored.
:Returns:
out : tuple
A 2-tuple containing *N* twice.
'''
return N, N
#--- End: def
#---------------------------------------------------------------------
# Sum
#---------------------------------------------------------------------
def sum_f(a, axis=None, masked=False):
'''
Return the sum of an array or the sum along an axis.
``sum_f(a, axis=axis)`` is equivalent to ``(numpy.sum(a,
axis=axis),)``
:Parameters:
array : numpy array-like
Input array
axis : int, optional
Axis along which to operate. By default, flattened input is
used.
kwargs : ignored
:Returns:
out : tuple
2-tuple
'''
N = sample_size_f(a, axis=axis, masked=masked)
asum = a.sum(axis=axis)
if not numpy_ndim(asum):
asum = numpy_asanyarray(asum)
return N, asum
#--- End: def
def sum_fpartial(out, out1=None):
'''
'''
N, asum = out
if out1 is not None:
N1, asum1 = out1
N = psum(N, N1)
asum = psum(asum, asum1)
#--- End: if
return N, asum
#--- End: def
def sum_ffinalise(out, sub_samples=None):
'''
sub_samples : *optional*
Ignored.
'''
return mask_where_too_few_values(1, *out)
#--- End: def
#---------------------------------------------------------------------
# Sum of weights
#---------------------------------------------------------------------
def sw_f(a, axis=None, masked=False, weights=None):
'''
'''
N = sample_size_f(a, axis=axis, masked=masked)
if weights is not None:
if weights.ndim < a.ndim:
weights = broadcast_array(weights, a.shape)
if masked:
weights = numpy_ma_array(weights, mask=a.mask, copy=False)
sw = weights.sum(axis=axis)
if not numpy_ndim(sw):
sw = numpy_asanyarray(sw)
else:
sw = N.copy()
return N, sw
#--- End: def
sw_fpartial = sum_fpartial
sw_ffinalise = sum_ffinalise
#---------------------------------------------------------------------
# Sum of squares of weights
#---------------------------------------------------------------------
def sw2_f(a, axis=None, masked=False, weights=None):
'''
'''
N = sample_size_f(a, axis=axis, masked=masked)
if weights is not None:
if weights.ndim < a.ndim:
weights = broadcast_array(weights, a.shape)
if masked:
weights = numpy_ma_array(weights, mask=a.mask, copy=False)
sw2 = (weights*weights).sum(axis=axis)
if not numpy_ndim(sw2):
sw2 = numpy_asanyarray(sw2)
else:
sw2 = N.copy()
return N, sw2
#--- End: def
sw2_fpartial = sum_fpartial
sw2_ffinalise = sum_ffinalise
#---------------------------------------------------------------------
# Variance
#---------------------------------------------------------------------
def var_f(a, axis=None, weights=None, masked=False, ddof=1, f=None):
'''
:Return:
out: 8-`tuple` of `numpy.ndarray`
'''
# ----------------------------------------------------------------
# Find the minimum and maximum values of the weights if required
# ----------------------------------------------------------------
if not f and weights is not None and ddof:
if weights.ndim <= 1:
# Weights are 1-d
wmin = weights.min()
wmax = weights.max()
else:
# Weights have the same shape as a
wmin = weights.min(axis=axis)
wmax = weights.max(axis=axis)
else:
wmin = None
wmax = None
# ----------------------------------------------------------------
# Methods:
#
# http://en.wikipedia.org/wiki/Standard_deviation#Population-based_statistics
# http://en.wikipedia.org/wiki/Weighted_mean#Weighted_sample_variance
# ----------------------------------------------------------------
N, avg, sw = mean_f(a, weights=weights, axis=axis, masked=masked)
if axis is not None and avg.size > 1:
# We collapsed over a single axis and the array has 2 or more
# axes, so add an extra size 1 axis to the mean so that
# broadcasting works when we calculate the variance.
reshape_avg = True
if masked:
expand_dims = numpy_ma_expand_dims
else:
expand_dims = numpy_expand_dims
avg = expand_dims(avg, axis)
else:
reshape_avg = False
var = a - avg
var *= var
if masked:
average = numpy_ma_average
else:
average = numpy_average
var = average(var, axis=axis, weights=weights)
if reshape_avg:
shape = avg.shape
avg = avg.reshape(shape[:axis] + shape[axis+1:])
if not numpy_ndim(var):
var = numpy_asanyarray(var)
return N, var, avg, sw, ddof, f, wmin, wmax
#--- End: def
def var_fpartial(out, out1=None):
'''
'''
N, var, avg, sw, ddof, f, wmin, wmax = out
if out1 is None:
# ------------------------------------------------------------
# var = sw(var+avg**2)
# avg = sw*avg
# ------------------------------------------------------------
var += avg*avg
var *= sw
avg *= sw
else:
# ------------------------------------------------------------
# var = var + sw1(var1+avg1**2)
# avg = avg + sw1*avg1
# sw = sw + sw1
# ------------------------------------------------------------
N1, var1, avg1, sw1, ddof, f, wmin1, wmax1 = out1
N = psum(N, N1)
var1 += avg1*avg1
var1 *= sw1
avg1 *= sw1
var = psum(var, var1)
avg = psum(avg, avg1)
sw = psum(sw , sw1)
if wmin is not None:
wmin = pmin(wmin, wmin1)
wmax = pmax(wmax, wmax1)
#--- End: def
return N, var, avg, sw, ddof, f, wmin, wmax
#--- End: def
def var_ffinalise(out, sub_samples=None):
'''
'''
N, var, avg, sw, ddof, f, wmin, wmax = out
N, var = mask_where_too_few_values(max(2, ddof+1), N, var)
if sub_samples:
# ----------------------------------------------------------------
# The global biased variance = {[SUM(psw(pv+pm**2)]/sw} - m**2
#
# where psw = partial sum of weights
# pv = partial biased variance
# pm = partial mean
# sw = global sum of weights
# m = global mean
#
# Currently: var = SUM(psw(pv+pm**2)
# avg = sw*m
#
# http://en.wikipedia.org/wiki/Standard_deviation#Population-based_statistics
# ----------------------------------------------------------------
avg /= sw
avg *= avg
var /= sw
var -= avg
#--- End: if
# ----------------------------------------------------------------
# var is now the global variance with sw degrees of freedom
# ----------------------------------------------------------------
if ddof:
if f:
sw *= f
elif wmin is not None:
# ---------------------------------------------------------
# The global variance is weighted and needs to be
# calculated with greater than 0 delta degrees of
# freedom. The sum of weights (sw) needs to be adjusted:
#
# sw = f*sw (approximately!)
#
# where f = smallest positive number whose products with
# the smallest and largest weights and the sum of
# weights are all integers
# ---------------------------------------------------------
wmin = wmin.astype(float)
wmax = wmax.astype(float)
sw = sw.astype(float)
wmax /= wmin
sw /= wmin
if (not numpy_allclose(wmax, wmax.astype(int), rtol=1e-05, atol=1e-08) or
not numpy_allclose(sw , sw.astype(int) , rtol=1e-05, atol=1e-08)):
m = numpy_zeros(wmax.shape, dtype=int)
n = 2
while True:
nwmax = n*wmax
nsw = n*sw
ccc = (m == 0)
ccc &= numpy.isclose(nwmax, nwmax.astype(int), rtol=1e-05, atol=1e-08)
ccc &= numpy.isclose(nsw , nsw.astype(int) , rtol=1e-05, atol=1e-08)
m = numpy_where(ccc, n, m)
if m.min():
# Every element of m has been set (to an integer
# greater than 1), so we are done.
break
# Some elements of m have not been set, so try the
# next multiplier.
n += 1
#--- End: while
sw *= m
#--- End: if
#--- End: if
# ------------------------------------------------------------
# Adjust the variance for fewer than sw degrees of freedom:
#
# var = var*sw/(sw-ddof)
# ------------------------------------------------------------
var *= sw
sw -= ddof
var /= sw
#--- End: if
return N, var
#--- End: def
#---------------------------------------------------------------------
# Standard deviation
#---------------------------------------------------------------------
sd_f = var_f
sd_fpartial = var_fpartial
def sd_ffinalise(out, sub_samples=None):
'''
:Parameters:
out : tuple
A 2-tuple
sub_samples : *optional*
Ignored.
'''
N, sd = var_ffinalise(out, sub_samples)
sd **= 0.5
return N, sd
#--- End: def
|