From 9aff15b5b94364c61cbb567f0321c73c40dafe73 Mon Sep 17 00:00:00 2001
From: David Hendriks <davidhendriks93@gmail.com>
Date: Mon, 28 Jun 2021 17:21:14 +0100
Subject: [PATCH] Added the manipulation of the ensemble dictionaries to the
 grid code

---
 binarycpython/utils/distribution_functions.py |  1 -
 binarycpython/utils/grid.py                   | 51 +++++++++++++++++++
 2 files changed, 51 insertions(+), 1 deletion(-)

diff --git a/binarycpython/utils/distribution_functions.py b/binarycpython/utils/distribution_functions.py
index 0fb0037c6..0b0e488c2 100644
--- a/binarycpython/utils/distribution_functions.py
+++ b/binarycpython/utils/distribution_functions.py
@@ -1268,7 +1268,6 @@ def build_q_table(options, m, p, verbosity=0):
                 Moecache["rinterpolator_q_given_{}_log10{}".format(m, p)].destroy()
                 gc.collect()
 
-
     #
     if not incache:
         # trim and/or expand the table to the range $qmin to $qmax.
diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py
index 53f9f66c1..bd592001f 100644
--- a/binarycpython/utils/grid.py
+++ b/binarycpython/utils/grid.py
@@ -64,8 +64,12 @@ from binarycpython.utils.functions import (
     update_dicts,
     extract_ensemble_json_from_string,
     get_moe_distefano_dataset,
+    recursive_change_key_to_float,
+    custom_sort_dict,
+    recursive_change_key_to_string,
 )
 
+
 # from binarycpython.utils.hpc_functions import (
 #     get_condor_version,
 #     get_slurm_version,
@@ -984,6 +988,30 @@ class Population:
             if result_queue.empty():
                 break
 
+        # Extra ensemble result manipulation:
+        # We need to reformat and multiply by a factor
+        original_ensemble_results = combined_output_dict['ensemble_results']
+
+        float_format_ensemble_results = recursive_change_key_to_float(original_ensemble_results)
+        del original_ensemble_results
+        gc.collect()
+
+        # Then sort the dictionary
+        sorted_ensemble_results = custom_sort_dict(float_format_ensemble_results)
+        del float_format_ensemble_results
+        gc.collect()
+
+        # Then Change the keys back to a string but with a %g format.
+        reformatted_ensemble_results = recursive_change_key_to_string(sorted_ensemble_results)
+        del sorted_ensemble_results
+        gc.collect()
+
+        # Put back in the dictionary
+        combined_output_dict['ensemble_results'] = reformatted_ensemble_results
+        gc.collect()
+
+        # Multiply the value by a factor:
+
         # Put the values back as object properties
         self.grid_results = combined_output_dict["results"]
         self.grid_ensemble_results = combined_output_dict[
@@ -1314,6 +1342,28 @@ class Population:
                     ensemble_raw_output
                 )  # Load this into a dict so that we can combine it later
 
+                # Extra ensemble result manipulation:
+                # We need to reformat and multiply by a factor
+                original_ensemble_results = ensemble_json["ensemble"]
+
+                float_format_ensemble_results = recursive_change_key_to_float(original_ensemble_results)
+                del original_ensemble_results
+                gc.collect()
+
+                # Then sort the dictionary
+                sorted_ensemble_results = custom_sort_dict(float_format_ensemble_results)
+                del float_format_ensemble_results
+                gc.collect()
+
+                # Then Change the keys back to a string but with a %g format.
+                reformatted_ensemble_results = recursive_change_key_to_string(sorted_ensemble_results)
+                del sorted_ensemble_results
+                gc.collect()
+
+                # Put back in the dictionary
+                ensemble_json["ensemble"] = reformatted_ensemble_results
+                gc.collect()
+
             else:
                 # If we do not allow this, automatically we will export this to the data_dir, in
                 # some formatted way
@@ -1424,6 +1474,7 @@ class Population:
             1,
         )
         self._clean_interpolators()
+        gc.collect()
 
         return
 
-- 
GitLab