-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_analyzer.py
More file actions
1648 lines (1427 loc) · 64.3 KB
/
Copy pathpath_analyzer.py
File metadata and controls
1648 lines (1427 loc) · 64.3 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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import logging
from collections import Counter, defaultdict
from datetime import datetime, timedelta
from typing import Any, Optional, Union
import networkx as nx
import pandas as pd
import polars as pl
# Import necessary classes from models.py
from models import (
FunnelConfig,
FunnelOrder,
PathAnalysisData,
ProcessMiningData,
ReentryMode,
)
class PathAnalyzer:
"""
Helper class for path analysis with optimized implementations for different funnel configurations.
This class encapsulates the complex logic for path analysis to improve performance and maintainability.
"""
def __init__(self, config: FunnelConfig):
"""Initialize with funnel configuration"""
self.config = config
self.logger = logging.getLogger(__name__)
# Elite optimization: Enable global string cache for Polars operations
try:
pl.enable_string_cache()
self.logger.debug("Polars string cache enabled in PathAnalyzer")
except Exception as e:
self.logger.warning(f"Could not enable Polars string cache in PathAnalyzer: {e}")
def analyze(
self,
funnel_events_df: pl.DataFrame,
full_history_df: pl.DataFrame,
funnel_steps: list[str],
) -> PathAnalysisData:
"""
Main entry point for path analysis. Handles preprocessing and delegates to specialized methods
based on funnel configuration.
Args:
funnel_events_df: Polars DataFrame with funnel events
full_history_df: Polars DataFrame with all user events
funnel_steps: List of funnel step names in order
Returns:
PathAnalysisData object with dropoff paths and between-steps events
"""
# Ensure we're working with eager DataFrames (not LazyFrames)
if hasattr(funnel_events_df, "collect") and callable(funnel_events_df.collect):
funnel_events_df = funnel_events_df.collect()
if hasattr(full_history_df, "collect") and callable(full_history_df.collect):
full_history_df = full_history_df.collect()
# Preprocess DataFrames to handle nested object types
funnel_events_df = self._preprocess_dataframe(funnel_events_df)
full_history_df = self._preprocess_dataframe(full_history_df)
# Initialize result containers
dropoff_paths = {}
between_steps_events = {}
# Ensure we have the required columns
try:
funnel_events_df.select("user_id", "event_name", "timestamp")
full_history_df.select("user_id", "event_name", "timestamp")
except Exception as e:
self.logger.error(f"Missing required columns in input DataFrames: {str(e)}")
return PathAnalysisData({}, {})
# Pre-calculate step user sets using Polars
step_user_sets = {}
for step in funnel_steps:
step_users = set(
funnel_events_df.filter(pl.col("event_name") == step)
.select("user_id")
.unique()
.to_series()
.to_list()
)
step_user_sets[step] = step_users
# Process each step pair in the funnel
for i, step in enumerate(funnel_steps[:-1]):
next_step = funnel_steps[i + 1]
# Find dropped users efficiently
step_users = step_user_sets[step]
next_step_users = step_user_sets[next_step]
dropped_users = step_users - next_step_users
# Analyze drop-off paths
if dropped_users:
next_events = self._analyze_dropoff_paths(
funnel_events_df, full_history_df, dropped_users, step
)
if next_events:
dropoff_paths[step] = dict(next_events.most_common(10))
# Find users who converted from current_step to next_step
conversion_pairs = self._find_conversion_pairs(
funnel_events_df, step, next_step, funnel_steps
)
# Extract user IDs from conversion pairs
if not conversion_pairs.is_empty():
truly_converted_users = set(
conversion_pairs.select("user_id").to_series().to_list()
)
else:
truly_converted_users = set()
# Analyze between-steps events for converted users
if truly_converted_users:
between_events = self._analyze_between_steps_events(
funnel_events_df,
full_history_df,
truly_converted_users,
step,
next_step,
funnel_steps,
conversion_pairs,
)
step_pair = f"{step} → {next_step}"
if between_events: # Only add if non-empty
between_steps_events[step_pair] = dict(between_events.most_common(10))
return PathAnalysisData(
dropoff_paths=dropoff_paths, between_steps_events=between_steps_events
)
def _preprocess_dataframe(self, df: pl.DataFrame) -> pl.DataFrame:
"""Preprocess DataFrame to handle nested object types and ensure proper column types"""
try:
# Handle complex data types by converting object columns to strings
for col in df.columns:
if col == "properties" or str(df[col].dtype).startswith("Object"):
df = df.with_columns([pl.col(col).cast(pl.Utf8)])
# Ensure timestamp column has proper type
if "timestamp" in df.columns:
df = df.with_columns([pl.col("timestamp").cast(pl.Datetime)])
# Ensure user_id is string type
if "user_id" in df.columns:
df = df.with_columns([pl.col("user_id").cast(pl.Utf8)])
# Remove existing _original_order column if it exists and add a new one
if "_original_order" in df.columns:
df = df.drop("_original_order")
df = df.with_row_index("_original_order")
return df
except Exception as e:
self.logger.warning(f"Error preprocessing DataFrame: {str(e)}")
return df
def _analyze_dropoff_paths(
self,
funnel_events_df: pl.DataFrame,
full_history_df: pl.DataFrame,
dropped_users: set,
step: str,
) -> Counter:
"""
Analyze what events users do after dropping off from a funnel step.
Args:
funnel_events_df: DataFrame with funnel events
full_history_df: DataFrame with all user events
dropped_users: Set of user IDs who dropped off
step: The step from which users dropped off
Returns:
Counter with next events and their counts
"""
next_events = Counter()
if not dropped_users:
return next_events
# Convert set to list for Polars filtering
dropped_user_list = list(str(user_id) for user_id in dropped_users)
# Use lazy evaluation for better query optimization
lazy_segment_df = funnel_events_df.lazy()
lazy_history_df = full_history_df.lazy()
# Find the timestamp of the last step event for each dropped user
last_step_events = (
lazy_segment_df.filter(
(pl.col("user_id").cast(pl.Utf8).is_in(dropped_user_list))
& (pl.col("event_name") == step)
)
.group_by("user_id")
.agg(pl.col("timestamp").max().alias("step_time"))
)
# Early exit if no step events found
if last_step_events.collect().height == 0:
return next_events
# Find the next event after the step for each user within 7 days
next_events_df = (
last_step_events.join(
lazy_history_df.filter(pl.col("user_id").cast(pl.Utf8).is_in(dropped_user_list)),
on="user_id",
how="inner",
)
.filter(
(pl.col("timestamp") > pl.col("step_time"))
& (pl.col("timestamp") <= pl.col("step_time") + pl.duration(days=7))
& (pl.col("event_name") != step)
)
# Use window function to find first event after step for each user
.with_columns([pl.col("timestamp").rank().over(["user_id"]).alias("event_rank")])
.filter(pl.col("event_rank") == 1)
.select(["user_id", "event_name"])
)
# Count next events
event_counts = next_events_df.group_by("event_name").agg(pl.len().alias("count")).collect()
# Convert to Counter format
if event_counts.height > 0:
next_events = Counter(
dict(
zip(
event_counts["event_name"].to_list(),
event_counts["count"].to_list(),
)
)
)
# Count users with no further activity
users_with_events = next_events_df.select(pl.col("user_id").unique()).collect().height
users_with_no_events = len(dropped_users) - users_with_events
if users_with_no_events > 0:
next_events["(no further activity)"] = users_with_no_events
return next_events
def _find_conversion_pairs(
self,
events_df: pl.DataFrame,
step: str,
next_step: str,
funnel_steps: list[str],
) -> pl.DataFrame:
"""
Find pairs of events representing conversions from one step to the next.
This method handles all combinations of funnel order and reentry mode.
Args:
events_df: DataFrame with funnel events
step: Current step name
next_step: Next step name
funnel_steps: List of all funnel steps in order
Returns:
DataFrame with conversion pairs (user_id, step_A_time, step_B_time)
"""
# Filter events to only include relevant steps
step_events = events_df.filter(pl.col("event_name").is_in([step, next_step]))
# Extract step A and step B events separately
step_A_df = step_events.filter(pl.col("event_name") == step).with_columns(
[pl.col("timestamp").alias("step_A_time"), pl.lit(step).alias("step")]
)
step_B_df = step_events.filter(pl.col("event_name") == next_step).with_columns(
[
pl.col("timestamp").alias("step_B_time"),
pl.lit(next_step).alias("step_name"), # Used in some fallback methods
]
)
# Early exit if either step has no events
if step_A_df.height == 0 or step_B_df.height == 0:
return pl.DataFrame(
{
"user_id": [],
"step": [],
"next_step": [],
"step_A_time": [],
"step_B_time": [],
}
)
conversion_window = pl.duration(hours=self.config.conversion_window_hours)
# Choose strategy based on funnel configuration
if self.config.funnel_order == FunnelOrder.ORDERED:
if self.config.reentry_mode == ReentryMode.FIRST_ONLY:
# Get first step A for each user
first_A = step_A_df.group_by("user_id").agg(
[pl.col("step_A_time").min(), pl.col("step").first()]
)
# For each user, find first B after A within conversion window
conversion_pairs = (
first_A.join(step_B_df, on="user_id", how="inner")
.filter(
(pl.col("step_B_time") > pl.col("step_A_time"))
& (pl.col("step_B_time") <= pl.col("step_A_time") + conversion_window)
)
# Use window function to find earliest B for each user
.with_columns([pl.col("step_B_time").rank().over(["user_id"]).alias("rank")])
.filter(pl.col("rank") == 1)
.select(["user_id", "step", "step_name", "step_A_time", "step_B_time"])
.rename({"step_name": "next_step"})
)
elif self.config.reentry_mode == ReentryMode.OPTIMIZED_REENTRY:
# Use join_asof for optimal performance with ORDERED + OPTIMIZED_REENTRY
try:
# Sort both DataFrames by timestamp
step_A_df = step_A_df.sort(["user_id", "step_A_time"])
step_B_df = step_B_df.sort(["user_id", "step_B_time"])
# Use join_asof to find the next B event after each A event within window
# This avoids the "join explosion" problem
conversion_pairs = pl.join_asof(
step_A_df.select(["user_id", "step", "step_A_time"]),
step_B_df.select(["user_id", "step_name", "step_B_time"]).rename(
{"step_name": "next_step"}
),
left_on="step_A_time",
right_on="step_B_time",
by="user_id",
strategy="forward",
).filter(
# Keep only pairs within conversion window
(pl.col("step_B_time") > pl.col("step_A_time"))
& (pl.col("step_B_time") <= pl.col("step_A_time") + conversion_window)
)
except Exception as e:
self.logger.warning(
f"join_asof failed: {str(e)}, falling back to optimal_step_pairs"
)
# Fall back to the optimal step pairs method
conversion_pairs = self._find_optimal_step_pairs(step_A_df, step_B_df)
else: # UNORDERED funnel
if self.config.reentry_mode == ReentryMode.FIRST_ONLY:
# For unordered funnels, we just need the first occurrence of each step
first_A = step_A_df.group_by("user_id").agg(
[pl.col("step_A_time").min(), pl.col("step").first()]
)
first_B = step_B_df.group_by("user_id").agg(
[
pl.col("step_B_time").min(),
pl.col("step_name").first().alias("next_step"),
]
)
# Join and filter by conversion window
conversion_pairs = (
first_A.join(first_B, on="user_id", how="inner")
.with_columns(
[
# Calculate absolute time difference
(
(pl.col("step_B_time") - pl.col("step_A_time"))
.dt.total_hours()
.abs()
).alias("time_diff_hours")
]
)
.filter(pl.col("time_diff_hours") <= self.config.conversion_window_hours)
.drop("time_diff_hours")
)
else: # UNORDERED + OPTIMIZED_REENTRY
# For unordered with reentry, we need to find all pairs within window
# and then group by user to find the earliest valid pair
joined = (
step_A_df.select(["user_id", "step", "step_A_time"])
.join(
step_B_df.select(["user_id", "step_name", "step_B_time"]).rename(
{"step_name": "next_step"}
),
on="user_id",
how="inner",
)
.with_columns(
[
# Calculate absolute time difference
(
(pl.col("step_B_time") - pl.col("step_A_time"))
.dt.total_hours()
.abs()
).alias("time_diff_hours")
]
)
.filter(pl.col("time_diff_hours") <= self.config.conversion_window_hours)
.drop("time_diff_hours")
)
# Find the earliest pair for each user (by earliest combined timestamp)
conversion_pairs = (
joined.with_columns(
[
# Use the minimum of the two timestamps to determine the earliest pair
pl.when(pl.col("step_A_time") <= pl.col("step_B_time"))
.then(pl.col("step_A_time"))
.otherwise(pl.col("step_B_time"))
.alias("earliest_time")
]
)
.sort(["user_id", "earliest_time"])
.group_by("user_id")
.agg(
[
pl.col("step").first(),
pl.col("next_step").first(),
pl.col("step_A_time").first(),
pl.col("step_B_time").first(),
]
)
)
return conversion_pairs
def _find_optimal_step_pairs(
self, step_A_df: pl.DataFrame, step_B_df: pl.DataFrame
) -> pl.DataFrame:
"""Helper function to find optimal step pairs when join_asof fails"""
conversion_window = pl.duration(hours=self.config.conversion_window_hours)
# Handle empty dataframes
if step_A_df.height == 0 or step_B_df.height == 0:
return pl.DataFrame(
{
"user_id": [],
"step": [],
"next_step": [],
"step_A_time": [],
"step_B_time": [],
}
)
try:
# Ensure we have step_A_time column
if "step_A_time" not in step_A_df.columns and "timestamp" in step_A_df.columns:
step_A_df = step_A_df.with_columns(pl.col("timestamp").alias("step_A_time"))
# Get step names for labels
step_name = "Step A"
next_step_name = "Step B"
if "step" in step_A_df.columns and step_A_df.height > 0:
step_name_col = step_A_df.select("step").unique()
if step_name_col.height > 0:
step_name = step_name_col[0, 0]
if "step_name" in step_B_df.columns and step_B_df.height > 0:
next_step_name_col = step_B_df.select("step_name").unique()
if next_step_name_col.height > 0:
next_step_name = next_step_name_col[0, 0]
# Use a fully vectorized approach using only Polars expressions
# First, create a cross join of users with their A and B times
user_with_A_times = step_A_df.select(["user_id", "step_A_time"])
# Ensure B times are properly named
if "step_B_time" in step_B_df.columns:
user_with_B_times = step_B_df.select(["user_id", "step_B_time"])
else:
user_with_B_times = step_B_df.select(["user_id", "timestamp"]).rename(
{"timestamp": "step_B_time"}
)
# Join both tables and filter for valid conversion pairs
valid_conversions = (
user_with_A_times.join(user_with_B_times, on="user_id", how="inner")
# Use only native Polars expressions for the filter condition
.filter(
(pl.col("step_B_time") > pl.col("step_A_time"))
& (pl.col("step_B_time") <= pl.col("step_A_time") + conversion_window)
)
# For each step_A_time, find the earliest valid step_B_time
.sort(["user_id", "step_A_time", "step_B_time"])
# Keep the first valid B time for each A time
.group_by(["user_id", "step_A_time"])
.agg(pl.col("step_B_time").first().alias("earliest_B_time"))
# Keep only the first A->B pair for each user
.sort(["user_id", "step_A_time"])
.group_by("user_id")
.agg(
[
pl.col("step_A_time").first(),
pl.col("earliest_B_time").first().alias("step_B_time"),
]
)
# Add step names as literals
.with_columns(
[
pl.lit(step_name).alias("step"),
pl.lit(next_step_name).alias("next_step"),
]
)
# Select columns in the right order
.select(["user_id", "step", "next_step", "step_A_time", "step_B_time"])
)
return valid_conversions
except Exception as e:
self.logger.error(f"Fully vectorized approach for finding step pairs failed: {e}")
# Final fallback with empty DataFrame with correct structure
return pl.DataFrame(
{
"user_id": [],
"step": [],
"next_step": [],
"step_A_time": [],
"step_B_time": [],
}
)
def _analyze_between_steps_events(
self,
funnel_events_df: pl.DataFrame,
full_history_df: pl.DataFrame,
converted_users: set,
step: str,
next_step: str,
funnel_steps: list[str],
conversion_pairs: pl.DataFrame,
) -> Counter:
"""
Analyze events occurring between two funnel steps for converted users.
Args:
funnel_events_df: DataFrame with funnel events
full_history_df: DataFrame with all user events
converted_users: Set of user IDs who converted
step: Current step name
next_step: Next step name
funnel_steps: List of all funnel steps
conversion_pairs: DataFrame with conversion pairs from _find_conversion_pairs
Returns:
Counter with between-steps events and their counts
"""
between_events = Counter()
if not converted_users:
return between_events
# Convert set to list for Polars filtering
converted_user_list = list(str(user_id) for user_id in converted_users)
# Use lazy evaluation for better query optimization
lazy_history_df = full_history_df.lazy()
# Get the conversion pairs for these users
# We already have this from _find_conversion_pairs
if conversion_pairs.is_empty():
return between_events
# For each user, find events between their step_A_time and step_B_time
between_steps_df = (
conversion_pairs.lazy()
.join(
lazy_history_df.filter(pl.col("user_id").cast(pl.Utf8).is_in(converted_user_list)),
on="user_id",
how="inner",
)
.filter(
# Events must be between step A and step B
(pl.col("timestamp") > pl.col("step_A_time"))
& (pl.col("timestamp") < pl.col("step_B_time"))
&
# Exclude the funnel steps themselves
~pl.col("event_name").is_in(funnel_steps)
)
.select(["user_id", "event_name"])
)
# Count events
event_counts = (
between_steps_df.group_by("event_name").agg(pl.len().alias("count")).collect()
)
# Convert to Counter format
if event_counts.height > 0:
between_events = Counter(
dict(
zip(
event_counts["event_name"].to_list(),
event_counts["count"].to_list(),
)
)
)
# Add special entry for users with no intermediate events
users_with_events = between_steps_df.select(pl.col("user_id").unique()).collect().height
users_with_no_events = len(converted_users) - users_with_events
if users_with_no_events > 0:
between_events["(direct conversion)"] = users_with_no_events
return between_events
def discover_process_mining_structure(
self,
events_df: Union[pd.DataFrame, pl.DataFrame],
min_frequency: int = 10,
include_cycles: bool = True,
time_window_hours: Optional[int] = None,
filter_events: Optional[list[str]] = None,
) -> ProcessMiningData:
"""
Automatic process discovery from user events using advanced algorithms
Args:
events_df: DataFrame with events (user_id, event_name, timestamp)
min_frequency: Minimum frequency to include transition
include_cycles: Whether to detect cycles and loops
time_window_hours: Optional time window for process analysis
filter_events: Optional list of event names to filter analysis to (e.g., funnel events only)
Returns:
ProcessMiningData with complete process structure
"""
# Convert to Polars for efficient processing with data type handling
if isinstance(events_df, pd.DataFrame):
# Clean data before conversion to avoid type conflicts
events_clean = events_df.copy()
# Ensure user_id is string
events_clean["user_id"] = events_clean["user_id"].astype(str)
# Filter out rows with None event_name
events_clean = events_clean[events_clean["event_name"].notna()]
events_clean["event_name"] = events_clean["event_name"].astype(str)
# Ensure timestamp is datetime
if not pd.api.types.is_datetime64_any_dtype(events_clean["timestamp"]):
events_clean["timestamp"] = pd.to_datetime(events_clean["timestamp"])
events_pl = pl.from_pandas(events_clean)
else:
events_pl = events_df
# Filter by time window if specified
if time_window_hours:
cutoff_time = datetime.now() - timedelta(hours=time_window_hours)
events_pl = events_pl.filter(pl.col("timestamp") >= cutoff_time)
# Filter by specific events if specified (e.g., funnel events only)
if filter_events:
events_pl = events_pl.filter(pl.col("event_name").is_in(filter_events))
# Build user journeys (optimized) - avoid dictionary conversion when possible
journey_df = self._build_user_journeys_optimized(events_pl)
# Discover activities and their characteristics (optimized)
activities = self._discover_activities(events_pl, None) # Pass None to use optimized path
# Discover transitions between activities (optimized)
transitions = self._discover_transitions_optimized(journey_df, min_frequency)
# Identify process variants (optimized)
variants = self._identify_process_variants_optimized(journey_df)
# Find start and end activities (optimized)
start_activities, end_activities = self._identify_start_end_activities_optimized(
journey_df
)
# Detect cycles and loops if requested (use optimized method first)
cycles = []
if include_cycles:
try:
# Try optimized Polars-based cycle detection first
cycles = self._detect_cycles_optimized(journey_df, transitions)
except Exception as e:
self.logger.warning(
f"Optimized cycle detection failed: {str(e)}, falling back to legacy method"
)
# Fallback to legacy method only if optimized fails
user_journeys = self._build_user_journeys(events_pl)
cycles = self._detect_cycles(user_journeys, transitions)
# Calculate process statistics (optimized to work with Polars DataFrame)
statistics = self._calculate_process_statistics_optimized(
journey_df, activities, transitions
)
# Generate automatic insights
insights = self._generate_process_insights(
activities, transitions, cycles, variants, statistics
)
return ProcessMiningData(
activities=activities,
transitions=transitions,
cycles=cycles,
variants=variants,
start_activities=start_activities,
end_activities=end_activities,
statistics=statistics,
insights=insights,
)
def _build_user_journeys_optimized(self, events_pl: pl.DataFrame) -> pl.DataFrame:
"""Build user journeys using pure Polars for maximum performance"""
# Sort events by user and timestamp
sorted_events = events_pl.sort(["user_id", "timestamp"])
# Add sequence numbers and calculate durations using window functions
journey_df = sorted_events.with_columns(
[
# Add row number within each user group
pl.int_range(pl.len()).over("user_id").alias("event_order"),
# Calculate duration to next event (in hours)
(pl.col("timestamp").shift(-1).over("user_id") - pl.col("timestamp"))
.dt.total_seconds()
.truediv(3600)
.alias("duration_to_next"),
# Mark start and end events
(pl.int_range(pl.len()).over("user_id") == 0).alias("is_start"),
(pl.int_range(pl.len()).over("user_id") == (pl.len().over("user_id") - 1)).alias(
"is_end"
),
]
)
return journey_df
def _build_user_journeys(self, events_pl: pl.DataFrame) -> dict[str, list[dict[str, Any]]]:
"""Build user journeys from events - optimized version"""
# Use optimized Polars implementation
journey_df = self._build_user_journeys_optimized(events_pl)
# Convert to dictionary format only when needed for legacy methods
journeys = {}
# Group by user_id and iterate
for user_id, user_df in journey_df.group_by("user_id"):
user_id = user_id[0] if isinstance(user_id, tuple) else user_id # Handle group key
journey = []
for row in user_df.iter_rows(named=True):
journey.append(
{
"event": row["event_name"],
"timestamp": row["timestamp"],
"order": row["event_order"],
"duration_to_next": row["duration_to_next"],
"is_start": row["is_start"],
"is_end": row["is_end"],
}
)
journeys[str(user_id)] = journey
return journeys
def _discover_activities(
self,
events_pl: pl.DataFrame,
user_journeys: Optional[dict[str, list[dict[str, Any]]]] = None,
) -> dict[str, dict[str, Any]]:
"""Discover activities and their characteristics - optimized version"""
# Get optimized journey DataFrame
journey_df = self._build_user_journeys_optimized(events_pl)
# Calculate activity statistics using pure Polars
activity_stats = journey_df.group_by("event_name").agg(
[
pl.len().alias("frequency"),
pl.col("user_id").n_unique().alias("unique_users"),
pl.col("timestamp").min().alias("first_occurrence"),
pl.col("timestamp").max().alias("last_occurrence"),
pl.col("duration_to_next")
.filter(pl.col("duration_to_next").is_not_null())
.mean()
.alias("avg_duration"),
pl.col("is_start").sum().alias("start_count"),
pl.col("is_end").sum().alias("end_count"),
]
)
activities = {}
for row in activity_stats.iter_rows(named=True):
activity_name = row["event_name"]
# Classify activity type
activity_type = self._classify_activity_type(
activity_name, row["start_count"], row["end_count"], row["frequency"]
)
# Calculate success rate (simplified for performance)
success_rate = self._calculate_activity_success_rate_optimized(
activity_name, journey_df
)
activities[activity_name] = {
"frequency": row["frequency"],
"unique_users": row["unique_users"],
"avg_duration": row["avg_duration"] or 0,
"is_start": row["start_count"] > 0,
"is_end": row["end_count"] > 0,
"activity_type": activity_type,
"success_rate": success_rate,
"first_occurrence": row["first_occurrence"],
"last_occurrence": row["last_occurrence"],
}
return activities
def _discover_transitions(
self, user_journeys: dict[str, list[dict[str, Any]]], min_frequency: int
) -> dict[tuple[str, str], dict[str, Any]]:
"""Discover transitions between activities"""
transition_counts = defaultdict(int)
transition_users = defaultdict(set)
transition_durations = defaultdict(list)
# Count transitions across all user journeys
for user_id, journey in user_journeys.items():
for i in range(len(journey) - 1):
from_event = journey[i]["event"]
to_event = journey[i + 1]["event"]
transition = (from_event, to_event)
transition_counts[transition] += 1
transition_users[transition].add(user_id)
if journey[i]["duration_to_next"]:
transition_durations[transition].append(journey[i]["duration_to_next"])
# Filter by minimum frequency and build transition data
transitions = {}
total_transitions = sum(transition_counts.values())
for transition, frequency in transition_counts.items():
if frequency >= min_frequency:
from_event, to_event = transition
durations = transition_durations[transition]
transitions[transition] = {
"frequency": frequency,
"unique_users": len(transition_users[transition]),
"avg_duration": sum(durations) / len(durations) if durations else 0,
"probability": (frequency / total_transitions) * 100,
"transition_type": self._classify_transition_type(
from_event, to_event, frequency
),
}
return transitions
def _detect_cycles(
self,
user_journeys: dict[str, list[dict[str, Any]]],
transitions: dict[tuple[str, str], dict[str, Any]],
) -> list[dict[str, Any]]:
"""Detect cycles and loops in user behavior using graph analysis"""
cycles = []
# Build directed graph from transitions
G = nx.DiGraph()
for (from_event, to_event), data in transitions.items():
G.add_edge(from_event, to_event, weight=data["frequency"])
# Find simple cycles
try:
simple_cycles = list(nx.simple_cycles(G))
for cycle_path in simple_cycles:
if len(cycle_path) <= 5: # Focus on short cycles
# Calculate cycle statistics
cycle_frequency = self._calculate_cycle_frequency(cycle_path, user_journeys)
cycle_impact = self._assess_cycle_impact(cycle_path, user_journeys)
cycles.append(
{
"path": cycle_path,
"frequency": cycle_frequency,
"type": "loop" if len(cycle_path) == 1 else "cycle",
"impact": cycle_impact,
"avg_cycle_time": self._calculate_avg_cycle_time(
cycle_path, user_journeys
),
}
)
except nx.NetworkXError:
# Handle cases where cycle detection fails
pass
# Sort by frequency and return top cycles
cycles.sort(key=lambda x: x["frequency"], reverse=True)
return cycles[:10] # Return top 10 cycles
def _detect_cycles_optimized(
self,
journey_df: pl.DataFrame,
transitions: dict[tuple[str, str], dict[str, Any]],
) -> list[dict[str, Any]]:
"""
Optimized cycle detection using Polars operations instead of NetworkX.
Focuses on finding the most common and impactful cycles efficiently.
"""
cycles = []
try:
# Create transitions DataFrame for easier manipulation
if not transitions:
return cycles
transition_data = []
for (from_event, to_event), data in transitions.items():
transition_data.append(
{
"from_event": from_event,
"to_event": to_event,
"frequency": data["frequency"],
}
)
transitions_df = pl.DataFrame(transition_data)
# 1. Find self-loops (most common cycles)
self_loops = transitions_df.filter(pl.col("from_event") == pl.col("to_event")).sort(
"frequency", descending=True
)
for row in self_loops.iter_rows(named=True):
event = row["from_event"]
frequency = row["frequency"]
# Calculate impact based on frequency
impact = (
"negative"
if any(keyword in event.lower() for keyword in ["error", "fail", "timeout"])
else "positive"
)
cycles.append(
{
"path": [event],
"frequency": frequency,
"type": "loop",
"impact": impact,
"avg_cycle_time": self._estimate_avg_cycle_time_optimized(
journey_df, [event]
),
}
)
# 2. Find 2-step cycles (A -> B -> A) using efficient joins
two_step_cycles = (
transitions_df.join(
transitions_df,
left_on="to_event",
right_on="from_event",
how="inner",
suffix="_right",
)
.filter(pl.col("from_event") == pl.col("to_event_right")) # Forms a cycle
.filter(pl.col("from_event") != pl.col("to_event")) # Not self-loops
.with_columns(
[pl.min_horizontal(["frequency", "frequency_right"]).alias("cycle_frequency")]
)
.select(
[
"from_event",
pl.col("to_event").alias("middle_event"),
"cycle_frequency",
]
)
.sort("cycle_frequency", descending=True)
.limit(5) # Top 5 two-step cycles
)
for row in two_step_cycles.iter_rows(named=True):
path = [row["from_event"], row["middle_event"]]
frequency = row["cycle_frequency"]
# Assess impact
impact = (
"negative"
if any("error" in event.lower() or "fail" in event.lower() for event in path)
else "positive"