From cd1154a5cd00d0409848cecc0f165c61dca3e61f Mon Sep 17 00:00:00 2001 From: krishnakumar b <79183768+krishnakumarbhat@users.noreply.github.com> Date: Thu, 9 Mar 2023 23:17:04 +0530 Subject: [PATCH] updated model.bin and scaler.bin i changed this because The initial code was not as safe as the second one because it uses the open() function to open the file and write to it, but it does not use the with statement to automatically close the file when the operation is completed or an error occurs. This means that if an error occurs during the write operation, the file may not be closed properly, which could lead to data loss or file corruption --- modules/flowmldetection/flowmldetection.py | 27 +++++++++++----------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/modules/flowmldetection/flowmldetection.py b/modules/flowmldetection/flowmldetection.py index 1ccdf0867..1e0466669 100644 --- a/modules/flowmldetection/flowmldetection.py +++ b/modules/flowmldetection/flowmldetection.py @@ -337,19 +337,20 @@ def detect(self): self.print(X_flow) self.print(traceback.print_exc(),0,1) - def store_model(self): - """ - Store the trained model on disk - """ - self.print(f'Storing the trained model and scaler on disk.', 0, 2) - f = open('./modules/flowmldetection/model.bin', 'wb') - data = pickle.dumps(self.clf) - f.write(data) - f.close() - g = open('./modules/flowmldetection/scaler.bin', 'wb') - data = pickle.dumps(self.scaler) - g.write(data) - g.close() +def store_model(self): + """ + Store the trained model on disk if it has been updated + """ + if self.model_updated: + self.print(f'Storing the updated trained model and scaler on disk.', 0, 2) + with open('./modules/flowmldetection/model.bin', 'wb') as f: + pickle.dump(self.clf, f) + with open('./modules/flowmldetection/scaler.bin', 'wb') as g: + pickle.dump(self.scaler, g) + self.model_updated = False + else: + self.print(f'Trained model has not been updated. Not storing on disk.', 0, 2) + def read_model(self): """