NPL
Neurological Programs and Libraries
utility.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2014 Micah C Chambers (micahc.vt@gmail.com)
3  *
4  * NPL is free software: you can redistribute it and/or modify it under the
5  * terms of the BSD 2-Clause License available in LICENSE or at
6  * http://opensource.org/licenses/BSD-2-Clause
7  *
8  * @file utility.h File with random utilities, for string processing, for
9  * determining basic filename information (basename/dirname).
10  *
11  *****************************************************************************/
12 #ifndef UTILITY_FUNCTIONS_H
13 #define UTILITY_FUNCTIONS_H
14 
15 #include <string>
16 #include <cmath>
17 #include <list>
18 #include <vector>
19 
20 #define __FUNCTION_STR__ std::string(__PRETTY_FUNCTION__)
21 
22 namespace npl {
23 
31 std::string dirname(std::string path);
32 
40 bool isTxt(std::string filename);
41 
49 bool fileExists(std::string filename);
50 
58 std::string chomp(std::string str);
59 
75 std::vector<std::string> parseLine(std::string line, std::string delim);
76 
94 std::vector<std::vector<std::string>> readStrCSV(std::string filename,
95  char& delim, char comment = '#');
96 
113 std::vector<std::vector<double>> readNumericCSV(std::string filename,
114  char comment = '#');
115 
125 double gammaPDF(double x, double k, double theta);
126 
127 
149 double cannonHrf(double t, double rdelay, double udelay, double rdisp,
150  double udisp, double puRatio, double onset, double total);
151 
159 double cannonHrf(double t);
160 
181 std::vector<double> getRegressor(std::vector<std::vector<double>>& spec,
182  double tr, size_t ntimes, double t0);
183 
204 std::vector<double> getRegressor(std::vector<std::vector<double>>& spec,
205  double tr, size_t ntimes, double t0);
206 
222 void boldsim(size_t len, double* iobuff, double dt, double learn);
223 
238 void convolve(std::vector<double>& signal, double(*foo)(double),
239  double tr, double length);
240 
250 inline
251 double fwhm_to_sd(double fwhm)
252 {
253  return fwhm/(2*sqrt(2*log(2)));
254 }
255 
264 inline
265 double sd_to_fwhm(double sd)
266 {
267  return sd*(2*sqrt(2*log(2)));
268 }
269 
276 class MemMap
277 {
278 public:
287  MemMap(std::string fn, size_t bsize);
288 
297  MemMap(std::string fn, bool writeable=false);
298 
303  MemMap() : m_size(0), m_fd(0), m_data(NULL) {};
304 
315  int64_t openNew(std::string fn, size_t bsize);
316 
328  int64_t openExisting(std::string fn, bool writeable, bool quiet = true);
329 
333  bool isopen() { return m_size > 0; };
334 
339  void close();
340 
344  ~MemMap();
345 
351  void* data() { return m_data; };
352 
358  const void* data() const { return m_data; };
359 
369  int64_t size() { return m_size; };
370 private:
371 
372  int64_t m_size;
373  int m_fd;
374  void* m_data;
375 };
376 
377 }
378 #endif // UTILITY_FUNCTIONS_H
379 
380 
Definition: accessors.h:29
MemMap()
Just create the object. Until open() is called the size() will be 0 and returned data() will be NULL;...
Definition: utility.h:303
double cannonHrf(double t, double rdelay, double udelay, double rdisp, double udisp, double puRatio, double onset, double total)
Cannonical hemodynamic response funciton from SPM. delay of response (relative to onset) = 6 delay of...
double fwhm_to_sd(double fwhm)
Computes the standard deviation from FWHM (because I can never remember the ratio) ...
Definition: utility.h:251
std::string dirname(std::string path)
Returns the directory name for the given file.
std::vector< std::vector< std::string > > readStrCSV(std::string filename, char &delim, char comment= '#')
This function parses an input and returns a list of rows.
int64_t openExisting(std::string fn, bool writeable, bool quiet=true)
Open the specified file with the specified size. If createNew is set then a new file will be created ...
std::string chomp(std::string str)
Removes whitespace at the beginning and end of a string.
int64_t openNew(std::string fn, size_t bsize)
Open the specified file with the specified size. If createNew is set then a new file will be created ...
bool fileExists(std::string filename)
Returns true if a file exists and is possible to open.
std::vector< std::vector< double > > readNumericCSV(std::string filename, char comment= '#')
This function parses an input and returns a list of rows.
bool isopen()
Return true if a file is currently open.
Definition: utility.h:333
void boldsim(size_t len, double *iobuff, double dt, double learn)
In place simulation of BOL timeseries using the balloon model. Habituation is accomplished by subtrac...
void * data()
Get pointer to data.
Definition: utility.h:351
void close()
Close the current file (if one is open). data() will return NULL and size() will return 0 after this ...
std::vector< std::string > parseLine(std::string line, std::string delim)
Given a delimiter splits the line based on the delmiter and removes extra white space as necessary...
Memory map class. The basic gyst is that this works like a malloc except that data may be initialized...
Definition: utility.h:276
double gammaPDF(double x, double k, double theta)
Gamma distribution, used by Cannonical HRF from SPM.
void convolve(std::vector< double > &signal, double(*foo)(double), double tr, double length)
Convolves a signal and a function using loops (not fast) No wrapping is done.
bool isTxt(std::string filename)
Reads a file and returns true if its entirely made up of printable ascii.
std::vector< double > getRegressor(std::vector< std::vector< double >> &spec, double tr, size_t ntimes, double t0)
Takes a 1 or 3 column format of regressor and produces a timeseries sampeled every tr...
const void * data() const
Get constant pointer to current data.
Definition: utility.h:358
double sd_to_fwhm(double sd)
Computes the FWHM from from standard deviation (because I can never remember the ratio) ...
Definition: utility.h:265
int64_t size()
Length (in bytes) of memory map.
Definition: utility.h:369
~MemMap()
Destructor, close file.