16 cout <<
"Usage: check_benchmark tested_image.hdr" << endl;
21 int main(
int argc,
char** argv)
28 string hdr_new = (string)argv[1];
29 string nb_dyn_imgs_str = (string)argv[2];
36 ifstream fhdr(hdr_new.c_str());
39 cerr <<
"***** Input header file '" << hdr_new <<
"' is missing or corrupted !" << endl;
45 string castor_version =
"";
49 fhdr.getline(line,1024);
54 string test = (string)line;
56 found = test.find(
"name of data file");
57 if (found!=string::npos)
60 found = test.find(
"=");
61 img_new = test.substr(found+1);
63 while ( (found=img_new.find(
" ")) != string::npos) img_new.replace(found,1,
"");
64 while ( (found=img_new.find(
"\r")) != string::npos) img_new.replace(found,1,
"");
67 found = test.find(
"CASToR version");
68 if (found!=string::npos)
71 found = test.find(
"=");
72 castor_version = test.substr(found+1);
74 while ( (found=castor_version.find(
" ")) != string::npos) castor_version.replace(found,1,
"");
75 while ( (found=castor_version.find(
"\r")) != string::npos) castor_version.replace(found,1,
"");
78 fhdr.getline(line,1024);
87 cerr <<
"***** Did not find the data file name in the provided header file !" << endl;
91 if (castor_version==
"")
93 cout <<
" --> Did not find the CASToR version in the provided header file. Assume it to be 1.0." << endl;
94 castor_version =
"1.0";
98 cout <<
" --> Found CASToR version: " << castor_version << endl;
105 string castor_version_trunc = castor_version;
107 size_t first_point = castor_version_trunc.find_first_of(
".");
109 size_t last_point = string::npos;
110 while ( (last_point=castor_version_trunc.find_last_of(
".")) != first_point )
112 castor_version_trunc = castor_version_trunc.substr(0,last_point);
115 if (castor_version_trunc!=castor_version)
117 cout <<
" --> Assume same results as CASToR version " << castor_version_trunc << endl;
118 castor_version = castor_version_trunc;
125 size_t pos_challenger = img_new.find(
"challenger");
126 if (pos_challenger==string::npos)
128 cerr <<
"***** The name of the image file being tested is supposed to contain the key work \"challenger\" in its name !" << endl;
132 string img_ref = img_new;
134 string reference =
"reference_v" + castor_version;
136 img_ref.replace(pos_challenger,10,reference);
143 int nb_dyn_imgs = atoi(nb_dyn_imgs_str.c_str());
147 int dim_z = nb_dyn_imgs;
148 int dim_xy = dim_x * dim_y;
149 int dim_xyz = dim_x * dim_y * dim_z;
152 float* image_ref = (
float*)malloc(dim_xyz*
sizeof(
float));
153 float* image_new = (
float*)malloc(dim_xyz*
sizeof(
float));
156 FILE* f_ref = fopen(img_ref.c_str(),
"rb");
159 cerr <<
"***** Input reference image file '" << img_ref <<
"' is missing or corrupted !" << endl;
162 int nb_data_ref = fread(image_ref,
sizeof(
float),dim_xyz,f_ref);
164 if (nb_data_ref!=dim_xyz)
166 cerr <<
"***** Failed to read all data from reference image file '" << img_ref <<
"' !" << endl;
171 FILE* f_new = fopen(img_new.c_str(),
"rb");
174 cerr <<
"***** Input tested image file '" << img_new <<
"' is missing or corrupted !" << endl;
177 int nb_data_new = fread(image_new,
sizeof(
float),dim_xyz,f_new);
179 if (nb_data_new!=dim_xyz)
181 cerr <<
"***** Failed to read all data from tested image file '" << img_new <<
"' !" << endl;
190 double* image_diff = (
double*)malloc(dim_xyz*
sizeof(
double));
193 for (
int v=0; v<dim_xyz; v++)
195 image_diff[v] = ((double)image_new[v])-((double)image_ref[v]);
207 double* mean_difference_per_slice = (
double*)calloc(dim_z,
sizeof(
double));
208 double* mean_reference_per_slice = (
double*)calloc(dim_z,
sizeof(
double));
209 for (
int z=0; z<dim_z; z++)
211 for (
int xy=0; xy<dim_xy; xy++)
213 int v = z * dim_xy + xy;
214 mean_difference_per_slice[z] += image_diff[v];
215 mean_reference_per_slice[z] += ((double)image_ref[v]);
217 mean_difference_per_slice[z] /= ((double)dim_xy);
218 mean_reference_per_slice[z] /= ((double)dim_xy);
222 double global_mean_difference = 0.;
223 double global_mean_reference = 0.;
224 for (
int z=0; z<dim_z; z++)
226 global_mean_difference += mean_difference_per_slice[z];
227 global_mean_reference += mean_reference_per_slice[z];
229 global_mean_difference /= ((double)dim_z);
230 global_mean_reference /= ((double)dim_z);
233 float global_mean_relative_difference = global_mean_difference / global_mean_reference;
236 float global_mean_relative_difference_threshold = 5.e-5;
237 if (fabs(global_mean_relative_difference) < global_mean_relative_difference_threshold)
239 cout <<
" --> Mean relative difference test over the whole image passed (value: " << global_mean_relative_difference <<
")." << endl;
244 cout <<
" --> !!!!! The mean relative difference over the whole image is " << global_mean_relative_difference <<
" !" << endl;
245 cout <<
" It is supposed to be between -" << global_mean_relative_difference_threshold <<
" and " 246 << global_mean_relative_difference_threshold <<
" !" << endl;
251 float slice_relative_difference_threshold = 5.e-4;
252 float max_mean_relative_difference_over_slices = 0.;
253 int slice_not_passed = 0;
254 for (
int z=0; z<dim_z; z++)
256 float value = fabs(mean_difference_per_slice[z] / mean_reference_per_slice[z]);
257 if (value > max_mean_relative_difference_over_slices) max_mean_relative_difference_over_slices = value;
258 if (value >= slice_relative_difference_threshold) slice_not_passed++;
260 if (slice_not_passed==0)
262 cout <<
" --> Mean relative difference test for all slices passed (maximum absolute value: " << max_mean_relative_difference_over_slices <<
")." << endl;
267 cout <<
" --> !!!!! The mean relative difference for each slice has a maximum absolute value of " << max_mean_relative_difference_over_slices <<
" !" << endl;
268 cout <<
" It is supposed to be between -" << slice_relative_difference_threshold <<
" and " << slice_relative_difference_threshold <<
" !" << endl;
269 cout <<
" The test failed for " << slice_not_passed <<
" slices !" << endl;
279 int voxel_margin = 5;
282 float max_abs_relative_difference = 0.;
283 for (
int z=0; z<dim_z; z++)
286 if (z<voxel_margin || z>dim_z-voxel_margin-1)
continue;
287 for (
int y=0; y<dim_y; y++)
290 if (y<voxel_margin || y>dim_y-voxel_margin-1)
continue;
291 for (
int x=0; x<dim_x; x++)
294 int v = z * dim_y * dim_x + y * dim_x + x;
296 if (x<voxel_margin || x>dim_x-voxel_margin-1)
continue;
297 float abs_relative_difference = 0.;
298 if (image_ref[v]!=0.) abs_relative_difference = fabs(image_diff[v]/image_ref[v]);
299 if (abs_relative_difference > max_abs_relative_difference) max_abs_relative_difference = abs_relative_difference;
305 float max_abs_relative_difference_threshold = 1.e-3;
306 if (max_abs_relative_difference < max_abs_relative_difference_threshold)
308 cout <<
" --> Maximum absolute relative difference test passed (value: " << max_abs_relative_difference <<
")." << endl;
313 cout <<
" --> !!!!! The maximum absolute relative difference is " << max_abs_relative_difference <<
" !" << endl;
314 cout <<
" It is supposed to be below " << max_abs_relative_difference_threshold <<
" !" << endl;
325 cout <<
" --> Benchmark successfully passed." << endl;
331 cout <<
" --> !!!!! Benchmark tests failed (" << problems <<
" problems found) !" << endl;
332 cout <<
" Double check all what you have done first. Then email the mailing list." << endl;
int main(int argc, char **argv)