Repository navigation
Add flag to phase cross correlation to disambiguate periodic shifts - #6617
Conversation
|
Oh, another question, I didn't know whether |
|
I'm fine with setting the default to True. I do wonder if Pearson is the best way to go about it; no property of the FFT (already computed?) to exploit? Review from a comfortable distance, so feel free to ignore ☺ |
|
No I don't think that the FFT values can help here — it needs to happen in real space. But there might be a faster function to compute cross-correlation in that space. |
|
Indeed |
In [2]: x = np.random.random(int(1e6))
In [3]: y = np.random.random(int(1e6))
In [4]: %timeit np.corrcoef(x, y)
28.4 ms ± 1.86 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [5]: %timeit np.cov(x, y)
27.2 ms ± 1.75 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [6]: np.corrcoef(x, y)
Out[6]:
array([[ 1. , -0.00121138],
[-0.00121138, 1. ]])
In [7]: from scipy import stats
In [8]: stats.pearsonr(x, y)
Out[8]: (-0.0012113788952588546, 0.22575063366199608)
In [9]: %timeit stats.pearsonr(x, y)
51.7 ms ± 3.3 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
…hase-xcorr-disambiguate
|
This looks good to me, thanks Juan. The only remaining question is what the memory usage is doing all the warps/correlations simultaneously (as now) vs in the loop. |
|
Sorry, can you elaborate on your question? Do you mean is it worth not doing |
|
Ah, right, so you only make shifts for the few candidate positions, yes that should be fine then. 👍 |
| one with the highest cross-correlation. | ||
|
|
||
| The strategy we use is to perform the shift on the moving image *using the | ||
| 'grid-wrap' mode* in `scipy.ndimage`. The original image borders then |
There was a problem hiding this comment.
Wait, what is the original image? Can we stick with 'reference image' and 'moving image,' please?
| disambiguate : bool | ||
| The shifts returned by this function are only accurate *modulo* the | ||
| image shape, due to the periodic nature of the Fourier transform. If | ||
| this is set to ``True``, the *real* space cross-correlation is |
There was a problem hiding this comment.
| this is set to ``True``, the *real* space cross-correlation is | |
| this parameter is set to ``True``, the *real* space cross-correlation is |
(Maybe we've lost track of what we're talking about because of the previous sentence--which is useful and helpful and welcome.)
| The shifts returned by this function are only accurate *modulo* the | ||
| image shape, due to the periodic nature of the Fourier transform. If | ||
| this is set to ``True``, the *real* space cross-correlation is | ||
| computed for each possible shift, and the shift with the highest |
There was a problem hiding this comment.
I prefer 'shift' in the singular form, but so far you've used the plural, so we should be consistent one way or the other. I prefer 'shift' so that, in this context (in these docstrings), by 'shift' we mean the tuple, say, (-150, 200), and by 'shifts' we mean the set of:
(-150, 200)
(-150, -1800)
(1850, 200)
(1850, -1800)
Otherwise, 'shifts' mean either (-150, 200) or the set of all possible shifts.
| used if any of ``reference_mask`` or ``moving_mask`` is not | ||
| None. | ||
| disambiguate : bool | ||
| The shifts returned by this function are only accurate *modulo* the |
There was a problem hiding this comment.
| The shifts returned by this function are only accurate *modulo* the | |
| The shift returned by this function is only accurate *modulo* the |
| moving_image : numpy array | ||
| The moving image: applying the shift to this image overlays it on the | ||
| reference image. Must be the same shape as the reference image. | ||
| shifts : tuple of float |
There was a problem hiding this comment.
My bad, I just deleted an in-line comment of mine here: It was my misunderstanding. Here, it's the plural indeed. Then, I suggest:
| shifts : tuple of float | |
| shifts : tuple of (tuple of) float |
even if, in 1D, it's only a tuple of floats, but I'd like to distinguish it from real_shift (line 145 below).
| Returns | ||
| ------- | ||
| real_shift : tuple of float | ||
| The shifts disambiguated in real space. |
There was a problem hiding this comment.
| The shifts disambiguated in real space. | |
| The shift disambiguated in real space. |
| The shifts returned by this function are only accurate *modulo* the | ||
| image shape, due to the periodic nature of the Fourier transform. If | ||
| this is set to ``True``, the *real* space cross-correlation is | ||
| computed for each possible shift, and the shift with the highest |
There was a problem hiding this comment.
| computed for each possible shift, and the shift with the highest | |
| computed for each possible shift, and the shift with highest |
| image shape, due to the periodic nature of the Fourier transform. If | ||
| this is set to ``True``, the *real* space cross-correlation is | ||
| computed for each possible shift, and the shift with the highest | ||
| cross-correlation of the overlapping area is returned. |
There was a problem hiding this comment.
| cross-correlation of the overlapping area is returned. | |
| cross-correlation with the overlapping area is returned. |
|
This pull request has been mentioned on Image.sc Forum. There might be relevant details there: |
|
This is super useful 🎉 |
Closes #6604
This PR adds a
disambiguate=Falsetophase_cross_correlationthat uses real-space cross-correlation to disambiguate between the possible (periodic) shifts implied by the shift returned byphase_cross_correlation.Perhaps we can default the flag to
True, but it will come at a performance cost. With theFalsedefault, behaviour is unchanged for existing users.