Skip to content

Commit

Permalink
Formatting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
franflame committed Sep 5, 2023
1 parent a7a1841 commit 39eda3b
Showing 1 changed file with 39 additions and 34 deletions.
73 changes: 39 additions & 34 deletions foqus_lib/framework/surrogate/keras_nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ def get_config(self):
)
return config

Check warning on line 144 in foqus_lib/framework/surrogate/keras_nn.py

View check run for this annotation

Codecov / codecov/patch

foqus_lib/framework/surrogate/keras_nn.py#L144

Added line #L144 was not covered by tests


def checkAvailable():
"""Plug-ins should have this function to check availability of any
additional required software. If requirements are not available
Expand Down Expand Up @@ -188,11 +189,11 @@ def __init__(self, dat=None):
" text-indent: -22px ;\n"
"}\n"
"</head>\n"
"The high-level neural network library of Keras integrates with TensorFlow’s"
"machine learning library to train complex models within Python’s user-friendly framework."
"The high-level neural network library of Keras integrates with TensorFlow’s"
"machine learning library to train complex models within Python’s user-friendly framework."
"Keras models may be largely split into two types: Sequential which build linearly connected"
"model layers, and Functional which build multiple interconnected layers in a complex system."
"More information on TensorFlow Keras model building is described by (Wu et al. 2020)."
"model layers, and Functional which build multiple interconnected layers in a complex system."
"More information on TensorFlow Keras model building is described by (Wu et al. 2020)."
"Users may follow the recommended workflow to install and use TensorFlow in a Python environment,"
"as described in the TensorFlow documentation: https://www.tensorflow.org/install."
'<p class="keras_nn</p>"</html>'
Expand Down Expand Up @@ -232,7 +233,7 @@ def __init__(self, dat=None):
"on specific data for each variable.",
validValues=["All", "None"],
)

self.options.add(
name="n_hidden",
section="DATA Settings",
Expand All @@ -241,7 +242,7 @@ def __init__(self, dat=None):
desc="Number of hidden layers",
hint="Number of hidden layers",
)

self.options.add(
name="n_neurons",
section="DATA Settings",
Expand All @@ -259,7 +260,7 @@ def __init__(self, dat=None):
hint="Layer activation function",
validValues=["relu", "sigmoid"],
)

self.options.add(
name="out_act",
section="DATA Settings",
Expand All @@ -269,16 +270,18 @@ def __init__(self, dat=None):
hint="Output activation function",
validValues=["relu", "sigmoid"],
)

self.options.add(
name="input_file",
section="DATA Settings",
dtype=str,
default=str(Path(__file__).with_name("MEA_carbon_capture_dataset_mimo.csv")),
default=str(
Path(__file__).with_name("MEA_carbon_capture_dataset_mimo.csv")
),
desc="Input file",
hint="Input file path",
)

self.options.add(
name="input_labels",
section="DATA Settings",
Expand All @@ -287,7 +290,7 @@ def __init__(self, dat=None):
desc="comma separated",
hint="Input columns",
)

self.options.add(
name="output_labels",
section="DATA Settings",
Expand All @@ -296,7 +299,7 @@ def __init__(self, dat=None):
desc="comma separated",
hint="Output columns",
)

self.options.add(
name="numpy_seed",
section="DATA Settings",
Expand All @@ -305,7 +308,7 @@ def __init__(self, dat=None):
desc="Seed for numpy",
hint="Seed for numpy",
)

self.options.add(
name="random_seed",
section="DATA Settings",
Expand All @@ -314,7 +317,7 @@ def __init__(self, dat=None):
desc="Seed for random",
hint="Seed for random",
)

self.options.add(
name="tensorflow_seed",
section="DATA Settings",
Expand All @@ -323,7 +326,7 @@ def __init__(self, dat=None):
desc="Seed for tensorflow",
hint="Seed for tensorflow",
)

self.options.add(
name="epoch",
section="DATA Settings",
Expand All @@ -332,7 +335,7 @@ def __init__(self, dat=None):
desc="Number of epochs",
hint="Number of epochs",
)

self.options.add(
name="verbose",
section="DATA Settings",
Expand All @@ -341,7 +344,7 @@ def __init__(self, dat=None):
desc="Verbose setting",
hint="Takes only 0 or 1",
)

def run(self):
"""
This function overloads the Thread class function,
Expand All @@ -355,18 +358,18 @@ def run(self):
np_seed = self.options["numpy_seed"].value
rn_seed = self.options["random_seed"].value
tf_seed = self.options["tensorflow_seed"].value

Check warning on line 360 in foqus_lib/framework/surrogate/keras_nn.py

View check run for this annotation

Codecov / codecov/patch

foqus_lib/framework/surrogate/keras_nn.py#L358-L360

Added lines #L358 - L360 were not covered by tests

epochs_count = self.options["epoch"].value
verbose_setting = self.options["verbose"].value
verbose_setting = self.options["verbose"].value

Check warning on line 363 in foqus_lib/framework/surrogate/keras_nn.py

View check run for this annotation

Codecov / codecov/patch

foqus_lib/framework/surrogate/keras_nn.py#L362-L363

Added lines #L362 - L363 were not covered by tests

# np.random.seed(46)
# rn.seed(1342)
# tf.random.set_seed(62)

np.random.seed(np_seed)
rn.seed(rn_seed)
tf.random.set_seed(tf_seed)

Check warning on line 371 in foqus_lib/framework/surrogate/keras_nn.py

View check run for this annotation

Codecov / codecov/patch

foqus_lib/framework/surrogate/keras_nn.py#L369-L371

Added lines #L369 - L371 were not covered by tests

self.msgQueue.put("Starting training process")

Check warning on line 373 in foqus_lib/framework/surrogate/keras_nn.py

View check run for this annotation

Codecov / codecov/patch

foqus_lib/framework/surrogate/keras_nn.py#L373

Added line #L373 was not covered by tests

# Example follows the sequence below:
Expand All @@ -377,29 +380,29 @@ def run(self):
# attributes to CustomLayer class object
# 4) Back to create_model() to compile and train model
# 5) Back to code at end of file to save, load and test model

# import data
#data = pd.read_csv(r"/Users/sfhome/Desktop/CCI_Spring_2023/foqus/CCSI-Toolset/FOQUS/examples/other_files/ML_AI_Plugin/MEA_carbon_capture_dataset_mimo.csv")
# data = pd.read_csv(r"/Users/sfhome/Desktop/CCI_Spring_2023/foqus/CCSI-Toolset/FOQUS/examples/other_files/ML_AI_Plugin/MEA_carbon_capture_dataset_mimo.csv")
current_directory = os.path.dirname(__file__)

Check warning on line 386 in foqus_lib/framework/surrogate/keras_nn.py

View check run for this annotation

Codecov / codecov/patch

foqus_lib/framework/surrogate/keras_nn.py#L386

Added line #L386 was not covered by tests

input_file = str(self.options["input_file"].value)

Check warning on line 388 in foqus_lib/framework/surrogate/keras_nn.py

View check run for this annotation

Codecov / codecov/patch

foqus_lib/framework/surrogate/keras_nn.py#L388

Added line #L388 was not covered by tests

# data = pd.read_csv(current_directory + os.path.sep + input_file)
data = pd.read_csv(input_file)

Check warning on line 391 in foqus_lib/framework/surrogate/keras_nn.py

View check run for this annotation

Codecov / codecov/patch

foqus_lib/framework/surrogate/keras_nn.py#L391

Added line #L391 was not covered by tests
# define these two after xlabels and zlabels
# xdata = data.iloc[:, :6] # there are 6 input variables/columns
# zdata = data.iloc[:, 6:] # the rest are output variables/columns

# xlabels = xdata.columns.tolist() # set labels as a list (default) from pandas
# zlabels = zdata.columns.tolist() # is a set of IndexedDataSeries objects

xlabels = str(self.options["input_labels"].value).split(",")
zlabels = str(self.options["output_labels"].value).split(",")

Check warning on line 400 in foqus_lib/framework/surrogate/keras_nn.py

View check run for this annotation

Codecov / codecov/patch

foqus_lib/framework/surrogate/keras_nn.py#L399-L400

Added lines #L399 - L400 were not covered by tests
#tried the below code that didn't work

# tried the below code that didn't work
xdata = data.loc[:, xlabels] # there are 6 input variables/columns
zdata = data.loc[:, zlabels] # the rest are output variables/columns

Check warning on line 404 in foqus_lib/framework/surrogate/keras_nn.py

View check run for this annotation

Codecov / codecov/patch

foqus_lib/framework/surrogate/keras_nn.py#L403-L404

Added lines #L403 - L404 were not covered by tests

xdata_bounds = {i: (xdata[i].min(), xdata[i].max()) for i in xdata} # x bounds
zdata_bounds = {j: (zdata[j].min(), zdata[j].max()) for j in zdata} # z bounds

Expand All @@ -424,14 +427,14 @@ def run(self):
# define x and z data, not used but will add to variable dictionary
xdata = model_data[:, :-2]
zdata = model_data[:, -2:]

Check warning on line 429 in foqus_lib/framework/surrogate/keras_nn.py

View check run for this annotation

Codecov / codecov/patch

foqus_lib/framework/surrogate/keras_nn.py#L428-L429

Added lines #L428 - L429 were not covered by tests

# method to create model
def create_model(data):

Check warning on line 432 in foqus_lib/framework/surrogate/keras_nn.py

View check run for this annotation

Codecov / codecov/patch

foqus_lib/framework/surrogate/keras_nn.py#L432

Added line #L432 was not covered by tests

inputs = tf.keras.Input(shape=(np.shape(data)[1],)) # create input layer

Check warning on line 434 in foqus_lib/framework/surrogate/keras_nn.py

View check run for this annotation

Codecov / codecov/patch

foqus_lib/framework/surrogate/keras_nn.py#L434

Added line #L434 was not covered by tests

layers = keras_nn( # define the rest of network using our custom class

Check warning on line 436 in foqus_lib/framework/surrogate/keras_nn.py

View check run for this annotation

Codecov / codecov/patch

foqus_lib/framework/surrogate/keras_nn.py#L436

Added line #L436 was not covered by tests
#add our 4 options
# add our 4 options
input_labels=xlabels,
output_labels=zlabels,
input_bounds=xdata_bounds,
Expand All @@ -446,14 +449,16 @@ def create_model(data):

model.compile(loss="mse", optimizer="RMSprop", metrics=["mae", "mse"])

Check warning on line 450 in foqus_lib/framework/surrogate/keras_nn.py

View check run for this annotation

Codecov / codecov/patch

foqus_lib/framework/surrogate/keras_nn.py#L450

Added line #L450 was not covered by tests

model.fit(xdata, zdata, epochs=epochs_count, verbose=verbose_setting) # train model
model.fit(

Check warning on line 452 in foqus_lib/framework/surrogate/keras_nn.py

View check run for this annotation

Codecov / codecov/patch

foqus_lib/framework/surrogate/keras_nn.py#L452

Added line #L452 was not covered by tests
xdata, zdata, epochs=epochs_count, verbose=verbose_setting
) # train model

return model

Check warning on line 456 in foqus_lib/framework/surrogate/keras_nn.py

View check run for this annotation

Codecov / codecov/patch

foqus_lib/framework/surrogate/keras_nn.py#L456

Added line #L456 was not covered by tests

# create model
model = create_model(xdata)
model.summary()

Check warning on line 460 in foqus_lib/framework/surrogate/keras_nn.py

View check run for this annotation

Codecov / codecov/patch

foqus_lib/framework/surrogate/keras_nn.py#L459-L460

Added lines #L459 - L460 were not covered by tests
#self.msgQueue.put(model)
# self.msgQueue.put(model)

self.msgQueue.put("Training complete")

Check warning on line 463 in foqus_lib/framework/surrogate/keras_nn.py

View check run for this annotation

Codecov / codecov/patch

foqus_lib/framework/surrogate/keras_nn.py#L463

Added line #L463 was not covered by tests
# save model as H5
Expand Down

0 comments on commit 39eda3b

Please sign in to comment.