MouseLand / suite2p

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

Two bugs related to zcorr processing #1079

Open liadJB opened 8 months ago

liadJB commented 8 months ago

Describe the issue:

There are two bugs preventing using register.registration_wrapper with more than one refImg (in order to get ops["zpos_registration"] for each plane.

(1) First, it is impossible to run the code without non-rigid registration, because of a clash of the number of expected output parameters

(2) Second, there is an error rising when trying to run the code on a 2 channels recording while trying to register by the second channel, this time because of a clash in the expected size of an internal

Reproduce the code example:

import suite2p
raw = True
twoc = True
ops = np.load(ops_path, allow_pickle=True).item()
        with io.BinaryFile(Ly=Ly, Lx=Lx, filename=raw_file, n_frames=n_frames) \
            if raw else null as f_raw, \
         io.BinaryFile(Ly=Ly, Lx=Lx, filename=reg_file, n_frames=n_frames) as f_reg, \
         io.BinaryFile(Ly=Ly, Lx=Lx, filename=raw_file_chan2, n_frames=n_frames) \
            if raw and twoc else null as f_raw_chan2,\
         io.BinaryFile(Ly=Ly, Lx=Lx, filename=reg_file_chan2, n_frames=n_frames) \
            if twoc else null as f_reg_chan2:
                registration_outputs = register.registration_wrapper(
                    f_reg, f_raw=f_raw, f_reg_chan2=f_reg_chan2, f_raw_chan2=f_raw_chan2,
                    refImg=refImgs, align_by_chan2=align_by_chan2, ops=ops)

(1)
ops = default_ops()
    ops["data_path"] = filePath[1:]

    ops["look_one_level_down"] = False
    ops["ignore_flyback"] = [0]
    ops["nchannels"] = 2
    ops["nplanes"] = 8
    ops["functional_chan"] = 1

    # registration ops
    ops["keep_movie_raw"] = True
    ops["align_by_chan"] = 2

    ops["block_size"] = [512, 512]
    ops["nonrigid"] = False
    ops["maxregshift"] = 0.2 # 0.1 default
    # run for only X number frames
    # ops['frames_include'] = 1000

    ops["reg_tif"] =  True
    ops["reg_tif_chan2"] =  True

    ops["fast_disk"] = ops["save_path0"]

    # localised optioed
    ops["delete_extra_frames"] = True
    ops["run_registration"] = True
    ops["run_detection"] = True

    # detection settings - do not change
    ops["allow_overlap"] = True
    ops["max_overlap"] = 0.2
    ops["max_iterations"] = 100

(2)
ops = default_ops()
    ops["data_path"] = filePath[1:]

    ops["look_one_level_down"] = False
    ops["ignore_flyback"] = [0]
    ops["nchannels"] = 2
    ops["nplanes"] = 8
    ops["functional_chan"] = 1

    # registration ops
    ops["keep_movie_raw"] = True
    ops["align_by_chan"] = 2

    ops["block_size"] = [512, 512]
    ops["nonrigid"] = True
    ops["maxregshift"] = 0.2 # 0.1 default
    # run for only X number frames
    # ops['frames_include'] = 1000

    ops["reg_tif"] =  True
    ops["reg_tif_chan2"] =  True

    ops["fast_disk"] = ops["save_path0"]

    # localised optioed
    ops["delete_extra_frames"] = True
    ops["run_registration"] = True
    ops["run_detection"] = True

    # detection settings - do not change
    ops["allow_overlap"] = True
    ops["max_overlap"] = 0.2
    ops["max_iterations"] = 100

Error message:

(1)
Traceback (most recent call last):
  File "c:\repositories\data\twop\zregister_function.py", line 65, in run_single_registration
    newOps = z_register_one_file(ops)
  File "c:\repositories\data\twop\zregister_function.py", line 264, in z_register_one_file
    registration_outputs = register.registration_wrapper(
  File "C:\Users\liad0\anaconda3\lib\site-packages\suite2p\registration\register.py", line 641, in registration_wrapper
    outputs = compute_reference_and_register_frames(f_align_in, f_align_out=f_align_out,
  File "C:\Users\liad0\anaconda3\lib\site-packages\suite2p\registration\register.py", line 449, in compute_reference_and_register_frames
    frames, ymax, xmax, cmax, ymax1, xmax1, cmax1, zest = register_frames(
  File "C:\Users\liad0\anaconda3\lib\site-packages\suite2p\registration\register.py", line 265, in register_frames
    frames, ymax, xmax, cmax, ymax1, xmax1, cmax1 = outputs_best
ValueError: not enough values to unpack (expected 7, got 4)

(2)
Traceback (most recent call last):
  File "c:\repositories\data\twop\zregister_function.py", line 65, in run_single_registration
    newOps = z_register_one_file(ops)
  File "c:\repositories\data\twop\zregister_function.py", line 264, in z_register_one_file
    registration_outputs = register.registration_wrapper(
  File "C:\Users\liad0\anaconda3\lib\site-packages\suite2p\registration\register.py", line 652, in registration_wrapper
    mean_img_alt = shift_frames_and_write(f_alt_in, f_alt_out, yoff, xoff, yoff1,
  File "C:\Users\liad0\anaconda3\lib\site-packages\suite2p\registration\register.py", line 522, in shift_frames_and_write
    frames = shift_frames(frames, yoffk, xoffk, yoff1k, xoff1k, blocks, ops)
  File "C:\Users\liad0\anaconda3\lib\site-packages\suite2p\registration\register.py", line 354, in shift_frames
    frames = nonrigid.transform_data(frames, yblock=blocks[0], xblock=blocks[1],
  File "C:\Users\liad0\anaconda3\lib\site-packages\suite2p\registration\nonrigid.py", line 428, in transform_data
    yup, xup = upsample_block_shifts(
  File "C:\Users\liad0\anaconda3\lib\site-packages\suite2p\registration\nonrigid.py", line 394, in upsample_block_shifts
    ymax1 = ymax1.reshape(nimg, nblocks[0], nblocks[1])
ValueError: cannot reshape array of size 4500 into shape (500,1,1)

Version information:

suite2p v0.14.2

Context for the issue:

We are using the z-correlation feature to choose the planes that stayed the most stable during the recording. Without this feature we cannot use the data, since we are recording boutons that can very easily move in and out of view. Many of our recordings contain 2 channels (one structural red - vgat), which again we have to use, since boutons are not stable enough to register the image properly a lot of the times. This error, currently means our analysis pipeline is halted.