You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to limit cross correlation window width in Numpy? #5954
I am learning numpy/scipy, coming from a MATLAB background. The xcorr function in Matlab has an optional argument "maxlag" that limits the lag range from –maxlag to maxlag. This is very useful if you are looking at the cross-correlation between two very long time series but are only interested in the correlation within a certain time range. The performance increases are enormous considering that cross-correlation is incredibly expensive to compute.
What is troubling me is that numpy.correlate does not have a maxlag feature. This means that even if I only want to see correlations between two time series with lags between -100 and +100 ms, for example, it will still calculate the correlation for every lag between -20000 and +20000 ms (which is the length of the time series). This (theoretically) gives a 200x performance hit! Is it possible that I could contribute this feature?
I have introduced this question as a scipy issue and on the scipy-dev list. It seems the best place to start is with numpy.correlate, so that is what I am requesting. I have done a simple implementation which gives 50x speedup under my conditions.
This is my first experience with contributing to open-source software, so any pointers are appreciated.
Out of curiosity, how do matlab (maybe R?) handle rescaling effects due to different values when the max lag is not much smaller then the amount of data?
I think I often use np.correlate(a, b[-max_lag:max_lag], mode='valid'), to use always the same amount of data (and as much of the same value as possible). Maybe I am just crazy here, but wondering.
Not sure I opened an issue, but I think some function hidden in matplotlib had this kind of thing, and when maxlag got larger, I was annoyed by rescaling effects IIRC.
Your workaround throws out huge amounts of data. You still want to know how b[-maxlag-1] correlates with entries in a that are within maxlag entries from that.
matplotlib.pyplot.xcorr has a maxlag argument, but it is just a wrapper for numpy.correlate, so it doesn't get any performance benefits because it ends up calculating the entire cross-correlation and then throwing part away before returning to the user.
Maybe that was a bit silly, since I was more concerned about getting something like xcorr right, as opposed to adding a keyword to current correlate.
I think the idea is not bad, maybe the correct question to answer is:
We have three modes, 'valid', 'same', 'full'. What are the exact ranges for all of these. Once you know that, adding such a keyword to the C code should be simple. This might be trivial, but on first sight, I am not sure it is.
On the one hand, maxlag you could say: Well if "valid" mode gives something smaller, that is fine. But then we want maxlag to be -maxlag, ...,0, ..., maxlag. Which is unclear for different sized arrays, making the keyword argument useless in "valid" mode?
yes, i plan to do that. I’m not 100% sure of how to deal with vectors of different sizes. Should I center the second vector with respect to the first or right align them?
On the one hand, maxlag you could say: Well if "valid" mode gives something smaller, that is fine. But then we want maxlag to be -maxlag, ...,0, ..., maxlag. Which is unclear for different sized arrays, making the keyword argument useless in "valid" mode?
—
Reply to this email directly or view it on GitHub #5954 (comment).
Hi bringingheavendown, the correlate function modification you made is exactly what I was looking for. I think the way you handle the minlag, maxlag is perfect. Any hope of seeing it in the offical numpy ?
For questions about what the user-facing API should look like, send an
email to the numpy-discussion list outlining what you have and what the
options you see are. (At, say, the level of detail you'd put in the
function documentation.)
For questions about internal implementation stuff, submit whatever you
have as a pull request for people to look at; that's our standard procedure
for doing code review. It doesn't have to be perfect to post a PR :-).
Hi bringingheavendown, the correlate function modification you made is
exactly what I was looking for. I think the way you handle the minlag,
maxlag is perfect. Any hope of seeing it in the offical numpy ?
—
Reply to this email directly or view it on GitHub #5954 (comment).
There already is a pull request: #5978. It passed all tests on all machines except for one, but I couldn't get enough help debugging it. What can I do about that?
There also already was discussion about the user-facing API on the numpy-discussion list back in June. At this point, I feel that the code is complete and just needs to be incorporated. If more discussion is desired, we can do that. In any case, I only dropped this because it didn't seem like it was going anywhere. Let me know what the process is at this point, and I would be happy to do it.
Sorry, I missed the pull request, was just looking at this bug. It sounds
like things are moving forward in that PR now though.
On Mon, Oct 26, 2015 at 8:05 AM, bringingheavendown < [email protected]> wrote:
There already is a pull request: #5978 #5978. It passed all tests on all
machines except for one, but I couldn't get enough help debugging it. What
can I do about that? What are the next steps you are envisioning?
There also already was discussion about the user-facing API on the
numpy-discussion list back in June. At this point, I feel that the code is
complete and just needs to be incorporated. If more discussion is desired,
we can do that. In any case, I only dropped this because it didn't seem
like it was going anywhere.
—
Reply to this email directly or view it on GitHub #5954 (comment).
The pull request is just waiting for approval now. I can squash commits and give appropriate commit messages once I know everything is ready to go forward.
Content in this issue overlaps with the actual PR (gh-5978), which has more up-to-date comments. So I'll close this issue to limit the amount of places the same maxlag enhancement is discussed.
Activity
seberg commentedon Jun 9, 2015
Out of curiosity, how do matlab (maybe R?) handle rescaling effects due to different values when the max lag is not much smaller then the amount of data?
I think I often use
np.correlate(a, b[-max_lag:max_lag], mode='valid'), to use always the same amount of data (and as much of the same value as possible). Maybe I am just crazy here, but wondering.Not sure I opened an issue, but I think some function hidden in matplotlib had this kind of thing, and when maxlag got larger, I was annoyed by rescaling effects IIRC.
HoniSanders commentedon Jun 9, 2015
Not sure what you mean by rescaling effects.
Your workaround throws out huge amounts of data. You still want to know how b[-maxlag-1] correlates with entries in a that are within maxlag entries from that.
matplotlib.pyplot.xcorr has a maxlag argument, but it is just a wrapper for numpy.correlate, so it doesn't get any performance benefits because it ends up calculating the entire cross-correlation and then throwing part away before returning to the user.
seberg commentedon Jun 9, 2015
Maybe that was a bit silly, since I was more concerned about getting something like
xcorrright, as opposed to adding a keyword to current correlate.I think the idea is not bad, maybe the correct question to answer is:
We have three modes, 'valid', 'same', 'full'. What are the exact ranges for all of these. Once you know that, adding such a keyword to the C code should be simple. This might be trivial, but on first sight, I am not sure it is.
seberg commentedon Jun 9, 2015
On the one hand, maxlag you could say: Well if "valid" mode gives something smaller, that is fine. But then we want maxlag to be
-maxlag, ...,0, ..., maxlag. Which is unclear for different sized arrays, making the keyword argument useless in "valid" mode?HoniSanders commentedon Jun 9, 2015
yes, i plan to do that. I’m not 100% sure of how to deal with vectors of different sizes. Should I center the second vector with respect to the first or right align them?
37 remaining items
Eridum commentedon Oct 24, 2015
Hi bringingheavendown, the correlate function modification you made is exactly what I was looking for. I think the way you handle the minlag, maxlag is perfect. Any hope of seeing it in the offical numpy ?
njsmith commentedon Oct 25, 2015
The way to move forward on this would be:
email to the numpy-discussion list outlining what you have and what the
options you see are. (At, say, the level of detail you'd put in the
function documentation.)
have as a pull request for people to look at; that's our standard procedure
for doing code review. It doesn't have to be perfect to post a PR :-).
On Sat, Oct 24, 2015 at 1:58 PM, Eridum [email protected] wrote:
Nathaniel J. Smith -- http://vorpus.org
HoniSanders commentedon Oct 26, 2015
There already is a pull request: #5978. It passed all tests on all machines except for one, but I couldn't get enough help debugging it. What can I do about that?
There also already was discussion about the user-facing API on the numpy-discussion list back in June. At this point, I feel that the code is complete and just needs to be incorporated. If more discussion is desired, we can do that. In any case, I only dropped this because it didn't seem like it was going anywhere. Let me know what the process is at this point, and I would be happy to do it.
njsmith commentedon Oct 26, 2015
Sorry, I missed the pull request, was just looking at this bug. It sounds
like things are moving forward in that PR now though.
On Mon, Oct 26, 2015 at 8:05 AM, bringingheavendown <
[email protected]> wrote:
Nathaniel J. Smith -- http://vorpus.org
HoniSanders commentedon Nov 18, 2015
The pull request is just waiting for approval now. I can squash commits and give appropriate commit messages once I know everything is ready to go forward.
rgommers commentedon May 28, 2016
Content in this issue overlaps with the actual PR (gh-5978), which has more up-to-date comments. So I'll close this issue to limit the amount of places the same
maxlagenhancement is discussed.ENH: add maxlag to numpy.correlate (see numpygh-5954)
MAINT: Improvements of numpy.correlate() (See numpy#5954)
BUG: fixed default arguments in correlate pipeline (See numpy#5954)
ENH: add maxlag to np.correlate & convolve (numpygh-5954, numpygh-4940,