Example
Block Operations in HDF5

Demonstrates slicing and other block operations for reading and writing parts of a dataset stored in an HDF5 file.


#include <MUQ/Utilities/HDF5/H5Object.h>


int main()
{
    // Open the HDF5 file for reading and writing
    auto f = muq::Utilities::OpenFile("Data.h5");
        
    // Store a vector in the group
    Eigen::VectorXd baseVector = Eigen::VectorXd::LinSpaced(11,0,10);
    f["/Vector"] = baseVector;

    // Now, extract the first two components of the dataset
    Eigen::VectorXd tempVector = f["/Vector"].head(2);

    std::cout << tempVector.transpose() << "\nvs\n"
	      << baseVector.head(2).transpose() << std::endl << std::endl;
    
    // Extract a length 3 segment from the middle of the dataset, staring at index 2.
    tempVector = f["/Vector"].segment(2,3);

    std::cout << tempVector.transpose() << "\nvs\n"
	      << baseVector.segment(2,3).transpose() << std::endl << std::endl;

    // Insert a vector into the middle of the dataset
    std::cout << "Old vector = "
	      << f["/Vector"].eval().transpose() << std::endl;
    
    f["/Vector"].segment(3,2) = Eigen::VectorXd::Zero(2).eval();
    
    std::cout << "New vector = "
	      << f["/Vector"].eval().transpose() << std::endl << std::endl;


    // Create a matrix dataset from the outer product of the [0,1,2...] vector
    f["/Matrix"] = (baseVector*baseVector.transpose()).eval();

    // Grab a block in the middle of matrix
    std::cout << "4x5 matrix block = \n" << f["/Matrix"].block(2,3,4,5).eval() << std::endl;

    // It is also possible to use all of the other Eigen block operations (e.g., .col(), .row(), .bottomLeftCorner(), .topRows(), .leftCols(), etc....)
    
    return 0;
}

Complete Code

#include <MUQ/Utilities/HDF5/H5Object.h>


int main()
{
    // Open the HDF5 file for reading and writing
    auto f = muq::Utilities::OpenFile("Data.h5");
        
    // Store a vector in the group
    Eigen::VectorXd baseVector = Eigen::VectorXd::LinSpaced(11,0,10);
    f["/Vector"] = baseVector;

    // Now, extract the first two components of the dataset
    Eigen::VectorXd tempVector = f["/Vector"].head(2);

    std::cout << tempVector.transpose() << "\nvs\n"
	      << baseVector.head(2).transpose() << std::endl << std::endl;
    
    // Extract a length 3 segment from the middle of the dataset, staring at index 2.
    tempVector = f["/Vector"].segment(2,3);

    std::cout << tempVector.transpose() << "\nvs\n"
	      << baseVector.segment(2,3).transpose() << std::endl << std::endl;

    // Insert a vector into the middle of the dataset
    std::cout << "Old vector = "
	      << f["/Vector"].eval().transpose() << std::endl;
    
    f["/Vector"].segment(3,2) = Eigen::VectorXd::Zero(2).eval();
    
    std::cout << "New vector = "
	      << f["/Vector"].eval().transpose() << std::endl << std::endl;


    // Create a matrix dataset from the outer product of the [0,1,2...] vector
    f["/Matrix"] = (baseVector*baseVector.transpose()).eval();

    // Grab a block in the middle of matrix
    std::cout << "4x5 matrix block = \n" << f["/Matrix"].block(2,3,4,5).eval() << std::endl;

    // It is also possible to use all of the other Eigen block operations (e.g., .col(), .row(), .bottomLeftCorner(), .topRows(), .leftCols(), etc....)
    
    return 0;
}
Slack LOGO MUQ is on Slack!
  • Join our slack workspace to connect with other users, get help, and discuss new features.
Test Status
Acknowledgments

NSF Logo

This material is based upon work supported by the National Science Foundation under Grant No. 1550487.

Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.

DOE Logo

This material is based upon work supported by the US Department of Energy, Office of Advanced Scientific Computing Research, SciDAC (Scientific Discovery through Advanced Computing) program under awards DE-SC0007099 and DE-SC0021226, for the QUEST and FASTMath SciDAC Institutes.