Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue where roundtripping a masked array strips the mask if no values are masked #1803

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
3.4.0 (unreleased)
------------------

-
- Fix issue where roundtripping a masked array with no masked values removes the mask [#1803]

3.3.0 (2024-07-12)
------------------
Expand Down
2 changes: 1 addition & 1 deletion asdf/_core/_converters/ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def to_yaml_tree(self, obj, tag, ctx):
if strides is not None:
result["strides"] = list(strides)

if isinstance(data, ma.MaskedArray) and np.any(data.mask):
if isinstance(data, ma.MaskedArray):
if options.storage_type == "inline":
ctx._blocks._set_array_storage(data.mask, "inline")

Expand Down
30 changes: 20 additions & 10 deletions asdf/_tests/tags/core/tests/test_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,17 +378,26 @@ def test_inline_bare():
assert_array_equal(ff.tree["arr"], [[1, 2, 3, 4], [5, 6, 7, 8]])


def test_mask_roundtrip(tmp_path):
x = np.arange(0, 10, dtype=float)
m = ma.array(x, mask=x > 5)
tree = {"masked_array": m, "unmasked_array": x}
@pytest.mark.parametrize(
"mask",
[
[[False, False, True], [False, True, False], [False, False, False]],
True,
False,
],
)
def test_mask_roundtrip(mask, tmp_path):
array = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 0]])
tree = {
"unmasked": array,
"masked": np.ma.array(array, mask=mask),
}

with roundtrip(tree) as af:
tree = af.tree

m = tree["masked_array"]

assert np.all(m.mask[6:])
# assert_array_equal ignores the mask, so use equality here
assert (tree["masked"] == af["masked"]).all()
# ensure tree validity
assert af["unmasked"] == af["masked"].data
zacharyburnett marked this conversation as resolved.
Show resolved Hide resolved
assert len(af._blocks.blocks) == 2


Expand Down Expand Up @@ -520,7 +529,8 @@ def test_inline_masked_array(tmp_path):

with asdf.open(testfile) as f2:
assert len(list(f2._blocks.blocks)) == 0
assert_array_equal(f.tree["test"], f2.tree["test"])
# assert_array_equal ignores the mask, so use equality here
assert (f.tree["test"] == f2.tree["test"]).all()

with open(testfile, "rb") as fd:
assert b"null" in fd.read()
Expand Down
Loading