Can't able to read the data from .h5 file

Hi Team,

We got a requirement to write and read the data into .h5 files by using Compound data type.
And i’m trying this in Windows Application for this we have taken two button click events i.e Insert to Write the data and Select to read the data.

We are using following code to Write the data to .h5 file and which is working fine and file size is 8 KB. (No issues in Writing the data).

private void btn_insert_Click(object sender, RoutedEventArgs e)
{
Data[] values = new Data[100];

        for (int i = 0; i < 100; i++)
        {
            values[i].Age = i;
            values[i].Weight = Convert.ToDouble(i);
        }

        HDFql.Execute("CREATE FILE paint8.h5");

        HDFql.Execute("USE FILE paint8.h5");

        HDFql.Execute("CREATE GROUP picasso ORDER TRACKED");

        HDFql.Execute("CREATE DATASET picasso/guernica AS COMPOUND(age AS UNSIGNED INT, weight AS FLOAT)(100)");

        HDFql.Execute("INSERT INTO picasso/guernica VALUES FROM MEMORY " + HDFql.VariableTransientRegister(values));

        HDFql.Execute("CREATE ATTRIBUTE picasso/guernica/subject AS  VARCHAR(2) VALUES(\"guerra civil española\")");

        HDFql.Execute("CLOSE FILE");
  
    }

And We are trying to Read the Same data from .h5 file but we can’t able to read it.
For this we are using following code,

private void btn_select_Click(object sender, RoutedEventArgs e)
{

        Data[] readvalues = new Data[100];

        HDFql.Execute("USE FILE paint8.h5");

        HDFql.Execute("SELECT FROM picasso/guernica INTO MEMORY" + HDFql.VariableTransientRegister(readvalues));

        HDFql.Execute("CLOSE FILE");

    }

Please kindly help us to read the data from .h5 file.

Thanks in advance
Leelakrishna

Hi @leelakrishna.k,

Looking at the code snippet you have posted, it seems that member Weight (belonging to struct Data) is of type double (64 bit) while the corresponding member of HDF5 compound dataset guernica is of type float (32 bit). Therefore, you need to change this corresponding member to type double. In other words:

HDFql.Execute("CREATE DATASET picasso/guernica AS COMPOUND(age AS UNSIGNED INT, weight AS DOUBLE)(100)");

Alternatively, if you wish member Weight (belonging to struct Data) to be a float (32 bit), you need to 1) declare it as such type and 2) change the line that converts variable i and assigns the result to Weight as follows:

values[i].Weight = Convert.ToSingle(i);

Hope it helps!

Thank you team,

But we can’t able to read the data from the .h5 file paint8.h5 We are using the following code.

private void btn_select_Click(object sender, RoutedEventArgs e)
{

        Data[] readvalues = new Data[100];

        HDFql.Execute("USE FILE paint8.h5");

        HDFql.Execute("SELECT FROM picasso/guernica INTO MEMORY" + HDFql.VariableTransientRegister(readvalues));

        HDFql.Execute("CLOSE FILE");
        
    }

Please help us in the solving the issue.

Thanks in advance

Hi again @leelakrishna.k,

There is another issue with the code you posted, namely the lack of a space between keyword MEMORY and the result of calling method VariableTransientRegister. To fix the issue have the following instead:

HDFql.Execute("SELECT FROM picasso/guernica INTO MEMORY " + HDFql.VariableTransientRegister(readvalues));

As a pro-tip, to have your code as simple as possible, you may want to replace the following lines:

HDFql.Execute("USE FILE paint8.h5");

HDFql.Execute("SELECT FROM picasso/guernica INTO MEMORY " + HDFql.VariableTransientRegister(readvalues));

HDFql.Execute("CLOSE FILE");

with only this one (functionally equivalent):

HDFql.Execute("SELECT FROM paint8.h5 picasso/guernica INTO MEMORY " + HDFql.VariableTransientRegister(readvalues));

Hope it helps!

Thank you much for the Solution.

But there is still a issue we are able to read the data only from 0-49 out of 100 values (able to read only the half data) and from 50-99 (remaining half) we can’t able to see any data there.

The Screenshot is attached below for the refertence.

Please requesting you to help us in reading the Complete data i.e from (0-100) and let us know if we are missing anything.

Thanks,
Leelakrishna

Hi @leelakrishna.k,

Would you mind to check with, e.g., HDFView dataset guernica and see if it stores all the expected values? Most likely the dataset is not storing all the values. Thanks!

Hi Team,

Yes we already checked with dataset after writing the data to .h5 file. It is storing all the values[0-100]
and we need to read all the values[0-100] however we are able to read values only from[0-49] which i sent the screenshot of it in previous thread.

Attaching the Screenshot below in which we are able to store all the values from[0-100] (in dataset).

Please kindly help us in acheiving the solution (to read all the values that are stored in dataset).

Thanks in advance

Hi @leelakrishna.k,

Would you mind to share file paint8.h5? It will help better understand the issue. Thanks!

Yes , i’m attaching the paint8.h5 file below.Before that,

Also i would like to mention that in the dataset ‘guernica’ we are not able to see the data what are Writing in it i.e [0-100]. We are able to see some weird data when we open it with HDFView and why it’s happening don’t know really.

And adding the screenshot below for reference.

image

Please find the paint8.h5 file below i.e which you asked to share.

paint8.h5 (7.6 KB)

Please kindly let us know if there are any changes to acheive the solution.

Thank you !

Hi @leelakrishna.k,

Thanks for sharing file paint8.h5 - it looks good!

We were able to read all the values stored in dataset guernica using HDFql with the following C# code:

using AS.HDFql;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, Pack = 0)]
struct Data
{
    public uint Age;
    public float Weight;
}

public class Example
{
    public static void Main(string []args)
    {
        Data[] values = new Data[100];
	
        HDFql.Execute("SELECT FROM paint8.h5 picasso/guernica INTO MEMORY " + HDFql.VariableTransientRegister(values));

        for(int i = 0; i < 100; i++)
        {
            System.Console.WriteLine("Row=" + i + " * Age=" + values[i].Age + " * Weight=" + values[i].Weight);
        }
    }
}

Could it be that you have more than one file named paint8.h5 and your C# code is picking a different file from the one you have shared? In addition, would you mind to share your C# struct Data? Thanks!

Hello Team,

Still we facing the same issue.Can’t able to read all the values after trying it with your code as well.

Below is C# struct Data,

image

Thanks,
Leelakrishna

Hi @leelakrishna.k,

In a previous post, it was mentioned that member Weight should be of type float. The struct you have posted still has this member as of type double. In addition, please notice that the struct must be packed - see the example posted above to have an idea about this.

Hope it helps!

Hello Team,

Thank you much for the Solution it really helped us and working fine without any issues.

Thanks & Regards,
Leelakrishna

1 Like

Great to know that it works perfectly fine now!

1 Like