Base.calculateOnElements

Base.calculateOnElements(toCalculate, points=None, features=None, preserveZeros=False, skipNoneReturnValues=False, outputType=None, *, useLog=None)

Apply a calculation to each element.

Return a new object with a function or mapping applied to each element in this object or subset of points and features in this object.

Parameters:
  • toCalculate (function, dict) –

    • function - in the form of toCalculate(elementValue) or toCalculate(elementValue, pointIndex, featureIndex)

    • dictionary - map the current element [key] to the transformed element [value].

  • points (point, list of points) – The subset of points to limit the calculation to. If None, the calculation will apply to all points.

  • features (feature, list of features) – The subset of features to limit the calculation to. If None, the calculation will apply to all features.

  • preserveZeros (bool) – Bypass calculation on zero values

  • skipNoneReturnValues (bool) – Bypass values when toCalculate returns None. If False, the value None will replace the value if None is returned.

  • outputType (nimble data type) – Return an object of the specified type. If None, the returned object will have the same type as the calling object.

  • useLog (bool, None) – Local control for whether to send object creation to the logger. If None (default), use the value as specified in the “logger” “enabledByDefault” configuration option. If True, send to the logger regardless of the global option. If False, do NOT send to the logger, regardless of the global option.

Returns:

nimble Base object

Examples

Simple calculation on all elements.

>>> X = nimble.ones(3, 3)
>>> twos = X.calculateOnElements(lambda elem: elem + 1)
>>> twos
<Matrix 3pt x 3ft
     0  1  2
   ┌────────
 0 │ 2  2  2
 1 │ 2  2  2
 2 │ 2  2  2
>

Calculate while preserving zero values.

>>> X = nimble.identity(3, returnType="Sparse")
>>> addTen = X.calculateOnElements(lambda x: x + 10,
...                                preserveZeros=True)
>>> addTen
<Sparse 3pt x 3ft
     0   1   2
   ┌───────────
 0 │ 11   0   0
 1 │  0  11   0
 2 │  0   0  11
>

Calculate on a subset of points and features.

>>> X = nimble.ones(4, 4, returnType="List")
>>> calc = X.calculateOnElements(lambda elem: elem + 1,
...                              points=[0, 1],
...                              features=[0, 2])
>>> calc
<List 2pt x 2ft
     0  1
   ┌─────
 0 │ 2  2
 1 │ 2  2
>

Calculating with None return values. With the addTenToEvens function defined below, An even values will be return a value, while an odd value will return None. If skipNoneReturnValues is False, the odd values will be replaced with None (or nan depending on the object type) if set to True the odd values will remain as is. Both cases are presented.

>>> def addTenToEvens(elem):
...     if elem % 2 == 0:
...         return elem + 10
...     return None
>>> lst = [[1, 2, 3],
...        [4, 5, 6],
...        [7, 8, 9]]
>>> X = nimble.data(lst)
>>> dontSkip = X.calculateOnElements(addTenToEvens)
>>> dontSkip
<Matrix 3pt x 3ft
     0   1   2
   ┌───────────
 0 │     12
 1 │ 14      16
 2 │     18
>
>>> skip = X.calculateOnElements(addTenToEvens,
...                              skipNoneReturnValues=True)
>>> skip
<Matrix 3pt x 3ft
     0   1   2
   ┌───────────
 0 │  1  12   3
 1 │ 14   5  16
 2 │  7  18   9
>

Keywords: apply, map, mapping, compute, measure, statistics, stats, applymap