Skip to content

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)
{'q0': 0.5, 'tau0': 4.0} 1.0 ['rad', 'N m']

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

FuncTransformer(
    fn: Callable,
    keys: tuple[str, ...] | None = None,
    whole: bool = False,
)

Bases: Transformer

Apply fn to channels, or to the whole dict.

Parameters:

Name Type Description Default
fn Callable

a callable taking one value (when whole is False) or the whole dict (when whole is True), and returning the new value or None to skip the channel.

required
keys tuple[str, ...] | None

names of the channels to transform. None transforms every channel when whole is False; an empty tuple transforms none.

None
whole bool

if True, fn receives the whole dict and must return a dict, which replaces the output.

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
def __init__(
    self,
    fn: Callable,
    keys: tuple[str, ...] | None = None,
    whole: bool = False,
) -> None:
    self.fn = fn
    self.keys = None if keys is None else list(keys)
    self.whole = bool(whole)

learn_one

learn_one(
    x: Any, y: Any = None, t: float | None = None
) -> FuncTransformer

No-op: the transformer is stateless.

Source code in dense_armor/core/infrastructure/compose/func.py
def learn_one(
    self, x: Any, y: Any = None, t: float | None = None
) -> "FuncTransformer":
    """No-op: the transformer is stateless."""
    self._time_step(t)
    return self

transform_one

transform_one(x: Any, t: float | None = None) -> Any

Apply the function and return the new sample.

Parameters:

Name Type Description Default
x Any

a :class:Signal or a plain dict.

required
t float | None

unused.

None

Returns:

Type Description
Any

A new :class:Signal (or dict) with the transformed values.

Source code in dense_armor/core/infrastructure/compose/func.py
def transform_one(self, x: Any, t: float | None = None) -> Any:
    """Apply the function and return the new sample.

    Args:
        x: a :class:`Signal` or a plain dict.
        t: unused.

    Returns:
        A new :class:`Signal` (or dict) with the transformed values.
    """
    _ = t
    if self.whole:
        return self._transform_whole(x)
    return self._transform_channels(x)

Discard

Discard(keys: tuple[str, ...] = ())

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
def __init__(self, keys: tuple[str, ...] = ()) -> None:
    self.keys = list(keys)

learn_one

learn_one(
    x: Any, y: Any = None, t: float | None = None
) -> Discard

No-op: the transformer is stateless.

Source code in dense_armor/core/infrastructure/compose/select.py
def learn_one(self, x: Any, y: Any = None, t: float | None = None) -> "Discard":
    """No-op: the transformer is stateless."""
    self._time_step(t)
    return self

transform_one

transform_one(x: Any, t: float | None = None) -> Any

Return a copy without the dropped keys.

Parameters:

Name Type Description Default
x Any

a :class:Signal or a plain dict.

required
t float | None

unused.

None

Returns:

Type Description
Any

A new :class:Signal (or dict) without the dropped keys.

Source code in dense_armor/core/infrastructure/compose/select.py
def transform_one(self, x: Any, t: float | None = None) -> Any:
    """Return a copy without the dropped keys.

    Args:
        x: a :class:`Signal` or a plain dict.
        t: unused.

    Returns:
        A new :class:`Signal` (or dict) without the dropped keys.
    """
    _ = t
    if isinstance(x, Signal):
        keep = [k for k in x.names if k not in self.keys]
    else:
        keep = [k for k in x if k not in self.keys]
    return _subset(x, keep)

Select

Select(keys: tuple[str, ...] = ())

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
def __init__(self, keys: tuple[str, ...] = ()) -> None:
    self.keys = list(keys)

learn_one

learn_one(
    x: Any, y: Any = None, t: float | None = None
) -> Select

No-op: the transformer is stateless.

Source code in dense_armor/core/infrastructure/compose/select.py
def learn_one(self, x: Any, y: Any = None, t: float | None = None) -> "Select":
    """No-op: the transformer is stateless."""
    self._time_step(t)
    return self

transform_one

transform_one(x: Any, t: float | None = None) -> Any

Return a copy with only the selected keys.

Parameters:

Name Type Description Default
x Any

a :class:Signal or a plain dict.

required
t float | None

unused.

None

Returns:

Type Description
Any

A new :class:Signal (or dict) with only the selected keys.

Source code in dense_armor/core/infrastructure/compose/select.py
def transform_one(self, x: Any, t: float | None = None) -> Any:
    """Return a copy with only the selected keys.

    Args:
        x: a :class:`Signal` or a plain dict.
        t: unused.

    Returns:
        A new :class:`Signal` (or dict) with only the selected keys.
    """
    _ = t
    return _subset(x, self.keys)