denisecailab / minian

miniscope analysis pipeline with interactive visualizations
GNU General Public License v3.0
91 stars 35 forks source link

How to subset multiple slices of a video? #273

Open cfiel opened 3 months ago

cfiel commented 3 months ago

Hi, could anybody tell me how to subset multiple slices from a video during preprocessing? This part of the code is glossed over in the youtube walkthrough and I've attempted a couple different approaches (like passing a list of slices) but it keeps causing errors later in the pipeline. I admittedly am not the best at coding so I'd appreciate the help!

I'm specifically referring to this part of the code:

varr.sel(frame=slice(0, 799)) # slice object is inclusive on both ends varr.sel({"frame": slice(0, 799)})

I understand that this works for a single slice. But what if I'd like to exclude frames 150-160, and 400-450? How should I modify the code to not throw errors at me?

austinbaggetta commented 3 months ago

Hi cfiel,

You could try something like I do below in a reproducible example:

import numpy as np
import xarray as xr

cell_act = np.random.normal(loc=0, scale=3, size=(1000, 300))
unit_id = np.arange(0, 300)
frame = np.arange(0, 1000)
da = xr.DataArray(cell_act, coords=[frame, unit_id], dims=['frame', 'unit_id'])

subset_da = da.sel(frame=(frame < 150) | (frame > 160) & (frame < 400) | (frame > 450))

Why do you want to exclude multiple chunks of frames? You'll have to be mindful of excluding the corresponding timestamps in the timestamps.csv file. Also, you'll have to make sure the motion correction step isn't impacted due to potentially large changes in which cells are active at the borders of where you are excluding frames. I've never excluded separate chunks of frames, so without knowing what errors you are getting later in the pipeline I am unsure if this will work.