plotTuningCurveArrayFromTable.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. function varargout = plotTuningCurveArrayFromTable(StimTableCell, figDir, varargin)
  2. %% Input should be a cell array of tables for XOFF, YOFF, XON, YON.
  3. switch nargin
  4. case 2
  5. alignToOff = false;
  6. alignToAll = false;
  7. thresholdResponseInd = 0;
  8. thresholdRSquare = -20;
  9. padWithNaNs = 0;
  10. case 3
  11. alignToOff = varargin{1};
  12. alignToAll = false;
  13. thresholdResponseInd = 0;
  14. thresholdRSquare = -20;
  15. padWithNaNs = 0;
  16. case 4
  17. alignToOff = varargin{1};
  18. alignToAll = varargin{2};
  19. thresholdResponseInd = 0;
  20. thresholdRSquare = -20;
  21. padWithNaNs = 0;
  22. case 5
  23. alignToOff = varargin{1};
  24. alignToAll = varargin{2};
  25. thresholdResponseInd = varargin{3};
  26. thresholdRSquare = -20;
  27. padWithNaNs = 0;
  28. case 6
  29. alignToOff = varargin{1};
  30. alignToAll = varargin{2};
  31. thresholdResponseInd = varargin{3};
  32. thresholdRSquare = varargin{4};
  33. padWithNaNs = 0;
  34. case 7
  35. alignToOff = varargin{1};
  36. alignToAll = varargin{2};
  37. thresholdResponseInd = varargin{3};
  38. thresholdRSquare = varargin{4};
  39. padWithNaNs = varargin{5};
  40. otherwise
  41. warning('Too many arguments, cannot process them meaningfully!');
  42. end
  43. % try
  44. [alignedRFsCell, ...
  45. alignedRFsCellNaN] = getAlignedTuningCurveArrayFromTable( StimTableCell, ...
  46. thresholdResponseInd, ...
  47. thresholdRSquare, ...
  48. alignToOff, ...
  49. alignToAll);
  50. % catch
  51. % fprintf('Something went wrong! \n')
  52. % return
  53. % end
  54. %
  55. qualityMeasure = 'responseQuality';
  56. hFig = createPrintFig(15 * 1 * [1 0.8]);
  57. nStims = numel(StimTableCell);
  58. data = cellfun(@vec, alignedRFsCell, 'uni', 0);
  59. data = cat(1, data{:});
  60. % colorBounds = ZeroCenteredBounds(data, [0 100]);
  61. for iStim = 1: nStims
  62. StimTable = StimTableCell{iStim};
  63. tuningCurves = alignedRFsCell{iStim};
  64. tuningCurvesNaN = alignedRFsCellNaN{iStim};
  65. hSubAx(iStim) = subplot(2, 2, iStim);
  66. colorMap = setFavoriteColormap;
  67. barSpacing = [StimTable.stimParams{1}.spacing2];
  68. barPosition = arrayfun(@(x) x.amp, [StimTable.stimParams{1}.stimtrans]);
  69. % Center position.
  70. barPosition = barPosition - median(barPosition);
  71. % To ignore bar position just use the units of indices.
  72. barPosition = mean(barSpacing) * (1: size(tuningCurves, 2));
  73. imAlpha = ones(size(tuningCurvesNaN(:, :)));
  74. if padWithNaNs
  75. imAlpha(isnan(tuningCurvesNaN(:, :))) = 0;
  76. end
  77. imagesc(barPosition(2: end), 1: size(tuningCurves, 1), tuningCurves(:, :), ...
  78. 'AlphaData', imAlpha);
  79. colorBounds = ZeroCenteredBounds(tuningCurves, [0 100]);
  80. set(gca, 'CLim', colorBounds);
  81. set(gca, 'color', 1 * [1 1 1]);
  82. end
  83. formatSubAxes(hSubAx, padWithNaNs);
  84. [~, ~, genotype] = parseFlyPath(fileparts(StimTable.timeSeriesPath{1}));
  85. figFileName = ['RF-TuningCurves-' genotype '-' ...
  86. qualityMeasure '-' num2str(thresholdResponseInd) '-'...
  87. 'rSquare-' num2str(thresholdRSquare) '-', ...
  88. 'All-' num2str(alignToAll) ...
  89. 'Off-' num2str(alignToOff)];
  90. % set(gcf,'renderer','painters')
  91. % set(gcf, 'PaperPositionMode', 'auto');
  92. print(hFig, [figDir figFileName '.pdf'], '-dpdf')
  93. print(hFig, [figDir figFileName '.png'], '-dpng', '-r300')
  94. % print(hFig, [figDir figFileName '.eps'], '-depsc2', '-r300')
  95. varargout{1} = alignedRFsCell;
  96. function tryout
  97. %% Plot tuning curves.
  98. nStims = 4;
  99. for iStim = 2: 5
  100. StimTable = StimTableCell{iStim};
  101. % Retrieve table data in arrays.
  102. [tcArray, responseIndArray, rSquareArray, fitParams] = getTuningCurveArrayFromTable(StimTable);
  103. % We can threshold now.
  104. thresholdResponseInd = 0.0;
  105. isAboveThreshold = responseIndArray > thresholdResponseInd;
  106. tcArray = tcArray(isAboveThreshold, :);
  107. responseIndArray = responseIndArray(isAboveThreshold);
  108. rSquareArray = rSquareArray(isAboveThreshold);
  109. % Sort cells by response quality.
  110. [sortedResponseIndArray, sortInd] = sort(responseIndArray, 'descend');
  111. subplot(4, nStims, iStim - 1);
  112. imagesc(tcArray(sortInd, :), ZeroCenteredBounds(tcArray, [0 100]));
  113. % setFavoriteColormap;
  114. colormap(brewermap([], 'BrBG'))
  115. % Sort by fit quality.
  116. [sortedRSquareArray, sortInd] = sort(rSquareArray, 'descend');
  117. subplot(4, nStims, iStim - 1 + nStims);
  118. imagesc(tcArray(sortInd, :), ZeroCenteredBounds(tcArray, [0 100]));
  119. setFavoriteColormap;
  120. colormap(brewermap([], 'BrBG'))
  121. % colormap(brewermap([], 'RdBu'))
  122. % Check how it looks aligning the tuning curves.
  123. [alignedRFsCell, lagDiffsCell] = alignRFsToAbsMax(tcArray, 0);
  124. % alignedRFs = tcArray;
  125. % [alignedRFs, lagDiffs] = alignRFsToAbsMax(alignedRFs, 0);
  126. % [alignedRFs, lagDiffs] = alignRFsToAbsMax(alignedRFs, 0);
  127. % [alignedRFs, lagDiffs] = alignRFsToAbsMax(alignedRFs, 0);
  128. [sortedRSquareArray, sortInd] = sort(rSquareArray, 'descend');
  129. subplot(4, nStims, iStim - 1 + 2 * nStims);
  130. imagesc(alignedRFsCell(:, :), ZeroCenteredBounds(alignedRFsCell, [0 100]));
  131. % setFavoriteColormap;
  132. colormap(brewermap([], 'BrBG'))
  133. % colormap(brewermap([], 'RdBu'))
  134. [sortedRSquareArray, sortInd] = sort(rSquareArray, 'descend');
  135. subplot(4, nStims, iStim - 1 + 3 * nStims);
  136. imagesc(tcArray(:, :), ZeroCenteredBounds(alignedRFsCell, [0 100]));
  137. % setFavoriteColormap;
  138. % colormap(brewermap([], 'RdBu'))
  139. % imagesc(sortedRSquareArray', ZeroCenteredBounds(sortedRSquareArray, [0 100]));
  140. end
  141. end
  142. end
  143. function polarity = getStimPolarity(StimTable)
  144. % Get stim polarity.
  145. tokens = regexp(StimTable(1, :).stimParamFileName{1}, '.*_([mp]).*Con_.*', 'tokens');
  146. switch tokens{1}{1}
  147. case 'm'
  148. polarity = 'off';
  149. case 'p'
  150. polarity = 'on';
  151. otherwise
  152. polarity = 'other';
  153. end
  154. end
  155. function formatSubAxes(hSubAx, varargin)
  156. if nargin > 1
  157. padWithNaNs = varargin{1};
  158. else
  159. padWithNaNs = 0;
  160. end
  161. % Move the plots closer together.
  162. hSubAx(2).Position(1) = hSubAx(2).Position(1) - 0.1;
  163. hSubAx(4).Position(1) = hSubAx(4).Position(1) - 0.1;
  164. % Set colorbar formatting.
  165. tempPos = hSubAx(2).Position;
  166. hColorBar(1) = colorbar(hSubAx(2));
  167. hSubAx(2).Position = tempPos;
  168. tempPos = hSubAx(4).Position;
  169. hColorBar(2) = colorbar(hSubAx(4));
  170. hSubAx(4).Position = tempPos;
  171. hColorBar(2).Label.String = '\DeltaF / F_0';
  172. hFig = hSubAx(1).Parent;
  173. fileStimSuffix = {'XOFF', 'YOFF', 'XON', 'YON'};
  174. axis(hSubAx, 'square');
  175. arrayfun(@(x) set(x, 'DataAspectRatio'), hSubAx);
  176. for iStim = 1: numel(hSubAx)
  177. hSubTitle(iStim) = title(hSubAx(iStim), fileStimSuffix{iStim});
  178. end
  179. hYLabel(1) = ylabel(hSubAx(3), 'ROI No.');
  180. hXLabel(1) = xlabel(hSubAx(3), 'Bar Position (\circ)');
  181. arrayfun(@prettifyAxes, [hSubAx hColorBar]);
  182. % arrayfun(@offsetAxes, hSubAx);
  183. % Make the yaxis same so you can see the relative contribution of number of
  184. % cells for each stimulus.
  185. % linkaxes(hSubAx, 'y');
  186. % Set color of axis to zero color to highlight lack of data. Maybe zero is
  187. % misleading but its perfectly uniform so i will not look like the rest of
  188. % the data.
  189. colorMap = colormap(hSubAx(1));
  190. if ~padWithNaNs
  191. set(hSubAx, 'Color', colorMap(end / 2, :));
  192. end
  193. setFontForThesis([hSubAx hColorBar], hFig)
  194. end