Decision Tables
DecisionTable: A flexible, serializable SSVC decision table model.
DecisionTable
Bases: _Registered
, _SchemaVersioned
, _GenericSsvcObject
, _Commented
, BaseModel
DecisionTable: A flexible, serializable SSVC decision table model.
This model represents a decision table that can be used to map combinations of decision point values to outcomes. It allows for flexible mapping and can be used with helper methods to generate DataFrame and CSV representations of the decision table.
Attributes:
Source code in src/ssvc/decision_tables/base.py
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 |
|
check_mapping_keys()
Validate that each item in the mapping has the correct keys. Keys for each item should match the keys of the decision point group.
Returns:
Name | Type | Description |
---|---|---|
self |
The DecisionTable instance with validated mapping keys. |
Raises: TypeError: If any item in the mapping is not a dictionary. ValueError: If any item in the mapping does not have the expected keys.
Source code in src/ssvc/decision_tables/base.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
|
obfuscate()
Obfuscate the decision table by renaming the dict keys.
Source code in src/ssvc/decision_tables/base.py
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
|
populate_mapping_if_empty()
Populate the mapping if it is not already set.
Returns:
Name | Type | Description |
---|---|---|
self |
The DecisionTable instance with the mapping populated if it was not set. If the mapping is already set, it returns the instance unchanged. |
Source code in src/ssvc/decision_tables/base.py
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 |
|
validate_mapping()
Validate the mapping after it has been populated.
This method checks that the mapping is consistent with the decision points and outcomes defined in the table. It raises a ValueError if the mapping is not valid.
Returns:
Name | Type | Description |
---|---|---|
self |
The DecisionTable instance with validated mapping. |
Source code in src/ssvc/decision_tables/base.py
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 |
|
check_topological_order(dt)
Check the topological order of the decision table.
This function uses the check_topological_order
function from the csv_analyzer module to verify the topological order of the decision table.
It returns a list of dictionaries containing any problems found in the topological order check.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dt
|
DecisionTable
|
DecisionTable: The decision table to check. |
required |
Returns:
Type | Description |
---|---|
list[dict]
|
list[dict]: A list of dictionaries containing any problems found in the topological order check. |
list[dict]
|
Problems are defined as any pair of mappings |
list[dict]
|
Each dictionary contains the following keys: |
list[dict]
|
"u": The lower decision point value |
list[dict]
|
"v": The higher decision point value |
list[dict]
|
"u_outcome": The outcome of the lower decision point value |
list[dict]
|
"v_outcome": The outcome of the higher decision point value |
Source code in src/ssvc/decision_tables/base.py
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 |
|
decision_table_to_csv(dt, **kwargs)
Wrapper around to_df to export to CSV string. Args: dt (DecisionTable): The decision table to export. kwargs: Additional keyword arguments to pass to pandas.DataFrame.to_csv().
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The mapping table as a CSV string. |
Source code in src/ssvc/decision_tables/base.py
394 395 396 397 398 399 400 401 402 403 |
|
decision_table_to_df(dt, longform=False)
Export the decision table to a pandas DataFrame.
This is just a wrapper around the shortform and longform export functions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dt
|
DecisionTable
|
The decision table to export. |
required |
longform
|
bool
|
Whether to export in long form or short form. Defaults to False (short form). |
False
|
Returns: pd.DataFrame: The mapping table as a pandas DataFrame.
Source code in src/ssvc/decision_tables/base.py
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
|
decision_table_to_longform_df(dt)
Given a DecisionTable, convert it to a long-form DataFrame. The DataFrame will have one row per decision point value combination, with columns for each decision point and the outcome. The column names will be the decision point names with their versions, and the values will be the value names. If the decsion point is from a namespace other than "ssvc", the column name will include the namespace in parentheses.
Example
Column Heading format: {decision_point_name} v{version} ({namespace})
row,Supplier Involvement v1.0.0,Exploitation v1.0.0,Public Value Added v1.0.0,MoSCoW v1.0.0 (basic)
0,fix ready,none,limited,won't
1,fix ready,none,ampliative,won't
2,fix ready,none,precedence,won't
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
The input DataFrame from |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: The converted DataFrame. |
Source code in src/ssvc/decision_tables/base.py
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 |
|
decision_table_to_shortform_df(dt)
Export the mapping to pandas DataFrame.
Columns: one per decision point, one for outcome. Column names are namespaceversion.
Individual decision point and outcome values are represented by their value key.
Example
Table values might look like:
ssvc:SINV:1.0.0,ssvc:E:1.0.0,ssvc:PVA:1.0.0,basic:MSCW:1.0.0
FR,N,L,W
FR,N,A,W
FR,N,P,W
FR,P,L,W
FR,P,A,W
FR,P,P,W
FR,A,L,W
FR,A,A,C
Returns:
Name | Type | Description |
---|---|---|
df |
DataFrame
|
pd.DataFrame: The mapping as a pandas DataFrame. |
Raises:
Type | Description |
---|---|
ValueError
|
If the decision table has no mapping to export. |
Source code in src/ssvc/decision_tables/base.py
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 |
|
distribute_outcomes_evenly(mapping, outcome_group)
Distribute the given outcome_values across the mapping item dicts in sorted order. Overwrites the outcome value in each mapping dict item with the corresponding outcome value. The earliest mappings get the lowest outcome value, the latest get the highest. If the mapping count is not divisible by the number of outcomes, the last outcome(s) get the remainder. Returns a new list of dicts with outcome values assigned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mapping
|
list[dict[str, str]]
|
The mapping to distribute outcomes across. |
required |
outcome_values
|
list[str]
|
The list of outcome values to distribute. |
required |
Returns: list[dict[str,str]]: A new list of dicts with outcome values assigned.
Source code in src/ssvc/decision_tables/base.py
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 |
|
dpdict_to_combination_list(dpdict, exclude=[])
Generate all combinations of decision point values as dictionaries. Each combination is a dictionary with decision point IDs as keys and value keys as values.
Source code in src/ssvc/decision_tables/base.py
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 |
|
feature_importance(dt)
Calculate feature importance for the decision table. Args: dt:
Returns:
Source code in src/ssvc/decision_tables/base.py
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 |
|
interpret_feature_importance(dt)
Interpret the feature importance for the decision table. This function is a wrapper around the feature_importance function to provide a more user-friendly output. It sorts the features by importance and adds a commentary column that describes the importance of each feature, calling out the most important features, those above median importance, low to medium importance features, low importance features, and irrelevant features. The commentary is based on the computed feature importance scores.
This function is useful for understanding which decision points and their values are most influential in the decision-making process of the table, and can help in identifying which features can be considered for removal or further investigation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dt
|
DecisionTable
|
The decision table to analyze. |
required |
Returns: pd.DataFrame: A DataFrame containing the feature importance scores.
Source code in src/ssvc/decision_tables/base.py
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 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 |
|
Provides helper functions for decision tables in SSVC.
dt2df_md(dt, longform=True)
Convert a decision table to a DataFrame. Args: decision_table (DecisionTable): The decision table to convert. longform (bool): Whether to return the longform or shortform DataFrame. Returns: str: A string representation of the DataFrame in CSV format.
Source code in src/ssvc/decision_tables/helpers.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
|
mapping2mermaid(rows, title=None)
Convert a decision table mapping to a Mermaid graph. Args: rows (list[dict[str:str]]): A list of dictionaries representing the decision table mapping. Each dictionary corresponds to a row in the table, with keys as column names and values as cell values. Each row should have the same keys, representing the columns of the decision table. Returns: str: A string containing a markdown Mermaid graph representation, including the code block markers.
Source code in src/ssvc/decision_tables/helpers.py
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 |
|