From 4f9fe123c3226b3cea7344c201d361acecc07762 Mon Sep 17 00:00:00 2001 From: Jacan Chaplais Date: Fri, 10 Nov 2023 16:30:25 +0000 Subject: [PATCH] python alias to wrap jaccard njit #165 --- graphicle/calculate.py | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/graphicle/calculate.py b/graphicle/calculate.py index 3105fc7..a1a3d0f 100644 --- a/graphicle/calculate.py +++ b/graphicle/calculate.py @@ -979,7 +979,7 @@ def spherocity( "float64(bool_[:], bool_[:], Omitted(None))", ] ) -def jaccard_distance( +def _jaccard_distance( u: base.BoolVector, v: base.BoolVector, w: ty.Optional[base.DoubleVector] = None, @@ -1012,3 +1012,35 @@ def jaccard_distance( if union_sum == 0.0: return 0.0 return difference_sum / union_sum + + +def jaccard_distance( + mask_1: ty.Union[base.MaskBase, base.BoolVector], + mask_2: ty.Union[base.MaskBase, base.BoolVector], + weights: ty.Optional[base.DoubleVector] = None, +) -> float: + """Computes the Jaccard distance between two sets. + + :group: calculate + + .. versionadded:: 0.3.8 + + Parameters + ---------- + mask_1, mask_2 : ndarray[bool_] or MaskBase + Boolean masks, identifying which elements belong to the + respective sets. + weights : ndarray[float64] + Weights associated with each element. If not passed, + will assume weights are 1. + + Returns + ------- + float + Jaccard distance between sets. + """ + if not isinstance(mask_1, np.ndarray): + mask_1 = mask_1.data + if not isinstance(mask_2, np.ndarray): + mask_2 = mask_2.data + return _jaccard_distance(mask_1, mask_2, weights)