|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from pm4py.util import xes_constants |
| 3 | + |
| 4 | + |
| 5 | +def _collect_activities_and_paths(log, activity_key): |
| 6 | + activities = set() |
| 7 | + paths = set() |
| 8 | + for trace in log: |
| 9 | + events = [event for event in trace if activity_key in event] |
| 10 | + for event in events: |
| 11 | + activities.add(event[activity_key]) |
| 12 | + for idx in range(1, len(events)): |
| 13 | + paths.add((events[idx - 1][activity_key], events[idx][activity_key])) |
| 14 | + activities = sorted(activities) |
| 15 | + paths = sorted(paths) |
| 16 | + return activities, paths |
| 17 | + |
| 18 | + |
| 19 | +def _build_prefix_features(log, activity_key, timestamp_key, target_mode): |
| 20 | + activities, paths = _collect_activities_and_paths(log, activity_key) |
| 21 | + activity_to_index = {activity: idx for idx, activity in enumerate(activities)} |
| 22 | + path_to_index = {path: idx for idx, path in enumerate(paths)} |
| 23 | + |
| 24 | + feature = [] |
| 25 | + target = [] |
| 26 | + case_ids = [] |
| 27 | + |
| 28 | + for trace_index, trace in enumerate(log): |
| 29 | + events = [event for event in trace if activity_key in event] |
| 30 | + if len(events) < 2: |
| 31 | + continue |
| 32 | + trace_id = trace.attributes.get( |
| 33 | + xes_constants.DEFAULT_TRACEID_KEY, trace_index |
| 34 | + ) |
| 35 | + end_time = events[-1].get(timestamp_key) if timestamp_key else None |
| 36 | + if target_mode == "remaining_time" and end_time is None: |
| 37 | + continue |
| 38 | + start_time = events[0].get(timestamp_key) if timestamp_key else None |
| 39 | + |
| 40 | + seen_activities = {events[0][activity_key]} |
| 41 | + seen_paths = set() |
| 42 | + |
| 43 | + for idx in range(1, len(events)): |
| 44 | + prev_event = events[idx - 1] |
| 45 | + curr_event = events[idx] |
| 46 | + |
| 47 | + if idx >= 2: |
| 48 | + prior_event = events[idx - 2] |
| 49 | + seen_paths.add( |
| 50 | + (prior_event[activity_key], prev_event[activity_key]) |
| 51 | + ) |
| 52 | + |
| 53 | + row = [0] * (len(activities) + len(paths)) |
| 54 | + for seen_activity in seen_activities: |
| 55 | + row[activity_to_index[seen_activity]] = 1 |
| 56 | + for seen_path in seen_paths: |
| 57 | + row[len(activities) + path_to_index[seen_path]] = 1 |
| 58 | + |
| 59 | + prev_to_penultimate = 0.0 |
| 60 | + if idx >= 2 and timestamp_key: |
| 61 | + prev_ts = prev_event.get(timestamp_key) |
| 62 | + prior_ts = events[idx - 2].get(timestamp_key) |
| 63 | + if prev_ts is not None and prior_ts is not None: |
| 64 | + prev_to_penultimate = (prev_ts - prior_ts).total_seconds() |
| 65 | + |
| 66 | + start_to_penultimate = 0.0 |
| 67 | + if timestamp_key and start_time is not None: |
| 68 | + penultimate_ts = prev_event.get(timestamp_key) |
| 69 | + if penultimate_ts is not None: |
| 70 | + start_to_penultimate = (penultimate_ts - start_time).total_seconds() |
| 71 | + |
| 72 | + path_time_diff = 0.0 |
| 73 | + if timestamp_key: |
| 74 | + prev_ts = prev_event.get(timestamp_key) |
| 75 | + curr_ts = curr_event.get(timestamp_key) |
| 76 | + if prev_ts is not None and curr_ts is not None: |
| 77 | + path_time_diff = (curr_ts - prev_ts).total_seconds() |
| 78 | + |
| 79 | + row.extend([prev_to_penultimate, start_to_penultimate, path_time_diff]) |
| 80 | + |
| 81 | + curr_activity = curr_event[activity_key] |
| 82 | + if target_mode == "next_activity": |
| 83 | + target.append(activity_to_index[curr_activity]) |
| 84 | + feature.append(row) |
| 85 | + else: |
| 86 | + curr_ts = curr_event.get(timestamp_key) if timestamp_key else None |
| 87 | + if curr_ts is not None: |
| 88 | + remaining = (end_time - curr_ts).total_seconds() |
| 89 | + target.append(remaining) |
| 90 | + feature.append(row) |
| 91 | + case_ids.append(trace_id) |
| 92 | + |
| 93 | + seen_activities.add(curr_activity) |
| 94 | + |
| 95 | + return feature, target, case_ids, activities, activity_to_index, paths, path_to_index |
| 96 | + |
| 97 | + |
| 98 | +def build_prefix_features_next_activity( |
| 99 | + log, |
| 100 | + activity_key=xes_constants.DEFAULT_NAME_KEY, |
| 101 | + timestamp_key=xes_constants.DEFAULT_TIMESTAMP_KEY, |
| 102 | +): |
| 103 | + feature, target, _, activities, activity_to_index, paths, path_to_index = ( |
| 104 | + _build_prefix_features(log, activity_key, timestamp_key, "next_activity") |
| 105 | + ) |
| 106 | + return feature, target, activities, activity_to_index, paths, path_to_index |
| 107 | + |
| 108 | + |
| 109 | +def build_prefix_features_remaining_time( |
| 110 | + log, |
| 111 | + activity_key=xes_constants.DEFAULT_NAME_KEY, |
| 112 | + timestamp_key=xes_constants.DEFAULT_TIMESTAMP_KEY, |
| 113 | +): |
| 114 | + feature, target, case_ids, activities, activity_to_index, paths, path_to_index = ( |
| 115 | + _build_prefix_features(log, activity_key, timestamp_key, "remaining_time") |
| 116 | + ) |
| 117 | + return feature, target, case_ids, activities, activity_to_index, paths, path_to_index |
0 commit comments