pyconform.physarray

Physical Array Class

This module contains the PhysArray class, which is the primary object passed along edges of a Data Flow graph. It is a subclass of the Numpy MaskedArray, and carries with its data the units associated with the data, the dimensions associated with each axis of the data.

Copyright 2017-2020, University Corporation for Atmospheric Research LICENSE: See the LICENSE.rst file for details

class pyconform.physarray.CharArray(indata, name=None, dimensions=None)[source]

Bases: pyconform.physarray.PhysArray

Special kind of PhysArray to deal with string arrays

convert(units)[source]

Return a new PhysArray with new units

Parameters

units (Unit) – The new units to which to convert the PhysArray

invert()[source]

Return a new PhysArray with the value of the array inverted (1/value)

property positive

Positive direction (up or down) for the data

stretch(newlen)[source]
transpose(*dims)[source]

Return a new PhysArray with dimensions transposed in the order given

Does nothing if no transpose is necesary

Parameters

dims (tuple) – Tuple of dimension names in the new order

property units

Units of the data

exception pyconform.physarray.DimensionsError[source]

Bases: ValueError

Exception indicating an error involving dimensions of a PhysArray object

class pyconform.physarray.PhysArray(indata, name=None, units=None, dimensions=None, positive='', **kwds)[source]

Bases: numpy.ma.core.MaskedArray

A PhysArray is an array of data with both units and dimensions

The PhysArray is deried from the Numpy MaskedArray and is the basic object along the edges of a Data Flow graph.

convert(units)[source]

Return a new PhysArray with new units

Parameters

units (Unit) – The new units to which to convert the PhysArray

property dimensions

Named dimensions of the data

down()[source]

Set the direction of the positive attribute to ‘down’ and multiply by -1, if necessary

Only multiplies by -1 if the positive attribute is already set to ‘up’.

flip()[source]

Flip the direction of the positive attribute, if set, and correspondingly multiply by -1

Does nothing if the positive attribute is not set (i.e., equals None)

invert()[source]

Return a new PhysArray with the value of the array inverted (1/value)

mean(dimensions=None, **kwds)[source]

Returns the average of the array elements along given axis.

Masked entries are ignored, and result elements which are not finite will be masked.

Refer to numpy.mean for full documentation.

See also

numpy.ndarray.mean()

corresponding function for ndarrays

numpy.mean()

Equivalent function

numpy.ma.average()

Weighted average.

Examples

>>> a = np.ma.array([1,2,3], mask=[False, False, True])
>>> a
masked_array(data=[1, 2, --],
             mask=[False, False,  True],
       fill_value=999999)
>>> a.mean()
1.5
property name

String name for the data

property positive

Positive direction (up or down) for the data

sum(dimensions=None, **kwds)[source]

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

Masked elements are set to 0 internally.

Refer to numpy.sum for full documentation.

See also

numpy.ndarray.sum()

corresponding function for ndarrays

numpy.sum()

equivalent function

Examples

>>> x = np.ma.array([[1,2,3],[4,5,6],[7,8,9]], mask=[0] + [1,0]*4)
>>> x
masked_array(
  data=[[1, --, 3],
        [--, 5, --],
        [7, --, 9]],
  mask=[[False,  True, False],
        [ True, False,  True],
        [False,  True, False]],
  fill_value=999999)
>>> x.sum()
25
>>> x.sum(axis=1)
masked_array(data=[4, 5, 16],
             mask=[False, False, False],
       fill_value=999999)
>>> x.sum(axis=0)
masked_array(data=[8, 5, 12],
             mask=[False, False, False],
       fill_value=999999)
>>> print(type(x.sum(axis=0, dtype=np.int64)[0]))
<class 'numpy.int64'>
transpose(*dims)[source]

Return a new PhysArray with dimensions transposed in the order given

Does nothing if no transpose is necesary

Parameters

dims (tuple) – Tuple of dimension names in the new order

property units

Units of the data

up()[source]

Set the direction of the positive attribute to ‘up’ and multiply by -1, if necessary

Only multiplies by -1 if the positive attribute is already set to ‘down’.

exception pyconform.physarray.UnitsError[source]

Bases: ValueError

Exception indicating an error involving units of a PhysArray object

pyconform.physarray.getdata(obj)[source]

Retrieve the ndarray data associated with an object

pyconform.physarray.getdimensions(obj)[source]

Retrieve the dimensions-tuple associated with the object

pyconform.physarray.getdtype(obj)[source]

Get the dtype associated with an object

pyconform.physarray.getmask(obj)[source]

Retrieve the mask associated with an object

pyconform.physarray.getname(obj)[source]

Retrieve the string name associated with an object

pyconform.physarray.getpositive(obj)[source]

Retrieve the positive attribute associated with the object

pyconform.physarray.getshape(obj)[source]

Get the shape associated with an object

pyconform.physarray.getunits(obj)[source]

Retrieve the Unit associated with the object

pyconform.physarray.ischartype(obj)[source]

Return whether the object is a string/character type