4141from pathlib import Path
4242from tokenize import String
4343
44+ from typing import Tuple
4445import numpy as np
4546import pandas as pd
4647import tensorflow as tf # pylint: disable=import-error
5253from foqus_lib .framework .surrogate .surrogate import surrogate
5354from foqus_lib .framework .uq .SurrogateParser import SurrogateParser
5455
56+ from foqus_lib .framework .surrogate .scaling import (
57+ BaseScaler ,
58+ LinearScaler ,
59+ LogScaler ,
60+ LogScaler2 ,
61+ PowerScaler ,
62+ PowerScaler2 ,
63+ map_name_to_scaler ,
64+ scale_dataframe
65+ )
66+
67+ # mapping between the human-readable name for the scaling variant
68+ # and an instance of the corresponding scaler class
5569
5670# custom class to define Keras NN layers
5771@tf .keras .utils .register_keras_serializable ()
@@ -293,6 +307,14 @@ def __init__(self, dat=None):
293307 desc = "Name of output file for model, should have file extension: .keras" ,
294308 hint = "Enter a custom file name if desired" ,
295309 )
310+ # add option for normalization_form, make dropdown option
311+ self .options .add (
312+ name = "scaling_function" ,
313+ default = "Linear" ,
314+ dtype = str ,
315+ desc = "Scaling/normalization function for input data" ,
316+ validValues = list (map_name_to_scaler .keys ()),
317+ )
296318
297319 def run (self ):
298320 """
@@ -316,6 +338,9 @@ def run(self):
316338 self .msgQueue .put (f"input data columns: { input_data .columns } " )
317339 self .msgQueue .put (f"output data columns: { output_data .columns } " )
318340
341+ # extract scaling function option, apply it to the input data
342+ # get scaler object
343+
319344 # np.random.seed(46)
320345 # rn.seed(1342)
321346 # tf.random.set_seed(62)
@@ -341,22 +366,13 @@ def run(self):
341366 xdata = input_data
342367 zdata = output_data
343368
344- xdata_bounds = {i : (xdata [i ].min (), xdata [i ].max ()) for i in xdata } # x bounds
345- zdata_bounds = {j : (zdata [j ].min (), zdata [j ].max ()) for j in zdata } # z bounds
346-
347- # normalize data using Linear form
348- # users can normalize with any allowed form # manually, and then pass the
349- # appropriate flag to FOQUS from the allowed list:
350- # ["Linear", "Log", "Power", "Log 2", "Power 2"] - see the documentation for
351- # details on the scaling formulations
352- xmax , xmin = xdata .max (axis = 0 ), xdata .min (axis = 0 )
353- zmax , zmin = zdata .max (axis = 0 ), zdata .min (axis = 0 )
354- xdata , zdata = np .array (xdata ), np .array (zdata )
355- for i in range (len (xdata )):
356- for j in range (len (xlabels )):
357- xdata [i , j ] = (xdata [i , j ] - xmin [j ]) / (xmax [j ] - xmin [j ])
358- for j in range (len (zlabels )):
359- zdata [i , j ] = (zdata [i , j ] - zmin [j ]) / (zmax [j ] - zmin [j ])
369+ scaling_func_option = self .options ["scaling_function" ].value
370+
371+ scaler_instance = map_name_to_scaler [scaling_func_option ]
372+ xdata , xdata_bounds = scale_dataframe (xdata , scaler_instance )
373+ zdata , zdata_bounds = scale_dataframe (zdata , scaler_instance )
374+
375+ print (f"using scaling function: { scaling_func_option } " )
360376
361377 # method to create model
362378 def create_model ():
@@ -370,7 +386,7 @@ def create_model():
370386 input_bounds = xdata_bounds ,
371387 output_bounds = zdata_bounds ,
372388 normalized = True ,
373- normalization_form = "Linear" ,
389+ normalization_form = scaling_func_option ,
374390 )
375391
376392 outputs = layers (inputs ) # use network as function outputs = f(inputs)
0 commit comments