PeyracheLab / miniscoPy

A package to analyse calcium imaging data recorded with the Miniscope.
GNU General Public License v2.0
32 stars 9 forks source link

Dimension mismatch in cnmfe initialisation #9

Closed lyprince closed 6 years ago

lyprince commented 6 years ago

Hi,

I'm trying to fit cnmfe to my own data. Its a video with 2357 frames, with dims=[486, 648].

A ValueError is thrown when initializing the fitting procedure:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-10-676b93ed6dca> in <module>()
      1 cnm = ms.CNMFE(data, parameters['cnmfe'])
----> 2 cnm.fit(procs)

~/anaconda2/envs/calcium/lib/python3.6/site-packages/miniscopy-0.1-py3.6-linux-x86_64.egg/miniscopy/cnmf_e/cnmfe.py in fit(self, procs)
    429                 # 2. Fit for each patch by calling the class Patch
    430                 #################################################################################################
--> 431                 self.fit_patch(procs)
    432 
    433                 #################################################################################################

~/anaconda2/envs/calcium/lib/python3.6/site-packages/miniscopy-0.1-py3.6-linux-x86_64.egg/miniscopy/cnmf_e/cnmfe.py in fit_patch(self, procs)
    139                 # fitting it
    140                 for i in tqdm(range(len(self.patch_index))):
--> 141                         self.patch_object[i].fit(procs)
    142                 return
    143 

~/anaconda2/envs/calcium/lib/python3.6/site-packages/miniscopy-0.1-py3.6-linux-x86_64.egg/miniscopy/cnmf_e/cnmfe.py in fit(self, procs)
     59                 # 2. Initialize components / CALL greedyROI_corr in CAIMAN PACKAGE
     60                 #################################################################################################
---> 61                 initialize_components(self, **self.parameters['init_params'])
     62 
     63                 #################################################################################################

~/anaconda2/envs/calcium/lib/python3.6/site-packages/miniscopy-0.1-py3.6-linux-x86_64.egg/miniscopy/cnmf_e/initialization.py in initialize_components(patch, gSig, gSiz, ssub, tsub, **kwargs)
    753     kwargs_copy['ssub'] = ssub
    754 
--> 755     greedyROI_corr(patch, Yc, **kwargs_copy)
    756 
    757     K = patch.A.shape[0] # number of neurons initialized

~/anaconda2/envs/calcium/lib/python3.6/site-packages/miniscopy-0.1-py3.6-linux-x86_64.egg/miniscopy/cnmf_e/initialization.py in greedyROI_corr(patch, Yc, tsub, ssub, ring_size_factor, **kwargs)
    606             # cause the error in upsampled background can be of similar size as neural signal
    607         else:
--> 608             B = patch.patch_group['Y'] - C.dot(A) # TO CHECK HERE
    609 
    610         tmp = B - b0  #(Y - AC - b0)

ValueError: operands could not be broadcast together with shapes (2357,21009) (2357,5180) 

Based on the dimensions of the data I don't know where these array shapes are coming from? Any help would be greatly appreciated

Luke

gviejo commented 6 years ago

Hi Luke,

This array shapes comes from one patch that is smaller than your original dims. Do you still have this problem of mismatch?

If yes, can you send me a sample of you dataset and your parameters?

Guillaume

lyprince commented 6 years ago

Hi Guillaume,

I don't know what the appropriate patch size is, so I still have the mismatch.

Here is a link to an example dataset: https://drive.google.com/file/d/1vY_VDeRLkUSlCMdOt8UV0P1w77bk0OWx/view?usp=sharing

and here is a link to parameters I'm using: https://drive.google.com/open?id=1gbmx7CuLns1JcKZKFlIAxxMyojxIYI3I

The parameters are the same as the example parameters at the moment, except for the frame rate and temporal downsampling factor.

Thanks, Luke

gviejo commented 6 years ago

Hi Luke,

The bug was the temporal dowsampling factor. Thanks for letting me know. I fixed this bug and it should work now. Can you test again your dataset and keep me updated? (I didn't went to the end of the analysis so there might be other bugs).

Guillaume

lyprince commented 6 years ago

Thanks Guillaume.

I tested with my dataset. It ran all the way through, but apparently no cells were found - the output matrices were empty. I thought it might be because I had used a lossy encoder ('mpeg4') to convert the video from its original .mkv format to .avi, so I tried it with a couple of lossless encoders. With h264 the file couldn't be opened, and with huffyuv some frames were not motion corrected properly and outputted as all NaNs, so CNMFE wouldn't run on it.

Can you recommend an encoder to use? Or should I adjust the parameters for detecting cells across frames? I'm new to this so I don't have much intuition for how these parameters affect the analysis.

DenisPolygalov commented 6 years ago

@lyprince Just an advice after reading your post: don't use any encoder for miniscope data. Never. Unless you wrote one from scratch by yourself :)

lyprince commented 6 years ago

I'm going to close this and open up some separate issues as there seem to be some issues with normcorre and cnmfe separately.

@DenisPolygalov I had to transcode the data as it was given to me in .mkv format written with a ffv1 encoder and yuv420p16le pixel format. Simply changing the container to .avi didn't work and couldn't be read - in converting a video to hdf5 format the parameters for PyAv to read the video didn't work. I eventually settled on a 'rawvideo' encoder and 'bgr24' pixel format as this is what is used in the example. Its lossless so it should be fine.

gviejo commented 6 years ago

Hello, I am working on your movie. The cnmfe problem is mainly the parameters. I can detect neurons with this: parameters['cnmfe']['init_params']['thresh_init'] = 0.5 parameters['cnmfe']['init_params']['min_corr'] = 0.5 parameters['cnmfe']['init_params']['min_pnr'] = 0.75

You can change them directly in main_cnmf_e.py. I put them in the script as they are the most importants.

Although there is an issue with normcorre as well with some inf appearing. I am working on it right now.

Best Guillaume