def test__mapping_repr(display_max_rows, n_vars, n_attr) -> None: long_name = "long_name" a = defchararray.add(long_name, np.arange(0, n_vars).astype(str)) b = defchararray.add("attr_", np.arange(0, n_attr).astype(str)) c = defchararray.add("coord", np.arange(0, n_vars).astype(str)) attrs = {k: 2 for k in b} coords = {_c: np.array([0, 1]) for _c in c} data_vars = dict() for (v, _c) in zip(a, coords.items()): data_vars[v] = xr.DataArray( name=v, data=np.array([3, 4]), 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.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 with xr.set_options( display_max_rows=display_max_rows, display_expand_coords=False, display_expand_data_vars=False, display_expand_attrs=False, ): actual = formatting.dataset_repr(ds) col_width = formatting._calculate_col_width(ds.variables) dims_start = formatting.pretty_print("Dimensions:", col_width) dims_values = formatting.dim_summary_limited(ds, col_width=col_width + 1, max_rows=display_max_rows) expected = f"""\ <xarray.Dataset> {dims_start}({dims_values}) Coordinates: ({n_vars}) Data variables: ({n_vars}) Attributes: ({n_attr})""" expected = dedent(expected) assert actual == expected
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)) attrs = {k: 2 for k in b} coords = dict(time=np.array([0, 1])) data_vars = dict() for v in a: data_vars[v] = xr.DataArray( name=v, data=np.array([3, 4]), dims=["time"], coords=coords, ) 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 = [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
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)) attrs = {k: 2 for k in b} coords = dict(time=np.array([0, 1])) data_vars = dict() for v in a: data_vars[v] = xr.DataArray( name=v, data=np.array([3, 4]), dims=["time"], coords=coords, ) 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 = [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 with xr.set_options( display_expand_coords=False, display_expand_data_vars=False, display_expand_attrs=False, ): actual = formatting.dataset_repr(ds) expected = dedent( f"""\ <xarray.Dataset> Dimensions: (time: 2) Coordinates: (1) Data variables: ({n_vars}) Attributes: ({n_attr})""" ) assert actual == expected