From 22049a497ce5da28ac3807d547ea07f163b183da Mon Sep 17 00:00:00 2001
From: David Hendriks <davidhendriks93@gmail.com>
Date: Sun, 23 Feb 2020 02:18:58 +0000
Subject: [PATCH] updated grid code to have a dedicated status printing
 function

---
 binarycpython/utils/grid.py                  | 47 +++++++++++++++++++-
 binarycpython/utils/grid_options_defaults.py |  6 +--
 tests/population/grid_tests.py               | 22 ++++-----
 3 files changed, 59 insertions(+), 16 deletions(-)

diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py
index 4fbfa7cff..7f9b6754a 100644
--- a/binarycpython/utils/grid.py
+++ b/binarycpython/utils/grid.py
@@ -39,6 +39,7 @@ from binarycpython.utils.functions import get_defaults, parse_binary_c_version_i
 # TODO: add functionality to return the nuclear_mass_list
 # TODO: add functionality to return the source_list
 # TODO: add functionality to return the ensemble_list
+# TODO: change the grid_options dict structure so that there is room for descriptions
 # DONE: add verbosity options
 
 # DONE: add functionality to return the evcode_version_string
@@ -778,6 +779,8 @@ class Population(object):
         #######################
         # MP run
         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_grid'] = time.time() # Setting start time of grid
+
 
         start_mp = time.time()
 
@@ -785,6 +788,7 @@ class Population(object):
 
         self.load_grid_function()
 
+        # def evolve_system(binary_cmdline_string):
         def evolve_system(binary_cmdline_string):
             out = binary_c_python_api.run_population(
                 binary_cmdline_string,
@@ -800,9 +804,11 @@ class Population(object):
                 full_system_dict.update(system)
 
                 binary_cmdline_string = self.return_argline(full_system_dict)
-                print("{}/{}".format(i+1, total_starcount_run), binary_cmdline_string)
+
+                self.print_info(i+1, total_starcount_run, full_system_dict)
+                # print("{}/{}".format(i+1, total_starcount_run), binary_cmdline_string)
                 yield binary_cmdline_string
-                # yield i
+
             print("generator done")
 
         # Create pool
@@ -817,6 +823,8 @@ class Population(object):
         p.join()
 
         stop_mp = time.time()
+        self.grid_options['start_time_grid'] = 0
+
 
         # Give feedback
         print(
@@ -1422,4 +1430,39 @@ class Population(object):
             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 = 10
+        else:
+            print_freq = 1
+
+
+        # Calculate amount of time left
+
+
+        # calculate amount of time passed
+        time_passed = time.time() - self.grid_options['start_time_grid'] 
+
+        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)
+
 ################################################################################################
diff --git a/binarycpython/utils/grid_options_defaults.py b/binarycpython/utils/grid_options_defaults.py
index 8210374b0..d37d6a1bd 100644
--- a/binarycpython/utils/grid_options_defaults.py
+++ b/binarycpython/utils/grid_options_defaults.py
@@ -40,12 +40,12 @@ grid_options_defaults_dict = {
     # binary
     "binary": 0,
     # Locations:
-    "tmp_dir": temp_dir(),
+    "tmp_dir": temp_dir(), # Setting the temp dir of the program
     # Probability:
     "weight": 1.0,  # weighting for the probability
     "repeat": 1.0,  # number of times to repeat each system (probability is adjusted to be 1/repeat)
-    "parse_function": None,
-
+    "parse_function": None, # 
+    "start_time_grid": 0, # Start time of the grid
     ##
     # return_array_refs=>1, # quicker data parsing mode
     # sort_args=>1,
diff --git a/tests/population/grid_tests.py b/tests/population/grid_tests.py
index a8947b521..e7e63f37c 100644
--- a/tests/population/grid_tests.py
+++ b/tests/population/grid_tests.py
@@ -294,19 +294,21 @@ test_pop.set(verbose=1,
     binary=0,
 )
 
+
+
+resolution = {'M_1': 10, 'per': 10}
+
 test_pop.add_grid_variable(
     name="M_1",
     longname="log primary mass",
     valuerange=[10, 100],
-    resolution="20",
-    spacingfunc="const(10, 100, 20)",
-    precode="",
+    resolution="{}".format(resolution['M_1']),
+    spacingfunc="const(10, 100, {})".format(resolution['M_1']),
     # precode="M_1=math.exp(lnm1)",
     probdist="flat(M_1)",
     # probdist='self.custom_options["extra_prob_function"](M_1)',
     dphasevol="dM_1",
     parameter_name="M_1",
-    condition="",
 )
 
 ### Grid generating test.
@@ -314,8 +316,8 @@ test_pop.add_grid_variable(
     name="period",
     longname="period",
     valuerange=["M_1", 20],
-    resolution="20",
-    spacingfunc="np.linspace(1, 10, 20)",
+    resolution="{}".format(resolution['per']),
+    spacingfunc="np.linspace(1, 10, {})".format(resolution['per']),
     precode="orbital_period = period**2",
     probdist="flat(orbital_period)",
     parameter_name="orbital_period",
@@ -324,12 +326,10 @@ test_pop.add_grid_variable(
 )
 
 test_pop.set(verbose=1, 
-    amt_cores=4,
+    amt_cores=1,
     binary=0,
 )
 
 
-test_pop.test_evolve_population_mp()
-
-# print(test_pop.grid_options["probtot"])
-# print(test_pop.grid_options["count"])
\ No newline at end of file
+# test_pop.test_evolve_population_mp()
+test_pop.evolve_population_mp_chunks()
-- 
GitLab