diff --git a/solventmapcreator/io/polynomialdatareader.py b/solventmapcreator/io/polynomialdatareader.py index f33d6ed3c81ac3c62846c25636245ea0e1c08eda..0e1bc96c00b99ca60da6fda098eed107652e69fd 100644 --- a/solventmapcreator/io/polynomialdatareader.py +++ b/solventmapcreator/io/polynomialdatareader.py @@ -58,8 +58,13 @@ def read_polynomial_data_line(line_list): 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} + fit_range = line_list[1] + RMSE = float(line_list[2]) + covariance = float(line_list[3]) + coefficients = np.array([float(x) for x in line_list[4:]]) + if fit_range == "All": + return {"order": order, "RMSE":RMSE, "covar":covariance, + "coefficients":coefficients} + else: + return {"order": order, "RMSE":RMSE, "covar":covariance, + "coefficients":coefficients, "fit_range":fit_range} diff --git a/solventmapcreator/io/polynomialdatawriter.py b/solventmapcreator/io/polynomialdatawriter.py index b690b6124ac2b57b15c5655086957c63378a619e..161d81040dec15ec54628329ad53a8815b9c16a6 100644 --- a/solventmapcreator/io/polynomialdatawriter.py +++ b/solventmapcreator/io/polynomialdatawriter.py @@ -13,6 +13,17 @@ logging.basicConfig() LOGGER = logging.getLogger(__name__) LOGGER.setLevel(logging.WARN) +def write_poly_data_to_file_split_fit(datapoints, order_list, filename, + pos_subset_list, neg_subset_list): + """This calculates the polynomial information for fitting to the 2 sub sets + and writes to file. + """ + poly_fit_data = generate_poly_fit_data_order_list_split_fit(datapoints, + order_list, + pos_subset_list, + neg_subset_list) + return write_poly_fit_data_to_file(poly_fit_data, filename, split_fit_range=True) + def write_poly_data_to_file(datapoints, order_list, filename): """This calculates the polynomial information, and then outputs the information to file. @@ -20,10 +31,21 @@ def write_poly_data_to_file(datapoints, order_list, filename): 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): +def write_poly_fit_data_to_file(poly_fit_data, filename, **kwargs): """This writes the information to file. """ - return resultswritertofile.write_poly_fit_information_to_file(poly_fit_data, filename) + return resultswritertofile.write_poly_fit_information_to_file(poly_fit_data, filename, split_fit_range=kwargs.get("split_fit_range", False)) + +def generate_poly_fit_data_order_list_split_fit(datapoints, order_list, pos_subset_list, neg_subset_list): + """This generates the fit data for fitting to the positive and negative regions separately. + """ + poly_fit_data = {} + for order in order_list: + poly_fit_data[order] = generate_polynomial_fit_data_positive_negative_separate_fit(datapoints, + order, + pos_subset_list, + neg_subset_list) + return poly_fit_data def generate_poly_fit_data_order_list(datapoints, order_list): """This cretes the polynomial fit data for all the given polynomial orders @@ -34,6 +56,21 @@ def generate_poly_fit_data_order_list(datapoints, order_list): poly_fit_data[order] = generate_polynomial_fit_data(datapoints, order) return poly_fit_data +def generate_polynomial_fit_data_positive_negative_separate_fit(datapoints, order, pos_subset_list, neg_subset_list): + """This generates the fit for the positive and negative datapoints separately. + A dictionary containing the 2 fits are returned. + """ + positive_fit = generate_polynomial_fit_data_subset(datapoints, order, pos_subset_list) + negative_fit = generate_polynomial_fit_data_subset(datapoints, order, neg_subset_list) + return {"positive":positive_fit, "negative":negative_fit} + +def generate_polynomial_fit_data_subset(datapoints, order, subset_list): + """This extracts the subset and then calculates the polynomial fit data. + """ + datapoints_subset = datapoints.createDatapointsSubGroup(subset_list) + datapoints_subset.createDataArrays() + return generate_polynomial_fit_data(datapoints_subset, order) + def generate_polynomial_fit_data(datapoints, order): """This creates the polynomial fit data for the given order of polynomial for the datapoints given. diff --git a/solventmapcreator/test/iotest/polynomialdatareadertest.py b/solventmapcreator/test/iotest/polynomialdatareadertest.py index aa201eb8ff9b197e2705bd7d84d0c9e7f2fcf78a..30c74750349f578f9a500bace033802123d4dff4 100644 --- a/solventmapcreator/test/iotest/polynomialdatareadertest.py +++ b/solventmapcreator/test/iotest/polynomialdatareadertest.py @@ -21,8 +21,8 @@ class PolynomialDataReaderTestCase(unittest.TestCase): 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_list = [['Order','Fit Range', 'RMSE', 'Covariance', 'Coefficients'], + ['1', 'All', '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, diff --git a/solventmapcreator/test/iotest/polynomialdatawritertest.py b/solventmapcreator/test/iotest/polynomialdatawritertest.py index 37c44a64701ca2ce6f8b3f618f125f2db5780a94..a94c3a6b48ba4f1e7ead3590980ddfe24978c83a 100644 --- a/solventmapcreator/test/iotest/polynomialdatawritertest.py +++ b/solventmapcreator/test/iotest/polynomialdatawritertest.py @@ -49,6 +49,12 @@ class PolynomialDataWriterTestCase(unittest.TestCase): del self.expected_file_name if os.path.isfile("actual_poly_fit.csv"): os.remove("actual_poly_fit.csv") + if os.path.isfile("actual_poly_fit2.csv"): + os.remove("actual_poly_fit2.csv") + def test_write_poly_data_to_file_split_fit(self): + """Test to see if expected file is written out. + """ + def test_write_poly_data_to_file(self): """Test to see if expected file is produced. """ @@ -73,6 +79,39 @@ class PolynomialDataWriterTestCase(unittest.TestCase): 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_split_fit(self): + """Test to see if expected dictionary is returned. + """ + expected_poly_dict = {1:{"positive":self.expected_polynomial_data[1], + "negative":self.expected_polynomial_data[1]}} + actual_poly_dict = polynomialdatawriter.generate_poly_fit_data_order_list_split_fit(self.datapoints, + [1], ['EXAMPLE1', + 'EXAMPLE2', + 'EXAMPLE3'], + ['EXAMPLE1', + 'EXAMPLE2', + 'EXAMPLE3']) + self.assertListEqual(sorted(expected_poly_dict.keys()), sorted(actual_poly_dict.keys())) + for poly_order in expected_poly_dict.keys(): + expected_mix_dictionary = expected_poly_dict[poly_order] + actual_mix_dictionary = actual_poly_dict[poly_order] + self.assertListEqual(sorted(expected_mix_dictionary.keys()), + sorted(actual_mix_dictionary.keys())) + for key in expected_mix_dictionary.keys(): + actual_dictionary = actual_mix_dictionary[key] + expected_dictionary = expected_mix_dictionary[key] + 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]) def test_generate_poly_fit_data_order_list(self): """Test to see if expected dictionary is produced. """ @@ -88,6 +127,51 @@ class PolynomialDataWriterTestCase(unittest.TestCase): 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_positive_negative_separate_fit(self): + """Test to see if expected dict is returned. + """ + expected_mix_dictionary = {"positive":self.expected_polynomial_data[1], + "negative":self.expected_polynomial_data[1]} + actual_mix_dictionary = polynomialdatawriter.generate_polynomial_fit_data_positive_negative_separate_fit(self.datapoints, + 1, ['EXAMPLE1', + 'EXAMPLE2', + 'EXAMPLE3'], + ['EXAMPLE1', + 'EXAMPLE2', + 'EXAMPLE3']) + self.assertListEqual(sorted(expected_mix_dictionary.keys()), + sorted(actual_mix_dictionary.keys())) + for key in expected_mix_dictionary.keys(): + actual_dictionary = actual_mix_dictionary[key] + expected_dictionary = expected_mix_dictionary[key] + 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]) + def test_generate_polynomial_fit_data_subset(self): + """Test to see if subset is fitted to and expected dict is returned. + """ + expected_dictionary = self.expected_polynomial_data[1] + actual_dictionary = polynomialdatawriter.generate_polynomial_fit_data_subset(self.datapoints, + 1, ['EXAMPLE1', + 'EXAMPLE2', + 'EXAMPLE3']) + 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]) def test_generate_polynomial_fit_data(self): """Test to see if expected dictionary is produced. """ diff --git a/solventmapcreator/test/resources/expected_poly_fit.csv b/solventmapcreator/test/resources/expected_poly_fit.csv index 6a37847194a114b7a1ce3ea25156b299a4cddf83..7dade0215094f9d975604b171147262a1bc1c83f 100644 --- a/solventmapcreator/test/resources/expected_poly_fit.csv +++ b/solventmapcreator/test/resources/expected_poly_fit.csv @@ -1,2 +1,2 @@ -Order RMSE Covariance Coefficients -1 0.0154303 0.0007143 -0.0500000 1.0714286 +Order Fit Range RMSE Covariance Coefficients +1 All 0.0154303 0.0007143 -0.0500000 1.0714286 diff --git a/solventmapcreator/test/resources/water_poly_fit.csv b/solventmapcreator/test/resources/water_poly_fit.csv index 0342fd8575a940e3f3ea75fd0cc8d82a911a87fa..828ee4dbfaab0ee3c122b80b24a71c225732a0fa 100644 --- a/solventmapcreator/test/resources/water_poly_fit.csv +++ b/solventmapcreator/test/resources/water_poly_fit.csv @@ -1,3 +1,3 @@ -Order RMSE Covariance Coefficients -2 4.0483148 180.2773802 -3.5005663 -0.7862852 -0.2194883 -4 0.3415296 1.2830673 0.5181064 0.2546154 -0.3766517 -0.0206377 -0.0003760 +Order Fit Range RMSE Covariance Coefficients +2 All 4.0483148 180.2773802 -3.5005663 -0.7862852 -0.2194883 +4 All 0.3415296 1.2830673 0.5181064 0.2546154 -0.3766517 -0.0206377 -0.0003760 diff --git a/solventmapcreator/test/resources/water_poly_fit_subset.csv b/solventmapcreator/test/resources/water_poly_fit_subset.csv index bd92d5594757a70f1d82eb20a4f31f37bf1a2db7..e5b49042b960fbd86f2a1c12d498cf28bc9ed459 100644 --- a/solventmapcreator/test/resources/water_poly_fit_subset.csv +++ b/solventmapcreator/test/resources/water_poly_fit_subset.csv @@ -1,3 +1,3 @@ -Order RMSE Covariance Coefficients -2 0.0188857 0.0017833 6.3245323 2.8927088 -0.0017566 -4 0.0000000 5.0388427 2.2906276 -0.0985583 -0.0064807 -0.0001546 +Order Fit Range RMSE Covariance Coefficients +2 All 0.0188857 0.0017833 6.3245323 2.8927088 -0.0017566 +4 All 0.0000000 5.0388427 2.2906276 -0.0985583 -0.0064807 -0.0001546