<gh_stars>1-10
#!/usr/bin/env python
__author__ = "XXX"
__email__ = "XXX"

import pytest
from constants import *
from datasets.split_dataset import split_hit_rate


def test_split_pandas_data_with_ratios(dummy_dataset_pandas):
    from datasets.split_dataset import split_pandas_data_with_ratios

    splits = split_pandas_data_with_ratios(dummy_dataset_pandas, ratios=[0.5, 0.5])
    # Added tolerance 1 for odd divisions
    assert len(splits[0]) == pytest.approx(round(len(dummy_dataset_pandas) / 2), 1)
    assert len(splits[1]) == pytest.approx(round(len(dummy_dataset_pandas) / 2), 1)

    splits = split_pandas_data_with_ratios(
        dummy_dataset_pandas, ratios=[0.12, 0.36, 0.52]
    )
    shape = dummy_dataset_pandas.shape[0]
    assert len(splits[0]) == round(shape * 0.12)
    assert len(splits[1]) == round(shape * 0.36)
    assert len(splits[2]) == round(shape * 0.52)

    with pytest.raises(ValueError):
        splits = split_pandas_data_with_ratios(
            dummy_dataset_pandas, ratios=[0.6, 0.2, 0.4]
        )


@pytest.mark.parametrize("ratios", [[0.8, 0.1, 0.1], [0.9, 0.1], [0.5, 0.5, 0.5]])
def test_split_stratified(dummy_dataset_pandas, ratios):
    from datasets.split_dataset import split_stratified

    splits = split_stratified(dummy_dataset_pandas, ratios)
    if len(splits) == 3:
        users_train = splits[0][DEFAULT_USER_COL].unique()
        users_val = splits[1][DEFAULT_USER_COL].unique()
        users_test = splits[2][DEFAULT_USER_COL].unique()
        assert set(users_train) == set(users_test) == set(users_val)

    for i, ratio in enumerate(ratios):
        assert len(splits[i]) == pytest.approx(len(dummy_dataset_pandas) * ratio, 1)

def test_split_hit_rate(dummy_dataset_pandas):
    train, test = split_hit_rate(dummy_dataset_pandas)
    # check only a single interaction per user has been removed
    assert len(train[DEFAULT_USER_COL].unique()) == len(test)

#!/usr/bin/env python
__author__ = "XXX"
__email__ = "XXX"

import pytest
from constants import *
from datasets.split_dataset import split_hit_rate


def test_split_pandas_data_with_ratios(dummy_dataset_pandas):
    from datasets.split_dataset import split_pandas_data_with_ratios

    splits = split_pandas_data_with_ratios(dummy_dataset_pandas, ratios=[0.5, 0.5])
    # Added tolerance 1 for odd divisions
    assert len(splits[0]) == pytest.approx(round(len(dummy_dataset_pandas) / 2), 1)
    assert len(splits[1]) == pytest.approx(round(len(dummy_dataset_pandas) / 2), 1)

    splits = split_pandas_data_with_ratios(
        dummy_dataset_pandas, ratios=[0.12, 0.36, 0.52]
    )
    shape = dummy_dataset_pandas.shape[0]
    assert len(splits[0]) == round(shape * 0.12)
    assert len(splits[1]) == round(shape * 0.36)
    assert len(splits[2]) == round(shape * 0.52)

    with pytest.raises(ValueError):
        splits = split_pandas_data_with_ratios(
            dummy_dataset_pandas, ratios=[0.6, 0.2, 0.4]
        )


@pytest.mark.parametrize("ratios", [[0.8, 0.1, 0.1], [0.9, 0.1], [0.5, 0.5, 0.5]])
def test_split_stratified(dummy_dataset_pandas, ratios):
    from datasets.split_dataset import split_stratified

    splits = split_stratified(dummy_dataset_pandas, ratios)
    if len(splits) == 3:
        users_train = splits[0][DEFAULT_USER_COL].unique()
        users_val = splits[1][DEFAULT_USER_COL].unique()
        users_test = splits[2][DEFAULT_USER_COL].unique()
        assert set(users_train) == set(users_test) == set(users_val)

    for i, ratio in enumerate(ratios):
        assert len(splits[i]) == pytest.approx(len(dummy_dataset_pandas) * ratio, 1)

def test_split_hit_rate(dummy_dataset_pandas):
    train, test = split_hit_rate(dummy_dataset_pandas)
    # check only a single interaction per user has been removed
    assert len(train[DEFAULT_USER_COL].unique()) == len(test)
#!/usr/bin/env python # Added tolerance 1 for odd divisions # check only a single interaction per user has been removed