unaiverse.streams.streamproxy¶
The processor of an agent reads several input streams and writes several output streams. These streams change from one request to the next. A StreamProxy is a named, ordered group of streams bound to one interaction uuid. The actions of an agent read and write through four proxies instead of accessing the streams directly:
| Proxy | Bound to |
|---|---|
agent.stdin |
The inputs of the processor. process reads them with stdin.get(uuid=uuid, requested_by="process"). |
agent.stdtar |
The targets of learning. learn reads them with stdtar.get(uuid=uuid, requested_by="learn"). |
agent.stdext |
The streams that come from the environment (a camera for instance). |
agent.stdout |
The outputs of the processor. process writes the whole output tuple with stdout.set(outputs, data_tag=tag, uuid=uuid). |
The agent builds the four proxies in its constructor. stdtar, stdext and stdout start empty. stdin is created with a replace_none_samples_with list that holds one entry per positional input of the processor. The entry is the default value of that parameter in the forward signature of the processor (None when the parameter has no default). An optional input that receives no sample therefore reaches the processor as its default value.
The agent then binds the proxies. Before each behavior runs, set_default_stream_binding binds stdin to the input streams of the processor, stdtar to nothing, stdext to the environment streams and stdout to the output streams. It binds them with the uuid of the system interaction (Custom.SYSTEM_INTERACTION_UUID) and uses the streams of the private network when the agent runs the behavior of a world and those of the public network otherwise. When the interaction manager runs an interaction that names streams, it rebinds stdin, stdtar and stdext to the streams of the interaction with its uuid and moves stdout to that uuid with bind_uuid_only. A proxy can also hold plain values in place of streams. An interaction uses them to bind its default arguments and its data samples under keys such as <default_pos_0>. See Data streams and Agents.
The keys of a proxy are the keys of the dictionary passed to its last bind. For streams these keys are user hashes ("<peer id>:<name>"). Each entry can also be reached by its bare name (the part of the key after the last :). When two streams share a bare name, that name refers to the first one. len(proxy) is the number of bound entries, iteration yields the bound objects, "name" in proxy tests a key or a bare name and proxy[key] is proxy.get(key). str(proxy) groups the names by peer: "PEER:a,b", "owner:<default_pos_0>" or "no-streams".
from unaiverse.streams.streams import Stream
from unaiverse.streams.streamproxy import StreamProxy
a, b = Stream(name="a", data_type="text"), Stream(name="b", data_type="text")
px = StreamProxy({"PEER:a": a, "PEER:b": b})
px.set("a", "hi")
px.get("a"), px.get("PEER:a"), px.get(0), px.get() # 'hi', 'hi', 'hi', ['hi', None]
StreamProxy
¶
StreamProxy(streams: dict[str, Stream | object] | None = None, replace_none_samples_with: list | None = None)
Source code in unaiverse/streams/streamproxy.py
| Argument | Type | Default | Meaning |
|---|---|---|---|
streams |
dict[str, Stream \| object] or None |
None |
The initial entries by key. A value that is not a Stream is a plain value returned as it is. The dictionary is used without a copy. |
replace_none_samples_with |
list or None |
None |
Values returned in place of a None sample (one per position). Entry i replaces the sample of the entry at position i. Replacement does not apply with all_uuids=True. |
The proxy keeps the original list in replace_none_samples_with_list and at each bind rebuilds from it the dictionary replace_none_samples_with that maps each key to its replacement. The default uuid is None until a bind gives one.
bind
¶
bind(streams: dict[str, Stream | object], uuid: str | None = None)
Source code in unaiverse/streams/streamproxy.py
Replaces the bound entries with a shallow copy of streams and rebuilds the bare-name index and the replacements. A uuid that is not None becomes the default uuid of get, set and get_tag. A uuid of None keeps the previous default.
add_new_bind
¶
add_new_bind(stream_hash: str, stream: Stream) -> None
Source code in unaiverse/streams/streamproxy.py
Adds stream under stream_hash when that key is not bound yet. An existing key is left as it is. The bare name is indexed when it is free and the replacements are rebuilt.
bind_uuid_only
¶
Replaces the default uuid with uuid (None included) and keeps the bound entries.
clear_data
¶
Calls clear_data(uuid=uuid) on every bound entry. The interactions are kept. A plain value among the entries raises AttributeError.
clear_all_data
¶
Calls clear_all_data() on every bound entry. As with clear_data, a plain value among the entries raises AttributeError.
enable
¶
Calls enable() on every bound entry.
disable
¶
Calls disable() on every bound entry. The agent disables stdin for a processor driven by a person.
get
¶
get(key: str | int | None = None, requested_by: str | None = None, uuid: str | None = None, all_uuids: bool = False, data_type: str | None = None)
Source code in unaiverse/streams/streamproxy.py
Reads samples from the bound streams.
| Argument | Type | Default | Meaning |
|---|---|---|---|
key |
str, int or None |
None |
A key, a bare name or a position. None reads every entry. |
requested_by |
str or None |
None |
Passed to Stream.get. Each distinct requested_by receives each sample once. |
uuid |
str or None |
None |
The interaction. None uses the default uuid. |
all_uuids |
bool |
False |
Passed to Stream.get. It is accepted only together with a key or a data_type. |
data_type |
str or None |
None |
With key=None, reads only the streams of this data type. It cannot be combined with a key. |
With a key the result is the sample of that entry (or the plain value) after replacement. With key=None the result is a list with one element per entry (plain values included). It is None instead when every stream returned None. The list form holds for a proxy with a single stream too: get() returns ['hi'], not 'hi'. An unknown key raises GenException("Unknown stream/key: ..."), a combination of key and data_type raises GenException and an empty proxy returns None.
Three behaviors of the current version follow from the code:
- The check on
all_uuidstests the truth value ofkey.get(0, all_uuids=True)raisesGenExceptionbecause the position0is false. - A replacement looked up through a bare name raises
KeyErrorbecause the replacements are keyed by full key.get("a")on the example ofreplace_none_samples_withfails when the stream of"a"has no sample. The callsget(0)andget("PEER:a")return the replacement. - A replacement looked up by position beyond the end of the list raises
IndexError.
>>> pr = StreamProxy({"P:a": Stream(name="a"), "P:b": Stream(name="b")},
... replace_none_samples_with=["DEF_A", "DEF_B"])
>>> pr.get(0), pr.get("P:b"), pr.get()
('DEF_A', 'DEF_B', ['DEF_A', 'DEF_B'])
add_interaction
¶
Calls add_interaction(interaction) on every bound entry. A plain value raises AttributeError.
has_interaction
¶
Source code in unaiverse/streams/streamproxy.py
True when every bound stream has an interaction for uuid. Plain values are skipped and a proxy without streams returns True.
get_interaction
¶
Source code in unaiverse/streams/streamproxy.py
Returns the interaction of uuid taken from the first bound stream when has_interaction(uuid) is True. It returns None otherwise.
set
¶
Source code in unaiverse/streams/streamproxy.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | |
Writes samples on the bound streams.
| Argument | Type | Default | Meaning |
|---|---|---|---|
key_or_data |
list, tuple, int or str |
A list or tuple writes one value per entry in order. An int or a str selects one entry by position, key or bare name. |
|
data |
any | None |
The sample for a single entry. |
data_tag |
int |
-1 |
The tag passed to Stream.set. A negative value lets the stream choose. |
uuid |
str or None |
None |
The interaction. None uses the default uuid. |
force |
bool |
False |
Passed to Stream.set. With True the sample is written on disabled streams too. |
A list must have exactly one value per entry (GenException otherwise). Each stream receives its value with the same data_tag and the method returns the list. When key_or_data selects a single entry, the method returns the result of Stream.set (the Data or None). When that entry is a plain value, the proxy replaces it with the new value in its three internal indexes (by key, by position and by bare name) and returns the new value. An empty proxy logs the call stack and raises GenException("No streams bound to this StreamProxy"). An unknown key raises GenException("Unknown stream: ...").
The docstring documents set(data) for a proxy with a single stream. In the current version data is looked up as a key and the call raises GenException("Unknown stream: ..."). Write set(0, data) or set([data]). The call by position set(index, data, data_tag) ignores data_tag and the stream tags the sample with the clock. The forms by key and by list pass the tag.
get_tag
¶
Source code in unaiverse/streams/streamproxy.py
Returns the tag of the current sample of one entry or of the whole proxy.
| Argument | Type | Default | Meaning |
|---|---|---|---|
key |
str, int or None |
None |
A full key or a position. None returns the largest tag among the entries. |
uuid |
str or None |
None |
The interaction. None uses the default uuid. |
With key=None the result is -1 as soon as one stream has no sample for the uuid. Plain values count as -1. A position returns the tag of that stream (None when it has no sample) or -1 for a plain value. A full key returns the tag or -1. The agent reads stdin.get_tag(uuid=uuid) and copies the tag to the outputs. An empty proxy raises GenException. Unlike get and set, this method refuses a bare name with GenException("Unknown stream: ...").
clear
¶
Calls set([None] * len(proxy)). Every stream stores an empty sample for the default uuid and every plain value becomes None.
items
¶
Yields the (key, entry) pairs in binding order.
values
¶
Yields the bound entries in binding order.
The list of the bound keys in binding order. For streams these are user hashes.