Isis 3.0 Programmer Reference
Back | Home
CorrelationMatrix.cpp
1 #include "CorrelationMatrix.h"
2 
3 #include <QDataStream>
4 #include <QDebug>
5 #include <QFile>
6 #include <QList>
7 #include <QString>
8 #include <QStringList>
9 #include <QtCore/qmath.h>
10 
11 #include "IException.h"
12 #include "LinearAlgebra.h"
13 #include "Pvl.h"
14 #include "PvlObject.h"
15 #include "SparseBlockMatrix.h"
16 
17 namespace Isis {
22  m_covarianceFileName = new FileName("");
26  m_diagonals = new QList<double>();
27  }
28 
29 
60  //m_imagesAndParameters = NULL;
64  m_diagonals = NULL;
65  m_visibleBlocks = NULL;
66 
67  if (storedMatrixData.name() != "CorrelationMatrixData") {
68  QString msg = "This Pvl Object does not have the correct correlation information. The Object "
69  "you are looking for is called CorrelationMatrixData.";
71  }
72 
73  try {
74  m_covarianceFileName =
75  new FileName(storedMatrixData.findKeyword("CovarianceMatrixFileName")[0]);
76  }
77  catch (IException &e) {
78  QString msg = "Could not find the Covariance Matrix .dat file name.";
79  throw IException(e, IException::User, msg, _FILEINFO_);
80  }
81 
82  try {
83  QString corrFileName = storedMatrixData.findKeyword("CorrelationMatrixFileName")[0];
84  if (corrFileName == "NULL") {
86  }
87  else {
88  m_correlationFileName = new FileName(corrFileName);
89  }
90  }
91  catch (IException &e) {
92  QString msg = "Could not find the Correlation Matrix .dat file name.";
93  throw IException(e, IException::User, msg, _FILEINFO_);
94  }
95 
96  try {
98  imgsIt = storedMatrixData.findGroup("ImagesAndParameters").begin();
99  while ( imgsIt != storedMatrixData.findGroup("ImagesAndParameters").end() ) {
100  QStringList params = (*imgsIt)[0].split(",");
101  m_imagesAndParameters->insert(imgsIt->name(), params);
102  imgsIt++;
103  }
104  }
105  catch (IException &e) {
106  QString msg = "Could not get Images and Parameters from ImagesAndParameters group.";
107  throw IException(e, IException::User, msg, _FILEINFO_);
108  }
109  }
110 
111 
121  m_diagonals = new QList<double>(*other.m_diagonals);
123  }
124 
125 
130  delete m_imagesAndParameters;
131  m_imagesAndParameters = NULL;
132 
133  delete m_covarianceFileName;
134  m_covarianceFileName = NULL;
135 
136  delete m_correlationFileName;
137  m_correlationFileName = NULL;
138 
139  delete m_diagonals;
140  m_diagonals = NULL;
141 
142  delete m_visibleBlocks;
143  m_visibleBlocks = NULL;
144  }
145 
146 
156 
157  if (&other != this) {
158 
159  delete m_imagesAndParameters;
160  m_imagesAndParameters = NULL;
162 
163  delete m_covarianceFileName;
164  m_covarianceFileName = NULL;
165  m_covarianceFileName = new FileName(*other.m_covarianceFileName);
166 
167  delete m_correlationFileName;
168  m_correlationFileName = NULL;
170 
171  delete m_diagonals;
172  m_diagonals = NULL;
173  m_diagonals = new QList<double>(*other.m_diagonals);
174 
175  delete m_visibleBlocks;
176  m_visibleBlocks = NULL;
177  m_visibleBlocks = new QList<SparseBlockColumnMatrix>(*other.m_visibleBlocks);
178 
179  }
180 
181  return *this;
182  }
183 
184 
195 
196  if ( !isValid() ) {
197  QString msg = "Cannot compute correlation matrix without a specified file name. Use "
198  "setCorrelationFileName(FileName) before calling computeCorrelationMatrix().";
200  }
201  delete m_visibleBlocks;
202  m_visibleBlocks = NULL;
204 
205  // Create file handle
206  QFile matrixInput( m_covarianceFileName->expanded() );
207  QFile matrixOutput( m_correlationFileName->expanded() );
208 
209  // Open file to write to
210  matrixInput.open(QIODevice::ReadOnly);
211  matrixOutput.open(QIODevice::WriteOnly);
212 
213  // Open Stream
214  QDataStream inStream(&matrixInput);
215  QDataStream outStream(&matrixOutput);
216 
217  double firstParam1 = 0, //starting param for each iteration
218  firstParam2 = 0;
219  double param1 = 0, // current param for each iteration
220  param2 = 0;
221  // Read one column at a time
223  while ( !inStream.atEnd() ) {
224  inStream >> sbcm;
225 
226  // Store diagonal
227  int numOfBlocks = sbcm.size();
228  int lastBlock = numOfBlocks - 1;
229  int numDiagonals = sbcm[lastBlock]->size1();
230 
231  // Get Diagonals
232  for (int i = 0; i < numDiagonals; i++) {
233  double val = ( *(sbcm[lastBlock]) )(i, i);
234  m_diagonals->append(val);
235  }
236 
237  // compute correlations
238  QMapIterator<int, LinearAlgebra::Matrix *> block(sbcm);
239 
240  while ( block.hasNext() ) { // each block in the column
241  block.next();
242  for (int row = 0; row < (int)block.value()->size1(); row++) { // each row in the block
243  for (int column = 0; column < (int)block.value()->size2(); column++) { // each column
244  // correlation = covariance / (variance1 * variance2)
245  ( *block.value() )(row, column) = ( *block.value() )(row, column) /
246  sqrt( (*m_diagonals)[param1] *
247  (*m_diagonals)[param2] );
248  param2++;
249  }
250  param1++;
251  param2 = firstParam2;
252  }
253  firstParam1 += block.value()->size1();
254  param1 = firstParam1;
255  }
256  firstParam1 = 0; // start each column at first element of diagonal list
257  param1 = firstParam1;
258  firstParam2 += block.value()->size2();
259  param2 = firstParam2;
260 
261  outStream << sbcm;
262  m_visibleBlocks->append(sbcm);
263  }
264 
265  // close file
266  matrixInput.close();
267  matrixOutput.close();
268  }
269 
270 
271 
280 
281 // if ( !correlationMatrixExists() ) {
282  // call computeCorrelationMatrix
283 // }
284  // read the values we want from the correlation matrix file.
285 
286  // store values by column?
287  // return list of values in m_visibleElements
288  }
289 
290 
291 
299 // QList<MatrixElement*> CorrelationMatrix::visibleElements() {
300 // return *m_visibleElements;
301 // }
302 
303 
304 
312 
313  return !(m_correlationFileName->name() == "" || m_covarianceFileName->name() == "");
314  }
315 
316 
325  return !(m_covarianceFileName->name() == "");
326  }
327 
328 
329  // Set Methods
335  if (m_correlationFileName == NULL) {
336  m_correlationFileName = new FileName(correlationFileName);
337  }
338  else {
340  }
341  }
342 
343 
349  if (m_covarianceFileName == NULL) {
350  m_covarianceFileName = new FileName(covarianceFileName);
351  }
352  else {
354  }
355  //Make the correlation matrix file name match the covariance matrix file name.
356  if (!isValid()) {
357  QString fName = covarianceFileName.expanded().replace( QString("inverse"),
358  QString("correlation") );
360  }
361  }
362 
363 
371  if (m_imagesAndParameters == NULL) {
373  }
374  else {
376  }
377  }
378 
379 
386  return *m_correlationFileName;
387  }
388 
389 
396  return *m_covarianceFileName;
397  }
398 
399 
407  return m_imagesAndParameters;
408  }
409 
410 
417 // SparseBlockColumnMatrix sbcm;
418 // QFile matrixInput( m_correlationFileName->expanded() );
419 // matrixInput.open(QIODevice::ReadOnly);
420 // QDataStream inStream(&matrixInput);
421 //
422 // while( !inStream.atEnd() ) {
423 // inStream >> sbcm;
424 // m_visibleBlocks->append(&sbcm);
425 // }
426  }
427 
428 
435  }
436 
437 
443  return m_visibleBlocks;
444  }
445 
446 
465  PvlObject corrMatInfo("CorrelationMatrixData");
466 
467  corrMatInfo += PvlKeyword( "CovarianceMatrixFileName", m_covarianceFileName->expanded() );
468  corrMatInfo += PvlKeyword( "CorrelationMatrixFileName", m_correlationFileName->expanded() );
469 
470  PvlGroup imgsAndParams("ImagesAndParameters");
471  QMapIterator<QString, QStringList> imgParamIt(*m_imagesAndParameters);
472  while ( imgParamIt.hasNext() ) {
473  imgParamIt.next();
474  imgsAndParams += PvlKeyword( imgParamIt.key(), imgParamIt.value().join(",") );
475  }
476  corrMatInfo += imgsAndParams;
477 
478  return corrMatInfo;
479  }
480 
481 
489  QDataStream &CorrelationMatrix::write(QDataStream &stream) const {
490  // QMaps
491  stream << *m_imagesAndParameters;
492  // FileNames
493  stream << m_covarianceFileName->expanded() << m_correlationFileName->expanded();
494  // QLists
495  stream << *m_diagonals << *m_visibleBlocks;
496  return stream;
497  }
498 
499 
506  QDataStream &CorrelationMatrix::read(QDataStream &stream) {
507  // QMaps
509  stream >> imagesAndParameters;
510  delete m_imagesAndParameters;
511  m_imagesAndParameters = NULL;
513 
514  // FileNames
515  QString covarianceFileName;
516  stream >> covarianceFileName;
517  delete m_covarianceFileName;
518  m_covarianceFileName = NULL;
519  m_covarianceFileName = new FileName(covarianceFileName);
520 
521  QString correlationFileName;
522  stream >> correlationFileName;
523  delete m_correlationFileName;
524  m_correlationFileName = NULL;
525  m_correlationFileName = new FileName(correlationFileName);
526 
527  // QLists
528  QList<double> diagonals;
529  stream >> diagonals;
530  delete m_diagonals;
531  m_diagonals = NULL;
532  m_diagonals = new QList<double>(diagonals);
533 
535  stream >> visibleBlocks;
536  delete m_visibleBlocks;
537  m_visibleBlocks = NULL;
539 
540  return stream;
541  }
542 
543 
550  QDataStream &operator<<(QDataStream &stream, const CorrelationMatrix &matrix) {
551  return matrix.write(stream);
552  }
553 
554 
561  QDataStream &operator>>(QDataStream &stream, CorrelationMatrix &matrix) {
562  return matrix.read(stream);
563  }
564 }
QMap< QString, QStringList > * m_imagesAndParameters
This map holds the images used to create this matrix and their associated parameters.
QList< SparseBlockColumnMatrix > * m_visibleBlocks
This will be the three blocks (or whole matrix depending on size) that apply to the given area...
void retrieveVisibleElements(int x, int y)
This method will open the correlation matrix file and read in the blocks that apply to the requested ...
void retrieveWholeMatrix()
This method will read the matrix in from the file and hold on to the whole thing. ...
PvlGroupIterator findGroup(const QString &name, PvlGroupIterator beg, PvlGroupIterator end)
Find a group with the specified name, within these indexes.
Definition: PvlObject.h:141
This is a container for the correlation matrix that comes from a bundle adjust.
FileName covarianceFileName()
Public access for the covariance matrix file name.
SparseBlockColumnMatrix.
File name manipulation and expansion.
Definition: FileName.h:111
CorrelationMatrix()
Default Constructor.
void setCorrelationFileName(FileName correlationFileName)
Set the qmap of images and parameters.
FileName correlationFileName()
Public access for the correlation matrix file name.
void computeCorrelationMatrix()
This method reads the covariance matrix in from a file, one SparseBlockColumnMatrix at a time...
void setImagesAndParameters(QMap< QString, QStringList > imagesAndParameters)
Set the qmap of images and parameters.
QDataStream & read(QDataStream &stream)
Reads CorrelationMatrix data from the input stream and places the data in member variables.
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:154
Unless noted otherwise, the portions of Isis written by the USGS are public domain.
PvlObject pvlObject()
This method creates a Pvl group with the information necessary to recreate this correlation matrix...
PvlKeyword & findKeyword(const QString &kname, FindOptions opts)
Finds a keyword in the current PvlObject, or deeper inside other PvlObjects and Pvlgroups within this...
Definition: PvlObject.cpp:148
Contains multiple PvlContainers.
Definition: PvlGroup.h:57
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:38
std::istream & operator>>(std::istream &is, CSVReader &csv)
Input read operator for input stream sources.
Definition: CSVReader.cpp:463
void retrieveThreeVisibleBlocks()
This method will be used when the matrix is too big to display the whole thing.
QList< double > * m_diagonals
List of the parameter values.
A type of error that could only have occurred due to a mistake on the user&#39;s part (e...
Definition: IException.h:134
A single keyword-value pair.
Definition: PvlKeyword.h:98
bool hasCovMat()
This is used to make sure the covariance matrix exists.
void setCovarianceFileName(FileName covarianceFileName)
Set the qmap of images and parameters.
Unless noted otherwise, the portions of Isis written by the USGS are public domain.
FileName * m_correlationFileName
FileName of the correlation matrix.
FileName * m_covarianceFileName
FileName of the covariance matrix calculated when the bundle was run.
bool isValid()
This is the public accessor for the list of elements that should be displayed in the current view...
Isis exception class.
Definition: IException.h:99
QList< SparseBlockColumnMatrix > * visibleBlocks()
Get the visible part of the matrix.
CorrelationMatrix & operator=(const CorrelationMatrix &other)
Equal Operator.
QDebug operator<<(QDebug debug, const Hillshade &hillshade)
Print this class out to a QDebug object.
Definition: Hillshade.cpp:308
Contains Pvl Groups and Pvl Objects.
Definition: PvlObject.h:74
QString name() const
Returns the container name.
Definition: PvlContainer.h:78
QDataStream & write(QDataStream &stream) const
Writes CorrelationMatrix data to the output stream and returns this stream to the user...
QMap< QString, QStringList > * imagesAndParameters()
Public access for the qmap of images and parameters.
QList< PvlKeyword >::iterator PvlKeywordIterator
The keyword iterator.
Definition: PvlContainer.h:172

U.S. Department of the Interior | U.S. Geological Survey
ISIS | Privacy & Disclaimers | Astrogeology Research Program
To contact us, please post comments and questions on the ISIS Support Center
File Modified: 07/12/2023 23:16:47