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

use encode for non-ascii #141

Merged
merged 4 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,4 @@ cython_debug/
.idea/

.vscode/
*.h5
9 changes: 9 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,12 @@ def s22_illegal_calc_results() -> list[ase.Atoms]:
def water() -> list[ase.Atoms]:
"""Get a dataset without positions."""
return [ase.Atoms("H2O")]


@pytest.fixture
def s22_no_ascii() -> list[ase.Atoms]:
images = []
for atoms in ase.collections.s22:
atoms.info["config"] = "βγ"
images.append(atoms)
return images
10 changes: 10 additions & 0 deletions tests/test_non_ascii.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import znh5md


def test_info_non_ascii(tmp_path, s22_no_ascii):
# io = znh5md.IO(tmp_path / "test.h5")
io = znh5md.IO("test.h5")
io.extend(s22_no_ascii)

for a, b in zip(io, s22_no_ascii):
assert a.info["config"] == b.info["config"] == "βγ"
2 changes: 1 addition & 1 deletion tests/test_znh5md.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


def test_version():
assert znh5md.__version__ == "0.3.6"
assert znh5md.__version__ == "0.3.7"
7 changes: 4 additions & 3 deletions znh5md/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ def extract_atoms_data(atoms: Atoms, use_ase_calc: bool = True) -> ASEData: # n
key = "force" if key == "forces" else key
uses_calc.append(key)
value = np.array(value) if isinstance(value, (int, float, list)) else value

if value.ndim > 1 and value.shape[0] == len(atomic_numbers):
particles[key] = value
else:
Expand All @@ -206,9 +205,11 @@ def extract_atoms_data(atoms: Atoms, use_ase_calc: bool = True) -> ASEData: # n
if isinstance(value, str):
if len(value) > NUMPY_STRING_DTYPE.itemsize:
raise ValueError(f"String {key} is too long to be stored.")
info_data[key] = np.array(value, dtype=NUMPY_STRING_DTYPE)
info_data[key] = np.array(value.encode(), dtype=NUMPY_STRING_DTYPE)
elif isinstance(value, dict):
info_data[key] = np.array(json.dumps(value), dtype=NUMPY_STRING_DTYPE)
info_data[key] = np.array(
json.dumps(value).encode(), dtype=NUMPY_STRING_DTYPE
)
metadata[key] = {"unit": None, "calc": False, "type": "json"}
else:
info_data[key] = value
Expand Down
Loading