AllenInstitute / visual_behavior_analysis

Python package for analyzing behavioral data for Brain Observatory: Visual Behavior
Other
21 stars 6 forks source link

Event filtering doesnt take into account sampling rate #698

Closed alexpiet closed 2 years ago

alexpiet commented 3 years ago

I believe right now VBA applies the same filter to the detected events on all cre_lines and on both mesoscope and scientifica data. But by "same" I mean it creates a filter: stats.halfnorm(loc=0, scale=2).pdf(np.arange(20)) where the timesteps are in the size of the ophys_frame_rate. In short: mesoscope data has a longer filter on its detected events than scientifica.

This creates two problems:

As an example of (1) see the attached plot filtering_bug

As an example of (2), see the following calculation in the screenshot. In the unfiltered events, the average event magnitude is the same order of magnitude for the meso and sci sessions. However filtered reduces the scientifica order of magnitude, but not the mesoscope.

Screen Shot 2020-12-17 at 5 52 54 PM

alexpiet commented 3 years ago

copying from slack:

Iryna and I have a proposed change to how the filtered_events are computed. We believe the current VBA implementation suffers from two problems. One of these problems I think everyone will agree on. The other could potential be a point of debate depending on your interpretation of the events, and (potentially) how Peter L computed the events. Attached is a figure demonstrating the problems and a proposed solution, and the code that generates the figure. In each row, the faint lines show the raw events, and the solid lines show the filtered events. The three rows show three different filtering solutions. Problem 1. The current VBA filtering does not take into account the ophys sampling rate, and thus filters mesoscope events over 3x longer of a time period as scientifica. In the top row of the plot we see the filtered mesoscope trace (magenta) extends longer than the scientifica trace (cyan). In the middle row, the first attempted fix does not solve the problem. In the bottom row, adjusting the scale parameter of scipy.stats.halfnorm() makes the scientifica and mesoscope filtered traces have the same temporal basis. Problem solved! Problem 2. How do we interpret the magnitude of the events? Iryna and I reasoned that the events and filtered_events should integrate to the same value. By that we mean sum(trace*dt) Because the mesoscope dt is larger, a mesoscope event of the same magnitude is a larger integrated value than a scientifica event of the same magnitude. Therefore the filtered_events for mesoscope appear larger than the scientifica filtered_events. Our implementation preserves the integrated value of the events and filtered events (the sci 1.0, meso 1.0 in the title), while the current VBA does not (0.83 in the title). I think we might want to have a discussion about how to interpret events, because I could be convinced that in some cases we want to adopt a convention where an event magnitude should be the same for mesoscope and scientifica. We might want to discuss with Peter L about how to think about these magnitudes. Note that problem 1 and 2 are completely separable, so we can resolve problem 1 no matter how we interpret problem 2 filter_issue_demonstration