Commit 0416b54
authored
Fixes #996.
The `deepmd/hdf5` writer reshapes and writes every optional frame array,
so an empty optional property (e.g. `forces`/`virials` when
`cal_force`/`cal_stress` is disabled) is stored as a `(nframes, 0)`
dataset. On load, `to_system_data` tries to reshape that zero-width
dataset back to `(nframes, natoms, 3)` and raises `ValueError: cannot
reshape array of size 0`.
Reproducer (fails on current `master`):
```python
import dpdata, numpy as np
s = dpdata.LabeledSystem(data={
"atom_names": ["H"], "atom_numbs": [1], "atom_types": np.array([0]),
"cells": np.eye(3).reshape(1, 3, 3), "coords": np.zeros((1, 1, 3)),
"energies": np.zeros(1), "orig": np.zeros(3),
"forces": np.zeros((1, 0, 3)),
})
s.to("deepmd/hdf5", "t.h5")
dpdata.LabeledSystem("t.h5", fmt="deepmd/hdf5") # ValueError before this change
```
The `deepmd/raw` and `deepmd/npy` writers already skip this case; this
makes the HDF5 writer consistent with them by skipping an optional array
whose size is 0 while the system still has frames. Non-empty arrays are
written and reloaded exactly as before.
Added `TestHDF5EmptyOptionalArray` covering both the previously-failing
empty round-trip and a non-empty round-trip to guard against regression.
The `deepmd` test suites pass (`test_deepmd_hdf5`, `test_deepmd_raw`,
`test_deepmd_comp`, `test_deepmd_mixed`) and `ruff` is clean.
*Disclosure: written with AI assistance; reviewed and tested before
submitting.*
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Bug Fixes**
* Improved HDF5 export/import handling for optional empty per-frame
arrays, avoiding invalid data files that could fail to load.
* Empty optional fields are now skipped during save, while valid data
continues to round-trip correctly.
* **Tests**
* Added regression coverage for empty optional array handling.
* Added verification that real force data is still preserved after save
and reload.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 221f254 commit 0416b54
2 files changed
Lines changed: 54 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
201 | 201 | | |
202 | 202 | | |
203 | 203 | | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
204 | 211 | | |
205 | 212 | | |
206 | 213 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
300 | 300 | | |
301 | 301 | | |
302 | 302 | | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
0 commit comments