Test function represent flow operational

This commit is contained in:
Remi Ehounou
2021-05-22 01:05:18 -04:00
parent 89bf06da6c
commit 00e46546f4
3 changed files with 46 additions and 48 deletions

View File

@ -26,6 +26,11 @@ import statistics as st
from scipy import signal, misc, ndimage from scipy import signal, misc, ndimage
def installFalcoo(mirror = 'https://utstat.toronto.edu/cran/'):
utils = importr('utils')
utils.install_packages('flacco', repos=mirror)
utils.install_packages('list', repos=mirror)
class counter: class counter:
#wraps a function, to keep a running count of how many #wraps a function, to keep a running count of how many
#times it's been called #times it's been called
@ -111,8 +116,8 @@ def writerepresentation(funcpath, charas):
# create a string format of the representation variables # create a string format of the representation variables
representation = '' representation = ''
for line in list(charas): for line in list(charas):
representation += '\n\t#_# ' + line + ': ' + str(charas[line]).replace('\n', ',') representation += '\n\t#_# ' + line + ': ' + repr(charas[line]).replace('\n','')
representation+='\n' representation+='\n\n\t#_# Represented: 1\n\n'
# Creating the new docstring to be inserted into the file # Creating the new docstring to be inserted into the file
with open(funcpath, "r") as file: with open(funcpath, "r") as file:
@ -121,7 +126,7 @@ def writerepresentation(funcpath, charas):
docstrs += representation docstrs += representation
repl = "\\1"+docstrs+"\t\\2" repl = "\\1"+docstrs+"\t\\2"
# Create the new content of the file to replace the old. Overwriting the whole thing # Create the new content of the file to replace the old. Replacing the whole thing
pattrn = re.compile("(def main\(.*?\):.*?''').*?('''.*?return\s+.*?\n|$)", flags=re.DOTALL) pattrn = re.compile("(def main\(.*?\):.*?''').*?('''.*?return\s+.*?\n|$)", flags=re.DOTALL)
newContent = pattrn.sub(repl, content, count=1) newContent = pattrn.sub(repl, content, count=1)
# Overwrite the test function file # Overwrite the test function file
@ -138,46 +143,45 @@ def representfunc(funcpath):
# Finding the function characteristics inside the docstring # Finding the function characteristics inside the docstring
if funcmodule.main.__doc__: if funcmodule.main.__doc__:
regex = re.compile("#_#\s?(\w+):\s?([-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)") regex = re.compile("#_#\s?(\w+):(.+)?\n") # this regular expression matches the characteristics already specified in the docstring section of the function -- old exp: "#_#\s?(\w+):\s?([-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)"
characs = re.findall(regex, funcmodule.main.__doc__) characs = re.findall(regex, funcmodule.main.__doc__)
results = {} results = {}
for charac in characs: for charac in characs:
results[charac[0]] = float(charac[1]) results[charac[0]] = eval(charac[1])
# Automatically generate the representation if the docstrings did not return anything # Automatically generate the representation if the docstrings did not return anything
if not ('Represented' in results): if not ('Represented' in results):
print("Warning, the Representation of the Test Function has not been specified\n===\n******Calculating the Characteristics******") print("Warning, the Representation of the Test Function has not been specified\n===\n******Calculating the Characteristics******")
n = int(results['dimmensions']) n = int(results['dimmensions'])
execpath = sys.executable # Importing FLACCO using rpy2
flacco = importr('flacco')
# creating the r functions # creating the r functions
rlist = robjs.r['list'] rlist = robjs.r['list']
rapply = robjs.r['apply'] rapply = robjs.r['apply']
rvector = robjs.r['c']
r_unlist = robjs.r['unlist']
rtestfunc = rinterface.rternalize(funcmodule.main) rtestfunc = rinterface.rternalize(funcmodule.main)
### ###
lower =-10 lower = r_unlist(rvector(results['lower']))
upper = 10 upper = r_unlist(rvector(results['upper']))
X = flacco.createInitialSample(n_obs = 500, dim = n, control = rlist(init_sample_type = 'lhs', init_sample_lower = lower, init_sample_upper = upper)) X = flacco.createInitialSample(n_obs = 500, dim = n, control = rlist(**{'init_sample.type' : 'lhs', 'init_sample.lower' : lower, 'init_sample.upper' : upper}))
y = rapply(X, 1, rtestfunc) y = rapply(X, 1, rtestfunc)
testfuncobj = flacco.createFeatureObject(X = X, y = y, fun = rtestfunc, lower = lower, upper = upper, blocks = 10) testfuncobj = flacco.createFeatureObject(X = X, y = y, fun = rtestfunc, lower = lower, upper = upper, blocks = 10)
# these are the retained features. Note that some features are being excluded for being problematic and to avoid overcomplicating the neural network # these are the retained features. Note that some features are being excluded for being problematic and to avoid overcomplicating the neural network.... the feature sets are redundant and the most relevant ones have been retained
# the excluded feature sets are: 'bt', 'ela_level' # the excluded feature sets are: 'bt', 'ela_level'
# feature sets that require special attention: 'cm_angle', 'cm_grad', 'limo', 'gcm' (soo big with some nans), # feature sets that require special attention: 'cm_angle', 'cm_grad', 'limo', 'gcm' (large set with some nans),
featureset = ['cm_angle','cm_conv','cm_grad','ela_conv','ela_curv','ela_distr','ela_local','ela_meta','basic','disp','limo','nbc','pca','gcm','ic'] featureset = ['cm_angle','cm_conv','cm_grad','ela_conv','ela_curv','ela_distr','ela_local','ela_meta','basic','disp','limo','nbc','pca','gcm','ic']
pyfeats = dict() pyfeats = dict()
for feature in featureset: for feature in featureset:
rawfeats = flacco.calculateFeatureSet(testfuncobj, set=feature) rawfeats = flacco.calculateFeatureSet(testfuncobj, set=feature)
pyfeats[feature] = asarray(rawfeats) pyfeats[feature] = asarray(rawfeats)
writerepresentation(funcpath, pyfeats) writerepresentation(funcpath, pyfeats)
return results return results
@ -234,14 +238,7 @@ if __name__ == '__main__':
# testfunctionpaths = ["/home/remi/Documents/MDAF-GitLAB/SourceCode/TestFunctions/Bukin4.py"] # testfunctionpaths = ["/home/remi/Documents/MDAF-GitLAB/SourceCode/TestFunctions/Bukin4.py"]
# funcnames = ["Bukin4"] # funcnames = ["Bukin4"]
# Installing the packages needed for FLACCO
utils = importr('utils')
#utils.install_packages('flacco', repos='https://utstat.toronto.edu/cran/')
#utils.install_packages('list', repos='https://utstat.toronto.edu/cran/')
####utils.install_packages('reticulate', repos='https://utstat.toronto.edu/cran/')
reticulate = importr('reticulate')
flacco = importr('flacco')
objs = 0 objs = 0
args = {"high": 200, "low": -200, "t": 1000, "p": 0.95} args = {"high": 200, "low": -200, "t": 1000, "p": 0.95}

View File

@ -1,28 +1,29 @@
def main(args): def main(args):
''' '''
:param args: list of floats
:return: float
#_# dimmensions: 2 #_# dimmensions: 2
#_# upper: [-5, 3]
#_# lower: [-15, -3]
#_# minimum: [-10,0]
#_# cm_angle: array([[3.49881571e-01], [1.07838645e-01], [3.22906733e-01], [1.09086923e-01], [1.36740093e+02], [3.72333248e+01], [6.24743683e-02], [1.45683932e-02], [0.00000000e+00], [7.70000000e-02]])
#_# cm_conv: array([[0.08928571], [0.04166667], [0.51785714], [0.48214286], [0. ], [0.038 ]])
#_# cm_grad: array([[0.80986036], [0.11715403], [0. ], [0.05 ]])
#_# ela_conv: array([[0.00000000e+00], [0.00000000e+00], [2.74360332e+00], [2.74360332e+00], [1.00000000e+03], [1.39000000e-01]])
#_# ela_curv: array([[1.00520667e+02], [1.01157352e+02], [1.02102548e+02], [1.01875092e+02], [1.03042277e+02], [1.04371521e+02], [1.09894163e+00], [0.00000000e+00], [3.34559633e+00], [4.02357156e+00], [5.48926800e+00], [5.13982675e+00], [6.55405463e+00], [9.78678891e+00], [1.70558030e+00], [0.00000000e+00], [3.29353234e+00], [1.48351404e+04], [2.19236653e+31], [4.03941866e+04], [3.14916313e+07], [4.17710288e+33], [2.95558281e+32], [0.00000000e+00], [8.40000000e+03], [1.06400000e+00]])
#_# ela_distr: array([[ 0.01739786], [-1.01700978], [ 1. ], [ 0. ], [ 0.02 ]])
#_# ela_local: array([[1.000e+00], [1.000e-02], [1.000e+00], [ nan], [1.000e+00], [0.000e+00], [1.000e+00], [1.000e+01], [1.000e+01], [1.000e+01], [1.000e+01], [1.000e+01], [1.000e+01], [0.000e+00], [1.001e+03], [9.300e-02]])
#_# ela_meta: array([[9.98338849e-01], [1.91750766e+02], [1.99998995e+01], [1.00059864e+02], [5.00301834e+00], [9.98335620e-01], [1.00000000e+00], [3.55077954e+14], [1.00000000e+00], [0.00000000e+00], [9.00000000e-03]])
#_# basic: array([[ 2. ], [ 500. ], [ -15. ], [ -3. ], [ -5. ], [ 3. ], [-388.17734244], [ 369.94308917], [ 10. ], [ 10. ], [ 100. ], [ 100. ], [ 1. ], [ 0. ], [ 0. ]])
#_# disp: array([[ 0.26139886], [ 0.36358526], [ 0.5516879 ], [ 0.80572442], [ 0.25023966], [ 0.32900436], [ 0.49402763], [ 0.72135232], [-3.13648576], [-2.70254902], [-1.90376706], [-0.82499549], [-3.02850003], [-2.7103465 ], [-2.04376951], [-1.125539 ], [ 0. ], [ 0.01 ]])
#_# limo: array([[ 1.02002699e+02], [ 9.98507972e-01], [ 1.02155253e+02], [ 1.13240181e+00], [-3.88783841e-02], [-9.92102532e-01], [ 5.48006296e+00], [ 1.74733189e+00], [ 3.39374032e+01], [ 5.00221530e+00], [ 2.94406289e+00], [ 3.22874553e-02], [ 0.00000000e+00], [ 9.50000000e-02]])
#_# nbc: array([[ 0.5341454 ], [ 0.87561488], [ 0.47062778], [ 0.15902654], [-0.19513986], [ 0. ], [ 0.027 ]])
#_# pca: array([[1. ], [1. ], [0.33333333], [0.66666667], [0.73549361], [0.51109865], [0.99976032], [0.66217335], [0. ], [0.002 ]])
#_# gcm: array([[1. ], [0.01 ], [0.99 ], [0. ], [1. ], [1. ], [1. ], [1. ], [ nan], [1. ], [1. ], [1. ], [1. ], [ nan], [1. ], [1. ], [1. ], [1. ], [1. ], [ nan], [1. ], [1. ], [0.01 ], [0. ], [0.085], [1. ], [0.01 ], [0.99 ], [0. ], [1. ], [1. ], [1. ], [1. ], [ nan], [1. ], [1. ], [1. ], [1. ], [ nan], [1. ], [1. ], [1. ], [1. ], [1. ], [ nan], [1. ], [1. ], [0.01 ], [0. ], [0.084], [1. ], [0.01 ], [0.99 ], [0. ], [1. ], [1. ], [1. ], [1. ], [ nan], [1. ], [1. ], [1. ], [1. ], [ nan], [1. ], [1. ], [1. ], [1. ], [1. ], [ nan], [1. ], [1. ], [0.01 ], [0. ], [0.088]])
#_# ic: array([[ 0.67118887], [ 2.02702703], [70.28244264], [ 1.90690691], [ 0.23293173], [ 0. ], [ 0.204 ]])
#_# Represented: 1
#_# cm_angle: [[1.33897501e+01], [ nan], [1.40464922e+01], [ nan], [3.10339907e+00], [ nan], [1.00000000e+00], [ nan], [0.00000000e+00], [1.40000000e-02]]
#_# cm_conv: [[0.0297619 ], [0.00595238], [0.22619048], [0.33333333], [0. ], [0.062 ]]
#_# cm_grad: [[0.64254617], [ nan], [0. ], [0.01 ]]
#_# ela_conv: [[0.00000000e+00], [0.00000000e+00], [2.77028225e-02], [2.77028225e-02], [1.00000000e+03], [1.31000000e-01]]
#_# ela_curv: [[1.00000000e+02], [1.00000355e+02], [1.00005040e+02], [1.00002931e+02], [1.00009346e+02], [1.00015828e+02], [5.10494927e-03], [0.00000000e+00], [5.62028278e+01], [7.31406032e+01], [5.13544460e+02], [1.30604608e+02], [3.75618981e+02], [1.05254867e+04], [1.23941806e+03], [0.00000000e+00], [3.02592268e+00], [9.02638335e+02], [1.56148314e+29], [3.81682127e+03], [3.25219248e+04], [2.89737144e+31], [2.05852895e+30], [5.00000000e-03], [8.40000000e+03], [1.13300000e+00]]
#_# ela_distr: [[-0.03671804], [-1.24272349], [ 5. ], [ 0. ], [ 0.023 ]]
#_# ela_local: [[3.00000000e+00], [3.00000000e-02], [1.00267380e+00], [6.66666667e-01], [1.10000000e-01], [4.45000000e-01], [4.45000000e-01], [2.00000000e+01], [2.00000000e+01], [2.26000000e+01], [2.00000000e+01], [2.50000000e+01], [3.00000000e+01], [3.51619629e+00], [2.26300000e+03], [2.35000000e-01]]
#_# ela_meta: [[9.99993630e-01], [1.01176187e+02], [8.08905581e-01], [9.99825941e+01], [1.23602305e+02], [9.99993660e-01], [1.00000000e+00], [3.95467951e+13], [1.00000000e+00], [0.00000000e+00], [1.00000000e-02]]
#_# basic: [[ 2. ], [500. ], [-10. ], [-10. ], [ 10. ], [ 10. ], [100.74931228], [199.87416126], [ 10. ], [ 10. ], [100. ], [ 1. ], [ 1. ], [ 0. ], [ 0. ]]
#_# disp: [[ 0.75879825], [ 0.61920947], [ 0.61336996], [ 0.68491606], [ 0.76149282], [ 0.5882951 ], [ 0.56069976], [ 0.60774698], [-0.12954581], [-0.20451683], [-0.20765314], [-0.16922681], [-0.12717398], [-0.21952442], [-0.23423848], [-0.20915252], [ 0. ], [ 0.011 ]]
#_# limo: [[9.99858662e+01], [1.00000000e+00], [9.99858662e+01], [ nan], [ nan], [ nan], [1.23602305e+02], [ nan], [ nan], [ nan], [ nan], [ nan], [0.00000000e+00], [3.00000000e-03]]
#_# nbc: [[ 0.30653068], [ 0.64759411], [ 0.26842005], [ 0.37025091], [-0.10065679], [ 0. ], [ 0.031 ]]
#_# pca: [[1. ], [1. ], [0.33333333], [0.66666667], [0.51467093], [0.50754981], [0.9998948 ], [0.66673408], [0. ], [0.002 ]]
#_# gcm: [[1.00000000e+00], [1.00000000e-02], [0.00000000e+00], [0.00000000e+00], [1.00000000e-02], [1.00000000e-02], [1.00000000e-02], [1.00000000e-02], [ nan], [1.00000000e-02], [1.00000000e-02], [1.00000000e-02], [1.00000000e-02], [ nan], [1.00000000e-02], [1.00000000e-02], [1.00000000e-02], [1.00000000e-02], [1.00000000e-02], [ nan], [1.00000000e-02], [1.00000000e-02], [1.00000000e-02], [0.00000000e+00], [6.00000000e-03], [1.00000000e+00], [1.00000000e-02], [0.00000000e+00], [0.00000000e+00], [1.00000000e-02], [1.00000000e-02], [1.00000000e-02], [1.00000000e-02], [ nan], [1.00000000e-02], [1.00000000e-02], [1.00000000e-02], [1.00000000e-02], [ nan], [1.00000000e-02], [1.00000000e-02], [1.00000000e-02], [1.00000000e-02], [1.00000000e-02], [ nan], [1.00000000e-02], [1.00000000e-02], [1.00000000e-02], [0.00000000e+00], [5.00000000e-03], [5.00000000e+00], [5.00000000e-02], [9.50000000e-01], [1.00000000e+00], [1.53846154e-01], [2.00000000e-01], [2.30769231e-01], [2.30769231e-01], [4.21325044e-02], [0.00000000e+00], [0.00000000e+00], [0.00000000e+00], [0.00000000e+00], [0.00000000e+00], [0.00000000e+00], [0.00000000e+00], [2.00000000e-01], [1.00000000e-02], [6.40000000e-01], [2.88183969e-01], [1.00000000e+00], [1.00000000e+00], [5.00000000e-02], [0.00000000e+00], [1.02000000e-01]]
#_# ic: [[ 0.71881882], [ 2.00700701], [64.09244019], [ 1.82682683], [ 0.29116466], [ 0. ], [ 0.203 ]]
''' '''
return 100*(args[1]-0.01*args[0]**2+1)+0.01*(args[0]+10)**2 return 100*(args[1]-0.01*args[0]**2+1)+0.01*(args[0]+10)**2
return 0