Julie-Fabre / bombcell

Automated quality control, curation and neuron classification of spike-sorted electrophysiology data
GNU General Public License v3.0
103 stars 26 forks source link

Questions about SNR #125

Closed claudio1212 closed 2 weeks ago

claudio1212 commented 1 month ago

I recently started to use this very good matlab package, but I have some doubts regarding SNR and the way it is calculated. I saw that the minimum "good" value is of 0.1, that is quite low at least considering those I have always seen that are usually in the order of 3,4,5 of above, so I have the doubt that this parameter is calculated in a different way than what is classically seen, is it true? And in that case, do you have any suggestions on ranges of values that can be considered to be acceptable (other than "higher than 0.1")?

Julie-Fabre commented 1 month ago

Hi Claudio,

Thanks for your message! Yes, you're absolutely right, the value of 0.1 is extremely permissive. Setting it to something like 1.5 (or at least anything >1) might make more sense. This value also depends a lot on your data and the amount of noise in your setup. I would have a look at a few of your units' SNR value in the GUI and then set a threshold that makes the most sense for your data. This holds for most thresholds - the best thing to do would be, in a few datasets, to look at units and their quality metric values in order to get an idea of what values make the most sense for you given your recording setup, and the intended use for the data. Feel free to send images over if you want an opinion on how to set the thresholds!

claudio1212 commented 1 month ago

Ok, I'll upload two examples. You can see that there are good units, in terms of waveforms shape, amplitude ecc. that are associated with very low SNR values, so I guess that it's very true that I have to consider my data in order to evaluate these values, even though I still expected higher ones from examples like these, So I ask you, do you think it is normal or there could be a problem with my data?

unit1 unit2

Julie-Fabre commented 1 month ago

Hi Claudio,

Thanks for sending those pictures. Those SNR values look a little low which makes me think there might a problem of how bombcell is extracting your waveforms in your data (specifically, the number of channels and/or number of sync channels might be wrongly set). Could you load up a few raw waveforms extracted by bombcell, either manually or by loading up bombcell's gui (e.g. run

 bc.load.loadMetricsForGUI;
 bc.viz.unitQualityGUI(memMapData, ephysData, qMetric, forGUI, rawWaveforms, param, probeLocation, unitType, 0); 

after extracting or loading your quality metrics)

claudio1212 commented 1 month ago

Ok, now I'm finally getting good results, by setting the nChannels to the actual real value (it was set to 385 but I have 32). It was actually a problem related to how raw waveforms were loaded up, and I saw in the GUI that they were very flat (I did not check it before, sorry). Now SNR values appear to be generally higher and in line with the actual units "appearance". Thank you very much for your help!

Julie-Fabre commented 1 month ago

Excellent news! Glad we figured it out! This has been an issue for several users already and I can see the FAQs aren't helping much, so I've made the example scripts clearer and hopefully no one else falls into this error. What Let me know if you run into any other issues or have some questions.

claudio1212 commented 1 month ago

Hi again, even though SNR appear to be higher the waveforms in the GUI are still kind of strange (in phy I can see them and they appear to be fine) example1

I'll upload an example to explain what I'm referring to here. Note that when I run the pipeline script I get the following warning:

Insufficient channels with similar X position for fitting.

In bc.qm.helpers.getSpatialDecay (line 29) In bc.qm.waveformShape (line 170) In bc.qm.runAllQualityMetrics (line 160)

Warning: could not plot global plots

In bc.qm.plotGlobalQualityMetric (line 273) In bc.qm.runAllQualityMetrics (line 201)

what could be the problem here? Could it be some kind of plotting error, given that when seeing the results in phy and using the quality metrics extracted from bombcell all seems fine, and they seem to be coherent with the actual waveform shape? For example, the image I'm uploading refers to a multi unit, and looking at phy it actually has a multi unit waveform while in the gui it appears to be a non somatic one, for this reason I think it could be some errors in the plotting phase

Julie-Fabre commented 1 month ago

That looks very odd, I think your format of data might not be well handled by bombcell. What probe are you using to record your data? Would you be able to send me your channel_positions.npy and templates.npy file so I can take a look and adjust bombcell?

claudio1212 commented 1 month ago

I'm using an FMA probe, that has 32 channels with different x and y locations. I'll send you the files. files.zip I saw looking at the auxiliary bc scripts that, for example, the matlab file used for the final GUI (in which the results for each unit are plotted) required different channels to be plotted at once (I guess because neuropixel probes are composed of nearly located channels), in my case just one would be cool and thus, putting nChanstoplot (or something like that) to 1 has at least adjusted the visualization of the template waveform, while the raw waveform and raw traces still look odd.

Julie-Fabre commented 1 month ago

That's weird, I can see that the GUI is plotting the wrong template (it looks like it is plotting the template of unit # 31 in the example above instead of #30 but I'm not sure why that would be the case). Could you additionally send me your spike_templates.npy, spike_times.npy, amplitudes.npy and whitening_mat_inv.npy files so I can rerun bombcell on my end?

claudio1212 commented 1 month ago

Ok, sure! files.zip

claudio1212 commented 1 month ago

I forgot to mention that I used kilosort version 4 and the recording was made with open ephys system

Julie-Fabre commented 1 month ago

I don't get the same error on my side when I quickly run bombcell. image

Could you pull the latest version of bombcell again (or copy paste in the GUI code) to make sure a typo hasn't crept it on your version of the code?

To fix the raw waveform display, could you also send me templates._bc_rawWaveforms.npy that bombcell outputs?

As a heads up, I made some slight changes recently to define somatic vs non-somatic units in a more robust way (specifically bombcell now calculates the ratio between the peak before the trough and peak after the trough). A few more of your units might be classified as non-somatic. If you aren't happy with that, lower the parameter value param.firstPeakRatio.

claudio1212 commented 1 month ago

Sorry but I couldn't proceed with trying what you asked yesterday, I tried re-downloading the package and now the mean raw waveform and the template waveforms are very similar to each other, thus it seems resolved, only the raw unwhitened data seems different to what presented in the examples that are here (I don't see the waveforms in blue in the raw trace, for example , and in general it is still similar to what i showed in the latter examples), so I will still send the bc_waveforms template for you to try seeing if everything is okay or not

files.zip

Julie-Fabre commented 1 month ago

Hi,

Sorry for my late reply. Those raw waveforms look alright to me, but I forgot to ask for one file to double check:

claudio1212 commented 1 month ago

No problem, thank you very much for your help! files.zip

Julie-Fabre commented 1 month ago

Thanks a lot! Ok, the raw waveforms look good to me but your peakChannels file seems to have some issues (it is the wrong size and doesn't always correctly refer to each units peak channel). That's the first time I have seen that and I cannot make sense of it. I was hoping to avoid this, is it feasible for you to also send your raw data? I don't think github will allow such large files, but by gmail (via google drive) will work: julie.mfabre@gmail.com. If you don't have google drive, shoot me an email and I'll send you a link to my drive where you can then upload the data.

claudio1212 commented 1 month ago

Thank you very much, I'll send you the raw data!

Julie-Fabre commented 1 month ago

Great, received the raw data and I just re-extracted the waveforms. They all look good to me and now the peak channels have the correct values (phew!). I'm not sure why your file was different but maybe it had to do with running bombcell several times with different parameters. I would suggest you rerun bombcell with param.reextractRaw = 1;

A few other things:

claudio1212 commented 1 month ago

Perfect, I will try again tomorrow following your suggestions, but I guess it's all solved now. Thank you again for you help, sorry to have caused some trouble!

Julie-Fabre commented 2 weeks ago

Closing this issue for now. Please reopen it if you run into more issues!