diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py index 52235de3fd9d08d052e62bac803ad654ed414a15..ed705648ed2e0fbc7c2cfb30e07a78a01b78f408 100644 --- a/binarycpython/utils/grid.py +++ b/binarycpython/utils/grid.py @@ -679,70 +679,6 @@ class Population(object): print("\n\nBinary_c output:") print(output) - ################################################### - # Unordered functions - ################################################### - - def remove_file(self, file, verbose): - """ - Function to remove files but with verbosity - """ - - if os.path.exists(file): - try: - if verbose > 0: - print("Removed {}".format(file)) - os.remove(file) - # TODO: Put correct exception here. - except: - print("Error while deleting file {}".format(file)) - raise FileNotFoundError - - def clean_up_custom_logging(self, evol_type): - """ - Function to clean up the custom logging. - Has two types: - 'single': - - removes the compiled shared library (which name is stored in grid_options['custom_logging_shared_library_file']) - - TODO: unloads/frees the memory allocated to that shared library (which is stored in grid_options['custom_logging_func_memaddr']) - - sets both to None - 'multiple': - - TODO: make this and design this - """ - - if evol_type == "single": - if self.grid_options["verbose"] > 0: - print("Cleaning up the custom logging stuff. type: single") - # TODO: Unset custom logging code - - # TODO: Unset function memory adress - # print(self.grid_options["custom_logging_func_memaddr"]) - - # remove shared library files - if self.grid_options["custom_logging_shared_library_file"]: - self.remove_file( - self.grid_options["custom_logging_shared_library_file"], - self.grid_options["verbose"], - ) - - if evol_type == "population": - if self.grid_options["verbose"] > 0: - print("Cleaning up the custom logging stuffs. type: population") - # TODO: make sure that these also work. not fully sure if necessary tho. whether its a single file, or a dict of files/memaddresses - - def increment_probtot(self, prob): - """ - Function to add to the total probability - """ - - self.grid_options["probtot"] += prob - - def increment_count(self): - """ - Function to add to the total amount of stars - """ - self.grid_options["count"] += 1 - ################################################### # Gridcode functions ################################################### @@ -1187,6 +1123,41 @@ class Population(object): total_starcount = system_generator(self) self.grid_options["total_starcount"] = total_starcount + def print_info(self, run_number, total_systems, full_system_dict): + """ + Function to print info about the current system and the progress of the grid. + + # color info tricks from https://ozzmaker.com/add-colour-to-text-in-python/ + https://stackoverflow.com/questions/287871/how-to-print-colored-text-in-terminal-in-python + """ + + # Define frequency + if self.grid_options["verbose"] == 1: + print_freq = 1 + else: + print_freq = 10 + + # Calculate amount of time left + + # calculate amount of time passed + time_passed = time.time() - self.grid_options["start_time_evolution"] + + if run_number % print_freq == 0: + binary_cmdline_string = self.return_argline(full_system_dict) + info_string = "{color_part_1} {text_part_1}{end_part_1}{color_part_2} {text_part_2}{end_part_2}".format( + color_part_1="\033[1;32;41m", + text_part_1="{}/{}".format(run_number, total_systems), + end_part_1="\033[0m", + color_part_2="\033[1;32;42m", + text_part_2="{}".format(binary_cmdline_string), + end_part_2="\033[0m", + ) + print(info_string) + + ################################################### + # Unordered functions + ################################################### + def write_binary_c_calls_to_file(self, output_dir=None, output_filename=None, include_defaults=False): """ Function that loops over the gridcode and writes the generated parameters to a file. In the form of a commandline call @@ -1247,36 +1218,6 @@ class Population(object): print("Error. No grid function found!") raise KeyError - def print_info(self, run_number, total_systems, full_system_dict): - """ - Function to print info about the current system and the progress of the grid. - - # color info tricks from https://ozzmaker.com/add-colour-to-text-in-python/ - https://stackoverflow.com/questions/287871/how-to-print-colored-text-in-terminal-in-python - """ - - # Define frequency - if self.grid_options["verbose"] == 1: - print_freq = 1 - else: - print_freq = 10 - - # Calculate amount of time left - - # calculate amount of time passed - time_passed = time.time() - self.grid_options["start_time_evolution"] - - if run_number % print_freq == 0: - binary_cmdline_string = self.return_argline(full_system_dict) - info_string = "{color_part_1} {text_part_1}{end_part_1}{color_part_2} {text_part_2}{end_part_2}".format( - color_part_1="\033[1;32;41m", - text_part_1="{}/{}".format(run_number, total_systems), - end_part_1="\033[0m", - color_part_2="\033[1;32;42m", - text_part_2="{}".format(binary_cmdline_string), - end_part_2="\033[0m", - ) - print(info_string) def cleanup_defaults(self): """ @@ -1298,5 +1239,67 @@ class Population(object): return cleaned_dict + def remove_file(self, file, verbose): + """ + Function to remove files but with verbosity + """ + + if os.path.exists(file): + try: + if verbose > 0: + print("Removed {}".format(file)) + os.remove(file) + # TODO: Put correct exception here. + except: + print("Error while deleting file {}".format(file)) + raise FileNotFoundError + + def clean_up_custom_logging(self, evol_type): + """ + Function to clean up the custom logging. + Has two types: + 'single': + - removes the compiled shared library (which name is stored in grid_options['custom_logging_shared_library_file']) + - TODO: unloads/frees the memory allocated to that shared library (which is stored in grid_options['custom_logging_func_memaddr']) + - sets both to None + 'multiple': + - TODO: make this and design this + """ + + if evol_type == "single": + if self.grid_options["verbose"] > 0: + print("Cleaning up the custom logging stuff. type: single") + # TODO: Unset custom logging code + + # TODO: Unset function memory adress + # print(self.grid_options["custom_logging_func_memaddr"]) + + # remove shared library files + if self.grid_options["custom_logging_shared_library_file"]: + self.remove_file( + self.grid_options["custom_logging_shared_library_file"], + self.grid_options["verbose"], + ) + + if evol_type == "population": + if self.grid_options["verbose"] > 0: + print("Cleaning up the custom logging stuffs. type: population") + # TODO: make sure that these also work. not fully sure if necessary tho. whether its a single file, or a dict of files/memaddresses + + def increment_probtot(self, prob): + """ + Function to add to the total probability + """ + + self.grid_options["probtot"] += prob + + def increment_count(self): + """ + Function to add to the total amount of stars + """ + self.grid_options["count"] += 1 + + + ################################################################################################