-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpelled.txt
543 lines (444 loc) · 22.3 KB
/
expelled.txt
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
//int BTracer::trace(
// float x, float y, float z,
// float vx, float vy, float vz,
// float r,
// unsigned char * img,
// int img_width,
// int img_height,
// int img_length,
// float angstd_deg,
// float gcsstdstd_pix,
// int tag_beg,
// int * tag_map
// //,unsigned char * fg
// ){
// nodes_reached = 0; //reset each call
// start_px = x;
// start_py = y;
// start_pz = z;
// start_vx = vx;
// start_vy = vy;
// start_vz = vz;
// int out = 0;
// for (int iter_counter = 0; iter_counter < Niterations; ++iter_counter) {
// iter(x, y, z, vx, vy, vz, r, img, img_width, img_height, img_length, angstd_deg, gcsstdstd_pix, false); // iter_counter==0
// // now that all the iter() steps are done take the Nstates highest ones
// // experiment - take one only - the highest and backtrack it
// // add them to xc, avoid xt now - just a rough scatch
// // extract the best (single) track with Nsteps, go backwards and add each to the xc, yc, zc, rc
// float max_post = 0.0;
// int sample_index = -1;
// for (int j = 0; j < (int)round(pow(Ndirs, Nsteps)); ++j) {
// if (postrtt[Nsteps-1][j]>max_post) {
// max_post = postrtt[Nsteps-1][j];
// sample_index = j;
// }
// }
// // set the source for the next iteration
// x = xtt[Nsteps-1][sample_index];
// y = ytt[Nsteps-1][sample_index];
// z = ztt[Nsteps-1][sample_index];
// vx = vxtt[Nsteps-1][sample_index];
// vy = vytt[Nsteps-1][sample_index];
// vz = vztt[Nsteps-1][sample_index];
// r = rtt[Nsteps-1][sample_index];
// // ADDING: there is no stopping criteria here, add the one that was at last step to xc
// xc[iter_counter*Nsteps+(Nsteps-1)] = xtt[Nsteps-1][sample_index];
// yc[iter_counter*Nsteps+(Nsteps-1)] = ytt[Nsteps-1][sample_index];
// zc[iter_counter*Nsteps+(Nsteps-1)] = ztt[Nsteps-1][sample_index];
// rc[iter_counter*Nsteps+(Nsteps-1)] = rtt[Nsteps-1][sample_index];
// // addd the reminder by backtracking (if there is 2 or more in that one)
// for (int si = Nsteps-1; si >= 1; --si) {
// sample_index = sample_index/Ndirs;
// xc[iter_counter*Nsteps+(si-1)] = xtt[si-1][sample_index];
// yc[iter_counter*Nsteps+(si-1)] = ytt[si-1][sample_index];
// zc[iter_counter*Nsteps+(si-1)] = ztt[si-1][sample_index];
// rc[iter_counter*Nsteps+(si-1)] = rtt[si-1][sample_index];
// }
// // check overlap for stopping criteria
// for (int ni = 0; ni < Nsteps; ++ni) {
// int idx = iter_counter*Nsteps+ni;
//// if (fg[(int)round(zc[idx])*img_width*img_height+(int)round(yc[idx])*img_width+(int)round(xc[idx])]==0) {nodes_reached=idx;return 1;}
//// for (int xnn = floor(xc[idx]-rc[idx]); xnn <= ceil(xc[idx]+rc[idx]); ++xnn) {
//// for (int ynn = floor(yc[idx]-rc[idx]); ynn <= ceil(yc[idx]+rc[idx]); ++ynn) {
//// for (int znn = floor(zc[idx]-rc[idx]/zDist); znn <= ceil(zc[idx]+rc[idx]/zDist); ++znn) {
//// if (gpnt_map[znn*img_width*img_height+ynn*img_width+xnn]>0 && gpnt_map[znn*img_width*img_height+ynn*img_width+xnn]!=start_label) {
//// nodes_reached=idx;
//// return 1;
//// }
//// if (fg[znn*img_width*img_height+ynn*img_width+xnn]==0) {nodes_reached=idx;return 1;}
//// }
//// }
//// }
// }
// nodes_reached = iter_counter*Nsteps + Nsteps;
// }
// return 1;
//}
//rotation_matrix( 1,0,0, vx, vy, vz, rot); // vz should be zero
//rotation_apply(rot, 0,1,0, ux, uy, uz); // get the othogonal vector
// rotation_matrix( 0,0,1, vx, vy, vz, rot);
// rotation_apply(rot, 1,0,0, ux, uy, uz); // get the othogonal vectors
// rotation_apply(rot, 0,1,0, wx, wy, wz);
// if (dbg){
// cout << vx << " " << vy << " " << vz << endl;
// cout << ux << " " << uy << " " << uz << endl;
// cout << wx << " " << wy << " " << wz << endl;
// }
// if (dbg) {
// cout << 1 << " " << v1%7 << " " << x << " " << y+ycoord << " " << z+zcoord << " " << 0.5 << " " << -1 << endl;
// }
// for (int gi = 0; gi < nodelist.size(); ++gi) {
// NeuronSWC nn;
// nn.nodeinseg_id = gi+1;
// nn.type = 6;
// nn.n = gi+1;
// nn.x = gpnt_list[gi][0];
// nn.y = gpnt_list[gi][1];
// nn.z = gpnt_list[gi][2];
// nn.r = gpnt_list[gi][3];
// nn.parent = -1;
// nt.listNeuron.append(nn);
// }
// once more go from each....
// int current_label = gpnt_list.size()+1; // take first available
// for (int gi = 0; gi < gpnt_list.size(); ++gi) {
// // bayesian_tracer.trace(xtest,ytest,ztest, gx/g,gy/g,gz/g, 1.5, data1d, N, M, P, PARA.angSig, PARA.gcsSig);
// int res = bay_trac.trace(
// gpnt_list[gi][0],
// gpnt_list[gi][1],
// gpnt_list[gi][2],
// gpnt_list[gi][4],
// gpnt_list[gi][5],
// gpnt_list[gi][6],
// gpnt_list[gi][3],
// data1d,
// N,
// M,
// P,
// PARA.angSig,
// PARA.gcsSig,
// gpnt_map,
// gi+1);
// if (res==1) {
// printf("\nfound trace %d \n", bay_trac.node_cnt);
// // add the trace to the map
// for (int idx = 0; idx < bay_trac.node_cnt; ++idx) {
// NeuronSWC nn;
// nn.nodeinseg_id = current_label;
// nn.type = 4;
// nn.n = current_label;
// nn.x = bay_trac.xc[idx];
// nn.y = bay_trac.yc[idx];
// nn.z = bay_trac.zc[idx];
// nn.r = bay_trac.rc[idx];
// nn.parent = (idx==0)?-1:current_label-1;
// nt.listNeuron.append(nn);
// for (int xnn = floor(bay_trac.xc[idx]-bay_trac.rc[idx]); xnn <= ceil(bay_trac.xc[idx]+bay_trac.rc[idx]); ++xnn) {
// for (int ynn = floor(bay_trac.yc[idx]-bay_trac.rc[idx]); ynn <= ceil(bay_trac.yc[idx]+bay_trac.rc[idx]); ++ynn) {
// for (int znn = floor(bay_trac.zc[idx]-bay_trac.rc[idx]/zDist); znn <= ceil(bay_trac.zc[idx]+bay_trac.rc[idx]/zDist); ++znn) {
// gpnt_map[znn*N*M+ynn*N+xnn] = current_label;
// }
// }
// }
// current_label++;
// }
// }
// }
// extract the best (single) track with Nsteps, go backwards and add each to the xc, yc, zc, rc
// set the source for the next iteration
// x = xtt[Nsteps-1][sample_index];
// y = ytt[Nsteps-1][sample_index];
// z = ztt[Nsteps-1][sample_index];
// vx = vxtt[Nsteps-1][sample_index];
// vy = vytt[Nsteps-1][sample_index];
// vz = vztt[Nsteps-1][sample_index];
// r = rtt[Nsteps-1][sample_index];
// //
// // ADDING: there is no stopping criteria here, add the one that was at last step to xc
// xc[iter_counter*Nsteps+(Nsteps-1)] = xtt[Nsteps-1][sample_index];
// yc[iter_counter*Nsteps+(Nsteps-1)] = ytt[Nsteps-1][sample_index];
// zc[iter_counter*Nsteps+(Nsteps-1)] = ztt[Nsteps-1][sample_index];
// rc[iter_counter*Nsteps+(Nsteps-1)] = rtt[Nsteps-1][sample_index];
// // addd the reminder by backtracking (if there is 2 or more in that one)
// for (int si = Nsteps-1; si >= 1; --si) {
// sample_index = sample_index/Ndirs;
// xc[iter_counter*Nsteps+(si-1)] = xtt[si-1][sample_index];
// yc[iter_counter*Nsteps+(si-1)] = ytt[si-1][sample_index];
// zc[iter_counter*Nsteps+(si-1)] = ztt[si-1][sample_index];
// rc[iter_counter*Nsteps+(si-1)] = rtt[si-1][sample_index];
// }
// predict(xloc, yloc, zloc,
// vxloc, vyloc, vzloc,
// rloc,
// angula_diff_std_deg, gcsstd_diff_std_pix,
// xtt1[i], ytt1[i], ztt1[i],
// vxtt1[i], vytt1[i], vztt1[i],
// rtt1[i],
// priortt1[i]
// );
// if (i_step==0) {
// predict(xloc, yloc, zloc,
// vxloc, vyloc, vzloc,
// rloc,
// angula_diff_std_deg, gcsstd_diff_std_pix,
// xtt1[i], ytt1[i], ztt1[i],
// vxtt1[i], vytt1[i], vztt1[i],
// rtt1[i],
// priortt1[i]
// );
// }
// else {
// for (int j = 0; j < round(pow(Ndirs,i)); ++j) { // loop the ones predicted in previous i
// predict(xtt[i-1][j], ytt[i-1][j], ztt[i-1][j],
// vxtt[i-1][j], vytt[i-1][j], vztt[i-1][j],
// rtt[i-1][j],
// angula_diff_std_deg, gcsstd_diff_std_pix,
// &xtt[i][j*Ndirs], &ytt[i][j*Ndirs], &ztt[i][j*Ndirs],
// &vxtt[i][j*Ndirs], &vytt[i][j*Ndirs], &vztt[i][j*Ndirs],
// &rtt[i][j*Ndirs],
// &priortt[i][j*Ndirs]
// );
// }
// }
// printf("%d;%d (%d): %f; %f; %f; %f; \n",
// i,j, j/Ndirs,
// ((i==0)?1:postrtt[i-1][j/Ndirs]),
// priortt[i][j],
// lhoodtt[i][j],
// postrtt[i][j]);
// if (postrtt[i][j]>mx) mx = postrtt[i][j];
// if (postrtt[i][j]<mn) mn = postrtt[i][j];
// check the likelihood to see if it makes sense to use it (if all likelihoods are zero then don'r embed them but propagate the prior as posterior)
// if (lhoods_mx>0.00001) { // larger than some minimum, it did not hapen that the likelihoods are all zero
// // there was something, makes sense to use it
// float mn = 1;
// float mx = -1;
// for (int j = 0; j < round(pow(Ndirs,i+1)); ++j) { // loop current step
// postrtt[i][j] = ((i==0)?1.0:postrtt[i-1][j/Ndirs]) * priortt[i][j] * lhoodtt[i][j];
//// printf("%d;%d (%d): %f; %f; %f; %f; \n",
//// i,j, j/Ndirs,
//// ((i==0)?1:postrtt[i-1][j/Ndirs]),
//// priortt[i][j],
//// lhoodtt[i][j],
//// postrtt[i][j]);
// if (postrtt[i][j]>mx) mx = postrtt[i][j];
// if (postrtt[i][j]<mn) mn = postrtt[i][j];
// }
//// printf("min--max = %f -- %f \n", mn, mx);
// for (int j = 0; j < round(pow(Ndirs,i+1)); ++j) {
// //postrtt[i][j] /= sm;
// postrtt[i][j] = (postrtt[i][j]-mn)/(mx-mn);
// // my tweak to visualize
// // rtt[i][j] = postrtt[i][j];
// }
// }
// else {
// // they were all zero
// float mn = 1;
// float mx = -1;
// for (int j = 0; j < round(pow(Ndirs,i+1)); ++j) {
// postrtt[i][j] = ((i==0)?1.0:postrtt[i-1][j/Ndirs]) *
// priortt[i][j] * 1.0;//((lhoods_sum>0.00001)?(lhoodtt[i][j]):1.0); // ### likelihood embedded here!
//// printf("%d;%d (%d): %f; %f; %f; %f; \n",
//// i,j, j/Ndirs,
//// ((i==0)?1:postrtt[i-1][j/Ndirs]),
//// priortt[i][j],
//// lhoodtt[i][j],
//// postrtt[i][j]);
// if (postrtt[i][j]>mx) mx = postrtt[i][j];
// if (postrtt[i][j]<mn) mn = postrtt[i][j];
// }
//// printf("min--max = %f -- %f \n", mn, mx);
// for (int j = 0; j < round(pow(Ndirs,i+1)); ++j) {
// //postrtt[i][j] /= sm;
// postrtt[i][j] = (postrtt[i][j]-mn)/(mx-mn);
// // my tweak to visualize
// // rtt[i][j] = postrtt[i][j];
// }
// }
int swc_idx = 1;
for (int i = 0; i < Nsteps; ++i) {
for (int j = 0; j < (int)round(pow(Ndirs,i+1)); ++j) {
printf("%d 6 %f %f %f %f -1 \n", swc_idx, xtt[i][j], ytt[i][j], ztt[i][j], postrtt[i][j]);
swc_idx++;
}
} // iter will predict from previous Nstates points: fill xtt, ytt, ztt, vxtt, vytt, vztt, rtt up
for (int k = 0; k < Nstates; ++k) {
// backtrack:
for (int step_i = Nsteps-1; step_i >= 0; --step_i) {
int sample_i_at_step = postrtt_midx/(int)round(pow(Ndirs,Nsteps-1-step_i));
// backtrack index [0, Ndirs), [0, Ndirs^2), [0, Ndirs^3)... at current step
// weighted mean wrt to the selected posteriors
float sample_w = postrtt[step_i][sample_i_at_step];
wt[iter_counter*Nsteps+step_i][k] = sample_w;
wsums[step_i] += sample_w;
xt[iter_counter*Nsteps+step_i][k] = xtt[step_i][sample_i_at_step];
yt[iter_counter*Nsteps+step_i][k] = ytt[step_i][sample_i_at_step];
zt[iter_counter*Nsteps+step_i][k] = ztt[step_i][sample_i_at_step];
vxt[iter_counter*Nsteps+step_i][k] = vxtt[step_i][sample_i_at_step];
vyt[iter_counter*Nsteps+step_i][k] = vytt[step_i][sample_i_at_step];
vzt[iter_counter*Nsteps+step_i][k] = vztt[step_i][sample_i_at_step];
rt[iter_counter*Nsteps+step_i][k] = rtt[step_i][sample_i_at_step];
} // loop steps
} // loop states
if (true) {
// test likelihood in
// start it from the highest intensity location, just test
int xtest = 0;
int ytest = 0;
int ztest = 0;
unsigned char max_val = 0;
for (long i = 0; i < size; ++i) {
if (data1d[i]>max_val) {
max_val = data1d[i];
xtest = i % N;
ztest = i / (N*M);
ytest = i/N-ztest*M;
}
}
// float ang = (170/180.0)*3.14;
// xtest = 100;
// ytest = 100;
// ztest = 20;
// xtest = 303;
// ytest = 533;
// ztest = 62;
// xtest = N/2;
// ytest = M/2;
// ztest = P/2;
printf("\n\n--->%d|%d|%d\n", xtest, ytest, ztest); // trace from the highest intensity point
float gx = 1;
float gy = 1; // -1
float gz = 0;
float g = sqrt(gx*gx+gy*gy+gz*gz);
cout<<"probing tracer likelihood..."<<endl;
float rads;
float got = bayesian_tracer.likelihood2(xtest, ytest, ztest, gx/g,gy/g,gz/g, data1d, N, M, P, rads, true);
cout<< got << endl;
if (true) {cout<<"(^^--^^)exiting..."<<endl; return; }
int * tagmap = new int[size];
for (long i = 0; i < size; ++i) tagmap[i] = -1;
int tag_init = tagmap[ztest*(N*M)+ytest*N+xtest];
// cout<< tag_init << " --> " << endl;
int tag_end = bayesian_tracer.trace(xtest,ytest,ztest, gx/g,gy/g,gz/g, 1.5,
data1d, N, M, P,
PARA.angSig, PARA.gcsSig,
tagmap, tag_init);
cout<< " --> " << tag_end << endl;
delete [] tagmap; tagmap = 0;
// save the test trace
NeuronTree nt_trace;
QString swc_name = PARA.inimg_file + "_testTrace.swc";
nt_trace.name = "testTrace";
for (int ni = 0; ni < bayesian_tracer.node_cnt; ++ni) {
NeuronSWC nn;
nn.nodeinseg_id = ni+1;
nn.type = 6;
nn.n = ni+1;
nn.x = bayesian_tracer.xc[ni];
nn.y = bayesian_tracer.yc[ni];
nn.z = bayesian_tracer.zc[ni];
nn.r = bayesian_tracer.rc[ni];
nn.parent = (ni==0)?-1:ni;
nt_trace.listNeuron.append(nn);
}
writeSWC_file(swc_name.toStdString().c_str(), nt_trace);
}
//
if (true) {cout<<"exiting..."<<endl; return; }
ofstream myfile;
for (int zz = 0; zz < P; ++zz) {
QString of = PARA.inimg_file + "_scr_"+QString("%1").arg(zz, 4, 10, QChar('0'))+".txt";
myfile.open (of.toStdString().c_str());
for (int yy = 0; yy < M; ++yy) { // height
for (int xx = 0; xx < N; ++xx) { // width
myfile << scr[zz*(N*M)+yy*N+xx] << " ";
}
myfile << endl;
}
myfile.close();
}
if (no_overlap(i2xyz[i][0], i2xyz[i][1], i2xyz[i][2], A, B, gpnt_map, N, M, P)) {
float * nn = new float[7];
nn[0] = i2xyz[i][0];
nn[1] = i2xyz[i][1];
nn[2] = i2xyz[i][2];
nn[3] = i2sig[i];
nn[4] = i2vx[i];
nn[5] = i2vy[i];
nn[6] = i2vz[i];
gpnt_list.push_back(nn); // add it to the list
// label the added region in the map
for (int xnn = i2xyz[i][0]-A; xnn <= i2xyz[i][0]+A; ++xnn) {
for (int ynn = i2xyz[i][1]-A; ynn <= i2xyz[i][1]+A; ++ynn) {
for (int znn = i2xyz[i][2]-B; znn <= i2xyz[i][2]+B; ++znn) {
if (xnn>=0 && xnn<N && ynn>=0 && ynn<M && znn>=0 && znn<P) {
gpnt_map[znn*N*M+ynn*N+xnn] = gpnt_list.size(); // >0
}
}
}
}
} // initial one was valid
else { // check if the initial one (indexed with 'i') overlaps
int A = ceil(SCALE_RADIUS*i2sig[i]);
int B = (P==1)? 0 : round(SCALE_RADIUS*(i2sig[i]/PARA.zDist));
} // !found
// unsigned char * out_zncc_plot = new unsigned char[size];
// for (long i = 0; i < cnt_fg; ++i) {
// if (i2zncc[i]>=0) {
// int x = i2xyz[i][0];
// int y = i2xyz[i][1];
// int z = i2xyz[i][2];
// out_zncc_plot[z*N*M+y*N+x] = i2zncc[i] * 255;
// }
// }
// QString outimg_file = PARA.inimg_file + QString("_corr.tif");
// simple_saveimage_wrapper(callback, outimg_file.toStdString().c_str(), out_zncc_plot, in_sz, V3D_UINT8);
// delete [] out_zncc_plot; out_zncc_plot = 0;
// mdl.get_corr(x,y,z, data1d, N, M, P, get_zncc, get_s, get_vx, get_vy, get_vz);
// int A = ceil(SCALE_RADIUS*get_s);
// int B = (P==1)? 0 : round(SCALE_RADIUS*(get_s/PARA.zDist));
// if (no_overlap(xn, yn, zn, A, B, gpnt_map, N, M, P)) {
// found = true;
// lmax_zncc = get_zncc; // so that the next one can pick up from there
// gpnt_x = xn;
// gpnt_y = yn;
// gpnt_z = zn;
// gpnt_s = get_s;
// gpnt_vx = get_vx;
// gpnt_vy = get_vy;
// gpnt_vz = get_vz;
// }
//mdl.get_corr(xn,yn,zn, data1d, N, M, P, get_zncc, get_s, get_vx, get_vy, get_vz);
// gpnt_map[zn*N*M+yn*N+xn] = 0;
// i2zncc[in] = get_zncc;
// i2sig[in] = get_s;
// i2vx[in] = get_vx;
// i2vy[in] = get_vy;
// i2vz[in] = get_vz;
// else {
// get_zncc = i2zncc[in];
// get_s = i2sig[in];
// get_vx = i2vx[in];
// get_vy = i2vy[in];
// get_vz = i2vz[in];
// }
// int in = xyz2i[zn*N*M+yn*N+xn];
// if (in>=0) { // if nbr. was in the fg.
// }
/// no overlap is expelled - inefficient - better to block starting the calcualtion at some location
//bool no_overlap(int atx, int aty, int atz, int lim_xy, int lim_z, int* input_gpnt_map, int width, int height, int length) {
// bool not_ovlping = true;
// for (int xnn = atx-lim_xy; xnn <= atx+lim_xy; ++xnn) {
// for (int ynn = aty-lim_xy; ynn <= aty+lim_xy; ++ynn) {
// for (int znn = atz-lim_z; znn <= atz+lim_z; ++znn) {
// if (xnn>=0 && xnn<width && ynn>=0 && ynn<height && znn>=0 && znn<length && input_gpnt_map[znn*width*height+ynn*width+xnn]>0) {
// not_ovlping = false; // it overlaps with some other region
// return not_ovlping;
// }
// }
// }
// }
// return not_ovlping;
//}