-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
757 lines (643 loc) · 34.1 KB
/
Main.java
File metadata and controls
757 lines (643 loc) · 34.1 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
import org.rosuda.REngine.*;
import java.util.*;
import java.io.*;
import java.nio.file.*;
import javax.swing.JOptionPane;
import javax.swing.JFileChooser;
/**
* This is the main class containing interface code for R and Java. This will call machine learning code in
* R to use and create the ML models.
*
* @author (Omar Farooq)
* @version (6/2/14)
*/
public class Main
{
static final double MATCH_THRESHOLD = 0.5; // this threshold determines if two output given by ANN match
static final String CLASS_PATH = (new File("").getAbsolutePath());
static final String DEFAULT_CONFIG_NAME = "def.conf";
//String sourceCode ; //this String contains the R source code
//REXP results ; //this is the REXP object that contains the results
static REngine re; //the R engine for calling the R code
static boolean readWeights = false; //was the network already trained which means weights would be read from file
static boolean saveWeights = false; //should you save weights in a file
static double percentTrain = 1.0; //the percent of the input data used for training
static String predVariable = "default10yr";
static int numFeatures = 0;
static String[] featureVariables = new String[]{"age","LTI" } ;
static boolean saveOutput = false; //should the output be written to a file
static String printColumn = "";
static boolean wasModelCreated = false; //was the program run and a model created
static String outputString;
public static void main (String[] args)
{
try{
String modelType="";
if(args.length==0){
System.err.println("ERROR: There were no arguments specified.Usage = <Configuration file> <Type of model>");
System.err.println("Type of Model -> ANN,SVM,BAY for Neural network, support vectore machine or naive bayesian");
System.exit(1);
}
else if(args.length ==1){
System.err.println("WARNING: No config file was specified, arg [0] used as model type ");
Config.main(new String[] {new String(CLASS_PATH+"/def.conf")}); //this line causes errors
modelType = args[0];
}else{
//reading the configuration file
Config.main(new String[] {args[0]} );
modelType = args[1];
}
//checking for errors
if(numFeatures != featureVariables.length){
System.err.println("ERROR: Feature list and number don't match");
}
//initializing new R-engine which imports the R library into java
re= REngine.engineForClass("org.rosuda.REngine.JRI.JRIEngine");
//dont know that this does
re.assign("num", new int[] { 1 });
//checks if the engine is working
if(MainWindow.DEBUG)System.out.println(re.parseAndEval ("num;").asInteger());
if(re.parseAndEval ("num;").asInteger()!=1){
System.out.println ("R Engine could not be started \n ABORTING...");
System.exit(1);
}
// String ss = getSourceCode(CLASS_PATH+"/Rsource/ANN.R");
// String[] ssp = splitCode(ss);
if(MainWindow.DEBUG)System.out.println(CLASS_PATH);
if(modelType.equals("ANN")){
createANN();
}
else if(modelType.equals("SVM")){
createSVM();
}else if (modelType.equals("BAY")){
createBAY();
}
re.close();
}catch(Exception e){
System.err.println("ERROR: File was not found (main)");
e.printStackTrace();
}
}
/**
* This method returns R code read from a file as a String.
*/
public static String getSourceCode (String filename){
try{//this only works for Java 7 onwards
String code = new Scanner(new File(filename)).useDelimiter("\\Z").next();
return code.trim();
}
catch(Exception e ){
e.printStackTrace();
}
return null;
}
/**
*This method checks if the package specified is installed in R.
*/
static void checkPackages(String packageName){
if(re == null){ //error check
System.err.println("ERROR: re is null");
}
try{
//the code will check if R has the package installed
String code = getSourceCode(CLASS_PATH+"/Rsource/pkgTest.R");
//put the code in one line
//code = code.replaceAll("[\r\n]+", " ");
//load the R function that checks packages
re.parseAndEval(code);
//check if the package is installed by calling the function
REXP retVal = re.parseAndEval("pkgTest(\""+ packageName +"\");" );
//give error if package couldn't be installed
if (retVal!= null ) {
if(retVal.asInteger() ==0 ){System.err.println("Package not found ERROR");
re.parseAndEval("install.packages(\""+packageName + "\");");
return;}
else{ //print package was succesfully installed
if(MainWindow.DEBUG)System.out.println("Package "+ packageName +" was found");
}
}else{
System.out.println("Bulls");}
}catch(Exception e ){
e.printStackTrace();
}
}
/**
* This method breaks the R code into portions where each portion is seperated by
* "#BREAK#"
*/
static String[] splitCode(String code){
//arraylist holding all the portions
ArrayList<String> array = new ArrayList<String>();
//scanner to scan through the code
Scanner s = new Scanner(code);
String aPortion = "";
String line = "";
while(s.hasNextLine()){
line = s.nextLine(); //advance to next line
if (line.contains("#BREAK#")) {//find the Break token
array.add(aPortion);
//System.out.println(aPortion);
aPortion = "";
}else if(line.startsWith("#")||line.contains("#")){
continue; //skip the line
}
else{
aPortion += line; //add chunks to the line
}
}
array.add(aPortion);
return array.toArray(new String[array.size()]); //return as an array
}
/**
* This method creates an ANN in R and returns the ANN
*/
static REXP createANN(){
// Checks if the neural net package is installed in R
checkPackages("neuralnet");
checkPackages("JavaGD");
//imports the R source Code and breaks it into parts for execution //change back to ANN.R
String [] sourceCode = splitCode(getSourceCode( CLASS_PATH+"/Rsource/ANN.R" ));
//for debug purposes
if(MainWindow.DEBUG){System.out.println(Arrays.toString(sourceCode));}
try{
//run all parts of source code
re.parseAndEval("library(neuralnet);");
//declaring parameters and variables in R using class variables
//which were originally read from configuration file
re.parseAndEval((String)("percentTrain="+String.valueOf(1.0)+";" ));
re.parseAndEval("PATH = \""+ CLASS_PATH.replaceAll("\\\\","/")+"/Rsource\";");
if(MainWindow.DEBUG){System.out.println("PATH in R : " + re.parseAndEval("PATH;").asString());}
re.parseAndEval("saveWeights = " + Boolean.toString(saveWeights).toUpperCase() + ";" );
int retVal = -1;
JFileChooser fileChooser = null;
for (int i = 0; i <sourceCode.length ;i++ ) {
if(MainWindow.DEBUG){System.out.println("At code portion: "+ i);} //for debug
if(i==1){
fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(CLASS_PATH));
retVal = fileChooser.showOpenDialog(null);
if(retVal == JFileChooser.APPROVE_OPTION ){
String file_path = fileChooser.getSelectedFile().getAbsolutePath();
sourceCode[i]=sourceCode[i].replaceAll("_PATHTOFILE", "\""+file_path.replaceAll("\\\\","/")+"\"");
}
}
if(i==3){
sourceCode[i] = sourceCode[i].replaceAll("PREDVARIABLE_", predVariable );
sourceCode[i] = sourceCode[i].replaceAll("FEATURES_", createFeatureString(0) );
if(retVal == JFileChooser.APPROVE_OPTION ){
String file_path = fileChooser.getSelectedFile().getAbsolutePath();
sourceCode[i]=sourceCode[i].replaceAll("_PATHTOFILE", "\""+file_path.replaceAll("\\\\","/")+"\"");
}
if(MainWindow.DEBUG) System.out.println(sourceCode[i] );
}else if( i==5 || i==6){
sourceCode[i] = sourceCode[i].replaceAll("FEATURES_", createFeatureString(1) );
if(MainWindow.DEBUG) System.out.println(sourceCode[i] );
}
if(MainWindow.DEBUG) System.out.println(sourceCode[i] );
re.parseAndEval (sourceCode[i]); //run the code portion
}
//debug stuff
if(MainWindow.DEBUG){
System.out.println("Path was set to : " + re.parseAndEval("PATH;").asString());
//System.out.println(Arrays.toString(re.parseAndEval("NNet$weights[[1]][[1]][,1];").asDoubles()));
}
//re.parseAndEval("testSet_Features = subset(testSet,select=c( "+createFeatureString(1) +"))");
//re.parseAndEval("NNresults = compute(NNet , testSet_Features ); final_results = as.vector(round(NNresults$net.result , 2 ))");
REXP ANN = re.parseAndEval("NNet;"); //store the ANN in an REXP object
wasModelCreated = true;
return ANN; //return ann object
}catch(Exception e){
e.printStackTrace();
}
return null;
}
static String testANN(){
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(CLASS_PATH));
int retVal = fileChooser.showOpenDialog(null);
if(retVal == JFileChooser.APPROVE_OPTION ){
String file_path = fileChooser.getSelectedFile().getAbsolutePath();
try{
if( re.parseAndEval("exists(\"NNet\")" ).asInteger() != 1 ){
System.err.println("ERRORRR");
}
if(MainWindow.DEBUG){
System.out.println("Does ANN exist " + re.parseAndEval("exists(\"NNet\")" ).asString() );
System.out.println("TESTING COMMANDS \n\n "+"test_data = read.csv (\""+file_path.replaceAll("\\\\","/")+"\");"
+"\n"+"testSet_Features = subset(test_data,select=c("+ createFeatureString(1) +")); write.csv(testSet_Features, file='blah.csv')"+
"\n"+"NNresults = compute(NNet , testSet_Features )" +
"\n"+"final_results = as.vector(round(NNresults$net.result , 5 ))");
}
//The code for the actual testing
//read the test file
re.parseAndEval("test_data = read.csv (\""+file_path.replaceAll("\\\\","/")+"\");");
//extract only the specified features from the test data set
re.parseAndEval("testSet_Features = subset(test_data,select=c("+ createFeatureString(1) +"));" );
// if(MainWindow.DEBUG) {System.out.print( Arrays.toString(re.parseAndEval("testSet_Features[\"Class\"]" ).asDoubles() ) ); }
//compute the stuff
re.parseAndEval("NNresults = compute(NNet , testSet_Features );");
//store the final results
re.parseAndEval("final_results = as.vector(round(NNresults$net.result , 5 ));");
if(saveOutput){//if we want to save the output as a csv
JFileChooser fileChooser2 = new JFileChooser();
fileChooser2.setCurrentDirectory(new File(CLASS_PATH));
int retVal2 = fileChooser2.showSaveDialog(null);
if(retVal2 == JFileChooser.APPROVE_OPTION ){
String file_path2 = fileChooser2.getSelectedFile().getAbsolutePath();
re.parseAndEval("testSet_Features[,'Predicted "+ predVariable+"'] = final_results;");
re.parseAndEval("write.csv(testSet_Features, file='" + file_path2.replaceAll("\\\\","/") +"')" );
if(MainWindow.DEBUG){System.out.println("write.csv(testSet_Features , file='" + file_path2.replaceAll("\\\\","/") +"')");}
}
else{
System.out.println("Didnt choose a place to save results");
}
}
double[] results = re.parseAndEval("final_results").asDoubles() ;
System.out.println(Arrays.toString(results));
String ans = "Predicted "+ predVariable+"\n----------------\n\n\t";
for (int i=0 ; i<results.length ; i++){
ans+= (results[i]) + "\n\t";
}
return ans;
}catch(Exception e){
}
}
else{
System.out.print("You didnt choose a test file");
return "Error testing ANN!";
}
return null;
}
static REXP createSVM(){
//checking if packages are installed
checkPackages("e1071");
checkPackages("rpart");
//imports the R source Code and breaks it into parts for execution
String [] sourceCode = splitCode(getSourceCode( CLASS_PATH+"/Rsource/SVM_S.R" ));
//for debug purposes
if(MainWindow.DEBUG){System.out.println(Arrays.toString(sourceCode));}
try{
//run all parts of source code
//declaring parameters and variables in R using class variables
//which were originally read from configuration file
re.parseAndEval((String)("percentTrain="+String.valueOf(percentTrain)+";" ));
re.parseAndEval("PATH = \""+ CLASS_PATH.replaceAll("\\\\","/")+"/Rsource\";");
re.parseAndEval("saveWeights = " + Boolean.toString(saveWeights).toUpperCase() + ";" );
re.parseAndEval("readWeights = " + Boolean.toString(readWeights).toUpperCase() + ";" );
int retVal = -1;
JFileChooser fileChooser = null;
//loops goes through and calls the source code
for (int i = 0; i <sourceCode.length ;i++ ) {
if(i==0){
fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(CLASS_PATH));
retVal = fileChooser.showOpenDialog(null);
if(retVal == JFileChooser.APPROVE_OPTION ){
String file_path = fileChooser.getSelectedFile().getAbsolutePath();
sourceCode[i]=sourceCode[i].replaceAll("_PATHTOFILE", "\""+file_path.replaceAll("\\\\","/")+"\"");
}
else{
System.out.println("PLEASE CHOOSE A DATA FILE");
}
}
if(MainWindow.DEBUG){System.out.println("At code portion: "+ i+ "\n"+sourceCode[i]+"\n" );} //for debug
//if(MainWindow.DEBUG && i==2){System.out.println("\n SVM SUMMARY :\n\n " + (Arrays.toString(re.parseAndEval("trainIndex;").asIntegers())));}
if(i==2){
sourceCode[i] = sourceCode[i].replaceAll("PREDVARIABLE_", predVariable );
sourceCode[i] = sourceCode[i].replaceAll("FEATURES_", createFeatureString(0) );
if(retVal == JFileChooser.APPROVE_OPTION ){
String file_path = fileChooser.getSelectedFile().getAbsolutePath();
sourceCode[i]=sourceCode[i].replaceAll("_PATHTOFILE", "\""+file_path.replaceAll("\\\\","/")+"\"");
}
if(MainWindow.DEBUG) System.out.println(sourceCode[i] );
}else if(i==3){
sourceCode[i] = sourceCode[i].replaceAll("FEATURES_", createFeatureString(1) );
sourceCode[i] = sourceCode[i].replaceAll("PREDVARIABLE_", predVariable );
if(MainWindow.DEBUG) System.out.println(sourceCode[i] );
}
re.parseAndEval (sourceCode[i]); //run the code portion
}
//Two arrays for the actual and predicted output
if(MainWindow.DEBUG){System.out.println("Path was set to : " + re.parseAndEval("PATH;").asString());}
// if(!re.parseAndEval("finish").asString().equals("Omar")){
// System.err.println("Error in R code; Didn't execute fully");
// }
REXP SVM = re.parseAndEval("SVM.Model;"); //store the ANN in an REXP object
wasModelCreated = true;
return SVM;
}catch(Exception e){
e.printStackTrace();
}
return null;
}
static String testSVM(){
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(CLASS_PATH));
int retVal = fileChooser.showOpenDialog(null);
if(retVal == JFileChooser.APPROVE_OPTION ){
String file_path = fileChooser.getSelectedFile().getAbsolutePath();
try{
if( re.parseAndEval("exists(\"SVM.Model\")" ).asInteger() != 1 ){
//System.err.println("ERROR SVM was not created");
return "ERROR SVM was not created";
}
if(MainWindow.DEBUG){
System.out.println("Does ANN exist " + re.parseAndEval("exists(\"SVM.Model\")" ).asString() );
System.out.println("TESTING COMMANDS \n\n "+"test_data = read.csv (\""+file_path.replaceAll("\\\\","/")+"\");"
+"\n"+"testSet_Features = subset(test_data,select=c("+ createFeatureString(1) +")); write.csv(testSet_Features, file='testfile.csv')"+
"\n"+" SVM.Pred = predict(SVM.Model ,testSet_Features );" +
"\n"+"final_results = round(as.data.frame(SVM.Pred)$SVM.Pred , 5) ;");
}
//read the file specified by the
re.parseAndEval("test_data = read.csv (\""+file_path.replaceAll("\\\\","/")+"\");");
//extract only the specified features from the test data set
re.parseAndEval("testSet_Features = subset(test_data,select=c("+ createFeatureString(1) +")); " );
// if(MainWindow.DEBUG) {System.out.print( Arrays.toString(re.parseAndEval("testSet_Features[\"Class\"]" ).asDoubles() ) ); }
//compute the stuff
re.parseAndEval("SVM.Pred = predict(SVM.Model ,testSet_Features );");
re.parseAndEval("final_results = round(as.data.frame(SVM.Pred)$SVM.Pred , 5) ;");
if(saveOutput){//if we want to save the output as a csv
JFileChooser fileChooser2 = new JFileChooser();
fileChooser2.setCurrentDirectory(new File(CLASS_PATH));
int retVal2 = fileChooser2.showSaveDialog(null);
if(retVal2 == JFileChooser.APPROVE_OPTION ){
String file_path2 = fileChooser2.getSelectedFile().getAbsolutePath();
re.parseAndEval("testSet_Features[,'Predicted "+ predVariable+"'] = final_results;");
re.parseAndEval("write.csv(testSet_Features, file='" + file_path2.replaceAll("\\\\","/") +"')" );
if(MainWindow.DEBUG){System.out.println("write.csv(testSet_Features , file='" + file_path2.replaceAll("\\\\","/") +"')");}
}
else{
System.out.println("Didnt choose a place to save results");
}
}
double[] results = re.parseAndEval("final_results").asDoubles() ;
if(MainWindow.DEBUG){System.out.println(Arrays.toString(results));}
String ans = "Predicted "+ predVariable+"\n----------------\n\n\t";
for (int i=0 ; i<results.length ; i++){
ans+= (results[i]) + "\n\t";
}
return ans;
}catch(Exception e){
}
}
else{
System.out.print("You didnt choose a test file");
return "Error testing SVM!";
}
return null;
}
static REXP createBAY(){
//checking if packages are installed
checkPackages("e1071");
//imports the R source Code and breaks it into parts for execution
String [] sourceCode = splitCode(getSourceCode( CLASS_PATH+"/Rsource/BAY.R" ));
//for debug purposes
if(MainWindow.DEBUG){System.out.println(Arrays.toString(sourceCode));}
try{
//run all parts of source code
//declaring parameters and variables in R using class variables
//which were originally read from configuration file
re.parseAndEval((String)("percentTrain="+String.valueOf(percentTrain)+";" ));
re.parseAndEval("PATH = \""+ CLASS_PATH.replaceAll("\\\\","/")+"/Rsource\";");
re.parseAndEval("saveWeights = " + Boolean.toString(saveWeights).toUpperCase() + ";" );
re.parseAndEval("readWeights = " + Boolean.toString(readWeights).toUpperCase() + ";" );
int retVal = -1;
JFileChooser fileChooser = null;
for (int i = 0; i <sourceCode.length ;i++ ) {
if(i==0){
fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(CLASS_PATH));
retVal = fileChooser.showOpenDialog(null);
if(retVal == JFileChooser.APPROVE_OPTION ){
String file_path = fileChooser.getSelectedFile().getAbsolutePath();
sourceCode[i]=sourceCode[i].replaceAll("_PATHTOFILE", "\""+file_path.replaceAll("\\\\","/")+"\"");
}
else{
System.out.println("PLEASE CHOOSE A DATA FILE");
}
}
if(MainWindow.DEBUG){System.out.println("At code portion: "+ i+ "\n"+sourceCode[i]+"\n" );} //for debug
//if(MainWindow.DEBUG && i==2){System.out.println("\n SVM SUMMARY :\n\n " + (Arrays.toString(re.parseAndEval("trainIndex;").asIntegers())));}
if(i==2){
sourceCode[i] = sourceCode[i].replaceAll("PREDVARIABLE_", predVariable );
sourceCode[i] = sourceCode[i].replaceAll("FEATURES_", createFeatureString(0) );
String file_path = fileChooser.getSelectedFile().getAbsolutePath();
sourceCode[i]=sourceCode[i].replaceAll("_PATHTOFILE", "\""+file_path.replaceAll("\\\\","/")+"\"");
if(MainWindow.DEBUG) System.out.println(sourceCode[i] );
}else if(i==3){
sourceCode[i] = sourceCode[i].replaceAll("FEATURES_", createFeatureString(1) );
sourceCode[i] = sourceCode[i].replaceAll("PREDVARIABLE_", predVariable );
if(MainWindow.DEBUG) System.out.println(sourceCode[i] );
}
re.parseAndEval (sourceCode[i]); //run the code portion
}
//Two arrays for the actual and predicted output
if(MainWindow.DEBUG){System.out.println("Path was set to : " + re.parseAndEval("PATH;").asString());}
REXP BAY_NETWORK = re.parseAndEval("classifier;"); //store the ANN in an REXP object
wasModelCreated = true;
return BAY_NETWORK;
}catch(Exception e){
e.printStackTrace();
}
return null;
}
static String testBAY(){
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(CLASS_PATH));
int retVal = fileChooser.showOpenDialog(null);
if(retVal == JFileChooser.APPROVE_OPTION ){
String file_path = fileChooser.getSelectedFile().getAbsolutePath();
try{
if( re.parseAndEval("exists(\"classifier\")" ).asInteger() != 1 ){
//System.err.println("ERROR SVM was not created");
return "ERROR Bayesian network was not created";
}
if(MainWindow.DEBUG){
System.out.println("Does BAY exist " + re.parseAndEval("exists(\"classifier\")" ).asString() );
System.out.println("TESTING COMMANDS \n\n "+"test_data = read.csv (\""+file_path.replaceAll("\\\\","/")+"\");"
+"\n"+"testSet_Features = subset(test_data,select=c("+ createFeatureString(1) +")); "+
"\n"+"" +
"\n"+"result = as.character(predict(classifier ,testSet_Features ));");
}
//read the file specified by the
re.parseAndEval("test_data = read.csv (\""+file_path.replaceAll("\\\\","/")+"\");");
//extract only the specified features from the test data set
re.parseAndEval("testSet_Features = subset(test_data,select=c("+ createFeatureString(1) +")); " );
// if(MainWindow.DEBUG) {System.out.print( Arrays.toString(re.parseAndEval("testSet_Features[\"Class\"]" ).asDoubles() ) ); }
//compute the stuff
re.parseAndEval("result = as.character(predict(classifier ,testSet_Features ));");
//re.parseAndEval("final_results = round(as.data.frame(SVM.Pred)$SVM.Pred , 5) ;");
if(saveOutput){//if we want to save the output as a csv
JFileChooser fileChooser2 = new JFileChooser();
fileChooser2.setCurrentDirectory(new File(CLASS_PATH));
int retVal2 = fileChooser2.showSaveDialog(null);
if(retVal2 == JFileChooser.APPROVE_OPTION ){
String file_path2 = fileChooser2.getSelectedFile().getAbsolutePath();
re.parseAndEval("testSet_Features[,'Predicted "+ predVariable+"'] = result;");
re.parseAndEval("write.csv(testSet_Features, file='" + file_path2.replaceAll("\\\\","/") +"')" );
if(MainWindow.DEBUG){System.out.println("write.csv(testSet_Features , file='" + file_path2.replaceAll("\\\\","/") +"')");}
}
else{
System.out.println("Didnt choose a place to save results");
}
}
String[] results = re.parseAndEval("result").asStrings() ;
if(MainWindow.DEBUG){System.out.println(Arrays.toString(results));}
String ans = "Predicted "+ predVariable+"\n----------------\n\n\t";
for (int i=0 ; i<results.length ; i++){
ans+= (results[i]) + "\n\t";
}
return ans;
}catch(Exception e){
}
}
else{
System.out.print("You didnt choose a test file");
return "Error testing SVM!";
}
return null;
}
static String saveModel(String modelType){
if(!wasModelCreated){
return "No model was created/trained !";
}
try{
//reading the file
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(CLASS_PATH));
int retVal = fileChooser.showSaveDialog(null);
if(retVal == JFileChooser.APPROVE_OPTION ){
String file_path = fileChooser.getSelectedFile().getAbsolutePath();
if(modelType.equals("ANN")){
if (MainWindow.DEBUG){ System.out.println("save( \"NNet\" ,file=\""+file_path.replaceAll("\\\\","/") + "\");");}
re.parseAndEval("save( \"NNet\" ,file=\""+file_path.replaceAll("\\\\","/") + "\");");
}else if(modelType.equals("SVM")){
if (MainWindow.DEBUG){ System.out.println("save( \"SVM.Model\" ,file=\""+file_path.replaceAll("\\\\","/") + "\");");}
re.parseAndEval("save( \"SVM.Model\" ,file=\""+file_path.replaceAll("\\\\","/") + "\");");
}else if(modelType.equals("BAY")){
if (MainWindow.DEBUG){ System.out.println("save( \"classifier\" ,file=\""+file_path.replaceAll("\\\\","/") + "\");");}
re.parseAndEval("save( \"classifier\" ,file=\""+file_path.replaceAll("\\\\","/") + "\");");
}
}
return "Save Successful";
}catch(Exception e){
}
return "ERROR saving model!!!!";
}
/**
* This method runs tests on an alrady created Machine learning structure.
* You will specify a data set to be read from a file. features will have been supplied
* from a configuration file
*/
static void runTest(String modelType ){
if(!wasModelCreated){
JOptionPane.showMessageDialog(null,"Model needs to be created to run tests","Error",JOptionPane.WARNING_MESSAGE);
}else{
try{
//reading the file
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(CLASS_PATH));
int retVal = fileChooser.showOpenDialog(null);
if(retVal == JFileChooser.APPROVE_OPTION ){
String file_path = fileChooser.getSelectedFile().getAbsolutePath();
re.parseAndEval("testData = read.csv(\""+file_path + "\");");
}
else{
System.out.println("PLEASE CHOOSE A DATA FILE for testing ");
}
//creating subset of data using the features only
re.parseAndEval("testData_features = subset(testData , select=c(" + createFeatureString(1) + "));");
//picking a model
if(modelType.equals("ANN")){
re.parseAndEval("NN_out= compute(NNet , testData_features)");
re.parseAndEval("final_out = as.vector(NN_out$net.result);");
}else if(modelType.equals("SVM")){
}else if (modelType.equals("BAY")){
}
//storing the output as an array
String [] results = re.parseAndEval("final_out;").asStrings();
//the column which is also to be printed
String [] idColumn = null;
if(!printColumn.equals(""))idColumn = re.parseAndEval("testSet$"+printColumn+ ";").asStrings();
//output string generated
String print_out = printOutput(idColumn , null , results);
}
catch(Exception e){
}
}
}
/**
* This method prints the output
*/
static String printOutput(String [] idCol , double[] actual, double[] predicted){
// Header for output
String out = "\n\t--------OUTPUT------\n\n" ;
out+= printColumn + " \t ACTUAL PREDICTED \n\n" ;
int count = 0;//counting how well the output matches
for (int i = 0;i<predicted.length ; i++) {
if(idCol!= null){
out+= idCol[i] + " " ;
}
if(actual!=null){
out+=" \t\t " + actual[i];
if(Math.abs(actual[i]-predicted[i])<MATCH_THRESHOLD){
count++;
}
}
out+= " "+predicted[i]+ "\n" ; //printing out table with predicted and actual
}
out+="\n--------SUMMARY-------\n Match Percent : " + (double)count*100.0/((double)predicted.length) ;
System.out.println(out);
outputString = out;
return out;
}
/**
* This method prints the output takes in two string arrays to compare their equality
*/
static String printOutput(String[] idCol , String[] actual, String[] predicted){
// Header for output
String out = "\n\t--------OUTPUT------\n\n" ;
out+=printColumn+ " \t ACTUAL \t PREDICTED \n\n" ;
int count = 0;//counting how well the output matches
for (int i = 0;i<predicted.length ; i++) {
if(idCol!= null){
out+= idCol[i] + " " ;
}
if(actual!=null){
out+=" \t\t " + actual[i];
if(actual[i].equalsIgnoreCase(predicted[i])){
count++;
}
}
out+= " "+predicted[i]+ "\n" ; //printing out table with predicted and actual
}
out+="\n--------SUMMARY-------\n Match Percent : " + (double)count*100.0/((double)predicted.length) ;
System.out.println(out);
outputString = out;
return out;
}
/**
* Creates a String that will be passed into R to specify the features.
* There will be two types of these Strings. One seperated by plus signs and the other by commas
*
* Type 0: seperated by +
* Type 1 = seperated by ,
*/
static String createFeatureString( int type){
String features = "";
if(type ==0){
for( int i = 0;i<(featureVariables).length -1 ;i++ ){
features += featureVariables[i] + "+" ;
}
features += featureVariables[featureVariables.length -1];
}else if(type ==1){
for( int i = 0;i<(featureVariables).length -1 ;i++ ){
features += ""+ featureVariables[i] + "," ;
}
features += ""+ featureVariables[featureVariables.length -1] +"";
}else{
System.err.println("ERROR:Wrong type of feature string parsing");
}
return features.trim();
}
}