Skip to content

Commit

Permalink
Add rustworkx backend (#111)
Browse files Browse the repository at this point in the history
* Update graph.py to support set_backend function
I also made a fuction to see the available backends and get the current backend

* Apply private _available_backends suggestions from code review

This commit contains the suggested changes regarding making the available_backends variable private, and refactoring the get_available_backends method to available_backends

Co-authored-by: Rocco Meli <r.meli@bluemail.ch>

* Print warning when backend is already set + make sure we use _available_backends everywhere where possible

* remove reliance on environment variables

* make _validate_backend function
print warning when a certain backend isn't installed

* Use precommit hooks
add __all__ back
refactor _alias_backendDict to _alias_to_backend

* Update ValueError message

Co-authored-by: Rocco Meli <r.meli@bluemail.ch>

* Don't return the backend when setting it

Co-authored-by: Rocco Meli <r.meli@bluemail.ch>

* Add dummy function to make mypy happy
remove __all__ assignment

* fist play with rustworkx backend

* add documentation for backend selection

* add cli backend selection

* cleanup

* changelog

* apply @Jnelen suggestion and add warning filter

* make molecule test also more robust

* add rx to ci

* add rustworkx to all backends tests

* add back graphtool

* add rx to test all backends and add verbose mode

* Update test_molecule.py

---------

Co-authored-by: jnelen <jnelen@ucam.edu>
Co-authored-by: Jochem Nelen <78348388+Jnelen@users.noreply.github.com>
  • Loading branch information
3 people authored Mar 11, 2024
1 parent fbff4a3 commit f250c3e
Show file tree
Hide file tree
Showing 12 changed files with 307 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
os: [macOS-latest, ubuntu-latest, windows-latest]
python-version: ["3.9", "3.10", "3.11", "3.12"]
chemlib: [obabel, rdkit]
graphlib: [nx, gt, all]
graphlib: [nx, gt, rx, all]
exclude:
# graph-tools does not work on Windows
- {os: "windows-latest", graphlib: "gt"}
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Contributors: @RMeli, @takluyver, @Jnelen

### Added

* Support for `rustworkx` graph library [PR 111 | @RMeli]
* Functionality to manually select the backend from CLI [PR #108 | @RMeli]
* Functionality to manually select the backend [PR #107 | @Jnelen]
* Python `3.12` to CI [PR #102 | @RMeli]
Expand Down
27 changes: 20 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ If you find `spyrmsd` useful, please consider citing the following paper:
`spyrmsd` is available on [PyPI](https://pypi.org/project/spyrmsd/) and [conda-forge](https://github.com/conda-forge/spyrmsd-feedstock) and can be easily installed from source. See [Dependencies](###Dependencies) for a description of all the dependencies.

> [!NOTE]
> `spyrmsd` will install [NetworkX](https://networkx.github.io/) (multi-platform). You can install [graph-tool](https://graph-tool.skewed.de/) on macOS and Linux for higher performance.
> `spyrmsd` will install [NetworkX](https://networkx.github.io/) (multi-platform). You can install the other backends for higher performance.
> [!WARNING]
> If `spyrmsd` is used as a standalone tool, it is required to install either [RDKit](https://rdkit.org/) or [Open Babel](http://openbabel.org/). Neither is automatically installed with `pip` nor `conda`.
Expand Down Expand Up @@ -71,12 +71,16 @@ pip install .

The following packages are required to use `spyrmsd` as a module:

* [graph-tool](https://graph-tool.skewed.de/) or [NetworkX](https://networkx.github.io/)
* [numpy](https://numpy.org/)
* [scipy](https://www.scipy.org/)

One of the following graph libraries is required:
* [graph-tool]
* [NetworkX]
* [rustworkx]

> [!NOTE]
> `spyrmsd` uses [graph-tool](https://graph-tool.skewed.de/) by default but will fall back to [NetworkX](https://networkx.github.io/) if the former is not installed (e.g. on Windows). However, in order to support cross-platform installation [NetworkX](https://networkx.github.io/) is installed by default, and [graph-tool](https://graph-tool.skewed.de/) needs to be installed manually.
> `spyrmsd` uses the following priority when multiple graph libraries are present: [graph-tool], [NetworkX], [rustworkx]. *This order might change. Use `set_backend` to ensure you are always using the same backend, if needed.* However, in order to support cross-platform installation [NetworkX](https://networkx.github.io/) is installed by default, and the other graph library need to be installed manually.
#### Standalone Tool

Expand Down Expand Up @@ -118,7 +122,7 @@ def rmsd(
```

> [!NOTE]
> Atomic properties (`aprops`) can be any Python object when using [NetworkX](https://networkx.github.io/), or integers, floats, or strings when using [graph-tool](https://graph-tool.skewed.de/).
> Atomic properties (`aprops`) can be any Python object when using [NetworkX] and [rustworkx], or integers, floats, or strings when using [graph-tool].
#### Symmetry-Corrected RMSD

Expand All @@ -142,11 +146,16 @@ def symmrmsd(
```

> [!NOTE]
> Atomic properties (`aprops`) can be any Python object when using [NetworkX](https://networkx.github.io/), or integers, floats, or strings when using [graph-tool](https://graph-tool.skewed.de/).
> Atomic properties (`aprops`) can be any Python object when using [NetworkX] and [rustworkx], or integers, floats, or strings when using [graph-tool](https://graph-tool.skewed.de/).
#### Select Backend

`spyrmsd` supports both [NetworkX](https://networkx.github.io/) and [graph-tool](https://graph-tool.skewed.de/) for the calculation of graph isomorphisms. You can check which backend is being used with
`spyrmsd` supports the following graph libraries for the calculation of graph isomorphisms:
* [graph-tool]
* [NetworkX]
* [rustworkx]

You can check which backend is being used with

```python
spyrmsd.get_backend()
Expand Down Expand Up @@ -176,7 +185,7 @@ Pre-commit `git` hooks can be installed with [pre-commit](https://pre-commit.com

## Copyright

Copyright (c) 2019-2021, Rocco Meli.
Copyright (c) 2019-2024, Rocco Meli.

## References

Expand All @@ -185,3 +194,7 @@ References are tracked with [duecredit](https://github.com/duecredit/duecredit/)
### Acknowledgements

Project based on the [Computational Molecular Science Python Cookiecutter](https://github.com/molssi/cookiecutter-cms) version `1.1`.

[rustworkx]: https://www.rustworkx.org
[NetworkX]: https://networkx.github.io/
[graph-tool]: https://graph-tool.skewed.de/
26 changes: 26 additions & 0 deletions devtools/conda-envs/spyrmsd-test-obabel-rx.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: spyrmsd
channels:
- conda-forge
dependencies:
# Base
- python
- setuptools

# Maths
- numpy
- scipy
- rustworkx

# Chemistry
- openbabel

# Testing
- pytest
- pytest-cov
- pytest-benchmark

# Dev
- mypy
- flake8
- black
- codecov
1 change: 1 addition & 0 deletions devtools/conda-envs/spyrmsd-test-rdkit-all.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies:
- scipy
- graph-tool
- networkx>=2
- rustworkx

# Chemistry
- rdkit
Expand Down
27 changes: 27 additions & 0 deletions devtools/conda-envs/spyrmsd-test-rdkit-rx.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: spyrmsd
channels:
- conda-forge
- rdkit
dependencies:
# Base
- python
- setuptools

# Maths
- numpy
- scipy
- rustworkx

# Chemistry
- rdkit

# Testing
- pytest
- pytest-cov
- pytest-benchmark

# Dev
- mypy
- flake8
- black
- codecov
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

project = "spyrmsd"
copyright = (
"2019-2021, Rocco Meli. Project structure based on the "
"2019-2024, Rocco Meli. Project structure based on the "
"Computational Molecular Science Python Cookiecutter version 1.1"
)
author = "Rocco Meli"
Expand Down
6 changes: 6 additions & 0 deletions spyrmsd/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
default=None,
help="Graph library (backend)",
)
parser.add_argument(
"-v", "--verbose", action="store_true", help="Enable verbose mode"
)

args = parser.parse_args()

Expand Down Expand Up @@ -63,6 +66,9 @@
warnings.simplefilter("ignore")
spyrmsd.set_backend(args.graph_backend)

if args.verbose:
print(f"Graph library: {spyrmsd.get_backend()}")

# Loop over molecules within fil
RMSDlist = rmsdwrapper(
ref,
Expand Down
22 changes: 21 additions & 1 deletion spyrmsd/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@

from spyrmsd import constants

_supported_backends = ("graph_tool", "networkx")
_supported_backends = ("graph_tool", "networkx", "rustworkx")

_available_backends = []
_current_backend = None

_backend_to_alias = {
"graph_tool": ["graph_tool", "graphtool", "graph-tool", "graph tool", "gt"],
"networkx": ["networkx", "nx"],
"rustworkx": ["rustworkx", "rx"],
}

_alias_to_backend = {}
Expand Down Expand Up @@ -141,6 +142,25 @@ def _set_backend(backend):
num_vertices = nx_num_vertices
vertex_property = nx_vertex_property

elif backend == "rustworkx":
from spyrmsd.graphs.rx import cycle as rx_cycle
from spyrmsd.graphs.rx import (
graph_from_adjacency_matrix as rx_graph_from_adjacency_matrix,
)
from spyrmsd.graphs.rx import lattice as rx_lattice
from spyrmsd.graphs.rx import match_graphs as rx_match_graphs
from spyrmsd.graphs.rx import num_edges as rx_num_edges
from spyrmsd.graphs.rx import num_vertices as rx_num_vertices
from spyrmsd.graphs.rx import vertex_property as rx_vertex_property

cycle = rx_cycle
graph_from_adjacency_matrix = rx_graph_from_adjacency_matrix
lattice = rx_lattice
match_graphs = rx_match_graphs
num_edges = rx_num_edges
num_vertices = rx_num_vertices
vertex_property = rx_vertex_property

_current_backend = backend


Expand Down
Loading

0 comments on commit f250c3e

Please sign in to comment.