【pm4py】process mining 读取log生成model/net/tree

文章目录

  • Ref
  • Quick Start
  • 1. 虚拟环境配置
  • 1. 读取log
    • 1.1 读取XES格式log
      • pm4py.read_xes(log)
      • xes_importer.apply(log)
    • 1.2 读取CSV格式log
  • 2. miner
    • Process model - BPMN
    • Petri net
    • Process tree
  • 3. view
  • 代码

Ref

【pm4py 官网】https://pm4py.fit.fraunhofer.de/
【ProM官网】http://promtools.org/doku.php

Quick Start

A sneak peak of pmp4y’s simple application:

import pm4py
log = pm4py.read_xes('')
process_model = pm4py.discover_bpmn_inductive(log)
pm4py.view_bpmn(process_model)

即读取XES log,基于inductive miner生成bpmn,并进行预览。例如:

【pm4py】process mining 读取log生成model/net/tree_第1张图片
【pm4py】process mining 读取log生成model/net/tree_第2张图片

1. 虚拟环境配置

  • 创建虚拟环境:
virtualenv -p python3.7 ~/venv/processmining
source ~/venv/processminging/bin/activate
  • 安装pm4py:
python3.7 -m pip install pm4py
pip list

pm4py为最新版本2.2.27

在这里插入图片描述

【pm4py】process mining 读取log生成model/net/tree_第3张图片

1. 读取log

可读取XES和CSV格式log:

1.1 读取XES格式log

pm4py.read_xes(log)

import pm4py
log = pm4py.read_xes('tests/input_data/running-example.xes')

此时可查看log,可看出log包含25个eventlog(即ProM中的cases),第一个eventlog包含92个traces(即Prom中第一个case包含92个event),第2个eventlog包含62个traces;这25个eventlog包含的traces数目最大为92,最小为34。

其中第一个eventlog的第一个trace(即ProM中第一个case的第一个event),concept:name为Start,lifecycle:transition为start……
【pm4py】process mining 读取log生成model/net/tree_第4张图片

下图即为ProM软件(【ProM官网】http://promtools.org/doku.php)对同一XES日志文件的读取分析数据,与pm4py相同。

【pm4py】process mining 读取log生成model/net/tree_第5张图片

xes_importer.apply(log)

除了pm4py官网上所显示的pm4py.read_xes('tests/input_data/running-example.xes'),还可以通过converter进行读取:

from pm4py.objects.log.importer.xes import importer as xes_importer
log = xes_importer.apply('')

【pm4py】process mining 读取log生成model/net/tree_第6张图片

1.2 读取CSV格式log

import pandas as pd
import pm4py

dataframe = pd.read_csv('tests/input_data/running-example.csv', sep=',')
dataframe = pm4py.format_dataframe(dataframe, case_id='case:concept:name', activity_key='concept:name', timestamp_key='time:timestamp')
event_log = pm4py.convert_to_event_log(dataframe)

要注意的是,表头应与case:concept:name, concept:nametime:timestamp对应;

但是当表格中有起始和终止时间时,未确定读取方法。(可在ProM中读取CSV,再转换为XES格式;然后即可通过1.1的方式读取)

2. miner

Process model - BPMN

process_model = pm4py.discover_bpmn_inductive(log)

【pm4py】process mining 读取log生成model/net/tree_第7张图片

Petri net

petri_net, initial_marking, final_marking = pm4py.discover_petri_net_inductive(log)

Process tree

process_tree = pm4py.discover_process_tree_inductive(log)

【pm4py】process mining 读取log生成model/net/tree_第8张图片

3. view

# BPMN
pm4py.view_bpmn(process_model_bpmn, format="pdf")

# Petri net
pm4py.view_petri_net(petri_net)

# Process tree
pm4py.view_process_tree(process_tree)

【pm4py】process mining 读取log生成model/net/tree_第9张图片

放大来看:
BPMN:
【pm4py】process mining 读取log生成model/net/tree_第10张图片
Petri net:
【pm4py】process mining 读取log生成model/net/tree_第11张图片

Process tree:
【pm4py】process mining 读取log生成model/net/tree_第12张图片

代码

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# @Time : 8/26/22 4:13 PM
# @Author : 
# @File : processmining.py
# @Version : 
# @Software: PyCharm
# @Description : 'process mining'

import pm4py
from pm4py.objects.log.importer.xes import importer as xes_importer

def quick_start():
    log = pm4py.read_xes('/home/deng/cugb/STL/CSTL/Process/PromEventLog/activitylog_uci_detailed_labour.xes')
    process_model = pm4py.discover_bpmn_inductive(log)
    pm4py.view_bpmn(process_model)

def read_xes():
    # read XES log
    # by pm4py.read_xes
    log = pm4py.read_xes('./PromEventLog/activitylog_uci_detailed_labour.xes')

    # by xes_importer.apply
    log = xes_importer.apply('./PromEventLog/activitylog_uci_detailed_labour.xes')
    log = xes_importer.apply('./PromEventLog/PurchasingExample_convertedByProM.xes')

    return log

def miner(log):
    # BPMN
    process_model_bpmn = pm4py.discover_bpmn_inductive(log)

    # Petri net
    petri_net, initial_marking, final_marking = pm4py.discover_petri_net_inductive(log)

    # Process tree
    process_tree = pm4py.discover_process_tree_inductive(log)

    return process_model_bpmn, petri_net, process_tree

def view(process_model_bpmn, petri_net, process_tree):
    # BPMN
    pm4py.view_bpmn(process_model_bpmn, format="pdf")
    pm4py.save_vis_bpmn(process_model_bpmn, 'test.pdf')

    # Petri net
    pm4py.view_petri_net(petri_net)

    # Process tree
    pm4py.view_process_tree(process_tree)

if __name__ == '__main__':
    quick_start()

    log = read_xes()
    process_model_bpmn, petri_net, process_tree = miner(log)
    view(process_model_bpmn, petri_net, process_tree)

    print('done')

你可能感兴趣的:(process,mining,pm4py,ProM)