1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
|
from __future__ import annotations
import sys
from textwrap import dedent
import numpy as np
import pandas as pd
import pytest
import xarray as xr
from xarray.core import formatting
from xarray.core.indexes import Index
from xarray.tests import requires_cftime, requires_dask, requires_netCDF4
class CustomIndex(Index):
names: tuple[str, ...]
def __init__(self, names: tuple[str, ...]):
self.names = names
def __repr__(self):
return f"CustomIndex(coords={self.names})"
class TestFormatting:
def test_get_indexer_at_least_n_items(self) -> None:
cases = [
((20,), (slice(10),), (slice(-10, None),)),
((3, 20), (0, slice(10)), (-1, slice(-10, None))),
((2, 10), (0, slice(10)), (-1, slice(-10, None))),
((2, 5), (slice(2), slice(None)), (slice(-2, None), slice(None))),
((1, 2, 5), (0, slice(2), slice(None)), (-1, slice(-2, None), slice(None))),
((2, 3, 5), (0, slice(2), slice(None)), (-1, slice(-2, None), slice(None))),
(
(1, 10, 1),
(0, slice(10), slice(None)),
(-1, slice(-10, None), slice(None)),
),
(
(2, 5, 1),
(slice(2), slice(None), slice(None)),
(slice(-2, None), slice(None), slice(None)),
),
((2, 5, 3), (0, slice(4), slice(None)), (-1, slice(-4, None), slice(None))),
(
(2, 3, 3),
(slice(2), slice(None), slice(None)),
(slice(-2, None), slice(None), slice(None)),
),
]
for shape, start_expected, end_expected in cases:
actual = formatting._get_indexer_at_least_n_items(shape, 10, from_end=False)
assert start_expected == actual
actual = formatting._get_indexer_at_least_n_items(shape, 10, from_end=True)
assert end_expected == actual
def test_first_n_items(self) -> None:
array = np.arange(100).reshape(10, 5, 2)
for n in [3, 10, 13, 100, 200]:
actual = formatting.first_n_items(array, n)
expected = array.flat[:n]
assert (expected == actual).all()
with pytest.raises(ValueError, match=r"at least one item"):
formatting.first_n_items(array, 0)
def test_last_n_items(self) -> None:
array = np.arange(100).reshape(10, 5, 2)
for n in [3, 10, 13, 100, 200]:
actual = formatting.last_n_items(array, n)
expected = array.flat[-n:]
assert (expected == actual).all()
with pytest.raises(ValueError, match=r"at least one item"):
formatting.first_n_items(array, 0)
def test_last_item(self) -> None:
array = np.arange(100)
reshape = ((10, 10), (1, 100), (2, 2, 5, 5))
expected = np.array([99])
for r in reshape:
result = formatting.last_item(array.reshape(r))
assert result == expected
def test_format_item(self) -> None:
cases = [
(pd.Timestamp("2000-01-01T12"), "2000-01-01T12:00:00"),
(pd.Timestamp("2000-01-01"), "2000-01-01"),
(pd.Timestamp("NaT"), "NaT"),
(pd.Timedelta("10 days 1 hour"), "10 days 01:00:00"),
(pd.Timedelta("-3 days"), "-3 days +00:00:00"),
(pd.Timedelta("3 hours"), "0 days 03:00:00"),
(pd.Timedelta("NaT"), "NaT"),
("foo", "'foo'"),
(b"foo", "b'foo'"),
(1, "1"),
(1.0, "1.0"),
(np.float16(1.1234), "1.123"),
(np.float32(1.0111111), "1.011"),
(np.float64(22.222222), "22.22"),
(np.zeros((1, 1)), "[[0.]]"),
(np.zeros(2), "[0. 0.]"),
(np.zeros((2, 2)), "[[0. 0.]\n [0. 0.]]"),
]
for item, expected in cases:
actual = formatting.format_item(item)
assert expected == actual
def test_format_items(self) -> None:
cases = [
(np.arange(4) * np.timedelta64(1, "D"), "0 days 1 days 2 days 3 days"),
(
np.arange(4) * np.timedelta64(3, "h"),
"00:00:00 03:00:00 06:00:00 09:00:00",
),
(
np.arange(4) * np.timedelta64(500, "ms"),
"00:00:00 00:00:00.500000 00:00:01 00:00:01.500000",
),
(pd.to_timedelta(["NaT", "0s", "1s", "NaT"]), "NaT 00:00:00 00:00:01 NaT"), # type: ignore[arg-type, unused-ignore]
(
pd.to_timedelta(["1 day 1 hour", "1 day", "0 hours"]), # type: ignore[arg-type, unused-ignore]
"1 days 01:00:00 1 days 00:00:00 0 days 00:00:00",
),
([1, 2, 3], "1 2 3"),
]
for item, expected in cases:
actual = " ".join(formatting.format_items(item))
assert expected == actual
def test_format_array_flat(self) -> None:
actual = formatting.format_array_flat(np.arange(100), 2)
expected = "..."
assert expected == actual
actual = formatting.format_array_flat(np.arange(100), 9)
expected = "0 ... 99"
assert expected == actual
actual = formatting.format_array_flat(np.arange(100), 10)
expected = "0 1 ... 99"
assert expected == actual
actual = formatting.format_array_flat(np.arange(100), 13)
expected = "0 1 ... 98 99"
assert expected == actual
actual = formatting.format_array_flat(np.arange(100), 15)
expected = "0 1 2 ... 98 99"
assert expected == actual
# NB: Probably not ideal; an alternative would be cutting after the
# first ellipsis
actual = formatting.format_array_flat(np.arange(100.0), 11)
expected = "0.0 ... ..."
assert expected == actual
actual = formatting.format_array_flat(np.arange(100.0), 12)
expected = "0.0 ... 99.0"
assert expected == actual
actual = formatting.format_array_flat(np.arange(3), 5)
expected = "0 1 2"
assert expected == actual
actual = formatting.format_array_flat(np.arange(4.0), 11)
expected = "0.0 ... 3.0"
assert expected == actual
actual = formatting.format_array_flat(np.arange(0), 0)
expected = ""
assert expected == actual
actual = formatting.format_array_flat(np.arange(1), 1)
expected = "0"
assert expected == actual
actual = formatting.format_array_flat(np.arange(2), 3)
expected = "0 1"
assert expected == actual
actual = formatting.format_array_flat(np.arange(4), 7)
expected = "0 1 2 3"
assert expected == actual
actual = formatting.format_array_flat(np.arange(5), 7)
expected = "0 ... 4"
assert expected == actual
long_str = [" ".join(["hello world" for _ in range(100)])]
actual = formatting.format_array_flat(np.asarray([long_str]), 21)
expected = "'hello world hello..."
assert expected == actual
def test_pretty_print(self) -> None:
assert formatting.pretty_print("abcdefghij", 8) == "abcde..."
assert formatting.pretty_print("ß", 1) == "ß"
def test_maybe_truncate(self) -> None:
assert formatting.maybe_truncate("ß", 10) == "ß"
def test_format_timestamp_invalid_pandas_format(self) -> None:
expected = "2021-12-06 17:00:00 00"
with pytest.raises(ValueError):
formatting.format_timestamp(expected)
def test_format_timestamp_out_of_bounds(self) -> None:
from datetime import datetime
date = datetime(1300, 12, 1)
expected = "1300-12-01"
result = formatting.format_timestamp(date)
assert result == expected
date = datetime(2300, 12, 1)
expected = "2300-12-01"
result = formatting.format_timestamp(date)
assert result == expected
def test_attribute_repr(self) -> None:
short = formatting.summarize_attr("key", "Short string")
long = formatting.summarize_attr("key", 100 * "Very long string ")
newlines = formatting.summarize_attr("key", "\n\n\n")
tabs = formatting.summarize_attr("key", "\t\t\t")
assert short == " key: Short string"
assert len(long) <= 80
assert long.endswith("...")
assert "\n" not in newlines
assert "\t" not in tabs
def test_index_repr(self) -> None:
coord_names = ("x", "y")
index = CustomIndex(coord_names)
names = ("x",)
normal = formatting.summarize_index(names, index, col_width=20)
assert names[0] in normal
assert len(normal.splitlines()) == len(names)
assert "CustomIndex" in normal
class IndexWithInlineRepr(CustomIndex):
def _repr_inline_(self, max_width: int):
return f"CustomIndex[{', '.join(self.names)}]"
index = IndexWithInlineRepr(coord_names)
inline = formatting.summarize_index(names, index, col_width=20)
assert names[0] in inline
assert index._repr_inline_(max_width=40) in inline
@pytest.mark.parametrize(
"names",
(
("x",),
("x", "y"),
("x", "y", "z"),
("x", "y", "z", "a"),
),
)
def test_index_repr_grouping(self, names) -> None:
index = CustomIndex(names)
normal = formatting.summarize_index(names, index, col_width=20)
assert all(name in normal for name in names)
assert len(normal.splitlines()) == len(names)
assert "CustomIndex" in normal
hint_chars = [line[2] for line in normal.splitlines()]
if len(names) <= 1:
assert hint_chars == [" "]
else:
assert hint_chars[0] == "┌" and hint_chars[-1] == "└"
assert len(names) == 2 or hint_chars[1:-1] == ["│"] * (len(names) - 2)
def test_diff_array_repr(self) -> None:
da_a = xr.DataArray(
np.array([[1, 2, 3], [4, 5, 6]], dtype="int64"),
dims=("x", "y"),
coords={
"x": np.array(["a", "b"], dtype="U1"),
"y": np.array([1, 2, 3], dtype="int64"),
},
attrs={"units": "m", "description": "desc"},
)
da_b = xr.DataArray(
np.array([1, 2], dtype="int64"),
dims="x",
coords={
"x": np.array(["a", "c"], dtype="U1"),
"label": ("x", np.array([1, 2], dtype="int64")),
},
attrs={"units": "kg"},
)
byteorder = "<" if sys.byteorder == "little" else ">"
expected = dedent(
f"""\
Left and right DataArray objects are not identical
Differing dimensions:
(x: 2, y: 3) != (x: 2)
Differing values:
L
array([[1, 2, 3],
[4, 5, 6]], dtype=int64)
R
array([1, 2], dtype=int64)
Differing coordinates:
L * x (x) {byteorder}U1 8B 'a' 'b'
R * x (x) {byteorder}U1 8B 'a' 'c'
Coordinates only on the left object:
* y (y) int64 24B 1 2 3
Coordinates only on the right object:
label (x) int64 16B 1 2
Differing attributes:
L units: m
R units: kg
Attributes only on the left object:
description: desc"""
)
actual = formatting.diff_array_repr(da_a, da_b, "identical")
try:
assert actual == expected
except AssertionError:
# depending on platform, dtype may not be shown in numpy array repr
assert actual == expected.replace(", dtype=int64", "")
da_a = xr.DataArray(
np.array([[1, 2, 3], [4, 5, 6]], dtype="int8"),
dims=("x", "y"),
coords=xr.Coordinates(
{
"x": np.array([True, False], dtype="bool"),
"y": np.array([1, 2, 3], dtype="int16"),
},
indexes={"y": CustomIndex(("y",))},
),
)
da_b = xr.DataArray(
np.array([1, 2], dtype="int8"),
dims="x",
coords=xr.Coordinates(
{
"x": np.array([True, False], dtype="bool"),
"label": ("x", np.array([1, 2], dtype="int16")),
},
indexes={"label": CustomIndex(("label",))},
),
)
expected = dedent(
"""\
Left and right DataArray objects are not equal
Differing dimensions:
(x: 2, y: 3) != (x: 2)
Differing values:
L
array([[1, 2, 3],
[4, 5, 6]], dtype=int8)
R
array([1, 2], dtype=int8)
Coordinates only on the left object:
* y (y) int16 6B 1 2 3
Coordinates only on the right object:
* label (x) int16 4B 1 2
""".rstrip()
)
actual = formatting.diff_array_repr(da_a, da_b, "equals")
assert actual == expected
va = xr.Variable(
"x", np.array([1, 2, 3], dtype="int64"), {"title": "test Variable"}
)
vb = xr.Variable(("x", "y"), np.array([[1, 2, 3], [4, 5, 6]], dtype="int64"))
expected = dedent(
"""\
Left and right Variable objects are not equal
Differing dimensions:
(x: 3) != (x: 2, y: 3)
Differing values:
L
array([1, 2, 3], dtype=int64)
R
array([[1, 2, 3],
[4, 5, 6]], dtype=int64)"""
)
actual = formatting.diff_array_repr(va, vb, "equals")
try:
assert actual == expected
except AssertionError:
assert actual == expected.replace(", dtype=int64", "")
@pytest.mark.filterwarnings("error")
def test_diff_attrs_repr_with_array(self) -> None:
attrs_a = {"attr": np.array([0, 1])}
attrs_b = {"attr": 1}
expected = dedent(
"""\
Differing attributes:
L attr: [0 1]
R attr: 1
"""
).strip()
actual = formatting.diff_attrs_repr(attrs_a, attrs_b, "equals")
assert expected == actual
attrs_c = {"attr": np.array([-3, 5])}
expected = dedent(
"""\
Differing attributes:
L attr: [0 1]
R attr: [-3 5]
"""
).strip()
actual = formatting.diff_attrs_repr(attrs_a, attrs_c, "equals")
assert expected == actual
# should not raise a warning
attrs_c = {"attr": np.array([0, 1, 2])}
expected = dedent(
"""\
Differing attributes:
L attr: [0 1]
R attr: [0 1 2]
"""
).strip()
actual = formatting.diff_attrs_repr(attrs_a, attrs_c, "equals")
assert expected == actual
def test__diff_mapping_repr_array_attrs_on_variables(self) -> None:
a = {
"a": xr.DataArray(
dims="x",
data=np.array([1], dtype="int16"),
attrs={"b": np.array([1, 2], dtype="int8")},
)
}
b = {
"a": xr.DataArray(
dims="x",
data=np.array([1], dtype="int16"),
attrs={"b": np.array([2, 3], dtype="int8")},
)
}
actual = formatting.diff_data_vars_repr(a, b, compat="identical", col_width=8)
expected = dedent(
"""\
Differing data variables:
L a (x) int16 2B 1
Differing variable attributes:
b: [1 2]
R a (x) int16 2B 1
Differing variable attributes:
b: [2 3]
""".rstrip()
)
assert actual == expected
def test_diff_dataset_repr(self) -> None:
ds_a = xr.Dataset(
data_vars={
"var1": (("x", "y"), np.array([[1, 2, 3], [4, 5, 6]], dtype="int64")),
"var2": ("x", np.array([3, 4], dtype="int64")),
},
coords={
"x": (
"x",
np.array(["a", "b"], dtype="U1"),
{"foo": "bar", "same": "same"},
),
"y": np.array([1, 2, 3], dtype="int64"),
},
attrs={"title": "mytitle", "description": "desc"},
)
ds_b = xr.Dataset(
data_vars={"var1": ("x", np.array([1, 2], dtype="int64"))},
coords={
"x": (
"x",
np.array(["a", "c"], dtype="U1"),
{"source": 0, "foo": "baz", "same": "same"},
),
"label": ("x", np.array([1, 2], dtype="int64")),
},
attrs={"title": "newtitle"},
)
byteorder = "<" if sys.byteorder == "little" else ">"
expected = dedent(
f"""\
Left and right Dataset objects are not identical
Differing dimensions:
(x: 2, y: 3) != (x: 2)
Differing coordinates:
L * x (x) {byteorder}U1 8B 'a' 'b'
Differing variable attributes:
foo: bar
R * x (x) {byteorder}U1 8B 'a' 'c'
Differing variable attributes:
source: 0
foo: baz
Coordinates only on the left object:
* y (y) int64 24B 1 2 3
Coordinates only on the right object:
label (x) int64 16B 1 2
Differing data variables:
L var1 (x, y) int64 48B 1 2 3 4 5 6
R var1 (x) int64 16B 1 2
Data variables only on the left object:
var2 (x) int64 16B 3 4
Differing attributes:
L title: mytitle
R title: newtitle
Attributes only on the left object:
description: desc"""
)
actual = formatting.diff_dataset_repr(ds_a, ds_b, "identical")
assert actual == expected
def test_array_repr(self) -> None:
ds = xr.Dataset(
coords={
"foo": np.array([1, 2, 3], dtype=np.uint64),
"bar": np.array([1, 2, 3], dtype=np.uint64),
}
)
ds[(1, 2)] = xr.DataArray(np.array([0], dtype=np.uint64), dims="test")
ds_12 = ds[(1, 2)]
# Test repr function behaves correctly:
actual = formatting.array_repr(ds_12)
expected = dedent(
"""\
<xarray.DataArray (1, 2) (test: 1)> Size: 8B
array([0], dtype=uint64)
Dimensions without coordinates: test"""
)
assert actual == expected
# Test repr, str prints returns correctly as well:
assert repr(ds_12) == expected
assert str(ds_12) == expected
# f-strings (aka format(...)) by default should use the repr:
actual = f"{ds_12}"
assert actual == expected
with xr.set_options(display_expand_data=False):
actual = formatting.array_repr(ds[(1, 2)])
expected = dedent(
"""\
<xarray.DataArray (1, 2) (test: 1)> Size: 8B
0
Dimensions without coordinates: test"""
)
assert actual == expected
def test_array_repr_variable(self) -> None:
var = xr.Variable("x", [0, 1])
formatting.array_repr(var)
with xr.set_options(display_expand_data=False):
formatting.array_repr(var)
def test_array_repr_recursive(self) -> None:
# GH:issue:7111
# direct recursion
var = xr.Variable("x", [0, 1])
var.attrs["x"] = var
formatting.array_repr(var)
da = xr.DataArray([0, 1], dims=["x"])
da.attrs["x"] = da
formatting.array_repr(da)
# indirect recursion
var.attrs["x"] = da
da.attrs["x"] = var
formatting.array_repr(var)
formatting.array_repr(da)
@requires_dask
def test_array_scalar_format(self) -> None:
# Test numpy scalars:
var = xr.DataArray(np.array(0))
assert format(var, "") == repr(var)
assert format(var, "d") == "0"
assert format(var, ".2f") == "0.00"
# Test dask scalars, not supported however:
import dask.array as da
var = xr.DataArray(da.array(0))
assert format(var, "") == repr(var)
with pytest.raises(TypeError) as excinfo:
format(var, ".2f")
assert "unsupported format string passed to" in str(excinfo.value)
# Test numpy arrays raises:
var = xr.DataArray([0.1, 0.2])
with pytest.raises(NotImplementedError) as excinfo: # type: ignore[assignment]
format(var, ".2f")
assert "Using format_spec is only supported" in str(excinfo.value)
def test_datatree_print_empty_node(self):
dt: xr.DataTree = xr.DataTree(name="root")
printout = str(dt)
assert printout == "<xarray.DataTree 'root'>\nGroup: /"
def test_datatree_print_empty_node_with_attrs(self):
dat = xr.Dataset(attrs={"note": "has attrs"})
dt: xr.DataTree = xr.DataTree(name="root", dataset=dat)
printout = str(dt)
assert printout == dedent(
"""\
<xarray.DataTree 'root'>
Group: /
Attributes:
note: has attrs"""
)
def test_datatree_print_node_with_data(self):
dat = xr.Dataset({"a": [0, 2]})
dt: xr.DataTree = xr.DataTree(name="root", dataset=dat)
printout = str(dt)
expected = [
"<xarray.DataTree 'root'>",
"Group: /",
"Dimensions",
"Coordinates",
"a",
]
for expected_line, printed_line in zip(
expected, printout.splitlines(), strict=True
):
assert expected_line in printed_line
def test_datatree_printout_nested_node(self):
dat = xr.Dataset({"a": [0, 2]})
root = xr.DataTree.from_dict(
{
"/results": dat,
}
)
printout = str(root)
assert printout.splitlines()[3].startswith(" ")
def test_datatree_repr_of_node_with_data(self):
dat = xr.Dataset({"a": [0, 2]})
dt: xr.DataTree = xr.DataTree(name="root", dataset=dat)
assert "Coordinates" in repr(dt)
def test_diff_datatree_repr_different_groups(self):
dt_1: xr.DataTree = xr.DataTree.from_dict({"a": None})
dt_2: xr.DataTree = xr.DataTree.from_dict({"b": None})
expected = dedent(
"""\
Left and right DataTree objects are not identical
Children at root node do not match: ['a'] vs ['b']"""
)
actual = formatting.diff_datatree_repr(dt_1, dt_2, "identical")
assert actual == expected
def test_diff_datatree_repr_different_subgroups(self):
dt_1: xr.DataTree = xr.DataTree.from_dict({"a": None, "a/b": None, "a/c": None})
dt_2: xr.DataTree = xr.DataTree.from_dict({"a": None, "a/b": None})
expected = dedent(
"""\
Left and right DataTree objects are not isomorphic
Children at node 'a' do not match: ['b', 'c'] vs ['b']"""
)
actual = formatting.diff_datatree_repr(dt_1, dt_2, "isomorphic")
assert actual == expected
def test_diff_datatree_repr_node_data(self):
# casting to int64 explicitly ensures that int64s are created on all architectures
ds1 = xr.Dataset({"u": np.int64(0), "v": np.int64(1)})
ds3 = xr.Dataset({"w": np.int64(5)})
dt_1: xr.DataTree = xr.DataTree.from_dict({"a": ds1, "a/b": ds3})
ds2 = xr.Dataset({"u": np.int64(0)})
ds4 = xr.Dataset({"w": np.int64(6)})
dt_2: xr.DataTree = xr.DataTree.from_dict({"a": ds2, "a/b": ds4}, name="foo")
expected = dedent(
"""\
Left and right DataTree objects are not identical
Differing names:
None != 'foo'
Data at node 'a' does not match:
Data variables only on the left object:
v int64 8B 1
Data at node 'a/b' does not match:
Differing data variables:
L w int64 8B 5
R w int64 8B 6"""
)
actual = formatting.diff_datatree_repr(dt_1, dt_2, "identical")
assert actual == expected
def test_diff_datatree_repr_equals(self) -> None:
ds1 = xr.Dataset(data_vars={"data": ("y", [5, 2])})
ds2 = xr.Dataset(data_vars={"data": (("x", "y"), [[5, 2]])})
dt1 = xr.DataTree.from_dict({"node": ds1})
dt2 = xr.DataTree.from_dict({"node": ds2})
expected = dedent(
"""\
Left and right DataTree objects are not equal
Data at node 'node' does not match:
Differing dimensions:
(y: 2) != (x: 1, y: 2)
Differing data variables:
L data (y) int64 16B 5 2
R data (x, y) int64 16B 5 2"""
)
actual = formatting.diff_datatree_repr(dt1, dt2, "equals")
assert actual == expected
def test_inline_variable_array_repr_custom_repr() -> None:
class CustomArray:
def __init__(self, value, attr):
self.value = value
self.attr = attr
def _repr_inline_(self, width):
formatted = f"({self.attr}) {self.value}"
if len(formatted) > width:
formatted = f"({self.attr}) ..."
return formatted
def __array_namespace__(self, *args, **kwargs):
return NotImplemented
@property
def shape(self) -> tuple[int, ...]:
return self.value.shape
@property
def dtype(self):
return self.value.dtype
@property
def ndim(self):
return self.value.ndim
value = CustomArray(np.array([20, 40]), "m")
variable = xr.Variable("x", value)
max_width = 10
actual = formatting.inline_variable_array_repr(variable, max_width=10)
assert actual == value._repr_inline_(max_width)
def test_set_numpy_options() -> None:
original_options = np.get_printoptions()
with formatting.set_numpy_options(threshold=10):
assert len(repr(np.arange(500))) < 200
# original options are restored
assert np.get_printoptions() == original_options
def test_short_array_repr() -> None:
cases = [
np.random.randn(500),
np.random.randn(20, 20),
np.random.randn(5, 10, 15),
np.random.randn(5, 10, 15, 3),
np.random.randn(100, 5, 1),
]
# number of lines:
# for default numpy repr: 167, 140, 254, 248, 599
# for short_array_repr: 1, 7, 24, 19, 25
for array in cases:
num_lines = formatting.short_array_repr(array).count("\n") + 1
assert num_lines < 30
# threshold option (default: 200)
array2 = np.arange(100)
assert "..." not in formatting.short_array_repr(array2)
with xr.set_options(display_values_threshold=10):
assert "..." in formatting.short_array_repr(array2)
def test_large_array_repr_length() -> None:
da = xr.DataArray(np.random.randn(100, 5, 1))
result = repr(da).splitlines()
assert len(result) < 50
@requires_netCDF4
def test_repr_file_collapsed(tmp_path) -> None:
arr_to_store = xr.DataArray(np.arange(300, dtype=np.int64), dims="test")
arr_to_store.to_netcdf(tmp_path / "test.nc", engine="netcdf4")
with (
xr.open_dataarray(tmp_path / "test.nc") as arr,
xr.set_options(display_expand_data=False),
):
actual = repr(arr)
expected = dedent(
"""\
<xarray.DataArray (test: 300)> Size: 2kB
[300 values with dtype=int64]
Dimensions without coordinates: test"""
)
assert actual == expected
arr_loaded = arr.compute()
actual = arr_loaded.__repr__()
expected = dedent(
"""\
<xarray.DataArray (test: 300)> Size: 2kB
0 1 2 3 4 5 6 7 8 9 10 11 12 ... 288 289 290 291 292 293 294 295 296 297 298 299
Dimensions without coordinates: test"""
)
assert actual == expected
@pytest.mark.parametrize(
"display_max_rows, n_vars, n_attr",
[(50, 40, 30), (35, 40, 30), (11, 40, 30), (1, 40, 30)],
)
def test__mapping_repr(display_max_rows, n_vars, n_attr) -> None:
long_name = "long_name"
a = np.char.add(long_name, np.arange(0, n_vars).astype(str))
b = np.char.add("attr_", np.arange(0, n_attr).astype(str))
c = np.char.add("coord", np.arange(0, n_vars).astype(str))
attrs = dict.fromkeys(b, 2)
coords = {_c: np.array([0, 1], dtype=np.uint64) for _c in c}
data_vars = dict()
for v, _c in zip(a, coords.items(), strict=True):
data_vars[v] = xr.DataArray(
name=v,
data=np.array([3, 4], dtype=np.uint64),
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.sizes, col_width=col_width + 1, max_rows=display_max_rows
)
expected_size = "1kB"
expected = f"""\
<xarray.Dataset> Size: {expected_size}
{dims_start}({dims_values})
Coordinates: ({n_vars})
Data variables: ({n_vars})
Attributes: ({n_attr})"""
expected = dedent(expected)
assert actual == expected
def test__mapping_repr_recursive() -> None:
# GH:issue:7111
# direct recursion
ds = xr.Dataset({"a": ("x", [1, 2, 3])})
ds.attrs["ds"] = ds
formatting.dataset_repr(ds)
# indirect recursion
ds2 = xr.Dataset({"b": ("y", [1, 2, 3])})
ds.attrs["ds"] = ds2
ds2.attrs["ds"] = ds
formatting.dataset_repr(ds2)
def test__element_formatter(n_elements: int = 100) -> None:
expected = """\
Dimensions without coordinates: dim_0: 3, dim_1: 3, dim_2: 3, dim_3: 3,
dim_4: 3, dim_5: 3, dim_6: 3, dim_7: 3,
dim_8: 3, dim_9: 3, dim_10: 3, dim_11: 3,
dim_12: 3, dim_13: 3, dim_14: 3, dim_15: 3,
dim_16: 3, dim_17: 3, dim_18: 3, dim_19: 3,
dim_20: 3, dim_21: 3, dim_22: 3, dim_23: 3,
...
dim_76: 3, dim_77: 3, dim_78: 3, dim_79: 3,
dim_80: 3, dim_81: 3, dim_82: 3, dim_83: 3,
dim_84: 3, dim_85: 3, dim_86: 3, dim_87: 3,
dim_88: 3, dim_89: 3, dim_90: 3, dim_91: 3,
dim_92: 3, dim_93: 3, dim_94: 3, dim_95: 3,
dim_96: 3, dim_97: 3, dim_98: 3, dim_99: 3"""
expected = dedent(expected)
intro = "Dimensions without coordinates: "
elements = [
f"{k}: {v}" for k, v in {f"dim_{k}": 3 for k in np.arange(n_elements)}.items()
]
values = xr.core.formatting._element_formatter(
elements, col_width=len(intro), max_rows=12
)
actual = intro + values
assert expected == actual
def test_lazy_array_wont_compute() -> None:
from xarray.core.indexing import LazilyIndexedArray
class LazilyIndexedArrayNotComputable(LazilyIndexedArray):
def __array__(
self, dtype: np.typing.DTypeLike = None, /, *, copy: bool | None = None
) -> np.ndarray:
raise NotImplementedError("Computing this array is not possible.")
arr = LazilyIndexedArrayNotComputable(np.array([1, 2]))
var = xr.DataArray(arr)
# These will crash if var.data are converted to numpy arrays:
var.__repr__()
var._repr_html_()
@pytest.mark.parametrize("as_dataset", (False, True))
def test_format_xindexes_none(as_dataset: bool) -> None:
# ensure repr for empty xindexes can be displayed #8367
expected = """\
Indexes:
*empty*"""
expected = dedent(expected)
obj: xr.DataArray | xr.Dataset = xr.DataArray()
obj = obj._to_temp_dataset() if as_dataset else obj
actual = repr(obj.xindexes)
assert actual == expected
@pytest.mark.parametrize("as_dataset", (False, True))
def test_format_xindexes(as_dataset: bool) -> None:
expected = """\
Indexes:
x PandasIndex"""
expected = dedent(expected)
obj: xr.DataArray | xr.Dataset = xr.DataArray([1], coords={"x": [1]})
obj = obj._to_temp_dataset() if as_dataset else obj
actual = repr(obj.xindexes)
assert actual == expected
@requires_cftime
def test_empty_cftimeindex_repr() -> None:
index = xr.coding.cftimeindex.CFTimeIndex([])
expected = """\
Indexes:
time CFTimeIndex([], dtype='object', length=0, calendar=None, freq=None)"""
expected = dedent(expected)
da = xr.DataArray([], coords={"time": index})
actual = repr(da.indexes)
assert actual == expected
def test_display_nbytes() -> None:
xds = xr.Dataset(
{
"foo": np.arange(1200, dtype=np.int16),
"bar": np.arange(111, dtype=np.int16),
}
)
# Note: int16 is used to ensure that dtype is shown in the
# numpy array representation for all OSes included Windows
actual = repr(xds)
expected = """
<xarray.Dataset> Size: 3kB
Dimensions: (foo: 1200, bar: 111)
Coordinates:
* foo (foo) int16 2kB 0 1 2 3 4 5 6 ... 1194 1195 1196 1197 1198 1199
* bar (bar) int16 222B 0 1 2 3 4 5 6 7 ... 104 105 106 107 108 109 110
Data variables:
*empty*
""".strip()
assert actual == expected
actual = repr(xds["foo"])
array_repr = repr(xds.foo.data).replace("\n ", "")
expected = f"""
<xarray.DataArray 'foo' (foo: 1200)> Size: 2kB
{array_repr}
Coordinates:
* foo (foo) int16 2kB 0 1 2 3 4 5 6 ... 1194 1195 1196 1197 1198 1199
""".strip()
assert actual == expected
def test_array_repr_dtypes():
# These dtypes are expected to be represented similarly
# on Ubuntu, macOS and Windows environments of the CI.
# Unsigned integer could be used as easy replacements
# for tests where the data-type does not matter,
# but the repr does, including the size
# (size of a int == size of an uint)
# Signed integer dtypes
ds = xr.DataArray(np.array([0], dtype="int8"), dims="x")
actual = repr(ds)
expected = """
<xarray.DataArray (x: 1)> Size: 1B
array([0], dtype=int8)
Dimensions without coordinates: x
""".strip()
assert actual == expected
ds = xr.DataArray(np.array([0], dtype="int16"), dims="x")
actual = repr(ds)
expected = """
<xarray.DataArray (x: 1)> Size: 2B
array([0], dtype=int16)
Dimensions without coordinates: x
""".strip()
assert actual == expected
# Unsigned integer dtypes
ds = xr.DataArray(np.array([0], dtype="uint8"), dims="x")
actual = repr(ds)
expected = """
<xarray.DataArray (x: 1)> Size: 1B
array([0], dtype=uint8)
Dimensions without coordinates: x
""".strip()
assert actual == expected
ds = xr.DataArray(np.array([0], dtype="uint16"), dims="x")
actual = repr(ds)
expected = """
<xarray.DataArray (x: 1)> Size: 2B
array([0], dtype=uint16)
Dimensions without coordinates: x
""".strip()
assert actual == expected
ds = xr.DataArray(np.array([0], dtype="uint32"), dims="x")
actual = repr(ds)
expected = """
<xarray.DataArray (x: 1)> Size: 4B
array([0], dtype=uint32)
Dimensions without coordinates: x
""".strip()
assert actual == expected
ds = xr.DataArray(np.array([0], dtype="uint64"), dims="x")
actual = repr(ds)
expected = """
<xarray.DataArray (x: 1)> Size: 8B
array([0], dtype=uint64)
Dimensions without coordinates: x
""".strip()
assert actual == expected
# Float dtypes
ds = xr.DataArray(np.array([0.0]), dims="x")
actual = repr(ds)
expected = """
<xarray.DataArray (x: 1)> Size: 8B
array([0.])
Dimensions without coordinates: x
""".strip()
assert actual == expected
ds = xr.DataArray(np.array([0], dtype="float16"), dims="x")
actual = repr(ds)
expected = """
<xarray.DataArray (x: 1)> Size: 2B
array([0.], dtype=float16)
Dimensions without coordinates: x
""".strip()
assert actual == expected
ds = xr.DataArray(np.array([0], dtype="float32"), dims="x")
actual = repr(ds)
expected = """
<xarray.DataArray (x: 1)> Size: 4B
array([0.], dtype=float32)
Dimensions without coordinates: x
""".strip()
assert actual == expected
ds = xr.DataArray(np.array([0], dtype="float64"), dims="x")
actual = repr(ds)
expected = """
<xarray.DataArray (x: 1)> Size: 8B
array([0.])
Dimensions without coordinates: x
""".strip()
assert actual == expected
# Signed integer dtypes
array = np.array([0])
ds = xr.DataArray(array, dims="x")
actual = repr(ds)
expected = f"""
<xarray.DataArray (x: 1)> Size: {array.dtype.itemsize}B
{array!r}
Dimensions without coordinates: x
""".strip()
assert actual == expected
array = np.array([0], dtype="int32")
ds = xr.DataArray(array, dims="x")
actual = repr(ds)
expected = f"""
<xarray.DataArray (x: 1)> Size: 4B
{array!r}
Dimensions without coordinates: x
""".strip()
assert actual == expected
array = np.array([0], dtype="int64")
ds = xr.DataArray(array, dims="x")
actual = repr(ds)
expected = f"""
<xarray.DataArray (x: 1)> Size: 8B
{array!r}
Dimensions without coordinates: x
""".strip()
assert actual == expected
def test_repr_pandas_range_index() -> None:
# lazy data repr but values shown in inline repr
xidx = xr.indexes.PandasIndex(pd.RangeIndex(10), "x")
ds = xr.Dataset(coords=xr.Coordinates.from_xindex(xidx))
actual = repr(ds.x)
expected = """
<xarray.DataArray 'x' (x: 10)> Size: 80B
[10 values with dtype=int64]
Coordinates:
* x (x) int64 80B 0 1 2 3 4 5 6 7 8 9
""".strip()
assert actual == expected
def test_repr_pandas_multi_index() -> None:
# lazy data repr but values shown in inline repr
midx = pd.MultiIndex.from_product([["a", "b"], [1, 2]], names=["foo", "bar"])
coords = xr.Coordinates.from_pandas_multiindex(midx, "x")
ds = xr.Dataset(coords=coords)
actual = repr(ds.x)
expected = """
<xarray.DataArray 'x' (x: 4)> Size: 32B
[4 values with dtype=object]
Coordinates:
* x (x) object 32B MultiIndex
* foo (x) object 32B 'a' 'a' 'b' 'b'
* bar (x) int64 32B 1 2 1 2
""".strip()
assert actual == expected
actual = repr(ds.foo)
expected = """
<xarray.DataArray 'foo' (x: 4)> Size: 32B
[4 values with dtype=object]
Coordinates:
* x (x) object 32B MultiIndex
* foo (x) object 32B 'a' 'a' 'b' 'b'
* bar (x) int64 32B 1 2 1 2
""".strip()
assert actual == expected
|