From fcbee42bc272ecb68143b176a9a3f36a4dcb3ad7 Mon Sep 17 00:00:00 2001
From: Mark Driver <mdd31@cam.ac.uk>
Date: Sun, 13 Aug 2017 23:20:41 +0100
Subject: [PATCH] added writer for solvation matrix.

---
 solventmapcreator/io/solvationmapwriter.py    | 41 +++++++++++++
 .../test/iotest/solvationmapwritertest.py     | 59 +++++++++++++++++++
 .../test/resources/similmatrix.csv            |  4 ++
 .../test/solvationmapcreatortests.py          |  3 +-
 4 files changed, 106 insertions(+), 1 deletion(-)
 create mode 100644 solventmapcreator/io/solvationmapwriter.py
 create mode 100644 solventmapcreator/test/iotest/solvationmapwritertest.py
 create mode 100644 solventmapcreator/test/resources/similmatrix.csv

diff --git a/solventmapcreator/io/solvationmapwriter.py b/solventmapcreator/io/solvationmapwriter.py
new file mode 100644
index 0000000..f5130a7
--- /dev/null
+++ b/solventmapcreator/io/solvationmapwriter.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+"""
+Script for writing out the solvation values generated for a solvent map.
+
+Format is 
+
+    Y1 Y2 ... 
+X1 S(X1,Y1) S(X1,Y2) ...
+X2 S(X2,Y1) S(X2,Y2) ... 
+.  .
+.  .
+.  .
+XN S(XN,Y1) S(XN,Y2) ...
+
+@author: mark
+"""
+
+import logging
+import csv
+
+logging.basicConfig()
+LOGGER = logging.getLogger(__name__)
+LOGGER.setLevel(logging.WARN)
+
+def write_matrix_to_file(filename, x_values, y_values, solvation_matrix):
+    """This writes the matrix to file.
+    """
+    with open(filename, 'w') as outfile:
+        csv_writer = csv.writer(outfile, delimiter='\t',
+                                quoting=csv.QUOTE_NONE)
+        csv_writer.writerow(["", *y_values])
+        for i in range(len(x_values)):
+            csv_writer.writerow(write_matrix_line(x_values[i], solvation_matrix[i]))
+    return 0
+
+
+def write_matrix_line(x_value, solvation_matrix_row):
+    """This creates the list of entries ready for writing.
+    """
+    return ["{:.3f}".format(x_value), *["{:.7f}".format(val) for val in solvation_matrix_row]]
diff --git a/solventmapcreator/test/iotest/solvationmapwritertest.py b/solventmapcreator/test/iotest/solvationmapwritertest.py
new file mode 100644
index 0000000..93232b2
--- /dev/null
+++ b/solventmapcreator/test/iotest/solvationmapwritertest.py
@@ -0,0 +1,59 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+"""
+Created on Sun Aug 13 22:42:58 2017
+
+@author: mark
+"""
+
+import logging
+import numpy as np
+import os
+import unittest
+import solventmapcreator.io.solvationmapwriter as solvmapwriter
+
+logging.basicConfig()
+LOGGER = logging.getLogger(__name__)
+LOGGER.setLevel(logging.WARN)
+
+class SolvationMapWriterTestCase(unittest.TestCase):
+    """Test case for the solvation
+    """
+    def setUp(self):
+        """Set up for tests.
+        """
+        self.x_list = np.array([float(x) for x in range(3)])
+        self.y_list = np.array([float(x) for x in range(2)])
+        self.similarity_matrix = np.array([[-4.332289, -5.403718],
+                                           [-5.403718, -5.757629],
+                                           [-6.475146, -6.167992]])
+    def tearDown(self):
+        """Clean up after tests
+        """
+        del self.x_list
+        del self.y_list
+        del self.similarity_matrix
+        if os.path.isfile("actualsimilmatrix.csv"):
+            os.remove("actualsimilmatrix.csv")
+    def test_write_similarity_matrix_file(self):
+        """Test to see if expected file is written out.
+        """
+        expected_file_name = "resources/similmatrix.csv"
+        actual_file_name = "actualsimilmatrix.csv"
+        simil_out_file = solvmapwriter.write_matrix_to_file(actual_file_name,
+                                                            self.x_list,
+                                                            self.y_list,
+                                                            self.similarity_matrix)
+        self.assertEqual(0, simil_out_file)
+        with open(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_create_file_line(self):
+        """Test to see if expected list is returned.
+        """
+        expected_list = ['0.000', '-4.3322890', '-5.4037180']
+        actual_list = solvmapwriter.write_matrix_line(self.x_list[0], self.similarity_matrix[0])
+        self.assertListEqual(expected_list, actual_list)
+        
\ No newline at end of file
diff --git a/solventmapcreator/test/resources/similmatrix.csv b/solventmapcreator/test/resources/similmatrix.csv
new file mode 100644
index 0000000..d4471c7
--- /dev/null
+++ b/solventmapcreator/test/resources/similmatrix.csv
@@ -0,0 +1,4 @@
+	0.0	1.0
+0.000	-4.3322890	-5.4037180
+1.000	-5.4037180	-5.7576290
+2.000	-6.4751460	-6.1679920
diff --git a/solventmapcreator/test/solvationmapcreatortests.py b/solventmapcreator/test/solvationmapcreatortests.py
index 0eec45c..1b714e1 100644
--- a/solventmapcreator/test/solvationmapcreatortests.py
+++ b/solventmapcreator/test/solvationmapcreatortests.py
@@ -15,6 +15,7 @@ from solventmapcreator.test.iotest.polynomialdatareadertest import PolynomialDat
 from solventmapcreator.test.iotest.similaritymatrixwritertest import SimilarityMatrixWriterTestCase
 from solventmapcreator.test.iotest.similaritymatrixreadertest import SimilarityMatrixReaderTestCase
 from solventmapcreator.test.iotest.linkagewritertest import LinkageWriterTestCase
+from solventmapcreator.test.iotest.solvationmapwritertest import SolvationMapWriterTestCase
 from solventmapcreator.test.solvationcalculationtest.solvationcalculatortest import SolvationCalculatorTestCase
 from solventmapcreator.test.solvationcalculationtest.solvationmapgeneratortest import SolvationMapGeneratorTestCase
 from solventmapcreator.test.solvationcalculationtest.solvationplotinformationtest import SolvationPlotInformationTestCase
@@ -34,7 +35,7 @@ LOGGER.setLevel(logging.WARN)
 IO_TEST_CASES = [SolvationEnergyReaderTestCase, SolventXMLReaderTestCase,
                  PolynomialDataWriterTestCase, PolynomialDataReaderTestCase,
                  SimilarityMatrixReaderTestCase, SimilarityMatrixWriterTestCase,
-                 LinkageWriterTestCase]
+                 LinkageWriterTestCase, SolvationMapWriterTestCase]
 
 SOLVATION_CALCULATION_TEST_CASES = [SolvationCalculatorTestCase,
                                     SolvationMapGeneratorTestCase,
-- 
GitLab