Isis 3 Programmer Reference
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 {
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;
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;
178 
179  }
180 
181  return *this;
182  }
183 
184 
196 
197  if ( !isValid() ) {
198  QString msg = "Cannot compute correlation matrix without a specified file name. Use "
199  "setCorrelationFileName(FileName) before calling computeCorrelationMatrix().";
201  }
202  delete m_visibleBlocks;
203  m_visibleBlocks = NULL;
205 
206  // Create file handle
207  QFile matrixInput( m_covarianceFileName->expanded() );
208  QFile matrixOutput( m_correlationFileName->expanded() );
209 
210  // Open file to write to
211  matrixInput.open(QIODevice::ReadOnly);
212  matrixOutput.open(QIODevice::WriteOnly);
213 
214  // Open Stream
215  QDataStream inStream(&matrixInput);
216  QDataStream outStream(&matrixOutput);
217 
218  double firstParam1 = 0, //starting param for each iteration
219  firstParam2 = 0;
220  double param1 = 0, // current param for each iteration
221  param2 = 0;
222  // Read one column at a time
224  while ( !inStream.atEnd() ) {
225  inStream >> sbcm;
226 
227  // Store diagonal
228  int numOfBlocks = sbcm.size();
229  int lastBlock = numOfBlocks - 1;
230  int numDiagonals = sbcm[lastBlock]->size1();
231 
232  // Get Diagonals
233  for (int i = 0; i < numDiagonals; i++) {
234  double val = ( *(sbcm[lastBlock]) )(i, i);
235  m_diagonals->append(val);
236  }
237 
238  // compute correlations
239  QMapIterator<int, LinearAlgebra::Matrix *> block(sbcm);
240 
241  while ( block.hasNext() ) { // each block in the column
242  block.next();
243  for (int row = 0; row < (int)block.value()->size1(); row++) { // each row in the block
244  for (int column = 0; column < (int)block.value()->size2(); column++) { // each column
245  // correlation = covariance / (variance1 * variance2)
246  ( *block.value() )(row, column) = ( *block.value() )(row, column) /
247  sqrt( (*m_diagonals)[param1] *
248  (*m_diagonals)[param2] );
249  param2++;
250  }
251  param1++;
252  param2 = firstParam2;
253  }
254  firstParam1 += block.value()->size1();
255  param1 = firstParam1;
256  }
257  firstParam1 = 0; // start each column at first element of diagonal list
258  param1 = firstParam1;
259  firstParam2 += block.value()->size2();
260  param2 = firstParam2;
261 
262  outStream << sbcm;
263  m_visibleBlocks->append(sbcm);
264  }
265 
266  // close file
267  matrixInput.close();
268  matrixOutput.close();
269  }
270 
271 
272 
282 
283 // if ( !correlationMatrixExists() ) {
284  // call computeCorrelationMatrix
285 // }
286  // read the values we want from the correlation matrix file.
287 
288  // store values by column?
289  // return list of values in m_visibleElements
290  }
291 
292 
293 
301 // QList<MatrixElement*> CorrelationMatrix::visibleElements() {
302 // return *m_visibleElements;
303 // }
304 
305 
306 
314 
315  return !(m_correlationFileName->name() == "" || m_covarianceFileName->name() == "");
316  }
317 
318 
328  return !(m_covarianceFileName->name() == "");
329  }
330 
331 
332  // Set Methods
338  if (m_correlationFileName == NULL) {
340  }
341  else {
343  }
344  }
345 
346 
352  if (m_covarianceFileName == NULL) {
354  }
355  else {
357  }
358  //Make the correlation matrix file name match the covariance matrix file name.
359  if (!isValid()) {
360  QString fName = covarianceFileName.expanded().replace( QString("inverse"),
361  QString("correlation") );
363  }
364  }
365 
366 
374  if (m_imagesAndParameters == NULL) {
376  }
377  else {
379  }
380  }
381 
382 
389  return *m_correlationFileName;
390  }
391 
392 
399  return *m_covarianceFileName;
400  }
401 
402 
410  return m_imagesAndParameters;
411  }
412 
413 
420 // SparseBlockColumnMatrix sbcm;
421 // QFile matrixInput( m_correlationFileName->expanded() );
422 // matrixInput.open(QIODevice::ReadOnly);
423 // QDataStream inStream(&matrixInput);
424 //
425 // while( !inStream.atEnd() ) {
426 // inStream >> sbcm;
427 // m_visibleBlocks->append(&sbcm);
428 // }
429  }
430 
431 
439  }
440 
441 
447  return m_visibleBlocks;
448  }
449 
450 
469  PvlObject corrMatInfo("CorrelationMatrixData");
470 
471  corrMatInfo += PvlKeyword( "CovarianceMatrixFileName", m_covarianceFileName->expanded() );
472  corrMatInfo += PvlKeyword( "CorrelationMatrixFileName", m_correlationFileName->expanded() );
473 
474  PvlGroup imgsAndParams("ImagesAndParameters");
475  QMapIterator<QString, QStringList> imgParamIt(*m_imagesAndParameters);
476  while ( imgParamIt.hasNext() ) {
477  imgParamIt.next();
478  imgsAndParams += PvlKeyword( imgParamIt.key(), imgParamIt.value().join(",") );
479  }
480  corrMatInfo += imgsAndParams;
481 
482  return corrMatInfo;
483  }
484 }
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)
Extract requested area from correlation matrix This method will open the correlation matrix file and ...
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:116
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()
Read covariance matrix and compute correlation values This method reads the covariance matrix in from...
void setImagesAndParameters(QMap< QString, QStringList > imagesAndParameters)
Set the qmap of images and parameters.
QString name() const
Returns the name of the file excluding the path and the attributes in the file name.
Definition: FileName.cpp:178
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:162
Unless noted otherwise, the portions of Isis written by the USGS are public domain.
QString name() const
Returns the container name.
Definition: PvlContainer.h:77
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:40
void retrieveThreeVisibleBlocks()
Display only part of a matrix This method will be used when the matrix is too big to display the whol...
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:142
A single keyword-value pair.
Definition: PvlKeyword.h:98
QString expanded() const
Returns a QString of the full file name including the file path, excluding the attributes.
Definition: FileName.cpp:212
bool hasCovMat()
Check if the correlation matrix has a covariance matrix This is used to make sure the covariance matr...
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:107
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
QList< SparseBlockColumnMatrix > * visibleBlocks()
Get the visible part of the matrix.
CorrelationMatrix & operator=(const CorrelationMatrix &other)
Equal Operator.
Contains Pvl Groups and Pvl Objects.
Definition: PvlObject.h:74
QMap< QString, QStringList > * imagesAndParameters()
Public access for the qmap of images and parameters.
QList< PvlKeyword >::iterator PvlKeywordIterator
The keyword iterator.
Definition: PvlContainer.h:171