MouseLand / suite2p

cell detection in calcium imaging recordings
http://www.suite2p.org
GNU General Public License v3.0
348 stars 240 forks source link

BUG: ROIs manually curated in combined view not saved in individual plane folders #1051

Closed mariacozan closed 11 months ago

mariacozan commented 11 months ago

The combined view is a great feature as it offers a nice overview over all the planes and doesn't require loading individual planes one by one. Because of this, I got into the habit of checking the ROIs labelled cells and not cells in the combined view. However, I recently discovered that the curation of the cells does not get saved into the individual plane outputs. See here in the combined view: image and then in the plane view: image Note how the cell circled in red is present in the combined view cell section whereas in the plane view it is still in the not cell section.

Version information:

v0.10.4.dev9+gf93c1ca.d20220615

Context for the issue:

Because of this issue I will need to redo all the manual curation I did previously as I actually need the data from each plane individually. I would greatly appreciate it if you could fix this issue as it would save me a lot of time. Thanks!

landoskape commented 11 months ago

This might not be something they want to completely "fix" (e.g. maybe it's not a bug in their opinion), but if you want to overwrite the individual plane folders with the results from combined, it wouldn't be too hard.

import os
import numpy as np

num_planes = 10000 # whatever this is for you
suite2p_dir = '/path/to/suite2p/folder' # whatever this is for you
combined_iscell = np.load(os.path.join(suite2p_dir, 'combined', 'iscell.npy'))
num_roi_in_combined = combined_iscell.shape[0]

plane_iscell = [np.load(os.path.join(suite2p_dir, f"plane{ii}", 'iscell.npy')) for ii in range(num_planes)]
num_roi_in_planes = [pic.shape[0] for pic in plane_iscell]

assert sum(num_roi_in_planes)==num_roi_in_combined, "if this fails, then combined has a different number of ROIs than the sum of the ROIs in each folder"

idx_start_stop = np.cumsum(np.array([0, *np.array(num_roi_in_planes)]))
idx_start = idx_start_stop[:-1]
idx_stop = idx_start_stop[1:]

new_plane_iscell = [combined_iscell[istart:istop] for istart, istop in zip(idx_start , idx_stop)]
for ii, np_iscell in enumerate(new_plane_iscell):
   np.save(os.path.join(suite2p_dir, f"plane{ii}", 'iscell.npy'), np_iscell)
mariacozan commented 11 months ago

@landoskape Great, I'll try it out! Thank you very much for this :)