Table Of Contents

This Page

Main data structures in PTSA

Attribute Array (AttrArray)

class dimarray.AttrArray

An AttrArray (short for Attribute Array) is simply a NumPy ndarray which allows the specification of custom attributes. These attributes can be specified as keyword arguments or set and changed on the fly as shown in the examples below.

AttrArray instances are initialized just like ndarray instances but they accept arbitrary keyword arguments that can be used to specify custom attributes during initialization. Every AttrArray has a protected (read-only) _required_attrs attribute, which is None when no attributes are required (as is the case for instances of AttrArray) or a dictionary that specifies required attributes (for child classes of AttrArray, such as Dim and DimArray).

Examples

>>> import numpy as np
>>> import dimarray as da
>>> data = da.AttrArray(np.random.rand(5), hello='world')
>>> print data.hello
world
>>> data.hello = 'good bye'
>>> print data.hello
good bye
>>> data.version = 1.0
>>> print data.version
1.0

These custom attributes are maintained when copying or manipulating the data in an AttrArray:

>>> data2 = data.mean()
>>> data2.hello
good bye    
nanmean(a, axis=None)

Compute the arithmetic mean along the specified axis ignoring nans.

Returns the average of the array elements treating nans as missing values. The average is taken over the flattened array by default, otherwise over the specified axis. float64 intermediate and return values are used for integer inputs.

Parameters:

a : array_like

Calculate the mean of these values.

axis : int, optional

Axis along which the means are computed. The default is to compute the mean of the flattened array.

Returns:

mean : {ndarray, scalar}

Return a new array containing the mean values (or scalar if axis is None).

See also

nanvar
Variance ignoring nans
nanstd
Standard deviation ignoring nans.
numpy.average
Weighted average.

Notes

The arithmetic mean is the sum of the elements along the axis divided by the number of elements. Nans are ignored.

This docstring is based on that for numpy.mean, the code is based on scipy.stats.nanmean.

Examples

>>> a = AttrArray([[1, 2], [3, 4], [5, 6]])
>>> a.nanmean()
3.5
>>> a = AttrArray([[np.nan, 2], [3, 4], [5, 6]])
>>> a.nanmean()
4.0
>>> a.nanmean(0)
AttrArray([ 4.0,  4.0)
>>> a.nanmean(1)
AttrArray([ 2.0,  3.5,  5.5])
nanstd(a, axis=None, ddof=0)

Compute the standard deviation along the specified axis ignoring nans.

Returns the standard deviation, a measure of the spread of a distribution, of the array elements, treating nans as missing values. The standard deviation is computed for the flattened array by default, otherwise over the specified axis.

Parameters:

a : array_like

Calculate the standard deviation of these values.

axis : int, optional

Axis along which the standard deviation is computed. The default is to compute the standard deviation of the flattened array.

ddof : int, optional

Means Delta Degrees of Freedom. The divisor used in calculations is N - ddof, where N represents the number of elements. By default ddof is zero (biased estimate).

Returns:

standard_deviation : {ndarray, scalar}

Return a new array containing the standard deviations (or scalar if axis is None). The dtype is always at least np.float64.

See also

nanvar
Variance ignoring nans
numpy.std
Standard deviation
numpy.var
Variance
numpy.mean
Average

Notes

If no nan values are present, returns the same value as numpy.std, otherwise, the standard deviation is calculated as if the nan values were not present.

The standard deviation is the square root of the average of the squared deviations from the mean, i.e., var = sqrt(mean(abs(x - x.mean())**2)).

The mean is normally calculated as x.sum() / N, where N = len(x). If, however, ddof is specified, the divisor N - ddof is used instead.

Note that, for complex numbers, std takes the absolute value before squaring, so that the result is always real and nonnegative.

This docstring is based on that for numpy.std, the code is based on scipy.stats.nanstd.

Examples

>>> a = AttrArray([[1, 2], [3, 4], [5, 6]])
>>> a.nanstd()
1.707825127659933
>>> a = AttrArray([[np.nan, 2], [3, 4], [5, 6]])
>>> a.nanstd()
1.4142135623730951
>>> a.nanstd(0)
AttrArray([ 1.,  1.6329931618554521])
>>> a.nanstd(1)
AttrArray([ 0.0,  0.5,  0.5])
nanvar(a, axis=None, ddof=0)

Compute the variance along the specified axis ignoring nans.

Returns the variance of the array elements, a measure of the spread of a distribution, treating nans as missing values. The variance is computed for the flattened array by default, otherwise over the specified axis.

Parameters:

a : array_like

Calculate the variance of these values.

axis : int, optional

Axis along which the variance is computed. The default is to compute the variance of the flattened array.

ddof : int, optional

Means Delta Degrees of Freedom. The divisor used in calculations is N - ddof, where N represents the number of elements. By default ddof is zero (biased estimate).

Returns:

variance : {ndarray, scalar}

Return a new array containing the variances (or scalar if axis is None). The dtype of the output is always at least np.float64.

See also

nanstd
Standard deviation ignoring nans.
numpy.std
Standard deviation
numpy.var
Variance
numpy.mean
Average

Notes

If no nan values are present, returns the same value as numpy.var, otherwise, the variance is calculated as if the nan values were not present.

The variance is the average of the squared deviations from the mean, i.e., var = mean(abs(x - x.mean())**2). The mean is normally calculated as x.sum() / N, where N = len(x). If, however, ddof is specified, the divisor N - ddof is used instead.

Note that, for complex numbers, var takes the absolute value before squaring, so that the result is always real and nonnegative.

This docstring is based on that for numpy.var, the code is based on scipy.stats.nanstd.

Examples

>>> a = AttrArray([[1, 2], [3, 4], [5, 6]])
>>> a.nanvar()
2.9166666666666665
>>> a = AttrArray([[np.nan, 2], [3, 4], [5, 6]])
>>> a.nanvar()
2.0
>>> a.nanvar(0)
AttrArray([ 1.,  2.6666666666666665)
>>> a.nanvar(1)
AttrArray([ 0.0,  0.25,  0.25])

Dimension (Dim)

class dimarray.Dim

Dim is a child class of AttrArray with the constraints that each instance be 1-dimensional and have a name attribute. If multi dimensional input is specified during initialization, an attempt is made to convert it to one dimension by collapsing over dimensions that only have one level (if that fails an error is raised).

Parameters:

data : {1-D array_like}

The values of the dimension (e.g., time points for a time dimension)

name : str

The name of the dimension (e.g., ‘time’). When used in a DimArray this must be a valid identifier name (i.e., consist only of letters, numbers and underscores and not start with a number).

dtype : numpy.dtype, optional

The data type.

copy : bool, optional

Flag specifying whether or not data should be copied.

**kwargs : {key word arguments}, optional

Additional custom attributes (e.g., units=’ms’).

Examples

>>> import numpy as np
>>> import dimarray as da
>>> test = da.Dim([[1,2,3]], name='dimension_1')
>>> print test
[1 2 3]

Dimensioned Array (DimArray)

class dimarray.DimArray

A DimArray (short for Dimensioned Array) is a child class of AttrArray with the constraints that each instance and have a dims attribute which specifies the dimensions as an array of Dim instances. The name of the Dim instances in dims must be unique and they must correspond to the dimensions of the DimArray in the correct order. If dims is not specified, generic dimensions will be automatically generated from the data.

The DimArray class provides a number of conveniences above and beyond normal ndarrays. These include the ability to refer to dimensions by name and to select subsets of the data based on complex queries using the dimension names.

Parameters:

data : array_like

The dimensioned data.

dims : {container of Dim instances}, optional

The dimensions of the data.

dtype : dtype,optional

The data type.

copy : bool,optional

Flag specifying whether or not data should be copied.

**kwargs : {key word arguments}, optional

Additional custom attributes.

Examples

>>> import numpy as np
>>> import dimarray as da
>>> dim1 = da.Dim(range(5),'Dim1')
>>> dim2 = da.Dim(['a','b','c'],'Dim2')
>>> data = da.DimArray(np.random.rand(5,3),[dim1,dim2])
>>> data
DimArray([[ 0.59645979,  0.92462876,  0.76882167],
       [ 0.3581822 ,  0.57873905,  0.76889117],   
       [ 0.40272846,  0.69372032,  0.59006832],   
       [ 0.69862889,  0.68334188,  0.10891802],   
       [ 0.14111733,  0.97454223,  0.73193147]])  
>>> data.dims
array([[0 1 2 3 4], ['a' 'b' 'c']], dtype=object)
>>> data['Dim1']
Dim([0, 1, 2, 3, 4])
>>> data['Dim2']
Dim(['a', 'b', 'c'],
      dtype='|S1')
>>> data.dim_names
['Dim1', 'Dim2']
>>> data.mean('Dim1')
DimArray([ 0.43942333,  0.77099445,  0.59372613])
>>> np.mean(data,'Dim1')
DimArray([ 0.43942333,  0.77099445,  0.59372613])
>>> data['Dim1 > 2']
DimArray([[ 0.69862889,  0.68334188,  0.10891802],
       [ 0.14111733,  0.97454223,  0.73193147]])
>>> data["Dim2 == 'c'"]
DimArray([ 0.76882167,  0.76889117,  0.59006832,  0.10891802,  0.73193147])
>>> data['Dim1 > 2',"Dim2 == 'a'"]
DimArray([ 0.69862889,  0.14111733])
>>> data['Dim1>2'] +=1
>>> data
DimArray([[ 0.59645979,  0.92462876,  0.76882167],
       [ 0.3581822 ,  0.57873905,  0.76889117],
       [ 0.40272846,  0.69372032,  0.59006832],
       [ 1.69862889,  1.68334188,  1.10891802],
       [ 1.14111733,  1.97454223,  1.73193147]])
>>> data['Dim1>2',"Dim2>'a'"]
DimArray([[ 1.68334188,  1.10891802],
        [ 1.97454223,  1.73193147]])
>>> data['Dim1>2',"Dim2>'a'"]+=1
>>> data
DimArray([[ 0.59645979,  0.92462876,  0.76882167],
       [ 0.3581822 ,  0.57873905,  0.76889117],
       [ 0.40272846,  0.69372032,  0.59006832],
       [ 1.69862889,  2.68334188,  2.10891802],
       [ 1.14111733,  2.97454223,  2.73193147]])
>>> data = da.DimArray(np.random.rand(4,5))
>>> data.dim_names
['dim1', 'dim2']
>>> data.dims
array([[0 1 2 3], [0 1 2 3 4]], dtype=object)
all(axis=None, out=None)
Below is the docstring from numpy.ndarray. For DimArray instances, the axis may be specified as string (dimension name).

a.all(axis=None, out=None)

Returns True if all elements evaluate to True.

Refer to numpy.all for full documentation.

See also

numpy.all
equivalent function
any(axis=None, out=None)
Below is the docstring from numpy.ndarray. For DimArray instances, the axis may be specified as string (dimension name).

a.any(axis=None, out=None)

Check if any of the elements of a are true.

Refer to numpy.any for full documentation.

See also

numpy.any
equivalent function
argmax(axis=None, out=None)
Below is the docstring from numpy.ndarray. For DimArray instances, the axis may be specified as string (dimension name).

a.argmax(axis=None, out=None)

Return indices of the maximum values along the given axis of a.
Parameters:

axis : int, optional

Axis along which to operate. By default flattened input is used.

out : ndarray, optional

Alternative output array in which to place the result. Must be of the same shape and buffer length as the expected output.

Returns:

index_array : ndarray

An array of indices or single index value, or a reference to out if it was specified.

Examples

>>> a = np.arange(6).reshape(2,3)
>>> a.argmax()
5
>>> a.argmax(0)
array([1, 1, 1])
>>> a.argmax(1)
array([2, 2])
argmin(axis=None, out=None)
Below is the docstring from numpy.ndarray. For DimArray instances, the axis may be specified as string (dimension name).

a.argmin(axis=None, out=None)

Return indices of the minimum values along the given axis of a.

Refer to numpy.ndarray.argmax for detailed documentation.

argsort(axis=-1, kind='quicksort', order=None)
Below is the docstring from numpy.ndarray. For DimArray instances, the axis may be specified as string (dimension name).

a.argsort(axis=-1, kind=’quicksort’, order=None)

Returns the indices that would sort this array.

Refer to numpy.argsort for full documentation.

See also

numpy.argsort
equivalent function
compress(condition, axis=None, out=None)
Below is the docstring from numpy.ndarray. For DimArray instances, the axis may be specified as string (dimension name).

a.compress(condition, axis=None, out=None)

Return selected slices of this array along given axis.

Refer to numpy.compress for full documentation.

See also

numpy.compress
equivalent function
cumprod(axis=None, dtype=None, out=None)
Below is the docstring from numpy.ndarray. For DimArray instances, the axis may be specified as string (dimension name).

a.cumprod(axis=None, dtype=None, out=None)

Return the cumulative product of the elements along the given axis.

Refer to numpy.cumprod for full documentation.

See also

numpy.cumprod
equivalent function
cumsum(axis=None, dtype=None, out=None)
Below is the docstring from numpy.ndarray. For DimArray instances, the axis may be specified as string (dimension name).

a.cumsum(axis=None, dtype=None, out=None)

Return the cumulative sum of the elements along the given axis.

Refer to numpy.cumsum for full documentation.

See also

numpy.cumsum
equivalent function
diagonal(*args, **kwargs)
CAUTION: the output of this method is cast to an AttrArray instance. Some attributes may no longer be valid after this Method is applied!

a.diagonal(offset=0, axis1=0, axis2=1)

Return specified diagonals.

Refer to numpy.diagonal for full documentation.

See also

numpy.diagonal
equivalent function
dim_names

List of dimension names

This is a property that is created from the dims attribute and can only be changed through changes in dims.

Examples

>>> import numpy as np
>>> import dimarray as da
>>> dim1 = da.Dim(range(5),'First dimension')
>>> dim2 = da.Dim(['a','b','c'],'Second dimension')
>>> data = da.DimArray(np.random.rand(5,3),[dim1,dim2])
>>> data.dim_names
['First dimension', 'Second dimension']
find(*args, **kwargs)

Returns a tuple of index arrays for the selected conditions.

There are three different ways to specify a filterstring illustrated in the examples.

See also

DimArray.select

Notes

data[data.find(filterstring)] returns the same slice as data.select(filterstring).

Examples

>>> import numpy as np
>>> import dimarray as da
>>> dim1 = da.Dim(range(5),'Dim1')
>>> dim2 = da.Dim(['a','b','c'],'Dim2')
>>> data = da.DimArray(np.random.rand(5,3),[dim1,dim2])
>>> data.find('Dim1>2',"Dim2=='b'")
(array([[3],
       [4]]), array([[1]]))
>>> data.find(Dim1=data2['Dim1']>2,Dim2=data2['Dim2']=='b')
(array([[3],
       [4]]), array([[1]]))
>>> data.find("Dim1>kwargs['a']","Dim2==kwargs['b']",a=2,b='b')
(array([[3],
       [4]]), array([[1]]))
>>> data[data.find('Dim1>2',"Dim2=='b'")] ==             data.select('Dim1>2',"Dim2=='b'")
DimArray([[ True],
       [ True]], dtype=bool)
>>> data[data.find(Dim1=data2['Dim1']>2,Dim2=data2['Dim2']=='b')] ==             data.select(Dim1=data2['Dim1']>2,Dim2=data2['Dim2']=='b')
DimArray([[ True],
       [ True]], dtype=bool)
>>> data[data.find("Dim1>kwargs['a']","Dim2==kwargs['b']",a=2,b='b')]             == data.select("Dim1>kwargs['a']","Dim2==kwargs['b']",a=2,b='b')
DimArray([[ True],
       [ True]], dtype=bool)
flatten(*args, **kwargs)
CAUTION: the output of this method is cast to an AttrArray instance. Some attributes may no longer be valid after this Method is applied!

a.flatten(order=’C’)

Return a copy of the array collapsed into one dimension.
Parameters:

order : {‘C’, ‘F’}, optional

Whether to flatten in C (row-major) or Fortran (column-major) order. The default is ‘C’.

Returns:

y : ndarray

A copy of the input array, flattened to one dimension.

See also

ravel
Return a flattened array.
flat
A 1-D flat iterator over the array.

Examples

>>> a = np.array([[1,2], [3,4]])
>>> a.flatten()
array([1, 2, 3, 4])
>>> a.flatten('F')
array([1, 3, 2, 4])
get_axis(axis)

Get the axis number for a dimension name.

Provides a convenient way to ensure an axis number, because it converts dimension names to axis numbers, but returns non-string input unchanged. Should only be needed in exceptional cases outside a class definition as any function that takes an axis keyword should also accept the corresponding dimension name.

Parameters:

axis : str

The name of a dimension.

Returns:

The axis number corresponding to the dimension name. :

If axis is not a string, it is returned unchanged. :

make_bins(axis, bins, function, bin_labels='function', error_on_nonexact=True, **kwargs)

Return a copy of the data with dimension (specified by axis) binned as specified.

Parameters:

axis : int

The dimension to be binned. Can be name or number.

bins : {int,list,tuple}

Specifies how the data should be binned. Acceptable values are: * the number of bins (equally spaced, if possible, roughly

equally spaced if not and error_on_nonexact is False). (Uses numpy.[array]split.)

  • A 1-D container (list or tuple) of the indices where the data should be split into bins. The value for error_on_nonexact does not influence the result. (Uses numpy.[array]split.)
  • A 2-D container (lists or tuples) where each container in the first dimension specifies the min (inclusive) and the max (exlusive) values and (optionally) a label for each bin. The value for error_on_nonexact must be True. If labels are specified in bins, they are used and the value of bin_labels is ignored.

function : function

The function to aggregate over within the bins. Needs to take the data as the first argument and an additional axis argument (numpy.mean is an example of a valid function).

bins_labels : {‘function’,’sequential’,array_like}, optional

‘function’ applies the function that is used for binning to the dimension, ‘sequential’ numbers the bins sequentially. Alternatively, a 1-D container that contains the bin labels can be specified.

error_on_nonexact : {True, False}, optional

Specifies whether roughly equal bin sizes are acceptable when the data cannot be evenly split in the specified number of bins (this parameter is only applicable when bins is an integer specifying the number of bins). When True, the function numpy.split is used, when False the function numpy.array_split is used.

kwargs : keyword arguments, optional

Optional key word arguments to be passed on to function.

Returns:

binned : DimArray

A new DimArray instance in which one of the dimensions is binned as specified.

Examples

>>> import numpy as np
>>> import dimarray as da
>>> data = da.DimArray(np.random.rand(4,5))
>>> data
DimArray([[ 0.74214411,  0.35124939,  0.52641061,  0.85086401,  0.38799751],
       [ 0.692385  ,  0.14314031,  0.61169269,  0.14904847,  0.65182813],
       [ 0.33258044,  0.07763733,  0.18474865,  0.67977018,  0.30520807],
       [ 0.05501445,  0.09936871,  0.55943639,  0.70683311,  0.10069493]])
>>> data.make_bins('dim1',2,np.mean)
DimArray([[ 0.71726456,  0.24719485,  0.56905165,  0.49995624,  0.51991282],
       [ 0.19379744,  0.08850302,  0.37209252,  0.69330165,  0.2029515 ]])
>>> data.make_bins('dim1',2,np.mean).dims
array([[ 0.5  2.5], [0 1 2 3 4]], dtype=object)
>>> data.make_bins('dim1',2,np.mean)
DimArray([[ 0.71726456,  0.24719485,  0.56905165,  0.49995624,  0.51991282],
       [ 0.19379744,  0.08850302,  0.37209252,  0.69330165,  0.2029515 ]])
>>> data.make_bins('dim2',2,np.mean,error_on_nonexact=False).dims
array([[0 1 2 3], [ 1.   3.5]], dtype=object)
max(axis=None, out=None)
Below is the docstring from numpy.ndarray. For DimArray instances, the axis may be specified as string (dimension name).

a.max(axis=None, out=None)

Return the maximum along a given axis.

Refer to numpy.amax for full documentation.

See also

numpy.amax
equivalent function
mean(axis=None, dtype=None, out=None)
Below is the docstring from numpy.ndarray. For DimArray instances, the axis may be specified as string (dimension name).

a.mean(axis=None, dtype=None, out=None)

Returns the average of the array elements along given axis.

Refer to numpy.mean for full documentation.

See also

numpy.mean
equivalent function
min(axis=None, out=None)
Below is the docstring from numpy.ndarray. For DimArray instances, the axis may be specified as string (dimension name).

a.min(axis=None, out=None)

Return the minimum along a given axis.

Refer to numpy.amin for full documentation.

See also

numpy.amin
equivalent function
nanmean(axis=None)

Below is the docstring from AttrArray. For DimArray instances, the axis may be specified as string (dimension name).

Compute the arithmetic mean along the specified axis ignoring nans.

Returns the average of the array elements treating nans as missing values. The average is taken over the flattened array by default, otherwise over the specified axis. float64 intermediate and return values are used for integer inputs.

Parameters:

a : array_like

Calculate the mean of these values.

axis : int, optional

Axis along which the means are computed. The default is to compute the mean of the flattened array.

Returns:

mean : {ndarray, scalar}

Return a new array containing the mean values (or scalar if axis is None).

See also

nanvar
Variance ignoring nans
nanstd
Standard deviation ignoring nans.
numpy.average
Weighted average.

Notes

The arithmetic mean is the sum of the elements along the axis divided by the number of elements. Nans are ignored.

This docstring is based on that for numpy.mean, the code is based on scipy.stats.nanmean.

Examples

>>> a = AttrArray([[1, 2], [3, 4], [5, 6]])
>>> a.nanmean()
3.5
>>> a = AttrArray([[np.nan, 2], [3, 4], [5, 6]])
>>> a.nanmean()
4.0
>>> a.nanmean(0)
AttrArray([ 4.0,  4.0)
>>> a.nanmean(1)
AttrArray([ 2.0,  3.5,  5.5])
nanstd(axis=None, ddof=0)

Below is the docstring from AttrArray. For DimArray instances, the axis may be specified as string (dimension name).

Compute the standard deviation along the specified axis ignoring nans.

Returns the standard deviation, a measure of the spread of a distribution, of the array elements, treating nans as missing values. The standard deviation is computed for the flattened array by default, otherwise over the specified axis.

Parameters:

a : array_like

Calculate the standard deviation of these values.

axis : int, optional

Axis along which the standard deviation is computed. The default is to compute the standard deviation of the flattened array.

ddof : int, optional

Means Delta Degrees of Freedom. The divisor used in calculations is N - ddof, where N represents the number of elements. By default ddof is zero (biased estimate).

Returns:

standard_deviation : {ndarray, scalar}

Return a new array containing the standard deviations (or scalar if axis is None). The dtype is always at least np.float64.

See also

nanvar
Variance ignoring nans
numpy.std
Standard deviation
numpy.var
Variance
numpy.mean
Average

Notes

If no nan values are present, returns the same value as numpy.std, otherwise, the standard deviation is calculated as if the nan values were not present.

The standard deviation is the square root of the average of the squared deviations from the mean, i.e., var = sqrt(mean(abs(x - x.mean())**2)).

The mean is normally calculated as x.sum() / N, where N = len(x). If, however, ddof is specified, the divisor N - ddof is used instead.

Note that, for complex numbers, std takes the absolute value before squaring, so that the result is always real and nonnegative.

This docstring is based on that for numpy.std, the code is based on scipy.stats.nanstd.

Examples

>>> a = AttrArray([[1, 2], [3, 4], [5, 6]])
>>> a.nanstd()
1.707825127659933
>>> a = AttrArray([[np.nan, 2], [3, 4], [5, 6]])
>>> a.nanstd()
1.4142135623730951
>>> a.nanstd(0)
AttrArray([ 1.,  1.6329931618554521])
>>> a.nanstd(1)
AttrArray([ 0.0,  0.5,  0.5])
nanvar(axis=None, ddof=0)

Below is the docstring from AttrArray. For DimArray instances, the axis may be specified as string (dimension name).

Compute the variance along the specified axis ignoring nans.

Returns the variance of the array elements, a measure of the spread of a distribution, treating nans as missing values. The variance is computed for the flattened array by default, otherwise over the specified axis.

Parameters:

a : array_like

Calculate the variance of these values.

axis : int, optional

Axis along which the variance is computed. The default is to compute the variance of the flattened array.

ddof : int, optional

Means Delta Degrees of Freedom. The divisor used in calculations is N - ddof, where N represents the number of elements. By default ddof is zero (biased estimate).

Returns:

variance : {ndarray, scalar}

Return a new array containing the variances (or scalar if axis is None). The dtype of the output is always at least np.float64.

See also

nanstd
Standard deviation ignoring nans.
numpy.std
Standard deviation
numpy.var
Variance
numpy.mean
Average

Notes

If no nan values are present, returns the same value as numpy.var, otherwise, the variance is calculated as if the nan values were not present.

The variance is the average of the squared deviations from the mean, i.e., var = mean(abs(x - x.mean())**2). The mean is normally calculated as x.sum() / N, where N = len(x). If, however, ddof is specified, the divisor N - ddof is used instead.

Note that, for complex numbers, var takes the absolute value before squaring, so that the result is always real and nonnegative.

This docstring is based on that for numpy.var, the code is based on scipy.stats.nanstd.

Examples

>>> a = AttrArray([[1, 2], [3, 4], [5, 6]])
>>> a.nanvar()
2.9166666666666665
>>> a = AttrArray([[np.nan, 2], [3, 4], [5, 6]])
>>> a.nanvar()
2.0
>>> a.nanvar(0)
AttrArray([ 1.,  2.6666666666666665)
>>> a.nanvar(1)
AttrArray([ 0.0,  0.25,  0.25])
nonzero()

Return the indices of the elements that are non-zero.

Refer to numpy.nonzero for full documentation.

See also

numpy.nonzero
equivalent function
prod(axis=None, dtype=None, out=None)
Below is the docstring from numpy.ndarray. For DimArray instances, the axis may be specified as string (dimension name).

a.prod(axis=None, dtype=None, out=None)

Return the product of the array elements over the given axis

Refer to numpy.prod for full documentation.

See also

numpy.prod
equivalent function
ptp(axis=None, out=None)
Below is the docstring from numpy.ndarray. For DimArray instances, the axis may be specified as string (dimension name).

a.ptp(axis=None, out=None)

Peak to peak (maximum - minimum) value along a given axis.

Refer to numpy.ptp for full documentation.

See also

numpy.ptp
equivalent function
ravel(*args, **kwargs)
CAUTION: the output of this method is cast to an AttrArray instance. Some attributes may no longer be valid after this Method is applied!

a.ravel([order])

Return a flattened array.

Refer to numpy.ravel for full documentation.

See also

numpy.ravel
equivalent function
ndarray.flat
a flat iterator on the array.
repeat(repeats, axis=None)
CAUTION: the output of this method is cast to an AttrArray instance. Some attributes may no longer be valid after this Method is applied!

a.repeat(repeats, axis=None)

Repeat elements of an array.

Refer to numpy.repeat for full documentation.

See also

numpy.repeat
equivalent function
reshape(shape, order='C')
CAUTION: the output of this method is cast to an AttrArray instance. Some attributes may no longer be valid after this Method is applied!

a.reshape(shape, order=’C’)

Returns an array containing the same data with a new shape.

Refer to numpy.reshape for full documentation.

See also

numpy.reshape
equivalent function
resize(*args, **kwargs)
Resizing is not possible for dimensioned arrays. Calling this method will throw a NotImplementedError exception. If resizing is desired the array needs to be converted to a different data type (e.g., numpy.ndarray), first!
select(*args, **kwargs)

Returns a slice of the data filtered with the select conditions.

There are three different ways to specify a filterstring illustrated in the examples.

See also

DimArray.find

Notes

data.select(filterstring) returns the same slice as data[data.find(filterstring)].

Examples

>>> import numpy as np
>>> import dimarray as da
>>> dim1 = da.Dim(range(5),'Dim1')
>>> dim2 = da.Dim(['a','b','c'],'Dim2')
>>> data = da.DimArray(np.random.rand(5,3),[dim1,dim2])
>>> data
DimArray([[ 0.52303181,  0.27313638,  0.28760072],
       [ 0.24885995,  0.40998977,  0.61080984],
       [ 0.43630142,  0.06662251,  0.61589201],
       [ 0.19332778,  0.27255998,  0.67924734],
       [ 0.57262178,  0.60912633,  0.80938473]])
>>> data.select('Dim1>2',"Dim2=='b'")
DimArray([[ 0.27255998],
       [ 0.60912633]])
>>> data.select(Dim1=data2['Dim1']>2,Dim2=data2['Dim2']=='b')
DimArray([[ 0.27255998],
       [ 0.60912633]])
>>> data.select("Dim1>kwargs['a']","Dim2==kwargs['b']",a=2,b='b')
DimArray([[ 0.27255998],
       [ 0.60912633]])
>>> data.select('Dim1>2',"Dim2=='b'") ==             data[data.find('Dim1>2',"Dim2=='b'")]
DimArray([[ True],
       [ True]], dtype=bool)
>>> data.select(Dim1=data2['Dim1']>2,Dim2=data2['Dim2']=='b') ==             data[data.find(Dim1=data2['Dim1']>2,Dim2=data2['Dim2']=='b')]
DimArray([[ True],
       [ True]], dtype=bool)
>>> data.select("Dim1>kwargs['a']","Dim2==kwargs['b']",a=2,b='b') ==             data[data.find("Dim1>kwargs['a']","Dim2==kwargs['b']",a=2,b='b')]
DimArray([[ True],
       [ True]], dtype=bool)
sort(axis=-1, kind='quicksort', order=None)
Below is the docstring from numpy.ndarray. For DimArray instances, the axis may be specified as string (dimension name).

a.sort(axis=-1, kind=’quicksort’, order=None)

Sort an array, in-place.
Parameters:

axis : int, optional

Axis along which to sort. Default is -1, which means sort along the last axis.

kind : {‘quicksort’, ‘mergesort’, ‘heapsort’}, optional

Sorting algorithm. Default is ‘quicksort’.

order : list, optional

When a is an array with fields defined, this argument specifies which fields to compare first, second, etc. Not all fields need be specified.

See also

numpy.sort
Return a sorted copy of an array.
argsort
Indirect sort.
lexsort
Indirect stable sort on multiple keys.
searchsorted
Find elements in sorted array.

Notes

See sort for notes on the different sorting algorithms.

Examples

>>> a = np.array([[1,4], [3,1]])
>>> a.sort(axis=1)
>>> a
array([[1, 4],
       [1, 3]])
>>> a.sort(axis=0)
>>> a
array([[1, 3],
       [1, 4]])

Use the order keyword to specify a field to use when sorting a structured array:

>>> a = np.array([('a', 2), ('c', 1)], dtype=[('x', 'S1'), ('y', int)])
>>> a.sort(order='y')
>>> a
array([('c', 1), ('a', 2)],
      dtype=[('x', '|S1'), ('y', '<i4')])
squeeze()

Remove single-dimensional entries from the shape of a.

Refer to numpy.squeeze for full documentation.

See also

numpy.squeeze
equivalent function
std(axis=None, dtype=None, out=None, ddof=0)
Below is the docstring from numpy.ndarray. For DimArray instances, the axis may be specified as string (dimension name).

a.std(axis=None, dtype=None, out=None, ddof=0)

Returns the standard deviation of the array elements along given axis.

Refer to numpy.std for full documentation.

See also

numpy.std
equivalent function
sum(axis=None, dtype=None, out=None)
Below is the docstring from numpy.ndarray. For DimArray instances, the axis may be specified as string (dimension name).

a.sum(axis=None, dtype=None, out=None)

Return the sum of the array elements over the given axis.

Refer to numpy.sum for full documentation.

See also

numpy.sum
equivalent function
swapaxes(axis1, axis2)
Below is the docstring from numpy.ndarray. The axes may be specified as strings (dimension names).

a.swapaxes(axis1, axis2)

Return a view of the array with axis1 and axis2 interchanged.

Refer to numpy.swapaxes for full documentation.

See also

numpy.swapaxes
equivalent function
take(indices, axis=None, out=None, mode='raise')
Below is the docstring from numpy.ndarray. For DimArray instances, the axis may be specified as string (dimension name).

a.take(indices, axis=None, out=None, mode=’raise’)

Return an array formed from the elements of a at the given indices.

Refer to numpy.take for full documentation.

See also

numpy.take
equivalent function
trace(*args, **kwargs)
CAUTION: the output of this method is cast to an AttrArray instance. Some attributes may no longer be valid after this Method is applied!

a.trace(offset=0, axis1=0, axis2=1, dtype=None, out=None)

Return the sum along diagonals of the array.

Refer to numpy.trace for full documentation.

See also

numpy.trace
equivalent function
transpose(*axes)
Below is the docstring from numpy.ndarray. The axes may be specified as strings (dimension names).

a.transpose(*axes)

Returns a view of ‘a’ with axes transposed. If no axes are given, or None is passed, switches the order of the axes. For a 2-d array, this is the usual matrix transpose. If axes are given, they describe how the axes are permuted.

See also

ndarray.T
array property returning the array transposed

Examples

>>> a = np.array([[1,2],[3,4]])
>>> a
array([[1, 2],
       [3, 4]])
>>> a.transpose()
array([[1, 3],
       [2, 4]])
>>> a.transpose((1,0))
array([[1, 3],
       [2, 4]])
>>> a.transpose(1,0)
array([[1, 3],
       [2, 4]])
var(axis=None, dtype=None, out=None, ddof=0)
Below is the docstring from numpy.ndarray. For DimArray instances, the axis may be specified as string (dimension name).

a.var(axis=None, dtype=None, out=None, ddof=0)

Returns the variance of the array elements, along given axis.

Refer to numpy.var for full documentation.

See also

numpy.var
equivalent function