Skip to content

Fix as_compatible_data for read-only np.ma.MaskedArray #7788

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

Merged
merged 2 commits into from
May 17, 2023

Conversation

maxhollmann
Copy link
Contributor

@maxhollmann maxhollmann commented Apr 26, 2023

@welcome
Copy link

welcome bot commented Apr 26, 2023

Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient.
If you have questions, some answers may be found in our contributing guidelines.

@kmuehlbauer kmuehlbauer marked this pull request as ready for review April 26, 2023 17:17
@kmuehlbauer
Copy link
Contributor

I've marked this by accident, sorry @maxhollmann. Let us know when you feel this is ready

@maxhollmann
Copy link
Contributor Author

Hi @kmuehlbauer, no worries! It's in draft because can't figure out how to reproduce this bug for the tests. data[mask] = fill_value was crashing when I tried to create a DataArray from a non-writeable MaskedArray I got via netCDF4 from a remote source. data = np.asarray(data, dtype=dtype) didn't set writeable to true in that case, but it does when I create a non-writeable MaskedArray in the tests. Any ideas how to test this properly?

@maxhollmann maxhollmann marked this pull request as draft April 26, 2023 17:45
@kmuehlbauer
Copy link
Contributor

@maxhollmann I'll have a look into this, I think I've seen something like this some time ago.

Maybe you can add the tests to the PR or as comment? This might get more attention and will really help to debug.

@maxhollmann maxhollmann force-pushed the fix-as-compatible-data branch from 0979e27 to 340e14b Compare April 26, 2023 18:00
@maxhollmann
Copy link
Contributor Author

@kmuehlbauer Sure, I pushed the test as I was hoping it would work.

@kmuehlbauer
Copy link
Contributor

kmuehlbauer commented Apr 27, 2023

@maxhollmann I've checked and memory served well, the following issue might be related: #2377. It looks like your use-case is at least connected to @gerritholl's. It would be great if you could add your original use case (as MCVE, if possible) to get more details.

A special case (masked integer arrays) is discussed in #3955. As this might give additional information, it might not exactly fit your problem.

@maxhollmann
Copy link
Contributor Author

@kmuehlbauer For some reason I can't reproduce it anymore. I'll monitor whether it occurs again in the original situation and close this otherwise after some time.

@maxhollmann maxhollmann force-pushed the fix-as-compatible-data branch from 340e14b to 806c3f7 Compare April 28, 2023 10:04
@maxhollmann
Copy link
Contributor Author

@kmuehlbauer Okay, I got it. It only seems to happen with float arrays. I adjusted the test, and it now fails without the fix.

Only tangentially related to this PR, but I noticed that as_compatible_data will modify the original data in this path, since asarray passes through the original and afterwards the masked values are replaced with fill_value. So masked_array > some_dataarray might modify masked_array. Shouldn't it default to creating a copy to prevent this?

@maxhollmann maxhollmann marked this pull request as ready for review April 28, 2023 10:13
@maxhollmann
Copy link
Contributor Author

@kmuehlbauer Do you need any adjustments to merge this?

@maxhollmann maxhollmann force-pushed the fix-as-compatible-data branch from 806c3f7 to 9cd0fb3 Compare May 12, 2023 07:27
@kmuehlbauer
Copy link
Contributor

@maxhollmann We might get at least some more views on this. There have been discussions on handling masked arrays and we should make sure this is exactly the solution we want to have.

@dcherian This changes as_compatible_data. Could you please have another look here? I'm a bit unclear about the implications.

@kmuehlbauer
Copy link
Contributor

@maxhollmann I'm sorry, I'm still finding my way into Xarray. I've taken a closer look at #2377, especially #2377 (comment).

There @shoyer suggested to just use:

data = duck_array_ops.where_method(data, ~mask, fill_value)

instead of

data[mask] = fill_value

I've checked and it works nicely with your test. That way we would get away without the flags test and the special handling will take place in duck_array_ops. Would be great if someone can double check.

@dcherian
Copy link
Contributor

I defer to @shoyer, the solution with where_method seems good to me.

@maxhollmann maxhollmann force-pushed the fix-as-compatible-data branch from 9cd0fb3 to c381dff Compare May 16, 2023 16:19
@maxhollmann
Copy link
Contributor Author

I like it, solves the concern in my previous comment as well. Updated the branch.

@dcherian
Copy link
Contributor

Thanks @maxhollmann can you add a note to whats-new.rst please?

@dcherian dcherian added the plan to merge Final call for comments label May 16, 2023
@maxhollmann maxhollmann force-pushed the fix-as-compatible-data branch from c381dff to 84680ae Compare May 17, 2023 10:56
@maxhollmann
Copy link
Contributor Author

@dcherian Sure, done :)

@dcherian
Copy link
Contributor

Thanks @maxhollmann I pushed a test for #2377.

I see this is your first contribution to Xarray. Welcome! #1792 would be a nice contribution if you're looking for more to do ;)

@dcherian dcherian enabled auto-merge (squash) May 17, 2023 15:45
@dcherian dcherian merged commit 0b6c6c9 into pydata:main May 17, 2023
@welcome
Copy link

welcome bot commented May 17, 2023

Congratulations on completing your first pull request! Welcome to Xarray! We are proud of you, and hope to see you again! celebration gif

dstansby pushed a commit to dstansby/xarray that referenced this pull request Jun 28, 2023
* Fix as_compatible_data for non-writeable float MaskedArray

* Add test for pydata#2377

---------

Co-authored-by: dcherian <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plan to merge Final call for comments
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Comparing scalar xarray with ma.masked fails with ValueError: assignment destination is read-only
3 participants