-
Notifications
You must be signed in to change notification settings - Fork 13
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
metatensor
interface
#158
Merged
+1,245
−367
Merged
metatensor
interface
#158
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
865c283
`metatensor` interface
frostedoyster ccd82ef
`metatensor.torch` for `sphericart.torch`
frostedoyster d9d6f03
Add example
frostedoyster 6d017c1
Fix circular imports
frostedoyster df64c88
Documentation
frostedoyster 5fdaca7
Documentation suggestion
frostedoyster a03f085
Make it work on GPU
frostedoyster e9f18b9
Merge branch 'main' into metatensor
frostedoyster dd41b36
Fix `metatensor-torch` documentation
frostedoyster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
Metatensor API | ||
============== | ||
|
||
``sphericart`` can be used in conjunction with | ||
`metatensor <https://docs.metatensor.org/latest/index.html>`_ in order to attach | ||
metadata to inputs and outputs, as well as to naturally obtain spherical harmonics, | ||
gradients and Hessians in a single object. | ||
|
||
Here is the API reference for the ``sphericart.metatensor`` and | ||
``sphericart.torch.metatensor`` modules. | ||
|
||
sphericart.metatensor | ||
--------------------- | ||
|
||
.. autoclass:: sphericart.metatensor.SphericalHarmonics | ||
:members: | ||
|
||
.. autoclass:: sphericart.metatensor.SolidHarmonics | ||
:members: | ||
|
||
sphericart.torch.metatensor | ||
--------------------------- | ||
|
||
.. autoclass:: sphericart.torch.metatensor.SphericalHarmonics | ||
:members: | ||
|
||
.. autoclass:: sphericart.torch.metatensor.SolidHarmonics | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Using sphericart with metatensor | ||
-------------------------------- | ||
|
||
``sphericart`` can be used in conjunction with | ||
`metatensor <https://docs.metatensor.org/latest/index.html>`_ in order to attach | ||
metadata to inputs and outputs, as well as to naturally obtain spherical harmonics, | ||
gradients and Hessians in a single object. | ||
|
||
This example shows how to use the ``sphericart.metatensor`` module to compute | ||
spherical harmonics, their gradients and their Hessians. | ||
|
||
.. literalinclude:: ../../examples/metatensor/example.py | ||
:language: python |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import numpy as np | ||
from metatensor import Labels, TensorBlock, TensorMap | ||
|
||
import sphericart | ||
import sphericart.metatensor | ||
|
||
|
||
l_max = 15 | ||
n_samples = 100 | ||
|
||
xyz = TensorMap( | ||
keys=Labels.single(), | ||
blocks=[ | ||
TensorBlock( | ||
values=np.random.rand(n_samples, 3, 1), | ||
samples=Labels( | ||
names=["sample"], | ||
values=np.arange(n_samples).reshape(-1, 1), | ||
), | ||
components=[ | ||
Labels( | ||
names=["xyz"], | ||
values=np.arange(3).reshape(-1, 1), | ||
) | ||
], | ||
properties=Labels.single(), | ||
) | ||
], | ||
) | ||
|
||
calculator = sphericart.metatensor.SphericalHarmonics(l_max) | ||
|
||
spherical_harmonics = calculator.compute(xyz) | ||
|
||
for single_l in range(l_max + 1): | ||
spherical_single_l = spherical_harmonics.block({"o3_lambda": single_l}) | ||
|
||
# check values against pure sphericart | ||
assert np.allclose( | ||
spherical_single_l.values.squeeze(-1), | ||
sphericart.SphericalHarmonics(single_l).compute( | ||
xyz.block().values.squeeze(-1) | ||
)[:, single_l**2 : (single_l + 1) ** 2], | ||
) | ||
|
||
# further example: obtaining gradients of l = 2 spherical harmonics | ||
spherical_harmonics = calculator.compute_with_gradients(xyz) | ||
l_2_gradients = spherical_harmonics.block({"o3_lambda": 2}).gradient("positions") | ||
|
||
# further example: obtaining Hessians of l = 2 spherical harmonics | ||
spherical_harmonics = calculator.compute_with_hessians(xyz) | ||
l_2_hessians = spherical_harmonics.block( | ||
{"o3_lambda": 2} | ||
).gradient("positions").gradient("positions") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we indicate somewhere (maybe here and in the docstring?) that the output will have the same samples as the input?