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
| package AJMParticleFilter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Vector;
public class LDB {
private static Path[] data; //Array of path objects
private static int nFeatures;
/*
* Start Main
*/
public void LDB(String s, double mindirchange, double minsd, double radiuss, double pfspr, double pfmv, int waggle, int start, int printvid)
throws IOException {
/*
* Get footage & footage parameters
*/
String footage = s;
BufferedReader br = new BufferedReader(new FileReader(String.format("footage_%s.csv", footage)));
StringTokenizer st = new StringTokenizer(br.readLine(), ",");
int fImage = new Integer(st.nextToken()).intValue();
int nBlocks = new Integer(st.nextToken()).intValue();
int nBlock = new Integer(st.nextToken()).intValue();
int ppm_width = new Integer(st.nextToken()).intValue();
int ppm_height = new Integer(st.nextToken()).intValue();
/*
* Initialisation and PF Arguments
*/
double min_dir_change = new Double(mindirchange).doubleValue();
double min_sd = new Double(minsd).doubleValue();
double radius = new Double(radiuss).doubleValue();
double pf_rad = radius;
double pf_spr = new Double(pfspr).doubleValue();
double pf_mv = new Double(pfmv).doubleValue();
int nWaggle = new Integer(waggle).intValue(); //Number of paths which constitute a waggle
int nStart = new Integer(start).intValue(); //Number of paths to start a Particle Filter
/*
* Output Option
*/
int print_videos = new Integer(printvid).intValue();
System.out.println("Print videos: "+print_videos);
/*
* Constants
*/
final int xres = 1;
final int yres = 1;
final int np = 50;
final int nContinue = 1;
final int metric = 1;
/*
* Object controls Particle Filters
*/
SMC sm = new SMC();
for (int k = fImage; k < fImage + nBlock * nBlocks - (nBlocks + 1); k += nBlock - 1) {
/*
* STEP 1: GENERATE FEATURE SPACE AT TIME K
*/
System.out.println(String.format("block %d...", k));
System.out.println(String.format("..reading block %d", k));
read_data(String.format("%svis_block_%08d.csv", footage, k));
System.out.println(String.format("..classifying block %d, (%d)", k, nFeatures));
Vector<Path> v_feat = new Vector<Path>(0, 1);
/*
* Generate Feature Space for this block
*/
for (int i = 0; i < nFeatures; i++) {
data[i].statx();
data[i].staty();
data[i].setstart(k);
if (data[i].dir_change(i) > min_dir_change && Math.max(data[i].stdx, data[i].stdy) > min_sd) {
v_feat.add(data[i]);
}
}
/*
* STEP 2: SUBSUME FEATURES INTO EXISTING PARTICLE FILTERS AT TIME K
*/
System.out.println(String.format("..compiling block %d, (%d)", k, v_feat.size()));
v_feat = sm.compile_features(v_feat, nContinue);
/*
* STEP 3: INITIALSE PARTICLE FILTERS WITH FEATURES AT TIME K
*/
Store s_counter = new Store();
System.out.println(String.format("..grouping block %d, (%d)", k, v_feat.size()));
/*
* Perform grouping for that block
*/
for (int x = 0; x < ppm_width; x += xres) {
for (int y = 0; y < ppm_height; y += yres) {
Counter c = new Counter((double) x, (double) y, radius);
for (int i = 0; i < v_feat.size(); i++) {
Path p_temp = v_feat.get(i);
p_temp.statx();
p_temp.staty();
if (Distance.distance(metric, p_temp.meanx, p_temp.meany, c.getx(), c.gety()) < radius) {
c.inc(p_temp);
}
}
if (c.getn() > 0) //Threshold to speed things up
{
s_counter.add_counter(c, radius, metric);
}
}
}
System.out.println(String.format("..initialising block %d, (%d)", k, v_feat.size()));
sm.add_store(s_counter, np, pf_rad, pf_spr, pf_mv, nStart);
sm.sort_pfs();
/*
* STEP 4: MEASURE ALL PARTICLE FILTERS
*/
System.out.println(String.format("..measuring block %d, (%d)", k, v_feat.size()));
sm.measure();
/*
* STEP 5: ADVANCE ALL PARTICLE FILTERS
*/
System.out.println(String.format("..advancing block %d, (%d)", k, v_feat.size()));
sm.advance();
Results print = new Results(sm, nWaggle);
// print.print_image(String.format("000000"+k+".ppm", footage), ppm_width, ppm_height);
}
/*
* End block
*/
sm.end(); //Wrap up existing particle filters
sm.final_sort(); //Sort in order of total number of particles
/*
* Process and output results
*/
Results r = new Results(sm, nWaggle); //Create Results object from SMC object
r.print_image(String.format("final_results_%s.ppm", footage), ppm_width, ppm_height);
r.print_file(String.format("final_results_%s.csv", footage));
if (print_videos == 1) {
r.print_videos(fImage, nBlocks, nBlock, footage, ppm_width, ppm_height);
}
}
/*
* End main
*/
/*
* Read csv files containg Complete Paths
*/
private static void read_data(String fname)
throws IOException {
nFeatures = 0; //Reset for next block
/*
* Count number of lines - makes it neater in the long run
*/
FileReader frx = new FileReader("results_tracker/x" + fname);
BufferedReader bfx = new BufferedReader(frx);
while (bfx.readLine() != null) {
nFeatures++;
}
/*
* Reset
*/
frx = new FileReader("results_tracker/x" + fname);
bfx = new BufferedReader(frx);
FileReader fry = new FileReader("results_tracker/y" + fname);
BufferedReader bfy = new BufferedReader(fry);
/*
* Now set up array and read in each line
*/
data = new Path[nFeatures];
String xin;
String yin;
int i = 0;
while ((xin = bfx.readLine()) != null && (yin = bfy.readLine()) != null) {
try {
data[i] = new Path(xin, yin);
} catch (NumberFormatException e) {
System.out.println("(CSV error: skipping broken Path)");
nFeatures--;
Path[] temp = new Path[nFeatures];
System.arraycopy(data, 0, temp, 0, i);
data = temp;
i--; //Count one back
}
i++;
}
}
/*
* End read_data
*/
} |