prosit-pm 1.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
prosit/__init__.py ADDED
@@ -0,0 +1,5 @@
1
+ __version__ = "1.0.1"
2
+
3
+ from prosit.simulator import SimulatorParameters, SimulatorEngine
4
+
5
+ __all__ = ["SimulatorParameters", "SimulatorEngine"]
File without changes
@@ -0,0 +1,192 @@
1
+ import pm4py
2
+ from pm4py.objects.log.obj import EventLog
3
+
4
+
5
+ DEFAULT_MIN_CONFIDENCE = 0.1
6
+ DEFAULT_MIN_SUPPORT = 0.1
7
+ DEFAULT_MIN_PARTICIPATION = 0.4
8
+
9
+ # The arrival calendar uses a much stricter support (cover most observed
10
+ # arrivals) than resource calendars.
11
+ DEFAULT_ARRIVAL_MIN_SUPPORT = 0.7
12
+
13
+
14
+ def _full_week_calendar(active: bool = True) -> dict:
15
+ return {wd: {h: active for h in range(24)} for wd in range(7)}
16
+
17
+
18
+ def _build_calendar_from_slot_data(
19
+ task_wd_dates: dict,
20
+ task_wd_h_dates: dict,
21
+ wd_h_count: dict,
22
+ wd_h_tasks: dict,
23
+ total_events: int,
24
+ min_confidence: float,
25
+ min_support: float,
26
+ ) -> dict:
27
+ """Apply a confidence + support filter to the accumulated slot data of a
28
+ single resource (or pseudo-resource).
29
+
30
+ - **Confidence**: for each (weekday, hour) seen, accept the slot when the
31
+ most frequent task has ``#dates(t,wd,h) / #dates(t,wd) >= min_confidence``.
32
+ - **Support boost**: if accepted slots cover < ``min_support`` of the
33
+ events, greedily re-add discarded slots ordered by frequency until the
34
+ support is met.
35
+ - Empty result falls back to a 24/7 calendar."""
36
+
37
+ cal = _full_week_calendar(False)
38
+ if total_events == 0:
39
+ return cal
40
+
41
+ accepted_events = 0
42
+ discarded = []
43
+ for wd in range(7):
44
+ for h in range(24):
45
+ slot_count = wd_h_count[wd][h]
46
+ if slot_count == 0:
47
+ continue
48
+ best_conf = 0.0
49
+ for t in wd_h_tasks[wd][h]:
50
+ h_dates = task_wd_h_dates[t][wd][h]
51
+ wd_dates = task_wd_dates[t][wd]
52
+ conf = len(h_dates) / len(wd_dates) if wd_dates else 0
53
+ if conf > best_conf:
54
+ best_conf = conf
55
+ if best_conf >= min_confidence:
56
+ cal[wd][h] = True
57
+ accepted_events += slot_count
58
+ else:
59
+ discarded.append((wd, h, slot_count))
60
+
61
+ support = accepted_events / total_events
62
+ if support < min_support and discarded:
63
+ discarded.sort(key=lambda x: x[2], reverse=True)
64
+ for wd, h, cnt in discarded:
65
+ cal[wd][h] = True
66
+ accepted_events += cnt
67
+ support = accepted_events / total_events
68
+ if support >= min_support:
69
+ break
70
+
71
+ if accepted_events == 0:
72
+ cal = _full_week_calendar(True)
73
+ return cal
74
+
75
+
76
+ def discover_arrival_calendar(
77
+ log: EventLog,
78
+ min_confidence: float = DEFAULT_MIN_CONFIDENCE,
79
+ min_support: float = DEFAULT_ARRIVAL_MIN_SUPPORT,
80
+ ) -> dict:
81
+ """Return a ``{weekday: {hour: bool}}`` calendar for case arrivals.
82
+
83
+ Uses the same confidence + support + greedy-boost pipeline as
84
+ ``discover_res_calendars``, treating all arrivals as a single virtual
85
+ resource performing a single virtual task. The participation filter is
86
+ skipped (only one resource). Granularity is 60 minutes per slot."""
87
+
88
+ task = '__arrival__'
89
+ task_wd_dates = {task: {wd: set() for wd in range(7)}}
90
+ task_wd_h_dates = {
91
+ task: {wd: {h: set() for h in range(24)} for wd in range(7)}
92
+ }
93
+ wd_h_count = {wd: {h: 0 for h in range(24)} for wd in range(7)}
94
+ wd_h_tasks = {wd: {h: set() for h in range(24)} for wd in range(7)}
95
+ total_events = 0
96
+
97
+ for trace in log:
98
+ ts = trace[0]['start:timestamp']
99
+ wd, h, date_key = ts.weekday(), ts.hour, ts.date()
100
+ task_wd_dates[task][wd].add(date_key)
101
+ task_wd_h_dates[task][wd][h].add(date_key)
102
+ wd_h_count[wd][h] += 1
103
+ wd_h_tasks[wd][h].add(task)
104
+ total_events += 1
105
+
106
+ return _build_calendar_from_slot_data(
107
+ task_wd_dates, task_wd_h_dates,
108
+ wd_h_count, wd_h_tasks,
109
+ total_events,
110
+ min_confidence, min_support,
111
+ )
112
+
113
+
114
+ def discover_res_calendars(
115
+ log: EventLog,
116
+ resources: list = None,
117
+ min_confidence: float = DEFAULT_MIN_CONFIDENCE,
118
+ min_support: float = DEFAULT_MIN_SUPPORT,
119
+ min_participation: float = DEFAULT_MIN_PARTICIPATION,
120
+ ) -> dict:
121
+ """Return ``{resource: {weekday: {hour: bool}}}`` at 60-minute granularity.
122
+
123
+ - **Participation**: a resource gets a discovered calendar only if
124
+ ``sum_t count[r,t] / sum_t max_r' count[r',t] >= min_participation``;
125
+ otherwise it falls back to a 24/7 calendar.
126
+ - **Confidence + support**: per-resource calendar built by
127
+ ``_build_calendar_from_slot_data``.
128
+
129
+ Each event is registered once at its end timestamp."""
130
+
131
+ if resources is None:
132
+ resources = []
133
+ if not resources:
134
+ resources = list(pm4py.get_event_attribute_values(log, 'org:resource').keys())
135
+
136
+ res_set = set(resources)
137
+ res_task_count = {r: {} for r in resources}
138
+ res_task_wd_dates = {r: {} for r in resources}
139
+ res_task_wd_h_dates = {r: {} for r in resources}
140
+ res_wd_h_count = {
141
+ r: {wd: {h: 0 for h in range(24)} for wd in range(7)} for r in resources
142
+ }
143
+ res_wd_h_tasks = {
144
+ r: {wd: {h: set() for h in range(24)} for wd in range(7)} for r in resources
145
+ }
146
+ res_total_events = {r: 0 for r in resources}
147
+
148
+ for trace in log:
149
+ for event in trace:
150
+ r = event.get('org:resource')
151
+ if r not in res_set:
152
+ continue
153
+ t = event.get('concept:name')
154
+ ts = event.get('time:timestamp') or event.get('start:timestamp')
155
+ if t is None or ts is None:
156
+ continue
157
+ wd, h, date_key = ts.weekday(), ts.hour, ts.date()
158
+
159
+ res_task_count[r][t] = res_task_count[r].get(t, 0) + 1
160
+ res_task_wd_dates[r].setdefault(t, {}).setdefault(wd, set()).add(date_key)
161
+ res_task_wd_h_dates[r].setdefault(t, {}).setdefault(wd, {}).setdefault(h, set()).add(date_key)
162
+ res_wd_h_count[r][wd][h] += 1
163
+ res_wd_h_tasks[r][wd][h].add(t)
164
+ res_total_events[r] += 1
165
+
166
+ max_resource_task_freq = {}
167
+ for r in resources:
168
+ for t, c in res_task_count[r].items():
169
+ if c > max_resource_task_freq.get(t, 0):
170
+ max_resource_task_freq[t] = c
171
+
172
+ calendars = {}
173
+ for r in resources:
174
+ if res_total_events[r] == 0:
175
+ calendars[r] = _full_week_calendar(False)
176
+ continue
177
+
178
+ denom = sum(max_resource_task_freq.get(t, 0) for t in res_task_count[r])
179
+ numer = sum(res_task_count[r].values())
180
+ participation = numer / denom if denom else 0
181
+ if participation < min_participation:
182
+ calendars[r] = _full_week_calendar(True)
183
+ continue
184
+
185
+ calendars[r] = _build_calendar_from_slot_data(
186
+ res_task_wd_dates[r], res_task_wd_h_dates[r],
187
+ res_wd_h_count[r], res_wd_h_tasks[r],
188
+ res_total_events[r],
189
+ min_confidence, min_support,
190
+ )
191
+
192
+ return calendars
@@ -0,0 +1,145 @@
1
+ import warnings
2
+ import pandas as pd
3
+ from tqdm import tqdm
4
+
5
+ from sklearn.tree import DecisionTreeClassifier
6
+ from sklearn.model_selection import GridSearchCV
7
+ from sklearn.dummy import DummyClassifier
8
+ from sklearn.exceptions import FitFailedWarning
9
+ from sklearn.pipeline import Pipeline
10
+
11
+ from prosit.utils.rule_utils import DecisionRules, BINARY_NEG_LOG_LOSS_SCORER, prune_low_signal_columns
12
+
13
+
14
+
15
+
16
+ def discover_weight_transitions(
17
+ df_features: pd.DataFrame,
18
+ net_transition_labels: list,
19
+ max_depths_cv: list = range(1, 6),
20
+ label_data_attributes: list = [],
21
+ label_data_attributes_categorical: list = [],
22
+ values_categorical: dict = dict(),
23
+ random_state: int = 72
24
+ ) -> dict:
25
+
26
+ if not max_depths_cv:
27
+ transitions = df_features['transition'].unique()
28
+ # Keyed by transition name (str): PetriNet.Transition hashes by id, so
29
+ # object keys drift from self.net.transitions under pickle/alignment
30
+ # round-trips and break every downstream lookup done by identity.
31
+ transition_weights = {t.name: (df_features["transition"] == t).sum() / df_features["prev_enabled_transitions"].apply(lambda t_set: t in t_set).sum() for t in transitions}
32
+ else:
33
+ transition_weights = build_models(
34
+ df_features,
35
+ net_transition_labels,
36
+ label_data_attributes,
37
+ label_data_attributes_categorical,
38
+ values_categorical,
39
+ max_depths_cv=max_depths_cv,
40
+ random_state=random_state
41
+ )
42
+
43
+ return transition_weights
44
+
45
+
46
+ def build_models(
47
+ df_features: pd.DataFrame,
48
+ net_transition_labels: list,
49
+ label_data_attributes: list,
50
+ label_data_attributes_categorical: list,
51
+ values_categorical: dict,
52
+ max_depths_cv: list = range(1,6),
53
+ random_state: int = 72
54
+ ) -> dict:
55
+
56
+ datasets_t = build_training_datasets(
57
+ df_features,
58
+ net_transition_labels,
59
+ label_data_attributes
60
+ )
61
+
62
+ param_grid = [
63
+ {
64
+ 'clf': [DecisionTreeClassifier(random_state=random_state)],
65
+ 'clf__max_depth': list(max_depths_cv),
66
+ },
67
+ {'clf': [DummyClassifier(strategy='prior', random_state=random_state)]},
68
+ ]
69
+
70
+ models_t = dict()
71
+
72
+ for t in tqdm(datasets_t.keys(), desc='transition models'):
73
+ data_t = datasets_t[t]
74
+ if len(data_t['class'].unique()) < 2:
75
+ # Constant label — store scalar probability so the simulator picks
76
+ # it up via the numeric branch in
77
+ # compute_transition_weights_from_model.
78
+ models_t[t] = float(data_t['class'].iloc[0]) if len(data_t) else 0.0
79
+ continue
80
+
81
+ for a in label_data_attributes_categorical:
82
+ for v in values_categorical[a]:
83
+ data_t[a + ' = ' + str(v)] = (data_t[a] == v).astype(int)
84
+ del data_t[a]
85
+
86
+ X = data_t.drop(columns=['class'])
87
+ X = prune_low_signal_columns(X)
88
+ y = data_t['class']
89
+
90
+ if max_depths_cv:
91
+ pipe = Pipeline([('clf', DecisionTreeClassifier(random_state=random_state))])
92
+ try:
93
+ with warnings.catch_warnings():
94
+ warnings.simplefilter('ignore', FitFailedWarning)
95
+ warnings.simplefilter('ignore', UserWarning)
96
+ grid_search = GridSearchCV(
97
+ estimator=pipe,
98
+ param_grid=param_grid,
99
+ cv=5,
100
+ scoring=BINARY_NEG_LOG_LOSS_SCORER,
101
+ ).fit(X, y)
102
+ best_clf = grid_search.best_estimator_.named_steps['clf']
103
+ except Exception:
104
+ best_clf = DecisionTreeClassifier(max_depth=2, random_state=random_state).fit(X, y)
105
+
106
+ if isinstance(best_clf, DummyClassifier):
107
+ models_t[t] = float(y.mean())
108
+ continue
109
+ clf_t_dtc = best_clf
110
+ else:
111
+ clf_t_dtc = DecisionTreeClassifier(random_state=random_state, max_depth=1)
112
+ clf_t_dtc.fit(X, y)
113
+
114
+ clf_t = DecisionRules()
115
+ clf_t.from_decision_tree(clf_t_dtc)
116
+ models_t[t] = clf_t
117
+
118
+ return models_t
119
+
120
+
121
+
122
+ def build_training_datasets(
123
+ df_features: pd.DataFrame,
124
+ net_transition_labels: list,
125
+ label_data_attributes: list
126
+ ) -> dict:
127
+
128
+ df_cf = df_features[["transition", "prev_enabled_transitions"] + label_data_attributes + net_transition_labels].copy()
129
+
130
+ df_cf = df_cf.explode('prev_enabled_transitions')
131
+ df_cf['class'] = (df_cf['prev_enabled_transitions'] == df_cf['transition']).astype(int)
132
+
133
+ df_cf = df_cf.drop(columns=['transition'])
134
+ # Group by transition NAME (str) rather than the Transition object: object
135
+ # identity can drift across alignment/pickle boundaries, which would
136
+ # silently break every downstream lookup done by identity.
137
+ df_cf['transition_name'] = df_cf['prev_enabled_transitions'].apply(lambda t: t.name)
138
+ df_cf = df_cf.drop(columns=['prev_enabled_transitions'])
139
+
140
+ datasets_t = {
141
+ t_name: group.drop(columns=['transition_name']).reset_index(drop=True)
142
+ for t_name, group in df_cf.groupby('transition_name', sort=False)
143
+ }
144
+
145
+ return datasets_t
@@ -0,0 +1,122 @@
1
+ from collections import Counter
2
+
3
+ import numpy as np
4
+ import pm4py
5
+ from pm4py.objects.log.obj import EventLog
6
+
7
+ from prosit.utils.distribution_utils import return_best_distribution
8
+
9
+ _ATTR_DIST_SEARCH = ['norm', 'expon', 'lognorm', 'gamma', 'uniform']
10
+
11
+
12
+ def discover_attributes_distribution(
13
+ log: EventLog,
14
+ label_data_attributes: list,
15
+ label_data_attributes_categorical: list,
16
+ mode: str = 'empirical'
17
+ ) -> dict:
18
+ """
19
+ Discover the distribution of case-level data attributes.
20
+
21
+ mode='empirical' — samples from the joint empirical distribution of attribute
22
+ tuples observed in the log. Preserves correlations between
23
+ attributes but scales poorly with many attributes/values.
24
+
25
+ mode='distribution' — models each attribute independently:
26
+ categorical -> frequency table {value: probability};
27
+ continuous -> best-fitting scipy distribution.
28
+ Scales well but ignores inter-attribute correlations.
29
+ """
30
+ if mode == 'empirical':
31
+ list_data_attributes = []
32
+ for trace in log:
33
+ try:
34
+ list_data_attributes.append(tuple(trace[0][a] for a in label_data_attributes))
35
+ except (KeyError, IndexError):
36
+ continue
37
+ frequency = Counter(list_data_attributes)
38
+ total = len(list_data_attributes)
39
+ data = {t: count / total for t, count in frequency.items()}
40
+ return {'mode': 'empirical', 'data': data}
41
+
42
+ elif mode == 'distribution':
43
+ df_log = pm4py.convert_to_dataframe(log)
44
+ # One row per case: take the first event's attributes
45
+ first_events = df_log.groupby('case:concept:name').first().reset_index()
46
+
47
+ data = {}
48
+ for a in label_data_attributes:
49
+ values = first_events[a].dropna()
50
+ if a in label_data_attributes_categorical:
51
+ freq = values.value_counts(normalize=True)
52
+ data[a] = {'type': 'categorical', 'values': freq.to_dict()}
53
+ else:
54
+ values_list = values.tolist()
55
+ if len(values_list) == 0:
56
+ data[a] = {'type': 'continuous', 'dist': 'fixed',
57
+ 'params': [0.0], 'min': 0.0, 'max': 0.0, 'mean': 0.0}
58
+ continue
59
+ dist, params = return_best_distribution(values_list, dist_search=_ATTR_DIST_SEARCH)
60
+ dist_name = dist if dist == 'fixed' else dist.name
61
+ data[a] = {
62
+ 'type': 'continuous',
63
+ 'dist': dist_name,
64
+ 'params': list(params),
65
+ 'min': float(np.min(values_list)),
66
+ 'max': float(np.max(values_list)),
67
+ 'mean': float(np.mean(values_list))
68
+ }
69
+ return {'mode': 'distribution', 'data': data}
70
+
71
+ else:
72
+ raise ValueError(f"Unknown attribute_mode '{mode}'. Use 'empirical' or 'distribution'.")
73
+
74
+
75
+ def return_label_data_attributes(log: EventLog) -> tuple:
76
+
77
+ standard_xes_columns = {
78
+ "case:concept:name", "concept:name", "time:timestamp",
79
+ "start:timestamp", "org:resource", "org:role"
80
+ }
81
+
82
+ import pandas as pd
83
+
84
+ df_log = pm4py.convert_to_dataframe(log)
85
+ label_data_attributes = list(set(df_log.columns) - standard_xes_columns)
86
+
87
+ # String-typed attributes that parse as numeric for every non-null value
88
+ # (e.g. BPI12's ``case:AMOUNT_REQ`` stored as "20000") are treated as
89
+ # continuous, not categorical — otherwise each distinct value becomes a
90
+ # one-hot column and most are pruned as low-signal.
91
+ label_data_attributes_categorical = []
92
+ numeric_attrs = []
93
+ for l in label_data_attributes:
94
+ col = df_log[l]
95
+ if col.dtype == 'object':
96
+ coerced = pd.to_numeric(col, errors='coerce')
97
+ non_null_src = col.notna().sum()
98
+ if non_null_src > 0 and coerced.notna().sum() == non_null_src:
99
+ numeric_attrs.append(l)
100
+ else:
101
+ label_data_attributes_categorical.append(l)
102
+
103
+ # Coerce values in place on the log so downstream consumers (the training
104
+ # df builders, which read via ``trace[0][a]``) see floats. ``convert_to_dataframe``
105
+ # above has already broadcast case-level attributes onto every event.
106
+ if numeric_attrs:
107
+ for trace in log:
108
+ for a in numeric_attrs:
109
+ short_a = a[5:] if a.startswith('case:') else a
110
+ if short_a in trace.attributes:
111
+ try:
112
+ trace.attributes[short_a] = float(trace.attributes[short_a])
113
+ except (ValueError, TypeError):
114
+ pass
115
+ for event in trace:
116
+ if a in event:
117
+ try:
118
+ event[a] = float(event[a])
119
+ except (ValueError, TypeError):
120
+ pass
121
+
122
+ return label_data_attributes, label_data_attributes_categorical
File without changes
@@ -0,0 +1,48 @@
1
+ import pandas as pd
2
+ from prosit.discovery.cf_discovery import build_training_datasets
3
+ from tqdm import tqdm
4
+ from river import tree
5
+ from prosit.utils.rule_utils import DecisionRules
6
+
7
+
8
+ def incremental_transition_weights_learning(
9
+ df_features: pd.DataFrame,
10
+ net_transition_labels: list,
11
+ max_depth: int = 3,
12
+ grace_period: int = 1000,
13
+ label_data_attributes: list = [],
14
+ label_data_attributes_categorical: list = [],
15
+ values_categorical: dict = dict(),
16
+ ):
17
+
18
+ datasets_t = build_training_datasets(
19
+ df_features,
20
+ net_transition_labels,
21
+ label_data_attributes
22
+ )
23
+
24
+ models_t = dict()
25
+
26
+ for t in tqdm(datasets_t.keys()):
27
+ data_t = datasets_t[t]
28
+ if len(data_t['class'].unique()) < 2:
29
+ models_t[t] = float(data_t['class'].iloc[0]) if len(data_t) else 0.0
30
+ continue
31
+
32
+ for a in label_data_attributes_categorical:
33
+ for v in values_categorical[a]:
34
+ data_t[a+' = '+str(v)] = (data_t[a] == v).astype(int)
35
+ del data_t[a]
36
+
37
+ m_t = tree.HoeffdingAdaptiveTreeClassifier(seed=72, max_depth=max_depth, grace_period=grace_period, leaf_prediction="mc")
38
+
39
+ for _, row in data_t.iterrows():
40
+ X_row = row.drop('class').to_dict()
41
+ y_row = row['class']
42
+ m_t.learn_one(X_row, y_row)
43
+
44
+ clf_t = DecisionRules()
45
+ clf_t.from_river_decision_tree(m_t)
46
+ models_t[t] = clf_t
47
+
48
+ return models_t
@@ -0,0 +1,62 @@
1
+ import pandas as pd
2
+ from prosit.discovery.resource_discovery import build_training_datasets
3
+ from tqdm import tqdm
4
+ from river import tree
5
+ from prosit.utils.rule_utils import DecisionRules
6
+
7
+
8
+ def incremental_resource_weights_learning(
9
+ df_features: pd.DataFrame,
10
+ act_to_resources: dict,
11
+ resources: list,
12
+ max_concurrency: dict,
13
+ max_depth: int = 3,
14
+ grace_period: int = 1000,
15
+ label_data_attributes: list = [],
16
+ label_data_attributes_categorical: list = [],
17
+ values_categorical: dict = dict(),
18
+ use_workload_features: bool = False,
19
+ ):
20
+ """Return ``{resource: classifier}`` using incremental trees.
21
+
22
+ Mirrors :func:`prosit.discovery.resource_discovery.build_models` but trains
23
+ a ``HoeffdingAdaptiveTreeClassifier`` per resource on the events where
24
+ the resource was a free candidate at the enabling time.
25
+ """
26
+
27
+ df_features = df_features[~df_features["resource"].isna()]
28
+
29
+ datasets = build_training_datasets(
30
+ df_features,
31
+ act_to_resources,
32
+ resources,
33
+ max_concurrency,
34
+ label_data_attributes,
35
+ use_workload_features=use_workload_features,
36
+ )
37
+
38
+ models = dict()
39
+
40
+ for r, data_r in tqdm(datasets.items()):
41
+ if len(data_r['class'].unique()) < 2:
42
+ models[r] = float(data_r['class'].iloc[0]) if len(data_r) else 0.0
43
+ continue
44
+
45
+ for a in label_data_attributes_categorical:
46
+ for v in values_categorical[a]:
47
+ data_r[a + ' = ' + str(v)] = (data_r[a] == v).astype(int)
48
+ del data_r[a]
49
+
50
+ m = tree.HoeffdingAdaptiveTreeClassifier(
51
+ seed=72, max_depth=max_depth, grace_period=grace_period, leaf_prediction="mc"
52
+ )
53
+ for _, row in data_r.iterrows():
54
+ X_row = row.drop('class').to_dict()
55
+ y_row = row['class']
56
+ m.learn_one(X_row, y_row)
57
+
58
+ clf = DecisionRules()
59
+ clf.from_river_decision_tree(m)
60
+ models[r] = clf
61
+
62
+ return models