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
Note that the length of the ophyst_timestamps attribute matches the length of the filtered_events attribute, so it would seem that the 'timestamps' attribute of the events dataframe is the outlier.
One more clue: the timestamps align at the beginning of the array, but not at the end. That'd seem to imply that the extra two timestamps in dataset.events.iloc[0]['timestamps'] are at the end:
@dougollerenshaw Yes. For now, don't use the timestamps in the events df. Use dataset.ophys_timestamps. Those are the ground truth from SDK. I am not sure what could be causing the timestamps in the event detection output to be off.
Hey guys,
Which time stamps are you guys talking about?
Note that once you load the npz file, there are different sets of time_stamps in there:
there is npz[‘ts’], which should be identical to what you get from the SDK because it’s just the result of
dataset = loading.get_ophys_dataset(eval(exp_id), include_invalid_rois=False)
ts = dataset.timestamps.ophys_frames.values[0]
Then inside the event_dict object there is the key ‘ts’, which contains the time stamps for the detected events, for each cell id: npz[‘event_dict’][cid][‘ts’]
Those time stamps are computed by upsampling the original time stamps:
ts30Hz = resample_poly(ts, uf, 1)
figuring out where there are events in the upsampled events trace:
event_idx = np.where(event30Hz>0)[0] #upsampled
and finally indexing with that into the upsampled time stamp trace:
event_ts = ts30Hz[event_idx]
What appears to be wrong?
Best,
Thanks @ledochowitsch. Sorry for being unclear about the underlying issue. I was initially struggling to understand it myself so this issue got a little muddied.
But the fundamental issue is this: the timestamps associated with the events are two values longer than the events arrays themselves.
For the same oeid I initially referenced in this issue, here's what happens when I go back to the cached events file:
import numpy as np
events_file = '//allen/programs/braintv/workgroups/nc-ophys/visual_behavior/event_detection/953443028.npz'
f = np.load(events_file, allow_pickle=True)
Get the length of the timestamps array: len(f['ts'])
from visual_behavior.data_access import loading
dataset = loading.get_ophys_dataset(oeid, include_invalid_rois=False)
len(dataset.timestamps.ophys_frames['timestamps'])
140014
So it'd seem that the dataset.timestamps.ophys_frames['timestamps'] attribute is the source of the confusion here. @matchings, do you know where that attribute is coming from and why it would be two elements longer than ophys_timestamps?
Ah, I see... That’s frustrating:/.
The good news is that the contents of npz[‘events’] will be unaffected by this issue. However, the events time stamps will be off by the same two samples…
dataset.timestamps.ophys_frames['timestamps'] are computed directly from the sync file, and these are what is used to create dataset.ophys_timestamps for mesoscope experiments because the SDK does not yet do the proper time resampling for mesoscope (or at least it didnt in the version we are using). For scientifica, dataset.ophys_timestamps is pulled directly from the SDK. If the SDK is doing some truncation of frames, it could lead to a discrepancy between dataset.ophys_timestamps and dataset.timestamps.ophys_frames['timestamps']. But that should be specific to Scientifica, because mesoscope uses the same thing for both. I hope that makes sense...
Thanks @matchings. It looks like you're correct that this is specific to scientifica sessions. Here's an example from mesoscope showing that both the SDK ophys_timestamps and the VBA dataset ophys_frames['timestamps'] vectors are the same length:
And here's a different 2P3 (scientifica) session with the same off-by-two error as above:
So does this mean that the problem is with the SDK? If so, we should submit an SDK issue to solve it. These discrepancies will undoubtedly confuse other users in the future.
im guessing that the SDK truncates the timestamps to match the ophys traces, which is probably a desired behavior, otherwise we would have mismatches all over the place. i believe the scientificas are known to give out a few extra TTL pulses at the end of the session (or at least MPE says its at the end, its nice that you just validated that here), which we want to remove so that everything is aligned. it surprises me that you are always seeing an off by exactly 2 issue though, because i thought the number of those extra pulses at the end was variable.
I had in fact double-checked that the tine stamps were the same length as the dff traces when I prototyped the code - on MesoScope data. Unfortunately, I have not re-checked when I generalized it to also work for Scientifica. Who would have thunk?
Activity
dougollerenshaw commentedon Dec 22, 2020
I just checked another session and it's also off by two:
len(dataset.events.iloc[0]['filtered_events'])len(dataset.ophys_timestamps)dougollerenshaw commentedon Dec 22, 2020
One more clue: the timestamps align at the beginning of the array, but not at the end. That'd seem to imply that the extra two timestamps in dataset.events.iloc[0]['timestamps'] are at the end:

dougollerenshaw commentedon Dec 22, 2020
@matchings are those timestamps inherited directly from the array that @ledochowitsch is saving to disk?
matchings commentedon Dec 22, 2020
@dougollerenshaw Yes. For now, don't use the timestamps in the events df. Use dataset.ophys_timestamps. Those are the ground truth from SDK. I am not sure what could be causing the timestamps in the event detection output to be off.
dougollerenshaw commentedon Dec 22, 2020
More evidence that the extra two timestamps at the end are extraneous:
Plotting with last two timestamps trimmed off:
gives us two aligned traces:
But trimming off the first two gives us misaligned traces:
ledochowitsch commentedon Dec 22, 2020
matchings commentedon Dec 22, 2020
i believe the timestamps Doug is talking about are the npz['ts'] ones, not the ones in the event_dict.
ledochowitsch commentedon Dec 22, 2020
dougollerenshaw commentedon Dec 22, 2020
Thanks @ledochowitsch. Sorry for being unclear about the underlying issue. I was initially struggling to understand it myself so this issue got a little muddied.
But the fundamental issue is this: the timestamps associated with the events are two values longer than the events arrays themselves.
For the same oeid I initially referenced in this issue, here's what happens when I go back to the cached events file:
Get the length of the timestamps array:
len(f['ts'])Get the length of an events trace:
Above you said:
When I go back to the directly to the SDK, I get this:
But you went on to say:
Checking that myself, I see:
So it'd seem that the
dataset.timestamps.ophys_frames['timestamps']attribute is the source of the confusion here. @matchings, do you know where that attribute is coming from and why it would be two elements longer thanophys_timestamps?ledochowitsch commentedon Dec 23, 2020
matchings commentedon Dec 23, 2020
dataset.timestamps.ophys_frames['timestamps'] are computed directly from the sync file, and these are what is used to create dataset.ophys_timestamps for mesoscope experiments because the SDK does not yet do the proper time resampling for mesoscope (or at least it didnt in the version we are using). For scientifica, dataset.ophys_timestamps is pulled directly from the SDK. If the SDK is doing some truncation of frames, it could lead to a discrepancy between dataset.ophys_timestamps and dataset.timestamps.ophys_frames['timestamps']. But that should be specific to Scientifica, because mesoscope uses the same thing for both. I hope that makes sense...
dougollerenshaw commentedon Dec 23, 2020
Thanks @matchings. It looks like you're correct that this is specific to scientifica sessions. Here's an example from mesoscope showing that both the SDK ophys_timestamps and the VBA dataset ophys_frames['timestamps'] vectors are the same length:
And here's a different 2P3 (scientifica) session with the same off-by-two error as above:
So does this mean that the problem is with the SDK? If so, we should submit an SDK issue to solve it. These discrepancies will undoubtedly confuse other users in the future.
matchings commentedon Dec 23, 2020
im guessing that the SDK truncates the timestamps to match the ophys traces, which is probably a desired behavior, otherwise we would have mismatches all over the place. i believe the scientificas are known to give out a few extra TTL pulses at the end of the session (or at least MPE says its at the end, its nice that you just validated that here), which we want to remove so that everything is aligned. it surprises me that you are always seeing an off by exactly 2 issue though, because i thought the number of those extra pulses at the end was variable.
alexpiet commentedon Dec 23, 2020
I'm always paranoid about convolutions. Should the mode be "valid" instead of "full" (default)?
visual_behavior_analysis/visual_behavior/ophys/response_analysis/response_processing.py
Line 422 in 0b07d46
https://numpy.org/doc/stable/reference/generated/numpy.convolve.html
ledochowitsch commentedon Dec 23, 2020