Documentation
Below, you will find the documentation of the
causalAssembly
project code.
Utility classes and functions related to causalAssembly. Copyright (c) 2023 Robert Bosch GmbH
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.
ProcessCell
Representation of a single Production Line Cell (to model a station / a process in a production line environment).
A Cell can contain multiple modules (sub-graphs, which are nx.DiGraph objects).
Note that none of the term Cell, Process or Module has a strict definition. The convention is based on a production line, consisting of several cells which are to be modeled by means of smaller graphs (modules) by a user of the repository.
Source code in causalAssembly/models_dag.py
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 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 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 |
|
causal_order: list[str]
property
Returns the causal order of the current graph. Note that this order is in general not unique.
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: Causal order |
edges: list[tuple]
property
Edges in the graph.
Returns:
Type | Description |
---|---|
list[tuple]
|
list[tuple] |
ground_truth: pd.DataFrame
property
Returns the current ground truth as pandas adjacency.
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: Adjacenccy matrix. |
nodes: list[str]
property
Nodes in the graph.
Returns:
Type | Description |
---|---|
list[str]
|
list[str] |
num_edges: int
property
Number of edges in the graph
Returns:
Type | Description |
---|---|
int
|
int |
num_nodes: int
property
Number of nodes in the graph
Returns:
Type | Description |
---|---|
int
|
int |
sparsity: float
property
Sparsity of the graph
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
in [0,1] |
__verify_edges_are_allowed(m1, m2, edges)
Check whether all starting point nodes (first value in edge tuple) are allowed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
m1 |
str
|
Module1 |
required |
m2 |
str
|
Module2 |
required |
edges |
list[tuple]
|
Edges |
required |
Raises:
Type | Description |
---|---|
ValueError
|
starting node not in M1 |
ValueError
|
ending node not in M2 |
Source code in causalAssembly/models_dag.py
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 |
|
add_module(graph, allow_in_edges=True, mark_hidden=False)
Adds module to cell graph. Module has to be as nx.DiGraph object
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph |
DiGraph
|
Graph to add to cell. |
required |
allow_in_edges |
bool
|
whether nodes in the module are allowed to have in-edges. Defaults to True. |
True
|
mark_hidden |
bool | list
|
If False all nodes' 'is_hidden' attribute is set to False. If list of node names is provided these get overwritten to True. Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
prefix of Module created |
Source code in causalAssembly/models_dag.py
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 |
|
connect_by_module(m1, m2, edges)
Connect two modules (by name, e.g. M2, M4) of the cell by a list of edges with the original node names.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
m1 |
str
|
str |
required |
m2 |
str
|
str |
required |
edges |
list[tuple]
|
list[tuple]: use the original node names before they have entered into the cell, i.e. not with Cy_Mx prefix |
required |
Source code in causalAssembly/models_dag.py
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
|
connect_by_random_edges(sparsity=0.1)
Add random edges to graph according to proportion, with restriction specified in node attributes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sparsity |
float
|
Sparsity parameter in (0,1). Defaults to 0.1. |
0.1
|
Raises:
Type | Description |
---|---|
NotImplementedError
|
when node attributes are not set. |
TypeError
|
when resulting graph contains cycles. |
Returns:
Type | Description |
---|---|
DiGraph
|
nx.DiGraph: DAG with new edges added. |
Source code in causalAssembly/models_dag.py
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 |
|
input_cellgraph_directly(graph, allow_in_edges=False)
Allow to input graphs on a cell-level. This should only be done if the graph
is already available for the entire cell, otherwise add_module()
is preferred.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph |
DiGraph
|
Cell graph to input |
required |
allow_in_edges |
bool
|
Defaults to False. |
False
|
Source code in causalAssembly/models_dag.py
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
|
interventional_sample_from_drf(size=10, smoothed=True)
Draw from the trained DRF.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size |
int
|
Number of samples to be drawn. Defaults to 10. |
10
|
smoothed |
bool
|
If set to true, marginal distributions will be sampled from smoothed bootstraps. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: Data frame that follows the distribution implied by the ground truth. |
Source code in causalAssembly/models_dag.py
365 366 367 368 369 370 371 372 373 374 375 376 |
|
next_module_prefix()
Return the next module prefix, e.g. if there are already 3 modules connected to the cell, will return module_prefix4
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
module_prefix |
Source code in causalAssembly/models_dag.py
515 516 517 518 519 520 521 522 523 |
|
parents(of_node)
Return parents of node in question.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
of_node |
str
|
Node in question. |
required |
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: parent set. |
Source code in causalAssembly/models_dag.py
250 251 252 253 254 255 256 257 258 259 |
|
sample_from_drf(size=10, smoothed=True)
Draw from the trained DRF.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size |
int
|
Number of samples to be drawn. Defaults to 10. |
10
|
smoothed |
bool
|
If set to true, marginal distributions will be sampled from smoothed bootstraps. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: Data frame that follows the distribution implied by the ground truth. |
Source code in causalAssembly/models_dag.py
352 353 354 355 356 357 358 359 360 361 362 363 |
|
save_drf(filename, location=None)
Writes a drf dict to file. Please provide the .pkl suffix!
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename |
str
|
name of the file to be written e.g. examplefile.pkl |
required |
location |
str
|
path to file in case it's not located in the current working directory. Defaults to None. |
None
|
Source code in causalAssembly/models_dag.py
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
|
show(meta_desc='')
Plots the cell graph by giving extra weight to nodes with high in- and out-degree.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
meta_desc |
str
|
Defaults to "". |
''
|
Source code in causalAssembly/models_dag.py
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 |
|
ProductionLineGraph
Blueprint of a Production Line Graph.
A Production Line consists of multiple Cells, each Cell can contain multiple modules. Modules can be instantiated randomly or manually. Cellgraphs and linegraphs can be instantiated directly from nx.DiGraph objects. Similarly, edges can be drawn at random (obeying certain probability choices that can be set by the user) between cells/moduls or manually.
Besides populating a production line with cell/module-graphs one can obtain semi-synthetic data obeying the standard causal assumptions:
1. Markov Property
2. Faithfulness
This can be achieved by fitting distributional random forests to the line/cell-graphs and draw data from these. A random number stream is initiated when calling this class. If desired this can be overwritten manually.
Source code in causalAssembly/models_dag.py
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 |
|
between_adjacency: pd.DataFrame
property
Returns adjacency matrix ignoring all within-cell edges.
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: adjacency matrix |
causal_order: list[str]
property
Returns the causal order of the current graph. Note that this order is in general not unique.
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: Causal order |
edges: list[tuple]
property
Edges in the graph.
Returns:
Type | Description |
---|---|
list[tuple]
|
list[tuple] |
eol_cell: ProcessCell | None
property
the EOL cell
(if any single cell has attr .is_eol = True), otherwise returns None
graph: nx.DiGraph
property
Returns a nx.DiGraph object of the actual graph.
The graph is only built HERE, i.e. all ProcessCells exist standalone in self.cells, with no connections between their nodes yet.
The edges are stored in self.cell_connetor_edges, where they are added by random methods or by user (the dag builder) himself.
ATTENTION: you can not work on self.graph and add manually edges, nodes and expect them to work.
Returns nx.DiGraph
ground_truth: pd.DataFrame
property
Returns the current ground truth as pandas adjacency.
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: Adjacenccy matrix. |
ground_truth_visible: pd.DataFrame
property
Generates a ground truth graph in form of a pandas adjacency matrix. Row and column names correspond to visible. The following integers can occur:
amat[i,j] = 1 indicates i -> j amat[i,j] = 0 indicates no edge amat[i,j] = amat[j,i] = 2 indicates i <-> j and there exists a common hidden confounder
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: amat with values in {0,1,2}. |
interventions: list
property
Returns all interventions performed on the original graph
Returns:
Name | Type | Description |
---|---|---|
list |
list
|
list of intervened upon nodes in do(x) notation. |
nodes: list[str]
property
Nodes in the graph.
Returns:
Type | Description |
---|---|
list[str]
|
list[str] |
num_edges: int
property
Number of edges in the graph
Returns:
Type | Description |
---|---|
int
|
int |
num_nodes: int
property
Number of nodes in the graph
Returns:
Type | Description |
---|---|
int
|
int |
sparsity: float
property
Sparsity of the graph
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
in [0,1] |
within_adjacency: pd.DataFrame
property
Returns adjacency matrix ignoring all between-cell edges.
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: adjacency matrix |
connect_across_cells_manually(edges)
Add edges manually across cells. You need to give the full name Args: edges (list[tuple]): list of edges to add
Source code in causalAssembly/models_dag.py
1090 1091 1092 1093 1094 1095 |
|
connect_cells(forward_probs=[0.1, 0.05])
Randomly connects cells in a ProductionLineGraph according to a forwards logic.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
forward_probs |
list[float]
|
Array of sparsity scalars of dimension max_forward. Defaults to [0.1, 0.05]. |
[0.1, 0.05]
|
Source code in causalAssembly/models_dag.py
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 |
|
copy()
Makes a full copy of the current ProductionLineGraph object
Returns:
Name | Type | Description |
---|---|---|
ProductionLineGraph |
ProductionLineGraph
|
copyied object. |
Source code in causalAssembly/models_dag.py
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 |
|
from_nx(g, cell_mapper)
classmethod
Convert nx.DiGraph to ProductionLineGraph. Requires a dict mapping where keys are cell names and values correspond to nodes within these cells.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
g |
DiGraph
|
graph to be converted |
required |
cell_mapper |
dict[str, list]
|
dict to indicate what nodes belong to which cell |
required |
Returns:
Name | Type | Description |
---|---|---|
ProductionLineGraph |
ProductionLineGraph
|
the graph as a ProductionLineGraph object. |
Source code in causalAssembly/models_dag.py
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 |
|
get_data()
classmethod
Load in semi-synthetic data as described in the paper: causalAssembly: Generating Realistic Production Data for Benchmarking Causal Discovery Returns: pd.DataFrame: Data from which data should be generated.
Source code in causalAssembly/models_dag.py
1202 1203 1204 1205 1206 1207 1208 1209 1210 |
|
get_ground_truth()
classmethod
Loads in the ground_truth as described in the paper: causalAssembly: Generating Realistic Production Data for Benchmarking Causal Discovery Returns: ProductionLineGraph: ground_truth for cells and line.
Source code in causalAssembly/models_dag.py
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 |
|
get_nodes_of_station(station_name)
Returns nodes in chosen Station.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
station_name |
str
|
name of station. |
required |
Raises:
Type | Description |
---|---|
AssertionError
|
if station name doesn't match pline. |
Returns:
Name | Type | Description |
---|---|---|
list |
list
|
nodes in chosen station |
Source code in causalAssembly/models_dag.py
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 |
|
hidden_nodes()
Returns list of nodes marked as hidden
Returns:
Name | Type | Description |
---|---|---|
list |
list
|
of hidden nodes |
Source code in causalAssembly/models_dag.py
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 |
|
intervene_on(nodes_values)
Specify hard or soft intervention. If you want to intervene
upon more than one node provide a list of nodes to intervene on
and a list of corresponding values to set these nodes to.
(see example). The mutilated dag will automatically be
stored in mutiliated_dags
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nodes_values |
dict[str, RandomSymbol | float]
|
either single real number or sympy.stats.RandomSymbol. If you like to intervene on more than one node, just provide more key-value pairs. |
required |
Raises:
Type | Description |
---|---|
AssertionError
|
If node(s) are not in the graph |
Source code in causalAssembly/models_dag.py
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 |
|
interventional_amat(which_intervention)
Returns the adjacency matrix of a chosen mutilated DAG.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
which_intervention |
int | str
|
Integer count of your chosen intervention or literal string. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
"The intervention you provide does not exist." |
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: Adjacency matrix. |
Source code in causalAssembly/models_dag.py
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 |
|
load_drf(filename, location=None)
classmethod
Loads a drf dict from a .pkl file into the workspace.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename |
str
|
name of the file e.g. examplefile.pkl |
required |
location |
str
|
path to file in case it's not located in the current working directory. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
DRF |
dict
|
dict of trained drf objects |
Source code in causalAssembly/models_dag.py
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 |
|
new_cell(name=None, is_eol=False)
Add a new cell to the production line.
If no name is given, cell name is given by counting available cells + 1
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Defaults to None. |
None
|
is_eol |
bool
|
Whether cell is end-of-line. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
ProcessCell
|
ProcessCell |
Source code in causalAssembly/models_dag.py
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 |
|
parents(of_node)
Return parents of node in question.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
of_node |
str
|
Node in question. |
required |
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: parent set. |
Source code in causalAssembly/models_dag.py
946 947 948 949 950 951 952 953 954 955 |
|
sample_from_drf(size=10, smoothed=True)
Draw from the trained DRF.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size |
int
|
Number of samples to be drawn. Defaults to 10. |
10
|
smoothed |
bool
|
If set to true, marginal distributions will be sampled from smoothed bootstraps. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: Data frame that follows the distribution implied by the ground truth. |
Source code in causalAssembly/models_dag.py
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 |
|
sample_from_interventional_drf(which_intervention=0, size=10, smoothed=True)
Draw from the trained and intervened upon DRF.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size |
int
|
Number of samples to be drawn. Defaults to 10. |
10
|
which_intervention |
str | int
|
Which intervention to choose from.
Both the literal name (see the property |
0
|
smoothed |
bool
|
If set to true, marginal distributions will be sampled from smoothed bootstraps. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: Data frame that follows the interventional distribution implied by the ground truth. |
Source code in causalAssembly/models_dag.py
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 |
|
save_drf(filename, location=None)
Writes a drf dict to file. Please provide the .pkl suffix!
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename |
str
|
name of the file to be written e.g. examplefile.pkl |
required |
location |
str
|
path to file in case it's not located in the current working directory. Defaults to None. |
None
|
Source code in causalAssembly/models_dag.py
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 |
|
show(meta_description=None, fig_size=(15, 8))
Plot full assembly line
Parameters:
Name | Type | Description | Default |
---|---|---|---|
meta_description |
list | None
|
Specify additional cell info. Defaults to None. |
None
|
fig_size |
tuple
|
Adjust depending on number of cells. Defaults to (15, 8). |
(15, 8)
|
Raises:
Type | Description |
---|---|
AssertionError
|
Meta list entry needs to exist for each cell! |
Source code in causalAssembly/models_dag.py
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 |
|
via_cell_number(n_cells, cell_prefix='C')
classmethod
Inits a ProductionLineGraph with predefined number of cells, e.g. n_cells = 3
Will create empty C0, C1 and C2 as cells if no other cell_prefix is given.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_cells |
int
|
Number of cells the graph will have |
required |
cell_prefix |
str
|
If you like other cell names pass them here. Defaults to "C". |
'C'
|
Source code in causalAssembly/models_dag.py
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 |
|
choose_edges_from_cells_randomly(from_cell, to_cell, probability, rng)
From two given cells (graphs), we take the cartesian product (end up with from_cell.number_of_nodes x to_cell.number_of_nodes possible edges (node tuples).
From this product we draw probability x cartesian product number of edges randomly.
In case we have a float number, we ceil the value, e.g. 17.3 edges will lead to 18 edges drawn.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
from_cell |
ProcessCell
|
ProcessCell from where we want the edges |
required |
to_cell |
ProcessCell
|
ProcessCell to where we want the edges |
required |
probability |
float
|
between 0 and 1 |
required |
Returns:
Type | Description |
---|---|
list[tuple[str, str]]
|
list[tuple[str, str]]: Chosen edges. |
Source code in causalAssembly/models_dag.py
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 |
|
get_arrow_head_candidates_from_graph(graph, node_attributes_to_filter=NodeAttributes.ALLOW_IN_EDGES)
Returns all arrow head (nodes where an arrow points to) nodes as list of candidates. To later build a list of tuples of potential edges.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph |
DiGraph
|
DAG |
required |
node_attributes_to_filter |
str
|
see NodeAttributes. Defaults to NodeAttributes.ALLOW_IN_EDGES. |
ALLOW_IN_EDGES
|
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: list of nodes |
Source code in causalAssembly/models_dag.py
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 |
|
Utility classes and functions related to causalAssembly. Copyright (c) 2023 Robert Bosch GmbH
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.
merge_dags(dag_to_insert, target_dag, mapping, remove_in_edges_in_target_dag=False)
Dag_to_insert will be connected to target_tag via mapping dict.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dag_to_insert |
DiGraph
|
DAG to insert. |
required |
target_dag |
DiGraph
|
DAG on which to map. |
required |
mapping |
dict
|
Mapping from insert to target dag e.g. {C1: D1, C5: D4} where node C1 from insert dag will be mapped to node D1 of target dag. |
required |
remove_in_edges_in_target_dag |
bool
|
Defaults to False. |
False
|
Raises:
Type | Description |
---|---|
ValueError
|
node does not exist in target_dag |
ValueError
|
node does not exist in dag_to_insert |
Returns: nx.DiGraph: merged DAG
Source code in causalAssembly/dag_utils.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 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 |
|
merge_dags_via_edges(left_dag, right_dag, edges=None, isolate_target_nodes=False)
Merges two dags via a list of edges.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
left_dag |
DiGraph
|
dag to merge to right_dag |
required |
right_dag |
DiGraph
|
dag to merge left_dag into |
required |
edges |
list[tuple]
|
list of edges that connect the two dags. Defaults to None. |
None
|
isolate_target_nodes |
bool
|
bool if True all incoming edges from the right_dag into the target node are removed: all influence from the left_dag, defined via edges list. Defaults to False. |
False
|
Raises:
Type | Description |
---|---|
ValueError
|
source or target nodes are not available in left dag |
ValueError
|
source or target nodes are not available in right dag |
Source code in causalAssembly/dag_utils.py
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 |
|
tuples_from_cartesian_product(l1, l2)
Given two lists l1 and l2 this creates the cartesian product and returns all tuples.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
l1 |
list
|
First list of nodes |
required |
l2 |
list
|
Second list of nodes typically |
required |
Returns:
Type | Description |
---|---|
list[tuple]
|
list[tuple]: list of edges typically |
Examples::
l1 = [0,1,2]
l2 = ['a','b','c']
>>> tuples_from_cartesian_product(l1,l2)
[(0,'a'), (0,'b'), (0,'c'), (1,'a'), (1,'b'), (1,'c'), (2,'a'), (2,'b'), (2,'c')]
Source code in causalAssembly/dag_utils.py
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 |
|
Utility classes and functions related to causalAssembly. Copyright (c) 2023 Robert Bosch GmbH
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.
FCM
Class to define, intervene and sample from an FCM.
Examples:
from sympy import symbols
from sympy.stats import Normal, Uniform, Gamma
x, y, z = symbols('x,y,z')
eq_x = Eq(x, Uniform("noise", left=-1, right=1))
eq_y = Eq(y, 2 * x ** 2 + Normal("error", 0, .5))
eq_z = Eq(z, 9 * y * x * Gamma("some_name", .5, .5))
eq_list = [eq_x, eq_y, eq_z]
self = FCM(name='test', seed=2023)
self.input_fcm(eq_list)
self.draw(size=10)
Source code in causalAssembly/models_fcm.py
30 31 32 33 34 35 36 37 38 39 40 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 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 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 672 673 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 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 |
|
causal_order: list[Symbol]
property
Returns the causal order of the current graph. Note that this order is in general not unique. To ensure uniqueness, we additionally sort lexicograpically.
Returns:
Type | Description |
---|---|
list[Symbol]
|
list[Symbol]: Causal order |
edges: list[tuple]
property
Edges in the graph.
Returns:
Type | Description |
---|---|
list[tuple]
|
list[tuple] |
ground_truth: pd.DataFrame
property
Returns the current ground truth as pandas adjacency.
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: Adjacenccy matrix. |
interventions: list
property
Returns all interventions performed on the original graph
Returns:
Name | Type | Description |
---|---|---|
list |
list
|
list of intervened upon nodes in do(x) notation. |
nodes: list[Symbol]
property
Nodes in the graph.
Returns:
Type | Description |
---|---|
list[Symbol]
|
list[str] |
num_edges: int
property
Number of edges in the graph
Returns:
Type | Description |
---|---|
int
|
int |
num_nodes: int
property
Number of nodes in the graph
Returns:
Type | Description |
---|---|
int
|
int |
source_nodes: list
property
Returns source nodes in the current DAG.
Returns:
Name | Type | Description |
---|---|---|
list |
list
|
List of source nodes. |
sparsity: float
property
Sparsity of the graph
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
in [0,1] |
__distribution_parameters_explicit(order, which_graph)
Returns true if distribution parameters are given explicitly, not symbolically.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
order |
node
|
node in graph |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
|
Source code in causalAssembly/models_fcm.py
571 572 573 574 575 576 577 578 579 580 581 582 583 |
|
__eval_expression(df, fcm_expr)
Eval given fcm_expression with the values in given dataframe
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
Data frame. |
required |
fcm_expr |
Expr
|
Sympy expression. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: Data frame after eval. |
Source code in causalAssembly/models_fcm.py
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 |
|
__source_df_condition(source_df)
Returns true if source_df colnames and graph nodenames agree.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source_df |
None | DataFrame
|
data frame containing source node data. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if names agree |
Source code in causalAssembly/models_fcm.py
585 586 587 588 589 590 591 592 593 594 595 596 |
|
causal_order_of(which_graph)
Returns the causal order of the chosen graph. Note that this order is in general not unique. To ensure uniqueness, we additionally sort lexicograpically.
Returns:
Type | Description |
---|---|
list[Symbol]
|
list[Symbol]: Causal order |
Source code in causalAssembly/models_fcm.py
200 201 202 203 204 205 206 207 208 |
|
display_functions()
Displays all functional assignments inputted into the FCM.
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
Dict with keys equal to nodes and values equal to functional assignments. |
Source code in causalAssembly/models_fcm.py
283 284 285 286 287 288 289 290 291 292 293 294 |
|
function_of(node)
Returns functional assignment for node in question.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node |
Symbol
|
node corresponding to lhs. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
key is node and value rhs of functional assignment. |
Source code in causalAssembly/models_fcm.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
|
input_fcm(fcm)
Automatically builds up DAG according to the FCM fed in.
Args:
fcm (list): list of sympy equations generated as:
[python]
x,y = symbols('x,y')
term_x = Eq(x, Normal('x', 0,1))
term_y = Eq(y, 2*x**2*Normal('noise', 0,1))
fcm = [term_x, term_y]
Source code in causalAssembly/models_fcm.py
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 |
|
intervene_on(nodes_values)
Specify hard or soft intervention. If you want to intervene
upon more than one node provide a list of nodes to intervene on
and a list of corresponding values to set these nodes to.
(see example). The mutilated dag will automatically be
stored in mutiliated_dags
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nodes_values |
dict[Symbol, RandomSymbol | float]
|
either single real number or RandmSymbol. If you provide more than one intervention just provide more key-value pairs. |
required |
Raises:
Type | Description |
---|---|
AssertionError
|
If node(s) are not in the graph |
Example
x,y = symbols("x,y")
eq_x = Eq(x, Gamma("source", 1,1))
eq_y = Eq(y, 4*x**3 + Uniform("noise", left=-0.5, right=0.5))
example_fcm = FCM()
example_fcm.input_fcm([eq_x,eq_y])
# Hard intervention
example_fcm.intervene_on(nodes_values = {y : 4})
# Soft intervention
example_fcm.intervene_on(nodes_values = {y : Normal("noise",0,1)})
Source code in causalAssembly/models_fcm.py
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 643 644 645 |
|
interventional_amat(which_intervention)
Returns the adjacency matrix of a chosen mutilated DAG.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
which_intervention |
int | str
|
Integer count of your chosen intervention or literal string. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
"The intervention you provide does not exist." |
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: Adjacency matrix. |
Source code in causalAssembly/models_fcm.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 |
|
interventional_sample(size, which_intervention=0, additive_gaussian_noise=False, snr=1 / 2, source_df=None)
Draw samples from the interventional distribution that factorizes according to the mutilated DAG after performing one or multiple interventions. Otherwise the method behaves similar to sampling from the non-interventional joint distribution. By default samples are drawn from the first intervention you performed. If you intervened upon more than one node, you'll have swith to another intervention for sampling from the corresponding interventional distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size |
int
|
Number of samples to draw. |
required |
which_intervention |
str | int
|
Which interventional distribution to draw
from. We recommend using integer counts starting from zero. But you can
also provide the literal string here, e.g. if you intervened on say two
nodes |
0
|
additive_gaussian_noise |
bool
|
This will attach additive Gaussian noise to all terms without a RandomSymbol that are not source nodes. It acts merely as a convenience option. Variance will then be chosen according to SNR. Defaults to False. |
False
|
snr |
None | float
|
Signal-to-noise ratio \( SNR = \frac{\text{Var}(\hat{X})}{\hat\sigma^2}. \). Defaults to 1/2. |
1 / 2
|
source_df |
None | DataFrame
|
Data frame containing source node data. The sample size must be at least as large as the number of samples you'd like to draw. Defaults to None. |
None
|
Raises:
Type | Description |
---|---|
NotImplementedError
|
Raised when |
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: Data frame with rows of length |
Source code in causalAssembly/models_fcm.py
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 |
|
parents(of_node)
Return parents of node in question.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
of_node |
str
|
Node in question. |
required |
Returns:
Type | Description |
---|---|
list[Symbol]
|
list[str]: parent set. |
Source code in causalAssembly/models_fcm.py
177 178 179 180 181 182 183 184 185 186 |
|
parents_of(node, which_graph)
Return parents of node in question for a chosen DAG.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node |
Symbol
|
node whose parents to return. |
required |
which_graph |
DiGraph
|
which graph along the interventions. |
required |
Returns:
Type | Description |
---|---|
list[Symbol]
|
list[Symbol]: list of parents. |
Source code in causalAssembly/models_fcm.py
188 189 190 191 192 193 194 195 196 197 198 |
|
sample(size, additive_gaussian_noise=False, snr=1 / 2, source_df=None)
Draw samples from the joint distribution that factorizes according to the DAG implied by the FCM fed in. To avoid unexpected/unintended behavior, avoid defining fully deterministic equation systems. If parameters in noise terms are additive and left unevaluated, they're set according to a chosen Signal-To-Noise (SNR) ratio. For convenience, you can add additive Gaussian noise to each equation. This will never overwrite any of the chosen noise distributions. You may also feed in a data frame for noise distributions (see below for more details).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size |
int
|
Number of samples to draw. |
required |
additive_gaussian_noise |
bool
|
This will attach additive Gaussian noise to all terms without a RandomSymbol that are not source nodes. It acts merely as a convenience option. Variance will then be chosen according to SNR. Defaults to False. |
False
|
snr |
None | float
|
Signal-to-noise ratio \( SNR = \frac{\text{Var}(\hat{X})}{\hat\sigma^2}. \). Defaults to 1/2. |
1 / 2
|
source_df |
None | DataFrame
|
Data frame containing source node data. The sample size must be at least as large as the number of samples you'd like to draw. Defaults to None. |
None
|
Raises:
Type | Description |
---|---|
AttributeError
|
if source node parameters are not given explicitly. |
ValueError
|
if source node sample size is too small. |
ValueError
|
if scale parameters are left unevaluated for non-additive terms. |
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: Data frame with rows of length |
Source code in causalAssembly/models_fcm.py
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 |
|
show(header=None, with_nodenames=True)
Plots the current DAG.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
header |
str | None
|
Header for the DAG. Defaults to None. |
None
|
with_nodenames |
bool
|
Whether or not to use nodenames as labels in the plot. Defaults to True. |
True
|
Returns:
Name | Type | Description |
---|---|---|
plt |
matplotlib
|
Plot of the DAG. |
Source code in causalAssembly/models_fcm.py
647 648 649 650 651 652 653 654 655 656 657 658 659 660 |
|
show_mutilated_dag(which_intervention=0, with_nodenames=True)
Plot mutilated DAG
Parameters:
Name | Type | Description | Default |
---|---|---|---|
which_intervention |
str | int
|
Which interventional distribution should be represented by a DAG. Defaults to 0. |
0
|
with_nodenames |
bool
|
Whether or not to use nodenames as labels in the plot. Defaults to True. |
True
|
Returns:
Name | Type | Description |
---|---|---|
plt |
matplotlib
|
Plot of the mutilated DAG. |
Source code in causalAssembly/models_fcm.py
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 |
|
source_nodes_of(which_graph)
Returns the source nodes of a chosen graph. This is mainly for choosing different mutilated DAGs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
which_graph |
DiGraph
|
DAG from which source nodes should be returned. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
list
|
List of nodes. |
Source code in causalAssembly/models_fcm.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
|
Utility classes and functions related to causalAssembly. Copyright (c) 2023 Robert Bosch GmbH
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.
DAGmetrics
Class to calculate performance metrics for DAGs.
Make sure that the ground truth and the estimated DAG have the same order of
rows/columns. If these objects are nx.DiGraphs, make sure that graph.nodes()
have the same oder or pass a new nodelist to the class when initiating. The
same can be done for pd.DataFrames. In case truth
and est
are np.ndarray
objects it is the users responsibility to make sure that the objects are
indeed comparable.
Source code in causalAssembly/metrics.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 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 |
|
collect_metrics()
Collects all metrics defined in this class in a dict.
Returns:
Type | Description |
---|---|
dict[str, float | int]
|
dict[str, float|int]: Metrics calculated |
Source code in causalAssembly/metrics.py
90 91 92 93 94 95 96 97 98 |
|
Utility classes and functions related to causalAssembly. Copyright (c) 2023 Robert Bosch GmbH
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.
DRF
Wrapper around the corresponding R package: Distributional Random Forests (Cevid et al., 2020). Closely adopted from their python wrapper.
Source code in causalAssembly/drf_fitting.py
34 35 36 37 38 39 40 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 |
|
fit(X, Y)
Fit DRF in order to estimate conditional distribution P(Y|X=x).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
DataFrame
|
Conditioning set. |
required |
Y |
DataFrame
|
Variable of interest (can be vector-valued). |
required |
Source code in causalAssembly/drf_fitting.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
produce_sample(newdata, random_state, n=1)
Sample data from fitted drf.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
newdata |
DataFrame
|
Data samples to predict from. |
required |
random_state |
Generator
|
control random state. |
required |
n |
int
|
Number of n-samples to draw. Defaults to 1. |
1
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: New predicted samlpe of Y. |
Source code in causalAssembly/drf_fitting.py
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 |
|
fit_drf(graph, data)
Fit distributional random forests to the factorization implied by the current graph Args: data (pd.DataFrame): Columns of dataframe need to match name and order of the graph
Raises:
Type | Description |
---|---|
ValueError
|
Raises error if columns don't meet this requirement |
Returns:
Type | Description |
---|---|
dict
|
dict of fitted DRFs. |
Source code in causalAssembly/drf_fitting.py
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 |
|
Utility classes and functions related to causalAssembly. Copyright (c) 2023 Robert Bosch GmbH
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.
PDAG
Class for dealing with partially directed graph i.e. graphs that contain both directed and undirected edges.
Source code in causalAssembly/pdag.py
29 30 31 32 33 34 35 36 37 38 39 40 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 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 |
|
adjacency_matrix: pd.DataFrame
property
Returns adjacency matrix where the i,jth entry being one indicates that there is an edge from i to j. A zero indicates that there is no edge.
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: adjacency matrix |
dir_edges: list[tuple]
property
Gives all directed edges in current PDAG.
Returns:
Type | Description |
---|---|
list[tuple]
|
list[tuple]: List of directed edges. |
nnodes: int
property
Number of nodes in current PDAG.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
Number of nodes |
nodes: list
property
Get all nods in current PDAG.
Returns:
Name | Type | Description |
---|---|---|
list |
list
|
list of nodes. |
num_adjacencies: int
property
Number of adjacent nodes in current PDAG.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
Number of adjacent nodes |
num_dir_edges: int
property
Number of directed edges in current PDAG.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
Number of directed edges |
num_undir_edges: int
property
Number of undirected edges in current PDAG.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
Number of undirected edges |
undir_edges: list[tuple]
property
Gives all undirected edges in current PDAG.
Returns:
Type | Description |
---|---|
list[tuple]
|
list[tuple]: List of undirected edges. |
children(node)
Gives all children of node node
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node |
str
|
node in current PDAG. |
required |
Returns:
Name | Type | Description |
---|---|---|
set |
set
|
set of children. |
Source code in causalAssembly/pdag.py
83 84 85 86 87 88 89 90 91 92 93 94 95 |
|
copy()
Return a copy of the graph
Source code in causalAssembly/pdag.py
391 392 393 |
|
from_pandas_adjacency(pd_amat)
classmethod
Build PDAG from a Pandas adjacency matrix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pd_amat |
DataFrame
|
input adjacency matrix. |
required |
Returns:
Type | Description |
---|---|
PDAG
|
PDAG |
Source code in causalAssembly/pdag.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 |
|
is_adjacent(i, j)
Return True if the graph contains an directed or undirected edge between i and j.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
i |
str
|
node i. |
required |
j |
str
|
node j. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if i-j or i->j or i<-j |
Source code in causalAssembly/pdag.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
|
is_clique(potential_clique)
Check every pair of node X potential_clique is adjacent.
Source code in causalAssembly/pdag.py
158 159 160 161 162 |
|
neighbors(node)
Gives all neighbors of node node
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node |
str
|
node in current PDAG. |
required |
Returns:
Name | Type | Description |
---|---|---|
set |
set
|
set of neighbors. |
Source code in causalAssembly/pdag.py
111 112 113 114 115 116 117 118 119 120 121 122 123 |
|
parents(node)
Gives all parents of node node
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node |
str
|
node in current PDAG. |
required |
Returns:
Name | Type | Description |
---|---|---|
set |
set
|
set of parents. |
Source code in causalAssembly/pdag.py
97 98 99 100 101 102 103 104 105 106 107 108 109 |
|
remove_edge(i, j)
Removes edge in question
Parameters:
Name | Type | Description | Default |
---|---|---|---|
i |
str
|
tail |
required |
j |
str
|
head |
required |
Raises:
Type | Description |
---|---|
AssertionError
|
if edge does not exist |
Source code in causalAssembly/pdag.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
|
remove_node(node)
Remove a node from the graph
Source code in causalAssembly/pdag.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
|
show()
Plot PDAG.
Source code in causalAssembly/pdag.py
395 396 397 398 399 |
|
to_allDAGs()
Recursion algorithm which recursively applies the following steps: 1. Orient the first undirected edge found. 2. Apply Meek rules. 3. Recurse with each direction of the oriented edge. This corresponds to Algorithm 2 in Wienöbst et al. (2023).
References
Wienöbst, Marcel, et al. "Efficient enumeration of Markov equivalent DAGs." Proceedings of the AAAI Conference on Artificial Intelligence. Vol. 37. No. 10. 2023.
Source code in causalAssembly/pdag.py
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
|
to_dag()
Algorithm as described in Chickering (2002):
1. From PDAG P create DAG G containing all directed edges from P
2. Repeat the following: Select node v in P s.t.
i. v has no outgoing edges (children) i.e. \(ch(v) = \emptyset \)
ii. \(neigh(v) \neq \emptyset\)
Then \( (pa(v) \cup (neigh(v) \) form a clique.
For each v that is in a clique and is part of an undirected edge in P
i.e. w - v, insert a directed edge w -> v in G.
Remove v and all incident edges from P and continue with next node.
Until all nodes have been deleted from P.
Returns:
Type | Description |
---|---|
DiGraph
|
nx.DiGraph: DAG that belongs to the MEC implied by the PDAG |
Source code in causalAssembly/pdag.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 318 319 320 321 322 323 |
|
to_networkx()
Convert to networkx graph.
Returns:
Type | Description |
---|---|
MultiDiGraph
|
nx.MultiDiGraph: Graph with directed and undirected edges. |
Source code in causalAssembly/pdag.py
401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
|
to_random_dag()
Provides a random DAG residing in the MEC.
Returns:
Type | Description |
---|---|
DiGraph
|
nx.DiGraph: random DAG living in MEC |
Source code in causalAssembly/pdag.py
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 |
|
undir_neighbors(node)
Gives all undirected neighbors
of node node
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node |
str
|
node in current PDAG. |
required |
Returns:
Name | Type | Description |
---|---|---|
set |
set
|
set of undirected neighbors. |
Source code in causalAssembly/pdag.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
|
undir_to_dir_edge(tail, head)
Takes a undirected edge and turns it into a directed one. tail indicates the starting node of the edge and head the end node, i.e. tail -> head.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tail |
str
|
starting node |
required |
head |
str
|
end node |
required |
Raises:
Type | Description |
---|---|
AssertionError
|
if edge does not exist or is not undirected. |
Source code in causalAssembly/pdag.py
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 |
|
vstructs()
Retrieve v-structures
Returns:
Name | Type | Description |
---|---|---|
set |
set
|
set of all v-structures |
Source code in causalAssembly/pdag.py
377 378 379 380 381 382 383 384 385 386 387 388 389 |
|
dag2cpdag(dag)
Convertes a DAG into its unique CPDAG
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dag |
DiGraph
|
DAG the CPDAG corresponds to. |
required |
Returns:
Name | Type | Description |
---|---|---|
PDAG |
PDAG
|
unique CPDAG |
Source code in causalAssembly/pdag.py
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 |
|
rule_1(pdag)
Given the following pattern X -> Y - Z. Orient Y - Z to Y -> Z if X and Z are non-adjacent (otherwise a new v-structure arises).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pdag |
PDAG
|
PDAG before application of rule. |
required |
Returns:
Name | Type | Description |
---|---|---|
PDAG |
PDAG
|
PDAG after application of rule. |
Source code in causalAssembly/pdag.py
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 |
|
rule_2(pdag)
Given the following directed triple X -> Y -> Z where X - Z are indeed adjacent. Orient X - Z to X -> Z otherwise a cycle arises.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pdag |
PDAG
|
PDAG before application of rule. |
required |
Returns:
Name | Type | Description |
---|---|---|
PDAG |
PDAG
|
PDAG after application of rule. |
Source code in causalAssembly/pdag.py
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 |
|
rule_3(pdag)
Orient X - Z to X -> Z, whenever there are two triples X - Y1 -> Z and X - Y2 -> Z such that Y1 and Y2 are non-adjacent.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pdag |
PDAG
|
PDAG before application of rule. |
required |
Returns:
Name | Type | Description |
---|---|---|
PDAG |
PDAG
|
PDAG after application of rule. |
Source code in causalAssembly/pdag.py
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 |
|
rule_4(pdag)
Orient X - Y1 to X -> Y1, whenever there are two triples with X - Z and X - Y1 <- Z and X - Y2 -> Z such that Y1 and Y2 are non-adjacent.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pdag |
PDAG
|
PDAG before application of rule. |
required |
Returns:
Name | Type | Description |
---|---|---|
PDAG |
PDAG
|
PDAG after application of rule. |
Source code in causalAssembly/pdag.py
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 |
|
vstructs(dag)
Retrieve all v-structures in a DAG.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dag |
DiGraph
|
DAG in question |
required |
Returns:
Name | Type | Description |
---|---|---|
set |
set
|
Set of all v-structures. |
Source code in causalAssembly/pdag.py
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 |
|
DAG class
DAG
General class for dealing with directed acyclic graph i.e. graphs that are directed and must not contain any cycles.
Source code in causalAssembly/dag.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 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 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 |
|
adjacency_matrix: pd.DataFrame
property
Returns adjacency matrix where the i,jth entry being one indicates that there is an edge from i to j. A zero indicates that there is no edge.
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: adjacency matrix |
causal_order: list[str]
property
Returns the causal order of the current graph. Note that this order is in general not unique.
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: Causal order |
edges: list[tuple]
property
Gives all directed edges in current DAG.
Returns:
Type | Description |
---|---|
list[tuple]
|
list[tuple]: List of directed edges. |
max_in_degree: int
property
Maximum in-degree of the graph.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
Maximum in-degree |
max_out_degree: int
property
Maximum out-degree of the graph.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
Maximum out-degree |
nodes: list
property
Get all nods in current DAG.
Returns:
Name | Type | Description |
---|---|---|
list |
list
|
list of nodes. |
num_edges: int
property
Number of directed edges in current DAG.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
Number of directed edges |
num_nodes: int
property
Number of nodes in current DAG.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
Number of nodes |
sparsity: float
property
Sparsity of the graph
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
in [0,1] |
add_edge(edge)
Add edge to DAG
Parameters:
Name | Type | Description | Default |
---|---|---|---|
edge |
tuple[str, str]
|
Edge to add |
required |
Source code in causalAssembly/dag.py
74 75 76 77 78 79 80 |
|
add_edges_from(edges)
Add multiple edges to DAG
Parameters:
Name | Type | Description | Default |
---|---|---|---|
edges |
list[tuple[str, str]]
|
Edges to add |
required |
Source code in causalAssembly/dag.py
82 83 84 85 86 87 88 89 |
|
children(of_node)
Gives all children of node of_node
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
of_node |
str
|
node in current DAG. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
list[str]
|
of children. |
Source code in causalAssembly/dag.py
91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
copy()
Return a copy of the graph
Source code in causalAssembly/dag.py
248 249 250 |
|
from_nx(nx_dag)
classmethod
Convert to DAG from nx.DiGraph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nx_dag |
DiGraph
|
DAG in question. |
required |
Raises:
Type | Description |
---|---|
TypeError
|
If DAG is not nx.DiGraph |
Returns:
Type | Description |
---|---|
DAG
|
DAG |
Source code in causalAssembly/dag.py
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
|
from_pandas_adjacency(pd_amat)
classmethod
Build DAG from a Pandas adjacency matrix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pd_amat |
DataFrame
|
input adjacency matrix. |
required |
Returns:
Type | Description |
---|---|
DAG
|
DAG |
Source code in causalAssembly/dag.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
|
induced_subgraph(nodes)
Returns the induced subgraph on the nodes in nodes
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nodes |
list[str]
|
List of nodes. |
required |
Returns:
Name | Type | Description |
---|---|---|
DAG |
DAG
|
Induced subgraph. |
Source code in causalAssembly/dag.py
119 120 121 122 123 124 125 126 127 128 129 |
|
is_acyclic()
Check if the graph is acyclic.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if graph is acyclic. |
Source code in causalAssembly/dag.py
150 151 152 153 154 155 156 157 |
|
is_adjacent(i, j)
Return True if the graph contains an directed edge between i and j.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
i |
str
|
node i. |
required |
j |
str
|
node j. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if i->j or i<-j |
Source code in causalAssembly/dag.py
131 132 133 134 135 136 137 138 139 140 141 142 |
|
is_clique(potential_clique)
Check every pair of node X potential_clique is adjacent.
Source code in causalAssembly/dag.py
144 145 146 147 148 |
|
load_drf(filename, location=None)
classmethod
Loads a drf dict from a .pkl file into the workspace.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename |
str
|
name of the file e.g. examplefile.pkl |
required |
location |
str
|
path to file in case it's not located in the current working directory. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
DRF |
dict
|
dict of trained drf objects |
Source code in causalAssembly/dag.py
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
|
parents(of_node)
Gives all parents of node of_node
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
of_node |
str
|
node in current DAG. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
list[str]
|
of parents. |
Source code in causalAssembly/dag.py
105 106 107 108 109 110 111 112 113 114 115 116 117 |
|
remove_edge(i, j)
Removes edge in question
Parameters:
Name | Type | Description | Default |
---|---|---|---|
i |
str
|
tail |
required |
j |
str
|
head |
required |
Raises:
Type | Description |
---|---|
AssertionError
|
if edge does not exist |
Source code in causalAssembly/dag.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
|
remove_node(node)
Remove a node from the graph
Source code in causalAssembly/dag.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
|
sample_from_drf(size=10, smoothed=True)
Draw from the trained DRF.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size |
int
|
Number of samples to be drawn. Defaults to 10. |
10
|
smoothed |
bool
|
If set to true, marginal distributions will be sampled from smoothed bootstraps. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: Data frame that follows the distribution implied by the ground truth. |
Source code in causalAssembly/dag.py
380 381 382 383 384 385 386 387 388 389 390 391 |
|
save_drf(filename, location=None)
Writes a drf dict to file. Please provide the .pkl suffix!
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename |
str
|
name of the file to be written e.g. examplefile.pkl |
required |
location |
str
|
path to file in case it's not located in the current working directory. Defaults to None. |
None
|
Source code in causalAssembly/dag.py
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
|
show()
Plot DAG.
Source code in causalAssembly/dag.py
252 253 254 255 256 |
|
to_networkx()
Convert to networkx graph.
Returns:
Type | Description |
---|---|
DiGraph
|
nx.DiGraph: DAG. |
Source code in causalAssembly/dag.py
258 259 260 261 262 263 264 265 266 267 268 |
|
vstructs()
Retrieve v-structures
Returns:
Name | Type | Description |
---|---|---|
set |
set
|
set of all v-structures |
Source code in causalAssembly/dag.py
234 235 236 237 238 239 240 241 242 243 244 245 246 |
|