From 2dcc8a9330c69177bb2f8631fc998dcbdecdb63f Mon Sep 17 00:00:00 2001 From: Sadie Louise Bartholomew Date: Tue, 1 Feb 2022 15:03:21 +0000 Subject: [PATCH 01/12] Convert Data.exp from use of func() to use of Dask built-in --- cf/data/data.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cf/data/data.py b/cf/data/data.py index a5cf4bf9fc..f6c709ae54 100644 --- a/cf/data/data.py +++ b/cf/data/data.py @@ -9219,7 +9219,8 @@ def exp(self, inplace=False, i=False): if d.Units: d.Units = _units_1 - d.func(np.exp, inplace=True) + dx = d._get_dask() + d._set_dask(da.exp(dx), reset_mask_hardness=False) return d From 5c50bc4724be1d8f78fe5669c7fa14f7aa848980 Mon Sep 17 00:00:00 2001 From: Sadie Louise Bartholomew Date: Tue, 1 Feb 2022 15:09:03 +0000 Subject: [PATCH 02/12] Convert Data.floor from use of func() to use of Dask built-in --- cf/data/data.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cf/data/data.py b/cf/data/data.py index f6c709ae54..cfc560699b 100644 --- a/cf/data/data.py +++ b/cf/data/data.py @@ -10138,6 +10138,7 @@ def flatten(self, axes=None, inplace=False): @daskified(_DASKIFIED_VERBOSE) @_deprecated_kwarg_check("i") + @_inplace_enabled(default=False) def floor(self, inplace=False, i=False): """Return the floor of the data array. @@ -10164,7 +10165,10 @@ def floor(self, inplace=False, i=False): [-2. -2. -2. -1. 0. 1. 1. 1. 1.] """ - return self.func(np.floor, inplace=inplace) + d = _inplace_enabled_define_and_cleanup(self) + dx = d._get_dask() + d._set_dask(da.floor(dx), reset_mask_hardness=False) + return d @_deprecated_kwarg_check("i") def outerproduct(self, e, inplace=False, i=False): From 9ad5fe603586914aefecad9464be6d55e0671fea Mon Sep 17 00:00:00 2001 From: Sadie Louise Bartholomew Date: Tue, 1 Feb 2022 15:12:58 +0000 Subject: [PATCH 03/12] Convert Data.rint from use of func() to use of Dask built-in --- cf/data/data.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cf/data/data.py b/cf/data/data.py index cfc560699b..e05419cc25 100644 --- a/cf/data/data.py +++ b/cf/data/data.py @@ -11027,6 +11027,7 @@ def isclose(self, y, rtol=None, atol=None): @daskified(_DASKIFIED_VERBOSE) @_deprecated_kwarg_check("i") + @_inplace_enabled(default=False) def rint(self, inplace=False, i=False): """Round the data to the nearest integer, element-wise. @@ -11055,7 +11056,10 @@ def rint(self, inplace=False, i=False): [-2. -2. -1. -1. 0. 1. 1. 2. 2.] """ - return self.func(np.rint, inplace=inplace) + d = _inplace_enabled_define_and_cleanup(self) + dx = d._get_dask() + d._set_dask(da.rint(dx), reset_mask_hardness=False) + return d def root_mean_square( self, From 07f6b0f35d55ae009b1adb7bb36695048c1da3db Mon Sep 17 00:00:00 2001 From: Sadie Louise Bartholomew Date: Tue, 1 Feb 2022 15:17:38 +0000 Subject: [PATCH 04/12] Convert Data.round from use of func() to use of Dask built-in --- cf/data/data.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cf/data/data.py b/cf/data/data.py index e05419cc25..28bb53d6f6 100644 --- a/cf/data/data.py +++ b/cf/data/data.py @@ -11142,6 +11142,7 @@ def root_mean_square( @daskified(_DASKIFIED_VERBOSE) @_deprecated_kwarg_check("i") + @_inplace_enabled(default=False) def round(self, decimals=0, inplace=False, i=False): """Evenly round elements of the data array to the given number of decimals. @@ -11185,7 +11186,10 @@ def round(self, decimals=0, inplace=False, i=False): [-0., -0., -0., -0., 0., 0., 0., 0., 0.] """ - return self.func(np.round, inplace=inplace, decimals=decimals) + d = _inplace_enabled_define_and_cleanup(self) + dx = d._get_dask() + d._set_dask(da.round(dx, decimals=decimals), reset_mask_hardness=False) + return d def stats( self, From 854d9ecd8424f8f0d5a0c003df0a81bcd2c40b92 Mon Sep 17 00:00:00 2001 From: Sadie Louise Bartholomew Date: Tue, 1 Feb 2022 15:19:47 +0000 Subject: [PATCH 05/12] Convert Data.trunc from use of func() to use of Dask built-in --- cf/data/data.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cf/data/data.py b/cf/data/data.py index 28bb53d6f6..a90cff15df 100644 --- a/cf/data/data.py +++ b/cf/data/data.py @@ -12315,6 +12315,7 @@ def transpose(self, axes=None, inplace=False, i=False): @daskified(_DASKIFIED_VERBOSE) @_deprecated_kwarg_check("i") + @_inplace_enabled(default=False) def trunc(self, inplace=False, i=False): """Return the truncated values of the data array. @@ -12345,7 +12346,10 @@ def trunc(self, inplace=False, i=False): [-1. -1. -1. -1. 0. 1. 1. 1. 1.] """ - return self.func(np.trunc, inplace=inplace) + d = _inplace_enabled_define_and_cleanup(self) + dx = d._get_dask() + d._set_dask(da.trunc(dx), reset_mask_hardness=False) + return d @classmethod def empty( From 4f39970a0e2b4aebc707723be856d8b92e6c682d Mon Sep 17 00:00:00 2001 From: Sadie Louise Bartholomew Date: Tue, 1 Feb 2022 15:23:16 +0000 Subject: [PATCH 06/12] Convert Data.ceil from use of func() to use of Dask built-in --- cf/data/data.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cf/data/data.py b/cf/data/data.py index a90cff15df..d347e422f9 100644 --- a/cf/data/data.py +++ b/cf/data/data.py @@ -2824,6 +2824,7 @@ def can_compute(self, functions=None, log_levels=None, override=False): @daskified(_DASKIFIED_VERBOSE) @_deprecated_kwarg_check("i") + @_inplace_enabled(default=False) def ceil(self, inplace=False, i=False): """The ceiling of the data, element-wise. @@ -2855,7 +2856,10 @@ def ceil(self, inplace=False, i=False): [-1. -1. -1. -1. 0. 1. 2. 2. 2.] """ - return self.func(np.ceil, inplace=inplace) + d = _inplace_enabled_define_and_cleanup(self) + dx = d._get_dask() + d._set_dask(da.ceil(dx), reset_mask_hardness=False) + return d @daskified(_DASKIFIED_VERBOSE) @_inplace_enabled(default=False) From cf3c85092eaccac76b232420b2a65f474cbeb98f Mon Sep 17 00:00:00 2001 From: Sadie Louise Bartholomew Date: Tue, 1 Feb 2022 15:49:40 +0000 Subject: [PATCH 07/12] Convert Data.log from use of func() to use of Dask built-in --- cf/data/data.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/cf/data/data.py b/cf/data/data.py index d347e422f9..78b8f253eb 100644 --- a/cf/data/data.py +++ b/cf/data/data.py @@ -12016,9 +12016,7 @@ def tanh(self, inplace=False): return d - # TODOASK: daskified except in the case of arbitrary base (not e, 2 or 10) - # which requires `__itruediv__` to be daskified. - # @daskified(_DASKIFIED_VERBOSE) + @daskified(_DASKIFIED_VERBOSE) @_deprecated_kwarg_check("i") @_inplace_enabled(default=False) def log(self, base=None, inplace=False, i=False): @@ -12038,16 +12036,21 @@ def log(self, base=None, inplace=False, i=False): """ d = _inplace_enabled_define_and_cleanup(self) + dx = d._get_dask() if base is None: - d.func(np.log, units=_units_1, inplace=True) + dx = da.log(dx) elif base == 10: - d.func(np.log10, units=_units_1, inplace=True) + dx = da.log10(dx) elif base == 2: - d.func(np.log2, units=_units_1, inplace=True) + dx = da.log2(dx) else: - d.func(np.log, units=_units_1, inplace=True) - d /= np.log(base) + dx = da.log(dx) + dx /= da.log(base) + + d._set_dask(dx, reset_mask_hardness=False) + + d._Units = _units_1 # all logarithm outputs are unitless return d From 0349624c314342432b60b975c1da7cd57b13f077 Mon Sep 17 00:00:00 2001 From: Sadie Louise Bartholomew Date: Tue, 1 Feb 2022 15:53:15 +0000 Subject: [PATCH 08/12] Fix #303 by reinstating Data.log test which passes after np.->da.log --- cf/test/test_Data.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/cf/test/test_Data.py b/cf/test/test_Data.py index 66421119af..1b5cd35957 100644 --- a/cf/test/test_Data.py +++ b/cf/test/test_Data.py @@ -3391,14 +3391,12 @@ def test_Data_log(self): self.assertEqual(d.shape, b.shape) # Test an arbitrary base, using 4 (not a special managed case like 10) - # TODODASK: reinstate this assertion once mask property is - # daskified. - # a = np.array([[4, 16, 4**3.5], [0, 1, 0.25]]) - # b = np.log(a) / np.log(4) # the numpy way, using log rules from school - # c = cf.Data(a, "s") - # d = c.log(base=4) - # self.assertTrue((d.array == b).all()) - # self.assertEqual(d.shape, b.shape) + a = np.array([[4, 16, 4 ** 3.5], [0, 1, 0.25]]) + b = np.log(a) / np.log(4) # the numpy way, using log rules from school + c = cf.Data(a, "s") + d = c.log(base=4) + self.assertTrue((d.array == b).all()) + self.assertEqual(d.shape, b.shape) # Text values outside of the restricted domain for a log a = np.array([0, -1, -2]) From 27ad42841977f651f580f5966169787b5b098427 Mon Sep 17 00:00:00 2001 From: "Sadie L. Bartholomew" Date: Wed, 2 Feb 2022 17:52:34 +0000 Subject: [PATCH 09/12] Update cf/data/data.py: improve units change in Data.log Co-authored-by: David Hassell --- cf/data/data.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cf/data/data.py b/cf/data/data.py index 78b8f253eb..a86c586cbe 100644 --- a/cf/data/data.py +++ b/cf/data/data.py @@ -12050,7 +12050,7 @@ def log(self, base=None, inplace=False, i=False): d._set_dask(dx, reset_mask_hardness=False) - d._Units = _units_1 # all logarithm outputs are unitless + d.override_units(_units_1, inplace=True) # all logarithm outputs are unitless return d From 58577e94e3e0d4373fb9e583e651a1130f8ab50b Mon Sep 17 00:00:00 2001 From: "Sadie L. Bartholomew" Date: Wed, 2 Feb 2022 18:17:46 +0000 Subject: [PATCH 10/12] Update cf/test/test_Data.py: test units in Data.log unit test Co-authored-by: David Hassell --- cf/test/test_Data.py | 1 + 1 file changed, 1 insertion(+) diff --git a/cf/test/test_Data.py b/cf/test/test_Data.py index 1b5cd35957..30edf80295 100644 --- a/cf/test/test_Data.py +++ b/cf/test/test_Data.py @@ -3397,6 +3397,7 @@ def test_Data_log(self): d = c.log(base=4) self.assertTrue((d.array == b).all()) self.assertEqual(d.shape, b.shape) + self.assertEqual(d.Units, cf.Units("1")) # Text values outside of the restricted domain for a log a = np.array([0, -1, -2]) From 70258a97b0b666e476a06c452c7065068fa25670 Mon Sep 17 00:00:00 2001 From: Sadie Louise Bartholomew Date: Wed, 2 Feb 2022 20:01:29 +0000 Subject: [PATCH 11/12] Convert error message from format-string to f-string --- cf/data/data.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cf/data/data.py b/cf/data/data.py index a86c586cbe..78868d0336 100644 --- a/cf/data/data.py +++ b/cf/data/data.py @@ -9217,7 +9217,7 @@ def exp(self, inplace=False, i=False): if units and not units.isdimensionless: raise ValueError( "Can't take exponential of dimensional " - "quantities: {!r}".format(units) + f"quantities: {units!r}" ) if d.Units: @@ -12050,7 +12050,9 @@ def log(self, base=None, inplace=False, i=False): d._set_dask(dx, reset_mask_hardness=False) - d.override_units(_units_1, inplace=True) # all logarithm outputs are unitless + d.override_units( + _units_1, inplace=True + ) # all logarithm outputs are unitless return d From 59564042cc70e963233d092a988a7c6c9addea03 Mon Sep 17 00:00:00 2001 From: Sadie Louise Bartholomew Date: Wed, 2 Feb 2022 20:10:44 +0000 Subject: [PATCH 12/12] Update test_Data_log comment to indicate specific units testing --- cf/test/test_Data.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cf/test/test_Data.py b/cf/test/test_Data.py index 30edf80295..5d8e678c13 100644 --- a/cf/test/test_Data.py +++ b/cf/test/test_Data.py @@ -3397,9 +3397,11 @@ def test_Data_log(self): d = c.log(base=4) self.assertTrue((d.array == b).all()) self.assertEqual(d.shape, b.shape) + + # Check units for general case self.assertEqual(d.Units, cf.Units("1")) - # Text values outside of the restricted domain for a log + # Text values outside of the restricted domain for a logarithm a = np.array([0, -1, -2]) b = np.log(a) c = cf.Data(a)