ampAtStim_funtest.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. function varargout = ampAtStim_funtest(file, varargin)
  2. %% Function to visually validate the results of ampAtStim
  3. % This function is identical to 'ampAtStim' in most respects, but it
  4. % additionally creates a plot for every trial showing the data that is used
  5. % for the calculations. These plots can facilitate the verification of the
  6. % results. The code sections that differ from the main function are marked
  7. % as such. Only one file at a time is analysed with ffor all stimuli, but
  8. % the trials can be specified.
  9. % -------------------------------------------------------------------------
  10. % Input [additional arguments are positional,
  11. % ----- optional arguments are name-value pairs]
  12. %
  13. % files: ID-number(s) of the '.mat' data-file(s),
  14. % Type: integer or vector
  15. % trials: number(s) of the trials to be analysed, type: integer or
  16. % (additional) vector, default: 'all'
  17. % Filter: select between three filter options:
  18. % (optional) 'hn' applys a hum-noise-remove filter
  19. % 'hnhfn' additonally applys a low pass filter to
  20. % remove high frequency noise
  21. % If not set, it will be set to "no" (default).
  22. % StimCell: set whether to plot the data for a stimulated T-cell ('T')
  23. % (optional) or a stimulated interneuron ('INT'). Default: 'T'
  24. % ErrorBars: set whether error bars (standard deviation) should be
  25. % (optional) plotted (1 or 0). Default: 1 (yes)
  26. % ------
  27. % Output
  28. % ------
  29. % visually output of the plots if no output variable is set, new figure
  30. % windows are created
  31. %
  32. % varargout{1}: structure, containing the calculated values, amplitudes
  33. % for the stimuli are sorted from the smallest to the
  34. % biggest stimulus
  35. % - MeanAmplitudeT/MeanAmplitudeINT: averaged amplitudes
  36. % for the trials where the 'stimCell' was stimulated
  37. % - AmplitudeT/AmplitudeINT: amplitudes for the cells
  38. % for all trials (not averaged)
  39. % - sdT/sdINT: standard deviation for all amplitudes
  40. % over all trials for each stimulus
  41. % - stimulatedT: trials with a stimulated T cell
  42. % -------------------------------------------------------------------------
  43. % Program by: Bjarne Schultze [last modified 07.03.2022]
  44. % -------------------------------------------------------------------------
  45. %% Preperations
  46. % parse the user input
  47. [file_num, trials, filter_choice, tol, ~, stimCell, error_bars, ~,~] = ...
  48. parse_input_peakfun(file, varargin{:});
  49. % Access the data using the function 'extract_data'
  50. dataIn = extract_data(file_num, filter_choice);
  51. recData = dataIn{1,1};
  52. timeVector = recData.timeVector;
  53. %% Find and sort the stimulus peaks
  54. % finds the points, where stimulus is positive
  55. [stim_peaks, stim_locs] = findpeaks(recData.stimulus, 10000);
  56. % find the points, where stimulus is negativ (therefor inverting the
  57. % stimulus data)
  58. [min_stim_peaks, min_stim_locs] = ...
  59. findpeaks(-recData.stimulus, 10000, 'MinPeakHeight', 0);
  60. % invert the peak values back
  61. min_stim_peaks = -min_stim_peaks;
  62. % concatenating positive and nagative stimlus peak data
  63. all_stim_locs = [min_stim_locs; stim_locs];
  64. all_stim_peaks = [min_stim_peaks; stim_peaks];
  65. % sort the values for the current in ascending order
  66. [all_stim_peaks_sort, sort_order] = sort(all_stim_peaks);
  67. all_stim_locs_sort = all_stim_locs(sort_order);
  68. %% Calculation of the amplitude and its standard deviation
  69. % length of stimulus (here it's the same for all stimuli)
  70. stim_len = 0.5;
  71. stim_num = length(all_stim_locs_sort); % number of stimuli
  72. % get the number of trials
  73. if isequal(trials,'all')
  74. trials = 1:size(recData.recordingT,2);
  75. end
  76. trial_num = length(trials);
  77. % pre-define vectors for the results of the calculations
  78. ampT = zeros(stim_num,trial_num);
  79. ampINT = ampT;
  80. % ------------------------------------------------------------------------
  81. % TEST PART:
  82. if isequal(stimCell, "INT")
  83. stimulated = setdiff(trials, recData.stimulatedT(:), 'stable');
  84. else
  85. stimulated = recData.stimulatedT(:);
  86. if ~isequal(trials,'all')
  87. stimulated = intersect(stimulated,trials);
  88. end
  89. end
  90. % ------------------------------------------------------------------------
  91. for exp = trials
  92. % ----------------------------------------------------
  93. % TEST-PART: Plot the recording data of the current trial
  94. if any(ismember(stimulated,exp))
  95. figure();
  96. tiledlayout(2,1);
  97. plt1 = nexttile;
  98. plot(plt1,recData.timeVector, recData.recordingT(:,exp));
  99. xlabel("time [s]"); ylabel("membrane potential [mV]");
  100. plt2 = nexttile;
  101. plot(plt2, recData.timeVector, recData.recordingINT(:,exp));
  102. xlabel("time [s]"); ylabel("membrane potential [mV]");
  103. end
  104. % ----------------------------------------------------
  105. % iterate through the stimuli calculating amplitude and sd for both cells
  106. for stim = 1:length(all_stim_locs_sort)
  107. % select the time frame in which one cell was stimulated
  108. search_index_amp = timeVector >= all_stim_locs_sort(stim) + tol ...
  109. & timeVector <= all_stim_locs_sort(stim) + stim_len;
  110. % select a time frame before the stimulus with the same length as
  111. % the stimulus as a reference
  112. search_index_preamp = timeVector <= all_stim_locs_sort(stim) ...
  113. & timeVector >= all_stim_locs_sort(stim) - stim_len + tol;
  114. % ----------------------------------------------------
  115. % TEST-PART: Color the sections used for calculations and add the
  116. % corresponding means
  117. if any(ismember(stimulated,exp))
  118. % select the stimulus time frame and in the reference time frame
  119. timeVector_amp = recData.timeVector(search_index_amp);
  120. timeVector_preamp = recData.timeVector(search_index_preamp);
  121. % calculate the mean of the recording data for the stimulus time
  122. % frame and the reference time frame for both cells
  123. mean_ampT = mean(recData.recordingT(search_index_amp,exp));
  124. mean_preampT = mean(recData.recordingT(search_index_preamp,exp));
  125. mean_ampINT = mean(recData.recordingINT(search_index_amp,exp));
  126. mean_preampINT = mean(recData.recordingINT(search_index_preamp,exp));
  127. % Color the sections respectively and add the means
  128. hold(plt1, 'on');
  129. plot(plt1, timeVector_amp, ...
  130. recData.recordingT(search_index_amp, exp),'-',...
  131. 'Color', [1,0.38,0.11]);
  132. plot(plt1, timeVector_preamp,...
  133. recData.recordingT(search_index_preamp,exp),'-',...
  134. 'Color', [0.2,0.76,0.1]);
  135. plot(plt1, timeVector_amp([1,end]),[mean_ampT, mean_ampT],'-',...
  136. 'Color',[0.6,0.1,0.1]);
  137. plot(plt1, timeVector_preamp([1,end]),...
  138. [mean_preampT, mean_preampT],'-', 'Color', [0.1,0.6,0.1])
  139. legend(plt1,'recording', 'on stimulus', 'reference',...
  140. 'Location', 'best', 'AutoUpdate', 'off');
  141. hold(plt1, 'off');
  142. hold(plt2, 'on');
  143. plot(plt2, timeVector_amp, ...
  144. recData.recordingINT(search_index_amp, exp),'-',...
  145. 'Color', [1,0.38,0.11]);
  146. plot(plt2, timeVector_preamp,...
  147. recData.recordingINT(search_index_preamp,exp),'-',...
  148. 'Color', [0.2,0.76,0.1]);
  149. plot(plt2, timeVector_amp([1,end]),[mean_ampINT, mean_ampINT],'-',...
  150. 'Color',[0.6,0.1,0.1]);
  151. plot(plt2, timeVector_preamp([1,end]), ...
  152. [mean_preampINT, mean_preampINT],'-', 'Color', [0.1,0.6,0.1])
  153. legend(plt2,'recording', 'on stimulus', 'reference',...
  154. 'Location', 'best', 'AutoUpdate', 'off');
  155. hold(plt2, 'off');
  156. end
  157. % ----------------------------------------------------
  158. % calculate the amplitude of the membrane potential in the T
  159. % cell in relation to the amplitude in the reference time frame
  160. ampT(stim,exp) = ...
  161. mean(recData.recordingT(search_index_amp,exp)) ...
  162. - mean(recData.recordingT(search_index_preamp,exp));
  163. % if the amplitude is smaller than 3 sd in the reference time
  164. % frame it is considered as zero
  165. if abs(ampT(stim,exp)) < ...
  166. 3 * std(recData.recordingT...
  167. (search_index_preamp,exp))
  168. ampT(stim,exp) = 0;
  169. end
  170. % same calculations for the interneuron
  171. ampINT(stim,exp) = ...
  172. mean(recData.recordingINT(search_index_amp,exp)) ...
  173. - mean(recData.recordingINT(search_index_preamp,exp));
  174. if abs(ampINT(stim,exp)) < ...
  175. 3 * std(recData.recordingINT...
  176. (search_index_preamp,exp))
  177. ampINT(stim,exp) = 0;
  178. end
  179. % ---------------------------------------------------
  180. % TEST PART: Add little scales for each amplitude
  181. if any(ismember(stimulated, exp))
  182. hold(plt1, 'on');
  183. plot(plt1, timeVector_preamp([end-50,end-50]),...
  184. [mean_preampT,mean_preampT + ampT(stim,exp)], '-_k');
  185. text(plt1, timeVector_preamp(end-100),...
  186. ((2 * mean_preampT + ampT(stim,exp))/2), ...
  187. num2str(round(ampT(stim,exp),1)), 'Color', 'black', ...
  188. 'HorizontalAlignment', 'right','FontSize',8);
  189. hold(plt1, 'off');
  190. hold(plt2, 'on');
  191. plot(plt2, timeVector_preamp([end-50,end-50]),...
  192. [mean_preampINT,mean_preampINT + ampINT(stim,exp)], '-_k');
  193. text(plt2, timeVector_preamp(end-100),...
  194. ((2 * mean_preampINT + ampINT(stim,exp))/2), ...
  195. num2str(round(ampINT(stim,exp),1)), 'Color', 'black', ...
  196. 'HorizontalAlignment', 'right', 'FontSize', 8);
  197. hold(plt2, 'off');
  198. end
  199. % ---------------------------------------------------
  200. end
  201. end
  202. %% Averaging of the amplitudes
  203. % pre-define vectors for the mean and standard deviation of the spiking
  204. % frequency over all trials
  205. m_ampT = zeros(stim_num,1); m_ampINT = zeros(stim_num,1);
  206. sd_ampT = zeros(stim_num,1); sd_ampINT = zeros(stim_num,1);
  207. if isequal(stimCell, "INT")
  208. stimulated = setdiff(trials, recData.stimulatedT(:), 'stable');
  209. plot_title = sprintf("File: %02.f - Interneuron stimulated", file_num);
  210. else
  211. stimulated = recData.stimulatedT(:);
  212. plot_title = sprintf("File: %02.f - T-cell stimulated", file_num);
  213. if ~isequal(trials,'all')
  214. stimulated = intersect(stimulated,trials);
  215. end
  216. end
  217. % find the average and standard deviation of the spiking frequency for each
  218. % stimulus
  219. for stim = 1:stim_num
  220. % mean and sd of trials where the T-cell was stimulated
  221. m_ampT(stim) = mean(ampT(stim,stimulated));
  222. sd_ampT(stim) = std(ampT(stim,stimulated));
  223. % mean and sd for the interneurons
  224. m_ampINT(stim) = mean(ampINT(stim,stimulated));
  225. sd_ampINT(stim) = std(ampINT(stim,stimulated));
  226. end
  227. %% Return and plot results
  228. % if an output is required, the calculated values are returned (structure)
  229. if isequal(nargout, 1)
  230. varargout{1} = struct('MeanAmplitudeT',m_ampT, ...
  231. 'MeanAmplitudeINT', m_ampINT, ...
  232. 'AmplitudeT', ampT, ...
  233. 'AmplitudeINT', ampINT, ...
  234. 'sdT', sd_ampT, 'sdINT', sd_ampINT, ...
  235. 'stimulatedT', recData.stimulatedT);
  236. else
  237. % plot the results either with or without error bars
  238. figure();
  239. if isequal(error_bars, 1)
  240. errorbar(all_stim_peaks_sort, m_ampT, sd_ampT);
  241. axis padded
  242. hold on;
  243. errorbar(all_stim_peaks_sort, m_ampINT, sd_ampINT);
  244. hold off;
  245. yline(0, 'Color', [0.6,0.6,0.6]);
  246. title(plot_title);
  247. xlabel("current [nA]"); ylabel("amplitude [mV]");
  248. legend("T-cell", "Interneuron", 'Location', 'best');
  249. subtitle("- error bars representing standard deviation between trials -");
  250. else
  251. plot(all_stim_peaks_sort, m_ampT, 'o-',...
  252. all_stim_peaks_sort, m_ampINT, 'o-', 'MarkerSize', 4);
  253. axis padded
  254. title(plot_title);
  255. xlabel("stimulation current [nA]"); ylabel("amplitude [mV]");
  256. legend("T-cell", "Interneuron", 'Location', 'best');
  257. yline(0, 'Color', [0.6,0.6,0.6]);
  258. end
  259. end
  260. % end of function definition
  261. end