Tomato
KWUtil.h
1 //
2 // KWUtil.h
3 // TomatoLib
4 //
5 // Created by Konrad Werys on 12/8/16.
6 // Copyright © 2016 Konrad Werys. All rights reserved.
7 //
8 
9 #ifndef KWUtil_h
10 #define KWUtil_h
11 
12 //#ifndef EXIT_SUCCESS
13 //#define EXIT_SUCCESS 0
14 //#endif
15 //
16 //#ifndef EXIT_FAILURE
17 //#define EXIT_FAILURE 1
18 //#endif
19 
20 #include <iostream>
21 #include <iomanip>
22 #include <cstdarg>
23 #include <cmath>
24 #include <vector>
25 #include <string>
26 #include <sstream>
27 #include <stdexcept>
28 #include <sys/stat.h>
29 #include <fstream>
30 #include <limits>
31 
32 // MS stuff problems
33 // https://stackoverflow.com/questions/26065359/m-pi-flagged-as-undeclared-identifier
34 #ifndef M_PI
35 #define M_PI 3.14159265358979323846
36 #endif
37 
38 
39 class KWUtil{
40 public:
41 
42  template< typename TYPE >
43  static void printKW(bool doPrint, char* fmt, ...);
44 
45  /***********************/
46  /* copyArrayToArray */
47  /***********************/
48  template< typename TYPE1, typename TYPE2 >
49  static void copyArrayToArray(int nSamples, TYPE1 *arrayTo, const TYPE2 *arrayFrom);
50 
51  /***********************/
52  /* printVector */
53  /***********************/
54 
60  template<typename TYPE>
61  static void printVector(const std::string& name, const std::vector<TYPE> vector);
62 
69  template<typename TYPE>
70  static void printVectorNewline(const std::string &name, const std::vector<TYPE> vector);
71 
72  /***********************/
73  /* printArray 1D */
74  /***********************/
75  template< typename TYPE >
76  static void printArray(int nSamples, const TYPE *myarray);
77 
78  template< typename TYPE >
79  static void printArray(int nSamples, const TYPE *myarray, int width);
80 
81  template< typename TYPE >
82  static void printArray(int nSamples, const TYPE *myarray, char* text);
83 
84  template< typename TYPE >
85  static void printArray(bool doPrint, int nSamples, const TYPE *myarray);
86 
87  template< typename TYPE >
88  static void printArray(bool doPrint, int nSamples, const TYPE *myarray, char* text);
89 
90  /***********************/
91  /* printArray 2D */
92  /***********************/
93  template< typename TYPE >
94  static void printArray2D(int nRows, int nCols, TYPE **myarray);
95 
96  template< typename TYPE >
97  static void printArray2D(int nRows, int nCols, TYPE **myarray, char* text);
98 
99  template< typename TYPE >
100  static void printArray2D(bool doPrint, int nRows, int nCols, TYPE **myarray);
101 
102  template< typename TYPE >
103  static void printArray2D(bool doPrint, int nRows, int nCols, TYPE **myarray, char* text);
104 
105  /***********************/
106  /* printStdVector 1D */
107  /***********************/
108 
109  template< typename TYPE >
110  static void printStdVector(const std::vector<TYPE> myvector);
111 
112  template< typename TYPE >
113  static void printStdVector(const std::vector<TYPE> myvector, char* text);
114 
115  template< typename TYPE >
116  static void printStdVector(bool doPrint, const std::vector<TYPE> myvector);
117 
118  template< typename TYPE >
119  static void printStdVector(bool doPrint, const std::vector<TYPE> myvector, char* text);
120 
121 
122  /***********************/
123  /* calculations */
124  /***********************/
125 
126  template< typename TYPE >
127  static void swap(TYPE &a, TYPE &b);
128 
129  template< typename TYPE >
130  static TYPE max(TYPE a, TYPE b);
131 
132  template< typename TYPE >
133  static TYPE min(TYPE a, TYPE b);
134 
135  template< typename TYPE >
136  static TYPE calcSumArray(int nSamples, const TYPE *myarray);
137 
138  template< typename TYPE >
139  static double calcMeanArray(int nSamples, const TYPE *myarray);
140 
141  template< typename TYPE >
142  static double calcMedianArray(int nSamples, const TYPE *myarray);
143 
144  template< typename TYPE >
145  static double calcStandardDeviationArray(int nSamples, const TYPE *myarray);
146 
147  template< typename TYPE >
148  static double calcR2ss (int nSamples, const TYPE *fitted, const TYPE *ysignal);
149 
150  template< typename TYPE >
151  static double calcR2cor(int nSamples, const TYPE *fitted, const TYPE *ysignal);
152 
153  template< typename TYPE >
154  static int linearFit(
155  int nSamples,
156  const TYPE *datax,
157  const TYPE *datay,
158  TYPE &a,
159  TYPE &b,
160  TYPE &siga,
161  TYPE &sigb,
162  TYPE &R2,
163  TYPE &chi2);
164 
165  template< typename TYPE >
166  static double SKPLinReg(const TYPE *datax, const TYPE *datay, int nSamples, TYPE &rslope, TYPE &roffset);
167 
168  template< typename TYPE >
169  static void SKPsort(int nSamples, const TYPE *myarray, int *index);
170 
171  template< typename TYPE >
172  static void quickSort(int nSamples, TYPE *myarray);
173 
174  template< typename TYPE >
175  static void quickSortIndex(int nSamples, TYPE *myarray, int *indexArray);
176 
177  template< typename TYPE >
178  static TYPE getChiSqrt(TYPE lastFValue, int nSamples);
179 
180  template< typename TYPE >
181  static TYPE calcFeFromT2(TYPE T2);
182 
183  template< typename TYPE >
184  static TYPE calcFeErrorFromT2(TYPE T2, TYPE deltaT2);
185 
186  template< typename TYPE >
187  static int calculateFitError(int nSamples, int nDims, const TYPE* jacobian, TYPE mFuncNorm, TYPE* fitError);
188 
197  template< typename TYPE >
198  static int calcMatrixInverse2x2(const TYPE *matrix, TYPE *matrixInverse);
199 
208  template< typename TYPE >
209  static int calcMatrixInverse3x3(const TYPE *matrix, TYPE *matrixInverse);
210 
211  template< typename TYPE >
212  static TYPE MOLLI_min(TYPE a[], int n, int *indm);
213 
214  template< typename TYPE >
215  static TYPE MOLLI_max(TYPE a[], int n, int *indm);
216 
217  /***********************/
218  /* string operations */
219  /***********************/
220  // http://www.cplusplus.com/articles/D9j2Nwbp/
221  template < typename TYPE >
222  static std::string NumberToString ( TYPE Number ){
223  std::ostringstream ss;
224  ss << Number;
225  return ss.str();
226  };
227 
228  //http://www.cplusplus.com/articles/D9j2Nwbp/
229  template < typename TYPE >
230  static TYPE StringToNumber ( const std::string &Text ){
231  std::istringstream ss(Text);
232  TYPE result;
233  return ss >> result ? result : 0;
234  };
235 
236  // for example to turn acquisition time to seconds
237  template < typename TYPE >
238  static TYPE dicomTime2Seconds(std::string dicomTimeString);
239 
240  /***********************/
241  /* threading */
242  /***********************/
243  template < typename TYPE >
244  static std::vector<int> bounds(int parts, int mem);
245 
246  /***********************/
247  /* files */
248  /***********************/
249 
250  static std::string PathSeparator(){
251 #if defined(WIN32) || defined(_WIN32) || defined __CYGWIN__
252  return std::string("\\");
253 #else
254  return std::string("/");
255 #endif
256  }
257 
258 // // http://www.cplusplus.com/reference/string/string/find_last_of/
259 // static void splitFilename (const std::string& str, std::string &path, std::string &file) {
260 // //std::cout << "Splitting: " << str << '\n';
261 // std::size_t found = str.find_last_of("/\\");
262 // path = str.substr(0,found);
263 // file = str.substr(found+1);
264 // }
265 //
266 // // http://www.cplusplus.com/doc/tutorial/files/
267 // static std::vector <std::string> readFile(const std::string filePath){
268 // std::vector <std::string> fileContent;
269 // std::string line;
270 //
271 // try {
272 // std::ifstream myfile(filePath.c_str());
273 // if (myfile.is_open()) {
274 // while (getline(myfile, line)) {
275 // fileContent.push_back(line);
276 // }
277 // myfile.close();
278 // } else {
279 // printf("Unable to open file: %s", filePath.c_str() );
280 // }
281 // } catch (const std::exception& e) {
282 // std::cout << e.what() << std::endl;
283 // printf("Unable to read the file: %s", filePath.c_str() );
284 // }
285 // return fileContent;
286 // }
287 
288  /***********************/
289  /* testing */
290  /***********************/
291  template< typename TYPE >
292  static bool array_expect_near(
293  int nElements,
294  const TYPE *array1,
295  const TYPE *array2,
296  TYPE abs_error = 0,
297  const std::string& comment = "");
298 
299  //static bool isFile(const char* path) {
300  // struct stat buf;
301  // stat(path, &buf);
302  // return S_ISREG(buf.st_mode);
303  //}
304 
305  //static bool isDir(const char* path) {
306  // struct stat buf;
307  // stat(path, &buf);
308  // return S_ISDIR(buf.st_mode);
309  //}
310 
311 private:
312  // can this implementation of quicksort be improved? http://codereview.stackexchange.com/questions/77782/quick-sort-implementation
313  template< typename TYPE >
314  static void quickSort(TYPE arr[], int left, int right);
315 
316  template< typename TYPE >
317  static void quickSortIndex(TYPE arr[], int indexArray[], int left, int right);
318 
319 };
320 
321 #include "KWUtil.hxx"
322 
323 #endif /* KWUtil_h */
static void printVectorNewline(const std::string &name, const std::vector< TYPE > vector)
Definition: KWUtil.hxx:44
Definition: KWUtil.h:39
static void printVector(const std::string &name, const std::vector< TYPE > vector)
Definition: KWUtil.hxx:35
static int calcMatrixInverse3x3(const TYPE *matrix, TYPE *matrixInverse)
Definition: KWUtil.hxx:542
static int calcMatrixInverse2x2(const TYPE *matrix, TYPE *matrixInverse)
Definition: KWUtil.hxx:522