-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
dataset __repr__
updates
#5580
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
dataset __repr__
updates
#5580
Changes from 4 commits
aef4435
6a8ca19
f171cd9
3dd645b
3896e8e
ac37672
888850c
4239b90
68b6e15
71cf034
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 |
---|---|---|
|
@@ -509,41 +509,60 @@ def test__mapping_repr(display_max_rows, n_vars, n_attr): | |
long_name = "long_name" | ||
a = np.core.defchararray.add(long_name, np.arange(0, n_vars).astype(str)) | ||
b = np.core.defchararray.add("attr_", np.arange(0, n_attr).astype(str)) | ||
c = np.core.defchararray.add("coord", np.arange(0, n_vars).astype(str)) | ||
attrs = {k: 2 for k in b} | ||
coords = dict(time=np.array([0, 1])) | ||
coords = {_c: np.array([0, 1]) for _c in c} | ||
data_vars = dict() | ||
for v in a: | ||
for (v, _c) in zip(a, coords.items()): | ||
data_vars[v] = xr.DataArray( | ||
name=v, | ||
data=np.array([3, 4]), | ||
dims=["time"], | ||
coords=coords, | ||
dims=[_c[0]], | ||
coords=dict([_c]), | ||
) | ||
ds = xr.Dataset(data_vars) | ||
ds.attrs = attrs | ||
|
||
with xr.set_options(display_max_rows=display_max_rows): | ||
|
||
# Parse the data_vars print and show only data_vars rows: | ||
summary = formatting.data_vars_repr(ds.data_vars).split("\n") | ||
summary = formatting.dataset_repr(ds).split("\n") | ||
summary = [v for v in summary if long_name in v] | ||
|
||
# The length should be less than or equal to display_max_rows: | ||
len_summary = len(summary) | ||
data_vars_print_size = min(display_max_rows, len_summary) | ||
assert len_summary == data_vars_print_size | ||
|
||
summary = formatting.data_vars_repr(ds.data_vars).split("\n") | ||
summary = [v for v in summary if long_name in v] | ||
# The length should be equal to the number of data variables | ||
len_summary = len(summary) | ||
assert len_summary == n_vars | ||
|
||
summary = formatting.coords_repr(ds.coords).split("\n") | ||
summary = [v for v in summary if "coord" in v] | ||
# The length should be equal to the number of data variables | ||
len_summary = len(summary) | ||
assert len_summary == n_vars | ||
|
||
summary = formatting.attrs_repr(ds.attrs).split("\n") | ||
summary = [v for v in summary if "attr_" in v] | ||
# The length should be equal to the number of attributes | ||
len_summary = len(summary) | ||
assert len_summary == n_attr | ||
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. related to that, I don't think we need to test this because 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.
Yes, you are right, I missed that. |
||
|
||
with xr.set_options( | ||
display_expand_coords=False, | ||
display_expand_data_vars=False, | ||
display_expand_attrs=False, | ||
): | ||
actual = formatting.dataset_repr(ds) | ||
coord_s = ", ".join([f"{c}: {len(v)}" for c, v in coords.items()]) | ||
expected = dedent( | ||
f"""\ | ||
<xarray.Dataset> | ||
Dimensions: (time: 2) | ||
Coordinates: (1) | ||
Dimensions: ({coord_s}) | ||
Coordinates: (40) | ||
Data variables: ({n_vars}) | ||
Attributes: ({n_attr})""" | ||
) | ||
|
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.
note that
.attrs
is a standard python dict, so there's no customrepr
: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.
Actually, there is a
attrs_repr
inxarray/core/formatting.py
.But I already changed the
whats_new
entry, probably I'll have to revert that again.