-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
PERF: nancorr_spearman #41857
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
Merged
Merged
PERF: nancorr_spearman #41857
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c9ff800
precommit fixup
mzeitlin11 d60902a
Add benchmark seed for stability
mzeitlin11 8886059
Add back all bench methods
mzeitlin11 db145f8
Merge remote-tracking branch 'upstream/master' into perf/corr
mzeitlin11 ff9519f
Remove random seed
mzeitlin11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -383,8 +383,8 @@ def nancorr_spearman(ndarray[float64_t, ndim=2] mat, Py_ssize_t minp=1) -> ndarr | |
Py_ssize_t i, j, xi, yi, N, K | ||
ndarray[float64_t, ndim=2] result | ||
ndarray[float64_t, ndim=2] ranked_mat | ||
ndarray[float64_t, ndim=1] maskedx | ||
ndarray[float64_t, ndim=1] maskedy | ||
ndarray[float64_t, ndim=1] rankedx, rankedy | ||
float64_t[::1] maskedx, maskedy | ||
ndarray[uint8_t, ndim=2] mask | ||
int64_t nobs = 0 | ||
float64_t vx, vy, sumx, sumxx, sumyy, mean, divisor | ||
|
@@ -399,56 +399,61 @@ def nancorr_spearman(ndarray[float64_t, ndim=2] mat, Py_ssize_t minp=1) -> ndarr | |
|
||
ranked_mat = np.empty((N, K), dtype=np.float64) | ||
|
||
# Note: we index into maskedx, maskedy in loops up to nobs, but using N is safe | ||
# here since N >= nobs and values are stored contiguously | ||
maskedx = np.empty(N, dtype=np.float64) | ||
maskedy = np.empty(N, dtype=np.float64) | ||
for i in range(K): | ||
ranked_mat[:, i] = rank_1d(mat[:, i], labels=labels_n) | ||
|
||
for xi in range(K): | ||
for yi in range(xi + 1): | ||
nobs = 0 | ||
# Keep track of whether we need to recompute ranks | ||
all_ranks = True | ||
for i in range(N): | ||
all_ranks &= not (mask[i, xi] ^ mask[i, yi]) | ||
if mask[i, xi] and mask[i, yi]: | ||
nobs += 1 | ||
|
||
if nobs < minp: | ||
result[xi, yi] = result[yi, xi] = NaN | ||
else: | ||
maskedx = np.empty(nobs, dtype=np.float64) | ||
maskedy = np.empty(nobs, dtype=np.float64) | ||
j = 0 | ||
|
||
with nogil: | ||
for xi in range(K): | ||
for yi in range(xi + 1): | ||
nobs = 0 | ||
# Keep track of whether we need to recompute ranks | ||
all_ranks = True | ||
for i in range(N): | ||
all_ranks &= not (mask[i, xi] ^ mask[i, yi]) | ||
if mask[i, xi] and mask[i, yi]: | ||
maskedx[j] = ranked_mat[i, xi] | ||
maskedy[j] = ranked_mat[i, yi] | ||
j += 1 | ||
|
||
if not all_ranks: | ||
labels_nobs = np.zeros(nobs, dtype=np.int64) | ||
maskedx = rank_1d(maskedx, labels=labels_nobs) | ||
maskedy = rank_1d(maskedy, labels=labels_nobs) | ||
|
||
mean = (nobs + 1) / 2. | ||
|
||
# now the cov numerator | ||
sumx = sumxx = sumyy = 0 | ||
|
||
for i in range(nobs): | ||
vx = maskedx[i] - mean | ||
vy = maskedy[i] - mean | ||
|
||
sumx += vx * vy | ||
sumxx += vx * vx | ||
sumyy += vy * vy | ||
|
||
divisor = sqrt(sumxx * sumyy) | ||
maskedx[nobs] = ranked_mat[i, xi] | ||
maskedy[nobs] = ranked_mat[i, yi] | ||
nobs += 1 | ||
|
||
if divisor != 0: | ||
result[xi, yi] = result[yi, xi] = sumx / divisor | ||
else: | ||
if nobs < minp: | ||
result[xi, yi] = result[yi, xi] = NaN | ||
else: | ||
if not all_ranks: | ||
with gil: | ||
# We need to slice back to nobs because rank_1d will | ||
# require arrays of nobs length | ||
labels_nobs = np.zeros(nobs, dtype=np.int64) | ||
rankedx = rank_1d(np.array(maskedx)[:nobs], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah should really take a memory view (or have a helper function to do it) |
||
labels=labels_nobs) | ||
rankedy = rank_1d(np.array(maskedy)[:nobs], | ||
labels=labels_nobs) | ||
for i in range(nobs): | ||
maskedx[i] = rankedx[i] | ||
maskedy[i] = rankedy[i] | ||
|
||
mean = (nobs + 1) / 2. | ||
|
||
# now the cov numerator | ||
sumx = sumxx = sumyy = 0 | ||
|
||
for i in range(nobs): | ||
vx = maskedx[i] - mean | ||
vy = maskedy[i] - mean | ||
|
||
sumx += vx * vy | ||
sumxx += vx * vx | ||
sumyy += vy * vy | ||
|
||
divisor = sqrt(sumxx * sumyy) | ||
|
||
if divisor != 0: | ||
result[xi, yi] = result[yi, xi] = sumx / divisor | ||
else: | ||
result[xi, yi] = result[yi, xi] = NaN | ||
|
||
return result | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why the gil here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rank_1d
can't be called withnogil
. Perhaps some refactoring could allow calling some nogilrank_1d
helper instead, but that would be a larger change.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i c, ok i think its worthile to make that nogil (but not in this PR), followon preferred.