NPL
Neurological Programs and Libraries
fmri_inference.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 fmri_inference.h Tools for performing ICA, including rewriting images as
9  * matrices. This file should really be fmri inference helpers or something
10  * along that line, because it also include GLM helpers.
11  *
12  *****************************************************************************/
13 #ifndef ICA_HELPERS_H
14 #define ICA_HELPERS_H
15 
16 #include <Eigen/Dense>
17 #include <Eigen/IterativeSolvers>
18 #include <string>
19 #include <vector>
20 
21 #include "npltypes.h"
22 #include "mrimage.h"
23 #include "utility.h"
24 #include "macros.h"
25 
26 namespace npl {
27 
28 class MatrixReorg;
29 
53 VectorXd onDiskSVD(const MatrixReorg& A,
54  int rank, size_t poweriters, double varthresh, double cvarthresh,
55  MatrixXd* U=NULL, MatrixXd* V=NULL);
56 
75 void gicaCreateMatrices(size_t tcat, size_t scat, vector<std::string> masks,
76  vector<std::string> inputs, std::string prefix, double maxmem, bool normts,
77  bool verbose);
78 
106 void gicaReduceFull(std::string inpref, std::string outpref, double varthresh,
107  double cvarthresh, size_t maxrank, bool verbose);
108 
139 void gicaReduceProb(std::string inpref, std::string outpref, double varthresh,
140  double cvarthresh, size_t rank, size_t poweriters, bool verbose);
141 
151 void gicaTemporalICA(std::string reorgpref, std::string reducepref,
152  std::string outpref, bool verbose);
153 
163 void gicaSpatialICA(std::string reorgpref, std::string reducepref,
164  std::string outpref, bool verbose);
165 
178 VectorXd covSVD(const MatrixReorg& A, double varthresh, double cvarthresh,
179  size_t maxrank, MatrixXd* U, MatrixXd* V);
180 
181 class MatMap
182 {
183 public:
187  MatMap() : mat(NULL, 0, 0)
188  {
189  };
190 
200  MatMap(std::string filename, size_t rows, size_t cols) : mat(NULL, 0, 0)
201  {
202  create(filename, rows, cols);
203  };
204 
214  MatMap(std::string filename, bool writeable=false) : mat(NULL, 0, 0)
215  {
216  open(filename, writeable);
217  };
218 
219  ~MatMap() { close(); };
220 
230  void open(std::string filename, bool writeable = false);
231 
241  void create(std::string filename, size_t newrows, size_t newcols);
242 
243  void close()
244  {
245  datamap.close();
246  };
247 
248  bool isopen()
249  {
250  return datamap.isopen();
251  };
252 
253  const size_t& rows() const { return m_rows; };
254  const size_t& cols() const { return m_cols; };
255 
256  Eigen::Map<MatrixXd> mat;
257 private:
258  MemMap datamap;
259  size_t m_rows;
260  size_t m_cols;
261 };
262 
271 {
272 public:
273 
283  MatrixReorg(std::string prefix="", size_t maxd=(1<<30), bool verbose=true);
284 
315  int createMats(size_t timeblocks, size_t spaceblocks,
316  const std::vector<std::string>& masknames,
317  const std::vector<std::string>& filenames, bool normts = true);
318 
326  int checkMats();
327 
328 // inline int nwide() const { return m_outrows.size(); };
329  inline int ntall() const { return m_outcols.size(); };
330 
331  inline const vector<int>& tallMatCols() const
332  {
333  return m_outcols;
334  };
335 
336  inline int tallMatRows() const
337  {
338  return m_totalrows;
339  };
340 
341  inline std::string tallMatName(size_t ii) const
342  {
343  return m_prefix+"_tall_"+std::to_string(ii);
344  };
345 
346  inline std::string inColMaskName(size_t ii) const
347  {
348  return m_prefix+"_mask_"+std::to_string(ii)+".nii.gz";
349  };
350 
351  void preMult(Eigen::Ref<MatrixXd> out, const Eigen::Ref<const MatrixXd> in,
352  bool transpose = false) const;
353 
354  void postMult(Eigen::Ref<MatrixXd> out, const Eigen::Ref<const MatrixXd> in,
355  bool transpose = false) const;
356 
357  inline int rows() const { return m_totalrows; };
358  inline int cols() const { return m_totalcols; };
359 
360  std::string mask_name(size_t ii) const
361  {
362  return m_prefix+"_mask_"+std::to_string(ii)+".nii.gz";
363  };
364 
365  std::string info_name() const
366  {
367  return m_prefix+".info";
368  };
369  std::string tall_name(size_t ii) const
370  {
371  return m_prefix+"_tall_"+std::to_string(ii);
372  };
373 
374  private:
375  int m_totalrows;
376  int m_totalcols;
377  std::string m_prefix;
378  bool m_verbose;
379 // vector<int> m_outrows;
380  vector<int> m_outcols;
381  size_t m_maxdoubles;
382 };
383 
393 void fmriGLM(ptr<const MRImage> fmri, const MatrixXd& X,
394  ptr<MRImage> bimg, ptr<MRImage> Timg, ptr<MRImage> pimg);
395 
396 
405 void fmriBandPass(ptr<MRImage> inimg, double cuton, double cutoff);
406 
415 ptr<MRImage> regressOut(ptr<const MRImage> inimg, const MatrixXd& X);
416 
433 MatrixXd extractLabelAVG(ptr<const MRImage> fmri,
434  ptr<const MRImage> labelmap);
435 
453 MatrixXd extractLabelPCA(ptr<const MRImage> fmri,
454  ptr<const MRImage> labelmap, size_t outsz);
455 
473 MatrixXd extractLabelICA(ptr<const MRImage> fmri,
474  ptr<const MRImage> labelmap, size_t outsz);
475 
476 } // NPL
477 
478 #endif //ICA_HELPERS_H
void create(std::string filename, size_t newrows, size_t newcols)
Open an new file as a memory map. ANY OLD FILE WILL BE DELETED The file is always opened for writing ...
Definition: accessors.h:29
MatMap(std::string filename, bool writeable=false)
Open an existing file as a memory map. The file must already exist. If writeable is true then the fil...
int rows() const
MatMap()
default constructor no file is opened
std::string mask_name(size_t ii) const
void postMult(Eigen::Ref< MatrixXd > out, const Eigen::Ref< const MatrixXd > in, bool transpose=false) const
std::string info_name() const
MatrixReorg(std::string prefix="", size_t maxd=(1<< 30), bool verbose=true)
Constructor.
void gicaTemporalICA(std::string reorgpref, std::string reducepref, std::string outpref, bool verbose)
Perform ICA with each dimension as a separate timeseries. This is essentially unmixing in space and p...
void gicaReduceFull(std::string inpref, std::string outpref, double varthresh, double cvarthresh, size_t maxrank, bool verbose)
Compute PCA for the given group, defined.
const vector< int > & tallMatCols() const
bool isopen()
Return true if a file is currently open.
Definition: utility.h:333
void gicaCreateMatrices(size_t tcat, size_t scat, vector< std::string > masks, vector< std::string > inputs, std::string prefix, double maxmem, bool normts, bool verbose)
Create on disk matrices based on an array of input images. The array will be concatinated in time tca...
void gicaReduceProb(std::string inpref, std::string outpref, double varthresh, double cvarthresh, size_t rank, size_t poweriters, bool verbose)
Compute PCA for the given group, defined.
MatrixXd extractLabelAVG(ptr< const MRImage > fmri, ptr< const MRImage > labelmap)
Creates a matrix of timeseries, then perfrorms principal components analysis on it to reduce the numb...
MatrixXd extractLabelICA(ptr< const MRImage > fmri, ptr< const MRImage > labelmap, size_t outsz)
Creates a matrix of timeseries, then perfrorms principal components analysis on it to reduce the numb...
void close()
Close the current file (if one is open). data() will return NULL and size() will return 0 after this ...
MatrixXd extractLabelPCA(ptr< const MRImage > fmri, ptr< const MRImage > labelmap, size_t outsz)
Creates a matrix of timeseries, then perfrorms principal components analysis on it to reduce the numb...
Memory map class. The basic gyst is that this works like a malloc except that data may be initialized...
Definition: utility.h:276
std::string inColMaskName(size_t ii) const
void gicaSpatialICA(std::string reorgpref, std::string reducepref, std::string outpref, bool verbose)
Perform ICA with each dimension as a separate timeseries. This is essentially unmixing in space and p...
VectorXd onDiskSVD(const MatrixReorg &A, int rank, size_t poweriters, double varthresh, double cvarthresh, MatrixXd *U=NULL, MatrixXd *V=NULL)
Uses randomized subspace approximation to reduce the input matrix (made up of blocks stored on disk w...
MatMap(std::string filename, size_t rows, size_t cols)
Open an new file as a memory map. ANY OLD FILE WILL BE DELETED The file is always opened for writing ...
VectorXd covSVD(const MatrixReorg &A, double varthresh, double cvarthresh, size_t maxrank, MatrixXd *U, MatrixXd *V)
Computes the SVD from XXt using the JacobiSVD.
void fmriGLM(ptr< const MRImage > fmri, const MatrixXd &X, ptr< MRImage > bimg, ptr< MRImage > Timg, ptr< MRImage > pimg)
Performs general linear model analysis on a 4D image.
int ntall() const
void fmriBandPass(ptr< MRImage > inimg, double cuton, double cutoff)
Takes the FFT of each line of the image, performs bandpass filtering on the line and then invert FFTs...
const size_t & rows() const
int tallMatRows() const
int createMats(size_t timeblocks, size_t spaceblocks, const std::vector< std::string > &masknames, const std::vector< std::string > &filenames, bool normts=true)
Creates two sets of matrices from a set of input images. The matrices (images) are ordered in column ...
void open(std::string filename, bool writeable=false)
Open an existing file as a memory map. The file must already exist. If writeable is true then the fil...
std::string tall_name(size_t ii) const
std::string tallMatName(size_t ii) const
void preMult(Eigen::Ref< MatrixXd > out, const Eigen::Ref< const MatrixXd > in, bool transpose=false) const
Reorganizes input images into tall and wide matrices (matrices that span the total rows and cols...
int checkMats()
Loads existing matrices by first reading ${prefix}_tall_0, ${prefix}_wide_0, and ${prefix}_mask_*, and checking that all the dimensions can be made to match (by loading the appropriate number of matrices/masks).
Eigen::Map< MatrixXd > mat
int cols() const
const size_t & cols() const
ptr< MRImage > regressOut(ptr< const MRImage > inimg, const MatrixXd &X)
Regresses out the given variables, creating time series which are uncorrelated with X...