rochefort-lab / fissa

A Python toolbox for Fast Image Signal Separation Analysis, designed for Calcium Imaging data.
GNU General Public License v3.0
30 stars 26 forks source link

Raw signals do not match suite2p raw signals for the same rois #286

Open just-meng opened 1 year ago

just-meng commented 1 year ago

Hi there,

I was running fissa on suite2p motion-corrected reg_tif files following this instruction. When comparing the result with the subtraction method (F = 0.7*Fneu) I noticed that they were nowhere similar to each other. Having a look at the extracted raw signals by fissa (experiment.raw) and by suite2p (F.npy) of the same roi I noticed that also the raw traces were not similar.

image

I triple-checked the roi identity and plotted experiment.roi_polys[i_roi, trial][0]to compare with suite2p GUI). It definitely is the same roi. Any idea why the raw traces do no agree?

Just to be sure, the code is attached here:

images = "./reg_tif"

# Get image size
Lx = ops["Lx"]
Ly = ops["Ly"]

# Generate ROI masks in a format usable by FISSA (in this case, a list of masks)
rois = [np.zeros((Ly, Lx), dtype=bool) for n in range(n_cells)]

for i, n in enumerate(cell_ids):
    # i is the position in cell_ids, and n is the actual cell number
    ypix = stat[n]["ypix"][~stat[n]["overlap"]] # get only the non-overlapping pixels
    xpix = stat[n]["xpix"][~stat[n]["overlap"]]
    rois[i][ypix, xpix] = 1

fissa_folder = "../../fissa"
fissa_experiment = fissa.Experiment(images, [rois], fissa_folder)
fissa_experiment.separate()

n_cells and cell_ids are previously obtained from F.npy and iscell.npy.

just-meng commented 1 year ago

I found the bug: if the folder of the suite2p registered images is passed to fissa.Experiment(images, rois, folder) as argument images, the tif files will be sorted according to self.images = sorted(glob.glob(os.path.join(images, "*.tif*"))) This leads to incorrectly sorted images simply due to the naming of the tif files: file000_chan0, file500_chan0, file1000_chan0 After sorting it lists: file000_chan0, file1000_chan0, file500_chan0 The result is temporally mis-appended signals.

A quick fix to my code (not elegant but works):

images_folder = "./reg_tif"
images_path = glob.glob(os.path.join(images_folder, "*.tif*"))
# extract the interger that indicates the number of the starting frame of the tif and add to the path as a tuple
images_path = [(int(path.split('_')[-2].split('file')[-1]), path) for path in images_path]
# sort the path list according to the frame numbers
sorted_path = sorted(images_path)
# get rid of the integer and make a list of the now sorted paths
sorted_path = list(np.array(sorted_path)[:,1])

Now one can run fissa.Experiment(sorted_path, rois, folder).