Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use tsub #651

Closed
x8808099 opened this issue Oct 9, 2019 · 8 comments
Closed

Cannot use tsub #651

x8808099 opened this issue Oct 9, 2019 · 8 comments

Comments

@x8808099
Copy link

x8808099 commented Oct 9, 2019

Operating system: Linux

Python version: 3.7.3

Working environment: VSCode / Jupyter

CaImAn version: 1.6.2

CaImAn installation process: conda-forge

  • Issues

tsub=1 works well, but tsub>1 will cause error. Tested with both large and small files or different patch size.

  • Error log

~/miniconda3/lib/python3.7/site-packages/caiman/source_extraction/cnmf/cnmf.py in fit(self, images, indices)
503 if self.estimates.A is None:
504 logging.info('initializing ...')
--> 505 self.initialize(Y)
506
507 if self.params.get('patch', 'only_init'): # only return values after initialization

~/miniconda3/lib/python3.7/site-packages/caiman/source_extraction/cnmf/cnmf.py in initialize(self, Y, **kwargs)
972 estim.A, estim.C, estim.b, estim.f, estim.center =
973 initialize_components(Y, sn=estim.sn, options_total=self.params.to_dict(),
--> 974 **self.params.get_group('init'))
975
976 self.estimates = estim

~/miniconda3/lib/python3.7/site-packages/caiman/source_extraction/cnmf/initialization.py in initialize_components(Y, K, gSig, gSiz, ssub, tsub, nIter, maxIter, nb, kernel, use_hals, normalize_init, img, method_init, max_iter_snmf, alpha_snmf, sigma_smooth_snmf, perc_baseline_snmf, options_local_NMF, rolling_sum, rolling_length, sn, options_total, min_corr, min_pnr, ring_size_factor, center_psf, ssub_B, init_iter, remove_baseline, SC_kernel, SC_sigma, SC_thr, SC_normalize, SC_use_NN, SC_nnn, lambda_gnmf)
419
420 center = np.asarray(
--> 421 [center_of_mass(a.reshape(d, order='F')) for a in Ain.T])
422 else:
423 Cin = np.empty((K, T), dtype=np.float32)

~/miniconda3/lib/python3.7/site-packages/caiman/source_extraction/cnmf/initialization.py in (.0)
419
420 center = np.asarray(
--> 421 [center_of_mass(a.reshape(d, order='F')) for a in Ain.T])
422 else:
423 Cin = np.empty((K, T), dtype=np.float32)

ValueError: cannot reshape array of size 3240 into shape (81,81)

Traceback (most recent call last):
File "/opt/anaconda3/envs/caiman/lib/python3.7/multiprocessing/pool.py", line 121, in worker
result = (True, func(*args, **kwds))
File "/opt/anaconda3/envs/caiman/lib/python3.7/multiprocessing/pool.py", line 44, in mapstar
return list(map(*args))
File "/opt/anaconda3/envs/caiman/lib/python3.7/site-packages/caiman/source_extraction/cnmf/map_reduce.py", line 137, in cnmf_patches
cnm = cnm.fit(images)
File "/opt/anaconda3/envs/caiman/lib/python3.7/site-packages/caiman/source_extraction/cnmf/cnmf.py", line 505, in fit
self.initialize(Y)
File "/opt/anaconda3/envs/caiman/lib/python3.7/site-packages/caiman/source_extraction/cnmf/cnmf.py", line 974, in initialize
**self.params.get_group('init'))
File "/opt/anaconda3/envs/caiman/lib/python3.7/site-packages/caiman/source_extraction/cnmf/initialization.py", line 307, in initialize_components
Y_ds = downscale(Y, tuple([ssub] * len(d) + [tsub]), opencv=True)
File "/opt/anaconda3/envs/caiman/lib/python3.7/site-packages/caiman/source_extraction/cnmf/initialization.py", line 83, in downscale
interpolation=cv2.INTER_AREA)
File "/opt/anaconda3/envs/caiman/lib/python3.7/site-packages/caiman/base/movies.py", line 939, in resize
interpolation=interpolation)
File "/opt/anaconda3/envs/caiman/lib/python3.7/site-packages/caiman/base/movies.py", line 962, in resize
fx=1, fy=fz, interpolation=interpolation)
cv2.error: OpenCV(4.1.1) ../modules/imgproc/src/resize.cpp:3726: error: (-215:Assertion failed) !dsize.empty() in function 'resize'

@epnev
Copy link
Contributor

epnev commented Oct 9, 2019

@x8808099 Can you share a small dataset where this happens and the script you're using?

@x8808099
Copy link
Author

x8808099 commented Oct 9, 2019

A 400-frame dataset.

Run these will produce the 'cannot reshape' error.

import numpy as np
import caiman as cm
from caiman.motion_correction import MotionCorrect
from caiman.source_extraction.cnmf import cnmf
from caiman.source_extraction.cnmf import params

t_sub=2; dview=None; n_processes=1
fname = 'test0.tif'
opts = params.CNMFParams(params_dict={
    'fnames': [fname],
    'fr': 8,
    'decay_time': 1,
    'strides': (96,96),
    'overlaps': (32,32),
    'max_shifts': (12,12),
    'max_deviation_rigid': 6,
    'num_frames_split': 100,
    'pw_rigid': True,
    'p': 0,
    'nb': 2,
    'rf': 32,
    'K': 8, 
    'stride': 14,
    'method_init': 'greedy_roi',
    'rolling_sum': True,
    'only_init': True,
    'gSig': [6,6],
    'ssub': 1,
    'tsub': t_sub,
    'noise_range': [0.3, 0.5]})
mc = MotionCorrect(opts.data['fnames'], dview=dview, **opts.get_group('motion'))
mc.motion_correct(save_movie=True)
fname_new = cm.save_memmap(mc.mmap_file, base_name='memmap', order='C', border_to_0=0, dview=dview)
Yr, dims, T = cm.load_memmap(fname_new)
images = np.reshape(Yr.T, [T] + list(dims), order='F') 
cnm = cnmf.CNMF(n_processes, params=opts, dview=dview)
cnm = cnm.fit(images)

Error log:

~/miniconda3/lib/python3.7/site-packages/caiman/source_extraction/cnmf/cnmf.py in fit(self, images, indices)
503 if self.estimates.A is None:
504 logging.info('initializing ...')
--> 505 self.initialize(Y)
506
507 if self.params.get('patch', 'only_init'): # only return values after initialization

~/miniconda3/lib/python3.7/site-packages/caiman/source_extraction/cnmf/cnmf.py in initialize(self, Y, **kwargs)
972 estim.A, estim.C, estim.b, estim.f, estim.center =
973 initialize_components(Y, sn=estim.sn, options_total=self.params.to_dict(),
--> 974 **self.params.get_group('init'))
975
976 self.estimates = estim

~/miniconda3/lib/python3.7/site-packages/caiman/source_extraction/cnmf/initialization.py in initialize_components(Y, K, gSig, gSiz, ssub, tsub, nIter, maxIter, nb, kernel, use_hals, normalize_init, img, method_init, max_iter_snmf, alpha_snmf, sigma_smooth_snmf, perc_baseline_snmf, options_local_NMF, rolling_sum, rolling_length, sn, options_total, min_corr, min_pnr, ring_size_factor, center_psf, ssub_B, init_iter, remove_baseline, SC_kernel, SC_sigma, SC_thr, SC_normalize, SC_use_NN, SC_nnn, lambda_gnmf)
419
420 center = np.asarray(
--> 421 [center_of_mass(a.reshape(d, order='F')) for a in Ain.T])
422 else:
423 Cin = np.empty((K, T), dtype=np.float32)

~/miniconda3/lib/python3.7/site-packages/caiman/source_extraction/cnmf/initialization.py in (.0)
419
420 center = np.asarray(
--> 421 [center_of_mass(a.reshape(d, order='F')) for a in Ain.T])
422 else:
423 Cin = np.empty((K, T), dtype=np.float32)

ValueError: cannot reshape array of size 2080 into shape (65,65)

@epnev
Copy link
Contributor

epnev commented Oct 9, 2019

Thanks, I can reproduce the error. Will take a look.

@epnev
Copy link
Contributor

epnev commented Oct 9, 2019

@x8808099 I identified the error (it was a mix between spatial and temporal dimensions) and produced a fix d9ae2ba#diff-c2edd928338c914eaa0831c9b274b80dR90
Since you use the conda installation and cannot modify the sources you'll have to wait until the next release which I'll push through after some testing. In the meantime, you can prevent this bug from taking place by using ssub=tsub (in which case the dimension mixup will not have an effect).

epnev added a commit that referenced this issue Oct 9, 2019
@x8808099
Copy link
Author

x8808099 commented Oct 9, 2019

Thanks! I want to avoid using ssub at this point. If I add together every 2 images when saving the raw data as tif, would it be equivalent to tsub=2?

@epnev
Copy link
Contributor

epnev commented Oct 10, 2019

Yes, tsub=2 should be fairly close to just taking the average every 2 frames.
The new caiman version is up on the conda-forge channel. Can you update and let me know whether the script goes through now?

@x8808099
Copy link
Author

Our machine is down and requires maintenance, will do so in a few days.

@x8808099
Copy link
Author

tsub works now.

@epnev epnev closed this as completed Oct 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants