1
+ import pickle
2
+ import os
3
+ import shutil
4
+
5
+
6
+ class File_Operation :
7
+ """
8
+ This class shall be used to save the model after training
9
+ and load the saved model for prediction.
10
+
11
+
12
+
13
+ """
14
+ def __init__ (self ,file_object ,logger_object ):
15
+ self .file_object = file_object
16
+ self .logger_object = logger_object
17
+ self .model_directory = 'models/'
18
+
19
+ def save_model (self ,model ,filename ):
20
+ """
21
+ Method Name: save_model
22
+ Description: Save the model file to directory
23
+ Outcome: File gets saved
24
+ On Failure: Raise Exception
25
+
26
+
27
+ """
28
+ self .logger_object .log (self .file_object , 'Entered the save_model method of the File_Operation class' )
29
+ try :
30
+ path = os .path .join (self .model_directory ,filename ) #create seperate directory for each cluster
31
+ if os .path .isdir (path ): #remove previously existing models for each clusters
32
+ shutil .rmtree (self .model_directory )
33
+ os .makedirs (path )
34
+ else :
35
+ os .makedirs (path ) #
36
+ with open (path + '/' + filename + '.sav' ,
37
+ 'wb' ) as f :
38
+ pickle .dump (model , f ) # save the model to file
39
+ self .logger_object .log (self .file_object ,
40
+ 'Model File ' + filename + ' saved. Exited the save_model method of the Model_Finder class' )
41
+
42
+ return 'success'
43
+ except Exception as e :
44
+ self .logger_object .log (self .file_object ,'Exception occured in save_model method of the Model_Finder class. Exception message: ' + str (e ))
45
+ self .logger_object .log (self .file_object ,
46
+ 'Model File ' + filename + ' could not be saved. Exited the save_model method of the Model_Finder class' )
47
+ raise Exception ()
48
+
49
+ def load_model (self ,filename ):
50
+ """
51
+ Method Name: load_model
52
+ Description: load the model file to memory
53
+ Output: The Model file loaded in memory
54
+ On Failure: Raise Exception
55
+
56
+
57
+ """
58
+ self .logger_object .log (self .file_object , 'Entered the load_model method of the File_Operation class' )
59
+ try :
60
+ with open (self .model_directory + filename + '/' + filename + '.sav' ,
61
+ 'rb' ) as f :
62
+ self .logger_object .log (self .file_object ,
63
+ 'Model File ' + filename + ' loaded. Exited the load_model method of the Model_Finder class' )
64
+ return pickle .load (f )
65
+ except Exception as e :
66
+ self .logger_object .log (self .file_object ,
67
+ 'Exception occured in load_model method of the Model_Finder class. Exception message: ' + str (
68
+ e ))
69
+ self .logger_object .log (self .file_object ,
70
+ 'Model File ' + filename + ' could not be saved. Exited the load_model method of the Model_Finder class' )
71
+ raise Exception ()
72
+
73
+ def find_correct_model_file (self ,cluster_number ):
74
+ """
75
+ Method Name: find_correct_model_file
76
+ Description: Select the correct model based on cluster number
77
+ Output: The Model file
78
+ On Failure: Raise Exception
79
+
80
+
81
+ """
82
+ self .logger_object .log (self .file_object , 'Entered the find_correct_model_file method of the File_Operation class' )
83
+ try :
84
+ self .cluster_number = cluster_number
85
+ self .folder_name = self .model_directory
86
+ self .list_of_model_files = []
87
+ self .list_of_files = os .listdir (self .folder_name )
88
+ for self .file in self .list_of_files :
89
+ try :
90
+ if (self .file .index (str ( self .cluster_number ))!= - 1 ):
91
+ self .model_name = self .file
92
+ except :
93
+ continue
94
+ self .model_name = self .model_name .split ('.' )[0 ]
95
+ self .logger_object .log (self .file_object ,
96
+ 'Exited the find_correct_model_file method of the Model_Finder class.' )
97
+ return self .model_name
98
+ except Exception as e :
99
+ self .logger_object .log (self .file_object ,
100
+ 'Exception occured in find_correct_model_file method of the Model_Finder class. Exception message: ' + str (
101
+ e ))
102
+ self .logger_object .log (self .file_object ,
103
+ 'Exited the find_correct_model_file method of the Model_Finder class with Failure' )
104
+ raise Exception ()
0 commit comments