This repository has been archived by the owner on Apr 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge tensor creation in numpy and nn utilities
the function random_single_block_no_components_tensor_map is now in one global utilities
- Loading branch information
1 parent
4ccee09
commit f01e400
Showing
4 changed files
with
98 additions
and
124 deletions.
There are no files selected for viewing
Empty file.
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,79 @@ | ||
import functools | ||
|
||
|
||
def random_single_block_no_components_tensor_map(use_torch, use_metatensor_torch): | ||
""" | ||
Create a dummy tensor map to be used in tests. This is the same one as the | ||
tensor map used in `tensor.rs` tests. | ||
""" | ||
if not use_torch and use_metatensor_torch: | ||
raise ValueError( | ||
"torch.TensorMap cannot be created without torch.Tensor block values." | ||
) | ||
if use_metatensor_torch: | ||
import torch | ||
from metatensor.torch import Labels, TensorBlock, TensorMap | ||
|
||
create_int32_array = functools.partial(torch.tensor, dtype=torch.int32) | ||
else: | ||
import numpy as np | ||
from metatensor import Labels, TensorBlock, TensorMap | ||
|
||
create_int32_array = functools.partial(np.array, dtype=np.int32) | ||
|
||
if use_torch: | ||
import torch | ||
|
||
create_random_array = torch.rand | ||
else: | ||
import numpy as np | ||
|
||
create_random_array = np.random.rand | ||
|
||
block_1 = TensorBlock( | ||
values=create_random_array(4, 2), | ||
samples=Labels( | ||
["sample", "structure"], | ||
create_int32_array([[0, 0], [1, 1], [2, 2], [3, 3]]), | ||
), | ||
components=[], | ||
properties=Labels(["properties"], create_int32_array([[0], [1]])), | ||
) | ||
positions_gradient = TensorBlock( | ||
values=create_random_array(7, 3, 2), | ||
samples=Labels( | ||
["sample", "structure", "center"], | ||
create_int32_array( | ||
[ | ||
[0, 0, 1], | ||
[0, 0, 2], | ||
[1, 1, 0], | ||
[1, 1, 1], | ||
[1, 1, 2], | ||
[2, 2, 0], | ||
[3, 3, 0], | ||
], | ||
), | ||
), | ||
components=[Labels(["direction"], create_int32_array([[0], [1], [2]]))], | ||
properties=block_1.properties, | ||
) | ||
block_1.add_gradient("positions", positions_gradient) | ||
|
||
cell_gradient = TensorBlock( | ||
values=create_random_array(4, 6, 2), | ||
samples=Labels( | ||
["sample", "structure"], | ||
create_int32_array([[0, 0], [1, 1], [2, 2], [3, 3]]), | ||
), | ||
components=[ | ||
Labels( | ||
["direction_xx_yy_zz_yz_xz_xy"], | ||
create_int32_array([[0], [1], [2], [3], [4], [5]]), | ||
) | ||
], | ||
properties=block_1.properties, | ||
) | ||
block_1.add_gradient("cell", cell_gradient) | ||
|
||
return TensorMap(Labels.single(), [block_1]) |