-
Notifications
You must be signed in to change notification settings - Fork 300
Tighten purpose of array_masked_to_nans
#2424
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
Changes from all commits
8b902e7
0da8438
12f7efc
7b158c2
f6f14fd
9e94584
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1037,9 +1037,11 @@ def _data_bytes_to_shaped_array(data_bytes, lbpack, boundary_packing, | |
| # Reform in row-column order | ||
| data.shape = data_shape | ||
|
|
||
| # Mask the array? | ||
| # Convert mdi to NaN. | ||
| if mdi in data: | ||
| data = array_masked_to_nans(data, data == mdi) | ||
| if data.dtype.kind == 'i': | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we pay attention to the name of |
||
| data = data.astype(np.dtype('f8')) | ||
| data[data == mdi] = np.nan | ||
|
|
||
| return data | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,4 +4,4 @@ failed to merge into a single cube. | |
| cube.attributes keys differ: 'stuffed' | ||
| cube.cell_methods differ | ||
| cube.shape differs: (3,) != (2,) | ||
| cube data dtype differs: int64 != float64 | ||
| cube data dtype differs: int64 != int8 | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this is fun: it looks like the changes in this PR have fixed this test result -- note how the dtype specified in the error text here now matches to the dtype set in the test code...
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this was what it originally was, but as part of the previous dask work it was changed to The correct handling of checking for |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,42 +31,70 @@ | |
|
|
||
|
|
||
| class Test(tests.IrisTest): | ||
| def test_masked(self): | ||
| masked_array = ma.masked_array([[1.0, 2.0], [3.0, 4.0]], | ||
| mask=[[0, 1], [0, 0]]) | ||
| def _common_checks(self, result): | ||
| self.assertIsInstance(result, np.ndarray) | ||
| self.assertFalse(ma.isMaskedArray(result)) | ||
|
|
||
| result = array_masked_to_nans(masked_array).data | ||
| def test_masked(self): | ||
| mask = [[False, True], [False, False]] | ||
| masked_array = ma.masked_array([[1.0, 2.0], [3.0, 4.0]], mask=mask) | ||
|
|
||
| self.assertIsInstance(result, np.ndarray) | ||
| self.assertFalse(isinstance(result, ma.MaskedArray)) | ||
| self.assertFalse(ma.is_masked(result)) | ||
| result = array_masked_to_nans(masked_array) | ||
|
|
||
| self.assertArrayAllClose(np.isnan(result), | ||
| [[False, True], [False, False]]) | ||
| self._common_checks(result) | ||
| self.assertArrayAllClose(np.isnan(result), mask) | ||
| result[0, 1] = 777.7 | ||
| self.assertArrayAllClose(result, [[1.0, 777.7], [3.0, 4.0]]) | ||
|
|
||
| def test_unmasked(self): | ||
| unmasked_array = np.array([1.0, 2.0]) | ||
| result = array_masked_to_nans(unmasked_array) | ||
| # Non-masked array is returned as-is, without copying. | ||
| self.assertIs(result, unmasked_array) | ||
|
|
||
| def test_empty_mask(self): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Masked arrays being the horrible things they are, it might be worth checking also the case where the 'mask' keyword is not set (the actual default is "mask=np.ma.nomask"). Urrrgh! Don't get me started...
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if this is getting a little close to testing the actual |
||
| masked_array = ma.masked_array([1.0, 2.0], mask=[0, 0]) | ||
|
|
||
| result = array_masked_to_nans(masked_array).data | ||
| result = array_masked_to_nans(masked_array) | ||
|
|
||
| self.assertIsInstance(result, np.ndarray) | ||
| self.assertFalse(isinstance(result, ma.MaskedArray)) | ||
| self.assertFalse(ma.is_masked(result)) | ||
| self._common_checks(result) | ||
| self.assertArrayAllClose(result, masked_array.data) | ||
|
|
||
| def test_no_mask(self): | ||
| masked_array = ma.masked_array([1.0, 2.0], mask=ma.nomask) | ||
|
|
||
| # self.assertIs(result, masked_array.data) | ||
| # NOTE: Wanted to check that result in this case is delivered without | ||
| # copying. However, it seems that ".data" is not just an internal | ||
| # reference, so copying *does* occur in this case. | ||
| result = array_masked_to_nans(masked_array) | ||
|
|
||
| self._common_checks(result) | ||
| self.assertArrayAllClose(result, masked_array.data) | ||
|
|
||
| def test_non_masked(self): | ||
| unmasked_array = np.array([1.0, 2.0]) | ||
| result = array_masked_to_nans(unmasked_array, mask=False) | ||
| def test_masked__integers(self): | ||
| mask = [[False, True], [False, False]] | ||
| masked_array = ma.masked_array([[1, 2], [3, 4]], mask=mask) | ||
|
|
||
| result = array_masked_to_nans(masked_array) | ||
|
|
||
| self._common_checks(result) | ||
| self.assertEqual(result.dtype, np.dtype('f8')) | ||
| self.assertArrayAllClose(np.isnan(result), mask) | ||
| result[0, 1] = 777.7 | ||
| self.assertArrayAllClose(result, [[1.0, 777.7], [3.0, 4.0]]) | ||
|
|
||
| def test_unmasked__integers(self): | ||
| unmasked_array = np.array([1, 2]) | ||
| result = array_masked_to_nans(unmasked_array) | ||
| # Non-masked array is returned as-is, without copying. | ||
| self.assertIs(result, unmasked_array) | ||
|
|
||
| def test_no_mask__integers(self): | ||
| datatype = np.dtype('i4') | ||
| masked_array = ma.masked_array([1, 2], dtype=datatype, mask=ma.nomask) | ||
|
|
||
| result = array_masked_to_nans(masked_array) | ||
|
|
||
| self._common_checks(result) | ||
| self.assertEqual(result.dtype, datatype) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| tests.main() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is nice 👍