diff --git a/solventmapcreator/clusteringanalysis/matrixinput.py b/solventmapcreator/clusteringanalysis/matrixinput.py new file mode 100644 index 0000000000000000000000000000000000000000..b252aefeb146a05eaac009b262c285621b1aede7 --- /dev/null +++ b/solventmapcreator/clusteringanalysis/matrixinput.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Script for preparing the Similarity Matrices for clustering. This contains +methods to reads in a matrix from a file, and return the labels as well as a +triangular matrix ready for clustering. + +@author: mark +""" + +import logging +import numpy as np +import solventmapcreator.io.similaritymatrixreader as similaritymatrixreader + +logging.basicConfig() +LOGGER = logging.getLogger(__name__) +LOGGER.setLevel(logging.WARN) + +def parse_similarity_matrix_file_trig_matrix(filename): + """This reads a file into a dictionary, then appends the trigonal dictionary. + """ + similarity_matrix_info = parse_similarity_matrix_file(filename) + append_trigonal_matrix(similarity_matrix_info) + return similarity_matrix_info + +def append_trigonal_matrix(similarity_matrix_info): + """This appends a triagonal matrix to the input dict. + """ + similarity_matrix_info["triagonal_matrix"] = convert_to_triangular_matrix(similarity_matrix_info["value_matrix"]) + +def convert_to_triangular_matrix(similarity_matrix): + """This converts the matrix to a triangular matrix. + """ + return np.triu(similarity_matrix) + +def parse_similarity_matrix_file(filename): + """This reads a file in to a dictionary, containing the labels and the + similarity matrix. + """ + return similaritymatrixreader.parse_similarity_matrix_file(filename) diff --git a/solventmapcreator/test/clusteringanalysistest/matrixinputtest.py b/solventmapcreator/test/clusteringanalysistest/matrixinputtest.py new file mode 100644 index 0000000000000000000000000000000000000000..84b879313c34c647ac801d6836caa43a15949172 --- /dev/null +++ b/solventmapcreator/test/clusteringanalysistest/matrixinputtest.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Script for testing the matrixinput script. + +@author: mark +""" + +import logging +import unittest +import numpy as np +import solventmapcreator.clusteringanalysis.matrixinput as matrixinput + +logging.basicConfig() +LOGGER = logging.getLogger(__name__) +LOGGER.setLevel(logging.WARN) + +class MatrixInputTestCase(unittest.TestCase): + """Test case for matrix input script + """ + def setUp(self): + """Set up before tests. + """ + self.input_filename = "resources/similarity_matrix_file.csv" + self.expected_dict = {"labels":["poly_fit", "poly_fit2", "poly_fit3"], + "value_matrix":np.array([[0.0, 1.0, 4.0], + [1.0, 0.0, 3.0], + [4.0, 3.0, 0.0]])} + def tearDown(self): + """Clean up after tests. + """ + del self.input_filename + del self.expected_dict + def test_parse_similarity_matrix_file_trig_matrix(self): + """Test to see if expected dictionary is produced. + """ + expected_dict = {"labels":["poly_fit", "poly_fit2", "poly_fit3"], + "value_matrix":np.array([[0.0, 1.0, 4.0], + [1.0, 0.0, 3.0], + [4.0, 3.0, 0.0]]), + "triagonal_matrix":np.array([[0.0, 1.0, 4.0], + [0.0, 0.0, 3.0], + [0.0, 0.0, 0.0]])} + actual_dict = matrixinput.parse_similarity_matrix_file_trig_matrix(self.input_filename) + self.assertListEqual(sorted(expected_dict.keys()), sorted(actual_dict.keys())) + self.assertListEqual(expected_dict["labels"], actual_dict["labels"]) + np.testing.assert_array_almost_equal(expected_dict["value_matrix"], + actual_dict["value_matrix"]) + np.testing.assert_array_almost_equal(expected_dict["triagonal_matrix"], + actual_dict["triagonal_matrix"]) + def test_append_trigonal_matrix(self): + """Test to see if matrix is appended as expected. + """ + expected_dict = {"labels":["poly_fit", "poly_fit2", "poly_fit3"], + "value_matrix":np.array([[0.0, 1.0, 4.0], + [1.0, 0.0, 3.0], + [4.0, 3.0, 0.0]]), + "triagonal_matrix":np.array([[0.0, 1.0, 4.0], + [0.0, 0.0, 3.0], + [0.0, 0.0, 0.0]])} + matrixinput.append_trigonal_matrix(self.expected_dict) + self.assertListEqual(sorted(self.expected_dict.keys()), sorted(expected_dict.keys())) + self.assertListEqual(self.expected_dict["labels"], expected_dict["labels"]) + np.testing.assert_array_almost_equal(self.expected_dict["value_matrix"], + expected_dict["value_matrix"]) + np.testing.assert_array_almost_equal(self.expected_dict["triagonal_matrix"], + expected_dict["triagonal_matrix"]) + def test_convert_to_triangular_matrix(self): + """Test to see if expected matrix is returned. + """ + expected_matrix = np.array([[0.0, 1.0, 4.0], + [0.0, 0.0, 3.0], + [0.0, 0.0, 0.0]]) + actual_matrix = matrixinput.convert_to_triangular_matrix(self.expected_dict["value_matrix"]) + np.testing.assert_array_almost_equal(expected_matrix, actual_matrix) + def test_parse_similarity_matrix_file(self): + """Test to see if expected matrix is read in. + """ + actual_dict = matrixinput.parse_similarity_matrix_file(self.input_filename) + self.assertListEqual(sorted(self.expected_dict.keys()), sorted(actual_dict.keys())) + self.assertListEqual(self.expected_dict["labels"], actual_dict["labels"]) + np.testing.assert_array_almost_equal(self.expected_dict["value_matrix"], + actual_dict["value_matrix"]) diff --git a/solventmapcreator/test/solvationmapcreatortests.py b/solventmapcreator/test/solvationmapcreatortests.py index 5f5c3258c8a67a59e19a94a1907b8f5bd72b51d4..4587d2ce6b7994c038988deb0e1197c7e677bcf0 100644 --- a/solventmapcreator/test/solvationmapcreatortests.py +++ b/solventmapcreator/test/solvationmapcreatortests.py @@ -21,6 +21,8 @@ from solventmapcreator.test.solvationcalculationtest.fractionaloccupancycalculat from solventmapcreator.test.polynomialanalysistest.polynomialdataanalysistest import PolynomialDataAnalysisTestCase from solventmapcreator.test.polynomialanalysistest.polynomialcomparisontest import PolynomialComparisonTestCase from solventmapcreator.test.polynomialanalysistest.polynomialplottingtest import PolynomialPlottingTestCase +from solventmapcreator.test.clusteringanalysistest.matrixinputtest import MatrixInputTestCase + logging.basicConfig() LOGGER = logging.getLogger(__name__) LOGGER.setLevel(logging.WARN) @@ -38,6 +40,8 @@ POLYNOMIAL_ANALYSIS_TEST_CASES = [PolynomialDataAnalysisTestCase, PolynomialComparisonTestCase, PolynomialPlottingTestCase] +CLUSTERING_ANALYSIS_TEST_CASES = [MatrixInputTestCase] + def test_suite(): """Function creates a test suite and then loads all the tests from the different test cases. @@ -54,6 +58,9 @@ def test_suite(): for test_case in POLYNOMIAL_ANALYSIS_TEST_CASES: LOGGER.debug("Adding %s", test_case) suite.addTests(loader.loadTestsFromTestCase(test_case)) + for test_case in CLUSTERING_ANALYSIS_TEST_CASES: + LOGGER.debug("Adding %s", test_case) + suite.addTests(loader.loadTestsFromTestCase(test_case)) return suite if __name__ == '__main__':