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

LAMA to Dask: _combined_units & further branch tidy #436

Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 10 additions & 3 deletions cf/data/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3144,7 +3144,7 @@ def _combined_units(self, data1, method, inplace):
return data0, data1, _units_1
else:
# units1 is defined and is not dimensionless
if data0._size > 1:
if data0.size > 1:
raise ValueError(
"Can only raise units to the power of a single "
"value at a time. Asking to raise to the power of "
Expand Down Expand Up @@ -3220,7 +3220,7 @@ def _combined_units(self, data1, method, inplace):
return data0, data1, _units_1
else:
# units0 is defined and is not dimensionless
if data1._size > 1:
if data1.size > 1:
raise ValueError(
"Can only raise units to the power of a single "
"value at a time. Asking to raise to the power of "
Expand Down Expand Up @@ -3372,6 +3372,13 @@ def _binary_operation(self, other, method):
elif inplace:
# Find non-in-place equivalent operator (remove 'i')
equiv_method = method[:2] + method[3:]
# Need to add check in here to ensure that the operation is not
# trying to cast in a way which is invalid. For example, doing
# [an int array] ** float value = [a float array] is fine, but
# doing this in-place would try to chance an int array into a
# float one, which isn't valid casting. Therefore we need to
# catch cases where __i<op>__ isn't possible even if __<op>__
# is due to datatype consistency rules.
result = getattr(dx0, equiv_method)(dx1)
else:
result = getattr(dx0, method)(dx1)
Expand Down Expand Up @@ -8496,7 +8503,7 @@ def datum(self, *index):
index = (slice(-1, None),) * self.ndim
elif isinstance(index, int):
if index < 0:
index += self._size
index += self.size

index = np.unravel_index(index, self.shape)
elif len(index) == self.ndim:
Expand Down
21 changes: 9 additions & 12 deletions cf/test/test_Data.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def test_Data_equals(self):
self.assertTrue(sa2.equals(sa2.copy()))
# Unlike for numeric types, for string-like data as long as the data
# is the same consider the arrays equal, even if the dtype differs.
# TODO DASK: this behaviour will be added via cfdm, test fails for now
# TODODASK: this behaviour will be added via cfdm, test fails for now
# ## self.assertTrue(sa1.equals(sa2))
sa3_data = sa2_data.astype("S5")
sa3 = cf.Data(sa3_data, "m", chunks=mask_test_chunksize)
Expand Down Expand Up @@ -2004,14 +2004,10 @@ def test_Data_BINARY_AND_UNARY_OPERATORS(self):
self.assertTrue(
(d // x).equals(cf.Data(a0 // x, "m"), verbose=1), message
)
# TODODASK SB: re-instate this once _combined_units is sorted,
# presently fails with error:
# AttributeError: 'Data' object has no attribute '_size'
#
# message = "Failed in {!r}**{}".format(d, x)
# self.assertTrue(
# (d ** x).equals(cf.Data(a0 ** x, "m2"), verbose=1), message
# )
message = "Failed in {!r}**{}".format(d, x)
self.assertTrue(
(d**x).equals(cf.Data(a0**x, "m2"), verbose=1), message
)
message = "Failed in {!r}.__truediv__{}".format(d, x)
self.assertTrue(
d.__truediv__(x).equals(
Expand Down Expand Up @@ -2124,9 +2120,10 @@ def test_Data_BINARY_AND_UNARY_OPERATORS(self):
e.equals(cf.Data(a, "m"), verbose=1), message
)

# TODODASK SB: re-instate this once _combined_units is sorted,
# presently fails with error, as with __pow__:
# AttributeError: 'Data' object has no attribute '_size'
# TODO: this test fails due to casting issues. It is actually
# testing against expected behaviour with contradicts that of
# NumPy so we might want to change the logic: see Issue 435,
# github.com/NCAS-CMS/cf-python/issues/435. Skip for now.
# a = a0.copy()
# try:
# a **= x
Expand Down