-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExperiment.java
181 lines (168 loc) · 7.06 KB
/
Experiment.java
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
import java.io.*;
import java.lang.*;
import java.util.*;
public class Experiment{
private static final String ENCLOSING_FOLDER = "/Users/Jeff/School/2014SenProj/calculated/";
private static final String FILE_EXT= ".data";
private static final int DEFAULT_K = 17;
private static final double DEFAULT_ALPHA = 1;
private static final String filter[] = {
"SLO Creek Water","Pennington Creek Water",
"Hu Cw and Dg","Hu and Cw","Hu and Dg","Cw and Dg",
"Human"};
private static HashSet hashFilter;
private static Pyroprint unknownPyro;
public static void main(String[] args)
throws FileNotFoundException, IOException, ClassNotFoundException{
int unknownLine = 0;
int endLine = 0;
int k = 0;
int successes = 0;
int failures = 0;
double alpha = 1;
String dataFileName;
ArrayList<Distance> calcDists;
hashFilter = new HashSet();
for(int ndx = 0; ndx < filter.length; ndx++){
hashFilter.add(filter[ndx]);
}
if(args.length == 1){
unknownLine = Integer.parseInt(args[0]);
endLine = unknownLine;
k = DEFAULT_K;
alpha = DEFAULT_ALPHA;
}
else if(args.length == 2){
unknownLine = Integer.parseInt(args[0]);
endLine = Integer.parseInt(args[1]);
k = DEFAULT_K;
alpha = DEFAULT_ALPHA;
}
else if(args.length == 3){
unknownLine = Integer.parseInt(args[0]);
endLine = Integer.parseInt(args[1]);
k = Integer.parseInt(args[2]);
alpha = DEFAULT_ALPHA;
}
else if(args.length == 4){
unknownLine = Integer.parseInt(args[0]);
endLine = Integer.parseInt(args[1]);
k = Integer.parseInt(args[2]);
alpha = Double.parseDouble(args[3]);
}
else{
System.out.println("usage: java Experiment <begin> <end> <k value> <alpha>");
}
System.out.println("begin: " + unknownLine +" end: "+ endLine +" k:" + k + " alpha:" + alpha);
for(;unknownLine <= endLine; unknownLine++){
dataFileName = ENCLOSING_FOLDER + unknownLine + FILE_EXT;
calcDists = readFromFile(dataFileName);
System.out.println("Top " + k + " (unfiltered): ");
printTopK(k,calcDists);
System.out.println(String.format("Beginning experiment for %s...",calcDists.get(0).getCommonName()));
if(countTopK(k,alpha,calcDists)){
successes++;
System.out.println("Line " + unknownLine);
System.out.println("--------------");
System.out.println("Woo! SUCCESS!!");
}
else{
failures++;
System.out.println("Line " + unknownLine);
System.out.println("--------------");
System.out.println("FAILURE");
}
System.out.println("--------------");
}
System.out.println(String.format("%.2f percent right, as usual.",100*successes/(1.0*successes+failures)));
}
public static void count(String thing, HashMap<String,Integer> table){//==>
Integer count = table.get(thing);
table.put(thing,(count == null) ? 1 : count + 1);
}//<==
public static void addSpecies(String species, int position, HashMap<String,Species> table) {//==>
if (table.containsKey(species)){
table.get(species).addPosition(position);
} else {
table.put(species, new Species(species,position));
}
}//<==
public static void printTopK(int k, ArrayList<Distance> pearsonCorrelation){//==>
Distance current;
for(int topKLoc = 0; topKLoc <= k; topKLoc++){
current = pearsonCorrelation.get(topKLoc);
System.out.println(String.format("%2d. %.6f: %s",topKLoc,current.getDistance(),current.getCommonName()));
}
}//<==
public static boolean countTopK(int k, double alpha, ArrayList<Distance> pearsonCorrelation){//==>
Distance current;
Species topCurrent;
HashMap<String,Species> topKTable = new HashMap<String,Species>();
System.out.println("Top " + k + " (filtered):");
current = pearsonCorrelation.get(1);
for(int topKLoc = 1; topKLoc <= k && alpha > (1-current.getDistance()) ; topKLoc++){
while(hashFilter.contains(pearsonCorrelation.get(topKLoc).getCommonName())){
// System.out.println("Filtered: " + pearsonCorrelation.get(topKLoc).getCommonName());
pearsonCorrelation.remove(topKLoc);
}
if(alpha > (1-current.getDistance())){
current = pearsonCorrelation.get(topKLoc);
System.out.println(String.format("%2d. %.6f: %s",topKLoc,current.getDistance(),current.getCommonName()));
addSpecies(current.getCommonName(),topKLoc,topKTable);
}
}
ArrayList<Species> topKList = new ArrayList<Species>();
topKList.addAll(topKTable.values());
Collections.sort(topKList);
System.out.println("Rnk # avPos: Common Name");
System.out.println("--------------------------");
for(int listLoc = 0; listLoc < topKList.size(); listLoc++){
topCurrent = topKList.get(listLoc);
System.out.println(String.format("%2d. %2d %2.3f: %s",listLoc+1,topCurrent.getCount(),
topCurrent.getAveragePosition(),topCurrent.getCommonName()));
}
if(topKList.size() > 0){
System.out.println(
pearsonCorrelation.get(0).getCommonName()
+ " was classified as "
+ topKList.get(0).getCommonName());
return pearsonCorrelation.get(0).getCommonName().equals(topKList.get(0).getCommonName());
}
else{
System.out.println("Nothing in top" + k + " matches.");
return false;
}
}//<==
// public static void printSpeciesCount(){//==>
// System.out.println("Species Table:");
// System.out.println("-------------");
// for (Map.Entry<String, Integer> entry : speciesTable.entrySet()) {
// System.out.println(String.format("%4d: %s",entry.getValue(),entry.getKey()));
// }
// System.out.println("-------------");
// }//<==
public static void writeToFile(String filename, ArrayList<Distance> list) //==>
throws FileNotFoundException, IOException{
FileOutputStream fileOut = new FileOutputStream(filename);
ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
System.out.println("Trimming...");
list.trimToSize();
System.out.println("Writing to " + filename + "...");
objectOut.writeObject(list);
fileOut.close();
}//<==
public static ArrayList<Distance> readFromFile(String filename)//==>
throws FileNotFoundException, IOException, ClassNotFoundException{
FileInputStream fileIn = new FileInputStream(filename);
ObjectInputStream objectIn = new ObjectInputStream(fileIn);
System.out.println("Reading from " + filename + "...");
Object obj = objectIn.readObject();
// System.out.println("Casting...");
ArrayList<Distance> returnArray = null;
if(obj instanceof ArrayList){
returnArray = (ArrayList<Distance>) obj;
}
fileIn.close();
return returnArray;
}//<==
}