Base.transformElements

Base.transformElements(toTransform, points=None, features=None, preserveZeros=False, skipNoneReturnValues=False, *, useLog=None)

Modify each element using a function or mapping.

Perform an inplace modification of the elements or subset of elements in this object.

Parameters:
  • toTransform (function, dict) –

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

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

  • points (identifier, list of identifiers) – May be a single point name or index, an iterable, container of point names and/or indices. None indicates application to all points.

  • features (identifier, list of identifiers) – May be a single feature name or index, an iterable, container of feature names and/or indices. None indicates application to all features.

  • preserveZeros (bool) – If True it does not apply toTransform to elements in the data that are 0, and that 0 is not modified.

  • skipNoneReturnValues (bool) – If True, any time toTransform() returns None, the value originally in the data will remain unmodified.

  • 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.

Examples

Simple transformation to all elements.

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

Transform while preserving zero values.

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

Transforming a subset of points and features.

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

Transforming 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]]
>>> dontSkip = nimble.data(lst)
>>> dontSkip.transformElements(addTenToEvens)
>>> dontSkip
<Matrix 3pt x 3ft
       0       1       2
   ┌───────────────────────
 0 │         12.000
 1 │ 14.000          16.000
 2 │         18.000
>
>>> skip = nimble.data(lst)
>>> skip.transformElements(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, change, modify, replace, transformation, recalculate, alter, applymap