Skip to content

Commit 8ebf398

Browse files
Merge remote-tracking branch 'upstream/hotfixes' into release
2 parents 1b1e9a5 + 03e33cf commit 8ebf398

3 files changed

Lines changed: 18 additions & 19 deletions

File tree

pm4py/algo/discovery/genetic/util.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,21 @@
2525
import itertools
2626

2727
# typing
28-
from typing import Self, Union
29-
from collections.abc import Iterable
30-
TransitionMap = dict[str,list[frozenset]]
28+
from typing import Dict, FrozenSet, Iterable, List, Set, Tuple, Union
29+
TransitionMap = Dict[str, List[FrozenSet[str]]]
3130
InputMap = OutputMap = TransitionMap
32-
Individual = tuple[InputMap,OutputMap]
31+
Individual = Tuple[InputMap, OutputMap]
3332

3433
class iset(frozenset):
3534
"Indexable frozenset printing as set, i.e. without `frozenset(…)`"
3635
def __repr__(self):
3736
return "{" + repr(sorted(self))[1:-1] + "}"
3837

3938
@staticmethod
40-
def flat(item: Iterable) -> Self:
39+
def flat(item: Iterable) -> "iset":
4140
return iset(itertools.chain(*item))
4241

43-
def rand_partition(pool: Iterable) -> list[Union[set,frozenset]]:
42+
def rand_partition(pool: Iterable) -> List[Union[Set[str], FrozenSet[str]]]:
4443
pool = set(pool)
4544
# also ensures no activity in two partitions
4645
# s. 4. Causal Matrix, Def. 4; https://doi.org/10.1007/11494744_5
@@ -70,7 +69,7 @@ def remove_transition_from_partitions(tmap: TransitionMap, key: str, t: str):
7069
del partitions[i]
7170
return
7271

73-
def get_src_sink_sets_for_wfnet(I: InputMap, O: OutputMap, T: list[str]) -> tuple[list[str],list[str]]:
72+
def get_src_sink_sets_for_wfnet(I: InputMap, O: OutputMap, T: List[str]) -> Tuple[List[str], List[str]]:
7473
"""Determines input set and output set, which need to be connected by a place to create a WF-net"""
7574
def add2graphs(graphs, t, nextT):
7675
# find graph

pm4py/algo/discovery/genetic/variants/classic.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def tqdm(iterable=None, *args, **kwargs):
4747
from pm4py.objects.genetic_matrix.obj import GeneticMatrix
4848

4949
# typing
50-
from typing import Any, Iterable, Optional, Union
50+
from typing import Any, Dict, Iterable, List, Optional, Tuple, Union
5151
from pandas.core.frame import DataFrame
5252
from pm4py.objects.log.obj import EventLog, EventStream
5353
from pm4py.objects.petri_net.obj import PetriNet, Marking
@@ -56,8 +56,8 @@ def tqdm(iterable=None, *args, **kwargs):
5656

5757
def apply(
5858
log: Union[EventLog, EventStream, DataFrame],
59-
parameters: Optional[dict[Union[str, Parameters], Any]] = None,
60-
) -> tuple[PetriNet, Marking, Marking]:
59+
parameters: Optional[Dict[Union[str, Parameters], Any]] = None,
60+
) -> Tuple[PetriNet, Marking, Marking]:
6161
"""
6262
Discovers a Petri net using Genetic Miner
6363
@@ -178,7 +178,7 @@ def apply(
178178
log_csv.writerow([datetime.now(), len(history)] + fitness)
179179
return matrix2petrinet(GeneticMatrix(*population[0], T))
180180

181-
def individuals(log: Union[DataFrame, EventLog], sample_size=1, T=None, keys: dict[str,str] = {"activity_key":xes.DEFAULT_NAME_KEY, "timestamp_key":xes.DEFAULT_TIMESTAMP_KEY, "case_id_key":constants.CASE_CONCEPT_NAME}) -> list[Individual]:
181+
def individuals(log: Union[DataFrame, EventLog], sample_size=1, T=None, keys: Dict[str, str] = {"activity_key":xes.DEFAULT_NAME_KEY, "timestamp_key":xes.DEFAULT_TIMESTAMP_KEY, "case_id_key":constants.CASE_CONCEPT_NAME}) -> List[Individual]:
182182
if not T:
183183
T = tuple(log[keys['activity_key']].unique())
184184
# polyfill for itertools.pairwise (requires Python 3.10+)
@@ -217,14 +217,14 @@ def pairwise(iterable):
217217
return samples
218218

219219
def tournament(
220-
population: list[Individual],
220+
population: List[Individual],
221221
log: Union[DataFrame, EventLog],
222222
T,
223223
sort=True,
224224
activity_key: str = xes.DEFAULT_NAME_KEY,
225225
timestamp_key: str = xes.DEFAULT_TIMESTAMP_KEY,
226226
case_id_key: str = constants.CASE_CONCEPT_NAME,
227-
) -> tuple[list[Individual], list[float]]:
227+
) -> Tuple[List[Individual], List[float]]:
228228
"""sort=True: sort descending by fitness (i.e. best first)"""
229229
# @src 6.2. Fitness Calculation; https://doi.org/10.1007/11494744_5
230230
fitness = []
@@ -260,7 +260,7 @@ def tournament(
260260
population, fitness = list(population), list(fitness)
261261
return (population, fitness)
262262

263-
def sample_parents(population: list[Individual], fitness: list[float], elitism_min_sample: int):
263+
def sample_parents(population: List[Individual], fitness: List[float], elitism_min_sample: int):
264264
population_fitness = tuple(zip(population, fitness))
265265
parent1 = sorted(
266266
random.sample(population_fitness, k=elitism_min_sample),
@@ -277,7 +277,7 @@ def sample_parents(population: list[Individual], fitness: list[float], elitism_m
277277
)[0][0]
278278
return (parent1, parent2)
279279

280-
def crossover(parent1: Individual, parent2: Individual, T: list[str]) -> tuple[Individual,Individual]:
280+
def crossover(parent1: Individual, parent2: Individual, T: List[str]) -> Tuple[Individual, Individual]:
281281
# @src 6.3. Genetic Operations: Crossover; https://doi.org/10.1007/11494744_5
282282
# 1. cross-over point
283283
t = random.choice(T)
@@ -344,7 +344,7 @@ def mutate(individual: Individual, rate: float = 0.01) -> Individual:
344344
O[t] = rand_partition(itertools.chain(*O[t]))
345345
return (I,O)
346346

347-
def repair(I: InputMap, O: OutputMap, C: numpy.ndarray, T: list[str]) -> Individual:
347+
def repair(I: InputMap, O: OutputMap, C: numpy.ndarray, T: List[str]) -> Individual:
348348
"""
349349
Merging partitions until petri-net is (coherent) workflow net
350350
see last requirement in 4. Causal Matrix, Def. 4; https://doi.org/10.1007/11494744_5

pm4py/objects/genetic_matrix/obj.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
class GeneticMatrix:
2727
def __init__(
2828
self,
29-
input_map: dict[str,list[frozenset]],
30-
output_map: dict[str,list[frozenset]],
31-
transitions: list[str],
29+
input_map: Dict[str, List[FrozenSet[str]]],
30+
output_map: Dict[str, List[FrozenSet[str]]],
31+
transitions: List[str],
3232
):
3333
"""
3434
Initialize a Genetic matrix

0 commit comments

Comments
 (0)