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