Skip to content

Commit

Permalink
Add CLI (#46)
Browse files Browse the repository at this point in the history
* add cli

* fix windows path

* add cli
  • Loading branch information
PythonFZ authored Mar 28, 2023
1 parent 42958ee commit ab39bce
Show file tree
Hide file tree
Showing 7 changed files with 1,526 additions and 1,409 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,11 @@ db.add(znh5md.io.AtomsReader(atoms))

data = znh5md.ASEH5MD("db.h5")
data.get_atoms_list() == atoms
```
```

## CLI
ZnH5MD provides a small set of CLI tools:

- `znh5md view <file.h5>` to view the File using `ase.visualize`
- `znh5md export <file.h5> <file.xyz>` to export the file to `.xyz` or any other supported file format
- `znh5md convert <file.xyz> <file.h5>` to save a `file.xyz` as `file.h5` in the H5MD standard.
2,843 changes: 1,440 additions & 1,403 deletions poetry.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ h5py = "^3.7.0"
dask = {extras = ["array"], version = "^2022.11.1"}
ase = "^3.22.1"
tqdm = "^4.65.0"
typer = "^0.7.0"

[tool.poetry.group.dev.dependencies]
black = "^23.1.0"
Expand All @@ -25,6 +26,8 @@ coverage = "^7.1.0"
[tool.poetry.group.notebook.dependencies]
jupyterlab = "^3.5.0"

[tool.poetry.scripts]
znh5md = 'znh5md.cli:app'

[build-system]
requires = ["poetry-core"]
Expand Down
60 changes: 60 additions & 0 deletions znh5md/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import typer
import importlib.metadata
import ase.io
import ase.visualize
import tqdm
import pathlib

app = typer.Typer()


@app.command()
def view(file: str):
"""Use ASE GUI to view a H5MD Trajectory."""
import znh5md

typer.echo(f"Loading atoms from {file}")

data = znh5md.ASEH5MD(file)
ase.visualize.view(data.get_atoms_list())


@app.command()
def export(file: str, output: str):
"""export a H5MD File into the output file."""
import znh5md

data = znh5md.ASEH5MD(file)
for atom in tqdm.tqdm(data.get_atoms_list(), ncols=120):
ase.io.write(output, atom, append=True)


@app.command()
def convert(file: str, db_file: str):
"""Save a trajectory as H5MD File."""
import znh5md

db = znh5md.io.DataWriter(db_file)
if not pathlib.Path(db_file).exists():
db.initialize_database_groups()

db.add(znh5md.io.ASEFileReader(file))


def version_callback(value: bool) -> None:
"""Get the installed dask4dvc version."""
if value:
typer.echo(f"znh5md {importlib.metadata.version('znh5md')}")
raise typer.Exit()


@app.callback()
def main(
version: bool = typer.Option(
None, "--version", callback=version_callback, is_eager=True
),
) -> None:
"""Dask4DVC CLI callback.
Run the DVC graph or DVC experiments in parallel using dask.
"""
_ = version # this would be greyed out otherwise
15 changes: 13 additions & 2 deletions znh5md/format/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,22 @@ class GRP:

@staticmethod
def encode_boundary(value) -> np.ndarray:
return np.array(["periodic".encode() if x else "none".encode() for x in value])
return np.array(
[
(
"periodic".encode(encoding="utf-8")
if x
else "none".encode(encoding="utf-8")
)
for x in value
]
)

@staticmethod
def decode_boundary(value) -> np.ndarray:
pbc = np.array([x == "periodic".encode() for x in value]).astype(bool)
pbc = np.array([x == "periodic".encode(encoding="utf-8") for x in value]).astype(
bool
)
return pbc


Expand Down
3 changes: 1 addition & 2 deletions znh5md/io/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import abc
import dataclasses
import logging
import os
import typing

import h5py
Expand Down Expand Up @@ -157,7 +156,7 @@ def yield_chunks(
@dataclasses.dataclass
class DataWriter:
filename: str
atoms_path: str = os.path.join("particles", "atoms")
atoms_path: str = "particles/atoms"

def initialize_database_groups(self):
"""Create all groups that are required.
Expand Down
2 changes: 1 addition & 1 deletion znh5md/io/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def yield_chunks(
# if the property was specifically selected, raise the error
raise err
else:
log.warning(f"Skipping {name} because {err}")
log.debug(f"Skipping {name} because {err}")
yield data
start_index = stop_index
pbar.update(self.frames_per_chunk)
Expand Down

0 comments on commit ab39bce

Please sign in to comment.