NAG C++ classes for Hyperspectral Imaging  0.1
SpecImage.hpp
Go to the documentation of this file.
1 /*
2  NAG Copyright 2016.
3 */
4 #ifndef SPECIMAGE_HPP
5 #define SPECIMAGE_HPP
6 #include <string>
7 #include <memory>
8 #include <vector>
9 
10 
243 namespace nag
244 {
245 
251  typedef enum source
252  {
256  } source_t;
257 
261  typedef enum pca_mat
262  {
265  } pca_mat_t;
266 
270  typedef enum kmeans_metric
271  {
274  } kmeans_metric_t;
275 
279  typedef enum matrix_ordering
280  {
284 
288  typedef enum wavelet_name
289  {
292  } wavelet_t;
293 
297  typedef enum wavelet_extension
298  {
303 
307  template<class FPType> struct csr_mat
308  {
309  size_t nnz;
310  size_t m;
311  size_t n;
312  std::vector<size_t> row_ptr;
314  std::vector<uint32_t> col_indx;
315  std::vector<FPType> values;
316  };
317  template<class FPType> using csr_mat_t = struct csr_mat<FPType>;
318 
324  struct cfree
325  {
326  void operator()(void *ptr) { free(ptr); }
327  };
328 
334  template<class FPType> struct pca_results
335  {
336  int k;
337  size_t m;
338  size_t n;
339  FPType pca_tot;
340  std::unique_ptr<FPType[], cfree> loadings;
341  std::unique_ptr<FPType[], cfree> scores;
342  std::vector<FPType> eigenvalues;
343 
344  // We won't get a default copy constructor since we can't copy unique_ptrs, so define a default move constructor
345  // - we seem to get away without this on Linux but not Windows
347  pca_results(pca_results&& a) : loadings(std::move(a.loadings)), scores(std::move(a.scores)), eigenvalues(std::move(a.eigenvalues)) { k = a.k; m = a.m; n = a.n; pca_tot = a.pca_tot; };
348  // Since we've defined a constructor the implicit default constructor is deleted, so explicitly request one
350  pca_results() = default;
352  pca_results& operator=(pca_results&& a) { loadings = std::move(a.loadings); scores = std::move(a.scores); eigenvalues = std::move(a.eigenvalues); k = a.k; m = a.m; n = a.n; pca_tot = a.pca_tot; return *this; };
353  // note we don't have a copy assignment operator since we're working with unique_ptrs.
354  };
360  template<class FPType> using pca_results_t = struct pca_results<FPType>;
361 
367  template<class FPType> struct tsne_results
368  {
369  int k;
370  size_t m;
371  size_t d;
372  FPType kl_divergence;
373  FPType theta;
374  FPType perplexity;
375  std::unique_ptr<FPType[], cfree> embedding;
377 
378  // We won't get a default copy constructor since we can't copy unique_ptrs, so define a default move constructor
379  // - we seem to get away without this on Linux but not Windows
381  tsne_results(tsne_results&& a) : embedding(std::move(a.embedding) ) { k = a.k; m = a.m; d = a.d; kl_divergence = a.kl_divergence; theta = a.theta; perplexity = a.perplexity; total_iter = a.total_iter;};
382  // Since we've defined a constructor the implicit default constructor is deleted, so explicitly request one
384  tsne_results() = default;
386  tsne_results& operator=(tsne_results&& a) { embedding = std::move(a.embedding); k = a.k; m = a.m; kl_divergence = a.kl_divergence; theta = a.theta; perplexity = a.perplexity; total_iter = a.total_iter; return *this; };
387  // note we don't have a copy assignment operator since we're working with unique_ptrs.
388  };
394  template<class FPType> using tsne_results_t = struct tsne_results<FPType>;
395 
401  template<class FPType> struct kmeans_results
402  {
403  int k;
404  size_t m;
405  size_t n;
407  std::unique_ptr<FPType[], cfree> cmeans;
408  std::vector<int> inc;
409  std::vector<int> nic;
410  std::vector<FPType> wcs;
411 
413  kmeans_results(kmeans_results&& a) : cmeans(std::move(a.cmeans)), inc(std::move(a.inc)), nic(std::move(a.nic)), wcs(std::move(a.wcs)) { k = a.k; m = a.m; n = a.n; metric = a.metric; };
415  kmeans_results() = default;
417  kmeans_results& operator=(kmeans_results&& a) { cmeans = std::move(a.cmeans); inc = std::move(a.inc); nic = std::move(a.nic); wcs = std::move(a.wcs); k = a.k; m = a.m; n = a.n; metric = a.metric; return *this; };
418  };
424  template<class FPType> using kmeans_results_t = struct kmeans_results<FPType>;
425 
431  template<class FPType> struct nmf_results
432  {
433  int k;
434  size_t m;
435  size_t n;
436  std::unique_ptr<FPType[], cfree> W;
437  std::unique_ptr<FPType[], cfree> H;
438  FPType tol;
439 
441  nmf_results(nmf_results&& a) : W(std::move(a.W)), H(std::move(a.H)) { k = a.k; m = a.m; n = a.n; tol = a.tol; };
443  nmf_results() = default;
445  nmf_results& operator=(nmf_results&& a) { W = std::move(a.W); H = std::move(a.H); k = a.k; m = a.m; n = a.n; tol = a.tol; return *this; };
446  };
452  template<class FPType> using nmf_results_t = struct nmf_results<FPType>;
453 
459  template<class FPType> struct cwt_results
460  {
461  size_t n;
462  int nscales;
463  size_t npixels;
464  std::vector<uint32_t> scales;
465  std::vector<size_t> pixels;
466  std::unique_ptr<FPType[], cfree> coefficients;
467 
469  cwt_results(cwt_results&& a) : scales(std::move(a.scales)), pixels(std::move(a.pixels)), coefficients(std::move(a.coefficients)) { n = a.n; nscales = a.nscales; npixels = a.npixels; }
471  cwt_results() = default;
473  cwt_results& operator=(cwt_results&& a) { scales = std::move(a.scales); pixels = std::move(a.pixels); coefficients = std::move(a.coefficients); n = a.n; nscales = a.nscales; npixels = a.npixels; return *this; }
474  };
480  template<class FPType> using cwt_results_t = struct cwt_results<FPType>;
481 
485  class Error
486  {
487  public:
488  std::string message;
489  Error(std::string msg) : message(msg) {};
490  };
491 
492 
496  template<class FPType> class SpecImage
497  {
498 
499  protected:
500  void *_imgh;
502 
503  public:
507  SpecImage() : _imgh(NULL) {};
508 
516  SpecImage(const csr_mat_t<FPType>& matrix, const FPType *mzvals = NULL);
517 
528  SpecImage(size_t m, size_t n, const FPType *matrix, const FPType *mzvals = NULL);
529 
533  virtual ~SpecImage();
534 
535 
536  /*
537  Preprocessing functions
538  */
547  void peakSelect();
548 
549 
550  /*
551  Analysis functions
552  */
597 
634  kmeans_results_t<FPType> kmeans(uint32_t k, FPType* initial_cmeans = NULL, kmeans_metric_t metric = Cosine);
635 
682  nmf_results_t<FPType> nmf(uint32_t k, FPType *initial_W = NULL, FPType *initial_H = NULL,
683  FPType tol = 1.e-2, uint32_t max_iter = 200);
684 
752  cwt_results_t<FPType> cwt(const std::vector<uint32_t>& scales, const std::vector<size_t>& pixels,
754 
802  tsne_results_t<FPType> tsne(uint32_t k, pca_results_t<FPType> *data = NULL, FPType *embedding = NULL,
803  FPType perplexity = 50, FPType theta = 0.3, uint32_t max_iter = 1000,
804  uint32_t verbosity = 50, FPType min_grad_norm = 1.0e-5,
805  uint32_t n_unimproved_iter = 30, FPType learning_rate = 100,
806  FPType early_exaggeration = 4.0, uint32_t exaggerated_iter = 50);
807 
808 
809  /*
810  Accessor functions. No set counterparts; setting
811  should be done by constructing a new object.
812 
813  Limits can be set only since they are provided by the user and stored internally
814  as matrix columns or rows, depending on the matrix ordering.
815  */
816  /*
817  Get matrix functions.
818 
819  Perform conversion if matrix is currently in different format, or requires transpose.
820  Sparse matrix is row major (so e.g. pixelsByChannels stores channels contiguously).
821  */
836 
852  std::unique_ptr<FPType[]> getDenseMatrix(matrix_ordering_t order = PixelsByChannels);
853 
854 
863  std::vector<FPType> getSinglePeak(FPType minmz, FPType maxmz);
864 
865 
875  size_t getM();
876 
884  size_t getN();
885 
889  size_t getNNZ();
890 
896  int getXDim();
897 
903  int getYDim();
904 
910  int getZDim();
911 
912  // rely on compiler elision / move semantics here
918  std::vector<FPType> getMZVals();
919 
927  void setLimits(const std::vector<FPType>& limits);
928 
946 
947  //TODO?
948  // Functions to add spectra incrementally? Or remove spectra?
949  // Matrix operators: +, *, '(transpose) ?
950  };
951 
952 }
953 #endif
enum nag::pca_mat pca_mat_t
The matrix used to perform principal component analysis.
void setLimits(const std::vector< FPType > &limits)
Set the limits to use during nag::SpecImage::peakSelect.
std::string message
Contains error details.
Definition: SpecImage.hpp:488
Indicates that the rows of the matrix represent pixels, and the columns mass channels.
Definition: SpecImage.hpp:281
A type used to hold the result of a 1D real continuous wavelet transform.
Definition: SpecImage.hpp:459
size_t npixels
The number of pixels for which the CWT has been performed.
Definition: SpecImage.hpp:463
size_t m
The number of rows of the input matrix.
Definition: SpecImage.hpp:434
std::unique_ptr< FPType[], cfree > cmeans
The k by n matrix whose rows define the k cluster centroids, stored in row-major ordering.
Definition: SpecImage.hpp:407
size_t n
The number of columns of the matrix.
Definition: SpecImage.hpp:338
pca_results_t< FPType > pca(uint32_t k, pca_mat_t t=Covariance)
Principal component analysis.
Use the Haar wavelet for nag::SpecImage::cwt.
Definition: SpecImage.hpp:291
Base class representing a spectral image.
Definition: SpecImage.hpp:496
enum nag::wavelet_extension wavelet_extension_t
The end extension method used to perform the continuous wavelet transform.
The source is unknown and the object has been constructed directly as a base nag::SpecImage type...
Definition: SpecImage.hpp:255
Use the covariance matrix to perform nag::SpecImage::pca.
Definition: SpecImage.hpp:263
Error(std::string msg)
Definition: SpecImage.hpp:489
matrix_ordering
A type used to indicate whether the ordering of the matrix is pixels by channels or channels by pixel...
Definition: SpecImage.hpp:279
int getZDim()
Return the number of pixels in the image in the dimension.
size_t n
The input length (number of channels).
Definition: SpecImage.hpp:461
std::unique_ptr< FPType[], cfree > coefficients
n by nscales by npixels 3D array of output CWT coefficients. The coefficient for the scale of the ...
Definition: SpecImage.hpp:466
size_t d
The number of columns of the input matrix; equivalent to the number of dimensions of the input data...
Definition: SpecImage.hpp:371
nmf_results_t< FPType > nmf(uint32_t k, FPType *initial_W=NULL, FPType *initial_H=NULL, FPType tol=1.e-2, uint32_t max_iter=200)
Non-negative matrix factorization.
wavelet_name
The wavelet used to perform the continuous wavelet transform.
Definition: SpecImage.hpp:288
kmeans_results_t< FPType > kmeans(uint32_t k, FPType *initial_cmeans=NULL, kmeans_metric_t metric=Cosine)
k-means clustering analysis
std::unique_ptr< FPType[], cfree > scores
The m by k PCA scores matrix, stored in column-major ordering.
Definition: SpecImage.hpp:341
Use the Euclidean distance metric for nag::SpecImage::kmeans analysis.
Definition: SpecImage.hpp:272
std::vector< FPType > eigenvalues
The eigenvalues vector of length k corresponding to the variance accounted for in the th component (i...
Definition: SpecImage.hpp:342
Overall namespace for the NAG C++ Hyperspectral Imaging Library.
Definition: MALDISpecImage.hpp:12
size_t n
The number of columns of the matrix.
Definition: SpecImage.hpp:405
size_t n
The number of columns of the input matrix.
Definition: SpecImage.hpp:435
std::vector< FPType > getMZVals()
Return the mass-to-charge values which label the channels in the image matrix.
int k
The selected inner dimension of the factorization.
Definition: SpecImage.hpp:433
A type used to hold the result of a non-negative matrix factorization.
Definition: SpecImage.hpp:431
tsne_results()=default
Default constructor.
size_t getM()
Return the number of rows in the matrix.
kmeans_metric_t metric
The distance metric used in the analysis.
Definition: SpecImage.hpp:406
std::unique_ptr< FPType[], cfree > embedding
The m by k lower-dimensional matrix, stored in column-major order.
Definition: SpecImage.hpp:375
nmf_results(nmf_results &&a)
Move constructor.
Definition: SpecImage.hpp:441
enum nag::source source_t
A type used to indicate the instrument source of the image. Returned by nag::SpecImage.getSource.
nmf_results & operator=(nmf_results &&a)
Move assignment operator.
Definition: SpecImage.hpp:445
A type used to hold the details of a principal component analysis.
Definition: SpecImage.hpp:334
std::vector< FPType > getSinglePeak(FPType minmz, FPType maxmz)
Return a single peak. A lower limit minmz and an upper limit maxmz are used to reduce the number of c...
size_t m
The number of rows of the matrix.
Definition: SpecImage.hpp:337
size_t m
The number of rows of the matrix.
Definition: SpecImage.hpp:404
size_t getN()
Return the number of columns in the matrix.
source
A type used to indicate the instrument source of the image. Returned by nag::SpecImage.getSource.
Definition: SpecImage.hpp:251
pca_mat
The matrix used to perform principal component analysis.
Definition: SpecImage.hpp:261
kmeans_results()=default
Default constructor.
std::vector< size_t > row_ptr
row_ptr is a vector of length m + 1. row_ptr[i] gives the start of row i in the col_indx and values v...
Definition: SpecImage.hpp:313
tsne_results_t< FPType > tsne(uint32_t k, pca_results_t< FPType > *data=NULL, FPType *embedding=NULL, FPType perplexity=50, FPType theta=0.3, uint32_t max_iter=1000, uint32_t verbosity=50, FPType min_grad_norm=1.0e-5, uint32_t n_unimproved_iter=30, FPType learning_rate=100, FPType early_exaggeration=4.0, uint32_t exaggerated_iter=50)
t-SNE analysis
int k
The number of dimensions in the embedding.
Definition: SpecImage.hpp:369
size_t n
n is the number of columns of the matrix.
Definition: SpecImage.hpp:311
kmeans_metric
The metric used to perform k-means clustering analysis.
Definition: SpecImage.hpp:270
std::vector< int > inc
A vector of length m in which inc[i] indicates the cluster to which the th pixel belongs.
Definition: SpecImage.hpp:408
tsne_results(tsne_results &&a)
Move constructor.
Definition: SpecImage.hpp:381
FPType theta
The parameter theta used in t-SNE; it represents a tradeoff between speed and accuracy: 0 is accurate...
Definition: SpecImage.hpp:373
A type used to wrap access to the C free() function.
Definition: SpecImage.hpp:324
Indicates that the rows of the matrix represent mass channels, and the columns pixels.
Definition: SpecImage.hpp:282
FPType tol
The tolerance used to produce the result.
Definition: SpecImage.hpp:438
tsne_results & operator=(tsne_results &&a)
Move assignment operator.
Definition: SpecImage.hpp:386
size_t getNNZ()
Return the number of non-zero elements in the matrix.
Use periodic end extension for nag::SpecImage::cwt.
Definition: SpecImage.hpp:299
int k
The number of principal components found.
Definition: SpecImage.hpp:336
Use the Mexican hat wavelet for nag::SpecImage::cwt.
Definition: SpecImage.hpp:290
FPType pca_tot
The sum total variance for all of the components.
Definition: SpecImage.hpp:339
std::vector< FPType > values
values is a vector of length nnz. values[i] gives the value of the ith non-zero element.
Definition: SpecImage.hpp:315
std::vector< uint32_t > col_indx
col_indx is a vector of length nnz. col_indx[i] gives the column index of the ith non-zero element...
Definition: SpecImage.hpp:314
csr_mat_t< FPType > getSparseMatrix(matrix_ordering_t order=PixelsByChannels)
Retrieve a copy of the image matrix in sparse storage format.
kmeans_results(kmeans_results &&a)
Move constructor.
Definition: SpecImage.hpp:413
std::unique_ptr< FPType[], cfree > loadings
The n by k PCA loadings matrix, stored in column-major ordering. The loadings are returned in decreas...
Definition: SpecImage.hpp:340
Use the correlation matrix to perform nag::SpecImage::pca.
Definition: SpecImage.hpp:264
SpecImage()
Default constructor.
Definition: SpecImage.hpp:507
size_t m
m is the number of rows of the matrix.
Definition: SpecImage.hpp:310
Use zero end extension for nag::SpecImage::cwt.
Definition: SpecImage.hpp:300
int k
The number of clusters in the analysis.
Definition: SpecImage.hpp:403
cwt_results_t< FPType > cwt(const std::vector< uint32_t > &scales, const std::vector< size_t > &pixels, wavelet_t wav=Haar, wavelet_extension_t ext=Periodic)
Batched one-dimensional real continuous wavelet transform.
std::unique_ptr< FPType[], cfree > W
The m by k matrix factor W, stored in column-major ordering.
Definition: SpecImage.hpp:436
int getXDim()
Return the number of pixels in the image in the dimension.
cwt_results & operator=(cwt_results &&a)
Move assignment operator.
Definition: SpecImage.hpp:473
nmf_results()=default
Default constructor.
FPType perplexity
The perplexity used in the t-SNE algorithm.
Definition: SpecImage.hpp:374
pca_results(pca_results &&a)
Move constructor.
Definition: SpecImage.hpp:347
wavelet_extension
The end extension method used to perform the continuous wavelet transform.
Definition: SpecImage.hpp:297
int getYDim()
Return the number of pixels in the image in the dimension.
A type used to hold the details of a t-SNE analysis.
Definition: SpecImage.hpp:367
size_t nnz
nnz is the number of non-zero elements.
Definition: SpecImage.hpp:309
cwt_results(cwt_results &&a)
Move constructor.
Definition: SpecImage.hpp:469
std::vector< FPType > wcs
A vector of length k in which wcs[i] gives the within-cluster sum of distances to the th cluster cent...
Definition: SpecImage.hpp:410
The source is a MALDI instrument, and the object is of type nag::MALDISpecImage.
Definition: SpecImage.hpp:254
int total_iter
The number of iterations that were performed.
Definition: SpecImage.hpp:376
std::vector< size_t > pixels
Vector of pixels used.
Definition: SpecImage.hpp:465
Class thrown on error.
Definition: SpecImage.hpp:485
int nscales
The number of scales over which the CWT has been performed.
Definition: SpecImage.hpp:462
void operator()(void *ptr)
Definition: SpecImage.hpp:326
std::vector< uint32_t > scales
Vector of scales used.
Definition: SpecImage.hpp:464
std::unique_ptr< FPType[]> getDenseMatrix(matrix_ordering_t order=PixelsByChannels)
Retrieve a copy of the image matrix in dense storage format.
Use symmetric end extension for nag::SpecImage::cwt.
Definition: SpecImage.hpp:301
virtual ~SpecImage()
Destructor.
std::unique_ptr< FPType[], cfree > H
The k by n matrix factor H, stored in column-major ordering.
Definition: SpecImage.hpp:437
size_t m
The number of rows of the input matrix (and the number of rows of the lower dimensional embedding)...
Definition: SpecImage.hpp:370
pca_results & operator=(pca_results &&a)
Move assignment operator.
Definition: SpecImage.hpp:352
FPType kl_divergence
The Kullback-Liebler divergence between the input data and the lower-dimensional embedding.
Definition: SpecImage.hpp:372
source_t getSource()
Determine the type of the object.
Definition: SpecImage.hpp:945
kmeans_results & operator=(kmeans_results &&a)
Move assignment operator.
Definition: SpecImage.hpp:417
void * _imgh
Opaque handle to internal library data structure.
Definition: SpecImage.hpp:500
The source is a SIMS instrument, and the object is of type nag::SIMSpecImage.
Definition: SpecImage.hpp:253
A type used to hold the details of a k-means clustering analysis.
Definition: SpecImage.hpp:401
cwt_results()=default
Default constructor.
source_t _source_type
Encodes what the underlying type is: nag::SIMSpecImage (value: SIMS), nag::MALDISpecImage (value: MAL...
Definition: SpecImage.hpp:501
enum nag::kmeans_metric kmeans_metric_t
The metric used to perform k-means clustering analysis.
enum nag::wavelet_name wavelet_t
The wavelet used to perform the continuous wavelet transform.
void peakSelect()
Perform peak selection.
Use the cosine distance metric for nag::SpecImage::kmeans analysis.
Definition: SpecImage.hpp:273
A type used to hold the components of a sparse matrix in compressed sparse row (CSR) format...
Definition: SpecImage.hpp:307
enum nag::matrix_ordering matrix_ordering_t
A type used to indicate whether the ordering of the matrix is pixels by channels or channels by pixel...
pca_results()=default
Default constructor.
std::vector< int > nic
A vector of length k in which nic[i] gives the number of pixels in the th cluster.
Definition: SpecImage.hpp:409