From fbacf34ba84fce361c048b01f3905b3878da3d59 Mon Sep 17 00:00:00 2001
From: Robert Izzard <r.izzard@surrey.ac.uk>
Date: Mon, 1 Aug 2022 17:12:11 +0100
Subject: [PATCH] update VERSION to 0.9.5

install.sh now uses "-y" to skip questions about uninstall

ensemble.py's load_ensemble() now has a 'quiet' options to stop the spinner being shown on the command line : useful when this is run in a script

grid.py's Xinit removal code has been cleaned up into a try/except instead of a load of nested checks on the dict
---
 VERSION                         |  2 +-
 binarycpython/utils/ensemble.py | 87 +++++++++++++++++++++------------
 binarycpython/utils/grid.py     |  8 ++-
 install.sh                      |  2 +-
 4 files changed, 60 insertions(+), 39 deletions(-)

diff --git a/VERSION b/VERSION
index a602fc9e2..b0bb87854 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-0.9.4
+0.9.5
diff --git a/binarycpython/utils/ensemble.py b/binarycpython/utils/ensemble.py
index 4c27268e0..a1d3e7b5c 100644
--- a/binarycpython/utils/ensemble.py
+++ b/binarycpython/utils/ensemble.py
@@ -115,7 +115,12 @@ def ensemble_file_type(filename):
 
 
 def load_ensemble(
-    filename, convert_float_keys=True, select_keys=None, timing=False, flush=False
+        filename,
+        convert_float_keys=True,
+        select_keys=None,
+        timing=False,
+        flush=False,
+        quiet=False
 ):
     """
     Function to load an ensemeble file, even if it is compressed,
@@ -129,7 +134,8 @@ def load_ensemble(
     # open the file
 
     # load with some info to the terminal
-    print("Loading JSON...", flush=flush)
+    if quiet == False:
+        print("Loading JSON...", flush=flush)
 
     # open the ensemble and get the file type
     file_object = open_ensemble(filename)
@@ -142,47 +148,64 @@ def load_ensemble(
         )
         sys.exit()
 
-    with Halo(text="Loading", interval=250, spinner="moon", color="yellow"):
+    if quiet == True:
         tstart = time.time()
-        _loaded = False
-
-        def _hook(obj):
-            """
-            Hook to load ensemble
-            """
-
-            nonlocal _loaded
-            if _loaded is False:
-                _loaded = True
-                print(
-                    "\nLoaded {} data, now putting in a dictionary".format(filetype),
-                    flush=True,
-                )
-            return obj
-
         if filetype == "JSON":
-            # orjson promises to be fast, but it doesn't seem to be
-            # and fails on "Infinity"... oops
-            # data = orjson.loads(file_object.read())
-
-            # simplejson is faster than standard json and "just works"
-            # on the big Moe set in 37s
-            data = simplejson.load(file_object, object_hook=_hook)
+            data = simplejson.load(file_object)
             file_object.close()
-
-            # standard json module
-            # on the big Moe set takes 42s
-            # data = json.load(file_object,
-            #                 object_hook=_hook)
         elif filetype == "msgpack":
             data = msgpack.load(file_object, object_hook=_hook)
             file_object.close()
-
         if timing:
             print(
                 "\n\nTook {} s to load the data\n\n".format(time.time() - tstart),
                 flush=True,
             )
+    else:
+        with Halo(text="Loading", interval=250, spinner="moon", color="yellow"):
+            tstart = time.time()
+            _loaded = False
+
+            def _hook(obj):
+                """
+                Hook to load ensemble
+                """
+
+                nonlocal _loaded
+                if _loaded is False:
+                    _loaded = True
+                    print(
+                        "\nLoaded {} data, now putting in a dictionary".format(filetype),
+                        flush=True,
+                    )
+                return obj
+
+            if filetype == "JSON":
+                # orjson promises to be fast, but it doesn't seem to be
+                # and fails on "Infinity"... oops
+                # data = orjson.loads(file_object.read())
+
+                # simplejson is faster than standard json and "just works"
+                # on the big Moe set in 37s
+                if quiet == False:
+                    data = simplejson.load(file_object, object_hook=_hook)
+                else:
+                    data = simplejson.load(file_object)
+                file_object.close()
+
+                # standard json module
+                # on the big Moe set takes 42s
+                # data = json.load(file_object,
+                #                 object_hook=_hook)
+            elif filetype == "msgpack":
+                data = msgpack.load(file_object, object_hook=_hook)
+                file_object.close()
+
+            if timing:
+                print(
+                    "\n\nTook {} s to load the data\n\n".format(time.time() - tstart),
+                    flush=True,
+                )
 
     # strip non-selected keys, if a list is given in select_keys
     if select_keys:
diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py
index a55f231ca..48e661711 100644
--- a/binarycpython/utils/grid.py
+++ b/binarycpython/utils/grid.py
@@ -1267,12 +1267,10 @@ class Population(
         for output_dict in iter(result_queue.get, sentinel):
             if output_dict:
                 # don't let Xinit be added
-                if (
-                    "ensemble_results" in combined_output_dict
-                    and "ensemble" in combined_output_dict["ensemble_results"]
-                    and "Xinit" in combined_output_dict["ensemble_results"]["ensemble"]
-                ):
+                try:
                     del combined_output_dict["ensemble_results"]["ensemble"]["Xinit"]
+                except:
+                    pass
 
                 # merge dicts
                 combined_output_dict = merge_dicts(
diff --git a/install.sh b/install.sh
index 42f91d612..f3204fd55 100755
--- a/install.sh
+++ b/install.sh
@@ -13,7 +13,7 @@ echo "installing binarcpython version $VERSION_NUMBER"
 
 # do stuff...
 $PYTHON setup.py clean
-$PIP uninstall binarycpython
+$PIP uninstall -y binarycpython
 $PYTHON setup.py build --force
 $PYTHON setup.py sdist
 $PIP install -v dist/binarycpython-$VERSION_NUMBER.tar.gz
-- 
GitLab