KordingLab / Neural_Decoding

A python package that includes many methods for decoding neural activity
BSD 3-Clause "New" or "Revised" License
429 stars 119 forks source link

NaiveBayesDecoder: example_data_hc statsmodel ValueError during predict #9

Open Blinky0815 opened 5 years ago

Blinky0815 commented 5 years ago

Hi, I am getting this error when trying to run the reconstruction using the NaiveBayesDecoder for example_data_hc.pickle There is no error when trying to run it on the example data sets. I am using Ubuntu 19.04 and python3.7


R2s: [0.53720666 0.79702171] time_elapsed: 38.46179175376892 R2s: [0.65030058 0.77625489] time_elapsed: 76.14730286598206 R2s: [0.68462371 0.53993597] time_elapsed: 116.00050210952759 R2s: [0.76831659 0.75020602] time_elapsed: 153.70069885253906 R2s: [0.53691629 0.64449254] time_elapsed: 192.6614019870758 /home/blinky/.local/lib/python3.7/site-packages/statsmodels/genmod/families/links.py:521: RuntimeWarning: overflow encountered in exp return np.exp(z) /home/blinky/.local/lib/python3.7/site-packages/statsmodels/genmod/families/family.py:430: RuntimeWarning: overflow encountered in multiply return 2 * resid_dev /home/blinky/.local/lib/python3.7/site-packages/statsmodels/genmod/families/family.py:134: RuntimeWarning: invalid value encountered in multiply return 1. / (self.link.deriv(mu)*2 self.variance(mu)) /home/blinky/.local/lib/python3.7/site-packages/statsmodels/genmod/families/family.py:134: RuntimeWarning: divide by zero encountered in true_divide return 1. / (self.link.deriv(mu)*2 self.variance(mu)) /home/blinky/.local/lib/python3.7/site-packages/statsmodels/genmod/generalized_linear_model.py:1163: RuntimeWarning: invalid value encountered in multiply

~/.local/lib/python3.7/site-packages/Neural_Decoding-0.1.2.dev0-py3.7.egg/Neural_Decoding/decoders.py in fit(self, X_b_train, y_train) 914 tuning=glm_run(y_train,X_b_train[:,j:j+1],input_xy) 915 if self.encoding_model=='quadratic': --> 916 tuning=glm_run(y_train_modified,X_b_train[:,j:j+1],input_xy_modified) 917 #Enter tuning curves into matrix 918 tuning_all[j,:]=np.squeeze(tuning)

~/.local/lib/python3.7/site-packages/Neural_Decoding-0.1.2.dev0-py3.7.egg/Neural_Decoding/decoders.py in glm_run(Xr, Yr, X_range) 829 poiss_model = sm.GLM(Yr, X2, family=sm.families.Poisson()) 830 try: --> 831 glm_results = poiss_model.fit() 832 Y_range=glm_results.predict(sm.add_constant(X_range)) 833 except np.linalg.LinAlgError:

~/.local/lib/python3.7/site-packages/statsmodels/genmod/generalized_linear_model.py in fit(self, start_params, maxiter, method, tol, scale, cov_type, cov_kwds, use_t, full_output, disp, max_start_irls, kwargs) 1026 return self._fit_irls(start_params=start_params, maxiter=maxiter, 1027 tol=tol, scale=scale, cov_type=cov_type, -> 1028 cov_kwds=cov_kwds, use_t=use_t, kwargs) 1029 else: 1030 self._optim_hessian = kwargs.get('optim_hessian')

~/.local/lib/python3.7/site-packages/statsmodels/genmod/generalized_linear_model.py in _fit_irls(self, start_params, maxiter, tol, scale, cov_type, cov_kwds, use_t, **kwargs) 1164 wls_mod = reg_tools._MinimalWLS(wlsendog, wlsexog, 1165 self.weights, check_endog=True, -> 1166 check_weights=True) 1167 wls_results = wls_mod.fit(method=wls_method) 1168 lin_pred = np.dot(self.exog, wls_results.params)

~/.local/lib/python3.7/site-packages/statsmodels/regression/_tools.py in init(self, endog, exog, weights, check_endog, check_weights) 46 if check_weights: 47 if not np.all(np.isfinite(w_half)): ---> 48 raise ValueError(self.msg.format('weights')) 49 50 if check_endog:

ValueError: NaN, inf or invalid value detected in weights, estimation infeasible.

jglaser2 commented 5 years ago

Use the notebook "Examples_naivebayes_decoder_hc.ipynb" inside the "Examples_hippocampus" folder if you're using the hippocampus dataset. There is some additional preprocessing for that dataset.

Blinky0815 commented 5 years ago

Thanks! The example doesn't crash.

Blinky0815 commented 5 years ago

Hi, I am sorry I have to reopen this, but the issue only goes away if I comment

In HC dataset, there is a long period without movement starting at ~80%, so we only use the first 80% of the data if dataset=='hc': X=X[:int(.8X.shape[0]),:] y=y[:int(.8y.shape[0]),:]

and instead, work on the entire dataset.

Also in order to make the code work I needed to add the Neural_Decoding. prefix in the import section.

Import function to get the covariate matrix that includes spike history from previous bins

from Neural_Decoding.preprocessing_funcs import get_spikes_with_history

Import metrics

from Neural_Decoding.metrics import get_R2 from Neural_Decoding.metrics import get_rho

Import decoder functions

from Neural_Decoding.decoders import NaiveBayesDecoder

Import Bayesian Optimization package

from bayes_opt import BayesianOptimization

jglaser2 commented 5 years ago

Thanks for mentioning the prefix - we did not update any of the examples in the Examples_hippocampus folder when we updated the code a few months ago. We will do that.

When I have time, I'll look into the issue with only using 80% of the data.

Blinky0815 commented 4 years ago

Nice! Thanks!