def test_file(seed, maximum_size, runs, file_extension): for _ in range(runs): size = _rd_randint(2, maximum_size) zeros = _rd_randint(0, size) mc_to = _MarkovChain.random(size, zeros=zeros, seed=seed) file_handler, file_path = _tf_mkstemp(suffix=file_extension) _os_close(file_handler) # noinspection PyBroadException try: mc_to.to_file(file_path) mc_from = _MarkovChain.from_file(file_path) exception = False except Exception: mc_from = None exception = True _os_remove(file_path) assert exception is False _npt.assert_allclose(mc_from.p, mc_to.p, rtol=1e-5, atol=1e-8)
def test_dictionary(seed, maximum_size, runs): for _ in range(runs): size = _rd_randint(2, maximum_size) zeros = _rd_randint(0, size) mc_to = _MarkovChain.random(size, zeros=zeros, seed=seed) d = mc_to.to_dictionary() mc_from = _MarkovChain.from_dictionary(d) _npt.assert_allclose(mc_from.p, mc_to.p, rtol=1e-5, atol=1e-8)
def generate_mc(self, states, window, T): # set transition p = np.array([[0.2, 0.8], [0.6, 0.4]]) mc = MarkovChain(p, states) # set of transions transitions = mc.walk(int(T / window)) data = [] for i in range(len(transitions)): for c in self.generate_zipf(float(transitions[i]), window): data.append(c) return data
def generate_mc(self, states, window, T, available=False): # set transition p = np.array([[0.4, 0.6], [0.75, 0.25]]) mc = MarkovChain(p, states) # set of transions transitions = mc.walk(int(T / window)) data = []; for transition in range(len(transitions)): for c in self.generate_zipf(float(transitions[transition]), window, transition=transition, available=available): data.append(c) return data
def test_matrix(seed, maximum_size, runs): _npr.seed(seed) for _ in range(runs): size = _rd_randint(2, maximum_size) m = _npr.randint(101, size=(size, size)) mc1 = _MarkovChain.from_matrix(m) m = mc1.to_matrix() mc2 = _MarkovChain.from_matrix(m) _npt.assert_allclose(mc1.p, mc2.p, rtol=1e-5, atol=1e-8)
def test_identity(size, value): mc = _MarkovChain.identity(size) actual = mc.p expected = _np.asarray(value) _npt.assert_allclose(actual, expected, rtol=1e-5, atol=1e-8)
def test_graph(seed, maximum_size, runs): for _ in range(runs): size = _rd_randint(2, maximum_size) zeros = _rd_randint(0, size) mc_to = _MarkovChain.random(size, zeros=zeros, seed=seed) graph = mc_to.to_graph(False) mc_from = _MarkovChain.from_graph(graph) _npt.assert_allclose(mc_from.p, mc_to.p, rtol=1e-5, atol=1e-8) graph = mc_to.to_graph(True) mc_from = _MarkovChain.from_graph(graph) _npt.assert_allclose(mc_from.p, mc_to.p, rtol=1e-5, atol=1e-8)
def test_birth_death(p, q, value): mc = _MarkovChain.birth_death(p, q) actual = mc.p expected = _np.asarray(value) _npt.assert_allclose(actual, expected, rtol=1e-5, atol=1e-8)
def test_urn_model(n, model, value): mc = _MarkovChain.urn_model(n, model) actual = mc.p expected = _np.asarray(value) _npt.assert_allclose(actual, expected, rtol=1e-5, atol=1e-8)
def test_fit_walk(fitting_type, possible_states, walk, k, value): mc = _MarkovChain.fit_walk(fitting_type, possible_states, walk, k) actual = mc.p expected = _np.asarray(value) _npt.assert_allclose(actual, expected, rtol=1e-5, atol=1e-8)
def test_gamblers_ruin(size, w, value): mc = _MarkovChain.gamblers_ruin(size, w) actual = mc.p expected = _np.asarray(value) _npt.assert_allclose(actual, expected, rtol=1e-5, atol=1e-8)
def test_approximation(size, approximation_type, alpha, sigma, rho, k, value): mc = _MarkovChain.approximation(size, approximation_type, alpha, sigma, rho, k) actual = mc.p expected = _np.asarray(value) _npt.assert_allclose(actual, expected, rtol=1e-5, atol=1e-8)
def test_plot_redistributions(seed, maximum_size, maximum_distributions, runs): rs = _rd_getstate() _rd_seed(seed) configs = [] for _ in range(runs): size = _rd_randint(2, maximum_size) zeros = _rd_randint(0, size) configs.append((size, zeros)) _rd_setstate(rs) mcs = [] plot_types = ['heatmap', 'projection'] for i in range(runs): size, zeros = configs[i] mc = _MarkovChain.random(size, zeros=zeros, seed=seed) if i == 0: distributions = mc.redistribute(1, output_last=False) initial_status = None plot_type = 'projection' else: r = _rd_randint(1, maximum_distributions) distributions = r if _rd_random() < 0.5 else mc.redistribute(r, output_last=False) initial_status = None if isinstance(distributions, int) or _rd_random() < 0.5 else distributions[0] plot_type = _rd_choice(plot_types) configs[i] = (distributions, initial_status, plot_type) mcs.append(mc) for i in range(runs): mc = mcs[i] distributions, initial_status, plot_type = configs[i] # noinspection PyBroadException try: figure, _ = _plot_redistributions(mc, distributions, initial_status, plot_type) _mplp.close(figure) exception = False except Exception: exception = True assert exception is False
def test_steady_states(p, steady_states): actual = MarkovChain(p).pi expected = [np.array(steady_state) for steady_state in steady_states] matches = 0 for a in actual: for e in expected: matches += 1 if np.allclose(a, e, rtol=0.0, atol=1e-6) else 0 assert matches == len(expected)
def run(final_result): print("Calculating frequency of occurance of packets...") #all unique transition pairs counter transition_counter = Counter() for i in range(len(final_result)): for j in range(len(final_result[i])): for k in range(len(final_result[i][j]) - 1): transition_counter[final_result[i][j][k] + "|||" + final_result[i][j][k + 1]] += 1 #total occurance of the first item in the transition pairs first_total = Counter() for i in transition_counter: first_total[i.split("|||")[0]] += transition_counter[i] #converting transition_counter to pure frequency in percentage final_frequency = defaultdict() for i in transition_counter: final_frequency[i] = transition_counter[i] / first_total[i.split("|||") [0]] #proceed to rule extraction rule_extraction(final_frequency) #list of all the nodes in first_total node_list = [] for i in first_total: node_list.append(i) # list of frequencies in matrixs format p = [[0] * len(node_list) for _ in range(len(node_list))] for i in node_list: for j in node_list: if i + "|||" + j in final_frequency: p[node_list.index(i)][node_list.index(j)] = final_frequency[ i + "|||" + j] ##node_list rename, save_correspondance to default_dict symbol_packet_pair = defaultdict() symbol = 0 print("Converting packet name to symbol...") for i in range(len(node_list)): symbol_packet_pair[str(symbol)] = node_list[i] node_list[i] = str(symbol) symbol = symbol + 1 print("Writing out symbol_packet_pair...") with open('extraction_symbol_packet_pair.txt', 'w') as f: for i in symbol_packet_pair: f.write(i + " : " + symbol_packet_pair[i] + "\n") print("Creating markov chain...") mc = MarkovChain(p, node_list) print("Plotting MC...") plt.figure.Figure = plot_graph(mc)[0] plt.subplots.figure = plot_graph(mc)[1] plt.savefig("figure.png")
def run(final_result): transition_counter = Counter() for i in range(len(final_result)): for j in range(len(final_result[i])): for k in range(len(final_result[i][j])-1): transition_counter[final_result[i][j][k]+"|||"+final_result[i][j][k+1]]+=1 first_total = Counter() for i in transition_counter: first_total[i.split("|||")[0]]+=transition_counter[i] final_frequency = defaultdict() for i in transition_counter: final_frequency[i] = transition_counter[i]/first_total[i.split("|||")[0]] #list of all the nodes node_list = [] for i in first_total: node_list.append(i) # list of frequencies p = [ [0]*len(node_list) for _ in range(len(node_list))] for i in node_list: for j in node_list: if i+"|||"+j in final_frequency: p[node_list.index(i)][node_list.index(j)]=final_frequency[i+"|||"+j] ##node_list rename, save_correspondance to default_dict symbol_packet_pair = defaultdict() symbol = 0 print("Converting packet name to symbol...") for i in range(len(node_list)): symbol_packet_pair[str(symbol)]=node_list[i] node_list[i]=str(symbol) symbol = symbol+1 print("Writing out symbol_packet_pair...") with open ('extraction_symbol_packet_pair.txt', 'w') as f: for i in symbol_packet_pair: f.write(i+" : "+symbol_packet_pair[i]+"\n") mc = MarkovChain(p, node_list) print(mc) print(mc.transition_probability('17','22'))
def test_fit_function(possible_states, f, quadrature_type, quadrature_interval, value): f = eval('lambda x_index, x_value, y_index, y_value: ' + f) quadrature_interval = None if quadrature_interval is None else tuple( quadrature_interval) mc = _MarkovChain.fit_function(possible_states, f, quadrature_type, quadrature_interval) actual = mc.p expected = _np.asarray(value) _npt.assert_allclose(actual, expected, rtol=1e-5, atol=1e-8)
def test_plot_walk(seed, maximum_size, maximum_simulations, runs): rs = _rd_getstate() _rd_seed(seed) configs = [] for _ in range(runs): size = _rd_randint(2, maximum_size) zeros = _rd_randint(0, size) configs.append((size, zeros)) _rd_setstate(rs) mcs = [] plot_types = ['histogram', 'sequence', 'transitions'] for i in range(runs): size, zeros = configs[i] mc = _MarkovChain.random(size, zeros=zeros, seed=seed) r = _rd_randint(2, maximum_simulations) walk = r if _rd_random() < 0.5 else mc.walk(r, output_indices=True) initial_state = None if isinstance(walk, int) or _rd_random() < 0.5 else walk[0] plot_type = _rd_choice(plot_types) configs[i] = (walk, initial_state, plot_type) mcs.append(mc) for i in range(runs): mc = mcs[i] walk, initial_state, plot_type = configs[i] # noinspection PyBroadException try: figure, _ = _plot_walk(mc, walk, initial_state, plot_type) _mplp.close(figure) exception = False except Exception: exception = True assert exception is False
def test_plot_eigenvalues(seed, maximum_size, runs): for _ in range(runs): size = _rd_randint(2, maximum_size) zeros = _rd_randint(0, size) mc = _MarkovChain.random(size, zeros=zeros, seed=seed) # noinspection PyBroadException try: figure, _ = _plot_eigenvalues(mc) _mplp.close(figure) exception = False except Exception: exception = True assert exception is False
def main(): for file in files: f = open(file, "r") lines = f.readlines() res = [line.split() for line in lines] cross_tab = transition_matrix(res) f.close() # print(file[:-4]) # print(cross_tab) mc = MarkovChain(cross_tab.values, cross_tab.index._data) r = open(file.replace("_trans", "_matrix"), "w") r.write(str(mc.p)) r.close() # matplotlib.interactive(True) figure, _ = plot_graph( mc, dpi=1000, path="/home/ja/Dokumenty/Pepper/data_analisys/" + file.replace("_trans.txt", "_graph.svg")) plt.close()
def test_plot_graph(seed, maximum_size, runs): rs = _rd_getstate() _rd_seed(seed) configs = [] for _ in range(runs): size = _rd_randint(2, maximum_size) zeros = _rd_randint(0, size) configs.append((size, zeros) + tuple(_rd_random() < 0.5 for _ in range(4))) _rd_setstate(rs) for i in range(runs): size, zeros, nodes_color, nodes_type, edges_color, edges_value = configs[i] mc = _MarkovChain.random(size, zeros=zeros, seed=seed) # noinspection PyBroadException try: figure, _ = _plot_graph(mc, nodes_color=nodes_color, nodes_type=nodes_type, edges_color=edges_color, edges_value=edges_value, force_standard=True) _mplp.close(figure) figure, _ = _plot_graph(mc, nodes_color=nodes_color, nodes_type=nodes_type, edges_color=edges_color, edges_value=edges_value, force_standard=False) _mplp.close(figure) exception = False except Exception: exception = True assert exception is False
def test_random(seed, size, zeros, mask, value): mc = _MarkovChain.random(size, None, zeros, mask, seed) actual = mc.p expected = _np.asarray(value) _npt.assert_allclose(actual, expected, rtol=1e-5, atol=1e-8) if zeros > 0 and mask is None: actual = size**2 - _np.count_nonzero(mc.p) expected = zeros assert actual == expected if mask is not None: indices = ~_np.isnan(_np.asarray(mask)) actual = mc.p[indices] expected = _np.asarray(value)[indices] _npt.assert_allclose(actual, expected, rtol=1e-5, atol=1e-8)
from pydtmc import (MarkovChain, plot_graph) from pytest import (mark) import matplotlib.pyplot as plt p = [[0.2, 0.7, 0.0, 0.1], [0.0, 0.6, 0.3, 0.1], [0.0, 0.0, 1.0, 0.0], [0.5, 0.0, 0.5, 0.0]] mc = MarkovChain(p, ['s1', 's2', 's3', 's4']) print(mc) plt.figure.Figure = plot_graph(mc)[0] plt.subplots.figure = plot_graph(mc)[1] #pp.axes._subplots.AxesSubplot = plot_graph(mc)[1] plt.savefig("figure.png") print(type(plot_graph(mc)[0])) print(type(plot_graph(mc)[1])) #s1/s2/s3/r1/r4/s2/s3/r4/r3/s2/s1/r3/r2/ #counter[s1/r2]: 90% #counter[s1se1se2/r2]: 80%
def test_periodicity(p, period): mc = MarkovChain(p) assert mc.period == period assert mc.is_aperiodic == (period == 1)