Choosing and reshaping channels¶
Select, Discard and FuncTransformer keep, drop and change the channels of a sample; their code is in core/infrastructure/compose. The overview of the section is on Streams.
3. Choosing and reshaping channels¶
A sample with a joint position, a torque and a temperature; the model needs only the first two, with the torque scaled.
from dense_armor.roles import Signal
from dense_armor.core.infrastructure.compose import FuncTransformer, Select
sig = Signal(values=[0.5, 2.0, 9.0], names=["q0", "tau0", "temp"], units=["rad", "N m", "C"], t=1.0)
pipe = Select(keys=("q0", "tau0")) | FuncTransformer(lambda v: v * 2.0, keys=("tau0",))
out = pipe.transform_one(sig)
print({k: float(v) for k, v in out.items()}, out.t, out.units)
Select keeps the named channels (Discard drops them), FuncTransformer applies a
function to some channels; joined with | they form a pipeline like any other in the library.
The timestamp and the units of the kept channels travel with the sample.
API reference¶
compose ¶
Compose: small transformers to select, drop and reshape channels.
FuncTransformer ¶
Bases: Transformer
Apply fn to channels, or to the whole dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
Callable
|
a callable taking one value (when |
required |
keys
|
tuple[str, ...] | None
|
names of the channels to transform. |
None
|
whole
|
bool
|
if True, |
False
|
Examples:
>>> from dense_armor.core.infrastructure.compose import FuncTransformer
>>> f = FuncTransformer(
... lambda d: {"y": sum(d.values())}, whole=True
... )
>>> f.transform_one({"a": 1.0, "b": 2.0})
{'y': 3.0}
Source code in dense_armor/core/infrastructure/compose/func.py
learn_one ¶
transform_one ¶
Apply the function and return the new sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
a :class: |
required |
t
|
float | None
|
unused. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
A new :class: |
Source code in dense_armor/core/infrastructure/compose/func.py
Discard ¶
Bases: Transformer
Drop the named channels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
tuple[str, ...]
|
names of the channels to drop. Keys not present in the input are silently ignored. |
()
|
Examples:
>>> from dense_armor.roles import Signal
>>> from dense_armor.core.infrastructure.compose import Discard
>>> s = Signal(values=[1.0, 2.0], names=["a", "b"],
... units=["", ""], t=0.0)
>>> sorted(Discard(keys=("a",)).transform_one(s))
['b']
Source code in dense_armor/core/infrastructure/compose/select.py
learn_one ¶
transform_one ¶
Return a copy without the dropped keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
a :class: |
required |
t
|
float | None
|
unused. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
A new :class: |
Source code in dense_armor/core/infrastructure/compose/select.py
Select ¶
Bases: Transformer
Keep only the named channels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
tuple[str, ...]
|
names of the channels to keep, in the order given. Keys not present in the input are silently ignored. |
()
|
Examples:
>>> from dense_armor.roles import Signal
>>> from dense_armor.core.infrastructure.compose import Select
>>> s = Signal(values=[1.0, 2.0], names=["a", "b"],
... units=["", ""], t=0.0)
>>> sorted(Select(keys=("b",)).transform_one(s))
['b']
Source code in dense_armor/core/infrastructure/compose/select.py
learn_one ¶
transform_one ¶
Return a copy with only the selected keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
a :class: |
required |
t
|
float | None
|
unused. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
A new :class: |