Skip to content

Commit 7b953e7

Browse files
committed
XES to CSV and back conversion
1 parent c80a320 commit 7b953e7

13 files changed

+1291623
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,5 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
131+
.idea/

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
# pm4py-wrapper
1+
# pm4py-wrapper
2+
3+
Wrapper for pm4py.
4+
5+
## Usage
6+
7+
```shell
8+
$ pm4py_wrapper -p log.xes xes_to_csv
9+
$ pm4py_wrapper -p log.csv xes_to_xes
10+
```

setup.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from setuptools import setup, find_packages
2+
3+
setup(
4+
name='pm4py-wrapper',
5+
version='1.0.0',
6+
packages=find_packages(where='src'),
7+
package_dir={"": "src"},
8+
include_package_data=True,
9+
install_requires=[
10+
'click',
11+
'pm4py',
12+
'pandas',
13+
'pytest',
14+
'setuptools',
15+
],
16+
entry_points={
17+
'console_scripts': [
18+
'pm4py_wrapper = pm4py_wrapper.cli:main',
19+
]
20+
}
21+
)

src/pm4py_wrapper/cli.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from pathlib import Path
2+
3+
import click
4+
from click import BaseCommand
5+
6+
from pm4py_wrapper.wrapper import convert_xes_to_csv, convert_csv_to_xes
7+
8+
9+
@click.group()
10+
@click.option('-i', '--input_log', default=None, required=True, type=Path, help='Path to the input event log.')
11+
@click.option('-o', '--output_dir', default=Path('.'), type=Path, help='Path to the output directory.')
12+
def main(input_log, output_dir):
13+
# This is a main group which includes other commands specified below.
14+
pass
15+
16+
17+
@main.command()
18+
@click.pass_context
19+
def xes_to_csv(ctx):
20+
log_path: Path = ctx.parent.params['input_log']
21+
output_dir: Path = ctx.parent.params['output_dir']
22+
output_path = output_dir / log_path.with_suffix('.csv').name
23+
convert_xes_to_csv(log_path, output_path)
24+
25+
26+
@main.command()
27+
@click.pass_context
28+
def csv_to_xes(ctx):
29+
log_path: Path = ctx.parent.params['input_log']
30+
output_dir: Path = ctx.parent.params['output_dir']
31+
output_path = output_dir / log_path.with_suffix('.xes').name
32+
convert_csv_to_xes(log_path, output_path)
33+
34+
35+
if __name__ == '__main__':
36+
main()

src/pm4py_wrapper/wrapper.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from pathlib import Path
2+
3+
import pandas as pd
4+
import pm4py
5+
from pm4py.objects.conversion.log import converter as log_converter
6+
from pm4py.objects.log.util import interval_lifecycle
7+
from pm4py.objects.log.exporter.xes import exporter as xes_exporter
8+
9+
10+
def convert_xes_to_csv(xes_path: Path, output_path: Path):
11+
log = pm4py.read_xes(str(xes_path))
12+
log_interval = interval_lifecycle.to_interval(log)
13+
df = log_converter.apply(log_interval, variant=log_converter.Variants.TO_DATA_FRAME)
14+
df.to_csv(output_path, index=False)
15+
16+
17+
def convert_csv_to_xes(csv_path: Path, output_path: Path):
18+
df = pd.read_csv(csv_path)
19+
log = log_converter.apply(df, variant=log_converter.Variants.TO_EVENT_LOG)
20+
xes_exporter.apply(log, str(output_path))

tests/assets/input/BPI_Challenge_2012_W_Two_TS.csv

Lines changed: 59303 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)