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

[Compound] Colons with numeric labels in wrong order #1765

Open
DrizzleatDusk opened this issue Mar 5, 2025 · 1 comment
Open

[Compound] Colons with numeric labels in wrong order #1765

DrizzleatDusk opened this issue Mar 5, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@DrizzleatDusk
Copy link

Describe the bug

I find a disarrangement of the columns when I want to display a structured numpy array.

To Reproduce

The bug can be reproduced by the following python code.

# create a structured array.
dtype = np.dtype(
    {"names": ['1', '0', '2'], "formats": [np.float64, np.float64, np.float64]}
)
data = np.array([(1.0, 2.0, 3.0), (4.0, 5.0, 6.0)], dtype=dtype)

# save.
file = h5py.File("./test.h5", 'w')
file.create_dataset("a", data=data, dtype=dtype)
file.close()

Expected behaviour

We can read from the file as follows:

file = h5py.File("./test.h5", 'r')
print(file["a"]["0"])     #[2. 5.]
print(file["a"]["1"])     #[1. 4.]
print(file["a"]["2"])     #[3. 6.]
file.close()

Screenshots

Image

There is no problem using non numeric string. But in the above example, it seems that the columns are rearranged according to the values of the array index, which I think is a confusing behavior.

Context

I run the above example in the vscode plugin. (h5web.vscode-h5web v0.1.7)

@DrizzleatDusk DrizzleatDusk added the bug Something isn't working label Mar 5, 2025
@loichuder
Copy link
Member

loichuder commented Mar 5, 2025

Thanks for the report, there is indeed something fishy there.

I changed slightly the formats to have different dtypes for each field

dtype = np.dtype(
    {"names": ["1", "0", "2"], "formats": [np.float64, np.int32, np.uint8]}
)

and it seems that these are parsed properly by h5wasm but the order changed:

  "type": {
    "class": "Compound",
    "fields": {
      "0": {
        "class": "Integer",
        "endianness": "little-endian",
        "size": 32
      },
      "1": {
        "class": "Float",
        "endianness": "little-endian",
        "size": 64
      },
      "2": {
        "class": "Integer (unsigned)",
        "endianness": "little-endian",
        "size": 8
      }
    }
  },

I wonder if this is the cause of the incorrect display by H5Web.

Interestingly, using names that cannot be converted to numbers gives the expected result:

dtype = np.dtype(
    {"names": ["1ff", "0ff", "2ff"], "formats": [np.float64, np.int32, np.uint8]}
)

Image

Fields were not reordered in this case which may be linked:

  "type": {
    "class": "Compound",
    "fields": {
      "1ff": {
        "class": "Float",
        "endianness": "little-endian",
        "size": 64
      },
      "0ff": {
        "class": "Integer",
        "endianness": "little-endian",
        "size": 32
      },
      "2ff": {
        "class": "Integer (unsigned)",
        "endianness": "little-endian",
        "size": 8
      }
    }
  },

@axelboc axelboc changed the title Error displayed when using numeric strings as array index [Compound] Colons with numeric labels in wrong order Mar 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants