mining: replace the stateful mining classes with pure functions #66

Open
opened 2026-10-08 10:50:30 +00:00 by ffrauenberger · 0 comments
Owner

Severity: improvement (the internal state of the mining classes causes problems in the app)
Requested by: the user (2026-10-08)

Where

  • app/mining_algorithms/mining_interface.py:8-50 — MiningInterface holds graph, min_node_size, logger; create_mining_instance builds an instance.
  • app/mining_algorithms/base_mining.py:10-95 — the constructor computes events, succession matrix, filtered logs, thresholds, start/end nodes and stores all of it on self.
  • app/mining_algorithms/genetic_mining.py:31-60, :62-135 — generate_graph first overwrites self.* parameters and results (dependency_matrix, best_individual, petri_net, …), then reads them back.
  • app/mining_algorithms/alpha_mining.py:9, fuzzy_mining.py:8, heuristic_mining.py:8, inductive_mining.py:17, inductive_mining_infrequent.py:16 — all subclass BaseMining.
  • app/ui/base_algorithm_ui/base_algorithm_controller.py:42, :70, :78-110, :162 — the controller keeps one model instance, calls generate_graph(**params) on it and reads getters (get_spm_threshold, …) to find out what the model did.

Problem

Each mining algorithm is a class. An instance carries its log, its thresholds, its intermediate results and its graph. generate_graph changes that state. So the result depends on the history of the instance, not only on the input.

This causes problems in the app:

  • The same input can give a different graph, depending on which calls came before on the instance.
  • The instance lives in the Streamlit session, so stale state survives reruns and parameter changes.
  • The controller has to ask the model what it did (getters, attribute reads) and compare that to the UI values. This is why have_parameters_changed and the threshold syncing exist.
  • The state cannot be cached, run in a worker, or tested without building an instance and checking its fields.

Related: #46 (models read and write st.session_state) and #48 (no parameter contract). This issue is the wider restructuring; those two are parts of it.

Fix

Make each algorithm a pure function of its input:

  • mine_<algorithm>(log, params) -> MiningResult (for example mine_heuristic, mine_genetic). MiningResult is an immutable value holding the graph and the effective parameters (for example the thresholds after normalizing).
  • The same log and params always give the same result. No instance fields, no streamlit, no module-level state, no reading of the clock. A random algorithm (genetic) takes a seed in params.
  • Shared steps from BaseMining (events, succession matrix, SPM and frequency filters, start/end nodes) become pure helper functions that take their input and return a new value. They do not write to self.
  • Remove MiningInterface, BaseMining and create_mining_instance. Callers get a registry of name to function, with the parameter schema from #48.
  • The controller keeps only the MiningResult and the last params. It calls the function again when the input changes. Streamlit's own caching can be used on top, since the function is pure.
  • Migrate one algorithm at a time, with the old class delegating to the new function until the last caller has moved.

Sub-issues

One issue per miner:

  • #67 mining.alpha
  • #68 mining.heuristic
  • #69 mining.fuzzy
  • #70 mining.inductive
  • #71 mining.inductive_infrequent (after #70)
  • #72 mining.genetic

Check

  • For each algorithm, a unit test calls the function twice with the same input and compares the results (equal graph and parameters).
  • A test calls it with the input unchanged after a call with other parameters, and the result is the same as on a fresh process.
  • git grep -n "^class .*Mining" app/mining_algorithms returns nothing, and git grep -n streamlit app/mining_algorithms returns nothing.
  • python -m unittest discover -s tests -p '*_test.py' passes.

from a session on 2026-10-08

**Severity:** improvement (the internal state of the mining classes causes problems in the app) **Requested by:** the user (2026-10-08) ## Where - `app/mining_algorithms/mining_interface.py:8-50` — `MiningInterface` holds `graph`, `min_node_size`, `logger`; `create_mining_instance` builds an instance. - `app/mining_algorithms/base_mining.py:10-95` — the constructor computes events, succession matrix, filtered logs, thresholds, start/end nodes and stores all of it on `self`. - `app/mining_algorithms/genetic_mining.py:31-60`, `:62-135` — `generate_graph` first overwrites `self.*` parameters and results (`dependency_matrix`, `best_individual`, `petri_net`, …), then reads them back. - `app/mining_algorithms/alpha_mining.py:9`, `fuzzy_mining.py:8`, `heuristic_mining.py:8`, `inductive_mining.py:17`, `inductive_mining_infrequent.py:16` — all subclass `BaseMining`. - `app/ui/base_algorithm_ui/base_algorithm_controller.py:42`, `:70`, `:78-110`, `:162` — the controller keeps one model instance, calls `generate_graph(**params)` on it and reads getters (`get_spm_threshold`, …) to find out what the model did. ## Problem Each mining algorithm is a class. An instance carries its log, its thresholds, its intermediate results and its graph. `generate_graph` changes that state. So the result depends on the history of the instance, not only on the input. This causes problems in the app: - The same input can give a different graph, depending on which calls came before on the instance. - The instance lives in the Streamlit session, so stale state survives reruns and parameter changes. - The controller has to ask the model what it did (getters, attribute reads) and compare that to the UI values. This is why `have_parameters_changed` and the threshold syncing exist. - The state cannot be cached, run in a worker, or tested without building an instance and checking its fields. Related: #46 (models read and write `st.session_state`) and #48 (no parameter contract). This issue is the wider restructuring; those two are parts of it. ## Fix Make each algorithm a pure function of its input: - `mine_<algorithm>(log, params) -> MiningResult` (for example `mine_heuristic`, `mine_genetic`). `MiningResult` is an immutable value holding the graph and the effective parameters (for example the thresholds after normalizing). - The same `log` and `params` always give the same result. No instance fields, no `streamlit`, no module-level state, no reading of the clock. A random algorithm (genetic) takes a `seed` in `params`. - Shared steps from `BaseMining` (events, succession matrix, SPM and frequency filters, start/end nodes) become pure helper functions that take their input and return a new value. They do not write to `self`. - Remove `MiningInterface`, `BaseMining` and `create_mining_instance`. Callers get a registry of name to function, with the parameter schema from #48. - The controller keeps only the `MiningResult` and the last `params`. It calls the function again when the input changes. Streamlit's own caching can be used on top, since the function is pure. - Migrate one algorithm at a time, with the old class delegating to the new function until the last caller has moved. ## Sub-issues One issue per miner: - [ ] #67 `mining.alpha` - [ ] #68 `mining.heuristic` - [ ] #69 `mining.fuzzy` - [ ] #70 `mining.inductive` - [ ] #71 `mining.inductive_infrequent` (after #70) - [ ] #72 `mining.genetic` ## Check - For each algorithm, a unit test calls the function twice with the same input and compares the results (equal graph and parameters). - A test calls it with the input unchanged after a call with other parameters, and the result is the same as on a fresh process. - `git grep -n "^class .*Mining" app/mining_algorithms` returns nothing, and `git grep -n streamlit app/mining_algorithms` returns nothing. - `python -m unittest discover -s tests -p '*_test.py'` passes. --- from a session on 2026-10-08
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
ffrauenberger/ProcessIntel#66
No description provided.