<s> """<STR_LIT>""" <EOL> import unittest <EOL> from pyRMSD . condensedMatrix import CondensedMatrix <EOL> from pyproct . clustering . clustering import Clustering <EOL> from pyproct . clustering . cluster import Cluster <EOL> from pyproct . clustering . evaluation . metrics . test import matrix <EOL> from pyproct . clustering . evaluation . metrics . meanMinimumDistance import MeanMinimumDistanceCalculator , mean_function <EOL> from pyproct . clustering . algorithms . random import RandomAlgorithm <EOL> from pyproct . clustering . evaluation . metrics . cohesion import CohesionCalculator <EOL> from pyproct . clustering . evaluation . metrics . separation import SeparationCalculator <EOL> class TestMetrics ( unittest . TestCase ) : <EOL> def test_mean_function ( self ) : <EOL> self . assertEqual ( mean_function ( [ ] ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( mean_function ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> , <NUM_LIT:9> ] ) , <NUM_LIT:5> ) <EOL> def test_subsample ( self ) : <EOL> calculator = MeanMinimumDistanceCalculator ( <NUM_LIT:10> ) <EOL> self . assertEqual ( <NUM_LIT> , calculator . subsample ( <NUM_LIT:10> , <NUM_LIT:20> , [ <NUM_LIT:4> , <NUM_LIT:6> , <NUM_LIT:3> , <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT:12> , <NUM_LIT:9> , <NUM_LIT> , <NUM_LIT:20> , <NUM_LIT> ] ) ) <EOL> self . assertEqual ( <NUM_LIT> , calculator . subsample ( <NUM_LIT:10> , <NUM_LIT:5> , [ <NUM_LIT:4> , <NUM_LIT:6> , <NUM_LIT:3> , <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT:12> , <NUM_LIT:9> , <NUM_LIT> , <NUM_LIT:20> , <NUM_LIT> ] ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , calculator . subsample ( <NUM_LIT:10> , <NUM_LIT:20> , [ ] ) ) <EOL> def test_get_min_distances ( self ) : <EOL> distances = CondensedMatrix ( [ <NUM_LIT:1.> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> ] ) <EOL> clusters = [ Cluster ( None , elements = [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] ) , <EOL> Cluster ( None , elements = [ <NUM_LIT:3> , <NUM_LIT:4> ] ) ] <EOL> calculator = MeanMinimumDistanceCalculator ( <NUM_LIT:10> ) <EOL> min_dists , mean = calculator . get_mean_and_min_distances ( clusters [ <NUM_LIT:0> ] , clusters [ <NUM_LIT:1> ] , distances ) <EOL> self . assertItemsEqual ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , min_dists ) <EOL> self . assertAlmostEquals ( <NUM_LIT> , mean , <NUM_LIT:2> ) <EOL> def test_get_distances_less_than_mean ( self ) : <EOL> calculator = MeanMinimumDistanceCalculator ( <NUM_LIT:10> ) <EOL> self . assertItemsEqual ( range ( <NUM_LIT> ) , calculator . get_distances_less_than_mean ( range ( <NUM_LIT:100> ) , <NUM_LIT:50> ) ) <EOL> def test_subsampled_mean_min_dist ( self ) : <EOL> calculator = MeanMinimumDistanceCalculator ( <NUM_LIT:10> ) <EOL> clusters = [ Cluster ( None , elements = [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] ) , <EOL> Cluster ( None , elements = [ <NUM_LIT:3> , <NUM_LIT:4> ] ) ] <EOL> triangle = [ <NUM_LIT:1.> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> ] <EOL> distances = CondensedMatrix ( triangle ) <EOL> self . assertEqual ( ( <NUM_LIT> , <NUM_LIT> ) , calculator . subsampled_mean_min_dist ( clusters [ <NUM_LIT:0> ] , clusters [ <NUM_LIT:1> ] , <NUM_LIT:20> , distances ) ) <EOL> def test_mini_evaluation ( self ) : <EOL> calculator = MeanMinimumDistanceCalculator ( <NUM_LIT:10> ) <EOL> clusters = [ Cluster ( None , elements = [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] ) , <EOL> Cluster ( None , elements = [ <NUM_LIT:3> , <NUM_LIT:4> ] ) ] <EOL> triangle = [ <NUM_LIT:1.> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> ] <EOL> distances = CondensedMatrix ( triangle ) <EOL> clustering = Clustering ( clusters ) <EOL> self . assertEqual ( <NUM_LIT> , calculator . evaluate ( clustering , distances , <NUM_LIT:20> ) ) <EOL> def test_full_run ( self ) : <EOL> condensed_matrix = CondensedMatrix ( matrix ) <EOL> cmax = condensed_matrix . calculateMax ( ) <EOL> alg = RandomAlgorithm . RandomClusteringAlgorithm ( condensed_matrix ) <EOL> values = [ ] <EOL> calculator = MeanMinimumDistanceCalculator ( <NUM_LIT:10> ) <EOL> for i in range ( <NUM_LIT:2> , <NUM_LIT:20> ) : <EOL> clustering = alg . perform_clustering ( { <EOL> "<STR_LIT>" : - <NUM_LIT:1> , <EOL> "<STR_LIT>" : i <EOL> } ) <EOL> values . append ( calculator . evaluate ( clustering , condensed_matrix , <NUM_LIT:30> ) ) <EOL> self . assertTrue ( max ( values ) < cmax ) <EOL> def test_cluster_cohesion_without_prototype ( self ) : <EOL> distances = CondensedMatrix ( [ <NUM_LIT:1.> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> ] ) <EOL> clusters_1 = [ Cluster ( None , elements = [ <NUM_LIT:0> , <NUM_LIT:1> ] ) , <EOL> Cluster ( None , elements = [ <NUM_LIT:2> ] ) , <EOL> Cluster ( None , elements = [ <NUM_LIT:3> , <NUM_LIT:4> ] ) ] <EOL> clusters_2 = [ Cluster ( None , elements = [ <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:4> ] ) , <EOL> Cluster ( None , elements = [ <NUM_LIT:1> , <NUM_LIT:3> ] ) ] <EOL> cohesion_calctor = CohesionCalculator ( ) <EOL> self . assertEqual ( cohesion_calctor . evaluate_cluster ( clusters_1 [ <NUM_LIT:0> ] , distances ) , <NUM_LIT:0.5> ) <EOL> self . assertEqual ( cohesion_calctor . evaluate_cluster ( clusters_1 [ <NUM_LIT:1> ] , distances ) , <NUM_LIT:0.> ) <EOL> self . assertEqual ( cohesion_calctor . evaluate_cluster ( clusters_1 [ <NUM_LIT:2> ] , distances ) , <NUM_LIT> ) <EOL> self . assertEqual ( cohesion_calctor . evaluate_cluster ( clusters_2 [ <NUM_LIT:0> ] , distances ) , <NUM_LIT> ) <EOL> self . assertEqual ( cohesion_calctor . evaluate_cluster ( clusters_2 [ <NUM_LIT:1> ] , distances ) , <NUM_LIT> ) <EOL> def test_cluster_mixed_cohesion_wo_prot ( self ) : <EOL> distances = CondensedMatrix ( [ <NUM_LIT:1.> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> ] ) <EOL> clusters_1 = [ Cluster ( None , elements = [ <NUM_LIT:0> , <NUM_LIT:1> ] ) , <EOL> Cluster ( None , elements = [ <NUM_LIT:2> ] ) , <EOL> Cluster ( None , elements = [ <NUM_LIT:3> , <NUM_LIT:4> ] ) ] <EOL> clusters_2 = [ Cluster ( None , elements = [ <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:4> ] ) , <EOL> Cluster ( None , elements = [ <NUM_LIT:1> , <NUM_LIT:3> ] ) ] <EOL> sep_calctor = SeparationCalculator ( ) <EOL> self . assertEqual ( sep_calctor . _SeparationCalculator__between_cluster_distance ( clusters_1 [ <NUM_LIT:0> ] , clusters_1 [ <NUM_LIT:1> ] , distances ) , <NUM_LIT> ) <EOL> self . assertEqual ( sep_calctor . _SeparationCalculator__between_cluster_distance ( clusters_1 [ <NUM_LIT:0> ] , clusters_1 [ <NUM_LIT:2> ] , distances ) , <NUM_LIT> ) <EOL> self . assertEqual ( sep_calctor . _SeparationCalculator__between_cluster_distance ( clusters_1 [ <NUM_LIT:1> ] , clusters_1 [ <NUM_LIT:2> ] , distances ) , <NUM_LIT> ) <EOL> self . assertEqual ( sep_calctor . _SeparationCalculator__between_cluster_distance ( clusters_2 [ <NUM_LIT:0> ] , clusters_2 [ <NUM_LIT:1> ] , distances ) , <NUM_LIT> ) <EOL> def test_cluster_cohe_sep_wo_prot_eval ( self ) : <EOL> distances = CondensedMatrix ( [ <NUM_LIT:1.> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> ] ) <EOL> clusters_1 = [ Cluster ( None , elements = [ <NUM_LIT:0> , <NUM_LIT:1> ] ) , <EOL> Cluster ( None , elements = [ <NUM_LIT:2> ] ) , <EOL> Cluster ( None , elements = [ <NUM_LIT:3> , <NUM_LIT:4> ] ) ] <EOL> clusters_2 = [ Cluster ( None , elements = [ <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:4> ] ) , <EOL> Cluster ( None , elements = [ <NUM_LIT:1> , <NUM_LIT:3> ] ) ] <EOL> clusterization_1 = Clustering ( clusters_1 ) <EOL> clusterization_2 = Clustering ( clusters_2 ) <EOL> sep_calctor = SeparationCalculator ( ) <EOL> self . assertEqual ( sep_calctor . cluster_separation ( clusters_1 [ <NUM_LIT:0> ] , clusterization_1 , <NUM_LIT:1.> , distances ) , <NUM_LIT> ) <EOL> self . assertEqual ( sep_calctor . cluster_separation ( clusters_1 [ <NUM_LIT:1> ] , clusterization_1 , <NUM_LIT:1.> , distances ) , <NUM_LIT> ) <EOL> self . assertEqual ( sep_calctor . cluster_separation ( clusters_1 [ <NUM_LIT:2> ] , clusterization_1 , <NUM_LIT:1.> , distances ) , <NUM_LIT> ) <EOL> self . assertEqual ( sep_calctor . cluster_separation ( clusters_2 [ <NUM_LIT:0> ] , clusterization_2 , <NUM_LIT:1.> , distances ) , <NUM_LIT> ) <EOL> self . assertEqual ( sep_calctor . cluster_separation ( clusters_2 [ <NUM_LIT:1> ] , clusterization_2 , <NUM_LIT:1.> , distances ) , <NUM_LIT> ) <EOL> def test_regression_separation_eval ( self ) : <EOL> distances = CondensedMatrix ( [ <NUM_LIT:1.> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> ] ) <EOL> clusters = [ Cluster ( None , elements = [ <NUM_LIT:0> , <NUM_LIT:1> ] ) , <EOL> Cluster ( None , elements = [ <NUM_LIT:2> ] ) , <EOL> Cluster ( None , elements = [ <NUM_LIT:3> , <NUM_LIT:4> ] ) ] <EOL> clustering = Clustering ( clusters ) <EOL> sep_calctor = SeparationCalculator ( ) <EOL> self . assertEqual ( sep_calctor . evaluate ( clustering , distances , [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ) , <NUM_LIT> + <NUM_LIT> + <NUM_LIT> ) <EOL> self . assertEqual ( sep_calctor . evaluate ( clustering , distances ) , ( <NUM_LIT:1> / <NUM_LIT:0.5> ) * <NUM_LIT> + ( <NUM_LIT:1> / <NUM_LIT> ) * <NUM_LIT> ) <EOL> def test_regression_cohesion_eval ( self ) : <EOL> distances = CondensedMatrix ( [ <NUM_LIT:1.> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> ] ) <EOL> clusters = [ Cluster ( None , elements = [ <NUM_LIT:0> , <NUM_LIT:1> ] ) , <EOL> Cluster ( None , elements = [ <NUM_LIT:2> ] ) , <EOL> Cluster ( None , elements = [ <NUM_LIT:3> , <NUM_LIT:4> ] ) ] <EOL> clustering = Clustering ( clusters ) <EOL> cohesion_calctor = CohesionCalculator ( ) <EOL> self . assertEqual ( cohesion_calctor . evaluate ( clustering , distances ) , <NUM_LIT> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
