catalystneuro / distributed-coding-matnwb

Converting the "Distributed coding" dataset to NWB using MATLAB
BSD 2-Clause "Simplified" License
1 stars 1 forks source link

Plot PSTH #10

Closed vigneswaran-chandrasekaran closed 3 years ago

vigneswaran-chandrasekaran commented 3 years ago

Function for plotting PSTH

Sample usecase:

nwb_file = nwbRead('nicklab~Subjects~Hench~2017-06-16~001~alf.nwb');
unit_id = 7;
before_time = -10
after_time = 5
group_by = 'response_choice';
num_bins = 50;
psth(nwb_file, unit_id, before_time, after_time, group_by, num_bins);

will give an output plot, psth

closes #6

bendichter commented 3 years ago

only have xlabel on last tile

bendichter commented 3 years ago

please add a docstring to the function

vigneswaran-chandrasekaran commented 3 years ago

Added some stylistic changes and used bin edges for histogram plotting to improve readability

The current version will give an output plot similar to,

psth

bendichter commented 3 years ago

@Vigneswaran-Chandrasekaran awesome! Is it ready for me to take a look?

vigneswaran-chandrasekaran commented 3 years ago

I've added Normal distribution plot but I am not sure that is the right way to do it! Other than the distribution part, I think now the PR is ready for your review :)

Now the plot along with the distribution subplot looks like, sample_plot

As the data points are close to uniform across, the bell curve is not very good looking

bendichter commented 3 years ago

@Vigneswaran-Chandrasekaran getting the smoothed curve is pretty tricky. You actually want to convolve a a gaussian over the average spike train for the trial group. If that's too tricky, I think a PSTH is fine, but this bottom graph isn't really right.

bendichter commented 3 years ago

Could you set the default number of bins to 30, and the default start and stop times to -0.5 and 1.0 seconds?

vigneswaran-chandrasekaran commented 3 years ago

Added a simple GUI interface to plot. The interface looks something similar to,

gui
bendichter commented 3 years ago

@Vigneswaran-Chandrasekaran this looks great! Would you be able to allow group_by to be optional, and if it is missing do not group the trials at all and have them all be black?

vigneswaran-chandrasekaran commented 3 years ago

@Vigneswaran-Chandrasekaran this looks great! Would you be able to allow group_by to be optional, and if it is missing do not group the trials at all and have them all be black?

Sure, currently the y axis is trials, in no group_by will the y axis be simply stacking of all group_by variables in all trials?

bendichter commented 3 years ago

@Vigneswaran-Chandrasekaran yes, that's right

vigneswaran-chandrasekaran commented 3 years ago

I applied Gaussian filter with sigma = 0.05 and got some satisfactory curves 😃 but I am not sure whether it is correct?

The main doubt, I have is should the time axis be, linspace(start_time, stop_time, num_bins) or linspace(start_time, stop_time, sampling_rate), currently I used mid-points of bin edges

untitled snap

bendichter commented 3 years ago

@Vigneswaran-Chandrasekaran this looks great!

vigneswaran-chandrasekaran commented 3 years ago

Thank you very much for your comments :) I made the changes and now the output figures look something similar to,

panel eg1

I made n_bins as equal to time points for applying Gaussian filter 🤔 and added a condition that n_bins should be more than 50 to plot Gaussians. Is this the right way? or I will have default (like equal to 1000) or get from the user?

bendichter commented 3 years ago

Please increase the number of timepoints for the Gaussian to 1000

vigneswaran-chandrasekaran commented 3 years ago

Thanks for your comment! I have made the timepoints as 1000

Some sample output plots,

Capture Capture1
bendichter commented 3 years ago

Can you make it so the standard deviation is expressed in seconds, not samples?

bendichter commented 3 years ago

Are you getting roughly the same values for the convolution compared to the histogram approach?

vigneswaran-chandrasekaran commented 3 years ago

Are you getting roughly the same values for the convolution compared to the histogram approach?

No, the values in the gaussian smoothed will be much lesser. I thought it is correct as histogram bins will be broader. Is that correct?

bendichter commented 3 years ago

If you are getting very different values then you are probably doing the gaussian blur incorrectly

vigneswaran-chandrasekaran commented 3 years ago

Thanks for your reply

The gaussian blur curve and histograms come closer as number of bins is closer to 1000 (gaussian blur's default time points)

bins_30

histogram with 30 bins 30_bins_hist

histogram with 500 bins 500_bins

histogram with 1000 bins 1k

Am I making any sense? If not kindly mention, I will try to check the gaussian smoothing function

bendichter commented 3 years ago

The average rate should not change when you change the bin width of our histogram. I think you are calculating firing rate incorrectly. Are you properly dividing by the bin width in seconds?

vigneswaran-chandrasekaran commented 3 years ago

What about if there are not many bins? Like what if there are 30 bins?

The firing rate is larger compared to the gaussian (the first figure in previous comment)

The average rate should not change when you change the bin width of our histogram. I think you are calculating firing rate incorrectly. Are you properly dividing by the bin width in seconds?

Yes, I divide the bin widths properly (linspace(before_time, after_time, 1000) for gaussian blur), I will check once where I am going wrong

vigneswaran-chandrasekaran commented 3 years ago

I modified the layout of the subplots and now the plots look something similar to,

sample1

sample2

Please let me know if any changes need to be done

bendichter commented 3 years ago

@Vigneswaran-Chandrasekaran can you please resolve the merge conflicts?

vigneswaran-chandrasekaran commented 3 years ago

I've resolved all the merge conflicts and it is ready for review