getStimArraysForSameRois.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. function [tcArrayCell, responseIndArrayCell, rSquareArrayCell, fitParamsCell, ...
  2. flyIndsCell, tcExtCellArray, tcArgExtCellArray, varargout] = getStimArraysForSameRois(StimTableCell, trimArray)
  3. if nargin < 2
  4. trimArray = 1;
  5. end
  6. [tcArrayCell, responseIndArrayCell, rSquareArrayCell, fitParamsCell, ...
  7. tcExtCellArray, tcArgExtCellArray] = cellfun(@getTuningCurveArrayFromTable, ...
  8. StimTableCell, 'uni', 0);
  9. nStims = numel(StimTableCell);
  10. for iStim = 1: nStims
  11. flyIndsCell{iStim} = [];
  12. for jFly = 1: size(StimTableCell{iStim})
  13. nRoisTemp = numel(StimTableCell{iStim}(jFly, 'responseIndex').responseIndex{1});
  14. flyIndsCell{iStim} = [flyIndsCell{iStim} repelem(StimTableCell{iStim}{jFly, 1}, nRoisTemp)];
  15. end
  16. end
  17. % Big ugly chunk to get rois that have seen all stimuli (bars in this case)
  18. if trimArray
  19. % find mismatches of number of ROIs accross stimuli
  20. nRoisCell = cellfun(@(y) cellfun(@numel, y.responseIndex), ...
  21. StimTableCell, 'uni', 0);
  22. nRoisArray = [nRoisCell{:}];
  23. % % use the fitting parameters, because using only response quality neglects
  24. % % the fitting failures.
  25. % nRoisCell = cellfun(@(y) cellfun(@numel, y.paramTuning), ...
  26. % StimTableCell, 'uni', 0);
  27. % nRoisArray = cat(1, nRoisCell{:});
  28. for iFly = 1: size(nRoisArray, 1)
  29. equalNoCells(iFly) = all(nRoisArray(iFly, :) == nRoisArray(iFly, 1));
  30. end
  31. % Check if there are any stimuli with distinct number of rois.
  32. if any(~equalNoCells)
  33. for iStim = 1: nStims
  34. roisMeta = StimTableCell{iStim}.roiMeta;
  35. bgRois = cellfun(@(x) x.backgroundRois, roisMeta);
  36. nValidRois = cellfun(@numel, StimTableCell{iStim}.responseIndex);
  37. % Last roi picked is the background.
  38. roiInds = arrayfun(@(x) 1: x, bgRois, 'uni', 0);
  39. bgOrInvalidRois = cellfun(@(x) [x.backgroundRois x.invalidRois], ...
  40. roisMeta, 'uni', 0);
  41. validRoiInds{iStim} = cellfun(@(x, y) setdiff(x, y), ...
  42. roiInds, bgOrInvalidRois, 'uni', 0);
  43. nFlies = size(StimTableCell{iStim}, 1);
  44. end
  45. validRoiIndsCellArray = [validRoiInds{:}];
  46. % Check what rois to remove only for flies with different roi
  47. % number.
  48. for iFly = find(~equalNoCells)
  49. if nStims == 4
  50. offInds = intersect(validRoiIndsCellArray{iFly, 1: 2});
  51. onInds = intersect(validRoiIndsCellArray{iFly, 3: 4});
  52. allInds = intersect(offInds, onInds);
  53. else
  54. allInds = validRoiIndsCellArray{iFly, 1};
  55. for iStim = 2: nStims
  56. allInds = intersect(allInds, validRoiIndsCellArray{iFly, iStim});
  57. end
  58. end
  59. for jStim = 1: nStims
  60. % Find roi index of the ones not in the intersection.
  61. validInds = validRoiIndsCellArray{iFly, jStim};
  62. invalidRoisInds = setdiff(validInds, allInds);
  63. tempInd = arrayfun(@(x) find(validInds == x), ...
  64. invalidRoisInds, 'uni', 0);
  65. roisToRemoveInd{iFly, jStim} = tempInd;
  66. end
  67. end
  68. for jStim = find(~all(cellfun(@isempty, roisToRemoveInd), 1))
  69. for iFly = find(~equalNoCells)
  70. % Quick fixup for only one invalid roi per fly
  71. if ~isempty(roisToRemoveInd{iFly, jStim})
  72. deleteInds = arrayfun(@(x) find(validRoiIndsCellArray{iFly, jStim} == x), ...
  73. roisToRemoveInd{iFly, jStim}{1}, 'uni', 1);
  74. validRoiIndsCellArray{iFly, jStim}(deleteInds) = 0;
  75. end
  76. end
  77. end
  78. transposedValidRois = cellfun(@(x) x', validRoiIndsCellArray, 'uni', 0);
  79. vectorValidRois = arrayfun(@(x) cat(1, transposedValidRois{:, x}), 1: size(validRoiInds, 2), 'uni', 0);
  80. for jStim = find(~all(cellfun(@isempty, roisToRemoveInd), 1))
  81. tempInd = vectorValidRois{jStim} == 0;
  82. responseIndArrayCell{jStim}(tempInd, :) = [];
  83. rSquareArrayCell{jStim}(tempInd) = [];
  84. fitParamsCell{jStim}.a1(tempInd) = [];
  85. fitParamsCell{jStim}.b1(tempInd) = [];
  86. fitParamsCell{jStim}.c1(tempInd) = [];
  87. flyIndsCell{jStim}(tempInd) = [];
  88. if ~isempty(tcArrayCell{jStim})
  89. tcArrayCell{jStim}(tempInd, :) = [];
  90. % tcExtCellArray{jStim}(tempInd, :) = [];
  91. % tcArgExtCellArray{jStim}(tempInd, :) = [];
  92. tcExtCellArray{jStim}(:, tempInd) = [];
  93. tcArgExtCellArray{jStim}(:, tempInd) = [];
  94. end
  95. end
  96. else
  97. roisToRemoveInd = {};
  98. end
  99. varargout{1} = roisToRemoveInd;
  100. % Check that all stimuli have same number of rois, it could be by
  101. % chance, but here we assume the cells all saw same stimulus. Actually
  102. % it should have an unique roisID but so far its not implemented.
  103. sizeCell = cellfun(@(x) size(x, 1), tcArrayCell, 'uni', 0);
  104. assert(isequal(sizeCell{:}));
  105. end
  106. end