Iml
IML stands for Interactive Machine Learning. Tölvera wraps the anguilla package to provide convenient ways for quickly creating mappings between vectors, functions and OSC routes.
Every Tölvera instance has an IMLDict, which is a dictionary of IML instances.
The IMLDict is accessible via the iml
attribute of a Tölvera instance, and can
be used to create and access IML instances.
There are 9 IML types, which are listed below.
Example
Here we create a mapping based on states created by tv.v.flock
,
where the per-particle state flock_p
is mapped to the species rule matrix flock_s
.
Since this is a fun2fun
mapping (see IML Types below), we provide input and output
functions, and Tölvera updates the mapping automatically every render()
call.
from tolvera import Tolvera, run
def main(**kwargs):
tv = Tolvera(**kwargs)
tv.iml.flock_p2flock_s = {
'size': (tv.s.flock_p.size, tv.s.flock_s.size),
'io': (tv.s.flock_p.to_vec, tv.s.flock_s.from_vec),
'randomise': True,
}
@tv.render
def _():
tv.px.diffuse(0.99)
tv.v.flock(tv.p)
tv.px.particles(tv.p, tv.s.species, 'circle')
return tv.px
if __name__ == '__main__':
run(main)
IML Types
vec2vec
: Vector to vector mapping.vec2fun
: Vector to function mapping.vec2osc
: Vector to OSC mapping.fun2vec
: Function to vector mapping.fun2fun
: Function to function mapping.fun2osc
: Function to OSC mapping.osc2vec
: OSC to vector mapping.osc2fun
: OSC to function mapping.osc2osc
: OSC to OSC mapping.
IMLBase
Bases: IML
This class inherits from anguilla and adds some functionality. It is not intended to be used directly, but rather to be inherited from.
The base class is initialised with a size tuple (input, output) and a config dict
which is passed to anguilla.IML
.
It provides a randomise
method which adds random pairs to the mapping.
It also provides methods to remove pairs (remove_oldest
, remove_newest
, remove_random
).
It also provides a lag
method which lags the mapped data.
Finally, it provides an update
method which is called by the updater
(see .osc.update
).
Source code in src/tolvera/iml.py
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 |
|
__call__(*args)
Call updater with args.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args |
Updater args. |
()
|
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
Mapped data. |
__init__(**kwargs)
Initialise IMLBase
kwargs
size (tuple, required): (input, output) sizes.
io (tuple, optional): (input, output) functions.
config (dict, optional): {embed_input, embed_output, interpolate, index, verbose}.
updater (cls, optional): See iipyper.osc.update (Updater, OSCSendUpdater, ...).
update_rate (int, optional): Updater's update rate (defaults to 1).
randomise (bool, optional): Randomise mapping on init (defaults to False).
rand_pairs (int, optional): Number of random pairs to add (defaults to 32).
rand_input_weight (Any, optional): Random input weight (defaults to None).
rand_output_weight (Any, optional): Random output weight (defaults to None).
rand_method (str, optional): rand_method type (see utils).
rand_kw (dict, optional): Random kwargs to pass to rand_method (see utils).
map_kw (dict, optional): kwargs to use in IML.map().
infun_kw (dict, optional): kwargs to use in infun() (type 'Fun2' only).
outfun_kw (dict, optional): kwargs to use in outfun() (type '2Fun' only).
lag (bool, optional): Lag mapped data (defaults to False). Incompatible with '*2Fun' types.
lag_coef (float, optional): Lag coefficient (defaults to 0.5 if lag
is True).
Source code in src/tolvera/iml.py
add_random_pair(input_weight=None, output_weight=None, **kwargs)
Add random pair to mapping.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_weight |
Any
|
Weighting for the input vector. Defaults to None. |
None
|
output_weight |
Any
|
Weighting for the output vector. Defaults to None. |
None
|
**kwargs |
see random_pair kwargs. |
{}
|
Source code in src/tolvera/iml.py
init_lag(**kwargs)
Initialise lag() method with kwargs
kwargs: see init kwargs.
Source code in src/tolvera/iml.py
init_randomise(**kwargs)
Initialise randomise() method with kwargs
kwargs: see init kwargs.
Source code in src/tolvera/iml.py
lag_mapped_data(lag_coef=0.5)
Lag mapped data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lag_coef |
float
|
Lag coefficient. Defaults to 0.5. |
0.5
|
random_input(**kwargs)
Random input vector.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs |
self.rand kwargs. |
{}
|
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: Random input vector. |
random_output(**kwargs)
Random output vector.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs |
self.rand kwargs |
{}
|
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: Random output vector. |
random_pair(input_weight=None, output_weight=None, **kwargs)
Create random pair.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_weight |
Any
|
Weighting for the input vector. Defaults to None. |
None
|
output_weight |
Any
|
Weighting for the output vector. Defaults to None. |
None
|
**kwargs |
rand_method (str, optional): Randomisation method. Defaults to "rand". rand_kw (dict, optional): Random kwargs to pass to rand_method (see utils). |
{}
|
Raises:
Type | Description |
---|---|
ValueError
|
Invalid input_weight type. |
ValueError
|
Invalid output_weight type. |
Returns:
Name | Type | Description |
---|---|---|
tuple |
(input, output) vectors. |
Source code in src/tolvera/iml.py
randomise(times, input_weight=None, output_weight=None, method='rand', **kwargs)
Randomise mapping.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
times |
int
|
Number of random pairs to add. |
required |
input_weight |
Any
|
Weighting for the input vector. Defaults to None. |
None
|
output_weight |
Any
|
Weighting for the output vector. Defaults to None. |
None
|
method |
str
|
Randomisation method. Defaults to "rand". |
'rand'
|
Source code in src/tolvera/iml.py
remove_newest(n=1)
Remove newest pair(s) from mapping.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n |
int
|
Number of pairs to remove. Defaults to 1. |
1
|
remove_oldest(n=1)
Remove oldest pair(s) from mapping.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n |
int
|
Number of pairs to remove. Defaults to 1. |
1
|
remove_random(n=1)
Remove random pair(s) from mapping.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n |
int
|
Number of pairs to remove. Defaults to 1. |
1
|
Source code in src/tolvera/iml.py
set_random_method(method='rand')
Set random method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
method |
str
|
Randomisation method. Defaults to "rand". |
'rand'
|
update(invec)
Update mapped data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
invec |
list | Tensor | ndarray
|
Input vector. |
required |
Returns:
Type | Description |
---|---|
list | Tensor | ndarray
|
list|torch.Tensor|np.ndarray: Mapped data. |
Source code in src/tolvera/iml.py
update_rate(rate=None)
Update rate getter/setter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rate |
int
|
Update rate. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
int |
Update rate. |
Source code in src/tolvera/iml.py
IMLDict
Bases: dotdict
IML mapping dict
Similarly to StateDict
, this class inherits from dotdict
to enable instantiation
via assignment.
Source code in src/tolvera/iml.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
|
__call__(name=None, *args, **kwargs)
Call IML instance or all IML instances.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Name of IML instance to call. Defaults to None. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
'name' not in dict. |
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
IML output or dict of IML outputs. |
Source code in src/tolvera/iml.py
__init__(context)
Initialise IMLDict
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
TolveraContext
|
TolveraContext instance. |
required |
__setattr__(__name, __value)
Set IML instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
__name |
str
|
Name of IML instance. |
required |
__value |
Any
|
IML instance kwargs. |
required |
add(name, iml_type, **kwargs)
Add IML instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Name of IML instance. |
required |
iml_type |
str
|
IML type. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
Invalid IML_TYPE. |
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
IML instance. |
Source code in src/tolvera/iml.py
infer_type(io)
Infer IML type from kwargs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
io |
tuple
|
IML input-output types. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
Invalid IML types. |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
IML type. |
Source code in src/tolvera/iml.py
set(name, kwargs)
Set IML instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Name of IML instance. |
required |
kwargs |
dict
|
IML instance kwargs. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
Cannot replace 'tv' IML instance. |
ValueError
|
Cannot replace 'i' IML instance. |
ValueError
|
Cannot replace 'o' IML instance. |
NotImplementedError
|
set() with tuple not implemented yet. |
TypeError
|
set() requires dict|tuple, not type. |
Exception
|
Other exceptions. |
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
IML instance. |
Source code in src/tolvera/iml.py
IMLFun2Fun
Bases: IMLBase
IML function to function mapping.
Example
Source code in src/tolvera/iml.py
__init__(**kwargs)
Initialise IMLFun2Fun
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kwargs |
io (tuple, required): (callable, callable) input and output functions. see IMLBase kwargs. |
{}
|
Source code in src/tolvera/iml.py
update()
Update mapped data.
Returns:
Type | Description |
---|---|
list | Tensor | ndarray
|
list|torch.Tensor|np.ndarray: Mapped data. |
Source code in src/tolvera/iml.py
IMLFun2OSC
Bases: IMLBase
IML function to OSC mapping
Example
This will send the output vector to '/out/vec'.
Source code in src/tolvera/iml.py
__init__(osc_map, **kwargs)
Initialise IMLFun2OSC
Parameters:
Name | Type | Description | Default |
---|---|---|---|
osc_map |
(OSCMap, required)
|
OSCMap instance. |
required |
kwargs |
io (tuple, required): (callable, str) input function and output OSC route. see IMLBase kwargs. |
{}
|
Source code in src/tolvera/iml.py
update()
Update mapped data.
Returns:
Type | Description |
---|---|
list[float]
|
list[float]: Mapped data. |
Source code in src/tolvera/iml.py
IMLFun2Vec
Bases: IMLBase
IML function to vector mapping.
Output vector is accessed via tv.iml.o['name']
.
Example
Source code in src/tolvera/iml.py
__init__(**kwargs)
Initialise IMLFun2Vec
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kwargs |
io (tuple, required): (callable, None) input function. see IMLBase kwargs. |
{}
|
Source code in src/tolvera/iml.py
update()
Update mapped data.
Returns:
Type | Description |
---|---|
list | Tensor | ndarray
|
list|torch.Tensor|np.ndarray: Mapped data. |
Source code in src/tolvera/iml.py
IMLOSC2Fun
Bases: IMLBase
IML OSC to function mapping
Example
Source code in src/tolvera/iml.py
__init__(osc_map, **kwargs)
Initialise IMLOSC2Fun
Parameters:
Name | Type | Description | Default |
---|---|---|---|
osc_map |
(OSCMap, required)
|
OSCMap instance. |
required |
kwargs |
io (tuple, required): (str, callable) input OSC route and output function. see IMLBase kwargs. |
{}
|
Source code in src/tolvera/iml.py
update(vector)
Update mapped data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vector |
list[float]
|
Input vector. |
required |
Returns:
Type | Description |
---|---|
list[float]
|
list[float]: Mapped data. |
Source code in src/tolvera/iml.py
IMLOSC2OSC
Bases: IMLBase
IML OSC to OSC mapping
Example
'/in/vec' is mapped and the output sent to '/out/vec'.
Source code in src/tolvera/iml.py
__init__(osc_map, osc, **kwargs)
Initialise IMLOSC2OSC
Parameters:
Name | Type | Description | Default |
---|---|---|---|
osc_map |
(OSCMap, required)
|
OSCMap instance. |
required |
osc |
OSC
|
iipyper OSC instance. |
required |
kwargs |
io (tuple, required): (str, str) input and output OSC routes. see IMLBase kwargs. |
{}
|
Source code in src/tolvera/iml.py
update(vector)
Update mapped data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vector |
list[float]
|
Input vector. |
required |
Returns:
Type | Description |
---|---|
list[float]
|
list[float]: Mapped data. |
Source code in src/tolvera/iml.py
IMLOSC2Vec
Bases: IMLBase
IML OSC to vector mapping
Example
This will map the OSC input to the output vector and store it in tv.iml.o['name']
.
Source code in src/tolvera/iml.py
__init__(osc_map, outvecs, name, **kwargs)
Initialise IMLOSC2Vec
Parameters:
Name | Type | Description | Default |
---|---|---|---|
osc_map |
(OSCMap, required)
|
OSCMap instance. |
required |
outvecs |
dict
|
Output vectors dict. |
required |
name |
str
|
Name of output vector. |
required |
kwargs |
io (tuple, required): (str, None) input OSC route. see IMLBase kwargs. |
{}
|
Source code in src/tolvera/iml.py
update(vector)
Update mapped data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vector |
list[float]
|
Input vector. |
required |
Returns:
Type | Description |
---|---|
list[float]
|
list[float]: Mapped data. |
Source code in src/tolvera/iml.py
IMLVec2Fun
Bases: IMLBase
IML vector to function mapping
Example
Source code in src/tolvera/iml.py
__init__(**kwargs)
Initialise IMLVec2Fun
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kwargs |
io (tuple, required): (None, callable) output function. see IMLBase kwargs. |
{}
|
Source code in src/tolvera/iml.py
update(invec)
Update mapped data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
invec |
list | Tensor | ndarray
|
Input vector. |
required |
Returns:
Type | Description |
---|---|
list | Tensor | ndarray
|
list|torch.Tensor|np.ndarray: Mapped data. |
Source code in src/tolvera/iml.py
IMLVec2OSC
Bases: IMLBase
IML vector to OSC mapping.
Example
Sends the output vector to '/tolvera/flock'.
Source code in src/tolvera/iml.py
__init__(osc_map, **kwargs)
Initialise IMLVec2OSC
Parameters:
Name | Type | Description | Default |
---|---|---|---|
osc_map |
(OSCMap, required)
|
OSCMap instance. |
required |
kwargs |
io (tuple, required): (None, str) output OSC route. see IMLBase kwargs. |
{}
|
Source code in src/tolvera/iml.py
update()
Update mapped data.
Returns:
Type | Description |
---|---|
list | Tensor | ndarray
|
list|torch.Tensor|np.ndarray: Mapped data. |
Source code in src/tolvera/iml.py
IMLVec2Vec
Bases: IMLBase
IML vector to vector mapping.
Input vector is accessed via tv.iml.i['name']
.
Output vector is accessed via tv.iml.o['name']
.
Example
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kwargs |
see IMLBase kwargs. |
{}
|
Source code in src/tolvera/iml.py
rand_select(method='rand')
Select randomisation method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
method |
str
|
Randomisation method. Defaults to "rand". |
'rand'
|
Raises:
Type | Description |
---|---|
ValueError
|
Invalid method. |
Returns:
Name | Type | Description |
---|---|---|
callable |
Randomisation method. |