flatironinstitute / CaImAn

Computational toolbox for large scale Calcium Imaging Analysis, including movie handling, motion correction, source extraction, spike deconvolution and result visualization.
https://caiman.readthedocs.io
GNU General Public License v2.0
637 stars 370 forks source link

UnboundLocalError: local variable 'res0' referenced before assignment when running modified demo_pipeline_cnmfE.py #899

Closed michaelfsp closed 3 years ago

michaelfsp commented 3 years ago

For better support, please use the template below to submit your issue. When your issue gets resolved please remember to close it.

Sometimes errors while running CNMF occur during parallel processing which prevents the log to provide a meaningful error message. Please reproduce your error with setting dview=None.

If you need to upgrade CaImAn follow the instructions given in the documentation.

/mnt/dataz/michael/src/CaImAn/caiman/source_extraction/cnmf/cnmf.py in fit(self, images, indices) 629 else: 630 while len(self.estimates.merged_ROIs) > 0: --> 631 self.merge_comps(Yr, mx=np.Inf, fast_merge=True) 632 #if len(self.estimates.merged_ROIs) > 0: 633 #not_merged = np.setdiff1d(list(range(len(self.estimates.YrA))),

/mnt/dataz/michael/src/CaImAn/caiman/source_extraction/cnmf/cnmf.py in merge_comps(self, Y, mx, fast_merge, max_merge_area) 952 g=self.estimates.g, thr=self.params.get('merging', 'merge_thr'), mx=mx, 953 fast_merge=fast_merge, merge_parallel=self.params.get('merging', 'merge_parallel'), --> 954 max_merge_area=max_merge_area) 955 956 return self

/mnt/dataz/michael/src/CaImAn/caiman/source_extraction/cnmf/merging.py in merge_components(Y, A, b, C, R, f, S, sn_pix, temporal_params, spatial_params, dview, thr, fast_merge, mx, bl, c1, sn, g, merge_parallel, max_merge_area) 245 g_idx = [merged_ROI[indx]] 246 bm, cm, computedA, computedC, gm, sm, ss, yra = merge_iteration(Acsc, C_to_norm, Ctmp, fast_merge, g, g_idx, --> 247 indx, temporal_params) 248 249 A_merged[:, i] = csr_matrix(computedA).T

/mnt/dataz/michael/src/CaImAn/caiman/source_extraction/cnmf/merging.py in merge_iteration(Acsc, C_to_norm, Ctmp, fast_merge, g, g_idx, indx, temporalparams) 349 print("Merge Iteration without g...") 350 deconvC, bm, cm, gm, sm, ss, lam = constrained_foopsi( --> 351 c_in, g=None, **temporal_params) 352 return bm, cm, computedA, deconvC, gm, sm, ss, c_in - deconvC

/mnt/dataz/michael/src/CaImAn/caiman/source_extraction/cnmf/deconvolution.py in constrained_foopsi(fluor, bl, c1, g, sn, p, method_deconvolution, bas_nonneg, noise_range, noise_method, lags, fudge_factor, verbosity, solvers, optimize_g, s_min, **kwargs) 161 c, sp, bl, g, lam = constrained_oasisAR2( 162 fluor.astype(np.float32), g, sn, optimize_b=True, b_nonneg=bas_nonneg, --> 163 optimize_g=optimize_g, penalty=penalty, s_min=smin) 164 else: 165 c, sp, , g, lam = constrained_oasisAR2(

/mnt/dataz/michael/src/CaImAn/caiman/source_extraction/cnmf/deconvolution.py in constrained_oasisAR2(y, g, sn, optimize_b, b_nonneg, optimize_g, decimate, shift, window, tol, max_iter, penalty, s_min) 925 i = (l + u) // 2 926 if i > 0: --> 927 c = res0 928 s = np.append([0, 0], c[2:] - g[0] c[1:-1] - g[1] c[:-2]) 929 else:

UnboundLocalError: local variable 'res0' referenced before assignment

Upon running %debug: ipdb> args y = array([-13.793553 , -21.974134 , -46.97406 , ..., -1.4499683, -23.734684 , -1.0313864], dtype=float32) g = array([ 1.19827518, -0.35684638]) sn = 31.121605 optimize_b = True b_nonneg = False optimize_g = 5 decimate = 5 shift = 100 window = 200 tol = 1e-09 max_iter = 1 penalty = 0 s_min = 0 ipdb> print(spikesizes) [13.16290771 27.83360049]

pgunn commented 3 years ago

@j-friedrich Looking at deconvolution.py:constrained_oasisAR2() It looks like res0 isn't always given a value before that c = res0 assignment.

I'm not sure how to fix that. Can you give it a look?

j-friedrich commented 3 years ago

I replicated the issue for spike trains that had only 0-2 spikes. I updated the code in the dev branch. @michaelfsp please report back whether the recent commit solves your issue. Thanks

j-friedrich commented 3 years ago

The fix made it into the master branch, thus closing.