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

Numpy strings cannot be loaded #88

Open
moritzlayer opened this issue May 12, 2021 · 0 comments
Open

Numpy strings cannot be loaded #88

moritzlayer opened this issue May 12, 2021 · 0 comments

Comments

@moritzlayer
Copy link

Hi, I just came across the following issue:

If you have numpy strings as keys of a dictionary and you save this dictionary, the h5py_wrapper raises the error

ValueError: malformed node or string: 
    <ast.Name object at 0x7fcfa1870970>

when trying to load the file. This can be reproduced using the following test (using pytest):

import pytest
import numpy as np
import h5py_wrapper as h5

from unittest import TestCase


def test_saving_and_consecutive_loading_of_numpy_string_keys(tmpdir):
    file = 'test.h5'
    keys = ['a', 'b', 'c']
    
    # this makes the keys numpy strings
    keys = np.atleast_1d(keys)
    output = {key: i for i, key in enumerate(keys)}

    # create temporary directory to save test file into
    tmp_test = tmpdir.mkdir('tmp_test')
    with tmp_test.as_cwd():
        h5.save(file, output)
        input = h5.load(file)
        TestCase().assertDictEqual(output, input)

The problem seems to be line 296 in wrapper.py where key_type is compared to ['str', 'unicode', 'string_'], but a numpy string leads to key_type = 'str_'.

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

1 participant