getAlignedTuningCurveArrayFromTable.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. function [alignedRFsCell, ...
  2. alignedRFsCellNaN, varargout] = getAlignedTuningCurveArrayFromTable(StimTableCell, ...
  3. thresholdResponseInd, ...
  4. thresholdRSquare, ...
  5. varargin)
  6. %% Input should be a cell array of tables for XOFF, YOFF, XON, YON.
  7. switch nargin
  8. case 3
  9. alignToOff = false;
  10. alignToAll = false;
  11. case 4
  12. alignToOff = varargin{1};
  13. aignToAll = false;
  14. case 5
  15. alignToOff = varargin{1};
  16. alignToAll = varargin{2};
  17. otherwise
  18. warning('Too many arguments, cannot process them meaningfully!');
  19. end
  20. %%
  21. nStims = numel(StimTableCell);
  22. % Retrieve table data in arrays.
  23. [tcArrayCell, responseIndArrayCell, rSquareArrayCell, fitParamsCell, ...
  24. flyIndsCell, tcExtCellArray, tcArgExtCellArray] = getStimArraysForSameRois(StimTableCell, alignToOff);
  25. %% Set thresholds.
  26. isAboveThresholdCell = cellfun(@(x, y) ...
  27. x(:) > thresholdResponseInd & ...
  28. y(:) > thresholdRSquare, ...
  29. responseIndArrayCell, rSquareArrayCell, 'uni', 0);
  30. if alignToOff
  31. offThreshold = isAboveThresholdCell{1} & isAboveThresholdCell{2};
  32. onThreshold = isAboveThresholdCell{3} & isAboveThresholdCell{4};
  33. if alignToAll
  34. [isAboveThresholdCell{:}] = deal(offThreshold & onThreshold);
  35. [current2OffInds{1:4}] = deal(1: sum(isAboveThresholdCell{1}));
  36. else
  37. [isAboveThresholdCell{1:2}] = deal(offThreshold);
  38. isAboveThresholdCell{3} = offThreshold & isAboveThresholdCell{3};
  39. isAboveThresholdCell{4} = offThreshold & isAboveThresholdCell{4};
  40. offInds = find(offThreshold);
  41. [current2OffInds{1:2}] = deal(offInds);
  42. current2OffInds{3} = arrayfun(@(x) find(x == offInds), ...
  43. find(isAboveThresholdCell{3}));
  44. current2OffInds{4} = arrayfun(@(x) find(x == offInds), ...
  45. find(isAboveThresholdCell{4}));
  46. end
  47. end
  48. % Fill in in case no cell passed the threshold.
  49. emptyStimulusInd = cellfun(@(x) ~any(x), isAboveThresholdCell);
  50. if all(emptyStimulusInd)
  51. fprintf('No cell fulfill requested conditions!!! Change thresholds \n')
  52. return
  53. end
  54. firstNonEmptyStim = isAboveThresholdCell{find(~emptyStimulusInd, 1)};
  55. for iStim = find(emptyStimulusInd)
  56. isAboveThresholdCell{iStim} = ones(sum(firstNonEmptyStim), 1);
  57. end
  58. % Now get the thresholded arrays. We still need the case for all stimuli
  59. % passing the threshold.
  60. tcArrayThres = cellfun(@(x, y) x(y, :), ...
  61. tcArrayCell, isAboveThresholdCell, 'uni', 0);
  62. respIndArrayThres = cellfun(@(x, y) x(y), ...
  63. responseIndArrayCell, isAboveThresholdCell, 'uni', 0);
  64. rSquareArrayThres = cellfun(@(x, y) x(y), ...
  65. rSquareArrayCell, isAboveThresholdCell, 'uni', 0);
  66. a1Thres = cellfun(@(x, y) x.a1(y), ...
  67. fitParamsCell, isAboveThresholdCell, 'uni', 0);
  68. b1Thres = cellfun(@(x, y) x.b1(y), ...
  69. fitParamsCell, isAboveThresholdCell, 'uni', 0);
  70. c1Thres = cellfun(@(x, y) x.c1(y), ...
  71. fitParamsCell, isAboveThresholdCell, 'uni', 0);
  72. flyIndsThres = cellfun(@(x, y) x(y), ...
  73. flyIndsCell, isAboveThresholdCell, 'uni', 0);
  74. tcExtArrayThres = cellfun(@(x, y) x(y), ...
  75. tcExtCellArray, isAboveThresholdCell, 'uni', 0);
  76. tcArgExtArrayThres = cellfun(@(x, y) x(y), ...
  77. tcArgExtCellArray, isAboveThresholdCell, 'uni', 0);
  78. padWithNaNs = 01;
  79. polarityCell = cellfun(@getStimPolarity, StimTableCell, 'uni', 0);
  80. %% Align the thresholded tuning curves.
  81. for iStim = 1: nStims
  82. if alignToOff && iStim > 2
  83. lagsToUse = lagDiffsCell{iStim - 2}(current2OffInds{iStim});
  84. else
  85. lagsToUse = [];
  86. end
  87. [alignedRFsCell{iStim}, lagDiffsCell{iStim}] = alignRFsToAbsMax(...
  88. tcArrayThres{iStim}, ...
  89. padWithNaNs * 0, ...
  90. lagsToUse, ...
  91. polarityCell{iStim});
  92. % Set to zero all array for stimuli without any rois aboive threshold.
  93. if ~any(responseIndArrayCell{iStim} > thresholdResponseInd & ...
  94. rSquareArrayCell{iStim} > thresholdRSquare)
  95. alignedRFsCell{iStim} = zeros(size(tcArrayCell{iStim}));
  96. end
  97. end
  98. % This is just to get the nans.
  99. for iStim = 1: nStims
  100. if alignToOff && iStim > 2
  101. lagsToUse = lagDiffsCell{iStim - 2}(current2OffInds{iStim});
  102. else
  103. lagsToUse = [];
  104. end
  105. [alignedRFsCellNaN{iStim}, lagDiffsCell{iStim}] = alignRFsToAbsMax(...
  106. tcArrayThres{iStim}, ...
  107. padWithNaNs, ...
  108. lagsToUse, ...
  109. polarityCell{iStim});
  110. % Set to zero all array for stimuli without any rois aboive threshold.
  111. if ~any(responseIndArrayCell{iStim} > thresholdResponseInd & ...
  112. rSquareArrayCell{iStim} > thresholdRSquare)
  113. alignedRFsCellNaN{iStim} = zeros(size(tcArrayCell{iStim}));
  114. end
  115. end
  116. fitParamsThres.a1 = a1Thres;
  117. fitParamsThres.b1 = b1Thres;
  118. fitParamsThres.c1 = c1Thres;
  119. varargout{1} = fitParamsThres;
  120. varargout{2} = flyIndsThres;
  121. varargout{3} = isAboveThresholdCell;
  122. varargout{4} = tcExtArrayThres;
  123. varargout{5} = tcArgExtArrayThres;
  124. varargout{6} = respIndArrayThres;
  125. varargout{7} = rSquareArrayThres;
  126. end
  127. function polarity = getStimPolarity(StimTable)
  128. % Get stim polarity.
  129. tokens = regexp(StimTable(1, :).stimParamFileName{1}, '.*_([mp]).*Con_.*', 'tokens');
  130. switch tokens{1}{1}
  131. case 'm'
  132. polarity = 'off';
  133. case 'p'
  134. polarity = 'on';
  135. otherwise
  136. polarity = 'other';
  137. end
  138. end