Figure_2.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. function fig = Figure_2( strNIXFolderPath, varargin )
  2. % Figure_2.m reproduces Figure 2 in the dataset publication
  3. % fig = Figure_2(strNIXFolderPath) reproduces Figure 2
  4. % strNIXFolderPath is the path of the folder with NIX files
  5. % fig is the figure handle
  6. % Default neuron number plotted is 30
  7. % fig = Figure_2(strNIXFolderPath,nNexampleNeuron) reproduces Figure 2 with the
  8. % example neuron nNexampleNeuron
  9. %
  10. % Add the toolbox 'gramm' to the MATLAB path
  11. % The toolbox can be downloaded from https://github.com/piermorel/gramm
  12. % Example neuron
  13. if(nargin>1)
  14. nExampleNeuron = varargin{1};
  15. else
  16. nExampleNeuron = 30;
  17. end
  18. SPIKES_All = [];
  19. % Load data for all neurons
  20. for nSubject = 1:9
  21. % File name
  22. strNIXFileName = sprintf('Data_Subject_%.2d_Session_01.h5',nSubject);
  23. % Read the NIX file
  24. f = nix.File([strNIXFolderPath,filesep,strNIXFileName],nix.FileMode.ReadOnly);
  25. % NIX data
  26. block = f.blocks{1};
  27. % Multitags for spike times
  28. group_MultiTagsSpikes = block.openGroup('Spike times multitags');
  29. multiTags_SpikeTimes = group_MultiTagsSpikes.multiTags;
  30. % If there are no neurons, continue
  31. if(isempty(multiTags_SpikeTimes))
  32. continue;
  33. end
  34. % List of neurons and trials
  35. % Format for the name is
  36. % 'Multitag_Spike_Times_Unit_<neuron number>_<micro wire name>_Trial_<trial number>'
  37. strSpikeTimeLabels = cellfun(@(x) x.name,multiTags_SpikeTimes,'UniformOutput',0);
  38. strSpikeTimeLabels = cellfun(@(x) strsplit(x,'_'),strSpikeTimeLabels,'UniformOutput',0);
  39. nNeuronsTrialsList = [cell2mat(cellfun(@(x) str2double(x{5}),strSpikeTimeLabels,'UniformOutput',0)),...
  40. cell2mat(cellfun(@(x) str2double(x{9}),strSpikeTimeLabels,'UniformOutput',0))];
  41. ranNeurons = unique(nNeuronsTrialsList(:,1));
  42. ranTrials = unique(nNeuronsTrialsList(:,2));
  43. % Load spike times and waveforms for each neuron
  44. SPIKES = [];
  45. for iNeuron = 1:length(ranNeurons)
  46. nNeuron = ranNeurons(iNeuron);
  47. spikes = [];
  48. for iTrial = 1:length(ranTrials)
  49. nTrial = ranTrials(iTrial);
  50. nDataArray = (nNeuronsTrialsList(:,1)==nNeuron)&(nNeuronsTrialsList(:,2)==nTrial);
  51. % Data array for the selected neuron and trial
  52. dataArray = multiTags_SpikeTimes{nDataArray}.openPositions;
  53. % Read spike times
  54. spike_times = dataArray.readAllData';
  55. spikes = [spikes;[spike_times,nTrial*ones(length(spike_times),1)]];
  56. end
  57. % Store spike times and trial numbers
  58. SPIKES(iNeuron).spikes = spikes;
  59. % Read waveform for the selected neuron
  60. dataArray_Waveform = multiTags_SpikeTimes{nDataArray}.features{1}.openData;
  61. % Store waveforms
  62. SPIKES(iNeuron).waveform = dataArray_Waveform.readAllData';
  63. end
  64. SPIKES_All = [SPIKES_All,SPIKES];
  65. end
  66. %% Firing rates
  67. FiringRatesHz = [];
  68. tTrialDuration = 26; % seconds
  69. for iNeuron = 1:length(SPIKES_All)
  70. nNumberOfTrials = max(SPIKES_All(iNeuron).spikes(:,2));
  71. FiringRatesHz = [FiringRatesHz,length(SPIKES_All(iNeuron).spikes(:,1))/(tTrialDuration*nNumberOfTrials)];
  72. end
  73. %% ISI
  74. isis = [];
  75. for iNeuron = 1:length(SPIKES_All)
  76. isis_ = [];
  77. for iTrial = 1:max(SPIKES_All(iNeuron).spikes(:,2))
  78. isis_ = [isis_;diff(sort(SPIKES_All(iNeuron).spikes(SPIKES_All(iNeuron).spikes(:,2)==iTrial,1))*1000)];
  79. end
  80. isis = [isis;(length(isis_(isis_<3))/length(isis_))*100];
  81. end
  82. %% SNR
  83. snr = [];
  84. waveform = [];
  85. for iNeuron = 1:length(SPIKES_All)
  86. snr_0 = max(abs(SPIKES_All(iNeuron).waveform(:,1)))/mean(SPIKES_All(iNeuron).waveform(:,2));
  87. snr = [snr snr_0];
  88. waveform = [waveform;SPIKES_All(iNeuron).waveform(:,1)'];
  89. end
  90. FR = SPIKES_All(nExampleNeuron).spikes;
  91. %% Firing rate for a single neuron
  92. c = [];
  93. fs = -2;
  94. ls = 24;
  95. binSize = 0.1;
  96. ranTrials = {};
  97. for i = 1:max(FR(:,2))
  98. for iTrial = [0 1] % 1 - red, 0 - blue
  99. if mod(i,2) == iTrial % odd - neutral, even - aversiv
  100. spikeTimes = FR(FR(:,2) == i,1);
  101. ranTrials{i} = spikeTimes;
  102. c = [c iTrial];
  103. temp = histc(spikeTimes,fs:binSize:ls);
  104. binned{i} = smooth(temp(1:end-1)/binSize,10);
  105. % we set here bin centers
  106. bins{i} = (fs+binSize/2):binSize:(ls-binSize/2);
  107. end
  108. end
  109. end
  110. %% Plot Figure 2
  111. clear g
  112. fig = figure;
  113. % FR
  114. g(1) = gramm( 'x',bins,'y',binned,'color',c );
  115. n = length(binned);
  116. custom_statfun = @(y)([mean(y);
  117. mean(y) - std(y)/sqrt(17);
  118. mean(y) + std(y)/sqrt(17)]);
  119. g(1).stat_summary('setylim',true,'type',custom_statfun);
  120. g(1).set_names( 'x','','y','Rate [Hz]');
  121. g(1).set_layout_options('position',[0.05,0.7,0.9,0.3]);
  122. g(1).no_legend();
  123. g(1).axe_property('xlim',[fs 20],'XColor','none');
  124. % Inset
  125. g(6) = gramm('x',((1:64)-20)/32,'y',waveform(nExampleNeuron,:));
  126. g(6).geom_line();
  127. g(6).set_names('x','Time (ms)','y','uV' );
  128. g(6).set_layout_options('position',[0.1,0.9,0.1,0.1]);
  129. g(6).set_text_options('base_size',4);
  130. g(6).set_color_options('chroma',0,'lightness',30);
  131. g(6).set_line_options('base_size',1);
  132. g(6).no_legend();
  133. % g(6).axe_property('xlim',[fs 20],'XColor','none');
  134. % raster
  135. g(2) = gramm('x',ranTrials,'color',c);
  136. g(2).geom_raster('geom','point');
  137. g(2).set_point_options('base_size',3);
  138. g(2).set_names('x','Time [s]','y','Trial no. (reordered)');
  139. g(2).set_layout_options('position',[0.05,0.43,0.9,0.3]);
  140. g(2).no_legend();
  141. g(2).axe_property('xlim',[fs,20],'ylim',[-1,9]);
  142. % ISI
  143. g(3) = gramm('x',isis);
  144. g(3).stat_bin();
  145. g(3).set_layout_options('position',[0.05,0.1,0.25,0.3]);
  146. g(3).set_names('x','Percentage of ISI < 3 ms','y','No. of units');
  147. g(3).set_color_options('hue_range',[155,200]);
  148. % FR all
  149. g(4) = gramm('x',FiringRatesHz);
  150. g(4).stat_bin();
  151. g(4).set_layout_options('position',[0.38,0.1,0.25,0.3]);
  152. g(4).set_names('x','Firing rate [Hz]','y','No. of units');
  153. g(4).axe_property('xlim',[0 20]);
  154. g(4).set_color_options('hue_range',[155 200]);
  155. % SNR
  156. g(5) = gramm('x',snr);
  157. g(5).stat_bin();
  158. g(5).set_layout_options('position',[0.71,0.1,0.25,0.3]);
  159. g(5).set_names('x','Waveform peak SNR','y','No. of neurons');
  160. g(5).axe_property('xlim',[0 10]);
  161. g(5).set_color_options('hue_range',[155 200]);
  162. % Labels
  163. x = [0,0,0,0.35,0.68];
  164. y = [0.97,0.7,0.37,0.37,0.37];
  165. l = ['a','b','c','d','e'];
  166. g(7) = gramm('x',x,'y',y,'label',l);
  167. g(7).geom_label('Color','k','fontweight','bold');
  168. g(7).axe_property('XColor','none','YColor','none','xlim',[0,1],...
  169. 'ylim',[0,1],'Visible','off');
  170. g(7).set_layout_options('position',[0,0,1,1]);
  171. g.draw();
  172. % Save figure
  173. print('-dtiff','-r300','fig_2.tiff')
  174. end