changeset 2656:946695de0dca

EZ minc test implimentation
author Vladimir S. FONOV <vladimir.fonov@gmail.com>
date Fri, 23 Mar 2012 16:41:29 -0400
parents 584b0bdfa619
children 5857c4de2150
files ezminc/CMakeLists.txt ezminc/minc_1_rw.cpp ezminc/tests/CMakeLists.txt ezminc/tests/minc_com.cpp ezminc/tests/minc_com_4d.cpp ezminc/tests/minc_rw_test.cpp ezminc/tests/transformpoint.cpp minc4itk/itkMincImageIO.cxx
diffstat 8 files changed, 188 insertions(+), 246 deletions(-) [+]
line wrap: on
line diff
--- a/ezminc/CMakeLists.txt
+++ b/ezminc/CMakeLists.txt
@@ -1,8 +1,8 @@
 OPTION(MINC2_BUILD_EZMINC_EXAMPLES   "Build EZminc examples" ON)
 
-IF(BUILD_MINC2)
+IF(MINC2_BUILD_V2)
   ADD_DEFINITIONS( -DMINC2 )
-ENDIF(BUILD_MINC2)
+ENDIF(MINC2_BUILD_V2)
 
 
 SET( MINC_IO_HEADERS 
--- a/ezminc/minc_1_rw.cpp
+++ b/ezminc/minc_1_rw.cpp
@@ -1327,6 +1327,7 @@
       mivarput1(_mincid, _icmin, 0, NC_DOUBLE, NULL, &_image_range[0]);
       mivarput1(_mincid, _icmax, 0, NC_DOUBLE, NULL, &_image_range[1]);
       miset_valid_range(_mincid, _imgid, _image_range);
+      _set_image_range=false;
     }
     minc_1_base::close();
   }
--- a/ezminc/tests/CMakeLists.txt
+++ b/ezminc/tests/CMakeLists.txt
@@ -1,1 +1,5 @@
 LINK_LIBRARIES(minc_io ${MINC2_LIBRARIES})
+
+ADD_EXECUTABLE(ezminc_rw_test ezminc_rw_test.cpp)
+
+ADD_TEST(ezminc_rw_test ezminc_rw_test ${CMAKE_CURRENT_BINARY_DIR})
\ No newline at end of file
deleted file mode 100644
--- a/ezminc/tests/minc_com.cpp
+++ /dev/null
@@ -1,48 +0,0 @@
-#include "minc_1_rw.h"
-#include <iostream>
-#include "minc_1_simple.h"
-#include "minc_1_simple_rw.h"
-
-using namespace minc;
-
-int main(int argc,char **argv)
-{
-  try
-  {
-    if(argc<2) {
-      std::cerr<<"Usage: "<<argv[0]<<" <input.mnc> "<<std::endl;
-      return 1;
-    }
-    minc_1_reader rdr;
-    rdr.open(argv[1],true);
-    simple_volume<float> vol;
-    load_simple_volume<float>(rdr,vol);
-    //calculate COM
-    fixed_vec<3,int> i;
-    fixed_vec<3,double> w;
-    double total=0;
-    double tx=0.0,ty=0.0,tz=0.0;
-    
-    for(i[2]=0;i[2]<vol.dim(2);i[2]++)
-      for(i[1]=0;i[1]<vol.dim(1);i[1]++)
-        for(i[0]=0;i[0]<vol.dim(0);i[0]++)
-        {
-          w=vol.voxel_to_world(i);
-          tx+=w[0]*vol.get(i);
-          ty+=w[1]*vol.get(i);
-          tz+=w[2]*vol.get(i);
-          total+=vol.get(i);
-        }
-    tx/=total;
-    ty/=total;
-    tz/=total;
-    std::cout<<tx<<","<<ty<<","<<tz<<std::endl;
-  } catch (const minc::generic_error & err) {
-    std::cerr << "Got an error at:" << err.file () << ":" << err.line () << std::endl;
-    std::cerr << err.msg()<<std::endl;
-    return 1;
-  }
-  
-  return 0;
-}
-
deleted file mode 100644
--- a/ezminc/tests/minc_com_4d.cpp
+++ /dev/null
@@ -1,70 +0,0 @@
-#include "minc_1_rw.h"
-#include <iostream>
-#include "minc_1_simple_rw.h"
-
-using namespace minc;
-
-int main(int argc,char **argv)
-{
-  try
-  {
-    if(argc<2) {
-      std::cerr<<"Usage: "<<argv[0]<<" <input.mnc> "<<std::endl;
-      return 1;
-    }
-    minc_1_reader rdr;
-    rdr.open(argv[1],true);
-    simple_4d_volume<float> vol;
-    load_4d_volume<float>(rdr,vol);
-    //calculate COM
-    fixed_vec<3,int> i;
-    fixed_vec<3,double> w;
-    
-    double total=0.0;
-    double tx=0.0,ty=0.0,tz=0.0,tT=0.0;
-    
-    //std::cout<<"T-start:"<<vol.t_start()<<std::endl;
-    //std::cout<<"T-step:"<<vol.t_step()<<std::endl;
-    
-    for(int t=0;t<vol.frames();t++)
-    {
-      double rt=t*vol.t_step()+vol.t_start();
-      
-      for(i[2]=0;i[2]<vol.dim(2);i[2]++)
-        for(i[1]=0;i[1]<vol.dim(1);i[1]++)
-          for(i[0]=0;i[0]<vol.dim(0);i[0]++)
-      {
-        w=vol.voxel_to_world(i);
-        
-        double v=vol.get(i,t);
-        
-        tx+=w[0]*v;
-        ty+=w[1]*v;
-        tz+=w[2]*v;
-        tT+=rt  *v;
-        
-        total+=v;
-        //std::cout<<v<<"\t";
-      }
-      //std::cout<<std::endl;
-      //std::cout<<rt<<"\t"<<tT<<"\t"<<std::flush;
-      std::cout<<"."<<std::flush;
-    }
-    std::cout<<std::endl;
-    
-    tx/=total;
-    ty/=total;
-    tz/=total;
-    tT/=total;
-    
-    std::cout<<tx<<","<<ty<<","<<tz<<","<<tT<<std::endl;
-    
-  } catch (const minc::generic_error & err) {
-    std::cerr << "Got an error at:" << err.file () << ":" << err.line () << std::endl;
-    std::cerr << err.msg()<<std::endl;
-    return 1;
-  }
-  
-  return 0;
-}
-
--- a/ezminc/tests/minc_rw_test.cpp
+++ b/ezminc/tests/minc_rw_test.cpp
@@ -1,123 +1,207 @@
+#include <iostream>
+#include <unistd.h>
+#include <stdlib.h>
+#include <vector>
+#include <math.h>
+
 #include "minc_1_rw.h"
-#include <iostream>
 #include "minc_1_simple.h"
 
 using namespace minc;
 
-template<class T>class volume_3d
+
+
+template<class TPixel> void make_rw_test(const char * filename,int slice_dim,nc_type datatype,bool is_signed,double max_diff=0.0)
 {
-  protected:
-    T* _data;
-    int _x,_y,_z;
-  public:
+
+  std::string history="History ";
+  
+  // generate info
+  minc_info info(3);
+  int volume=1.0;
+  
+  for(int i=0;i<3;i++)
+  {
+    info[i].dim=dim_info::dimensions( dim_info::DIM_X+i);
+    
+    info[i].length=10+i;
+    info[i].step  =i+0.1;
+    info[i].start =i-5.0;
+    
+    info[i].have_dir_cos=true;
+    
+    for(int j=0;j<3;j++)
+      info[i].dir_cos[j]=(i==j?1.0:0.0);    
     
-    volume_3d(T* data,int x,int y,int z):_data(data),_x(x),_y(y),_z(z)
-    {
-    }
+    volume*=info[i].length;
+  }
+  
+  //fill the buffer
+  std::vector<TPixel> buffer(volume);
+  
+  if(typeid(TPixel)==typeid(float) || typeid(TPixel)==typeid(double))
+    for(int i=0;i<volume;i++)
+      buffer[i]=static_cast<TPixel>(random())*100.0/RAND_MAX;
+  else
+    for(int i=0;i<volume;i++)
+      buffer[i]=static_cast<TPixel>(random());
+  
+    
+  std::vector<double> double_attr(10);
+  std::vector<int> int_attr(10);
+  std::vector<short> short_attr(10);
+  std::string  string_attr="Test string attribuite";
+  
+  for(int i=0;i<10;i++)
+  {
+    double_attr[i]=i+0.1;
+    int_attr[i]=i*100;
+    short_attr[i]=i;
+  }
     
-    volume_3d(std::vector<T> data,int x,int y,int z):_data(&data[0]),_x(x),_y(y),_z(z)
-    {
-    }
+  //now let's write volume
+  minc_1_writer wrt;
+  wrt.open(filename,info,slice_dim,datatype,is_signed);
+  
+  //atributes
+  wrt.append_history(history.c_str());
+  wrt.insert("patient","double_attr",double_attr);
+  wrt.insert("patient","int_attr",int_attr);
+  wrt.insert("patient","short_attr",short_attr);
+  wrt.insert("patient","string_attr",string_attr.c_str());
+  
+  if(typeid(TPixel)==typeid(unsigned char))
+    wrt.setup_write_byte();
+  else if(typeid(TPixel)==typeid(int))
+    wrt.setup_write_int();
+  else if(typeid(TPixel)==typeid(float))
+    wrt.setup_write_float(); 
+  else if(typeid(TPixel)==typeid(double))
+    wrt.setup_write_double(); 
+  else 
+    REPORT_ERROR("Data type not supported for minc io"); 
+  
+  
+  
+  //write volume
+  save_standard_volume(wrt,&buffer[0]);
+  
+  wrt.close();
+
+  //reading volume
+  minc_1_reader rdr;
+  rdr.open(filename);
+  
+  if(history!=rdr.history())
+      REPORT_ERROR("Mismatched history");
+ 
+  std::vector<double> double_attr_rd=rdr.att_value_double("patient","double_attr");
+  std::vector<int>  int_attr_rd=rdr.att_value_int("patient","int_attr");
+  std::vector<short> short_attr_rd=rdr.att_value_short("patient","short_attr");
+  std::string string_attr_rd=rdr.att_value_string("patient","string_attr");
+  
+  if(string_attr_rd!=string_attr)
+    REPORT_ERROR("Mismatched string attribute");
+  
+  for(int i=0;i<10;i++)
+  {
+    if(double_attr_rd[i]!=double_attr[i])
+      REPORT_ERROR("Mismatched double attribute");
+    if(int_attr_rd[i]!=int_attr[i])
+      REPORT_ERROR("Mismatched int attribute");
+    if(short_attr_rd[i]!=short_attr[i])
+      REPORT_ERROR("Mismatched short attribute");
+  }
+  // let's compare info
+  for(int i=0;i<3;i++)
+  {
+    if(rdr.info()[i].dim!=info[i].dim) 
+      REPORT_ERROR("Mismatched dimension");
     
-    T& operator()(int x,int y,int z)
-    {
-      return _data[x+y*_x+z*_x*_y];
-    }
+    if(rdr.info()[i].length!=info[i].length) 
+      REPORT_ERROR("Mismatched dimension length");
+    
+    if(rdr.info()[i].step!=info[i].step) 
+      REPORT_ERROR("Mismatched step");
+    
+    if(rdr.info()[i].start!=info[i].start) 
+      REPORT_ERROR("Mismatched step");
+    
+    if(rdr.info()[i].have_dir_cos!=info[i].have_dir_cos) 
+      REPORT_ERROR("Mismatched have direction cosines");
+    
+    for(int j=0;j<3;j++)
+      if(rdr.info()[i].dir_cos[j]!=info[i].dir_cos[j])
+        REPORT_ERROR("Mismatched direction cosines");
+  }
   
-};
+  if(typeid(TPixel)==typeid(unsigned char))
+    rdr.setup_read_byte();
+  else if(typeid(TPixel)==typeid(int))
+    rdr.setup_read_int();
+  else if(typeid(TPixel)==typeid(float))
+    rdr.setup_read_float(); 
+  else if(typeid(TPixel)==typeid(double))
+    rdr.setup_read_double();
+  else 
+    REPORT_ERROR("Data type not supported for minc io");
+  
+  
+  std::vector<TPixel> in_buffer(volume);
+  load_standard_volume(rdr,&in_buffer[0]);
+  rdr.close();
+  
+  if(max_diff==0.0)
+  {
+    for(int i=0;i<volume;i++)
+      if(buffer[i]!=in_buffer[i])
+        REPORT_ERROR("Data mismatched!");
+  } else { //ALLOW rounding error
+    for(int i=0;i<volume;i++)
+      if(fabs(buffer[i]-in_buffer[i])>max_diff)
+      {
+        std::cerr<<"Expected:"<<buffer[i]<<" got:"<<in_buffer[i]<<" @ "<<i<<std::endl;
+        REPORT_ERROR("Data mismatched too much!");
+      }
+  }
+}
+
 
 int main(int argc,char **argv)
 {
   try
   {
-    if(argc<4) {
-      std::cerr<<"Usage: "<<argv[0]<<" <input.mnc> <output.mnc> <output2.mnc>"<<std::endl;
-      return 1;
-    }
-    minc_1_reader rdr;
-    rdr.open(argv[1]);
-    int i;
-    for(i=0;i<rdr.dim_no();i++)
-      std::cout<<rdr.dim(i).name<<" "<<rdr.dim(i).length<<std::endl;
-    std::cout<<"Slice len="<<rdr.slice_len()<<std::endl;
-    std::cout<<"History:"<<rdr.history().c_str()<<std::endl;
-    for(int v=0;v<rdr.var_number();v++)
+    if(argc>1) 
     {
-      std::string var=rdr.var_name(v);
-      for(int a=0;a<rdr.att_number(v);a++)
-      {
-        std::string aname=rdr.att_name(v,a);
-        nc_type dt=rdr.att_type(v,aname.c_str());
-        
-        std::cout<<var.c_str()<<":"<<aname.c_str()<<" ";
-        if(dt==NC_CHAR) 
-          std::cout<<rdr.att_value_string(v,aname.c_str());
-        else if(dt=NC_DOUBLE) 
-        {
-          std::vector<double> val=rdr.att_value_double(v,aname.c_str());
-          for(int d=0;d<val.size();d++)
-            std::cout<<val[d]<<"\t";
-        } else {
-          std::cout<<"???";
-        }
-        std::cout<<std::endl;
-      }
+      //chdir(argv[1]);
     }
     
-    rdr.setup_read_float();
+    //no rounding expected
+    make_rw_test<unsigned char>("EZminc_byte_2.mnc",2,NC_BYTE,false);
+    make_rw_test<unsigned char>("EZminc_byte_3.mnc",3,NC_BYTE,false);
     
-    minc_1_writer wrt;
-    wrt.open(argv[2],rdr.info(),2,NC_FLOAT,true);
-    wrt.setup_write_float();
-    std::vector<float> v(rdr.slice_len());
-
-    int c=0;
-    double s=0;
-    for(rdr.begin(),wrt.begin();!rdr.last();rdr.next_slice(),wrt.next_slice())
-    {
-      //std::cout<<wrt.current_slice()[0]<<std::endl;
-      rdr.read(&v[0]);
-      for(int i=0;i<rdr.slice_len();i++)
-      {
-        s+=v[i];
-        c++;
-      }
-      wrt.write(&v[0]);
-    }
-    s/=c;
-    //std::cout<<wrt._image_range[0]<<" "<<wrt._image_range[1]<<std::endl;
-    std::cout<<s<<std::endl;
-    wrt.copy_headers(rdr);
-    wrt.append_history("minc_rw_test test1\n");
+    make_rw_test<int>("EZminc_int_2.mnc",2,NC_INT,true);
+    make_rw_test<int>("EZminc_int_3.mnc",3,NC_INT,true);
+    
+    make_rw_test<float>("EZminc_float_2.mnc",2,NC_FLOAT,true);
+    make_rw_test<float>("EZminc_float_3.mnc",3,NC_FLOAT,true);
+    
+    make_rw_test<double>("EZminc_double_2.mnc",2,NC_DOUBLE,true);
+    make_rw_test<double>("EZminc_double_3.mnc",3,NC_DOUBLE,true);
     
-    //second test
-    unsigned long size=1;
-    for(i=0;i<rdr.dim_no();i++)
-      size*=rdr.dim(i).length;
+    // some rounding expected
+    make_rw_test<float>("EZminc_float_2_short.mnc",2,NC_SHORT,true,0.1);
+    make_rw_test<float>("EZminc_float_3_short.mnc",3,NC_SHORT,true,0.1);
     
-    minc_1_writer wrt2;
-    wrt2.open(argv[3],rdr.info(),3,NC_SHORT,false);
-    wrt2.setup_write_float();
+    make_rw_test<double>("EZminc_double_2_short.mnc",2,NC_SHORT,true,0.1);
+    make_rw_test<double>("EZminc_double_3_short.mnc",3,NC_SHORT,true,0.1);
     
-    std::vector<float> buffer(size);
-    load_standard_volume<float>(rdr,&buffer[0]);
-    double avg2=0;
-    //for(int i=0;i<size;i++)
-    //  avg2+=buffer[i];
-    volume_3d<float> volume(buffer,rdr.ndim(dim_info::DIM_X),rdr.ndim(dim_info::DIM_Y),rdr.ndim(dim_info::DIM_Z));
-    for(int z=0;z<rdr.ndim(dim_info::DIM_Z);z++)
-      for(int y=0;y<rdr.ndim(dim_info::DIM_Y);y++)
-        for(int x=0;x<rdr.ndim(dim_info::DIM_X);x++)
-        {
-          avg2+=volume(x,y,z);
-        }
+    make_rw_test<float>("EZminc_float_2_byte.mnc",2,NC_BYTE,false,0.5);
+    make_rw_test<float>("EZminc_float_3_byte.mnc",3,NC_BYTE,false,0.5);
     
-    avg2/=size;
-    std::cout<<"avg2 "<<avg2<<std::endl;
+    make_rw_test<double>("EZminc_double_2_byte.mnc",2,NC_BYTE,false,0.5);
+    make_rw_test<double>("EZminc_double_3_byte.mnc",3,NC_BYTE,false,0.5);
     
-    save_standard_volume<float>(wrt2,&buffer[0]);
-    wrt2.copy_headers(rdr);
-    wrt2.append_history("minc_rw_test test2\n");
   } catch (const minc::generic_error & err) {
     std::cerr << "Got an error at:" << err.file () << ":" << err.line () << std::endl;
     std::cerr << err.msg()<<std::endl;
deleted file mode 100644
--- a/ezminc/tests/transformpoint.cpp
+++ /dev/null
@@ -1,29 +0,0 @@
-#include <volume_io.h>
-#include <iostream>
-
-
-int main(int argc, char **argv)
-{
-  if(argc<5)
-  {
-    std::cerr<<"Usage:"<<argv[0]<<"<XFM file> X Y Z"<<std::endl;
-    return 1;
-  }
-  double x,y,z;
-  double _x,_y,_z;
-  
-  x=atof(argv[2]);
-  y=atof(argv[3]);
-  z=atof(argv[4]);
-  
-  General_transform _xfm;
-  if(input_transform_file((char*)argv[1], &_xfm)!=OK)
-  {
-    std::cerr<<"Error reading:"<<argv[1]<<std::endl;
-    return 1;
-  }
-  general_transform_point( &_xfm, x, y, z,&_x, &_y, &_z);  
-  delete_general_transform(&_xfm);
-  std::cout<<_x<<" "<<_y<<" "<<_z<<std::endl;
-  return 0;
-};
\ No newline at end of file
--- a/minc4itk/itkMincImageIO.cxx
+++ b/minc4itk/itkMincImageIO.cxx
@@ -179,7 +179,7 @@
         SetNumberOfComponents(_rdr->ndim(0)+_rdr->ndim(4));
       }
       
-      for(int i=1,c=0; i < 4; i++)
+      for(int i=1,c=0; i < 5; i++)
       {
         if(_rdr->ndim(i)<=0) continue;
         SetDimensions(c,_rdr->ndim(i));