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

dask: Data.inspect #394

Merged
merged 5 commits into from
May 9, 2022
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
22 changes: 18 additions & 4 deletions cf/data/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ def __bool__(self):
"elements is ambiguous. Use d.any() or d.all()"
)

return bool(self._get_dask())
return bool(self.to_dask_array())

def __repr__(self):
"""Called by the `repr` built-in function.
Expand Down Expand Up @@ -5665,7 +5665,7 @@ def all(self, axis=None, keepdims=True, split_every=None):

"""
d = self.copy(array=False)
dx = self._get_dask()
dx = self.to_dask_array()
dx = da.all(dx, axis=axis, keepdims=keepdims, split_every=split_every)
d._set_dask(dx, reset_mask_hardness=False)
d.hardmask = _DEFAULT_HARDMASK
Expand Down Expand Up @@ -5782,7 +5782,7 @@ def any(self, axis=None, keepdims=True, split_every=None):

"""
d = self.copy(array=False)
dx = self._get_dask()
dx = self.to_dask_array()
dx = da.any(dx, axis=axis, keepdims=keepdims, split_every=split_every)
d._set_dask(dx, reset_mask_hardness=False)
d.hardmask = _DEFAULT_HARDMASK
Expand Down Expand Up @@ -9544,6 +9544,7 @@ def flip(self, axes=None, inplace=False, i=False):

return d

@daskified(_DASKIFIED_VERBOSE)
def inspect(self):
"""Inspect the object for debugging.

Expand All @@ -9553,10 +9554,23 @@ def inspect(self):

`None`

**Examples**

>>> d = cf.Data([9], 'm')
>>> d.inspect()
<CF Data(1): [9] m>
-------------------
{'_components': {'custom': {'_Units': <Units: m>,
'_axes': ('dim0',),
'_cyclic': set(),
'_hardmask': True,
'dask': dask.array<cf_harden_mask, shape=(1,), dtype=int64, chunksize=(1,), chunktype=numpy.ndarray>},
'netcdf': {}}}

"""
from ..functions import inspect

print(inspect(self)) # pragma: no cover
inspect(self)

def isclose(self, y, rtol=None, atol=None):
"""Return where data are element-wise equal to other,
Expand Down
15 changes: 9 additions & 6 deletions cf/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2688,14 +2688,17 @@ def inspect(self):
`None`

"""
name = repr(self)
out = [name, "".ljust(len(name), "-")]
from pprint import pprint

if hasattr(self, "__dict__"):
for key, value in sorted(self.__dict__.items()):
out.append(f"{key}: {value!r}")
try:
name = repr(self)
except Exception:
name = self.__class__.__name__

print("\n".join(out))
print("\n".join([name, "".ljust(len(name), "-")]))

if hasattr(self, "__dict__"):
pprint(self.__dict__)


def broadcast_array(array, shape):
Expand Down
13 changes: 11 additions & 2 deletions cf/test/test_Data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import contextlib
import datetime
import faulthandler
import inspect
import io
import itertools
import os
import unittest
Expand Down Expand Up @@ -3909,8 +3911,15 @@ def test_Data_rtol(self):
self.assertEqual(d._rtol, cf.rtol())
cf.rtol(0.001)
self.assertEqual(d._rtol, 0.001)



def test_Data_inspect(self):
d = cf.Data([9], "m")

f = io.StringIO()
with contextlib.redirect_stdout(f):
self.assertIsNone(d.inspect())


if __name__ == "__main__":
print("Run date:", datetime.datetime.now())
cf.environment()
Expand Down