diff --git a/binarycpython/tests/main.py b/binarycpython/tests/main.py index 9d7e00f632b5b42ac96c21ced287609a3d7f622f..abafd95b221ca038e06521ed47a79c4f7198e336 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 9e9600f4105f95848ce3be951e7d2217a81a6e76..17f54545f0941c2a5dbb8db823d6e12bafb958d0 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/tests/test_functions.py b/binarycpython/tests/test_functions.py index 432a48253585c59e4b5ae3c681d465b574cba77f..4c15060dc6682aa76dd0b9f78930347ac6deb87b 100644 --- a/binarycpython/tests/test_functions.py +++ b/binarycpython/tests/test_functions.py @@ -166,7 +166,7 @@ class test_return_binary_c_version_info(unittest.TestCase): Test for the raw version_info output """ - version_info = return_binary_c_version_info() + version_info = return_binary_c_version_info(parsed=False) self.assertTrue(isinstance(version_info, str)) self.assertIn("Build", version_info) @@ -210,7 +210,7 @@ class test_parse_binary_c_version_info(unittest.TestCase): Test for the parsed versio info, more detailed """ - info = return_binary_c_version_info() + info = return_binary_c_version_info(parsed=False) parsed_info = parse_binary_c_version_info(info) self.assertIn("isotopes", parsed_info.keys()) diff --git a/binarycpython/tests/test_grid.py b/binarycpython/tests/test_grid.py index 5c761801ca11679d978ad55c69b1a3ba1048f1ac..ce1bda06b48be6b565a12f2ca9391a7971171578 100644 --- a/binarycpython/tests/test_grid.py +++ b/binarycpython/tests/test_grid.py @@ -256,17 +256,17 @@ class test_Population(unittest.TestCase): self.assertIn("custom_options", population_settings) self.assertTrue(population_settings["custom_options"]["data_dir"] == "/tmp") - def test__return_binary_c_version_info(self): + def test_return_binary_c_version_info(self): with Capturing() as output: - self._test__return_binary_c_version_info() + self._test_return_binary_c_version_info() - def _test__return_binary_c_version_info(self): + def _test_return_binary_c_version_info(self): """ - Unittests for the function _return_binary_c_version_info + Unittests for the function return_binary_c_version_info """ test_pop = Population() - binary_c_version_info = test_pop._return_binary_c_version_info(parsed=True) + binary_c_version_info = test_pop.return_binary_c_version_info(parsed=True) self.assertTrue(isinstance(binary_c_version_info, dict)) self.assertIn("isotopes", binary_c_version_info) @@ -289,17 +289,17 @@ class test_Population(unittest.TestCase): if binary_c_version_info["macros"]["NUCSYN_ID_SOURCES"] == "on": self.assertIsNotNone(binary_c_version_info["nucleosynthesis_sources"]) - def test__return_binary_c_defaults(self): + def test_return_binary_c_defaults(self): with Capturing() as output: - self._test__return_binary_c_defaults() + self._test_return_binary_c_defaults() - def _test__return_binary_c_defaults(self): + def _test_return_binary_c_defaults(self): """ - Unittests for the function _return_binary_c_defaults + Unittests for the function return_binary_c_defaults """ test_pop = Population() - binary_c_defaults = test_pop._return_binary_c_defaults() + binary_c_defaults = test_pop.return_binary_c_defaults() self.assertIn("probability", binary_c_defaults) self.assertIn("phasevol", binary_c_defaults) self.assertIn("metallicity", binary_c_defaults) diff --git a/binarycpython/utils/functions.py b/binarycpython/utils/functions.py index c1c2379beea2d2facf824381440234b89389a138..5a1caf4cee196eed44bf03d894d5371bb0711f6b 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 c0bfae70ae996d079c6fc2c28fdf348d8919b333..d027b8a4cd4858ad26a06fd98fef274922f47c57 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( diff --git a/examples/notebook_individual_systems.ipynb b/examples/notebook_individual_systems.ipynb index 7aec69eae0a90a6d767573701958bdff340b21ec..21d49016f3b2826d6f9d5ec87a1f6d8bb18df7af 100644 --- a/examples/notebook_individual_systems.ipynb +++ b/examples/notebook_individual_systems.ipynb @@ -27,7 +27,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 1, "id": "e32dcdee", "metadata": {}, "outputs": [], @@ -39,7 +39,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 2, "id": "425efed3-d8e3-432d-829e-41d8ebe05162", "metadata": {}, "outputs": [], @@ -50,7 +50,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 3, "id": "b2abab48-433d-4936-8434-14804c52c9f6", "metadata": {}, "outputs": [ @@ -78,7 +78,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 4, "id": "029fc3f2-f09a-49af-a32b-248505738f2e", "metadata": {}, "outputs": [ @@ -86,7 +86,7 @@ "name": "stdout", "output_type": "stream", "text": [ - " TIME M1 M2 K1 K2 SEP PER ECC R1/ROL1 R2/ROL2 TYPE RANDOM_SEED=9935 RANDOM_COUNT=0\n", + " TIME M1 M2 K1 K2 SEP PER ECC R1/ROL1 R2/ROL2 TYPE RANDOM_SEED=17089 RANDOM_COUNT=0\n", " 0.0000 1.000 0.000 1 15 -1 -1 -1.00 0.000 0.000 \"INITIAL \"\n", " 11003.1302 1.000 0.000 2 15 -1 -1 -1.00 0.000 0.000 \"OFF_MS\"\n", " 11003.1302 1.000 0.000 2 15 -1 -1 -1.00 0.000 0.000 \"TYPE_CHNGE\"\n", @@ -118,7 +118,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 5, "id": "e6a23b55-ca42-440d-83ac-e76a24a83a67", "metadata": { "tags": [] @@ -167,7 +167,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 6, "id": "3822721f-217a-495b-962e-d57137b9e290", "metadata": {}, "outputs": [ @@ -217,7 +217,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 7, "id": "654a07ed-2a88-46ff-9da0-b7759580f9f3", "metadata": {}, "outputs": [ @@ -268,7 +268,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 8, "id": "4a98ffca-1b72-4bb8-8df1-3bf3187d882f", "metadata": {}, "outputs": [], @@ -287,7 +287,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 9, "id": "bff1cc2e-6b32-4ba0-879f-879ffbabd223", "metadata": {}, "outputs": [ @@ -333,7 +333,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 10, "id": "dd748bab-b57e-4129-8350-9ea11fa179d0", "metadata": { "scrolled": true, @@ -358,7 +358,7 @@ "Creating and loading custom logging functionality\n", "Running binary_c M_1 10 api_log_filename_prefix /tmp/binary_c_python/notebooks/notebook_individual_systems\n", "Cleaning up the custom logging stuff. type: single\n", - "Removed /tmp/binary_c_python/custom_logging/libcustom_logging_491813c1f1184f97951202e364eb3b55.so\n", + "Removed /tmp/binary_c_python/custom_logging/libcustom_logging_8967553693ac4e11a49c42d4eef773e8.so\n", "EXAMPLE_MASSLOSS 0.000000000000e+00 10 0 10 1\n", "EXAMPLE_MASSLOSS 0.000000000000e+00 10 10 10 1\n", "EXAMPLE_MASSLOSS 1.000000000000e-06 10 10 10 1\n", @@ -398,7 +398,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 11, "id": "fec39154-cce6-438c-8c2c-509d76b00f34", "metadata": {}, "outputs": [], @@ -449,7 +449,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 12, "id": "57347512-fd4a-434b-b13c-5e6dbd3ac415", "metadata": { "scrolled": true, @@ -460,13 +460,13 @@ "name": "stdout", "output_type": "stream", "text": [ - "adding: parse_function=<function object_parse_function at 0x7fa3136e5378> to grid_options\n", + "adding: parse_function=<function object_parse_function at 0x7fb4d41ebbf8> to grid_options\n", "<<<< Warning: Key does not match previously known parameter: adding: output_dir=/tmp/binary_c_python/notebooks/notebook_individual_systems to custom_options >>>>\n", "adding: api_log_filename_prefix=/tmp/binary_c_python/notebooks/notebook_individual_systems to BSE_options\n", "Creating and loading custom logging functionality\n", "Running binary_c M_1 10 api_log_filename_prefix /tmp/binary_c_python/notebooks/notebook_individual_systems\n", "Cleaning up the custom logging stuff. type: single\n", - "Removed /tmp/binary_c_python/custom_logging/libcustom_logging_83729c10ca52451cb773d29d6b28ea96.so\n", + "Removed /tmp/binary_c_python/custom_logging/libcustom_logging_5d7779e8190e4b79b10c7e6a44cb0e7e.so\n", "[['time', 'mass', 'initial_mass', 'stellar_type'], [0.0, 10.0, 0.0, 10.0, 1.0], [0.0, 10.0, 10.0, 10.0, 1.0], [1e-06, 10.0, 10.0, 10.0, 1.0]]\n", "dict_keys(['time', 'mass', 'initial_mass', 'stellar_type'])\n" ] @@ -511,7 +511,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 13, "id": "ec48125c-6bf5-48f4-9357-8261800b5d8b", "metadata": {}, "outputs": [ diff --git a/examples/notebook_population.ipynb b/examples/notebook_population.ipynb index 56f3b153c23064e82863f188906d50243e795715..b6a37baa8d90a2f36c0fd19311548ed49e77f173 100644 --- a/examples/notebook_population.ipynb +++ b/examples/notebook_population.ipynb @@ -3,7 +3,9 @@ { "cell_type": "markdown", "id": "bbbaafbb-fd7d-4b73-a970-93506ba35d71", - "metadata": {}, + "metadata": { + "tags": [] + }, "source": [ "# Running populations with binarycpython\n", "This notebook will show you how to evolve a population of stars\n", @@ -27,15 +29,6 @@ "# help(Population) # Uncomment to see the public functions of this object" ] }, - { - "cell_type": "markdown", - "id": "a081ab23-7822-4971-aa82-991548534714", - "metadata": {}, - "source": [ - "- running ensemble\n", - "- using M&S grid" - ] - }, { "cell_type": "markdown", "id": "f268eff3-4e08-4f6b-8b59-f22dba4d2074", @@ -329,7 +322,7 @@ ], "source": [ "# Add grid variables\n", - "resolution = {\"M_1\": 20, \"q\": 20, \"per\": 40}\n", + "resolution = {\"M_1\": 20}\n", "\n", "# Mass\n", "example_pop.add_grid_variable(\n", @@ -1125,7 +1118,7 @@ ")\n", "\n", "# Add grid variables\n", - "resolution = {\"M_1\": 5, \"q\": 5, \"per\": 5}\n", + "resolution = {\"M_1\": 3, \"q\": 3, \"per\": 3}\n", "\n", "# Mass\n", "example_pop.add_grid_variable(\n",