Skip to content

Commit

Permalink
Merge pull request #230 from zacharyburnett/deprecate/copy_arrays
Browse files Browse the repository at this point in the history
replace usages of ``copy_arrays`` with ``memmap`` for ``asdf>=3.1.0``
  • Loading branch information
braingram authored Jul 23, 2024
2 parents e7fbd71 + e686404 commit 9348f89
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- drop support for python 3.9. [#232]

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

0.6.1 (2024-04-05)
------------------
Expand Down
19 changes: 13 additions & 6 deletions asdf_astropy/converters/unit/tests/test_quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@
from asdf.testing import helpers
from astropy import units
from astropy.units import Quantity
from astropy.utils.introspection import minversion
from numpy.testing import assert_array_equal


def asdf_open_memory_mapping_kwarg(memmap: bool) -> dict:
if minversion("asdf", "3.1.0"):
return {"memmap": memmap}
return {"copy_arrays": not memmap}


def create_quantities():
return [
# Scalar:
Expand Down Expand Up @@ -77,7 +84,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 +102,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 +112,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 +135,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 +145,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()
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ extend-ignore = [
"FIX", # flake8-fixme
# Individually ignored checks
"SLF001", # private-member-access
"FBT001", # boolean positional arguments in function definition
]
extend-exclude = ["docs/*"]

Expand Down

0 comments on commit 9348f89

Please sign in to comment.