Spaces:
Running
Running
File size: 18,000 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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
'''
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.objects.log.obj import EventLog
import pandas as pd
from typing import Union, Dict, Optional, Any, List
from pm4py.algo.discovery.declare.templates import *
from pm4py.util import exec_utils, constants, xes_constants, pandas_utils
from collections import Counter
class Parameters(Enum):
CASE_ID_KEY = constants.PARAMETER_CONSTANT_CASEID_KEY
ACTIVITY_KEY = constants.PARAMETER_CONSTANT_ACTIVITY_KEY
def __check_existence(trace: List[str], model: Dict[str, Dict[Any, Dict[str, int]]], trace_dict: Dict[str, List[Any]],
parameters: Optional[Dict[Any, Any]] = None):
if EXISTENCE in model:
for act in model[EXISTENCE]:
if act not in trace:
trace_dict["deviations"].append([EXISTENCE, act])
def __check_absence(trace: List[str], model: Dict[str, Dict[Any, Dict[str, int]]], trace_dict: Dict[str, List[Any]],
parameters: Optional[Dict[Any, Any]] = None):
if ABSENCE in model:
for act in model[ABSENCE]:
if act in trace:
trace_dict["deviations"].append([ABSENCE, act])
def __check_exactly_one(trace: List[str], model: Dict[str, Dict[Any, Dict[str, int]]], trace_dict: Dict[str, List[Any]],
parameters: Optional[Dict[Any, Any]] = None):
if EXACTLY_ONE in model:
trace_counter = Counter(trace)
for act in model[EXACTLY_ONE]:
if trace_counter[act] != 1:
trace_dict["deviations"].append([EXACTLY_ONE, act])
def __check_init(trace: List[str], model: Dict[str, Dict[Any, Dict[str, int]]], trace_dict: Dict[str, List[Any]],
parameters: Optional[Dict[Any, Any]] = None):
if INIT in model:
for act in model[INIT]:
if len(trace) == 0 or trace[0] != act:
trace_dict["deviations"].append([INIT, act])
def __check_responded_existence(trace: List[str], model: Dict[str, Dict[Any, Dict[str, int]]],
trace_dict: Dict[str, List[Any]], parameters: Optional[Dict[Any, Any]] = None):
if RESPONDED_EXISTENCE in model:
for act_couple in model[RESPONDED_EXISTENCE]:
if act_couple[0] in trace and act_couple[1] not in trace:
trace_dict["deviations"].append([RESPONDED_EXISTENCE, act_couple])
def __check_coexistence(trace: List[str], model: Dict[str, Dict[Any, Dict[str, int]]], trace_dict: Dict[str, List[Any]],
parameters: Optional[Dict[Any, Any]] = None):
if COEXISTENCE in model:
for act_couple in model[COEXISTENCE]:
if (act_couple[0] in trace and act_couple[1] not in trace) or (
act_couple[1] in trace and act_couple[0] not in trace):
trace_dict["deviations"].append([COEXISTENCE, act_couple])
def __check_non_coexistence(trace: List[str], model: Dict[str, Dict[Any, Dict[str, int]]],
trace_dict: Dict[str, List[Any]], parameters: Optional[Dict[Any, Any]] = None):
if NONCOEXISTENCE in model:
for act_couple in model[NONCOEXISTENCE]:
if act_couple[0] in trace and act_couple[1] in trace:
trace_dict["deviations"].append([NONCOEXISTENCE, act_couple])
def __check_response(trace: List[str], model: Dict[str, Dict[Any, Dict[str, int]]], trace_dict: Dict[str, List[Any]],
act_idxs: Dict[str, List[int]], parameters: Optional[Dict[Any, Any]] = None):
if RESPONSE in model:
for act_couple in model[RESPONSE]:
if act_couple[0] in trace:
if (not act_couple[1] in trace) or max(act_idxs[act_couple[0]]) > max(act_idxs[act_couple[1]]):
trace_dict["deviations"].append([RESPONSE, act_couple])
def __check_precedence(trace: List[str], model: Dict[str, Dict[Any, Dict[str, int]]], trace_dict: Dict[str, List[Any]],
act_idxs: Dict[str, List[int]], parameters: Optional[Dict[Any, Any]] = None):
if PRECEDENCE in model:
for act_couple in model[PRECEDENCE]:
if act_couple[1] in trace:
if (not act_couple[0] in trace) or min(act_idxs[act_couple[0]]) > min(act_idxs[act_couple[1]]):
trace_dict["deviations"].append([PRECEDENCE, act_couple])
def __check_succession(trace: List[str], model: Dict[str, Dict[Any, Dict[str, int]]], trace_dict: Dict[str, List[Any]],
act_idxs: Dict[str, List[int]], parameters: Optional[Dict[Any, Any]] = None):
if SUCCESSION in model:
for act_couple in model[SUCCESSION]:
if (not act_couple[0] in trace or not act_couple[1] in trace) or min(act_idxs[act_couple[0]]) > min(
act_idxs[act_couple[1]]) or max(act_idxs[act_couple[0]]) > max(act_idxs[act_couple[1]]):
trace_dict["deviations"].append([SUCCESSION, act_couple])
def __check_alt_response(trace: List[str], model: Dict[str, Dict[Any, Dict[str, int]]],
trace_dict: Dict[str, List[Any]], act_idxs: Dict[str, List[int]],
parameters: Optional[Dict[Any, Any]] = None):
if ALTRESPONSE in model:
for act_couple in model[RESPONSE]:
spec_idxs = []
if act_couple[0] in trace:
spec_idxs = spec_idxs + [(act_couple[0], i) for i in act_idxs[act_couple[0]]]
if act_couple[1] in trace:
spec_idxs = spec_idxs + [(act_couple[1], i) for i in act_idxs[act_couple[1]]]
spec_idxs = sorted(spec_idxs, key=lambda x: (x[1], x[0]))
while spec_idxs:
if spec_idxs[0][0] != act_couple[0]:
del spec_idxs[0]
else:
break
is_ok = True
for i in range(len(spec_idxs)):
if i % 2 == 0 and (spec_idxs[i][0] != act_couple[0] or i == len(spec_idxs) - 1 or spec_idxs[i + 1][0] !=
act_couple[1]):
is_ok = False
break
if not is_ok:
trace_dict["deviations"].append([ALTRESPONSE, act_couple])
def __check_chain_response(trace: List[str], model: Dict[str, Dict[Any, Dict[str, int]]],
trace_dict: Dict[str, List[Any]], act_idxs: Dict[str, List[int]],
parameters: Optional[Dict[Any, Any]] = None):
if CHAINRESPONSE in model:
for act_couple in model[CHAINRESPONSE]:
spec_idxs = []
if act_couple[0] in trace:
spec_idxs = spec_idxs + [(act_couple[0], i) for i in act_idxs[act_couple[0]]]
if act_couple[1] in trace:
spec_idxs = spec_idxs + [(act_couple[1], i) for i in act_idxs[act_couple[1]]]
spec_idxs = sorted(spec_idxs, key=lambda x: (x[1], x[0]))
while spec_idxs:
if spec_idxs[0][0] != act_couple[0]:
del spec_idxs[0]
else:
break
is_ok = True
for i in range(len(spec_idxs)):
if i % 2 == 0 and (spec_idxs[i][0] != act_couple[0] or i == len(spec_idxs) - 1 or spec_idxs[i + 1][0] !=
act_couple[1] or spec_idxs[i + 1][1] != spec_idxs[i][1] + 1):
is_ok = False
break
if not is_ok:
trace_dict["deviations"].append([CHAINRESPONSE, act_couple])
def __check_alt_precedence(trace: List[str], model: Dict[str, Dict[Any, Dict[str, int]]],
trace_dict: Dict[str, List[Any]], act_idxs: Dict[str, List[int]],
parameters: Optional[Dict[Any, Any]] = None):
if ALTPRECEDENCE in model:
for act_couple in model[ALTPRECEDENCE]:
spec_idxs = []
if act_couple[0] in trace:
spec_idxs = spec_idxs + [(act_couple[0], i) for i in act_idxs[act_couple[0]]]
if act_couple[1] in trace:
spec_idxs = spec_idxs + [(act_couple[1], i) for i in act_idxs[act_couple[1]]]
spec_idxs = sorted(spec_idxs, key=lambda x: (x[1], x[0]))
while len(spec_idxs) > 1:
if spec_idxs[1][0] != act_couple[1]:
del spec_idxs[0]
else:
break
is_ok = True
for i in range(len(spec_idxs)):
if i % 2 == 0 and (spec_idxs[i][0] != act_couple[0] or i == len(spec_idxs) - 1 or spec_idxs[i + 1][0] !=
act_couple[1]):
is_ok = False
break
if not is_ok:
trace_dict["deviations"].append([ALTPRECEDENCE, act_couple])
def __check_chain_precedence(trace: List[str], model: Dict[str, Dict[Any, Dict[str, int]]],
trace_dict: Dict[str, List[Any]], act_idxs: Dict[str, List[int]],
parameters: Optional[Dict[Any, Any]] = None):
if CHAINPRECEDENCE in model:
for act_couple in model[CHAINPRECEDENCE]:
spec_idxs = []
if act_couple[0] in trace:
spec_idxs = spec_idxs + [(act_couple[0], i) for i in act_idxs[act_couple[0]]]
if act_couple[1] in trace:
spec_idxs = spec_idxs + [(act_couple[1], i) for i in act_idxs[act_couple[1]]]
spec_idxs = sorted(spec_idxs, key=lambda x: (x[1], x[0]))
while len(spec_idxs) > 1:
if spec_idxs[1][0] != act_couple[1]:
del spec_idxs[0]
else:
break
is_ok = True
for i in range(len(spec_idxs)):
if i % 2 == 0 and (spec_idxs[i][0] != act_couple[0] or i == len(spec_idxs) - 1 or spec_idxs[i + 1][0] !=
act_couple[1] or spec_idxs[i + 1][1] != spec_idxs[i][1] + 1):
is_ok = False
break
if not is_ok:
trace_dict["deviations"].append([CHAINPRECEDENCE, act_couple])
def __check_alt_succession(trace: List[str], model: Dict[str, Dict[Any, Dict[str, int]]],
trace_dict: Dict[str, List[Any]], act_idxs: Dict[str, List[int]],
parameters: Optional[Dict[Any, Any]] = None):
if ALTSUCCESSION in model:
for act_couple in model[ALTSUCCESSION]:
spec_idxs = []
if act_couple[0] in trace:
spec_idxs = spec_idxs + [(act_couple[0], i) for i in act_idxs[act_couple[0]]]
if act_couple[1] in trace:
spec_idxs = spec_idxs + [(act_couple[1], i) for i in act_idxs[act_couple[1]]]
spec_idxs = sorted(spec_idxs, key=lambda x: (x[1], x[0]))
is_ok = True
for i in range(len(spec_idxs)):
if i % 2 == 0 and (spec_idxs[i][0] != act_couple[0] or i == len(spec_idxs) - 1 or spec_idxs[i + 1][0] !=
act_couple[1]):
is_ok = False
break
if not is_ok:
trace_dict["deviations"].append([ALTSUCCESSION, act_couple])
def __check_chain_succession(trace: List[str], model: Dict[str, Dict[Any, Dict[str, int]]],
trace_dict: Dict[str, List[Any]], act_idxs: Dict[str, List[int]],
parameters: Optional[Dict[Any, Any]] = None):
if CHAINSUCCESSION in model:
for act_couple in model[CHAINSUCCESSION]:
spec_idxs = []
if act_couple[0] in trace:
spec_idxs = spec_idxs + [(act_couple[0], i) for i in act_idxs[act_couple[0]]]
if act_couple[1] in trace:
spec_idxs = spec_idxs + [(act_couple[1], i) for i in act_idxs[act_couple[1]]]
spec_idxs = sorted(spec_idxs, key=lambda x: (x[1], x[0]))
is_ok = True
for i in range(len(spec_idxs)):
if i % 2 == 0 and (spec_idxs[i][0] != act_couple[0] or i == len(spec_idxs) - 1 or spec_idxs[i + 1][0] !=
act_couple[1] or spec_idxs[i + 1][1] != spec_idxs[i][1] + 1):
is_ok = False
break
if not is_ok:
trace_dict["deviations"].append([CHAINSUCCESSION, act_couple])
def apply_list(projected_log: List[List[str]], model: Dict[str, Dict[Any, Dict[str, int]]],
parameters: Optional[Dict[Any, Any]] = None) -> List[Dict[str, Any]]:
if parameters is None:
parameters = {}
conf_cases = []
total_num_constraints = 0
for k in model:
total_num_constraints += len(model[k])
for trace in projected_log:
act_idxs = {}
for i in range(len(trace)):
if trace[i] not in act_idxs:
act_idxs[trace[i]] = []
act_idxs[trace[i]].append(i)
ret = {}
ret["no_constr_total"] = total_num_constraints
ret["deviations"] = []
__check_existence(trace, model, ret, parameters)
__check_exactly_one(trace, model, ret, parameters)
__check_init(trace, model, ret, parameters)
__check_responded_existence(trace, model, ret, parameters)
__check_coexistence(trace, model, ret, parameters)
__check_non_coexistence(trace, model, ret, parameters)
__check_response(trace, model, ret, act_idxs, parameters)
__check_precedence(trace, model, ret, act_idxs, parameters)
__check_succession(trace, model, ret, act_idxs, parameters)
__check_alt_response(trace, model, ret, act_idxs, parameters)
__check_chain_response(trace, model, ret, act_idxs, parameters)
__check_alt_precedence(trace, model, ret, act_idxs, parameters)
__check_chain_precedence(trace, model, ret, act_idxs, parameters)
__check_alt_succession(trace, model, ret, act_idxs, parameters)
__check_chain_succession(trace, model, ret, act_idxs, parameters)
__check_absence(trace, model, ret, parameters)
__check_non_coexistence(trace, model, ret, parameters)
ret["no_dev_total"] = len(ret["deviations"])
ret["dev_fitness"] = 1.0 - ret["no_dev_total"] / ret["no_constr_total"] if ret["no_constr_total"] > 0 else 1.0
ret["is_fit"] = ret["no_dev_total"] == 0
conf_cases.append(ret)
return conf_cases
def apply(log: Union[EventLog, pd.DataFrame], model: Dict[str, Dict[Any, Dict[str, int]]],
parameters: Optional[Dict[Any, Any]] = None) -> List[Dict[str, Any]]:
"""
Applies conformance checking against a DECLARE model.
Paper:
F. M. Maggi, A. J. Mooij and W. M. P. van der Aalst, "User-guided discovery of declarative process models," 2011 IEEE Symposium on Computational Intelligence and Data Mining (CIDM), Paris, France, 2011, pp. 192-199, doi: 10.1109/CIDM.2011.5949297.
Parameters
--------------
log
Event log / Pandas dataframe
model
DECLARE model
parameters
Possible parameters of the algorithm, including:
- Parameters.ACTIVITY_KEY => the attribute to be used as activity
- Parameters.CASE_ID_KEY => the attribute to be used as case identifier
Returns
-------------
lst_conf_res
List containing for every case a dictionary with different keys:
- no_constr_total => the total number of constraints of the DECLARE model
- deviations => a list of deviations
- no_dev_total => the total number of deviations
- dev_fitness => the fitness (1 - no_dev_total / no_constr_total)
- is_fit => True if the case is perfectly fit
"""
if parameters is None:
parameters = {}
activity_key = exec_utils.get_param_value(Parameters.ACTIVITY_KEY, parameters, xes_constants.DEFAULT_NAME_KEY)
case_id_key = exec_utils.get_param_value(Parameters.CASE_ID_KEY, parameters, constants.CASE_CONCEPT_NAME)
import pm4py
projected_log = pm4py.project_on_event_attribute(log, activity_key, case_id_key=case_id_key)
ret = apply_list(projected_log, model, parameters=parameters)
return ret
def get_diagnostics_dataframe(log, conf_result, parameters=None) -> pd.DataFrame:
"""
Gets the diagnostics dataframe from a log and the results
of DECLARE-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]["no_dev_total"]
no_constr_total = conf_result[index]["no_constr_total"]
dev_fitness = conf_result[index]["dev_fitness"]
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)
|