metatensor-operations 0.3.2__tar.gz → 0.3.4__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.
Files changed (56) hide show
  1. {metatensor_operations-0.3.2/metatensor_operations.egg-info → metatensor_operations-0.3.4}/PKG-INFO +5 -5
  2. metatensor_operations-0.3.4/git_version_info +2 -0
  3. metatensor_operations-0.3.4/metatensor/operations/__init__.py +75 -0
  4. metatensor_operations-0.3.2/metatensor/operations/abs.py → metatensor_operations-0.3.4/metatensor/operations/_abs.py +1 -6
  5. metatensor_operations-0.3.2/metatensor/operations/add.py → metatensor_operations-0.3.4/metatensor/operations/_add.py +3 -3
  6. metatensor_operations-0.3.2/metatensor/operations/allclose.py → metatensor_operations-0.3.4/metatensor/operations/_allclose.py +6 -5
  7. {metatensor_operations-0.3.2 → metatensor_operations-0.3.4}/metatensor/operations/_backend.py +24 -22
  8. metatensor_operations-0.3.2/metatensor/operations/block_from_array.py → metatensor_operations-0.3.4/metatensor/operations/_block_from_array.py +3 -3
  9. {metatensor_operations-0.3.2 → metatensor_operations-0.3.4}/metatensor/operations/_checks.py +16 -17
  10. {metatensor_operations-0.3.2 → metatensor_operations-0.3.4}/metatensor/operations/_dispatch.py +2 -32
  11. metatensor_operations-0.3.2/metatensor/operations/divide.py → metatensor_operations-0.3.4/metatensor/operations/_divide.py +6 -9
  12. metatensor_operations-0.3.2/metatensor/operations/drop_blocks.py → metatensor_operations-0.3.4/metatensor/operations/_drop_blocks.py +3 -3
  13. metatensor_operations-0.3.2/metatensor/operations/empty_like.py → metatensor_operations-0.3.4/metatensor/operations/_empty_like.py +3 -3
  14. metatensor_operations-0.3.2/metatensor/operations/equal_metadata.py → metatensor_operations-0.3.4/metatensor/operations/_equal_metadata.py +55 -35
  15. metatensor_operations-0.3.2/metatensor/operations/filter_blocks.py → metatensor_operations-0.3.4/metatensor/operations/_filter_blocks.py +4 -7
  16. metatensor_operations-0.3.2/metatensor/operations/join.py → metatensor_operations-0.3.4/metatensor/operations/_join.py +8 -8
  17. metatensor_operations-0.3.2/metatensor/operations/lstsq.py → metatensor_operations-0.3.4/metatensor/operations/_lstsq.py +2 -2
  18. metatensor_operations-0.3.2/metatensor/operations/manipulate_dimension.py → metatensor_operations-0.3.4/metatensor/operations/_manipulate_dimension.py +45 -52
  19. metatensor_operations-0.3.2/metatensor/operations/multiply.py → metatensor_operations-0.3.4/metatensor/operations/_multiply.py +5 -8
  20. metatensor_operations-0.3.2/metatensor/operations/one_hot.py → metatensor_operations-0.3.4/metatensor/operations/_one_hot.py +3 -5
  21. metatensor_operations-0.3.2/metatensor/operations/ones_like.py → metatensor_operations-0.3.4/metatensor/operations/_ones_like.py +3 -3
  22. metatensor_operations-0.3.2/metatensor/operations/pow.py → metatensor_operations-0.3.4/metatensor/operations/_pow.py +5 -6
  23. metatensor_operations-0.3.2/metatensor/operations/random_like.py → metatensor_operations-0.3.4/metatensor/operations/_random_like.py +3 -3
  24. metatensor_operations-0.3.2/metatensor/operations/reduce_over_samples.py → metatensor_operations-0.3.4/metatensor/operations/_reduce_over_samples.py +29 -70
  25. metatensor_operations-0.3.2/metatensor/operations/slice.py → metatensor_operations-0.3.4/metatensor/operations/_slice.py +7 -7
  26. metatensor_operations-0.3.2/metatensor/operations/solve.py → metatensor_operations-0.3.4/metatensor/operations/_solve.py +2 -2
  27. metatensor_operations-0.3.2/metatensor/operations/sort.py → metatensor_operations-0.3.4/metatensor/operations/_sort.py +9 -13
  28. metatensor_operations-0.3.2/metatensor/operations/split.py → metatensor_operations-0.3.4/metatensor/operations/_split.py +12 -12
  29. metatensor_operations-0.3.2/metatensor/operations/subtract.py → metatensor_operations-0.3.4/metatensor/operations/_subtract.py +5 -5
  30. metatensor_operations-0.3.2/metatensor/operations/unique_metadata.py → metatensor_operations-0.3.4/metatensor/operations/_unique_metadata.py +6 -14
  31. metatensor_operations-0.3.2/metatensor/operations/zeros_like.py → metatensor_operations-0.3.4/metatensor/operations/_zeros_like.py +3 -3
  32. {metatensor_operations-0.3.2 → metatensor_operations-0.3.4/metatensor_operations.egg-info}/PKG-INFO +5 -5
  33. metatensor_operations-0.3.4/metatensor_operations.egg-info/SOURCES.txt +50 -0
  34. metatensor_operations-0.3.4/metatensor_operations.egg-info/requires.txt +1 -0
  35. {metatensor_operations-0.3.2 → metatensor_operations-0.3.4}/pyproject.toml +3 -3
  36. {metatensor_operations-0.3.2 → metatensor_operations-0.3.4}/setup.py +2 -2
  37. metatensor_operations-0.3.2/git_version_info +0 -2
  38. metatensor_operations-0.3.2/metatensor/operations/__init__.py +0 -72
  39. metatensor_operations-0.3.2/metatensor_operations.egg-info/SOURCES.txt +0 -50
  40. metatensor_operations-0.3.2/metatensor_operations.egg-info/requires.txt +0 -1
  41. {metatensor_operations-0.3.2 → metatensor_operations-0.3.4}/AUTHORS +0 -0
  42. {metatensor_operations-0.3.2 → metatensor_operations-0.3.4}/LICENSE +0 -0
  43. {metatensor_operations-0.3.2 → metatensor_operations-0.3.4}/MANIFEST.in +0 -0
  44. {metatensor_operations-0.3.2 → metatensor_operations-0.3.4}/README.rst +0 -0
  45. /metatensor_operations-0.3.2/metatensor/operations/detach.py → /metatensor_operations-0.3.4/metatensor/operations/_detach.py +0 -0
  46. /metatensor_operations-0.3.2/metatensor/operations/dot.py → /metatensor_operations-0.3.4/metatensor/operations/_dot.py +0 -0
  47. /metatensor_operations-0.3.2/metatensor/operations/equal.py → /metatensor_operations-0.3.4/metatensor/operations/_equal.py +0 -0
  48. /metatensor_operations-0.3.2/metatensor/operations/is_contiguous.py → /metatensor_operations-0.3.4/metatensor/operations/_is_contiguous.py +0 -0
  49. /metatensor_operations-0.3.2/metatensor/operations/make_contiguous.py → /metatensor_operations-0.3.4/metatensor/operations/_make_contiguous.py +0 -0
  50. /metatensor_operations-0.3.2/metatensor/operations/remove_gradients.py → /metatensor_operations-0.3.4/metatensor/operations/_remove_gradients.py +0 -0
  51. /metatensor_operations-0.3.2/metatensor/operations/requires_grad.py → /metatensor_operations-0.3.4/metatensor/operations/_requires_grad.py +0 -0
  52. {metatensor_operations-0.3.2 → metatensor_operations-0.3.4}/metatensor/operations/_utils.py +0 -0
  53. {metatensor_operations-0.3.2 → metatensor_operations-0.3.4}/metatensor_operations.egg-info/dependency_links.txt +0 -0
  54. {metatensor_operations-0.3.2 → metatensor_operations-0.3.4}/metatensor_operations.egg-info/top_level.txt +0 -0
  55. {metatensor_operations-0.3.2 → metatensor_operations-0.3.4}/metatensor_operations.egg-info/zip-safe +0 -0
  56. {metatensor_operations-0.3.2 → metatensor_operations-0.3.4}/setup.cfg +0 -0
@@ -1,9 +1,9 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: metatensor-operations
3
- Version: 0.3.2
3
+ Version: 0.3.4
4
4
  Summary: Operations to manipulate metatensor data types
5
5
  Author: Guillaume Fraux, Philip Loche, Filippo Bigi, Joseph W. Abbott, Davide Tisi, Alexander Goscinski, Michele Ceriotti
6
- License: BSD-3-Clause
6
+ License-Expression: BSD-3-Clause
7
7
  Project-URL: homepage, https://docs.metatensor.org/latest/
8
8
  Project-URL: documentation, https://docs.metatensor.org/latest/
9
9
  Project-URL: repository, https://github.com/metatensor/metatensor
@@ -11,7 +11,6 @@ Project-URL: changelog, https://docs.metatensor.org/latest/operations/CHANGELOG.
11
11
  Keywords: machine learning,molecular modeling
12
12
  Classifier: Development Status :: 4 - Beta
13
13
  Classifier: Intended Audience :: Science/Research
14
- Classifier: License :: OSI Approved :: BSD License
15
14
  Classifier: Operating System :: POSIX
16
15
  Classifier: Operating System :: MacOS :: MacOS X
17
16
  Classifier: Operating System :: Microsoft :: Windows
@@ -27,8 +26,9 @@ Requires-Python: >=3.9
27
26
  Description-Content-Type: text/x-rst
28
27
  License-File: LICENSE
29
28
  License-File: AUTHORS
30
- Requires-Dist: metatensor-core<0.2.0,>=0.1.10
29
+ Requires-Dist: metatensor-core<0.2.0,>=0.1.13
31
30
  Dynamic: author
31
+ Dynamic: license-file
32
32
  Dynamic: requires-dist
33
33
 
34
34
  metatensor-operations
@@ -0,0 +1,2 @@
1
+ 0
2
+ git.d910ecc
@@ -0,0 +1,75 @@
1
+ import importlib.metadata
2
+
3
+
4
+ __version__ = importlib.metadata.version("metatensor-operations")
5
+
6
+
7
+ from ._abs import abs # noqa: F401
8
+ from ._add import add # noqa: F401
9
+ from ._allclose import ( # noqa: F401
10
+ allclose,
11
+ allclose_block,
12
+ allclose_block_raise,
13
+ allclose_raise,
14
+ )
15
+ from ._block_from_array import block_from_array # noqa: F401
16
+ from ._checks import ( # noqa: F401
17
+ checks_enabled,
18
+ unsafe_disable_checks,
19
+ unsafe_enable_checks,
20
+ )
21
+ from ._detach import detach, detach_block # noqa: F401
22
+ from ._divide import divide # noqa: F401
23
+ from ._dot import dot # noqa: F401
24
+ from ._drop_blocks import drop_blocks # noqa: F401
25
+ from ._empty_like import empty_like, empty_like_block # noqa: F401
26
+ from ._equal import equal, equal_block, equal_block_raise, equal_raise # noqa: F401
27
+ from ._equal_metadata import ( # noqa: F401
28
+ equal_metadata,
29
+ equal_metadata_block,
30
+ equal_metadata_block_raise,
31
+ equal_metadata_raise,
32
+ )
33
+ from ._filter_blocks import filter_blocks # noqa: F401
34
+ from ._is_contiguous import ( # noqa: F401
35
+ is_contiguous,
36
+ is_contiguous_block,
37
+ )
38
+ from ._join import join # noqa: F401
39
+ from ._lstsq import lstsq # noqa: F401
40
+ from ._make_contiguous import ( # noqa: F401
41
+ make_contiguous,
42
+ make_contiguous_block,
43
+ )
44
+ from ._manipulate_dimension import ( # noqa: F401
45
+ append_dimension,
46
+ insert_dimension,
47
+ permute_dimensions,
48
+ remove_dimension,
49
+ rename_dimension,
50
+ )
51
+ from ._multiply import multiply # noqa: F401
52
+ from ._one_hot import one_hot # noqa: F401
53
+ from ._ones_like import ones_like, ones_like_block # noqa: F401
54
+ from ._pow import pow # noqa: F401
55
+ from ._random_like import random_uniform_like, random_uniform_like_block # noqa: F401
56
+ from ._reduce_over_samples import ( # noqa: F401
57
+ mean_over_samples,
58
+ mean_over_samples_block,
59
+ std_over_samples,
60
+ std_over_samples_block,
61
+ sum_over_samples,
62
+ sum_over_samples_block,
63
+ var_over_samples,
64
+ var_over_samples_block,
65
+ )
66
+ from ._remove_gradients import remove_gradients, remove_gradients_block # noqa: F401
67
+ from ._requires_grad import requires_grad, requires_grad_block # noqa: F401
68
+ from ._slice import slice, slice_block # noqa: F401
69
+ from ._solve import solve # noqa: F401
70
+ from ._sort import sort, sort_block # noqa: F401
71
+ from ._split import split, split_block # noqa: F401
72
+ from ._subtract import subtract # noqa: F401
73
+ from ._unique_metadata import unique_metadata, unique_metadata_block # noqa: F401
74
+ from ._utils import NotEqualError # noqa: F401
75
+ from ._zeros_like import zeros_like, zeros_like_block # noqa: F401
@@ -1,8 +1,3 @@
1
- """
2
- Module to find the absolute values of a :py:class:`TensorMap`, returning a new
3
- :py:class:`TensorMap`.
4
- """
5
-
6
1
  from typing import List
7
2
 
8
3
  from . import _dispatch
@@ -40,7 +35,7 @@ def _abs_block(block: TensorBlock) -> TensorBlock:
40
35
  # The sign_values have the same dimensions as that of the block.values.
41
36
  # Reshape the sign_values to allow multiplication with gradient.values
42
37
  new_grad = gradient.values[:] * sign_values[
43
- _dispatch.to_index_array(gradient.samples.column("sample"))
38
+ gradient.samples.column("sample")
44
39
  ].reshape([-1] + [1] * diff_components + _shape)
45
40
 
46
41
  gradient = TensorBlock(
@@ -3,7 +3,7 @@ from typing import List, Union
3
3
  from ._backend import (
4
4
  TensorBlock,
5
5
  TensorMap,
6
- is_metatensor_class,
6
+ isinstance_metatensor,
7
7
  torch_jit_is_scripting,
8
8
  torch_jit_script,
9
9
  )
@@ -104,14 +104,14 @@ def add(A: TensorMap, B: Union[int, float, TensorMap]) -> TensorMap:
104
104
  """
105
105
 
106
106
  if not torch_jit_is_scripting():
107
- if not is_metatensor_class(A, TensorMap):
107
+ if not isinstance_metatensor(A, "TensorMap"):
108
108
  raise TypeError(f"`A` must be a metatensor TensorMap, not {type(A)}")
109
109
 
110
110
  blocks: List[TensorBlock] = []
111
111
  if torch_jit_is_scripting():
112
112
  is_tensor_map = isinstance(B, TensorMap)
113
113
  else:
114
- is_tensor_map = is_metatensor_class(B, TensorMap)
114
+ is_tensor_map = isinstance_metatensor(B, "TensorMap")
115
115
 
116
116
  if isinstance(B, (float, int)):
117
117
  B = float(B)
@@ -248,7 +248,7 @@ def allclose_raise(
248
248
  Examples
249
249
  --------
250
250
  >>> import numpy as np
251
- >>> import metatensor
251
+ >>> import metatensor as mts
252
252
  >>> from metatensor import Labels, TensorBlock
253
253
 
254
254
  Create simple block, with one py:obj:`np.nan` value.
@@ -311,7 +311,7 @@ def allclose_raise(
311
311
  If this is executed yourself, you will see a nested exception explaining that the
312
312
  ``values`` of the two blocks are not `allclose`.
313
313
 
314
- >>> allclose_raise(tensor_1, tensor_2)
314
+ >>> mts.allclose_raise(tensor_1, tensor_2)
315
315
  Traceback (most recent call last):
316
316
  ...
317
317
  metatensor.operations._utils.NotEqualError: blocks for key (key=0) are different: \
@@ -364,6 +364,7 @@ def allclose_block(
364
364
  Examples
365
365
  --------
366
366
  >>> import numpy as np
367
+ >>> import metatensor as mts
367
368
  >>> from metatensor import Labels, TensorBlock
368
369
 
369
370
  Create simple block
@@ -414,7 +415,7 @@ def allclose_block(
414
415
  because the default ``rtol`` is ``1e-13``, and the difference in the first value
415
416
  between the two blocks is ``1e-5``.
416
417
 
417
- >>> allclose_block(block_1, block_2)
418
+ >>> mts.allclose_block(block_1, block_2)
418
419
  False
419
420
 
420
421
  Calling :py:func:`metatensor.allclose_block()` with the optional argument
@@ -458,7 +459,7 @@ def allclose_block_raise(
458
459
  Examples
459
460
  --------
460
461
  >>> import numpy as np
461
- >>> import metatensor
462
+ >>> import metatensor as mts
462
463
  >>> from metatensor import Labels, TensorBlock
463
464
 
464
465
  Create simple block
@@ -510,7 +511,7 @@ def allclose_block_raise(
510
511
  :py:func:`metatensor.NotEqualError` because the properties of the two blocks are not
511
512
  `equal`.
512
513
 
513
- >>> allclose_block_raise(block_1, block_2)
514
+ >>> mts.allclose_block_raise(block_1, block_2)
514
515
  Traceback (most recent call last):
515
516
  ...
516
517
  metatensor.operations._utils.NotEqualError: inputs to 'allclose' should have the \
@@ -68,27 +68,29 @@ def _version_at_least(version, expected):
68
68
  return version >= expected
69
69
 
70
70
 
71
- def is_metatensor_class(value, typ):
72
- assert typ in (Labels, TensorBlock, TensorMap)
73
-
74
- if isinstance(value, typ):
75
- return True
71
+ # Warning: this function (as all functions in this module) is part of the public API of
72
+ # metatensor-operations, updating it means that new versions of metatensor-torch will
73
+ # not be able to work with old versions of metatensor-operations, so any update should
74
+ # be treated as a breaking change.
75
+ def isinstance_metatensor(value, typename):
76
+ assert typename in ("Labels", "TensorBlock", "TensorMap")
77
+
78
+ if _HAS_TORCH and isinstance(value, torch.ScriptObject):
79
+ is_metatensor_torch_class = "metatensor" in str(value._type())
80
+ if is_metatensor_torch_class:
81
+ warnings.warn(
82
+ "Trying to use operations from metatensor with objects from "
83
+ "metatensor-torch, you should use the operation from "
84
+ "`metatensor.torch` as well, e.g. `metatensor.torch.add(...)` "
85
+ "instead of `metatensor.add(...)`",
86
+ stacklevel=2,
87
+ )
88
+
89
+ if typename == "Labels":
90
+ return isinstance(value, Labels)
91
+ elif typename == "TensorBlock":
92
+ return isinstance(value, TensorBlock)
93
+ elif typename == "TensorMap":
94
+ return isinstance(value, TensorMap)
76
95
  else:
77
- if _HAS_TORCH and isinstance(value, torch.ScriptObject):
78
- if _version_at_least(torch.__version__, "2.1.0"):
79
- # _type() is only working for torch >= 2.1
80
- is_metatensor_torch_class = "metatensor" in str(value._type())
81
- else:
82
- # we don't know, it's fine
83
- is_metatensor_torch_class = False
84
-
85
- if is_metatensor_torch_class:
86
- warnings.warn(
87
- "Trying to use operations from metatensor with objects from "
88
- "metatensor-torch, you should use the operation from "
89
- "`metatensor.torch` as well, e.g. `metatensor.torch.add(...)` "
90
- "instead of `metatensor.add(...)`",
91
- stacklevel=2,
92
- )
93
-
94
96
  return False
@@ -57,11 +57,11 @@ def block_from_array(
57
57
  corresponding axis. The returned :py:class:`TensorBlock` has no gradients.
58
58
 
59
59
  >>> import numpy as np
60
- >>> import metatensor
60
+ >>> import metatensor as mts
61
61
  >>> # Construct a simple 4D array:
62
62
  >>> array = np.linspace(0, 10, 42).reshape((7, 3, 1, 2))
63
63
  >>> # Transform it into a TensorBlock:
64
- >>> tensor_block = metatensor.block_from_array(array)
64
+ >>> tensor_block = mts.block_from_array(array)
65
65
  >>> print(tensor_block)
66
66
  TensorBlock
67
67
  samples (7): ['sample']
@@ -74,7 +74,7 @@ def block_from_array(
74
74
  >>> # High-dimensional tensor
75
75
  >>> array = np.linspace(0, 10, 60).reshape((2, 3, 5, 1, 2))
76
76
  >>> # Specify axes names:
77
- >>> tensor_block = metatensor.block_from_array(
77
+ >>> tensor_block = mts.block_from_array(
78
78
  ... array, sample_names=["a", "b"], property_names=["y"]
79
79
  ... )
80
80
  >>> print(tensor_block)
@@ -48,27 +48,26 @@ class unsafe_enable_checks(_SetChecks):
48
48
  Checks are default enabled. Calling this function permanently enables all metatensor
49
49
  operations checks.
50
50
 
51
- >>> import metatensor
52
- >>> metatensor.unsafe_enable_checks()
51
+ >>> import metatensor as mts
52
+ >>> mts.unsafe_enable_checks()
53
53
  checks enabled
54
- >>> print(metatensor.checks_enabled())
54
+ >>> print(mts.checks_enabled())
55
55
  True
56
56
 
57
57
  You can also use a compound statement to enable checks only temporarily. This can be
58
58
  useful in nested constructions.
59
59
 
60
- >>> import metatensor
61
- >>> print(metatensor.checks_enabled())
60
+ >>> print(mts.checks_enabled())
62
61
  True
63
- >>> with metatensor.unsafe_disable_checks():
62
+ >>> with mts.unsafe_disable_checks():
64
63
  ... # checks are disabled here
65
- ... print(metatensor.checks_enabled())
66
- ... with metatensor.unsafe_enable_checks():
64
+ ... print(mts.checks_enabled())
65
+ ... with mts.unsafe_enable_checks():
67
66
  ... # checks enabled here again
68
- ... print(metatensor.checks_enabled())
67
+ ... print(mts.checks_enabled())
69
68
  False
70
69
  True
71
- >>> print(metatensor.checks_enabled())
70
+ >>> print(mts.checks_enabled())
72
71
  True
73
72
  """
74
73
 
@@ -81,21 +80,21 @@ class unsafe_disable_checks(_SetChecks):
81
80
 
82
81
  Calling this function permanently disables all metatensor operations checks.
83
82
 
84
- >>> import metatensor
85
- >>> metatensor.unsafe_disable_checks()
83
+ >>> import metatensor as mts
84
+ >>> mts.unsafe_disable_checks()
86
85
  checks disabled
87
- >>> print(metatensor.checks_enabled())
86
+ >>> print(mts.checks_enabled())
88
87
  False
89
88
 
90
89
  You can also use a compound statement to disable checks only temporarily.
91
90
 
92
- >>> metatensor.unsafe_enable_checks()
91
+ >>> mts.unsafe_enable_checks()
93
92
  checks enabled
94
- >>> with metatensor.unsafe_disable_checks():
93
+ >>> with mts.unsafe_disable_checks():
95
94
  ... # checks are disabled here
96
- ... print(metatensor.checks_enabled())
95
+ ... print(mts.checks_enabled())
97
96
  False
98
- >>> print(metatensor.checks_enabled())
97
+ >>> print(mts.checks_enabled())
99
98
  True
100
99
  """
101
100
 
@@ -21,7 +21,6 @@ try:
21
21
 
22
22
  torch_dtype = torch.dtype
23
23
  torch_device = torch.device
24
- torch_version = parse_version(torch.__version__)
25
24
 
26
25
  except ImportError:
27
26
 
@@ -34,8 +33,6 @@ except ImportError:
34
33
  class torch_device:
35
34
  pass
36
35
 
37
- torch_version = (0, 0, 0)
38
-
39
36
 
40
37
  UNKNOWN_ARRAY_TYPE = (
41
38
  "unknown array type, only numpy arrays and torch tensors are supported"
@@ -490,7 +487,7 @@ def index_add(output_array, input_array, index):
490
487
  output_array.index_add_(0, torch.tensor(index),input_array)
491
488
 
492
489
  """
493
- index = to_index_array(index)
490
+ _index_array_checks(index)
494
491
  if isinstance(input_array, TorchTensor):
495
492
  if not isinstance(index, TorchTensor):
496
493
  index = torch.tensor(index).to(device=input_array.device)
@@ -860,7 +857,7 @@ def to(
860
857
  raise TypeError(UNKNOWN_ARRAY_TYPE)
861
858
 
862
859
 
863
- def _to_index_array_checks(array):
860
+ def _index_array_checks(array):
864
861
  if len(array.shape) != 1:
865
862
  raise ValueError("Index arrays must be 1D")
866
863
 
@@ -874,33 +871,6 @@ def _to_index_array_checks(array):
874
871
  raise TypeError(UNKNOWN_ARRAY_TYPE)
875
872
 
876
873
 
877
- if torch_version >= (2, 0, 0):
878
-
879
- def to_index_array(array):
880
- """
881
- Returns an array that is suitable for indexing a dimension of
882
- a different array.
883
- """
884
- _to_index_array_checks(array)
885
- return array
886
-
887
- else:
888
-
889
- def to_index_array(array):
890
- """
891
- Returns an array that is suitable for indexing a dimension of
892
- a different array, converting torch data to long/64-bit integers
893
- """
894
- _to_index_array_checks(array)
895
-
896
- if isinstance(array, TorchTensor):
897
- return array.to(torch.long)
898
- elif isinstance(array, np.ndarray):
899
- return array
900
- else:
901
- raise TypeError(UNKNOWN_ARRAY_TYPE)
902
-
903
-
904
874
  def unique(array, axis: Optional[int] = None):
905
875
  """Find the unique elements of an array."""
906
876
  if isinstance(array, TorchTensor):
@@ -1,10 +1,9 @@
1
1
  from typing import List, Union
2
2
 
3
- from . import _dispatch
4
3
  from ._backend import (
5
4
  TensorBlock,
6
5
  TensorMap,
7
- is_metatensor_class,
6
+ isinstance_metatensor,
8
7
  torch_jit_is_scripting,
9
8
  torch_jit_script,
10
9
  )
@@ -75,16 +74,14 @@ def _divide_block_block(block_1: TensorBlock, block_2: TensorBlock) -> TensorBlo
75
74
 
76
75
  gradient_samples_to_values_samples_1 = gradient_1.samples.column("sample")
77
76
  gradient_samples_to_values_samples_2 = gradient_2.samples.column("sample")
78
- gradient_values = -block_1.values[
79
- _dispatch.to_index_array(gradient_samples_to_values_samples_1)
80
- ].reshape(
77
+ gradient_values = -block_1.values[gradient_samples_to_values_samples_1].reshape(
81
78
  [-1] + [1] * diff_components + _shape
82
79
  ) * gradient_2.values / block_2.values[
83
- _dispatch.to_index_array(gradient_samples_to_values_samples_2)
80
+ gradient_samples_to_values_samples_2
84
81
  ].reshape(
85
82
  [-1] + [1] * diff_components + _shape
86
83
  ) ** 2 + gradient_1.values / block_2.values[
87
- _dispatch.to_index_array(gradient_samples_to_values_samples_2)
84
+ gradient_samples_to_values_samples_2
88
85
  ].reshape([-1] + [1] * diff_components + _shape)
89
86
 
90
87
  result_block.add_gradient(
@@ -103,7 +100,7 @@ def _divide_block_block(block_1: TensorBlock, block_2: TensorBlock) -> TensorBlo
103
100
  @torch_jit_script
104
101
  def divide(A: TensorMap, B: Union[float, int, TensorMap]) -> TensorMap:
105
102
  if not torch_jit_is_scripting():
106
- if not is_metatensor_class(A, TensorMap):
103
+ if not isinstance_metatensor(A, "TensorMap"):
107
104
  raise TypeError(f"`A` must be a metatensor TensorMap, not {type(A)}")
108
105
 
109
106
  r"""Return a new :class:`TensorMap` with the values being the element-wise
@@ -136,7 +133,7 @@ def divide(A: TensorMap, B: Union[float, int, TensorMap]) -> TensorMap:
136
133
  if torch_jit_is_scripting():
137
134
  is_tensor_map = isinstance(B, TensorMap)
138
135
  else:
139
- is_tensor_map = is_metatensor_class(B, TensorMap)
136
+ is_tensor_map = isinstance_metatensor(B, "TensorMap")
140
137
 
141
138
  if isinstance(B, (float, int)):
142
139
  B = float(B)
@@ -5,7 +5,7 @@ from ._backend import (
5
5
  Labels,
6
6
  TensorBlock,
7
7
  TensorMap,
8
- is_metatensor_class,
8
+ isinstance_metatensor,
9
9
  torch_jit_is_scripting,
10
10
  torch_jit_script,
11
11
  )
@@ -29,12 +29,12 @@ def drop_blocks(tensor: TensorMap, keys: Labels, copy: bool = False) -> TensorMa
29
29
  """
30
30
  # Check arg types
31
31
  if not torch_jit_is_scripting():
32
- if not is_metatensor_class(tensor, TensorMap):
32
+ if not isinstance_metatensor(tensor, "TensorMap"):
33
33
  raise TypeError(
34
34
  f"`tensor` must be a metatensor TensorMap, not {type(tensor)}"
35
35
  )
36
36
 
37
- if not is_metatensor_class(keys, Labels):
37
+ if not isinstance_metatensor(keys, "Labels"):
38
38
  raise TypeError(f"`keys` must be a metatensor Labels, not {type(keys)}")
39
39
 
40
40
  if not isinstance(copy, bool):
@@ -89,7 +89,7 @@ def empty_like(
89
89
  option is only relevant for torch.
90
90
 
91
91
  >>> import numpy as np
92
- >>> import metatensor
92
+ >>> import metatensor as mts
93
93
  >>> from metatensor import TensorBlock, TensorMap, Labels
94
94
  >>> np.random.seed(1)
95
95
 
@@ -132,7 +132,7 @@ def empty_like(
132
132
  Then we use ``empty_like`` to create a :py:class:`TensorMap` with the same
133
133
  metadata as ``tensor``, but with all values uninitialized.
134
134
 
135
- >>> tensor_empty = metatensor.empty_like(tensor)
135
+ >>> tensor_empty = mts.empty_like(tensor)
136
136
  >>> print(tensor_empty.block(0))
137
137
  TensorBlock
138
138
  samples (4): ['sample']
@@ -143,7 +143,7 @@ def empty_like(
143
143
  Note that if we copy just the gradient ``alpha``, ``beta`` is no longer
144
144
  available.
145
145
 
146
- >>> tensor_empty = metatensor.empty_like(tensor, gradients="alpha")
146
+ >>> tensor_empty = mts.empty_like(tensor, gradients="alpha")
147
147
  >>> print(tensor_empty.block(0).gradients_list())
148
148
  ['alpha']
149
149
  """