def test_ultrametrize_with_empty_levels(self): # Ultrametrize and set to 2 levels already ultrametric from tree below # 0 1 2 3 # \/ d=1 \/ d=3 # 01 23 # \ / d=8 # 0123 src_matr = [ [0, 1, 8, 8], [1, 0, 8, 8], [8, 8, 0, 3], [8, 8, 3, 0], ] expected_matr = src_matr res = get_ultra_metric(src_matr, UltraMetricParams(max_level=8)) self.assertEqual(expected_matr, res)
def test_ultrametrize_non_ultrametric(self): # get matrix from sample 1 (test_ultrametrize_already_ultrametric) # but some distances aren't ultrametric src_matr = [ [0, 1, 7, 8], # here the difference: 7 [1, 0, 8, 8], [7, 8, 0, 3], # here the difference: 7 [8, 8, 3, 0], ] expected_matr = [ [0, 1, 2, 2], [1, 0, 2, 2], [2, 2, 0, 1], [2, 2, 1, 0], ] res = get_ultra_metric(src_matr, UltraMetricParams(max_level=2)) self.assertEqual(expected_matr, res)
def test_ultrametrize_to_top_level(self): # Ultrametrize and set to 2 levels already ultrametric from tree below # 0 1 2 3 # \/ d=1 \/ d=4.6 (here the difference, d=4.6, it will be 2nd level) # 01 23 # \ / d=8 # 0123 src_matr = [ [0, 1, 8, 8], [1, 0, 8, 8], [8, 8, 0, 4.6], # here the difference - 4.6 [8, 8, 4.6, 0], # here the difference - 4.6 ] expected_matr = [ [0, 1, 2, 2], [1, 0, 2, 2], [2, 2, 0, 2], [2, 2, 2, 0], ] res = get_ultra_metric(src_matr, UltraMetricParams(max_level=2)) self.assertEqual(expected_matr, res)
chain_length_weight=0.0, level_weight_multiplier=level_weight_multiplier, use_flipping=use_flipping) experiment_matrix = matrDiff.make_experiment_matrix( global_params) # corr = matrDiff.corrcoef(experiment_matrix=experiment_matrix) # name = f"param_a={param_a}_corr_{corr:0.2f}_{systematic_tree}_{cluster_algorithm}_subtree_(thr,mult)=({global_params.subtree_threshold},{global_params.subtree_multiplier})_lev_mult={global_params.level_weight_multiplier}" #print_matrix(experiment_matrix, name, matrDiff.names, corr, with_headers=True) #experiment_array = make_experiment_array(experiment_matrix) effective_cluster_algorithm = cluster_algorithm if cluster_algorithm == 'ultrametric': ultra_matrix = get_ultra_metric( to_full_matrix(experiment_matrix), UltraMetricParams(max_level=len(experiment_matrix))) experiment_matrix = ultra_matrix effective_cluster_algorithm = 'average' plot_matrix = to_full_matrix(experiment_matrix) # convert the redundant n*n square matrix form into a condensed nC2 array # dist_array[{n choose 2}-{n-i choose 2} + (j-i-1)] is the distance between points i and j dist_array = ssd.squareform(plot_matrix) #clustered_trees = hierarchy.linkage(np.asarray(experiment_array), cluster_algorithm) clustered_trees = hierarchy.linkage( dist_array, effective_cluster_algorithm) corr = corr_clustered_trees(clustered_trees, matrDiff.names, matrDiff.make_systematic_matrix())
"../../input/systematic_tree_morph.xtg", ["Angiosperms"], max_level=10) init_values = [0.5, 0.1, 0.0] x = init_values global_params = GlobalParams(max_level=10, param_a=x[0], g_weight=x[1], chain_length_weight=x[2]) experiment_matrix = matrDiff.make_full_experiment_matrix(global_params) print(experiment_matrix) for level_count in range(30): name = f"ultrametric_{x[0]}_{x[1]}_{x[2]}_{level_count + 1}" ultra_matrix = get_ultra_metric( experiment_matrix, UltraMetricParams(max_level=level_count + 1)) ultra_array = make_experiment_array(ultra_matrix) clustered_trees = hierarchy.linkage(np.asarray(ultra_array), 'average') corrcoef = matrDiff.corrcoef(ultra_matrix) corrcoef2 = common_corrcoef(experiment_matrix, ultra_matrix) print_matrix(ultra_matrix, name, matrDiff.names, corrcoef, with_headers=True) draw_plot(clustered_trees, matrDiff.names, name, f"../../output/ultra_metric/{name}.png")