Skip to content

Commit

Permalink
fix clustering ranks when all dists are equal
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremymanning committed Nov 6, 2023
1 parent 194ebd1 commit 202296b
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions quail/analysis/clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ def _get_weight_exact(egg, feature, distdict, permute, n_perms):
dists = distmat[pres.index(c),:]
di = dists[pres.index(n)]
dists_filt = np.array([dist for idx, dist in enumerate(dists) if idx not in past_idxs])
ranks.append(np.mean(np.where(np.sort(dists_filt)[::-1] == di)[0]+1) / len(dists_filt))

if len(np.unique(dists_filt)) == 1:
ranks.append(0.5)
else:
ranks.append(np.mean(np.where(np.sort(dists_filt)[::-1] == di)[0]+1) / len(dists_filt))
past_idxs.append(pres.index(c))
past_words.append(c)
return np.nanmean(ranks)
Expand All @@ -97,7 +101,10 @@ def _get_weight_best(egg, feature, distdict, permute, n_perms, distance):
dists = distmat[cdx, :]
di = dists[ndx]
dists_filt = np.array([dist for idx, dist in enumerate(dists)])
ranks.append(np.mean(np.where(np.sort(dists_filt)[::-1] == di)[0]+1) / len(dists_filt))
if len(np.unique(dists_filt)) == 1:
ranks.append(0.5)
else:
ranks.append(np.mean(np.where(np.sort(dists_filt)[::-1] == di)[0] + 1) / len(dists_filt))
return np.nanmean(ranks)

def _get_weight_smooth(egg, feature, distdict, permute, n_perms, distance):
Expand All @@ -120,7 +127,10 @@ def _get_weight_smooth(egg, feature, distdict, permute, n_perms, distance):
dists = distmat[cdx, :]
di = dists[ndx]
dists_filt = np.array([dist for idx, dist in enumerate(dists)])
ranks.append(np.mean(np.where(np.sort(dists_filt)[::-1] == di)[0]+1) / len(dists_filt))
if len(np.unique(dists_filt)) == 1:
ranks.append(0.5)
else:
ranks.append(np.mean(np.where(np.sort(dists_filt)[::-1] == di)[0] + 1) / len(dists_filt))
return np.nanmean(ranks)

def get_distmat(egg, feature, distdict):
Expand Down

0 comments on commit 202296b

Please sign in to comment.