Skip to content

Commit

Permalink
Adress review
Browse files Browse the repository at this point in the history
  • Loading branch information
Luthaf committed Jul 4, 2024
1 parent e2d5c01 commit 1313a85
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -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
32 changes: 16 additions & 16 deletions python/metatensor-operations/tests/is_contiguous.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)))
Expand Down
32 changes: 16 additions & 16 deletions python/metatensor-operations/tests/make_contiguous.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))

Expand Down

0 comments on commit 1313a85

Please sign in to comment.