def setup_hmm(rg, EM_params): G, node_map = hmmconf.rg_to_nx_undirected(rg, map_nodes=True) n_states = len(node_map) # deal with initial marking and get start probability is_inv = lambda t: t.name is None startprob = hmmconf.compute_startprob(rg, node_map, n_states, is_inv) # add epsilon mass to all states startprob += 1e-5 hmmconf.utils.normalize(startprob, axis=1) # remove invisible transitions to_remove = list() for t in rg.transitions: if is_inv(t): to_remove.append(t) for t in to_remove: rg.transitions.remove(t) t.from_state.outgoing.remove(t) t.to_state.incoming.remove(t) dist_df = hmmconf.compute_distance_matrix(G, node_map, as_dataframe=True) distmat = dist_df.values # print('Distance df: \n{}'.format(dist_df)) obsmap = {t.name: int(t.name) for t in rg.transitions} int2state = {val:key for key, val in node_map.items()} int2obs = {val:key for key, val in obsmap.items()} n_obs = len(obsmap) logger.info('No. of states: {}'.format(n_states)) transcube = hmmconf.compute_state_trans_cube(rg, node_map, obsmap, n_obs, n_states) emitmat = hmmconf.compute_emission_mat(rg, node_map, obsmap, n_obs, n_states) confmat = hmmconf.compute_conformance_mat(emitmat) conform_f = hmmconf.conform hmm = hmmconf.HMMConf(conform_f, startprob, transcube, emitmat, confmat, distmat, int2state, int2obs, n_states, n_obs, params='to', verbose=True, n_jobs=EM_params[N_JOBS], tol=EM_params[TOL], n_iter=EM_params[N_ITER], random_seed=EM_params[RANDOM_SEED_PARAM]) return hmm
is_inv) sorted_states = sorted(list(rg.states), key=lambda s: (s.data['disc'], s.name)) node_map = { key: val for val, key in enumerate(map(lambda state: state.name, sorted_states)) } int2state = {val: key for key, val in node_map.items()} state2int = {val: key for key, val in int2state.items()} took_rg = time.time() - start_rg time_dict[TIME_BUILD_RG] = took_rg logger.info(f"Building reachability graph took: {took_rg:.3f}s") is_inv_rg = lambda t: t.name is None init = hmmconf.get_init_marking(rg) startprob = hmmconf.compute_startprob(rg, state2int, is_inv_rg) conf_obsmap = {i: i for i in obs2int.values()} confmat = hmmconf.compute_confmat(rg, init, is_inv_rg, state2int, conf_obsmap) logger.info("Estimating conformining parameters") start_conform = time.time() params = estimate_conform_params( filtered_event_df, state2int, obs2int, net_orig, init_marking_orig, final_marking_orig, is_inv, add_prior=EXPERIMENT_CONFIGS[ADD_PRIOR],