OpenWalnut  1.5.0dev
WReaderNIfTI_test.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WREADERNIFTI_TEST_H
26 #define WREADERNIFTI_TEST_H
27 
28 #include <memory>
29 #include <vector>
30 
31 #include <cxxtest/TestSuite.h>
32 
33 #include "../WReaderNIfTI.cpp" //need this to be able instatiate template function
34 #include "../WReaderNIfTI.h"
35 #include "core/common/WLogger.h"
36 #include "core/dataHandler/exceptions/WDHNoSuchFile.h"
37 
38 /**
39  * test class the nifti reader class
40  */
41 class WReaderNIfTITest : public CxxTest::TestSuite
42 {
43 public:
44  /**
45  * Per test initialization routines
46  */
47  void setUp( void )
48  {
50  }
51 
52  /**
53  * For each test the tidy up function.
54  */
55  void tearDown( void )
56  {
57  }
58 
59  /**
60  * Test instantiation with non existing file
61  */
63  {
64  TS_ASSERT_THROWS( WReaderNIfTI( "no such file" ), const WDHNoSuchFile &e );
65  }
66 
67  /**
68  * Test instantiation
69  */
70  void testInstantiation( void )
71  {
72  TS_ASSERT_THROWS_NOTHING( WReaderNIfTI( W_FIXTURE_PATH + "scalar_signed_short.nii.gz" ) );
73  TS_ASSERT_THROWS_NOTHING( WReaderNIfTI( W_FIXTURE_PATH + "scalar_unsigned_char.nii.gz" ) );
74  TS_ASSERT_THROWS_NOTHING( WReaderNIfTI( W_FIXTURE_PATH + "scalar_float.nii.gz" ) );
75  TS_ASSERT_THROWS_NOTHING( WReaderNIfTI( W_FIXTURE_PATH + "vector_float.nii.gz" ) );
76  TS_ASSERT_THROWS_NOTHING( WReaderNIfTI( W_FIXTURE_PATH + "symmetric_2nd_order_tensor_float.nii.gz" ) );
77  TS_ASSERT_THROWS_NOTHING( WReaderNIfTI( W_FIXTURE_PATH + "vector_unsigned_char.nii.gz" ) );
78  }
79 
80  /**
81  * Test if the loaded files are really loaded
82  */
83  void testLoading1( void )
84  {
85  WReaderNIfTI reader1( W_FIXTURE_PATH + "scalar_signed_short.nii.gz" );
86  TS_ASSERT( reader1.load() );
87  }
88 
89  /**
90  * Test if the loaded files are really loaded
91  */
92  void testLoading2( void )
93  {
94  WReaderNIfTI reader2( W_FIXTURE_PATH + "scalar_unsigned_char.nii.gz" );
95  TS_ASSERT( reader2.load() );
96  }
97 
98  /**
99  * Test if the loaded files are really loaded
100  */
101  void testLoading3( void )
102  {
103  WReaderNIfTI reader3( W_FIXTURE_PATH + "scalar_float.nii.gz" );
104  TS_ASSERT( reader3.load() );
105  }
106 
107  /**
108  * Test if the loaded files are really loaded
109  */
110  void testLoading4( void )
111  {
112  WReaderNIfTI reader4( W_FIXTURE_PATH + "vector_float.nii.gz" );
113  TS_ASSERT( reader4.load() );
114  }
115 
116  /**
117  * Test if the loaded files are really loaded
118  */
119  void testLoading5( void )
120  {
121  WReaderNIfTI reader5( W_FIXTURE_PATH + "symmetric_2nd_order_tensor_float.nii.gz" );
122  TS_ASSERT( reader5.load() );
123  }
124 
125  /**
126  * Test if the loaded files are really loaded
127  */
128  void testLoading6( void )
129  {
130  WReaderNIfTI reader6( W_FIXTURE_PATH + "vector_unsigned_char.nii.gz" );
131  TS_ASSERT( reader6.load() );
132  }
133 
134  /**
135  * Test conversion of nifti matrix to OW matrix.
136  */
137  void testMatrixConversion( void )
138  {
139  mat44 dummy;
140  dummy.m[0][0] = 1.1;
141  dummy.m[0][1] = 1.2;
142  dummy.m[0][2] = 1.3;
143  dummy.m[0][3] = 1.4;
144  dummy.m[1][0] = 1.5;
145  dummy.m[1][1] = 1.6;
146  dummy.m[1][2] = 1.7;
147  dummy.m[1][3] = 1.8;
148  dummy.m[2][0] = 1.9;
149  dummy.m[2][1] = 1.11;
150  dummy.m[2][2] = 1.12;
151  dummy.m[2][3] = 1.13;
152  dummy.m[3][0] = 1.14;
153  dummy.m[3][1] = 1.15;
154  dummy.m[3][2] = 1.16;
155  dummy.m[3][3] = 1.17;
156 
157  // need this for calling the function
158  WReaderNIfTI reader1( W_FIXTURE_PATH + "scalar_signed_short.nii.gz" );
159 
160  WMatrix< double > result = reader1.convertMatrix( dummy );
161 
162  TS_ASSERT_EQUALS( result.getNbRows(), 4 );
163  TS_ASSERT_EQUALS( result.getNbCols(), 4 );
164 
165  double delta = 1e-7;
166  TS_ASSERT_DELTA( result( 0, 0 ), 1.1, delta );
167  TS_ASSERT_DELTA( result( 0, 1 ), 1.2, delta );
168  TS_ASSERT_DELTA( result( 0, 2 ), 1.3, delta );
169  TS_ASSERT_DELTA( result( 0, 3 ), 1.4, delta );
170  TS_ASSERT_DELTA( result( 1, 0 ), 1.5, delta );
171  TS_ASSERT_DELTA( result( 1, 1 ), 1.6, delta );
172  TS_ASSERT_DELTA( result( 1, 2 ), 1.7, delta );
173  TS_ASSERT_DELTA( result( 1, 3 ), 1.8, delta );
174  TS_ASSERT_DELTA( result( 2, 0 ), 1.9, delta );
175  TS_ASSERT_DELTA( result( 2, 1 ), 1.11, delta );
176  TS_ASSERT_DELTA( result( 2, 2 ), 1.12, delta );
177  TS_ASSERT_DELTA( result( 2, 3 ), 1.13, delta );
178  TS_ASSERT_DELTA( result( 3, 0 ), 1.14, delta );
179  TS_ASSERT_DELTA( result( 3, 1 ), 1.15, delta );
180  TS_ASSERT_DELTA( result( 3, 2 ), 1.16, delta );
181  TS_ASSERT_DELTA( result( 3, 3 ), 1.17, delta );
182  }
183 
184  /**
185  * Test the function copying an array into a vector
186  */
187  void testCopyArray( void )
188  {
189  // need this for calling the function
190  WReaderNIfTI reader1( W_FIXTURE_PATH + "scalar_signed_short.nii.gz" );
191 
192  const size_t nbVoxels = 10;
193  const size_t vDim = 3;
194  double* dataArray = new double[nbVoxels * vDim];
195 
196  for( unsigned int voxId = 0; voxId < nbVoxels; ++voxId )
197  {
198  for( unsigned int dim = 0; dim < vDim; ++dim )
199  {
200  unsigned int i = ( voxId * vDim + dim );
201  dataArray[i] = 1.1 * i;
202  }
203  }
204  std::shared_ptr< std::vector< double > > vec = reader1.copyArray( dataArray, nbVoxels, vDim );
205 
206  TS_ASSERT_EQUALS( vec->size(), nbVoxels * vDim );
207 
208  double delta = 1e-12;
209  for( unsigned int voxId = 0; voxId < nbVoxels; ++voxId )
210  {
211  for( unsigned int dim = 0; dim < vDim; ++dim )
212  {
213  // The following two test exactly the same thing.
214  TS_ASSERT_DELTA( vec->at( voxId * vDim + dim ), dataArray[voxId + nbVoxels * dim], delta );
215  TS_ASSERT_DELTA( vec->at( voxId * vDim + dim ), 1.1 * ( voxId + nbVoxels * dim ), delta );
216  }
217  }
218  delete[] dataArray;
219  }
220 };
221 
222 #endif // WREADERNIFTI_TEST_H
File not found exception.
Definition: WDHNoSuchFile.h:38
static void startup(std::ostream &output=std::cout, LogLevel level=LL_DEBUG)
Create the first and only instance of the logger as it is a singleton.
Definition: WLogger.cpp:41
size_t getNbRows() const
Get number of rows.
Definition: WMatrix.h:375
size_t getNbCols() const
Get number of columns.
Definition: WMatrix.h:383
test class the nifti reader class
void tearDown(void)
For each test the tidy up function.
void testInstantiation(void)
Test instantiation.
void testMatrixConversion(void)
Test conversion of nifti matrix to OW matrix.
void testLoading4(void)
Test if the loaded files are really loaded.
void setUp(void)
Per test initialization routines.
void testLoading6(void)
Test if the loaded files are really loaded.
void testLoading1(void)
Test if the loaded files are really loaded.
void testCopyArray(void)
Test the function copying an array into a vector.
void testLoading2(void)
Test if the loaded files are really loaded.
void testLoading5(void)
Test if the loaded files are really loaded.
void testInstantiationNonExisting(void)
Test instantiation with non existing file.
void testLoading3(void)
Test if the loaded files are really loaded.
Reader for the NIfTI file format.
Definition: WReaderNIfTI.h:43
WMatrix< double > convertMatrix(const mat44 &in)
This function converts a 4x4 matrix from the NIfTI libs into the format used by OpenWalnut.
std::shared_ptr< std::vector< T > > copyArray(const T *dataArray, const size_t countVoxels, const size_t vDim)
This function allows one to copy the data given as a T* by niftilibio into a std::vector< T >
virtual std::shared_ptr< WDataSet > load(DataSetType dataSetType=W_DATASET_NONE)
Loads the dataset.