12 cout <<
"Usage: check_benchmark tested_image.hdr" << endl;
17 int main(
int argc,
char** argv)
24 string hdr_new = (string)argv[1];
31 ifstream fhdr(hdr_new.c_str());
34 cerr <<
"***** Input header file '" << hdr_new <<
"' is missing or corrupted !" << endl;
40 string castor_version =
"";
44 fhdr.getline(line,1024);
49 string test = (string)line;
51 found = test.find(
"name of data file");
52 if (found!=string::npos)
55 found = test.find(
"=");
56 img_new = test.substr(found+1);
58 while ( (found=img_new.find(
" ")) != string::npos) img_new.replace(found,1,
"");
59 while ( (found=img_new.find(
"\r")) != string::npos) img_new.replace(found,1,
"");
62 found = test.find(
"CASToR version");
63 if (found!=string::npos)
66 found = test.find(
"=");
67 castor_version = test.substr(found+1);
69 while ( (found=castor_version.find(
" ")) != string::npos) castor_version.replace(found,1,
"");
70 while ( (found=castor_version.find(
"\r")) != string::npos) castor_version.replace(found,1,
"");
73 fhdr.getline(line,1024);
82 cerr <<
"***** Did not find the data file name in the provided header file !" << endl;
86 if (castor_version==
"")
88 cout <<
" --> Did not find the CASToR version in the provided header file. Assume it to be 1.0." << endl;
89 castor_version =
"1.0";
93 cout <<
" --> Found CASToR version: " << castor_version << endl;
100 string castor_version_trunc = castor_version;
102 size_t first_point = castor_version_trunc.find_first_of(
".");
104 size_t last_point = string::npos;
105 while ( (last_point=castor_version_trunc.find_last_of(
".")) != first_point )
107 castor_version_trunc = castor_version_trunc.substr(0,last_point);
110 if (castor_version_trunc!=castor_version)
112 cout <<
" --> Assume same results as CASToR version " << castor_version_trunc << endl;
113 castor_version = castor_version_trunc;
120 size_t pos_challenger = img_new.find(
"challenger");
121 if (pos_challenger==string::npos)
123 cerr <<
"***** The name of the image file being tested is supposed to contain the key work \"challenger\" in its name !" << endl;
127 string img_ref = img_new;
129 string reference =
"reference_v" + castor_version;
131 img_ref.replace(pos_challenger,10,reference);
141 int dim_xy = dim_x * dim_y;
142 int dim_xyz = dim_x * dim_y * dim_z;
145 float* image_ref = (
float*)malloc(dim_xyz*
sizeof(
float));
146 float* image_new = (
float*)malloc(dim_xyz*
sizeof(
float));
149 FILE* f_ref = fopen(img_ref.c_str(),
"rb");
152 cerr <<
"***** Input reference image file '" << img_ref <<
"' is missing or corrupted !" << endl;
155 int nb_data_ref = fread(image_ref,
sizeof(
float),dim_xyz,f_ref);
157 if (nb_data_ref!=dim_xyz)
159 cerr <<
"***** Failed to read all data from reference image file '" << img_ref <<
"' !" << endl;
164 FILE* f_new = fopen(img_new.c_str(),
"rb");
167 cerr <<
"***** Input tested image file '" << img_new <<
"' is missing or corrupted !" << endl;
170 int nb_data_new = fread(image_new,
sizeof(
float),dim_xyz,f_new);
172 if (nb_data_new!=dim_xyz)
174 cerr <<
"***** Failed to read all data from tested image file '" << img_new <<
"' !" << endl;
183 double* image_diff = (
double*)malloc(dim_xyz*
sizeof(
double));
186 for (
int v=0; v<dim_xyz; v++)
188 image_diff[v] = ((double)image_new[v])-((double)image_ref[v]);
200 double* mean_difference_per_slice = (
double*)calloc(dim_z,
sizeof(
double));
201 double* mean_reference_per_slice = (
double*)calloc(dim_z,
sizeof(
double));
202 for (
int z=0; z<dim_z; z++)
204 for (
int xy=0; xy<dim_xy; xy++)
206 int v = z * dim_xy + xy;
207 mean_difference_per_slice[z] += image_diff[v];
208 mean_reference_per_slice[z] += ((double)image_ref[v]);
210 mean_difference_per_slice[z] /= ((double)dim_xy);
211 mean_reference_per_slice[z] /= ((double)dim_xy);
215 double global_mean_difference = 0.;
216 double global_mean_reference = 0.;
217 for (
int z=0; z<dim_z; z++)
219 global_mean_difference += mean_difference_per_slice[z];
220 global_mean_reference += mean_reference_per_slice[z];
222 global_mean_difference /= ((double)dim_z);
223 global_mean_reference /= ((double)dim_z);
226 float global_mean_relative_difference = global_mean_difference / global_mean_reference;
229 float global_mean_relative_difference_threshold = 5.e-5;
230 if (fabs(global_mean_relative_difference) < global_mean_relative_difference_threshold)
232 cout <<
" --> Mean relative difference test over the whole image passed (value: " << global_mean_relative_difference <<
")." << endl;
237 cout <<
" --> !!!!! The mean relative difference over the whole image is " << global_mean_relative_difference <<
" !" << endl;
238 cout <<
" It is supposed to be between -" << global_mean_relative_difference_threshold <<
" and " 239 << global_mean_relative_difference_threshold <<
" !" << endl;
244 float slice_relative_difference_threshold = 5.e-4;
245 float max_mean_relative_difference_over_slices = 0.;
246 int slice_not_passed = 0;
247 for (
int z=0; z<dim_z; z++)
249 float value = fabs(mean_difference_per_slice[z] / mean_reference_per_slice[z]);
250 if (value > max_mean_relative_difference_over_slices) max_mean_relative_difference_over_slices = value;
251 if (value >= slice_relative_difference_threshold) slice_not_passed++;
253 if (slice_not_passed==0)
255 cout <<
" --> Mean relative difference test for all slices passed (maximum absolute value: " << max_mean_relative_difference_over_slices <<
")." << endl;
260 cout <<
" --> !!!!! The mean relative difference for each slice has a maximum absolute value of " << max_mean_relative_difference_over_slices <<
" !" << endl;
261 cout <<
" It is supposed to be between -" << slice_relative_difference_threshold <<
" and " << slice_relative_difference_threshold <<
" !" << endl;
262 cout <<
" The test failed for " << slice_not_passed <<
" slices !" << endl;
272 int voxel_margin = 5;
275 float max_abs_relative_difference = 0.;
276 for (
int z=0; z<dim_z; z++)
279 if (z<voxel_margin || z>dim_z-voxel_margin-1)
continue;
280 for (
int y=0; y<dim_y; y++)
283 if (y<voxel_margin || y>dim_y-voxel_margin-1)
continue;
284 for (
int x=0; x<dim_x; x++)
287 int v = z * dim_y * dim_x + y * dim_x + x;
289 if (x<voxel_margin || x>dim_x-voxel_margin-1)
continue;
290 float abs_relative_difference = 0.;
291 if (image_ref[v]!=0.) abs_relative_difference = fabs(image_diff[v]/image_ref[v]);
292 if (abs_relative_difference > max_abs_relative_difference) max_abs_relative_difference = abs_relative_difference;
298 float max_abs_relative_difference_threshold = 1.e-3;
299 if (max_abs_relative_difference < max_abs_relative_difference_threshold)
301 cout <<
" --> Maximum absolute relative difference test passed (value: " << max_abs_relative_difference <<
")." << endl;
306 cout <<
" --> !!!!! The maximum absolute relative difference is " << max_abs_relative_difference <<
" !" << endl;
307 cout <<
" It is supposed to be below " << max_abs_relative_difference_threshold <<
" !" << endl;
318 cout <<
" --> Benchmark successfully passed." << endl;
324 cout <<
" --> !!!!! Benchmark tests failed (" << problems <<
" problems found) !" << endl;
325 cout <<
" Double check all what you have done first. Then email the mailing list." << endl;
int main(int argc, char **argv)