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

Added the direct dtype conversion in zipped_hist #152

Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/insight/metrics/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def zipped_hist(

joint = pd.concat(data)
is_continuous = check.continuous(pd.Series(joint))
joint = check.infer_dtype(joint)
data = tuple([check.infer_dtype(series) for series in data])

# Compute histograms of the data, bin if continuous
if is_continuous:
Expand Down
21 changes: 21 additions & 0 deletions tests/test_metrics/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,27 @@ def test_kl_divergence(group1):
assert kl_divergence(group1, group1) == 0


def test_kl_divergence_with_custom_check():

class CustomCheck(ColumnCheck):
def infer_dtype(self, sr: pd.Series) -> pd.Series:
col = sr.copy()
col = pd.to_numeric(col, errors="coerce")
return col.astype(int)

sr_g = pd.Series(["012345", "67890", "123456", "789012"])
sr_h = pd.Series(["123456", "78901", "234567", "890123"])
# sanity check
check = CustomCheck(min_num_unique=1)
assert check.infer_dtype(sr_g).dtype == np.int64
assert check.continuous(sr_g)
assert check.infer_dtype(sr_h).dtype == np.int64
assert check.continuous(sr_h)
# actual test
kl_divergence_with_custom_check = KullbackLeiblerDivergence(check=check)
assert abs(kl_divergence_with_custom_check(sr_g, sr_h) - 0.3) < 0.01


def test_js_divergence(group1, group2, group3):
assert js_divergence(pd.Series([1, 0]), pd.Series([1, 0])) == 0

Expand Down
Loading