From 33fc558de5c23b7406189c1a6b5eabd9108c268a Mon Sep 17 00:00:00 2001
From: Mark Driver <mdd31@cam.ac.uk>
Date: Wed, 5 Jul 2017 23:01:57 +0100
Subject: [PATCH] created script for writing out the polynomial fit
 information, and corresponding tests.

---
 solventmapcreator/io/polynomialdatawriter.py  |  41 +++++++
 .../test/iotest/polynomialdatawritertest.py   | 104 ++++++++++++++++++
 .../test/resources/expected_poly_fit.csv      |   2 +
 .../test/solvationmapcreatortests.py          |   3 +-
 4 files changed, 149 insertions(+), 1 deletion(-)
 create mode 100644 solventmapcreator/io/polynomialdatawriter.py
 create mode 100644 solventmapcreator/test/iotest/polynomialdatawritertest.py
 create mode 100644 solventmapcreator/test/resources/expected_poly_fit.csv

diff --git a/solventmapcreator/io/polynomialdatawriter.py b/solventmapcreator/io/polynomialdatawriter.py
new file mode 100644
index 0000000..b690b61
--- /dev/null
+++ b/solventmapcreator/io/polynomialdatawriter.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+"""
+Script for generating the polynomial fit information and writing to file.
+
+@author: mark
+"""
+
+import logging
+import resultsanalysis.resultsoutput.resultswritertofile as resultswritertofile
+
+logging.basicConfig()
+LOGGER = logging.getLogger(__name__)
+LOGGER.setLevel(logging.WARN)
+
+def write_poly_data_to_file(datapoints, order_list, filename):
+    """This calculates the polynomial information, and then outputs the
+    information to file.
+    """
+    poly_fit_data = generate_poly_fit_data_order_list(datapoints, order_list)
+    return write_poly_fit_data_to_file(poly_fit_data, filename)
+
+def write_poly_fit_data_to_file(poly_fit_data, filename):
+    """This writes the information to file.
+    """
+    return resultswritertofile.write_poly_fit_information_to_file(poly_fit_data, filename)
+
+def generate_poly_fit_data_order_list(datapoints, order_list):
+    """This cretes the polynomial fit data for all the given polynomial orders
+    for the datapoints given
+    """
+    poly_fit_data = {}
+    for order in order_list:
+        poly_fit_data[order] = generate_polynomial_fit_data(datapoints, order)
+    return poly_fit_data
+
+def generate_polynomial_fit_data(datapoints, order):
+    """This creates the polynomial fit data for the given order of polynomial
+    for the datapoints given.
+    """
+    return datapoints.poly_fit_data(order)
diff --git a/solventmapcreator/test/iotest/polynomialdatawritertest.py b/solventmapcreator/test/iotest/polynomialdatawritertest.py
new file mode 100644
index 0000000..37c44a6
--- /dev/null
+++ b/solventmapcreator/test/iotest/polynomialdatawritertest.py
@@ -0,0 +1,104 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+"""
+Script for testingthe polynomialdatawriter script.
+
+@author: mark
+"""
+
+import logging
+import unittest
+import numpy as np
+import os
+from resultsanalysis.dataclasses.datapoint import Datapoint
+from resultsanalysis.dataclasses.units import Units
+from resultsanalysis.dataclasses.value import Value
+import resultsanalysis.dataclasses.datapoints as Datapoints
+import solventmapcreator.io.polynomialdatawriter as polynomialdatawriter
+
+logging.basicConfig()
+LOGGER = logging.getLogger(__name__)
+LOGGER.setLevel(logging.WARN)
+
+class PolynomialDataWriterTestCase(unittest.TestCase):
+    """Test case for the polynomial data writer.
+    """
+    def setUp(self):
+        """Set up for test cases
+        """
+        self.datapoints = Datapoints.Datapoints(('Log(Sw)', 'Log(Sw)'),
+                                                  (Units.dimensionless, Units.dimensionless))
+        self.datapoints.addDatapointList([Datapoint(Value(1.0, 'Log(Sw)', Units.dimensionless),
+                                                      Value(1.0, 'Log(Sw)', Units.dimensionless), 'EXAMPLE1'),
+                                            Datapoint(Value(2.0, 'Log(Sw)', Units.dimensionless),
+                                                      Value(2.1, 'Log(Sw)', Units.dimensionless), 'EXAMPLE2'),
+                                            Datapoint(Value(0.5, 'Log(Sw)', Units.dimensionless),
+                                                      Value(0.5, 'Log(Sw)', Units.dimensionless), 'EXAMPLE3')])
+        self.datapoints.createDataArrays()
+        self.expected_polynomial_data = {1:{"coefficients":np.array([-0.05, 
+                                                                     1.07142857]),
+                                            "RMSE":0.015430334996209221,
+                                            "order":1,
+                                            "covar":np.array([0.0007143])}}
+        self.expected_file_name = "resources/expected_poly_fit.csv"
+    def tearDown(self):
+        """Tear down after tests
+        """
+        del self.datapoints
+        del self.expected_polynomial_data
+        del self.expected_file_name
+        if os.path.isfile("actual_poly_fit.csv"):
+            os.remove("actual_poly_fit.csv")
+    def test_write_poly_data_to_file(self):
+        """Test to see if expected file is produced.
+        """
+        actual_file_name = "actual_poly_fit.csv"
+        poly_file_out = polynomialdatawriter.write_poly_data_to_file(self.datapoints,
+                                                                     [1], actual_file_name)
+        self.assertEqual(0, poly_file_out)
+        with open(self.expected_file_name, 'r') as exp_file:
+            exp_file_lines = exp_file.readlines()
+            with open(actual_file_name, 'r') as act_file:
+                act_file_lines = act_file.readlines()
+                self.assertListEqual(act_file_lines, exp_file_lines)
+    def test_write_poly_fit_data_to_file(self):
+        """Test to see if expected file is produced.
+        """
+        actual_file_name = "actual_poly_fit.csv"
+        poly_file_out = polynomialdatawriter.write_poly_fit_data_to_file(self.expected_polynomial_data,
+                                                                         actual_file_name)
+        self.assertEqual(0, poly_file_out)
+        with open(self.expected_file_name, 'r') as exp_file:
+            exp_file_lines = exp_file.readlines()
+            with open(actual_file_name, 'r') as act_file:
+                act_file_lines = act_file.readlines()
+                self.assertListEqual(act_file_lines, exp_file_lines)
+    def test_generate_poly_fit_data_order_list(self):
+        """Test to see if expected dictionary is produced.
+        """
+        actual_poly_dictionary = polynomialdatawriter.generate_poly_fit_data_order_list(self.datapoints, [1])
+        self.assertListEqual(sorted(self.expected_polynomial_data.keys()), sorted(actual_poly_dictionary.keys()))
+        for poly_order in self.expected_polynomial_data.keys():
+            expected_dictionary = self.expected_polynomial_data[poly_order]
+            actual_dictionary = actual_poly_dictionary[poly_order]
+            for key in expected_dictionary.keys():
+                if key == "RMSE":
+                    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_polynomial_fit_data(self):
+        """Test to see if expected dictionary is produced.
+        """
+        actual_dictionary = polynomialdatawriter.generate_polynomial_fit_data(self.datapoints, 1)
+        expected_dictionary = self.expected_polynomial_data[1]
+        self.assertListEqual(sorted(expected_dictionary.keys()), sorted(actual_dictionary.keys()))
+        for key in expected_dictionary.keys():
+            if key == "RMSE":
+                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])
+        
diff --git a/solventmapcreator/test/resources/expected_poly_fit.csv b/solventmapcreator/test/resources/expected_poly_fit.csv
new file mode 100644
index 0000000..6a37847
--- /dev/null
+++ b/solventmapcreator/test/resources/expected_poly_fit.csv
@@ -0,0 +1,2 @@
+Order	RMSE	Covariance	Coefficients
+1	0.0154303	0.0007143	-0.0500000	1.0714286
diff --git a/solventmapcreator/test/solvationmapcreatortests.py b/solventmapcreator/test/solvationmapcreatortests.py
index 151dbdc..da27e68 100644
--- a/solventmapcreator/test/solvationmapcreatortests.py
+++ b/solventmapcreator/test/solvationmapcreatortests.py
@@ -9,13 +9,14 @@ Script for running the tests.
 import logging
 import unittest
 from solventmapcreator.test.iotest.solvationenergyreadertest import SolvationEnergyReaderTestCase
+from solventmapcreator.test.iotest.polynomialdatawritertest import PolynomialDataWriterTestCase
 from solventmapcreator.test.solvationcalculationtest.solvationcalculatortest import SolvationCalculatorTestCase
 
 logging.basicConfig()
 LOGGER = logging.getLogger(__name__)
 LOGGER.setLevel(logging.WARN)
 
-IO_TEST_CASES = [SolvationEnergyReaderTestCase]
+IO_TEST_CASES = [SolvationEnergyReaderTestCase, PolynomialDataWriterTestCase]
 
 SOLVATION_CALCULATION_TEST_CASES = [SolvationCalculatorTestCase]
 def test_suite():
-- 
GitLab