-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNeuroProof_stack.cpp
332 lines (261 loc) · 10.3 KB
/
NeuroProof_stack.cpp
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
#include "DataStructures/Stack.h"
// #include "Priority/GPR.h"
// #include "Priority/LocalEdgePriority.h"
// #include "Utilities/ScopeTime.h"
// #include "ImportsExports/ImportExportRagPriority.h"
#include <fstream>
#include <sstream>
#include <cassert>
#include <iostream>
#include <memory>
// #include <json/json.h>
// #include <json/value.h>
#include "Utilities/h5read.h"
#include "Utilities/h5write.h"
#include <ctime>
#include <cmath>
#include <cstring>
using std::cerr; using std::cout; using std::endl;
using std::ifstream;
using std::string;
using std::stringstream;
using namespace NeuroProof;
using std::vector;
template <class T>
void padZero(T* data, const size_t* dims, int pad_length, T** ppadded_data){
// implemented only for 3D arrays
unsigned long int newsize = (dims[0]+2*pad_length)*(dims[1]+2*pad_length)*(dims[2]+2*pad_length);
*ppadded_data = new T[newsize];
T* padded_data = *ppadded_data;
memset((void*) padded_data, 0, sizeof(T)*newsize);
unsigned int width, plane_size, width0, plane_size0, i0,j0,k0, i,j,k;
for (i=pad_length, i0=0; i<= dims[0] ; i++, i0++)
for (j=pad_length, j0=0; j<= dims[1]; j++, j0++)
for(k=pad_length, k0=0; k<= dims[2]; k++, k0++){
plane_size = (dims[1]+2*pad_length)*(dims[2]+2*pad_length);
width = dims[2]+2*pad_length;
plane_size0 = dims[1]*dims[2];
width0 = dims[2];
padded_data[i*plane_size+ j*width + k] = data[i0*plane_size0 + j0*width0 + k0];
}
}
bool endswith(string filename, string extn){
unsigned found = filename.find_last_of(".");
string fextn = filename.substr(found);
if (fextn.compare(extn) == 0 )
return true;
else return false;
}
int main(int argc, char** argv)
{
int i, j, k;
cout<< "Reading data ..." <<endl;
if (argc<15){
printf("format: NeuroProof_stack -watershed watershed_h5_file dataset \
-prediction prediction_h5_file dataset \
-classifier classifier_file \
-output output_h5_file dataset \
-groundtruth groundtruth_h5_file dataset \
-threshold agglomeration_threshold \
-algorithm algorithm_type\n");
return 0;
}
int argc_itr=1;
string watershed_filename="";
string watershed_dataset_name="";
string prediction_filename="";
string prediction_dataset_name="";
string output_filename;
string output_dataset_name;
string groundtruth_filename="";
string groundtruth_dataset_name="";
string classifier_filename;
string output_filename_nomito;
double threshold = 0.2;
int agglo_type = 1;
bool merge_mito = true;
bool merge_mito_by_chull = false;
bool read_off_rwts = false;
double mito_thd=0.3;
size_t min_region_sz=100;
while(argc_itr<argc){
if (!(strcmp(argv[argc_itr],"-watershed"))){
watershed_filename = argv[++argc_itr];
watershed_dataset_name = argv[++argc_itr];
}
if (!(strcmp(argv[argc_itr],"-classifier"))){
classifier_filename = argv[++argc_itr];
}
if (!(strcmp(argv[argc_itr],"-prediction"))){
prediction_filename = argv[++argc_itr];
prediction_dataset_name = argv[++argc_itr];
}
if (!(strcmp(argv[argc_itr],"-output"))){
output_filename = argv[++argc_itr];
output_dataset_name = argv[++argc_itr];
}
if (!(strcmp(argv[argc_itr],"-groundtruth"))){
groundtruth_filename = argv[++argc_itr];
groundtruth_dataset_name = argv[++argc_itr];
}
if (!(strcmp(argv[argc_itr],"-threshold"))){
threshold = atof(argv[++argc_itr]);
}
if (!(strcmp(argv[argc_itr],"-algorithm"))){
agglo_type = atoi(argv[++argc_itr]);
}
if (!(strcmp(argv[argc_itr],"-min_region_sz"))){
min_region_sz = atoi(argv[++argc_itr]);
}
if (!(strcmp(argv[argc_itr],"-nomito"))){
merge_mito = false;
}
if (!(strcmp(argv[argc_itr],"-mito_thd"))){
mito_thd = atof(argv[++argc_itr]);
}
if (!(strcmp(argv[argc_itr],"-read_off"))){
if (agglo_type==2)
read_off_rwts = true;
}
++argc_itr;
}
output_filename_nomito = output_filename;
size_t ofn = output_filename_nomito.find_last_of(".");
output_filename_nomito.erase(ofn-1,1);
time_t start, end;
time(&start);
H5Read watershed(watershed_filename.c_str(),watershed_dataset_name.c_str());
Label* watershed_data=NULL;
watershed.readData(&watershed_data);
int depth = watershed.dim()[0];
int height = watershed.dim()[1];
int width = watershed.dim()[2];
int pad_len=1;
Label *zp_watershed_data=NULL;
padZero(watershed_data, watershed.dim(),pad_len,&zp_watershed_data);
StackPredict* stackp = new StackPredict(zp_watershed_data, depth+2*pad_len, height+2*pad_len, width+2*pad_len, pad_len);
stackp->set_feature_mgr(new FeatureMgr());
stackp->set_merge_mito(merge_mito, mito_thd);
H5Read prediction(prediction_filename.c_str(),prediction_dataset_name.c_str(), true);
float* prediction_data=NULL;
prediction.readData(&prediction_data);
double* prediction_single_ch = new double[depth*height*width];
double* prediction_ch0 = new double[depth*height*width];
size_t cube_size, plane_size, element_size=prediction.dim()[prediction.total_dim()-1];
size_t position, count;
for (int ch=0; ch < element_size; ch++){
count = 0;
for(i=0; i<depth; i++){
cube_size = height*width*element_size;
for(j=0; j<height; j++){
plane_size = width*element_size;
for(k=0; k<width; k++){
position = i*cube_size + j*plane_size + k*element_size + ch;
prediction_single_ch[count] = prediction_data[position];
count++;
}
}
}
double* zp_prediction_single_ch = NULL;
padZero(prediction_single_ch,watershed.dim(),pad_len,&zp_prediction_single_ch);
if (ch == 0)
memcpy(prediction_ch0, prediction_single_ch, depth*height*width*sizeof(double));
stackp->add_prediction_channel(zp_prediction_single_ch);
}
stackp->set_basic_features();
EdgeClassifier* eclfr;
if (endswith(classifier_filename, ".h5")){
string nameonly = classifier_filename.substr(0, classifier_filename.find_last_of("."));
// if (nameonly.find("parallel") != std::string::npos)
// eclfr = new VigraRFclassifierP(classifier_filename.c_str());
// else
eclfr = new VigraRFclassifier(classifier_filename.c_str());
}
// eclfr = new VigraRFclassifier(classifier_filename.c_str());
else if (endswith(classifier_filename, ".xml")){
string nameonly = classifier_filename.substr(0, classifier_filename.find_last_of("."));
// if (nameonly.find("parallel") != std::string::npos)
// eclfr = new OpencvRFclassifierP(classifier_filename.c_str());
// else
eclfr = new OpencvRFclassifier(classifier_filename.c_str());
}
//eclfr = new CompositeRFclassifier(classifier_filename.c_str());
// eclfr = new OpencvRFclassifier(classifier_filename.c_str());
stackp->get_feature_mgr()->set_classifier(eclfr);
Label* groundtruth_data=NULL;
if (groundtruth_filename.size()>0){
H5Read groundtruth(groundtruth_filename.c_str(),groundtruth_dataset_name.c_str());
groundtruth.readData(&groundtruth_data);
stackp->set_groundtruth(groundtruth_data);
}
cout<<"Building RAG ...";
stackp->build_rag();
cout<<"done with "<< stackp->get_num_bodies()<< " regions\n";
cout<<"Inclusion removal ...";
stackp->remove_inclusions();
cout<<"done with "<< stackp->get_num_bodies()<< " regions\n";
stackp->compute_vi();
stackp->compute_groundtruth_assignment();
if (agglo_type==0){
cout<<"Agglomerating (flat) upto threshold "<< threshold<< " ...\n";
stackp->agglomerate_rag_flat(threshold);
}
else if (agglo_type==1){
cout<<"Agglomerating (agglo) upto threshold "<< threshold<< " ...\n";
stackp->agglomerate_rag(threshold, false);
}
else if (agglo_type == 2){
cout<<"Agglomerating (mrf) upto threshold "<< threshold<< " ...\n";
stackp->agglomerate_rag_mrf(threshold, read_off_rwts, output_filename, classifier_filename);
}
else if (agglo_type == 3){
cout<<"Agglomerating (queue) upto threshold "<< threshold<< " ...\n";
stackp->agglomerate_rag_queue(threshold, false);
}
else if (agglo_type == 4){
cout<<"Agglomerating (flat) upto threshold "<< threshold<< " ...\n";
stackp->agglomerate_rag_flat(threshold, false, output_filename, classifier_filename);
}
cout << "Done with "<< stackp->get_num_bodies()<< " regions\n";
cout<<"Inclusion removal ...";
stackp->remove_inclusions();
cout<<"done with "<< stackp->get_num_bodies()<< " regions\n";
stackp->compute_vi();
Label * temp_label_volume1D = stackp->get_label_volume();
if ((!merge_mito) && (min_region_sz>0))
stackp->absorb_small_regions2(prediction_ch0, temp_label_volume1D, min_region_sz);
hsize_t dims_out[3];
dims_out[0]=depth; dims_out[1]= height; dims_out[2]= width;
H5Write(output_filename_nomito.c_str(),output_dataset_name.c_str(),3,dims_out, temp_label_volume1D);
printf("Output-nomito written to %s, dataset %s\n",output_filename_nomito.c_str(),output_dataset_name.c_str());
delete[] temp_label_volume1D;
if (merge_mito){
cout<<"Merge Mitochondria (border-len) ...";
stackp->merge_mitochondria_a();
cout<<"done with "<< stackp->get_num_bodies()<< " regions\n";
cout<<"Inclusion removal ...";
stackp->remove_inclusions();
cout<<"done with "<< stackp->get_num_bodies()<< " regions\n";
stackp->compute_vi();
temp_label_volume1D = stackp->get_label_volume();
if (min_region_sz>0)
stackp->absorb_small_regions2(prediction_ch0, temp_label_volume1D, min_region_sz);
dims_out[0]=depth; dims_out[1]= height; dims_out[2]= width;
H5Write(output_filename.c_str(),output_dataset_name.c_str(),3,dims_out, temp_label_volume1D);
printf("Output written to %s, dataset %s\n",output_filename.c_str(),output_dataset_name.c_str());
delete[] temp_label_volume1D;
}
//stackp->determine_edge_locations();
//stackp->write_graph(output_filename);
time(&end);
printf("Time elapsed: %.2f\n", (difftime(end,start))*1.0/60);
if (watershed_data)
delete[] watershed_data;
delete[] zp_watershed_data;
if (prediction_data)
delete[] prediction_data;
delete[] prediction_single_ch;
if (groundtruth_data)
delete[] groundtruth_data;
return 0;
}