In Module numarray.ma, an array is an instance of class MaskedArray, which is defined in the module numarray.ma. An instance of class MaskedArray can be thought of as containing the following parts:
filled(x)
that is converted
to a string.
A) |
yn = numarray.array(x)
type) |
y = x.astype(Float32)
) |
y = x.byte_swapped()
) |
y = x.compressed()
axis=None) |
n = x.count() y = x.count(0)
) |
v = x.fill_value()
fill_value=None) |
yn = x.filled()
) |
id1, id2 = x.ids()
) |
if x.iscontiguous():
) |
) |
m = x.mask()
values) |
x.put(values)
values) |
x.putmask(values) assert getmask(x) is None
) |
d = x.raw_data ()
v) |
x.savespace (1)
v) |
args...) |
x.set_shape (3, 12)
axis) |
totalsize = x.size () col_len = x.size (1)
) |
flag = x.spacesaver()
fill_value=None) |
self.filled(fill_value).tolist()
; note
that masked values are filled.
alist=x.tolist()
fill_value=None) |
self.filled(fill_value).tostring()s = x.tostring()
) |
x.unmask()
) |
x.unshare_mask()
data, type=None, copy=1, savespace=0, mask=None, fill_value=None) |
array(d, copy=0)
is d.
data, mask=None, fill_value=None) |
typecode = None
, copy = 0
. When
data is newly-created this function can be used to make it a masked
array without copying the data if data is already a numarray
array.
data, value, rtol=1.e-5, atol=1.e-8, type=None, copy=1, savespace=0)) |
(18.1) |
data, value, copy=1, savespace=0) |
data, type=None) |
array(data, typecode, copy=0)
. It is a short way
of ensuring that something is an instance of MaskedArray of a given
type before proceeding, as in "data = asarray(data)".
If data already is a masked array and type is None then the return value is data; nothing is copied in that case.
condition, data, copy=1)) |
x[1]
is masked, the quantity x[1]
will be this special
constant. This special element is discussed more fully in section
18.6.1 ``The constant masked''.
The following additional constructors are provided for convenience.
data, value, copy=1) |
data, value, copy=1) |
data, value, copy=1) |
data, value, copy=1) |
data, value, copy=1) |
data, value, copy=1) |
masked_where(greater(data,
value), data))
. Similarly, masked_greater_equal,
masked_equal, masked_not_equal, masked_less,
masked_less_equal are called in the same way with the obvious
meanings. Note that for floating point data, masked_values is
preferable to masked_equal in most cases.
data, v1, v2, copy=1) |
[v1, v2]
masked.
v1 and v2 may be in either order.
data, v1, v2, copy=1) |
[v1, v2]
masked. v1 and v2 may be in either order.
On entry to any of these constructors, data must be any object which the numarray package can accept to create an array (with the desired type, if specified). The mask, if given, must be None or any object that can be turned into a numarray array of integer type (it will be converted to type MaskType, if necessary), have the same shape as data, and contain only values of 0 or 1.
If the mask is not None but its shape does not match that of data, an exception will be thrown, unless one of the two is of length 1, in which case the scalar will be resized (using numarray.resize) to match the other.
See section 18.3.7 ``Copying or not'' for a discussion of whether or not the resulting array shares its data or its mask with the arguments given to these constructors.
x, value=None) |
numarray.array(x)
is returned.
fill_value(x)
if value is None, is returned. If
the fill value used is of a different type or precision than x, the
result may be of a different type or precision than x.
The function filled plays a central role in our design. It is the ``exit'' back to numarray, and is used whenever the invalid values must be replaced before an operation. For example, adding two masked arrays a and b is roughly:
masked_array(filled(a, 0) + filled(b, 0), mask_or(getmask(a), getmask(b))
filled also can be used to simply be certain that some expression is a contiguous numarray array at little cost. If its argument is a numarray array already, it is returned without copying.
If you are certain that a masked array x contains a mask that is None or is all zeros, you can convert it to a numarray array with the numarray.array(x) constructor. If you turn out to be wrong, an MAError exception is raised.
x) |
) |
fill_value(x)
and the method x.fill_value()
on masked arrays,
return a value suitable for filling x based on its type. If x
is a masked array, then x.fill_value() results. The returned value for
a given type can be changed by assigning to the following names in module
numarray.ma. They should be set to scalars or one element arrays.
default_real_fill_value = numarray.array([1.0e20], Float32) default_complex_fill_value = numarray.array([1.0e20 + 0.0j], Complex32) default_character_fill_value = masked default_integer_fill_value = numarray.array([0]).astype(UnsignedInt8) default_object_fill_value = masked
set_fill_value(a,
fill_value)
is the same as a.set_fill_value (fill_value)
if a
is a masked array; otherwise it does nothing. Please note that the fill
value is mostly cosmetic; it is used when it is needed to convert the masked
array to a plain numarray array but not involved in most
operations. In particular, setting the fill_value to
1.e20 will not, repeat not, cause elements of the array
whose values are currently 1.e20 to be masked. For that sort of behavior use
the masked_value constructor.
Masks are either None or 1-byte numarray arrays of 1's and 0's. To avoid excessive performance penalties, mask arrays are never checked to be sure that the values are 1's and 0's, and supplying a mask argument to a constructor with an illegal mask will have undefined consequences later.
Masks have the savespace attribute set. This attribute, discussed in
part I, may have surprising consequences if you
attempt to do any operations on them other than those supplied by this package.
In particular, do not add or multiply a quantity involving a mask. For example,
if m is a mask consisting of 1080 1 values, sum(m)
is 56, not
1080. Oops.
m) |
m, copy=0, flag=0) |
x) |
x.mask()
, the mask of
x, if x is a masked array, and None otherwise.
Note:
getmask may return None if x is a masked
array but has a mask of None. (Please see caution above about
operating on the result).
x) |
x.mask()
if x is a masked array and has a mask that is
not None; otherwise it returns a zero mask array of the same
shape as x. Unlike getmask, getmaskarray always
returns an numarray array of typecode MaskType. (Please
see caution above about operating on the result).
m1, m2) |
Masked arrays support the operators , , , , , and unary plus and minus. The other operand can be another masked array, a scalar, a numarray array, or something numarray.array can convert to a numarray array. The results are masked arrays.
In addition masked arrays support the in-place operators , , , and . Implementation of in-place operators differs from numarray semantics in being more generous about converting the right-hand side to the required type: any kind or lesser type accepted via an astype conversion. In-place operators truly operate in-place when the target is not masked.
Depending on the arguments results of constructors may or may not contain a
separate copy of the data or mask arguments. The easiest way to think about
this is as follows: the given field, be it data or a mask, is required to be a
numarray array, possibly with a given typecode, and a mask's shape
must match that of the data. If the copy argument is zero, and the candidate
array otherwise qualifies, a reference will be made instead of a copy. If for
any reason the data is unsuitable as is, an attempt will be made to make a copy
that is suitable. Should that fail, an exception will be thrown. Thus, a
copy=0
argument is more of a hope than a command.
If the basic array constructor is given a masked
array as the first argument, its mask, typecode, spacesaver flag, and fill
value will be used unless specifically specified by one of the remaining
arguments. In particular, if d is a masked array, array(d, copy=0)
is d.
Since the default behavior for masks is to use a reference if possible, rather than a copy, which produces a sizeable time and space savings, it is especially important not to modify something you used as a mask argument to a masked array creation routine, if it was a numarray array of typecode MaskType.
a) |
a) |
a) |
a) |
filled(a)
.
Indexing and slicing differ from Numeric: while generally the same, they return a copy, not a reference, when used in an expression that produces a non-scalar result. Consider this example:
from Numeric import * x = array([1.,2.,3.]) y = x[1:] y[0] = 9. print x
[1., 9., 3.]
since x[1:]
returns a reference to a
portion of x. Doing the same operation using numarray.ma,
from numarray.ma import * x = array([1.,2.,3.]) y = x[1:] y[0] = 9. print x
[1., 2., 3.]
, while y will be a separate array whose
present value would be [9., 3.]
. While sentiment on the correct
semantics here is divided amongst the Numeric Python community as a whole, it
is not divided amongst the author's community, on whose behalf this package is
written.
Using multiple sets of square brackets on the left side of an assignment statement will not produce the desired result:
x = array([[1,2],[3,4]]) x[1][1] = 20. # Error, does not change x x[1,1] = 20. # Correct, changes x
x[1]
is a copy, so changing it changes that copy, not
x. Always use just one single square bracket for assignments.
If indexing or another operation on a masked array produces a scalar result, then a scalar value is returned rather than a one-element masked array. This raises the issue of what to return if that result is masked. The answer is that the module constant masked is returned. This constant is discussed in section 18.6.1. While this most frequently occurs from indexing, you can also get such a result from other functions. For example, averaging a 1-D array, all of whom's values are invalid, would result in masked.
Assignment of a normal value to a single element or slice of a masked array has the effect of clearing the mask in those locations. In this way previously invalid elements become valid. The value being assigned is filled first, so that you are guaranteed that all the elements on the left-hand side are now valid.
Assignment of None to a single element or slice of a masked array has the effect of setting the mask in those locations, and the locations become invalid.
Since these operations change the mask, the result afterwards will no longer share a mask, since masks have copy-on-write semantics.
Send comments to the NumArray community.