8000 [BUG]KeyError: 0 in meshio._mesh.__init__ when using cell_data (meshio 4.4.6, 5.2.0, 5.3.5 on Python 3.10/Windows with Numpy 1.25.x) · Issue #1520 · nschloe/meshio · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
[BUG]KeyError: 0 in meshio._mesh.__init__ when using cell_data (meshio 4.4.6, 5.2.0, 5.3.5 on Python 3.10/Windows with Numpy 1.25.x) #1520
Open
@slwu1024

Description

@slwu1024

Describe the bug
When creating a meshio.Mesh object and providing cell_data, a KeyError: 0 is raised internally within the meshio._mesh.py init method at the line data[k] = np.asarray(data[k]). This issue occurs even with a very simple triangular mesh and a cell_data dictionary containing only a single data field for the 'triangle' cell type.
The error persists across multiple meshio versions (tested 4.4.6, 5.2.0, 5.3.5) and with numpy versions known to be compatible with other packages in the environment (e.g., numpy 1.25.x).
Interestingly, creating the mesh object:
Without any cell_data (only points and cells) works successfully.
With point_data but no cell_data also works successfully.
The issue seems specifically related to how meshio processes the cell_data argument.

To Reproduce
The following minimal Python script reproduces the error:
import meshio
import numpy as np
import os

Define a minimal mesh

points = np.array([
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
], dtype=float)

cells_triangle_connectivity = np.array([[0, 1, 2]], dtype=int)
cells_dict_form = {"triangle": cells_triangle_connectivity} # Cell connectivity

Define simple cell data (one field for the single triangle)

cell_data_content = {
"triangle": {
"cell_value_single": np.array([10.0], dtype=float)
}
}

print("--- DEBUG: Inputs to meshio.Mesh ---")
print(f"points shape: {points.shape}, dtype: {points.dtype}")
print(f"cells_dict_form: {cells_dict_form}")
print(f"cell_data_content: {cell_data_content}")
print("------------------------------------")

try:
# Attempt to create Mesh object with cell_data
mesh_obj = meshio.Mesh(
points,
cells_dict_form,
# point_data=None, # Error occurs even without point_data
cell_data=cell_data_content
)

# Attempt to write (may not be reached if __init__ fails)
output_dir = "meshio_test_output"
os.makedirs(output_dir, exist_ok=True)
filename = os.path.join(output_dir, "mre_test_with_cell_data.vtk")
mesh_obj.write(filename)
print(f"Successfully wrote {filename}")

except Exception as e:
print(f"Error during meshio operation: {e}")
if isinstance(e, KeyError):
print(f" KeyError detail: {e.args}")
import traceback
traceback.print_exc()

Diagnose
Operating System: Windows 10/11 (请填写你的具体 Windows 版本)
Python Version: 3.10.x (请填写你的具体 Python 3.10 小版本)
Meshio Versions Tested (all exhibit the error with cell_data):
5.3.5
5.2.0
4.4.6
NumPy Versions Tested:
1.25.2 (used with meshio 4.4.6 and 5.2.0, error persists)
1.26.3 (used with meshio 5.3.5, error persists)
(Initially tested with numpy 2.2.5 which had other dependency conflicts, but the KeyError: 0 issue remained when cell_data was present even after resolving those primary conflicts by downgrading numpy).

Console Output for the MRE (Minimal Reproducible Example) using meshio 4.4.6 and numpy 1.25.2:
(This output is identical for meshio 5.2.0 with numpy 1.25.2, and for meshio 5.3.5 with numpy 1.26.3 when cell_data is included)
pip freeze | grep meshio

--- DEBUG: Inputs to meshio.Mesh ---
points shape: (3, 3), dtype: float64
cells_dict_form: {'triangle': array([[0, 1, 2]])}
cell_data_content: {'triangle': {'cell_value_single': array([10.])}}
------------------------------------
Error during meshio operation: 0
  KeyError detail: (0,)
Traceback (most recent call last):
  File "path/to/your/test_meshio_save.py", line XX, in <module> # 请将 XX 替换为 meshio.Mesh 调用所在的行号
    mesh_obj = meshio.Mesh(
  File "D:\Apps\python310\lib\site-packages\meshio\_mesh.py", line YYY, in __init__ # 请将 YYY 替换为实际报错的行号 (如 75 或 180)
    data[k] = np.asarray(data[k])
KeyError: 0

Additional Context
The error occurs during the initialization of the meshio.Mesh object itself, specifically within the loop that seems to process point_data or cell_data.
The structure of points, cells (using a dictionary format {"triangle": connectivity_array}), and the cell_data dictionary ({"triangle": {"field_name": data_array}}) appears to be standard and correct according to documentation and examples.
The traceback consistently points to data[k] = np.asarray(data[k]) with k being the integer 0.
Any insights or fixes would be greatly appreciated. Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0