From 1313a85d49aeddc09440bb985183f55e71338880 Mon Sep 17 00:00:00 2001 From: Guillaume Fraux Date: Thu, 4 Jul 2024 15:49:10 +0200 Subject: [PATCH] Adress review --- .../manipulation/make-contiguous.rst | 3 ++ .../tests/is_contiguous.py | 32 +++++++++---------- .../tests/make_contiguous.py | 32 +++++++++---------- 3 files changed, 35 insertions(+), 32 deletions(-) diff --git a/docs/src/operations/reference/manipulation/make-contiguous.rst b/docs/src/operations/reference/manipulation/make-contiguous.rst index 11589a0fc..504247f15 100644 --- a/docs/src/operations/reference/manipulation/make-contiguous.rst +++ b/docs/src/operations/reference/manipulation/make-contiguous.rst @@ -1,6 +1,9 @@ make_contiguous =============== +These operations are equivalent of :py:func:`numpy.ascontiguousarray` in numpy +and :py:meth:`torch.Tensor.contiguous` in torch. + .. autofunction:: metatensor.make_contiguous .. autofunction:: metatensor.make_contiguous_block diff --git a/python/metatensor-operations/tests/is_contiguous.py b/python/metatensor-operations/tests/is_contiguous.py index dab00409a..258933812 100644 --- a/python/metatensor-operations/tests/is_contiguous.py +++ b/python/metatensor-operations/tests/is_contiguous.py @@ -18,22 +18,6 @@ def tensor(): ) -@pytest.fixture -def non_contiguous_tensor(tensor) -> TensorMap: - """ - Make a TensorMap non-contiguous by reversing the order of the samples/properties - rows/columns in all values and gradient blocks. - """ - keys = tensor.keys - new_blocks = [] - - for block in tensor.blocks(): - new_block = _non_contiguous_block(block) - new_blocks.append(new_block) - - return TensorMap(keys=keys, blocks=new_blocks) - - def _non_contiguous_block(block: TensorBlock) -> TensorBlock: """ Make a non-contiguous block by reversing the order in both the main value block and @@ -59,6 +43,22 @@ def _non_contiguous_block(block: TensorBlock) -> TensorBlock: return new_block +@pytest.fixture +def non_contiguous_tensor(tensor) -> TensorMap: + """ + Make a TensorMap non-contiguous by reversing the order of the samples/properties + rows/columns in all values and gradient blocks. + """ + keys = tensor.keys + new_blocks = [] + + for block in tensor.blocks(): + new_block = _non_contiguous_block(block) + new_blocks.append(new_block) + + return TensorMap(keys=keys, blocks=new_blocks) + + def test_is_contiguous_block(tensor): assert metatensor.is_contiguous_block(tensor.block(0)) assert not metatensor.is_contiguous_block(_non_contiguous_block(tensor.block(0))) diff --git a/python/metatensor-operations/tests/make_contiguous.py b/python/metatensor-operations/tests/make_contiguous.py index 5237f1a81..5af2ec3ae 100644 --- a/python/metatensor-operations/tests/make_contiguous.py +++ b/python/metatensor-operations/tests/make_contiguous.py @@ -18,22 +18,6 @@ def tensor(): ) -@pytest.fixture -def non_contiguous_tensor(tensor) -> TensorMap: - """ - Make a TensorMap non-contiguous by reversing the order of the samples/properties - rows/columns in all values and gradient blocks. - """ - keys = tensor.keys - new_blocks = [] - - for _key, block in tensor.items(): - new_block = _non_contiguous_block(block) - new_blocks.append(new_block) - - return TensorMap(keys=keys, blocks=new_blocks) - - def _non_contiguous_block(block: TensorBlock) -> TensorBlock: """ Make a non-contiguous block by reversing the order in both the main value block and @@ -59,6 +43,22 @@ def _non_contiguous_block(block: TensorBlock) -> TensorBlock: return new_block +@pytest.fixture +def non_contiguous_tensor(tensor) -> TensorMap: + """ + Make a TensorMap non-contiguous by reversing the order of the samples/properties + rows/columns in all values and gradient blocks. + """ + keys = tensor.keys + new_blocks = [] + + for _key, block in tensor.items(): + new_block = _non_contiguous_block(block) + new_blocks.append(new_block) + + return TensorMap(keys=keys, blocks=new_blocks) + + def test_make_contiguous_block(tensor): block = _non_contiguous_block(tensor.block(0))