Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add testing for JSD metrics #100

Merged
merged 6 commits into from
Jan 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions tests/metrics/test_jsd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from __future__ import annotations

import random

import numpy as np
import pandas as pd
import pytest

from sdgx.metrics.column.jsd import JSD


# 创建测试数据
@pytest.fixture
def dummy_data(dummy_single_table_path):
yield pd.read_csv(dummy_single_table_path)


@pytest.fixture
def test_data():
role_set = ["admin", "user", "guest"]
df = pd.DataFrame(
{
"role": [random.choice(role_set) for _ in range(10)],
"feature_x": [random.random() for _ in range(10)],
}
)
return df


@pytest.fixture
def jsd_instance():
return JSD()


def test_jsd_discrete(dummy_data, test_data, jsd_instance):
cols = ["role"]
result = jsd_instance.calculate(dummy_data, test_data, cols, discrete=True)
result1 = jsd_instance.calculate(dummy_data, dummy_data, cols, discrete=True)
result2 = jsd_instance.calculate(test_data, dummy_data, cols, discrete=True)

assert result >= 0
assert result <= 1
assert result1 == 0
assert result2 == result


def test_jsd_continuous(dummy_data, test_data, jsd_instance):
cols = ["feature_x"]
result = jsd_instance.calculate(dummy_data, test_data, cols, discrete=False)
result1 = jsd_instance.calculate(dummy_data, dummy_data, cols, discrete=False)

assert result >= 0
assert result <= 1
assert result1 == 0


if __name__ == "__main__":
pytest.main(["-vv", "-s", __file__])