Skip to content

Numpy and Numba do not give exactly the same output in unit test environment #2990

Closed

Description

@JoeZiminski

There was a test in test_correlogram.py that indicates the output of numpy and numba are slightly different when building correlograms.

def _test_correlograms(sorting, window_ms, bin_ms, methods):
    for method in methods:
        correlograms, bins = compute_correlograms_on_sorting(sorting, window_ms=window_ms, bin_ms=bin_ms, method=method)
        if method == "numpy":
            ref_correlograms = correlograms
            ref_bins = bins
        else:
            # ~ import matplotlib.pyplot as plt
            # ~ for i in range(ref_correlograms.shape[1]):
            # ~ for j in range(ref_correlograms.shape[1]):
            # ~ fig, ax = plt.subplots()
            # ~ ax.plot(bins[:-1], ref_correlograms[i, j, :], color='green', label='numpy')
            # ~ ax.plot(bins[:-1], correlograms[i, j, :], color='red', label=method)
            # ~ ax.legend()
            # ~ ax.set_title(f'{i} {j}')
            # ~ plt.show()

            # numba and numyp do not have exactly the same output
            # assert np.all(correlograms == ref_correlograms), f"Failed with method={method}"

            assert np.allclose(bins, ref_bins, atol=1e-10), f"Failed with method={method}"

The commented section was removed in #2984 but should be looked into, I'm happy to do this going to work a bit on postprocessing tests soon.

Activity

  1. h-mayorquin commented on Jun 6, 2024

    @h-mayorquin
    Contributor

    Does this assert np.all(correlograms == ref_correlograms) still fail if you use np.allclose like in the last line?

  2. JoeZiminski commented on Jun 7, 2024

    @JoeZiminski
    ContributorAuthor

    Unfortunately not, the values are integer (I guess performing cross-correlations on binary arrays where 1 indicates a spike). For example I get:

    ref_correlogram:

           [[0, 1, 1, 0, 1, 1, 0, 3, 1, 2, 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,
             1, 0, 0, 1, 0, 0, 4, 0, 1],
            [0, 3, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 3, 0, 1, 2, 0, 0,
             2, 0, 1, 0, 1, 2, 2, 0, 1],
            [0, 1, 0, 0, 2, 1, 2, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 3, 1, 0,
             0, 1, 0, 0, 0, 2, 1, 0, 1],
            [1, 1, 0, 0, 2, 0, 0, 0, 2, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0,
             2, 0, 0, 0, 2, 0, 0, 1, 1],
            [0, 0, 0, 0, 1, 0, 0, 2, 4, 1, 1, 1, 2, 1, 0, 0, 3, 0, 2, 0, 2,
             0, 1, 0, 1, 0, 1, 0, 1, 0]],
    

    vs correlogram:

           [[0, 1, 1, 0, 1, 1, 0, 3, 2, 1, 2, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,
             1, 0, 0, 1, 0, 0, 4, 0, 1],
            [0, 3, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 3, 0, 1, 2, 0, 1,
             1, 0, 1, 0, 1, 2, 2, 0, 1],
            [0, 1, 0, 0, 3, 0, 2, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 3, 1, 0,
             0, 1, 0, 0, 0, 2, 1, 0, 1],
            [1, 1, 0, 0, 2, 0, 0, 0, 2, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0,
             2, 0, 0, 0, 2, 0, 0, 1, 1],
            [0, 0, 0, 0, 1, 0, 0, 2, 4, 1, 1, 1, 2, 1, 0, 0, 3, 0, 2, 0, 2,
             0, 1, 0, 1, 0, 1, 0, 1, 0]],
    

    they are very similar, but there are a few discrepancies. The implementations are a little different and are doing more than I anticipated, my from niave perspective I thought they would be cross-correlations of binarized data (1 x num samples per unit spiketrain) where 1 is a spike sample and 0 is a non-spike sample. I guess this would be extremely memory hungry as compared to these much more efficient implementations. @samuelgarcia @DradeAW should be able to comment further. Additionally, would it be possible to extend the docstrings in the correlograms.py, with an overview of what the algorithms are doing / why, and Parameters section that indicates the type / shape of the variables? It would be great as would make working with this modules much easier / faster.

  3. samuelgarcia commented on Jun 7, 2024

    @samuelgarcia
    Member

    The old implementation in numpy a modifiyed version from Cyril Rossant. This one is not totally accurate.
    The one implementated in numba done by Aurelien is more accurate and faster but need numba this is why we kept the old one.
    When refactoring theses 2 functions a while ago I really wanted then to give the exact same result but finally gave up because it is impossible The reason is corners cases and rounding around the central bin. I do not remember exactly the details.
    This is why I think I commented out this test.

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