diff --git a/solventmapcreator/clusteringanalysis/clusteranalysis.py b/solventmapcreator/clusteringanalysis/clusteranalysis.py index 587e3ea5f4278bdf1f08bc508a23932586f3338a..b2078845cd59d85212aabce3f2e2a19371f37802 100644 --- a/solventmapcreator/clusteringanalysis/clusteranalysis.py +++ b/solventmapcreator/clusteringanalysis/clusteranalysis.py @@ -22,7 +22,7 @@ def create_dend_and_stats_all_methods(condensed_matrix, labels, filename_stem, * """This generates the linkage matrices for each method, and returns the dendrogram and linkage file and statistics for each method. """ - linkage_matrix_dict = clustering_by_all_methods(condensed_distance_matrix) + linkage_matrix_dict = clustering_by_all_methods(condensed_matrix) output_dict= {} for method_label, linkage_matrix in linkage_matrix_dict.keys(): method_filename_stem= filename_stem + '_' + method_label @@ -79,7 +79,7 @@ def generate_cophenetic_filename(filename_stem): """This generates the cophenetic and inconsistency filenamefrom the given stem. """ - return filename_stem + '_coph_inconst.csv + return filename_stem + '_coph_inconst.csv' def generate_linkage_filename(filename_stem): """This generates the filename for the linkage file. diff --git a/solventmapcreator/test/clusteringanalysistest/clusteranalysistest.py b/solventmapcreator/test/clusteringanalysistest/clusteranalysistest.py new file mode 100644 index 0000000000000000000000000000000000000000..9e2e3d0af7b0ce84e5e42a7d0c15359c50a66ff1 --- /dev/null +++ b/solventmapcreator/test/clusteringanalysistest/clusteranalysistest.py @@ -0,0 +1,183 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Script containing tests for methods in the clusteranalysis script that do not plot. + +@author: mark +""" + +import logging +import numpy as np +import os +import unittest +import solventmapcreator.clusteringanalysis.clusteranalysis as clusteranalysis + + +logging.basicConfig() +LOGGER = logging.getLogger(__name__) +LOGGER.setLevel(logging.WARN) + +class ClusterAnalysisTestCase(unittest.TestCase): + """Test Case for clusteranlysis script. + """ + def setUp(self): + """Set up before tests. + """ + self.example_distance_matrix = np.array([17, 21, 31, 23, 30, 34, 21, 28, 39, 43]) + self.expected_linkage_matrix_dict = {"ward_clustering":np.array([[0., 1., 17., 2.], + [4., 5., 23.459184, 3.], + [2., 3., 28., 2.], + [6., 7., 43.875582, 5.]]), + "wpgmc_clustering":np.array([[0., 1., 17., 2.], + [4., 5., 20.31625, 3.], + [2., 3., 28., 2.], + [6., 7., 30.650245, 5.]]), + "upgmc_clustering":np.array([[0., 1., 17., 2.], + [4., 5., 20.31625, 3.], + [2., 3., 28., 2.], + [6., 7., 28.321566, 5.]]), + "wpgma_clustering":np.array([[0., 1., 17., 2.], + [4., 5., 22., 3.], + [2., 3., 28., 2.], + [6., 7., 35., 5.]]), + "upgma_clustering":np.array([[0., 1., 17., 2.], + [4., 5., 22., 3.], + [2., 3., 28., 2.], + [6., 7., 33., 5.]]), + "max_clustering": np.array([[0., 1., 17., 2.], + [4., 5., 23., 3.], + [2., 3., 28., 2.], + [6., 7., 43., 5.]]), + "min_clustering":np.array([[0., 1., 17., 2.], + [2., 5., 21., 3.], + [4., 6., 21., 4.], + [3., 7., 28., 5.]])} + self.expected_filename = "resources/expectedlinkagematrix.csv" + self.inconsistency_matrix = np.array([[17., 0., 1., 0.], + [19.5, 3.535534, 2., 0.707107], + [28., 0., 1., 0.], + [27.666667,5.507571, 3., 0.968364]]) + self.cophenetic_coefficient = 0.73020316123580109 + self.cophenetic_distance_matrix = np.array([17., 33., 33., 22., 33., + 33., 22., 28., 33., 33.]) + self.expected_cop_filename = "resources/expectedcopfile.csv" + def tearDown(self): + """Clean up after tests. + """ + del self.example_distance_matrix + del self.expected_linkage_matrix_dict + del self.expected_filename + del self.inconsistency_matrix + del self.cophenetic_coefficient + del self.cophenetic_distance_matrix + del self.expected_cop_filename + if os.path.isfile("linkagematrix.csv"): + os.remove("linkagematrix.csv") + if os.path.isfile("coph_incons.csv"): + os.remove("coph_incons.csv") + if os.path.isfile("coph_incons2.csv"): + os.remove("coph_incons2.csv") + def test_generate_cophenetic_and_inconsistency_file(self): + """Test to see if expected file is produced. + """ + actual_filename = "coph_incons2.csv" + coph_out = clusteranalysis.generate_cophenetic_and_inconsistency_file(self.expected_linkage_matrix_dict["upgma_clustering"], + self.example_distance_matrix, + actual_filename) + with open("resources/expectedcopfile2.csv", 'r') as exp_file: + exp_file_lines = exp_file.readlines() + with open(actual_filename, 'r') as act_file: + act_file_lines = act_file.readlines() + self.assertListEqual(act_file_lines, exp_file_lines) + def test_generate_cophenetic_filename(self): + """Test to see if expected filename is produced + """ + expected_filename = "example_coph_inconst.csv" + actual_filename = clusteranalysis.generate_cophenetic_filename("example") + self.assertEqual(expected_filename, actual_filename) + def test_generate_linkage_filename(self): + """Test to see if expected filename is produced + """ + expected_filename = "example.csv" + actual_filename = clusteranalysis.generate_linkage_filename("example") + self.assertEqual(expected_filename, actual_filename) + def test_write_linkage_matrix(self): + """Test to see if expected file is produced. + """ + actual_filename = "linkagematrix.csv" + link_mat_out = clusteranalysis.write_linkage_matrix(self.expected_linkage_matrix_dict["upgma_clustering"], + actual_filename) + self.assertEqual(0, link_mat_out) + with open(self.expected_filename, 'r') as exp_file: + exp_file_lines = exp_file.readlines() + with open(actual_filename, 'r') as act_file: + act_file_lines = act_file.readlines() + self.assertListEqual(act_file_lines, exp_file_lines) + def test_write_cophenetic_and_inconsistency(self): + """Test to see if expected file is produced. + """ + actual_filename = "coph_incons.csv" + coph_out = clusteranalysis.write_cophenetic_and_inconsistency(self.cophenetic_coefficient, + self.cophenetic_distance_matrix, + self.inconsistency_matrix, + actual_filename) + with open(self.expected_cop_filename, 'r') as exp_file: + exp_file_lines = exp_file.readlines() + with open(actual_filename, 'r') as act_file: + act_file_lines = act_file.readlines() + self.assertListEqual(act_file_lines, exp_file_lines) + def test_calculate_cophenetic_and_inconsistency(self): + """Test to see if expected dictionary is produced. + """ + expected_dict = {"cophenetic_coeff":0.73020316123580109, + "cophenetic_distances":np.array([17., 33., 33., 22., + 33., 33., + 22., 28., 33., 33.]), + "inconsistency_matrix":np.array([[17., 0., 1., 0.], + [19.5, 3.535534, + 2., 0.707107], + [28., 0., 1., 0.], + [27.666667,5.507571, + 3., 0.968364]])} + actual_dict = clusteranalysis.calculate_cophenetic_and_inconsistency(self.expected_linkage_matrix_dict["upgma_clustering"], + self.example_distance_matrix) + self.assertListEqual(sorted(expected_dict.keys()), sorted(actual_dict.keys())) + for key in expected_dict.keys(): + if key == "cophenetic_coeff": + self.assertAlmostEqual(expected_dict[key], actual_dict[key]) + else: + np.testing.assert_array_almost_equal(expected_dict[key], actual_dict[key]) + def test_calculate_inconsistency(self): + """Test to see if expected inconsistency matrix is produced. + """ + expected_matrix = np.array([[17., 0., 1., 0.], + [19.5, 3.535534, 2., 0.707107], + [28., 0., 1., 0.], + [27.666667,5.507571, 3., 0.968364]]) + actual_matrix = clusteranalysis.calculate_inconsistency(self.expected_linkage_matrix_dict["upgma_clustering"]) + np.testing.assert_array_almost_equal(expected_matrix, actual_matrix) + def test_calculate_cophenetic_distance(self): + """Test to see if expected cophenetic coefficientand distances are produced. + """ + expected_distance = 0.73020316123580109 + expected_matrix = np.array([17., 33., 33., 22., 33., 33., 22., 28., + 33., 33.]) + actual_distance, actual_matrix = clusteranalysis.calculate_cophenetic_distance(self.expected_linkage_matrix_dict["upgma_clustering"], + self.example_distance_matrix) + self.assertAlmostEqual(expected_distance, actual_distance) + np.testing.assert_array_almost_equal(expected_matrix, actual_matrix) + def test_clustering_given_method(self): + """Test to see if expected matrix is produced. + """ + actual_matrix = clusteranalysis.clustering_given_method(self.example_distance_matrix,"average") + np.testing.assert_array_almost_equal(self.expected_linkage_matrix_dict["upgma_clustering"], + actual_matrix) + def test_clustering_by_all_methods(self): + """Test to see if expected dict is produced. + """ + actual_dict = clusteranalysis.clustering_by_all_methods(self.example_distance_matrix) + self.assertListEqual(sorted(self.expected_linkage_matrix_dict.keys()), + sorted(actual_dict.keys())) + for key in self.expected_linkage_matrix_dict.keys(): + np.testing.assert_array_almost_equal(self.expected_linkage_matrix_dict[key], + actual_dict[key]) diff --git a/solventmapcreator/test/resources/expectedcopfile2.csv b/solventmapcreator/test/resources/expectedcopfile2.csv new file mode 100644 index 0000000000000000000000000000000000000000..5184b87b2ad7dab69790c26919bd51f79195725d --- /dev/null +++ b/solventmapcreator/test/resources/expectedcopfile2.csv @@ -0,0 +1,12 @@ +Cophenetic coefficient 0.7302032 +Cophenetic Distance Matrix +0.0000000 17.0000000 33.0000000 33.0000000 22.0000000 +17.0000000 0.0000000 33.0000000 33.0000000 22.0000000 +33.0000000 33.0000000 0.0000000 28.0000000 33.0000000 +33.0000000 33.0000000 28.0000000 0.0000000 33.0000000 +22.0000000 22.0000000 33.0000000 33.0000000 0.0000000 +Mean Heights of links SD for links Number of links Inconsistency coefficient +17.0000000 0.0000000 1.0 0.0000000 +19.5000000 3.5355339 2.0 0.7071068 +28.0000000 0.0000000 1.0 0.0000000 +27.6666667 5.5075705 3.0 0.9683641 diff --git a/solventmapcreator/test/solvationmapcreatortests.py b/solventmapcreator/test/solvationmapcreatortests.py index b6b59921a8bb6153a44ac74a076fbf0d1200acb8..8f0c8a91a0389ef343d8fb6e4d2396090f3b6df3 100644 --- a/solventmapcreator/test/solvationmapcreatortests.py +++ b/solventmapcreator/test/solvationmapcreatortests.py @@ -25,7 +25,7 @@ from solventmapcreator.test.polynomialanalysistest.polynomialplottingtest import from solventmapcreator.test.clusteringanalysistest.matrixinputtest import MatrixInputTestCase from solventmapcreator.test.clusteringanalysistest.clustercalculationtest import ClusterCalculationTestCase from solventmapcreator.test.clusteringanalysistest.clusterstatisticstest import ClusterStatisticsTestCase - +from solventmapcreator.test.clusteringanalysistest.clusteranalysistest import ClusterAnalysisTestCase logging.basicConfig() LOGGER = logging.getLogger(__name__) LOGGER.setLevel(logging.WARN) @@ -45,7 +45,7 @@ POLYNOMIAL_ANALYSIS_TEST_CASES = [PolynomialDataAnalysisTestCase, PolynomialPlottingTestCase] CLUSTERING_ANALYSIS_TEST_CASES = [MatrixInputTestCase, ClusterCalculationTestCase, - ClusterStatisticsTestCase] + ClusterStatisticsTestCase, ClusterAnalysisTestCase] def test_suite(): """Function creates a test suite and then loads all the tests from the