Skip to content

How to limit cross correlation window width in Numpy? #5954

Closed

Description

@HoniSanders

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.

Other issues related to correlate functions include ENH: Fold fftconvolve into convolve/correlate functions as a parameter #2651, Use FFT in np.correlate/convolve? (Trac #1260) #1858, and normalized cross-correlation (Trac #1714) #2310.

Activity

  1. seberg commented on Jun 9, 2015

    @seberg
    Member

    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.

  2. HoniSanders commented on Jun 9, 2015

    @HoniSanders
    Author

    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.

  3. seberg commented on Jun 9, 2015

    @seberg
    Member

    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.

  4. seberg commented on Jun 9, 2015

    @seberg
    Member

    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?

  5. HoniSanders commented on Jun 9, 2015

    @HoniSanders
    Author

    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 Jun 9, 2015, at 3:47 AM, seberg [email protected] wrote:

    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).

  6. 37 remaining items

  7. Eridum commented on Oct 24, 2015

    @Eridum

    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 ?

  8. njsmith commented on Oct 25, 2015

    @njsmith
    Member

    The way to move forward on this would be:

    • 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 :-).

    On Sat, Oct 24, 2015 at 1:58 PM, Eridum [email protected] wrote:

    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).

    Nathaniel J. Smith -- http://vorpus.org

  9. HoniSanders commented on Oct 26, 2015

    @HoniSanders
    Author

    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.

  10. njsmith commented on Oct 26, 2015

    @njsmith
    Member

    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).

    Nathaniel J. Smith -- http://vorpus.org

  11. HoniSanders commented on Nov 18, 2015

    @HoniSanders
    Author

    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.

  12. rgommers commented on May 28, 2016

    @rgommers
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions