Coverage for biobb_ml/neural_networks/autoencoder_neural_network.py: 89%
151 statements
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-07 09:39 +0000
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-07 09:39 +0000
1#!/usr/bin/env python3
3"""Module containing the AutoencoderNeuralNetwork class and the command line interface."""
4import argparse
5import h5py
6import json
7import numpy as np
8import pandas as pd
9from biobb_common.generic.biobb_object import BiobbObject
10from tensorflow.python.keras.saving import hdf5_format
11from tensorflow.keras.models import Model
12from tensorflow.keras.layers import Input, LSTM, Dense, RepeatVector, TimeDistributed
13from biobb_common.configuration import settings
14from biobb_common.tools import file_utils as fu
15from biobb_common.tools.file_utils import launchlogger
16from biobb_ml.neural_networks.common import check_input_path, check_output_path
19class AutoencoderNeuralNetwork(BiobbObject):
20 """
21 | biobb_ml AutoencoderNeuralNetwork
22 | Wrapper of the TensorFlow Keras LSTM method for encoding.
23 | Fits and tests a given dataset and save the compiled model for an Autoencoder Neural Network. Visit the `LSTM documentation page <https://www.tensorflow.org/api_docs/python/tf/keras/layers/LSTM>`_ in the TensorFlow Keras official website for further information.
25 Args:
26 input_decode_path (str): Path to the input decode dataset. File type: input. `Sample file <https://github.com/bioexcel/biobb_ml/raw/master/biobb_ml/test/data/neural_networks/dataset_autoencoder_decode.csv>`_. Accepted formats: csv (edam:format_3752).
27 input_predict_path (str) (Optional): Path to the input predict dataset. File type: input. `Sample file <https://github.com/bioexcel/biobb_ml/raw/master/biobb_ml/test/data/neural_networks/dataset_autoencoder_predict.csv>`_. Accepted formats: csv (edam:format_3752).
28 output_model_path (str): Path to the output model file. File type: output. `Sample file <https://github.com/bioexcel/biobb_ml/raw/master/biobb_ml/test/reference/neural_networks/ref_output_model_autoencoder.h5>`_. Accepted formats: h5 (edam:format_3590).
29 output_test_decode_path (str) (Optional): Path to the test decode table file. File type: output. `Sample file <https://github.com/bioexcel/biobb_ml/raw/master/biobb_ml/test/reference/neural_networks/ref_output_test_decode_autoencoder.csv>`_. Accepted formats: csv (edam:format_3752).
30 output_test_predict_path (str) (Optional): Path to the test predict table file. File type: output. `Sample file <https://github.com/bioexcel/biobb_ml/raw/master/biobb_ml/test/reference/neural_networks/ref_output_test_predict_autoencoder.csv>`_. Accepted formats: csv (edam:format_3752).
31 properties (dic - Python dictionary object containing the tool parameters, not input/output files):
32 * **optimizer** (*string*) - ("Adam") Name of optimizer instance. Values: Adadelta (Adadelta optimization is a stochastic gradient descent method that is based on adaptive learning rate per dimension to address two drawbacks: the continual decay of learning rates throughout training and the need for a manually selected global learning rate), Adagrad (Adagrad is an optimizer with parameter-specific learning rates; which are adapted relative to how frequently a parameter gets updated during training. The more updates a parameter receives; the smaller the updates), Adam (Adam optimization is a stochastic gradient descent method that is based on adaptive estimation of first-order and second-order moments), Adamax (It is a variant of Adam based on the infinity norm. Default parameters follow those provided in the paper. Adamax is sometimes superior to adam; specially in models with embeddings), Ftrl (Optimizer that implements the FTRL algorithm), Nadam (Much like Adam is essentially RMSprop with momentum; Nadam is Adam with Nesterov momentum), RMSprop (Optimizer that implements the RMSprop algorithm), SGD (Gradient descent -with momentum- optimizer).
33 * **learning_rate** (*float*) - (0.02) [0~100|0.01] Determines the step size at each iteration while moving toward a minimum of a loss function
34 * **batch_size** (*int*) - (100) [0~1000|1] Number of samples per gradient update.
35 * **max_epochs** (*int*) - (100) [0~1000|1] Number of epochs to train the model. As the early stopping is enabled, this is a maximum.
36 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files.
37 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist.
39 Examples:
40 This is a use example of how to use the building block from Python::
42 from biobb_ml.neural_networks.autoencoder_neural_network import autoencoder_neural_network
43 prop = {
44 'optimizer': 'Adam',
45 'learning_rate': 0.01,
46 'batch_size': 32,
47 'max_epochs': 300
48 }
49 autoencoder_neural_network(input_decode_path='/path/to/myDecodeDataset.csv',
50 output_model_path='/path/to/newModel.h5',
51 input_predict_path='/path/to/myPredictDataset.csv',
52 output_test_decode_path='/path/to/newDecodeDataset.csv',
53 output_test_predict_path='/path/to/newPredictDataset.csv',
54 properties=prop)
56 Info:
57 * wrapped_software:
58 * name: TensorFlow Keras LSTM
59 * version: >2.1.0
60 * license: MIT
61 * ontology:
62 * name: EDAM
63 * schema: http://edamontology.org/EDAM.owl
65 """
67 def __init__(self, input_decode_path, output_model_path,
68 input_predict_path=None, output_test_decode_path=None,
69 output_test_predict_path=None, properties=None, **kwargs) -> None:
70 properties = properties or {}
72 # Call parent class constructor
73 super().__init__(properties)
74 self.locals_var_dict = locals().copy()
76 # Input/Output files
77 self.io_dict = {
78 "in": {"input_decode_path": input_decode_path, "input_predict_path": input_predict_path},
79 "out": {"output_model_path": output_model_path, "output_test_decode_path": output_test_decode_path, "output_test_predict_path": output_test_predict_path}
80 }
82 # Properties specific for BB
83 self.optimizer = properties.get('optimizer', 'Adam')
84 self.learning_rate = properties.get('learning_rate', 0.02)
85 self.batch_size = properties.get('batch_size', 100)
86 self.max_epochs = properties.get('max_epochs', 100)
87 self.properties = properties
89 # Check the properties
90 self.check_properties(properties)
91 self.check_arguments()
93 def check_data_params(self, out_log, err_log):
94 """ Checks all the input/output paths and parameters """
95 self.io_dict["in"]["input_decode_path"] = check_input_path(self.io_dict["in"]["input_decode_path"], "input_decode_path", False, out_log, self.__class__.__name__)
96 if self.io_dict["in"]["input_predict_path"]:
97 self.io_dict["in"]["input_predict_path"] = check_input_path(self.io_dict["in"]["input_predict_path"], "input_predict_path", True, out_log, self.__class__.__name__)
98 self.io_dict["out"]["output_model_path"] = check_output_path(self.io_dict["out"]["output_model_path"], "output_model_path", False, out_log, self.__class__.__name__)
99 if self.io_dict["out"]["output_test_decode_path"]:
100 self.io_dict["out"]["output_test_decode_path"] = check_output_path(self.io_dict["out"]["output_test_decode_path"], "output_test_decode_path", True, out_log, self.__class__.__name__)
101 if self.io_dict["out"]["output_test_predict_path"]:
102 self.io_dict["out"]["output_test_predict_path"] = check_output_path(self.io_dict["out"]["output_test_predict_path"], "output_test_predict_path", True, out_log, self.__class__.__name__)
104 def build_model(self, n_in, n_out=None):
105 """ Builds Neural network """
107 # outputs list
108 outputs = []
110 # define encoder
111 visible = Input(shape=(n_in, 1))
112 encoder = LSTM(100, activation='relu')(visible)
114 # define reconstruct decoder
115 decoder1 = RepeatVector(n_in)(encoder)
116 decoder1 = LSTM(100, activation='relu', return_sequences=True)(decoder1)
117 decoder1 = TimeDistributed(Dense(1))(decoder1)
119 outputs.append(decoder1)
121 # define predict decoder
122 if n_out:
123 decoder2 = RepeatVector(n_out)(encoder)
124 decoder2 = LSTM(100, activation='relu', return_sequences=True)(decoder2)
125 decoder2 = TimeDistributed(Dense(1))(decoder2)
126 outputs.append(decoder2)
128 # tie it together
129 model = Model(inputs=visible, outputs=outputs)
131 return model
133 @launchlogger
134 def launch(self) -> int:
135 """Execute the :class:`AutoencoderNeuralNetwork <neural_networks.autoencoder_neural_network.AutoencoderNeuralNetwork>` neural_networks.autoencoder_neural_network.AutoencoderNeuralNetwork object."""
137 # check input/output paths and parameters
138 self.check_data_params(self.out_log, self.err_log)
140 # Setup Biobb
141 if self.check_restart():
142 return 0
143 self.stage_files()
145 # load decode dataset
146 fu.log('Getting decode dataset from %s' % self.io_dict["in"]["input_decode_path"], self.out_log, self.global_log)
147 data_dec = pd.read_csv(self.io_dict["in"]["input_decode_path"])
148 seq_in = np.array(data_dec)
150 # reshape input into [samples, timesteps, features]
151 n_in = len(seq_in)
152 seq_in = seq_in.reshape((1, n_in, 1))
154 # load predict dataset
155 n_out = None
156 if (self.io_dict["in"]["input_predict_path"]):
157 fu.log('Getting predict dataset from %s' % self.io_dict["in"]["input_predict_path"], self.out_log, self.global_log)
158 data_pred = pd.read_csv(self.io_dict["in"]["input_predict_path"])
159 seq_out = np.array(data_pred)
161 # reshape output into [samples, timesteps, features]
162 n_out = len(seq_out)
163 seq_out = seq_out.reshape((1, n_out, 1))
165 # build model
166 fu.log('Building model', self.out_log, self.global_log)
167 model = self.build_model(n_in, n_out)
169 # model summary
170 stringlist = []
171 model.summary(print_fn=lambda x: stringlist.append(x))
172 model_summary = "\n".join(stringlist)
173 fu.log('Model summary:\n\n%s\n' % model_summary, self.out_log, self.global_log)
175 # get optimizer
176 mod = __import__('tensorflow.keras.optimizers', fromlist=[self.optimizer])
177 opt_class = getattr(mod, self.optimizer)
178 opt = opt_class(lr=self.learning_rate)
179 # compile model
180 model.compile(optimizer=opt, loss='mse', metrics=['mse', 'mae'])
182 # fitting
183 fu.log('Training model', self.out_log, self.global_log)
184 y_list = [seq_in]
185 if n_out:
186 y_list.append(seq_out)
187 # fit the model
188 mf = model.fit(seq_in,
189 y_list,
190 batch_size=self.batch_size,
191 epochs=self.max_epochs,
192 verbose=1)
194 train_metrics = pd.DataFrame()
195 metric = []
196 coefficient = []
197 for key, lst in mf.history.items():
198 metric.append(' '.join(x.capitalize() or '_' for x in key.split('_')))
199 coefficient.append(lst[-1])
201 train_metrics['metric'] = metric
202 train_metrics['coefficient'] = coefficient
204 fu.log('Calculating metrics\n\nMETRICS TABLE\n\n%s\n' % train_metrics, self.out_log, self.global_log)
206 # predicting
207 fu.log('Predicting model', self.out_log, self.global_log)
208 yhat = model.predict(seq_in, verbose=1)
210 decoding_table = pd.DataFrame()
211 if (self.io_dict["in"]["input_predict_path"]):
212 decoding_table['reconstructed'] = np.squeeze(np.asarray(yhat[0][0]))
213 decoding_table['original'] = data_dec
214 else:
215 decoding_table['reconstructed'] = np.squeeze(np.asarray(yhat[0]))
216 decoding_table['original'] = np.squeeze(np.asarray(data_dec))
217 decoding_table['residual'] = decoding_table['original'] - decoding_table['reconstructed']
218 decoding_table['difference %'] = np.absolute(decoding_table['residual']/decoding_table['original']*100)
219 pd.set_option('display.float_format', lambda x: '%.5f' % x)
220 # sort by difference in %
221 decoding_table = decoding_table.sort_values(by=['difference %'])
222 decoding_table = decoding_table.reset_index(drop=True)
223 fu.log('RECONSTRUCTION TABLE\n\n%s\n' % decoding_table, self.out_log, self.global_log)
225 # save reconstruction data
226 if (self.io_dict["out"]["output_test_decode_path"]):
227 fu.log('Saving reconstruction data to %s' % self.io_dict["out"]["output_test_decode_path"], self.out_log, self.global_log)
228 decoding_table.to_csv(self.io_dict["out"]["output_test_decode_path"], index=False, header=True)
230 if (self.io_dict["in"]["input_predict_path"]):
231 prediction_table = pd.DataFrame()
232 prediction_table['predicted'] = np.squeeze(np.asarray(yhat[1][0]))
233 prediction_table['original'] = data_pred
234 prediction_table['residual'] = prediction_table['original'] - prediction_table['predicted']
235 prediction_table['difference %'] = np.absolute(prediction_table['residual']/prediction_table['original']*100)
236 pd.set_option('display.float_format', lambda x: '%.5f' % x)
237 # sort by difference in %
238 prediction_table = prediction_table.sort_values(by=['difference %'])
239 prediction_table = prediction_table.reset_index(drop=True)
240 fu.log('PREDICTION TABLE\n\n%s\n' % prediction_table, self.out_log, self.global_log)
242 # save decoding data
243 if (self.io_dict["out"]["output_test_predict_path"]):
244 fu.log('Saving prediction data to %s' % self.io_dict["out"]["output_test_predict_path"], self.out_log, self.global_log)
245 prediction_table.to_csv(self.io_dict["out"]["output_test_predict_path"], index=False, header=True)
247 # save model and parameters
248 vars_obj = {
249 'type': 'autoencoder'
250 }
251 variables = json.dumps(vars_obj)
252 fu.log('Saving model to %s' % self.io_dict["out"]["output_model_path"], self.out_log, self.global_log)
253 with h5py.File(self.io_dict["out"]["output_model_path"], mode='w') as f:
254 hdf5_format.save_model_to_hdf5(model, f)
255 f.attrs['variables'] = variables
257 # Copy files to host
258 self.copy_to_host()
260 self.tmp_files.extend([
261 self.stage_io_dict.get("unique_dir")
262 ])
263 self.remove_tmp_files()
265 self.check_arguments(output_files_created=True, raise_exception=False)
267 return 0
270def autoencoder_neural_network(input_decode_path: str, output_model_path: str, input_predict_path: str = None, output_test_decode_path: str = None, output_test_predict_path: str = None, properties: dict = None, **kwargs) -> int:
271 """Execute the :class:`AutoencoderNeuralNetwork <neural_networks.autoencoder_neural_network.AutoencoderNeuralNetwork>` class and
272 execute the :meth:`launch() <neural_networks.autoencoder_neural_network.AutoencoderNeuralNetwork.launch>` method."""
274 return AutoencoderNeuralNetwork(input_decode_path=input_decode_path,
275 output_model_path=output_model_path,
276 input_predict_path=input_predict_path,
277 output_test_decode_path=output_test_decode_path,
278 output_test_predict_path=output_test_predict_path,
279 properties=properties, **kwargs).launch()
282def main():
283 """Command line execution of this building block. Please check the command line documentation."""
284 parser = argparse.ArgumentParser(description="Wrapper of the TensorFlow Keras LSTM method for encoding.", formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999))
285 parser.add_argument('--config', required=False, help='Configuration file')
287 # Specific args of each building block
288 required_args = parser.add_argument_group('required arguments')
289 required_args.add_argument('--input_decode_path', required=True, help='Path to the input decode dataset. Accepted formats: csv.')
290 parser.add_argument('--input_predict_path', required=False, help='Path to the input predict dataset. Accepted formats: csv.')
291 required_args.add_argument('--output_model_path', required=True, help='Path to the output model file. Accepted formats: h5.')
292 parser.add_argument('--output_test_decode_path', required=False, help='Path to the test decode table file. Accepted formats: csv.')
293 parser.add_argument('--output_test_predict_path', required=False, help='Path to the test predict table file. Accepted formats: csv.')
295 args = parser.parse_args()
296 args.config = args.config or "{}"
297 properties = settings.ConfReader(config=args.config).get_prop_dic()
299 # Specific call of each building block
300 autoencoder_neural_network(input_decode_path=args.input_decode_path,
301 output_model_path=args.output_model_path,
302 input_predict_path=args.input_predict_path,
303 output_test_decode_path=args.output_test_decode_path,
304 output_test_predict_path=args.output_test_predict_path,
305 properties=properties)
308if __name__ == '__main__':
309 main()