From 025657fd2c54f83e2082847e65e2d6981fab2805 Mon Sep 17 00:00:00 2001 From: David Hendriks <davidhendriks93@gmail.com> Date: Thu, 19 Aug 2021 22:27:56 +0100 Subject: [PATCH] fixing the tests for 2.2.1 --- binarycpython/tests/main.py | 2 +- binarycpython/tests/test_custom_logging.py | 2 +- binarycpython/utils/functions.py | 30 +++++++++++++++++++ binarycpython/utils/grid.py | 35 ++++------------------ 4 files changed, 37 insertions(+), 32 deletions(-) diff --git a/binarycpython/tests/main.py b/binarycpython/tests/main.py index 9d7e00f63..abafd95b2 100755 --- a/binarycpython/tests/main.py +++ b/binarycpython/tests/main.py @@ -5,7 +5,6 @@ Main file for the tests. This file imports all the combined_test functions from import unittest - from binarycpython.tests.test_c_bindings import * from binarycpython.tests.test_custom_logging import * from binarycpython.tests.test_distributions import * @@ -18,6 +17,7 @@ from binarycpython.tests.test_spacing_functions import * from binarycpython.tests.test_useful_funcs import * from binarycpython.tests.test_grid_options_defaults import * from binarycpython.tests.test_stellar_types import * +from test_notebooks import * if __name__ == "__main__": unittest.main() diff --git a/binarycpython/tests/test_custom_logging.py b/binarycpython/tests/test_custom_logging.py index 9e9600f41..17f54545f 100644 --- a/binarycpython/tests/test_custom_logging.py +++ b/binarycpython/tests/test_custom_logging.py @@ -128,7 +128,7 @@ class test_custom_logging(unittest.TestCase): input_2 = "version" output_2 = from_binary_c_config(BINARY_C_CONFIG, input_2) - self.assertIn(output_2, ["2.1.7", "2.2pre1"], msg="binary_c version doesnt match") + self.assertIn(output_2, ["2.1.7", "2.2pre1", "2.2.0", "2.2.1"], msg="binary_c version doesnt match") def test_return_compilation_dict(self): with Capturing() as output: diff --git a/binarycpython/utils/functions.py b/binarycpython/utils/functions.py index c1c2379be..5a1caf4ce 100644 --- a/binarycpython/utils/functions.py +++ b/binarycpython/utils/functions.py @@ -10,6 +10,7 @@ Tasks: import json import os +import gc import tempfile import copy import inspect @@ -73,6 +74,35 @@ def get_size(obj, seen=None): size += sum([get_size(i, seen) for i in obj]) return size + +def format_ensemble_results(ensemble_dictionary): + """ + Function to handle all the steps of formatting the ensemble output again. + + Input: + ensemble_dictionary: dictionary containing all the ensemble results + """ + + original_ensemble_results = ensemble_dictionary + + float_format_ensemble_results = recursive_change_key_to_float(original_ensemble_results) + del original_ensemble_results + gc.collect() + + # Then sort the dictionary + sorted_ensemble_results = custom_sort_dict(float_format_ensemble_results) + del float_format_ensemble_results + gc.collect() + + # Then Change the keys back to a string but with a %g format. + reformatted_ensemble_results = recursive_change_key_to_string(sorted_ensemble_results) + del sorted_ensemble_results + gc.collect() + + # Put back in the dictionary + return reformatted_ensemble_results + + def subtract_dicts(dict_1: dict, dict_2: dict) -> dict: """ Function to subtract two dictionaries. diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py index c0bfae70a..d027b8a4c 100644 --- a/binarycpython/utils/grid.py +++ b/binarycpython/utils/grid.py @@ -72,6 +72,7 @@ from binarycpython.utils.functions import ( custom_sort_dict, recursive_change_key_to_string, multiply_values_dict, + format_ensemble_results, ) @@ -961,33 +962,6 @@ class Population: for _ in range(amt_cores): job_queue.put("STOP") - def format_ensemble_results(self, ensemble_dictionary): - """ - Function to handle all the steps of formatting the ensemble output again. - - Input: - ensemble_dictionary: dictionary containing all the ensemble results - """ - - original_ensemble_results = ensemble_dictionary - - float_format_ensemble_results = recursive_change_key_to_float(original_ensemble_results) - del original_ensemble_results - gc.collect() - - # Then sort the dictionary - sorted_ensemble_results = custom_sort_dict(float_format_ensemble_results) - del float_format_ensemble_results - gc.collect() - - # Then Change the keys back to a string but with a %g format. - reformatted_ensemble_results = recursive_change_key_to_string(sorted_ensemble_results) - del sorted_ensemble_results - gc.collect() - - # Put back in the dictionary - return reformatted_ensemble_results - def _evolve_population_grid(self): """ Function that handles running the population using multiprocessing. @@ -1049,7 +1023,7 @@ class Population: break # Extra ensemble result manipulation: - combined_output_dict['ensemble_results']['ensemble'] = self.format_ensemble_results(combined_output_dict['ensemble_results'].get('ensemble', {})) + combined_output_dict['ensemble_results']['ensemble'] = format_ensemble_results(combined_output_dict['ensemble_results'].get('ensemble', {})) gc.collect() # Take into account that we run this on multiple cores @@ -1421,10 +1395,11 @@ class Population: ), ) + # TODO: consider writing this in a formatted structure # Write to file - ensemble_output = extract_ensemble_json_from_string(ensemble_raw_output) with open(output_file, "w") as f: - f.write(json.dumps(self.format_ensemble_results(ensemble_output))) + f.write(ensemble_raw_output) + # f.write(json.dumps(self.format_ensemble_results(ensemble_output))) print( "Thread {}: Wrote ensemble results directly to file: {}".format( -- GitLab