jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def test_fetch_openml_notarget(monkeypatch, gzip_response):
    data_id = 61
    target_column = None
    expected_observations = 150
    expected_features = 5

    _monkey_patch_webbased_functions(monkeypatch, data_id, gzip_response)
    data = fetch_openml(data_id=data_id, target_column=target_column,
                        cache=False, as_frame=False)
    assert data.data.shape == (expected_observations, expected_features)
    assert data.target is None
//  [python]
def test_fetch_openml_emotions_pandas(monkeypatch):
    # classification dataset with multiple targets (natively)
    pd = pytest.importorskip('pandas')
    CategoricalDtype = pd.api.types.CategoricalDtype

    data_id = 40589
    target_column = ['amazed.suprised', 'happy.pleased', 'relaxing.calm',
                     'quiet.still', 'sad.lonely', 'angry.aggresive']
    data_shape = (13, 72)
    target_shape = (13, 6)
    frame_shape = (13, 78)

    expected_frame_categories = 6
    expected_frame_floats = 72

    _monkey_patch_webbased_functions(monkeypatch, data_id, True)
    bunch = fetch_openml(data_id=data_id, as_frame=True, cache=False,
                         target_column=target_column)
    data = bunch.data
    target = bunch.target
    frame = bunch.frame

    assert isinstance(data, pd.DataFrame)
    assert data.shape == data_shape

    assert isinstance(target, pd.DataFrame)
    assert target.shape == target_shape
    assert np.all(target.columns == target_column)

    assert isinstance(frame, pd.DataFrame)
    assert frame.shape == frame_shape
    n_categories = len([dtype for dtype in frame.dtypes
                       if isinstance(dtype, CategoricalDtype)])
    n_floats = len([dtype for dtype in frame.dtypes if dtype.kind == 'f'])
    assert expected_frame_categories == n_categories
    assert expected_frame_floats == n_floats
//  [python]
def test_fetch_openml_with_ignored_feature(monkeypatch, gzip_response):
    # Regression test for #14340
    # 62 is the ID of the ZOO dataset
    data_id = 62
    _monkey_patch_webbased_functions(monkeypatch, data_id, gzip_response)

    dataset = sklearn.datasets.fetch_openml(data_id=data_id, cache=False,
                                            as_frame=False)
    assert dataset is not None
    # The dataset has 17 features, including 1 ignored (animal),
    # so we assert that we don't have the ignored feature in the final Bunch
    assert dataset['data'].shape == (101, 16)
    assert 'animal' not in dataset['feature_names']
//  [python]
def test_fetch_openml_with_ignored_feature(monkeypatch, gzip_response, parser):
    """Check that we can load the "zoo" dataset.
    Non-regression test for:
    https://github.com/scikit-learn/scikit-learn/issues/14340
    """
    if parser == "pandas":
        pytest.importorskip("pandas")
    data_id = 62
    _monkey_patch_webbased_functions(monkeypatch, data_id, gzip_response)

    dataset = sklearn.datasets.fetch_openml(
        data_id=data_id, cache=False, as_frame=False, parser=parser
    )
    assert dataset is not None
    # The dataset has 17 features, including 1 ignored (animal),
    # so we assert that we don't have the ignored feature in the final Bunch
    assert dataset["data"].shape == (101, 16)
    assert "animal" not in dataset["feature_names"]
//  [python]
def test_fetch_openml_deprecation_parser(monkeypatch):
    """Check that we raise a deprecation warning for parser parameter."""
    pytest.importorskip("pandas")
    data_id = 61
    _monkey_patch_webbased_functions(monkeypatch, data_id=data_id, gzip_response=False)

    with pytest.warns(FutureWarning, match="The default value of `parser` will change"):
        sklearn.datasets.fetch_openml(data_id=data_id)
//  [python]
def test_fetch_openml_titanic_pandas(monkeypatch):
    # dataset with strings
    pd = pytest.importorskip('pandas')
    CategoricalDtype = pd.api.types.CategoricalDtype

    data_id = 40945
    data_shape = (1309, 13)
    target_shape = (1309, )
    frame_shape = (1309, 14)
    name_to_dtype = {
        'pclass': np.float64,
        'name': object,
        'sex': CategoricalDtype(['female', 'male']),
        'age': np.float64,
        'sibsp': np.float64,
        'parch': np.float64,
        'ticket': object,
        'fare': np.float64,
        'cabin': object,
        'embarked': CategoricalDtype(['C', 'Q', 'S']),
        'boat': object,
        'body': np.float64,
        'home.dest': object,
        'survived': CategoricalDtype(['0', '1'])
    }

    frame_columns = ['pclass', 'survived', 'name', 'sex', 'age', 'sibsp',
                     'parch', 'ticket', 'fare', 'cabin', 'embarked',
                     'boat', 'body', 'home.dest']
    frame_dtypes = [name_to_dtype[col] for col in frame_columns]
    feature_names = ['pclass', 'name', 'sex', 'age', 'sibsp',
                     'parch', 'ticket', 'fare', 'cabin', 'embarked',
                     'boat', 'body', 'home.dest']
    target_name = 'survived'

    _monkey_patch_webbased_functions(monkeypatch, data_id, True)
    bunch = fetch_openml(data_id=data_id, as_frame=True, cache=False)
    data = bunch.data
    target = bunch.target
    frame = bunch.frame

    assert isinstance(data, pd.DataFrame)
    assert data.shape == data_shape
    assert np.all(data.columns == feature_names)
    assert bunch.target_names == [target_name]

    assert isinstance(target, pd.Series)
    assert target.shape == target_shape
    assert target.name == target_name
    assert target.dtype == name_to_dtype[target_name]

    assert isinstance(frame, pd.DataFrame)
    assert frame.shape == frame_shape
    assert np.all(frame.dtypes == frame_dtypes)
//  [python]
def test_fetch_openml_verify_checksum(monkeypatch, as_frame, cache, tmpdir):
    if as_frame:
        pytest.importorskip('pandas')

    data_id = 2
    _monkey_patch_webbased_functions(monkeypatch, data_id, True)

    # create a temporary modified arff file
    dataset_dir = os.path.join(currdir, 'data', 'openml', str(data_id))
    original_data_path = os.path.join(dataset_dir,
                                      'data-v1-download-1666876.arff.gz')
    corrupt_copy = os.path.join(tmpdir, "test_invalid_checksum.arff")
    with gzip.GzipFile(original_data_path, "rb") as orig_gzip, \
            gzip.GzipFile(corrupt_copy, "wb") as modified_gzip:
        data = bytearray(orig_gzip.read())
        data[len(data)-1] = 37
        modified_gzip.write(data)

    # Requests are already mocked by monkey_patch_webbased_functions.
    # We want to re-use that mock for all requests except file download,
    # hence creating a thin mock over the original mock
    mocked_openml_url = sklearn.datasets._openml.urlopen

    def swap_file_mock(request):
        url = request.get_full_url()
        if url.endswith('data/v1/download/1666876'):
            return _MockHTTPResponse(open(corrupt_copy, "rb"), is_gzip=True)
        else:
            return mocked_openml_url(request)

    monkeypatch.setattr(sklearn.datasets._openml, 'urlopen', swap_file_mock)

    # validate failed checksum
    with pytest.raises(ValueError) as exc:
        sklearn.datasets.fetch_openml(data_id=data_id, cache=False,
                                      as_frame=as_frame)
    # exception message should have file-path
    assert exc.match("1666876")
//  [python]
def test_fetch_openml_iris_pandas_equal_to_no_frame(monkeypatch):
    # as_frame = True returns the same underlying data as as_frame = False
    pytest.importorskip("pandas")
    data_id = 61

    _monkey_patch_webbased_functions(monkeypatch, data_id, True)

    frame_bunch = fetch_openml(data_id=data_id, as_frame=True, cache=False)
    frame_data = frame_bunch.data
    frame_target = frame_bunch.target

    norm_bunch = fetch_openml(data_id=data_id, as_frame=False, cache=False)
    norm_data = norm_bunch.data
    norm_target = norm_bunch.target

    assert_allclose(norm_data, frame_data)
    assert_array_equal(norm_target, frame_target)
//  [python]
def test_fetch_openml_australian_pandas_error_sparse(monkeypatch):
    data_id = 292

    _monkey_patch_webbased_functions(monkeypatch, data_id, True)

    msg = "Cannot return dataframe with sparse data"
    with pytest.raises(ValueError, match=msg):
        fetch_openml(data_id=data_id, as_frame=True, cache=False)
//  [python]
def test_fetch_openml_iris_pandas(monkeypatch):
    # classification dataset with numeric only columns
    pd = pytest.importorskip("pandas")
    CategoricalDtype = pd.api.types.CategoricalDtype
    data_id = 61
    data_shape = (150, 4)
    target_shape = (150,)
    frame_shape = (150, 5)

    target_dtype = CategoricalDtype(
        ["Iris-setosa", "Iris-versicolor", "Iris-virginica"]
    )
    data_dtypes = [np.float64] * 4
    data_names = ["sepallength", "sepalwidth", "petallength", "petalwidth"]
    target_name = "class"

    _monkey_patch_webbased_functions(monkeypatch, data_id, True)

    bunch = fetch_openml(data_id=data_id, as_frame=True, cache=False)
    data = bunch.data
    target = bunch.target
    frame = bunch.frame

    assert isinstance(data, pd.DataFrame)
    assert np.all(data.dtypes == data_dtypes)
    assert data.shape == data_shape
    assert np.all(data.columns == data_names)
    assert np.all(bunch.feature_names == data_names)
    assert bunch.target_names == [target_name]

    assert isinstance(target, pd.Series)
    assert target.dtype == target_dtype
    assert target.shape == target_shape
    assert target.name == target_name
    assert target.index.is_unique

    assert isinstance(frame, pd.DataFrame)
    assert frame.shape == frame_shape
    assert np.all(frame.dtypes == data_dtypes + [target_dtype])
    assert frame.index.is_unique
//  [python]
def _test_features_list(data_id):
    # XXX Test is intended to verify/ensure correct decoding behavior
    # Not usable with sparse data or datasets that have columns marked as
    # {row_identifier, ignore}
    def decode_column(data_bunch, col_idx):
        col_name = data_bunch.feature_names[col_idx]
        if col_name in data_bunch.categories:
            # XXX: This would be faster with np.take, although it does not
            # handle missing values fast (also not with mode='wrap')
            cat = data_bunch.categories[col_name]
            result = [
                None if is_scalar_nan(idx) else cat[int(idx)]
                for idx in data_bunch.data[:, col_idx]
            ]
            return np.array(result, dtype="O")
        else:
            # non-nominal attribute
            return data_bunch.data[:, col_idx]

    data_bunch = fetch_openml(
        data_id=data_id, cache=False, target_column=None, as_frame=False
    )

    # also obtain decoded arff
    data_description = _get_data_description_by_id(data_id, None)
    sparse = data_description["format"].lower() == "sparse_arff"
    if sparse is True:
        raise ValueError(
            "This test is not intended for sparse data, to keep code relatively simple"
        )
    url = _DATA_FILE.format(data_description["file_id"])
    with _open_openml_url(url, data_home=None) as f:
        data_arff = _arff.load(
            (line.decode("utf-8") for line in f),
            return_type=(_arff.COO if sparse else _arff.DENSE_GEN),
            encode_nominal=False,
        )

    data_downloaded = np.array(list(data_arff["data"]), dtype="O")

    for i in range(len(data_bunch.feature_names)):
        # XXX: Test per column, as this makes it easier to avoid problems with
        # missing values

        np.testing.assert_array_equal(
            data_downloaded[:, i], decode_column(data_bunch, i)
        )
//  [python]
def test_fetch_openml_as_frame_auto(monkeypatch):
    pd = pytest.importorskip("pandas")

    data_id = 61  # iris dataset version 1
    _monkey_patch_webbased_functions(monkeypatch, data_id, True)
    data = fetch_openml(data_id=data_id, as_frame="auto", cache=False)
    assert isinstance(data.data, pd.DataFrame)

    data_id = 292  # Australian dataset version 1
    _monkey_patch_webbased_functions(monkeypatch, data_id, True)
    data = fetch_openml(data_id=data_id, as_frame="auto", cache=False)
    assert isinstance(data.data, scipy.sparse.csr_matrix)
//  [python]
def test_fetch_openml_anneal_pandas(monkeypatch):
    # classification dataset with numeric and categorical columns
    pd = pytest.importorskip("pandas")
    CategoricalDtype = pd.api.types.CategoricalDtype

    data_id = 2
    target_column = "class"
    data_shape = (11, 38)
    target_shape = (11,)
    frame_shape = (11, 39)
    expected_data_categories = 32
    expected_data_floats = 6

    _monkey_patch_webbased_functions(monkeypatch, data_id, True)

    bunch = fetch_openml(
        data_id=data_id, as_frame=True, target_column=target_column, cache=False
    )
    data = bunch.data
    target = bunch.target
    frame = bunch.frame

    assert isinstance(data, pd.DataFrame)
    assert data.shape == data_shape
    n_categories = len(
        [dtype for dtype in data.dtypes if isinstance(dtype, CategoricalDtype)]
    )
    n_floats = len([dtype for dtype in data.dtypes if dtype.kind == "f"])
    assert expected_data_categories == n_categories
    assert expected_data_floats == n_floats

    assert isinstance(target, pd.Series)
    assert target.shape == target_shape
    assert isinstance(target.dtype, CategoricalDtype)

    assert isinstance(frame, pd.DataFrame)
    assert frame.shape == frame_shape
//  [python]
def test_fetch_openml_iris_multitarget_pandas(monkeypatch):
    # classification dataset with numeric only columns
    pd = pytest.importorskip("pandas")
    CategoricalDtype = pd.api.types.CategoricalDtype
    data_id = 61
    data_shape = (150, 3)
    target_shape = (150, 2)
    frame_shape = (150, 5)
    target_column = ["petalwidth", "petallength"]

    cat_dtype = CategoricalDtype(["Iris-setosa", "Iris-versicolor", "Iris-virginica"])
    data_dtypes = [np.float64, np.float64] + [cat_dtype]
    data_names = ["sepallength", "sepalwidth", "class"]
    target_dtypes = [np.float64, np.float64]
    target_names = ["petalwidth", "petallength"]

    _monkey_patch_webbased_functions(monkeypatch, data_id, True)

    bunch = fetch_openml(
        data_id=data_id, as_frame=True, cache=False, target_column=target_column
    )
    data = bunch.data
    target = bunch.target
    frame = bunch.frame

    assert isinstance(data, pd.DataFrame)
    assert np.all(data.dtypes == data_dtypes)
    assert data.shape == data_shape
    assert np.all(data.columns == data_names)
    assert np.all(bunch.feature_names == data_names)
    assert bunch.target_names == target_names

    assert isinstance(target, pd.DataFrame)
    assert np.all(target.dtypes == target_dtypes)
    assert target.shape == target_shape
    assert np.all(target.columns == target_names)

    assert isinstance(frame, pd.DataFrame)
    assert frame.shape == frame_shape
    assert np.all(frame.dtypes == [np.float64] * 4 + [cat_dtype])
//  [python]
def test_convert_arff_data_dataframe_warning_low_memory_pandas(monkeypatch):
    pytest.importorskip("pandas")

    data_id = 1119
    _monkey_patch_webbased_functions(monkeypatch, data_id, True)

    msg = "Could not adhere to working_memory config."
    with pytest.warns(UserWarning, match=msg):
        with config_context(working_memory=1e-6):
            fetch_openml(data_id=data_id, as_frame=True, cache=False)
//  [python]
def test_fetch_openml_cpu_pandas(monkeypatch):
    # regression dataset with numeric and categorical columns
    pd = pytest.importorskip("pandas")
    CategoricalDtype = pd.api.types.CategoricalDtype
    data_id = 561
    data_shape = (209, 7)
    target_shape = (209,)
    frame_shape = (209, 8)

    cat_dtype = CategoricalDtype(
        [
            "adviser",
            "amdahl",
            "apollo",
            "basf",
            "bti",
            "burroughs",
            "c.r.d",
            "cdc",
            "cambex",
            "dec",
            "dg",
            "formation",
            "four-phase",
            "gould",
            "hp",
            "harris",
            "honeywell",
            "ibm",
            "ipl",
            "magnuson",
            "microdata",
            "nas",
            "ncr",
            "nixdorf",
            "perkin-elmer",
            "prime",
            "siemens",
            "sperry",
            "sratus",
            "wang",
        ]
    )
    data_dtypes = [cat_dtype] + [np.float64] * 6
    feature_names = ["vendor", "MYCT", "MMIN", "MMAX", "CACH", "CHMIN", "CHMAX"]
    target_name = "class"

    _monkey_patch_webbased_functions(monkeypatch, data_id, True)
    bunch = fetch_openml(data_id=data_id, as_frame=True, cache=False)
    data = bunch.data
    target = bunch.target
    frame = bunch.frame

    assert isinstance(data, pd.DataFrame)
    assert data.shape == data_shape
    assert np.all(data.dtypes == data_dtypes)
    assert np.all(data.columns == feature_names)
    assert np.all(bunch.feature_names == feature_names)
    assert bunch.target_names == [target_name]

    assert isinstance(target, pd.Series)
    assert target.shape == target_shape
    assert target.dtype == np.float64
    assert target.name == target_name

    assert isinstance(frame, pd.DataFrame)
    assert frame.shape == frame_shape
//  [python]
def test_fetch_openml_adultcensus_pandas_return_X_y(monkeypatch):
    pd = pytest.importorskip("pandas")
    CategoricalDtype = pd.api.types.CategoricalDtype

    data_id = 1119
    data_shape = (10, 14)
    target_shape = (10,)

    expected_data_categories = 8
    expected_data_floats = 6
    target_column = "class"

    _monkey_patch_webbased_functions(monkeypatch, data_id, True)
    X, y = fetch_openml(data_id=data_id, as_frame=True, cache=False, return_X_y=True)
    assert isinstance(X, pd.DataFrame)
    assert X.shape == data_shape
    n_categories = len(
        [dtype for dtype in X.dtypes if isinstance(dtype, CategoricalDtype)]
    )
    n_floats = len([dtype for dtype in X.dtypes if dtype.kind == "f"])
    assert expected_data_categories == n_categories
    assert expected_data_floats == n_floats

    assert isinstance(y, pd.Series)
    assert y.shape == target_shape
    assert y.name == target_column
//  [python]
def test_fetch_openml_iris(monkeypatch, gzip_response):
    # classification dataset with numeric only columns
    data_id = 61
    data_name = "iris"

    _monkey_patch_webbased_functions(monkeypatch, data_id, gzip_response)

    msg = (
        "Multiple active versions of the dataset matching the name"
        " iris exist. Versions may be fundamentally different, "
        "returning version 1."
    )
    with pytest.warns(UserWarning, match=msg):
        fetch_openml(name=data_name, as_frame=False, cache=False)
//  [python]
def test_fetch_openml_adultcensus_pandas(monkeypatch):
    pd = pytest.importorskip("pandas")
    CategoricalDtype = pd.api.types.CategoricalDtype

    # Check because of the numeric row attribute (issue #12329)
    data_id = 1119
    data_shape = (10, 14)
    target_shape = (10,)
    frame_shape = (10, 15)

    expected_data_categories = 8
    expected_data_floats = 6
    target_column = "class"

    _monkey_patch_webbased_functions(monkeypatch, data_id, True)
    bunch = fetch_openml(data_id=data_id, as_frame=True, cache=False)
    data = bunch.data
    target = bunch.target
    frame = bunch.frame

    assert isinstance(data, pd.DataFrame)
    assert data.shape == data_shape
    n_categories = len(
        [dtype for dtype in data.dtypes if isinstance(dtype, CategoricalDtype)]
    )
    n_floats = len([dtype for dtype in data.dtypes if dtype.kind == "f"])
    assert expected_data_categories == n_categories
    assert expected_data_floats == n_floats

    assert isinstance(target, pd.Series)
    assert target.shape == target_shape
    assert target.name == target_column

    assert isinstance(frame, pd.DataFrame)
    assert frame.shape == frame_shape
//  [python]
def test_fetch_openml_cache(monkeypatch, gzip_response, tmpdir):
    def _mock_urlopen_raise(request):
        raise ValueError(
            "This mechanism intends to test correct cache"
            "handling. As such, urlopen should never be "
            "accessed. URL: %s"
            % request.get_full_url()
        )

    data_id = 2
    cache_directory = str(tmpdir.mkdir("scikit_learn_data"))
    _monkey_patch_webbased_functions(monkeypatch, data_id, gzip_response)
    X_fetched, y_fetched = fetch_openml(
        data_id=data_id,
        cache=True,
        data_home=cache_directory,
        return_X_y=True,
        as_frame=False,
    )

    monkeypatch.setattr(sklearn.datasets._openml, "urlopen", _mock_urlopen_raise)

    X_cached, y_cached = fetch_openml(
        data_id=data_id,
        cache=True,
        data_home=cache_directory,
        return_X_y=True,
        as_frame=False,
    )
    np.testing.assert_array_equal(X_fetched, X_cached)
    np.testing.assert_array_equal(y_fetched, y_cached)
//  [python]
def test_fetch_openml_miceprotein_pandas(monkeypatch):
    # JvR: very important check, as this dataset defined several row ids
    # and ignore attributes. Note that data_features json has 82 attributes,
    # and row id (1), ignore attributes (3) have been removed.
    pd = pytest.importorskip("pandas")
    CategoricalDtype = pd.api.types.CategoricalDtype

    data_id = 40966
    data_shape = (7, 77)
    target_shape = (7,)
    frame_shape = (7, 78)

    target_column = "class"
    frame_n_categories = 1
    frame_n_floats = 77

    _monkey_patch_webbased_functions(monkeypatch, data_id, True)
    bunch = fetch_openml(data_id=data_id, as_frame=True, cache=False)
    data = bunch.data
    target = bunch.target
    frame = bunch.frame

    assert isinstance(data, pd.DataFrame)
    assert data.shape == data_shape
    assert np.all(data.dtypes == np.float64)

    assert isinstance(target, pd.Series)
    assert isinstance(target.dtype, CategoricalDtype)
    assert target.shape == target_shape
    assert target.name == target_column

    assert isinstance(frame, pd.DataFrame)
    assert frame.shape == frame_shape
    n_categories = len(
        [dtype for dtype in frame.dtypes if isinstance(dtype, CategoricalDtype)]
    )
    n_floats = len([dtype for dtype in frame.dtypes if dtype.kind == "f"])
    assert frame_n_categories == n_categories
    assert frame_n_floats == n_floats
//  [python]
def test_fetch_nonexiting(monkeypatch, gzip_response):
    # there is no active version of glass2
    data_id = 40675
    _monkey_patch_webbased_functions(monkeypatch, data_id, gzip_response)
    # Note that we only want to search by name (not data id)
    msg = "No active dataset glass2 found"
    with pytest.raises(ValueError, match=msg):
        fetch_openml(name="glass2", cache=False)
//  [python]
def test_fetch_openml_inactive(monkeypatch, gzip_response):
    # fetch inactive dataset by id
    data_id = 40675
    _monkey_patch_webbased_functions(monkeypatch, data_id, gzip_response)
    msg = "Version 1 of dataset glass2 is inactive,"
    with pytest.warns(UserWarning, match=msg):
        glas2 = fetch_openml(data_id=data_id, cache=False, as_frame=False)
    # fetch inactive dataset by name and version
    assert glas2.data.shape == (163, 9)
    with pytest.warns(UserWarning, match=msg):
        glas2_by_version = fetch_openml(
            data_id=None, name="glass2", cache=False, version=1, as_frame=False
        )
    assert int(glas2_by_version.details["id"]) == data_id
//  [python]
def test_fetch_openml_emotions_pandas(monkeypatch):
    # classification dataset with multiple targets (natively)
    pd = pytest.importorskip("pandas")
    CategoricalDtype = pd.api.types.CategoricalDtype

    data_id = 40589
    target_column = [
        "amazed.suprised",
        "happy.pleased",
        "relaxing.calm",
        "quiet.still",
        "sad.lonely",
        "angry.aggresive",
    ]
    data_shape = (13, 72)
    target_shape = (13, 6)
    frame_shape = (13, 78)

    expected_frame_categories = 6
    expected_frame_floats = 72

    _monkey_patch_webbased_functions(monkeypatch, data_id, True)
    bunch = fetch_openml(
        data_id=data_id, as_frame=True, cache=False, target_column=target_column
    )
    data = bunch.data
    target = bunch.target
    frame = bunch.frame

    assert isinstance(data, pd.DataFrame)
    assert data.shape == data_shape

    assert isinstance(target, pd.DataFrame)
    assert target.shape == target_shape
    assert np.all(target.columns == target_column)

    assert isinstance(frame, pd.DataFrame)
    assert frame.shape == frame_shape
    n_categories = len(
        [dtype for dtype in frame.dtypes if isinstance(dtype, CategoricalDtype)]
    )
    n_floats = len([dtype for dtype in frame.dtypes if dtype.kind == "f"])
    assert expected_frame_categories == n_categories
    assert expected_frame_floats == n_floats
//  [python]
def test_raises_illegal_multitarget(monkeypatch, gzip_response):
    data_id = 61
    targets = ["sepalwidth", "class"]
    _monkey_patch_webbased_functions(monkeypatch, data_id, gzip_response)
    # Note that we only want to search by name (not data id)
    msg = "Can only handle homogeneous multi-target datasets,"
    with pytest.raises(ValueError, match=msg):
        fetch_openml(data_id=data_id, target_column=targets, cache=False)
//  [python]
def test_string_attribute_without_dataframe(monkeypatch, gzip_response):
    data_id = 40945
    _monkey_patch_webbased_functions(monkeypatch, data_id, gzip_response)
    # single column test
    msg = (
        "STRING attributes are not supported for "
        "array representation. Try as_frame=True"
    )
    with pytest.raises(ValueError, match=msg):
        fetch_openml(data_id=data_id, cache=False, as_frame=False)
//  [python]
def test_dataset_with_openml_warning(monkeypatch, gzip_response):
    data_id = 3
    _monkey_patch_webbased_functions(monkeypatch, data_id, gzip_response)
    msg = "OpenML raised a warning on the dataset. It might be unusable. Warning:"
    with pytest.warns(UserWarning, match=msg):
        fetch_openml(data_id=data_id, cache=False, as_frame=False)
//  [python]
def test_fetch_openml_raises_missing_values_target(monkeypatch, gzip_response):
    data_id = 2
    _monkey_patch_webbased_functions(monkeypatch, data_id, gzip_response)
    msg = "Target column "
    with pytest.raises(ValueError, match=msg):
        fetch_openml(data_id=data_id, target_column="family")
//  [python]
def test_illegal_column(monkeypatch, gzip_response):
    data_id = 61
    _monkey_patch_webbased_functions(monkeypatch, data_id, gzip_response)
    msg = "Could not find target_column="
    with pytest.raises(KeyError, match=msg):
        fetch_openml(data_id=data_id, target_column="undefined", cache=False)

    with pytest.raises(KeyError, match=msg):
        fetch_openml(data_id=data_id, target_column=["undefined", "class"], cache=False)
//  [python]
def test_missing_values_pandas(monkeypatch):
    """check that missing values in categories are compatible with pandas
    categorical"""
    pytest.importorskip("pandas")

    data_id = 42585
    _monkey_patch_webbased_functions(monkeypatch, data_id, True)
    penguins = fetch_openml(data_id=data_id, cache=False, as_frame=True)

    cat_dtype = penguins.data.dtypes["sex"]
    # there are nans in the categorical
    assert penguins.data["sex"].isna().any()
    assert_array_equal(cat_dtype.categories, ["FEMALE", "MALE", "_"])
//  [python]
def test_warn_ignore_attribute(monkeypatch, gzip_response):
    data_id = 40966
    expected_row_id_msg = "target_column={} has flag is_row_identifier."
    expected_ignore_msg = "target_column={} has flag is_ignore."
    _monkey_patch_webbased_functions(monkeypatch, data_id, gzip_response)
    # single column test
    target_col = "MouseID"
    msg = expected_row_id_msg.format(target_col)
    with pytest.warns(UserWarning, match=msg):
        fetch_openml(
            data_id=data_id, target_column=target_col, cache=False, as_frame=False
        )
    target_col = "Genotype"
    msg = expected_ignore_msg.format(target_col)
    with pytest.warns(UserWarning, match=msg):
        fetch_openml(
            data_id=data_id, target_column=target_col, cache=False, as_frame=False
        )
    # multi column test
    target_col = "MouseID"
    msg = expected_row_id_msg.format(target_col)
    with pytest.warns(UserWarning, match=msg):
        fetch_openml(
            data_id=data_id,
            target_column=[target_col, "class"],
            cache=False,
            as_frame=False,
        )
    target_col = "Genotype"
    msg = expected_ignore_msg.format(target_col)
    with pytest.warns(UserWarning, match=msg):
        fetch_openml(
            data_id=data_id,
            target_column=[target_col, "class"],
            cache=False,
            as_frame=False,
        )
//  [python]
def test_fetch_openml_raises_illegal_argument():
    msg = "Dataset data_id=-1 and version=version passed, but you can only"
    with pytest.raises(ValueError, match=msg):
        fetch_openml(data_id=-1, name=None, version="version")

    msg = "Dataset data_id=-1 and name=name passed, but you can only"
    with pytest.raises(ValueError, match=msg):
        fetch_openml(data_id=-1, name="nAmE")

    with pytest.raises(ValueError, match=msg):
        fetch_openml(data_id=-1, name="nAmE", version="version")

    msg = "Neither name nor data_id are provided. Please provide name or data_id."
    with pytest.raises(ValueError, match=msg):
        fetch_openml()
//  [python]
def encoder_test() -> pl.DataFrame:
    data = fetch_openml(name="house_prices", as_frame=True, parser="auto")
    use = ["Id", "MSSubClass", "MSZoning", "LotFrontage", "YearBuilt", "Heating", "CentralAir"]
    df = data.data[use].copy()
    df["target"] = [1 if x > 200000 else 0 for x in data.target]
    df = pd.concat([df.copy()]*50)
    return pl.from_pandas(df)
//  [python]
print("loading data")
data, labels = s.fetch_mldata('mnist_original', data_home='./')
//  [python]
print("loading data")
data, labels = skl_data.fetch_openml('mnist_784', version=1, return_X_y=True)
//  [python]
def load_ames_housing():
    df = fetch_openml(name="house_prices", version=1, as_frame=True, parser="pandas")
    X = df.data
    y = df.target

    features = [
        "YrSold",
        "HeatingQC",
        "Street",
        "YearRemodAdd",
        "Heating",
        "MasVnrType",
        "BsmtUnfSF",
        "Foundation",
        "MasVnrArea",
        "MSSubClass",
        "ExterQual",
        "Condition2",
        "GarageCars",
        "GarageType",
        "OverallQual",
        "TotalBsmtSF",
        "BsmtFinSF1",
        "HouseStyle",
        "MiscFeature",
        "MoSold",
    ]

    X = X.loc[:, features]
    X, y = shuffle(X, y, random_state=0)

    X = X.iloc[:600]
    y = y.iloc[:600]
    return X, np.log(y)
//  [python]
def load_ames_housing():
    df = fetch_openml(name="house_prices", as_frame=True, parser="pandas")
    X = df.data
    y = df.target

    features = [
        "YrSold",
        "HeatingQC",
        "Street",
        "YearRemodAdd",
        "Heating",
        "MasVnrType",
        "BsmtUnfSF",
        "Foundation",
        "MasVnrArea",
        "MSSubClass",
        "ExterQual",
        "Condition2",
        "GarageCars",
        "GarageType",
        "OverallQual",
        "TotalBsmtSF",
        "BsmtFinSF1",
        "HouseStyle",
        "MiscFeature",
        "MoSold",
    ]

    X = X.loc[:, features]
    X, y = shuffle(X, y, random_state=0)

    X = X.iloc[:600]
    y = y.iloc[:600]
    return X, np.log(y)
//  [python]
def get_mauna_loa() -> T.Tuple[TensorLike, TensorLike]:
    """
    Loads the Mauna Loa CO2 dataset.

    This function loads the Mauna Loa CO2 dataset, which contains measurements of atmospheric CO2 concentrations
    at the Mauna Loa Observatory in Hawaii.

    :return: A tuple containing the input data (X) and target labels (y).
    :rtype: tuple[TensorLike, TensorLike]
    """
    co2 = datasets.load_linnerud()
    co2_data = pd.DataFrame(data=co2.data, columns=co2.feature_names)
    co2_data["date"] = pd.to_datetime(co2_data[["year", "month", "day"]])
    co2_data = co2_data[["date", "co2"]].set_index("date")
    X = (co2_data.index.year + co2_data.index.month / 12).to_numpy().reshape(-1, 1)
    y = co2_data["co2"].to_numpy()
    return X, y
//  [python]
def get_mauna_loa() -> T.Tuple[TensorLike, TensorLike]:
    """
    Loads the Mauna Loa CO2 dataset.

    This function loads the Mauna Loa CO2 dataset, which contains measurements of atmospheric CO2 concentrations
    at the Mauna Loa Observatory in Hawaii.

    :return: A tuple containing the input data (X) and target labels (y).
    :rtype: tuple[TensorLike, TensorLike]
    """
    co2 = sklearn.datasets.fetch_openml(data_id=41187, as_frame=True)
    co2_data = co2.frame
    co2_data["date"] = pd.to_datetime(co2_data[["year", "month", "day"]])
    co2_data = co2_data[["date", "co2"]].set_index("date")
    X = (co2_data.index.year + co2_data.index.month / 12).to_numpy().reshape(-1, 1)
    y = co2_data["co2"].to_numpy()
    return X, y
//  [python]
print("loading data")

# Load data from https://www.openml.org/d/554
data, labels = skl_data.fetch_openml('mnist_784', version=1, return_X_y=True)
//  [python]
def get_openml(id):
    return sklearn.datasets.fetch_openml(
        data_id=id,
        target_column=None,
        as_frame=True,
    )['data']
//  [python]
def get_abalone_datamanager():
    # https://www.openml.org/d/183
    dataset_name = "abalone"
    data = sklearn.datasets.fetch_openml(data_id=183, as_frame=True)
    feat_type = {
        i: "Categorical" if x.name == "category" else "Numerical"
        for i, x in enumerate(data["data"].dtypes)
    }
    X, y = sklearn.datasets.fetch_openml(data_id=183, return_X_y=True, as_frame=False)
    y = preprocessing.LabelEncoder().fit_transform(y)
    X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(
        X, y, random_state=1
    )

    D = XYDataManager(
        X_train,
        y_train,
        X_test,
        y_test,
        MULTICLASS_CLASSIFICATION,
        feat_type,
        dataset_name,
    )
    return D
//  [python]
seed = 42
X, y = sklearn.datasets.fetch_openml(data_id=61, return_X_y=True, as_frame=True)
//  [python]
def get_adult(n_foreground, n_background):
    X, y = sklearn.datasets.fetch_openml("adult", version=1, return_X_y=True, as_frame=False)
    y = np.array([y_i != '<=50K' for y_i in y])
    model = xgb.XGBClassifier().fit(X, y)
    X_foreground = X[:n_foreground]
    X_background = X[:n_background].copy()
    tree_explainer = shap.TreeExplainer(model, X_background)
    exact_shap_values = tree_explainer.shap_values(X_foreground)
    return (model, X_background, X_foreground, exact_shap_values)
//  [python]
def get_adult(n_foreground, n_background):
    X, y = sklearn.datasets.fetch_openml("adult", return_X_y=True)
    y = np.array([y_i != '<=50K' for y_i in y])
    model = xgb.XGBClassifier().fit(X, y)
    X_foreground = X[:n_foreground]
    X_background = X[:n_background].copy()
    tree_explainer = shap.TreeExplainer(model, X_background)
    exact_shap_values = tree_explainer.shap_values(X_foreground)
    return (model, X_background, X_foreground, exact_shap_values)
//  [python]
def test_1(self):
        """
        From https://contrib.scikit-learn.org/category_encoders/targetencoder.html
        >>> from category_encoders import *
        >>> import pandas as pd
        >>> from sklearn.datasets import fetch_openml
        >>> display_cols = ["Id", "MSSubClass", "MSZoning", "LotFrontage", "YearBuilt", "Heating", "CentralAir"]
        >>> bunch = fetch_openml(name="house_prices", as_frame=True)
        >>> y = bunch.target > 200000
        >>> X = pd.DataFrame(bunch.data, columns=bunch.feature_names)[display_cols]
        >>> enc = TargetEncoder(cols=['CentralAir', 'Heating'], min_samples_leaf=20, smoothing=10).fit(X, y)
        >>> numeric_dataset = enc.transform(X)
        """
        display_cols = [
            "Id",
            "MSSubClass",
            "MSZoning",
            "LotFrontage",
            "YearBuilt",
            "Heating",
            "CentralAir",
        ]
        bunch = fetch_openml(name="house_prices", as_frame=True)
        y = bunch.target > 200000
        X = pd.DataFrame(bunch.data, columns=bunch.feature_names)[display_cols]
        sk_enc = SkTargetEncoder(
            cols=["CentralAir", "Heating"], min_samples_leaf=20, smoothing=10
        ).fit(X, y)
        sk_transformed = sk_enc.transform(X)
        classes = sorted(list(y.unique()))
        for target in self.targets:
            rasl_enc = RaslTargetEncoder(
                cols=["CentralAir", "Heating"],
                min_samples_leaf=20,
                smoothing=10,
                classes=classes,
            ).fit(X, y)
            X2 = Convert(astype=target).transform(X)
            rasl_transformed = rasl_enc.transform(X2)
            _check_trained_target_encoder(self, sk_enc, rasl_enc.impl, target)
            _check_data(self, sk_transformed, rasl_transformed, target)
//  [python]
def test_tabular_input_support(openml_id, backend):
    """
    Make sure we can process inputs with NaN in categorical and Object columns
    when the later is possible
    """

    # Get the data and check that contents of data-manager make sense
    X, y = fetch_openml(
        data_id=int(openml_id),
        return_X_y=True, as_frame=True
    )

    # Make sure we are robust against objects
    X[X.columns[0]] = X[X.columns[0]].astype(object)

    X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(
        X, y, random_state=1)
    # Search for a good configuration
    estimator = TabularClassificationTask(
        backend=backend,
        resampling_strategy=HoldoutValTypes.holdout_validation,
        ensemble_size=0,
    )

    estimator._do_dummy_prediction = unittest.mock.MagicMock()

    with unittest.mock.patch.object(AutoMLSMBO, 'run_smbo') as AutoMLSMBOMock:
        AutoMLSMBOMock.return_value = (RunHistory(), {}, 'epochs')
        estimator.search(
            X_train=X_train, y_train=y_train,
            X_test=X_test, y_test=y_test,
            optimize_metric='accuracy',
            total_walltime_limit=150,
            func_eval_time_limit_secs=50,
            enable_traditional_pipeline=False,
            load_models=False,
        )
//  [python]
def test_tabular_input_support(openml_id, backend):
    """
    Make sure we can process inputs with NaN in categorical and Object columns
    when the later is possible
    """

    # Get the data and check that contents of data-manager make sense
    X, y = sklearn.datasets.fetch_openml(
        data_id=int(openml_id),
        return_X_y=True, as_frame=True
    )

    # Make sure we are robust against objects
    X[X.columns[0]] = X[X.columns[0]].astype(object)

    X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(
        X, y, random_state=1)
    # Search for a good configuration
    estimator = TabularClassificationTask(
        backend=backend,
        resampling_strategy=HoldoutValTypes.holdout_validation,
        ensemble_size=0,
    )

    estimator._do_dummy_prediction = unittest.mock.MagicMock()

    with unittest.mock.patch.object(AutoMLSMBO, 'run_smbo') as AutoMLSMBOMock:
        AutoMLSMBOMock.return_value = (RunHistory(), {}, 'epochs')
        estimator.search(
            X_train=X_train, y_train=y_train,
            X_test=X_test, y_test=y_test,
            optimize_metric='accuracy',
            total_walltime_limit=150,
            func_eval_time_limit_secs=50,
            enable_traditional_pipeline=False,
            load_models=False,
        )
//  [python]
def test_portfolio_selection_failure(openml_id, backend, n_samples):

    # Get the data and check that contents of data-manager make sense
    X, y, _, _ = sklearn.datasets.fetch_openml(
        data_id=int(openml_id),
        return_X_y=True, as_frame=True
    )
    X, y = X.iloc[:n_samples], y.iloc[:n_samples]

    X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(
        X, y, random_state=1)

    estimator = TabularClassificationTask(
        backend=backend,
        resampling_strategy=HoldoutValTypes.holdout_validation,
    )
    with pytest.raises(FileNotFoundError, match=r"The path: .+? provided for 'portfolio_selection' "
                                                r"for the file containing the portfolio configurations "
                                                r"does not exist\. Please provide a valid path"):
        estimator.search(
            X_train=X_train, y_train=y_train,
            X_test=X_test, y_test=y_test,
            optimize_metric='accuracy',
            total_walltime_limit=30,
            func_eval_time_limit_secs=5,
            enable_traditional_pipeline=False,
            portfolio_selection="random_path_to_test.json"
        )
//  [python]
def test_portfolio_selection_failure(openml_id, backend, n_samples):

    # Get the data and check that contents of data-manager make sense
    X, y = sklearn.datasets.fetch_openml(
        data_id=int(openml_id),
        return_X_y=True, as_frame=True
    )
    X, y = X.iloc[:n_samples], y.iloc[:n_samples]

    X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(
        X, y, random_state=1)

    estimator = TabularClassificationTask(
        backend=backend,
        resampling_strategy=HoldoutValTypes.holdout_validation,
    )
    with pytest.raises(FileNotFoundError, match=r"The path: .+? provided for 'portfolio_selection' "
                                                r"for the file containing the portfolio configurations "
                                                r"does not exist\. Please provide a valid path"):
        estimator.search(
            X_train=X_train, y_train=y_train,
            X_test=X_test, y_test=y_test,
            optimize_metric='accuracy',
            total_walltime_limit=30,
            func_eval_time_limit_secs=5,
            enable_traditional_pipeline=False,
            portfolio_selection="random_path_to_test.json"
        )
//  [python]
def _test():
    import matplotlib.pyplot as plt
    import sklearn.datasets
    import tqdm.auto

    np.random.seed(32)

    batch_size = 256
    train_epochs = 4
    learning_rate = 2e-4

    channels_num = (1, 8, 8)
    kernel_sizes = (3, 3)

    linear_dims = (4608, 128, 10)

    X, y = sklearn.datasets.fetch_mldata("mnist-original", data_home=".")
    X += 64 * np.random.randn(*X.shape)
    X = (X / 255.0).reshape(-1, 28, 28, 1)

    inds = np.arange(y.size)
    np.random.shuffle(inds)
    X = X[inds, :]
    y = y[inds]

    y = y.astype(int, copy=False)

    X_train, X_eval, X_test = X[:6000], X[6000:7000], X[8000:9000]
    y_train, y_eval, y_test = y[:6000], y[6000:7000], y[8000:9000]

    print("Train shape :", X_train.shape)
    print("Eval shape  :", X_eval.shape)
    print("Test shape  :", X_test.shape)

    n = X_train.shape[0]

    model = AttentionCNN(channels_num, kernel_sizes, linear_dims)
    criterion = losses.CrossEntropyLoss()
    optim = optimizers.Nadam(model.parameters, learning_rate=learning_rate)

    inds = np.arange(X_train.shape[0])

    for epoch in np.arange(1, 1 + train_epochs):
        total_loss_train = total_loss_eval = 0.0
        it = 0

        np.random.shuffle(inds)
        X_train = X_train[inds, :]
        y_train = y_train[inds]

        for start in tqdm.auto.tqdm(np.arange(0, n, batch_size)):
            optim.zero_grad()

            end = start + batch_size
            X_batch = X_train[start:end, :]
            y_batch = y_train[start:end]

            model.train()
            y_logits = model(X_batch)
            loss, loss_grad = criterion(y_batch, y_logits)
            model.backward(loss_grad)
            total_loss_train += loss

            optim.clip_grads_norm()
            optim.step()

            model.eval()
            y_logits = model(X_eval)
            loss, _ = criterion(y_eval, y_logits)
            total_loss_eval += loss

            it += 1

        total_loss_train /= it
        total_loss_eval /= it

        print(f"Total loss (train) : {total_loss_train:.3f}")
        print(f"Total loss (eval)  : {total_loss_eval:.3f}")

    model.train()
    y_preds_logits = model(X_test)
    loss, loss_grad = criterion(y_test, y_preds_logits)
    dX = model.backward(loss_grad)

    y_preds = y_preds_logits.argmax(axis=-1).ravel()
    print(y_preds[:10])
    print(y_test[:10])
    test_acc = float(np.mean(y_preds == y_test.ravel()))
    print(f"Test acc: {test_acc:.3f}")

    X_test_plot = X_test[:10, ...]
    dX_test_plot = dX[:10, ...]
    fig, axes = plt.subplots(
        3, X_test_plot.shape[0], figsize=(15, 10), sharex=True, sharey=True
    )

    for i, (X, dX) in enumerate(zip(X_test_plot, dX_test_plot)):
        A = np.squeeze(X)
        B = np.squeeze(dX)
        W = A * (B - np.min(B)) / (1e-7 + np.ptp(B))
        axes[0][i].imshow(A, cmap="hot")
        axes[1][i].imshow(B, cmap="gray")
        axes[2][i].imshow(W, cmap="hot")

    plt.tight_layout()
    plt.show()
//  [python]
def _test():
    import matplotlib.pyplot as plt
    import sklearn.datasets
    import tqdm.auto

    np.random.seed(32)

    batch_size = 256
    train_epochs = 4
    learning_rate = 2e-4

    channels_num = (1, 8, 8)
    kernel_sizes = (3, 3)

    linear_dims = (4608, 128, 10)

    X, y = sklearn.datasets.fetch_openml(
        "mnist_784", version=1, return_X_y=True, as_frame=False
    )
    X += 64 * np.random.randn(*X.shape)
    X = (X / 255.0).reshape(-1, 28, 28, 1)

    inds = np.arange(y.size)
    np.random.shuffle(inds)
    X = X[inds, :]
    y = y[inds]

    y = y.astype(int, copy=False)

    X_train, X_eval, X_test = X[:6000], X[6000:7000], X[8000:9000]
    y_train, y_eval, y_test = y[:6000], y[6000:7000], y[8000:9000]

    print("Train shape :", X_train.shape)
    print("Eval shape  :", X_eval.shape)
    print("Test shape  :", X_test.shape)

    n = X_train.shape[0]

    model = AttentionCNN(channels_num, kernel_sizes, linear_dims)
    criterion = losses.CrossEntropyLoss()
    optim = optimizers.Nadam(model.parameters, learning_rate=learning_rate)

    inds = np.arange(X_train.shape[0])

    for epoch in np.arange(1, 1 + train_epochs):
        total_loss_train = total_loss_eval = 0.0
        it = 0

        np.random.shuffle(inds)
        X_train = X_train[inds, :]
        y_train = y_train[inds]

        for start in tqdm.auto.tqdm(np.arange(0, n, batch_size)):
            optim.zero_grad()

            end = start + batch_size
            X_batch = X_train[start:end, :]
            y_batch = y_train[start:end]

            model.train()
            y_logits = model(X_batch)
            loss, loss_grad = criterion(y_batch, y_logits)
            model.backward(loss_grad)
            total_loss_train += loss

            optim.clip_grads_norm()
            optim.step()

            model.eval()
            y_logits = model(X_eval)
            loss, _ = criterion(y_eval, y_logits)
            total_loss_eval += loss

            it += 1

        total_loss_train /= it
        total_loss_eval /= it

        print(f"Total loss (train) : {total_loss_train:.3f}")
        print(f"Total loss (eval)  : {total_loss_eval:.3f}")

    model.train()
    y_preds_logits = model(X_test)
    loss, loss_grad = criterion(y_test, y_preds_logits)
    dX = model.backward(loss_grad)

    y_preds = y_preds_logits.argmax(axis=-1).ravel()
    print(y_preds[:10])
    print(y_test[:10])
    test_acc = float(np.mean(y_preds == y_test.ravel()))
    print(f"Test acc: {test_acc:.3f}")

    X_test_plot = X_test[:10, ...]
    dX_test_plot = dX[:10, ...]
    fig, axes = plt.subplots(
        3, X_test_plot.shape[0], figsize=(15, 10), sharex=True, sharey=True
    )

    for i, (X, dX) in enumerate(zip(X_test_plot, dX_test_plot)):
        A = np.squeeze(X)
        B = np.squeeze(dX)
        W = A * (B - np.min(B)) / (1e-7 + np.ptp(B))
        axes[0][i].imshow(A, cmap="hot")
        axes[1][i].imshow(B, cmap="gray")
        axes[2][i].imshow(W, cmap="hot")

    plt.tight_layout()
    plt.show()