hdfview icon indicating copy to clipboard operation
hdfview copied to clipboard

Shape oppositely interpreted?

Open olafx opened this issue 3 years ago • 1 comments

I'm creating a 2 by 4 by 3 tensor in Python, verifying in C++ that it's indeed a 2 by 4 by 3 row major tensor where 3 is the fastest dimension. When I open this tensor in HDFView, I see 3 slides of 2 by 4 matrices, which implies the 3 dimension is the slowest. I used to think that it was just the visualization that was odd, but now I think it's a bug. Here are some programs I used to generate the data, maybe useful for testing purposes.

import h5py
import numpy as np

fp = h5py.File('0.h5', 'w')
data = np.array([[[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]], [[5, 5, 5], [6, 6, 6], [7, 7, 7], [8, 8, 8]]], dtype=np.float64)
print(data)
print(data.shape) # 2, 4, 3
fp.create_dataset('0', data=data.flatten(), shape=(2, 4, 3))
# fp.create_dataset('0', data=data) # same thing
fp.close()
#include <H5Cpp.h>
#include <iostream>
#include <cstddef>

using namespace H5;

int main()
{
    H5File fp {"../0.h5", H5F_ACC_RDWR};
    DataSet dataset {fp.openDataSet("0")};
    DataSpace dataspace {dataset.getSpace()};
    auto *raw_data = new double[24];
    dataset.read(raw_data, PredType::NATIVE_DOUBLE);
    hsize_t shape[3];
    dataspace.getSimpleExtentDims(shape);
    //  Let's access the z component of the 2nd particle at the 2nd time. Should be 6 if row major.
    std::size_t index = 4*3*(2-1) /* 2nd time */ + 3*(2-1) /* 2nd particle */ + (3-1) /* z component*/;
    std::cout << shape[0]    << ' ' << shape[1] << ' ' << shape[2] << '\n'; // 2 4 3
    std::cout << raw_data[index] << '\n'; // 6
    delete[] raw_data;
}

olafx avatar Nov 24 '21 13:11 olafx

HDFView does not try to interpret the data, unless the data indicates an Image convention, and will choose to assign the frame to the index value assigned to the 3rd (selectedIndex[2]) index. The selected indices defaults are assigned by the type of object and the assignment is: // select dim1 and dim2 as 2D data for display, and slice through dim0 selectedIndex[0] = 1; selectedIndex[1] = 2; selectedIndex[1] = 0;

This can be changed by opening the table with the "Show Data with Options" button.

byrnHDF avatar Jan 16 '22 17:01 byrnHDF