GitLab Repo

amachine.am_create.am_isomorphic_to_with_category_permutations

 1from ..am_hmm          import HMM
 2from ..am_structured_symbol_set import StructuredSymbolSet
 3
 4import warnings
 5
 6def isomorphic_to_with_category_permutations(
 7    m : HMM,
 8    symbol_set : StructuredSymbolSet, 
 9    permutations : dict[ int, dict[str,str] ],
10    decorator : str ) -> HMM :
11    
12    alphabet = m.alphabet
13
14    states = [ 
15        s.modified_deep_copy( name=f"{s.name}{decorator}" )
16        for s in m.states
17    ]
18
19    category_symbols = symbol_set.category_symbols()
20
21    transitions = []
22    for tr in m.transitions :
23        
24        s = alphabet[ tr.symbol_idx ]
25        c = symbol_set.symbol_categories[ s ]
26        
27        if c in permutations and s in permutations[c] :
28
29            new_s = permutations[c ][ s ]
30            new_symbol_idx = m.symbol_idx_map[ new_s ]
31
32            # same transition but with different symbol
33            transitions.append(
34                tr.modified_deep_copy( symbol_idx=new_symbol_idx )
35            )
36
37        else :
38            warnings.warn( f"Symbol {s} w with category {c} missing in permutations." )
39            transitions.append( tr.deepcopy() )
40
41    res = HMM(
42        states=states,
43        transitions=transitions,
44        start_state=0,
45        alphabet=alphabet
46    )
47
48    if not res.is_row_stochastic() :
49        raise Exception( "Category permutation is not row stochastic" )
50
51    if not  res.is_unifilar() :
52        raise Exception( "Category permutation is not unifilar" )
53
54    return res
def isomorphic_to_with_category_permutations( m: amachine.am_hmm.HMM, symbol_set: amachine.am_structured_symbol_set.StructuredSymbolSet, permutations: dict[int, dict[str, str]], decorator: str) -> amachine.am_hmm.HMM:
 7def isomorphic_to_with_category_permutations(
 8    m : HMM,
 9    symbol_set : StructuredSymbolSet, 
10    permutations : dict[ int, dict[str,str] ],
11    decorator : str ) -> HMM :
12    
13    alphabet = m.alphabet
14
15    states = [ 
16        s.modified_deep_copy( name=f"{s.name}{decorator}" )
17        for s in m.states
18    ]
19
20    category_symbols = symbol_set.category_symbols()
21
22    transitions = []
23    for tr in m.transitions :
24        
25        s = alphabet[ tr.symbol_idx ]
26        c = symbol_set.symbol_categories[ s ]
27        
28        if c in permutations and s in permutations[c] :
29
30            new_s = permutations[c ][ s ]
31            new_symbol_idx = m.symbol_idx_map[ new_s ]
32
33            # same transition but with different symbol
34            transitions.append(
35                tr.modified_deep_copy( symbol_idx=new_symbol_idx )
36            )
37
38        else :
39            warnings.warn( f"Symbol {s} w with category {c} missing in permutations." )
40            transitions.append( tr.deepcopy() )
41
42    res = HMM(
43        states=states,
44        transitions=transitions,
45        start_state=0,
46        alphabet=alphabet
47    )
48
49    if not res.is_row_stochastic() :
50        raise Exception( "Category permutation is not row stochastic" )
51
52    if not  res.is_unifilar() :
53        raise Exception( "Category permutation is not unifilar" )
54
55    return res