landlab.data_record.aggregators

aggregate_items_as_count(ids, size=None)[source]

Count the number of time an id appears in an array.

Parameters:
  • ids (array_like of int) – An array of ids.

  • size (int, optional) – The size of the output array. This is useful if the ids array doesn’t contain all possible ids.

Returns:

The number of times each id appears.

Return type:

ndarray of int

Examples

>>> from landlab.data_record.aggregators import aggregate_items_as_count
>>> aggregate_items_as_count([1, 2, 3, 3, 1, 5])
array([0, 2, 1, 2, 0, 1])
>>> aggregate_items_as_count([1, 2, 3, 3, 1, 5], size=8)
array([0, 2, 1, 2, 0, 1, 0, 0])

Negative ids are ignored.

>>> aggregate_items_as_count([1, 2, 3, 3, -1, 5])
array([0, 1, 1, 2, 0, 1])
aggregate_items_as_gmean(ids, values, *, weights=None, where=None, out=None)[source]

Compute a weighted geometric mean of values grouped by integer IDs.

Group values by ids and compute the weighted geometric mean for each group.

Parameters:
  • ids (array_like of int, shape (n,)) – Integer group labels for each value. Negative IDs are ignored.

  • values (array_like, shape (n,)) – Values to aggregate. Must be strictly positive.

  • weights (array_like, shape (n,), optional) – Weights associated with each value. If not provided, all weights are taken to be 1.

  • where (array_like of bool, shape (n,), optional) – Boolean mask indicating which items to include. If not provided, all items are included.

  • out (ndarray of float, shape (n_groups,), optional) – Output array. If provided, results are written in-place.

Returns:

out – Weighted geometric mean for each group. Elements with no valid items are left unchanged in out if provided, or contain uninitialized values if out was created internally.

Return type:

ndarray of float, shape (n_groups,)

Examples

>>> import numpy as np
>>> ids = [0, 0, 1, 1, 1]
>>> values = [1.0, 4.0, 1.0, 3.0, 9.0]
>>> weights = [1.0, 1.0, 1.0, 2.0, 1.0]
>>> aggregate_items_as_gmean(ids, values, weights=weights)
array([2., 3.])

Use where to filter items:

>>> where = [True, True, False, False, True]
>>> aggregate_items_as_gmean(ids, values, where=where)
array([2., 9.])

Reuse an output array:

>>> out = np.full(2, np.nan)
>>> where = [True, True, False, False, False]
>>> aggregate_items_as_gmean(ids, values, out=out, where=where)
array([ 2., nan])
>>> out
array([ 2., nan])
aggregate_items_as_mean(ids, values, weights=None, size=None)[source]

Find the mean of values associated with an id.

Parameters:
  • ids (array_like of int) – An array of ids.

  • values (array_like) – The value associated with the corresponding id in the id array.

  • size (int, optional) – The size of the output array. This is useful if the ids array doesn’t contain all possible ids.

  • weights (ArrayLike | None)

Returns:

The mean of the values at each id.

Return type:

ndarray of int

Examples

>>> from landlab.data_record.aggregators import aggregate_items_as_mean
>>> aggregate_items_as_mean([0, 0, 1, 3, 4, 5], [1, 2, 3, 3, 1, 5])
array([1.5, 3. , 0. , 3. , 1. , 5. ])
>>> aggregate_items_as_mean([0, 0, 1, 3, 4, 5], [1, 2, 3, 3, 1, 5], size=8)
array([1.5, 3. , 0. , 3. , 1. , 5. , 0. , 0. ])

Negative ids are ignored.

>>> aggregate_items_as_mean([0, -1, 1, 3, 4, 5], [1, 2, 3, 3, 1, 5])
array([1., 3., 0., 3., 1., 5.])
aggregate_items_as_sum(ids, values, size=None)[source]

Find the sum of values associated with an id.

Parameters:
  • ids (array_like of int) – An array of ids.

  • values (array_like) – The value associated with the corresponding id in the id array.

  • size (int, optional) – The size of the output array. This is useful if the ids array doesn’t contain all possible ids.

Returns:

The sum of the values at each id.

Return type:

ndarray of int

Examples

>>> from landlab.data_record.aggregators import aggregate_items_as_sum
>>> aggregate_items_as_sum([0, 0, 1, 3, 4, 5], [1, 2, 3, 3, 1, 5])
array([3., 3., 0., 3., 1., 5.])
>>> aggregate_items_as_sum([0, 0, 1, 3, 4, 5], [1, 2, 3, 3, 1, 5], size=8)
array([3., 3., 0., 3., 1., 5., 0., 0.])

Negative ids are ignored.

>>> aggregate_items_as_sum([0, -1, 1, 3, 4, 5], [1, 2, 3, 3, 1, 5])
array([1., 3., 0., 3., 1., 5.])