getTuningCurveArrayFromTable.m 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. function [tcArray, responseIndArray, rSquareArray, fitParams, varargout] = getTuningCurveArrayFromTable(StimTable)
  2. %% Show some raw data.
  3. % Get the non-parametric tuning struct to show the tuning curves. Here
  4. % somehow some flies saw stimuli that had more or less bar positions, but
  5. % those flies had no good cells (above 0.5 response quality index).
  6. % The dimensions of the tuningStruct is 1 x nChannels, each element is then
  7. % 1 x nTuningMethods, here I use the roi function to get all structs for
  8. % one method into an array.
  9. nonParamTunings = getRoisFeaturePerChannel(cellfun(@(x) x', StimTable.nonParamTuning, 'uni', 0));
  10. if isfield(nonParamTunings(1), 'tuningMethods')
  11. tuningMethods = nonParamTunings(1).tuningMethods;
  12. tuningMethodInd = strcmp(tuningMethods, 'extreme');
  13. tcCellArray = arrayfun(@(x) x.tc{tuningMethodInd}, nonParamTunings, 'uni', 0);
  14. tcExtCellArray = arrayfun(@(x) x.tcParams{tuningMethodInd}.extremeResp, nonParamTunings, 'uni', 0);
  15. tcArgExtCellArray = arrayfun(@(x) x.tcParams{tuningMethodInd}.argExtreme, nonParamTunings, 'uni', 0);
  16. domainCellArray = arrayfun(@(x) x.domain, nonParamTunings, 'uni', 0);
  17. domainSize = cellfun(@numel, domainCellArray);
  18. tcCellArray = filterBgInvalidRois(tcCellArray);
  19. tcExtCellArray = filterBgInvalidRois(tcExtCellArray);
  20. tcArgExtCellArray = filterBgInvalidRois(tcArgExtCellArray);
  21. % Do the same for responseIndex.
  22. responseIndArray = getRoisFeaturePerChannel(StimTable.responseIndex(domainSize == mode(domainSize)))';
  23. % Now that all is homogeneous we can concatenate the arrays.
  24. tcArray = cell2mat(cellfun(@squeeze, tcCellArray, 'uni', 0)');
  25. tcExtArray = cell2mat(cellfun(@(x) squeeze(x), tcExtCellArray, 'uni', 0));
  26. tcArgExtArray = cell2mat(cellfun(@(x) squeeze(x), tcArgExtCellArray, 'uni', 0));
  27. % Alternative way.
  28. % tcCellArray = [cellfun(@squeeze, tcCellArray, 'uni', 0)'];
  29. % cat(1, tcCellArray{:});
  30. % Lets keep plotting.
  31. % Make sure all ROIs have a tuning curve and a quality
  32. % index.
  33. assert(numel(responseIndArray) == size(tcArray, 1) && ...
  34. numel(responseIndArray) == numel(tcExtArray) && ...
  35. numel(responseIndArray) == numel(tcArgExtArray), ...
  36. ['Mismatch of number of ROIs for tuning curve array and response' ...
  37. 'quality index array.']);
  38. varargout{1} = tcExtArray;
  39. varargout{2} = tcArgExtArray;
  40. else
  41. domainSize = ones(1, numel(StimTable.responseIndex));
  42. % Do the same for responseIndex.
  43. responseIndArray = getRoisFeaturePerChannel(StimTable.responseIndex(domainSize == mode(domainSize)))';
  44. tcArray = [];
  45. varargout{1} = [];
  46. varargout{2} = [];
  47. end
  48. % We can load the rSquare of the Gaussian fit.
  49. [fitParams, gof] = getFitParamsStructArrayFromTable(StimTable(domainSize == mode(domainSize), :));
  50. rSquareArray = [gof.rsquare];
  51. function outputCellArray = filterBgInvalidRois(inputCellArray)
  52. % Actually the last tuning curve is from the background. Check it now!
  53. bgRois = cellfun(@(x) x.backgroundRois, StimTable.roiMeta);
  54. invalidRois = cellfun(@(x) x.invalidRois, StimTable.roiMeta, 'uni', 0);
  55. % Lets get rid of background ROIs. Later check if invalidRois give trouble.
  56. for iFly = 1: numel(bgRois)
  57. nRois = size(inputCellArray{iFly}, 2);
  58. roisToKeep = setdiff(1: nRois, [bgRois(iFly) invalidRois{iFly}]);
  59. inputCellArray{iFly} = inputCellArray{iFly}(:, roisToKeep, :);
  60. end
  61. % Checkn that the stimuli are same size and discard the ones that are
  62. % different (outliers). Anyways they did not contribute good cells for this
  63. % stimulus in particular.
  64. outputCellArray = inputCellArray(domainSize == mode(domainSize));
  65. end
  66. end