Skip to content

Commit

Permalink
Merge branch 'ft-1313-utility-parse-collection-string-traces' into 'i…
Browse files Browse the repository at this point in the history
…ntegration'

FT 1313 Utility to parse a collection of string traces to an event log

See merge request pm4py/pm4py-core!512
  • Loading branch information
fit-sebastiaan-van-zelst committed Oct 15, 2021
2 parents 527694b + 5bd2684 commit 5b85d5d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pm4py/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
get_minimum_self_distances, get_minimum_self_distance_witnesses, \
get_case_arrival_average, get_rework_cases_per_activity, get_case_overlap, get_cycle_time, \
get_all_case_durations, get_case_duration
from pm4py.utils import format_dataframe, parse_process_tree, serialize, deserialize, set_classifier
from pm4py.utils import format_dataframe, parse_process_tree, serialize, deserialize, set_classifier, parse_event_log_string
from pm4py.vis import view_petri_net, save_vis_petri_net, view_dfg, save_vis_dfg, view_process_tree, \
save_vis_process_tree, \
view_heuristics_net, save_vis_heuristics_net, view_bpmn, save_vis_bpmn, view_sna, save_vis_sna,\
Expand Down
46 changes: 44 additions & 2 deletions pm4py/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from typing import Optional, Tuple, Any
import datetime
from typing import Optional, Tuple, Any, Collection

import pandas as pd

from pm4py.objects.log.obj import EventLog, Trace
from pm4py.objects.log.obj import EventLog, Trace, Event
from pm4py.objects.process_tree.obj import ProcessTree
from pm4py.util import constants, xes_constants, pandas_utils

Expand Down Expand Up @@ -267,6 +268,47 @@ def set_classifier(log, classifier, classifier_attribute=constants.DEFAULT_CLASS
return log


def parse_event_log_string(traces: Collection[str], sep: str = ",",
activity_key: str = xes_constants.DEFAULT_NAME_KEY,
timestamp_key: str = xes_constants.DEFAULT_TIMESTAMP_KEY,
case_id_key : str = xes_constants.DEFAULT_TRACEID_KEY) -> EventLog:
"""
Parse a collection of traces expressed as strings
(e.g., ["A,B,C,D", "A,C,B,D", "A,D"])
to an event log
Parameters
------------------
traces
Collection of traces expressed as strings
sep
Separator used to split the activities of a string trace
activity_key
The attribute that should be used as activity
timestamp_key
The attribute that should be used as timestamp
case_id_key
The attribute that should be used as case identifier
Returns
-----------------
log
Event log
"""
log = EventLog()
this_timest = 10000000
for index, trace in enumerate(traces):
activities = trace.split(sep)
trace = Trace()
trace.attributes[case_id_key] = str(index)
for act in activities:
event = Event({activity_key: act, timestamp_key: datetime.datetime.fromtimestamp(this_timest)})
trace.append(event)
this_timest = this_timest + 1
log.append(trace)
return log


def general_checks_classical_event_log(log):
"""
Method to checks the consistency of the provided classical event log.
Expand Down

0 comments on commit 5b85d5d

Please sign in to comment.