diff --git a/solventmapcreator/polynomialanalysis/polynomialvaluecalculator.py b/solventmapcreator/polynomialanalysis/polynomialvaluecalculator.py new file mode 100644 index 0000000000000000000000000000000000000000..d02cb2e4f87b5a3fa1ee2da0e0fab1ac733e83a5 --- /dev/null +++ b/solventmapcreator/polynomialanalysis/polynomialvaluecalculator.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Script for calculating the Energy values when given polynomial coefficients. + +This copes with fitting to positive and negative plot regions as well as +fitting over the full range. + +@author: mark +""" + +import logging +import numpy as np +import numpy.polynomial.polynomial as poly + +logging.basicConfig() +LOGGER = logging.getLogger(__name__) +LOGGER.setLevel(logging.WARN) + +def calculate_polynomial_values(x_values, polynomial_coefficients): + """This evaluates the polynomial function for the given x values. + If a dict is given, signifying 2 different fitting regimes, that is used, + else it just calls polyval, assuming a single numpy array of coefficients + was given. + """ + if type(polynomial_coefficients) is dict: + return calculate_poly_value_for_array_from_dict(x_values, polynomial_coefficients) + else: + return calculate_polynomial_value(x_values, polynomial_coefficients) + +def calculate_poly_value_for_array_from_dict(x_values, poly_coeffs): + """This calculates the polynomial values for the given x values, + using a different coefficient set depending on x value sign. + """ + poly_values = np.empty(x_values.shape) + for i in range(len(x_values)): + poly_values[i] = calculate_poly_value_from_dict(x_values[i], poly_coeffs) + return poly_values + + +def calculate_poly_value_from_dict(x_value, poly_coeffs): + """This calculates the polynomial value + """ + if x_value < 0.0: + return calculate_polynomial_value(x_value, poly_coeffs["negative"]) + else: + return calculate_polynomial_value(x_value, poly_coeffs["positive"]) + +def calculate_polynomial_value(x_value, polynomial_coefficients): + """This calculates the value. + """ + return poly.polyval(x_value, polynomial_coefficients) diff --git a/solventmapcreator/test/polynomialanalysistest/polynomialvaluecalculatortest.py b/solventmapcreator/test/polynomialanalysistest/polynomialvaluecalculatortest.py new file mode 100644 index 0000000000000000000000000000000000000000..5035ffba1368f8e6a602c206e7cf12b2aca54973 --- /dev/null +++ b/solventmapcreator/test/polynomialanalysistest/polynomialvaluecalculatortest.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Script for testing the polynomialvaluecalculator. + +@author: mark +""" + +import logging +import numpy as np +import unittest +import solventmapcreator.polynomialanalysis.polynomialvaluecalculator as polyvalcalc + +logging.basicConfig() +LOGGER = logging.getLogger(__name__) +LOGGER.setLevel(logging.WARN) + +class PolynomialValueCalculatorTestCase(unittest.TestCase): + """Test case for the polynomialvaluecalculator script. + """ + def setUp(self): + """Set up for tests + """ + self.poly_coeff_dict = {"positive":np.array([0.5, 0.0, 1.0]), + "negative":np.array([0.5, 0.0, -1.0])} + self.input_array = np.array([x - 2.0 for x in range(5)]) + def tearDown(self): + """Clean up after tests. + """ + del self.poly_coeff_dict + def test_calculate_polynomial_values(self): + """Test to see if expected arrays are produced. + """ + expected_array1 = np.array([-3.5, -0.5, 0.5, 1.5, 4.5]) + actual_array1 = polyvalcalc.calculate_polynomial_values(self.input_array, self.poly_coeff_dict) + np.testing.assert_array_almost_equal(expected_array1, actual_array1) + expected_array2 = np.array([4.5, 1.5, 0.5, 1.5, 4.5]) + actual_array2 = polyvalcalc.calculate_polynomial_values(self.input_array, self.poly_coeff_dict["positive"]) + np.testing.assert_array_almost_equal(expected_array2, actual_array2) + def test_calculate_poly_value_for_array_from_dict(self): + """Test to see if expected array iss produced. + """ + expected_array = np.array([-3.5, -0.5, 0.5, 1.5, 4.5]) + actual_array = polyvalcalc.calculate_poly_value_for_array_from_dict(self.input_array, self.poly_coeff_dict) + np.testing.assert_array_almost_equal(expected_array, actual_array) + def test_calculate_poly_value_from_dict(self): + """Test to see if expected values are calculated based on sign. + """ + expected_value1 = 1.5 + actual_value1 = polyvalcalc.calculate_poly_value_from_dict(1.0, self.poly_coeff_dict) + self.assertAlmostEqual(expected_value1, actual_value1) + expected_value2 = -0.5 + actual_value2 = polyvalcalc.calculate_poly_value_from_dict(-1.0, self.poly_coeff_dict) + self.assertAlmostEqual(expected_value2, actual_value2) + def test_calculate_polynomial_value(self): + """Test to see if expected value is returned. + """ + expected_value = 1.5 + actual_value = polyvalcalc.calculate_polynomial_value(1.0, self.poly_coeff_dict["positive"]) + self.assertAlmostEqual(expected_value, actual_value) diff --git a/solventmapcreator/test/solvationmapcreatortests.py b/solventmapcreator/test/solvationmapcreatortests.py index 8f0c8a91a0389ef343d8fb6e4d2396090f3b6df3..0eec45cf1f8b643db4ca5e8459d488edd1d2f137 100644 --- a/solventmapcreator/test/solvationmapcreatortests.py +++ b/solventmapcreator/test/solvationmapcreatortests.py @@ -22,6 +22,7 @@ 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.polynomialanalysistest.polynomialvaluecalculatortest import PolynomialValueCalculatorTestCase from solventmapcreator.test.clusteringanalysistest.matrixinputtest import MatrixInputTestCase from solventmapcreator.test.clusteringanalysistest.clustercalculationtest import ClusterCalculationTestCase from solventmapcreator.test.clusteringanalysistest.clusterstatisticstest import ClusterStatisticsTestCase @@ -42,7 +43,8 @@ SOLVATION_CALCULATION_TEST_CASES = [SolvationCalculatorTestCase, POLYNOMIAL_ANALYSIS_TEST_CASES = [PolynomialDataAnalysisTestCase, PolynomialComparisonTestCase, - PolynomialPlottingTestCase] + PolynomialPlottingTestCase, + PolynomialValueCalculatorTestCase] CLUSTERING_ANALYSIS_TEST_CASES = [MatrixInputTestCase, ClusterCalculationTestCase, ClusterStatisticsTestCase, ClusterAnalysisTestCase]