From 00c1720ccea2baee2f1c8ae29a909cd307085bc3 Mon Sep 17 00:00:00 2001
From: farid <farid.mohammadi@iws.uni-stuttgart.de>
Date: Mon, 26 Jul 2021 15:30:32 +0200
Subject: [PATCH] [PA-A] use _pickle as pickle and joblib to dump the object.

---
 BayesValidRox/tests/PA-A/Benchmark_PAA.py     |  8 +++---
 .../tests/PA-A/ffpm_validation_stokesdarcy.py | 22 +++++++--------
 .../tests/PA-A/ffpm_validation_stokespnm.py   | 27 +++++++++----------
 3 files changed, 27 insertions(+), 30 deletions(-)

diff --git a/BayesValidRox/tests/PA-A/Benchmark_PAA.py b/BayesValidRox/tests/PA-A/Benchmark_PAA.py
index f32cf5ae4..9f37c1842 100755
--- a/BayesValidRox/tests/PA-A/Benchmark_PAA.py
+++ b/BayesValidRox/tests/PA-A/Benchmark_PAA.py
@@ -36,7 +36,7 @@ plt.rc('figure', titlesize=SIZE)  # fontsize of the figure title
 try:
     import cPickle as pickle
 except ModuleNotFoundError:
-    import pickle
+    import _pickle as pickle
 
 # Add BayesValidRox path
 sys.path.insert(0,'./../../../BayesValidRox/')
@@ -334,7 +334,7 @@ if __name__ == "__main__":
 
     # Load the objects
     # with open(result_folder+'/outputs_ffpm-stokesdarcyBJ/PA_A_Bayesffpm-stokesdarcyBJ-valid.pkl', 'rb') as input:
-    #     BayesValid_BJ = pickle.load(input)
+    #     BayesValid_BJ = joblib.load(input)
 
     # Model stokesdarcy-ER
     PCEModel_ER, BayesCalib_ER, BayesValid_ER = stokesdarcy.run(paramsAvg, couplingcond='ER',
@@ -343,14 +343,14 @@ if __name__ == "__main__":
 
     # Load the objects
     # with open(result_folder+'/outputs_ffpm-stokesdarcyER/PA_A_Bayesffpm-stokesdarcyER-valid.pkl', 'rb') as input:
-    #     BayesValid_ER = pickle.load(input)
+    #     BayesValid_ER = joblib.load(input)
 
     # Model stokesdarcy-pnm with the averaged data
     PCEModel_PNM, BayesCalib_PNM, BayesValid_PNM = stokespnm.run(paramsAvg, PCEEDMethod=PCEExpDesignMethod)
 
     # Load the objects
     # with open(result_folder+'/outputs_ffpm-stokespnm/PA_A_Bayesffpm-stokespnm-valid.pkl', 'rb') as input:
-    #     BayesValid_PNM = pickle.load(input)
+    #     BayesValid_PNM = joblib.load(input)
     
     # Model stokesdarcy-pnm without the averaged data
     PCEModel_PNM_NA, BayesCalib_PNM_NA, BayesValid_PNM_NA = stokespnm.run(params, averaging=False,
diff --git a/BayesValidRox/tests/PA-A/ffpm_validation_stokesdarcy.py b/BayesValidRox/tests/PA-A/ffpm_validation_stokesdarcy.py
index 1da3e35ff..e90ef0707 100755
--- a/BayesValidRox/tests/PA-A/ffpm_validation_stokesdarcy.py
+++ b/BayesValidRox/tests/PA-A/ffpm_validation_stokesdarcy.py
@@ -5,7 +5,7 @@ Created on Thu Aug 13 09:53:11 2020
 
 @author: farid
 """
-import sys, os
+import sys, os, joblib
 import numpy as np
 import scipy.stats as stats
 import shutil
@@ -13,7 +13,7 @@ import pandas as pd
 try:
     import cPickle as pickle
 except ModuleNotFoundError:
-    import pickle
+    import _pickle as pickle
 
 import matplotlib
 matplotlib.use('agg')
@@ -255,11 +255,11 @@ def run(params, errorPerc=0.05, couplingcond='BJ', PCEEDMethod='normal'):
 
     ## Save PCE models
     with open('PCEModel_'+Model.Name+'.pkl', 'wb') as output:
-        pickle.dump(PCEModel, output, pickle.HIGHEST_PROTOCOL)
+        joblib.dump(PCEModel, output, 2)
 
     # Load the objects
     # with open('PCEModel_'+Model.Name+'.pkl', 'rb') as input:
-    #     PCEModel = pickle.load(input)
+    #     PCEModel = joblib.load(input)
 
     #=====================================================
     #=========  POST PROCESSING OF METAMODELS  ===========
@@ -351,11 +351,11 @@ def run(params, errorPerc=0.05, couplingcond='BJ', PCEEDMethod='normal'):
 
     #Save class objects
     with open('PA_A_Bayes'+Model.Name+'.pkl', 'wb') as output:
-        pickle.dump(BayesCalib, output, pickle.HIGHEST_PROTOCOL)
+        joblib.dump(BayesCalib, output, 2)
 
     # Load the objects
     # with open('PA_A_Bayes'+Model.Name+'.pkl', 'rb') as input:
-    #     BayesCalib = pickle.load(input)
+    #     BayesCalib = joblib.load(input)
     #=====================================================
     #=================  Visualization  ===================
     #=====================================================
@@ -428,11 +428,11 @@ def run(params, errorPerc=0.05, couplingcond='BJ', PCEEDMethod='normal'):
 
     # Save PCE models
     with open('PCEModel_'+ValidModel.Name+'.pkl', 'wb') as output:
-        pickle.dump(ValidPCEModel, output, pickle.HIGHEST_PROTOCOL)
+        joblib.dump(ValidPCEModel, output, 2)
 
     # Load the objects
     # with open('PCEModel_'+ValidModel.Name+'.pkl', 'rb') as input:
-    #     ValidPCEModel = pickle.load(input)
+    #     ValidPCEModel = joblib.load(input)
 
     #=====================================================
     #=========  POST PROCESSING OF METAMODELS  ===========
@@ -521,10 +521,8 @@ def run(params, errorPerc=0.05, couplingcond='BJ', PCEEDMethod='normal'):
     #==============  Save class objects  =================
     #=====================================================
     with open('PA_A_Bayes'+ValidModel.Name+'.pkl', 'wb') as output:
-        pickle.dump(BayesValid, output, pickle.HIGHEST_PROTOCOL)
-    # p = pickle.Pickler(open('PA_A_Bayes'+ValidModel.Name+'.pkl',"wb"))
-    # p.fast = True
-    # p.dump(BayesValid)
+        pickle.joblib(BayesValid, output, 2)
+
     #=====================================================
     #=================  Visualization  ===================
     #=====================================================
diff --git a/BayesValidRox/tests/PA-A/ffpm_validation_stokespnm.py b/BayesValidRox/tests/PA-A/ffpm_validation_stokespnm.py
index cd66e015c..bd79bde60 100755
--- a/BayesValidRox/tests/PA-A/ffpm_validation_stokespnm.py
+++ b/BayesValidRox/tests/PA-A/ffpm_validation_stokespnm.py
@@ -5,7 +5,7 @@ Created on Thu Aug 13 09:53:11 2020
 
 @author: farid
 """
-import sys, os
+import sys, os, joblib
 import numpy as np
 import scipy.stats as stats
 import pandas as pd
@@ -13,8 +13,7 @@ import shutil
 try:
     import cPickle as pickle
 except ModuleNotFoundError:
-    import pickle
-
+    import _pickle as pickle
 
 import matplotlib
 matplotlib.use('agg')
@@ -268,11 +267,11 @@ def run(params, averaging=True,errorPerc=0.05, PCEEDMethod='normal'):
 
     # Save PCE models
     with open('PCEModel_'+Model.Name+'.pkl', 'wb') as output:
-        pickle.dump(PCEModel, output, pickle.HIGHEST_PROTOCOL)
+        joblib.dump(PCEModel, output, 2)
 
     # Load the objects
     # with open('PCEModel_'+Model.Name+'.pkl', 'rb') as input:
-    #     PCEModel = pickle.load(input)
+    #     PCEModel = joblib.load(input)
 
     #=====================================================
     #=========  POST PROCESSING OF METAMODELS  ===========
@@ -356,15 +355,15 @@ def run(params, averaging=True,errorPerc=0.05, PCEEDMethod='normal'):
     # BayesOptsCalib.MeasurementError = np.sqrt(calibMeasuError)
 
     # ----- Strat Bayesian inference -------
-    # BayesCalib = BayesOptsCalib.create_Inference()
+    BayesCalib = BayesOptsCalib.create_Inference()
 
-    # # Save class objects
-    # with open('PA_A_Bayes'+Model.Name+'.pkl', 'wb') as output:
-    #     pickle.dump(BayesCalib, output, pickle.HIGHEST_PROTOCOL)
+    # Save class objects
+    with open('PA_A_Bayes'+Model.Name+'.pkl', 'wb') as output:
+        joblib.dump(BayesCalib, output, 2)
 
     # Load the objects
-    with open('PA_A_Bayes'+Model.Name+'.pkl', 'rb') as input:
-        BayesCalib = pickle.load(input)
+    # with open('PA_A_Bayes'+Model.Name+'.pkl', 'rb') as input:
+    #     BayesCalib = joblib.load(input)
 
     #=====================================================
     #=================  Visualization  ===================
@@ -435,11 +434,11 @@ def run(params, averaging=True,errorPerc=0.05, PCEEDMethod='normal'):
 
     # Save PCE models
     with open('PCEModel_'+ValidModel.Name+'.pkl', 'wb') as output:
-        pickle.dump(ValidPCEModel, output, pickle.HIGHEST_PROTOCOL)
+        joblib.dump(ValidPCEModel, output, 2)
 
     # Load the objects
     # with open('PCEModel_'+ValidModel.Name+'.pkl', 'rb') as input:
-    #     ValidPCEModel = pickle.load(input)
+    #     ValidPCEModel = joblib.load(input)
 
     #=====================================================
     #=========  POST PROCESSING OF METAMODELS  ===========
@@ -526,7 +525,7 @@ def run(params, averaging=True,errorPerc=0.05, PCEEDMethod='normal'):
 
     # Save class objects
     with open('PA_A_Bayes'+ValidModel.Name+'.pkl', 'wb') as output:
-        pickle.dump(BayesValid, output, pickle.HIGHEST_PROTOCOL)
+        joblib.dump(BayesValid, output, 2)
 
     #=====================================================
     #=================  Visualization  ===================
-- 
GitLab