```python
# Installation of PM4Py Python package
#Source: https://pm4py.fit.fraunhofer.de/install-page
!pip install pm4py
```

    Collecting pm4py
    [?25l  Downloading https://files.pythonhosted.org/packages/d2/83/dcfa7bfa85d7fda35417f4bf76c8e5980919de92aa8d458c28d1518cc162/pm4py-2.2.10.1-py3-none-any.whl (1.4MB)
    [K     |████████████████████████████████| 1.5MB 5.1MB/s 
    [?25hRequirement already satisfied: pytz in /usr/local/lib/python3.7/dist-packages (from pm4py) (2018.9)
    Collecting pulp<=2.1
    [?25l  Downloading https://files.pythonhosted.org/packages/41/34/757c88c320f80ce602199603afe63aed1e0bc11180b9a9fb6018fb2ce7ef/PuLP-2.1-py3-none-any.whl (40.6MB)
    [K     |████████████████████████████████| 40.6MB 73kB/s 
    [?25hRequirement already satisfied: pydotplus in /usr/local/lib/python3.7/dist-packages (from pm4py) (2.0.2)
    Requirement already satisfied: matplotlib in /usr/local/lib/python3.7/dist-packages (from pm4py) (3.2.2)
    Requirement already satisfied: intervaltree in /usr/local/lib/python3.7/dist-packages (from pm4py) (2.1.0)
    Requirement already satisfied: cvxopt in /usr/local/lib/python3.7/dist-packages (from pm4py) (1.2.6)
    Collecting pyvis
      Downloading https://files.pythonhosted.org/packages/07/d1/e87844ec86e96df7364f21af2263ad6030c0d727660ae89935c7af56a540/pyvis-0.1.9-py3-none-any.whl
    Requirement already satisfied: graphviz in /usr/local/lib/python3.7/dist-packages (from pm4py) (0.10.1)
    Requirement already satisfied: pandas in /usr/local/lib/python3.7/dist-packages (from pm4py) (1.1.5)
    Collecting deprecation
      Downloading https://files.pythonhosted.org/packages/02/c3/253a89ee03fc9b9682f1541728eb66db7db22148cd94f89ab22528cd1e1b/deprecation-2.1.0-py2.py3-none-any.whl
    Requirement already satisfied: scikit-learn in /usr/local/lib/python3.7/dist-packages (from pm4py) (0.22.2.post1)
    Requirement already satisfied: scipy in /usr/local/lib/python3.7/dist-packages (from pm4py) (1.4.1)
    Requirement already satisfied: lxml in /usr/local/lib/python3.7/dist-packages (from pm4py) (4.2.6)
    Requirement already satisfied: sympy in /usr/local/lib/python3.7/dist-packages (from pm4py) (1.7.1)
    Collecting jsonpickle
      Downloading https://files.pythonhosted.org/packages/bb/1a/f2db026d4d682303793559f1c2bb425ba3ec0d6fd7ac63397790443f2461/jsonpickle-2.0.0-py2.py3-none-any.whl
    Requirement already satisfied: networkx in /usr/local/lib/python3.7/dist-packages (from pm4py) (2.5.1)
    Requirement already satisfied: numpy!=1.19.4 in /usr/local/lib/python3.7/dist-packages (from pm4py) (1.19.5)
    Requirement already satisfied: tqdm in /usr/local/lib/python3.7/dist-packages (from pm4py) (4.41.1)
    Collecting stringdist
      Downloading https://files.pythonhosted.org/packages/85/f0/c56cbe92b4b06fbc7adaa81917ad34d7027834e166fff2d2db73961c67fa/StringDist-1.0.9.tar.gz
    Requirement already satisfied: pyparsing>=2.0.1 in /usr/local/lib/python3.7/dist-packages (from pulp<=2.1->pm4py) (2.4.7)
    Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/dist-packages (from matplotlib->pm4py) (0.10.0)
    Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->pm4py) (1.3.1)
    Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->pm4py) (2.8.1)
    Requirement already satisfied: sortedcontainers in /usr/local/lib/python3.7/dist-packages (from intervaltree->pm4py) (2.4.0)
    Requirement already satisfied: jinja2>=2.9.6 in /usr/local/lib/python3.7/dist-packages (from pyvis->pm4py) (2.11.3)
    Requirement already satisfied: ipython>=5.3.0 in /usr/local/lib/python3.7/dist-packages (from pyvis->pm4py) (5.5.0)
    Requirement already satisfied: packaging in /usr/local/lib/python3.7/dist-packages (from deprecation->pm4py) (21.0)
    Requirement already satisfied: joblib>=0.11 in /usr/local/lib/python3.7/dist-packages (from scikit-learn->pm4py) (1.0.1)
    Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.7/dist-packages (from sympy->pm4py) (1.2.1)
    Requirement already satisfied: importlib-metadata; python_version < "3.8" in /usr/local/lib/python3.7/dist-packages (from jsonpickle->pm4py) (4.6.1)
    Requirement already satisfied: decorator<5,>=4.3 in /usr/local/lib/python3.7/dist-packages (from networkx->pm4py) (4.4.2)
    Requirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from cycler>=0.10->matplotlib->pm4py) (1.15.0)
    Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python3.7/dist-packages (from jinja2>=2.9.6->pyvis->pm4py) (2.0.1)
    Requirement already satisfied: pexpect; sys_platform != "win32" in /usr/local/lib/python3.7/dist-packages (from ipython>=5.3.0->pyvis->pm4py) (4.8.0)
    Requirement already satisfied: simplegeneric>0.8 in /usr/local/lib/python3.7/dist-packages (from ipython>=5.3.0->pyvis->pm4py) (0.8.1)
    Requirement already satisfied: pygments in /usr/local/lib/python3.7/dist-packages (from ipython>=5.3.0->pyvis->pm4py) (2.6.1)
    Requirement already satisfied: setuptools>=18.5 in /usr/local/lib/python3.7/dist-packages (from ipython>=5.3.0->pyvis->pm4py) (57.2.0)
    Requirement already satisfied: traitlets>=4.2 in /usr/local/lib/python3.7/dist-packages (from ipython>=5.3.0->pyvis->pm4py) (5.0.5)
    Requirement already satisfied: pickleshare in /usr/local/lib/python3.7/dist-packages (from ipython>=5.3.0->pyvis->pm4py) (0.7.5)
    Requirement already satisfied: prompt-toolkit<2.0.0,>=1.0.4 in /usr/local/lib/python3.7/dist-packages (from ipython>=5.3.0->pyvis->pm4py) (1.0.18)
    Requirement already satisfied: typing-extensions>=3.6.4; python_version < "3.8" in /usr/local/lib/python3.7/dist-packages (from importlib-metadata; python_version < "3.8"->jsonpickle->pm4py) (3.7.4.3)
    Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.7/dist-packages (from importlib-metadata; python_version < "3.8"->jsonpickle->pm4py) (3.5.0)
    Requirement already satisfied: ptyprocess>=0.5 in /usr/local/lib/python3.7/dist-packages (from pexpect; sys_platform != "win32"->ipython>=5.3.0->pyvis->pm4py) (0.7.0)
    Requirement already satisfied: ipython-genutils in /usr/local/lib/python3.7/dist-packages (from traitlets>=4.2->ipython>=5.3.0->pyvis->pm4py) (0.2.0)
    Requirement already satisfied: wcwidth in /usr/local/lib/python3.7/dist-packages (from prompt-toolkit<2.0.0,>=1.0.4->ipython>=5.3.0->pyvis->pm4py) (0.2.5)
    Building wheels for collected packages: stringdist
      Building wheel for stringdist (setup.py) ... [?25l[?25hdone
      Created wheel for stringdist: filename=StringDist-1.0.9-cp37-cp37m-linux_x86_64.whl size=23562 sha256=1fe20d631016b567e747cf0f7a152dc67bfcfdff3279bc22ac1c98ba0d2af1a4
      Stored in directory: /root/.cache/pip/wheels/c3/1b/bb/bf0de4d64d8ca38759811fe3353e441f12feb606fb3d1d2e11
    Successfully built stringdist
    Installing collected packages: pulp, jsonpickle, pyvis, deprecation, stringdist, pm4py
    Successfully installed deprecation-2.1.0 jsonpickle-2.0.0 pm4py-2.2.10.1 pulp-2.1 pyvis-0.1.9 stringdist-1.0.9



```python
from google.colab import drive
drive.mount('/content/drive')
```


```python
# Import dependencies and set the filepath
import pm4py
import os
import pandas as pd
import numpy as np
import datetime as dt
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px         # needed for plotly express in general
import plotly.graph_objects as go   # needed for plotly graph objects e.g. pie
```


```python
# Set the file path
# Please upload the local file sent via email to colab before running this
file_path = '/content/Hospital Billing - Event Log.xes' # if using the original file
#file_path = '/content/log.xes' # if renamed
```


```python
# Read the XES file and create the log
event_log = pm4py.read_xes(file_path)
```


    HBox(children=(FloatProgress(value=0.0, description='parsing log, completed traces :: ', max=85269.0, style=Pr…


    



```python
def import_xes(file_path):
  event_log = pm4py.read_xes(file_path)
  start_activities = pm4py.get_start_activities(event_log)
  end_activities = pm4py.get_end_activities(event_log)
print("Start activities: {}\nEnd activities: {}".format(start_activities, end_activities))
```


```python
# Obtaining a Process Model
# event_log = pm4py.read_xes('C:/Users/demo/Downloads/running-example.xes')

process_tree = pm4py.discover_process_tree_inductive(event_log) # discover_tree_inductive is deprecated
bpmn_model = pm4py.convert_to_bpmn(process_tree)
pm4py.view_bpmn(bpmn_model)
```


```python
#obtain process map 
dfg, start_activities, end_activities = pm4py.discover_dfg(event_log)
pm4py.view_dfg(dfg, start_activities, end_activities)
```


```python
#heuristics map
map = pm4py.discover_heuristics_net(event_log)
pm4py.view_heuristics_net(map)
```


```python
# View Process Tree
process_tree = pm4py.discover_process_tree_inductive(event_log) # discover_tree_inductive is deprecated
pm4py.view_process_tree(process_tree)
```


```python
print("Start activities: {}\nEnd activities: {}".format(start_activities, end_activities))
```

## Miner Algorithms
Please refer to the documentation here:
https://pm4py.fit.fraunhofer.de/documentation#item-3-1


```python
# Alpha Miner implementation

from pm4py.algo.discovery.alpha import algorithm as alpha_miner
net, initial_marking, final_marking = alpha_miner.apply(event_log)

# Inductive Miner implementation
from pm4py.algo.discovery.inductive import algorithm as inductive_miner
net, initial_marking, final_marking = inductive_miner.apply(event_log)
```

## Visualisations


```python
# Visualisation of IM

from pm4py.visualization.process_tree import visualizer as pt_visualizer
tree = inductive_miner.apply_tree(event_log)

gviz = pt_visualizer.apply(tree)
pt_visualizer.view(gviz)
```




    '/tmp/tmpn43cpox3.gv.png'




```python

```


```python
# Converting process tree in petri net

from pm4py.objects.conversion.process_tree import converter as pt_converter
net, initial_marking, final_marking = pt_converter.apply(tree, variant=pt_converter.Variants.TO_PETRI_NET)
gviz = pt_visualizer.apply(tree)
pt_visualizer.view(gviz)
```




    '/tmp/tmpgzmi0vfv.gv.png'




```python
# Dotted chart
pm4py.view_dotted_chart(event_log, format="svg")
```

Decision tree & Root Cause Analysis



```python
from pm4py.objects.log.util import get_class_representation , get_log_representation
from sklearn import tree
data, feature_names = get_log_representation.get_default_representation(event_log)

#changing into pandas data frame
dataframe = pd.DataFrame(data, columns=feature_names)

#creating decision tree
target, classes = get_class_representation.get_class_representation_by_trace_duration(event_log, 11007360)
clf = tree.DecisionTreeClassifier()
clf.fit(data, target)
```

    /usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:3: DeprecatedWarning:
    
    get_default_representation is deprecated as of 2.2.8 and will be removed in 3.0.0. please use pm4py.algo.transformation.log_to_features instead
    





    DecisionTreeClassifier(ccp_alpha=0.0, class_weight=None, criterion='gini',
                           max_depth=None, max_features=None, max_leaf_nodes=None,
                           min_impurity_decrease=0.0, min_impurity_split=None,
                           min_samples_leaf=1, min_samples_split=2,
                           min_weight_fraction_leaf=0.0, presort='deprecated',
                           random_state=None, splitter='best')




```python
#visualizing decision tree 


from pm4py.visualization.decisiontree import visualizer as dectree_visualizer
from pm4py.objects.log.util import get_class_representation
gviz = dectree_visualizer.apply(clf, feature_names, classes)
target, classes = get_class_representation.get_class_representation_by_trace_duration(event_log, 11007360)
dectree_visualizer.view(gviz)
```




    '/tmp/tmpdu8qp6ui.gv.png'



ipynb
12a4ce76abc8a9c2fd4be5ab22ab37d69dc122b3
Jupyter Notebook
c26469d1b0a542cc23a3a0b0086badc1a20f36db
MIT
sami5001/hosp-billing-PM4py
notebooks/root-cause-analysis.ipynb
c26469d1b0a542cc23a3a0b0086badc1a20f36db
MIT
sami5001/hosp-billing-PM4py
notebooks/root-cause-analysis.ipynb
c26469d1b0a542cc23a3a0b0086badc1a20f36db
MIT
sami5001/hosp-billing-PM4py
notebooks/root-cause-analysis.ipynb