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

enabled ufuncs for external classes #898

Merged
merged 5 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 21 additions & 0 deletions changes/898.feat.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Enabled implicit conversion of unknown objects

Now users can automatically convert unknown objects
and use them directly in `sisl` methods that are
implemented for various methods.

E.g.

.. code::

import ase
import sisl

gr = sisl.geom.graphene()
sisl.rotate(gr, ...)

gr = ase.Atoms(...)
sisl.rotate(gr, ...)

will both work. The latter will return a sisl
geometry.
11 changes: 11 additions & 0 deletions src/sisl/_core/tests/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import numpy as np
import pytest

import sisl as si

Check notice

Code scanning / CodeQL

Module is imported with 'import' and 'import from' Note test

Module 'sisl' is imported with both 'import' and 'import from'.
import sisl.geom as sisl_geom
from sisl import (
Atom,
Expand Down Expand Up @@ -1561,6 +1562,16 @@

# Test ASE (but only fail if present)

def test_geometry_dispatch(self):
pytest.importorskip("ase", reason="ase not available")
gr = sisl_geom.graphene()
to_ase = gr.to.ase()

ase_rotate = si.rotate(to_ase, 30, [0, 0, 1])
geom_rotate = si.rotate(gr, 30, [0, 0, 1])

assert geom_rotate.equal(ase_rotate, R=False)

def test_geometry_ase_new_to(self):
pytest.importorskip("ase", reason="ase not available")
gr = sisl_geom.graphene()
Expand Down
43 changes: 40 additions & 3 deletions src/sisl/_ufuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import inspect
from functools import singledispatch
from textwrap import dedent
from typing import Optional
from typing import Any, Optional

from sisl._lib._docscrape import FunctionDoc
from sisl.messages import SislError, warn
Expand Down Expand Up @@ -142,9 +142,46 @@ def deco(method: FuncType):
# create a new method that will be stored
# as a place-holder for the dispatch methods.

def method_registry(obj, *args, **kwargs):
# The default method needs access to it-self.
# In this way we can figure out if there are
# some mechanism by which we can recover
# a meaningful action.
# I.e. this small hack will allow one to do this:
# @register_sisl_dispatch(Geometry)
# def func(geometry: Geometry, ...)
#
# from ase import Atoms
# func(Atoms(...), ...)
#
# The reason is that func won't find any registered
# classes under Atoms, and so it will run through
# the registered classes, trying out `cls.new` for each
# of them. And then re-call the function it self.

def method_registry(obj: Any, *args, **kwargs):
nonlocal name, method_registry

# Obviously, the method has been called without finding the
# correct dispatch method.
for cls, cls_func in method_registry.registry.items():

# Try and get the conversion method
# Currently we'll only use `new` for consistency
# I don't know if this will work for other
# keys as well...
new = getattr(cls, "new", None)

if new is not None:
try:
# Try and convert to a sisl-compatible object
obj = cls.new(obj)
return cls_func(obj, *args, **kwargs)
except KeyError:
pass

raise SislError(
f"Calling '{name}' with a non-registered type, {type(obj)} has not been registered."
f"Calling '{name}' with a non-registered type, {type(obj)} has not "
"been registered, and cannot be converted to a sisl type."
)

doc = dedent(
Expand Down
Loading