From d18ceba124752a4c1aa287c35d419477430fd87c Mon Sep 17 00:00:00 2001 From: Mark Driver <mdd31@cam.ac.uk> Date: Thu, 6 Jul 2017 16:11:13 +0100 Subject: [PATCH] added script containing methods for reading in polynomial fit data. --- solventmapcreator/io/polynomialdatareader.py | 65 +++++++++++++ .../test/iotest/polynomialdatareadertest.py | 95 +++++++++++++++++++ .../test/solvationmapcreatortests.py | 4 +- 3 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 solventmapcreator/io/polynomialdatareader.py create mode 100644 solventmapcreator/test/iotest/polynomialdatareadertest.py diff --git a/solventmapcreator/io/polynomialdatareader.py b/solventmapcreator/io/polynomialdatareader.py new file mode 100644 index 0000000..e51e8c9 --- /dev/null +++ b/solventmapcreator/io/polynomialdatareader.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Script for reading in polynomial data from the csv files. + +@author: mark +""" + +import logging +import numpy as np +import csv + +logging.basicConfig() +LOGGER = logging.getLogger(__name__) +LOGGER.setLevel(logging.WARN) + +def parse_polynomial_data_file_list(filename_list): + """This parses each file, and then returns the dictionary containing the + information, by solvent ID. + """ + polynomial_data_by_solventid ={} + for filename in filename_list: + solvent_id = generate_solvent_id(filename) + polynomial_fit_data = parse_polynomial_data_file(filename) + polynomial_data_by_solventid[solvent_id] = polynomial_fit_data + return polynomial_data_by_solventid + +def generate_solvent_id(filename): + """This gets the solvent ID from the filename. assumes file name is of the + form '*/solventID.csv' where 'solventID' is the solvent ID. + """ + return filename.split('/')[-1].replace('.csv', '') + +def parse_polynomial_data_file(filename): + """This reads in the file and parses the polynomial lines. + """ + polynomial_data_dict = {} + data_in = read_polynomial_data_file(filename) + for line_list in data_in[1:]: + polynomial_data = read_polynomial_data_line(line_list) + polynomial_data_dict[polynomial_data["order"]] = polynomial_data + return polynomial_data_dict + +def read_polynomial_data_file(filename): + """This reads in the tab separated variable file containing the polynomial fit data. + """ + data_in = [] + with open(filename, "r") as file_in: + file_contents = csv.reader(file_in, delimiter='\t') + for line in file_contents: + data_in.append(line) + return data_in + +def read_polynomial_data_line(line_list): + """This reads in the values to a dictionary. + This assumes that the values in the line are formatted such that they are + ordered: + Order, RMSE, covariance, coefficients. + """ + order = int(line_list[0]) + RMSE = float(line_list[1]) + covariance = float(line_list[2]) + coefficients = np.array([float(x) for x in line_list[3:]]) + return {"order": order, "RMSE":RMSE, "covar":covariance, + "coefficients":coefficients} diff --git a/solventmapcreator/test/iotest/polynomialdatareadertest.py b/solventmapcreator/test/iotest/polynomialdatareadertest.py new file mode 100644 index 0000000..cf39aea --- /dev/null +++ b/solventmapcreator/test/iotest/polynomialdatareadertest.py @@ -0,0 +1,95 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Script for testing the polynomialdatareader script. + +@author: mark +""" + +import logging +import unittest +import numpy as np +import solventmapcreator.io.polynomialdatareader as polynomialdatareader + +logging.basicConfig() +LOGGER = logging.getLogger(__name__) +LOGGER.setLevel(logging.WARN) + +class PolynomialDataReaderTestCase(unittest.TestCase): + """Test Case for the polynomialdatareader script. + """ + def setUp(self): + """Set up for tests. + """ + self.expected_list = [['Order', 'RMSE', 'Covariance', 'Coefficients'], + ['1', '0.0154303', '0.0007143', '-0.0500000', '1.0714286']] + self.expected_data_by_solvent_id = {"expected_poly_fit":{1:{"coefficients":np.array([-0.05, + 1.07142857]), + "RMSE":0.015430334996209221, + "order":1, + "covar":0.0007143}}} + def tearDown(self): + """Tear down after tests. + """ + del self.expected_data_by_solvent_id + del self.expected_list + def test_parse_polynomial_data_file_list(self): + """Test to see if expected information is read in from file list. + """ + actual_data_by_solvent_id = polynomialdatareader.parse_polynomial_data_file_list(["resources/expected_poly_fit.csv"]) + self.assertListEqual(sorted(self.expected_data_by_solvent_id.keys()), sorted(actual_data_by_solvent_id.keys())) + expected_poly_dictionary = self.expected_data_by_solvent_id["expected_poly_fit"] + actual_poly_dictionary = actual_data_by_solvent_id["expected_poly_fit"] + self.assertListEqual(sorted(expected_poly_dictionary.keys()), sorted(actual_poly_dictionary.keys())) + for poly_order in expected_poly_dictionary.keys(): + expected_dictionary = expected_poly_dictionary[poly_order] + actual_dictionary = actual_poly_dictionary[poly_order] + for key in expected_dictionary.keys(): + if key == "RMSE" or key == "covar": + self.assertAlmostEqual(expected_dictionary[key], actual_dictionary[key]) + elif key == "order": + self.assertEqual(expected_dictionary[key], actual_dictionary[key]) + else: + np.testing.assert_array_almost_equal(expected_dictionary[key], actual_dictionary[key]) + def test_generate_solvent_id(self): + """Test to see if expected id is extracted. + """ + expected_id = "water" + actual_id1 = polynomialdatareader.generate_solvent_id("water.csv") + self.assertEqual(expected_id, actual_id1) + actual_id2 = polynomialdatareader.generate_solvent_id("resources/water.csv") + self.assertEqual(expected_id, actual_id2) + def test_parse_polynomial_data_file(self): + """Test to see if file is parsed as expected. + """ + expected_poly_dictionary = self.expected_data_by_solvent_id["expected_poly_fit"] + actual_poly_dictionary = polynomialdatareader.parse_polynomial_data_file("resources/expected_poly_fit.csv") + self.assertListEqual(sorted(expected_poly_dictionary.keys()), sorted(actual_poly_dictionary.keys())) + for poly_order in expected_poly_dictionary.keys(): + expected_dictionary = expected_poly_dictionary[poly_order] + actual_dictionary = actual_poly_dictionary[poly_order] + for key in expected_dictionary.keys(): + if key == "RMSE" or key == "covar": + self.assertAlmostEqual(expected_dictionary[key], actual_dictionary[key]) + elif key == "order": + self.assertEqual(expected_dictionary[key], actual_dictionary[key]) + else: + np.testing.assert_array_almost_equal(expected_dictionary[key], actual_dictionary[key]) + def test_read_polynomial_data_file(self): + """Test to see if file is read as expected. + """ + actual_list = polynomialdatareader.read_polynomial_data_file("resources/expected_poly_fit.csv") + self.assertListEqual(self.expected_list, actual_list) + def test_read_polynomial_data_line(self): + """Test to see if line is read as expected. + """ + expected_dictionary = self.expected_data_by_solvent_id["expected_poly_fit"][1] + actual_dictionary = polynomialdatareader.read_polynomial_data_line(self.expected_list[1]) + self.assertListEqual(sorted(expected_dictionary.keys()), sorted(actual_dictionary.keys())) + for key in expected_dictionary.keys(): + if key == "RMSE" or key == "covar": + self.assertAlmostEqual(expected_dictionary[key], actual_dictionary[key]) + elif key == "order": + self.assertEqual(expected_dictionary[key], actual_dictionary[key]) + else: + np.testing.assert_array_almost_equal(expected_dictionary[key], actual_dictionary[key]) \ No newline at end of file diff --git a/solventmapcreator/test/solvationmapcreatortests.py b/solventmapcreator/test/solvationmapcreatortests.py index da27e68..a251085 100644 --- a/solventmapcreator/test/solvationmapcreatortests.py +++ b/solventmapcreator/test/solvationmapcreatortests.py @@ -10,13 +10,15 @@ import logging import unittest from solventmapcreator.test.iotest.solvationenergyreadertest import SolvationEnergyReaderTestCase from solventmapcreator.test.iotest.polynomialdatawritertest import PolynomialDataWriterTestCase +from solventmapcreator.test.iotest.polynomialdatareadertest import PolynomialDataReaderTestCase from solventmapcreator.test.solvationcalculationtest.solvationcalculatortest import SolvationCalculatorTestCase logging.basicConfig() LOGGER = logging.getLogger(__name__) LOGGER.setLevel(logging.WARN) -IO_TEST_CASES = [SolvationEnergyReaderTestCase, PolynomialDataWriterTestCase] +IO_TEST_CASES = [SolvationEnergyReaderTestCase, PolynomialDataWriterTestCase, + PolynomialDataReaderTestCase] SOLVATION_CALCULATION_TEST_CASES = [SolvationCalculatorTestCase] def test_suite(): -- GitLab