Spaces:
Sleeping
Sleeping
File size: 49,414 Bytes
8097001 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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 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 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 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 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Handling Event Data\n",
"*by: Sebastiaan J. van Zelst*"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"Process mining exploits Event Logs to generate knowledge of a process.\n",
"A wide variety of information systems, e.g., SAP, ORACLE, SalesForce, etc., allow us to extract, in one way or the other,\n",
"event logs similar to the example event logs.\n",
"All the examples we show in this notebook and all algorithms implemented in pm4py assume that we have already extracted\n",
"the event data into an appropriate event log format.\n",
"Hence, the core of pm4py does not support any data extraction features.\n",
"\n",
"In order to support interoperability between different process mining tools and libraries, two standard data formats are\n",
"used to capture event logs, i.e., Comma Separated Value (CSV) files and eXtensible Event Stream (XES) files.\n",
"CSV files resemble the example tables shown in the previous section, i.e., Table 1 and Table 2. Each line in such a file\n",
"describes an event that occurred. The columns represent the same type of data, as shown in the examples, e.g., the case\n",
"for which the event occurred, the activity, the timestamp, the resource executing the activity, etc.\n",
"The XES file format is an XML-based format that allows us to describe process behavior.\n",
"We will not go into specific details w.r.t. the format of XES files, i.e., we refer to http://xes-standard.org/ for an\n",
"overview.\n",
"\n",
"In this tutorial, we will use an oftenly used dummy example event log to explain the basic process mining operations.\n",
"The process that we are considering is a simplified process related to customer complaint handling, i.e., taken from the\n",
"book of van der Aalst (https://www.springer.com/de/book/9783662498507). The process, and the event data we are going to\n",
"use, looks as follows."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Importing CSV Files"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"Let’s get started!\n",
"We have prepared a small sample event log, containing behavior similar equal to the process model in Figure 3.\n",
"You can find the sample event log [here](data/running_example.csv).\n",
"\n",
"We are going to load the event data, and, we are going to count how many cases are present in the event log, as well as\n",
"the number of events. Note that, for all this, we are effectively using a third-party library called pandas.\n",
"We do so because pandas is the de-facto standard of loading/manipulating csv-based data.\n",
"Hence, any process mining algorithm implemented in pm4py, using an event log as an input, can work directly with a\n",
"pandas file!\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"pycharm": {
"name": "#%%\n"
},
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"text/plain": " case_id activity timestamp costs org:resource\n0 3 register request 2010-12-30 14:32:00+01:00 50 Pete\n1 3 examine casually 2010-12-30 15:06:00+01:00 400 Mike\n2 3 check ticket 2010-12-30 16:34:00+01:00 100 Ellen\n3 3 decide 2011-01-06 09:18:00+01:00 200 Sara\n4 3 reinitiate request 2011-01-06 12:18:00+01:00 200 Sara\n5 3 examine thoroughly 2011-01-06 13:06:00+01:00 400 Sean\n6 3 check ticket 2011-01-08 11:43:00+01:00 100 Pete\n7 3 decide 2011-01-09 09:55:00+01:00 200 Sara\n8 3 pay compensation 2011-01-15 10:45:00+01:00 200 Ellen\n9 2 register request 2010-12-30 11:32:00+01:00 50 Mike\n10 2 check ticket 2010-12-30 12:12:00+01:00 100 Mike\n11 2 examine casually 2010-12-30 14:16:00+01:00 400 Sean\n12 2 decide 2011-01-05 11:22:00+01:00 200 Sara\n13 2 pay compensation 2011-01-08 12:05:00+01:00 200 Ellen\n14 1 register request 2010-12-30 11:02:00+01:00 50 Pete\n15 1 examine thoroughly 2010-12-31 10:06:00+01:00 400 Sue\n16 1 check ticket 2011-01-05 15:12:00+01:00 100 Mike\n17 1 decide 2011-01-06 11:18:00+01:00 200 Sara\n18 1 reject request 2011-01-07 14:24:00+01:00 200 Pete\n19 6 register request 2011-01-06 15:02:00+01:00 50 Mike\n20 6 examine casually 2011-01-06 16:06:00+01:00 400 Ellen\n21 6 check ticket 2011-01-07 16:22:00+01:00 100 Mike\n22 6 decide 2011-01-07 16:52:00+01:00 200 Sara\n23 6 pay compensation 2011-01-16 11:47:00+01:00 200 Mike\n24 5 register request 2011-01-06 09:02:00+01:00 50 Ellen\n25 5 examine casually 2011-01-07 10:16:00+01:00 400 Mike\n26 5 check ticket 2011-01-08 11:22:00+01:00 100 Pete\n27 5 decide 2011-01-10 13:28:00+01:00 200 Sara\n28 5 reinitiate request 2011-01-11 16:18:00+01:00 200 Sara\n29 5 check ticket 2011-01-14 14:33:00+01:00 100 Ellen\n30 5 examine casually 2011-01-16 15:50:00+01:00 400 Mike\n31 5 decide 2011-01-19 11:18:00+01:00 200 Sara\n32 5 reinitiate request 2011-01-20 12:48:00+01:00 200 Sara\n33 5 examine casually 2011-01-21 09:06:00+01:00 400 Sue\n34 5 check ticket 2011-01-21 11:34:00+01:00 100 Pete\n35 5 decide 2011-01-23 13:12:00+01:00 200 Sara\n36 5 reject request 2011-01-24 14:56:00+01:00 200 Mike\n37 4 register request 2011-01-06 15:02:00+01:00 50 Pete\n38 4 check ticket 2011-01-07 12:06:00+01:00 100 Mike\n39 4 examine thoroughly 2011-01-08 14:43:00+01:00 400 Sean\n40 4 decide 2011-01-09 12:02:00+01:00 200 Sara\n41 4 reject request 2011-01-12 15:44:00+01:00 200 Ellen",
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>case_id</th>\n <th>activity</th>\n <th>timestamp</th>\n <th>costs</th>\n <th>org:resource</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>3</td>\n <td>register request</td>\n <td>2010-12-30 14:32:00+01:00</td>\n <td>50</td>\n <td>Pete</td>\n </tr>\n <tr>\n <th>1</th>\n <td>3</td>\n <td>examine casually</td>\n <td>2010-12-30 15:06:00+01:00</td>\n <td>400</td>\n <td>Mike</td>\n </tr>\n <tr>\n <th>2</th>\n <td>3</td>\n <td>check ticket</td>\n <td>2010-12-30 16:34:00+01:00</td>\n <td>100</td>\n <td>Ellen</td>\n </tr>\n <tr>\n <th>3</th>\n <td>3</td>\n <td>decide</td>\n <td>2011-01-06 09:18:00+01:00</td>\n <td>200</td>\n <td>Sara</td>\n </tr>\n <tr>\n <th>4</th>\n <td>3</td>\n <td>reinitiate request</td>\n <td>2011-01-06 12:18:00+01:00</td>\n <td>200</td>\n <td>Sara</td>\n </tr>\n <tr>\n <th>5</th>\n <td>3</td>\n <td>examine thoroughly</td>\n <td>2011-01-06 13:06:00+01:00</td>\n <td>400</td>\n <td>Sean</td>\n </tr>\n <tr>\n <th>6</th>\n <td>3</td>\n <td>check ticket</td>\n <td>2011-01-08 11:43:00+01:00</td>\n <td>100</td>\n <td>Pete</td>\n </tr>\n <tr>\n <th>7</th>\n <td>3</td>\n <td>decide</td>\n <td>2011-01-09 09:55:00+01:00</td>\n <td>200</td>\n <td>Sara</td>\n </tr>\n <tr>\n <th>8</th>\n <td>3</td>\n <td>pay compensation</td>\n <td>2011-01-15 10:45:00+01:00</td>\n <td>200</td>\n <td>Ellen</td>\n </tr>\n <tr>\n <th>9</th>\n <td>2</td>\n <td>register request</td>\n <td>2010-12-30 11:32:00+01:00</td>\n <td>50</td>\n <td>Mike</td>\n </tr>\n <tr>\n <th>10</th>\n <td>2</td>\n <td>check ticket</td>\n <td>2010-12-30 12:12:00+01:00</td>\n <td>100</td>\n <td>Mike</td>\n </tr>\n <tr>\n <th>11</th>\n <td>2</td>\n <td>examine casually</td>\n <td>2010-12-30 14:16:00+01:00</td>\n <td>400</td>\n <td>Sean</td>\n </tr>\n <tr>\n <th>12</th>\n <td>2</td>\n <td>decide</td>\n <td>2011-01-05 11:22:00+01:00</td>\n <td>200</td>\n <td>Sara</td>\n </tr>\n <tr>\n <th>13</th>\n <td>2</td>\n <td>pay compensation</td>\n <td>2011-01-08 12:05:00+01:00</td>\n <td>200</td>\n <td>Ellen</td>\n </tr>\n <tr>\n <th>14</th>\n <td>1</td>\n <td>register request</td>\n <td>2010-12-30 11:02:00+01:00</td>\n <td>50</td>\n <td>Pete</td>\n </tr>\n <tr>\n <th>15</th>\n <td>1</td>\n <td>examine thoroughly</td>\n <td>2010-12-31 10:06:00+01:00</td>\n <td>400</td>\n <td>Sue</td>\n </tr>\n <tr>\n <th>16</th>\n <td>1</td>\n <td>check ticket</td>\n <td>2011-01-05 15:12:00+01:00</td>\n <td>100</td>\n <td>Mike</td>\n </tr>\n <tr>\n <th>17</th>\n <td>1</td>\n <td>decide</td>\n <td>2011-01-06 11:18:00+01:00</td>\n <td>200</td>\n <td>Sara</td>\n </tr>\n <tr>\n <th>18</th>\n <td>1</td>\n <td>reject request</td>\n <td>2011-01-07 14:24:00+01:00</td>\n <td>200</td>\n <td>Pete</td>\n </tr>\n <tr>\n <th>19</th>\n <td>6</td>\n <td>register request</td>\n <td>2011-01-06 15:02:00+01:00</td>\n <td>50</td>\n <td>Mike</td>\n </tr>\n <tr>\n <th>20</th>\n <td>6</td>\n <td>examine casually</td>\n <td>2011-01-06 16:06:00+01:00</td>\n <td>400</td>\n <td>Ellen</td>\n </tr>\n <tr>\n <th>21</th>\n <td>6</td>\n <td>check ticket</td>\n <td>2011-01-07 16:22:00+01:00</td>\n <td>100</td>\n <td>Mike</td>\n </tr>\n <tr>\n <th>22</th>\n <td>6</td>\n <td>decide</td>\n <td>2011-01-07 16:52:00+01:00</td>\n <td>200</td>\n <td>Sara</td>\n </tr>\n <tr>\n <th>23</th>\n <td>6</td>\n <td>pay compensation</td>\n <td>2011-01-16 11:47:00+01:00</td>\n <td>200</td>\n <td>Mike</td>\n </tr>\n <tr>\n <th>24</th>\n <td>5</td>\n <td>register request</td>\n <td>2011-01-06 09:02:00+01:00</td>\n <td>50</td>\n <td>Ellen</td>\n </tr>\n <tr>\n <th>25</th>\n <td>5</td>\n <td>examine casually</td>\n <td>2011-01-07 10:16:00+01:00</td>\n <td>400</td>\n <td>Mike</td>\n </tr>\n <tr>\n <th>26</th>\n <td>5</td>\n <td>check ticket</td>\n <td>2011-01-08 11:22:00+01:00</td>\n <td>100</td>\n <td>Pete</td>\n </tr>\n <tr>\n <th>27</th>\n <td>5</td>\n <td>decide</td>\n <td>2011-01-10 13:28:00+01:00</td>\n <td>200</td>\n <td>Sara</td>\n </tr>\n <tr>\n <th>28</th>\n <td>5</td>\n <td>reinitiate request</td>\n <td>2011-01-11 16:18:00+01:00</td>\n <td>200</td>\n <td>Sara</td>\n </tr>\n <tr>\n <th>29</th>\n <td>5</td>\n <td>check ticket</td>\n <td>2011-01-14 14:33:00+01:00</td>\n <td>100</td>\n <td>Ellen</td>\n </tr>\n <tr>\n <th>30</th>\n <td>5</td>\n <td>examine casually</td>\n <td>2011-01-16 15:50:00+01:00</td>\n <td>400</td>\n <td>Mike</td>\n </tr>\n <tr>\n <th>31</th>\n <td>5</td>\n <td>decide</td>\n <td>2011-01-19 11:18:00+01:00</td>\n <td>200</td>\n <td>Sara</td>\n </tr>\n <tr>\n <th>32</th>\n <td>5</td>\n <td>reinitiate request</td>\n <td>2011-01-20 12:48:00+01:00</td>\n <td>200</td>\n <td>Sara</td>\n </tr>\n <tr>\n <th>33</th>\n <td>5</td>\n <td>examine casually</td>\n <td>2011-01-21 09:06:00+01:00</td>\n <td>400</td>\n <td>Sue</td>\n </tr>\n <tr>\n <th>34</th>\n <td>5</td>\n <td>check ticket</td>\n <td>2011-01-21 11:34:00+01:00</td>\n <td>100</td>\n <td>Pete</td>\n </tr>\n <tr>\n <th>35</th>\n <td>5</td>\n <td>decide</td>\n <td>2011-01-23 13:12:00+01:00</td>\n <td>200</td>\n <td>Sara</td>\n </tr>\n <tr>\n <th>36</th>\n <td>5</td>\n <td>reject request</td>\n <td>2011-01-24 14:56:00+01:00</td>\n <td>200</td>\n <td>Mike</td>\n </tr>\n <tr>\n <th>37</th>\n <td>4</td>\n <td>register request</td>\n <td>2011-01-06 15:02:00+01:00</td>\n <td>50</td>\n <td>Pete</td>\n </tr>\n <tr>\n <th>38</th>\n <td>4</td>\n <td>check ticket</td>\n <td>2011-01-07 12:06:00+01:00</td>\n <td>100</td>\n <td>Mike</td>\n </tr>\n <tr>\n <th>39</th>\n <td>4</td>\n <td>examine thoroughly</td>\n <td>2011-01-08 14:43:00+01:00</td>\n <td>400</td>\n <td>Sean</td>\n </tr>\n <tr>\n <th>40</th>\n <td>4</td>\n <td>decide</td>\n <td>2011-01-09 12:02:00+01:00</td>\n <td>200</td>\n <td>Sara</td>\n </tr>\n <tr>\n <th>41</th>\n <td>4</td>\n <td>reject request</td>\n <td>2011-01-12 15:44:00+01:00</td>\n <td>200</td>\n <td>Ellen</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"\n",
"df = pd.read_csv('data/running_example.csv', sep=';')\n",
"df"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"Let's inspect the small event log.\n",
"The first line (i.e., row) specifies the name of each column (i.e., event attribute).\n",
"Observe that, in the data table described by the file, we have 5 columns, being: *case_id*, *activity*,\n",
"*timestamp*, *costs* and *org:resource*.\n",
"The first column represents the *case identifier*, i.e., allowing us to identify what activity has been logged in the\n",
"context of what instance of the process.\n",
"The second column (*activity*) records the activity that has been performed.\n",
"The third column shows at what point in time the activity was recorded (*timestamp*).\n",
"In this example data, additional information is present as well.\n",
"In this case, the fourth column tracks the costs of the activity (*costs* attribute), whereas the fifth row tracks what\n",
"resource has performed the activity (*org:resource*).\n",
"\n",
"Observe that, row 2-10 show the events that have been recorded for the process identified by *case identifier* 3.\n",
"We observe that first a register request activity was performed, followed by the examine casually, check ticket, decide,\n",
"reinitiate request, examine thoroughly, check ticket,decide, and finally, pay compensation activities.\n",
"Note that, in this case, the recorded process instance behaves as described by the model depicted in Figure 3.\n",
"\n",
"Let's investigate some basic statistics of our log, e.g., the total number of cases described and the total number of events."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"pycharm": {
"name": "#%%\n"
},
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# number of cases\n",
"len(df['case_id'].unique())"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"pycharm": {
"name": "#%%\n"
},
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"42"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# number of events\n",
"len(df)\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Formatting Data Frames"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"Now we have loaded our first event log, it is time to put some pm4py into the mix.\n",
"pm4py uses standardized column names to represent the *case identifier*, the *activity name* and the timstamp.\n",
"These are, respectively, ```case:concept:name```, ```concept:name``` and ```time:timestamp```.\n",
"Hence, to make pm4py work with the provided csv file, we need to rename the ```case_id```, ```activity``` and ```timestamp``` columns.\n",
"pm4py provides a dedicated utility function for this:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"pycharm": {
"name": "#%%\n"
},
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>case:concept:name</th>\n",
" <th>concept:name</th>\n",
" <th>time:timestamp</th>\n",
" <th>costs</th>\n",
" <th>org:resource</th>\n",
" <th>@@index</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>1</td>\n",
" <td>register request</td>\n",
" <td>2010-12-30 10:02:00+00:00</td>\n",
" <td>50</td>\n",
" <td>Pete</td>\n",
" <td>14</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>1</td>\n",
" <td>examine thoroughly</td>\n",
" <td>2010-12-31 09:06:00+00:00</td>\n",
" <td>400</td>\n",
" <td>Sue</td>\n",
" <td>15</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>1</td>\n",
" <td>check ticket</td>\n",
" <td>2011-01-05 14:12:00+00:00</td>\n",
" <td>100</td>\n",
" <td>Mike</td>\n",
" <td>16</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>1</td>\n",
" <td>decide</td>\n",
" <td>2011-01-06 10:18:00+00:00</td>\n",
" <td>200</td>\n",
" <td>Sara</td>\n",
" <td>17</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18</th>\n",
" <td>1</td>\n",
" <td>reject request</td>\n",
" <td>2011-01-07 13:24:00+00:00</td>\n",
" <td>200</td>\n",
" <td>Pete</td>\n",
" <td>18</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>2</td>\n",
" <td>register request</td>\n",
" <td>2010-12-30 10:32:00+00:00</td>\n",
" <td>50</td>\n",
" <td>Mike</td>\n",
" <td>9</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>2</td>\n",
" <td>check ticket</td>\n",
" <td>2010-12-30 11:12:00+00:00</td>\n",
" <td>100</td>\n",
" <td>Mike</td>\n",
" <td>10</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>2</td>\n",
" <td>examine casually</td>\n",
" <td>2010-12-30 13:16:00+00:00</td>\n",
" <td>400</td>\n",
" <td>Sean</td>\n",
" <td>11</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>2</td>\n",
" <td>decide</td>\n",
" <td>2011-01-05 10:22:00+00:00</td>\n",
" <td>200</td>\n",
" <td>Sara</td>\n",
" <td>12</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>2</td>\n",
" <td>pay compensation</td>\n",
" <td>2011-01-08 11:05:00+00:00</td>\n",
" <td>200</td>\n",
" <td>Ellen</td>\n",
" <td>13</td>\n",
" </tr>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>3</td>\n",
" <td>register request</td>\n",
" <td>2010-12-30 13:32:00+00:00</td>\n",
" <td>50</td>\n",
" <td>Pete</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>3</td>\n",
" <td>examine casually</td>\n",
" <td>2010-12-30 14:06:00+00:00</td>\n",
" <td>400</td>\n",
" <td>Mike</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3</td>\n",
" <td>check ticket</td>\n",
" <td>2010-12-30 15:34:00+00:00</td>\n",
" <td>100</td>\n",
" <td>Ellen</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>3</td>\n",
" <td>decide</td>\n",
" <td>2011-01-06 08:18:00+00:00</td>\n",
" <td>200</td>\n",
" <td>Sara</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>3</td>\n",
" <td>reinitiate request</td>\n",
" <td>2011-01-06 11:18:00+00:00</td>\n",
" <td>200</td>\n",
" <td>Sara</td>\n",
" <td>4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>3</td>\n",
" <td>examine thoroughly</td>\n",
" <td>2011-01-06 12:06:00+00:00</td>\n",
" <td>400</td>\n",
" <td>Sean</td>\n",
" <td>5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>3</td>\n",
" <td>check ticket</td>\n",
" <td>2011-01-08 10:43:00+00:00</td>\n",
" <td>100</td>\n",
" <td>Pete</td>\n",
" <td>6</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>3</td>\n",
" <td>decide</td>\n",
" <td>2011-01-09 08:55:00+00:00</td>\n",
" <td>200</td>\n",
" <td>Sara</td>\n",
" <td>7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>3</td>\n",
" <td>pay compensation</td>\n",
" <td>2011-01-15 09:45:00+00:00</td>\n",
" <td>200</td>\n",
" <td>Ellen</td>\n",
" <td>8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>37</th>\n",
" <td>4</td>\n",
" <td>register request</td>\n",
" <td>2011-01-06 14:02:00+00:00</td>\n",
" <td>50</td>\n",
" <td>Pete</td>\n",
" <td>37</td>\n",
" </tr>\n",
" <tr>\n",
" <th>38</th>\n",
" <td>4</td>\n",
" <td>check ticket</td>\n",
" <td>2011-01-07 11:06:00+00:00</td>\n",
" <td>100</td>\n",
" <td>Mike</td>\n",
" <td>38</td>\n",
" </tr>\n",
" <tr>\n",
" <th>39</th>\n",
" <td>4</td>\n",
" <td>examine thoroughly</td>\n",
" <td>2011-01-08 13:43:00+00:00</td>\n",
" <td>400</td>\n",
" <td>Sean</td>\n",
" <td>39</td>\n",
" </tr>\n",
" <tr>\n",
" <th>40</th>\n",
" <td>4</td>\n",
" <td>decide</td>\n",
" <td>2011-01-09 11:02:00+00:00</td>\n",
" <td>200</td>\n",
" <td>Sara</td>\n",
" <td>40</td>\n",
" </tr>\n",
" <tr>\n",
" <th>41</th>\n",
" <td>4</td>\n",
" <td>reject request</td>\n",
" <td>2011-01-12 14:44:00+00:00</td>\n",
" <td>200</td>\n",
" <td>Ellen</td>\n",
" <td>41</td>\n",
" </tr>\n",
" <tr>\n",
" <th>24</th>\n",
" <td>5</td>\n",
" <td>register request</td>\n",
" <td>2011-01-06 08:02:00+00:00</td>\n",
" <td>50</td>\n",
" <td>Ellen</td>\n",
" <td>24</td>\n",
" </tr>\n",
" <tr>\n",
" <th>25</th>\n",
" <td>5</td>\n",
" <td>examine casually</td>\n",
" <td>2011-01-07 09:16:00+00:00</td>\n",
" <td>400</td>\n",
" <td>Mike</td>\n",
" <td>25</td>\n",
" </tr>\n",
" <tr>\n",
" <th>26</th>\n",
" <td>5</td>\n",
" <td>check ticket</td>\n",
" <td>2011-01-08 10:22:00+00:00</td>\n",
" <td>100</td>\n",
" <td>Pete</td>\n",
" <td>26</td>\n",
" </tr>\n",
" <tr>\n",
" <th>27</th>\n",
" <td>5</td>\n",
" <td>decide</td>\n",
" <td>2011-01-10 12:28:00+00:00</td>\n",
" <td>200</td>\n",
" <td>Sara</td>\n",
" <td>27</td>\n",
" </tr>\n",
" <tr>\n",
" <th>28</th>\n",
" <td>5</td>\n",
" <td>reinitiate request</td>\n",
" <td>2011-01-11 15:18:00+00:00</td>\n",
" <td>200</td>\n",
" <td>Sara</td>\n",
" <td>28</td>\n",
" </tr>\n",
" <tr>\n",
" <th>29</th>\n",
" <td>5</td>\n",
" <td>check ticket</td>\n",
" <td>2011-01-14 13:33:00+00:00</td>\n",
" <td>100</td>\n",
" <td>Ellen</td>\n",
" <td>29</td>\n",
" </tr>\n",
" <tr>\n",
" <th>30</th>\n",
" <td>5</td>\n",
" <td>examine casually</td>\n",
" <td>2011-01-16 14:50:00+00:00</td>\n",
" <td>400</td>\n",
" <td>Mike</td>\n",
" <td>30</td>\n",
" </tr>\n",
" <tr>\n",
" <th>31</th>\n",
" <td>5</td>\n",
" <td>decide</td>\n",
" <td>2011-01-19 10:18:00+00:00</td>\n",
" <td>200</td>\n",
" <td>Sara</td>\n",
" <td>31</td>\n",
" </tr>\n",
" <tr>\n",
" <th>32</th>\n",
" <td>5</td>\n",
" <td>reinitiate request</td>\n",
" <td>2011-01-20 11:48:00+00:00</td>\n",
" <td>200</td>\n",
" <td>Sara</td>\n",
" <td>32</td>\n",
" </tr>\n",
" <tr>\n",
" <th>33</th>\n",
" <td>5</td>\n",
" <td>examine casually</td>\n",
" <td>2011-01-21 08:06:00+00:00</td>\n",
" <td>400</td>\n",
" <td>Sue</td>\n",
" <td>33</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34</th>\n",
" <td>5</td>\n",
" <td>check ticket</td>\n",
" <td>2011-01-21 10:34:00+00:00</td>\n",
" <td>100</td>\n",
" <td>Pete</td>\n",
" <td>34</td>\n",
" </tr>\n",
" <tr>\n",
" <th>35</th>\n",
" <td>5</td>\n",
" <td>decide</td>\n",
" <td>2011-01-23 12:12:00+00:00</td>\n",
" <td>200</td>\n",
" <td>Sara</td>\n",
" <td>35</td>\n",
" </tr>\n",
" <tr>\n",
" <th>36</th>\n",
" <td>5</td>\n",
" <td>reject request</td>\n",
" <td>2011-01-24 13:56:00+00:00</td>\n",
" <td>200</td>\n",
" <td>Mike</td>\n",
" <td>36</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19</th>\n",
" <td>6</td>\n",
" <td>register request</td>\n",
" <td>2011-01-06 14:02:00+00:00</td>\n",
" <td>50</td>\n",
" <td>Mike</td>\n",
" <td>19</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20</th>\n",
" <td>6</td>\n",
" <td>examine casually</td>\n",
" <td>2011-01-06 15:06:00+00:00</td>\n",
" <td>400</td>\n",
" <td>Ellen</td>\n",
" <td>20</td>\n",
" </tr>\n",
" <tr>\n",
" <th>21</th>\n",
" <td>6</td>\n",
" <td>check ticket</td>\n",
" <td>2011-01-07 15:22:00+00:00</td>\n",
" <td>100</td>\n",
" <td>Mike</td>\n",
" <td>21</td>\n",
" </tr>\n",
" <tr>\n",
" <th>22</th>\n",
" <td>6</td>\n",
" <td>decide</td>\n",
" <td>2011-01-07 15:52:00+00:00</td>\n",
" <td>200</td>\n",
" <td>Sara</td>\n",
" <td>22</td>\n",
" </tr>\n",
" <tr>\n",
" <th>23</th>\n",
" <td>6</td>\n",
" <td>pay compensation</td>\n",
" <td>2011-01-16 10:47:00+00:00</td>\n",
" <td>200</td>\n",
" <td>Mike</td>\n",
" <td>23</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" case:concept:name concept:name time:timestamp costs \\\n",
"14 1 register request 2010-12-30 10:02:00+00:00 50 \n",
"15 1 examine thoroughly 2010-12-31 09:06:00+00:00 400 \n",
"16 1 check ticket 2011-01-05 14:12:00+00:00 100 \n",
"17 1 decide 2011-01-06 10:18:00+00:00 200 \n",
"18 1 reject request 2011-01-07 13:24:00+00:00 200 \n",
"9 2 register request 2010-12-30 10:32:00+00:00 50 \n",
"10 2 check ticket 2010-12-30 11:12:00+00:00 100 \n",
"11 2 examine casually 2010-12-30 13:16:00+00:00 400 \n",
"12 2 decide 2011-01-05 10:22:00+00:00 200 \n",
"13 2 pay compensation 2011-01-08 11:05:00+00:00 200 \n",
"0 3 register request 2010-12-30 13:32:00+00:00 50 \n",
"1 3 examine casually 2010-12-30 14:06:00+00:00 400 \n",
"2 3 check ticket 2010-12-30 15:34:00+00:00 100 \n",
"3 3 decide 2011-01-06 08:18:00+00:00 200 \n",
"4 3 reinitiate request 2011-01-06 11:18:00+00:00 200 \n",
"5 3 examine thoroughly 2011-01-06 12:06:00+00:00 400 \n",
"6 3 check ticket 2011-01-08 10:43:00+00:00 100 \n",
"7 3 decide 2011-01-09 08:55:00+00:00 200 \n",
"8 3 pay compensation 2011-01-15 09:45:00+00:00 200 \n",
"37 4 register request 2011-01-06 14:02:00+00:00 50 \n",
"38 4 check ticket 2011-01-07 11:06:00+00:00 100 \n",
"39 4 examine thoroughly 2011-01-08 13:43:00+00:00 400 \n",
"40 4 decide 2011-01-09 11:02:00+00:00 200 \n",
"41 4 reject request 2011-01-12 14:44:00+00:00 200 \n",
"24 5 register request 2011-01-06 08:02:00+00:00 50 \n",
"25 5 examine casually 2011-01-07 09:16:00+00:00 400 \n",
"26 5 check ticket 2011-01-08 10:22:00+00:00 100 \n",
"27 5 decide 2011-01-10 12:28:00+00:00 200 \n",
"28 5 reinitiate request 2011-01-11 15:18:00+00:00 200 \n",
"29 5 check ticket 2011-01-14 13:33:00+00:00 100 \n",
"30 5 examine casually 2011-01-16 14:50:00+00:00 400 \n",
"31 5 decide 2011-01-19 10:18:00+00:00 200 \n",
"32 5 reinitiate request 2011-01-20 11:48:00+00:00 200 \n",
"33 5 examine casually 2011-01-21 08:06:00+00:00 400 \n",
"34 5 check ticket 2011-01-21 10:34:00+00:00 100 \n",
"35 5 decide 2011-01-23 12:12:00+00:00 200 \n",
"36 5 reject request 2011-01-24 13:56:00+00:00 200 \n",
"19 6 register request 2011-01-06 14:02:00+00:00 50 \n",
"20 6 examine casually 2011-01-06 15:06:00+00:00 400 \n",
"21 6 check ticket 2011-01-07 15:22:00+00:00 100 \n",
"22 6 decide 2011-01-07 15:52:00+00:00 200 \n",
"23 6 pay compensation 2011-01-16 10:47:00+00:00 200 \n",
"\n",
" org:resource @@index \n",
"14 Pete 14 \n",
"15 Sue 15 \n",
"16 Mike 16 \n",
"17 Sara 17 \n",
"18 Pete 18 \n",
"9 Mike 9 \n",
"10 Mike 10 \n",
"11 Sean 11 \n",
"12 Sara 12 \n",
"13 Ellen 13 \n",
"0 Pete 0 \n",
"1 Mike 1 \n",
"2 Ellen 2 \n",
"3 Sara 3 \n",
"4 Sara 4 \n",
"5 Sean 5 \n",
"6 Pete 6 \n",
"7 Sara 7 \n",
"8 Ellen 8 \n",
"37 Pete 37 \n",
"38 Mike 38 \n",
"39 Sean 39 \n",
"40 Sara 40 \n",
"41 Ellen 41 \n",
"24 Ellen 24 \n",
"25 Mike 25 \n",
"26 Pete 26 \n",
"27 Sara 27 \n",
"28 Sara 28 \n",
"29 Ellen 29 \n",
"30 Mike 30 \n",
"31 Sara 31 \n",
"32 Sara 32 \n",
"33 Sue 33 \n",
"34 Pete 34 \n",
"35 Sara 35 \n",
"36 Mike 36 \n",
"19 Mike 19 \n",
"20 Ellen 20 \n",
"21 Mike 21 \n",
"22 Sara 22 \n",
"23 Mike 23 "
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pm4py\n",
"log = pm4py.format_dataframe(df, case_id='case_id',activity_key='activity',\n",
" timestamp_key='timestamp')\n",
"log\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
},
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"Observe that the column names are updated as expected.\n",
"\n",
"Let us assume that we are not only interested in the number of events and cases, yet, we also want to figure out what\n",
"activities occur first, and what activities occur last in the traces described by the event log.\n",
"pm4py has a specific built-in function for this, i.e., ```pm4py.get_start_activities()``` and ```pm4py.get_end_activities()``` respectively."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"pycharm": {
"name": "#%%\n"
},
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"{'register request': 6}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pm4py.get_start_activities(log)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"pycharm": {
"name": "#%%\n"
},
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"{'pay compensation': 3, 'reject request': 3}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pm4py.get_end_activities(log)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"The ```pm4py.get_start_activities()``` and ```pm4py.get_end_activities()``` both return a dictionary containing the activities\n",
"as a key, and, the number of observations (i.e., number of traces in which they occur first, respectively, last) in\n",
"the event log.\n",
"\n",
"pm4py exploits a built-in pandas function to detect the format of the timestamps in the input data automatically.\n",
"However, pandas looks at the timestamp values in each row in isolation.\n",
"In some cases, this can lead to problems.\n",
"For example, if the provided value is 2020-01-18, i.e., first the year, then the month, and then the day of the date,\n",
"in some cases, a value of 2020-02-01 may be interpreted wrongly as January 2nd, i.e., rather than February 1st.\n",
"To alleviate this problem, an additional parameter can be provided to the ```format_dataframe()``` method, i.e.,\n",
"the timest_format parameter. The default Python timestamp format codes can be used to provide the timestamp format.\n",
"In this example, the timestamp format is ```%Y-%m-%d %H:%M:%S%z```.\n",
"In general, we advise to always specify the timestamp format."
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
},
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Importing XES Files"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"Next to CSV files, event data can also be stored in an XML-based format, i.e., in XES files.\n",
"In an XES file, we can describe a containment relation, i.e., a log contains a number of traces, which in turn contain several events.\n",
"Furthermore, an object, i.e., a log, trace, or event, is allowed to have attributes.\n",
"The advantage is that certain data attributes that are constant for a log or a trace, can be stored at that level.\n",
"For example, assume that we only know the total costs of a case, rather than the costs of the individual events.\n",
"If we want to store this information in a CSV file, we either need to replicate this information (i.e., we can only\n",
"store data in rows, which directly refer to events), or, we need to explicitly define that certain columns only get a\n",
"value once, i.e., referring to case-level attributes.\n",
"The XES standard more naturally supports the storage of this type of information.\n",
"Click [here](data/running_example.xes) to obtain the .xes file of the running_example.\n",
"\n",
"Importing an XES file is fairly straightforward.\n",
"pm4py has a special read_xes()-function that can parse a given xes file and load it in pm4py, i.e., as an Event Log object.\n",
"Consider the following code snippet, in which we show how to import an XES event log.\n",
"Like the previous example, the script outputs activities that can start and end a trace."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"pycharm": {
"name": "#%%\n"
},
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "a356969d9a9b4ffa928c5670f630d3fc",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"parsing log, completed traces :: 0%| | 0/6 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"{'register request': 6}"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"log_xes = pm4py.read_xes('data/running_example.xes', return_legacy_log_object=True)\n",
"pm4py.get_start_activities(log_xes)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"pycharm": {
"name": "#%%\n"
},
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"{'pay compensation': 3, 'reject request': 3}"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pm4py.get_end_activities(log_xes)\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Exporting Event Data"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"Now we have seen how to import event data into pm4py, let’s take a look at the opposite, i.e., exporting event data.\n",
"Exporting of event logs can be very useful, e.g., we might want to convert a .csv file into a ```.xes``` file or we might\n",
"want to filter out certain (noisy) cases and save the filtered event log. Like importing, exporting of event data is\n",
"possible in two ways, i.e., exporting to ```csv``` (using ```pandas```) and exporting event logs to xes. In the upcoming\n",
"sections, we show how to export an event log stored as a ```pandas data frame``` into a ```csv``` file, a ```pandas data frame``` as an\n",
"```xes file```, a pm4py ```event log object``` as a ```csv file``` and finally, a pm4py ```event log object``` as an ```xes file```."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Storing a Pandas Data Frame as a csv file"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"Storing an event log that is represented as a pandas dataframe is straightforward, i.e., we can directly use the ```to_csv```\n",
" ([full reference here](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html)) function\n",
" of the pandas DataFrame object. Consider the following example snippet of code, in which we show this functionality."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"pycharm": {
"name": "#%%\n"
},
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"log.to_csv('running_example_exported.csv')"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Storing a Pandas DataFrame as a .xes file"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"It is also possible to store a pandas data frame to a xes file. This is simply done by calling the ```pm4py.write_xes()```\n",
"function. You can pass the dataframe as an input parameter to the function, i.e., pm4py handles the internal conversion\n",
"of the dataframe to an event log object prior to writing it to disk. Note that this construct only works if you have\n",
"formatted the data frame, i.e., as highlighted earlier in the importing CSV section."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"is_executing": true,
"name": "#%%\n"
},
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"pm4py.write_xes(log, 'running_example_csv_exported_as_xes.xes')\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Storing an Event Log object as a .csv file"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
},
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"In some cases, we might want to store an event log object, e.g., obtained by importing a .xes file, as a csv file.\n",
"For example, certain (commercial) process mining tools only support csv importing. \n",
"For this purpose, pm4py offers conversion functionality that allows you to convert your event log object into a data frame,\n",
"which you can subsequently export using pandas.\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"pycharm": {
"name": "#%%\n"
},
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"df = pm4py.convert_to_dataframe(log_xes)\n",
"df.to_csv('running_example_xes_exported_as_csv.csv')\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
},
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Storing an Event Log Object as a .xes File"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
},
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"Storing an event log object as a .xes file is rather straightforward. In pm4py, the write_xes() method allows us to do so.\n",
"Consider the simple example script below in which we show an example of this functionality."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
},
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"pm4py.write_xes(log_xes, 'running_example_exported.xes')"
]
}
],
"metadata": {
"celltoolbar": "Slideshow",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 1
} |