def compute_alignment(xes_file, pnml_file): """ Compute alignments for event log with given model. Save alignments results and all metrics during computation in a csv file. Parameters ---------- xes_file : .xes file The xes file of the event log pnml_file : .pnml file The petri net model """ event_log = xes_importer.apply(xes_file) model_net, model_im, model_fm = petri_importer.apply(pnml_file) # get log name and model name log_name = Path(log_path).stem model_name = Path(model_path).stem # define the column name in result file field_names = [ 'case_id', 'total', 'heuristic', 'queue', 'states', 'arcs', 'sum', 'num_insert', 'num_removal', 'num_update', 'simple_lp', 'complex_lp', 'restart', 'split_num', 'trace_length', 'alignment_length', 'cost', 'alignment' ] df = pd.DataFrame(columns=field_names) trace_variant_lst = {} # iterate every case in log for case_index in tqdm(range(len(event_log))): events_lst = [] for event in event_log[case_index]: events_lst.append(event['concept:name']) trace_str = ''.join(events_lst) # if the sequence of events is met for the first time if trace_str not in trace_variant_lst: # construct synchronous product net sync_product = SynchronousProduct(event_log[case_index], model_net, model_im, model_fm) initial_marking, final_marking, cost_function, incidence_matrix, trace_sync, trace_log \ = sync_product.construct_sync_product(event_log[case_index], model_net, model_im, model_fm) # compute alignment with split-pint-based algorithm + caching strategy + reopen method start_time = timeit.default_timer() ali_with_split_astar = AlignmentWithCacheReopenAstar( initial_marking, final_marking, cost_function, incidence_matrix, trace_sync, trace_log) alignment_result = ali_with_split_astar.search() # get the total computation time alignment_result['total'] = timeit.default_timer() - start_time alignment_result['case_id'] = event_log[case_index].attributes[ 'concept:name'] trace_variant_lst[trace_str] = alignment_result else: alignment_result = trace_variant_lst[trace_str] alignment_result['case_id'] = event_log[case_index].attributes[ 'concept:name'] df = df.append(alignment_result, ignore_index=True) # The name of result csv file is of the form: 'log_name + model_name + algorithm type.csv' df.to_csv('../results/log=' + log_name + '&model=' + model_name + '&algorithm=cache_reopen_astar' + '.csv', index=False)
def test_importingPetriLogAlignment(self): # to avoid static method warnings in tests, # that by construction of the unittest package have to be expressed in such way self.dummy_variable = "dummy_value" imported_petri1, marking1, fmarking1 = petri_importer.apply( os.path.join(INPUT_DATA_DIR, "running-example.pnml")) log = xes_importer.apply( os.path.join(INPUT_DATA_DIR, "running-example.xes")) final_marking = petri_net.obj.Marking() for p in imported_petri1.places: if not p.out_arcs: final_marking[p] = 1 for trace in log: cf_result = align_alg.apply( trace, imported_petri1, marking1, final_marking, variant=align_alg.VERSION_DIJKSTRA_NO_HEURISTICS)['alignment'] is_fit = True for couple in cf_result: if not (couple[0] == couple[1] or couple[0] == ">>" and couple[1] is None): is_fit = False if not is_fit: raise Exception("should be fit")
def test_simulate_petrinet_start_params(self): net, im, fm = pnml_importer.apply( os.path.join(INPUT_DATA_DIR, "running-example.pnml")) number_of_traces = 10 timestamp = 50000000 case_id = 5 eventlog = simulator.apply( net, im, fm, variant=simulator.Variants.BASIC_PLAYOUT, parameters={ simulator.Variants.BASIC_PLAYOUT.value.Parameters.NO_TRACES: number_of_traces, simulator.Variants.BASIC_PLAYOUT.value.Parameters.INITIAL_TIMESTAMP: timestamp, simulator.Variants.BASIC_PLAYOUT.value.Parameters.INITIAL_CASE_ID: case_id }) self.assertEqual(len(eventlog), number_of_traces) last_case_id = case_id + number_of_traces - 1 self.assertEqual(eventlog[0].attributes['concept:name'], str(case_id)) self.assertEqual(datetime.timestamp(eventlog[0][0]['time:timestamp']), timestamp) self.assertEqual(eventlog[-1].attributes['concept:name'], str(last_case_id))
def test_importingPetriLogTokenReplay(self): # to avoid static method warnings in tests, # that by construction of the unittest package have to be expressed in such way self.dummy_variable = "dummy_value" imported_petri1, marking1, fmarking1 = petri_importer.apply( os.path.join(INPUT_DATA_DIR, "running-example.pnml")) log = xes_importer.apply( os.path.join(INPUT_DATA_DIR, "running-example.xes")) aligned_traces = token_replay.apply(log, imported_petri1, marking1, fmarking1) del aligned_traces
def test_importingExportingPetri(self): # to avoid static method warnings in tests, # that by construction of the unittest package have to be expressed in such way self.dummy_variable = "dummy_value" imported_petri1, marking1, fmarking1 = petri_importer.apply( os.path.join(INPUT_DATA_DIR, "running-example.pnml")) petri_exporter.apply( imported_petri1, marking1, os.path.join(OUTPUT_DATA_DIR, "running-example.pnml")) imported_petri2, marking2, fmarking2 = petri_importer.apply( os.path.join(OUTPUT_DATA_DIR, "running-example.pnml")) self.assertEqual(sorted([x.name for x in imported_petri1.places]), sorted([x.name for x in imported_petri2.places])) self.assertEqual(sorted([x.name for x in imported_petri1.transitions]), sorted([x.name for x in imported_petri2.transitions])) self.assertEqual( sorted( [x.source.name + x.target.name for x in imported_petri1.arcs]), sorted( [x.source.name + x.target.name for x in imported_petri2.arcs])) self.assertEqual([x.name for x in marking1], [x.name for x in marking2]) os.remove(os.path.join(OUTPUT_DATA_DIR, "running-example.pnml"))
def testNoSynchronousDiscountedAlignment(): ''' This function runs an alignment based on the discounted edit distance By using the Petri net and petri_net.utils.align_utils.discountedEditDistance function ''' log_path = os.path.join("..", "tests", "input_data", "running-example.xes") pnml_path = os.path.join("..", "tests", "input_data", "running-example.pnml") log = xes_importer.apply(log_path) net, marking, fmarking = petri_importer.apply(pnml_path) start=time.time() alignments1 = ali.apply(log._list[0], net, marking, fmarking, variant=ali.VERSION_DISCOUNTED_A_STAR, parameters={ali.Parameters.SYNCHRONOUS:False,ali.Parameters.EXPONENT:1.1}) print(alignments1) print("Time:",(time.time()-start))
def read_pnml(file_path: str) -> Tuple[PetriNet, Marking, Marking]: """ Reads a Petri net from the .PNML format Parameters ---------------- file_path File path Returns ---------------- petri_net Petri net object initial_marking Initial marking final_marking Final marking """ from pm4py.objects.petri_net.importer import importer as pnml_importer net, im, fm = pnml_importer.apply(file_path) return net, im, fm
def testSynchronousDiscountedAlignment(): ''' This function runs an alignment based on the discounted edit distance By using the synchronous product :return: ''' log_path = os.path.join("..", "tests", "input_data", "running-example.xes") pnml_path = os.path.join("..", "tests", "input_data", "running-example.pnml") log = xes_importer.apply(log_path) net, marking, fmarking = petri_importer.apply(pnml_path) # to see the net : #vizu(net,marking,fmarking).view() start=time.time() alignments1 = ali.apply(log._list[0], net, marking, fmarking, variant=ali.VERSION_DISCOUNTED_A_STAR, parameters={ali.Parameters.SYNCHRONOUS:True,ali.Parameters.EXPONENT:1.1}) print(alignments1) print("Time:",(time.time()-start))
def read_petri_net(file_path: str) -> Tuple[PetriNet, Marking, Marking]: warnings.warn('read_petri_net is deprecated, use read_pnml instead', DeprecationWarning) """ Reads a Petri net from the .PNML format Parameters ---------------- file_path File path Returns ---------------- petri_net Petri net object initial_marking Initial marking final_marking Final marking """ from pm4py.objects.petri_net.importer import importer as pnml_importer net, im, fm = pnml_importer.apply(file_path) return net, im, fm
def execute_script(): log_path = os.path.join("..", "tests", "input_data", "running-example.xes") pnml_path = os.path.join("..", "tests", "input_data", "running-example.pnml") # log_path = 'C:/Users/bas/Documents/tue/svn/private/logs/a32_logs/a32f0n05.xes' # pnml_path = 'C:/Users/bas/Documents/tue/svn/private/logs/a32_logs/a32.pnml' log = xes_importer.apply(log_path) net, marking, fmarking = petri_importer.apply(pnml_path) model_cost_function = dict() sync_cost_function = dict() for t in net.transitions: if t.label is not None: model_cost_function[t] = 1000 sync_cost_function[t] = 0 else: model_cost_function[t] = 1 alignments = ali.petri_net.algorithm.apply(log, net, marking, fmarking) print(alignments) pretty_print_alignments(alignments)
def test_simulate_petrinet(self): net, im, fm = pnml_importer.apply( os.path.join(INPUT_DATA_DIR, "running-example.pnml")) number_of_traces = 10 eventlog = simulator.apply( net, im, fm, variant=simulator.Variants.BASIC_PLAYOUT, parameters={ simulator.Variants.BASIC_PLAYOUT.value.Parameters.NO_TRACES: number_of_traces }) self.assertEqual(len(eventlog), number_of_traces) case_id_default = 0 last_case_id = case_id_default + number_of_traces - 1 timestamp_default = 10000000 self.assertEqual(eventlog[0].attributes['concept:name'], str(case_id_default)) self.assertEqual(datetime.timestamp(eventlog[0][0]['time:timestamp']), timestamp_default) self.assertEqual(eventlog[-1].attributes['concept:name'], str(last_case_id))
import os from pm4py.algo.conformance.antialignments.variants.discounted_a_star import apply as antii from pm4py.algo.conformance.antialignments.algorithm import Parameters from pm4py.objects.log.importer.xes import importer as xes_importer from pm4py.objects.petri_net.importer import importer as petri_importer if __name__ == '__main__': log_path = os.path.join("..", "tests", "input_data", "running-example.xes") pnml_path = os.path.join("..", "tests", "input_data", "running-example.pnml") log = xes_importer.apply(log_path) net, marking, fmarking = petri_importer.apply(pnml_path) THETA = 1.5 MU = 20 EPSILON = 0.01 resAnti = antii(log, net, marking, fmarking, parameters={ Parameters.EXPONENT: THETA, Parameters.EPSILON: EPSILON, Parameters.MARKING_LIMIT: MU }) print(resAnti['anti-alignment']) print("Precision:", resAnti['precision'])