Spaces:
Running
Running
File size: 12,833 Bytes
e60e568 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
'''
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.log.util import xes
from pm4py.algo.discovery.log_skeleton import trace_skel
from pm4py.util import xes_constants
from pm4py.util import variants_util, pandas_utils
from pm4py.util import exec_utils
from typing import Optional, Dict, Any, Union, List, Set
from pm4py.objects.log.obj import EventLog, Trace
import pandas as pd
from enum import Enum
from pm4py.util.constants import PARAMETER_CONSTANT_ACTIVITY_KEY, PARAMETER_CONSTANT_CASEID_KEY, CASE_CONCEPT_NAME
class Parameters(Enum):
# parameter for the noise threshold
NOISE_THRESHOLD = "noise_threshold"
# considered constraints in conformance checking among: equivalence, always_after, always_before, never_together, directly_follows, activ_freq
CONSIDERED_CONSTRAINTS = "considered_constraints"
# default choice for conformance checking
DEFAULT_CONSIDERED_CONSTRAINTS = ["equivalence", "always_after", "always_before", "never_together",
"directly_follows", "activ_freq"]
CASE_ID_KEY = PARAMETER_CONSTANT_CASEID_KEY
ACTIVITY_KEY = PARAMETER_CONSTANT_ACTIVITY_KEY
PARAMETER_VARIANT_DELIMITER = "variant_delimiter"
NOISE_THRESHOLD = Parameters.NOISE_THRESHOLD
CONSIDERED_CONSTRAINTS = Parameters.CONSIDERED_CONSTRAINTS
DEFAULT_CONSIDERED_CONSTRAINTS = Parameters.DEFAULT_CONSIDERED_CONSTRAINTS
ACTIVITY_KEY = Parameters.ACTIVITY_KEY
PARAMETER_VARIANT_DELIMITER = Parameters.PARAMETER_VARIANT_DELIMITER
class DiscoveryOutputs(Enum):
EQUIVALENCE = "equivalence"
ALWAYS_AFTER = "always_after"
ALWAYS_BEFORE = "always_before"
NEVER_TOGETHER = "never_together"
DIRECTLY_FOLLOWS = "directly_follows"
ACTIV_FREQ = "activ_freq"
class Outputs(Enum):
DEVIATIONS = "deviations"
NO_DEV_TOTAL = "no_dev_total"
NO_CONSTR_TOTAL = "no_constr_total"
DEV_FITNESS = "dev_fitness"
IS_FIT = "is_fit"
def apply_log(log: Union[EventLog, pd.DataFrame], model: Dict[str, Any], parameters: Optional[Dict[Union[str, Parameters], Any]] = None) -> List[Set[Any]]:
"""
Apply log-skeleton based conformance checking given an event log
and a log-skeleton model
Parameters
--------------
log
Event log
model
Log-skeleton model
parameters
Parameters of the algorithm, including:
- Parameters.ACTIVITY_KEY
- Parameters.CONSIDERED_CONSTRAINTS, among: equivalence, always_after, always_before, never_together, directly_follows, activ_freq
Returns
--------------
aligned_traces
Conformance checking results for each trace:
- Outputs.IS_FIT => boolean that tells if the trace is perfectly fit according to the model
- Outputs.DEV_FITNESS => deviation based fitness (between 0 and 1; the more the trace is near to 1 the more fit is)
- Outputs.DEVIATIONS => list of deviations in the model
"""
if parameters is None:
parameters = {}
activity_key = exec_utils.get_param_value(Parameters.ACTIVITY_KEY, parameters, xes.DEFAULT_NAME_KEY)
if pandas_utils.check_is_pandas_dataframe(log):
case_id_key = exec_utils.get_param_value(Parameters.CASE_ID_KEY, parameters, CASE_CONCEPT_NAME)
traces = [tuple(x) for x in log.groupby(case_id_key)[activity_key].agg(list).to_dict().values()]
else:
traces = [tuple(y[activity_key] for y in x) for x in log]
grouped_traces = {}
gtk = []
inv_idxs = {}
for i in range(len(traces)):
tr = traces[i]
if not tr in grouped_traces:
grouped_traces[tr] = []
gtk.append(tr)
grouped_traces[tr].append(i)
inv_idxs[i] = gtk.index(tr)
res0 = []
for trace in grouped_traces:
res0.append(apply_actlist(trace, model, parameters=parameters))
res = []
for i in range(len(traces)):
res.append(res0[inv_idxs[i]])
return res
def apply_trace(trace: Trace, model: Dict[str, Any], parameters: Optional[Dict[Union[str, Parameters], Any]] = None) -> List[Set[Any]]:
"""
Apply log-skeleton based conformance checking given a trace
and a log-skeleton model
Parameters
--------------
trace
Trace
model
Log-skeleton model
parameters
Parameters of the algorithm, including:
- the activity key (pm4py:param:activity_key)
- the list of considered constraints (considered_constraints) among: equivalence, always_after, always_before, never_together, directly_follows, activ_freq
Returns
--------------
aligned_trace
Containing:
- is_fit => boolean that tells if the trace is perfectly fit according to the model
- dev_fitness => deviation based fitness (between 0 and 1; the more the trace is near to 1 the more fit is)
- deviations => list of deviations in the model
"""
if parameters is None:
parameters = {}
activity_key = exec_utils.get_param_value(Parameters.ACTIVITY_KEY, parameters, xes.DEFAULT_NAME_KEY)
trace = [x[activity_key] for x in trace]
return apply_actlist(trace, model, parameters=parameters)
def apply_actlist(trace, model, parameters=None):
"""
Apply log-skeleton based conformance checking given the list of activities of a trace
and a log-skeleton model
Parameters
--------------
trace
List of activities of a trace
model
Log-skeleton model
parameters
Parameters of the algorithm, including:
- the activity key (pm4py:param:activity_key)
- the list of considered constraints (considered_constraints) among: equivalence, always_after, always_before, never_together, directly_follows, activ_freq
Returns
--------------
aligned_trace
Containing:
- is_fit => boolean that tells if the trace is perfectly fit according to the model
- dev_fitness => deviation based fitness (between 0 and 1; the more the trace is near to 1 the more fit is)
- deviations => list of deviations in the model
"""
if parameters is None:
parameters = {}
consid_constraints = exec_utils.get_param_value(Parameters.CONSIDERED_CONSTRAINTS, parameters, Parameters.DEFAULT_CONSIDERED_CONSTRAINTS.value)
trace_info = trace_skel.get_trace_info(trace)
ret = {}
ret[Outputs.DEVIATIONS.value] = []
dev_total = 0
conf_total = 0
default_considered_constraints = Parameters.DEFAULT_CONSIDERED_CONSTRAINTS.value
i = 0
while i < len(default_considered_constraints):
if default_considered_constraints[i] in consid_constraints:
if default_considered_constraints[i] == DiscoveryOutputs.ACTIV_FREQ.value:
this_constraints = {x: y for x, y in model[default_considered_constraints[i]].items()}
conf_total += len(list(act for act in trace_info[i] if act in this_constraints)) + len(list(act for act in trace_info[i] if act not in this_constraints)) + len(list(act for act in this_constraints if min(this_constraints[act]) > 0 and not act in trace))
for act in trace_info[i]:
if act in this_constraints:
if trace_info[i][act] not in this_constraints[act]:
dev_total += 1
ret[Outputs.DEVIATIONS.value].append((default_considered_constraints[i], (act, trace_info[i][act])))
else:
dev_total += 1
ret[Outputs.DEVIATIONS.value].append((default_considered_constraints[i], (act, 0)))
for act in this_constraints:
if min(this_constraints[act]) > 0 and not act in trace:
dev_total += 1
ret[Outputs.DEVIATIONS.value].append((default_considered_constraints[i], (act, 0)))
elif default_considered_constraints[i] == DiscoveryOutputs.NEVER_TOGETHER.value:
this_constraints = {x for x in model[default_considered_constraints[i]] if x[0] in trace}
conf_total += len(this_constraints)
setinte = this_constraints.intersection(trace_info[i])
dev_total += len(setinte)
if len(setinte) > 0:
ret[Outputs.DEVIATIONS.value].append((default_considered_constraints[i], tuple(setinte)))
else:
this_constraints = {x for x in model[default_considered_constraints[i]] if x[0] in trace}
conf_total += len(this_constraints)
setdiff = this_constraints.difference(trace_info[i])
dev_total += len(setdiff)
if len(setdiff) > 0:
ret[Outputs.DEVIATIONS.value].append((default_considered_constraints[i], tuple(setdiff)))
i = i + 1
ret[Outputs.NO_DEV_TOTAL.value] = dev_total
ret[Outputs.NO_CONSTR_TOTAL.value] = conf_total
ret[Outputs.DEV_FITNESS.value] = 1.0 - float(dev_total)/float(conf_total) if conf_total > 0 else 1.0
ret[Outputs.DEVIATIONS.value] = sorted(ret[Outputs.DEVIATIONS.value], key=lambda x: (x[0], x[1]))
ret[Outputs.IS_FIT.value] = len(ret[Outputs.DEVIATIONS.value]) == 0
return ret
def apply_from_variants_list(var_list, model, parameters=None):
"""
Performs conformance checking using the log skeleton,
applying it from a list of variants
Parameters
--------------
var_list
List of variants
model
Log skeleton model
parameters
Parameters
Returns
--------------
conformance_dictio
Dictionary containing, for each variant, the result
of log skeleton checking
"""
if parameters is None:
parameters = {}
conformance_output = {}
for cv in var_list:
v = cv[0]
trace = variants_util.variant_to_trace(v, parameters=parameters)
conformance_output[v] = apply_trace(trace, model, parameters=parameters)
return conformance_output
def after_decode(log_skeleton):
"""
Prepares the log skeleton after decoding
Parameters
--------------
log_skeleton
Log skeleton
Returns
--------------
log_skeleton
Log skeleton (with sets instead of lists)
"""
log_skeleton[DiscoveryOutputs.EQUIVALENCE.value] = set(log_skeleton[DiscoveryOutputs.EQUIVALENCE.value])
log_skeleton[DiscoveryOutputs.ALWAYS_AFTER.value] = set(log_skeleton[DiscoveryOutputs.ALWAYS_AFTER.value])
log_skeleton[DiscoveryOutputs.ALWAYS_BEFORE.value] = set(log_skeleton[DiscoveryOutputs.ALWAYS_BEFORE.value])
log_skeleton[DiscoveryOutputs.NEVER_TOGETHER.value] = set(log_skeleton[DiscoveryOutputs.NEVER_TOGETHER.value])
log_skeleton[DiscoveryOutputs.DIRECTLY_FOLLOWS.value] = set(log_skeleton[DiscoveryOutputs.DIRECTLY_FOLLOWS.value])
for act in log_skeleton[DiscoveryOutputs.ACTIV_FREQ.value]:
log_skeleton[DiscoveryOutputs.ACTIV_FREQ.value][act] = set(log_skeleton[DiscoveryOutputs.ACTIV_FREQ.value][act])
return log_skeleton
def get_diagnostics_dataframe(log, conf_result, parameters=None):
"""
Gets the diagnostics dataframe from a log and the results
of log skeleton-based conformance checking
Parameters
--------------
log
Event log
conf_result
Results of conformance checking
Returns
--------------
diagn_dataframe
Diagnostics dataframe
"""
if parameters is None:
parameters = {}
case_id_key = exec_utils.get_param_value(Parameters.CASE_ID_KEY, parameters, xes_constants.DEFAULT_TRACEID_KEY)
import pandas as pd
diagn_stream = []
for index in range(len(log)):
case_id = log[index].attributes[case_id_key]
no_dev_total = conf_result[index][Outputs.NO_DEV_TOTAL.value]
no_constr_total = conf_result[index][Outputs.NO_CONSTR_TOTAL.value]
dev_fitness = conf_result[index][Outputs.DEV_FITNESS.value]
diagn_stream.append({"case_id": case_id, "no_dev_total": no_dev_total, "no_constr_total": no_constr_total, "dev_fitness": dev_fitness})
return pandas_utils.instantiate_dataframe(diagn_stream)
|