Skip to content

Commit

Permalink
Add cli to python api (#50)
Browse files Browse the repository at this point in the history
* Add cli to python api with entry point. Fixes #45
  • Loading branch information
oscarhiggott committed Nov 6, 2022
1 parent ca7d0f8 commit 44819d2
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 12 deletions.
3 changes: 3 additions & 0 deletions data/three_errors.dem
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
error(0.1) D0 L0
error(0.1) D0 D1 L1
error(0.1) D1 L2
4 changes: 4 additions & 0 deletions data/three_errors.dets
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
shot
shot D0
shot D1
shot D0 D1
6 changes: 3 additions & 3 deletions docs/developer_documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,16 @@ pip install -e .
First install the sphinx requirements:

```bash
pip install -r docs/requirements.txt
pip install -r docs/sphinx_docs/requirements.txt
```

You will also need to install the latest version of pymatching, and you may also need to [install pandoc](https://pandoc.org/installing.html).

Then, to build the html sphinx docs, run:
```bash
cd docs
cd docs/sphinx_docs
make html
```

and view `doc/build/html/index.html` in a browser.
and view `docs/sphinx_docs/build/html/index.html` in a browser.

14 changes: 12 additions & 2 deletions docs/sphinx_docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@ Python API Documentation


Matching
--------------------
--------
.. automodule:: pymatching.matching
:members:
:special-members: __init__
:special-members: __init__

Command line interface
----------------------
.. automethod:: pymatching.cli

Random number generator
-----------------------
.. automethod:: pymatching.set_seed
.. automethod:: pymatching.randomize
.. automethod:: pymatching.rand_float
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ def build_extension(self, ext):
cmdclass={"build_ext": CMakeBuild},
zip_safe=False,
extras_require={"test": ["pytest>=6.0"]},
entry_points={
'console_scripts': ['pymatching=pymatching._cli_argv:cli_argv'],
},
python_requires=">=3.6",
install_requires=['scipy', 'numpy', 'networkx', 'retworkx>=0.11.0', 'matplotlib']
)
1 change: 1 addition & 0 deletions src/pymatching/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

from pymatching._cpp_pymatching import (randomize, set_seed, rand_float) # noqa
from pymatching._cpp_pymatching import main as cli # noqa
from pymatching.matching import Matching # noqa

randomize() # Set random seed using std::random_device
7 changes: 7 additions & 0 deletions src/pymatching/_cli_argv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import sys

import pymatching


def cli_argv():
pymatching.cli(command_line_args=sys.argv[1:])
2 changes: 2 additions & 0 deletions src/pymatching/rand/rand_gen.pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ void pm_pybind::pybind_rand_gen_methods(py::module &m) {
m.def("rand_float", &pm::rand_float, "from"_a, "to"_a, R"pbdoc(
Generate a floating point number chosen uniformly at random
over the interval between `from` and `to`
Parameters
----------
from: float
Smallest float that can be drawn from the distribution
to: float
Largest float that can be drawn from the distribution
Returns
-------
float
Expand Down
51 changes: 51 additions & 0 deletions tests/cli_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import os
import sys
from contextlib import contextmanager

import pymatching
from pymatching import cli
from pymatching._cli_argv import cli_argv

from .config import DATA_DIR


@contextmanager
def three_errors_data():
out_fn = os.path.join(DATA_DIR, "three_errors_predictions.dets")
if os.path.isfile(out_fn):
os.remove(out_fn)
assert not os.path.exists(out_fn)
args = [
"predict",
"--dem", os.path.join(DATA_DIR, "three_errors.dem"),
"--in", os.path.join(DATA_DIR, "three_errors.dets"),
"--in_format", "dets",
"--out", out_fn,
"--out_format", "dets",
]
yield args
assert os.path.isfile(out_fn)
with open(out_fn) as f:
assert f.read() == """shot
shot L0
shot L2
shot L1
"""
os.remove(out_fn)


def test_cli():
with three_errors_data() as args:
cli(command_line_args=args)


def test_protected_cli():
with three_errors_data() as args:
pymatching._cpp_pymatching.main(command_line_args=args)


def test_cli_argv():
from unittest.mock import patch
with three_errors_data() as args:
with patch.object(sys, 'argv', ["cli"] + args):
cli_argv()
5 changes: 5 additions & 0 deletions tests/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import pathlib
import os

THIS_DIR = pathlib.Path(__file__).parent.resolve()
DATA_DIR = os.path.join(pathlib.Path(THIS_DIR).parent.absolute(), "data")
5 changes: 0 additions & 5 deletions tests/matching/config.py

This file was deleted.

2 changes: 1 addition & 1 deletion tests/matching/decode_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import pymatching
from pymatching import Matching

from .config import DATA_DIR
from tests.config import DATA_DIR


def repetition_code(n):
Expand Down
2 changes: 1 addition & 1 deletion tests/matching/load_from_stim_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import pytest

from pymatching.matching import Matching
from .config import DATA_DIR
from tests.config import DATA_DIR


def test_load_from_stim_objects():
Expand Down

0 comments on commit 44819d2

Please sign in to comment.