Skip to content

Commit

Permalink
replace usages of copy_arrays with memmap
Browse files Browse the repository at this point in the history
add change log entry

modify arguments
  • Loading branch information
zacharyburnett committed Jul 18, 2024
1 parent dab5b4d commit 4edd2d9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- strip None factor for spectral_density in equivalency converter
to avoid deprecation warnings for astropy 7. [#229]

- replace usages of ``copy_arrays`` with ``memmap`` [#230]

0.6.1 (2024-04-05)
------------------
Expand Down
21 changes: 15 additions & 6 deletions asdf_astropy/converters/unit/tests/test_quantity.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import importlib.metadata

import asdf
import numpy as np
import pytest
Expand All @@ -7,6 +9,13 @@
from numpy.testing import assert_array_equal


def asdf_open_memory_mapping_kwarg(memmap: bool) -> dict:
if tuple(int(part) for part in importlib.metadata.version("asdf").split(".")) >= (3, 1, 0):
return {"memmap": memmap}
else :
return {"copy_arrays": not memmap}


def create_quantities():
return [
# Scalar:
Expand Down Expand Up @@ -77,7 +86,7 @@ def test_read_array_value():

def test_memmap(tmp_path):
"""
Test that memmap (copy_arrays=False) works with quantities.
Test that memmap (memmap=True) works with quantities.
Unfortunately, this is not a simple `isinstance(obj, np.memmap)`
Instead it requires a more complicated check.
Expand All @@ -95,7 +104,7 @@ def test_memmap(tmp_path):
af.write_to(file_path)

# Update a value in the ASDF file
with asdf.open(file_path, mode="rw", copy_arrays=False) as af:
with asdf.open(file_path, mode="rw", **asdf_open_memory_mapping_kwarg(memmap=True)) as af:
assert (af.tree["quantity"] == quantity).all()
assert af.tree["quantity"][-1, -1] != new_value

Expand All @@ -105,15 +114,15 @@ def test_memmap(tmp_path):
assert (af.tree["quantity"] != quantity).any()
assert (af.tree["quantity"] == new_quantity).all()

with asdf.open(file_path, mode="rw", copy_arrays=False) as af:
with asdf.open(file_path, mode="rw", **asdf_open_memory_mapping_kwarg(memmap=True)) as af:
assert af.tree["quantity"][-1, -1] == new_value
assert (af.tree["quantity"] != quantity).any()
assert (af.tree["quantity"] == new_quantity).all()


def test_no_memmap(tmp_path):
"""
Test that turning off memmap (copy_arrays=True) works as expected for quantities
Test that turning off memmap (memmap=False) works as expected for quantities
"""
file_path = tmp_path / "test.asdf"
quantity = Quantity(np.arange(100, dtype=np.float64).reshape(5, 20), units.km)
Expand All @@ -128,7 +137,7 @@ def test_no_memmap(tmp_path):
af.write_to(file_path)

# Update a value in the ASDF file
with asdf.open(file_path, mode="rw", copy_arrays=True) as af:
with asdf.open(file_path, mode="rw", asdf_open_memory_mapping_kwarg(memmap=False)) as af:
assert (af.tree["quantity"] == quantity).all()
assert af.tree["quantity"][-1, -1] != new_value

Expand All @@ -138,7 +147,7 @@ def test_no_memmap(tmp_path):
assert (af.tree["quantity"] != quantity).any()
assert (af.tree["quantity"] == new_quantity).all()

with asdf.open(file_path, mode="rw", copy_arrays=True) as af:
with asdf.open(file_path, mode="rw", asdf_open_memory_mapping_kwarg(memmap=False)) as af:
assert af.tree["quantity"][-1, -1] != new_value
assert (af.tree["quantity"] != new_quantity).any()
assert (af.tree["quantity"] == quantity).all()

0 comments on commit 4edd2d9

Please sign in to comment.