diff --git a/binarycpython/utils/functions.py b/binarycpython/utils/functions.py index f7e4532b73130630798e868a4e0f5643901c1c7f..fc107281a492f6fee8ab4148cc45a5c938396137 100644 --- a/binarycpython/utils/functions.py +++ b/binarycpython/utils/functions.py @@ -2066,7 +2066,7 @@ def binaryc_json_serializer(obj: Any) -> Any: Either string representation of object if the object is a function, or the object itself """ - if inspect.isfunction(obj) or isinstance(obj, py_rinterpolate.Rinterpolate): + if inspect.isfunction(obj) or isinstance(obj, py_rinterpolate.Rinterpolate) or inspect.isgenerator(obj): return str(obj) return obj diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py index fa949ec7bcbc4c8324c25e829a11706d3f2f00d5..ef574fb7daf7d750e6257217c393dc0ce1d198ff 100644 --- a/binarycpython/utils/grid.py +++ b/binarycpython/utils/grid.py @@ -37,6 +37,8 @@ from typing import Union, Any from collections import ( OrderedDict, ) +from collections.abc import Iterable # drop `.abc` with Python 2.7 or lower + import setproctitle import py_rinterpolate @@ -943,6 +945,10 @@ class Population: ): if self.grid_options["evolution_type"] == "grid": self._evolve_population_grid() + elif self.grid_options["evolution_type"] == "custom_generator": + # Use the same as the normal grid evolution but just a different generator + self._evolve_population_grid() + # elif self.grid_options["evolution_type"] == "mc": # # TODO: add MC option else: @@ -1031,11 +1037,15 @@ class Population: stream_logger.debug(f"setting up the system_queue_filler now") # Setup of the generator - self._generate_grid_code(dry_run=False) + # Check again if we use custom generator or not: + if self.grid_options["evolution_type"] == "custom_generator": + generator = self.grid_options['custom_generator'] + else: + self._generate_grid_code(dry_run=False) - self._load_grid_function() + self._load_grid_function() - generator = self.grid_options["_system_generator"](self, print_results=False) + generator = self.grid_options["_system_generator"](self, print_results=False) # TODO: build in method to handle with the HPC. # Continuously fill the queue @@ -1050,20 +1060,6 @@ class Population: 2, ) - # Print current size - # print("Current size: {}".format(save_que.qsize())) - - # if system_number%10==0: - # print("system_queue_filler: system_number: {}".format(system_number)) - # bytes_size_Moecache = get_size(Moecache) - # print("\tsystem_queue_filler: Size moecache: {}".format(convert_bytes(bytes_size_Moecache))) - - # bytes_size_distribution_constants = get_size(distribution_constants) - # print("\tsystem_queue_filler: Size distribution_constants: {}".format(convert_bytes(bytes_size_distribution_constants))) - - # bytes_size_self = get_size(dir(self)) - # print("\tsystem_queue_filler: Size dir(self): {}".format(convert_bytes(bytes_size_self))) - # Send closing signal to workers. When they receive this they will terminate if self.grid_options['verbosity'] >= _LOGGER_VERBOSITY_LEVEL: stream_logger.debug(f"Signaling stop to processes") # DEBUG @@ -1426,7 +1422,7 @@ class Population: # Check option to ignore 0 probability systems if not self.grid_options["run_zero_probability_system"]: - if full_system_dict['probability'] == 0: + if full_system_dict.get('probability', 1) == 0: run_system = False zero_prob_stars_skipped += 1 @@ -1466,7 +1462,7 @@ class Population: # Tallying system information # Keep track of systems: - probability_of_systems_run += full_system_dict["probability"] + probability_of_systems_run += full_system_dict.get("probability", 1) number_of_systems_run += 1 localcounter += 1 @@ -1479,7 +1475,7 @@ class Population: ) total_mass_run += total_mass_system total_probability_weighted_mass_run += ( - total_mass_system * full_system_dict["probability"] + total_mass_system * full_system_dict.get("probability", 1) ) # Set status to finishing @@ -1699,9 +1695,6 @@ class Population: msg = "No actual evolution options passed to the evolve call. Aborting" raise ValueError(msg) - - - def _setup(self): """ Function to set up the necessary stuff for the population evolution. @@ -1742,8 +1735,6 @@ class Population: # ### Custom logging code: self._set_custom_logging() - # ### Load store: Make sure this actually works. - ### ensemble: make some checks for this ## check the settings and set all the warnings. if self.bse_options.get("ensemble", None): @@ -1780,11 +1771,14 @@ class Population: ) raise ValueError - # Check which type of population generation + # Unset some value + self.grid_options["_probtot"] = 0 + + ## Check which type of population generation + # grid type if self.grid_options["evolution_type"] == "grid": ####################### # Dry run and getting starcount - self.grid_options["_probtot"] = 0 # Put in check if len(self.grid_options["_grid_variables"]) == 0: @@ -1808,60 +1802,36 @@ class Population: ) ) - ####################### - # Reset values and prepare the grid function - self.grid_options[ - "_probtot" - ] = 0 # To make sure that the values are reset. TODO: fix this in a cleaner way - self.grid_options[ - "_start_time_evolution" - ] = time.time() # Setting start time of grid - - # # Making sure the loaded grid code isn't lingering in the main PID - # self._generate_grid_code(dry_run=False) - - # # - # self._load_grid_function() - # self.grid_options["_system_generator"] = None + # user-provided custom generator + if self.grid_options["evolution_type"] == "custom_generator": + if not isinstance(self.grid_options['custom_generator'], Iterable): + print("Error. provided no or wrong custom value for the system generator (custom_generator: {})".format(self.grid_options['custom_generator'])) + raise ValueError + + # NOTE: In the part above i apparently have moved the load grid function call to another part. Now i wonder if that was useful, because it would be best if that is handled in this function + # TODO: place the load grid function back to the part above + # Source file elif self.grid_options["evolution_type"] == "source_file": - ####################### - # Dry run and getting starcount - self.grid_options["_probtot"] = 0 - - # Load the grid code # TODO: fix this function - # self._load_source_file_function() - - if self.grid_options['do_dry_run']: - # Do a dry run - self._dry_run_source_file() - - print( - "Total starcount for this run will be: {}".format( - self.grid_options["_total_starcount"] - ) - ) + raise ValueError("This functionality is not available yet") - ####################### - # Reset values and prepare the grid function - self.grid_options[ - "_probtot" - ] = 0 # To make sure that the values are reset. TODO: fix this in a cleaner way - self.grid_options[ - "_start_time_evolution" - ] = time.time() # Setting start time of grid - - # + # Source file + elif self.grid_options["evolution_type"] == "montecarlo": # TODO: fix this function - # self._load_source_file_function() - # self._load_source_file_function(dry_run=False) + raise ValueError("This functionality is not available yet") - # - self._load_grid_function() + ####################### + # Reset values and prepare the grid function + self.grid_options[ + "_probtot" + ] = 0 # To make sure that the values are reset. TODO: fix this in a cleaner way + self.grid_options[ + "_start_time_evolution" + ] = time.time() # Setting start time of grid def _cleanup(self): """ diff --git a/binarycpython/utils/grid_options_defaults.py b/binarycpython/utils/grid_options_defaults.py index 00b3e0d07b42c91437b2b9b0184b1825c8063ed4..32c1afabb897ed2a3bed82715aa7b6323dc3568c 100644 --- a/binarycpython/utils/grid_options_defaults.py +++ b/binarycpython/utils/grid_options_defaults.py @@ -43,6 +43,7 @@ grid_options_defaults_dict = { "_zero_prob_stars_skipped": 0, "ensemble_factor_in_probability_weighted_mass": False, # Whether to multiply the ensemble results by 1/probability_weighted_mass "do_dry_run": True, # Whether to do a dry run to calculate the total probability for this run + "custom_generator": None, # Place for the custom system generator ########################## # Execution log: ########################## @@ -95,7 +96,8 @@ grid_options_defaults_dict = { ## General "evolution_type": "grid", # Flag for type of population evolution "_evolution_type_options": [ - "grid", + "grid", + "custom_generator", ], # available choices for type of population evolution. # TODO: fill later with Monte Carlo, source file "_system_generator": None, # value that holds the function that generates the system # (result of building the grid script)