MouseLand / suite2p

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

FEATURE: generate ROI.zip compatible with imageJ from segmented cells #1036

Open brian1000 opened 10 months ago

brian1000 commented 10 months ago

Feature you'd like to see:

I understand that suite2p, unlike cellpose, does not include the functionality to generate rois compatible with imageJ. I wanted to know if anyone has scripts that can do this based on suite2p's output. Either a segmentation mask or an ROI is fine. I think the issue is that suite2p classifies at the pixel level so it doesn't generate masks natively. In that case, I imagine the simplest solution is to generate a convexhull for each 2d point cloud belonging to a particular cell and save that as an imageJ compatible roi. If anyone's done this in the past and has a script to share I'd greatly appreciate it!

Attempted alternative approaches:

Probably the simplest method based on suite2p's output would be to use the x,y coordinates of each cell and the mean_radius to generate circles where each roi is. My main issue is that I don't know how to export these rois into an imageJ compatible format.

Additional Context

No response

brian1000 commented 10 months ago

I've found a workaround for this and will post sample code tomorrow in case anyone else needs this in the feature.

Eddymorphling commented 10 months ago

Hi. I am interested in doing something along these lines that outputs all Cellpose detected ROIs that I can later open in ImageJ or other tools. Were you able to find a way to do this?

brian1000 commented 10 months ago

@Eddymorphling yes mb I got lazy.

Here's a working example using the roifile package and scipy's convexhull function for python.

from pathlib import Path 
import matplotlib.pyplot as plt
import numpy as np
#import suite2p

#import suite2p post-processing files
save_path = Path(r"C:\Users\emricklab\Desktop\suite2p\test_live2\suite2p\plane0")
iscell = np.load(save_path.joinpath('iscell.npy'), allow_pickle=True)[:, 0].astype(bool)
stats = np.load(save_path.joinpath('stat.npy'), allow_pickle=True)
ops = np.load(save_path.joinpath('ops.npy'), allow_pickle=True).item()
f_cells = np.load(save_path.joinpath('F.npy'), allow_pickle=True)
f_neuropils = np.load(save_path.joinpath('Fneu.npy'), allow_pickle=True)
spks = np.load(save_path.joinpath('spks.npy'), allow_pickle=True)

import roifile
from roifile import ImagejRoi, ROI_TYPE, roiwrite
from scipy.spatial import ConvexHull

true_cell = np.where(iscell)[0]

roi_list = []
for i in true_cell:
    ypix = stats[i]['ypix']
    xpix = stats[i]['xpix']
    coords = list(zip(xpix,ypix))
    points = np.array(coords)

    hull = ConvexHull(coords)
    boundary_coords = [points[i] for i in hull.vertices]

    roi = ImagejRoi.frompoints(boundary_coords)
    roi.roitype = ROI_TYPE.POLYGON

    roi_list.append(roi)

# SAVE ROIs AS .ZIP COMPATIBLE WITH FIJI(IMAGEJ)
roiwrite(save_path.joinpath('pyROi.zip'), roi_list)

print("Detected " + str(len(np.where(iscell)[0])) + " ROIs from total of " + str(len(iscell)) + " cells.")

Let me know if the paths are confusing -- you should set the path to the folder plane0 automatically generated by suite2p which holds the processed .npy files, and the output .zip file called 'pyROI' will be saved in this same folder. You can adjust it however you need. Additionally, you can read the roifile documentation and select the type of roi you want (polygon is set as default by me). Cheers.

**EDIT: forgot to add my code specifically draws ROIs for the masks considered positive by suite2p's cell classifier (e.g. where iscell = true)

Eddymorphling commented 10 months ago

Hi, Thanks for the quick response. I had it working without any issues. Would you happen to know if there is also a way to extract the top 40 cells based on the Skewness factor from the numpy array? The top 40 cells is a function inside the Suite2p GUI.

brian1000 commented 10 months ago

Hey @Eddymorphling. Unfortunately I'm not aware of where the skewness data is stored. I would look through the .npy files ~ its very likely somewhere in stats.npy . I'm lowkey already on a different project unrelated with suite2p xD. Glad to see the roi script worked at least.