Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to write enum data type into a dataset? #276

Open
neroarth94 opened this issue Feb 21, 2023 · 7 comments
Open

How to write enum data type into a dataset? #276

neroarth94 opened this issue Feb 21, 2023 · 7 comments

Comments

@neroarth94
Copy link

neroarth94 commented Feb 21, 2023

I am trying to write a enum type into the dataset but is unable to. It always converts it to an int type. May I know how to insert into the dataset as an enum type?

example:
image

what i got instead:
image

what i have tried:

public enum AnEnum
{
    START,
    PAUSE,
    STOP
}
        
Hdf5.WriteDataset(groupId, "AnEnumDataset", new List<AnEnum>() { AnEnum.START}.ToArray());
Hdf5.WriteObject(groupId, new List<AnEnum>() { AnEnum.START}.ToArray(), "AnEnumDataset");
Hdf5.WriteOneValue(groupId, "AnEnumDataset", AnEnum.START, new Dictionary<string, List<string>>());
@flappah
Copy link

flappah commented Jun 21, 2023

That's not possible right now but I ran into a similar issue and circumvented the issue by going to the underlying P/Invoke library. I've included one of the enumerations I'm using to write the data type to HDF5.

public class CommonPointRuleEnum : CustomEnumBase
    {
        private Dictionary<byte, string> _values = new Dictionary<byte, string>()
        {
            { 1, "average" },
            { 2, "low" },
            { 3, "high" },
            { 4, "all" }
        };

        private byte _current;

        public CommonPointRuleEnum()
        {

        }

        public CommonPointRuleEnum(byte current)
        {
            if (_values.ContainsKey(current))
            {
                this._current = current;
            }
            else
            {
                throw new InvalidDataException($"{current} is an invalid value!");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="groupId"></param>
        /// <param name="current"></param>
        public override void WriteToHdf5(long groupId, byte current)
        {
            if (_values.ContainsKey(current))
            {
                _current = current;
                WriteToHdf5(groupId);
            }
            else
            {
                throw new InvalidDataException($"{current} is an invalid value!");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="groupId"></param>
        public override void WriteToHdf5(long groupId)
        {
            long enum_id = H5T.enum_create(H5T.NATIVE_UINT8);

            var currentHandle = GCHandle.Alloc(_current, GCHandleType.Pinned);
            var handles = new List<GCHandle>();
            foreach (KeyValuePair<byte, string> item in _values)
            {
                var handle = GCHandle.Alloc(item.Key, GCHandleType.Pinned);
                H5T.enum_insert(enum_id, item.Value, handle.AddrOfPinnedObject());
                handles.Add(handle);
            }

            try
            {
                var space_id = H5S.create(H5S.class_t.SCALAR);
                var acpl = H5P.create(H5P.ATTRIBUTE_CREATE);
                H5P.set_char_encoding(acpl, H5T.cset_t.UTF8);

                var attr_id = H5A.create(groupId, "commonPointRule", enum_id, space_id, acpl, H5P.DEFAULT);
                H5A.write(attr_id, enum_id, currentHandle.AddrOfPinnedObject());
                H5A.close(attr_id);
            }
            finally
            {
                foreach (GCHandle handle in handles)
                {
                    handle.Free();
                }
                currentHandle.Free();
            }
        }
    }

Call it with:

var cprEnum = new CommonPointRuleEnum(1);
cprEnum.WriteToHdf5(hdf5GroupId);

@LiorBanai
Copy link
Owner

@flappah can't we wrap your implementation at my library so the user will have simplified Api to work with?

@flappah
Copy link

flappah commented Jun 25, 2023

Sure! I think we need to generalize my approach a bit but yeah. We could definitely add it to your library!

@flappah
Copy link

flappah commented Jun 26, 2023

Do you want me to generalize the code or do you want to do it yourself? I'm quite short in time this week since I've got a deadline coming up at the end of the week and next week I'm a week off but I could take a look at it in say a week or two.

@LiorBanai
Copy link
Owner

@flappah no pressure. If you would like to take a shoot on it it would be great. if not also ok :)
whatever you prefer

@flappah
Copy link

flappah commented Aug 17, 2023

Good morning @LiorBanai. I still haven't gotten around adding my approach. It's been quite busy lately. But I'm planning on doing this as soon as I can get my hands free. Hope this'll be somewhere at the end of September now.

@LiorBanai
Copy link
Owner

Take your time. We are all busy those days 🙂

Thanks for the update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants