Author Topic: Calculate spatial firing rate  (Read 4634 times)

chalspark

  • Newbie
  • *
  • Karma: +2/-0
    • View Profile
Calculate spatial firing rate
« on: November 01, 2016, 09:22:03 AM »
Hello. I always thanks miniscope for sharing great method for neuroscience.
I tried to analyze sample data(G200) before I get mine. In Github, there are no exist the function of msLineatSFR(it maybe msLinearSFR). I think it was missed or deleted.
May i get the script file for calculating spatial firing rate?
Thanks.


Daniel Aharoni

  • Administrator
  • Full Member
  • *****
  • Karma: +15/-0
    • View Profile
Re: Calculate spatial firing rate
« Reply #1 on: November 01, 2016, 04:13:04 PM »
Hi Chalspark,
I wrote msLinearSFR to handle a specific experiment and never got around to make it a more general function. When I have time I will update it and upload it to GitHub.

That being said, you should already have almost everything you need to generate spatial firing rate maps if you have ran through the rest of the analysis functions. You need to break up the track into spatial bins and use something like hist, accumarray, or histcn to count the occupancy of the animal and firing of each segment in each bin. Maybe do some filtering/smoothing then divide the binned firing by the occupancy. Applying a low speed threshold is also a good idea.

chalspark

  • Newbie
  • *
  • Karma: +2/-0
    • View Profile
Re: Calculate spatial firing rate
« Reply #2 on: November 02, 2016, 06:20:17 AM »
Hi Daniel Aharoni,
Thanks for quick reply. Yes. I already have everything to generate spatial firing rate maps, like you said(behav.position and ms.firing). There are few questions to generate rate map.

I got the value of x-position(horizontal) from behav.position(:,1) and calculated occupancy of each bin(histcn/sampling_rate (sec)). In this step, I was expect that sampling rate is 30fps. It wasn't, because maxFramesperFile was 1000. Also, ms.maxFramesperFile was 1000. It means that down sampling 30fps to 20fps.

Q1. Should I use maxFramesperFile to 1000? Why limit maxFramesperFile? Down sampling doesn't matter to calculate occupancy. I am just curious.

I got the firing data from ms.firing(:,segments). When I generate rate map(or place map) with spikes data from single unit recording, spike time was accurate. I find peak point using findpeaks() with threshold. In your paper(Nature,2016), 'Place fields were calculated by deconvolving calcium dF/F traces with an exponential to extract approximate spike times.'

Q2. How do i extract approximate spike time from data? Is one peak of firing one spike?

Finally, I try to run script with all msCam data from G200(behavior data limited 1 to 14). I think data was split because parallel processing. When running msBatchSegment, MATLAB has memory problem. Physical memory was enough when i was monitor memory information.

Q3. How do i solve memory problem?

I'm newbie in optical recording. Sometimes, I asking stupid question. Please forgive me. Your answer will help to generate rate map as you draw. It will be great experience to analyze own data.

Thanks.
« Last Edit: November 02, 2016, 08:41:42 AM by chalspark »

Daniel Aharoni

  • Administrator
  • Full Member
  • *****
  • Karma: +15/-0
    • View Profile
Re: Calculate spatial firing rate
« Reply #3 on: November 02, 2016, 11:07:42 PM »
Hi chalspark,
These are all great questions.
Quote
Q1. Should I use maxFramesperFile to 1000? Why limit maxFramesperFile? Down sampling doesn't matter to calculate occupancy. I am just curious.
maxFrameperFile can pretty much be ignored and means something different than you are thinking. maxFrameperFile just defines the number of frames the DAQ software saves into each .avi file before another .avi is created. This has nothing to do with FPS and is just used to make the individual .avi files smaller and easier to manage. The first thing the MATLAB scripts do is find all these files and merge them into a single data set structure so all the they can be treated as 1 file to the rest of MATLAB analysis. While the Miniscope FPS should pretty much be rock solid on 30FPS the behavioral camera may vary widely depending on what camera is used. You will need to look at the behavioral camera's timestamps in timestamp.dat or, preferably, look at behav.time. This will tell you the time in milliseconds each frame was acquired. ms.time and behav.time will allow you to interpolate the behavioral positions with the dF/F traces. Consider something like ms.pos = interp1(behav.time,ms.time,behav.positions(:,1));
Quote
Q2. How do i extract approximate spike time from data? Is one peak of firing one spike?
ms.trace holds the extracted dF/F signal from each identified segment. Due to the slow decay of fluorescent calcium transients you generally need to do an additional processing set before the data is usable. That being said, there isn't a clear cut answer for what processing should be done here. A few options are:
  • Detect the rise of each calcium transient and say that that is the start of a burst of activity. This can be done by taking the derivative (diff) of the dF/F trace and detecting its peaks. You could also just detect the raw dF/F peaks which is pretty close to detecting the rise of the event.
  • Apply a deconvolution algorithm to remove the slow decay of the calcium signal. The output of the deconvolution will be an approximation of the cells actual activity (scaled by an unknown constant). This can be done using a direct deconvolution algorithm like ones provided by MATLAB (look at deconvlucy) or a more complicated approach like the one implemented here, https://github.com/jovo/fast-oopsi/blob/master/manuscript/fast_oopsi.pdf. Our function msExtractFiring attempts to implement Fast Oopsi on ms.trace and outputs ms.firing which is the non-negative deconvolved dF/F traces. The parameter values you choose make a huge difference in the deconvolution so use at your own risk.
  • I'm sure there are a bunch of other approaches to pulling out activity measures from dF/F traces as well.
Quote
Q3. How do i solve memory problem?
When MATLAB has an issue with 'memory' it is due to your RAM getting completely filled up. All of the Miniscope MATLAB functions, except for the segmentation algorithm, read frames directly from the hard drive and save only small chucks of data to RAM, requiring only a small amount of RAM. The segmentation algorithm on the other hand reads in a temporally down sampled version of the entire recording session into RAM. When running the batch scripts this uses up even more RAM since it is running the segmentation on multiple data sets simultaneously. You will need to figure out how large and how many data session your can run at the down sampled rate you are trying to achieve (usually down sample = 5 for 30FPS recordings work well).