metatensor-operations 0.2.3__tar.gz → 0.2.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 (52) hide show
  1. {metatensor_operations-0.2.3/metatensor_operations.egg-info → metatensor_operations-0.2.4}/PKG-INFO +2 -2
  2. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/_dispatch.py +19 -0
  3. metatensor_operations-0.2.4/metatensor/operations/block_from_array.py +165 -0
  4. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4/metatensor_operations.egg-info}/PKG-INFO +2 -2
  5. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/pyproject.toml +1 -1
  6. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/setup.py +1 -1
  7. metatensor_operations-0.2.3/metatensor/operations/block_from_array.py +0 -103
  8. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/AUTHORS +0 -0
  9. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/LICENSE +0 -0
  10. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/MANIFEST.in +0 -0
  11. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/README.rst +0 -0
  12. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/__init__.py +0 -0
  13. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/_backend.py +0 -0
  14. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/_checks.py +0 -0
  15. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/_utils.py +0 -0
  16. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/abs.py +0 -0
  17. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/add.py +0 -0
  18. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/allclose.py +0 -0
  19. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/detach.py +0 -0
  20. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/divide.py +0 -0
  21. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/dot.py +0 -0
  22. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/drop_blocks.py +0 -0
  23. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/empty_like.py +0 -0
  24. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/equal.py +0 -0
  25. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/equal_metadata.py +0 -0
  26. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/is_contiguous.py +0 -0
  27. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/join.py +0 -0
  28. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/lstsq.py +0 -0
  29. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/make_contiguous.py +0 -0
  30. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/manipulate_dimension.py +0 -0
  31. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/multiply.py +0 -0
  32. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/one_hot.py +0 -0
  33. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/ones_like.py +0 -0
  34. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/pow.py +0 -0
  35. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/random_like.py +0 -0
  36. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/reduce_over_samples.py +0 -0
  37. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/remove_gradients.py +0 -0
  38. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/requires_grad.py +0 -0
  39. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/slice.py +0 -0
  40. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/solve.py +0 -0
  41. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/sort.py +0 -0
  42. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/split.py +0 -0
  43. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/subtract.py +0 -0
  44. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/unique_metadata.py +0 -0
  45. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor/operations/zeros_like.py +0 -0
  46. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor_operations.egg-info/SOURCES.txt +0 -0
  47. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor_operations.egg-info/dependency_links.txt +0 -0
  48. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor_operations.egg-info/requires.txt +0 -0
  49. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor_operations.egg-info/top_level.txt +0 -0
  50. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/metatensor_operations.egg-info/zip-safe +0 -0
  51. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/n_commits_since_last_tag +0 -0
  52. {metatensor_operations-0.2.3 → metatensor_operations-0.2.4}/setup.cfg +0 -0
@@ -1,12 +1,12 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: metatensor-operations
3
- Version: 0.2.3
3
+ Version: 0.2.4
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
7
  Project-URL: homepage, https://docs.metatensor.org/latest/
8
8
  Project-URL: documentation, https://docs.metatensor.org/latest/
9
- Project-URL: repository, https://github.com/lab-cosmo/metatensor
9
+ Project-URL: repository, https://github.com/metatensor/metatensor
10
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
@@ -527,6 +527,25 @@ def int_array_like(int_list: List[int], like):
527
527
  raise TypeError(UNKNOWN_ARRAY_TYPE)
528
528
 
529
529
 
530
+ def indices_like(shape: List[int], like):
531
+ """
532
+ Creates a tensor of shape ``shape`` filled with ints that
533
+ enumerate the indices of the entries in the array, e.g.
534
+ ``shape = (3,2)`` returns ``[[0,0],[0,1],[1,0],[1,1],[2,0],[2,1]]``.
535
+ """
536
+
537
+ if isinstance(like, TorchTensor):
538
+ indices = torch.meshgrid(
539
+ [torch.arange(s, dtype=torch.int64, device=like.device) for s in shape],
540
+ indexing="ij",
541
+ )
542
+ return torch.stack(indices, dim=-1).reshape(-1, len(shape))
543
+ elif isinstance(like, np.ndarray):
544
+ return np.indices(shape).reshape(len(shape), -1).T.astype(np.int64)
545
+ else:
546
+ raise TypeError(UNKNOWN_ARRAY_TYPE)
547
+
548
+
530
549
  def lstsq(X, Y, rcond: Optional[float], driver: Optional[str] = None):
531
550
  """
532
551
  Computes a solution to the least squares problem of a system of linear
@@ -0,0 +1,165 @@
1
+ from typing import List, Optional
2
+
3
+ import numpy as np
4
+
5
+ from . import _dispatch
6
+ from ._backend import Labels, TensorBlock, torch_jit_is_scripting, torch_jit_script
7
+
8
+
9
+ try:
10
+ import torch
11
+
12
+ TorchScriptClass = torch.ScriptClass
13
+ except ImportError:
14
+
15
+ class TorchScriptClass:
16
+ pass
17
+
18
+
19
+ @torch_jit_script
20
+ def block_from_array(
21
+ array,
22
+ sample_names: Optional[List[str]] = None,
23
+ component_names: Optional[List[str]] = None,
24
+ property_names: Optional[List[str]] = None,
25
+ ) -> TensorBlock:
26
+ """
27
+ Creates a simple TensorBlock from an array.
28
+
29
+ The metadata in the resulting :py:class:`TensorBlock` is filled with ranges of
30
+ integers. This function should be seen as a quick way of creating a
31
+ :py:class:`TensorBlock` from arbitrary data. However, the metadata generated in this
32
+ way has little meaning.
33
+
34
+ :param array: An array with two or more dimensions. This can either be a
35
+ :py:class:`numpy.ndarray` or a :py:class:`torch.Tensor`.
36
+ :param sample_names: A list containing ``d_samples`` names for the sample
37
+ dimensions. The first ``d_samples`` dimensions in the array will be interpreted
38
+ as enumerating samples. ``None`` implies a single dimension named ``"sample"``.
39
+ :param property_names: A list containing ``d_properties`` names for the property
40
+ dimensions. The last ``d_properties`` dimensions in the array will be
41
+ interpreted as enumerating properties. ``None`` implies a single dimension named
42
+ ``"property"``.
43
+ :param component_names: A list containing ``n_component`` names for the component
44
+ dimensions. The middle ``d_components`` dimensions in the array will be
45
+ interpreted as enumerating components. ``None`` implies that all the middle
46
+ dimensions (after removing any sample and property dimensions) will be
47
+ considered components, named ``"component_xxx"``.
48
+
49
+ :return: A :py:class:`TensorBlock` whose values correspond to the provided
50
+ ``array``. If no name options are provided, the metadata names are set to
51
+ ``"sample"`` for samples; ``"component_1"``, ``"component_2"``, ... for
52
+ components; and ``property`` for properties. The number of ``component`` labels
53
+ is adapted to the dimensionality of the input array. If axes names are given, as
54
+ indicated in the parameter list, the dimensions of the array will be interpreted
55
+ accordingly, and indices also generated in a similar way. The metadata
56
+ associated with each axis is a range of integers going from 0 to the size of the
57
+ corresponding axis. The returned :py:class:`TensorBlock` has no gradients.
58
+
59
+ >>> import numpy as np
60
+ >>> import metatensor
61
+ >>> # Construct a simple 4D array:
62
+ >>> array = np.linspace(0, 10, 42).reshape((7, 3, 1, 2))
63
+ >>> # Transform it into a TensorBlock:
64
+ >>> tensor_block = metatensor.block_from_array(array)
65
+ >>> print(tensor_block)
66
+ TensorBlock
67
+ samples (7): ['sample']
68
+ components (3, 1): ['component_1', 'component_2']
69
+ properties (2): ['property']
70
+ gradients: None
71
+ >>> # The data inside the TensorBlock will correspond to the provided array:
72
+ >>> print(np.all(array == tensor_block.values))
73
+ True
74
+ >>> # High-dimensional tensor
75
+ >>> array = np.linspace(0, 10, 60).reshape((2, 3, 5, 1, 2))
76
+ >>> # Specify axes names:
77
+ >>> tensor_block = metatensor.block_from_array(
78
+ ... array, sample_names=["a", "b"], property_names=["y"]
79
+ ... )
80
+ >>> print(tensor_block)
81
+ TensorBlock
82
+ samples (6): ['a', 'b']
83
+ components (5, 1): ['component_1', 'component_2']
84
+ properties (2): ['y']
85
+ gradients: None
86
+ """
87
+
88
+ if torch_jit_is_scripting():
89
+ # we are using metatensor-torch
90
+ labels_array_like = torch.empty(0)
91
+ else:
92
+ if isinstance(Labels, TorchScriptClass):
93
+ # we are using metatensor-torch
94
+ labels_array_like = torch.empty(0)
95
+ else:
96
+ # we are using metatensor-core
97
+ labels_array_like = np.empty(0)
98
+
99
+ shape = array.shape
100
+ n_dimensions = len(shape)
101
+ if n_dimensions < 2:
102
+ raise ValueError(
103
+ f"the array provided to `block_from_array` \
104
+ must have at least two dimensions. Too few provided: {n_dimensions}"
105
+ )
106
+
107
+ # constructs the default label names and counts
108
+ if sample_names is None:
109
+ sample_names = ["sample"]
110
+ d_samples = len(sample_names)
111
+ if property_names is None:
112
+ property_names = ["property"]
113
+ d_properties = len(property_names)
114
+ # guess number of components
115
+ d_components = n_dimensions - d_samples - d_properties
116
+ if d_components < 0:
117
+ raise ValueError(
118
+ f"the array provided to `block_from_array` with shape {shape} "
119
+ + "does not have enough dimensions to match the sample and property names"
120
+ )
121
+ if component_names is None:
122
+ component_names = [
123
+ f"component_{component_index + 1}"
124
+ for component_index in range(d_components)
125
+ ]
126
+ if len(component_names) != d_components:
127
+ raise ValueError(
128
+ f"the array provided to `block_from_array` with shape {shape} "
129
+ + "does not have enough dimensions to match the given sample, "
130
+ + "component, and property names"
131
+ )
132
+
133
+ samples = Labels(
134
+ names=sample_names,
135
+ values=_dispatch.indices_like(shape[0:d_samples], labels_array_like),
136
+ )
137
+ components = [
138
+ Labels(
139
+ names=[component_names[component_index]],
140
+ values=_dispatch.int_array_like(
141
+ list(range(axis_size)), labels_array_like
142
+ ).reshape(-1, 1),
143
+ )
144
+ for component_index, axis_size in enumerate(shape[d_samples:-d_properties])
145
+ ]
146
+ properties = Labels(
147
+ names=property_names,
148
+ values=_dispatch.indices_like(shape[-d_properties:], labels_array_like),
149
+ )
150
+
151
+ device = _dispatch.get_device(array)
152
+ samples = samples.to(device)
153
+ components = [component.to(device) for component in components]
154
+ properties = properties.to(device)
155
+
156
+ # reshape the array if multiple axes of the input array are grouped as samples or
157
+ # properties (i.e. if `len(sample_names) > 1` or `len(property_names) > 1`)
158
+ if d_samples > 1 or d_properties > 1:
159
+ block_shape = [len(samples)]
160
+ for i in range(d_samples, d_samples + d_components):
161
+ block_shape.append(shape[i])
162
+ block_shape.append(len(properties))
163
+ array = array.reshape(block_shape)
164
+
165
+ return TensorBlock(array, samples, components, properties)
@@ -1,12 +1,12 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: metatensor-operations
3
- Version: 0.2.3
3
+ Version: 0.2.4
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
7
  Project-URL: homepage, https://docs.metatensor.org/latest/
8
8
  Project-URL: documentation, https://docs.metatensor.org/latest/
9
- Project-URL: repository, https://github.com/lab-cosmo/metatensor
9
+ Project-URL: repository, https://github.com/metatensor/metatensor
10
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
@@ -28,7 +28,7 @@ classifiers = [
28
28
  [project.urls]
29
29
  homepage = "https://docs.metatensor.org/latest/"
30
30
  documentation = "https://docs.metatensor.org/latest/"
31
- repository = "https://github.com/lab-cosmo/metatensor"
31
+ repository = "https://github.com/metatensor/metatensor"
32
32
  changelog = "https://docs.metatensor.org/latest/operations/CHANGELOG.html"
33
33
 
34
34
  ### ======================================================================== ###
@@ -12,7 +12,7 @@ from setuptools.command.sdist import sdist
12
12
  ROOT = os.path.realpath(os.path.dirname(__file__))
13
13
  METATENSOR_CORE = os.path.realpath(os.path.join(ROOT, "..", "metatensor-core"))
14
14
 
15
- METATENSOR_OPERATIONS_VERSION = "0.2.3"
15
+ METATENSOR_OPERATIONS_VERSION = "0.2.4"
16
16
 
17
17
 
18
18
  class bdist_egg_disabled(bdist_egg):
@@ -1,103 +0,0 @@
1
- import numpy as np
2
-
3
- from . import _dispatch
4
- from ._backend import Labels, TensorBlock, torch_jit_is_scripting, torch_jit_script
5
-
6
-
7
- try:
8
- import torch
9
-
10
- TorchScriptClass = torch.ScriptClass
11
- except ImportError:
12
-
13
- class TorchScriptClass:
14
- pass
15
-
16
-
17
- @torch_jit_script
18
- def block_from_array(array) -> TensorBlock:
19
- """
20
- Creates a simple TensorBlock from an array.
21
-
22
- The metadata in the resulting :py:class:`TensorBlock` is filled with ranges
23
- of integers. This function should be seen as a quick way of creating a
24
- :py:class:`TensorBlock` from arbitrary data. However, the metadata generated
25
- in this way has little meaning.
26
-
27
- :param array: An array with two or more dimensions. This can either be a
28
- :py:class:`numpy.ndarray` or a :py:class:`torch.Tensor`.
29
-
30
- :return: A :py:class:`TensorBlock` whose values correspond to the provided
31
- ``array``. The metadata names are set to ``"sample"`` for samples;
32
- ``"component_1"``, ``"component_2"``, ... for components; and
33
- ``property`` for properties. The number of ``component`` labels is
34
- adapted to the dimensionality of the input array. The metadata
35
- associated with each label is a range of integers going from 0 to the
36
- size of the corresponding axis. The returned :py:class:`TensorBlock` has
37
- no gradients.
38
-
39
-
40
- >>> import numpy as np
41
- >>> import metatensor
42
- >>> # Construct a simple 4D array:
43
- >>> array = np.linspace(0, 10, 42).reshape((7, 3, 1, 2))
44
- >>> # Transform it into a TensorBlock:
45
- >>> tensor_block = metatensor.block_from_array(array)
46
- >>> print(tensor_block)
47
- TensorBlock
48
- samples (7): ['sample']
49
- components (3, 1): ['component_1', 'component_2']
50
- properties (2): ['property']
51
- gradients: None
52
- >>> # The data inside the TensorBlock will correspond to the provided array:
53
- >>> print(np.all(array == tensor_block.values))
54
- True
55
- """
56
-
57
- shape = array.shape
58
- n_dimensions = len(shape)
59
- if n_dimensions < 2:
60
- raise ValueError(
61
- f"the array provided to `block_from_array` \
62
- must have at least two dimensions. Too few provided: {n_dimensions}"
63
- )
64
-
65
- if torch_jit_is_scripting():
66
- # we are using metatensor-torch
67
- labels_array_like = torch.empty(0)
68
- else:
69
- if isinstance(Labels, TorchScriptClass):
70
- # we are using metatensor-torch
71
- labels_array_like = torch.empty(0)
72
- else:
73
- # we are using metatensor-core
74
- labels_array_like = np.empty(0)
75
-
76
- samples = Labels(
77
- names=["sample"],
78
- values=_dispatch.int_array_like(
79
- list(range(shape[0])), labels_array_like
80
- ).reshape(-1, 1),
81
- )
82
- components = [
83
- Labels(
84
- names=[f"component_{component_index+1}"],
85
- values=_dispatch.int_array_like(
86
- list(range(axis_size)), labels_array_like
87
- ).reshape(-1, 1),
88
- )
89
- for component_index, axis_size in enumerate(shape[1:-1])
90
- ]
91
- properties = Labels(
92
- names=["property"],
93
- values=_dispatch.int_array_like(
94
- list(range(shape[-1])), labels_array_like
95
- ).reshape(-1, 1),
96
- )
97
-
98
- device = _dispatch.get_device(array)
99
- samples = samples.to(device)
100
- components = [component.to(device) for component in components]
101
- properties = properties.to(device)
102
-
103
- return TensorBlock(array, samples, components, properties)