API
CoBaIR is split into the config model and the inference:
cobair.modeldescribes a scenario (contexts, intentions, influences, decision threshold), validates it, reads and writes config files and computes the conditional probability tables. It only needspyyaml.cobair.inferinfers the intentions for given evidence with the two-layer Bayesian net. It needs the[infer]extra:pip install "cobair[infer]".
from cobair.model import Config
from cobair.infer import Inference
config = Config.load('small_example.yml')
config.change_influence_value('pick up tool', 'speech commands', 'pickup', 5)
print(config.validate()) # [] if the config is valid
inference = Inference(config) # create a new Inference after editing the config
intention, decision_threshold, probabilities = inference.infer({'speech commands': 'pickup'})
cobair.model
The config model of CoBaIR: contexts, intentions, influences and the decision threshold.
This module is pure Python (it only needs pyyaml), so it can be used without the inference
dependencies, e.g. in the GUIs. The conditional probability tables of the two-layer Bayesian net
are computed here as well, cobair.infer only hands them to pgmpy.
Example
from cobair.model import Config
config = Config.load('small_example.yml')
config.add_context('weather', {'sunny': 0.7, 'rainy': 0.3})
config.change_influence_value('pick up tool', 'weather', 'rainy', 4)
print(config.validate()) # [] if the config is valid
config.save('my_config.yml')
CPTs
dataclass
The conditional probability tables of the two-layer Bayesian net.
The order of the states is the order of the instantiations in the config.
The columns of the intention tables iterate over all combinations of context instantiations
in the order of contexts, the last context changes fastest (the order pgmpy expects).
Attributes:
| Name | Type | Description |
|---|---|---|
contexts |
list
|
All contexts, the parents of every intention. |
states |
dict
|
The instantiations of every context. |
context_priors |
dict
|
The apriori probability of every instantiation of every context. |
intentions |
list
|
All intentions. |
intention_values |
dict
|
For every intention |
Source code in cobair/model.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
cards
property
The number of instantiations of every context in the order of contexts.
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
The cardinalities. |
Config
A mutable CoBaIR config.
Attributes:
| Name | Type | Description |
|---|---|---|
contexts |
|
|
intentions |
|
|
decision_threshold |
The probability an intention must exceed to be picked. |
Source code in cobair/model.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 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 346 347 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 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 | |
valid
property
True if validate() finds no problems.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
Validity of the config. |
__eq__(other)
Configs are equal if contexts, intentions and decision threshold are equal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
The object to compare with. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if equal. |
Source code in cobair/model.py
249 250 251 252 253 254 255 256 257 258 259 260 | |
__init__(contexts=None, intentions=None, decision_threshold=0.0)
Create a config, the given dicts are copied.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
contexts
|
dict
|
|
None
|
intentions
|
dict
|
|
None
|
decision_threshold
|
float
|
The probability an intention must exceed to be picked. |
0.0
|
Source code in cobair/model.py
146 147 148 149 150 151 152 153 154 155 156 157 | |
add_combined_influence(intention, contexts, instantiations, value)
Set the influence of a combination of context instantiations on an intention.
If all instantiations of the combination are present, the combined influence replaces the single influences of these contexts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
intention
|
Hashable
|
Name of the intention. |
required |
contexts
|
tuple
|
The names of the contexts. |
required |
instantiations
|
tuple
|
One instantiation for each of the contexts. |
required |
value
|
int
|
The influence value, one out of [0, 1, 2, 3, 4, 5]. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the contexts are empty, the intention does not exist or an instantiation does not exist. |
Source code in cobair/model.py
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 | |
add_context(context, instantiations)
Add a new context; its influence on all intentions is set to 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
Hashable
|
Name of the new context. |
required |
instantiations
|
dict
|
|
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the context already exists. |
Source code in cobair/model.py
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | |
add_intention(intention)
Add a new intention; the influence of all contexts is set to 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
intention
|
Hashable
|
Name of the new intention. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the intention already exists. |
Source code in cobair/model.py
416 417 418 419 420 421 422 423 424 425 426 427 428 429 | |
change_context_apriori_value(context, instantiation, value)
Change the apriori probability of a context instantiation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
Hashable
|
Name of the context. |
required |
instantiation
|
Hashable
|
The instantiation. |
required |
value
|
float
|
The new apriori probability. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the context or instantiation does not exist. |
Source code in cobair/model.py
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 | |
change_decision_threshold(decision_threshold)
Change the decision threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
decision_threshold
|
float
|
The new decision threshold. |
required |
Source code in cobair/model.py
545 546 547 548 549 550 551 | |
change_influence_value(intention, context, instantiation, value)
Change the influence of a context instantiation on an intention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
intention
|
Hashable
|
Name of the intention. |
required |
context
|
Hashable
|
Name of the context (or tuple of contexts for a combined influence). |
required |
instantiation
|
Hashable
|
The instantiation (or tuple of instantiations). |
required |
value
|
int
|
The new influence value, one out of [0, 1, 2, 3, 4, 5]. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the influence does not exist. |
Source code in cobair/model.py
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 | |
combined_influences(intention)
Return the combined influences of an intention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
intention
|
Hashable
|
Name of the intention. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
|
Source code in cobair/model.py
553 554 555 556 557 558 559 560 561 562 563 | |
copy()
Return a deep copy.
Returns:
| Name | Type | Description |
|---|---|---|
Config |
Config
|
The copy. |
Source code in cobair/model.py
186 187 188 189 190 191 192 | |
cpts()
Compute the conditional probability tables of the two-layer Bayesian net.
The probability of an intention for a combination of context instantiations is the
average of the probabilities of the influences (see INFLUENCE_TO_PROBABILITY).
If a combined influence matches the combination, its value replaces the single influences
of the combined contexts; if several match, the first one wins.
Returns:
| Name | Type | Description |
|---|---|---|
CPTs |
CPTs
|
The conditional probability tables. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the config is not valid. |
Source code in cobair/model.py
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 | |
del_combined_influence(intention, contexts, instantiations)
Remove the combined influence of a combination of context instantiations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
intention
|
Hashable
|
Name of the intention. |
required |
contexts
|
tuple
|
The names of the contexts. |
required |
instantiations
|
tuple
|
One instantiation for each of the contexts. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the combined influence does not exist. |
Source code in cobair/model.py
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 | |
del_context(context)
Remove a context including all its (combined) influences.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
Hashable
|
Name of the context to delete. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the context does not exist. |
Source code in cobair/model.py
401 402 403 404 405 406 407 408 409 410 411 412 413 414 | |
del_intention(intention)
Remove an intention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
intention
|
Hashable
|
Name of the intention to delete. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the intention does not exist. |
Source code in cobair/model.py
448 449 450 451 452 453 454 455 456 457 458 459 460 | |
dumps()
Return the config as YAML text with header.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The content of a config file. |
Source code in cobair/model.py
231 232 233 234 235 236 237 238 239 | |
edit_context(context, instantiations, new_name=None)
Change the instantiations of a context and optionally rename it.
Influences of removed instantiations are removed, new instantiations get the influence 0. Combined influences follow a rename and are removed if they use a removed instantiation.
Note
Changing the name of an instantiation will always set the influence value of this instantiation to zero for all intentions!
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
Hashable
|
Name of the context to edit. |
required |
instantiations
|
dict
|
|
required |
new_name
|
Hashable
|
A new name for the context. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the context does not exist or |
Source code in cobair/model.py
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 | |
edit_intention(intention, new_name)
Rename an intention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
intention
|
Hashable
|
Name of the intention to edit. |
required |
new_name
|
Hashable
|
A new name for the intention. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the intention does not exist or |
Source code in cobair/model.py
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 | |
from_dict(config)
classmethod
Create a config from a dict in the config file format; unknown keys are ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict
|
A dict with the keys |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Config |
Config
|
The new config. |
Source code in cobair/model.py
162 163 164 165 166 167 168 169 170 171 172 173 174 | |
load(path)
classmethod
Load a config from a YAML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path to the config file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Config |
Config
|
The loaded config. |
Source code in cobair/model.py
219 220 221 222 223 224 225 226 227 228 229 | |
loads(text)
classmethod
Create a config from YAML text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The content of a config file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Config |
Config
|
The loaded config. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the text is not a mapping or was written with a newer format version. |
Source code in cobair/model.py
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
save(path)
Save the config to a YAML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path to the config file. |
required |
Source code in cobair/model.py
241 242 243 244 245 246 247 | |
to_dict()
Return the config as plain dicts in the config file format (without header).
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
A deep copy of the config. |
Source code in cobair/model.py
176 177 178 179 180 181 182 183 184 | |
validate()
Check that the config is complete and consistent.
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
Messages describing all problems, empty if the config is valid. |
Source code in cobair/model.py
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 | |
PrettySafeLoader
Bases: SafeLoader
A YAML loader that constructs Python tuples from YAML sequences.
Source code in cobair/model.py
54 55 56 57 58 59 60 61 62 63 64 65 66 | |
construct_python_tuple(node)
Construct a Python tuple from a YAML sequence node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Any
|
The YAML sequence node to construct a tuple from. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
The constructed tuple. |
Source code in cobair/model.py
57 58 59 60 61 62 63 64 65 66 | |
cobair.infer
Inference of intentions from context with the two-layer Bayesian net.
This is a thin adapter: the conditional probability tables are computed by cobair.model,
this module hands them to pgmpy / bnlearn. It needs the [infer] extra
(pip install "cobair[infer]").
Example
from cobair.model import Config
from cobair.infer import Inference
inference = Inference(Config.load('small_example.yml'))
intention, decision_threshold, probabilities = inference.infer({'speech commands': 'pickup'})
Inference
Infers the probabilities of the intentions of a valid config.
The inference works on a copy of the config: create a new Inference after editing it.
Source code in cobair/infer.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 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 | |
__init__(config)
Build the Bayesian net for a config, which must be valid (else ValueError).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
Config
|
A valid config. |
required |
Source code in cobair/infer.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
bind_discretization_function(context, discretization_function)
Bind a discretization function to a context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
Hashable
|
One of the contexts of the config. |
required |
discretization_function
|
Callable
|
A function which takes one parameter and returns one of the instantiations of the context. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the context does not exist. |
Source code in cobair/infer.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | |
check_evidence(context, instantiation)
Check if an instantiation is valid evidence for a context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
Hashable
|
A context. |
required |
instantiation
|
An instantiation of the context. |
required |
Returns:
| Type | Description |
|---|---|
tuple[bool, str]
|
tuple[bool, str]: Validity and an error or warning message (empty if fine). |
Source code in cobair/infer.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
infer(evidence, normalized=True, decision_threshold=None)
Infer the probabilities of the intentions given evidence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
evidence
|
dict
|
|
required |
normalized
|
bool
|
Normalize the probabilities to sum up to 1. |
True
|
decision_threshold
|
float
|
A threshold for picking the most likely intention, between 0 and 1. Defaults to the decision threshold of the config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
InferenceResult |
InferenceResult
|
The most likely intention (None if the decision threshold is not exceeded), the decision threshold and the probabilities of all intentions. |
Source code in cobair/infer.py
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 | |
InferenceResult
Bases: NamedTuple
The result of an inference, unpacks like the tuple BayesNet.infer returned.
Attributes:
| Name | Type | Description |
|---|---|---|
intention |
Hashable | None
|
The most likely intention or None if its probability doesn't exceed the decision threshold. |
decision_threshold |
float
|
The decision threshold that was used. |
probabilities |
dict
|
|
Source code in cobair/infer.py
36 37 38 39 40 41 42 43 44 45 46 47 48 | |
normalize(probabilities)
Normalize the probabilities of the intentions to a probability distribution.
Each intention is inferred independently as True or False, so the raw probabilities don't sum up to 1 and can't be compared as a distribution over intentions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
probabilities
|
dict
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
|
Source code in cobair/infer.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 | |
Deprecated: cobair.bayes_net
The 4.x API BayesNet, load_config, config_to_default_dict and default_to_regular still works in 5.x,
but raises a DeprecationWarning and will be removed in 6.0.0.
Deprecated 4.x API, use cobair.model and cobair.infer instead. Removed in 6.0.0.
| 4.x | 5.x |
|---|---|
BayesNet(config) |
Config.from_dict(config) + Inference(config) |
BayesNet().load(path), load_config(path) |
Config.load(path) |
bayes_net.add_context(...) etc. |
config.add_context(...) etc. |
bayes_net.validate_config() |
config.validate() |
bayes_net.infer(evidence) |
Inference(config).infer(evidence) |
bayes_net.save(path) |
config.save(path) |
BayesNet
Deprecated facade over cobair.model.Config and cobair.infer.Inference.
config is the source of truth like in 4.x: every method reads it, applies the change through
Config and replaces it with the result.
Source code in cobair/bayes_net.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 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 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | |
__init__(config=None, bn_verbosity=0, validate=True)
Initializes the BayesNet with the given config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict
|
A dict with a config following the config format. |
None
|
bn_verbosity
|
int
|
Ignored since bnlearn 1.x, which logs through |
0
|
validate
|
bool
|
Flag if the given config should be validated or not. This is necessary to load invalid configs |
True
|
Source code in cobair/bayes_net.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
add_combined_influence(intention, contexts, instantiations, value)
Adds an influence value for a combination of context instantiations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
intention
|
str
|
Name of the intention |
required |
contexts
|
tuple
|
tuple containing the names of the contexts |
required |
instantiations
|
tuple
|
Tuple of context instances to set influence value for. |
required |
value
|
int
|
influence value. Can be one out of [0, 1, 2, 3, 4, 5] |
required |
Source code in cobair/bayes_net.py
219 220 221 222 223 224 225 226 227 228 229 | |
add_context(context, instantiations)
This will add a new context to the config and updates the bayesNet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
str
|
a new context for the config |
required |
instantiations
|
dict
|
a dict of the instantiations and their apriori probabilities |
required |
Source code in cobair/bayes_net.py
146 147 148 149 150 151 152 153 | |
add_intention(intention)
This will add a new intention to the config and updates the bayesNet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
intention
|
str
|
Name of a new intention |
required |
Source code in cobair/bayes_net.py
155 156 157 158 159 160 161 | |
bind_discretization_function(context, discretization_function)
Binds a discretization_function to a specific context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
One of the possible contexts from the config |
required | |
discretization_function
|
A discretization function which has to take one parameter and return one of the possible discrete context instantiations. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the context does not exist. |
Source code in cobair/bayes_net.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | |
change_context_apriori_value(context, instantiation, value)
Changes the apriori_value for a context instantiation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
str
|
Name of the context |
required |
instantiation
|
The instantiation for which the apriori value needs to be changed |
required | |
value
|
float
|
the new apriori value |
required |
Source code in cobair/bayes_net.py
198 199 200 201 202 203 204 205 206 | |
change_decision_threshold(decision_threshold)
Changes the decision threshold in the config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
decision_threshold
|
The new decision threshold. |
required |
Source code in cobair/bayes_net.py
241 242 243 244 245 246 247 | |
change_influence_value(intention, context, instantiation, value)
Update the influence value of a specific intention for a particular context instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
intention
|
str
|
Name of the intention |
required |
context
|
str
|
name of the context |
required |
instantiation
|
The instantiation for which the influence value should be changed |
required | |
value
|
int
|
the new influence value. Can be one out of [0, 1, 2, 3, 4, 5] |
required |
Source code in cobair/bayes_net.py
208 209 210 211 212 213 214 215 216 217 | |
del_combined_influence(intention, contexts, instantiations)
Removes the influence value for a combination of context instantiations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
intention
|
str
|
Name of the intention |
required |
contexts
|
tuple
|
tuple containing the names of the contexts |
required |
instantiations
|
tuple
|
tuple of context instantiations |
required |
Source code in cobair/bayes_net.py
231 232 233 234 235 236 237 238 239 | |
del_context(context)
Removes a context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
str
|
Name of the context to delete |
required |
Source code in cobair/bayes_net.py
182 183 184 185 186 187 188 | |
del_intention(intention)
Remove an intention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
intention
|
Name of the intention to delete |
required |
Source code in cobair/bayes_net.py
190 191 192 193 194 195 196 | |
edit_context(context, instantiations, new_name=None)
Edits an existing context - this can also be used to remove instantiations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
str
|
Name of the context to edit |
required |
instantiations
|
dict
|
A Dict of instantiations and their apriori probabilities. |
required |
new_name
|
str
|
A new name for the context |
None
|
Source code in cobair/bayes_net.py
163 164 165 166 167 168 169 170 171 | |
edit_intention(intention, new_name)
Edits an existing intention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
intention
|
str
|
Name of the intention to edit |
required |
new_name
|
str
|
A new name for the intention |
required |
Source code in cobair/bayes_net.py
173 174 175 176 177 178 179 180 | |
infer(evidence, normalized=True, decision_threshold=None)
Infers the probabilities for the intentions with given evidence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
evidence
|
Evidence to infer the probabilities of all intentions. |
required | |
normalized
|
Flag if the returned inference is normalized to sum up to 1. |
True
|
|
decision_threshold
|
a threshold for picking the most likely intention. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple
|
the highest ranking intention (or None), the decision threshold and a dictionary of intentions and the corresponding probabilities. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the config is invalid. |
Source code in cobair/bayes_net.py
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | |
load(path)
Loads a config from file and reinitializes the bayesNet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
path to the file the config is saved in |
required |
Source code in cobair/bayes_net.py
264 265 266 267 268 269 270 | |
normalize_inference(inference)
Normalizes the inference to a proper probability distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inference
|
dict
|
dictionary of intentions and the corresponding probabilities |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
dictionary of intentions and the corresponding normalized probabilities. |
Source code in cobair/bayes_net.py
327 328 329 330 331 332 333 334 335 336 | |
save(path, save_invalid=True)
Saves the config of the bayesNet to a yml file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
path to the file the config will be saved in |
required |
save_invalid
|
bool
|
Flag to decide if invalid configs can be saved |
True
|
Source code in cobair/bayes_net.py
252 253 254 255 256 257 258 259 260 261 262 | |
valid_evidence(context, instantiation)
Tests if evidence is a valid instantiation for the context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
str
|
a context |
required |
instantiation
|
an instantiation of the context |
required |
Returns:
| Type | Description |
|---|---|
tuple[bool, str]
|
tuple[bool, str]: validity and an error/warn message |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the config is invalid. |
Source code in cobair/bayes_net.py
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | |
validate_config()
Validate that the current config follows the correct format.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if config is valid, False otherwise. A warning is raised for every problem. |
Source code in cobair/bayes_net.py
131 132 133 134 135 136 137 138 139 140 141 | |
PrettySafeLoader
Bases: SafeLoader
A YAML loader that constructs Python tuples from YAML sequences.
Source code in cobair/model.py
54 55 56 57 58 59 60 61 62 63 64 65 66 | |
construct_python_tuple(node)
Construct a Python tuple from a YAML sequence node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Any
|
The YAML sequence node to construct a tuple from. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
The constructed tuple. |
Source code in cobair/model.py
57 58 59 60 61 62 63 64 65 66 | |
config_to_default_dict(config=None)
This casts a config given as dict into a defaultdict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict
|
A dict with a config following the config format. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
a defaultdict containing the config |
Source code in cobair/bayes_net.py
392 393 394 395 396 397 398 399 400 401 402 | |
default_to_regular(d)
This casts a defaultdict to a regular dict which is needed for saving as yml file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
the dict which should be casted |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
a regular dict casted from the defaultdict |
Source code in cobair/bayes_net.py
418 419 420 421 422 423 424 425 426 427 428 | |
load_config(path)
Helper function to load a config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
path to the file the config is saved in |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
a defaultdict containing the config |
Source code in cobair/bayes_net.py
405 406 407 408 409 410 411 412 413 414 415 | |