Isis 3 Programmer Reference
CorrelationMatrix.cpp
1 
7 /* SPDX-License-Identifier: CC0-1.0 */
8 
9 #include "CorrelationMatrix.h"
10 
11 #include <QDataStream>
12 #include <QDebug>
13 #include <QFile>
14 #include <QList>
15 #include <QString>
16 #include <QStringList>
17 #include <QtCore/qmath.h>
18 
19 #include "IException.h"
20 #include "LinearAlgebra.h"
21 #include "Pvl.h"
22 #include "PvlObject.h"
23 #include "SparseBlockMatrix.h"
24 
25 namespace Isis {
34  m_diagonals = new QList<double>();
35  }
36 
37 
68  //m_imagesAndParameters = NULL;
72  m_diagonals = NULL;
73  m_visibleBlocks = NULL;
74 
75  if (storedMatrixData.name() != "CorrelationMatrixData") {
76  QString msg = "This Pvl Object does not have the correct correlation information. The Object "
77  "you are looking for is called CorrelationMatrixData.";
78  throw IException(IException::User, msg, _FILEINFO_);
79  }
80 
81  try {
83  new FileName(storedMatrixData.findKeyword("CovarianceMatrixFileName")[0]);
84  }
85  catch (IException &e) {
86  QString msg = "Could not find the Covariance Matrix .dat file name.";
87  throw IException(e, IException::User, msg, _FILEINFO_);
88  }
89 
90  try {
91  QString corrFileName = storedMatrixData.findKeyword("CorrelationMatrixFileName")[0];
92  if (corrFileName == "NULL") {
94  }
95  else {
96  m_correlationFileName = new FileName(corrFileName);
97  }
98  }
99  catch (IException &e) {
100  QString msg = "Could not find the Correlation Matrix .dat file name.";
101  throw IException(e, IException::User, msg, _FILEINFO_);
102  }
103 
104  try {
106  imgsIt = storedMatrixData.findGroup("ImagesAndParameters").begin();
107  while ( imgsIt != storedMatrixData.findGroup("ImagesAndParameters").end() ) {
108  QStringList params = (*imgsIt)[0].split(",");
109  m_imagesAndParameters->insert(imgsIt->name(), params);
110  imgsIt++;
111  }
112  }
113  catch (IException &e) {
114  QString msg = "Could not get Images and Parameters from ImagesAndParameters group.";
115  throw IException(e, IException::User, msg, _FILEINFO_);
116  }
117  }
118 
119 
129  m_diagonals = new QList<double>(*other.m_diagonals);
131  }
132 
133 
138  delete m_imagesAndParameters;
139  m_imagesAndParameters = NULL;
140 
141  delete m_covarianceFileName;
142  m_covarianceFileName = NULL;
143 
144  delete m_correlationFileName;
145  m_correlationFileName = NULL;
146 
147  delete m_diagonals;
148  m_diagonals = NULL;
149 
150  delete m_visibleBlocks;
151  m_visibleBlocks = NULL;
152  }
153 
154 
164 
165  if (&other != this) {
166 
167  delete m_imagesAndParameters;
168  m_imagesAndParameters = NULL;
170 
171  delete m_covarianceFileName;
172  m_covarianceFileName = NULL;
174 
175  delete m_correlationFileName;
176  m_correlationFileName = NULL;
178 
179  delete m_diagonals;
180  m_diagonals = NULL;
181  m_diagonals = new QList<double>(*other.m_diagonals);
182 
183  delete m_visibleBlocks;
184  m_visibleBlocks = NULL;
186 
187  }
188 
189  return *this;
190  }
191 
192 
204 
205  if ( !isValid() ) {
206  QString msg = "Cannot compute correlation matrix without a specified file name. Use "
207  "setCorrelationFileName(FileName) before calling computeCorrelationMatrix().";
208  throw IException(IException::Programmer, msg, _FILEINFO_);
209  }
210  delete m_visibleBlocks;
211  m_visibleBlocks = NULL;
213 
214  // Create file handle
215  QFile matrixInput( m_covarianceFileName->expanded() );
216  QFile matrixOutput( m_correlationFileName->expanded() );
217 
218  // Open file to write to
219  matrixInput.open(QIODevice::ReadOnly);
220  matrixOutput.open(QIODevice::WriteOnly);
221 
222  // Open Stream
223  QDataStream inStream(&matrixInput);
224  QDataStream outStream(&matrixOutput);
225 
226  double firstParam1 = 0, //starting param for each iteration
227  firstParam2 = 0;
228  double param1 = 0, // current param for each iteration
229  param2 = 0;
230  // Read one column at a time
232  while ( !inStream.atEnd() ) {
233  inStream >> sbcm;
234 
235  // Store diagonal
236  int numOfBlocks = sbcm.size();
237  int lastBlock = numOfBlocks - 1;
238  int numDiagonals = sbcm[lastBlock]->size1();
239 
240  // Get Diagonals
241  for (int i = 0; i < numDiagonals; i++) {
242  double val = ( *(sbcm[lastBlock]) )(i, i);
243  m_diagonals->append(val);
244  }
245 
246  // compute correlations
247  QMapIterator<int, LinearAlgebra::Matrix *> block(sbcm);
248 
249  while ( block.hasNext() ) { // each block in the column
250  block.next();
251  for (int row = 0; row < (int)block.value()->size1(); row++) { // each row in the block
252  for (int column = 0; column < (int)block.value()->size2(); column++) { // each column
253  // correlation = covariance / (variance1 * variance2)
254  ( *block.value() )(row, column) = ( *block.value() )(row, column) /
255  sqrt( (*m_diagonals)[param1] *
256  (*m_diagonals)[param2] );
257  param2++;
258  }
259  param1++;
260  param2 = firstParam2;
261  }
262  firstParam1 += block.value()->size1();
263  param1 = firstParam1;
264  }
265  firstParam1 = 0; // start each column at first element of diagonal list
266  param1 = firstParam1;
267  firstParam2 += block.value()->size2();
268  param2 = firstParam2;
269 
270  outStream << sbcm;
271  m_visibleBlocks->append(sbcm);
272  }
273 
274  // close file
275  matrixInput.close();
276  matrixOutput.close();
277  }
278 
279 
280 
290 
291 // if ( !correlationMatrixExists() ) {
292  // call computeCorrelationMatrix
293 // }
294  // read the values we want from the correlation matrix file.
295 
296  // store values by column?
297  // return list of values in m_visibleElements
298  }
299 
300 
301 
309 // QList<MatrixElement*> CorrelationMatrix::visibleElements() {
310 // return *m_visibleElements;
311 // }
312 
313 
314 
322 
323  return !(m_correlationFileName->name() == "" || m_covarianceFileName->name() == "");
324  }
325 
326 
336  return !(m_covarianceFileName->name() == "");
337  }
338 
339 
340  // Set Methods
346  if (m_correlationFileName == NULL) {
348  }
349  else {
351  }
352  }
353 
354 
360  if (m_covarianceFileName == NULL) {
362  }
363  else {
365  }
366  //Make the correlation matrix file name match the covariance matrix file name.
367  if (!isValid()) {
368  QString fName = covarianceFileName.expanded().replace( QString("inverse"),
369  QString("correlation") );
371  }
372  }
373 
374 
382  if (m_imagesAndParameters == NULL) {
384  }
385  else {
387  }
388  }
389 
390 
397  return *m_correlationFileName;
398  }
399 
400 
407  return *m_covarianceFileName;
408  }
409 
410 
418  return m_imagesAndParameters;
419  }
420 
421 
428 // SparseBlockColumnMatrix sbcm;
429 // QFile matrixInput( m_correlationFileName->expanded() );
430 // matrixInput.open(QIODevice::ReadOnly);
431 // QDataStream inStream(&matrixInput);
432 //
433 // while( !inStream.atEnd() ) {
434 // inStream >> sbcm;
435 // m_visibleBlocks->append(&sbcm);
436 // }
437  }
438 
439 
447  }
448 
449 
455  return m_visibleBlocks;
456  }
457 
458 
477  PvlObject corrMatInfo("CorrelationMatrixData");
478 
479  corrMatInfo += PvlKeyword( "CovarianceMatrixFileName", m_covarianceFileName->expanded() );
480  corrMatInfo += PvlKeyword( "CorrelationMatrixFileName", m_correlationFileName->expanded() );
481 
482  PvlGroup imgsAndParams("ImagesAndParameters");
483  QMapIterator<QString, QStringList> imgParamIt(*m_imagesAndParameters);
484  while ( imgParamIt.hasNext() ) {
485  imgParamIt.next();
486  imgsAndParams += PvlKeyword( imgParamIt.key(), imgParamIt.value().join(",") );
487  }
488  corrMatInfo += imgsAndParams;
489 
490  return corrMatInfo;
491  }
492 }
Isis::PvlObject::findGroup
PvlGroupIterator findGroup(const QString &name, PvlGroupIterator beg, PvlGroupIterator end)
Find a group with the specified name, within these indexes.
Definition: PvlObject.h:129
Isis::CorrelationMatrix::correlationFileName
FileName correlationFileName()
Public access for the correlation matrix file name.
Definition: CorrelationMatrix.cpp:396
Isis::PvlObject
Contains Pvl Groups and Pvl Objects.
Definition: PvlObject.h:61
Isis::SparseBlockColumnMatrix
SparseBlockColumnMatrix.
Definition: SparseBlockMatrix.h:58
Isis::CorrelationMatrix::~CorrelationMatrix
~CorrelationMatrix()
Destructor.
Definition: CorrelationMatrix.cpp:137
Isis::PvlKeyword
A single keyword-value pair.
Definition: PvlKeyword.h:82
QList
This is free and unencumbered software released into the public domain.
Definition: BoxcarCachingAlgorithm.h:13
Isis::FileName::name
QString name() const
Returns the name of the file excluding the path and the attributes in the file name.
Definition: FileName.cpp:162
Isis::CorrelationMatrix::m_imagesAndParameters
QMap< QString, QStringList > * m_imagesAndParameters
This map holds the images used to create this matrix and their associated parameters.
Definition: CorrelationMatrix.h:101
Isis::CorrelationMatrix::setImagesAndParameters
void setImagesAndParameters(QMap< QString, QStringList > imagesAndParameters)
Set the qmap of images and parameters.
Definition: CorrelationMatrix.cpp:381
Isis::CorrelationMatrix::CorrelationMatrix
CorrelationMatrix()
Default Constructor.
Definition: CorrelationMatrix.cpp:29
Isis::FileName
File name manipulation and expansion.
Definition: FileName.h:100
Isis::CorrelationMatrix::retrieveVisibleElements
void retrieveVisibleElements(int x, int y)
Extract requested area from correlation matrix This method will open the correlation matrix file and ...
Definition: CorrelationMatrix.cpp:289
Isis::CorrelationMatrix::covarianceFileName
FileName covarianceFileName()
Public access for the covariance matrix file name.
Definition: CorrelationMatrix.cpp:406
Isis::CorrelationMatrix::computeCorrelationMatrix
void computeCorrelationMatrix()
Read covariance matrix and compute correlation values This method reads the covariance matrix in from...
Definition: CorrelationMatrix.cpp:203
Isis::CorrelationMatrix::pvlObject
PvlObject pvlObject()
This method creates a Pvl group with the information necessary to recreate this correlation matrix.
Definition: CorrelationMatrix.cpp:476
QStringList
Isis::CorrelationMatrix
This is a container for the correlation matrix that comes from a bundle adjust.
Definition: CorrelationMatrix.h:61
Isis::FileName::expanded
QString expanded() const
Returns a QString of the full file name including the file path, excluding the attributes.
Definition: FileName.cpp:196
Isis::PvlGroup
Contains multiple PvlContainers.
Definition: PvlGroup.h:41
Isis::CorrelationMatrix::m_correlationFileName
FileName * m_correlationFileName
FileName of the correlation matrix.
Definition: CorrelationMatrix.h:107
Isis::CorrelationMatrix::retrieveThreeVisibleBlocks
void retrieveThreeVisibleBlocks()
Display only part of a matrix This method will be used when the matrix is too big to display the whol...
Definition: CorrelationMatrix.cpp:446
Isis::PvlContainer::name
QString name() const
Returns the container name.
Definition: PvlContainer.h:63
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::CorrelationMatrix::operator=
CorrelationMatrix & operator=(const CorrelationMatrix &other)
Equal Operator.
Definition: CorrelationMatrix.cpp:163
Isis::CorrelationMatrix::m_diagonals
QList< double > * m_diagonals
List of the parameter values.
Definition: CorrelationMatrix.h:113
Isis::IException::Programmer
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:146
Isis::CorrelationMatrix::setCovarianceFileName
void setCovarianceFileName(FileName covarianceFileName)
Set the qmap of images and parameters.
Definition: CorrelationMatrix.cpp:359
Isis::CorrelationMatrix::hasCovMat
bool hasCovMat()
Check if the correlation matrix has a covariance matrix This is used to make sure the covariance matr...
Definition: CorrelationMatrix.cpp:335
Isis::CorrelationMatrix::m_covarianceFileName
FileName * m_covarianceFileName
FileName of the covariance matrix calculated when the bundle was run.
Definition: CorrelationMatrix.h:104
Isis::PvlContainer::PvlKeywordIterator
QList< PvlKeyword >::iterator PvlKeywordIterator
The keyword iterator.
Definition: PvlContainer.h:157
QMap< QString, QStringList >
Isis::PvlObject::findKeyword
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:177
Isis::CorrelationMatrix::isValid
bool isValid()
This is the public accessor for the list of elements that should be displayed in the current view.
Definition: CorrelationMatrix.cpp:321
Isis::CorrelationMatrix::m_visibleBlocks
QList< SparseBlockColumnMatrix > * m_visibleBlocks
This will be the three blocks (or whole matrix depending on size) that apply to the given area.
Definition: CorrelationMatrix.h:119
Isis::CorrelationMatrix::visibleBlocks
QList< SparseBlockColumnMatrix > * visibleBlocks()
Get the visible part of the matrix.
Definition: CorrelationMatrix.cpp:454
Isis::CorrelationMatrix::imagesAndParameters
QMap< QString, QStringList > * imagesAndParameters()
Public access for the qmap of images and parameters.
Definition: CorrelationMatrix.cpp:417
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16
Isis::CorrelationMatrix::setCorrelationFileName
void setCorrelationFileName(FileName correlationFileName)
Set the qmap of images and parameters.
Definition: CorrelationMatrix.cpp:345
Isis::IException::User
@ User
A type of error that could only have occurred due to a mistake on the user's part (e....
Definition: IException.h:126
Isis::CorrelationMatrix::retrieveWholeMatrix
void retrieveWholeMatrix()
This method will read the matrix in from the file and hold on to the whole thing.
Definition: CorrelationMatrix.cpp:427