Commit ea31fd2a authored by Tyler Wilson's avatar Tyler Wilson
Browse files

Fixed a problem with the CorrelationMatrix(PvlObject data) constructor giving...

Fixed a problem with the CorrelationMatrix(PvlObject data) constructor giving a segmentation fault when run in the unit test. Also updated documentation for many of the functions.  Fixes #3999.

git-svn-id: http://subversion.wr.usgs.gov/repos/prog/isis3/branches/ipce@6764 41f8697f-d340-4b68-9986-7bafba869bb8
parent bfa5d0b8
Loading
Loading
Loading
Loading
+71 −20
Original line number Diff line number Diff line
@@ -43,17 +43,30 @@ namespace Isis {
   *
   * End_Object
   *
   * @param storedMatrixData  A PvlObject containing data about the
   * covariance/correlation matrix.
   *
   * @throws IException::User "This Pvl Object does not have the correct correlation information.
   * The Object you are looking for is called CorrelationMatrixData"
   *
   * @throws IException::User "Could not find the Covariance Matrix .dat file name."
   *
   * @throws IException::User "Could not find the Correlation Matrix .dat file name."
   *
   * @throws IExceptoin::User"Could not get Images and Parameters from ImagesAndParameters group."
   *
   */
  CorrelationMatrix::CorrelationMatrix(PvlObject storedMatrixData) {
    m_imagesAndParameters = NULL;
    m_covarianceFileName = NULL;
    m_correlationFileName = NULL;
    //m_imagesAndParameters = NULL;
    m_imagesAndParameters = new QMap<QString, QStringList>();
    m_covarianceFileName = new FileName("");
    m_correlationFileName = new FileName("");
    m_diagonals = NULL;
    m_visibleBlocks = NULL;

    if (storedMatrixData.name() != "CorrelationMatrixData") {
      QString msg = "This Pvl Object does not have the correct correlation information. The Object "
                    "you are looking for is called CorrelationMatrixData";
                    "you are looking for is called CorrelationMatrixData.";
      throw IException(IException::User, msg, _FILEINFO_);
    }

@@ -81,10 +94,12 @@ namespace Isis {
    }

    try {
      PvlObject::PvlKeywordIterator imgsIt = storedMatrixData.findGroup("ImagesAndParameters").begin();
      PvlObject::PvlKeywordIterator
          imgsIt = storedMatrixData.findGroup("ImagesAndParameters").begin();
      while ( imgsIt != storedMatrixData.findGroup("ImagesAndParameters").end() ) {
        QStringList params = (*imgsIt)[0].split(",");
        m_imagesAndParameters->insert(imgsIt->name(), params);
        imgsIt++;
      }
    }
    catch (IException &e) {
@@ -170,11 +185,14 @@ namespace Isis {
  }



  /**
  * This mehtod reads the covariance matrix in from a file, one SparseBlockColumnMatrix at a time.
  * It then stores the diagonal values from that column and computes the correlation values. The
  * resulting SparseBlockMatrix is written to a new file, one SparseBlockColumnMatrix at a time.
  * @brief This mehtod reads the covariance matrix in from a file,
  * one SparseBlockColumnMatrix at a time.  It then stores the diagonal values from that column
  * and computes the correlation values. The resulting SparseBlockMatrix is written to a
  * new file, one SparseBlockColumnMatrix at a time.
  *
  * @throws IException::Progammer  "Cannot compute correlation matrix without a specified
  * file name.  Use setCorrelationFileName(FileName) before calling computeCorrelationMatrix()."
  */
  void CorrelationMatrix::computeCorrelationMatrix() {

@@ -288,13 +306,16 @@ namespace Isis {


  /**
   * See if the correlation matrix has already been calculated by checking to see if
   * @brief See if the correlation matrix has already been calculated by checking to see if
   *   the correlation matrix file has been created.
   *
   * @return true if the correlation matrix has already been set.
   */
  bool CorrelationMatrix::isValid() {

      return !(m_correlationFileName->name() == "" || m_covarianceFileName->name() == "");


  }


@@ -317,7 +338,7 @@ namespace Isis {
  /**
   * Set the qmap of images and parameters.
   *
   * @param
   * @param correlationFileName The FileName of the stored correlation matrix data.
   */
  void CorrelationMatrix::setCorrelationFileName(FileName correlationFileName) {
    if (m_correlationFileName == NULL) {
@@ -333,7 +354,7 @@ namespace Isis {
  /**
   *Set the qmap of images and parameters.
   *
   * @param
   * @param  covarianceFileName  The FileName of the stored covariance matrix data.
   */
  void CorrelationMatrix::setCovarianceFileName(FileName covarianceFileName) {
    if (m_covarianceFileName == NULL) {
@@ -353,9 +374,10 @@ namespace Isis {


  /**
   * Set the qmap of images and parameters.
   * @brief Set the qmap of images and parameters.
   *
   * @param
   * @param imagesAndParameters a QMap structure indexed by image keys, with an arbitrary set of
   * parameters for each image.
   */
  void CorrelationMatrix::setImagesAndParameters(QMap<QString, QStringList> imagesAndParameters) {
    if (m_imagesAndParameters == NULL) {
@@ -369,9 +391,9 @@ namespace Isis {


  /**
   * Public access for the correlation matrix file name.
   * @brief Public access for the correlation matrix file name.
   *
   * @return
   * @return A pointer to the FileName of the correlation matrix data file.
   */
  FileName CorrelationMatrix::correlationFileName() {
    return *m_correlationFileName;
@@ -382,7 +404,7 @@ namespace Isis {
  /**
   * Public access for the covariance matrix file name.
   *
   * @return
   * @return A pointer to the FileName of the covariance data file.
   */
  FileName CorrelationMatrix::covarianceFileName() {
    return *m_covarianceFileName;
@@ -393,7 +415,8 @@ namespace Isis {
  /**
   * Public access for the qmap of images and parameters.
   *
   * @return
   * @return  A pointer to the QMap containing a list of images (the keys) and their
   * associated parameter values.
   */
  QMap<QString, QStringList> *CorrelationMatrix::imagesAndParameters() {
    return m_imagesAndParameters;
@@ -431,7 +454,8 @@ namespace Isis {


  /**
   * Get the visible part of the matrix.
   * @brief Get the visible part of the matrix.
   * @return Returns a QList of the non-empty diagonal blocks of the correlation matrix.
   */
  QList<SparseBlockColumnMatrix> *CorrelationMatrix::visibleBlocks() {
    return m_visibleBlocks;
@@ -440,7 +464,7 @@ namespace Isis {


  /**
   * This method creates a pvl group with the information necessary to recreate this correlation
   * @brief This method creates a pvl group with the information necessary to recreate this correlation
   * matrix.
   *
   * Object = CorrelationMatrixData
@@ -473,6 +497,13 @@ namespace Isis {
    return corrMatInfo;
  }

  /**
   * @brief Writes CorrelationMatrix data to the output stream and returns this stream
   * to the user.
   * @param stream  The input stream containing the data.
   * @return The same stream but now containing the matrix data.
   */



  QDataStream &CorrelationMatrix::write(QDataStream &stream) const {
@@ -486,6 +517,14 @@ namespace Isis {
  }


  /**
   * @brief Reads CorrelationMatrix data from the input stream and places the data
   * in member variables.
   * @param stream  The input input stream containing the data.
   * @return Also the input stream which is returned back to the user should they need it.
   */



  QDataStream &CorrelationMatrix::read(QDataStream &stream) {
    // QMaps
@@ -524,12 +563,24 @@ namespace Isis {
    return stream;
  }

  /**
   * @brief operator <<
   * @param stream The output stream upon which the matrix data is written
   * @param matrix The CorrelationMatrix containing the data.
   * @return The output stream containing the matrix data.
   */


  QDataStream &operator<<(QDataStream &stream, const CorrelationMatrix &matrix) {
    return matrix.write(stream);
  }

  /**
   * @brief operator >>
   * @param stream  The input stream containing the CorrelationMatrix data.
   * @param matrix  The matrix which is going to be overwritten by the input stream.
   * @return  The output stream containing the matrix data.
   */


  QDataStream &operator>>(QDataStream &stream, CorrelationMatrix &matrix) {
+8 −0
Original line number Diff line number Diff line
@@ -38,6 +38,8 @@ namespace Isis {
  class MosaicSceneWidget;
  class PvlObject;
  class SparseBlockColumnMatrix;


  /**
   * @brief This is a container for the correlation matrix that comes from a bundle adjust
   *
@@ -56,6 +58,12 @@ namespace Isis {
   *                           copy constructor and operator= methods.
   *   @history 2015-10-14 Jeffrey Covington - Declared CorrelationMatrix as a
   *                           Qt metatype for use with QVariant.
   *   @history 2016-06-06 Tyler Wilson - Fixed a problem with a PvlKeywordIterator not
   *                           being incremented in the constructor which accepts a PvlObject.
   *                           There was also an issue with a QMap data structure not being
   *                           initialized, resulting in a segmentation fault.  Also added
   *                           testing for exceptions being thrown in this constructor,
   *                           as well as the function computeCorrelationMatrix. Fixes #3999.
   */
  class CorrelationMatrix {
    public:
+38 −0
Original line number Diff line number Diff line
@@ -3,6 +3,8 @@ Test Copy Constructor
Test Equal Operator ( = )
Does the matrix exist? false
Creating Covariance Matrix.
Does the Correlation Matrix have a covariance matrix?  false
Does the Correlation Matrix have a covariance matrix now that it has been set?  true
Covariance Matrix:
 New Block 
       1           2           
@@ -17,6 +19,9 @@ Covariance Matrix:
       

Testing Compute Correlation Matrix Method.
Tesing Exception in Compute Correlation Matrix Method
Exception 1:
"**PROGRAMMER ERROR** Cannot compute correlation matrix without a specified file name. Use setCorrelationFileName(FileName) before calling computeCorrelationMatrix()."
Correlation Matrix:
 New Block 
       1           1           
@@ -44,3 +49,36 @@ Images and Parameters:
	 "Image2"
		 "X"
		 "Y"
Testing pvlObject()
Testing constructor that takes a pvlObject

***Correlation Matrix Information***

Covariance File Name: "corrMat.dat"
Correlation File Name: "corrMat.dat"
Images and Parameters:
	 "Image1"
		 "X"
		 "Y"
	 "Image2"
		 "X"
		 "Y"
Testing CorrelationMatrix(PvlObject storedMatrixData)
Exception 1
"**USER ERROR** This Pvl Object does not have the correct correlation information. The Object you are looking for is called CorrelationMatrixData."
Exception 2
"**USER ERROR** Could not find the Covariance Matrix .dat file name.\n**ERROR** PVL Keyword [CovarianceMatrixFileName] does not exist in [Object = CorrelationMatrixData]."
Exception 3
"**USER ERROR** Could not find the Correlation Matrix .dat file name.\n**ERROR** PVL Keyword [CorrelationMatrixFileName] does not exist in [Object = CorrelationMatrixData]."
Exception 4
"**USER ERROR** Could not get Images and Parameters from ImagesAndParameters group.\n**ERROR** Unable to find PVL group [ImagesAndParameters]."
Object = CorrelationMatrixData
  CovarianceMatrixFileName  = covMat.dat
  CorrelationMatrixFileName = corrMat.dat

  Group = ImagesAndParameters
    Image1 = "X,Y"
    Image2 = "X,Y"
  End_Group
End_Object
End
 No newline at end of file
+101 −32
Original line number Diff line number Diff line
@@ -51,7 +51,11 @@ int main() {
  FileName covFile = "covMat.dat";

  corrMat.setCorrelationFileName(corrFile);
  qDebug() << "Does the Correlation Matrix have a covariance matrix? "  <<  corrMat.hasCovMat();
  corrMat.setCovarianceFileName(covFile);
  qDebug() << "Does the Correlation Matrix have a covariance matrix now that it has been set? "
           <<  corrMat.hasCovMat();


  // Make Dummy Covariance Matrix
  SparseBlockMatrix tmpMat;
@@ -85,6 +89,17 @@ int main() {

  qDebug() << "Testing Compute Correlation Matrix Method.";
  corrMat.computeCorrelationMatrix();
  qDebug() << "Tesing Exception in Compute Correlation Matrix Method";

  CorrelationMatrix  cm;
  try {
    cm.computeCorrelationMatrix();
  }
  catch (IException &e) {
    qDebug() << "Exception 1:";
    qDebug() << e.toString();

  }

  qDebug() << "Correlation Matrix:";
  foreach( SparseBlockColumnMatrix sbcm, *corrMat.visibleBlocks() ) {
@@ -120,38 +135,92 @@ int main() {
    }
  }

//   qDebug() << "Testing pvlObject()";
// 
//   PvlObject corrMatObject( corrMat.pvlObject() );
//   Pvl tester;
//   tester += corrMatObject;
//   cout << tester;
// 
//   qDebug() << "Testing constructor that takes a pvlObject";
// 
//   try {
//     CorrelationMatrix corrMat4(corrMatObject);
// 
//     qDebug() << "";
//     qDebug() << "***Correlation Matrix Information***";
//     qDebug() << "";
//     qDebug() << "Covariance File Name:" << corrMat4.correlationFileName().expanded();
//     qDebug() << "Correlation File Name:" << corrMat4.correlationFileName().expanded();
//     qDebug() << "Images and Parameters:";
//   //   QMapIterator<QString, QStringList> imgIt( *corrMat4.imagesAndParameters() );
//     imgIt.toFront();
//     while ( imgIt.hasNext() ) {
//       imgIt.next();
//       qDebug() << "\t" << imgIt.key();
//       foreach ( QString param, imgIt.value() ) {
//         qDebug() << "\t\t" << param;
//       }
//     }
//   }
//   catch(IException &e) {
//     QString msg = "Failed to create object using CorrelationMatrix(PvlObject).";
//     throw(e, IException::Programmer, msg, _FILEINFO_);
//   }
   qDebug() << "Testing pvlObject()";

   PvlObject corrMatObject( corrMat.pvlObject() );
   Pvl tester;
   tester += corrMatObject;
   cout << tester;

   qDebug() << "Testing constructor that takes a pvlObject";

   try {
     CorrelationMatrix corrMat4(corrMatObject);

     qDebug() << "";
     qDebug() << "***Correlation Matrix Information***";
     qDebug() << "";
     qDebug() << "Covariance File Name:" << corrMat4.correlationFileName().expanded();
     qDebug() << "Correlation File Name:" << corrMat4.correlationFileName().expanded();
     qDebug() << "Images and Parameters:";
       QMapIterator<QString, QStringList> imgIt( *corrMat4.imagesAndParameters() );
     imgIt.toFront();
     while ( imgIt.hasNext() ) {
       imgIt.next();
       qDebug() << "\t" << imgIt.key();
       foreach ( QString param, imgIt.value() ) {
         qDebug() << "\t\t" << param;
       }
     }
   }
   catch(IException &e) {
     QString msg = "Failed to create object using CorrelationMatrix(PvlObject).";
     throw(e, IException::Programmer, msg, _FILEINFO_);
   }


   qDebug() << "Testing CorrelationMatrix(PvlObject storedMatrixData)";


   PvlObject exception1("EmptyObject");
   PvlObject exception2("CorrelationMatrixData");


   PvlObject exception3("CorrelationMatrixData");
   exception3 += PvlKeyword("CovarianceMatrixFileName","CovMatFileName");

   PvlObject exception4("CorrelationMatrixData");
   exception4 += PvlKeyword("CovarianceMatrixFileName","CovMatFileName");
   exception4 += PvlKeyword("CorrelationMatrixFileName","CorrMatFileName");


   try {
      CorrelationMatrix except1(exception1);
   }

   catch(IException &e) {
     qDebug() << "Exception 1";
     qDebug() << e.toString();
   }

   try {
      CorrelationMatrix except2(exception2);
   }

   catch(IException &e) {
     qDebug() << "Exception 2";
     qDebug() << e.toString();
   }

   try {
      CorrelationMatrix except3(exception3);
   }

   catch(IException &e) {
     qDebug() << "Exception 3";
     qDebug() << e.toString();
   }

   try {
      CorrelationMatrix except4(exception4);
   }

   catch(IException &e) {
     qDebug() << "Exception 4";
     qDebug() << e.toString();
   }


    
  //delete mat files
  covQFile.remove();