From d7d7f035c5629a0716ea5eaa3f27587eb029f1fd Mon Sep 17 00:00:00 2001 From: dh00601 <dh00601@surrey.ac.uk> Date: Sat, 4 Jun 2022 13:18:04 +0100 Subject: [PATCH] cleaning things for the sourcefile --- binarycpython/utils/grid.py | 37 +++++++++++++++----- binarycpython/utils/grid_options_defaults.py | 1 + 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py index 7dcd814f7..5228374a3 100644 --- a/binarycpython/utils/grid.py +++ b/binarycpython/utils/grid.py @@ -1116,6 +1116,10 @@ class Population: 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"] == "source_file": + # 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 @@ -1209,11 +1213,14 @@ class Population: if self.grid_options["verbosity"] >= _LOGGER_VERBOSITY_LEVEL: stream_logger.debug(f"setting up the system_queue_filler now") - # Setup of the generator + # Setup of the generator and do some # Check again if we use custom generator or not: if self.grid_options["evolution_type"] == "custom_generator": generator = self.grid_options["custom_generator"] - else: + elif self.grid_options["evolution_type"] == "source_file": + generator = self.grid_options["_system_generator"] + else: + # TODO: this might go somewhere else? self._generate_grid_code(dry_run=False) self._load_grid_function() @@ -2133,11 +2140,11 @@ class Population: # Source file elif self.grid_options["evolution_type"] == "source_file": - # TODO: fix this function - raise ValueError("This functionality is not available yet") - # Source file - elif self.grid_options["evolution_type"] == "montecarlo": + # load the source file + self._load_source_file() + + # Do a dry run if wanted if self.grid_options["do_dry_run"]: # Do a dry run self._dry_run_source_file() @@ -2157,7 +2164,8 @@ class Population: "_start_time_evolution" ] = time.time() # Setting start time of grid - # + # Source file + elif self.grid_options["evolution_type"] == "montecarlo": # TODO: fix this function raise ValueError("This functionality is not available yet") @@ -3041,9 +3049,14 @@ class Population: ) raise ValueError + # Get filehandle source_file_filehandle = open(self.grid_options["source_file_filename"], "r") - self.grid_options["_system_generator"] = source_file_filehandle + # Create a generator that returns dictionaries + source_file_generator = self._create_generator_for_sourcefile(source_file_filehandle) + + # Set this as the generator + self.grid_options["_system_generator"] = source_file_generator verbose_print("Source file loaded", self.grid_options["verbosity"], 1) @@ -3066,6 +3079,14 @@ class Population: return arg_dict + def _create_generator_for_sourcefile(self, filehandle): + """ + Function to create a generator for handling systems from a sourcefile + """ + + for line in filehandle: + yield self._dict_from_line_source_file(line) + ################################################### # SLURM functions # diff --git a/binarycpython/utils/grid_options_defaults.py b/binarycpython/utils/grid_options_defaults.py index 68db0f4b9..02ecbf34f 100644 --- a/binarycpython/utils/grid_options_defaults.py +++ b/binarycpython/utils/grid_options_defaults.py @@ -102,6 +102,7 @@ grid_options_defaults_dict = { "_evolution_type_options": [ "grid", "custom_generator", + "source_file", ], # 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) -- GitLab