diff --git a/binarycpython/tests/python_API_test.py b/binarycpython/tests/python_API_test.py index 8bbe87204438e34ef4b4013e425ddac3b04a4557..510141312dfe9a0ad9c574001aa833bea1e9df4b 100755 --- a/binarycpython/tests/python_API_test.py +++ b/binarycpython/tests/python_API_test.py @@ -42,6 +42,7 @@ def test_run_system(): print("Binary_c output:") print(textwrap.indent(output, "\t")) + def test_run_system_with_log(): m1 = 15.0 # Msun m2 = 14.0 # Msun @@ -77,7 +78,9 @@ def test_run_system_with_custom_logging(): # generate logging lines. Here you can choose whatever you want to have logged, and with what header # this generates working print statements logging_line = autogen_C_logging_code( - {"MY_STELLAR_DATA": ["model.time", "star[0].mass"],} + { + "MY_STELLAR_DATA": ["model.time", "star[0].mass"], + } ) # Generate entire shared lib code around logging lines diff --git a/binarycpython/tests/test_c_bindings.py b/binarycpython/tests/test_c_bindings.py index 5021bcf5cafb63829fd1537e6b9a2d4fc8dab4fc..35c8c8fdacfc239cd990cf3278ad7bdc6dc0c11c 100644 --- a/binarycpython/tests/test_c_bindings.py +++ b/binarycpython/tests/test_c_bindings.py @@ -45,10 +45,12 @@ def test_run_system(): assert "SINGLE_STAR_LIFETIME" in output, "Run system not working properly" + ####################################################################################################################################################### ### memaddr test ####################################################################################################################################################### + def test_return_store_memaddr(): output = _binary_c_bindings.return_store_memaddr() @@ -67,6 +69,7 @@ def test_unload_store_memaddr(): _ = _binary_c_bindings.free_store_memaddr(output) print("freed store memaddr") + ####################################################################################################################################################### ### ensemble tests ####################################################################################################################################################### @@ -116,6 +119,7 @@ ensemble_filters_off {8} ensemble_filter_{9} 1 probability 0.1" return argstring + def test_minimal_ensemble_output(): """ Tase case to check if the ensemble output is correctly written to the buffer instead of printed @@ -146,8 +150,13 @@ def test_minimal_ensemble_output(): print(test_json.keys()) print(test_json) - assert test_json, "Ensemble output not correctly written passed to the buffer in _binary_c_bindings" - assert "number_counts" in test_json.keys(), "Output doesn't contain the correct information" + assert ( + test_json + ), "Ensemble output not correctly written passed to the buffer in _binary_c_bindings" + assert ( + "number_counts" in test_json.keys() + ), "Output doesn't contain the correct information" + def test_return_persistent_data_memaddr(): """ @@ -298,8 +307,10 @@ def test_adding_ensemble_output(): ) # Have the persistent_memory adress be released and have the json outputted - test_2_output = _binary_c_bindings.free_persistent_data_memaddr_and_return_json_output( - test_2_persistent_data_memaddr + test_2_output = ( + _binary_c_bindings.free_persistent_data_memaddr_and_return_json_output( + test_2_persistent_data_memaddr + ) ) test_2_ensemble_json = [ line for line in test_2_output.splitlines() if line.startswith("ENSEMBLE_JSON") @@ -365,6 +376,7 @@ def test_adding_ensemble_output(): ), assert_message_1 # assert inspect_dict(test_1_merged_dict, print_structure=False) == inspect_dict(test_3_json, print_structure=False), assert_message_2 + def test_free_and_json_output(): """ Function that tests the freeing of the memory adress and the output of the json @@ -387,8 +399,10 @@ def test_free_and_json_output(): ) # Free memory adress - json_output_by_freeing = _binary_c_bindings.free_persistent_data_memaddr_and_return_json_output( - persistent_data_memaddr + json_output_by_freeing = ( + _binary_c_bindings.free_persistent_data_memaddr_and_return_json_output( + persistent_data_memaddr + ) ) # print(textwrap.indent(str(json_output_by_freeing), "\t")) @@ -398,6 +412,7 @@ def test_free_and_json_output(): assert "number_counts" in parsed_json.keys(), "Output not correct" + def test_combine_with_empty_json(): """ Test for merging with an empty dict @@ -412,12 +427,12 @@ def test_combine_with_empty_json(): ensemble_jsons_1[0][len("ENSEMBLE_JSON ") :] ) - assert_message = ( "combining output json with empty dict should give same result as initial json" ) assert merge_dicts(json_1, {}) == json_1, assert_message + ############# def test_full_ensemble_output(): """ @@ -431,7 +446,9 @@ def test_full_ensemble_output(): ensemble_jsons_1 = [ line for line in output_1.splitlines() if line.startswith("ENSEMBLE_JSON") ] - json_1 = handle_ensemble_string_to_json(ensemble_jsons_1[0][len("ENSEMBLE_JSON ") :]) + json_1 = handle_ensemble_string_to_json( + ensemble_jsons_1[0][len("ENSEMBLE_JSON ") :] + ) # assert statements: assert "number_counts" in json_1.keys() @@ -441,6 +458,7 @@ def test_full_ensemble_output(): assert "distributions" in json_1.keys() assert "scalars" in json_1.keys() + def all(): test_run_system() test_return_store_memaddr() @@ -464,4 +482,4 @@ if __name__ == "__main__": # test_free_and_json_output() # test_combine_with_empty_json() all() - print("Done") \ No newline at end of file + print("Done") diff --git a/binarycpython/tests/test_custom_logging.py b/binarycpython/tests/test_custom_logging.py index 7554d6b56995d6e214aa1900c692d872d1810b9b..d105487ba25eaabe93fe7c0c627b6257d0377c5c 100644 --- a/binarycpython/tests/test_custom_logging.py +++ b/binarycpython/tests/test_custom_logging.py @@ -1,2 +1 @@ from binarycpython.utils.custom_logging_functions import * - diff --git a/binarycpython/tests/test_functions.py b/binarycpython/tests/test_functions.py index 387a1e15a85905c2cf4cb45847003ce2b4f630f4..150e337e2e4583ae8974716d287f3f6abc0e74b9 100644 --- a/binarycpython/tests/test_functions.py +++ b/binarycpython/tests/test_functions.py @@ -3,6 +3,7 @@ from binarycpython.utils.functions import * ############################# # Script that contains unit tests for functions from the binarycpython.utils.functions file + def test_get_help_super(): """ Function to test the get_help_super function diff --git a/binarycpython/utils/custom_logging_functions.py b/binarycpython/utils/custom_logging_functions.py index 1f1dc60bf93f0afc12609f9b83cffda39621a406..826f07ad767cf8ce8b2247a775fb3a5f08c9a85f 100644 --- a/binarycpython/utils/custom_logging_functions.py +++ b/binarycpython/utils/custom_logging_functions.py @@ -28,7 +28,7 @@ def autogen_C_logging_code(logging_dict, verbose=0): 'model.dt' ] } - + """ # Check if the input is of the correct form @@ -256,13 +256,15 @@ def compile_shared_lib(code, sourcefile_name, outfile_name, verbose=0): compilation_dict = return_compilation_dict(verbose) # Construct full command - command = "{cc} -fPIC {ccflags} {libs} -o {outfile_name} {sourcefile_name} {inc}".format( - cc=compilation_dict["cc"], - ccflags=compilation_dict["ccflags"], - libs=compilation_dict["libs"], - outfile_name=outfile_name, - sourcefile_name=sourcefile_name, - inc=compilation_dict["inc"], + command = ( + "{cc} -fPIC {ccflags} {libs} -o {outfile_name} {sourcefile_name} {inc}".format( + cc=compilation_dict["cc"], + ccflags=compilation_dict["ccflags"], + libs=compilation_dict["libs"], + outfile_name=outfile_name, + sourcefile_name=sourcefile_name, + inc=compilation_dict["inc"], + ) ) # remove extra whitespaces: @@ -320,7 +322,8 @@ def create_and_load_logging_function(custom_logging_code, verbose=0): _ = ctypes.CDLL("libbinary_c.so", mode=ctypes.RTLD_GLOBAL) libcustom_logging = ctypes.CDLL( - library_name, mode=ctypes.RTLD_GLOBAL, + library_name, + mode=ctypes.RTLD_GLOBAL, ) # loads the shared library # Get memory adress of function. mimicking a pointer diff --git a/binarycpython/utils/distribution_functions.py b/binarycpython/utils/distribution_functions.py index 045d632fe263cc6ce66bc9e698b036afed17d0b8..ad14543f3b96eddad67c8634ea0c88c3d4cf94d0 100644 --- a/binarycpython/utils/distribution_functions.py +++ b/binarycpython/utils/distribution_functions.py @@ -518,7 +518,7 @@ def sana12(M1, M2, a, P, amin, amax, x0, x1, p): def Izzard2012_period_distribution(P, M1, log10Pmin=1): """ - period distribution which interpolates between + period distribution which interpolates between Duquennoy and Mayor 1991 at low mass (G/K spectral type <~1.15Msun) and Sana et al 2012 at high mass (O spectral type >~16.3Msun) @@ -597,7 +597,7 @@ def Izzard2012_period_distribution(P, M1, log10Pmin=1): def interpolate_in_mass_izzard2012(M, high, low): """ Function to interpolate in mass - + high: at M=16.3 low: at 1.15 """ diff --git a/binarycpython/utils/functions.py b/binarycpython/utils/functions.py index 798cfc01adddf790d70d8c89587eff0e354d6cc5..de8ba71d4acee1e98ace32c84f7a7dbce7b931f5 100644 --- a/binarycpython/utils/functions.py +++ b/binarycpython/utils/functions.py @@ -314,7 +314,9 @@ def parse_binary_c_version_info(version_info_string): split_info = el.split("Nucleosynthesis source")[-1].strip().split(" is ") nucsyn_sources_dict[int(split_info[0])] = split_info[-1] - version_info_dict["nucleosynthesis_sources"] = nucsyn_sources_dict if nucsyn_sources_dict else None + version_info_dict["nucleosynthesis_sources"] = ( + nucsyn_sources_dict if nucsyn_sources_dict else None + ) ########################## # miscellaneous: @@ -329,26 +331,25 @@ def parse_binary_c_version_info(version_info_string): git_branch = [el for el in cleaned if el.startswith("git branch")] misc_dict["git_branch"] = git_branch[0].split("git branch ")[-1].replace('"', "") - cleaned = cleaned-set(git_branch) + cleaned = cleaned - set(git_branch) build = [el for el in cleaned if el.startswith("Build")] misc_dict["build"] = build[0].split("Build: ")[-1].replace('"', "") - cleaned = cleaned-set(build) + cleaned = cleaned - set(build) email = [el for el in cleaned if el.startswith("Email")] misc_dict["email"] = email[0].split("Email ")[-1].split(",") - cleaned = cleaned-set(email) + cleaned = cleaned - set(email) - other_items = set([el for el in cleaned if ' is ' in el]) + other_items = set([el for el in cleaned if " is " in el]) cleaned = cleaned - other_items for el in other_items: - split = el.split(' is ') + split = el.split(" is ") key = split[0].strip() - val = ' is '.join(split[1:]).strip() + val = " is ".join(split[1:]).strip() misc_dict[key] = val - misc_dict["uncaught"] = "\n".join(cleaned) version_info_dict["miscellaneous"] = misc_dict if misc_dict else None @@ -748,7 +749,9 @@ def get_help_super(print_help=False, fail_silently=True): # Get detailed help info detailed_help = get_help( - parameter_name, print_help=False, fail_silently=fail_silently, + parameter_name, + print_help=False, + fail_silently=fail_silently, ) if detailed_help: @@ -972,7 +975,7 @@ class binarycDecoder(json.JSONDecoder): class BinaryCEncoder(json.JSONEncoder): def default(self, o): - print('inarycoij') + print("inarycoij") try: str_repr = str(o) except TypeError: @@ -982,6 +985,7 @@ class BinaryCEncoder(json.JSONEncoder): # Let the base class default method raise the TypeError return JSONEncoder.default(self, o) + def binaryc_json_serializer(obj): """ Custom serializer for binary_c to use when functions are present in the dictionary @@ -995,6 +999,7 @@ def binaryc_json_serializer(obj): else: return obj + def handle_ensemble_string_to_json(raw_output): """ Function that deals with the raw output of the ensemble and diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py index 356774313d8fc9085734416304a194efa1004db0..e647a0f259ad33ac249d97ed848d211cac1fbe08 100644 --- a/binarycpython/utils/grid.py +++ b/binarycpython/utils/grid.py @@ -40,7 +40,7 @@ from binarycpython.utils.functions import ( verbose_print, binarycDecoder, merge_dicts, - BinaryCEncoder + BinaryCEncoder, ) from binarycpython.utils.hpc_functions import ( get_condor_version, @@ -274,8 +274,8 @@ class Population: Each of the grid variables will get create a deeper for loop. The real function that generates the numbers will get written to a new file in the TMP_DIR, and then loaded imported and evaluated. - beware that if you insert some destructive piece of code, it will be executed anyway. Use at own risk. - + beware that if you insert some destructive piece of code, it will be executed anyway. Use at own risk. + name: name of parameter. This is evaluated as a parameter and you can use it throughout the rest of the function example: name = 'lnm1' @@ -420,7 +420,7 @@ class Population: TODO: theres flawed logic here. rewrite this part pls - TODO: consider actually just removing the whole 'output to file' part and let the user do this. + TODO: consider actually just removing the whole 'output to file' part and let the user do this. """ all_info = self.return_all_info( @@ -459,7 +459,8 @@ class Population: with open(settings_fullname, "w") as file: file.write( json.dumps( - all_info_cleaned, indent=4, + all_info_cleaned, + indent=4, default=binaryc_json_serializer, ) ) @@ -469,7 +470,7 @@ class Population: self.grid_options["verbosity"], 1, ) - if not outfile.endswith('json'): + if not outfile.endswith("json"): verbose_print( "Error: outfile ({}) must end with .json".format(outfile), self.grid_options["verbosity"], @@ -607,7 +608,7 @@ class Population: def evolve(self): """ - Entrypoint function of the whole object. From here, based on the settings, + Entrypoint function of the whole object. From here, based on the settings, we set up a SLURM or CONDOR grid, or if no setting is given we go straight to evolving the population """ @@ -633,7 +634,7 @@ class Population: - to evolve a population via multiprocessing or linearly on 1 core. - to evolve a population via a variable grid, a source file or MC - TODO: include options for different ways of generating a population here. + TODO: include options for different ways of generating a population here. """ ## @@ -1131,7 +1132,9 @@ class Population: code_string += ( indent * (depth + 1) + "{}".format( - grid_variable["precode"].replace("\n", "\n" + indent * (depth+1)) + grid_variable["precode"].replace( + "\n", "\n" + indent * (depth + 1) + ) ) + "\n" ) @@ -1191,7 +1194,9 @@ class Population: code_string += ( indent * (depth + 1) - + "starcounts[{}] += 1".format(grid_variable["grid_variable_number"],) + + "starcounts[{}] += 1".format( + grid_variable["grid_variable_number"], + ) + "\n" ) @@ -1666,14 +1671,18 @@ class Population: ) slurm_script_contents += '# set status to "running"\n' - slurm_script_contents += 'echo "running" > {}/status/$jobid.$jobarrayindex\n\n'.format( - self.grid_options["slurm_dir"] + slurm_script_contents += ( + 'echo "running" > {}/status/$jobid.$jobarrayindex\n\n'.format( + self.grid_options["slurm_dir"] + ) ) slurm_script_contents += "# run grid of stars\n" slurm_script_contents += "{}\n\n".format(command) slurm_script_contents += '# set status to "finished"\n' - slurm_script_contents += 'echo "finished" > {}/status/$jobid.$jobarrayindex\n'.format( - self.grid_options["slurm_dir"] + slurm_script_contents += ( + 'echo "finished" > {}/status/$jobid.$jobarrayindex\n'.format( + self.grid_options["slurm_dir"] + ) ) slurm_script_contents += "\n" @@ -1745,7 +1754,7 @@ class Population: Which stage is used is determined by the value of grid_options['condor_command']: - <empty>: the function will know its the user that executed the script and + <empty>: the function will know its the user that executed the script and it will set up the necessary condor stuff 'evolve': evolve_population is called to evolve the population of stars diff --git a/binarycpython/utils/plot_functions.py b/binarycpython/utils/plot_functions.py index e18d26a50e7483711a3f71e73fc1b3e5889fefc4..e40e1514978e37664c3f93001a7328a6d720c3d0 100644 --- a/binarycpython/utils/plot_functions.py +++ b/binarycpython/utils/plot_functions.py @@ -255,7 +255,7 @@ def plot_orbit(df, show_stellar_types=False, show_plot=True): def plot_masses(df, show_stellar_types=False, show_plot=True): """ Function to plot the masses of the system. - + Function requires the following keys: - time - pms_mass_1 diff --git a/examples/example_population.py b/examples/example_population.py index 3c1dd6393563853c8095984b2eba86691d0e02c2..3316f5fe5ee56f96b986e2ac61a3f474abf6d588 100644 --- a/examples/example_population.py +++ b/examples/example_population.py @@ -17,7 +17,7 @@ from binarycpython.utils.custom_logging_functions import temp_dir ## Quick script to get some output def output_lines(output): """ - Function that outputs the lines that were recieved from the binary_c run. + Function that outputs the lines that were recieved from the binary_c run. """ return output.splitlines() diff --git a/examples/examples.py b/examples/examples.py index 7d306e3a18ae12f8645765574beb320275ac8b59..c15add4afb42129b6fd58b27caadabc32a8f01f7 100644 --- a/examples/examples.py +++ b/examples/examples.py @@ -21,7 +21,7 @@ Use these as inspiration/base. def run_example_binary(): """ - Function to run a binary system. Very basic approach which directly adresses the run_system(..) python-c wrapper function. + Function to run a binary system. Very basic approach which directly adresses the run_system(..) python-c wrapper function. """ m1 = 15.0 # Msun @@ -53,16 +53,16 @@ run_example_binary() def run_example_binary_with_run_system(): """ - This function serves as an example on the function run_system and parse_output. + This function serves as an example on the function run_system and parse_output. There is more functionality with this method and several tasks are done behind the scene. Requires pandas, numpy to run. run_system: mostly just makes passing arguments to the function easier. It also loads all the necessary defaults in the background - parse_output: Takes the raw output of binary_c and selects those lines that start with the given header. + parse_output: Takes the raw output of binary_c and selects those lines that start with the given header. Note, if you dont use the custom_logging functionality binary_c should be configured to have output that starts with that given header - The parsing of the output only works correctly if either all of the values are described inline like `mass=<number>' or none of them are. + The parsing of the output only works correctly if either all of the values are described inline like `mass=<number>' or none of them are. """ import pandas as pd @@ -156,7 +156,7 @@ def run_example_custom_logging_autogenerated(): def run_example_binary_with_custom_logging(): """ - Function that will use a automatically generated piece of logging code. Compile it, load it + Function that will use a automatically generated piece of logging code. Compile it, load it into memory and run a binary system. See run_system on how several things are done in the background here. """ @@ -166,7 +166,9 @@ def run_example_binary_with_custom_logging(): # generate logging lines. Here you can choose whatever you want to have logged, and with what header # this generates working print statements logging_line = autogen_C_logging_code( - {"MY_STELLAR_DATA": ["model.time", "star[0].mass"],} + { + "MY_STELLAR_DATA": ["model.time", "star[0].mass"], + } ) # OR # You can also decide to `write` your own logging_line, which allows you to write a more complex logging statement with conditionals. diff --git a/setup.py b/setup.py index a08af5105cb4e7b34c63ff115b29dbff47fd80ec..709d7b7b98c7b825ec34a5b20d35cf95bb189b7a 100644 --- a/setup.py +++ b/setup.py @@ -46,7 +46,7 @@ def check_version(installed_binary_c_version, required_binary_c_versions): def execute_make(): """ - Function to execute the makefile. + Function to execute the makefile. This makefile builds the binary_c_python_api library that python will use to interface wth """