-
Notifications
You must be signed in to change notification settings - Fork 2
/
final_automate_iris.py~
executable file
·233 lines (150 loc) · 7.83 KB
/
final_automate_iris.py~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import os
########################## first time run ##########################################
print ("*** FIRST TIME RUN STARTING . . . ")
os.system("hadoop fs -rmr /user/hduser/iris_training.data")
# ^^^^^^^^^^^^^ path change required here^^^^^^^^^^^^^^^^^^^^^^^
# %%%%%%%%%%%%% input dataset can be changed here %%%%%%%%%%%%%%%%%%%%
os.system("hadoop fs -copyFromLocal /home/aditi/SVM_iris/iris_training2.data /user/hduser/iris_training.data")
print ("### TEST DATA COPIED")
#copying cases and data file to hdfs
os.system("hadoop fs -rmr /user/hduser/cases.txt")
# ^^^^^^^^^^^^^ path change required here^^^^^^^^^^^^^^^^^^^^^^^
os.system("hadoop fs -copyFromLocal /home/aditi/SVM_iris/cases.txt /user/hduser/")
print ("*** CASES.TXT COPIED TO HDFS")
print ("*** removing previous output directories")
os.system("hadoop fs -rmr /user/hduser/multi_output")
os.system("hadoop fs -rmr /user/hduser/multi_acc8")
print ("### TRAINING PART 1 RUNNING . . .")
# ^^^^^^^^^^^^^ 4 path changes required here ^^^^^^^^^^^^^^^^^^^^^^^
os.system("bin/hadoop jar contrib/streaming/hadoop-*streaming*.jar -file /home/aditi/SVM_iris/test.py -mapper /home/aditi/SVM_iris/test.py -file /home/aditi/SVM_iris/test_reducer.py -reducer /home/aditi/SVM_iris/test_reducer.py -cacheFile 'hdfs://aditi-Aspire-5741Z:54310/user/hduser/cases.txt#cases' -input /user/hduser/iris_training.data -output /user/hduser/multi_output")
print ("*** TRAINING PART 2 RUNNING . . .")
# ^^^^^^^^^^^^^ 4 path changes required here ^^^^^^^^^^^^^^^^^^^^^^^
os.system("bin/hadoop jar contrib/streaming/hadoop-*streaming*.jar -file /home/aditi/SVM_iris/svm_test.py -mapper /home/aditi/SVM_iris/svm_test.py -file /home/aditi/SVM_iris/svm_test_reducer.py -reducer /home/aditi/SVM_iris/svm_test_reducer.py -cacheFile 'hdfs://aditi-Aspire-5741Z:54310/user/hduser/multi_output/part-00000#SVMvalues.txt' -cacheFile 'hdfs://aditi-Aspire-5741Z:54310/user/hduser/cases.txt#cases.txt' -input /user/hduser/iris_training.data -output /user/hduser/multi_acc8")
os.system("rm -rf part-00000")
# ^^^^^^^^^^^^^ path change required here ^^^^^^^^^^^^^^^^^^^^^^^
os.system("bin/hadoop fs -copyToLocal /user/hduser/multi_acc8/part-00000 /home/aditi/SVM_iris/")
# ^^^^^^^^^^^^^ path change required here ^^^^^^^^^^^^^^^^^^^^^^^
f = open("/home/aditi/SVM_iris/part-00000", "r")
words = f.read().split()
posclass = words[0].split(",")
negclass = words[1].split(",")
index = 1
print ("### First Enqueue operation to pos neg file queue ")
#writing to posneg queue file ----------------------------------
# ^^^^^^^^^^^^^ path change required here ^^^^^^^^^^^^^^^^^^^^^^^
f1 = open("/home/aditi/SVM_iris/posneg_queue","a")
f1.write(str(index)+","+"p")
for x in posclass:
f1.write(","+x)
f1.write(",\n")
f1.write(str(index)+","+"n")
for x in negclass:
f1.write(","+x)
f1.write(",\n")
print ("Appending Content to Final_result")
#final result --------------------------------------------------- appending
# ^^^^^^^^^^^^^ path change required here ^^^^^^^^^^^^^^^^^^^^^^^
f2 = open("/home/aditi/SVM_iris/final_result","a")
f2.write(str(index)+","+"p")
for x in posclass:
f2.write(","+x)
f2.write("\t")
f2.write(str(index)+","+"n")
for x in negclass:
f2.write(","+x)
f2.write("\t")
f2.write(words[2]+"\t"+words[3]+"\t"+words[4]+"\n")
f.close()
f1.close()
f2.close()
#---------------------------------------
print ("******************** FIRST RUN OVER *********************************")
############################# first time run end ####################################
print ("@@@@@@@@@@@@@@@ WHILE RUN STARTS @@@@@@@@@@@@@@@@@@@@@@@")
i=0
while (1):
i = i+1
print ("----------------- Loop Run No ------"+ str(i) +"-----------")
#dequeue business --------------------------------
print ("************** Deque Operation performing . . . *******************")
# ^^^^^^^^^^^^^ path change required here ^^^^^^^^^^^^^^^^^^^^^^^
with open('/home/aditi/SVM_iris/posneg_queue', 'r') as fin:
data = fin.read().splitlines(True)
if(not data):
print ("!!!!!!!!!!! Nothing to Dequeue , Voila Process Done ! !!!!!!!!!!!!")
break
# ^^^^^^^^^^^^^ path change required here ^^^^^^^^^^^^^^^^^^^^^^^
with open('/home/aditi/SVM_iris/posneg_queue', 'w') as fout:
if(len(data)==1 ):
fout.truncate()
else:
fout.writelines(data[1:])
tocases = data[0].split(",")
tocases1 = tocases[2:-1]
argument = ""
for k in tocases1:
argument += " "+ k
#-------------------------------------
print ("$$$$$$$$$$$$$ Calculating Node Number $$$$$$$$$$$$$$$")
#set index
if (tocases[1]=='p'):
index = int(tocases[0])*2
if (tocases[1]=='n'):
index = (int(tocases[0])*2)+1
print ("______________ Running Cases .py _________________________")
# ^^^^^^^^^^^^^ path change required here ^^^^^^^^^^^^^^^^^^^^^^^
os.system ("python /home/aditi/SVM_iris/cases.py "+ str(len(tocases1)) + argument )
os.system("hadoop fs -rmr /user/hduser/cases.txt")
# ^^^^^^^^^^^^^ path change required here ^^^^^^^^^^^^^^^^^^^^^^^
os.system("hadoop fs -copyFromLocal /home/aditi/SVM_iris/cases.txt /user/hduser/")
print ("*** CASES.TXT COPIED TO HDFS")
print ("*** removing previous output directories")
os.system("hadoop fs -rmr /user/hduser/multi_output")
os.system("hadoop fs -rmr /user/hduser/multi_acc8")
print ("### TRAINING PART 1 RUNNING . . .")
# ^^^^^^^^^^^^^ 4 path changes required here ^^^^^^^^^^^^^^^^^^^^^^^
os.system("bin/hadoop jar contrib/streaming/hadoop-*streaming*.jar -file /home/aditi/SVM_iris/test.py -mapper /home/aditi/SVM_iris/test.py -file /home/aditi/SVM_iris/test_reducer.py -reducer /home/aditi/SVM_iris/test_reducer.py -cacheFile 'hdfs://aditi-Aspire-5741Z:54310/user/hduser/cases.txt#cases' -input /user/hduser/iris_training.data -output /user/hduser/multi_output")
print ("### TRAINING PART 2 RUNNING . . .")
# ^^^^^^^^^^^^^ 4 path changes required here ^^^^^^^^^^^^^^^^^^^^^^^
os.system("bin/hadoop jar contrib/streaming/hadoop-*streaming*.jar -file /home/aditi/SVM_iris/svm_test.py -mapper /home/aditi/SVM_iris/svm_test.py -file /home/aditi/SVM_iris/svm_test_reducer.py -reducer /home/aditi/SVM_iris/svm_test_reducer.py -cacheFile 'hdfs://aditi-Aspire-5741Z:54310/user/hduser/multi_output/part-00000#SVMvalues' -cacheFile 'hdfs://aditi-Aspire-5741Z:54310/user/hduser/cases.txt#cases' -input /user/hduser/iris_training.data -output /user/hduser/multi_acc8")
os.system("rm -rf part-00000")
# ^^^^^^^^^^^^^ path change required here ^^^^^^^^^^^^^^^^^^^^^^^
os.system("bin/hadoop fs -copyToLocal /user/hduser/multi_acc8/part-00000 /home/aditi/SVM_iris/")
# ^^^^^^^^^^^^^ path change required here ^^^^^^^^^^^^^^^^^^^^^^^
f = open("/home/aditi/SVM_iris/part-00000", "r")
words = f.read().split()
posclass = words[0].split(",")
negclass = words[1].split(",")
#writing to posneg queue file ----------------------------------
if (len(posclass) > 1):
print ("### Pos Class Enqueue operation")
# ^^^^^^^^^^^^^ path change required here ^^^^^^^^^^^^^^^^^^^^^^^
f1 = open("/home/aditi/SVM_iris/posneg_queue","a")
f1.write(str(index)+","+"p")
for x in posclass:
f1.write(","+x)
f1.write(",\n")
if (len(negclass) > 1):
print ("### Neg Class Enqueue operation")
f1.write(str(index)+","+"n")
for x in negclass:
f1.write(","+x)
f1.write(",\n")
#os.system("cat posneg_queue")
print ("^^^^^^^^^^^^ Appending Result to final_result ^^^^^^^^^^^^^^^^^^^^^^")
#final result --------------------------------------------------- appending
# ^^^^^^^^^^^^^ path change required here ^^^^^^^^^^^^^^^^^^^^^^^
f2 = open("/home/aditi/SVM_iris/final_result","a")
f2.write(str(index)+","+"p")
for x in posclass:
f2.write(","+x)
f2.write("\t")
f2.write(str(index)+","+"n")
for x in negclass:
f2.write(","+x)
f2.write("\t")
f2.write(words[2]+"\t"+words[3]+"\t"+words[4]+"\n")
f.close()
f1.close()
f2.close()
print (" ---------------------- While Loop No -----------"+ str(i) +"------------- ENDS")