collectRFTraces.m 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. % Function collects the receptive fields (X,Y) (On, Off) from all neurons
  2. % into a cell array 4 x nChannels with rows: (xOff, yOff, xOn, yOn)
  3. % This reads only first layer recorded in a fly.
  4. % For this only data with 5 stimuli can be used:
  5. % OnOff flashes plus 4 bar stimuli
  6. function receptiveFieldsAll = collectRFTraces(dataEdges, varargin)
  7. if nargin >= 3
  8. deleteLastROI = varargin{1};
  9. else
  10. deleteLastROI = 1;
  11. end
  12. % Define the array to compare to randomized recordings.
  13. xOffStimFileName = 'StandingStripe_1s_XAxis_5degWide_2degSep_m1.0Con_rand_USEFRUSTUM.txt';
  14. yOffStimFileName = 'StandingStripe_1s_YAxis_5degWide_2degSep_m1.0Con_rand_USEFRUSTUM.txt';
  15. xOnStimFileName = 'StandingStripe_1s_XAxis_5degWide_2degSep_p1.0Con_rand_USEFRUSTUM.txt';
  16. yOnStimFileName = 'StandingStripe_1s_YAxis_5degWide_2degSep_p1.0Con_rand_USEFRUSTUM.txt';
  17. barStimFileNames = {xOffStimFileName, yOffStimFileName, xOnStimFileName, yOnStimFileName};
  18. %% Get the shortest time points of the interpolated traces, to have a common
  19. % reference frame.
  20. barStimInds = cellfun(@(x) find(strcmp(x.stimulusName, barStimFileNames)), ...
  21. squeeze(dataEdges), 'uni', 0);
  22. barStimInds(:, 1) = [];
  23. invalidFlies = any(cellfun(@isempty, barStimInds), 2);
  24. tStart = cellfun(@(x) x.times(1), squeeze(dataEdges));
  25. tEnd = cellfun(@(x) x.times(end), squeeze(dataEdges));
  26. tStart(:, 1) = [];
  27. tEnd(:, 1) = [];
  28. tStart(invalidFlies, :) = [];
  29. tEnd(invalidFlies, :) = [];
  30. barStimInds(invalidFlies, :) = [];
  31. barStimInds = cell2mat(barStimInds);
  32. for iFly = 1: size(barStimInds, 1)
  33. [~, sortInds] = sort(barStimInds(iFly, :));
  34. tStartSorted(iFly, :) = tStart(iFly, sortInds);
  35. tEndSorted(iFly, :) = tEnd(iFly, sortInds);
  36. end
  37. maxTStart = max(tStartSorted, [], 1);
  38. minTEnd = min(tEndSorted, [], 1);
  39. %%
  40. nChannels = numel(dataEdges{1}.roiTCsInterp);
  41. receptiveFieldsAll = cell(4, nChannels);
  42. for iFly = 1: size(dataEdges, 1)
  43. % flyPathInd = iFly;
  44. for jStim = 2:5
  45. % Find index of matching stimulus.
  46. rfInd = find(strcmp(dataEdges{iFly, 1, jStim}.stimulusName, barStimFileNames));
  47. if ~isempty(rfInd)
  48. for kChannel = 1: nChannels
  49. nEpochs = numel(dataEdges{iFly, 1, jStim}.stimFrameInds);
  50. nCells = size(dataEdges{iFly, 1, jStim}.roiTCsInterp{kChannel}, 2);
  51. roiTCs = dataEdges{iFly, 1, jStim}.roiTCsInterp{kChannel};
  52. startInd = find(dataEdges{iFly, 1, jStim}.times == maxTStart(rfInd));
  53. % For better numerical tolerance.
  54. endInd = find(abs(dataEdges{iFly, 1, jStim}.times - minTEnd(rfInd)) < 1e-5);
  55. receptiveFields = squeeze(roiTCs);
  56. receptiveFields = receptiveFields(1: end - 1 * deleteLastROI, startInd : endInd);
  57. receptiveFieldsAll{rfInd, kChannel} = [receptiveFieldsAll{rfInd, kChannel}; receptiveFields];
  58. end
  59. end
  60. end
  61. clear receptiveFields
  62. end
  63. % Hard-coded error correction for some extra ROI.
  64. if contains(dataEdges{1}.flyPath, 'Tm9GCaMP6f-Tm4jRGECO1a')
  65. receptiveFieldsAll{1,1}(10, :) =[];
  66. end
  67. end