Example #1
0
def execute_script():
    # imports a XES event log
    log = pm4py.read_xes(
        os.path.join("..", "tests", "input_data", "receipt.xes"))
    # converts the log into a list of events (not anymore grouped in cases)
    event_stream = pm4py.convert_to_event_stream(log)
    # calculates a process tree using the IMf algorithm (30% noise)
    net, im, fm = pm4py.discover_petri_net_inductive(log, noise_threshold=0.3)
    # creates a live event stream (an object that distributes the messages to the algorithm)
    live_stream = LiveEventStream()
    # creates the TBR streaming conformance checking object
    conf_obj = streaming_tbr.apply(net, im, fm)
    # register the conformance checking object to the live event stream
    live_stream.register(conf_obj)
    # start the recording of events from the live event stream
    live_stream.start()
    # append each event of the original log to the live event stream
    # (so it is sent to the conformance checking algorithm)
    for index, event in enumerate(event_stream):
        live_stream.append(event)
    #time.sleep(5)
    # stops the live event stream
    live_stream.stop()
    # sends a termination signal to the conformance checking algorithm;
    # the conditions on the closure of all the cases are checked
    # (for each case, it is checked whether the final marking is reached)
    diagn_df = conf_obj.get()
    conf_obj.terminate_all()
    print(diagn_df)
    print(diagn_df[diagn_df["is_fit"] == False])
Example #2
0
def execute_script():
    # imports a XES event log
    log = pm4py.read_xes(
        os.path.join("..", "tests", "input_data", "receipt.xes"))
    # converts the log into a list of events (not anymore grouped in cases)
    event_stream = pm4py.convert_to_event_stream(log)
    # creates a live event stream (an object that distributes the messages to the algorithm)
    live_stream = LiveEventStream()
    # creates the streaming DFG discovery object
    stream_dfg_disc = dfg_discovery.apply()
    # register the discovery algorithm to the stream
    live_stream.register(stream_dfg_disc)
    # start the recording of events from the live event stream
    live_stream.start()
    # append each event of the original log to the live event stream
    # (so it is sent to the conformance checking algorithm)
    for event in event_stream:
        live_stream.append(event)
    # stops the live event stream
    live_stream.stop()
    # gets the DFG along with the start and end activities from the stream
    dfg, activities, start_activities, end_activities = stream_dfg_disc.get()
    # visualize the DFG
    gviz = dfg_visualizer.apply(dfg,
                                variant=dfg_visualizer.Variants.FREQUENCY,
                                activities_count=activities,
                                parameters={
                                    "format": "svg",
                                    "start_activities": start_activities,
                                    "end_activities": end_activities
                                })
    dfg_visualizer.view(gviz)
Example #3
0
def execute_script():
    # imports a XES event log
    log = pm4py.read_xes(
        os.path.join("..", "tests", "input_data", "receipt.xes"))
    # converts the log into a list of events (not anymore grouped in cases)
    event_stream = pm4py.convert_to_event_stream(log)
    # calculates a process tree using the IMf algorithm (50% noise)
    tree = pm4py.discover_tree_inductive(log, noise_threshold=0.5)
    # discovers the footprint matrix from the process tree
    footprints = fp_discovery.apply(tree)
    # creates a live event stream (an object that distributes the messages to the algorithm)
    live_stream = LiveEventStream()
    # creates the TBR streaming conformance checking object
    conf_obj = streaming_fp_conf.apply(footprints)
    # register the conformance checking object to the live event stream
    live_stream.register(conf_obj)
    # start the recording of events from the live event stream
    live_stream.start()
    # append each event of the original log to the live event stream
    # (so it is sent to the conformance checking algorithm)
    for event in event_stream:
        live_stream.append(event)
    # stops the live event stream
    live_stream.stop()
    # sends a termination signal to the conformance checking algorithm;
    # the conditions on the closure of all the cases are checked
    # (for each case, it is checked whether the end activity of the case
    # is possible according to the footprints)
    diagn_df = conf_obj.get()
    conf_obj.terminate_all()
    print(diagn_df)
    print(diagn_df[diagn_df["is_fit"] == False])
Example #4
0
 def test_csv(self):
     df = pd.read_csv("input_data/running-example.csv")
     df = pm4py.format_dataframe(df, case_id="case:concept:name", activity_key="concept:name",
                                 timestamp_key="time:timestamp")
     log2 = pm4py.convert_to_event_log(df)
     stream1 = pm4py.convert_to_event_stream(log2)
     df2 = pm4py.convert_to_dataframe(log2)
     pm4py.write_xes(log2, "test_output_data/log.xes")
     os.remove("test_output_data/log.xes")
def execute_script():
    log = pm4py.read_xes("../tests/input_data/receipt.xes")
    static_stream = pm4py.convert_to_event_stream(log)
    temporal_profile = temporal_profile_disc.apply(log)
    cc = streaming_temporal_conformance.apply(temporal_profile)
    live_stream = LiveEventStream()
    live_stream.register(cc)
    live_stream.start()
    for index, ev in enumerate(static_stream):
        live_stream.append(ev)
    live_stream.stop()
    print(cc.get())
Example #6
0
def rebase(
        log_obj: Union[EventLog, EventStream, pd.DataFrame],
        case_id: str = constants.CASE_CONCEPT_NAME,
        activity_key: str = xes_constants.DEFAULT_NAME_KEY,
        timestamp_key: str = xes_constants.DEFAULT_TIMESTAMP_KEY,
        start_timestamp_key: str = xes_constants.DEFAULT_START_TIMESTAMP_KEY):
    """
    Re-base the log object, changing the case ID, activity and timestamp attributes.

    Parameters
    -----------------
    log_obj
        Log object
    case_id
        Case identifier
    activity_key
        Activity
    timestamp_key
        Timestamp
    start_timestamp_key
        Start timestamp

    Returns
    -----------------
    rebased_log_obj
        Rebased log object
    """
    import pm4py

    if isinstance(log_obj, pd.DataFrame):
        return format_dataframe(log_obj,
                                case_id=case_id,
                                activity_key=activity_key,
                                timestamp_key=timestamp_key,
                                start_timestamp_key=start_timestamp_key)
    elif isinstance(log_obj, EventLog):
        log_obj = pm4py.convert_to_dataframe(log_obj)
        log_obj = format_dataframe(log_obj,
                                   case_id=case_id,
                                   activity_key=activity_key,
                                   timestamp_key=timestamp_key,
                                   start_timestamp_key=start_timestamp_key)
        from pm4py.objects.conversion.log import converter
        return converter.apply(log_obj,
                               variant=converter.Variants.TO_EVENT_LOG)
    elif isinstance(log_obj, EventStream):
        log_obj = pm4py.convert_to_dataframe(log_obj)
        log_obj = format_dataframe(log_obj,
                                   case_id=case_id,
                                   activity_key=activity_key,
                                   timestamp_key=timestamp_key,
                                   start_timestamp_key=start_timestamp_key)
        return pm4py.convert_to_event_stream(log_obj)
Example #7
0
def get_resource_count(log, mydict):
    result_list = []
    # This part differentiates between csv and xes files
    # Event logs have a 'origin' key in their attributes dictionary that stores what type of file they came from
    if log.attributes['origin'] == 'csv':
        activity_str = 'concept:name'
        resource_str = 'resource'
    elif log.attributes['origin'] == 'xes':
        activity_str = 'concept:name'
        resource_str = 'org:resource'
    for i in mydict['activity']:
        counter = 0
        check_list = []
        event_stream = pm4py.convert_to_event_stream(log)
        event_stream_temp = functools.filter_(lambda t: t[activity_str] == i,
                                              event_stream)
        for e in event_stream_temp:
            if e[resource_str] not in check_list:
                check_list.append(e[resource_str])
                counter += 1

        result_list.append(counter)
    return result_list
def execute_script():
    ENABLE_VISUALIZATION = True

    # reads a XES into an event log
    log1 = pm4py.read_xes("../tests/input_data/running-example.xes")

    # reads a CSV into a dataframe
    df = pd.read_csv("../tests/input_data/running-example.csv")
    # formats the dataframe with the mandatory columns for process mining purposes
    df = pm4py.format_dataframe(df,
                                case_id="case:concept:name",
                                activity_key="concept:name",
                                timestamp_key="time:timestamp")
    # converts the dataframe to an event log
    log2 = pm4py.convert_to_event_log(df)

    # converts the log read from XES into a stream and dataframe respectively
    stream1 = pm4py.convert_to_event_stream(log1)
    df2 = pm4py.convert_to_dataframe(log1)

    # writes the log1 to a XES file
    pm4py.write_xes(log1, "ru1.xes")

    dfg, dfg_sa, dfg_ea = pm4py.discover_dfg(log1)
    petri_alpha, im_alpha, fm_alpha = pm4py.discover_petri_net_alpha(log1)
    petri_inductive, im_inductive, fm_inductive = pm4py.discover_petri_net_inductive(
        log1)
    petri_heuristics, im_heuristics, fm_heuristics = pm4py.discover_petri_net_heuristics(
        log1)
    tree_inductive = pm4py.discover_tree_inductive(log1)
    heu_net = pm4py.discover_heuristics_net(log1)

    pm4py.write_dfg(dfg, dfg_sa, dfg_ea, "ru_dfg.dfg")
    pm4py.write_petri_net(petri_alpha, im_alpha, fm_alpha, "ru_alpha.pnml")
    pm4py.write_petri_net(petri_inductive, im_inductive, fm_inductive,
                          "ru_inductive.pnml")
    pm4py.write_petri_net(petri_heuristics, im_heuristics, fm_heuristics,
                          "ru_heuristics.pnml")
    pm4py.write_process_tree(tree_inductive, "ru_inductive.ptml")

    dfg, dfg_sa, dfg_ea = pm4py.read_dfg("ru_dfg.dfg")
    petri_alpha, im_alpha, fm_alpha = pm4py.read_petri_net("ru_alpha.pnml")
    petri_inductive, im_inductive, fm_inductive = pm4py.read_petri_net(
        "ru_inductive.pnml")
    petri_heuristics, im_heuristics, fm_heuristics = pm4py.read_petri_net(
        "ru_heuristics.pnml")
    tree_inductive = pm4py.read_process_tree("ru_inductive.ptml")

    pm4py.save_vis_petri_net(petri_alpha, im_alpha, fm_alpha, "ru_alpha.png")
    pm4py.save_vis_petri_net(petri_inductive, im_inductive, fm_inductive,
                             "ru_inductive.png")
    pm4py.save_vis_petri_net(petri_heuristics, im_heuristics, fm_heuristics,
                             "ru_heuristics.png")
    pm4py.save_vis_process_tree(tree_inductive, "ru_inductive_tree.png")
    pm4py.save_vis_heuristics_net(heu_net, "ru_heunet.png")
    pm4py.save_vis_dfg(dfg, dfg_sa, dfg_ea, "ru_dfg.png")

    if ENABLE_VISUALIZATION:
        pm4py.view_petri_net(petri_alpha, im_alpha, fm_alpha, format="svg")
        pm4py.view_petri_net(petri_inductive,
                             im_inductive,
                             fm_inductive,
                             format="svg")
        pm4py.view_petri_net(petri_heuristics,
                             im_heuristics,
                             fm_heuristics,
                             format="svg")
        pm4py.view_process_tree(tree_inductive, format="svg")
        pm4py.view_heuristics_net(heu_net, format="svg")
        pm4py.view_dfg(dfg, dfg_sa, dfg_ea, format="svg")

    aligned_traces = pm4py.conformance_alignments(log1, petri_inductive,
                                                  im_inductive, fm_inductive)
    replayed_traces = pm4py.conformance_tbr(log1, petri_inductive,
                                            im_inductive, fm_inductive)

    fitness_tbr = pm4py.evaluate_fitness_tbr(log1, petri_inductive,
                                             im_inductive, fm_inductive)
    print("fitness_tbr", fitness_tbr)
    fitness_align = pm4py.evaluate_fitness_alignments(log1, petri_inductive,
                                                      im_inductive,
                                                      fm_inductive)
    print("fitness_align", fitness_align)
    precision_tbr = pm4py.evaluate_precision_tbr(log1, petri_inductive,
                                                 im_inductive, fm_inductive)
    print("precision_tbr", precision_tbr)
    precision_align = pm4py.evaluate_precision_alignments(
        log1, petri_inductive, im_inductive, fm_inductive)
    print("precision_align", precision_align)

    print("log start activities = ", pm4py.get_start_activities(log2))
    print("df start activities = ", pm4py.get_start_activities(df2))
    print("log end activities = ", pm4py.get_end_activities(log2))
    print("df end activities = ", pm4py.get_end_activities(df2))
    print("log attributes = ", pm4py.get_attributes(log2))
    print("df attributes = ", pm4py.get_attributes(df2))
    print("log org:resource values = ",
          pm4py.get_attribute_values(log2, "org:resource"))
    print("df org:resource values = ",
          pm4py.get_attribute_values(df2, "org:resource"))

    print("start_activities len(filt_log) = ",
          len(pm4py.filter_start_activities(log2, ["register request"])))
    print("start_activities len(filt_df) = ",
          len(pm4py.filter_start_activities(df2, ["register request"])))
    print("end_activities len(filt_log) = ",
          len(pm4py.filter_end_activities(log2, ["pay compensation"])))
    print("end_activities len(filt_df) = ",
          len(pm4py.filter_end_activities(df2, ["pay compensation"])))
    print(
        "attributes org:resource len(filt_log) (cases) cases = ",
        len(
            pm4py.filter_attribute_values(log2,
                                          "org:resource", ["Ellen"],
                                          level="case")))
    print(
        "attributes org:resource len(filt_log) (cases)  events = ",
        len(
            pm4py.filter_attribute_values(log2,
                                          "org:resource", ["Ellen"],
                                          level="event")))
    print(
        "attributes org:resource len(filt_df) (events) cases = ",
        len(
            pm4py.filter_attribute_values(df2,
                                          "org:resource", ["Ellen"],
                                          level="case")))
    print(
        "attributes org:resource len(filt_df) (events) events = ",
        len(
            pm4py.filter_attribute_values(df2,
                                          "org:resource", ["Ellen"],
                                          level="event")))
    print(
        "attributes org:resource len(filt_df) (events) events notpositive = ",
        len(
            pm4py.filter_attribute_values(df2,
                                          "org:resource", ["Ellen"],
                                          level="event",
                                          retain=False)))

    print("variants log = ", pm4py.get_variants(log2))
    print("variants df = ", pm4py.get_variants(df2))
    print(
        "variants filter log = ",
        len(
            pm4py.filter_variants(log2, [[
                "register request", "examine thoroughly", "check ticket",
                "decide", "reject request"
            ]])))
    print(
        "variants filter df = ",
        len(
            pm4py.filter_variants(df2, [[
                "register request", "examine thoroughly", "check ticket",
                "decide", "reject request"
            ]])))
    print("variants filter percentage = ",
          len(pm4py.filter_variants_percentage(log2, threshold=0.8)))

    print(
        "paths filter log len = ",
        len(
            pm4py.filter_directly_follows_relation(
                log2, [("register request", "examine casually")])))
    print(
        "paths filter dataframe len = ",
        len(
            pm4py.filter_directly_follows_relation(
                df2, [("register request", "examine casually")])))

    print(
        "timeframe filter log events len = ",
        len(
            pm4py.filter_time_range(log2,
                                    "2011-01-01 00:00:00",
                                    "2011-02-01 00:00:00",
                                    mode="events")))
    print(
        "timeframe filter log traces_contained len = ",
        len(
            pm4py.filter_time_range(log2,
                                    "2011-01-01 00:00:00",
                                    "2011-02-01 00:00:00",
                                    mode="traces_contained")))
    print(
        "timeframe filter log traces_intersecting len = ",
        len(
            pm4py.filter_time_range(log2,
                                    "2011-01-01 00:00:00",
                                    "2011-02-01 00:00:00",
                                    mode="traces_intersecting")))
    print(
        "timeframe filter df events len = ",
        len(
            pm4py.filter_time_range(df2,
                                    "2011-01-01 00:00:00",
                                    "2011-02-01 00:00:00",
                                    mode="events")))
    print(
        "timeframe filter df traces_contained len = ",
        len(
            pm4py.filter_time_range(df2,
                                    "2011-01-01 00:00:00",
                                    "2011-02-01 00:00:00",
                                    mode="traces_contained")))
    print(
        "timeframe filter df traces_intersecting len = ",
        len(
            pm4py.filter_time_range(df2,
                                    "2011-01-01 00:00:00",
                                    "2011-02-01 00:00:00",
                                    mode="traces_intersecting")))

    # remove the temporary files
    os.remove("ru1.xes")
    os.remove("ru_dfg.dfg")
    os.remove("ru_alpha.pnml")
    os.remove("ru_inductive.pnml")
    os.remove("ru_heuristics.pnml")
    os.remove("ru_inductive.ptml")
    os.remove("ru_alpha.png")
    os.remove("ru_inductive.png")
    os.remove("ru_heuristics.png")
    os.remove("ru_inductive_tree.png")
    os.remove("ru_heunet.png")
    os.remove("ru_dfg.png")