metatensor-operations 0.2.0__tar.gz → 0.2.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {metatensor-operations-0.2.0/metatensor_operations.egg-info → metatensor_operations-0.2.2}/PKG-INFO +4 -4
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/__init__.py +1 -1
- metatensor_operations-0.2.2/metatensor/operations/_backend.py +54 -0
- metatensor-operations-0.2.0/metatensor/operations/checks.py → metatensor_operations-0.2.2/metatensor/operations/_checks.py +3 -32
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/_dispatch.py +288 -30
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/_utils.py +2 -2
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/abs.py +27 -26
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/add.py +70 -63
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/allclose.py +14 -10
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/block_from_array.py +2 -1
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/detach.py +24 -22
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/divide.py +75 -68
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/dot.py +49 -48
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/drop_blocks.py +3 -1
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/empty_like.py +63 -61
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/equal.py +5 -1
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/equal_metadata.py +11 -1
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/join.py +139 -137
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/lstsq.py +40 -33
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/manipulate_dimension.py +98 -65
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/multiply.py +66 -59
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/one_hot.py +5 -4
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/ones_like.py +63 -61
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/pow.py +42 -35
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/random_like.py +69 -67
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/reduce_over_samples.py +58 -51
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/remove_gradients.py +34 -32
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/requires_grad.py +22 -20
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/slice.py +86 -178
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/solve.py +56 -48
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/sort.py +24 -17
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/split.py +177 -107
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/subtract.py +8 -2
- metatensor_operations-0.2.2/metatensor/operations/unique_metadata.py +245 -0
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/zeros_like.py +63 -61
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2/metatensor_operations.egg-info}/PKG-INFO +4 -4
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor_operations.egg-info/SOURCES.txt +2 -2
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/pyproject.toml +3 -3
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/setup.py +1 -1
- metatensor-operations-0.2.0/metatensor/operations/_classes.py +0 -27
- metatensor-operations-0.2.0/metatensor/operations/unique_metadata.py +0 -263
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/AUTHORS +0 -0
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/LICENSE +0 -0
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/MANIFEST.in +0 -0
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/README.rst +0 -0
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor_operations.egg-info/dependency_links.txt +0 -0
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor_operations.egg-info/requires.txt +0 -0
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor_operations.egg-info/top_level.txt +0 -0
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor_operations.egg-info/zip-safe +0 -0
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/n_commits_since_last_tag +0 -0
- {metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/setup.cfg +0 -0
{metatensor-operations-0.2.0/metatensor_operations.egg-info → metatensor_operations-0.2.2}/PKG-INFO
RENAMED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: metatensor-operations
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.2
|
|
4
4
|
Summary: Operations to manipulate metatensor data types
|
|
5
5
|
Author: Guillaume Fraux, Davide Tisi, Philip Loche, Joseph W. Abbott, Jigyasa Nigam, Chiheb Ben Mahmoud
|
|
6
6
|
License: BSD-3-Clause
|
|
7
|
-
Project-URL: homepage, https://
|
|
8
|
-
Project-URL: documentation, https://
|
|
7
|
+
Project-URL: homepage, https://docs.metatensor.org/latest/
|
|
8
|
+
Project-URL: documentation, https://docs.metatensor.org/latest/
|
|
9
9
|
Project-URL: repository, https://github.com/lab-cosmo/metatensor
|
|
10
|
-
Project-URL: changelog, https://
|
|
10
|
+
Project-URL: changelog, https://docs.metatensor.org/latest/operations/CHANGELOG.html
|
|
11
11
|
Keywords: machine learning,molecular modeling
|
|
12
12
|
Classifier: Development Status :: 4 - Beta
|
|
13
13
|
Classifier: Intended Audience :: Science/Research
|
{metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/__init__.py
RENAMED
|
@@ -4,7 +4,7 @@ __version__ = importlib.metadata.version("metatensor-operations")
|
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
from ._utils import NotEqualError # noqa
|
|
7
|
-
from .
|
|
7
|
+
from ._checks import checks_enabled, unsafe_disable_checks, unsafe_enable_checks # noqa
|
|
8
8
|
|
|
9
9
|
from .abs import abs # noqa
|
|
10
10
|
from .add import add # noqa
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# This file defines the default set of classes to use in metatensor-operations
|
|
2
|
+
#
|
|
3
|
+
# The code from metatensor-operations can be used in two different modes: either pure
|
|
4
|
+
# Python mode or TorchScript mode. In the second case, we need to use a different
|
|
5
|
+
# version of the classes above; to do without having to rewrite everything, we re-import
|
|
6
|
+
# metatensor-operations in a special context.
|
|
7
|
+
#
|
|
8
|
+
# See metatensor-torch/metatensor/torch/operations.py for more information.
|
|
9
|
+
#
|
|
10
|
+
# Any change to this file MUST be also be made to `metatensor/torch/operations.py`.
|
|
11
|
+
from typing import Union
|
|
12
|
+
|
|
13
|
+
import numpy as np
|
|
14
|
+
|
|
15
|
+
import metatensor
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
try:
|
|
19
|
+
import torch
|
|
20
|
+
|
|
21
|
+
Array = Union[np.ndarray, torch.Tensor]
|
|
22
|
+
except ImportError:
|
|
23
|
+
Array = np.ndarray
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
Labels = metatensor.Labels
|
|
27
|
+
TensorBlock = metatensor.TensorBlock
|
|
28
|
+
TensorMap = metatensor.TensorMap
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def torch_jit_is_scripting():
|
|
32
|
+
return False
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def torch_jit_script(function):
|
|
36
|
+
"""
|
|
37
|
+
This function takes the place of ``@torch.jit.script`` when running in pure Python
|
|
38
|
+
mode (i.e. when using classes from metatensor-core).
|
|
39
|
+
|
|
40
|
+
When used as a decorator (``@torch_jit_script def foo()``), it does nothing to the
|
|
41
|
+
function.
|
|
42
|
+
"""
|
|
43
|
+
return function
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def torch_jit_annotate(type, value):
|
|
47
|
+
"""
|
|
48
|
+
This function takes the place of ``torch.jit.annotate`` when running in pure Python
|
|
49
|
+
mode (i.e. when using classes from metatensor-core).
|
|
50
|
+
"""
|
|
51
|
+
return value
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
check_isinstance = isinstance
|
|
@@ -1,32 +1,3 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Metatensor performs `shape` as well as `consistency` checks for the metadata during each
|
|
3
|
-
operation. The check status can be verified with :py:func:`metatensor.checks_enabled()`
|
|
4
|
-
and controlled by :py:class:`metatensor.unsafe_enable_checks()` and
|
|
5
|
-
:py:class:`metatensor.unsafe_disable_checks()`.
|
|
6
|
-
|
|
7
|
-
.. warning::
|
|
8
|
-
Using metatensor without checks is unsafe, can lead to unwanted results, and may
|
|
9
|
-
produce cryptic error messages. Therefore, disabling checks is not recommended and
|
|
10
|
-
should only be used by advanced users! If you see strange results after disabling
|
|
11
|
-
checks, try running the code again with checks enabled.
|
|
12
|
-
|
|
13
|
-
Checks can either be disabled temporarily
|
|
14
|
-
via a compound statements or globally and permanently by calling the speicific function
|
|
15
|
-
directly.
|
|
16
|
-
|
|
17
|
-
The checks can also controlled wit the environment variable
|
|
18
|
-
``METATENSOR_UNSAFE_DISABLE_CHECKS``. To disable checks set
|
|
19
|
-
|
|
20
|
-
.. code-block:: bash
|
|
21
|
-
|
|
22
|
-
export METATENSOR_UNSAFE_DISABLE_CHECKS=1
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
Note that :py:class:`metatensor.unsafe_enable_checks()` or
|
|
26
|
-
:py:class:`metatensor.unsafe_disable_checks()` overwrite the defintion of the enviroment
|
|
27
|
-
variable.
|
|
28
|
-
"""
|
|
29
|
-
|
|
30
1
|
import os
|
|
31
2
|
|
|
32
3
|
|
|
@@ -48,7 +19,7 @@ def checks_enabled() -> bool:
|
|
|
48
19
|
class _SetChecks:
|
|
49
20
|
"""Private parent class for setting metatensor check control to a certain state.
|
|
50
21
|
|
|
51
|
-
Refer to the docstring of ``
|
|
22
|
+
Refer to the docstring of ``disable_checks`` for more details."""
|
|
52
23
|
|
|
53
24
|
def __init__(self, state):
|
|
54
25
|
global _CHECKS_ENABLED
|
|
@@ -74,7 +45,7 @@ class _SetChecks:
|
|
|
74
45
|
class unsafe_enable_checks(_SetChecks):
|
|
75
46
|
"""Enable metatensor checks.
|
|
76
47
|
|
|
77
|
-
Checks are default enabled. Calling this function
|
|
48
|
+
Checks are default enabled. Calling this function permanently enables all metatensor
|
|
78
49
|
operations checks.
|
|
79
50
|
|
|
80
51
|
>>> import metatensor
|
|
@@ -109,7 +80,7 @@ class unsafe_enable_checks(_SetChecks):
|
|
|
109
80
|
class unsafe_disable_checks(_SetChecks):
|
|
110
81
|
"""Disable metatensor checks.
|
|
111
82
|
|
|
112
|
-
Calling this function
|
|
83
|
+
Calling this function permanently disables all metatensor operations checks.
|
|
113
84
|
|
|
114
85
|
>>> import metatensor
|
|
115
86
|
>>> metatensor.unsafe_disable_checks()
|
{metatensor-operations-0.2.0 → metatensor_operations-0.2.2}/metatensor/operations/_dispatch.py
RENAMED
|
@@ -1,9 +1,18 @@
|
|
|
1
|
+
import re
|
|
1
2
|
import warnings
|
|
2
|
-
from typing import List, Optional, Union
|
|
3
|
+
from typing import List, Optional, Tuple, Union
|
|
3
4
|
|
|
4
5
|
import numpy as np
|
|
5
6
|
|
|
6
|
-
from .
|
|
7
|
+
from ._backend import torch_jit_is_scripting, torch_jit_script
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def parse_version(version):
|
|
11
|
+
match = re.match(r"(\d+)\.(\d+)\.(\d+).*", version)
|
|
12
|
+
if match:
|
|
13
|
+
return tuple(map(int, match.groups()))
|
|
14
|
+
else:
|
|
15
|
+
raise ValueError("Invalid version string format")
|
|
7
16
|
|
|
8
17
|
|
|
9
18
|
try:
|
|
@@ -12,6 +21,7 @@ try:
|
|
|
12
21
|
|
|
13
22
|
torch_dtype = torch.dtype
|
|
14
23
|
torch_device = torch.device
|
|
24
|
+
torch_version = parse_version(torch.__version__)
|
|
15
25
|
|
|
16
26
|
except ImportError:
|
|
17
27
|
|
|
@@ -24,6 +34,8 @@ except ImportError:
|
|
|
24
34
|
class torch_device:
|
|
25
35
|
pass
|
|
26
36
|
|
|
37
|
+
torch_version = (0, 0, 0)
|
|
38
|
+
|
|
27
39
|
|
|
28
40
|
UNKNOWN_ARRAY_TYPE = (
|
|
29
41
|
"unknown array type, only numpy arrays and torch tensors are supported"
|
|
@@ -70,11 +82,11 @@ def all(a, axis: Optional[int] = None):
|
|
|
70
82
|
# torch.all has two implementation, and picks one depending if more than one
|
|
71
83
|
# parameter is given. The second one does not supports setting dim to `None`
|
|
72
84
|
if axis is None:
|
|
73
|
-
return torch.all(
|
|
85
|
+
return torch.all(a)
|
|
74
86
|
else:
|
|
75
|
-
return torch.all(
|
|
87
|
+
return torch.all(a, dim=axis)
|
|
76
88
|
elif isinstance(a, np.ndarray):
|
|
77
|
-
return np.all(a
|
|
89
|
+
return np.all(a, axis=axis)
|
|
78
90
|
else:
|
|
79
91
|
raise TypeError(UNKNOWN_ARRAY_TYPE)
|
|
80
92
|
|
|
@@ -93,12 +105,10 @@ def allclose(
|
|
|
93
105
|
"""
|
|
94
106
|
if isinstance(a, TorchTensor):
|
|
95
107
|
_check_all_torch_tensor([b])
|
|
96
|
-
return torch.allclose(
|
|
97
|
-
input=a, other=b, rtol=rtol, atol=atol, equal_nan=equal_nan
|
|
98
|
-
)
|
|
108
|
+
return torch.allclose(a, b, rtol=rtol, atol=atol, equal_nan=equal_nan)
|
|
99
109
|
elif isinstance(a, np.ndarray):
|
|
100
110
|
_check_all_np_ndarray([b])
|
|
101
|
-
return np.allclose(a
|
|
111
|
+
return np.allclose(a, b, rtol=rtol, atol=atol, equal_nan=equal_nan)
|
|
102
112
|
else:
|
|
103
113
|
raise TypeError(UNKNOWN_ARRAY_TYPE)
|
|
104
114
|
|
|
@@ -114,13 +124,139 @@ def argsort_labels_values(labels_values, reverse: bool = False):
|
|
|
114
124
|
:return: indices corresponding to the sorted values in ``labels_values``
|
|
115
125
|
"""
|
|
116
126
|
if isinstance(labels_values, TorchTensor):
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
127
|
+
if labels_values.shape[1] == 1:
|
|
128
|
+
# Index is appended at the end to get
|
|
129
|
+
# the indices corresponding to the
|
|
130
|
+
# sorted values. Because we append the indices at
|
|
131
|
+
# the end and since metadata is unique, we do not affect
|
|
132
|
+
# the sorted order. Using `torch.argsort` is not an option
|
|
133
|
+
# because only on Tensors and sorting a Tensor is
|
|
134
|
+
# different than sorting a List(Tuple)
|
|
135
|
+
data = [(int(row[0]), i) for i, row in enumerate(labels_values)]
|
|
136
|
+
return torch.tensor(
|
|
137
|
+
[i[-1] for i in sort_list_2(data, reverse=reverse)],
|
|
138
|
+
dtype=torch.int64,
|
|
139
|
+
device=labels_values.device,
|
|
140
|
+
)
|
|
141
|
+
if labels_values.shape[1] == 2:
|
|
142
|
+
data = [
|
|
143
|
+
(int(row[0]), int(row[1]), i) for i, row in enumerate(labels_values)
|
|
144
|
+
]
|
|
145
|
+
return torch.tensor(
|
|
146
|
+
[i[-1] for i in sort_list_3(data, reverse=reverse)],
|
|
147
|
+
dtype=torch.int64,
|
|
148
|
+
device=labels_values.device,
|
|
149
|
+
)
|
|
150
|
+
if labels_values.shape[1] == 3:
|
|
151
|
+
data = [
|
|
152
|
+
(int(row[0]), int(row[1]), int(row[2]), i)
|
|
153
|
+
for i, row in enumerate(labels_values)
|
|
154
|
+
]
|
|
155
|
+
return torch.tensor(
|
|
156
|
+
[i[-1] for i in sort_list_4(data, reverse=reverse)],
|
|
157
|
+
dtype=torch.int64,
|
|
158
|
+
device=labels_values.device,
|
|
159
|
+
)
|
|
160
|
+
if labels_values.shape[1] == 4:
|
|
161
|
+
data = [
|
|
162
|
+
(int(row[0]), int(row[1]), int(row[2]), int(row[3]), i)
|
|
163
|
+
for i, row in enumerate(labels_values)
|
|
164
|
+
]
|
|
165
|
+
return torch.tensor(
|
|
166
|
+
[i[-1] for i in sort_list_5(data, reverse=reverse)],
|
|
167
|
+
dtype=torch.int64,
|
|
168
|
+
device=labels_values.device,
|
|
169
|
+
)
|
|
170
|
+
if labels_values.shape[1] == 5:
|
|
171
|
+
data = [
|
|
172
|
+
(int(row[0]), int(row[1]), int(row[2]), int(row[3]), int(row[4]), i)
|
|
173
|
+
for i, row in enumerate(labels_values)
|
|
174
|
+
]
|
|
175
|
+
return torch.tensor(
|
|
176
|
+
[i[-1] for i in sort_list_6(data, reverse=reverse)],
|
|
177
|
+
dtype=torch.int64,
|
|
178
|
+
device=labels_values.device,
|
|
179
|
+
)
|
|
180
|
+
if labels_values.shape[1] == 6:
|
|
181
|
+
data = [
|
|
182
|
+
(
|
|
183
|
+
int(row[0]),
|
|
184
|
+
int(row[1]),
|
|
185
|
+
int(row[2]),
|
|
186
|
+
int(row[3]),
|
|
187
|
+
int(row[4]),
|
|
188
|
+
int(row[5]),
|
|
189
|
+
i,
|
|
190
|
+
)
|
|
191
|
+
for i, row in enumerate(labels_values)
|
|
192
|
+
]
|
|
193
|
+
return torch.tensor(
|
|
194
|
+
[i[-1] for i in sort_list_7(data, reverse=reverse)],
|
|
195
|
+
dtype=torch.int64,
|
|
196
|
+
device=labels_values.device,
|
|
197
|
+
)
|
|
198
|
+
if labels_values.shape[1] == 7:
|
|
199
|
+
data = [
|
|
200
|
+
(
|
|
201
|
+
int(row[0]),
|
|
202
|
+
int(row[1]),
|
|
203
|
+
int(row[2]),
|
|
204
|
+
int(row[3]),
|
|
205
|
+
int(row[4]),
|
|
206
|
+
int(row[5]),
|
|
207
|
+
int(row[6]),
|
|
208
|
+
i,
|
|
209
|
+
)
|
|
210
|
+
for i, row in enumerate(labels_values)
|
|
211
|
+
]
|
|
212
|
+
return torch.tensor(
|
|
213
|
+
[i[-1] for i in sort_list_8(data, reverse=reverse)],
|
|
214
|
+
dtype=torch.int64,
|
|
215
|
+
device=labels_values.device,
|
|
216
|
+
)
|
|
217
|
+
if labels_values.shape[1] == 8:
|
|
218
|
+
data = [
|
|
219
|
+
(
|
|
220
|
+
int(row[0]),
|
|
221
|
+
int(row[1]),
|
|
222
|
+
int(row[2]),
|
|
223
|
+
int(row[3]),
|
|
224
|
+
int(row[4]),
|
|
225
|
+
int(row[5]),
|
|
226
|
+
int(row[6]),
|
|
227
|
+
int(row[7]),
|
|
228
|
+
i,
|
|
229
|
+
)
|
|
230
|
+
for i, row in enumerate(labels_values)
|
|
231
|
+
]
|
|
232
|
+
return torch.tensor(
|
|
233
|
+
[i[-1] for i in sort_list_9(data, reverse=reverse)],
|
|
234
|
+
dtype=torch.int64,
|
|
235
|
+
device=labels_values.device,
|
|
236
|
+
)
|
|
237
|
+
if labels_values.shape[1] == 9:
|
|
238
|
+
data = [
|
|
239
|
+
(
|
|
240
|
+
int(row[0]),
|
|
241
|
+
int(row[1]),
|
|
242
|
+
int(row[2]),
|
|
243
|
+
int(row[3]),
|
|
244
|
+
int(row[4]),
|
|
245
|
+
int(row[5]),
|
|
246
|
+
int(row[6]),
|
|
247
|
+
int(row[7]),
|
|
248
|
+
int(row[8]),
|
|
249
|
+
i,
|
|
250
|
+
)
|
|
251
|
+
for i, row in enumerate(labels_values)
|
|
252
|
+
]
|
|
253
|
+
return torch.tensor(
|
|
254
|
+
[i[-1] for i in sort_list_10(data, reverse=reverse)],
|
|
255
|
+
dtype=torch.int64,
|
|
256
|
+
device=labels_values.device,
|
|
257
|
+
)
|
|
258
|
+
else:
|
|
259
|
+
raise Exception("labels_values.shape[1]> 9 is not supported")
|
|
124
260
|
elif isinstance(labels_values, np.ndarray):
|
|
125
261
|
# Index is appended at the end to get the indices corresponding to the
|
|
126
262
|
# sorted values. Because we append the indices at the end and since metadata
|
|
@@ -185,7 +321,7 @@ def concatenate(arrays: List[TorchTensor], axis: int):
|
|
|
185
321
|
"""
|
|
186
322
|
if isinstance(arrays[0], TorchTensor):
|
|
187
323
|
_check_all_torch_tensor(arrays)
|
|
188
|
-
return torch.
|
|
324
|
+
return torch.cat(arrays, axis)
|
|
189
325
|
elif isinstance(arrays[0], np.ndarray):
|
|
190
326
|
_check_all_np_ndarray(arrays)
|
|
191
327
|
return np.concatenate(arrays, axis)
|
|
@@ -508,12 +644,15 @@ def sign(array):
|
|
|
508
644
|
"""
|
|
509
645
|
Returns an indication of the sign of the elements in the array.
|
|
510
646
|
|
|
511
|
-
It is equivalent of np.sign(array) and torch.
|
|
647
|
+
It is equivalent of np.sign(array) (as defined in v2.0) and torch.sgn(tensor)
|
|
512
648
|
"""
|
|
513
649
|
if isinstance(array, TorchTensor):
|
|
514
|
-
return torch.
|
|
650
|
+
return torch.sgn(array)
|
|
515
651
|
elif isinstance(array, np.ndarray):
|
|
516
|
-
|
|
652
|
+
if np.issubdtype(array.dtype, np.complexfloating):
|
|
653
|
+
return array / np.abs(array)
|
|
654
|
+
else:
|
|
655
|
+
return np.sign(array)
|
|
517
656
|
else:
|
|
518
657
|
raise TypeError(UNKNOWN_ARRAY_TYPE)
|
|
519
658
|
|
|
@@ -641,29 +780,47 @@ def to(
|
|
|
641
780
|
raise TypeError(UNKNOWN_ARRAY_TYPE)
|
|
642
781
|
|
|
643
782
|
|
|
644
|
-
def
|
|
645
|
-
"""Returns an array that is suitable for indexing a dimension of
|
|
646
|
-
a different array.
|
|
647
|
-
|
|
648
|
-
After a few checks (int, 1D), this operation will convert the dtype to
|
|
649
|
-
torch.long (which is, in some torch versions, the only acceptable type
|
|
650
|
-
of index tensor). Numpy arrays are left unchanged.
|
|
651
|
-
"""
|
|
783
|
+
def _to_index_array_checks(array):
|
|
652
784
|
if len(array.shape) != 1:
|
|
653
785
|
raise ValueError("Index arrays must be 1D")
|
|
654
786
|
|
|
655
787
|
if isinstance(array, TorchTensor):
|
|
656
788
|
if torch.is_floating_point(array):
|
|
657
789
|
raise ValueError("Index arrays must be integers")
|
|
658
|
-
return array.to(torch.long)
|
|
659
790
|
elif isinstance(array, np.ndarray):
|
|
660
791
|
if not np.issubdtype(array.dtype, np.integer):
|
|
661
792
|
raise ValueError("Index arrays must be integers")
|
|
662
|
-
return array
|
|
663
793
|
else:
|
|
664
794
|
raise TypeError(UNKNOWN_ARRAY_TYPE)
|
|
665
795
|
|
|
666
796
|
|
|
797
|
+
if torch_version >= (2, 0, 0):
|
|
798
|
+
|
|
799
|
+
def to_index_array(array):
|
|
800
|
+
"""
|
|
801
|
+
Returns an array that is suitable for indexing a dimension of
|
|
802
|
+
a different array.
|
|
803
|
+
"""
|
|
804
|
+
_to_index_array_checks(array)
|
|
805
|
+
return array
|
|
806
|
+
|
|
807
|
+
else:
|
|
808
|
+
|
|
809
|
+
def to_index_array(array):
|
|
810
|
+
"""
|
|
811
|
+
Returns an array that is suitable for indexing a dimension of
|
|
812
|
+
a different array, converting torch data to long/64-bit integers
|
|
813
|
+
"""
|
|
814
|
+
_to_index_array_checks(array)
|
|
815
|
+
|
|
816
|
+
if isinstance(array, TorchTensor):
|
|
817
|
+
return array.to(torch.long)
|
|
818
|
+
elif isinstance(array, np.ndarray):
|
|
819
|
+
return array
|
|
820
|
+
else:
|
|
821
|
+
raise TypeError(UNKNOWN_ARRAY_TYPE)
|
|
822
|
+
|
|
823
|
+
|
|
667
824
|
def unique(array, axis: Optional[int] = None):
|
|
668
825
|
"""Find the unique elements of an array."""
|
|
669
826
|
if isinstance(array, TorchTensor):
|
|
@@ -725,3 +882,104 @@ def zeros_like(array, shape: Optional[List[int]] = None, requires_grad: bool = F
|
|
|
725
882
|
return np.zeros_like(array, shape=shape, subok=False)
|
|
726
883
|
else:
|
|
727
884
|
raise TypeError(UNKNOWN_ARRAY_TYPE)
|
|
885
|
+
|
|
886
|
+
|
|
887
|
+
@torch_jit_script
|
|
888
|
+
def sort_list_2(
|
|
889
|
+
data: List[Tuple[int, int]], reverse: bool = False
|
|
890
|
+
) -> List[Tuple[int, int]]:
|
|
891
|
+
# Using the `reverse` flag of sorted does not work with TorchScript
|
|
892
|
+
if reverse:
|
|
893
|
+
return list(sorted(data))[::-1]
|
|
894
|
+
else:
|
|
895
|
+
return list(sorted(data))
|
|
896
|
+
|
|
897
|
+
|
|
898
|
+
@torch_jit_script
|
|
899
|
+
def sort_list_3(
|
|
900
|
+
data: List[Tuple[int, int, int]], reverse: bool = False
|
|
901
|
+
) -> List[Tuple[int, int, int]]:
|
|
902
|
+
# Using the `reverse` flag of sorted does not work with TorchScript
|
|
903
|
+
if reverse:
|
|
904
|
+
return list(sorted(data))[::-1]
|
|
905
|
+
else:
|
|
906
|
+
return list(sorted(data))
|
|
907
|
+
|
|
908
|
+
|
|
909
|
+
@torch_jit_script
|
|
910
|
+
def sort_list_4(
|
|
911
|
+
data: List[Tuple[int, int, int, int]], reverse: bool = False
|
|
912
|
+
) -> List[Tuple[int, int, int, int]]:
|
|
913
|
+
# Using the `reverse` flag of sorted does not work with TorchScript
|
|
914
|
+
if reverse:
|
|
915
|
+
return list(sorted(data))[::-1]
|
|
916
|
+
else:
|
|
917
|
+
return list(sorted(data))
|
|
918
|
+
|
|
919
|
+
|
|
920
|
+
@torch_jit_script
|
|
921
|
+
def sort_list_5(
|
|
922
|
+
data: List[Tuple[int, int, int, int, int]], reverse: bool = False
|
|
923
|
+
) -> List[Tuple[int, int, int, int, int]]:
|
|
924
|
+
# Using the `reverse` flag of sorted does not work with TorchScript
|
|
925
|
+
if reverse:
|
|
926
|
+
return list(sorted(data))[::-1]
|
|
927
|
+
else:
|
|
928
|
+
return list(sorted(data))
|
|
929
|
+
|
|
930
|
+
|
|
931
|
+
@torch_jit_script
|
|
932
|
+
def sort_list_6(
|
|
933
|
+
data: List[Tuple[int, int, int, int, int, int]], reverse: bool = False
|
|
934
|
+
) -> List[Tuple[int, int, int, int, int, int]]:
|
|
935
|
+
# Using the `reverse` flag of sorted does not work with TorchScript
|
|
936
|
+
if reverse:
|
|
937
|
+
return list(sorted(data))[::-1]
|
|
938
|
+
else:
|
|
939
|
+
return list(sorted(data))
|
|
940
|
+
|
|
941
|
+
|
|
942
|
+
@torch_jit_script
|
|
943
|
+
def sort_list_7(
|
|
944
|
+
data: List[Tuple[int, int, int, int, int, int, int]], reverse: bool = False
|
|
945
|
+
) -> List[Tuple[int, int, int, int, int, int, int]]:
|
|
946
|
+
# Using the `reverse` flag of sorted does not work with TorchScript
|
|
947
|
+
if reverse:
|
|
948
|
+
return list(sorted(data))[::-1]
|
|
949
|
+
else:
|
|
950
|
+
return list(sorted(data))
|
|
951
|
+
|
|
952
|
+
|
|
953
|
+
@torch_jit_script
|
|
954
|
+
def sort_list_8(
|
|
955
|
+
data: List[Tuple[int, int, int, int, int, int, int, int]], reverse: bool = False
|
|
956
|
+
) -> List[Tuple[int, int, int, int, int, int, int, int]]:
|
|
957
|
+
# Using the `reverse` flag of sorted does not work with TorchScript
|
|
958
|
+
if reverse:
|
|
959
|
+
return list(sorted(data))[::-1]
|
|
960
|
+
else:
|
|
961
|
+
return list(sorted(data))
|
|
962
|
+
|
|
963
|
+
|
|
964
|
+
@torch_jit_script
|
|
965
|
+
def sort_list_9(
|
|
966
|
+
data: List[Tuple[int, int, int, int, int, int, int, int, int]],
|
|
967
|
+
reverse: bool = False,
|
|
968
|
+
) -> List[Tuple[int, int, int, int, int, int, int, int, int]]:
|
|
969
|
+
# Using the `reverse` flag of sorted does not work with TorchScript
|
|
970
|
+
if reverse:
|
|
971
|
+
return list(sorted(data))[::-1]
|
|
972
|
+
else:
|
|
973
|
+
return list(sorted(data))
|
|
974
|
+
|
|
975
|
+
|
|
976
|
+
@torch_jit_script
|
|
977
|
+
def sort_list_10(
|
|
978
|
+
data: List[Tuple[int, int, int, int, int, int, int, int, int, int]],
|
|
979
|
+
reverse: bool = False,
|
|
980
|
+
) -> List[Tuple[int, int, int, int, int, int, int, int, int, int]]:
|
|
981
|
+
# Using the `reverse` flag of sorted does not work with TorchScript
|
|
982
|
+
if reverse:
|
|
983
|
+
return list(sorted(data))[::-1]
|
|
984
|
+
else:
|
|
985
|
+
return list(sorted(data))
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
from typing import List, Union
|
|
2
2
|
|
|
3
|
-
from .
|
|
3
|
+
from ._backend import TensorBlock, TensorMap
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
class NotEqualError(Exception):
|
|
@@ -167,7 +167,7 @@ def _check_blocks_impl(
|
|
|
167
167
|
)
|
|
168
168
|
else:
|
|
169
169
|
raise ValueError(
|
|
170
|
-
f"{metadata}
|
|
170
|
+
f"'{metadata}' does not refer to metadata to check, "
|
|
171
171
|
"choose from 'samples', 'properties' and 'components'"
|
|
172
172
|
)
|
|
173
173
|
return ""
|
|
@@ -6,32 +6,7 @@ Module to find the absolute values of a :py:class:`TensorMap`, returning a new
|
|
|
6
6
|
from typing import List
|
|
7
7
|
|
|
8
8
|
from . import _dispatch
|
|
9
|
-
from .
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
def abs(A: TensorMap) -> TensorMap:
|
|
13
|
-
r"""
|
|
14
|
-
Return a new :py:class:`TensorMap` with the same metadata as A and absolute
|
|
15
|
-
values of ``A``.
|
|
16
|
-
|
|
17
|
-
.. math::
|
|
18
|
-
A \rightarrow = \vert A \vert
|
|
19
|
-
|
|
20
|
-
If gradients are present in ``A``:
|
|
21
|
-
|
|
22
|
-
.. math::
|
|
23
|
-
\nabla(A) \rightarrow \nabla(\vert A \vert) = (A/\vert A \vert)*\nabla A
|
|
24
|
-
|
|
25
|
-
:param A: the input :py:class:`TensorMap`.
|
|
26
|
-
|
|
27
|
-
:return: a new :py:class:`TensorMap` with the same metadata as ``A`` and
|
|
28
|
-
absolute values of ``A``.
|
|
29
|
-
"""
|
|
30
|
-
blocks: List[TensorBlock] = []
|
|
31
|
-
keys = A.keys
|
|
32
|
-
for i in range(len(keys)):
|
|
33
|
-
blocks.append(_abs_block(block=A.block(keys.entry(i))))
|
|
34
|
-
return TensorMap(keys, blocks)
|
|
9
|
+
from ._backend import TensorBlock, TensorMap, torch_jit_script
|
|
35
10
|
|
|
36
11
|
|
|
37
12
|
def _abs_block(block: TensorBlock) -> TensorBlock:
|
|
@@ -74,3 +49,29 @@ def _abs_block(block: TensorBlock) -> TensorBlock:
|
|
|
74
49
|
result_block.add_gradient(parameter, gradient)
|
|
75
50
|
|
|
76
51
|
return result_block
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@torch_jit_script
|
|
55
|
+
def abs(A: TensorMap) -> TensorMap:
|
|
56
|
+
r"""
|
|
57
|
+
Return a new :py:class:`TensorMap` with the same metadata as A and absolute
|
|
58
|
+
values of ``A``.
|
|
59
|
+
|
|
60
|
+
.. math::
|
|
61
|
+
A \rightarrow = \vert A \vert
|
|
62
|
+
|
|
63
|
+
If gradients are present in ``A``:
|
|
64
|
+
|
|
65
|
+
.. math::
|
|
66
|
+
\nabla(A) \rightarrow \nabla(\vert A \vert) = (A/\vert A \vert)*\nabla A
|
|
67
|
+
|
|
68
|
+
:param A: the input :py:class:`TensorMap`.
|
|
69
|
+
|
|
70
|
+
:return: a new :py:class:`TensorMap` with the same metadata as ``A`` and
|
|
71
|
+
absolute values of ``A``.
|
|
72
|
+
"""
|
|
73
|
+
blocks: List[TensorBlock] = []
|
|
74
|
+
keys = A.keys
|
|
75
|
+
for i in range(len(keys)):
|
|
76
|
+
blocks.append(_abs_block(block=A.block(keys.entry(i))))
|
|
77
|
+
return TensorMap(keys, blocks)
|