Skip to content
Snippets Groups Projects
Commit c75acc01 authored by Simon Wolf's avatar Simon Wolf
Browse files

Added loadability functionality to import OECL from Cortado and pass own data...

Added loadability functionality to import OECL from Cortado and pass own data structure from backend to cortado. Furthermore adapted evaluation package of pm4py and integrated it into project to get rid of dependency mismatch between cortado and ocpa/ocsv.
parent ddea8c87
No related branches found
No related tags found
1 merge request!1Added loadability functionality to import OECL from Cortado and pass own data...
Showing
with 874 additions and 0 deletions
'''
This file is part of PM4Py (More Info: https://pm4py.fit.fraunhofer.de).
PM4Py is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PM4Py is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PM4Py. If not, see <https://www.gnu.org/licenses/>.
'''
from evaluation.generalization.variants import token_based
from pm4py.objects.conversion.log import converter as log_conversion
from enum import Enum
from pm4py.util import exec_utils
import deprecation
from pm4py.meta import VERSION
import warnings
class Variants(Enum):
GENERALIZATION_TOKEN = token_based
GENERALIZATION_TOKEN = Variants.GENERALIZATION_TOKEN
VERSIONS = {GENERALIZATION_TOKEN}
@deprecation.deprecated(deprecated_in="2.2.5", removed_in="3.0",
current_version=VERSION,
details="Use the pm4py.algo.evaluation.generalization package")
def apply(log, petri_net, initial_marking, final_marking, parameters=None, variant=GENERALIZATION_TOKEN):
warnings.warn("Use the pm4py.algo.evaluation.generalization package")
if parameters is None:
parameters = {}
return exec_utils.get_variant(variant).apply(log_conversion.apply(log, parameters, log_conversion.TO_EVENT_LOG),
petri_net,
initial_marking, final_marking, parameters=parameters)
'''
This file is part of PM4Py (More Info: https://pm4py.fit.fraunhofer.de).
PM4Py is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PM4Py is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PM4Py. If not, see <https://www.gnu.org/licenses/>.
'''
from enum import Enum
from pm4py.util import constants
class Parameters(Enum):
ACTIVITY_KEY = constants.PARAMETER_CONSTANT_ACTIVITY_KEY
'''
This file is part of PM4Py (More Info: https://pm4py.fit.fraunhofer.de).
PM4Py is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PM4Py is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PM4Py. If not, see <https://www.gnu.org/licenses/>.
'''
from evaluation.generalization.variants import token_based
File added
File added
'''
This file is part of PM4Py (More Info: https://pm4py.fit.fraunhofer.de).
PM4Py is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PM4Py is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PM4Py. If not, see <https://www.gnu.org/licenses/>.
'''
from collections import Counter
from math import sqrt
from pm4py import util as pmutil
from pm4py.algo.conformance.tokenreplay import algorithm as token_replay
from evaluation.generalization.parameters import Parameters
from pm4py.util import exec_utils
def get_generalization(petri_net, aligned_traces):
"""
Gets the generalization from the Petri net and the list of activated transitions
during the replay
The approach has been suggested by the paper
Buijs, Joos CAM, Boudewijn F. van Dongen, and Wil MP van der Aalst. "Quality dimensions in process discovery:
The importance of fitness, precision, generalization and simplicity."
International Journal of Cooperative Information Systems 23.01 (2014): 1440001.
A token replay is applied and, for each transition, we can measure the number of occurrences
in the replay. The following formula is applied for generalization
\sum_{t \in transitions} (math.sqrt(1.0/(n_occ_replay(t)))
1 - ----------------------------------------------------------
# transitions
Parameters
-----------
petri_net
Petri net
aligned_traces
Result of the token-replay
Returns
-----------
generalization
Generalization measure
"""
trans_occ_map = Counter()
for trace in aligned_traces:
for trans in trace["activated_transitions"]:
trans_occ_map[trans] += 1
inv_sq_occ_sum = 0.0
for trans in trans_occ_map:
this_term = 1.0 / sqrt(trans_occ_map[trans])
inv_sq_occ_sum = inv_sq_occ_sum + this_term
for trans in petri_net.transitions:
if trans not in trans_occ_map:
inv_sq_occ_sum = inv_sq_occ_sum + 1
generalization = 1.0
if len(petri_net.transitions) > 0:
generalization = 1.0 - inv_sq_occ_sum / float(len(petri_net.transitions))
return generalization
def apply(log, petri_net, initial_marking, final_marking, parameters=None):
"""
Calculates generalization on the provided log and Petri net.
The approach has been suggested by the paper
Buijs, Joos CAM, Boudewijn F. van Dongen, and Wil MP van der Aalst. "Quality dimensions in process discovery:
The importance of fitness, precision, generalization and simplicity."
International Journal of Cooperative Information Systems 23.01 (2014): 1440001.
A token replay is applied and, for each transition, we can measure the number of occurrences
in the replay. The following formula is applied for generalization
\sum_{t \in transitions} (math.sqrt(1.0/(n_occ_replay(t)))
1 - ----------------------------------------------------------
# transitions
Parameters
-----------
log
Trace log
petri_net
Petri net
initial_marking
Initial marking
final_marking
Final marking
parameters
Algorithm parameters
Returns
-----------
generalization
Generalization measure
"""
if parameters is None:
parameters = {}
activity_key = exec_utils.get_param_value(Parameters.ACTIVITY_KEY, parameters, pmutil.xes_constants.DEFAULT_NAME_KEY)
parameters_tr = {Parameters.ACTIVITY_KEY: activity_key}
aligned_traces = token_replay.apply(log, petri_net, initial_marking, final_marking, parameters=parameters_tr)
return get_generalization(petri_net, aligned_traces)
'''
This file is part of PM4Py (More Info: https://pm4py.fit.fraunhofer.de).
PM4Py is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PM4Py is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PM4Py. If not, see <https://www.gnu.org/licenses/>.
'''
from evaluation.precision import evaluator, variants
File added
File added
File added
File added
'''
This file is part of PM4Py (More Info: https://pm4py.fit.fraunhofer.de).
PM4Py is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PM4Py is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PM4Py. If not, see <https://www.gnu.org/licenses/>.
'''
from evaluation.precision.variants import etconformance_token, align_etconformance
from pm4py.objects.conversion.log import converter as log_conversion
from pm4py.objects.petri_net.utils.check_soundness import check_easy_soundness_net_in_fin_marking
from enum import Enum
from pm4py.util import exec_utils
import deprecation
from pm4py.meta import VERSION
import warnings
class Variants(Enum):
ETCONFORMANCE_TOKEN = etconformance_token
ALIGN_ETCONFORMANCE = align_etconformance
ETCONFORMANCE_TOKEN = Variants.ETCONFORMANCE_TOKEN
ALIGN_ETCONFORMANCE = Variants.ALIGN_ETCONFORMANCE
VERSIONS = {ETCONFORMANCE_TOKEN, ALIGN_ETCONFORMANCE}
@deprecation.deprecated(deprecated_in="2.2.5", removed_in="3.0",
current_version=VERSION,
details="Use the pm4py.algo.evaluation.precision package")
def apply(log, net, marking, final_marking, parameters=None, variant=None):
"""
Method to apply ET Conformance
Parameters
-----------
log
Trace log
net
Petri net
marking
Initial marking
final_marking
Final marking
parameters
Parameters of the algorithm, including:
pm4py.util.constants.PARAMETER_CONSTANT_ACTIVITY_KEY -> Activity key
variant
Variant of the algorithm that should be applied:
- Variants.ETCONFORMANCE_TOKEN
- Variants.ALIGN_ETCONFORMANCE
"""
warnings.warn("Use the pm4py.algo.evaluation.precision package")
if parameters is None:
parameters = {}
log = log_conversion.apply(log, parameters, log_conversion.TO_EVENT_LOG)
# execute the following part of code when the variant is not specified by the user
if variant is None:
if not (check_easy_soundness_net_in_fin_marking(
net,
marking,
final_marking)):
# in the case the net is not a easy sound workflow net, we must apply token-based replay
variant = ETCONFORMANCE_TOKEN
else:
# otherwise, use the align-etconformance approach (safer, in the case the model contains duplicates)
variant = ALIGN_ETCONFORMANCE
return exec_utils.get_variant(variant).apply(log, net, marking,
final_marking, parameters=parameters)
'''
This file is part of PM4Py (More Info: https://pm4py.fit.fraunhofer.de).
PM4Py is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PM4Py is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PM4Py. If not, see <https://www.gnu.org/licenses/>.
'''
from enum import Enum
from pm4py.util import constants
from pm4py.algo.conformance.tokenreplay import algorithm
class Parameters(Enum):
ACTIVITY_KEY = constants.PARAMETER_CONSTANT_ACTIVITY_KEY
TOKEN_REPLAY_VARIANT = "token_replay_variant"
CLEANING_TOKEN_FLOOD = "cleaning_token_flood"
SHOW_PROGRESS_BAR = "show_progress_bar"
'''
This file is part of PM4Py (More Info: https://pm4py.fit.fraunhofer.de).
PM4Py is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PM4Py is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PM4Py. If not, see <https://www.gnu.org/licenses/>.
'''
from collections import Counter
from pm4py.objects.log.obj import EventLog, Event, Trace
from pm4py.util import xes_constants as xes_util, constants
import heapq
from pm4py.objects.petri_net.utils.petri_utils import decorate_places_preset_trans, decorate_transitions_prepostset
from pm4py.objects.petri_net.utils import align_utils as utils
from pm4py.objects.petri_net.utils.incidence_matrix import construct
def __search(sync_net, ini, fin, stop, cost_function, skip):
decorate_transitions_prepostset(sync_net)
decorate_places_preset_trans(sync_net)
incidence_matrix = construct(sync_net)
ini_vec, fin_vec, cost_vec = utils.__vectorize_initial_final_cost(incidence_matrix, ini, fin, cost_function)
closed = set()
ini_state = utils.SearchTuple(0, 0, 0, ini, None, None, None, True)
open_set = [ini_state]
heapq.heapify(open_set)
visited = 0
queued = 0
traversed = 0
# return all the prefix markings of the optimal alignments as set
ret_markings = None
# keep track of the optimal cost of an alignment (to trim search when needed)
optimal_cost = None
while not len(open_set) == 0:
curr = heapq.heappop(open_set)
current_marking = curr.m
# trim alignments when we already reached an optimal alignment and the
# current cost is greater than the optimal cost
if optimal_cost is not None and curr.f > optimal_cost:
break
already_closed = current_marking in closed
if already_closed:
continue
if stop <= current_marking:
# add the current marking to the set
# of returned markings
if ret_markings is None:
ret_markings = set()
ret_markings.add(current_marking)
# close the marking
closed.add(current_marking)
# set the optimal cost
optimal_cost = curr.f
continue
closed.add(current_marking)
visited += 1
enabled_trans = set()
for p in current_marking:
for t in p.ass_trans:
if t.sub_marking <= current_marking:
enabled_trans.add(t)
trans_to_visit_with_cost = [(t, cost_function[t]) for t in enabled_trans if
not (t is None or utils.__is_log_move(t, skip) or (
utils.__is_model_move(t, skip) and not t.label[1] is None))]
for t, cost in trans_to_visit_with_cost:
traversed += 1
new_marking = utils.add_markings(current_marking, t.add_marking)
if new_marking in closed:
continue
g = curr.g + cost
queued += 1
new_f = g
tp = utils.SearchTuple(new_f, g, 0, new_marking, curr, t, None, True)
heapq.heappush(open_set, tp)
return ret_markings
def get_log_prefixes(log, activity_key=xes_util.DEFAULT_NAME_KEY):
"""
Get log prefixes
Parameters
----------
log
Trace log
activity_key
Activity key (must be provided if different from concept:name)
"""
prefixes = {}
prefix_count = Counter()
for trace in log:
for i in range(1, len(trace)):
red_trace = trace[0:i]
prefix = constants.DEFAULT_VARIANT_SEP.join([x[activity_key] for x in red_trace])
next_activity = trace[i][activity_key]
if prefix not in prefixes:
prefixes[prefix] = set()
prefixes[prefix].add(next_activity)
prefix_count[prefix] += 1
return prefixes, prefix_count
def form_fake_log(prefixes_keys, activity_key=xes_util.DEFAULT_NAME_KEY):
"""
Form fake log for replay (putting each prefix as separate trace to align)
Parameters
----------
prefixes_keys
Keys of the prefixes (to form a log with a given order)
activity_key
Activity key (must be provided if different from concept:name)
"""
fake_log = EventLog()
for prefix in prefixes_keys:
trace = Trace()
prefix_activities = prefix.split(constants.DEFAULT_VARIANT_SEP)
for activity in prefix_activities:
event = Event()
event[activity_key] = activity
trace.append(event)
fake_log.append(trace)
return fake_log
'''
This file is part of PM4Py (More Info: https://pm4py.fit.fraunhofer.de).
PM4Py is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PM4Py is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PM4Py. If not, see <https://www.gnu.org/licenses/>.
'''
from evaluation.precision.variants import etconformance_token, align_etconformance
File added
File added
File added
'''
This file is part of PM4Py (More Info: https://pm4py.fit.fraunhofer.de).
PM4Py is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PM4Py is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PM4Py. If not, see <https://www.gnu.org/licenses/>.
'''
from pm4py.objects import log as log_lib
from evaluation.precision import utils as precision_utils
from pm4py.objects.petri_net.utils import align_utils as utils, check_soundness
from pm4py.objects.petri_net.obj import Marking
from pm4py.objects.petri_net.utils.petri_utils import construct_trace_net
from pm4py.objects.petri_net.utils.synchronous_product import construct
from pm4py.statistics.start_activities.log.get import get_start_activities
from pm4py.objects.petri_net.utils.align_utils import get_visible_transitions_eventually_enabled_by_marking
from evaluation.precision.parameters import Parameters
from pm4py.util import exec_utils
from pm4py.util import xes_constants
import pkgutil
def apply(log, net, marking, final_marking, parameters=None):
"""
Get Align-ET Conformance precision
Parameters
----------
log
Trace log
net
Petri net
marking
Initial marking
final_marking
Final marking
parameters
Parameters of the algorithm, including:
Parameters.ACTIVITY_KEY -> Activity key
"""
if parameters is None:
parameters = {}
debug_level = parameters["debug_level"] if "debug_level" in parameters else 0
activity_key = exec_utils.get_param_value(Parameters.ACTIVITY_KEY, parameters, log_lib.util.xes.DEFAULT_NAME_KEY)
# default value for precision, when no activated transitions (not even by looking at the initial marking) are found
precision = 1.0
sum_ee = 0
sum_at = 0
unfit = 0
if not check_soundness.check_easy_soundness_net_in_fin_marking(net, marking, final_marking):
raise Exception("trying to apply Align-ETConformance on a Petri net that is not a easy sound net!!")
prefixes, prefix_count = precision_utils.get_log_prefixes(log, activity_key=activity_key)
prefixes_keys = list(prefixes.keys())
fake_log = precision_utils.form_fake_log(prefixes_keys, activity_key=activity_key)
align_stop_marking = align_fake_log_stop_marking(fake_log, net, marking, final_marking, parameters=parameters)
all_markings = transform_markings_from_sync_to_original_net(align_stop_marking, net, parameters=parameters)
for i in range(len(prefixes)):
markings = all_markings[i]
if markings is not None:
log_transitions = set(prefixes[prefixes_keys[i]])
activated_transitions_labels = set()
for m in markings:
# add to the set of activated transitions in the model the activated transitions
# for each prefix
activated_transitions_labels = activated_transitions_labels.union(
x.label for x in utils.get_visible_transitions_eventually_enabled_by_marking(net, m) if
x.label is not None)
escaping_edges = activated_transitions_labels.difference(log_transitions)
sum_at += len(activated_transitions_labels) * prefix_count[prefixes_keys[i]]
sum_ee += len(escaping_edges) * prefix_count[prefixes_keys[i]]
if debug_level > 1:
print("")
print("prefix=", prefixes_keys[i])
print("log_transitions=", log_transitions)
print("activated_transitions=", activated_transitions_labels)
print("escaping_edges=", escaping_edges)
else:
unfit += prefix_count[prefixes_keys[i]]
if debug_level > 0:
print("\n")
print("overall unfit", unfit)
print("overall activated transitions", sum_at)
print("overall escaping edges", sum_ee)
# fix: also the empty prefix should be counted!
start_activities = set(get_start_activities(log, parameters=parameters))
trans_en_ini_marking = set([x.label for x in get_visible_transitions_eventually_enabled_by_marking(net, marking)])
diff = trans_en_ini_marking.difference(start_activities)
sum_at += len(log) * len(trans_en_ini_marking)
sum_ee += len(log) * len(diff)
# end fix
if sum_at > 0:
precision = 1 - float(sum_ee) / float(sum_at)
return precision
def transform_markings_from_sync_to_original_net(markings0, net, parameters=None):
"""
Transform the markings of the sync net (in which alignment stops) into markings of the original net
(in order to measure the precision)
Parameters
-------------
markings0
Markings on the sync net (expressed as place name with count)
net
Petri net
parameters
Parameters of the algorithm
Returns
-------------
markings
Markings of the original model (expressed as place with count)
"""
if parameters is None:
parameters = {}
places_corr = {p.name: p for p in net.places}
markings = []
for i in range(len(markings0)):
res_list = markings0[i]
# res_list shall be a list of markings.
# If it is None, then there is no correspondence markings
# in the original Petri net
if res_list is not None:
# saves all the markings reached by the optimal alignment
# as markings of the original net
markings.append([])
for j in range(len(res_list)):
res = res_list[j]
atm = Marking()
for pl, count in res.items():
if pl[0] == utils.SKIP:
atm[places_corr[pl[1]]] = count
markings[-1].append(atm)
else:
markings.append(None)
return markings
def align_fake_log_stop_marking(fake_log, net, marking, final_marking, parameters=None):
"""
Align the 'fake' log with all the prefixes in order to get the markings in which
the alignment stops
Parameters
-------------
fake_log
Fake log
net
Petri net
marking
Marking
final_marking
Final marking
parameters
Parameters of the algorithm
Returns
-------------
alignment
For each trace in the log, return the marking in which the alignment stops (expressed as place name with count)
"""
if parameters is None:
parameters = {}
show_progress_bar = exec_utils.get_param_value(Parameters.SHOW_PROGRESS_BAR, parameters, True)
align_result = []
progress = None
if pkgutil.find_loader("tqdm") and show_progress_bar and len(fake_log) > 1:
from tqdm.auto import tqdm
progress = tqdm(total=len(fake_log), desc="computing precision with alignments, completed variants :: ")
for i in range(len(fake_log)):
trace = fake_log[i]
sync_net, sync_initial_marking, sync_final_marking = build_sync_net(trace, net, marking, final_marking,
parameters=parameters)
stop_marking = Marking()
for pl, count in sync_final_marking.items():
if pl.name[1] == utils.SKIP:
stop_marking[pl] = count
cost_function = utils.construct_standard_cost_function(sync_net, utils.SKIP)
# perform the alignment of the prefix
res = precision_utils.__search(sync_net, sync_initial_marking, sync_final_marking, stop_marking, cost_function,
utils.SKIP)
if res is not None:
align_result.append([])
for mark in res:
res2 = {}
for pl in mark:
# transforms the markings for easier correspondence at the end
# (distributed engine friendly!)
res2[(pl.name[0], pl.name[1])] = mark[pl]
align_result[-1].append(res2)
else:
# if there is no path from the initial marking
# replaying the given prefix, then add None
align_result.append(None)
if progress is not None:
progress.update()
# gracefully close progress bar
if progress is not None:
progress.close()
del progress
return align_result
def build_sync_net(trace, petri_net, initial_marking, final_marking, parameters=None):
"""
Build the sync product net between the Petri net and the trace prefix
Parameters
---------------
trace
Trace prefix
petri_net
Petri net
initial_marking
Initial marking
final_marking
Final marking
parameters
Possible parameters of the algorithm
"""
if parameters is None:
parameters = {}
activity_key = exec_utils.get_param_value(Parameters.ACTIVITY_KEY, parameters, xes_constants.DEFAULT_NAME_KEY)
trace_net, trace_im, trace_fm = construct_trace_net(trace, activity_key=activity_key)
sync_prod, sync_initial_marking, sync_final_marking = construct(trace_net, trace_im,
trace_fm, petri_net,
initial_marking,
final_marking,
utils.SKIP)
return sync_prod, sync_initial_marking, sync_final_marking
'''
This file is part of PM4Py (More Info: https://pm4py.fit.fraunhofer.de).
PM4Py is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PM4Py is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PM4Py. If not, see <https://www.gnu.org/licenses/>.
'''
from pm4py.algo.conformance.tokenreplay.variants import token_replay
from pm4py.algo.conformance.tokenreplay import algorithm as executor
from pm4py.objects import log as log_lib
from evaluation.precision import utils as precision_utils
from pm4py.statistics.start_activities.log.get import get_start_activities
from pm4py.objects.petri_net.utils.align_utils import get_visible_transitions_eventually_enabled_by_marking
from evaluation.precision.parameters import Parameters
from pm4py.util import exec_utils
"""
Implementation of the approach described in paper
Muñoz-Gama, Jorge, and Josep Carmona. "A fresh look at precision in process conformance." International Conference
on Business Process Management. Springer, Berlin, Heidelberg, 2010.
for measuring precision.
For each prefix in the log, the reflected tasks are calculated (outgoing attributes from the prefix)
Then, a token replay is done on the prefix in order to get activated transitions
Escaping edges is the set difference between activated transitions and reflected tasks
Then, precision is calculated by the formula used in the paper
At the moment, the precision value is different from the one provided by the ProM plug-in,
although the implementation seems to follow the paper concept
"""
def apply(log, net, marking, final_marking, parameters=None):
"""
Get ET Conformance precision
Parameters
----------
log
Trace log
net
Petri net
marking
Initial marking
final_marking
Final marking
parameters
Parameters of the algorithm, including:
Parameters.ACTIVITY_KEY -> Activity key
"""
if parameters is None:
parameters = {}
cleaning_token_flood = exec_utils.get_param_value(Parameters.CLEANING_TOKEN_FLOOD, parameters, False)
token_replay_variant = exec_utils.get_param_value(Parameters.TOKEN_REPLAY_VARIANT, parameters,
executor.Variants.TOKEN_REPLAY)
activity_key = exec_utils.get_param_value(Parameters.ACTIVITY_KEY, parameters, log_lib.util.xes.DEFAULT_NAME_KEY)
# default value for precision, when no activated transitions (not even by looking at the initial marking) are found
precision = 1.0
sum_ee = 0
sum_at = 0
parameters_tr = {
token_replay.Parameters.CONSIDER_REMAINING_IN_FITNESS: False,
token_replay.Parameters.TRY_TO_REACH_FINAL_MARKING_THROUGH_HIDDEN: False,
token_replay.Parameters.STOP_IMMEDIATELY_UNFIT: True,
token_replay.Parameters.WALK_THROUGH_HIDDEN_TRANS: True,
token_replay.Parameters.CLEANING_TOKEN_FLOOD: cleaning_token_flood,
token_replay.Parameters.ACTIVITY_KEY: activity_key
}
prefixes, prefix_count = precision_utils.get_log_prefixes(log, activity_key=activity_key)
prefixes_keys = list(prefixes.keys())
fake_log = precision_utils.form_fake_log(prefixes_keys, activity_key=activity_key)
aligned_traces = executor.apply(fake_log, net, marking, final_marking, variant=token_replay_variant,
parameters=parameters_tr)
# fix: also the empty prefix should be counted!
start_activities = set(get_start_activities(log, parameters=parameters))
trans_en_ini_marking = set([x.label for x in get_visible_transitions_eventually_enabled_by_marking(net, marking)])
diff = trans_en_ini_marking.difference(start_activities)
sum_at += len(log) * len(trans_en_ini_marking)
sum_ee += len(log) * len(diff)
# end fix
for i in range(len(aligned_traces)):
if aligned_traces[i]["trace_is_fit"]:
log_transitions = set(prefixes[prefixes_keys[i]])
activated_transitions_labels = set(
[x.label for x in aligned_traces[i]["enabled_transitions_in_marking"] if x.label is not None])
sum_at += len(activated_transitions_labels) * prefix_count[prefixes_keys[i]]
escaping_edges = activated_transitions_labels.difference(log_transitions)
sum_ee += len(escaping_edges) * prefix_count[prefixes_keys[i]]
if sum_at > 0:
precision = 1 - float(sum_ee) / float(sum_at)
return precision
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment