00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "testutil.h"
00037 #include "xydata.h"
00038
00039 #include <algorithm>
00040 #include "emassert.h"
00041
00042
00043 #ifdef _WIN32
00044 #include <windows.h>
00045 #define MAXPATHLEN MAX_PATH
00046 #else
00047 #include <sys/param.h>
00048 #endif //_WIN32
00049
00050 using std::vector;
00051 using std::string;
00052 using std::map;
00053
00054 using namespace EMAN;
00055
00056 int TestUtil::ti[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
00057 float TestUtil::tf[] = {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5};
00058
00059 const char * TestUtil::EMDATA_HEADER_EXT = ".head";
00060 const char * TestUtil::EMDATA_DATA_EXT = ".data";
00061
00062 string TestUtil::progname = "";
00063
00064 int TestUtil::get_debug_int(int i)
00065 {
00066 return ti[i];
00067 }
00068
00069 float TestUtil::get_debug_float(int i)
00070 {
00071 return tf[i];
00072 }
00073
00074 string TestUtil::get_debug_string(int i)
00075 {
00076 char a[32];
00077 sprintf(a, "%d%d", i+1, i+1);
00078 return string(a);
00079 }
00080
00081 string TestUtil::get_debug_image(const string & imagename)
00082 {
00083 char imgpath[MAXPATHLEN];
00084 char * path_env = getenv("DEBUG_IMAGE_PATH");
00085 if (path_env) {
00086 sprintf(imgpath, "%s/%s", path_env, imagename.c_str());
00087 }
00088 else {
00089 sprintf(imgpath, "%s/images/%s", getenv("HOME"), imagename.c_str());
00090 }
00091 return string(imgpath);
00092 }
00093
00094 string TestUtil::get_golden_image(const string & imagename)
00095 {
00096 char imgpath[MAXPATHLEN];
00097 char * path_env = getenv("DEBUG_IMAGE_PATH");
00098 if (path_env) {
00099 sprintf(imgpath, "%s/testdata/%s", path_env, imagename.c_str());
00100 }
00101 else {
00102 sprintf(imgpath, "%s/images/testdata/%s", getenv("HOME"), imagename.c_str());
00103 }
00104 return string(imgpath);
00105 }
00106
00107 void TestUtil::to_emobject(const Dict& d)
00108 {
00109 if (d.has_key("floatarray")) {
00110 vector<float> array = d["floatarray"];
00111 for (size_t i = 0; i < array.size(); i++) {
00112 Assert(array[i] == tf[i]);
00113 LOGDEBUG("floatarray[%d] = %f\n", i, array[i]);
00114 }
00115 }
00116
00117 if (d.has_key("emdata")) {
00118 EMData * img = d["emdata"];
00119 if (img) {
00120 int nx = img->get_xsize();
00121 int ny = img->get_ysize();
00122 int nz = img->get_zsize();
00123 Assert(nx == ti[0]);
00124 Assert(ny == ti[1]);
00125 Assert(nz == ti[2]);
00126 LOGDEBUG("image size = (%d, %d, %d)\n", nx, ny, nz);
00127 }
00128 }
00129
00130 if (d.has_key("int")) {
00131 int n = d["int"];
00132 Assert(n == ti[0]);
00133 LOGDEBUG("int n = %d\n", n);
00134 }
00135
00136 if (d.has_key("float")) {
00137 float f = d["float"];
00138 Assert(f == tf[0]);
00139 LOGDEBUG("float f = %f\n", f);
00140 }
00141
00142 if (d.has_key("long")) {
00143 int l = (int)d["long"];
00144 Assert(l == ti[0]);
00145 LOGDEBUG("long l = %d\n", l);
00146 }
00147
00148 if (d.has_key("string")) {
00149 string s = (const char*)d["string"];
00150 string s2 = get_debug_string(0);
00151 Assert(s == s2);
00152 }
00153
00154
00155 if (d.has_key("xydata")) {
00156 XYData *xyd = d["xydata"];
00157 size_t nitems = xyd->get_size();
00158 for (size_t i = 0; i < nitems; i++) {
00159 float xi = xyd->get_x(i);
00160 float yi = xyd->get_y(i);
00161 LOGDEBUG("xydata[%d] = (%f,%f)\n", i, xi, yi);
00162 Assert(xi == tf[i]);
00163 Assert(yi == tf[i]);
00164 }
00165 }
00166
00167 if (d.has_key("stringarray")) {
00168 vector<string> array = d["stringarray"];
00169 for (size_t i = 0; i < array.size(); i++) {
00170 Assert(array[i] == get_debug_string(i));
00171 LOGDEBUG("stringarray[%d] = %s\n", i, array[i].c_str());
00172 }
00173 }
00174 }
00175
00176
00177 IntPoint TestUtil::test_IntPoint(const IntPoint & p)
00178 {
00179 Assert(p[0] == ti[0]);
00180 Assert(p[1] == ti[1]);
00181 Assert(p[2] == ti[2]);
00182 LOGDEBUG("IntPoint p = (%d, %d, %d)\n", p[0], p[1], p[2]);
00183 return IntPoint(ti[0], ti[1], ti[2]);
00184 }
00185
00186 FloatPoint TestUtil::test_FloatPoint(const FloatPoint & p)
00187 {
00188 Assert(p[0] == tf[0]);
00189 Assert(p[1] == tf[1]);
00190 Assert(p[2] == tf[2]);
00191 LOGDEBUG("FloatPoint p = (%f, %f, %f)\n", p[0], p[1], p[2]);
00192 return FloatPoint(tf[0], tf[1], tf[2]);
00193 }
00194
00195
00196 IntSize TestUtil::test_IntSize(const IntSize & p)
00197 {
00198 Assert(p[0] == ti[0]);
00199 Assert(p[1] == ti[1]);
00200 Assert(p[2] == ti[2]);
00201 LOGDEBUG("IntSize p = (%d, %d, %d)\n", p[0], p[1], p[2]);
00202 return IntSize(ti[0], ti[1], ti[2]);
00203 }
00204
00205
00206 FloatSize TestUtil::test_FloatSize(const FloatSize & p)
00207 {
00208 Assert(p[0] == tf[0]);
00209 Assert(p[1] == tf[1]);
00210 Assert(p[2] == tf[2]);
00211 LOGDEBUG("FloatSize p = (%f, %f, %f)\n", p[0], p[1], p[2]);
00212 return FloatSize(tf[0], tf[1], tf[2]);
00213 }
00214
00215
00216 Vec3i TestUtil::test_Vec3i(const Vec3i & p)
00217 {
00218 Assert(p[0] == ti[0]);
00219 Assert(p[1] == ti[1]);
00220 Assert(p[2] == ti[2]);
00221 LOGDEBUG("Vec3i p = (%d, %d, %d)\n", p[0], p[1], p[2]);
00222 return Vec3i(ti[0], ti[1], ti[2]);
00223 }
00224
00225 Vec3f TestUtil::test_Vec3f(const Vec3f & p)
00226 {
00227 Assert(p[0] == tf[0]);
00228 Assert(p[1] == tf[1]);
00229 Assert(p[2] == tf[2]);
00230 LOGDEBUG("Vec3f p = (%f, %f, %f)\n", p[0], p[1], p[2]);
00231 return Vec3f(tf[0], tf[1], tf[2]);
00232 }
00233
00234
00235 map<string, int> TestUtil::test_map_int(const map<string, int>& d)
00236 {
00237 map<string, int> r;
00238 map<string, int>::const_iterator p;
00239 for (p = d.begin(); p != d.end(); p++) {
00240 LOGDEBUG("map[\"%s\"] = %d; ", p->first.c_str(), p->second);
00241 r[p->first] = p->second;
00242 }
00243 LOGDEBUG("\n");
00244 return r;
00245 }
00246
00247 map<string, long> TestUtil::test_map_long(const map<string, long>& d)
00248 {
00249 map<string, long> r;
00250 map<string, long>::const_iterator p;
00251 for (p = d.begin(); p != d.end(); p++) {
00252 LOGDEBUG("map[\"%s\"] = %d; ", p->first.c_str(), p->second);
00253 r[p->first] = p->second;
00254 }
00255 LOGDEBUG("\n");
00256 return r;
00257 }
00258
00259 map<string, float> TestUtil::test_map_float(const map<string, float>& d)
00260 {
00261 map<string, float> r;
00262 map<string, float>::const_iterator p;
00263 for (p = d.begin(); p != d.end(); p++) {
00264 LOGDEBUG("map[\"%s\"] = %f; ", p->first.c_str(), p->second);
00265 r[p->first] = p->second;
00266 }
00267 LOGDEBUG("\n");
00268 return r;
00269 }
00270
00271 map<string, string> TestUtil::test_map_string(const map<string, string>& d)
00272 {
00273 map<string, string> r;
00274 map<string, string>::const_iterator p;
00275 for (p = d.begin(); p != d.end(); p++) {
00276 LOGDEBUG("map[\"%s\"] = %s; ", p->first.c_str(), p->second.c_str());
00277 r[p->first] = p->second;
00278 }
00279 LOGDEBUG("\n");
00280 return r;
00281 }
00282
00283 map<string, EMObject> TestUtil::test_map_emobject(const map<string, EMObject>& d)
00284 {
00285 map<string, EMObject> r;
00286 map<string, EMObject>::const_iterator p;
00287 for (p = d.begin(); p != d.end(); p++) {
00288 LOGDEBUG("map[\"%s\"] = %f; ", p->first.c_str(), (float)(p->second));
00289 r[p->first] = EMObject(p->second);
00290 }
00291 LOGDEBUG("\n");
00292 return r;
00293 }
00294
00295 map<string, vector<string> > TestUtil::test_map_vecstring(const map<string,
00296 vector<string> >&)
00297 {
00298 map<string, vector<string> > r;
00299 return r;
00300 }
00301
00302
00303 vector<int> TestUtil::test_vector_int(const vector<int> & v)
00304 {
00305 vector<int> r;
00306 for (size_t i = 0; i < v.size(); i++) {
00307 LOGDEBUG("v[%d]=%d; ", i, v[i]);
00308 Assert(v[i] == ti[i]);
00309 r.push_back(v[i]);
00310 }
00311 LOGDEBUG("\n");
00312 return r;
00313 }
00314
00315 vector<float> TestUtil::test_vector_float(const vector<float> & v)
00316 {
00317 vector<float> r;
00318 for (size_t i = 0; i < v.size(); i++) {
00319 LOGDEBUG("v[%d]=%f; ", i, v[i]);
00320 Assert(v[i] == tf[i]);
00321 r.push_back(v[i]);
00322 }
00323 LOGDEBUG("\n");
00324 return r;
00325 }
00326
00327 vector<long> TestUtil::test_vector_long(const vector<long> & v)
00328 {
00329 vector<long> r;
00330 for (size_t i = 0; i < v.size(); i++) {
00331 LOGDEBUG("v[%d]=%d; ", i, (int)v[i]);
00332 Assert((int)v[i] == ti[i]);
00333 r.push_back(v[i]);
00334 }
00335 LOGDEBUG("\n");
00336 return r;
00337 }
00338
00339 vector<string> TestUtil::test_vector_string(const vector<string> & v)
00340 {
00341 vector<string> r;
00342 for (size_t i = 0; i < v.size(); i++) {
00343 LOGDEBUG("v[%d]=%s; ", i, v[i].c_str());
00344 r.push_back(v[i]);
00345 }
00346 LOGDEBUG("\n");
00347 return r;
00348 }
00349
00350 vector<EMData*> TestUtil::test_vector_emdata(const vector<EMData*> & v)
00351 {
00352 vector<EMData*> r;
00353 for (size_t i = 0; i < v.size(); i++) {
00354 EMData * e = v[i];
00355 LOGDEBUG("Image(%d,%d,%d); ", e->get_xsize(), e->get_ysize(), e->get_zsize());
00356 r.push_back(v[i]);
00357 }
00358 LOGDEBUG("\n");
00359 return r;
00360 }
00361
00362 vector<Pixel> TestUtil::test_vector_pixel(const vector<Pixel> & v)
00363 {
00364 vector<Pixel> r;
00365 for (size_t i = 0; i < v.size(); i++) {
00366 Pixel p = v[i];
00367 LOGDEBUG("Pixel(%d,%d,%d)=%4.2f; ", p.x, p.y, p.z, p.value);
00368 Pixel p2(p.x, p.y, p.z, p.value);
00369 r.push_back(p2);
00370 }
00371
00372 return r;
00373 }
00374
00375 Dict TestUtil::test_dict(const Dict & d)
00376 {
00377 Dict r;
00378
00379 vector<string> keys = d.keys();
00380 sort(keys.begin(), keys.end());
00381
00382 for (size_t i = 0; i < keys.size(); i++) {
00383 LOGDEBUG("keys[%s] = %f\n", keys[i].c_str(), (float)d[keys[i]]);
00384 Assert(keys[i] == get_debug_string(i));
00385 Assert(((float)d[keys[i]]) == tf[i]);
00386 r[keys[i]] = d[keys[i]];
00387 }
00388
00389 return r;
00390 }
00391
00392
00393 int TestUtil::check_image(const string& imagefile, EMData * image)
00394 {
00395 #if DEBUG
00396 string headerfile1 = Util::sbasename(imagefile) + EMDATA_HEADER_EXT;
00397 string datafile1 = Util::sbasename(imagefile) + EMDATA_DATA_EXT;
00398
00399 char imgpath[MAXPATHLEN];
00400 char * path_env = getenv("DEBUG_IMAGE_PATH");
00401 if (path_env) {
00402 sprintf(imgpath, "%s/testdata/%s/", path_env, progname.c_str());
00403 }
00404 else {
00405 sprintf(imgpath, "%s/images/testdata/%s/", getenv("HOME"), progname.c_str());
00406 }
00407
00408 string headerfile2 = string(imgpath) + headerfile1;
00409 string datafile2 = string(imgpath) + datafile1;
00410
00411
00412 if (image) {
00413 dump_emdata(image, imagefile);
00414 }
00415 else {
00416 dump_image_from_file(imagefile);
00417 }
00418
00419 if (!Util::is_file_exist(headerfile2) ||
00420 !Util::is_file_exist(datafile2)) {
00421 return 0;
00422 }
00423
00424 string diffcmd1 = "diff " + headerfile1 + " " + headerfile2;
00425
00426 int err = system(diffcmd1.c_str());
00427 if (!err) {
00428 string diffcmd2 = "diff " + datafile1 + " " + datafile2;
00429 err = system(diffcmd2.c_str());
00430 }
00431 if (err) {
00432 LOGERR("check_image on %s FAILED\n", imagefile.c_str());
00433 }
00434
00435 return err;
00436 #endif
00437 return 0;
00438 }
00439
00440 void TestUtil::dump_image_from_file(const string & filename)
00441 {
00442 EMData * e = new EMData();
00443 e->read_image(filename);
00444 dump_emdata(e, filename);
00445 if( e )
00446 {
00447 delete e;
00448 e = 0;
00449 }
00450 }
00451
00452 void TestUtil::dump_emdata(EMData * image, const string & filename)
00453 {
00454 string filebase = Util::sbasename(filename);
00455 string headerfile = filebase + EMDATA_HEADER_EXT;
00456 string datafile = filebase + EMDATA_DATA_EXT;
00457
00458 FILE *hfile = fopen(headerfile.c_str(), "wb");
00459 if (!hfile) {
00460 throw FileAccessException(headerfile);
00461 }
00462 #if 0
00463 vector<string> excl_keys;
00464 excl_keys.push_back("MRC.label");
00465 excl_keys.push_back("IMAGIC.minute");
00466 excl_keys.push_back("IMAGIC.sec");
00467
00468 Dict attr_dict = image->get_attr_dict();
00469 vector < string > keys = attr_dict.keys();
00470
00471
00472
00473 for (size_t i = 0; i < keys.size(); i++) {
00474
00475 bool is_exclude = false;
00476 for (size_t j = 0; j < excl_keys.size(); j++) {
00477 if (Util::sstrncmp(keys[i].c_str(), excl_keys[j].c_str())) {
00478 is_exclude = true;
00479 break;
00480 }
00481 }
00482 if (!is_exclude) {
00483 fprintf(hfile, "%s = %s\n", keys[i].c_str(),
00484 attr_dict[keys[i]].to_str().c_str());
00485 }
00486 }
00487 #endif
00488
00489 fprintf(hfile, "nx = %d\n", image->get_xsize());
00490 fprintf(hfile, "ny = %d\n", image->get_ysize());
00491 fprintf(hfile, "nz = %d\n", image->get_zsize());
00492
00493 fclose(hfile);
00494 hfile = 0;
00495
00496 FILE *dfile = fopen(datafile.c_str(), "wb");
00497 if (!dfile) {
00498 throw FileAccessException(datafile);
00499 }
00500
00501 int nx = image->get_xsize();
00502 int ny = image->get_ysize();
00503 int nz = image->get_zsize();
00504
00505 size_t row_size = nx * sizeof(float);
00506 size_t nxy = nx * ny;
00507 float * rdata = image->get_data();
00508
00509 for (int i = 0; i < nz; i++) {
00510 for (int j = 0; j < ny; j++) {
00511 fwrite(&rdata[i * nxy + j * nx], row_size, 1, dfile);
00512 }
00513 }
00514 fclose(dfile);
00515 dfile = 0;
00516 }
00517
00518 void TestUtil::set_progname(const string & cur_progname)
00519 {
00520 progname = Util::sbasename(cur_progname);
00521 }
00522
00523
00524 void TestUtil::make_image_file_by_mode(const string & filename,
00525 EMUtil::ImageType image_type, int mode,
00526 EMUtil::EMDataType datatype,
00527 int nx, int ny, int nz)
00528 {
00529 EMData * e = new EMData();
00530 e->set_size(nx, ny, nz);
00531 bool is_complex = EMUtil::is_complex_type(datatype);
00532
00533 e->set_attr("is_complex", (int)is_complex);
00534 e->set_attr("datatype", (int)datatype);
00535 float * data = e->get_data();
00536
00537 size_t l = 0;
00538 for (int i = 0; i < nz; i++) {
00539 for (int j = 0; j < ny; j++) {
00540 for (int k = 0; k < nx; k++) {
00541 if (mode == 1) {
00542 data[l] = get_pixel_value_by_dist1(nx, ny, nz, k, j, i);
00543 }
00544 else if (mode == 2) {
00545 data[l] = get_pixel_value_by_dist2(nx, ny, nz, k, j, i);
00546 }
00547 l++;
00548 }
00549 }
00550 }
00551
00552 if (!is_complex) {
00553 e->write_image(filename, 0, image_type, false, 0, datatype, true);
00554 }
00555 else {
00556 e->update();
00557 e->set_attr("is_complex", false);
00558 EMData * fft = e->do_fft();
00559 fft->write_image(filename, 0, image_type, false, 0, datatype, true);
00560 if( fft )
00561 {
00562 delete fft;
00563 fft = 0;
00564 }
00565 }
00566
00567 if( e )
00568 {
00569 delete e;
00570 e = 0;
00571 }
00572 }
00573
00574 int TestUtil::verify_image_file_by_mode(const string & filename,
00575 EMUtil::ImageType, int mode,
00576 EMUtil::EMDataType datatype,
00577 int nx, int ny, int nz)
00578 {
00579 int err = 0;
00580
00581 EMData * e = new EMData();
00582 e->read_image(filename);
00583
00584 Dict attr_dict = e->get_attr_dict();
00585 bool is_complex = EMUtil::is_complex_type(datatype);
00586
00587 if (is_complex) {
00588 nx = (nx+2);
00589 }
00590
00591 if (nx != (int) attr_dict["nx"]) {
00592 LOGERR("nx: %d != %d\n", nx, (int) attr_dict["nx"]);
00593 return 1;
00594 }
00595
00596 if (ny != (int) attr_dict["ny"]) {
00597 LOGERR("ny: %d != %d\n", ny, (int) attr_dict["ny"]);
00598 return 1;
00599 }
00600
00601 if (nz != (int) attr_dict["nz"]) {
00602 LOGERR("nz: %d != %d\n", nz, (int) attr_dict["nz"]);
00603 return 1;
00604 }
00605
00606 if (datatype != (int) attr_dict["datatype"]) {
00607 LOGERR("datatype: %d != %d\n", datatype, (int) attr_dict["datatype"]);
00608 return 1;
00609 }
00610
00611
00612 if ((int)is_complex != (int) attr_dict["is_complex"]) {
00613 LOGERR("is_complex: %d != %d\n", is_complex, (int) attr_dict["is_complex"]);
00614 return 1;
00615 }
00616
00617
00618 if (!is_complex) {
00619 float * data = e->get_data();
00620 size_t l = 0;
00621 for (int i = 0; i < nz; i++) {
00622 for (int j = 0; j < ny; j++) {
00623 for (int k = 0; k < nx; k++) {
00624
00625 int d2 = 0;
00626 if (mode == 1) {
00627 d2 = (int)get_pixel_value_by_dist1(nx,ny,nz,k,j,i);
00628 }
00629 else if (mode == 2) {
00630 d2 = (int)get_pixel_value_by_dist2(nx,ny,nz,k,j,i);
00631 }
00632
00633 if ((int)data[l] != d2) {
00634 LOGERR("(%d,%d,%d): %d != %d\n", i,j,k,(int)data[l], d2);
00635 break;
00636 err = 1;
00637 }
00638 l++;
00639 }
00640 }
00641 }
00642 }
00643
00644 return err;
00645 }
00646
00647 EMObject TestUtil::emobject_to_py(bool b)
00648 {
00649 return EMObject(b);
00650 }
00651
00652 EMObject TestUtil::emobject_to_py(unsigned int un)
00653 {
00654 return EMObject(un);
00655 }
00656
00657 EMObject TestUtil::emobject_to_py(int n)
00658 {
00659 return EMObject(n);
00660 }
00661
00662 EMObject TestUtil::emobject_to_py(float f)
00663 {
00664 return EMObject(f);
00665 }
00666
00667 EMObject TestUtil::emobject_to_py(double f)
00668 {
00669 return EMObject(f);
00670 }
00671
00672 EMObject TestUtil::emobject_to_py(const string& str)
00673 {
00674 return EMObject(str);
00675 }
00676
00677
00678 EMObject TestUtil::emobject_to_py(EMData * emdata)
00679 {
00680 return EMObject(emdata);
00681 }
00682
00683
00684 EMObject TestUtil::emobject_to_py(XYData * xydata)
00685 {
00686 return EMObject(xydata);
00687 }
00688
00689
00690 EMObject TestUtil::emobject_farray_to_py()
00691 {
00692 vector<float> v(3);
00693 for (int i = 0; i < 3; i++) {
00694 v[i] = tf[i];
00695 }
00696 return EMObject(v);
00697 }
00698
00699
00700 EMObject TestUtil::emobject_strarray_to_py()
00701 {
00702 vector<string> v(3);
00703 for (int i = 0; i < 3; i++) {
00704 v[i] = get_debug_string(i);
00705 }
00706 return EMObject(v);
00707 }
00708
00709 EMObject TestUtil::emobject_to_py(Transform * t)
00710 {
00711 return EMObject(t);
00712 }
00713
00714 EMObject TestUtil::emobject_to_py(Ctf * ctf_)
00715 {
00716 return EMObject(ctf_);
00717 }
00718
00719
00720
00721