Skip to content

Commit

Permalink
KendallTauCorrelation datetime support (#158)
Browse files Browse the repository at this point in the history
* cast datetime to int in kt correlation
  • Loading branch information
nialldevlin1 authored Jan 8, 2024
1 parent 2b430fc commit 5f19bc5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/insight/metrics/metrics.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""This module contains various metrics used across synthesized."""
import datetime as dt
import typing as ty

import numpy as np
Expand Down Expand Up @@ -75,6 +76,13 @@ def check_column_types(cls, sr_a: pd.Series, sr_b: pd.Series, check: Check = Col
return False
return True

@staticmethod
def _check_dtype(sr, func) -> bool:
for val in sr:
if pd.notna(val) and not func(val):
return False
return True

def _compute_metric(self, sr_a: pd.Series, sr_b: pd.Series):
"""Calculate the metric.
Expand All @@ -85,6 +93,12 @@ def _compute_metric(self, sr_a: pd.Series, sr_b: pd.Series):
Returns:
The Kendall Tau coefficient between sr_a and sr_b.
"""

if self._check_dtype(sr_a, lambda x: isinstance(x, dt.datetime)):
sr_a = sr_a.astype("int")
if self._check_dtype(sr_b, lambda x: isinstance(x, dt.datetime)):
sr_b = sr_b.astype("int")

if hasattr(sr_a, "cat") and sr_a.cat.ordered:
sr_a = sr_a.cat.codes

Expand Down
2 changes: 2 additions & 0 deletions tests/test_metrics/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,15 @@ def test_kt_correlation():
sr_f = pd.Series(
list("feeddd"), dtype=pd.CategoricalDtype(categories=list("fed"), ordered=True)
)
sr_g = pd.to_datetime(pd.Series(np.random.normal(0, 1, 5), name="g"))

kt_corr = KendallTauCorrelation()

assert kt_corr(sr_a, sr_a) is not None
assert kt_corr(sr_b, sr_c) is not None
assert kt_corr(sr_c, sr_d) is None
assert kt_corr(sr_e, sr_f) == 1.0
assert kt_corr(sr_g, sr_g) is not None


def test_cramers_v_basic():
Expand Down

0 comments on commit 5f19bc5

Please sign in to comment.