import re
import numpy as np
import pytest
from sklearn import config_context
from sklearn.utils._repr_html.fitted_attributes import (
AttrsDict,
_fitted_attr_html_repr,
_read_fitted_attr,
)
fitted_attrs = AttrsDict(
fitted_attrs={
"a": {"type_name": "int", "value": 6},
"b": {
"type_name": "ndarray",
"shape": (1,),
"dtype": np.dtype("float64"),
"value": 8,
},
}
)
def test_numpy_fitted_attr():
html_fitted_attr_out = _fitted_attr_html_repr(fitted_attrs)
expected_html_fitted_attr = (
r'
'
r"\s*
"
r"\s*Fitted attributes
"
r'\s*'
r"\s*"
r"\s*"
r"\s*| Name | "
r"\s*Type | "
r"\s*Value | "
r"\s*
"
r'\s*'
r'\s*| '
r'\s*a'
r"\s* | "
r'\s*int | '
r"\s*6 | "
r"\s*
"
r'\s*'
r'\s*| '
r'\s*b'
r"\s* | "
r'\s*ndarray\[float64\]\(1,\) | '
r"\s*8 | "
r"\s*
"
r"\s*"
r"\s*
"
r"\s* "
r"\s*
"
)
assert re.search(expected_html_fitted_attr, html_fitted_attr_out, flags=re.DOTALL)
def test_fitted_attrs_dict_repr_html_error():
out = fitted_attrs._repr_html_()
assert "Fitted attributes" in out
assert 'int | ' in out
assert 'ndarray[float64](1,) | ' in out
with config_context(display="text"):
msg = "_repr_html_ is only defined when"
with pytest.raises(AttributeError, match=msg):
fitted_attrs._repr_html_()
def test_fitted_attrs_dict_repr_mimebundle():
out = fitted_attrs._repr_mimebundle_()
assert "text/plain" in out
assert "text/html" in out
assert "Fitted attributes" in out["text/html"]
plain_text = (
"{'a': {'type_name': 'int', 'value': 6}, "
"'b': {'type_name': 'ndarray', 'shape': (1,), "
"'dtype': dtype('float64'), 'value': 8}}"
)
assert out["text/plain"] == plain_text
with config_context(display="text"):
out = fitted_attrs._repr_mimebundle_()
assert "text/plain" in out
assert "text/html" not in out
def test_fitted_attr_html_repr():
out = _fitted_attr_html_repr(fitted_attrs)
assert "Fitted attributes" in out
assert '' in out
def test_pandas_column_names():
pd = pytest.importorskip("pandas")
fitted_attrs_with_pandas_cols = AttrsDict(
fitted_attrs={
"myabc_": {
"type_name": "DataFrame",
"shape": (3, 3),
"dtype": np.dtype("int64"),
"value": pd.DataFrame({"A": [0, 2, 4], "B": [3, 4, 5], "C": [3, 4, 4]}),
}
}
)
html_fitted_attr_out = _fitted_attr_html_repr(fitted_attrs_with_pandas_cols)
expected_html_fitted_attr = (
r''
r"\s*
"
r"\s*Fitted attributes
"
r'\s*'
r"\s*"
r"\s*"
r"\s*| Name | "
r"\s*Type | "
r"\s*Value | "
r"\s*
"
r"\s*"
r"\s*| "
r'\s*myabc_'
r"\s* | "
r'\s*DataFrame\[int64\]\(3,\s*3\) | '
r"\s*\s*A\s*B\s*C\s*0\s*\.\.\.\s*4\s*2\s*4\s*5\s*4\s* | "
r"\s*
"
r"\s*"
r"\s*
"
r"\s* "
r"\s*
"
)
assert re.search(expected_html_fitted_attr, html_fitted_attr_out, flags=re.DOTALL)
def test_pandas_series_fitted_attr():
pd = pytest.importorskip("pandas")
fitted_attrs_with_series = AttrsDict(
fitted_attrs={
"new_": {
"type_name": "Series",
"shape": (3,),
"dtype": np.dtype("int64"),
"value": pd.Series({"a": 1, "b": 2, "c": 3}),
}
}
)
html_fitted_attr_out = _fitted_attr_html_repr(fitted_attrs_with_series)
expected_html_fitted_attr = (
r''
r"\s*
"
r"\s*Fitted attributes
"
r'\s*'
r"\s*"
r"\s*"
r"\s*| Name | "
r"\s*Type | "
r"\s*Value | "
r"\s*
"
r'\s*'
r'\s*| '
r'\s*new_'
r"\s* | "
r'\s*Series\[int64\]\(3,\) | '
r"\s*a\s*1\s*b\s*2...3\s*dtype:\s*int64 | "
r"\s*
"
r"\s*"
r"\s*
"
r"\s* "
r"\s*
"
)
assert re.search(expected_html_fitted_attr, html_fitted_attr_out, flags=re.DOTALL)
@pytest.mark.parametrize(
"value, expected",
[
(123.456, "123.5"),
(0.00123456, "0.001235"),
(1234567.0, "1.235e+06"),
],
)
def test_read_fitted_attr_float_formatting(value, expected):
assert _read_fitted_attr(value) == expected