landlab.utils.jaggedarray_ma

Store arrays of variable-length arrays implemented with masked arrays.

Implements a MaskedJaggedArray class using numpy masked arrays.

Examples

Create a MaskedJaggedArray that stores link IDs for the links attached to the nodes of a 3x3 grid.

>>> from landlab.utils.jaggedarray_ma import MaskedJaggedArray
>>> links_at_node = MaskedJaggedArray(
...     [
...         [0, 6],
...         [1, 7, 0],
...         [8, 1],
...         [2, 9, 6],
...         [3, 10, 2, 7],
...         [11, 3, 8],
...         [4, 7],
...         [5, 10, 4],
...         [5, 11],
...     ]
... )

Make up some data that provides values at each of the links.

>>> value_at_link = np.arange(12, dtype=float)

Create another MaskedJaggedArray. Here we store the values at each of the links attached to nodes of the grid.

>>> values_at_node = MaskedJaggedArray.empty_like(links_at_node, dtype=float)
>>> values_at_node.array = value_at_link[links_at_node.array]

Now operate on the link values for each node.

>>> values_at_node.foreach_row(np.sum)
array([ 6.,   8.,   9.,  17.,  22.,  22.,  11.,  19.,  16.])
>>> values_at_node.foreach_row(np.min)
array([0.,  0.,  1.,  2.,  2.,  3.,  4.,  4.,  5.])
>>> values_at_node.foreach_row(np.max)
array([ 6.,  7.,  8.,  9.,  10.,  11.,  7.,  10.,  11.])

Note: np.ptp doesn’t work on masked arrays but since max and min seem to, you can create a new ptp that returns the expected values.

>>> ptp = lambda x, axis=None: np.max(x, axis=axis) - np.min(x, axis=axis)
>>> values_at_node.foreach_row(ptp)
array([6.,  7.,  7.,  7.,  8.,  8.,  3.,  6.,  6.])

Or access the underlying masked array directly,

>>> values_at_node.masked_array.ptp(axis=1).data
array([6.,  7.,  7.,  7.,  8.,  8.,  3.,  6.,  6.])
class MaskedJaggedArray[source]

Bases: object

A container for an array of variable-length arrays.

MaskedJaggedArray([row0, row1, …]) MaskedJaggedArray(values, values_per_row)

Examples

Create a MaskedJaggedArray with an array of arrays.

>>> from landlab.utils.jaggedarray_ma import MaskedJaggedArray
>>> x = MaskedJaggedArray([[0, 1, 2], [3, 4]])
>>> x.array
array([0, 1, 2, 3, 4])

Create a MaskedJaggedArray as a 1D array and a list or row lengths.

>>> x = MaskedJaggedArray([0, 1, 2, 3, 4], (3, 2))
>>> x.array
array([0, 1, 2, 3, 4])

MaskedJaggedArray([row0, row1, …]) MaskedJaggedArray(values, values_per_row)

Examples

Create a MaskedJaggedArray with an array of arrays.

>>> from landlab.utils.jaggedarray_ma import MaskedJaggedArray
>>> x = MaskedJaggedArray([[0, 1, 2], [3, 4]])
>>> x.array
array([0, 1, 2, 3, 4])

Create a MaskedJaggedArray as a 1D array and a list or row lengths.

>>> x = MaskedJaggedArray([0, 1, 2, 3, 4], (3, 2))
>>> x.array
array([0, 1, 2, 3, 4])
__init__(*args)[source]

MaskedJaggedArray([row0, row1, …]) MaskedJaggedArray(values, values_per_row)

Examples

Create a MaskedJaggedArray with an array of arrays.

>>> from landlab.utils.jaggedarray_ma import MaskedJaggedArray
>>> x = MaskedJaggedArray([[0, 1, 2], [3, 4]])
>>> x.array
array([0, 1, 2, 3, 4])

Create a MaskedJaggedArray as a 1D array and a list or row lengths.

>>> x = MaskedJaggedArray([0, 1, 2, 3, 4], (3, 2))
>>> x.array
array([0, 1, 2, 3, 4])
__iter__()[source]

Iterate over the rows of the array.

Examples

>>> from landlab.utils.jaggedarray_ma import MaskedJaggedArray
>>> x = MaskedJaggedArray([[0, 1, 2], [3, 4]])
>>> for row in x:
...     row
...
array([0, 1, 2])
array([3, 4])
__new__(**kwargs)
property array

The jagged array as a 1D array.

Returns:

A view of the underlying 1D array.

Return type:

array

Examples

>>> from landlab.utils.jaggedarray_ma import MaskedJaggedArray
>>> x = MaskedJaggedArray([[0, 1, 2], [3, 4]])
>>> x.array
array([0, 1, 2, 3, 4])
>>> x.array = np.array([1, 1, 2, 3, 4])
>>> x.array
array([1, 1, 2, 3, 4])
static empty_like(jagged, dtype=None)[source]

Create a new MaskedJaggedArray that is like another one.

Parameters:
  • jagged (MaskedJaggedArray) – A MaskedJaggedArray to copy.

  • dtype (np.dtype) – The data type of the new MaskedJaggedArray.

Returns:

A new MaskedJaggedArray.

Return type:

MaskedJaggedArray

foreach_row(func, out=None)[source]

Apply an operator row-by-row.

Examples

>>> from landlab.utils.jaggedarray_ma import MaskedJaggedArray
>>> x = MaskedJaggedArray([[0, 1, 2], [3, 4]])
>>> x.foreach_row(np.sum)
array([3, 7])
>>> out = np.empty(2, dtype=int)
>>> x.foreach_row(np.sum, out=out) is out
True
>>> out
array([3, 7])
length_of_row(row)[source]

Number of values in a given row.

Parameters:

row (int) – Index to a row.

Returns:

Number of values in the row.

Return type:

int

Examples

>>> from landlab.utils.jaggedarray_ma import MaskedJaggedArray
>>> x = MaskedJaggedArray([[0, 1, 2], [3, 4]])
>>> x.length_of_row(0)
3
>>> x.length_of_row(1)
2
static ma_from_flat_array(array, values_per_row)[source]

Create a masked array from a flat array.

Parameters:
  • array (array_like) – Values of the jagged array.

  • values_per_row (array_like of int) – Number of values in each row of the jagged array.

Returns:

A new masked array.

Return type:

np.masked_array

static ma_from_list_of_lists(rows, dtype=None)[source]

Create a masked array from a list of lists.

Parameters:
  • rows (array_like or array_like) – Rows of the jagged array.

  • dtype (np.dtype, optional) – The data type of the new masked array.

Returns:

A new masked array.

Return type:

np.masked_array

property masked_array

The jagged array as a masked array.

Returns:

The underlying masked array.

Return type:

np.masked_array

property number_of_rows

Number of array rows.

Returns:

Number of rows in the array.

Return type:

int

Examples

>>> from landlab.utils.jaggedarray_ma import MaskedJaggedArray
>>> x = MaskedJaggedArray([[0, 1, 2], [3, 4]])
>>> x.number_of_rows == 2
True
row(row)[source]

Get the values of a row as an array.

Parameters:

row (int) – Index to a row.

Returns:

Values in the row as a slice of the underlying array.

Return type:

array

Examples

>>> from landlab.utils.jaggedarray_ma import MaskedJaggedArray
>>> x = MaskedJaggedArray([[0, 1, 2], [3, 4]])
>>> x.row(0)
array([0, 1, 2])
>>> x.row(1)
array([3, 4])
property size

Number of array elements.

Returns:

Number of values in the array.

Return type:

int

Examples

>>> from landlab.utils.jaggedarray_ma import MaskedJaggedArray
>>> x = MaskedJaggedArray([[0, 1, 2], [3, 4]])
>>> x.size
5