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
25namespace Isis {
32 m_visibleBlocks = new QList<SparseBlockColumnMatrix>();
33 m_imagesAndParameters = new QMap<QString, QStringList>();
34 m_diagonals = new QList<double>();
35 }
36
37
68 //m_imagesAndParameters = NULL;
69 m_imagesAndParameters = new QMap<QString, QStringList>();
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
126 m_imagesAndParameters = new QMap<QString, QStringList>(*other.m_imagesAndParameters);
127 m_covarianceFileName = new FileName(*other.m_covarianceFileName);
128 m_correlationFileName = new FileName(*other.m_correlationFileName);
129 m_diagonals = new QList<double>(*other.m_diagonals);
130 m_visibleBlocks = new QList<SparseBlockColumnMatrix>(*other.m_visibleBlocks);
131 }
132
133
140
143
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
169 m_imagesAndParameters = new QMap<QString, QStringList>(*other.m_imagesAndParameters);
170
173 m_covarianceFileName = new FileName(*other.m_covarianceFileName);
174
177 m_correlationFileName = new FileName(*other.m_correlationFileName);
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;
185 m_visibleBlocks = new QList<SparseBlockColumnMatrix>(*other.m_visibleBlocks);
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;
212 m_visibleBlocks = new QList<SparseBlockColumnMatrix>;
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
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
381 void CorrelationMatrix::setImagesAndParameters(QMap<QString, QStringList> imagesAndParameters) {
382 if (m_imagesAndParameters == NULL) {
383 m_imagesAndParameters = new QMap<QString, QStringList>(imagesAndParameters);
384 }
385 else {
387 }
388 }
389
390
399
400
409
410
417 QMap<QString, QStringList> *CorrelationMatrix::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
448
449
454 QList<SparseBlockColumnMatrix> *CorrelationMatrix::visibleBlocks() {
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}
This is a container for the correlation matrix that comes from a bundle adjust.
QList< double > * m_diagonals
List of the parameter values.
void retrieveWholeMatrix()
This method will read the matrix in from the file and hold on to the whole thing.
QMap< QString, QStringList > * imagesAndParameters()
Public access for the qmap of images and parameters.
void setCovarianceFileName(FileName covarianceFileName)
Set the qmap of images and parameters.
CorrelationMatrix & operator=(const CorrelationMatrix &other)
Equal Operator.
bool isValid()
This is the public accessor for the list of elements that should be displayed in the current view.
FileName covarianceFileName()
Public access for the covariance matrix file name.
FileName * m_covarianceFileName
FileName of the covariance matrix calculated when the bundle was run.
void setImagesAndParameters(QMap< QString, QStringList > imagesAndParameters)
Set the qmap of images and parameters.
FileName * m_correlationFileName
FileName of the correlation matrix.
CorrelationMatrix()
Default Constructor.
void retrieveVisibleElements(int x, int y)
Extract requested area from correlation matrix This method will open the correlation matrix file and ...
bool hasCovMat()
Check if the correlation matrix has a covariance matrix This is used to make sure the covariance matr...
QList< SparseBlockColumnMatrix > * visibleBlocks()
Get the visible part of the matrix.
QMap< QString, QStringList > * m_imagesAndParameters
This map holds the images used to create this matrix and their associated parameters.
void setCorrelationFileName(FileName correlationFileName)
Set the qmap of images and parameters.
PvlObject pvlObject()
This method creates a Pvl group with the information necessary to recreate this correlation matrix.
void computeCorrelationMatrix()
Read covariance matrix and compute correlation values This method reads the covariance matrix in from...
FileName correlationFileName()
Public access for the correlation matrix file name.
QList< SparseBlockColumnMatrix > * m_visibleBlocks
This will be the three blocks (or whole matrix depending on size) that apply to the given area.
void retrieveThreeVisibleBlocks()
Display only part of a matrix This method will be used when the matrix is too big to display the whol...
File name manipulation and expansion.
Definition FileName.h:100
QString name() const
Returns the name of the file excluding the path and the attributes in the file name.
Definition FileName.cpp:162
QString expanded() const
Returns a QString of the full file name including the file path, excluding the attributes.
Definition FileName.cpp:196
Isis exception class.
Definition IException.h:91
@ User
A type of error that could only have occurred due to a mistake on the user's part (e....
Definition IException.h:126
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition IException.h:146
QList< PvlKeyword >::iterator PvlKeywordIterator
The keyword iterator.
Contains multiple PvlContainers.
Definition PvlGroup.h:41
A single keyword-value pair.
Definition PvlKeyword.h:87
Contains Pvl Groups and Pvl Objects.
Definition PvlObject.h:61
SparseBlockColumnMatrix.
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16