Giter VIP home page Giter VIP logo

normcorre's Introduction

Join the chat at https://gitter.im/epnev/ca_source_extraction

NoRMCorre: Non-Rigid Motion Correction

This package provides a Matlab implementation of the NoRMCorre algorithm [1], and can be used for online piecewise rigid motion correction of 2d (planar) or 3d (volumetric) calcium imaging data.

Citation

If you find this package useful please cite the companion paper [1]:

@article{pnevmatikakis2017normcorre,
  title={NoRMCorre: An online algorithm for piecewise rigid motion correction of calcium imaging data},
  author={Pnevmatikakis, Eftychios A and Giovannucci, Andrea},
  journal={Journal of neuroscience methods},
  volume={291},
  pages={83--94},
  year={2017},
  publisher={Elsevier}
}

Synopsis

The algorithm operates by splitting the field of view into a set of overlapping patches. For each patch and each frame a rigid translation is estimated by aligning the patch against a template using an efficient, FFT based, algorithm for subpixel registration [2]. The estimated set of translations is further upsampled to a finer resolution to create a smooth motion field that is applied to a set of smaller overlapping patches. Extra care is taken to avoid smearing caused by interpolating overlapping patches with drastically different motion vectors. The registered frame is used to update the template in an online fashion by calculating a running/mean of past registered frames. The pipeline is summarized in the figure below.

Alt text

Code details

See the function demo.m for an example of the code. The algorithm is implemented in the function normcorre.m. If you have access to the parallel computing toolbox, then the function normcorre_batch.m can offer speed gains by enabling within mini-batch parallel processing. The user gives a dataset (either as 3D or 4D tensor loaded in RAM or memory mapped, or a pointer to a .tiff stack or .hdf5 file), and a parameters struct options. Optionally, an initial template can also be given. The algorithm can also be used for motion correction of 1p micro-endoscopic data, by estimating the shifts on high pass spatially filtered version of the data. See the script demo_1p.m for an example.

The algorithm can also be ran using the MotionCorrection object. See demo_mc_class.m for an example on how to use the object for 2p and 1p data.

The options struct can be set either manually or by using the function NoRMCorreSetParms.m. Some parameters of the options struct are the following:

Parameter name Description
d1,d2,d3 dimensions of field of view
grid_size size of non-overlapping portion of each patch the grid in each direction (x-y-z)
overlap_pre size of overlapping region in each direction before upsampling
mot_uf upsampling factor for smoothing and refinement of motion field
overlap_post size of overlapping region in each direction after upsampling
max_shift maximum allowed shift for rigid translation
max_dev maximum deviation of each patch from estimated rigid translation
upd_template update the template online after registering some frames
bin_width length of bin over which the registered frames are averaged to update the template
init_batch number of frames to be taken for computing initial template
iter number of times to go over the dataset
output_type type of output registered file
phase_flag flag for using phase correlation
correct_bidir check for offset due to bidirectional scanning (default: true)

The performance of registration can be evaluated using the function motion_metrics.m. The function simply computes the correlation coefficient of each (registered) frame, with the mean (registered) frame across time, the mean registered frame, and its crispness.

Developers

Eftychios A. Pnevmatikakis, Flatiron Institure, Simons Foundation

External packages

This package includes functions from the following packages

Integrations

This package will be integrated with the Matlab code for source extraction and deconvolution using CNMF.

A python version of this algorithm developed from Andrea A. Giovannuci is included as part of the CaImAn package that provides a complete pipeline for calcium imaging data pre-processing.

Although the two implementations give almost identical results for the same input file, there are some slight differences in the way they are called and their capabilities. These differences are highlighted here.

More details, contact information, and citing NoRMCorre

Check the wiki for more details and some frequently asked questions.

Please use the gitter chat room for questions and comments, and create an issue for any bugs you might encounter.

If you find this package useful please cite the following paper:

Eftychios A. Pnevmatikakis and Andrea Giovannucci, NoRMCorre: An online algorithm for piecewise rigid motion correction of calcium imaging data, Journal of Neuroscience Methods, vol. 291, pp 83-94, 2017; doi: https://doi.org/10.1016/j.jneumeth.2017.07.031

Acknowledgements

The 2p example dataset is kindly provided from Andrea Giovannucci, taken at Wang lab (Princeton University). The 1p example dataset is kindly provided by Daniel Aharoni and Peyman Golshani (UCLA, Miniscope project).

References

[1] Eftychios A. Pnevmatikakis and Andrea Giovannucci, NoRMCorre: An online algorithm for piecewise rigid motion correction of calcium imaging data, Journal of Neuroscience Methods, vol. 291, pp 83-94, 2017; doi: https://doi.org/10.1016/j.jneumeth.2017.07.031

[2] Guizar-Sicairos, M., Thurman, S. T., & Fienup, J. R. (2008). Efficient subpixel image registration algorithms. Optics letters, 33(2), 156-158. Matlab implementation available here.

normcorre's People

Contributors

epnev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

normcorre's Issues

Image registration progress on command line

Hi,

I think it is great to have the progress printed to the command line, but it results in a lot of text, and sometimes I want to see messages printed before I started image registration.

I have some few pieces of code that I use in my scripts to update the the textstring on the command line, instead of printing a new one:

str=[num2str(n), 'out of ', num2str(nFrames), 'frames registered'];
refreshdisp(str, prevstr, n);
prevstr=str; 

where refreshdisp looks like this:

function refreshdisp(str,prevstr,iteration)

if ~exist('iteration')
    iteration=2;
end

if iteration==1
    fprintf(str)
else
    fprintf(char(8*ones(1,length(prevstr))));
    fprintf(str);
end

also need to initialize prevstr.

Maybe this could be added to normcorre?

Some videos exit with error

My videos have non-square pixels. In some (but not most) of my videos exit with the following error:

Error using normcorre_batch (line 283)
Index exceeds array bounds.

Here is the code I use to call normcorre_batch:

d1 = size(M1,1);
d2 = size(M1,2);

options_nonrigid = NoRMCorreSetParms(...
    'd1',d1,...
    'd2',d2,...
    ...
    'grid_size',[d1/4,d2/4],...
    'overlap_pre',[d1/16,d2/16],...
    'min_patch_size',[d1/16,d2/16],...
    'min_diff',[d1/32,d2/32,5],...
    'us_fac',50*d2/d1,...    
    'mot_uf',[2048/d1 2048/d2],...
    'max_dev',[d1/(170+2/3) d2/(170+2/3)],...
    'overlap_post',[d1/16 d2/16],...
    'max_shift',[d1/(512/70) d2/(512/70)],...
    ...    
    'bin_width',200,...        
    'init_batch',600,...
    'boundary',NaN);
tic; [M2,shifts2,template2,options_nonrigid] = normcorre_batch(M1,options_nonrigid); toc

A video that crashes normcorre can be found here (https://bit.ly/3hwT2k9).

Have trouble registering an image stack with a template

I have two image stacks. The size of them are 20482048600. I can get good results if I register each stack separately. However, if I register stack1 firstly and then use the average image of stack1(after registration) to register stack2. The alignment is pretty off.
Here's the average image of my stack1:
image
Here's the average image of my stack 2 after registration using the average of stack1:
image

I can get good results if I don't set template to register stack2.

I only used rigid motion correction. Here're my parameters:
NoRMCorreSetParms('d1',size(Y,1),'d2',size(Y,2),'bin_width',round(T),'max_shift',50,'us_fac',50,'init_batch',200);

Unexpected effect of correct_bidirectional_offset function.

I am using the correct_bidirectional_offset to correct data after resonance scanning. We just found out that when using [col_shift, M] = correct_bidirectional_offset(Y, nFrames, us) the pixel values of the output is different than the pixel values of the input.

Whereas the pixel values for the input varies between 31040 and 65534, the pixel values of the output varies between 26849 and 71852.

I usually run the code with an upsampling factor of 10 (default). I tried running with the us variabel set to 1 and then I got the expected minimum and maximum values on the output.

bug using options.boundary with options.shifts_method = 'cubic'

Hi Eftychios

When using the options.shifts_method = 'cubic', the imwarp function by default set to zero all values outside the image. So options.boundary does not have an effect on this, however you can fix that if you add the imwarp(*, *, 'FillValues', add_value).

one could easily fix this on apply_shifts.m by adding:
add_value = 0;
if strcmpi(options.boundary, 'nan')
add_value = nan;
end
on line: 213
and editing imwarp to accept 'FillValues' as add_value.
This would only work if you want those values to be 0 or nan, but dont quite know how to get the options.boundary options: 'copy' and 'template' implemented.

Diego

bug in normcorre.m when using 3D data

When using the cubic shift_method, I get this error:

Error using imwarp>checkImageAgreementWithTform (line 269)
The number of dimensions of the input image A must be 3 when the specified geometric transformation is 3-D.

for dm = 1:3; shifts_up(:,:,:,dm) = imwarp(shifts_temp(:,:,:,dm),tform,'OutputView',imref3d([options.d1,options.d2,options.d3])); end

  • the size of shifts_temp(:,:,:,dm) is actually 1, but tform is three-dimensional.

Also there is a bug in line 306:

  • buffer{i,j,k}(:,:,ind) = Mt2{ii};
  • if nd == 2; buffer{i,j,k}(:,:,ind) = Mt2{ii}; end
  • if nd == 3; buffer{i,j,k}(:,:,:,ind) = Mt2{ii}; end

Documentation, references for code

Hello,
I am trying to understand this codebase. Can you please provide proper references and documentation for understanding this codebase better ?
For example, the code in correct_bidirectional_offset.m corrects phase artifacts.I'd like to understand the math behind this.It will be helpful to have the appropriate papers and research articles behind this code.
Thank you

python implementation

Hey all,

I am planning on starting a python implementation of this package here. Let me know if you would like to contribute!

Unnecessary reading of TIFF IFDs?

While registering each image to the template, normcorre_batch.m calls read_file.m to read in a number of frames equal to bin_width, which defaults to 10 and which we frequently set to be much larger. Every time read_file.m is called (at least for TIFFs) it must first read all the IFDs, which can take around 1 minute each time for large TIFFs. If we've read the code correctly, this means for a 30,000-frame movie with bin_width set to 300, the IFDs are read 100X... normcorre would spend 100 minutes on unnecessary file I/O. It might be good to revise the related code to prevent these re-reads.

Thanks.

Unknown error

Hi,
I started to encounter an error (see below) lately, and I wonder if you have any ideas why it might happen.

I am aligning an array of 5000 images, and this is the options I am using:

                Y1 = double(tmp_imArray);
                options_nonrigid = NoRMCorreSetParms('d1',size(Y1,1),'d2',size(Y1,2),...
                                       'grid_size',[64, 64],'mot_uf',4,'bin_width',43,...
                                       'max_shift',15,'max_dev',30,'us_fac',50);

                [tmp_imArray, ~, ~] = normcorre_batch(Y1, options_nonrigid);

This is the error:

Error using remove_boundaries (line 30)
Index exceeds matrix dimensions.

Error in shift_reconstruct (line 48)
    I = remove_boundaries(I,shifts,method,add_value);

Error in normcorre_batch>(parfor body) (line 332)
                                    M_fin{i,j,k} =
                                    shift_reconstruct(I_temp,shifts_up(i,j,k,:),diff_up(i,j,k),us_fac,Nr{i,j,k},Nc{i,j,k},Np{i,j,k},options.boundary,add_value);
                                    
Error in normcorre_batch (line 262)
        parfor ii = 1:lY

Small bright artifacts after registration

Hi, I've been using NoRMCorre to register single-plane 2-photon calcium imaging movies. Often, after performing non-rigid or rigid registration, the output movie contains one or more bright artifacts that do not correspond to any labeled cell structure. These artifacts usually consist of a bright 2x2 pixel square with more bright spots radiating out from this central spot in all 4 directions (see in lower right of attached image). I am running normcorre on 256x256 movies with 400+ frames, with the following parameters, all others being default:

options_nonrigid = NoRMCorreSetParms('d1',size(Y,1),'d2',size(Y,2),'grid_size',[64,64],'overlap_pre',[16,16],'mot_uf',4,'max_shift',[20,20],'max_dev',[8,8],'shifts_method','FFT');

I have tried changing patch and overlap size slightly, but the artifact remains. Using cubic or linear shifts reduces the intensity of the artifact, but it still remains. Any ideas of parameters to change to prevent this?

testImage

Videos after applying non-rigid correction does not differ from Raw Video

Hi, @epnev

I applied this NoRMCorre at a summer school, where we have access to a zebrafish data. The fish tail moves in a way such that partial field of view is compressing. I tried different ways to create patches (either set it to 4x3, or 8x8) for non-rigid correction in NoRMCorre, but the video looks almost the same between raw video and the registered video.. All the other parameters are used defaults as provided by demo.

Here I am attaching Google Slides where the video can be viewed: https://docs.google.com/presentation/d/1-BgCStw3yRDxCDEbarVGeDKUJShkI6FrXF06cKek0No/edit?usp=sharing

I don't understand why registered images look almost the same as the raw images. Should I set a lot more patches? Thanks a lot.

Motion_metrics giving different results for same data set

Hello,

We are using NormCorre and are checking our results on different computers. Each time we use a different computer with the same exact data set we are getting different results. We have identified the issue as a problem with the corr function.
According to our debugging, the corr(X,Y) function gives very different results when you loop through each column of the matrix i.e. call the corr function for each column and calculate corr(X(:,i), Y) at each column of X, versus when you just call the corr function once on the entire X matrix and Y.

We believe that this problem is what is leading to the different results for the same data set. We also believe that this problem is present in MATLAB versions of 2017a and previous versions. 2017b and beyond seem to be okay.

Have you experienced this problem and are there any solutions? Or did you already know this about the corr function and specifically chose to call it once on the entire matrix instead of looping through?

Thank you very much for you help!

Ginny
Richard Axel Laboratory

Parallel processing errors when reading large bin_widths

Hello,
I was wondering if you have tried to test the upper limit of how much you can increase bin_widths?

I was able to do motion correction on a 512x512x80000 .mat file using bin_widths of 10,000. I usually run this on a 40-core processor with 500G RAM. It takes about 2-3 hours with 12 parallel workers, which is called in the beginning with the gcp command and I do not enable memory mapping and also point to the path of file (without loading input .mat file into memory).

In general, the output seems motion corrected, however my videos have significant slow z-drift throughout. When I attempted to use bin_widths of 30,000, I run into several problems:

  1. If I monitor the progress of the script, it appears to call call parpool several times (it calls 12 in the beginning and in the middle it also calls again, sometimes twice more), however the process is extremely slow and towards the end only a few cores seems to have a >0% load, even though the script technically called for more.

  2. If I left the process run overnight, I get the following error:

Offset 4.0e-01 pixels due to bidirectional scanning detected.
Registering the first 10000 frames just to obtain a good template......done.
Template initialization complete. Now registering all the frames with new template.
The file was saved successfully. Elapsed time : 1140.249 s.
30000 out of 82521 frames registered, iteration 1 out of 1..Starting parallel pool (parpool) using the 'local' profile ...
connected to 12 workers.
Starting parallel pool (parpool) using the 'local' profile ...

Error using parfeval (line 58)
Parallel pool failed to start with the following error. For more detailed information, validate the profile 'local' in the Cluster Profile Manager.

Error in normcorre_batch_even (line 255)
future_results(i) = parfeval(@register_frame, 2, Yt,fftTempMat,fftTemp,patches,options);

Error in twostep_moco_applyshifts_clusterMAT (line 102)
[~,shifts,template] = normcorre_batch_even(fullname_R,options_mc,template);

Caused by:
Error using parallel.internal.pool.InteractiveClient>iThrowWithCause (line 676)
Failed to initialize the interactive session.
Error using parallel.internal.pool.InteractiveClient>iThrowIfBadParallelJobStatus (line 790)
The interactive communicating job failed with no message.

Is it possible that there is no upperlimit to parpool being called and so it keeps calling it until it runs out and errors? Any tips would be greatly appreciated. Or if there is a better way to deal with drift that would be great too.

Thank you!

read_file avi lost frames

The definition of v by videoreader needs to be repeated after the first readFrame command, otherwise the inital frame is lost. This only seems to be the case for avi files.

An error appeared when NoRMCorre ran on Matlab.

In NoRMCorre,when I selected a local tif file(transformed by a bmp file),an error appeared on function imwarp:
错误使用 imwarp
需要的 tform 应为以下类型之一:

images.geotrans.internal.GeometricTransformation

但其类型是 double。Meaning that I use the imwrap incorrectly,the tform should be this type:images.geotrans.internal.GeometricTransformation.But actually it was double.

I don't konw how to solve this problem,so ask for help and reply.Thank you!

sizY not specified when loading a tiff via filename

I was trying to run NoRMCorre with the filename of a tiff as the input argument Y.
[M1,shifts1,template1] = normcorre(name,options_rigid)

turns out it grabs the info for the tiff (lines 24-26 of normcorre)
tiffInfo = imfinfo(Y);
filetype = 'tif';
T = length(tiffInfo);
but did not specify the variable sizY and ran into an error at line 49 of normcorre
nd = length(sizY)-1; % determine whether imaging is 2d or 3d

Registration of dual channels.

Thanks for a great package! Works great, and quicker than anything else I tried.

Would it be straight forward to apply shifts from one set of images onto another, e.g if we do dual color imaging?

Possible bug with tiff files.

I am running the following code:
`name = '/Users/eivinhen/Desktop/imreg_test_reg.tif';
Y = name;
iminfo = imfinfo(Y);

options_nonrigid = NoRMCorreSetParms('d1',iminfo(1).Height,'d2',iminfo(1).Width,'grid_size',[64,64],'mot_uf',4,'bin_width',50,'max_shift',25,'max_dev',20);
[M2,shifts2,template2] = normcorre_batch(Y,options_nonrigid);`

And it produces an error:
`Subscripted assignment dimension mismatch.

Error in cell2mat_ov (line 23)
X(xx_s(i):xx_f(i),yy_s(j):yy_f(j),zz_s(k):zz_f(k)) = ...

Error in normcorre_batch (line 306)
Mf{ii} =
cell2mat_ov(M_fin,xx_us,xx_uf,yy_us,yy_uf,zz_us,zz_uf,overlap_post,sizY)
- add_value;

Error in demo (line 46)
[M2,shifts2,template2] = normcorre_batch(Y,options_nonrigid);`

How does normcorre deal with edges

For example, I provide a template for the normcorre function, and this template has a different crop than the stack that I want to align. Will this cause a poor alignment or alignment artifacts, or does the method ignore edges like that?

Zigzag artefacts in motion corrected data

Hi there. We recently came across an issue where NoRMCorre corrupts the output data in odd ways. We have used the library successfully for a long time, and experience the problem only with data from a new 2-photon microscope (Scanimage). The issue appears in some recordings, but not all. We are wondering if this is a known issue, and in any case if there is a fix?

Issue:

Tried:

  • Shifting values to above 0, and ensuring format is uint16
  • Removing extreme values (helps, but zig-zag artefact is still present)

Vh Knut Åbjørsbråten

problem when mod(stackSize, binwidth) = 1

I was aligning a stack containing 701 frames using a bin width of 50 and got an error on alignment of the last bin:

Error using mat2cell (line 80)
Number of input vector arguments, 3, does not match the input matrix's number of dimensions, 2.

Error in normcorre_batch (line 266)
if nd == 2; Ytc = mat2cell(Ytm,d1,d2,ones(1,size(Ytm,ndims(Ytm)))); end

Error in imregSession (line 131)
[M1, shifts, ~] = normcorre_batch(Y1, options_nonrigid);

Error in runPipelineTMP (line 152)
imregSession(sessionID)

I ran it again using bin width of 40 and then it worked fine. Have not reproduced it with other combinations of stack sizes/bin widths.

NoRMCorre Usecase discussion

I am looking to adopt NoRMCorre for a 4 channel dataset consisting of 3d stacks of images obtained along the axial plane of mice brain.So, the Field of View along z changes (thickness of brain slice=1 micron). Can i use NoRMCorre to correct motion artifacts in each image and correct alignment issues in every multichannel 3d stack ?
The dataset is 1024*1024 and z=44. We are imaging dendrites and looking to reconstruct the 3d view of dendrites from the 2d slices. There are no neurons.
Is NoRMCorre useful for my case?
Attached is a sample 3d stack of one of the channels.

Substack (1-8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.