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_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_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 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_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_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 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_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)