FAQ | This is a LIVE service | Changelog

Skip to content
Snippets Groups Projects
Commit 33fc558d authored by M.D. Driver's avatar M.D. Driver
Browse files

created script for writing out the polynomial fit information, and corresponding tests.

parent f8824f4c
No related branches found
No related tags found
No related merge requests found
#!/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)
#!/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])
Order RMSE Covariance Coefficients
1 0.0154303 0.0007143 -0.0500000 1.0714286
...@@ -9,13 +9,14 @@ Script for running the tests. ...@@ -9,13 +9,14 @@ Script for running the tests.
import logging import logging
import unittest import unittest
from solventmapcreator.test.iotest.solvationenergyreadertest import SolvationEnergyReaderTestCase from solventmapcreator.test.iotest.solvationenergyreadertest import SolvationEnergyReaderTestCase
from solventmapcreator.test.iotest.polynomialdatawritertest import PolynomialDataWriterTestCase
from solventmapcreator.test.solvationcalculationtest.solvationcalculatortest import SolvationCalculatorTestCase from solventmapcreator.test.solvationcalculationtest.solvationcalculatortest import SolvationCalculatorTestCase
logging.basicConfig() logging.basicConfig()
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.WARN) LOGGER.setLevel(logging.WARN)
IO_TEST_CASES = [SolvationEnergyReaderTestCase] IO_TEST_CASES = [SolvationEnergyReaderTestCase, PolynomialDataWriterTestCase]
SOLVATION_CALCULATION_TEST_CASES = [SolvationCalculatorTestCase] SOLVATION_CALCULATION_TEST_CASES = [SolvationCalculatorTestCase]
def test_suite(): def test_suite():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment