-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.py
More file actions
187 lines (161 loc) · 7.42 KB
/
Copy pathanalysis.py
File metadata and controls
187 lines (161 loc) · 7.42 KB
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
"""
P2P Process Mining — Request for Payment Bottleneck Analysis
==============================================================
Analyzes a real-life event log (BPI Challenge 2020, "Request For Payment")
to discover the process flow, find bottlenecks, measure rejection/rework
rate, and identify candidates for RPA automation.
Data source: BPI Challenge 2020, 4TU.ResearchData / bptlab cleaned logs.
"""
import pandas as pd
import pm4py
import matplotlib.pyplot as plt
DATA_PATH = "data/RequestForPayment.xes.gz"
# ---------------------------------------------------------------
# 1. Load the event log
# ---------------------------------------------------------------
print("Loading event log...")
log = pm4py.read_xes(DATA_PATH)
# Keep only the columns we actually need, with friendly names
df = log.rename(columns={
"case:concept:name": "case_id",
"concept:name": "activity",
"time:timestamp": "timestamp",
"org:resource": "resource",
"org:role": "role",
})[["case_id", "activity", "timestamp", "resource", "role"]]
df["timestamp"] = pd.to_datetime(df["timestamp"], utc=True)
df = df.sort_values(["case_id", "timestamp"]).reset_index(drop=True)
print(f"Loaded {df['case_id'].nunique()} cases, {len(df)} events, "
f"{df['activity'].nunique()} distinct activities.")
# ---------------------------------------------------------------
# 2. Discover and visualize the process map (Directly-Follows Graph)
# ---------------------------------------------------------------
print("Discovering process map...")
log_for_pm4py = log.rename(columns={
"case:concept:name": "case:concept:name",
"concept:name": "concept:name",
"time:timestamp": "time:timestamp",
})
dfg, start_activities, end_activities = pm4py.discover_dfg(
log, activity_key="concept:name",
timestamp_key="time:timestamp", case_id_key="case:concept:name"
)
pm4py.save_vis_dfg(
dfg, start_activities, end_activities,
"images/process_map.png",
activity_key="concept:name",
timestamp_key="time:timestamp", case_id_key="case:concept:name"
)
print("Saved images/process_map.png")
# ---------------------------------------------------------------
# 3. Measure time spent on each step (bottleneck analysis)
# ---------------------------------------------------------------
print("Computing step durations...")
# Time between consecutive activities within the same case
df["next_activity"] = df.groupby("case_id")["activity"].shift(-1)
df["next_timestamp"] = df.groupby("case_id")["timestamp"].shift(-1)
df["duration_hours"] = (
(df["next_timestamp"] - df["timestamp"]).dt.total_seconds() / 3600
)
step_duration = (
df.dropna(subset=["duration_hours"])
.groupby("activity")["duration_hours"]
.agg(["mean", "median", "count"])
.sort_values("mean", ascending=False)
)
step_duration.to_csv("images/step_durations.csv")
print(step_duration.head(10))
# Bar chart of slowest steps
top_slow = step_duration.head(10).iloc[::-1]
plt.figure(figsize=(8, 5))
plt.barh(top_slow.index, top_slow["mean"], color="#c0392b")
plt.xlabel("Average time to next step (hours)")
plt.title("Top 10 slowest steps — Request for Payment process")
plt.tight_layout()
plt.savefig("images/bottleneck_chart.png", dpi=150)
plt.close()
print("Saved images/bottleneck_chart.png")
# ---------------------------------------------------------------
# 4. Rejection / rework rate
# ---------------------------------------------------------------
print("Computing rejection rate...")
rejection_keywords = ["Reject", "Reset", "Rejected"]
cases_with_rejection = df[
df["activity"].str.contains("|".join(rejection_keywords), case=False, na=False)
]["case_id"].nunique()
total_cases = df["case_id"].nunique()
rejection_rate = cases_with_rejection / total_cases * 100
print(f"Cases with at least one rejection/resubmission: "
f"{cases_with_rejection} / {total_cases} ({rejection_rate:.1f}%)")
# ---------------------------------------------------------------
# 5. Resource workload (who handles the most, who is slowest)
# ---------------------------------------------------------------
print("Computing resource workload...")
resource_stats = (
df.dropna(subset=["duration_hours"])
.groupby("resource")["duration_hours"]
.agg(["mean", "count"])
.query("count >= 30") # ignore resources with too few events
.sort_values("mean", ascending=False)
)
resource_stats.to_csv("images/resource_stats.csv")
print(resource_stats.head(5))
# ---------------------------------------------------------------
# 6. Activity frequency (which steps are most repetitive -> RPA candidates)
# ---------------------------------------------------------------
activity_freq = df["activity"].value_counts()
activity_freq.to_csv("images/activity_frequency.csv")
# ---------------------------------------------------------------
# 7. ROI estimate for automating the top RPA candidate
# ---------------------------------------------------------------
print("Computing ROI estimate...")
# --- Assumptions (clearly stated, can be adjusted for a real engagement) ---
MANUAL_MINUTES_PER_REVIEW = 4 # avg. time a human spends on one
# "Approved by Administration" check
HOURLY_COST_EUR = 18 # fully loaded cost of an SSC/BPO admin
# staff member (salary + overhead)
ROBOT_RUNTIME_SECONDS = 20 # avg. time a bot takes for the same
# rule-based check
ANNUALIZATION_FACTOR = 1.0 # the log covers ~1 year of cases already,
# so no scaling needed here
target_activity = "Request For Payment APPROVED by ADMINISTRATION"
n_occurrences = int((df["activity"] == target_activity).sum())
manual_hours_per_year = n_occurrences * MANUAL_MINUTES_PER_REVIEW / 60
manual_cost_per_year = manual_hours_per_year * HOURLY_COST_EUR
robot_hours_per_year = n_occurrences * ROBOT_RUNTIME_SECONDS / 3600
# Approximate robot run cost as negligible compared to license/maintenance;
# we instead report hours saved, which is the metric BPO clients care about.
hours_saved_per_year = manual_hours_per_year - robot_hours_per_year
cost_saved_per_year = manual_cost_per_year # staff time freed up, not
# spent on RPA infrastructure
roi_summary = pd.DataFrame([{
"activity": target_activity,
"annual_occurrences": n_occurrences,
"manual_minutes_per_case": MANUAL_MINUTES_PER_REVIEW,
"hourly_cost_eur": HOURLY_COST_EUR,
"manual_hours_per_year": round(manual_hours_per_year, 1),
"manual_cost_per_year_eur": round(manual_cost_per_year, 0),
"robot_hours_per_year": round(robot_hours_per_year, 1),
"hours_saved_per_year": round(hours_saved_per_year, 1),
"estimated_annual_savings_eur": round(cost_saved_per_year, 0),
}])
roi_summary.to_csv("images/roi_estimate.csv", index=False)
print(roi_summary.T)
# ROI bar chart
fig, ax = plt.subplots(figsize=(6, 4))
bars = ax.bar(
["Manual process", "After RPA"],
[manual_hours_per_year, robot_hours_per_year],
color=["#c0392b", "#27ae60"],
)
ax.set_ylabel("Hours per year")
ax.set_title('Time spent on "Approved by Administration" step')
for bar in bars:
height = bar.get_height()
ax.annotate(f"{height:.0f}h", xy=(bar.get_x() + bar.get_width() / 2, height),
xytext=(0, 5), textcoords="offset points", ha="center")
plt.tight_layout()
plt.savefig("images/roi_chart.png", dpi=150)
plt.close()
print("Saved images/roi_chart.png")
print("\nDone. All results saved in images/ folder.")