Skip to content

Commit

Permalink
Handling all zero pairwise distances case
Browse files Browse the repository at this point in the history
Signed-off-by: Nicholas Parente <[email protected]>
  • Loading branch information
nparent1 committed Sep 6, 2024
1 parent 8885b32 commit c7229ee
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 3 additions & 2 deletions dodiscover/toporder/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ def kernel_width(X: NDArray):
Matrix of the data.
"""
X_diff = np.expand_dims(X, axis=1) - X # Gram matrix of the data
D = np.linalg.norm(X_diff, axis=2)
s = np.median(D.flatten()[D.flatten() > 0]) # removes zero elements
D = np.linalg.norm(X_diff, axis=2).flatten()
D_nonzeros = D[D > 0] # Remove zeros
s = np.median(D_nonzeros) if np.any(D_nonzeros) else 1
return s


Expand Down
4 changes: 4 additions & 0 deletions tests/unit_tests/toporder/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ def test_kernel_width_when_zero_median_pairwise_distances():
arr = np.zeros((100, 1), dtype=np.int64)
arr[1] = 1
assert kernel_width(arr) == 1

def test_kernel_width_when_constant_pairwise_distances():
arr = np.ones((100, 1), dtype=np.int64)
assert kernel_width(arr) == 1

0 comments on commit c7229ee

Please sign in to comment.