From: Jean-Philippe ARGAUD Date: Mon, 24 Jun 2024 07:01:52 +0000 (+0200) Subject: Minor source review update for type compatibilities and test update X-Git-Tag: V9_13_0rc1~2 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=ee4942bfdca7016d27a55ca7938ec71e6675d4ba;p=modules%2Fadao.git Minor source review update for type compatibilities and test update --- diff --git a/src/daComposant/daCore/BasicObjects.py b/src/daComposant/daCore/BasicObjects.py index 838cb1d..03cfe56 100644 --- a/src/daComposant/daCore/BasicObjects.py +++ b/src/daComposant/daCore/BasicObjects.py @@ -2387,7 +2387,10 @@ class Covariance(object): def __repr__(self): "x.__repr__() <==> repr(x)" - return repr(self.__C) + if isinstance(self.__C, numpy.float64): + return repr(float(self.__C)) + else: + return repr(self.__C) def __str__(self): "x.__str__() <==> str(x)" diff --git a/src/daComposant/daCore/NumericObjects.py b/src/daComposant/daCore/NumericObjects.py index dc72f85..5903cdd 100644 --- a/src/daComposant/daCore/NumericObjects.py +++ b/src/daComposant/daCore/NumericObjects.py @@ -25,7 +25,8 @@ __doc__ = """ """ __author__ = "Jean-Philippe ARGAUD" -import os, copy, types, sys, logging, math, numpy, scipy, itertools +import os, copy, types, sys, logging, math, numpy, scipy, itertools, warnings +import scipy.linalg # Py3.6 from daCore.BasicObjects import Operator, Covariance, PartialAlgorithm from daCore.PlatformInfo import PlatformInfo, vt, vfloat mpr = PlatformInfo().MachinePrecision() @@ -643,7 +644,6 @@ def EnsembleErrorCovariance( __Ensemble, __Quick = False ): def SingularValuesEstimation( __Ensemble, __Using = "SVDVALS"): "Renvoie les valeurs singulières de l'ensemble et leur carré" if __Using == "SVDVALS": # Recommandé - import scipy __sv = scipy.linalg.svdvals( __Ensemble ) __svsq = __sv**2 elif __Using == "SVD": @@ -1385,7 +1385,6 @@ def BuildComplexSampleList( sampleList = itertools.product(*coordinatesList) # --------------------------- elif len(__SampleAsMinMaxLatinHyperCube) > 0: - import scipy, warnings if vt(scipy.version.version) <= vt("1.7.0"): __msg = "In order to use Latin Hypercube sampling, you must at least use Scipy version 1.7.0 (and you are presently using Scipy %s). A void sample is then generated."%scipy.version.version warnings.warn(__msg, FutureWarning, stacklevel=50) @@ -1404,7 +1403,6 @@ def BuildComplexSampleList( sampleList = scipy.stats.qmc.scale(__sample, __l_bounds, __u_bounds) # --------------------------- elif len(__SampleAsMinMaxSobolSequence) > 0: - import scipy, warnings if vt(scipy.version.version) <= vt("1.7.0"): __msg = "In order to use Latin Hypercube sampling, you must at least use Scipy version 1.7.0 (and you are presently using Scipy %s). A void sample is then generated."%scipy.version.version warnings.warn(__msg, FutureWarning, stacklevel=50) diff --git a/src/daComposant/daCore/Persistence.py b/src/daComposant/daCore/Persistence.py index 588465e..29d7f71 100644 --- a/src/daComposant/daCore/Persistence.py +++ b/src/daComposant/daCore/Persistence.py @@ -300,7 +300,7 @@ class Persistence(object): __sr = [numpy.mean(item, dtype=mfp).astype('float') for item in self.__values] except Exception: raise TypeError("Base type is incompatible with numpy") - return( numpy.array(__sr).tolist() ) + return numpy.array(__sr).tolist() def stds(self, ddof=0): """ @@ -318,7 +318,7 @@ class Persistence(object): return [numpy.array(item).std(dtype=mfp).astype('float') for item in self.__values] except Exception: raise TypeError("Base type is incompatible with numpy") - return( numpy.array(__sr).tolist() ) + return numpy.array(__sr).tolist() def sums(self): """ @@ -330,7 +330,7 @@ class Persistence(object): __sr = [numpy.array(item).sum() for item in self.__values] except Exception: raise TypeError("Base type is incompatible with numpy") - return( numpy.array(__sr).tolist() ) + return numpy.array(__sr).tolist() def mins(self): """ @@ -342,7 +342,7 @@ class Persistence(object): __sr = [numpy.array(item).min() for item in self.__values] except Exception: raise TypeError("Base type is incompatible with numpy") - return( numpy.array(__sr).tolist() ) + return numpy.array(__sr).tolist() def maxs(self): """ @@ -354,7 +354,7 @@ class Persistence(object): __sr = [numpy.array(item).max() for item in self.__values] except Exception: raise TypeError("Base type is incompatible with numpy") - return( numpy.array(__sr).tolist() ) + return numpy.array(__sr).tolist() def powers(self, x2): """ @@ -366,7 +366,7 @@ class Persistence(object): __sr = [numpy.power(item, x2) for item in self.__values] except Exception: raise TypeError("Base type is incompatible with numpy") - return( numpy.array(__sr).tolist() ) + return numpy.array(__sr).tolist() def norms(self, _ord=None): """ @@ -380,7 +380,7 @@ class Persistence(object): __sr = [numpy.linalg.norm(item, _ord) for item in self.__values] except Exception: raise TypeError("Base type is incompatible with numpy") - return( numpy.array(__sr).tolist() ) + return numpy.array(__sr).tolist() def traces(self, offset=0): """ @@ -394,7 +394,7 @@ class Persistence(object): __sr = [numpy.trace(item, offset, dtype=mfp).astype('float') for item in self.__values] except Exception: raise TypeError("Base type is incompatible with numpy") - return( numpy.array(__sr).tolist() ) + return numpy.array(__sr).tolist() def maes(self, _predictor=None): """ @@ -422,7 +422,7 @@ class Persistence(object): __sr = [numpy.mean(numpy.abs(numpy.ravel(item) - numpy.ravel(_predictor[i]))) for i, item in enumerate(self.__values)] except Exception: raise TypeError("Base type is incompatible with numpy") - return( numpy.array(__sr).tolist() ) + return numpy.array(__sr).tolist() def mses(self, _predictor=None): """ @@ -452,7 +452,7 @@ class Persistence(object): __sr = [(numpy.linalg.norm(numpy.ravel(item) - numpy.ravel(_predictor[i]))**2 / __n) for i, item in enumerate(self.__values)] except Exception: raise TypeError("Base type is incompatible with numpy") - return( numpy.array(__sr).tolist() ) + return numpy.array(__sr).tolist() msds = mses # Mean-Square Deviation (MSD=MSE) @@ -484,7 +484,7 @@ class Persistence(object): __sr = [(numpy.linalg.norm(numpy.ravel(item) - numpy.ravel(_predictor[i])) / math.sqrt(__n)) for i, item in enumerate(self.__values)] except Exception: raise TypeError("Base type is incompatible with numpy") - return( numpy.array(__sr).tolist() ) + return numpy.array(__sr).tolist() rmsds = rmses # Root-Mean-Square Deviation (RMSD=RMSE) diff --git a/src/daComposant/daNumerics/pst4mod/modelica_calibration/case.py b/src/daComposant/daNumerics/pst4mod/modelica_calibration/case.py index 65b921c..f605b9e 100644 --- a/src/daComposant/daNumerics/pst4mod/modelica_calibration/case.py +++ b/src/daComposant/daNumerics/pst4mod/modelica_calibration/case.py @@ -430,7 +430,12 @@ class Calibration(object): if "MaximumNumberOfSteps" in Params: print(" [VERBOSE] Maximum possible number of iteration:",Params['MaximumNumberOfSteps']) print(" [VERBOSE] Background read:",Background) - print(" [VERBOSE] Bounds read:",Params['Bounds']) + __v = Params['Bounds'] + if isinstance(__v, (numpy.ndarray, numpy.matrix, list, tuple)): + __v = numpy.array(__v).astype('float') + __v = numpy.where(numpy.isnan(__v), None, __v) + __v = __v.tolist() + print(" [VERBOSE] Bounds read:",__v) # if "DifferentialIncrement" not in Params: Params["DifferentialIncrement"] = 0.001 @@ -662,7 +667,12 @@ class Calibration(object): print(" [VERBOSE] Links read") print(" [VERBOSE] Optimization information read") print(" [VERBOSE] Background read:",Background) - print(" [VERBOSE] Bounds read:",Params['Bounds']) + __v = Params['Bounds'] + if isinstance(__v, (numpy.ndarray, numpy.matrix, list, tuple)): + __v = numpy.array(__v).astype('float') + __v = numpy.where(numpy.isnan(__v), None, __v) + __v = __v.tolist() + print(" [VERBOSE] Bounds read:",__v) # if "DifferentialIncrement" not in Params: Params["DifferentialIncrement"] = 0.001 @@ -1921,7 +1931,14 @@ def _saveResults(__resultats, __filename=None, __level=None, __format="Guess", _ keys = list(__resultats.keys()) keys.sort() for k in keys: - output.append("%18s = %s"%(k,tuple(__resultats[k]))) + __v = __resultats[k] + if isinstance(__v, numpy.matrix): # no1 + __v = __v.astype('float').tolist() + elif isinstance(__v, numpy.ndarray): # no2 + __v = tuple(__v.astype('float').tolist()) + else: + __v = tuple(__v) + output.append("%22s = %s"%(k,__v)) output.append("") with open(__filename, 'w') as fid: fid.write( "\n".join(output) ) @@ -1932,7 +1949,14 @@ def _saveResults(__resultats, __filename=None, __level=None, __format="Guess", _ keys = list(__resultats.keys()) keys.sort() for k in keys: - output.append("%s = %s"%(k,tuple(__resultats[k]))) + __v = __resultats[k] + if isinstance(__v, numpy.matrix): # no1 + __v = __v.astype('float').tolist() + elif isinstance(__v, numpy.ndarray): # no2 + __v = tuple(__v.astype('float').tolist()) + else: + __v = tuple(__v) + output.append("%s = %s"%(k,__v)) output.append("") with open(__filename, 'w') as fid: fid.write( "\n".join(output) ) @@ -1949,7 +1973,14 @@ def _saveResults(__resultats, __filename=None, __level=None, __format="Guess", _ keys = list(__resultats.keys()) keys.sort() for k in keys: - output.append("%18s = %s"%(k,tuple(__resultats[k]))) + __v = __resultats[k] + if isinstance(__v, numpy.matrix): # no1 + __v = __v.astype('float').tolist() + elif isinstance(__v, numpy.ndarray): # no2 + __v = tuple(__v.astype('float').tolist()) + else: + __v = tuple(__v) + output.append("%22s = %s"%(k,__v)) output.append("") print( "\n".join(output) ) #--------------------------------------------- diff --git a/test/test6905/test001_ADAO_Elementary_Blue_tui.py b/test/test6905/test001_ADAO_Elementary_Blue_tui.py index 2a5c06a..0fd9c61 100644 --- a/test/test6905/test001_ADAO_Elementary_Blue_tui.py +++ b/test/test6905/test001_ADAO_Elementary_Blue_tui.py @@ -2,6 +2,7 @@ # # Python script using ADAO TUI # +import numpy as np from numpy import array, matrix from adao import adaoBuilder case = adaoBuilder.New('') diff --git a/test/test6905/test001_ADAO_External_variables_Blue_tui.py b/test/test6905/test001_ADAO_External_variables_Blue_tui.py index f6a95fb..f302bdb 100644 --- a/test/test6905/test001_ADAO_External_variables_Blue_tui.py +++ b/test/test6905/test001_ADAO_External_variables_Blue_tui.py @@ -2,6 +2,7 @@ # # Python script using ADAO TUI # +import numpy as np from numpy import array, matrix from adao import adaoBuilder case = adaoBuilder.New('') diff --git a/test/test6905/test002_ADAO_Elementary_3DVAR_tui.py b/test/test6905/test002_ADAO_Elementary_3DVAR_tui.py index 9afb343..d252805 100644 --- a/test/test6905/test002_ADAO_Elementary_3DVAR_tui.py +++ b/test/test6905/test002_ADAO_Elementary_3DVAR_tui.py @@ -2,6 +2,7 @@ # # Python script using ADAO TUI # +import numpy as np from numpy import array, matrix from adao import adaoBuilder case = adaoBuilder.New('') diff --git a/test/test6905/test002_ADAO_External_variables_3DVAR_tui.py b/test/test6905/test002_ADAO_External_variables_3DVAR_tui.py index 36b5eba..fffdb3c 100644 --- a/test/test6905/test002_ADAO_External_variables_3DVAR_tui.py +++ b/test/test6905/test002_ADAO_External_variables_3DVAR_tui.py @@ -2,6 +2,7 @@ # # Python script using ADAO TUI # +import numpy as np from numpy import array, matrix from adao import adaoBuilder case = adaoBuilder.New('') diff --git a/test/test6905/test020_ADAO_Elementary_GradientTest_tui.py b/test/test6905/test020_ADAO_Elementary_GradientTest_tui.py index 4b1b49c..618f326 100644 --- a/test/test6905/test020_ADAO_Elementary_GradientTest_tui.py +++ b/test/test6905/test020_ADAO_Elementary_GradientTest_tui.py @@ -2,6 +2,7 @@ # # Python script using ADAO TUI # +import numpy as np from numpy import array, matrix from adao import adaoBuilder case = adaoBuilder.New('')