V4 Miniscope Noise Removal and Compression Converter Jupyter Notebook

1,108 views
Skip to first unread message

Daniel Aharoni

unread,
Sep 18, 2020, 11:51:44 PM9/18/20
to Miniscope
Hi Everyone,
We just added a Jupyter Notebook to the V4 Miniscope Github repo that will walk you through the process of cleaning up the two main sources of noise in V4 Miniscope imaging data (it can be found at: https://github.com/Aharoni-Lab/Miniscope-v4/tree/master/Miniscope-v4-Denoising-Notebook). At the end of the Notebook you will be able to save the results back to .avi files and choose which compression codec you want to use.

Below I will outline the main sources of noise and how the Notebook deals with them but I also wanted to mention that recently we found a hardware solution that removes this noise from ever showing up in the first place. We are working on a simple modification anyone can do to their V4 Miniscopes to retro fit them with this fix. More to come in the next week or two.
  • Depending on your V4 Miniscope and the level of signal and Gain you use, you will have varying levels of noise in your recordings. The two types of noise in your imaging are:
    • Thin horizontal bands that will slowly scan up or down throughout the recording.
    • Fluctuations of around 3 to 10Hz of the entire field-of-view's intensity. This usually are no more than +/- 5 to 8% of the pixel's value. 
  • Luckily both these types of noise have features far away from neural dynamics which makes it relatively easy to detect and remove.
  • For the thin horizontal stripes, we use a 2D FFT spatial frequency filter to trim away the spatial frequencies relating to these stripes and then take the 2D inverse FFT to reconstruct the frame. This is all visualized in the Notebook.
  • For the few Hz fluctuation in intensity across time, we first measure the mean intensity per frame across all frames. We then build a lowpass butterworth filter to separate this noise from the rest of the data. Finally, we scale the pixel values on each from by the difference in raw and filtered mean intensity per frame.
  • Finally, the Notebook can save the results back into .avi files for further processing. You can select a different compression codec here than what you used in the Miniscope Software as I know some people have been asking about how to switch between codecs.
Below is a screenshot of the final output display of the Notebook:
Denoising Notebook Example.PNG

Let me know if you have any questions and I would love to get your feedback!

Zeeshan Haqqee

unread,
Sep 21, 2020, 2:22:36 AM9/21/20
to Miniscope
Hi Daniel,

Many thanks for putting this together. I've begun trying it on a couple recordings.

One thing I'm a bit confused about is selecting the appropriate filter order (butterOrder). I notice between orders 1-31, it looks essentially the same by eye:

1-31.PNG
For order 32, it looks a bit different, as though it is capturing more of the (positive) high frequency portion?

32.PNG
For order 33, it seems to only capture something at the very beginning, then nothing afterwards. And for 34 onward, none of the data gets filtered.
33.PNG34.PNG

How would you interpret these graphs, and which order level would you recommend using? I went with the default (order = 6) but I noticed the cleaned videos still had a lot of noticeable flickering. So perhaps order 32 would make more sense to use?

Thank you,
Zeeshan

cke...@gmail.com

unread,
Sep 21, 2020, 11:00:12 AM9/21/20
to Miniscope
IIR filters (like the butterworth) should only be used with small orders (like 4 - 9 is already making me nervous). Filters like the butterworth have an analytical formula for the filter coefficients. As you increase the order, the floating point approximation to the real-valued coefficients differ enough from their proper values such that the filter become unstable and produces incorrect results. If you want to create a very steep filter band you should really use an FIR filter. It will take longer to compute, but the results will be right.

Daniel Aharoni

unread,
Sep 21, 2020, 2:09:31 PM9/21/20
to Miniscope
Thanks for posting Zeeshan. 

As Caleb said, you generally don't want to use orders higher than 9 (and even that is very high) for butterworth filters. The higher the filter order the steeper the transition in frequency between signals that are passed through the filter and those that are blocked. This Notebook plots out 3 options for filter order to show you how order affects the filter shape:
LP_Filt_order.PNG
In our case, we really don't need a sharp cutoff so stick to low orders. This will also run faster and produce less artifacts. You can adjust the cutoff frequency a bit and see how it affects your data as well.

When you apply the filter, zoom into the generated plot to see how the filtered data compares to the raw data:
filtExample.PNG
You want to see that the Filtered Data (blue) follows the overall trend of the Raw Data (black) but has the higher frequency fluctuations removed.

Hope this helps!

Federico Sangiuliano

unread,
Sep 21, 2020, 2:30:12 PM9/21/20
to Miniscope
Just to add to the discussion, the correct way of increasing the attenuation slope is by changing the filter to cheby1,cheby2 o elliptic filters.
Message has been deleted

elham ghanbarian

unread,
Oct 5, 2020, 5:27:49 PM10/5/20
to Miniscope
Hi Daniel, 

You said " We are working on a simple modification anyone can do to their V4 Miniscopes to retro fit them with this fix. More to come in the next week or two. "  I am wondering if this modification would be possible to do after finishing the assembly. Do we need to disassemble the scopes that we have made? 

Thanks, 
Elham 

Daniel Aharoni

unread,
Oct 5, 2020, 6:41:32 PM10/5/20
to Miniscope
Hi Elham,
This modification should be able to be made on a fully assembled V4 Miniscope without disassembly. We should have more information coming in the next week or two.

Fa Peng

unread,
Dec 7, 2020, 12:45:40 AM12/7/20
to Miniscope
Hi everyone,
New version of V4 Miniscope had been updated. Does it solve the problem of  electronic noise, by adding isolated NCP163? Has anyone tested the solution?
Best wishes!
Fa Peng

Federico Sangiuliano

unread,
Dec 7, 2020, 1:14:40 AM12/7/20
to Miniscope
Hello Fa Peng. We tested this solution extensively in the lab and works great. What would you like to know about it?

Thank you

Federico

Fa Peng

unread,
Dec 7, 2020, 6:15:51 AM12/7/20
to Miniscope
Hi  Federico,
 Thanks for the reply. New version of V4_40 Miniscope needs to  make the new circuit board, we want  to confirm the  current solution of V 4_40  miniscope. 
Do you have any other hardware solution for V4_34 Miniscope's  electronic noise , which has no NCP163? Our miniscope are V4_34, we don't want to waste the hardware.
Thaks,
Fa Peng

Federico Sangiuliano

unread,
Dec 7, 2020, 6:30:25 AM12/7/20
to Miniscope
With the v4.34 you have either the software option as detailed by Daniel or a hardware solution with a hot-fix PCB with the circuitry of the NCP163. The later will be available on OEPS but I can't tell when it's going to be available for purchase.

Thank you

Federico

Fa Peng

unread,
Dec 10, 2020, 10:34:11 PM12/10/20
to Miniscope
Thank you very much. We'll try the hardware solution with the v4.34.  
Best wishes!
Fa Peng

Katherine McCormack

unread,
Jun 7, 2021, 3:13:05 PM6/7/21
to Miniscope
I am having trouble getting the code to run successfully. The code throws an error after checking if vignetteCreated status during this "rows, cols = frame.shape[:2]" command. The error is AttributeError: 'NoneType' object has no attribute 'shape'. Would you know how to correct for this error?

Daniel Aharoni

unread,
Jun 7, 2021, 4:36:11 PM6/7/21
to Miniscope
Hi,
Do you have all the necessary Python modules installed? Sounds like you might be missing numpy?

Message has been deleted

Katherine McCormack

unread,
Jun 28, 2021, 1:16:25 PM6/28/21
to Miniscope
Hi, 

I have been using the denoising script quite a bit lately and there are a few videos that all have the report the same error. I have been skipping those videos, but I wanted to see if you knew what was the cause. 

Here is the error message I get:  

  File "C:\Users\Katherine\Nehs Lab Working Folder\experiment_analysis\denoising_script.py", line 243, in <module>
    meanFiltered = filtfilt(b,a,meanFrame)

  File "C:\Users\Katherine\miniconda3\envs\caimanenv\lib\site-packages\scipy\signal\signaltools.py", line 3894, in filtfilt
    ntaps=max(len(a), len(b)))

  File "C:\Users\Katherine\miniconda3\envs\caimanenv\lib\site-packages\scipy\signal\signaltools.py", line 3945, in _validate_pad
    "than padlen, which is %d." % edge)

  ValueError: The length of the input vector x must be greater than padlen, which is 21.

It goes through all the portions of the script, so I can't really see where it is going wrong. 
If you have any ideas about what is going wrong, let me know. 

Thanks

On Friday, September 18, 2020 at 11:51:44 PM UTC-4 dbah...@gmail.com wrote:

Samara Miller

unread,
Jul 8, 2022, 9:31:00 PM7/8/22
to Miniscope
Hi all, 

I realize this is an older thread but hoping someone can help...

I am getting sporadic but significant horizontal noise lines at the beginning and end of recordings during ~15 min behavior trials. I realize this is a hardware issue but am wondering if anyone has had success with the post-processing denoising code? I have run several videos through but the noise remains. I am attaching a denoised video file, as well as some images showing the noise. 

1) Are there particular filter settings I can adjust in the denoising jupyter notebook code that can help fix this?
2) Is this noise too significant to filter out? If so, does anyone have alternative suggestions for how to analyze these recordings using minian? The "glow removal" step applies the horizontal lines across the whole video, so even a single frame with horizontal lines during the whole recording will spoil the whole analysis (see below). Screen Shot 2022-07-08 at 6.20.02 PM.png

Help please! Thanks so much.
Samara
220.jpg221.jpg
denoised0.m4v

Daniel Aharoni

unread,
Aug 15, 2022, 3:14:22 PM8/15/22
to Miniscope
HI.

Take a look at this forum thread, https://groups.google.com/g/miniscope/c/pr7PMiDbK68/m/z3lokJu3EAAJ. Usually the source of this horizontal striping is a slightly unstable cable, connector, or commutator. This happens so inconsistently in our hands that I have never been able to track down a fix for it other than redoing all cabling, connectors, and switching commutators.

Anna Teruel

unread,
Oct 20, 2023, 11:19:56 AM10/20/23
to Miniscope
Hey everyone!
Sorry for reopening this conversation. I have some videos with horizontal strides and I am trying to use the jupyter notebook you wrote to clean them. However, I cannot see any difference in the output I obtain and I would like to get some feedback regarding the parameters I should use on the notebook in order to clean the noise.

Please, find attached the raw video and the denoised video. I explain a bit about what I have done so far.

Among the parameters to be edited, I am using goodRadius = 1000, notchHafWidth = 31; centerHalfHeightToLeave = 50, which are a bit different from the default values in the notebook. Find attached a figure showing the 2D FFT. When I display the filtered vs original videos I see no difference between them. I think I don't really understand what notchHalfWidth and centerHalfHeightToLeave are doing.

Then, after calculating the mean fluorescence per frame, I chose a butterOrder=6 because I read in this discussion that it is not convenient to take a high value.

Perhaps I should try to change the filter to cheby1,cheby2 and elliptic filters, as Federico suggested. Has anyone use it and could give some feedback on the performance?

As seen in the videos, I have many cells and I would like to clean these videos for the analysis. I am using Minian and the thing is that I get a lot of noise from these strides, a lot of false seeds.

Thank you all for your time!
Anna

PD: I converted the video to mp4 because then it was lighter.
Figure 1.png
Figure3.png
denoised0.mp4
0.mp4
Reply all
Reply to author
Forward
0 new messages