From f5894530ac0d75abe0894278182acff7a7e3148b Mon Sep 17 00:00:00 2001 From: gaelias Date: Wed, 1 May 2019 19:20:20 -0700 Subject: [PATCH 001/133] Create PositionInformationContent_SM.m --- Analyses/PositionInformationContent_SM.m | 380 +++++++++++++++++++++++ 1 file changed, 380 insertions(+) create mode 100644 Analyses/PositionInformationContent_SM.m diff --git a/Analyses/PositionInformationContent_SM.m b/Analyses/PositionInformationContent_SM.m new file mode 100644 index 0000000..3d4826f --- /dev/null +++ b/Analyses/PositionInformationContent_SM.m @@ -0,0 +1,380 @@ +function PositionInformationContent_SM + +origDir = cd; +[fileDir] = uigetdir(origDir); +if fileDir==0 + disp('Analysis Cancelled') + return +else + cd(fileDir) +end +dirContents = dir(fileDir); +fileNames = {dirContents.name}; + + +%% Load Relevant Data +load(fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'BehaviorMatrix'))}); +load(fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'EnsembleMatrix'))}); +load(fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'OrientationMatrix'))}); + +%% Pre-Process the Orientation Matrix Data +portPosX = strcmp(orientMatrixColIDs, 'PortX'); +portPosY = strcmp(orientMatrixColIDs, 'PortY'); +headPosX = strcmp(orientMatrixColIDs, 'HeadX'); +headPosY = strcmp(orientMatrixColIDs, 'HeadY'); +tailPosX = strcmp(orientMatrixColIDs, 'TailX'); +tailPosY = strcmp(orientMatrixColIDs, 'TailY'); + +portAngle = nan(size(orientMatrix,1),1); +headAngle = nan(size(orientMatrix,1),1); +tailAngle = nan(size(orientMatrix,1),1); + +htVal = nan(size(orientMatrix,1),1); +hpVal = nan(size(orientMatrix,1),1); +ptVal = nan(size(orientMatrix,1),1); + +posIndices = find(~isnan(orientMatrix(:,2))); + +for pos = 1:length(posIndices) + curPosNdx = posIndices(pos); + curPortX = orientMatrix(curPosNdx, portPosX); + curPortY = orientMatrix(curPosNdx, portPosY); + curHeadX = orientMatrix(curPosNdx, headPosX); + curHeadY = orientMatrix(curPosNdx, headPosY); + curTailX = orientMatrix(curPosNdx, tailPosX); + curTailY = orientMatrix(curPosNdx, tailPosY); + + htVal(curPosNdx) = sqrt((curTailX - curHeadX)^2 + (curTailY - curHeadY)^2); + hpVal(curPosNdx) = sqrt((curPortX - curHeadX)^2 + (curPortY - curHeadY)^2); + ptVal(curPosNdx) = sqrt((curTailX - curPortX)^2 + (curTailY - curPortY)^2); + + portAngle(curPosNdx) = rad2deg(acos((ptVal(curPosNdx)^2 + hpVal(curPosNdx)^2 - htVal(curPosNdx)^2)/(2*ptVal(curPosNdx)*hpVal(curPosNdx)))); + headAngle(curPosNdx) = rad2deg(acos((htVal(curPosNdx)^2 + hpVal(curPosNdx)^2 - ptVal(curPosNdx)^2)/(2*htVal(curPosNdx)*hpVal(curPosNdx)))); + tailAngle(curPosNdx) = rad2deg(acos((htVal(curPosNdx)^2 + ptVal(curPosNdx)^2 - hpVal(curPosNdx)^2)/(2*htVal(curPosNdx)*ptVal(curPosNdx)))); +end + +orientMatrix = [orientMatrix, portAngle, headAngle, tailAngle, htVal, hpVal, ptVal]; +orientMatrixColIDs = [orientMatrixColIDs, {'PortAngle'}, {'HeadAngle'}, {'TailAngle'}, {'HeadTailLength'}, {'HeadPortLength'}, {'PortTailLength'}]; + +%% Calculate Instantaneous Firing Rates +% For this I'm opting to use the instantaneous firing rate (i.e. convolved +% with a gaussian) because the camera capture rate is not exactly uniform, +% which is what is needed for using discrete time bins. This may lead to +% some distortion in the spiking value... I may want to do it with discrete +% bins down the road to verify I get similar values. + +% Create Gaussian +slideWindowSize = 50; +instFRgauss = gausswin(slideWindowSize); +instFRgauss = instFRgauss/(length(instFRgauss)*mode(diff(behavMatrix(:,1)))); + +% Convolve Gaussian with Binary Spike Trains +uniInstFR = [ensembleMatrix(:,1), nan(size(ensembleMatrix,1), size(ensembleMatrix,2)-1)]; +for uni = 2:size(ensembleMatrix,2) + uniInstFR(:,uni) = conv(ensembleMatrix(:,uni), instFRgauss, 'same'); +end + +%% Organize Trial Data +% Create Extraction Matrices +pokeInTrialMatrix = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.25 0.45], 'PokeIn'); +pokeOutTrialMatrix = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.45 0.25], 'PokeOut'); + +% Create Trial Logical Vectors +corrTrlLog = [pokeInTrialMatrix.Performance]; +isLog = [pokeInTrialMatrix.TranspositionDistance]==0; +pokeInNdxs = {pokeInTrialMatrix.PokeInIndex}; +pokeInNdxsISC = pokeInNdxs(corrTrlLog & isLog); + +% Extract Orientation Data +% Pull out the InSeq Correct Trials +pokeInOrientationAll = ExtractTrialData_SM(pokeInTrialMatrix, orientMatrix); %#ok<*NODEF> +pokeInOrientationISC = pokeInOrientationAll(corrTrlLog & isLog); +pokeOutOrientationAll = ExtractTrialData_SM(pokeOutTrialMatrix, orientMatrix); +pokeOutOrientationISC = pokeOutOrientationAll(corrTrlLog & isLog); +% Now concatenate them into a single cell vector +orientationTrialDataRaw = cellfun(@(a,b)[a;b], pokeInOrientationISC, pokeOutOrientationISC, 'uniformoutput', 0); +% Now correct the timestamps in the vector so that poke In and poke Out are +% equi-distant. +relTimeVects = cellfun(@(a,b,c)[a(:,1); b(:,1) - (b(1,1)-a(end,1)) + (b(2,1)-b(1,1))] - orientMatrix(c,1), pokeInOrientationISC, pokeOutOrientationISC, pokeInNdxsISC, 'uniformoutput',0); +orientationTrialData = cellfun(@(a,b)[a,b(:,2:end)], relTimeVects, orientationTrialDataRaw, 'uniformoutput', 0); + +% Extract Spike Data +pokeInFRall = ExtractTrialData_SM(pokeInTrialMatrix, uniInstFR); %#ok<*NODEF> +pokeInFRisc = pokeInFRall(corrTrlLog & isLog); +pokeOutFRall = ExtractTrialData_SM(pokeOutTrialMatrix, uniInstFR); +pokeOutFRisc = pokeOutFRall(corrTrlLog & isLog); +% Now concatenate them into a single cell vector +firingRateTrialDataRaw = cellfun(@(a,b)[a;b], pokeInFRisc, pokeOutFRisc, 'uniformoutput', 0); +relTimeVects = cellfun(@(a,b,c)[a(:,1); b(:,1) - (b(1,1)-a(end,1)) + (b(2,1)-b(1,1))] - orientMatrix(c,1), pokeInFRisc, pokeOutFRisc, pokeInNdxsISC, 'uniformoutput',0); +firingRateTrialData = cellfun(@(a,b)[a,b(:,2:end)], relTimeVects, firingRateTrialDataRaw, 'uniformoutput', 0); + +% Create Trial Position cell vectors +trialIDall = arrayfun(@(a,b)[ones(sum(a.TrialLogVect),1)*a.Odor; ones(sum(b.TrialLogVect),1)*b.Odor], pokeInTrialMatrix, pokeOutTrialMatrix, 'uniformoutput', 0); +trialIDvals = trialIDall(corrTrlLog & isLog); + +%% Calculate Standard Variables Used in Analysis +% Average Firing Rate +% For this I need to extract the firing rate values for each unit every +% time there's a position value taken. This is simply because I'm using the +% instantaneous firing rate for expedience. If I were simply binning the +% data into discrete time bins then there wouldn't be an issue, I would +% just calculate the average across bins. Since I'm not, I need to extract +% the instantaneous rate at each capture instance and compute the average +% from that. +% I'm using cellfun here to save space/time. +% I'm using the trial wise orientation data to create a logical vector of +% capture times and then applying it to the spike date. Since they're +% both organized by trial with the same indices extracted it's simple to +% do. +allFRvals = cell2mat(cellfun(@(a,b)b(~isnan(a(:,2)),2:end), orientationTrialData, firingRateTrialData, 'uniformoutput', 0)'); +allTimeVals = cell2mat(cellfun(@(a,b)b(~isnan(a(:,2)),1), orientationTrialData, firingRateTrialData, 'uniformoutput', 0)'); +meanFRvals = mean(allFRvals,1); + +% Pull out the trial position value to match each of the camera capture +% times +allTrialIDvals = cell2mat(cellfun(@(a,b)b(~isnan(a(:,2)),1), orientationTrialData, trialIDvals, 'uniformoutput', 0)'); + +% Orientation Occupancy Bins: Define spaces and calculate p(occupy) +% First break the orientation data out of the trial organization +allOrientData = cell2mat(cellfun(@(a)a(~isnan(a(:,2)),:), orientationTrialData, 'uniformoutput', 0)'); +% Head Position: Head X/Y +headXcolLog = strcmp(orientMatrixColIDs, 'HeadX'); +headXbins = 30:2:80; +headYcolLog = strcmp(orientMatrixColIDs, 'HeadY'); +headYbins = 240:300; +headOccupancyMatrix = histcounts2(allOrientData(:,headXcolLog), allOrientData(:,headYcolLog), headXbins, headYbins, 'Normalization', 'probability')'; +% Tail Position: Tail X/Y +tailXcolLog = strcmp(orientMatrixColIDs, 'TailX'); +tailXbins = 60:2:180; +tailYcolLog = strcmp(orientMatrixColIDs, 'TailY'); +tailYbins = 180:5:260; +tailOccupancyMatrix = histcounts2(allOrientData(:,tailXcolLog), allOrientData(:,tailYcolLog), tailXbins, tailYbins, 'Normalization', 'probability')'; +% Head Angle: Angle at the head between the port and the tail +headAngleColLog = strcmp(orientMatrixColIDs, 'HeadAngle'); +headAngleBins = 0:15:180; +headAngleOccupancyVector = histcounts(allOrientData(:,headAngleColLog), headAngleBins, 'Normalization', 'probability'); +% Tail Angle: Angle at the tail between the port and the head +tailAngleColLog = strcmp(orientMatrixColIDs, 'TailAngle'); +tailAngleBins = 0:15:180; +tailAngleOccupancyVector = histcounts(allOrientData(:,tailAngleColLog), tailAngleBins, 'Normalization', 'probability'); +% Port Angle: Angle at the port between the head and the tail +portAngleColLog = strcmp(orientMatrixColIDs, 'PortAngle'); +portAngleBins = 0:15:180; +portAngleOccupancyVector = histcounts(allOrientData(:,portAngleColLog), portAngleBins, 'Normalization', 'probability'); + +% Trial Position Occupancy Bins: Here I'm considering position to be a 2-D +% where the x-axis is Time and the Y-axis is trial position +posBins = 0.5:4.5; +timeBins = firingRateTrialData{1}(1,1)-0.05:0.05:firingRateTrialData{1}(end,1)+0.05; +timeBinOccupancyMatrix = histcounts2(allTimeVals, allTrialIDvals, timeBins, posBins, 'Normalization', 'probability')'; + +%% Run Analyses +for uni = 1:length(ensembleUnitSummaries) + %% Port Orientation Alone Analyses + % Head Position Information + [headFRmap, headICmap, headIC] = CalculateFieldIC(allOrientData(:,headXcolLog), allOrientData(:,headYcolLog),... + headXbins, headYbins, headOccupancyMatrix, allFRvals(:,uni), meanFRvals(uni)); + % Tail Position Information + [tailFRmap, tailICmap, tailIC] = CalculateFieldIC(allOrientData(:,tailXcolLog), allOrientData(:,tailYcolLog),... + tailXbins, tailYbins, tailOccupancyMatrix, allFRvals(:,uni), meanFRvals(uni)); + + % Head Angle Information + [headAngleFRvect, headAngleICvect, headAngleICval] = CalculateVectorIC(allOrientData(:,headAngleColLog),... + headAngleBins, headAngleOccupancyVector, allFRvals(:,uni), meanFRvals(uni)); + % Tail Angle Information + [tailAngleFRvect, tailAngleICvect, tailAngleICval] = CalculateVectorIC(allOrientData(:,tailAngleColLog),... + tailAngleBins, tailAngleOccupancyVector, allFRvals(:,uni), meanFRvals(uni)); + % Port Angle Information + [portAngleFRvect, portAngleICvect, portAngleICval] = CalculateVectorIC(allOrientData(:,portAngleColLog),... + portAngleBins, portAngleOccupancyVector, allFRvals(:,uni), meanFRvals(uni)); + + + figure + % Head Occupancy Matrix + subplot(3,3,1) + imagesc(headOccupancyMatrix); + set(gca, 'ydir', 'normal'); + title('Head Occupancy'); + cb = colorbar('southoutside'); + cb.Label.String = 'p(Occupancy)'; + z = colormap('jet'); + z(1,:) = [1 1 1]; + colormap(z); + % Head FR Map + subplot(3,3,2) + imagesc(headFRmap); + set(gca, 'ydir', 'normal'); + title('Head Position FR'); + cb = colorbar('southoutside'); + cb.Label.String = 'Mean Firing Rate (spk/s)'; + colormap(z); + % Head IC Map + subplot(3,3,3) + imagesc(headICmap); + set(gca, 'ydir', 'normal'); + title(sprintf('Head Position IC (Overall = %.03f bits)', headIC)); + cb = colorbar('southoutside'); + cb.Label.String = 'Information Content (bits)'; + colormap(z); + + % Tail Occupancy Matrix + subplot(3,3,4) + imagesc(tailOccupancyMatrix); + set(gca, 'ydir', 'normal'); + title('Tail Occupancy'); + cb = colorbar('southoutside'); + cb.Label.String = 'p(Occupancy)'; + colormap(z); + % Head FR Map + subplot(3,3,5) + imagesc(tailFRmap); + set(gca, 'ydir', 'normal'); + title('Tail Position FR'); + cb = colorbar('southoutside'); + cb.Label.String = 'Mean Firing Rate (spk/s)'; + colormap(z); + % Head IC Map + subplot(3,3,6) + imagesc(tailICmap); + set(gca, 'ydir', 'normal'); + title(sprintf('Tail Position IC (Overall = %.03f bits)', tailIC)); + cb = colorbar('southoutside'); + cb.Label.String = 'Information Content (bits)'; + colormap(z); + + % Head Angle + subplot(3,3,7) + yyaxis left + plot(headAngleBins(2:end) - diff(headAngleBins)/2, headAngleOccupancyVector, 'linestyle', '--', 'color', 'k'); + hold on; + yyaxis right + plot(headAngleBins(2:end) - diff(headAngleBins)/2, headAngleFRvect, 'linestyle', '-', 'color', 'k'); + yyaxis left + plot(headAngleBins(2:end) - diff(headAngleBins)/2, headAngleICvect, 'linestyle', '-', 'color', 'r'); + box off; + yyaxis left + ylabel 'Occupancy OR IC (bits)' + yyaxis right + ylabel 'Firing Rate' + legend('Occ', 'FR', 'IC', 'location', 'best'); + title(sprintf('Head Angle (Overall IC = %.03f bits', headAngleICval)); + + subplot(3,3,8) + yyaxis left + plot(tailAngleBins(2:end) - diff(tailAngleBins)/2, tailAngleOccupancyVector, 'linestyle', '--', 'color', 'k'); + hold on; + yyaxis right + plot(tailAngleBins(2:end) - diff(tailAngleBins)/2, tailAngleFRvect, 'linestyle', '-', 'color', 'k'); + yyaxis left + plot(tailAngleBins(2:end) - diff(tailAngleBins)/2, tailAngleICvect, 'linestyle', '-', 'color', 'r'); + box off; + yyaxis left + ylabel 'Occupancy OR IC (bits)' + yyaxis right + ylabel 'Firing Rate' + legend('Occ', 'FR', 'IC', 'location', 'best'); + title(sprintf('Tail Angle (Overall IC = %.03f bits', tailAngleICval)); + + subplot(3,3,9) + yyaxis left + plot(portAngleBins(2:end) - diff(portAngleBins)/2, portAngleOccupancyVector, 'linestyle', '--', 'color', 'k'); + hold on; + yyaxis right + plot(portAngleBins(2:end) - diff(portAngleBins)/2, portAngleFRvect, 'linestyle', '-', 'color', 'k'); + yyaxis left + plot(portAngleBins(2:end) - diff(portAngleBins)/2, portAngleICvect, 'linestyle', '-', 'color', 'r'); + box off; + yyaxis left + ylabel 'Occupancy OR IC (bits)' + yyaxis right + ylabel 'Firing Rate' + legend('Occ', 'FR', 'IC', 'location', 'best'); + title(sprintf('Port Angle (Overall IC = %.03f bits', portAngleICval)); + + annotation('textbox', [0.05 0.9 0.9 0.1], 'String', ensembleMatrixColIDs{uni+1}, 'linestyle', 'none', 'FontSize', 20); %#ok + annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string', sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); + drawnow + + orient(gcf, 'tall'); + orient(gcf, 'landscape'); +% print + print('-painters', gcf, '-dpdf', sprintf('%s_OrientInfo_Summary', ensembleMatrixColIDs{uni+1})); + close(gcf); + + %% Trial Position Alone + % Trial Position + [trialFRmap, trialICmap, trialIC] = CalculateFieldIC(allTimeVals, allTrialIDvals,... + timeBins, posBins, timeBinOccupancyMatrix, allFRvals(:,uni), meanFRvals(uni)); + + figure; + subplot(1,3,1) + imagesc(firingRateTrialData{1}(1,1):0.1:firingRateTrialData{1}(end,1),1:4,timeBinOccupancyMatrix); + set(gca, 'ytick', 1:4); + hold on; + line([0 0], [0.5 4.5], 'linewidth', 2, 'color', 'k'); + title('Trial Occupancy'); + cb = colorbar('southoutside'); + cb.Label.String = 'p(Occupancy)'; + z = colormap('jet'); + z(1,:) = [1 1 1]; + colormap(z); + + subplot(1,3,2) + imagesc(firingRateTrialData{1}(1,1):0.1:firingRateTrialData{1}(end,1),1:4,trialFRmap); + set(gca, 'ytick', 1:4); + hold on; + line([0 0], [0.5 4.5], 'linewidth', 2, 'color', 'k'); +% set(gca, 'ydir', 'normal'); + title('Trial Position FR'); + cb = colorbar('southoutside'); + cb.Label.String = 'Mean Firing Rate (spk/s)'; + colormap(z); + + subplot(1,3,3) + imagesc(firingRateTrialData{1}(1,1):0.1:firingRateTrialData{1}(end,1),1:4,trialICmap); + set(gca, 'ytick', 1:4); + hold on; + line([0 0], [0.5 4.5], 'linewidth', 2, 'color', 'k'); +% set(gca, 'ydir', 'normal'); + title(sprintf('Trial Position IC (Overall = %.03f bits)', trialIC)); + cb = colorbar('southoutside'); + cb.Label.String = 'Information Content (bits)'; + colormap(z); + + annotation('textbox', [0.05 0.9 0.9 0.1], 'String', ensembleMatrixColIDs{uni+1}, 'linestyle', 'none', 'FontSize', 20); + annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string', sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); + drawnow + + orient(gcf, 'tall'); + orient(gcf, 'landscape'); +% print + print('-painters', gcf, '-dpdf', sprintf('%s_PositionInfo_Summary', ensembleMatrixColIDs{uni+1})); + close(gcf); +end + +end + +function [frMap, icMap, totalICval] = CalculateFieldIC(curXdata, curYdata, xBinLims, yBinLims, occupancyMatrix, uniFR, meanFR) +frMap = nan(length(yBinLims)-1, length(xBinLims)-1); +icMap = nan(length(yBinLims)-1, length(xBinLims)-1); +for r = 1:length(yBinLims)-1 + yValLog = (curYdata>=yBinLims(r) & curYdata=xBinLims(c) & curXdata=xBinLims(c) & curXdata Date: Thu, 2 May 2019 18:23:16 -0700 Subject: [PATCH 002/133] Update PositionInformationContent_SM.m --- Analyses/PositionInformationContent_SM.m | 242 ++++++++++++++++++++--- 1 file changed, 219 insertions(+), 23 deletions(-) diff --git a/Analyses/PositionInformationContent_SM.m b/Analyses/PositionInformationContent_SM.m index 3d4826f..eeebe4e 100644 --- a/Analyses/PositionInformationContent_SM.m +++ b/Analyses/PositionInformationContent_SM.m @@ -11,6 +11,10 @@ dirContents = dir(fileDir); fileNames = {dirContents.name}; +%% Define Standard Variables +timeBinSize = 0.025; +slideWindowSize = 30; +spatialBinSize = 2; %% Load Relevant Data load(fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'BehaviorMatrix'))}); @@ -64,7 +68,6 @@ % bins down the road to verify I get similar values. % Create Gaussian -slideWindowSize = 50; instFRgauss = gausswin(slideWindowSize); instFRgauss = instFRgauss/(length(instFRgauss)*mode(diff(behavMatrix(:,1)))); @@ -84,6 +87,7 @@ isLog = [pokeInTrialMatrix.TranspositionDistance]==0; pokeInNdxs = {pokeInTrialMatrix.PokeInIndex}; pokeInNdxsISC = pokeInNdxs(corrTrlLog & isLog); +odorIDsISC = [pokeInTrialMatrix(corrTrlLog & isLog).Odor]; % Extract Orientation Data % Pull out the InSeq Correct Trials @@ -135,20 +139,23 @@ allTrialIDvals = cell2mat(cellfun(@(a,b)b(~isnan(a(:,2)),1), orientationTrialData, trialIDvals, 'uniformoutput', 0)'); % Orientation Occupancy Bins: Define spaces and calculate p(occupy) -% First break the orientation data out of the trial organization +% First break the orientation data out of the trial organization. +% Note: 1pixel = ~1.7mm allOrientData = cell2mat(cellfun(@(a)a(~isnan(a(:,2)),:), orientationTrialData, 'uniformoutput', 0)'); % Head Position: Head X/Y headXcolLog = strcmp(orientMatrixColIDs, 'HeadX'); -headXbins = 30:2:80; +headXbins = 30:spatialBinSize:90; headYcolLog = strcmp(orientMatrixColIDs, 'HeadY'); -headYbins = 240:300; +headYbins = 240:spatialBinSize:300; headOccupancyMatrix = histcounts2(allOrientData(:,headXcolLog), allOrientData(:,headYcolLog), headXbins, headYbins, 'Normalization', 'probability')'; +headOccupancyMatrix(headOccupancyMatrix==0) = nan; % Tail Position: Tail X/Y tailXcolLog = strcmp(orientMatrixColIDs, 'TailX'); -tailXbins = 60:2:180; +tailXbins = 60:spatialBinSize:180; tailYcolLog = strcmp(orientMatrixColIDs, 'TailY'); -tailYbins = 180:5:260; +tailYbins = 180:spatialBinSize:264; tailOccupancyMatrix = histcounts2(allOrientData(:,tailXcolLog), allOrientData(:,tailYcolLog), tailXbins, tailYbins, 'Normalization', 'probability')'; +tailOccupancyMatrix(tailOccupancyMatrix==0) = nan; % Head Angle: Angle at the head between the port and the tail headAngleColLog = strcmp(orientMatrixColIDs, 'HeadAngle'); headAngleBins = 0:15:180; @@ -165,27 +172,61 @@ % Trial Position Occupancy Bins: Here I'm considering position to be a 2-D % where the x-axis is Time and the Y-axis is trial position posBins = 0.5:4.5; -timeBins = firingRateTrialData{1}(1,1)-0.05:0.05:firingRateTrialData{1}(end,1)+0.05; +timeBins = firingRateTrialData{1}(1,1)-timeBinSize:timeBinSize:firingRateTrialData{1}(end,1)+timeBinSize; timeBinOccupancyMatrix = histcounts2(allTimeVals, allTrialIDvals, timeBins, posBins, 'Normalization', 'probability')'; +%% Analyze the Orientation Data +% For this I will be breaking down the head and tail spatial position by +% trial position and analyzing the variation across and within trials. +tPosHeadOccupyFig = PlotTrialPositionOccupancy(allOrientData, orientationTrialData, headXcolLog, headYcolLog, headXbins, headYbins, allTrialIDvals, odorIDsISC, timeBins, 'Head'); +figure(tPosHeadOccupyFig); +annotation('textbox', [0.2 0.9 0.8 0.1], 'String', sprintf('Spatial Bin = %.02fmm, Temporal Bin = %.02fms', spatialBinSize*1.7, timeBinSize*1000), 'HorizontalAlignment', 'right', 'linestyle', 'none', 'FontSize', 12); +annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string', sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); +orient(tPosHeadOccupyFig, 'tall'); +orient(tPosHeadOccupyFig, 'landscape'); +% print +print('-painters', tPosHeadOccupyFig, '-dpdf', 'HeadLocationTrialPositionSummary'); +tPosTailOccupyFig = PlotTrialPositionOccupancy(allOrientData, orientationTrialData, tailXcolLog, tailYcolLog, tailXbins, tailYbins, allTrialIDvals, odorIDsISC, timeBins, 'Tail'); +figure(tPosTailOccupyFig); +annotation('textbox', [0.2 0.9 0.8 0.1], 'String', sprintf('Spatial Bin = %.02fmm, Temporal Bin = %.02fms', spatialBinSize*1.7, timeBinSize*1000), 'HorizontalAlignment', 'right', 'linestyle', 'none', 'FontSize', 12); +annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string', sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); +orient(tPosTailOccupyFig, 'tall'); +orient(tPosTailOccupyFig, 'landscape'); +% print +print('-painters', tPosTailOccupyFig, '-dpdf', 'TailLocationTrialPositionSummary'); +% Compute the deviation (distance) for the Head and Tail during the first +% frame of every trial from the spatial position on trial #1 in the +% sequence + +% Compute the deviation (distance) of the Head and Tail during the trial +% period for every trial + +% Calculate the spatial position for Head and Tail for each trial position +% for the entire trial period + +% Calculate the difference across trial positions for the spatial position +% matrix (just do subtraction for now) and plot the difference in grayscale + + + %% Run Analyses for uni = 1:length(ensembleUnitSummaries) %% Port Orientation Alone Analyses % Head Position Information - [headFRmap, headICmap, headIC] = CalculateFieldIC(allOrientData(:,headXcolLog), allOrientData(:,headYcolLog),... + [headFRmap, headICmap, headIC, headICrate] = CalculateFieldIC(allOrientData(:,headXcolLog), allOrientData(:,headYcolLog),... headXbins, headYbins, headOccupancyMatrix, allFRvals(:,uni), meanFRvals(uni)); % Tail Position Information - [tailFRmap, tailICmap, tailIC] = CalculateFieldIC(allOrientData(:,tailXcolLog), allOrientData(:,tailYcolLog),... + [tailFRmap, tailICmap, tailIC, tailICrate] = CalculateFieldIC(allOrientData(:,tailXcolLog), allOrientData(:,tailYcolLog),... tailXbins, tailYbins, tailOccupancyMatrix, allFRvals(:,uni), meanFRvals(uni)); % Head Angle Information - [headAngleFRvect, headAngleICvect, headAngleICval] = CalculateVectorIC(allOrientData(:,headAngleColLog),... + [headAngleFRvect, headAngleICvect, headAngleICval, headAngleICrate] = CalculateVectorIC(allOrientData(:,headAngleColLog),... headAngleBins, headAngleOccupancyVector, allFRvals(:,uni), meanFRvals(uni)); % Tail Angle Information - [tailAngleFRvect, tailAngleICvect, tailAngleICval] = CalculateVectorIC(allOrientData(:,tailAngleColLog),... + [tailAngleFRvect, tailAngleICvect, tailAngleICval, tailAngleICrate] = CalculateVectorIC(allOrientData(:,tailAngleColLog),... tailAngleBins, tailAngleOccupancyVector, allFRvals(:,uni), meanFRvals(uni)); % Port Angle Information - [portAngleFRvect, portAngleICvect, portAngleICval] = CalculateVectorIC(allOrientData(:,portAngleColLog),... + [portAngleFRvect, portAngleICvect, portAngleICval, portAngleICrate] = CalculateVectorIC(allOrientData(:,portAngleColLog),... portAngleBins, portAngleOccupancyVector, allFRvals(:,uni), meanFRvals(uni)); @@ -200,6 +241,7 @@ z = colormap('jet'); z(1,:) = [1 1 1]; colormap(z); + set(gca, 'clim', [-0.01, max(get(gca, 'clim'))]); % Head FR Map subplot(3,3,2) imagesc(headFRmap); @@ -212,7 +254,7 @@ subplot(3,3,3) imagesc(headICmap); set(gca, 'ydir', 'normal'); - title(sprintf('Head Position IC (Overall = %.03f bits)', headIC)); + title(sprintf('Head Position IC (Overall = %.03f bits | %.03f bit/sec)', headIC, headICrate)); cb = colorbar('southoutside'); cb.Label.String = 'Information Content (bits)'; colormap(z); @@ -225,6 +267,7 @@ cb = colorbar('southoutside'); cb.Label.String = 'p(Occupancy)'; colormap(z); + set(gca, 'clim', [-0.01, max(get(gca, 'clim'))]); % Head FR Map subplot(3,3,5) imagesc(tailFRmap); @@ -237,7 +280,7 @@ subplot(3,3,6) imagesc(tailICmap); set(gca, 'ydir', 'normal'); - title(sprintf('Tail Position IC (Overall = %.03f bits)', tailIC)); + title(sprintf('Tail Position IC (Overall = %.03f bits | %.03f bit/sec)', tailIC, tailICrate)); cb = colorbar('southoutside'); cb.Label.String = 'Information Content (bits)'; colormap(z); @@ -257,7 +300,7 @@ yyaxis right ylabel 'Firing Rate' legend('Occ', 'FR', 'IC', 'location', 'best'); - title(sprintf('Head Angle (Overall IC = %.03f bits', headAngleICval)); + title(sprintf('Head Angle (Overall IC = %.03f bits | %.03f bit/sec)', headAngleICval, headAngleICrate)); subplot(3,3,8) yyaxis left @@ -273,7 +316,7 @@ yyaxis right ylabel 'Firing Rate' legend('Occ', 'FR', 'IC', 'location', 'best'); - title(sprintf('Tail Angle (Overall IC = %.03f bits', tailAngleICval)); + title(sprintf('Tail Angle (Overall IC = %.03f bits | %.03f bit/sec)', tailAngleICval, tailAngleICrate)); subplot(3,3,9) yyaxis left @@ -289,9 +332,10 @@ yyaxis right ylabel 'Firing Rate' legend('Occ', 'FR', 'IC', 'location', 'best'); - title(sprintf('Port Angle (Overall IC = %.03f bits', portAngleICval)); + title(sprintf('Port Angle (Overall IC = %.03f bits | %.03f bit/sec)', portAngleICval, portAngleICrate)); annotation('textbox', [0.05 0.9 0.9 0.1], 'String', ensembleMatrixColIDs{uni+1}, 'linestyle', 'none', 'FontSize', 20); %#ok + annotation('textbox', [0.2 0.9 0.8 0.1], 'String', sprintf('Spatial Bin = %.02fmm, Gaussian = %.02fms', spatialBinSize*1.7, slideWindowSize), 'HorizontalAlignment', 'right', 'linestyle', 'none', 'FontSize', 12); annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string', sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); drawnow @@ -303,7 +347,7 @@ %% Trial Position Alone % Trial Position - [trialFRmap, trialICmap, trialIC] = CalculateFieldIC(allTimeVals, allTrialIDvals,... + [trialFRmap, trialICmap, trialIC, trialICrate] = CalculateFieldIC(allTimeVals, allTrialIDvals,... timeBins, posBins, timeBinOccupancyMatrix, allFRvals(:,uni), meanFRvals(uni)); figure; @@ -318,6 +362,7 @@ z = colormap('jet'); z(1,:) = [1 1 1]; colormap(z); + set(gca, 'clim', [-0.01, max(get(gca, 'clim'))]); subplot(1,3,2) imagesc(firingRateTrialData{1}(1,1):0.1:firingRateTrialData{1}(end,1),1:4,trialFRmap); @@ -336,12 +381,13 @@ hold on; line([0 0], [0.5 4.5], 'linewidth', 2, 'color', 'k'); % set(gca, 'ydir', 'normal'); - title(sprintf('Trial Position IC (Overall = %.03f bits)', trialIC)); + title(sprintf('Trial Position IC (Overall = %.03f bits | %.03f bit/sec)', trialIC, trialICrate)); cb = colorbar('southoutside'); cb.Label.String = 'Information Content (bits)'; colormap(z); annotation('textbox', [0.05 0.9 0.9 0.1], 'String', ensembleMatrixColIDs{uni+1}, 'linestyle', 'none', 'FontSize', 20); + annotation('textbox', [0.2 0.9 0.8 0.1], 'String', sprintf('Temporal Bin = %.02fmm, Gaussian = %.02fms', timeBinSize*1000, slideWindowSize), 'HorizontalAlignment', 'right', 'linestyle', 'none', 'FontSize', 12); annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string', sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); drawnow @@ -353,8 +399,8 @@ end end - -function [frMap, icMap, totalICval] = CalculateFieldIC(curXdata, curYdata, xBinLims, yBinLims, occupancyMatrix, uniFR, meanFR) +%% +function [frMap, icMap, totalICval, overallICrate] = CalculateFieldIC(curXdata, curYdata, xBinLims, yBinLims, occupancyMatrix, uniFR, meanFR) frMap = nan(length(yBinLims)-1, length(xBinLims)-1); icMap = nan(length(yBinLims)-1, length(xBinLims)-1); for r = 1:length(yBinLims)-1 @@ -366,9 +412,10 @@ end end totalICval = nansum(icMap(:)); +overallICrate = totalICval*meanFR; end - -function [frVect, icVect, totalICval] = CalculateVectorIC(curXdata, xBinLims, occupancyVector, uniFR, meanFR) +%% +function [frVect, icVect, totalICval, overallICrate] = CalculateVectorIC(curXdata, xBinLims, occupancyVector, uniFR, meanFR) frVect = nan(1,length(xBinLims)-1); icVect = nan(1,length(xBinLims)-1); for c = 1:length(xBinLims)-1 @@ -377,4 +424,153 @@ icVect(c) = occupancyVector(c)*(frVect(c)/meanFR)*log2(frVect(c)/meanFR); end totalICval = nansum(icVect); +overallICrate = totalICval*meanFR; +end + +%% +function [curTrialOccupancyPlot] = PlotTrialPositionOccupancy(allOrientData, orientationTrialData, xColLog, yColLog, xBins, yBins, allTrialIDvals, odorIDsISC, timeBins, feature) + +trialOccupancy = cell(1,4); +for pos = 1:4 + trialOccupancy{pos} = histcounts2(allOrientData(allTrialIDvals==pos,xColLog), allOrientData(allTrialIDvals==pos,yColLog), xBins, yBins, 'Normalization', 'probability')'; + trialOccupancy{pos}(trialOccupancy{pos}==0) = nan; +end + +% This is going to be hard coded unfortunately, but I think that's going to +% be the easiest way to handle it. +curTrialOccupancyPlot = figure; +jMod = colormap('jet'); +jMod(1,:) = [1 1 1]; +pMod = colormap('parula'); +pMod(1,:) = [1 1 1]; + +pos1 = subplot(4,4,1); +imagesc(trialOccupancy{1}); +set(pos1, 'ydir', 'normal', 'clim', [-0.01, max(get(gca, 'clim'))]); +title(sprintf('Trial 1 %s Location', feature)); +colormap(pos1, jMod); + +pos2 = subplot(4,4,6); +imagesc(trialOccupancy{2}); +set(pos2, 'ydir', 'normal', 'clim', [-0.01, max(get(gca, 'clim'))]); +title(sprintf('Trial 2 %s Location', feature)); +colormap(pos2, jMod); + +pos3 = subplot(4,4,11); +imagesc(trialOccupancy{3}); +set(pos3, 'ydir', 'normal', 'clim', [-0.01, max(get(gca, 'clim'))]); +title(sprintf('Trial 3 %s Location', feature)); +colormap(pos3, jMod); + +pos4 = subplot(4,4,16); +imagesc(trialOccupancy{4}); +set(pos4, 'ydir', 'normal', 'clim', [-0.01, max(get(gca, 'clim'))]); +title(sprintf('Trial 4 %s Location', feature)); +colormap(pos4, jMod); + +% Now, zero out the nans... I know I nan'd out my zeros before. That's +% better for the bulk of the analyses, this is better for this analysis. +for t = 1:4 + trialOccupancy{t}(isnan(trialOccupancy{t})) = 0; +end + +pos2m1 = subplot(4,4,2); +curDiffMtx = CalculateOccupyDiffMtx(trialOccupancy{1}, trialOccupancy{2}); +imagesc(curDiffMtx); +set(pos2m1, 'ydir', 'normal', 'clim', [min(get(gca, 'clim'))-0.01, max(get(gca, 'clim'))]); +title('Trial 2 - Trial 1'); +colormap(pos2m1, pMod); + +pos3m1 = subplot(4,4,3); +curDiffMtx = CalculateOccupyDiffMtx(trialOccupancy{1}, trialOccupancy{3}); +imagesc(curDiffMtx); +set(pos3m1, 'ydir', 'normal', 'clim', [min(get(gca, 'clim'))-0.01, max(get(gca, 'clim'))]); +title('Trial 3 - Trial 1'); +colormap(pos3m1, pMod); + +pos4m1 = subplot(4,4,4); +curDiffMtx = CalculateOccupyDiffMtx(trialOccupancy{1}, trialOccupancy{4}); +imagesc(curDiffMtx); +set(pos4m1, 'ydir', 'normal', 'clim', [min(get(gca, 'clim'))-0.01, max(get(gca, 'clim'))]); +title('Trial 4 - Trial 1'); +colormap(pos4m1, pMod); + +pos3m2 = subplot(4,4,7); +curDiffMtx = CalculateOccupyDiffMtx(trialOccupancy{2}, trialOccupancy{3}); +imagesc(curDiffMtx); +set(pos3m2, 'ydir', 'normal', 'clim', [min(get(gca, 'clim'))-0.01, max(get(gca, 'clim'))]); +title('Trial 3 - Trial 2'); +colormap(pos3m2, pMod); + +pos4m2 = subplot(4,4,8); +curDiffMtx = CalculateOccupyDiffMtx(trialOccupancy{2}, trialOccupancy{4}); +imagesc(curDiffMtx); +set(pos4m2, 'ydir', 'normal', 'clim', [min(get(gca, 'clim'))-0.01, max(get(gca, 'clim'))]); +title('Trial 4 - Trial 2'); +colormap(pos4m2, pMod); + +pos4m3 = subplot(4,4,12); +curDiffMtx = CalculateOccupyDiffMtx(trialOccupancy{3}, trialOccupancy{4}); +imagesc(curDiffMtx); +set(pos4m3, 'ydir', 'normal', 'clim', [min(get(gca, 'clim'))-0.01, max(get(gca, 'clim'))]); +title('Trial 4 - Trial 3'); +colormap(pos4m3, pMod); + +%% Extract spatial position in the first frame after poke in for every trial +xFF = nan(1,length(orientationTrialData)); +yFF = nan(1,length(orientationTrialData)); +trialMoveVect = cell(1,length(orientationTrialData)); +for trl = 1:length(orientationTrialData) + curTrlData = orientationTrialData{trl}; + curTrlData(isnan(curTrlData(:,2)),:) = []; + xFF(trl) = curTrlData(find(curTrlData(:,1)>=0, 1, 'first'), xColLog); + yFF(trl) = curTrlData(find(curTrlData(:,1)>=0, 1, 'first'), yColLog); + trialMoveVect{trl} = [curTrlData(:,1), cell2mat(arrayfun(@(a,b)sqrt(((a-xFF(trl))^2)+((b-yFF(trl))^2)), curTrlData(:,xColLog), curTrlData(:,yColLog), 'uniformoutput', 0))]; +end +% Compute the average spatial position for Head and Tail across trial +% positions +trialHeadXff = nan(2,4); +trialHeadYff = nan(2,4); +trialMoveVals = repmat({nan(2,length(timeBins)-1)}, [1 4]); +for pos = 1:4 + trialHeadXff(1,pos) = mean(xFF(odorIDsISC==pos)); + trialHeadXff(2,pos) = std(xFF(odorIDsISC==pos)); + + trialHeadYff(1,pos) = mean(yFF(odorIDsISC==pos)); + trialHeadYff(2,pos) = std(yFF(odorIDsISC==pos)); + curPosTrlVects = cell2mat(trialMoveVect(odorIDsISC==pos)'); + tempDistVect = trialMoveVals{pos}; + for tb = 1:length(timeBins)-1 + tempDistVect(1,tb) = mean(curPosTrlVects(curPosTrlVects(:,1)>=timeBins(tb) & curPosTrlVects(:,1)=timeBins(tb) & curPosTrlVects(:,1) Date: Thu, 9 May 2019 14:36:50 -0700 Subject: [PATCH 003/133] Create PositionInformationFval_SM.m --- Analyses/PositionInformationFval_SM.m | 238 ++++++++++++++++++++++++++ 1 file changed, 238 insertions(+) create mode 100644 Analyses/PositionInformationFval_SM.m diff --git a/Analyses/PositionInformationFval_SM.m b/Analyses/PositionInformationFval_SM.m new file mode 100644 index 0000000..90e1802 --- /dev/null +++ b/Analyses/PositionInformationFval_SM.m @@ -0,0 +1,238 @@ + +origDir = cd; +[fileDir] = uigetdir(origDir); +if fileDir==0 + disp('Analysis Cancelled') + return +else + cd(fileDir) +end +dirContents = dir(fileDir); +fileNames = {dirContents.name}; + +%% Define Standard Variables +slideWindowSize = 30; +spatialBinSize = 2; +numPerms = 10; + +%% Load Relevant Data +load(fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'BehaviorMatrix'))}); +load(fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'EnsembleMatrix'))}); +load(fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'OrientationMatrix'))}); + +%% Pre-Process the Orientation Matrix Data +portPosX = strcmp(orientMatrixColIDs, 'PortX'); +portPosY = strcmp(orientMatrixColIDs, 'PortY'); +headPosX = strcmp(orientMatrixColIDs, 'HeadX'); +headPosY = strcmp(orientMatrixColIDs, 'HeadY'); +tailPosX = strcmp(orientMatrixColIDs, 'TailX'); +tailPosY = strcmp(orientMatrixColIDs, 'TailY'); + +portAngle = nan(size(orientMatrix,1),1); +headAngle = nan(size(orientMatrix,1),1); +tailAngle = nan(size(orientMatrix,1),1); + +htVal = nan(size(orientMatrix,1),1); +hpVal = nan(size(orientMatrix,1),1); +ptVal = nan(size(orientMatrix,1),1); + +posIndices = find(~isnan(orientMatrix(:,2))); + +for pos = 1:length(posIndices) + curPosNdx = posIndices(pos); + curPortX = orientMatrix(curPosNdx, portPosX); + curPortY = orientMatrix(curPosNdx, portPosY); + curHeadX = orientMatrix(curPosNdx, headPosX); + curHeadY = orientMatrix(curPosNdx, headPosY); + curTailX = orientMatrix(curPosNdx, tailPosX); + curTailY = orientMatrix(curPosNdx, tailPosY); + + htVal(curPosNdx) = sqrt((curTailX - curHeadX)^2 + (curTailY - curHeadY)^2); + hpVal(curPosNdx) = sqrt((curPortX - curHeadX)^2 + (curPortY - curHeadY)^2); + ptVal(curPosNdx) = sqrt((curTailX - curPortX)^2 + (curTailY - curPortY)^2); + + portAngle(curPosNdx) = rad2deg(acos((ptVal(curPosNdx)^2 + hpVal(curPosNdx)^2 - htVal(curPosNdx)^2)/(2*ptVal(curPosNdx)*hpVal(curPosNdx)))); + headAngle(curPosNdx) = rad2deg(acos((htVal(curPosNdx)^2 + hpVal(curPosNdx)^2 - ptVal(curPosNdx)^2)/(2*htVal(curPosNdx)*hpVal(curPosNdx)))); + tailAngle(curPosNdx) = rad2deg(acos((htVal(curPosNdx)^2 + ptVal(curPosNdx)^2 - hpVal(curPosNdx)^2)/(2*htVal(curPosNdx)*ptVal(curPosNdx)))); +end + +orientMatrix = [orientMatrix, portAngle, headAngle, tailAngle, htVal, hpVal, ptVal]; +orientMatrixColIDs = [orientMatrixColIDs, {'PortAngle'}, {'HeadAngle'}, {'TailAngle'}, {'HeadTailLength'}, {'HeadPortLength'}, {'PortTailLength'}]; + +% Now Interpolate the port/head/tail locations. No interpolation of the +% other vals right now... +orientMatrix(:,portPosX) = interp1(orientMatrix(posIndices,1), orientMatrix(posIndices,portPosX), orientMatrix(:,1)); +orientMatrix(:,portPosY) = interp1(orientMatrix(posIndices,1), orientMatrix(posIndices,portPosY), orientMatrix(:,1)); +orientMatrix(:,headPosX) = interp1(orientMatrix(posIndices,1), orientMatrix(posIndices,headPosX), orientMatrix(:,1)); +orientMatrix(:,headPosY) = interp1(orientMatrix(posIndices,1), orientMatrix(posIndices,headPosY), orientMatrix(:,1)); +orientMatrix(:,tailPosX) = interp1(orientMatrix(posIndices,1), orientMatrix(posIndices,tailPosX), orientMatrix(:,1)); +orientMatrix(:,tailPosY) = interp1(orientMatrix(posIndices,1), orientMatrix(posIndices,tailPosY), orientMatrix(:,1)); + +%% Calculate Instantaneous Firing Rates +% For this I'm opting to use the instantaneous firing rate (i.e. convolved +% with a gaussian) because the camera capture rate is not exactly uniform, +% which is what is needed for using discrete time bins. This may lead to +% some distortion in the spiking value... I may want to do it with discrete +% bins down the road to verify I get similar values. + +% Convolve w/Gaussian +% Create Gaussian +instFRgauss = gausswin(slideWindowSize); +instFRgauss = instFRgauss/(length(instFRgauss)*mode(diff(behavMatrix(:,1)))); + +% Convolve Gaussian with Binary Spike Trains +uniInstFR = [ensembleMatrix(:,1), nan(size(ensembleMatrix,1), size(ensembleMatrix,2)-1)]; +for uni = 2:size(ensembleMatrix,2) + uniInstFR(:,uni) = conv(ensembleMatrix(:,uni), instFRgauss, 'same'); +end +% +% % Alternatively, just bin the firing rates +% uniInstFR = [ensembleMatrix(:,1), nan(size(ensembleMatrix,1), size(ensembleMatrix,2)-1)]; +% for uni = 2:size(ensembleMatrix,2) +% uniInstFR(:,uni) = conv(ensembleMatrix(:,uni), ones(1,slideWindowSize)/(length(instFRgauss)*mode(diff(behavMatrix(:,1)))), 'same'); +% end + +%% Organize Trial Data +% Create Extraction Matrices +pokeInTrialMatrix = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.2 0.5], 'PokeIn'); +pokeInTSs = behavMatrix(pokeInTrialMatrix(1).TrialLogVect,1)-behavMatrix(pokeInTrialMatrix(1).PokeInIndex,1); +pokeOutTrialMatrix = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.5 0.2], 'PokeOut'); +pokeOutTSs = behavMatrix(pokeOutTrialMatrix(1).TrialLogVect,1)-behavMatrix(pokeOutTrialMatrix(1).PokeOutIndex,1); + +% Create Trial Logical Vectors +corrTrlLog = [pokeInTrialMatrix.Performance]; +isLog = [pokeInTrialMatrix.TranspositionDistance]==0; +posIDsISC = [pokeInTrialMatrix(corrTrlLog & isLog).Position]; + +% Pull out spiking activity +pokeInFR = ExtractTrialData_SM(pokeInTrialMatrix, uniInstFR); %#ok<*NODEF> +pokeInFRisc = cell2mat(reshape(pokeInFR(corrTrlLog & isLog), [1,1,sum(corrTrlLog & isLog)])); +pokeOutFR = ExtractTrialData_SM(pokeOutTrialMatrix, uniInstFR); +pokeOutFRisc = cell2mat(reshape(pokeOutFR(corrTrlLog & isLog), [1,1,sum(corrTrlLog & isLog)])); + +% Pull out and interpolate positional information +pokeInOrientation = ExtractTrialData_SM(pokeInTrialMatrix, orientMatrix); %#ok<*NODEF> +pokeInOrientationInterp = cell(size(pokeInOrientation)); +pokeOutOrientation = ExtractTrialData_SM(pokeOutTrialMatrix, orientMatrix); +pokeOutOrientationInterp = cell(size(pokeOutOrientation)); + +portXcolLog = strcmp(orientMatrixColIDs, 'PortX'); +portYcolLog = strcmp(orientMatrixColIDs, 'PortY'); +headXcolLog = strcmp(orientMatrixColIDs, 'HeadX'); +headYcolLog = strcmp(orientMatrixColIDs, 'HeadY'); +tailXcolLog = strcmp(orientMatrixColIDs, 'TailX'); +tailYcolLog = strcmp(orientMatrixColIDs, 'TailY'); +headAngleColLog = strcmp(orientMatrixColIDs, 'HeadAngle'); +tailAngleColLog = strcmp(orientMatrixColIDs, 'TailAngle'); +portAngleColLog = strcmp(orientMatrixColIDs, 'PortAngle'); +htLengthColLog = strcmp(orientMatrixColIDs, 'HeadTailLength'); +hpLengthColLog = strcmp(orientMatrixColIDs, 'HeadPortLength'); +ptLengthColLog = strcmp(orientMatrixColIDs, 'PortTailLength'); +% First thing to do is nan out the pre trial periods that don't have +% positional data (in the pokeIn aligned) as well as the post trial periods +% that don't have positional data (in the pokeOut aligned) + +for trl = 1:length(pokeInOrientation) + curPIori = pokeInOrientation{trl}; + firstPreTrialNdx = find(~isnan(curPIori(:,headAngleColLog)),1,'first'); + curPIori(1:firstPreTrialNdx, 2:7) = nan; + for ndx = firstPreTrialNdx:size(curPIori,1) + curPIori(ndx,ptLengthColLog) = sqrt((curPIori(ndx,tailXcolLog) - curPIori(ndx,portXcolLog))^2 + (curPIori(ndx,tailYcolLog) - curPIori(ndx,portYcolLog))^2); + curPIori(ndx,hpLengthColLog) = sqrt((curPIori(ndx,portXcolLog) - curPIori(ndx,headXcolLog))^2 + (curPIori(ndx,portYcolLog) - curPIori(ndx,headYcolLog))^2); + curPIori(ndx,htLengthColLog) = sqrt((curPIori(ndx,tailXcolLog) - curPIori(ndx,headXcolLog))^2 + (curPIori(ndx,tailYcolLog) - curPIori(ndx,headYcolLog))^2); + curPIori(ndx,headAngleColLog) = rad2deg(acos((curPIori(ndx,htLengthColLog)^2 + curPIori(ndx,hpLengthColLog)^2 - curPIori(ndx,ptLengthColLog)^2)/(2*curPIori(ndx,htLengthColLog)*curPIori(ndx,hpLengthColLog)))); + curPIori(ndx,portAngleColLog) = rad2deg(acos((curPIori(ndx,ptLengthColLog)^2 + curPIori(ndx,hpLengthColLog)^2 - curPIori(ndx,htLengthColLog)^2)/(2*curPIori(ndx,ptLengthColLog)*curPIori(ndx,hpLengthColLog)))); + curPIori(ndx,tailAngleColLog) = rad2deg(acos((curPIori(ndx,htLengthColLog)^2 + curPIori(ndx,ptLengthColLog)^2 - curPIori(ndx,hpLengthColLog)^2)/(2*curPIori(ndx,htLengthColLog)*curPIori(ndx,ptLengthColLog)))); + end + curPOori = pokeOutOrientation{trl}; + lastTrialNdx = find(~isnan(curPOori(:,headAngleColLog)),1,'last'); + curPOori(lastTrialNdx+1:end, 2:7) = nan; + for ndx = 1:lastTrialNdx + curPOori(ndx,ptLengthColLog) = sqrt((curPOori(ndx,tailXcolLog) - curPOori(ndx,portXcolLog))^2 + (curPOori(ndx,tailYcolLog) - curPOori(ndx,portYcolLog))^2); + curPOori(ndx,hpLengthColLog) = sqrt((curPOori(ndx,portXcolLog) - curPOori(ndx,headXcolLog))^2 + (curPOori(ndx,portYcolLog) - curPOori(ndx,headYcolLog))^2); + curPOori(ndx,htLengthColLog) = sqrt((curPOori(ndx,tailXcolLog) - curPOori(ndx,headXcolLog))^2 + (curPOori(ndx,tailYcolLog) - curPOori(ndx,headYcolLog))^2); + curPOori(ndx,headAngleColLog) = rad2deg(acos((curPOori(ndx,htLengthColLog)^2 + curPOori(ndx,hpLengthColLog)^2 - curPOori(ndx,ptLengthColLog)^2)/(2*curPOori(ndx,htLengthColLog)*curPOori(ndx,hpLengthColLog)))); + curPOori(ndx,portAngleColLog) = rad2deg(acos((curPOori(ndx,ptLengthColLog)^2 + curPOori(ndx,hpLengthColLog)^2 - curPOori(ndx,htLengthColLog)^2)/(2*curPOori(ndx,ptLengthColLog)*curPOori(ndx,hpLengthColLog)))); + curPOori(ndx,tailAngleColLog) = rad2deg(acos((curPOori(ndx,htLengthColLog)^2 + curPOori(ndx,ptLengthColLog)^2 - curPOori(ndx,hpLengthColLog)^2)/(2*curPOori(ndx,htLengthColLog)*curPOori(ndx,ptLengthColLog)))); + end + + htVal(curPosNdx) = sqrt((curTailX - curHeadX)^2 + (curTailY - curHeadY)^2); + hpVal(curPosNdx) = sqrt((curPortX - curHeadX)^2 + (curPortY - curHeadY)^2); + ptVal(curPosNdx) = sqrt((curTailX - curPortX)^2 + (curTailY - curPortY)^2); + + pokeInOrientationInterp{trl} = curPIori; + pokeOutOrientationInterp{trl} = curPOori; +end +pokeInOrientISC = cell2mat(reshape(pokeInOrientationInterp(corrTrlLog & isLog), [1,1,sum(corrTrlLog & isLog)])); +pokeOutOrientISC = cell2mat(reshape(pokeOutOrientationInterp(corrTrlLog & isLog), [1,1,sum(corrTrlLog & isLog)])); + + + +%% Run Sliding F-Ratio Analysis +for uni = 1:length(ensembleUnitSummaries) + curUniPokeInFR = reshape(pokeInFRisc(:,uni+1,:), [size(pokeInFRisc,1), size(pokeInFRisc,3), 1]); + curUniPokeOutFR = reshape(pokeOutFRisc(:,uni+1,:), [size(pokeOutFRisc,1), size(pokeOutFRisc,3), 1]); + + [curUniPIfvalPOS, curUniPIfvalPOSz] = SlidingFvalCalc(curUniPokeInFR, posIDsISC, numPerms); + [curUniPOfvalPOS, curUniPOfvalPOSz] = SlidingFvalCalc(curUniPokeOutFR, posIDsISC, numPerms); + + [curUniPIfvalHeadX, curUniPIfvalHeadXz] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,headXcolLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); + + + + + figure; + subplot(1,2,1); + plot(pokeInTSs, curUniPIfvalPOSz); + hold on; + plot(pokeInTSs, curUniPIfvalHeadXz, 'k'); + + subplot(1,2,2); + plot(pokeOutTSs, curUniPOfvalPOSz); + + + + + + +end + +%% +function [fVectRaw, fVectZ] = SlidingFvalCalc(curUniFR, idVect, numPerms) +% UniFR here is organized as a 2-D matrix where each column is a trial and +% each row is a timepoint. +if size(idVect,1) == 1 + idVect = repmat(idVect, [size(curUniFR,1),1]); +end + +fVectRaw = nan(1,size(curUniFR,1)); +parfor t = 1:size(curUniFR,1) + [~,table,~] = anova1(curUniFR(t,:)', idVect(t,:), 'off'); + if ~isempty(table{2,5}) + fVectRaw(t) = table{2,5}; + else + fVectRaw(t) = 1; + end +end +fVectRaw(isnan(fVectRaw)) = 1; +fVectPerm = nan(numPerms, size(curUniFR,1)); +for r = 1:numPerms + idVectShflVect = randperm(size(idVect,2)); + idVectShfl = idVect(:,idVectShflVect); + parfor t = 1:size(curUniFR,1) + [~,tableRND,~] = anova1(curUniFR(t,:)', idVectShfl(t,:), 'off'); + if ~isempty(tableRND{2,5}) + fVectPerm(r,t) = tableRND{2,5}; + else + fVectPerm(r,t) = 1; + end + end +end +fVectPerm(isnan(fVectPerm)) = 1; +fVectZ = nan(1,size(curUniFR,1)); +for t = 1:size(curUniFR,1) + tempZ = zscore([fVectRaw(t); fVectPerm(:,t)]); + fVectZ(t) = tempZ(1); +end + +end \ No newline at end of file From 8ab61acb528d969e3951112903767154920c0d4c Mon Sep 17 00:00:00 2001 From: gaelias Date: Thu, 9 May 2019 14:37:19 -0700 Subject: [PATCH 004/133] Update PositionInformationContent_SM.m Beginning development of firing rate mask for FR location maps --- Analyses/PositionInformationContent_SM.m | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Analyses/PositionInformationContent_SM.m b/Analyses/PositionInformationContent_SM.m index eeebe4e..33e5187 100644 --- a/Analyses/PositionInformationContent_SM.m +++ b/Analyses/PositionInformationContent_SM.m @@ -343,7 +343,7 @@ orient(gcf, 'landscape'); % print print('-painters', gcf, '-dpdf', sprintf('%s_OrientInfo_Summary', ensembleMatrixColIDs{uni+1})); - close(gcf); +% close(gcf); %% Trial Position Alone % Trial Position @@ -402,12 +402,14 @@ %% function [frMap, icMap, totalICval, overallICrate] = CalculateFieldIC(curXdata, curYdata, xBinLims, yBinLims, occupancyMatrix, uniFR, meanFR) frMap = nan(length(yBinLims)-1, length(xBinLims)-1); +frStdMap = nan(length(yBinLims)-1, length(xBinLims)-1); icMap = nan(length(yBinLims)-1, length(xBinLims)-1); for r = 1:length(yBinLims)-1 yValLog = (curYdata>=yBinLims(r) & curYdata=xBinLims(c) & curXdata Date: Thu, 9 May 2019 15:04:48 -0700 Subject: [PATCH 005/133] Update PositionInformationFval_SM.m --- Analyses/PositionInformationFval_SM.m | 75 +++++++++++++++++++++++---- 1 file changed, 65 insertions(+), 10 deletions(-) diff --git a/Analyses/PositionInformationFval_SM.m b/Analyses/PositionInformationFval_SM.m index 90e1802..4e303b7 100644 --- a/Analyses/PositionInformationFval_SM.m +++ b/Analyses/PositionInformationFval_SM.m @@ -11,9 +11,9 @@ fileNames = {dirContents.name}; %% Define Standard Variables -slideWindowSize = 30; +slideWindowSize = 100; spatialBinSize = 2; -numPerms = 10; +numPerms = 50; %% Load Relevant Data load(fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'BehaviorMatrix'))}); @@ -173,28 +173,83 @@ curUniPokeInFR = reshape(pokeInFRisc(:,uni+1,:), [size(pokeInFRisc,1), size(pokeInFRisc,3), 1]); curUniPokeOutFR = reshape(pokeOutFRisc(:,uni+1,:), [size(pokeOutFRisc,1), size(pokeOutFRisc,3), 1]); + % Poke In Oriented [curUniPIfvalPOS, curUniPIfvalPOSz] = SlidingFvalCalc(curUniPokeInFR, posIDsISC, numPerms); - [curUniPOfvalPOS, curUniPOfvalPOSz] = SlidingFvalCalc(curUniPokeOutFR, posIDsISC, numPerms); [curUniPIfvalHeadX, curUniPIfvalHeadXz] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,headXcolLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); + [curUniPIfvalHeadY, curUniPIfvalHeadYz] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,headYcolLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); + [curUniPIfvalTailX, curUniPIfvalTailXz] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,tailXcolLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); + [curUniPIfvalTailY, curUniPIfvalTailYz] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,tailYcolLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); + [curUniPIfvalPortAngle, curUniPIfvalPortAnglez] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,portAngleColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); + [curUniPIfvalHeadAngle, curUniPIfvalHeadAnglez] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,headAngleColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); + [curUniPIfvalTailAngle, curUniPIfvalTailAnglez] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,tailAngleColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); + [curUniPIfvalHeadTailDist, curUniPIfvalHeadTailDistZ] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,htLengthColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); + [curUniPIfvalHeadPortDist, curUniPIfvalHeadPortDistZ] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,hpLengthColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); + [curUniPIfvalTailPortDist, curUniPIfvalTailPortDistZ] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,ptLengthColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); - figure; - subplot(1,2,1); - plot(pokeInTSs, curUniPIfvalPOSz); - hold on; - plot(pokeInTSs, curUniPIfvalHeadXz, 'k'); + % Poke Out Oriented + [curUniPOfvalPOS, curUniPOfvalPOSz] = SlidingFvalCalc(curUniPokeOutFR, posIDsISC, numPerms); - subplot(1,2,2); - plot(pokeOutTSs, curUniPOfvalPOSz); + [curUniPOfvalHeadX, curUniPOfvalHeadXz] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,headXcolLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); + [curUniPOfvalHeadY, curUniPOfvalHeadYz] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,headYcolLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); + [curUniPOfvalTailX, curUniPOfvalTailXz] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,tailXcolLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); + [curUniPOfvalTailY, curUniPOfvalTailYz] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,tailYcolLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); + [curUniPOvalPortAngle, curUniPOfvalPortAnglez] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,portAngleColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); + [curUniPOfvalHeadAngle, curUniPOfvalHeadAnglez] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,headAngleColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); + [curUniPOfvalTailAngle, curUniPOfvalTailAnglez] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,tailAngleColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); + [curUniPOfvalHeadTailDist, curUniPOfvalHeadTailDistZ] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,htLengthColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); + [curUniPOfvalHeadPortDist, curUniPOfvalHeadPortDistZ] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,hpLengthColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); + [curUniPOfvalTailPortDist, curUniPOfvalTailPortDistZ] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,ptLengthColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); + + figure; + sp1 = subplot(1,2,1); + plot(pokeInTSs, curUniPIfvalPOSz,'linewidth', 2); + hold on; + plot(pokeInTSs, curUniPIfvalHeadXz, 'k'); + plot(pokeInTSs, curUniPIfvalHeadYz, '--k'); + plot(pokeInTSs, curUniPIfvalTailXz, 'r'); + plot(pokeInTSs, curUniPIfvalTailYz, '--r'); + plot(pokeInTSs, curUniPIfvalPortAnglez, 'g'); + plot(pokeInTSs, curUniPIfvalHeadAnglez, '--g'); + plot(pokeInTSs, curUniPIfvalTailAnglez, '.g'); + plot(pokeInTSs, curUniPIfvalHeadTailDistZ, 'c'); + plot(pokeInTSs, curUniPIfvalHeadPortDistZ, '--c'); + plot(pokeInTSs, curUniPIfvalTailPortDistZ, '.c'); + title('Poke In Aligned') + axis tight + legend('Sequence Position', 'Head Loc X', 'Head Loc Y', 'Tail Loc X', 'Tail Loc Y', 'Port Angle', 'Head Angle', 'Tail Angle', 'Head-Tail', 'Head-Port', 'Tail-Port', 'location', 'best'); + sp2 = subplot(1,2,2); + plot(pokeOutTSs, curUniPOfvalPOSz, 'linewidth', 2); + hold on; + plot(pokeOutTSs, curUniPOfvalHeadXz, 'k'); + plot(pokeOutTSs, curUniPOfvalHeadYz, '--k'); + plot(pokeOutTSs, curUniPOfvalTailXz, 'r'); + plot(pokeOutTSs, curUniPOfvalTailYz, '--r'); + plot(pokeOutTSs, curUniPOfvalPortAnglez, 'g'); + plot(pokeOutTSs, curUniPOfvalHeadAnglez, '--g'); + plot(pokeOutTSs, curUniPOfvalTailAnglez, '.g'); + plot(pokeOutTSs, curUniPOfvalHeadTailDistZ, 'c'); + plot(pokeOutTSs, curUniPOfvalHeadPortDistZ, '--c'); + plot(pokeOutTSs, curUniPOfvalTailPortDistZ, '.c'); + title('Poke Out Aligned'); + axis tight + linkaxes([sp1, sp2], 'y'); + annotation('textbox', [0.05 0.9 0.9 0.1], 'String', ensembleMatrixColIDs{uni+1}, 'linestyle', 'none', 'FontSize', 20); + annotation('textbox', [0.2 0.9 0.8 0.1], 'String', sprintf('Gaussian = %.02fms Number of Permutations = %i', slideWindowSize, numPerms), 'HorizontalAlignment', 'right', 'linestyle', 'none', 'FontSize', 12); + annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string', sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); + orient(gcf, 'tall'); + orient(gcf, 'landscape'); +% print + print('-painters', gcf, '-dpdf', sprintf('%s_OrientFvalInfo_Summary', ensembleMatrixColIDs{uni+1})); end %% From dd9e70e0612f66bf3c096fd2210b30be1617c9d7 Mon Sep 17 00:00:00 2001 From: gaelias Date: Thu, 9 May 2019 17:39:26 -0700 Subject: [PATCH 006/133] Update PositionInformationFval_SM.m --- Analyses/PositionInformationFval_SM.m | 185 ++++++++++++++++---------- 1 file changed, 115 insertions(+), 70 deletions(-) diff --git a/Analyses/PositionInformationFval_SM.m b/Analyses/PositionInformationFval_SM.m index 4e303b7..e312b8c 100644 --- a/Analyses/PositionInformationFval_SM.m +++ b/Analyses/PositionInformationFval_SM.m @@ -11,8 +11,8 @@ fileNames = {dirContents.name}; %% Define Standard Variables -slideWindowSize = 100; -spatialBinSize = 2; +slideWindowSize = 30; +spatialBinSize = 5; numPerms = 50; %% Load Relevant Data @@ -59,6 +59,27 @@ orientMatrix = [orientMatrix, portAngle, headAngle, tailAngle, htVal, hpVal, ptVal]; orientMatrixColIDs = [orientMatrixColIDs, {'PortAngle'}, {'HeadAngle'}, {'TailAngle'}, {'HeadTailLength'}, {'HeadPortLength'}, {'PortTailLength'}]; +% Now redefine the column ID vectors +portXcolLog = strcmp(orientMatrixColIDs, 'PortX'); +portYcolLog = strcmp(orientMatrixColIDs, 'PortY'); +headXcolLog = strcmp(orientMatrixColIDs, 'HeadX'); +headYcolLog = strcmp(orientMatrixColIDs, 'HeadY'); +tailXcolLog = strcmp(orientMatrixColIDs, 'TailX'); +tailYcolLog = strcmp(orientMatrixColIDs, 'TailY'); +headAngleColLog = strcmp(orientMatrixColIDs, 'HeadAngle'); +tailAngleColLog = strcmp(orientMatrixColIDs, 'TailAngle'); +portAngleColLog = strcmp(orientMatrixColIDs, 'PortAngle'); +htLengthColLog = strcmp(orientMatrixColIDs, 'HeadTailLength'); +hpLengthColLog = strcmp(orientMatrixColIDs, 'HeadPortLength'); +ptLengthColLog = strcmp(orientMatrixColIDs, 'PortTailLength'); + +% Determine spatial bins +headXbins = floor(min(orientMatrix(:,headXcolLog))/spatialBinSize)*spatialBinSize:spatialBinSize:ceil(max(orientMatrix(:,headXcolLog))/spatialBinSize)*spatialBinSize; +headYbins = floor(min(orientMatrix(:,headYcolLog))/spatialBinSize)*spatialBinSize:spatialBinSize:ceil(max(orientMatrix(:,headYcolLog))/spatialBinSize)*spatialBinSize; + +tailXbins = floor(min(orientMatrix(:,tailXcolLog))/spatialBinSize)*spatialBinSize:spatialBinSize:ceil(max(orientMatrix(:,tailXcolLog))/spatialBinSize)*spatialBinSize; +tailYbins = floor(min(orientMatrix(:,tailYcolLog))/spatialBinSize)*spatialBinSize:spatialBinSize:ceil(max(orientMatrix(:,tailYcolLog))/spatialBinSize)*spatialBinSize; + % Now Interpolate the port/head/tail locations. No interpolation of the % other vals right now... orientMatrix(:,portPosX) = interp1(orientMatrix(posIndices,1), orientMatrix(posIndices,portPosX), orientMatrix(:,1)); @@ -116,18 +137,7 @@ pokeOutOrientation = ExtractTrialData_SM(pokeOutTrialMatrix, orientMatrix); pokeOutOrientationInterp = cell(size(pokeOutOrientation)); -portXcolLog = strcmp(orientMatrixColIDs, 'PortX'); -portYcolLog = strcmp(orientMatrixColIDs, 'PortY'); -headXcolLog = strcmp(orientMatrixColIDs, 'HeadX'); -headYcolLog = strcmp(orientMatrixColIDs, 'HeadY'); -tailXcolLog = strcmp(orientMatrixColIDs, 'TailX'); -tailYcolLog = strcmp(orientMatrixColIDs, 'TailY'); -headAngleColLog = strcmp(orientMatrixColIDs, 'HeadAngle'); -tailAngleColLog = strcmp(orientMatrixColIDs, 'TailAngle'); -portAngleColLog = strcmp(orientMatrixColIDs, 'PortAngle'); -htLengthColLog = strcmp(orientMatrixColIDs, 'HeadTailLength'); -hpLengthColLog = strcmp(orientMatrixColIDs, 'HeadPortLength'); -ptLengthColLog = strcmp(orientMatrixColIDs, 'PortTailLength'); +%% Pre-Process the positional data v2 % First thing to do is nan out the pre trial periods that don't have % positional data (in the pokeIn aligned) as well as the post trial periods % that don't have positional data (in the pokeOut aligned) @@ -163,10 +173,34 @@ pokeInOrientationInterp{trl} = curPIori; pokeOutOrientationInterp{trl} = curPOori; end +% Now bin the orientation data +orientBinValsPI = cell(size(pokeInOrientationInterp)); +orientBinValsPO = cell(size(pokeOutOrientationInterp)); +for trl = 1:length(pokeInOrientation) + curPIori = pokeInOrientationInterp{trl}; + tempPIndxVals = nan(size(curPIori,1),2); + for piT = 1:size(curPIori,1) + if ~isnan(curPIori(piT,headXcolLog)) + tempPIndxVals(piT,1) = sub2ind([length(headYbins)-1 length(headXbins)-1], find(curPIori(piT,headYcolLog)>=headYbins,1,'last'), find(curPIori(piT,headXcolLog)>=headXbins,1,'last')); + tempPIndxVals(piT,2) = sub2ind([length(tailYbins)-1 length(tailXbins)-1], find(curPIori(piT,tailYcolLog)>=tailYbins,1,'last'), find(curPIori(piT,tailXcolLog)>=tailXbins,1,'last')); + end + end + curPOori = pokeOutOrientationInterp{trl}; + tempPOndxVals = nan(size(curPOori,1),2); + for poT = 1:size(curPOori,1) + if ~isnan(curPOori(poT,headXcolLog)) + tempPOndxVals(poT,1) = sub2ind([length(headYbins)-1 length(headXbins)-1], find(curPOori(poT,headYcolLog)>=headYbins,1,'last'), find(curPOori(poT,headXcolLog)>=headXbins,1,'last')); + tempPOndxVals(poT,2) = sub2ind([length(tailYbins)-1 length(tailXbins)-1], find(curPOori(poT,tailYcolLog)>=tailYbins,1,'last'), find(curPOori(poT,tailXcolLog)>=tailXbins,1,'last')); + end + end + orientBinValsPI{trl} = tempPIndxVals; + orientBinValsPO{trl} = tempPOndxVals; +end pokeInOrientISC = cell2mat(reshape(pokeInOrientationInterp(corrTrlLog & isLog), [1,1,sum(corrTrlLog & isLog)])); pokeOutOrientISC = cell2mat(reshape(pokeOutOrientationInterp(corrTrlLog & isLog), [1,1,sum(corrTrlLog & isLog)])); - +pokeInOrientBinsISC = cell2mat(reshape(orientBinValsPI(corrTrlLog & isLog), [1,1,sum(corrTrlLog & isLog)])); +pokeOutOrientBinsISC = cell2mat(reshape(orientBinValsPO(corrTrlLog & isLog), [1,1,sum(corrTrlLog & isLog)])); %% Run Sliding F-Ratio Analysis for uni = 1:length(ensembleUnitSummaries) @@ -175,75 +209,86 @@ % Poke In Oriented [curUniPIfvalPOS, curUniPIfvalPOSz] = SlidingFvalCalc(curUniPokeInFR, posIDsISC, numPerms); - - [curUniPIfvalHeadX, curUniPIfvalHeadXz] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,headXcolLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); - [curUniPIfvalHeadY, curUniPIfvalHeadYz] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,headYcolLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); - - [curUniPIfvalTailX, curUniPIfvalTailXz] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,tailXcolLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); - [curUniPIfvalTailY, curUniPIfvalTailYz] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,tailYcolLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); - - [curUniPIfvalPortAngle, curUniPIfvalPortAnglez] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,portAngleColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); - [curUniPIfvalHeadAngle, curUniPIfvalHeadAnglez] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,headAngleColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); - [curUniPIfvalTailAngle, curUniPIfvalTailAnglez] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,tailAngleColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); - - [curUniPIfvalHeadTailDist, curUniPIfvalHeadTailDistZ] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,htLengthColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); - [curUniPIfvalHeadPortDist, curUniPIfvalHeadPortDistZ] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,hpLengthColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); - [curUniPIfvalTailPortDist, curUniPIfvalTailPortDistZ] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,ptLengthColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); - + [curUniPIfvalHeadLoc, curUniPIfvalHeadLocZ] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientBinsISC(:,1,:), [size(pokeInOrientBinsISC,1), size(pokeInOrientBinsISC,3), 1]), numPerms); + [curUniPIfvalTailLoc, curUniPIfvalTailLocZ] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeOutOrientBinsISC(:,2,:), [size(pokeOutOrientBinsISC,1), size(pokeOutOrientBinsISC,3), 1]), numPerms); + +% +% [curUniPIfvalHeadX, curUniPIfvalHeadXz] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,headXcolLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); +% [curUniPIfvalHeadY, curUniPIfvalHeadYz] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,headYcolLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); +% +% [curUniPIfvalTailX, curUniPIfvalTailXz] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,tailXcolLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); +% [curUniPIfvalTailY, curUniPIfvalTailYz] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,tailYcolLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); +% +% [curUniPIfvalPortAngle, curUniPIfvalPortAnglez] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,portAngleColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); +% [curUniPIfvalHeadAngle, curUniPIfvalHeadAnglez] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,headAngleColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); +% [curUniPIfvalTailAngle, curUniPIfvalTailAnglez] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,tailAngleColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); +% +% [curUniPIfvalHeadTailDist, curUniPIfvalHeadTailDistZ] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,htLengthColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); +% [curUniPIfvalHeadPortDist, curUniPIfvalHeadPortDistZ] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,hpLengthColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); +% [curUniPIfvalTailPortDist, curUniPIfvalTailPortDistZ] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,ptLengthColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); +% % Poke Out Oriented [curUniPOfvalPOS, curUniPOfvalPOSz] = SlidingFvalCalc(curUniPokeOutFR, posIDsISC, numPerms); - - [curUniPOfvalHeadX, curUniPOfvalHeadXz] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,headXcolLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); - [curUniPOfvalHeadY, curUniPOfvalHeadYz] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,headYcolLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); - - [curUniPOfvalTailX, curUniPOfvalTailXz] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,tailXcolLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); - [curUniPOfvalTailY, curUniPOfvalTailYz] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,tailYcolLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); - - [curUniPOvalPortAngle, curUniPOfvalPortAnglez] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,portAngleColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); - [curUniPOfvalHeadAngle, curUniPOfvalHeadAnglez] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,headAngleColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); - [curUniPOfvalTailAngle, curUniPOfvalTailAnglez] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,tailAngleColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); - - [curUniPOfvalHeadTailDist, curUniPOfvalHeadTailDistZ] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,htLengthColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); - [curUniPOfvalHeadPortDist, curUniPOfvalHeadPortDistZ] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,hpLengthColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); - [curUniPOfvalTailPortDist, curUniPOfvalTailPortDistZ] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,ptLengthColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); - + [curUniPOfvalHeadLoc, curUniPOfvalHeadLocZ] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeOutOrientBinsISC(:,1,:), [size(pokeOutOrientBinsISC,1), size(pokeOutOrientBinsISC,3), 1]), numPerms); + [curUniPOfvalTailLoc, curUniPOfvalTailLocZ] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeOutOrientBinsISC(:,2,:), [size(pokeOutOrientBinsISC,1), size(pokeOutOrientBinsISC,3), 1]), numPerms); + +% +% [curUniPOfvalHeadX, curUniPOfvalHeadXz] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,headXcolLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); +% [curUniPOfvalHeadY, curUniPOfvalHeadYz] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,headYcolLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); +% +% [curUniPOfvalTailX, curUniPOfvalTailXz] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,tailXcolLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); +% [curUniPOfvalTailY, curUniPOfvalTailYz] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,tailYcolLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); +% +% [curUniPOvalPortAngle, curUniPOfvalPortAnglez] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,portAngleColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); +% [curUniPOfvalHeadAngle, curUniPOfvalHeadAnglez] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,headAngleColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); +% [curUniPOfvalTailAngle, curUniPOfvalTailAnglez] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,tailAngleColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); +% +% [curUniPOfvalHeadTailDist, curUniPOfvalHeadTailDistZ] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,htLengthColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); +% [curUniPOfvalHeadPortDist, curUniPOfvalHeadPortDistZ] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,hpLengthColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); +% [curUniPOfvalTailPortDist, curUniPOfvalTailPortDistZ] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,ptLengthColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); +% figure; - sp1 = subplot(1,2,1); + sp1 = subplot(2,1,1); plot(pokeInTSs, curUniPIfvalPOSz,'linewidth', 2); - hold on; - plot(pokeInTSs, curUniPIfvalHeadXz, 'k'); - plot(pokeInTSs, curUniPIfvalHeadYz, '--k'); - plot(pokeInTSs, curUniPIfvalTailXz, 'r'); - plot(pokeInTSs, curUniPIfvalTailYz, '--r'); - plot(pokeInTSs, curUniPIfvalPortAnglez, 'g'); - plot(pokeInTSs, curUniPIfvalHeadAnglez, '--g'); - plot(pokeInTSs, curUniPIfvalTailAnglez, '.g'); - plot(pokeInTSs, curUniPIfvalHeadTailDistZ, 'c'); - plot(pokeInTSs, curUniPIfvalHeadPortDistZ, '--c'); - plot(pokeInTSs, curUniPIfvalTailPortDistZ, '.c'); + hold on; + plot(pokeInTSs, curUniPIfvalHeadLocZ, 'k'); + plot(pokeInTSs, curUniPIfvalTailLocZ, '--k'); +% plot(pokeInTSs, curUniPIfvalHeadXz, 'k'); +% plot(pokeInTSs, curUniPIfvalHeadYz, '--k'); +% plot(pokeInTSs, curUniPIfvalTailXz, 'r'); +% plot(pokeInTSs, curUniPIfvalTailYz, '--r'); +% plot(pokeInTSs, curUniPIfvalPortAnglez, 'g'); +% plot(pokeInTSs, curUniPIfvalHeadAnglez, '--g'); +% plot(pokeInTSs, curUniPIfvalTailAnglez, '.g'); +% plot(pokeInTSs, curUniPIfvalHeadTailDistZ, 'c'); +% plot(pokeInTSs, curUniPIfvalHeadPortDistZ, '--c'); +% plot(pokeInTSs, curUniPIfvalTailPortDistZ, '.c'); title('Poke In Aligned') axis tight - legend('Sequence Position', 'Head Loc X', 'Head Loc Y', 'Tail Loc X', 'Tail Loc Y', 'Port Angle', 'Head Angle', 'Tail Angle', 'Head-Tail', 'Head-Port', 'Tail-Port', 'location', 'best'); + legend('Sequence Position', 'Head', 'Tail', 'location', 'best'); +% legend('Sequence Position', 'Head Loc X', 'Head Loc Y', 'Tail Loc X', 'Tail Loc Y', 'Port Angle', 'Head Angle', 'Tail Angle', 'Head-Tail', 'Head-Port', 'Tail-Port', 'location', 'best'); - sp2 = subplot(1,2,2); + sp2 = subplot(2,1,2); plot(pokeOutTSs, curUniPOfvalPOSz, 'linewidth', 2); hold on; - plot(pokeOutTSs, curUniPOfvalHeadXz, 'k'); - plot(pokeOutTSs, curUniPOfvalHeadYz, '--k'); - plot(pokeOutTSs, curUniPOfvalTailXz, 'r'); - plot(pokeOutTSs, curUniPOfvalTailYz, '--r'); - plot(pokeOutTSs, curUniPOfvalPortAnglez, 'g'); - plot(pokeOutTSs, curUniPOfvalHeadAnglez, '--g'); - plot(pokeOutTSs, curUniPOfvalTailAnglez, '.g'); - plot(pokeOutTSs, curUniPOfvalHeadTailDistZ, 'c'); - plot(pokeOutTSs, curUniPOfvalHeadPortDistZ, '--c'); - plot(pokeOutTSs, curUniPOfvalTailPortDistZ, '.c'); + plot(pokeOutTSs, curUniPOfvalHeadLocZ, 'k'); + plot(pokeOutTSs, curUniPOfvalTailLocZ, '--k'); +% plot(pokeOutTSs, curUniPOfvalHeadXz, 'k'); +% plot(pokeOutTSs, curUniPOfvalHeadYz, '--k'); +% plot(pokeOutTSs, curUniPOfvalTailXz, 'r'); +% plot(pokeOutTSs, curUniPOfvalTailYz, '--r'); +% plot(pokeOutTSs, curUniPOfvalPortAnglez, 'g'); +% plot(pokeOutTSs, curUniPOfvalHeadAnglez, '--g'); +% plot(pokeOutTSs, curUniPOfvalTailAnglez, '.g'); +% plot(pokeOutTSs, curUniPOfvalHeadTailDistZ, 'c'); +% plot(pokeOutTSs, curUniPOfvalHeadPortDistZ, '--c'); +% plot(pokeOutTSs, curUniPOfvalTailPortDistZ, '.c'); title('Poke Out Aligned'); axis tight linkaxes([sp1, sp2], 'y'); annotation('textbox', [0.05 0.9 0.9 0.1], 'String', ensembleMatrixColIDs{uni+1}, 'linestyle', 'none', 'FontSize', 20); - annotation('textbox', [0.2 0.9 0.8 0.1], 'String', sprintf('Gaussian = %.02fms Number of Permutations = %i', slideWindowSize, numPerms), 'HorizontalAlignment', 'right', 'linestyle', 'none', 'FontSize', 12); + annotation('textbox', [0.2 0.9 0.8 0.1], 'String', sprintf('Spatial Bin = %.02fmm Gaussian = %.02fms Number of Permutations = %i', spatialBinSize*1.7, slideWindowSize, numPerms), 'HorizontalAlignment', 'right', 'linestyle', 'none', 'FontSize', 12); annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string', sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); orient(gcf, 'tall'); From 44a723eabc1b72b37acb785a27f9e0653f668cd5 Mon Sep 17 00:00:00 2001 From: gaelias Date: Thu, 9 May 2019 18:34:56 -0700 Subject: [PATCH 007/133] Update PositionInformationFval_SM.m --- Analyses/PositionInformationFval_SM.m | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/Analyses/PositionInformationFval_SM.m b/Analyses/PositionInformationFval_SM.m index e312b8c..654cf3c 100644 --- a/Analyses/PositionInformationFval_SM.m +++ b/Analyses/PositionInformationFval_SM.m @@ -123,13 +123,20 @@ % Create Trial Logical Vectors corrTrlLog = [pokeInTrialMatrix.Performance]; isLog = [pokeInTrialMatrix.TranspositionDistance]==0; -posIDsISC = [pokeInTrialMatrix(corrTrlLog & isLog).Position]; +pos1log = [pokeInTrialMatrix.Position]==1; +trialLog = (corrTrlLog & isLog & ~pos1log); +seqType = 'All InSeq Correct Except Position 1'; +% trialLog = (corrTrlLog & isLog); +% seqType = 'All InSeq Correct Trials'; +% trialLog = (corrTrlLog); +% seqType = 'All Correct Trials'; +posIDsISC = [pokeInTrialMatrix(trialLog).Position]; % Pull out spiking activity pokeInFR = ExtractTrialData_SM(pokeInTrialMatrix, uniInstFR); %#ok<*NODEF> -pokeInFRisc = cell2mat(reshape(pokeInFR(corrTrlLog & isLog), [1,1,sum(corrTrlLog & isLog)])); +pokeInFRisc = cell2mat(reshape(pokeInFR(trialLog), [1,1,sum(trialLog)])); pokeOutFR = ExtractTrialData_SM(pokeOutTrialMatrix, uniInstFR); -pokeOutFRisc = cell2mat(reshape(pokeOutFR(corrTrlLog & isLog), [1,1,sum(corrTrlLog & isLog)])); +pokeOutFRisc = cell2mat(reshape(pokeOutFR(trialLog), [1,1,sum(trialLog)])); % Pull out and interpolate positional information pokeInOrientation = ExtractTrialData_SM(pokeInTrialMatrix, orientMatrix); %#ok<*NODEF> @@ -196,11 +203,11 @@ orientBinValsPI{trl} = tempPIndxVals; orientBinValsPO{trl} = tempPOndxVals; end -pokeInOrientISC = cell2mat(reshape(pokeInOrientationInterp(corrTrlLog & isLog), [1,1,sum(corrTrlLog & isLog)])); -pokeOutOrientISC = cell2mat(reshape(pokeOutOrientationInterp(corrTrlLog & isLog), [1,1,sum(corrTrlLog & isLog)])); +pokeInOrientISC = cell2mat(reshape(pokeInOrientationInterp(trialLog), [1,1,sum(trialLog)])); +pokeOutOrientISC = cell2mat(reshape(pokeOutOrientationInterp(trialLog), [1,1,sum(trialLog)])); -pokeInOrientBinsISC = cell2mat(reshape(orientBinValsPI(corrTrlLog & isLog), [1,1,sum(corrTrlLog & isLog)])); -pokeOutOrientBinsISC = cell2mat(reshape(orientBinValsPO(corrTrlLog & isLog), [1,1,sum(corrTrlLog & isLog)])); +pokeInOrientBinsISC = cell2mat(reshape(orientBinValsPI(trialLog), [1,1,sum(trialLog)])); +pokeOutOrientBinsISC = cell2mat(reshape(orientBinValsPO(trialLog), [1,1,sum(trialLog)])); %% Run Sliding F-Ratio Analysis for uni = 1:length(ensembleUnitSummaries) @@ -288,7 +295,7 @@ linkaxes([sp1, sp2], 'y'); annotation('textbox', [0.05 0.9 0.9 0.1], 'String', ensembleMatrixColIDs{uni+1}, 'linestyle', 'none', 'FontSize', 20); - annotation('textbox', [0.2 0.9 0.8 0.1], 'String', sprintf('Spatial Bin = %.02fmm Gaussian = %.02fms Number of Permutations = %i', spatialBinSize*1.7, slideWindowSize, numPerms), 'HorizontalAlignment', 'right', 'linestyle', 'none', 'FontSize', 12); + annotation('textbox', [0.2 0.9 0.8 0.1], 'String', sprintf('%s Spatial Bin = %.02fmm Gaussian = %.02fms Number of Permutations = %i', seqType, spatialBinSize*1.7, slideWindowSize, numPerms), 'HorizontalAlignment', 'right', 'linestyle', 'none', 'FontSize', 12); annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string', sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); orient(gcf, 'tall'); From b182114e61c4b7f41fcd2c97c2f0d41a81b65441 Mon Sep 17 00:00:00 2001 From: gaelias Date: Fri, 10 May 2019 13:05:12 -0700 Subject: [PATCH 008/133] Update PositionInformationFval_SM.m --- Analyses/PositionInformationFval_SM.m | 78 +++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 5 deletions(-) diff --git a/Analyses/PositionInformationFval_SM.m b/Analyses/PositionInformationFval_SM.m index 654cf3c..b9a6e39 100644 --- a/Analyses/PositionInformationFval_SM.m +++ b/Analyses/PositionInformationFval_SM.m @@ -11,7 +11,7 @@ fileNames = {dirContents.name}; %% Define Standard Variables -slideWindowSize = 30; +slideWindowSize = 100; spatialBinSize = 5; numPerms = 50; @@ -217,7 +217,13 @@ % Poke In Oriented [curUniPIfvalPOS, curUniPIfvalPOSz] = SlidingFvalCalc(curUniPokeInFR, posIDsISC, numPerms); [curUniPIfvalHeadLoc, curUniPIfvalHeadLocZ] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientBinsISC(:,1,:), [size(pokeInOrientBinsISC,1), size(pokeInOrientBinsISC,3), 1]), numPerms); - [curUniPIfvalTailLoc, curUniPIfvalTailLocZ] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeOutOrientBinsISC(:,2,:), [size(pokeOutOrientBinsISC,1), size(pokeOutOrientBinsISC,3), 1]), numPerms); + [curUniPIfvalTailLoc, curUniPIfvalTailLocZ] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientBinsISC(:,2,:), [size(pokeInOrientBinsISC,1), size(pokeInOrientBinsISC,3), 1]), numPerms); + + [curUniPIfvalInteract, curUniPIfvalInteractZ] = SlidingFvalCalcInteract(curUniPokeInFR, posIDsISC,... + reshape(pokeInOrientBinsISC(:,1,:), [size(pokeInOrientBinsISC,1), size(pokeInOrientBinsISC,3), 1]),... + reshape(pokeInOrientBinsISC(:,2,:), [size(pokeInOrientBinsISC,1), size(pokeInOrientBinsISC,3), 1]),... + numPerms); + % % [curUniPIfvalHeadX, curUniPIfvalHeadXz] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,headXcolLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); @@ -236,9 +242,13 @@ % % Poke Out Oriented [curUniPOfvalPOS, curUniPOfvalPOSz] = SlidingFvalCalc(curUniPokeOutFR, posIDsISC, numPerms); - [curUniPOfvalHeadLoc, curUniPOfvalHeadLocZ] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeOutOrientBinsISC(:,1,:), [size(pokeOutOrientBinsISC,1), size(pokeOutOrientBinsISC,3), 1]), numPerms); - [curUniPOfvalTailLoc, curUniPOfvalTailLocZ] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeOutOrientBinsISC(:,2,:), [size(pokeOutOrientBinsISC,1), size(pokeOutOrientBinsISC,3), 1]), numPerms); + [curUniPOfvalHeadLoc, curUniPOfvalHeadLocZ] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientBinsISC(:,1,:), [size(pokeOutOrientBinsISC,1), size(pokeOutOrientBinsISC,3), 1]), numPerms); + [curUniPOfvalTailLoc, curUniPOfvalTailLocZ] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientBinsISC(:,2,:), [size(pokeOutOrientBinsISC,1), size(pokeOutOrientBinsISC,3), 1]), numPerms); + [curUniPOfvalInteract, curUniPOfvalInteractZ] = SlidingFvalCalcInteract(curUniPokeOutFR, posIDsISC,... + reshape(pokeOutOrientBinsISC(:,1,:), [size(pokeOutOrientBinsISC,1), size(pokeOutOrientBinsISC,3), 1]),... + reshape(pokeOutOrientBinsISC(:,2,:), [size(pokeOutOrientBinsISC,1), size(pokeOutOrientBinsISC,3), 1]),... + numPerms); % % [curUniPOfvalHeadX, curUniPOfvalHeadXz] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,headXcolLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); % [curUniPOfvalHeadY, curUniPOfvalHeadYz] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,headYcolLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); @@ -260,6 +270,7 @@ hold on; plot(pokeInTSs, curUniPIfvalHeadLocZ, 'k'); plot(pokeInTSs, curUniPIfvalTailLocZ, '--k'); + plot(pokeInTSs, curUniPIfvalInteractZ, 'r'); % plot(pokeInTSs, curUniPIfvalHeadXz, 'k'); % plot(pokeInTSs, curUniPIfvalHeadYz, '--k'); % plot(pokeInTSs, curUniPIfvalTailXz, 'r'); @@ -272,7 +283,7 @@ % plot(pokeInTSs, curUniPIfvalTailPortDistZ, '.c'); title('Poke In Aligned') axis tight - legend('Sequence Position', 'Head', 'Tail', 'location', 'best'); + legend('Sequence Position', 'Head', 'Tail', 'Interaction', 'location', 'best'); % legend('Sequence Position', 'Head Loc X', 'Head Loc Y', 'Tail Loc X', 'Tail Loc Y', 'Port Angle', 'Head Angle', 'Tail Angle', 'Head-Tail', 'Head-Port', 'Tail-Port', 'location', 'best'); sp2 = subplot(2,1,2); @@ -280,6 +291,7 @@ hold on; plot(pokeOutTSs, curUniPOfvalHeadLocZ, 'k'); plot(pokeOutTSs, curUniPOfvalTailLocZ, '--k'); + plot(pokeOutTSs, curUniPOfvalInteractZ, 'r'); % plot(pokeOutTSs, curUniPOfvalHeadXz, 'k'); % plot(pokeOutTSs, curUniPOfvalHeadYz, '--k'); % plot(pokeOutTSs, curUniPOfvalTailXz, 'r'); @@ -342,4 +354,60 @@ fVectZ(t) = tempZ(1); end +end + +%% +function [fVectRaw, fVectZ] = SlidingFvalCalcInteract(curUniFR, idVect1, idVect2, idVect3, numPerms) +if size(idVect1,1) == 1 + idVect1 = repmat(idVect1, [size(curUniFR,1),1]); +end +if size(idVect2,1) == 1 + idVect2 = repmat(idVect2, [size(curUniFR,1),1]); +end +if size(idVect3,1) == 1 + idVect3 = repmat(idVect3, [size(curUniFR,1),1]); +end + +fVectRaw = nan(1,size(curUniFR,1)); +parfor t = 1:size(curUniFR,1) + if mean(isnan(idVect3(t,:)))>0.5 || mean(isnan(idVect2(t,:)))>0.5 || mean(isnan(idVect3(t,:)))>0.5 + continue; + else + [~,table,~] = anovan(curUniFR(t,:)', {idVect1(t,:), idVect2(t,:), idVect3(t,:)}, 'model', [1 1 1], 'sstype', 'h', 'display', 'off') + if ~isempty(table{2,5}) + fVectRaw(t) = table{2,6}; + else + fVectRaw(t) = 1; + end + end +end +fVectRaw(isnan(fVectRaw)) = 1; +fVectPerm = nan(numPerms, size(curUniFR,1)); +for r = 1:numPerms + idVect1ShflVect = randperm(size(idVect1,2)); + idVect1Shfl = idVect1(:,idVect1ShflVect); + idVect2ShflVect = randperm(size(idVect2,2)); + idVect2Shfl = idVect2(:,idVect2ShflVect); + idVect3ShflVect = randperm(size(idVect3,2)); + idVect3Shfl = idVect1(:,idVect3ShflVect); + parfor t = 1:size(curUniFR,1) + if mean(isnan(idVect1Shfl(t,:)))>0.5 || mean(isnan(idVect2Shfl(t,:)))>0.5 || mean(isnan(idVect3Shfl(t,:)))>0.5 + continue; + else + [~,table,~] = anovan(curUniFR(t,:)', {idVect1Shfl(t,:), idVect2Shfl(t,:), idVect3Shfl(t,:)}, 'model', [1 1 1], 'sstype', 'h', 'display', 'off') + if ~isempty(table{2,5}) + fVectPerm(r,t) = table{2,6}; + else + fVectPerm(r,t) = 1; + end + end + end +end +fVectPerm(isnan(fVectPerm)) = 1; +fVectZ = nan(1,size(curUniFR,1)); +for t = 1:size(curUniFR,1) + tempZ = zscore([fVectRaw(t); fVectPerm(:,t)]); + fVectZ(t) = tempZ(1); +end + end \ No newline at end of file From 2df911d77547c2afc42557c22d4d80c1e3854b9c Mon Sep 17 00:00:00 2001 From: gabe Date: Fri, 10 May 2019 13:11:20 -0700 Subject: [PATCH 009/133] Update PositionInformationFval_SM.m --- Analyses/PositionInformationFval_SM.m | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Analyses/PositionInformationFval_SM.m b/Analyses/PositionInformationFval_SM.m index b9a6e39..8a28db3 100644 --- a/Analyses/PositionInformationFval_SM.m +++ b/Analyses/PositionInformationFval_SM.m @@ -209,6 +209,8 @@ pokeInOrientBinsISC = cell2mat(reshape(orientBinValsPI(trialLog), [1,1,sum(trialLog)])); pokeOutOrientBinsISC = cell2mat(reshape(orientBinValsPO(trialLog), [1,1,sum(trialLog)])); +fValsPI = cell(4,length(ensembleUnitSummaries)); +fValsPO = cell(4,length(ensembleUnitSummaries)); %% Run Sliding F-Ratio Analysis for uni = 1:length(ensembleUnitSummaries) curUniPokeInFR = reshape(pokeInFRisc(:,uni+1,:), [size(pokeInFRisc,1), size(pokeInFRisc,3), 1]); @@ -216,13 +218,17 @@ % Poke In Oriented [curUniPIfvalPOS, curUniPIfvalPOSz] = SlidingFvalCalc(curUniPokeInFR, posIDsISC, numPerms); + fValsPI{1,uni} = curUniPIfvalPOSz; [curUniPIfvalHeadLoc, curUniPIfvalHeadLocZ] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientBinsISC(:,1,:), [size(pokeInOrientBinsISC,1), size(pokeInOrientBinsISC,3), 1]), numPerms); + fValsPI{2,uni} = curUniPIfvalHeadLocZ; [curUniPIfvalTailLoc, curUniPIfvalTailLocZ] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientBinsISC(:,2,:), [size(pokeInOrientBinsISC,1), size(pokeInOrientBinsISC,3), 1]), numPerms); + fValsPI{3,uni} = curUniPIfvalTailLocZ; [curUniPIfvalInteract, curUniPIfvalInteractZ] = SlidingFvalCalcInteract(curUniPokeInFR, posIDsISC,... reshape(pokeInOrientBinsISC(:,1,:), [size(pokeInOrientBinsISC,1), size(pokeInOrientBinsISC,3), 1]),... reshape(pokeInOrientBinsISC(:,2,:), [size(pokeInOrientBinsISC,1), size(pokeInOrientBinsISC,3), 1]),... numPerms); + fValsPI{4,uni} = curUniPIfvalInteractZ; % @@ -242,13 +248,17 @@ % % Poke Out Oriented [curUniPOfvalPOS, curUniPOfvalPOSz] = SlidingFvalCalc(curUniPokeOutFR, posIDsISC, numPerms); + fValsPO{1,uni} = curUniPOfvalPOSz; [curUniPOfvalHeadLoc, curUniPOfvalHeadLocZ] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientBinsISC(:,1,:), [size(pokeOutOrientBinsISC,1), size(pokeOutOrientBinsISC,3), 1]), numPerms); + fValsPO{2,uni} = curUniPOfvalHeadLocZ; [curUniPOfvalTailLoc, curUniPOfvalTailLocZ] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientBinsISC(:,2,:), [size(pokeOutOrientBinsISC,1), size(pokeOutOrientBinsISC,3), 1]), numPerms); + fValsPO{3,uni} = curUniPOfvalTailLocZ; [curUniPOfvalInteract, curUniPOfvalInteractZ] = SlidingFvalCalcInteract(curUniPokeOutFR, posIDsISC,... reshape(pokeOutOrientBinsISC(:,1,:), [size(pokeOutOrientBinsISC,1), size(pokeOutOrientBinsISC,3), 1]),... reshape(pokeOutOrientBinsISC(:,2,:), [size(pokeOutOrientBinsISC,1), size(pokeOutOrientBinsISC,3), 1]),... numPerms); + fValsPO{4,uni} = curUniPOfvalInteractZ; % % [curUniPOfvalHeadX, curUniPOfvalHeadXz] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,headXcolLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); % [curUniPOfvalHeadY, curUniPOfvalHeadYz] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,headYcolLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); From 0e7b53f805105fef458d852a83f6d64c29da3f1e Mon Sep 17 00:00:00 2001 From: gaelias Date: Sat, 11 May 2019 17:07:57 -0700 Subject: [PATCH 010/133] Update PositionInformationContent_SM.m --- Analyses/PositionInformationContent_SM.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Analyses/PositionInformationContent_SM.m b/Analyses/PositionInformationContent_SM.m index 33e5187..b7e25d8 100644 --- a/Analyses/PositionInformationContent_SM.m +++ b/Analyses/PositionInformationContent_SM.m @@ -13,8 +13,8 @@ %% Define Standard Variables timeBinSize = 0.025; -slideWindowSize = 30; -spatialBinSize = 2; +slideWindowSize = 100; +spatialBinSize = 5; %% Load Relevant Data load(fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'BehaviorMatrix'))}); From 53e9a16a96a87f0e09e984a2b40175b62735b87c Mon Sep 17 00:00:00 2001 From: gaelias Date: Sat, 11 May 2019 17:08:03 -0700 Subject: [PATCH 011/133] Update PositionInformationFval_SM.m --- Analyses/PositionInformationFval_SM.m | 369 +++++++++++++++++++++----- 1 file changed, 305 insertions(+), 64 deletions(-) diff --git a/Analyses/PositionInformationFval_SM.m b/Analyses/PositionInformationFval_SM.m index 8a28db3..56928e6 100644 --- a/Analyses/PositionInformationFval_SM.m +++ b/Analyses/PositionInformationFval_SM.m @@ -102,9 +102,9 @@ instFRgauss = instFRgauss/(length(instFRgauss)*mode(diff(behavMatrix(:,1)))); % Convolve Gaussian with Binary Spike Trains -uniInstFR = [ensembleMatrix(:,1), nan(size(ensembleMatrix,1), size(ensembleMatrix,2)-1)]; +uniInstFR = nan(size(ensembleMatrix,1), size(ensembleMatrix,2)-1); for uni = 2:size(ensembleMatrix,2) - uniInstFR(:,uni) = conv(ensembleMatrix(:,uni), instFRgauss, 'same'); + uniInstFR(:,uni-1) = conv(ensembleMatrix(:,uni), instFRgauss, 'same'); end % % % Alternatively, just bin the firing rates @@ -144,6 +144,12 @@ pokeOutOrientation = ExtractTrialData_SM(pokeOutTrialMatrix, orientMatrix); pokeOutOrientationInterp = cell(size(pokeOutOrientation)); +%% Remove Units with low firing rates +uniFRthreshLog = max([max(mean(pokeInFRisc,3)); max(mean(pokeOutFRisc,3))])<1; +pokeInFRisc(:,uniFRthreshLog,:) = []; +pokeOutFRisc(:,uniFRthreshLog,:) = []; +goodUniNames = {ensembleUnitSummaries(~uniFRthreshLog).UnitName}; + %% Pre-Process the positional data v2 % First thing to do is nan out the pre trial periods that don't have % positional data (in the pokeIn aligned) as well as the post trial periods @@ -209,12 +215,60 @@ pokeInOrientBinsISC = cell2mat(reshape(orientBinValsPI(trialLog), [1,1,sum(trialLog)])); pokeOutOrientBinsISC = cell2mat(reshape(orientBinValsPO(trialLog), [1,1,sum(trialLog)])); +trialOrientBinsISC = [pokeInOrientBinsISC; pokeOutOrientBinsISC]; + fValsPI = cell(4,length(ensembleUnitSummaries)); fValsPO = cell(4,length(ensembleUnitSummaries)); + +%% Calculate each unit's FR map and positional firing rates and such... +for uni = 1:length(goodUniNames) + curUniFR = [pokeInFRisc(:,uni,:); pokeOutFRisc(:,uni,:)]; + tempHeadFRmap = nan(length(headYbins)-1, length(headXbins)-1); + for headNdx = 1:((length(headXbins)-1)*(length(headYbins)-1)) + tempHeadFRmap(headNdx) = mean(curUniFR(trialOrientBinsISC(:,1,:)==headNdx)); + end + tempTailFRmap = nan(length(tailYbins)-1, length(tailXbins)-1); + for tailNdx = 1:((length(tailXbins)-1)*(length(tailYbins)-1)) + tempTailFRmap(tailNdx) = mean(curUniFR(trialOrientBinsISC(:,2,:)==tailNdx)); + end + + figure; + sp1 = subplot(1,2,1); + imagesc(tempHeadFRmap) + set(gca, 'ydir', 'normal'); + title('Head Occupancy'); + cb = colorbar('southoutside'); + cb.Label.String = 'Mean Firing Rate (spk/s)'; + z = colormap('jet'); + z(1,:) = [1 1 1]; + colormap(z); + set(gca, 'clim', [-0.01, max(get(gca, 'clim'))]); + sp2 = subplot(1,2,2); + imagesc(tempTailFRmap); + set(gca, 'ydir', 'normal'); + title('Head Position FR'); + cb = colorbar('southoutside'); + cb.Label.String = 'Mean Firing Rate (spk/s)'; + colormap(z); + set(sp1, 'clim', [-0.01, max([get(sp1, 'clim'), get(sp2, 'clim')])]); + set(sp2, 'clim', [-0.01, max([get(sp1, 'clim'), get(sp2, 'clim')])]); + annotation('textbox', [0.05 0.9 0.9 0.1], 'String', goodUniNames{uni}, 'linestyle', 'none', 'FontSize', 20); + annotation('textbox', [0.2 0.9 0.8 0.1], 'String', sprintf('%s Spatial Bin = %.02fmm Gaussian = %.02fms Number of Permutations = %i', seqType, spatialBinSize*1.7, slideWindowSize, numPerms), 'HorizontalAlignment', 'right', 'linestyle', 'none', 'FontSize', 12); + annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string', sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); + + orient(gcf, 'tall'); + orient(gcf, 'landscape'); + print('-painters', gcf, '-dpdf', sprintf('%s_FiringRateLocationMaps', goodUniNames{uni})); + + %*****************Make these figures broken down by sequence position + % Then do a comparison of FR maps across sequence position to look for + % modifications in FR maps by sequence position. +end + %% Run Sliding F-Ratio Analysis -for uni = 1:length(ensembleUnitSummaries) - curUniPokeInFR = reshape(pokeInFRisc(:,uni+1,:), [size(pokeInFRisc,1), size(pokeInFRisc,3), 1]); - curUniPokeOutFR = reshape(pokeOutFRisc(:,uni+1,:), [size(pokeOutFRisc,1), size(pokeOutFRisc,3), 1]); +for uni = 1:length(goodUniNames) + curUniPokeInFR = reshape(pokeInFRisc(:,uni,:), [size(pokeInFRisc,1), size(pokeInFRisc,3), 1]); + curUniPokeOutFR = reshape(pokeOutFRisc(:,uni,:), [size(pokeOutFRisc,1), size(pokeOutFRisc,3), 1]); % Poke In Oriented [curUniPIfvalPOS, curUniPIfvalPOSz] = SlidingFvalCalc(curUniPokeInFR, posIDsISC, numPerms); @@ -229,23 +283,23 @@ reshape(pokeInOrientBinsISC(:,2,:), [size(pokeInOrientBinsISC,1), size(pokeInOrientBinsISC,3), 1]),... numPerms); fValsPI{4,uni} = curUniPIfvalInteractZ; - - -% -% [curUniPIfvalHeadX, curUniPIfvalHeadXz] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,headXcolLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); -% [curUniPIfvalHeadY, curUniPIfvalHeadYz] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,headYcolLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); -% -% [curUniPIfvalTailX, curUniPIfvalTailXz] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,tailXcolLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); -% [curUniPIfvalTailY, curUniPIfvalTailYz] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,tailYcolLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); -% -% [curUniPIfvalPortAngle, curUniPIfvalPortAnglez] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,portAngleColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); -% [curUniPIfvalHeadAngle, curUniPIfvalHeadAnglez] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,headAngleColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); -% [curUniPIfvalTailAngle, curUniPIfvalTailAnglez] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,tailAngleColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); -% -% [curUniPIfvalHeadTailDist, curUniPIfvalHeadTailDistZ] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,htLengthColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); -% [curUniPIfvalHeadPortDist, curUniPIfvalHeadPortDistZ] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,hpLengthColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); -% [curUniPIfvalTailPortDist, curUniPIfvalTailPortDistZ] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,ptLengthColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); -% + + + % + % [curUniPIfvalHeadX, curUniPIfvalHeadXz] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,headXcolLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); + % [curUniPIfvalHeadY, curUniPIfvalHeadYz] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,headYcolLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); + % + % [curUniPIfvalTailX, curUniPIfvalTailXz] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,tailXcolLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); + % [curUniPIfvalTailY, curUniPIfvalTailYz] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,tailYcolLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); + % + % [curUniPIfvalPortAngle, curUniPIfvalPortAnglez] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,portAngleColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); + % [curUniPIfvalHeadAngle, curUniPIfvalHeadAnglez] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,headAngleColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); + % [curUniPIfvalTailAngle, curUniPIfvalTailAnglez] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,tailAngleColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); + % + % [curUniPIfvalHeadTailDist, curUniPIfvalHeadTailDistZ] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,htLengthColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); + % [curUniPIfvalHeadPortDist, curUniPIfvalHeadPortDistZ] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,hpLengthColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); + % [curUniPIfvalTailPortDist, curUniPIfvalTailPortDistZ] = SlidingFvalCalc(curUniPokeInFR, reshape(pokeInOrientISC(:,ptLengthColLog,:), [size(pokeInOrientISC,1), size(pokeInOrientISC,3), 1]), numPerms); + % % Poke Out Oriented [curUniPOfvalPOS, curUniPOfvalPOSz] = SlidingFvalCalc(curUniPokeOutFR, posIDsISC, numPerms); fValsPO{1,uni} = curUniPOfvalPOSz; @@ -253,48 +307,48 @@ fValsPO{2,uni} = curUniPOfvalHeadLocZ; [curUniPOfvalTailLoc, curUniPOfvalTailLocZ] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientBinsISC(:,2,:), [size(pokeOutOrientBinsISC,1), size(pokeOutOrientBinsISC,3), 1]), numPerms); fValsPO{3,uni} = curUniPOfvalTailLocZ; - + [curUniPOfvalInteract, curUniPOfvalInteractZ] = SlidingFvalCalcInteract(curUniPokeOutFR, posIDsISC,... reshape(pokeOutOrientBinsISC(:,1,:), [size(pokeOutOrientBinsISC,1), size(pokeOutOrientBinsISC,3), 1]),... reshape(pokeOutOrientBinsISC(:,2,:), [size(pokeOutOrientBinsISC,1), size(pokeOutOrientBinsISC,3), 1]),... numPerms); fValsPO{4,uni} = curUniPOfvalInteractZ; -% -% [curUniPOfvalHeadX, curUniPOfvalHeadXz] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,headXcolLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); -% [curUniPOfvalHeadY, curUniPOfvalHeadYz] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,headYcolLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); -% -% [curUniPOfvalTailX, curUniPOfvalTailXz] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,tailXcolLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); -% [curUniPOfvalTailY, curUniPOfvalTailYz] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,tailYcolLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); -% -% [curUniPOvalPortAngle, curUniPOfvalPortAnglez] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,portAngleColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); -% [curUniPOfvalHeadAngle, curUniPOfvalHeadAnglez] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,headAngleColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); -% [curUniPOfvalTailAngle, curUniPOfvalTailAnglez] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,tailAngleColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); -% -% [curUniPOfvalHeadTailDist, curUniPOfvalHeadTailDistZ] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,htLengthColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); -% [curUniPOfvalHeadPortDist, curUniPOfvalHeadPortDistZ] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,hpLengthColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); -% [curUniPOfvalTailPortDist, curUniPOfvalTailPortDistZ] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,ptLengthColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); -% - figure; + % + % [curUniPOfvalHeadX, curUniPOfvalHeadXz] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,headXcolLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); + % [curUniPOfvalHeadY, curUniPOfvalHeadYz] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,headYcolLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); + % + % [curUniPOfvalTailX, curUniPOfvalTailXz] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,tailXcolLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); + % [curUniPOfvalTailY, curUniPOfvalTailYz] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,tailYcolLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); + % + % [curUniPOvalPortAngle, curUniPOfvalPortAnglez] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,portAngleColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); + % [curUniPOfvalHeadAngle, curUniPOfvalHeadAnglez] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,headAngleColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); + % [curUniPOfvalTailAngle, curUniPOfvalTailAnglez] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,tailAngleColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); + % + % [curUniPOfvalHeadTailDist, curUniPOfvalHeadTailDistZ] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,htLengthColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); + % [curUniPOfvalHeadPortDist, curUniPOfvalHeadPortDistZ] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,hpLengthColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); + % [curUniPOfvalTailPortDist, curUniPOfvalTailPortDistZ] = SlidingFvalCalc(curUniPokeOutFR, reshape(pokeOutOrientISC(:,ptLengthColLog,:), [size(pokeOutOrientISC,1), size(pokeOutOrientISC,3), 1]), numPerms); + % + figure; sp1 = subplot(2,1,1); plot(pokeInTSs, curUniPIfvalPOSz,'linewidth', 2); - hold on; + hold on; plot(pokeInTSs, curUniPIfvalHeadLocZ, 'k'); plot(pokeInTSs, curUniPIfvalTailLocZ, '--k'); plot(pokeInTSs, curUniPIfvalInteractZ, 'r'); -% plot(pokeInTSs, curUniPIfvalHeadXz, 'k'); -% plot(pokeInTSs, curUniPIfvalHeadYz, '--k'); -% plot(pokeInTSs, curUniPIfvalTailXz, 'r'); -% plot(pokeInTSs, curUniPIfvalTailYz, '--r'); -% plot(pokeInTSs, curUniPIfvalPortAnglez, 'g'); -% plot(pokeInTSs, curUniPIfvalHeadAnglez, '--g'); -% plot(pokeInTSs, curUniPIfvalTailAnglez, '.g'); -% plot(pokeInTSs, curUniPIfvalHeadTailDistZ, 'c'); -% plot(pokeInTSs, curUniPIfvalHeadPortDistZ, '--c'); -% plot(pokeInTSs, curUniPIfvalTailPortDistZ, '.c'); + % plot(pokeInTSs, curUniPIfvalHeadXz, 'k'); + % plot(pokeInTSs, curUniPIfvalHeadYz, '--k'); + % plot(pokeInTSs, curUniPIfvalTailXz, 'r'); + % plot(pokeInTSs, curUniPIfvalTailYz, '--r'); + % plot(pokeInTSs, curUniPIfvalPortAnglez, 'g'); + % plot(pokeInTSs, curUniPIfvalHeadAnglez, '--g'); + % plot(pokeInTSs, curUniPIfvalTailAnglez, '.g'); + % plot(pokeInTSs, curUniPIfvalHeadTailDistZ, 'c'); + % plot(pokeInTSs, curUniPIfvalHeadPortDistZ, '--c'); + % plot(pokeInTSs, curUniPIfvalTailPortDistZ, '.c'); title('Poke In Aligned') axis tight legend('Sequence Position', 'Head', 'Tail', 'Interaction', 'location', 'best'); -% legend('Sequence Position', 'Head Loc X', 'Head Loc Y', 'Tail Loc X', 'Tail Loc Y', 'Port Angle', 'Head Angle', 'Tail Angle', 'Head-Tail', 'Head-Port', 'Tail-Port', 'location', 'best'); + % legend('Sequence Position', 'Head Loc X', 'Head Loc Y', 'Tail Loc X', 'Tail Loc Y', 'Port Angle', 'Head Angle', 'Tail Angle', 'Head-Tail', 'Head-Port', 'Tail-Port', 'location', 'best'); sp2 = subplot(2,1,2); plot(pokeOutTSs, curUniPOfvalPOSz, 'linewidth', 2); @@ -302,30 +356,217 @@ plot(pokeOutTSs, curUniPOfvalHeadLocZ, 'k'); plot(pokeOutTSs, curUniPOfvalTailLocZ, '--k'); plot(pokeOutTSs, curUniPOfvalInteractZ, 'r'); -% plot(pokeOutTSs, curUniPOfvalHeadXz, 'k'); -% plot(pokeOutTSs, curUniPOfvalHeadYz, '--k'); -% plot(pokeOutTSs, curUniPOfvalTailXz, 'r'); -% plot(pokeOutTSs, curUniPOfvalTailYz, '--r'); -% plot(pokeOutTSs, curUniPOfvalPortAnglez, 'g'); -% plot(pokeOutTSs, curUniPOfvalHeadAnglez, '--g'); -% plot(pokeOutTSs, curUniPOfvalTailAnglez, '.g'); -% plot(pokeOutTSs, curUniPOfvalHeadTailDistZ, 'c'); -% plot(pokeOutTSs, curUniPOfvalHeadPortDistZ, '--c'); -% plot(pokeOutTSs, curUniPOfvalTailPortDistZ, '.c'); + % plot(pokeOutTSs, curUniPOfvalHeadXz, 'k'); + % plot(pokeOutTSs, curUniPOfvalHeadYz, '--k'); + % plot(pokeOutTSs, curUniPOfvalTailXz, 'r'); + % plot(pokeOutTSs, curUniPOfvalTailYz, '--r'); + % plot(pokeOutTSs, curUniPOfvalPortAnglez, 'g'); + % plot(pokeOutTSs, curUniPOfvalHeadAnglez, '--g'); + % plot(pokeOutTSs, curUniPOfvalTailAnglez, '.g'); + % plot(pokeOutTSs, curUniPOfvalHeadTailDistZ, 'c'); + % plot(pokeOutTSs, curUniPOfvalHeadPortDistZ, '--c'); + % plot(pokeOutTSs, curUniPOfvalTailPortDistZ, '.c'); title('Poke Out Aligned'); axis tight linkaxes([sp1, sp2], 'y'); - annotation('textbox', [0.05 0.9 0.9 0.1], 'String', ensembleMatrixColIDs{uni+1}, 'linestyle', 'none', 'FontSize', 20); + annotation('textbox', [0.05 0.9 0.9 0.1], 'String', goodUniNames{uni}, 'linestyle', 'none', 'FontSize', 20); annotation('textbox', [0.2 0.9 0.8 0.1], 'String', sprintf('%s Spatial Bin = %.02fmm Gaussian = %.02fms Number of Permutations = %i', seqType, spatialBinSize*1.7, slideWindowSize, numPerms), 'HorizontalAlignment', 'right', 'linestyle', 'none', 'FontSize', 12); annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string', sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); orient(gcf, 'tall'); orient(gcf, 'landscape'); -% print - print('-painters', gcf, '-dpdf', sprintf('%s_OrientFvalInfo_Summary', ensembleMatrixColIDs{uni+1})); + % print + print('-painters', gcf, '-dpdf', sprintf('%s_OrientFvalInfo_Summary', goodUniNames{uni})); +end + +%% Now summarize the population data +% Row 1 = Position vs Head Location +% Row 2 = Position vs Tail Location +% Row 3 = Head vs Tail Location +piCorr = nan(3,length(goodUniNames)); +poCorr = nan(3,length(goodUniNames)); +allCorr = nan(3,length(goodUniNames)); +for uni = 1:length(goodUniNames) + curPIcorr = corr(cell2mat(fValsPI(:,uni))'); + piCorr(1,uni) = curPIcorr(2,1); + piCorr(2,uni) = curPIcorr(3,1); + piCorr(3,uni) = curPIcorr(3,2); + + curPOcorr = corr(cell2mat(fValsPO(:,uni))'); + poCorr(1,uni) = curPOcorr(2,1); + poCorr(2,uni) = curPOcorr(3,1); + poCorr(3,uni) = curPOcorr(3,2); + + curAllCorr = corr([cell2mat(fValsPI(:,uni)), cell2mat(fValsPO(:,uni))]'); + allCorr(1,uni) = curAllCorr(2,1); + allCorr(2,uni) = curAllCorr(3,1); + allCorr(3,uni) = curAllCorr(3,2); +end +figure; +sp1 = subplot(3,3,1); +histogram(piCorr(1,:), -1:0.1:1); +hold on; +line(gca, [nanmean(piCorr(1,:)) nanmean(piCorr(1,:))], get(gca, 'ylim'), 'color','r', 'linewidth', 2); +title('PI: Position vs. Head'); +sp2 = subplot(3,3,2); +histogram(piCorr(2,:), -1:0.1:1); +hold on; +line(gca, [nanmean(piCorr(2,:)) nanmean(piCorr(2,:))], get(gca, 'ylim'), 'color','r', 'linewidth', 2); +title('PI: Position vs. Tail'); +sp3 = subplot(3,3,3); +histogram(piCorr(3,:), -1:0.1:1); +hold on; +line(gca, [nanmean(piCorr(3,:)) nanmean(piCorr(3,:))], get(gca, 'ylim'), 'color','r', 'linewidth', 2); +title('PI: Head vs. Tail'); +sp4 = subplot(3,3,4); +histogram(poCorr(1,:), -1:0.1:1); +hold on; +line(gca, [nanmean(poCorr(1,:)) nanmean(poCorr(1,:))], get(gca, 'ylim'), 'color','r', 'linewidth', 2); +title('PO: Position vs. Head'); +sp5 = subplot(3,3,5); +histogram(poCorr(2,:), -1:0.1:1); +hold on; +line(gca, [nanmean(poCorr(2,:)) nanmean(poCorr(2,:))], get(gca, 'ylim'), 'color','r', 'linewidth', 2); +title('PO: Position vs. Tail'); +sp6 = subplot(3,3,6); +histogram(poCorr(3,:), -1:0.1:1); +hold on; +line(gca, [nanmean(poCorr(3,:)) nanmean(poCorr(3,:))], get(gca, 'ylim'), 'color','r', 'linewidth', 2); +title('PO: Head vs. Tail'); +sp7 = subplot(3,3,7); +histogram(allCorr(1,:), -1:0.1:1); +hold on; +line(gca, [nanmean(allCorr(1,:)) nanmean(allCorr(1,:))], get(gca, 'ylim'), 'color','r', 'linewidth', 2); +title('All: Position vs. Head'); +sp8 = subplot(3,3,8); +histogram(allCorr(2,:), -1:0.1:1); +hold on; +line(gca, [nanmean(allCorr(2,:)) nanmean(allCorr(2,:))], get(gca, 'ylim'), 'color','r', 'linewidth', 2); +title('All: Position vs. Tail'); +sp9 = subplot(3,3,9); +histogram(allCorr(3,:), -1:0.1:1); +hold on; +line(gca, [nanmean(allCorr(3,:)) nanmean(allCorr(3,:))], get(gca, 'ylim'), 'color','r', 'linewidth', 2); +title('All: Head vs. Tail'); + +linkaxes([sp1, sp2, sp2, sp3, sp4, sp5, sp6, sp7, sp8, sp9], 'y'); +annotation('textbox', [0.05 0.9 0.9 0.1], 'String', 'All Timepoints', 'linestyle', 'none', 'FontSize', 20); +annotation('textbox', [0.2 0.9 0.8 0.1], 'String', sprintf('%s Spatial Bin = %.02fmm Gaussian = %.02fms Number of Permutations = %i', seqType, spatialBinSize*1.7, slideWindowSize, numPerms), 'HorizontalAlignment', 'right', 'linestyle', 'none', 'FontSize', 12); +annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string', sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); +%% Now run a thresholded version +% Row 1 = Position vs Head Location +% Row 2 = Position vs Tail Location +% Row 3 = Head vs Tail Location +piCorr = nan(3,length(goodUniNames)); +poCorr = nan(3,length(goodUniNames)); +allCorr = nan(3,length(goodUniNames)); +for uni = 1:length(goodUniNames) + curPIphCorr = corrcoef(fValsPI{1,uni}(fValsPI{1,uni}>1 | fValsPI{2,uni}>1), fValsPI{2,uni}(fValsPI{1,uni}>1 | fValsPI{2,uni}>1)); + piCorr(1,uni) = curPIphCorr(2,1); + curPIptCorr = corrcoef(fValsPI{1,uni}(fValsPI{1,uni}>1 | fValsPI{3,uni}>1), fValsPI{3,uni}(fValsPI{1,uni}>1 | fValsPI{3,uni}>1)); + piCorr(2,uni) = curPIptCorr(2,1); + curPIhtCorr = corrcoef(fValsPI{2,uni}(fValsPI{2,uni}>1 | fValsPI{3,uni}>1), fValsPI{3,uni}(fValsPI{2,uni}>1 | fValsPI{3,uni}>1)); + piCorr(3,uni) = curPIhtCorr(2,1); + + curPOphCorr = corrcoef(fValsPO{1,uni}(fValsPO{1,uni}>1 | fValsPO{2,uni}>1), fValsPO{2,uni}(fValsPO{1,uni}>1 | fValsPO{2,uni}>1)); + poCorr(1,uni) = curPOphCorr(2,1); + curPOptCorr = corrcoef(fValsPO{1,uni}(fValsPO{1,uni}>1 | fValsPO{3,uni}>1), fValsPO{3,uni}(fValsPO{1,uni}>1 | fValsPO{3,uni}>1)); + poCorr(2,uni) = curPOptCorr(2,1); + curPOhtCorr = corrcoef(fValsPO{2,uni}(fValsPO{2,uni}>1 | fValsPO{3,uni}>1), fValsPO{3,uni}(fValsPO{2,uni}>1 | fValsPO{3,uni}>1)); + poCorr(3,uni) = curPOhtCorr(2,1); + + curAllPosF = [fValsPI{1,uni}, fValsPO{1,uni}]; + curAllHeadF = [fValsPI{2,uni}, fValsPO{2,uni}]; + curAllTailF = [fValsPI{3,uni}, fValsPO{3,uni}]; + curALLphCorr = corrcoef(curAllPosF(curAllPosF>1 | curAllHeadF>1), curAllHeadF(curAllPosF>1 | curAllHeadF>1)); + allCorr(1,uni) = curALLphCorr(2,1); + curALLptCorr = corrcoef(curAllPosF(curAllPosF>1 | curAllTailF>1), curAllTailF(curAllPosF>1 | curAllTailF>1)); + allCorr(2,uni) = curALLptCorr(2,1); + curALLhtCorr = corrcoef(curAllHeadF(curAllHeadF>1 | curAllTailF>1), curAllTailF(curAllHeadF>1 | curAllTailF>1)); + allCorr(3,uni) = curALLhtCorr(2,1); + +end +figure; +sp1 = subplot(3,3,1); +histogram(piCorr(1,:), -1:0.1:1); +hold on; +line(gca, [nanmean(piCorr(1,:)) nanmean(piCorr(1,:))], get(gca, 'ylim'), 'color','r', 'linewidth', 2); +title('PI: Position vs. Head'); +sp2 = subplot(3,3,2); +histogram(piCorr(2,:), -1:0.1:1); +hold on; +line(gca, [nanmean(piCorr(2,:)) nanmean(piCorr(2,:))], get(gca, 'ylim'), 'color','r', 'linewidth', 2); +title('PI: Position vs. Tail'); +sp3 = subplot(3,3,3); +histogram(piCorr(3,:), -1:0.1:1); +hold on; +line(gca, [nanmean(piCorr(3,:)) nanmean(piCorr(3,:))], get(gca, 'ylim'), 'color','r', 'linewidth', 2); +title('PI: Head vs. Tail'); +sp4 = subplot(3,3,4); +histogram(poCorr(1,:), -1:0.1:1); +hold on; +line(gca, [nanmean(poCorr(1,:)) nanmean(poCorr(1,:))], get(gca, 'ylim'), 'color','r', 'linewidth', 2); +title('PO: Position vs. Head'); +sp5 = subplot(3,3,5); +histogram(poCorr(2,:), -1:0.1:1); +hold on; +line(gca, [nanmean(poCorr(2,:)) nanmean(poCorr(2,:))], get(gca, 'ylim'), 'color','r', 'linewidth', 2); +title('PO: Position vs. Tail'); +sp6 = subplot(3,3,6); +histogram(poCorr(3,:), -1:0.1:1); +hold on; +line(gca, [nanmean(poCorr(3,:)) nanmean(poCorr(3,:))], get(gca, 'ylim'), 'color','r', 'linewidth', 2); +title('PO: Head vs. Tail'); +sp7 = subplot(3,3,7); +histogram(allCorr(1,:), -1:0.1:1); +hold on; +line(gca, [nanmean(allCorr(1,:)) nanmean(allCorr(1,:))], get(gca, 'ylim'), 'color','r', 'linewidth', 2); +title('All: Position vs. Head'); +sp8 = subplot(3,3,8); +histogram(allCorr(2,:), -1:0.1:1); +hold on; +line(gca, [nanmean(allCorr(2,:)) nanmean(allCorr(2,:))], get(gca, 'ylim'), 'color','r', 'linewidth', 2); +title('All: Position vs. Tail'); +sp9 = subplot(3,3,9); +histogram(allCorr(3,:), -1:0.1:1); +hold on; +line(gca, [nanmean(allCorr(3,:)) nanmean(allCorr(3,:))], get(gca, 'ylim'), 'color','r', 'linewidth', 2); +title('All: Head vs. Tail'); + +linkaxes([sp1, sp2, sp2, sp3, sp4, sp5, sp6, sp7, sp8, sp9], 'y'); +annotation('textbox', [0.05 0.9 0.9 0.1], 'String', 'Threshold Fz>1', 'linestyle', 'none', 'FontSize', 20); +annotation('textbox', [0.2 0.9 0.8 0.1], 'String', sprintf('%s Spatial Bin = %.02fmm Gaussian = %.02fms Number of Permutations = %i', seqType, spatialBinSize*1.7, slideWindowSize, numPerms), 'HorizontalAlignment', 'right', 'linestyle', 'none', 'FontSize', 12); +annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string', sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); + +%% Examine the relationship between the peak FR and peak IC +fVals = cellfun(@(a,b)[a,b], fValsPI, fValsPO, 'uniformoutput', 0); +frMean = [mean(pokeInFRisc,3); mean(pokeOutFRisc,3)]; +fvMaxNdx = nan(3,length(goodUniNames)); +frMaxNdx = nan(1,length(goodUniNames)); +for uni = 1:length(goodUniNames) + fvMaxNdx(1,uni) = find(fVals{1,uni}==max(fVals{1,uni}),1,'first'); + fvMaxNdx(2,uni) = find(fVals{2,uni}==max(fVals{2,uni}),1,'first'); + fvMaxNdx(3,uni) = find(fVals{3,uni}==max(fVals{3,uni}),1,'first'); + frMaxNdx(uni) = find(frMean(:,uni)==max(frMean(:,uni)),1,'first'); end +figure; +subplot(1,3,1) +corrScatPlot(fvMaxNdx(1,:)', frMaxNdx', 'Index of Max Seq Pos Info', 'Index of Max Firing Rate', []); +subplot(1,3,2) +corrScatPlot(fvMaxNdx(2,:)', frMaxNdx', 'Index of Max Head Loc Info', 'Index of Max Firing Rate', []); +subplot(1,3,3) +corrScatPlot(fvMaxNdx(3,:)', frMaxNdx', 'Index of Max Tail Loc Info', 'Index of Max Firing Rate', []); + +figure; +BarPlotErrorbars([mean(abs(fvMaxNdx(1,:) - frMaxNdx)), mean(abs(fvMaxNdx(2,:) - frMaxNdx)), mean(abs(fvMaxNdx(3,:) - frMaxNdx))],... + [std(fvMaxNdx(1,:) - frMaxNdx)/sqrt(length(goodUniNames)-1), std(fvMaxNdx(2,:) - frMaxNdx)/sqrt(length(goodUniNames)-1), std(fvMaxNdx(3,:) - frMaxNdx)/sqrt(length(goodUniNames)-1)]); +set(gca, 'xticklabel', {'Position', 'Head', 'Tail'}); +title('Temporal Difference From Max Firing Rate'); +ylabel('Time (ms)'); + + %% function [fVectRaw, fVectZ] = SlidingFvalCalc(curUniFR, idVect, numPerms) % UniFR here is organized as a 2-D matrix where each column is a trial and From d0e152abc598cb28bbc85ea6d5293f1454a3b86d Mon Sep 17 00:00:00 2001 From: gaelias Date: Thu, 1 Aug 2019 10:17:46 -0700 Subject: [PATCH 012/133] 080119 - Updating Accumulated Changes --- Analyses/Old/UnitSummary_SM.m | 22 +- .../Plotting Functions/PlotUnitFeatures_SM.m | 2 +- Analyses/PositionInformationFval_SM.m | 22 +- Analyses/Prototypes/BayesPFCbsPROTO.m | 105 +++ Analyses/Prototypes/NoiseCorrPROTO.m | 755 ++++++++++++++++++ Analyses/Prototypes/PFC_DualList_PROTO.m | 44 + .../SequenceNeuralViewer_SM PROTO.m | 163 ++++ Analyses/SequenceNeuralViewer_SM.m | 166 ++++ Analyses/SummarizeUnits_SM.m | 104 +-- Data Management/OrganizeTrialData_SM.m | 12 +- manualArtifactRemoval.m | 2 +- statMatrix Creation/StatMatrixCreator.m | 12 +- .../Supporting Code/SummarizePLXevents_SD.m | 5 +- 13 files changed, 1336 insertions(+), 78 deletions(-) create mode 100644 Analyses/Prototypes/BayesPFCbsPROTO.m create mode 100644 Analyses/Prototypes/NoiseCorrPROTO.m create mode 100644 Analyses/Prototypes/PFC_DualList_PROTO.m create mode 100644 Analyses/Prototypes/SequenceNeuralViewer_SM PROTO.m create mode 100644 Analyses/SequenceNeuralViewer_SM.m diff --git a/Analyses/Old/UnitSummary_SM.m b/Analyses/Old/UnitSummary_SM.m index f43eb38..ef909ae 100644 --- a/Analyses/Old/UnitSummary_SM.m +++ b/Analyses/Old/UnitSummary_SM.m @@ -97,7 +97,7 @@ curUnis = statMatrixColIDs(cellfun(@(a)~isempty(a), regexp(statMatrixColIDs, '-U([0-9]*)'))); unitSummaryUnis = {unitSummary.UnitName}; - clear curTetStatDiffPERF curTetStatDiffTC +% clear curTetStatDiffPERF curTetStatDiffTC %% Plot Per-Tetrode Spectrograms % Performance @@ -114,11 +114,11 @@ PlotTrialEventSpect_SM(curTet, behEventData, behEventDataIDs,... tcCorrLogs, tcCorrLogIDs,... statMatrix, statMatrixColIDs, eventWindow, spectFreqWindow, printYN); - - % Temporal Context - Incorrect - PlotTrialEventSpect_SM(curTet, behEventData, behEventDataIDs,... - tcInCorrLogs, tcInCorrLogIDs,... - statMatrix, statMatrixColIDs, eventWindow, spectFreqWindow, printYN); +% +% % Temporal Context - Incorrect +% PlotTrialEventSpect_SM(curTet, behEventData, behEventDataIDs,... +% tcInCorrLogs, tcInCorrLogIDs,... +% statMatrix, statMatrixColIDs, eventWindow, spectFreqWindow, printYN); %% Spike Phase Relationship Stuff PlotSpikePhaseRelations_SM(statMatrix, statMatrixColIDs,... trialAlignedBehavMatrix,... @@ -138,7 +138,7 @@ % Figure 1: Overall Unit Summary fig1 = figure('Name', [curUnit ' Summary'], 'NumberTitle', 'off'); PlotUnitFeatures_SM(curUnitSummary, curUniSpikeTimes, fig1, printYN); - +% %% Performance PEHs % Overall overallPerf = PlotTrialEventPEH_SM(curUnit, behEventData, behEventDataIDs,... @@ -158,7 +158,7 @@ curUniSpikeLog, eventWindow, pehBinSize, printYN); % F-Ratio Analysis Odor vs Position - curTetStatDiffPERF(uni) = FratioAnalysis_SM(curUnit, perfByOdor, perfByPosition); %#ok +% curTetStatDiffPERF(uni) = FratioAnalysis_SM(curUnit, perfByOdor, perfByPosition); %#ok %% Temporal Context PEH % Overall @@ -179,7 +179,7 @@ curUniSpikeLog, eventWindow, pehBinSize, printYN); % F-Ratio Analysis Odor vs Position - curTetStatDiffTC(uni) = FratioAnalysis_SM(curUnit, tcByOdor, tcByPosition); %#ok +% curTetStatDiffTC(uni) = FratioAnalysis_SM(curUnit, tcByOdor, tcByPosition); %#ok %% Temporal Context X Performance PEH % Overall InSeq v OutSeq Corr @@ -226,7 +226,7 @@ %% close all end - statDiffPerTetPERF{t} = curTetStatDiffPERF; - statDiffPerTetTC{t} = curTetStatDiffTC; +% statDiffPerTetPERF{t} = curTetStatDiffPERF; +% statDiffPerTetTC{t} = curTetStatDiffTC; end %% \ No newline at end of file diff --git a/Analyses/Plotting Functions/PlotUnitFeatures_SM.m b/Analyses/Plotting Functions/PlotUnitFeatures_SM.m index 3ef24ac..fe5dc0c 100644 --- a/Analyses/Plotting Functions/PlotUnitFeatures_SM.m +++ b/Analyses/Plotting Functions/PlotUnitFeatures_SM.m @@ -110,7 +110,7 @@ function PlotUnitFeatures_SM(curUnitSummary, curUniSpikeTimes, figID, saveYN) text(5,2, ['\fontsize{8}' sprintf('%.02g', curUnitSummary.Spike_Phase_Relations.Theta.R_Length)], 'horizontalalignment', 'center'); text(5,1.2, ['\fontsize{8}' sprintf('r=%.02f', curUnitSummary.Spike_Phase_Relations.Theta.R_Test(2))], 'horizontalalignment', 'center'); text(5,0.8, ['\fontsize{8}' sprintf('(p=%.02f)', curUnitSummary.Spike_Phase_Relations.Theta.R_Test(1))], 'horizontalalignment', 'center'); - +% text(7,6, '\bf \fontsize{9} \alpha', 'horizontalalignment', 'center'); text(7,5, ['\fontsize{8}' sprintf('%.02g', curUnitSummary.Spike_Phase_Relations.Alpha.Mean)], 'horizontalalignment', 'center'); text(7,4, ['\fontsize{8}' sprintf('%.02g', curUnitSummary.Spike_Phase_Relations.Alpha.Median)], 'horizontalalignment', 'center'); diff --git a/Analyses/PositionInformationFval_SM.m b/Analyses/PositionInformationFval_SM.m index 56928e6..a2a805e 100644 --- a/Analyses/PositionInformationFval_SM.m +++ b/Analyses/PositionInformationFval_SM.m @@ -451,9 +451,13 @@ title('All: Head vs. Tail'); linkaxes([sp1, sp2, sp2, sp3, sp4, sp5, sp6, sp7, sp8, sp9], 'y'); + +orient(gcf, 'tall'); +orient(gcf, 'landscape'); annotation('textbox', [0.05 0.9 0.9 0.1], 'String', 'All Timepoints', 'linestyle', 'none', 'FontSize', 20); annotation('textbox', [0.2 0.9 0.8 0.1], 'String', sprintf('%s Spatial Bin = %.02fmm Gaussian = %.02fms Number of Permutations = %i', seqType, spatialBinSize*1.7, slideWindowSize, numPerms), 'HorizontalAlignment', 'right', 'linestyle', 'none', 'FontSize', 12); annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string', sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); +print('-painters', gcf, '-dpdf', 'LocationVsPositionCorrelationSummary(All)'); %% Now run a thresholded version % Row 1 = Position vs Head Location % Row 2 = Position vs Tail Location @@ -535,10 +539,13 @@ title('All: Head vs. Tail'); linkaxes([sp1, sp2, sp2, sp3, sp4, sp5, sp6, sp7, sp8, sp9], 'y'); + +orient(gcf, 'tall'); +orient(gcf, 'landscape'); annotation('textbox', [0.05 0.9 0.9 0.1], 'String', 'Threshold Fz>1', 'linestyle', 'none', 'FontSize', 20); annotation('textbox', [0.2 0.9 0.8 0.1], 'String', sprintf('%s Spatial Bin = %.02fmm Gaussian = %.02fms Number of Permutations = %i', seqType, spatialBinSize*1.7, slideWindowSize, numPerms), 'HorizontalAlignment', 'right', 'linestyle', 'none', 'FontSize', 12); annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string', sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); - +print('-painters', gcf, '-dpdf', 'LocationVsPositionCorrelationSummary(Thresholded)'); %% Examine the relationship between the peak FR and peak IC fVals = cellfun(@(a,b)[a,b], fValsPI, fValsPO, 'uniformoutput', 0); frMean = [mean(pokeInFRisc,3); mean(pokeOutFRisc,3)]; @@ -559,6 +566,13 @@ subplot(1,3,3) corrScatPlot(fvMaxNdx(3,:)', frMaxNdx', 'Index of Max Tail Loc Info', 'Index of Max Firing Rate', []); +orient(gcf, 'tall'); +orient(gcf, 'landscape'); +annotation('textbox', [0.05 0.9 0.9 0.1], 'String', 'FR vs FZ correlation', 'linestyle', 'none', 'FontSize', 20); +annotation('textbox', [0.2 0.9 0.8 0.1], 'String', sprintf('%s Spatial Bin = %.02fmm Gaussian = %.02fms Number of Permutations = %i', seqType, spatialBinSize*1.7, slideWindowSize, numPerms), 'HorizontalAlignment', 'right', 'linestyle', 'none', 'FontSize', 12); +annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string', sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); +print('-painters', gcf, '-dpdf', 'FzVsFRcorr'); + figure; BarPlotErrorbars([mean(abs(fvMaxNdx(1,:) - frMaxNdx)), mean(abs(fvMaxNdx(2,:) - frMaxNdx)), mean(abs(fvMaxNdx(3,:) - frMaxNdx))],... [std(fvMaxNdx(1,:) - frMaxNdx)/sqrt(length(goodUniNames)-1), std(fvMaxNdx(2,:) - frMaxNdx)/sqrt(length(goodUniNames)-1), std(fvMaxNdx(3,:) - frMaxNdx)/sqrt(length(goodUniNames)-1)]); @@ -566,7 +580,11 @@ title('Temporal Difference From Max Firing Rate'); ylabel('Time (ms)'); - +orient(gcf, 'tall'); +orient(gcf, 'landscape'); +annotation('textbox', [0.2 0.9 0.8 0.1], 'String', sprintf('%s Spatial Bin = %.02fmm Gaussian = %.02fms Number of Permutations = %i', seqType, spatialBinSize*1.7, slideWindowSize, numPerms), 'HorizontalAlignment', 'right', 'linestyle', 'none', 'FontSize', 12); +annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string', sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); +print('-painters', gcf, '-dpdf', 'PeakInfoFiringRateLatency'); %% function [fVectRaw, fVectZ] = SlidingFvalCalc(curUniFR, idVect, numPerms) % UniFR here is organized as a 2-D matrix where each column is a trial and diff --git a/Analyses/Prototypes/BayesPFCbsPROTO.m b/Analyses/Prototypes/BayesPFCbsPROTO.m new file mode 100644 index 0000000..32e689f --- /dev/null +++ b/Analyses/Prototypes/BayesPFCbsPROTO.m @@ -0,0 +1,105 @@ +%% Runtime variables +binSize = 20; +dsRate = 5; + +%% +smPath = uigetdir; +cd(smPath); +files = dir(smPath); +fileNames = {files.name}; +% Load the behavior matrix file for poke events plots +behMatFile = fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'BehaviorMatrix'))}; +nsmblMatFile = fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'EnsembleMatrix'))}; +load([smPath '\' behMatFile]); +load([smPath '\' nsmblMatFile]); +% Identify list of all statMatrix files +smFileList = fileNames(cellfun(@(a)~isempty(a), regexp(fileNames, '_SM\>')))'; + +%% Extract Behavioral Periods +trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.5 1.5], 'PokeIn'); +trialEnsemble = ExtractTrialData_SM(trialPeriodTD, ensembleMatrix(:,2:end)); %#ok<*NODEF> +trialEnsembleMtx = cell2mat(reshape(trialEnsemble, [1 1 length(trialEnsemble)])); + +trialTimes = behavMatrix(trialPeriodTD(1).TrialLogVect,1) - behavMatrix(trialPeriodTD(1).PokeInIndex,1); + +%% Bin the spiking data +% First convolve the entire trialEnsembleMtx with a square to bin the +% spikes +binnedEnsembleMtx = nan(size(trialEnsembleMtx)); +for t = 1:size(trialEnsembleMtx,3) + for u = 1:size(trialEnsembleMtx,2) + binnedEnsembleMtx(:,u,t) = conv(trialEnsembleMtx(:,u,t), ones(1,binSize)./(binSize/1000), 'same'); + end +end +% Now downsample the binned matrix +dsVect = downsample(1:size(binnedEnsembleMtx,1), dsRate); +spikeMatrix = binnedEnsembleMtx(dsVect,:,:); +trialTime = trialTimes(dsVect); + +%% Create Logical Vectors +perfLog = [trialPeriodTD.Performance]; +inSeqLog = [trialPeriodTD.TranspositionDistance]==0; +outSeqLog = [trialPeriodTD.TranspositionDistance]~=0 & abs([trialPeriodTD.TranspositionDistance])<10; +odorAlog = [trialPeriodTD.Odor] == 1; +odorBlog = [trialPeriodTD.Odor] == 2; +odorClog = [trialPeriodTD.Odor] == 3; +odorDlog = [trialPeriodTD.Odor] == 4; + +fullInSeqSeqsStart = find(conv([trialPeriodTD.Odor], 1:4, 'valid')==20 & conv([trialPeriodTD.Position], 1:4, 'valid')==20 & conv([trialPeriodTD.Performance]*1, ones(1,4), 'valid')==4); +inSeqSeqs = nan(3,length(fullInSeqSeqsStart)); +for iS = 1:length(fullInSeqSeqsStart) + inSeqSeqs(1,iS) = fullInSeqSeqsStart(iS); + inSeqSeqs(2,iS) = fullInSeqSeqsStart(iS) + 1; + inSeqSeqs(3,iS) = fullInSeqSeqsStart(iS) + 2; + inSeqSeqs(4,iS) = fullInSeqSeqsStart(iS) + 3; +end +fullInSeqLog = false(1,length(trialPeriodTD)); +fullInSeqLog(inSeqSeqs(:)) = true; + +%% +uniFRthreshLog = max(mean(spikeMatrix,3))<1; +spkMtx = spikeMatrix; +spkMtx(:,uniFRthreshLog,:) = []; +goodUniNames = {ensembleUnitSummaries(~uniFRthreshLog).UnitName}; +%% +corrISmtx = mean(spkMtx(:,:,perfLog & inSeqLog),3); + + + + + +%% +function [postMtx] = CalculatePostProb(meanFR, trialFR) +propVect = CalculateProportionalConstant(meanFR); +post = nan(size(trialFR,1), size(trialFR,1), size(trialFR,3)); + +for trl = 1:size(trialFR,3) + tic + for i1 = 1:size(trialFR,1) + curPopVect = trialFR(i1,:,trl); + curPopVectFact = factorial(curPopVect); + for i2 = 1:size(trialFR,1) + curMeanFR = meanFR(i2,:); + condProbSpkPerUni = nan(size(curMeanFR)); + for uni = 1:size(trialFR,2) + condProbSpkPerUni(uni) = (((binSize/1000 * curMeanFR(uni))^curPopVect(uni))/curPopVectFact(uni)) * exp(-(binSize/1000*curMeanFR(uni))); + end + post(i1,i2,trl) = propVect(i1)*prod(condProbSpkPerUni); + end + end + toc +end + +end + +%% +function [propConst] = CalculateProportionalConstant(rateMtx) +% propConstMtx = nan(size(rateMtx)); +% for u = 1:size(rateMtx,2) +% propConstMtx(:,u) = 1./(rateMtx(:,u).*sum(rateMtx(:,u)~=0)); +% end +% propConstMtx(isinf(propConstMtx)) = 0; + +% sum(rateMtx'.*(1./sum(rateMtx'))) +propConstMtx = 1./sum(rateMtx'); +end diff --git a/Analyses/Prototypes/NoiseCorrPROTO.m b/Analyses/Prototypes/NoiseCorrPROTO.m new file mode 100644 index 0000000..57bbfda --- /dev/null +++ b/Analyses/Prototypes/NoiseCorrPROTO.m @@ -0,0 +1,755 @@ +function NoiseCorrPROTO +smPath = uigetdir; +cd(smPath); +files = dir(smPath); +fileNames = {files.name}; +% Load the behavior matrix file for poke events plots +behMatFile = fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'BehaviorMatrix'))}; +nsmblMatFile = fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'EnsembleMatrix'))}; +load([smPath '\' behMatFile]); +load([smPath '\' nsmblMatFile]); +% Identify list of all statMatrix files +smFileList = fileNames(cellfun(@(a)~isempty(a), regexp(fileNames, '_SM\>')))'; + +%% Extract Behavioral Periods +preTrialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.5 0], 'PokeIn'); +preTrialEnsemble = ExtractTrialData_SM(preTrialPeriodTD, ensembleMatrix(:,2:end)); %#ok<*NODEF> +preTrialPopVect = cell2mat(cellfun(@(a)sum(a,1), preTrialEnsemble, 'uniformoutput', 0)'); + +rlyTrialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [0 0.5], 'PokeIn'); +rlyTrialEnsemble = ExtractTrialData_SM(rlyTrialPeriodTD, ensembleMatrix(:,2:end)); +rlyTrialPopVect = cell2mat(cellfun(@(a)sum(a,1), rlyTrialEnsemble, 'uniformoutput', 0)'); + +latTrialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.5 0], 'PokeOut'); +latTrialEnsemble = ExtractTrialData_SM(latTrialPeriodTD, ensembleMatrix(:,2:end)); +latTrialPopVect = cell2mat(cellfun(@(a)sum(a,1), latTrialEnsemble, 'uniformoutput', 0)'); + +pstTrialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [0 0.5], 'PokeOut'); +pstTrialEnsemble = ExtractTrialData_SM(pstTrialPeriodTD, ensembleMatrix(:,2:end)); +pstTrialPopVect = cell2mat(cellfun(@(a)sum(a,1), pstTrialEnsemble, 'uniformoutput', 0)'); + +allTrialPopVectZ = zscore([preTrialPopVect; rlyTrialPopVect; latTrialPopVect;pstTrialPopVect],0,1); + +preTrialPopVectZall = allTrialPopVectZ(1:size(allTrialPopVectZ,1)/4,:); +rlyTrialPopVectZall = allTrialPopVectZ(size(allTrialPopVectZ,1)/4+1:size(allTrialPopVectZ,1)/2,:); +latTrialPopVectZall = allTrialPopVectZ(size(allTrialPopVectZ,1)/2+1:size(allTrialPopVectZ,1)*.75,:); +pstTrialPopVectZall = allTrialPopVectZ(size(allTrialPopVectZ,1)*.75+1:end,:); + +%% Create Session Trial ID Logical Vectors +% Using preTrialPeriodTD here arbitrarilly, any of the TD variables above +% would work and they're all identical. +perfLog = [preTrialPeriodTD.Performance]; +inSeqLog = [preTrialPeriodTD.TranspositionDistance]==0; +outSeqLog = [preTrialPeriodTD.TranspositionDistance]~=0 & abs([preTrialPeriodTD.TranspositionDistance])<10; +odorAlog = [preTrialPeriodTD.Odor] == 1; +odorBlog = [preTrialPeriodTD.Odor] == 2; +odorClog = [preTrialPeriodTD.Odor] == 3; +odorDlog = [preTrialPeriodTD.Odor] == 4; + +fullInSeqSeqsStart = find(conv([preTrialPeriodTD.Odor], 1:4, 'valid')==20 & conv([preTrialPeriodTD.Position], 1:4, 'valid')==20 & conv([preTrialPeriodTD.Performance]*1, ones(1,4), 'valid')==4); +inSeqSeqs = nan(3,length(fullInSeqSeqsStart)); +for iS = 1:length(fullInSeqSeqsStart) + inSeqSeqs(1,iS) = fullInSeqSeqsStart(iS); + inSeqSeqs(2,iS) = fullInSeqSeqsStart(iS) + 1; + inSeqSeqs(3,iS) = fullInSeqSeqsStart(iS) + 2; + inSeqSeqs(4,iS) = fullInSeqSeqsStart(iS) + 3; +end +fullInSeqLog = false(1,length(preTrialPeriodTD)); +fullInSeqLog(inSeqSeqs(:)) = true; + +%% Conv test/verification +% anss = nan(4); +% for a = 1:4 +% for b = 1:4 +% temp = 1:4; +% temp(b) = a; +% anss(a,b) = conv(temp, 1:4, 'valid'); +% end +% end +% anss +%% +% [corrTrlsPop, corrTrlsNoise] = ExaminePopAndNoiseCorrs(preTrialPopVectZall, rlyTrialPopVectZall, latTrialPopVectZall, pstTrialPopVectZall,... +% perfLog & inSeqLog, 'ISC Trials'); +% PlotPeriodDiffNoiseCorrs(corrTrlsNoise, 'ISC Trials'); +% +% [inCorrTrlsPop, inCorrTrlsNoise] = ExaminePopAndNoiseCorrs(preTrialPopVectZall, rlyTrialPopVectZall, latTrialPopVectZall, pstTrialPopVectZall,... +% ~perfLog & inSeqLog, 'ISIc Trials'); +% PlotPeriodDiffNoiseCorrs(inCorrTrlsNoise, 'ISIc Trials'); +% +% [aTrialsCorrPopISC, aTrialsCorrNoiseISC] = ExaminePopAndNoiseCorrs(preTrialPopVectZall, rlyTrialPopVectZall, latTrialPopVectZall, pstTrialPopVectZall,... +% perfLog & odorAlog & inSeqLog, 'Odor A ISC'); +% PlotPeriodDiffNoiseCorrs(aTrialsCorrNoiseISC, 'Odor A ISC'); +% +% [bTrialsCorrPopISC, bTrialsCorrNoiseISC] = ExaminePopAndNoiseCorrs(preTrialPopVectZall, rlyTrialPopVectZall, latTrialPopVectZall, pstTrialPopVectZall,... +% perfLog & odorBlog & inSeqLog, 'Odor B ISC'); +% PlotPeriodDiffNoiseCorrs(bTrialsCorrNoiseISC, 'Odor B ISC'); +% +% [cTrialsCorrPopISC, cTrialsCorrNoiseISC] = ExaminePopAndNoiseCorrs(preTrialPopVectZall, rlyTrialPopVectZall, latTrialPopVectZall, pstTrialPopVectZall,... +% perfLog & odorClog & inSeqLog, 'Odor C ISC'); +% PlotPeriodDiffNoiseCorrs(cTrialsCorrNoiseISC, 'Odor C ISC'); +% +% [dTrialsCorrPopISC, dTrialsCorrNoiseISC] = ExaminePopAndNoiseCorrs(preTrialPopVectZall, rlyTrialPopVectZall, latTrialPopVectZall, pstTrialPopVectZall,... +% perfLog & odorDlog & inSeqLog, 'Odor D ISC'); +% PlotPeriodDiffNoiseCorrs(dTrialsCorrNoiseISC, 'Odor D ISC'); +% +% [aTrialsCorrPopOSC, aTrialsCorrNoiseOSC] = ExaminePopAndNoiseCorrs(preTrialPopVectZall, rlyTrialPopVectZall, latTrialPopVectZall, pstTrialPopVectZall,... +% perfLog & odorAlog & outSeqLog, 'Odor A OSC'); +% PlotPeriodDiffNoiseCorrs(aTrialsCorrNoiseOSC, 'Odor A OSC'); +% +% [bTrialsCorrPopOSC, bTrialsCorrNoiseOSC] = ExaminePopAndNoiseCorrs(preTrialPopVectZall, rlyTrialPopVectZall, latTrialPopVectZall, pstTrialPopVectZall,... +% perfLog & odorBlog & outSeqLog, 'Odor B OSC'); +% PlotPeriodDiffNoiseCorrs(bTrialsCorrNoiseOSC, 'Odor B OSC'); +% +% [cTrialsCorrPopOSC, cTrialsCorrNoiseOSC] = ExaminePopAndNoiseCorrs(preTrialPopVectZall, rlyTrialPopVectZall, latTrialPopVectZall, pstTrialPopVectZall,... +% perfLog & odorClog & outSeqLog, 'Odor C OSC'); +% PlotPeriodDiffNoiseCorrs(cTrialsCorrNoiseOSC, 'Odor C OSC'); +% +% [dTrialsCorrPopOSC, dTrialsCorrNoiseOSC] = ExaminePopAndNoiseCorrs(preTrialPopVectZall, rlyTrialPopVectZall, latTrialPopVectZall, pstTrialPopVectZall,... +% perfLog & odorDlog & outSeqLog, 'Odor D OSC'); +% PlotPeriodDiffNoiseCorrs(dTrialsCorrNoiseOSC, 'Odor D OSC'); + +%% Only Trials from Complete InSeq Seqs +% [aTrialsCorrPopCompISC, aTrialsCorrNoiseCompISC] = ExaminePopAndNoiseCorrs(preTrialPopVectZall, rlyTrialPopVectZall, latTrialPopVectZall, pstTrialPopVectZall,... +% perfLog & odorAlog & fullInSeqLog, 'Odor A Complete Seqs'); +% PlotPeriodDiffNoiseCorrs(aTrialsCorrNoiseCompISC, 'Odor A Complete Seqs'); +% +% [bTrialsCorrPopCompISC, bTrialsCorrNoiseCompISC] = ExaminePopAndNoiseCorrs(preTrialPopVectZall, rlyTrialPopVectZall, latTrialPopVectZall, pstTrialPopVectZall,... +% perfLog & odorBlog & fullInSeqLog, 'Odor B Complete Seqs'); +% PlotPeriodDiffNoiseCorrs(bTrialsCorrNoiseCompISC, 'Odor B Complete Seqs'); +% +% [cTrialsCorrPopCompISC, cTrialsCorrNoiseCompISC] = ExaminePopAndNoiseCorrs(preTrialPopVectZall, rlyTrialPopVectZall, latTrialPopVectZall, pstTrialPopVectZall,... +% perfLog & odorClog & fullInSeqLog, 'Odor C Complete Seqs'); +% PlotPeriodDiffNoiseCorrs(cTrialsCorrNoiseCompISC, 'Odor C Complete Seqs'); +% +% [dTrialsCorrPopCompISC, dTrialsCorrNoiseCompISC] = ExaminePopAndNoiseCorrs(preTrialPopVectZall, rlyTrialPopVectZall, latTrialPopVectZall, pstTrialPopVectZall,... +% perfLog & odorDlog & fullInSeqLog, 'Odor D Complete Seqs'); +% PlotPeriodDiffNoiseCorrs(dTrialsCorrNoiseCompISC, 'Odor D Complete Seqs'); +% +% PlotOdorDiffNoiseCorrs(aTrialsCorrNoiseCompISC, bTrialsCorrNoiseCompISC, cTrialsCorrNoiseCompISC, dTrialsCorrNoiseCompISC, 'Complete IS') + +%% Run Permutations to derive 'significant' noise correlation values +% [~, aTrialsCorrNoiseCompISCz, ~, ~] = ExaminePopAndNoiseCorrsPerm(preTrialPopVectZall, rlyTrialPopVectZall, latTrialPopVectZall, pstTrialPopVectZall,... +% perfLog & odorAlog & fullInSeqLog, 1000, 'Odor A Complete Seqs'); +% +% [~, bTrialsCorrNoiseCompISCz, ~, ~] = ExaminePopAndNoiseCorrsPerm(preTrialPopVectZall, rlyTrialPopVectZall, latTrialPopVectZall, pstTrialPopVectZall,... +% perfLog & odorBlog & fullInSeqLog, 1000, 'Odor B Complete Seqs'); +% +% [~, cTrialsCorrNoiseCompISCz, ~, ~] = ExaminePopAndNoiseCorrsPerm(preTrialPopVectZall, rlyTrialPopVectZall, latTrialPopVectZall, pstTrialPopVectZall,... +% perfLog & odorClog & fullInSeqLog, 1000, 'Odor C Complete Seqs'); +% +% [~, dTrialsCorrNoiseCompISCz, ~, ~] = ExaminePopAndNoiseCorrsPerm(preTrialPopVectZall, rlyTrialPopVectZall, latTrialPopVectZall, pstTrialPopVectZall,... +% perfLog & odorDlog & fullInSeqLog, 1000, 'Odor D Complete Seqs'); + +[~, aTrialsCorrNoiseCompISCz, ~, ~] = ExaminePopAndNoiseCorrsPerm(preTrialPopVectZall, rlyTrialPopVectZall, latTrialPopVectZall, pstTrialPopVectZall,... + perfLog & odorAlog & inSeqLog, 1000, 'Odor A ISC'); + +[~, bTrialsCorrNoiseCompISCz, ~, ~] = ExaminePopAndNoiseCorrsPerm(preTrialPopVectZall, rlyTrialPopVectZall, latTrialPopVectZall, pstTrialPopVectZall,... + perfLog & odorBlog & inSeqLog, 1000, 'Odor B ISC'); + +[~, cTrialsCorrNoiseCompISCz, ~, ~] = ExaminePopAndNoiseCorrsPerm(preTrialPopVectZall, rlyTrialPopVectZall, latTrialPopVectZall, pstTrialPopVectZall,... + perfLog & odorClog & inSeqLog, 1000, 'Odor C ISC'); + +[~, dTrialsCorrNoiseCompISCz, ~, ~] = ExaminePopAndNoiseCorrsPerm(preTrialPopVectZall, rlyTrialPopVectZall, latTrialPopVectZall, pstTrialPopVectZall,... + perfLog & odorDlog & inSeqLog, 1000, 'Odor D ISC'); + +% PlotOdorDiffNoiseCorrs(aTrialsCorrNoiseCompISCz, bTrialsCorrNoiseCompISCz, cTrialsCorrNoiseCompISCz, dTrialsCorrNoiseCompISCz, 'Complete IS') + + +%% +% PlotOdorDiffNoiseCorrs(aTrialsCorrNoiseISC, bTrialsCorrNoiseISC, cTrialsCorrNoiseISC, dTrialsCorrNoiseISC, 'ISC') +% PlotOdorDiffNoiseCorrs(aTrialsCorrNoiseOSC, bTrialsCorrNoiseOSC, cTrialsCorrNoiseOSC, dTrialsCorrNoiseOSC, 'OSC') + + +end + +function [popCorrsZ, noiseCorrsZ, popCorrsChance, noiseCorrsChance] = ExaminePopAndNoiseCorrsPerm(preTrial, rlyTrial, latTrial, pstTrial, sortVect, numPerms, sortVectID) +%% Chance up that random stream +randperm(floor(sum(clock))); +threshVal = 0; +%% +preSorted = preTrial(sortVect,:); +rlySorted = rlyTrial(sortVect,:); +latSorted = latTrial(sortVect,:); +pstSorted = pstTrial(sortVect,:); + +noiseCorrs{1} = corr(preSorted); +popCorrs{1} = corr(preSorted'); + +noiseCorrs{2} = corr(rlySorted); +popCorrs{2} = corr(rlySorted'); + +noiseCorrs{3} = corr(latSorted); +popCorrs{3} = corr(latSorted'); + +noiseCorrs{4} = corr(pstSorted); +popCorrs{4} = corr(pstSorted'); + +noiseCorrsZ = repmat({nan(size(noiseCorrs{1}))}, 1, 4); +popCorrsZ = repmat({nan(size(popCorrs{1}))}, 1, 4); +noiseCorrsChance = repmat({nan(size(noiseCorrs{1},1), size(noiseCorrs{1},2), numPerms)},1,4); +popCorrsChance = repmat({nan(size(popCorrs{1},1), size(popCorrs{1},2), numPerms)},1,4); +for perm = 1:numPerms + tempPre = preSorted; + tempRly = rlySorted; + tempLat = latSorted; + tempPst = pstSorted; + + preIDmtx = reshape(1:length(tempPre(:)), size(tempPre,1), size(tempPre,2)); + for c = 1:size(preIDmtx,2) + preIDmtx(:,c) = preIDmtx(randperm(size(preIDmtx,1)),c); + end + noiseCorrsChance{1}(:,:,perm) = corr(tempPre(preIDmtx)); + popCorrsChance{1}(:,:,perm) = corr(tempPre(preIDmtx)'); + + rlyIDmtx = reshape(1:length(tempRly(:)), size(tempRly,1), size(tempRly,2)); + for c = 1:size(rlyIDmtx,2) + rlyIDmtx(:,c) = rlyIDmtx(randperm(size(rlyIDmtx,1)),c); + end + noiseCorrsChance{2}(:,:,perm) = corr(tempRly(rlyIDmtx)); + popCorrsChance{2}(:,:,perm) = corr(tempRly(rlyIDmtx)'); + + latIDmtx = reshape(1:length(tempLat(:)), size(tempLat,1), size(tempLat,2)); + for c = 1:size(latIDmtx,2) + latIDmtx(:,c) = latIDmtx(randperm(size(latIDmtx,1)),c); + end + noiseCorrsChance{3}(:,:,perm) = corr(tempLat(latIDmtx)); + popCorrsChance{3}(:,:,perm) = corr(tempLat(latIDmtx)'); + + pstIDmtx = reshape(1:length(tempPst(:)), size(tempPst,1), size(tempPst,2)); + for c = 1:size(pstIDmtx,2) + pstIDmtx(:,c) = pstIDmtx(randperm(size(pstIDmtx,1)),c); + end + noiseCorrsChance{4}(:,:,perm) = corr(tempPst(pstIDmtx)); + popCorrsChance{4}(:,:,perm) = corr(tempPst(pstIDmtx)'); +end +for nIr = 1:size(noiseCorrsZ{1},1) + for nIc = 1:size(noiseCorrsZ{1},2) + tempZpre = zscore([reshape(noiseCorrsChance{1}(nIr,nIc,:), 1, numPerms), noiseCorrs{1}(nIr,nIc)]); + noiseCorrsZ{1}(nIr,nIc) = tempZpre(end); + + tempZrly = zscore([reshape(noiseCorrsChance{2}(nIr,nIc,:), 1, numPerms), noiseCorrs{2}(nIr,nIc)]); + noiseCorrsZ{2}(nIr,nIc) = tempZrly(end); + + tempZlat = zscore([reshape(noiseCorrsChance{3}(nIr,nIc,:), 1, numPerms), noiseCorrs{3}(nIr,nIc)]); + noiseCorrsZ{3}(nIr,nIc) = tempZlat(end); + + tempZpst = zscore([reshape(noiseCorrsChance{4}(nIr,nIc,:), 1, numPerms), noiseCorrs{4}(nIr,nIc)]); + noiseCorrsZ{4}(nIr,nIc) = tempZpst(end); + end +end + +for pIr = 1:size(popCorrsZ{1},1) + for pIc = 1:size(popCorrsZ{1},2) + tempZpre = zscore([reshape(popCorrsChance{1}(pIr,pIc,:), 1, numPerms), popCorrs{1}(pIr,pIc)]); + popCorrsZ{1}(pIr,pIc) = tempZpre(end); + + tempZrly = zscore([reshape(popCorrsChance{2}(pIr,pIc,:), 1, numPerms), popCorrs{2}(pIr,pIc)]); + popCorrsZ{2}(pIr,pIc) = tempZrly(end); + + tempZlat = zscore([reshape(popCorrsChance{3}(pIr,pIc,:), 1, numPerms), popCorrs{3}(pIr,pIc)]); + popCorrsZ{3}(pIr,pIc) = tempZlat(end); + + tempZpst = zscore([reshape(popCorrsChance{4}(pIr,pIc,:), 1, numPerms), popCorrs{4}(pIr,pIc)]); + popCorrsZ{4}(pIr,pIc) = tempZpst(end); + end +end + +maxAbsZNoise = max(max(abs(cell2mat(noiseCorrsZ)))); +maxAbsZPop = max(max(abs(cell2mat(popCorrsZ)))); + +figure; +subplot(4,4,1) +PlotCombinedMatrix(noiseCorrs{1}, noiseCorrsZ{1}, maxAbsZNoise); +title('Pre Trial') +xlabel('Z-Normed') +ylabel('Observed') + +subplot(4,4,2) +PlotCombinedMatrix(noiseCorrsZ{2}, noiseCorrsZ{1}, maxAbsZNoise); +title('Pre vs Early') +xlabel('Pre'); +ylabel('Early'); + +subplot(4,4,3) +PlotCombinedMatrix(noiseCorrsZ{3}, noiseCorrsZ{1}, maxAbsZNoise); +title('Pre vs Late') +xlabel('Pre'); +ylabel('Late'); + +subplot(4,4,4) +PlotCombinedMatrix(noiseCorrsZ{4}, noiseCorrsZ{1}, maxAbsZNoise); +title('Pre vs Post') +xlabel('Pre'); +ylabel('Post'); + +subplot(4,4,5) +PlotNoiseCorrChanceDist(noiseCorrsChance{1}, noiseCorrsChance{2}, noiseCorrsZ{1}, noiseCorrsZ{2}); +% upperPre = ExtractUpperTriangle(noiseCorrsZ{1}); +% upperEarly = ExtractUpperTriangle(noiseCorrsZ{2}); +% log = (~isnan(upperPre) & ~isnan(upperEarly)) & (abs(upperPre)>=threshVal | abs(upperEarly)>=threshVal); +% corrScatPlot(upperPre(log),upperEarly(log), 'Pre','Early',[]); +title('Pre vs Early'); + +subplot(4,4,6) +PlotCombinedMatrix(noiseCorrs{2}, noiseCorrsZ{2}, maxAbsZNoise); +title('Early Trial') +xlabel('Z-Normed') +ylabel('Observed') + +subplot(4,4,7) +PlotCombinedMatrix(noiseCorrsZ{3}, noiseCorrsZ{2}, maxAbsZNoise); +title('Early vs Late') +xlabel('Early') +ylabel('Late') + +subplot(4,4,8) +PlotCombinedMatrix(noiseCorrsZ{4}, noiseCorrsZ{2}, maxAbsZNoise); +title('Early vs Post') +xlabel('Early') +ylabel('Post') + +subplot(4,4,9) +PlotNoiseCorrChanceDist(noiseCorrsChance{1}, noiseCorrsChance{3}, noiseCorrsZ{1}, noiseCorrsZ{3}); +% upperPre = ExtractUpperTriangle(noiseCorrsZ{1}); +% upperLate = ExtractUpperTriangle(noiseCorrsZ{3}); +% log = (~isnan(upperPre) & ~isnan(upperLate)) & (abs(upperPre)>=threshVal | abs(upperPre)>=threshVal); +% corrScatPlot(upperPre(log),upperLate(log), 'Pre','Late',[]); +title('Pre vs Late') + +subplot(4,4,10) +PlotNoiseCorrChanceDist(noiseCorrsChance{2}, noiseCorrsChance{3}, noiseCorrsZ{2}, noiseCorrsZ{3}); +% upperEarly = ExtractUpperTriangle(noiseCorrsZ{2}); +% upperLate = ExtractUpperTriangle(noiseCorrsZ{3}); +% log = (~isnan(upperEarly) & ~isnan(upperLate)) & (abs(upperEarly)>=threshVal | abs(upperLate)>=threshVal); +% corrScatPlot(upperEarly(log),upperLate(log), 'Early','Late',[]); +title('Early vs Late') + +subplot(4,4,11) +PlotCombinedMatrix(noiseCorrs{3}, noiseCorrsZ{3}, maxAbsZNoise); +title('Late Trial') +xlabel('Z-Normed') +ylabel('Observed') + +subplot(4,4,12) +PlotCombinedMatrix(noiseCorrsZ{4}, noiseCorrsZ{3}, maxAbsZNoise); +title('Late vs Post') +xlabel('Late') +ylabel('Post') + +subplot(4,4,13) +PlotNoiseCorrChanceDist(noiseCorrsChance{1}, noiseCorrsChance{4}, noiseCorrsZ{1}, noiseCorrsZ{4}); +% upperPre = ExtractUpperTriangle(noiseCorrsZ{1}); +% upperPost = ExtractUpperTriangle(noiseCorrsZ{4}); +% log = (~isnan(upperPre) & ~isnan(upperPost)) & (abs(upperPre)>=threshVal | abs(upperPost)>=threshVal); +% corrScatPlot(upperPre(log),upperPost(log), 'Pre','Post',[]); +title('Pre vs Post') + +subplot(4,4,14) +PlotNoiseCorrChanceDist(noiseCorrsChance{2}, noiseCorrsChance{4}, noiseCorrsZ{2}, noiseCorrsZ{4}); +% upperEarly = ExtractUpperTriangle(noiseCorrsZ{2}); +% upperPost = ExtractUpperTriangle(noiseCorrsZ{4}); +% log = (~isnan(upperEarly) & ~isnan(upperPost)) & (abs(upperEarly)>=threshVal | abs(upperPost)>=threshVal); +% corrScatPlot(upperEarly(log),upperPost(log), 'Early','Post',[]); +title('Early vs Post') + +subplot(4,4,15) +PlotNoiseCorrChanceDist(noiseCorrsChance{3}, noiseCorrsChance{4}, noiseCorrsZ{3}, noiseCorrsZ{4}); +% upperLate = ExtractUpperTriangle(noiseCorrsZ{3}); +% upperPost = ExtractUpperTriangle(noiseCorrsZ{4}); +% log = (~isnan(upperLate) & ~isnan(upperPost)) & (abs(upperLate)>=threshVal | abs(upperPost)>=threshVal); +% corrScatPlot(upperLate(log),upperPost(log), 'Late','Post',[]); +title('Late vs Post') + +subplot(4,4,16) +PlotCombinedMatrix(noiseCorrs{4}, noiseCorrsZ{4}, maxAbsZNoise); +title('Post Trial') +xlabel('Z-Normed') +ylabel('Observed') + +annotation('textbox', 'position', [0.025 0.935 0.7 0.05], 'String', ['\bf\fontsize{14}' sortVectID],... + 'linestyle', 'none', 'horizontalalignment', 'left'); +curDir = cd; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); + +colormap jet +axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +axis(axesHandles,'square') +orient(gcf, 'tall'); +orient(gcf, 'landscape'); + +% +% figure; +% subplot(4,4,3) +% PlotCombinedMatrix(popCorrs{1}, popCorrsZ{1}, maxAbsZPop); +% subplot(4,4,7) +% PlotCombinedMatrix(popCorrs{2}, popCorrsZ{2}, maxAbsZPop); +% subplot(4,4,11) +% PlotCombinedMatrix(popCorrs{3}, popCorrsZ{3}, maxAbsZPop); +% subplot(4,4,15) +% PlotCombinedMatrix(popCorrs{4}, popCorrsZ{4}, maxAbsZPop); +% +% colormap jet +% +% axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +% axis(axesHandles,'square') +% orient(gcf, 'tall'); +% orient(gcf, 'landscape'); + +end + +%% +function PlotNoiseCorrChanceDist(chanceX, chanceY, realX, realY) +[upperRealX, ~] = ExtractUpperTriangle(realX); +[upperRealY, ~] = ExtractUpperTriangle(realY); +log = ~isnan(upperRealX) & ~isnan(upperRealY); +realCorr = corr(upperRealX(log), upperRealY(log)); +chanceCorr = nan(1,size(chanceX,3)); +for perm = 1:size(chanceX,3) + [upperX, ~] = ExtractUpperTriangle(chanceX(:,:,perm)); + [upperY, ~] = ExtractUpperTriangle(chanceY(:,:,perm)); + log = ~isnan(upperX) & ~isnan(upperY); + chanceCorr(perm) = corr(upperX(log), upperY(log)); +end +histogram(chanceCorr, -0.2:0.01:.2) +hold on; +line([realCorr, realCorr], get(gca, 'ylim'), 'color', 'r', 'linewidth', 2); +box off; +grid on; +set(gca, 'color', 'none'); +temp = zscore([chanceCorr, realCorr]); +xlabel(sprintf('Z = %.02f', temp(end))) +end +%% +function PlotCombinedMatrix(realVals, zVals, cMax) +[~, realMtxNanD] = ExtractUpperTriangle(realVals); +[~, zMtxNanD] = ExtractUpperTriangle(zVals); +imagesc(nansum(reshape([fliplr(realMtxNanD), fliplr(zMtxNanD')], size(realMtxNanD,1), size(realMtxNanD,2),2),3), [-cMax cMax]); +end + + +%% +function PlotPeriodDiffNoiseCorrs(mtx, id) +figure; +subplot(4,4,1) +imagesc(mtx{1}, [-1 1]); +title('Pre Trial'); + +subplot(4,4,2) +preUpper = ExtractUpperTriangle(mtx{1}); +rlyUpper = ExtractUpperTriangle(mtx{2}); +corrScatPlot(preUpper(~isnan(preUpper) & ~isnan(rlyUpper)),rlyUpper(~isnan(preUpper) & ~isnan(rlyUpper)), 'Pre','Early',[]); + +subplot(4,4,3) +preUpper = ExtractUpperTriangle(mtx{1}); +latUpper = ExtractUpperTriangle(mtx{3}); +corrScatPlot(preUpper(~isnan(preUpper) & ~isnan(latUpper)),latUpper(~isnan(preUpper) & ~isnan(latUpper)), 'Pre','Late',[]); + +subplot(4,4,4) +preUpper = ExtractUpperTriangle(mtx{1}); +pstUpper = ExtractUpperTriangle(mtx{4}); +corrScatPlot(preUpper(~isnan(preUpper) & ~isnan(pstUpper)),pstUpper(~isnan(preUpper) & ~isnan(pstUpper)), 'Pre','Post',[]); + +subplot(4,4,5) +imagesc(mtx{1} - mtx{2}, [-1 1]); +title('Pre - Early'); + +subplot(4,4,6) +imagesc(mtx{2}, [-1 1]); +title('Early Trial') + +subplot(4,4,7) +rlyUpper = ExtractUpperTriangle(mtx{2}); +latUpper = ExtractUpperTriangle(mtx{3}); +corrScatPlot(rlyUpper(~isnan(rlyUpper) & ~isnan(latUpper)),latUpper(~isnan(rlyUpper) & ~isnan(latUpper)), 'Early','Late',[]); + +subplot(4,4,8) +rlyUpper = ExtractUpperTriangle(mtx{2}); +pstUpper = ExtractUpperTriangle(mtx{4}); +corrScatPlot(rlyUpper(~isnan(rlyUpper) & ~isnan(pstUpper)),pstUpper(~isnan(rlyUpper) & ~isnan(pstUpper)), 'Early','Post',[]); + +subplot(4,4,9) +imagesc(mtx{1}-mtx{3}, [-1 1]); +title('Pre - Late'); + +subplot(4,4,10) +imagesc(mtx{2}-mtx{3}, [-1 1]); +title('Early - Late'); + +subplot(4,4,11) +imagesc(mtx{3}, [-1 1]); +title('Late Trial'); + +subplot(4,4,12) +latUpper = ExtractUpperTriangle(mtx{3}); +pstUpper = ExtractUpperTriangle(mtx{4}); +corrScatPlot(latUpper(~isnan(latUpper) & ~isnan(pstUpper)),pstUpper(~isnan(latUpper) & ~isnan(pstUpper)), 'Early','Post',[]); + +subplot(4,4,13) +imagesc(mtx{1}-mtx{4}, [-1 1]); +title('Pre - Post'); + +subplot(4,4,14) +imagesc(mtx{2}-mtx{4}, [-1 1]); +title('Early - Post'); + +subplot(4,4,15) +imagesc(mtx{3}-mtx{4}, [-1 1]); +title('Late - Post'); + +subplot(4,4,16) +imagesc(mtx{4}, [-1 1]); +title('Post Trial'); + +annotation('textbox', 'position', [0.025 0.935 0.7 0.05], 'String', ['\bf\fontsize{14}' id],... + 'linestyle', 'none', 'horizontalalignment', 'left'); + +curDir = cd; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); + +colormap jet + +axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +axis(axesHandles,'square') +orient(gcf, 'tall'); +orient(gcf, 'landscape'); +end + +%% +function [upperMtxVect, lowerMtxNand] = ExtractUpperTriangle(mtx) +upperLog = triu(true(size(mtx,1)),1); +upperMtxVect = mtx(upperLog); +lowerMtxNand = mtx; +lowerMtxNand(~upperLog) = nan; +end + +%% +function PlotOdorDiffNoiseCorrs(aTrialsCorrNoise, bTrialsCorrNoise, cTrialsCorrNoise, dTrialsCorrNoise, id) +trlPrdIDs = [{'Pre-Trial'}, {'Early-Trial'}, {'Late-Trial'}, {'Post-Trial'}]; +for trlPrd = 1:4 + currA = aTrialsCorrNoise{trlPrd}; + currB = bTrialsCorrNoise{trlPrd}; + currC = cTrialsCorrNoise{trlPrd}; + currD = dTrialsCorrNoise{trlPrd}; + figure; + abDiff = currA - currB; + subplot(4,4,2) + imagesc(abDiff, [-1 1]); + title('A-B') + subplot(4,4,5) + upperA = ExtractUpperTriangle(currA); + upperB = ExtractUpperTriangle(currB); + corrScatPlot(upperA(~isnan(upperA) & ~isnan(upperB)),upperB(~isnan(upperA) & ~isnan(upperB)), 'A','B',[]); + set(gca, 'xlim', [-1 1], 'ylim', [-1 1]); +% histogram(abDiff(logical(triu(ones(size(abDiff)),1))), -1:0.01:1, 'edgecolor', 'none', 'facecolor', 'k'); + set(gca, 'color', 'none', 'box', 'off'); + grid on + hold on +% line([0 0], get(gca,'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); + title('A vs B') + + acDiff = currA - currC; + subplot(4,4,3) + imagesc(acDiff, [-1 1]); + title('A-C') + subplot(4,4,9) + upperA = ExtractUpperTriangle(currA); + upperC = ExtractUpperTriangle(currC); + corrScatPlot(upperA(~isnan(upperA) & ~isnan(upperC)),upperC(~isnan(upperA) & ~isnan(upperC)), 'A','C',[]); + set(gca, 'xlim', [-1 1], 'ylim', [-1 1]); +% histogram(acDiff(logical(triu(ones(size(acDiff)),1))), -1:0.01:1, 'edgecolor', 'none', 'facecolor', 'k'); + set(gca, 'color', 'none', 'box', 'off'); + grid on + hold on +% line([0 0], get(gca,'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); + title('A vs C') + + adDiff = currA - currD; + subplot(4,4,4) + imagesc(adDiff, [-1 1]); + title('A-D') + subplot(4,4,13) + upperA = ExtractUpperTriangle(currA); + upperD = ExtractUpperTriangle(currD); + corrScatPlot(upperA(~isnan(upperA) & ~isnan(upperD)),upperD(~isnan(upperA) & ~isnan(upperD)), 'A','D',[]); + set(gca, 'xlim', [-1 1], 'ylim', [-1 1]); +% histogram(adDiff(logical(triu(ones(size(adDiff)),1))), -1:0.01:1, 'edgecolor', 'none', 'facecolor', 'k'); + set(gca, 'color', 'none', 'box', 'off'); + grid on + hold on +% line([0 0], get(gca,'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); + title('A vs D') + + bcDiff = currB - currC; + subplot(4,4,7) + imagesc(bcDiff, [-1 1]); + title('B-C') + subplot(4,4,10) + upperB = ExtractUpperTriangle(currB); + upperC = ExtractUpperTriangle(currC); + corrScatPlot(upperB(~isnan(upperB) & ~isnan(upperC)),upperC(~isnan(upperB) & ~isnan(upperC)), 'B','C',[]); + set(gca, 'xlim', [-1 1], 'ylim', [-1 1]); +% histogram(bcDiff(logical(triu(ones(size(bcDiff)),1))), -1:0.01:1, 'edgecolor', 'none', 'facecolor', 'k'); + set(gca, 'color', 'none', 'box', 'off'); + grid on + hold on +% line([0 0], get(gca,'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); + title('B vs C') + + bdDiff = currB - currD; + subplot(4,4,8) + imagesc(bdDiff, [-1 1]); + title('B-D') + subplot(4,4,14) + upperB = ExtractUpperTriangle(currB); + upperD = ExtractUpperTriangle(currD); + corrScatPlot(upperB(~isnan(upperB) & ~isnan(upperD)),upperD(~isnan(upperB) & ~isnan(upperD)), 'B','D',[]); + set(gca, 'xlim', [-1 1], 'ylim', [-1 1]); +% histogram(bdDiff(logical(triu(ones(size(bdDiff)),1))), -1:0.01:1, 'edgecolor', 'none', 'facecolor', 'k'); + set(gca, 'color', 'none', 'box', 'off'); + grid on + hold on +% line([0 0], get(gca,'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); + title('B vs D') + + cdDiff = currC - currD; + subplot(4,4,12) + imagesc(cdDiff, [-1 1]); + title('C-D') + subplot(4,4,15) + upperC = ExtractUpperTriangle(currC); + upperD = ExtractUpperTriangle(currD); + corrScatPlot(upperC(~isnan(upperC) & ~isnan(upperD)),upperD(~isnan(upperC) & ~isnan(upperD)), 'C','D',[]); + set(gca, 'xlim', [-1 1], 'ylim', [-1 1]); +% histogram(cdDiff(logical(triu(ones(size(cdDiff)),1))), -1:0.01:1, 'edgecolor', 'none', 'facecolor', 'k'); + set(gca, 'color', 'none', 'box', 'off'); + grid on + hold on +% line([0 0], get(gca,'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); + title('C vs D') + + annotation('textbox', 'position', [0.025 0.935 0.7 0.05], 'String', ['\bf\fontsize{14}' sprintf('%s %s', trlPrdIDs{trlPrd}, id)],... + 'linestyle', 'none', 'horizontalalignment', 'left'); + + curDir = cd; + annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); + + colormap jet + + axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); + axis(axesHandles,'square') + orient(gcf, 'tall'); + orient(gcf, 'landscape'); +end +end + +%% +function [popCorrs, noiseCorrs] = ExaminePopAndNoiseCorrs(preTrial, rlyTrial, latTrial, pstTrial, sortVect, sortVectID) +preSorted = preTrial(sortVect,:); +rlySorted = rlyTrial(sortVect,:); +latSorted = latTrial(sortVect,:); +pstSorted = pstTrial(sortVect,:); + +figure; +subplot(4,4,1) +noiseCorrs{1} = corr(preSorted); +imagesc(noiseCorrs{1}, [-1 1]); +title('Pre-Trial Period: Unit Noise Correlations'); +subplot(4,4,2) +histogram(noiseCorrs{1}(logical(triu(ones(size(noiseCorrs{1})),1))), -1:0.01:1, 'edgecolor', 'none', 'facecolor', 'k'); +set(gca, 'color', 'none', 'box', 'off'); +grid on +hold on +% line([0 0], get(gca,'ylim'), 'linestyle', ':', 'color', 'r', 'linewidth', 1); +subplot(4,4,3) +popCorrs{1} = corr(preSorted'); +imagesc(popCorrs{1}, [-1 1]); +title('Trial Population Correlations'); +subplot(4,4,4) +histogram(popCorrs{1}(logical(triu(ones(size(popCorrs{1})),1))), -1:0.01:1, 'edgecolor', 'none', 'facecolor', 'k'); +set(gca, 'color', 'none', 'box', 'off'); +grid on +hold on +% line([0 0], get(gca,'ylim'), 'linestyle', ':', 'color', 'r', 'linewidth', 1); + +subplot(4,4,5) +noiseCorrs{2} = corr(rlySorted); +imagesc(noiseCorrs{2}, [-1 1]); +title('Early-Trial Period: Unit Noise Correlations'); +subplot(4,4,6) +histogram(noiseCorrs{2}(logical(triu(ones(size(noiseCorrs{2})),1))), -1:0.01:1, 'edgecolor', 'none', 'facecolor', 'k'); +set(gca, 'color', 'none', 'box', 'off'); +grid on +hold on +% line([0 0], get(gca,'ylim'), 'linestyle', ':', 'color', 'r', 'linewidth', 1); +subplot(4,4,7) +popCorrs{2} = corr(rlySorted'); +imagesc(popCorrs{2}, [-1 1]); +title('Trial Population Correlations'); +subplot(4,4,8) +histogram(popCorrs{2}(logical(triu(ones(size(popCorrs{2})),1))), -1:0.01:1, 'edgecolor', 'none', 'facecolor', 'k'); +set(gca, 'color', 'none', 'box', 'off'); +grid on +hold on +% line([0 0], get(gca,'ylim'), 'linestyle', ':', 'color', 'r', 'linewidth', 1); + +subplot(4,4,9) +noiseCorrs{3} = corr(latSorted); +imagesc(noiseCorrs{3}, [-1 1]); +title('Late-Trial Period: Unit Noise Correlations'); +subplot(4,4,10) +histogram(noiseCorrs{3}(logical(triu(ones(size(noiseCorrs{3})),1))), -1:0.01:1, 'edgecolor', 'none', 'facecolor', 'k'); +set(gca, 'color', 'none', 'box', 'off'); +grid on +hold on +% line([0 0], get(gca,'ylim'), 'linestyle', ':', 'color', 'r', 'linewidth', 1); +subplot(4,4,11) +popCorrs{3} = corr(latSorted'); +imagesc(popCorrs{3}, [-1 1]); +title('Trial Population Correlations'); +subplot(4,4,12) +histogram(popCorrs{3}(logical(triu(ones(size(popCorrs{3})),1))), -1:0.01:1, 'edgecolor', 'none', 'facecolor', 'k'); +set(gca, 'color', 'none', 'box', 'off'); +grid on +hold on +% line([0 0], get(gca,'ylim'), 'linestyle', ':', 'color', 'r', 'linewidth', 1); + +subplot(4,4,13) +noiseCorrs{4} = corr(pstSorted); +imagesc(noiseCorrs{4}, [-1 1]); +title('Post-Trial Period: Unit Noise Correlations'); +subplot(4,4,14) +histogram(noiseCorrs{4}(logical(triu(ones(size(noiseCorrs{4})),1))), -1:0.01:1, 'edgecolor', 'none', 'facecolor', 'k'); +set(gca, 'color', 'none', 'box', 'off'); +grid on +hold on +% line([0 0], get(gca,'ylim'), 'linestyle', ':', 'color', 'r', 'linewidth', 1); +subplot(4,4,15) +popCorrs{4} = corr(pstSorted'); +imagesc(popCorrs{4}, [-1 1]); +title('Trial Population Correlations'); +subplot(4,4,16) +histogram(popCorrs{4}(logical(triu(ones(size(popCorrs{4})),1))), -1:0.01:1, 'edgecolor', 'none', 'facecolor', 'k'); +set(gca, 'color', 'none', 'box', 'off'); +grid on +hold on +% line([0 0], get(gca,'ylim'), 'linestyle', ':', 'color', 'r', 'linewidth', 1); + +annotation('textbox', 'position', [0.025 0.935 0.7 0.05], 'String', ['\bf\fontsize{14}' sortVectID],... + 'linestyle', 'none', 'horizontalalignment', 'left'); + +curDir = cd; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); + +colormap jet + +axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +axis(axesHandles,'square') +orient(gcf, 'tall'); +orient(gcf, 'landscape'); +end + + diff --git a/Analyses/Prototypes/PFC_DualList_PROTO.m b/Analyses/Prototypes/PFC_DualList_PROTO.m new file mode 100644 index 0000000..17eb7c4 --- /dev/null +++ b/Analyses/Prototypes/PFC_DualList_PROTO.m @@ -0,0 +1,44 @@ + +origDir = cd; +[fileDir] = uigetdir(origDir); +if fileDir==0 + disp('Analysis Cancelled') + return +else + cd(fileDir) +end +dirContents = dir(fileDir); +fileNames = {dirContents.name}; + +%% Define Standard Variables +slideWindowSize = 100; +spatialBinSize = 5; +numPerms = 50; + +% Create Gaussian +instFRgauss = gausswin(slideWindowSize); +instFRgauss = instFRgauss/(length(instFRgauss)*mode(diff(behavMatrix(:,1)))); + +%% Load Relevant Data +load(fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'BehaviorMatrix'))}); +% load(fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'EnsembleMatrix'))}); + +%% +pokeInTrialMatrix = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.2 0.5], 'PokeIn'); +pokeInTSs = behavMatrix(pokeInTrialMatrix(1).TrialLogVect,1)-behavMatrix(pokeInTrialMatrix(1).PokeInIndex,1); +pokeOutTrialMatrix = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.5 0.2], 'PokeOut'); +pokeOutTSs = behavMatrix(pokeOutTrialMatrix(1).TrialLogVect,1)-behavMatrix(pokeOutTrialMatrix(1).PokeOutIndex,1); + +% Create Trial Logical Vectors +corrTrlLog = [pokeInTrialMatrix.Performance]; +isLog = [pokeInTrialMatrix.TranspositionDistance]==0; +pos1log = [pokeInTrialMatrix.Position]==1; +% trialLog = (corrTrlLog & isLog & ~pos1log); +% seqType = 'All InSeq Correct Except Position 1'; +trialLog = (corrTrlLog & isLog); +seqType = 'All InSeq Correct Trials'; +% trialLog = (corrTrlLog); +% seqType = 'All Correct Trials'; +posIDsISC = [pokeInTrialMatrix(trialLog).Position]; +odorIDsISC = [pokeInTrialMatrix(trialLog).Odor]; +%% \ No newline at end of file diff --git a/Analyses/Prototypes/SequenceNeuralViewer_SM PROTO.m b/Analyses/Prototypes/SequenceNeuralViewer_SM PROTO.m new file mode 100644 index 0000000..ad7d479 --- /dev/null +++ b/Analyses/Prototypes/SequenceNeuralViewer_SM PROTO.m @@ -0,0 +1,163 @@ +function SequenceNeuralViewer_SM +%% + +%% Load data +[smFile,smPath] = uigetfile('*.mat'); +cd(smPath); +files = dir(smPath); +fileNames = {files.name}; +% Load the behavior matrix file for poke events plots +behMatFile = fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'BehaviorMatrix'))}; +nsmblMatFile = fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'EnsembleMatrix'))}; +load([smPath behMatFile]); +load([smPath nsmblMatFile]); +% Identify list of all statMatrix files +smFileList = fileNames(cellfun(@(a)~isempty(a), regexp(fileNames, '_SM\>')))'; + +%% Organize Ensemble Data +% Declare histogram bin size for peak FR latency decision +histBins = 0:0.1:1.2; + +% Extract session variables & ensemble FR +behavMatrixTrialStruct = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [min(histBins) max(histBins)], 'PokeIn'); +ensembleTrialData = ExtractTrialData_SM(behavMatrixTrialStruct, ensembleMatrix(:,2:end)); %#ok<*NODEF> +trialTSvect = behavMatrix(behavMatrixTrialStruct(1).TrialLogVect,1)-behavMatrix(behavMatrixTrialStruct(1).PokeInIndex,1); +inSeqLog = [behavMatrixTrialStruct.TranspositionDistance]==0; +perfLog = [behavMatrixTrialStruct.Performance]==1; +oAlog = [behavMatrixTrialStruct.Position]==1; +% iscEnsembleData = ensembleTrialData(inSeqLog & perfLog); +iscEnsembleData = ensembleTrialData(inSeqLog & perfLog & ~oAlog); + +% Bin trial wise FR +iscEnsembleDataBinned = nan(length(histBins)-1, length(ensembleUnitSummaries), length(iscEnsembleData)); +for tr = 1:length(iscEnsembleData) + for uni = 1:length(ensembleUnitSummaries) + iscEnsembleDataBinned(:,uni,tr) = histcounts(trialTSvect(logical(iscEnsembleData{tr}(:,uni))), histBins, 'Normalization', 'CountDensity'); + end +end +meanISCbinned = mean(iscEnsembleDataBinned,3); + +% Normalize the FR and identify the peak FR bin +iscBinnedNormed = nan(size(meanISCbinned)); +iscPeakLat = nan(size(meanISCbinned,2),3); +for uni = 1:size(meanISCbinned,2); + iscBinnedNormed(:,uni) = meanISCbinned(:,uni)./max(meanISCbinned(:,uni)); + curPeakLat = find(iscBinnedNormed(:,uni)==1,1,'first'); + if isempty(curPeakLat) + iscPeakLat(uni,:) = [size(iscBinnedNormed,1), uni, max(meanISCbinned(:,uni))]; + else + iscPeakLat(uni,:) = [curPeakLat, uni, max(meanISCbinned(:,uni))]; + end +end +% Select cells based on peak FR value +trialFRthreshLog = iscPeakLat(:,3)<1; +iscBinnedNormed(:,trialFRthreshLog) = []; +iscPeakLat(trialFRthreshLog,:) = []; + +% Separate out principal cells vs interneurons using spike width... +%%%%% NOTE: This value is chosen by eye based on the data currently... a +%%%%% more principled method for choosing the value should be used in the +%%%%% future +spkWdth = [ensembleUnitSummaries(~trialFRthreshLog).Spike_Width]*40; +spkRt = [ensembleUnitSummaries(~trialFRthreshLog).Mean_SpikeRate]; +% Width Based +% widthThresh = 0.45; +% pcLog = (spkWdth>widthThresh; +% Spike Rate Based +spkRtThresh = 5; +pcLog = spkRt'))); +lfpBands = cellfun(@(a,b)a(b:end), statMatrixColIDs(lfpColIDs), regexp(statMatrixColIDs(lfpColIDs), '[(A-Z)|(a-z)]*\>'), 'uniformoutput', 0); +lfpVals = statMatrix(:,lfpColIDs(strcmp(lfpBands, band2plot))); +lfpVals = lfpVals/(max(abs(lfpVals))); + +plot(statMatrix(:,1), lfpVals-1, 'color', [0.4 0.4 0.4]); + +% Plot Spikes +hold on; +for uni = 1:size(uniScatData,2) + scatter(uniScatData{1,uni}, uniScatData{2,uni}, '*k'); +end +set(gca, 'ydir', 'reverse', 'color', 'none'); + +% Patch Trial Periods +for trl = 1:length(behavMatrixTrialStruct) + switch behavMatrixTrialStruct(trl).Odor + case 1 + patchColor = [44/255 168/255 224/255]; + case 2 + patchColor = [154/255 133/255 122/255]; + case 3 + patchColor = [9/255 161/255 74/255]; + case 4 + patchColor = [128/255 66/255 151/255]; + case 5 + patchColor = 'k'; + end + patch(gca, 'XData', [behavMatrix(behavMatrixTrialStruct(trl).PokeInIndex,1), behavMatrix(behavMatrixTrialStruct(trl).PokeOutIndex,1), behavMatrix(behavMatrixTrialStruct(trl).PokeOutIndex,1),behavMatrix(behavMatrixTrialStruct(trl).PokeInIndex,1)],... + 'YData', [zeros(1,2), repmat(icScatData{2,end}(1)+1,[1,2])],... + 'FaceColor', patchColor, 'FaceAlpha', 0.15,... + 'EdgeColor', patchColor); +end + +%% +figure; +scatter([ensembleUnitSummaries.Mean_SpikeRate], [ensembleUnitSummaries.Spike_Width]*40); set(gca, 'xscale', 'log') +%% Create Figure +seqViewFig = figure; +figAxes = axes(seqViewFig, 'position', [0.25, 0.1, 0.7, 0.85]); +set(figAxes, 'ydir', 'reverse', 'color', 'none'); + +% UI Control Buttons +selectFilebtn = uicontrol(artifactRemovalFig, 'Units', 'Normalized', 'Style', 'listbox', 'String', smFileList,... + 'Position', [0.0375,0.65,0.125,0.28],'Callback', @selectFile); + +%% \ No newline at end of file diff --git a/Analyses/SequenceNeuralViewer_SM.m b/Analyses/SequenceNeuralViewer_SM.m new file mode 100644 index 0000000..c673869 --- /dev/null +++ b/Analyses/SequenceNeuralViewer_SM.m @@ -0,0 +1,166 @@ +function SequenceNeuralViewer_SM +%% Declare Runtime Variables + +% Histogram bins for peak FR latency decision +histBins = 0:0.1:1.2; +% FR threshold for trial period firing for inclusion in plot +trialFRthresh = 1; +% Spike width threshold for PC determination +widthThresh = 0.45; +% Spike rate threshold for PC determination +spkRtThresh = 5; +% LFP Band to plot +band2plot = 'Raw'; +%% Load data +[smFile,smPath] = uigetfile('*.mat'); +cd(smPath); +files = dir(smPath); +fileNames = {files.name}; +% Load the behavior matrix file for poke events plots +behMatFile = fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'BehaviorMatrix'))}; +nsmblMatFile = fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'EnsembleMatrix'))}; +load([smPath behMatFile]); +load([smPath nsmblMatFile]); +% Identify list of all statMatrix files +smFileList = fileNames(cellfun(@(a)~isempty(a), regexp(fileNames, '_SM\>')))'; + +%% Organize Ensemble Data +% Extract session variables & ensemble FR +behavMatrixTrialStruct = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [min(histBins) max(histBins)], 'PokeIn'); +ensembleTrialData = ExtractTrialData_SM(behavMatrixTrialStruct, ensembleMatrix(:,2:end)); %#ok<*NODEF> +trialTSvect = behavMatrix(behavMatrixTrialStruct(1).TrialLogVect,1)-behavMatrix(behavMatrixTrialStruct(1).PokeInIndex,1); +inSeqLog = [behavMatrixTrialStruct.TranspositionDistance]==0; +perfLog = [behavMatrixTrialStruct.Performance]==1; +oAlog = [behavMatrixTrialStruct.Position]==1; +% iscEnsembleData = ensembleTrialData(inSeqLog & perfLog); +iscEnsembleData = ensembleTrialData(inSeqLog & perfLog & oAlog); + +% Bin trial wise FR +iscEnsembleDataBinned = nan(length(histBins)-1, length(ensembleUnitSummaries), length(iscEnsembleData)); +for tr = 1:length(iscEnsembleData) + for uni = 1:length(ensembleUnitSummaries) + iscEnsembleDataBinned(:,uni,tr) = histcounts(trialTSvect(logical(iscEnsembleData{tr}(:,uni))), histBins, 'Normalization', 'CountDensity'); + end +end +meanISCbinned = mean(iscEnsembleDataBinned,3); + +% Normalize the FR and identify the peak FR bin +iscBinnedNormed = nan(size(meanISCbinned)); +iscPeakLat = nan(size(meanISCbinned,2),3); +for uni = 1:size(meanISCbinned,2) + iscBinnedNormed(:,uni) = meanISCbinned(:,uni)./max(meanISCbinned(:,uni)); + curPeakLat = find(iscBinnedNormed(:,uni)==1,1,'first'); + if isempty(curPeakLat) + iscPeakLat(uni,:) = [size(iscBinnedNormed,1), uni, max(meanISCbinned(:,uni))]; + else + iscPeakLat(uni,:) = [curPeakLat, uni, max(meanISCbinned(:,uni))]; + end +end +% Select cells based on peak FR value +trialFRthreshLog = iscPeakLat(:,3)widthThresh; +% Spike Rate Based +% pcLog = spkRt'))); +lfpBands = cellfun(@(a,b)a(b:end), statMatrixColIDs(lfpColIDs), regexp(statMatrixColIDs(lfpColIDs), '[(A-Z)|(a-z)]*\>'), 'uniformoutput', 0); +lfpVals = statMatrix(:,lfpColIDs(strcmp(lfpBands, band2plot))); +lfpVals = lfpVals/(max(abs(lfpVals))); + +plot(statMatrix(:,1), lfpVals-1, 'color', [0.4 0.4 0.4]); + +% Plot Spikes +hold on; +for uni = 1:size(uniScatData,2) + scatter(uniScatData{1,uni}, uniScatData{2,uni}, '*k'); +end +set(gca, 'ydir', 'reverse', 'color', 'none'); + +% Patch Trial Periods +for trl = 1:length(behavMatrixTrialStruct) + switch behavMatrixTrialStruct(trl).Odor + case 1 + patchColor = [44/255 168/255 224/255]; + case 2 + patchColor = [154/255 133/255 122/255]; + case 3 + patchColor = [9/255 161/255 74/255]; + case 4 + patchColor = [128/255 66/255 151/255]; + case 5 + patchColor = 'k'; + end + patch(gca, 'XData', [behavMatrix(behavMatrixTrialStruct(trl).PokeInIndex,1), behavMatrix(behavMatrixTrialStruct(trl).PokeOutIndex,1), behavMatrix(behavMatrixTrialStruct(trl).PokeOutIndex,1),behavMatrix(behavMatrixTrialStruct(trl).PokeInIndex,1)],... + 'YData', [zeros(1,2), repmat(icScatData{2,end}(1)+1,[1,2])],... + 'FaceColor', patchColor, 'FaceAlpha', 0.15,... + 'EdgeColor', patchColor); +end + +%% +figure; +scatter([ensembleUnitSummaries.Mean_SpikeRate], [ensembleUnitSummaries.Spike_Width]*40); set(gca, 'xscale', 'log') +%% Create Figure +% seqViewFig = figure; +% figAxes = axes(seqViewFig, 'position', [0.25, 0.1, 0.7, 0.85]); +% set(figAxes, 'ydir', 'reverse', 'color', 'none'); +% +% % UI Control Buttons +% selectFilebtn = uicontrol(artifactRemovalFig, 'Units', 'Normalized', 'Style', 'listbox', 'String', smFileList,... +% 'Position', [0.0375,0.65,0.125,0.28],'Callback', @selectFile); + +%% \ No newline at end of file diff --git a/Analyses/SummarizeUnits_SM.m b/Analyses/SummarizeUnits_SM.m index 33d549e..bec277a 100644 --- a/Analyses/SummarizeUnits_SM.m +++ b/Analyses/SummarizeUnits_SM.m @@ -61,7 +61,7 @@ end % Number of permutations for chance distribution estimation -numPerms = 10; +numPerms = 5; %% Analysis #1: Examine Average Evoked activity during trial periods fprintf('Starting Analysis #1 @%s....', datetime); @@ -420,57 +420,57 @@ fprintf('Completed\n'); %% Analysis #3: Examine Information content by LFP Phase -fprintf('Starting Analysis 3 @%s....', datetime); -% This is best done using the epoch extraction script since it give lfp -% phase values. -[earlyUnitEpoch, earlyUnitIDs, earlyLfpEpoch, earlyLfpIDs, ~, rlyTimeBins, earlyTrialInfo] = EpochExtraction_SM('PokeIn', -0.9, 2.1, 'org', 'TiUTr', 'lfpBand', 'All', 'lfpData', 'Phase'); - -currPos = nan(size(earlyTrialInfo,1),1); -currOdr = nan(size(earlyTrialInfo,1),1); -prevOdr = nan(size(earlyTrialInfo,1),1); -for trl = 2:size(earlyTrialInfo,1) - if earlyTrialInfo(trl,1)==1 && (earlyTrialInfo(trl,3) - earlyTrialInfo(trl-1,3) == 1) - currPos(trl) = earlyTrialInfo(trl,3); - currOdr(trl) = earlyTrialInfo(trl,4); - prevOdr(trl) = earlyTrialInfo(trl-1,4); - end -end -earlyEpochEnsmbl = earlyUnitEpoch(:,:,~isnan(currPos)); -earlyEpochLFP = earlyLfpEpoch(:,:,~isnan(currPos)); -currPos = currPos(~isnan(currPos)); -currOdr = currOdr(~isnan(currOdr)); -prevOdr = prevOdr(~isnan(prevOdr)); - -lfpIDparts = cellfun(@(b)[b(1);b(3)], cellfun(@(a)strsplit(a, '_'), earlyLfpIDs, 'uniformoutput', 0), 'uniformoutput', 0); -lfpIDparts = [lfpIDparts{:}]; -bands = unique(lfpIDparts(2,:)); -bands(strcmp(bands, 'Raw')) = []; - -for uni = 1:length(earlyUnitIDs) - curUniInfoSpot = strcmp(earlyUnitIDs{uni}, {unitInfo.UnitName}); - curTet = earlyUnitIDs{uni}(1:regexp(earlyUnitIDs{uni}, '-')-1); - curTetEpochLog = logical(earlyEpochEnsmbl(:,uni,:)); - for band = 1:length(bands) - curTetLFPepoch = earlyEpochLFP(:,strcmp(bands{band},lfpIDparts(2,:)) & strcmp(curTet,lfpIDparts(1,:)),:); - curTetSpikePhaseVals = nan(size(curTetEpochLog)); - curTetSpikePhaseVals(curTetEpochLog) = curTetLFPepoch(curTetEpochLog); - for phase = 1:length(phaseBins)-1 - curTetCurBandPhaseBinSpikes = double(curTetSpikePhaseVals>=phaseBins(phase) & curTetSpikePhaseVals=phaseBins(phase) & curTetSpikePhaseVals Date: Thu, 1 Aug 2019 11:57:09 -0700 Subject: [PATCH 013/133] Update BayesPFCbsPROTO.m It's working? --- Analyses/Prototypes/BayesPFCbsPROTO.m | 63 ++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 12 deletions(-) diff --git a/Analyses/Prototypes/BayesPFCbsPROTO.m b/Analyses/Prototypes/BayesPFCbsPROTO.m index 32e689f..3476513 100644 --- a/Analyses/Prototypes/BayesPFCbsPROTO.m +++ b/Analyses/Prototypes/BayesPFCbsPROTO.m @@ -1,5 +1,5 @@ %% Runtime variables -binSize = 20; +binSize = 100; dsRate = 5; %% @@ -16,7 +16,7 @@ smFileList = fileNames(cellfun(@(a)~isempty(a), regexp(fileNames, '_SM\>')))'; %% Extract Behavioral Periods -trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.5 1.5], 'PokeIn'); +trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.5 1.4], 'PokeIn'); trialEnsemble = ExtractTrialData_SM(trialPeriodTD, ensembleMatrix(:,2:end)); %#ok<*NODEF> trialEnsembleMtx = cell2mat(reshape(trialEnsemble, [1 1 length(trialEnsemble)])); @@ -62,19 +62,51 @@ spkMtx(:,uniFRthreshLog,:) = []; goodUniNames = {ensembleUnitSummaries(~uniFRthreshLog).UnitName}; %% -corrISmtx = mean(spkMtx(:,:,perfLog & inSeqLog),3); - - +% corrISmtx = mean(spkMtx(:,:,perfLog & inSeqLog),3); % All InSeq Correct Trials +corrNonAisMtx = mean(spkMtx(:,:,perfLog & inSeqLog & odorAlog),3); % All A InSeq Correct Trials +[postMtx] = CalculatePostProb(corrNonAisMtx, spkMtx(:,:,perfLog & inSeqLog & odorAlog), binSize); +figure; +imagesc(trialTimes, trialTimes, nanmean(postMtx,3)); +title('Odor A'); +xlabel('Decoded Time'); +ylabel('True Time'); +drawnow + +corrNonBisMtx = mean(spkMtx(:,:,perfLog & inSeqLog & odorBlog),3); % All B InSeq Correct Trials +[postMtx] = CalculatePostProb(corrNonBisMtx, spkMtx(:,:,perfLog & inSeqLog & odorBlog), binSize); +figure; +imagesc(trialTimes, trialTimes, nanmean(postMtx,3)); +title('Odor B'); +xlabel('Decoded Time'); +ylabel('True Time'); +drawnow + +corrNonCisMtx = mean(spkMtx(:,:,perfLog & inSeqLog & odorClog),3); % All C InSeq Correct Trials +[postMtx] = CalculatePostProb(corrNonCisMtx, spkMtx(:,:,perfLog & inSeqLog & odorClog), binSize); +figure; +imagesc(trialTimes, trialTimes, nanmean(postMtx,3)); +title('Odor C'); +xlabel('Decoded Time'); +ylabel('True Time'); +drawnow + +corrNonDisMtx = mean(spkMtx(:,:,perfLog & inSeqLog & odorDlog),3); % All D InSeq Correct Trials +[postMtx] = CalculatePostProb(corrNonDisMtx, spkMtx(:,:,perfLog & inSeqLog & odorDlog), binSize); +figure; +imagesc(trialTimes, trialTimes, nanmean(postMtx,3)); +title('Odor D'); +xlabel('Decoded Time'); +ylabel('True Time'); +drawnow %% -function [postMtx] = CalculatePostProb(meanFR, trialFR) +function [postMtx] = CalculatePostProb(meanFR, trialFR, binSize) propVect = CalculateProportionalConstant(meanFR); post = nan(size(trialFR,1), size(trialFR,1), size(trialFR,3)); for trl = 1:size(trialFR,3) - tic for i1 = 1:size(trialFR,1) curPopVect = trialFR(i1,:,trl); curPopVectFact = factorial(curPopVect); @@ -85,21 +117,28 @@ condProbSpkPerUni(uni) = (((binSize/1000 * curMeanFR(uni))^curPopVect(uni))/curPopVectFact(uni)) * exp(-(binSize/1000*curMeanFR(uni))); end post(i1,i2,trl) = propVect(i1)*prod(condProbSpkPerUni); +% pois1 = nan(size(curMeanFR)); +% pois2 = nan(size(curMeanFR)); +% for uni = 1:size(trialFR,2) +% pois1(uni) = (((binSize/1000 * curMeanFR(uni))^curPopVect(uni))/curPopVectFact(uni)); +% pois2(uni) = exp(-(binSize/1000*curMeanFR(uni))); +% end +% post(i1,i2,trl) = propVect(i1)*prod(pois1)*sum(pois2); end + post(i1,:,trl) = post(i1,:,trl)./max(post(i1,:,trl)); end - toc end - +postMtx = post; end %% function [propConst] = CalculateProportionalConstant(rateMtx) -% propConstMtx = nan(size(rateMtx)); +% propConst = nan(size(rateMtx)); % for u = 1:size(rateMtx,2) % propConstMtx(:,u) = 1./(rateMtx(:,u).*sum(rateMtx(:,u)~=0)); % end -% propConstMtx(isinf(propConstMtx)) = 0; +% propConst(isinf(propConstMtx)) = 0; % sum(rateMtx'.*(1./sum(rateMtx'))) -propConstMtx = 1./sum(rateMtx'); +propConst = 1./sum(rateMtx,2); end From 5266be53f00f2c41a3ff27370e2acfbfdebb0e57 Mon Sep 17 00:00:00 2001 From: gabe Date: Thu, 1 Aug 2019 12:39:06 -0700 Subject: [PATCH 014/133] Update BayesPFCbsPROTO.m --- Analyses/Prototypes/BayesPFCbsPROTO.m | 31 ++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/Analyses/Prototypes/BayesPFCbsPROTO.m b/Analyses/Prototypes/BayesPFCbsPROTO.m index 3476513..d4f5013 100644 --- a/Analyses/Prototypes/BayesPFCbsPROTO.m +++ b/Analyses/Prototypes/BayesPFCbsPROTO.m @@ -1,5 +1,5 @@ %% Runtime variables -binSize = 100; +binSize = 200; dsRate = 5; %% @@ -70,6 +70,14 @@ title('Odor A'); xlabel('Decoded Time'); ylabel('True Time'); +curDir = cd; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +colormap jet +axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +axis(axesHandles,'square') +orient(gcf, 'tall'); +orient(gcf, 'landscape'); drawnow corrNonBisMtx = mean(spkMtx(:,:,perfLog & inSeqLog & odorBlog),3); % All B InSeq Correct Trials @@ -79,6 +87,13 @@ title('Odor B'); xlabel('Decoded Time'); ylabel('True Time'); +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +colormap jet +axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +axis(axesHandles,'square') +orient(gcf, 'tall'); +orient(gcf, 'landscape'); drawnow corrNonCisMtx = mean(spkMtx(:,:,perfLog & inSeqLog & odorClog),3); % All C InSeq Correct Trials @@ -88,6 +103,13 @@ title('Odor C'); xlabel('Decoded Time'); ylabel('True Time'); +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +colormap jet +axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +axis(axesHandles,'square') +orient(gcf, 'tall'); +orient(gcf, 'landscape'); drawnow corrNonDisMtx = mean(spkMtx(:,:,perfLog & inSeqLog & odorDlog),3); % All D InSeq Correct Trials @@ -97,6 +119,13 @@ title('Odor D'); xlabel('Decoded Time'); ylabel('True Time'); +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +colormap jet +axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +axis(axesHandles,'square') +orient(gcf, 'tall'); +orient(gcf, 'landscape'); drawnow From dca5f34120830dc1ed7a63769babee32596ba3b5 Mon Sep 17 00:00:00 2001 From: gaelias Date: Thu, 1 Aug 2019 19:16:42 -0700 Subject: [PATCH 015/133] Update BayesPFCbsPROTO.m --- Analyses/Prototypes/BayesPFCbsPROTO.m | 155 ++++++++++++++------------ 1 file changed, 84 insertions(+), 71 deletions(-) diff --git a/Analyses/Prototypes/BayesPFCbsPROTO.m b/Analyses/Prototypes/BayesPFCbsPROTO.m index d4f5013..65c5ed5 100644 --- a/Analyses/Prototypes/BayesPFCbsPROTO.m +++ b/Analyses/Prototypes/BayesPFCbsPROTO.m @@ -16,11 +16,14 @@ smFileList = fileNames(cellfun(@(a)~isempty(a), regexp(fileNames, '_SM\>')))'; %% Extract Behavioral Periods -trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.5 1.4], 'PokeIn'); +trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.5 1.5], 'PokeIn'); +% trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-1.5 0.5], 'PokeOut'); trialEnsemble = ExtractTrialData_SM(trialPeriodTD, ensembleMatrix(:,2:end)); %#ok<*NODEF> trialEnsembleMtx = cell2mat(reshape(trialEnsemble, [1 1 length(trialEnsemble)])); trialTimes = behavMatrix(trialPeriodTD(1).TrialLogVect,1) - behavMatrix(trialPeriodTD(1).PokeInIndex,1); +% trialTimes = behavMatrix(trialPeriodTD(1).TrialLogVect,1) - behavMatrix(trialPeriodTD(1).PokeOutIndex,1); + %% Bin the spiking data % First convolve the entire trialEnsembleMtx with a square to bin the @@ -59,66 +62,53 @@ %% uniFRthreshLog = max(mean(spikeMatrix,3))<1; spkMtx = spikeMatrix; -spkMtx(:,uniFRthreshLog,:) = []; -goodUniNames = {ensembleUnitSummaries(~uniFRthreshLog).UnitName}; -%% -% corrISmtx = mean(spkMtx(:,:,perfLog & inSeqLog),3); % All InSeq Correct Trials -corrNonAisMtx = mean(spkMtx(:,:,perfLog & inSeqLog & odorAlog),3); % All A InSeq Correct Trials -[postMtx] = CalculatePostProb(corrNonAisMtx, spkMtx(:,:,perfLog & inSeqLog & odorAlog), binSize); +% spkMtx(:,uniFRthreshLog,:) = []; +% goodUniNames = {ensembleUnitSummaries(~uniFRthreshLog).UnitName}; +%% Calculate Decoded Time Per Trial Type figure; -imagesc(trialTimes, trialTimes, nanmean(postMtx,3)); -title('Odor A'); -xlabel('Decoded Time'); -ylabel('True Time'); +corrAisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorAlog),3); % All A InSeq Correct Trials +[postA] = CalculatePostProb(corrAisMtx, spkMtx(:,:,perfLog & inSeqLog & odorAlog & ~fullInSeqLog), binSize); +subplot(2,2,1); +aCaxis = PlotPostMtx(trialTimes, postA, 'Odor A'); + +corrBisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorBlog),3); % All B InSeq Correct Trials +[postB] = CalculatePostProb(corrBisMtx, spkMtx(:,:,perfLog & inSeqLog & odorBlog & ~fullInSeqLog), binSize); +subplot(2,2,2); +bCaxis = PlotPostMtx(trialTimes, postB, 'Odor B'); + +corrCisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorClog),3); % All C InSeq Correct Trials +[postC] = CalculatePostProb(corrCisMtx, spkMtx(:,:,perfLog & inSeqLog & odorClog & ~fullInSeqLog), binSize); +subplot(2,2,3); +cCaxis = PlotPostMtx(trialTimes, postC, 'Odor C'); + +corrDisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorDlog),3); % All D InSeq Correct Trials +[postD] = CalculatePostProb(corrDisMtx, spkMtx(:,:,perfLog & inSeqLog & odorDlog & ~fullInSeqLog), binSize); +subplot(2,2,4); +dCaxis = PlotPostMtx(trialTimes, postD, 'Odor D'); + +cAx = [min([aCaxis, bCaxis, cCaxis, dCaxis]), max([aCaxis, bCaxis, cCaxis, dCaxis])]; + +annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... + 'linestyle', 'none', 'horizontalalignment', 'right'); curDir = cd; annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); colormap jet axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); axis(axesHandles,'square') +set(axesHandles, 'clim', cAx); orient(gcf, 'tall'); orient(gcf, 'landscape'); -drawnow -corrNonBisMtx = mean(spkMtx(:,:,perfLog & inSeqLog & odorBlog),3); % All B InSeq Correct Trials -[postMtx] = CalculatePostProb(corrNonBisMtx, spkMtx(:,:,perfLog & inSeqLog & odorBlog), binSize); -figure; -imagesc(trialTimes, trialTimes, nanmean(postMtx,3)); -title('Odor B'); -xlabel('Decoded Time'); -ylabel('True Time'); -annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... - 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); -colormap jet -axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); -axis(axesHandles,'square') -orient(gcf, 'tall'); -orient(gcf, 'landscape'); -drawnow - -corrNonCisMtx = mean(spkMtx(:,:,perfLog & inSeqLog & odorClog),3); % All C InSeq Correct Trials -[postMtx] = CalculatePostProb(corrNonCisMtx, spkMtx(:,:,perfLog & inSeqLog & odorClog), binSize); -figure; -imagesc(trialTimes, trialTimes, nanmean(postMtx,3)); -title('Odor C'); -xlabel('Decoded Time'); -ylabel('True Time'); -annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... - 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); -colormap jet -axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); -axis(axesHandles,'square') -orient(gcf, 'tall'); -orient(gcf, 'landscape'); -drawnow +%% +figure +corrISmtx = mean(spkMtx(:,:,perfLog & fullInSeqLog),3); % All InSeq Correct Trials +[post] = CalculatePostProb(corrISmtx, spkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog), binSize); +PlotPostMtx(trialTimes, post, 'InSeq Correct Trials'); -corrNonDisMtx = mean(spkMtx(:,:,perfLog & inSeqLog & odorDlog),3); % All D InSeq Correct Trials -[postMtx] = CalculatePostProb(corrNonDisMtx, spkMtx(:,:,perfLog & inSeqLog & odorDlog), binSize); -figure; -imagesc(trialTimes, trialTimes, nanmean(postMtx,3)); -title('Odor D'); -xlabel('Decoded Time'); -ylabel('True Time'); +annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... + 'linestyle', 'none', 'horizontalalignment', 'right'); +curDir = cd; annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); colormap jet @@ -126,38 +116,61 @@ axis(axesHandles,'square') orient(gcf, 'tall'); orient(gcf, 'landscape'); +%% +function cAx = PlotPostMtx(trialTimes, postMtx, id) +imagesc(trialTimes, trialTimes, nanmean(postMtx,3)'); +set(gca, 'ydir', 'normal') +title(id); +xlabel('True Time'); +ylabel('Decoded Time'); drawnow - +cAx = get(gca, 'clim'); +end %% function [postMtx] = CalculatePostProb(meanFR, trialFR, binSize) propVect = CalculateProportionalConstant(meanFR); post = nan(size(trialFR,1), size(trialFR,1), size(trialFR,3)); - -for trl = 1:size(trialFR,3) - for i1 = 1:size(trialFR,1) - curPopVect = trialFR(i1,:,trl); - curPopVectFact = factorial(curPopVect); - for i2 = 1:size(trialFR,1) - curMeanFR = meanFR(i2,:); - condProbSpkPerUni = nan(size(curMeanFR)); - for uni = 1:size(trialFR,2) - condProbSpkPerUni(uni) = (((binSize/1000 * curMeanFR(uni))^curPopVect(uni))/curPopVectFact(uni)) * exp(-(binSize/1000*curMeanFR(uni))); - end - post(i1,i2,trl) = propVect(i1)*prod(condProbSpkPerUni); -% pois1 = nan(size(curMeanFR)); -% pois2 = nan(size(curMeanFR)); +% +% for trl = 1:size(trialFR,3) +% for i1 = 1:size(trialFR,1) +% curPopVect = trialFR(i1,:,trl); +% curPopVectFact = factorial(curPopVect); +% for i2 = 1:size(trialFR,1) +% curMeanFR = meanFR(i2,:); +% condProbSpkPerUni = nan(size(curMeanFR)); % for uni = 1:size(trialFR,2) -% pois1(uni) = (((binSize/1000 * curMeanFR(uni))^curPopVect(uni))/curPopVectFact(uni)); -% pois2(uni) = exp(-(binSize/1000*curMeanFR(uni))); +% condProbSpkPerUni(uni) = (((binSize/1000 * curMeanFR(uni))^curPopVect(uni))/curPopVectFact(uni)) * exp(-(binSize/1000*curMeanFR(uni))); % end -% post(i1,i2,trl) = propVect(i1)*prod(pois1)*sum(pois2); - end - post(i1,:,trl) = post(i1,:,trl)./max(post(i1,:,trl)); +% post(i1,i2,trl) = propVect(i1)*prod(condProbSpkPerUni); +% end +% post(i1,:,trl) = post(i1,:,trl)./max(post(i1,:,trl)); +% end +% end +% postMtx = post; + +postPE = nan(size(trialFR,1), size(trialFR,1), size(trialFR,3)); +for trl = 1:size(trialFR,3) + for t = 1:size(meanFR,1) + p = nan(size(meanFR)); +% e = nan(size(meanFR)); + curPopVect = trialFR(t,:,trl); + curPopFact = factorial(curPopVect); + for u = 1:size(meanFR,2) + curAvgUniFR = meanFR(:,u); + p(:,u) = (((binSize/1000)*curAvgUniFR).^curPopVect(u))./curPopFact(u); +% e(:,u) = exp(-(binSize/1000*curAvgUniFR)); + end + pp = prod(p,2); +% ee = sum(e,2); + ee = exp(-(binSize/1000*sum(meanFR,2))); + tempPost = propVect.*pp.*ee; + postPE(t,:,trl) = tempPost./max(tempPost); end end -postMtx = post; +postMtx = postPE; + end %% From c60052c2123e5819413744f4f69a412894f183df Mon Sep 17 00:00:00 2001 From: gabe Date: Fri, 2 Aug 2019 08:08:00 -0700 Subject: [PATCH 016/133] Update BayesPFCbsPROTO.m --- Analyses/Prototypes/BayesPFCbsPROTO.m | 35 +++++++-------------------- 1 file changed, 9 insertions(+), 26 deletions(-) diff --git a/Analyses/Prototypes/BayesPFCbsPROTO.m b/Analyses/Prototypes/BayesPFCbsPROTO.m index 65c5ed5..6d5fbd5 100644 --- a/Analyses/Prototypes/BayesPFCbsPROTO.m +++ b/Analyses/Prototypes/BayesPFCbsPROTO.m @@ -116,6 +116,7 @@ axis(axesHandles,'square') orient(gcf, 'tall'); orient(gcf, 'landscape'); + %% function cAx = PlotPostMtx(trialTimes, postMtx, id) imagesc(trialTimes, trialTimes, nanmean(postMtx,3)'); @@ -129,47 +130,29 @@ %% -function [postMtx] = CalculatePostProb(meanFR, trialFR, binSize) +function [postNorm, postRaw] = CalculatePostProb(meanFR, trialFR, binSize) propVect = CalculateProportionalConstant(meanFR); -post = nan(size(trialFR,1), size(trialFR,1), size(trialFR,3)); -% -% for trl = 1:size(trialFR,3) -% for i1 = 1:size(trialFR,1) -% curPopVect = trialFR(i1,:,trl); -% curPopVectFact = factorial(curPopVect); -% for i2 = 1:size(trialFR,1) -% curMeanFR = meanFR(i2,:); -% condProbSpkPerUni = nan(size(curMeanFR)); -% for uni = 1:size(trialFR,2) -% condProbSpkPerUni(uni) = (((binSize/1000 * curMeanFR(uni))^curPopVect(uni))/curPopVectFact(uni)) * exp(-(binSize/1000*curMeanFR(uni))); -% end -% post(i1,i2,trl) = propVect(i1)*prod(condProbSpkPerUni); -% end -% post(i1,:,trl) = post(i1,:,trl)./max(post(i1,:,trl)); -% end -% end -% postMtx = post; - -postPE = nan(size(trialFR,1), size(trialFR,1), size(trialFR,3)); +postNorm = nan(size(trialFR,1), size(trialFR,1), size(trialFR,3)); +postRaw = nan(size(trialFR,1), size(trialFR,1), size(trialFR,3)); for trl = 1:size(trialFR,3) for t = 1:size(meanFR,1) p = nan(size(meanFR)); -% e = nan(size(meanFR)); +% e = nan(size(meanFR)); % I think this is wrong curPopVect = trialFR(t,:,trl); curPopFact = factorial(curPopVect); for u = 1:size(meanFR,2) curAvgUniFR = meanFR(:,u); p(:,u) = (((binSize/1000)*curAvgUniFR).^curPopVect(u))./curPopFact(u); -% e(:,u) = exp(-(binSize/1000*curAvgUniFR)); +% e(:,u) = exp(-(binSize/1000*curAvgUniFR)); % I think this is wrong end pp = prod(p,2); -% ee = sum(e,2); +% ee = sum(e,2); % I think this is wrong ee = exp(-(binSize/1000*sum(meanFR,2))); tempPost = propVect.*pp.*ee; - postPE(t,:,trl) = tempPost./max(tempPost); + postRaw(t,:,trl) = tempPost; + postNorm(t,:,trl) = tempPost./max(tempPost); end end -postMtx = postPE; end From 7363592f7bc4ffc0e94932649c1684b7891d88cc Mon Sep 17 00:00:00 2001 From: gabe Date: Fri, 2 Aug 2019 12:22:01 -0700 Subject: [PATCH 017/133] Update BayesPFCbsPROTO.m --- Analyses/Prototypes/BayesPFCbsPROTO.m | 218 ++++++++++++++++++++------ 1 file changed, 166 insertions(+), 52 deletions(-) diff --git a/Analyses/Prototypes/BayesPFCbsPROTO.m b/Analyses/Prototypes/BayesPFCbsPROTO.m index 6d5fbd5..2471f4f 100644 --- a/Analyses/Prototypes/BayesPFCbsPROTO.m +++ b/Analyses/Prototypes/BayesPFCbsPROTO.m @@ -1,5 +1,9 @@ +%% +clc +clear all + %% Runtime variables -binSize = 200; +binSize = 250; dsRate = 5; %% @@ -16,7 +20,8 @@ smFileList = fileNames(cellfun(@(a)~isempty(a), regexp(fileNames, '_SM\>')))'; %% Extract Behavioral Periods -trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.5 1.5], 'PokeIn'); +% Taking 1/2 the binSize on either end to get rid of edge effects. +trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.5-(binSize/2/1000) 1.5+(binSize/2/1000)], 'PokeIn'); % trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-1.5 0.5], 'PokeOut'); trialEnsemble = ExtractTrialData_SM(trialPeriodTD, ensembleMatrix(:,2:end)); %#ok<*NODEF> trialEnsembleMtx = cell2mat(reshape(trialEnsemble, [1 1 length(trialEnsemble)])); @@ -34,9 +39,11 @@ binnedEnsembleMtx(:,u,t) = conv(trialEnsembleMtx(:,u,t), ones(1,binSize)./(binSize/1000), 'same'); end end +% Now remove the binSize/2 padding +unPaddedBinnedEnsembleMtx = binnedEnsembleMtx((binSize/2)+1:end-(binSize/2),:,:); % Now downsample the binned matrix -dsVect = downsample(1:size(binnedEnsembleMtx,1), dsRate); -spikeMatrix = binnedEnsembleMtx(dsVect,:,:); +dsVect = downsample(1:size(unPaddedBinnedEnsembleMtx,1), dsRate); +spikeMatrix = unPaddedBinnedEnsembleMtx(dsVect,:,:); trialTime = trialTimes(dsVect); %% Create Logical Vectors @@ -62,44 +69,163 @@ %% uniFRthreshLog = max(mean(spikeMatrix,3))<1; spkMtx = spikeMatrix; -% spkMtx(:,uniFRthreshLog,:) = []; -% goodUniNames = {ensembleUnitSummaries(~uniFRthreshLog).UnitName}; -%% Calculate Decoded Time Per Trial Type +spkMtx(:,uniFRthreshLog,:) = []; +goodUniNames = {ensembleUnitSummaries(~uniFRthreshLog).UnitName}; +%% Decode Trial Time Across Odors +% figure; +% corrAisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorAlog),3); % All A InSeq Correct Trials +% [postAnorm, postAraw] = CalculatePostProb(corrAisMtx, spkMtx(:,:,perfLog & inSeqLog & odorAlog & ~fullInSeqLog), binSize); +% subplot(2,2,1); +% aCaxis = PlotPostMtx(trialTimes, postAnorm, 'Odor A'); +% +% corrBisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorBlog),3); % All B InSeq Correct Trials +% [postBnorm, postBraw] = CalculatePostProb(corrBisMtx, spkMtx(:,:,perfLog & inSeqLog & odorBlog & ~fullInSeqLog), binSize); +% subplot(2,2,2); +% bCaxis = PlotPostMtx(trialTimes, postBnorm, 'Odor B'); +% +% corrCisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorClog),3); % All C InSeq Correct Trials +% [postCnorm, postCraw] = CalculatePostProb(corrCisMtx, spkMtx(:,:,perfLog & inSeqLog & odorClog & ~fullInSeqLog), binSize); +% subplot(2,2,3); +% cCaxis = PlotPostMtx(trialTimes, postCnorm, 'Odor C'); +% +% corrDisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorDlog),3); % All D InSeq Correct Trials +% [postDnorm, postDraw] = CalculatePostProb(corrDisMtx, spkMtx(:,:,perfLog & inSeqLog & odorDlog & ~fullInSeqLog), binSize); +% subplot(2,2,4); +% dCaxis = PlotPostMtx(trialTimes, postDnorm, 'Odor D'); +% +% cAx = [min([aCaxis, bCaxis, cCaxis, dCaxis]), max([aCaxis, bCaxis, cCaxis, dCaxis])]; +% +% annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... +% 'linestyle', 'none', 'horizontalalignment', 'right'); +% curDir = cd; +% annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... +% 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +% colormap jet +% axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +% axis(axesHandles,'square') +% set(axesHandles, 'clim', cAx); +% orient(gcf, 'tall'); +% orient(gcf, 'landscape'); + +%% Decode Trial Time and Odor Across Odors +nonAIStrials = spkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog); +nonAISodors = [trialPeriodTD(perfLog & inSeqLog & ~fullInSeqLog).Odor]; +corrAisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorAlog),3); % All A InSeq Correct Trials Prior +[~, aPriorAllPostRaw] = CalculatePostProb(corrAisMtx, nonAIStrials, binSize); + +corrBisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorBlog),3); % All B InSeq Correct Trials Prior +[~, bPriorAllPostRaw] = CalculatePostProb(corrBisMtx, nonAIStrials, binSize); + +corrCisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorClog),3); % All C InSeq Correct Trials Prior +[~, cPriorAllPostRaw] = CalculatePostProb(corrCisMtx, nonAIStrials, binSize); + +corrDisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorDlog),3); % All D InSeq Correct Trials Prior +[~, dPriorAllPostRaw] = CalculatePostProb(corrDisMtx, nonAIStrials, binSize); + figure; -corrAisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorAlog),3); % All A InSeq Correct Trials -[postA] = CalculatePostProb(corrAisMtx, spkMtx(:,:,perfLog & inSeqLog & odorAlog & ~fullInSeqLog), binSize); -subplot(2,2,1); -aCaxis = PlotPostMtx(trialTimes, postA, 'Odor A'); +oAfr = subplot(4,4,1); +imagesc(trialTimes, 1:size(spkMtx,2), corrAisMtx'); +title('Odor A'); +abCorr = subplot(4,4,2); +corrScatPlot(corrAisMtx(:),corrBisMtx(:), 'A','B', 'markerStyle', '.', 'markerColor', 'k'); +acCorr = subplot(4,4,3); +corrScatPlot(corrAisMtx(:),corrCisMtx(:), 'A','C', 'markerStyle', '.', 'markerColor', 'k'); +adCorr = subplot(4,4,4); +corrScatPlot(corrAisMtx(:),corrDisMtx(:), 'A','B', 'markerStyle', '.', 'markerColor', 'k'); -corrBisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorBlog),3); % All B InSeq Correct Trials -[postB] = CalculatePostProb(corrBisMtx, spkMtx(:,:,perfLog & inSeqLog & odorBlog & ~fullInSeqLog), binSize); -subplot(2,2,2); -bCaxis = PlotPostMtx(trialTimes, postB, 'Odor B'); +oBfr = subplot(4,4,6); +imagesc(trialTimes, 1:size(spkMtx,2), corrBisMtx'); +title('Odor B'); +bcCorr = subplot(4,4,7); +corrScatPlot(corrBisMtx(:),corrCisMtx(:), 'B','C', 'markerStyle', '.', 'markerColor', 'k'); +bdCorr = subplot(4,4,8); +corrScatPlot(corrBisMtx(:),corrDisMtx(:), 'B','D', 'markerStyle', '.', 'markerColor', 'k'); -corrCisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorClog),3); % All C InSeq Correct Trials -[postC] = CalculatePostProb(corrCisMtx, spkMtx(:,:,perfLog & inSeqLog & odorClog & ~fullInSeqLog), binSize); -subplot(2,2,3); -cCaxis = PlotPostMtx(trialTimes, postC, 'Odor C'); +oCfr = subplot(4,4,11); +imagesc(trialTimes, 1:size(spkMtx,2), corrCisMtx'); +title('Odor C'); +cdCorr = subplot(4,4,12); +corrScatPlot(corrCisMtx(:),corrDisMtx(:), 'C','D', 'markerStyle', '.', 'markerColor', 'k'); -corrDisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorDlog),3); % All D InSeq Correct Trials -[postD] = CalculatePostProb(corrDisMtx, spkMtx(:,:,perfLog & inSeqLog & odorDlog & ~fullInSeqLog), binSize); -subplot(2,2,4); -dCaxis = PlotPostMtx(trialTimes, postD, 'Odor D'); +oDfr = subplot(4,4,16); +imagesc(trialTimes, 1:size(spkMtx,2), corrDisMtx'); +title('Odor D'); -cAx = [min([aCaxis, bCaxis, cCaxis, dCaxis]), max([aCaxis, bCaxis, cCaxis, dCaxis])]; +frMapSPs = [oAfr, oBfr, oCfr, oDfr]; +curCL = cell2mat(get(frMapSPs, 'clim')); +set(frMapSPs, 'clim', [min(curCL(:,1)), max(curCL(:,2))], 'ydir', 'normal'); +corrSPs = [abCorr, acCorr, adCorr, bcCorr, bdCorr, cdCorr]; +curXL = cell2mat(get(corrSPs, 'xlim')); +curYL = cell2mat(get(corrSPs, 'ylim')); +set(corrSPs, 'xlim', [0 max(curXL(:,2))], 'ylim', [0 max(curYL(:,2))]); + +axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +axis(axesHandles,'square'); annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... 'linestyle', 'none', 'horizontalalignment', 'right'); +annotation('textbox', 'position', [0.025 0.935 0.5 0.05], 'String', '\bf\fontsize{14}Priors',... + 'linestyle', 'none', 'horizontalalignment', 'left'); +curDir = cd; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +colormap jet +orient(gcf, 'tall'); +orient(gcf, 'landscape'); +drawnow; + +nonFISaLog = perfLog & inSeqLog & odorAlog & ~fullInSeqLog; +nonFISbLog = perfLog & inSeqLog & odorBlog & ~fullInSeqLog; +nonFIScLog = perfLog & inSeqLog & odorClog & ~fullInSeqLog; +nonFISdLog = perfLog & inSeqLog & odorDlog & ~fullInSeqLog; + +allNonFISpost = [aPriorAllPostRaw, bPriorAllPostRaw, cPriorAllPostRaw, dPriorAllPostRaw]; +for c = 1:size(allNonFISpost,1) + allNonFISpost(c,:,:) = allNonFISpost(c,:,:)./max(allNonFISpost(c,:,:)); +end + +aPriorAllPostNorm = allNonFISpost(:,1:size(aPriorAllPostRaw,1),:); +bPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)+1:size(aPriorAllPostRaw,1)*2,:); +cPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)*2+1:size(aPriorAllPostRaw,1)*3,:); +dPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)*3+1:end,:); + +figure; +cAx = nan(4,4,2); +for prior = 1:4 + switch prior + case 1 + curPrior = aPriorAllPostNorm; + case 2 + curPrior = bPriorAllPostNorm; + case 3 + curPrior = cPriorAllPostNorm; + case 4 + curPrior = dPriorAllPostNorm; + end + for post = 1:4 + curPostLog = nonAISodors == post; + curDecode = curPrior(:,:,curPostLog); + subplot(4,4,sub2ind([4 4], post, prior)) + cAx(prior,post,:) = PlotPostMtx(trialTimes, curDecode, sprintf('Prior%i; Decode%i', prior, post)); + end +end +cAxs = [min(min(cAx(:,:,1))), max(max(cAx(:,:,2)))/3]; + +annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... + 'linestyle', 'none', 'horizontalalignment', 'right'); +annotation('textbox', 'position', [0.025 0.935 0.5 0.05], 'String', '\bf\fontsize{14}Decoding Trial and Odor Across Odors',... + 'linestyle', 'none', 'horizontalalignment', 'left'); curDir = cd; annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); colormap jet axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); axis(axesHandles,'square') -set(axesHandles, 'clim', cAx); +set(axesHandles, 'clim', cAxs); orient(gcf, 'tall'); orient(gcf, 'landscape'); - +drawnow; + %% figure corrISmtx = mean(spkMtx(:,:,perfLog & fullInSeqLog),3); % All InSeq Correct Trials @@ -116,10 +242,11 @@ axis(axesHandles,'square') orient(gcf, 'tall'); orient(gcf, 'landscape'); +drawnow; %% function cAx = PlotPostMtx(trialTimes, postMtx, id) -imagesc(trialTimes, trialTimes, nanmean(postMtx,3)'); +imagesc(trialTimes, trialTimes, nanmean(postMtx,3)); set(gca, 'ydir', 'normal') title(id); xlabel('True Time'); @@ -130,40 +257,27 @@ %% -function [postNorm, postRaw] = CalculatePostProb(meanFR, trialFR, binSize) -propVect = CalculateProportionalConstant(meanFR); -postNorm = nan(size(trialFR,1), size(trialFR,1), size(trialFR,3)); -postRaw = nan(size(trialFR,1), size(trialFR,1), size(trialFR,3)); -for trl = 1:size(trialFR,3) - for t = 1:size(meanFR,1) - p = nan(size(meanFR)); +function [postNorm, postRaw] = CalculatePostProb(prior, obsv, binSize) +propVect = 1./sum(prior,2); +postNorm = nan(size(obsv,1), size(obsv,1), size(obsv,3)); +postRaw = nan(size(obsv,1), size(obsv,1), size(obsv,3)); +for trl = 1:size(obsv,3) + for t = 1:size(prior,1) + p = nan(size(prior)); % e = nan(size(meanFR)); % I think this is wrong - curPopVect = trialFR(t,:,trl); + curPopVect = obsv(t,:,trl); curPopFact = factorial(curPopVect); - for u = 1:size(meanFR,2) - curAvgUniFR = meanFR(:,u); + for u = 1:size(prior,2) + curAvgUniFR = prior(:,u); p(:,u) = (((binSize/1000)*curAvgUniFR).^curPopVect(u))./curPopFact(u); % e(:,u) = exp(-(binSize/1000*curAvgUniFR)); % I think this is wrong end pp = prod(p,2); % ee = sum(e,2); % I think this is wrong - ee = exp(-(binSize/1000*sum(meanFR,2))); + ee = exp(-(binSize/1000*sum(prior,2))); tempPost = propVect.*pp.*ee; postRaw(t,:,trl) = tempPost; postNorm(t,:,trl) = tempPost./max(tempPost); end end - -end - -%% -function [propConst] = CalculateProportionalConstant(rateMtx) -% propConst = nan(size(rateMtx)); -% for u = 1:size(rateMtx,2) -% propConstMtx(:,u) = 1./(rateMtx(:,u).*sum(rateMtx(:,u)~=0)); -% end -% propConst(isinf(propConstMtx)) = 0; - -% sum(rateMtx'.*(1./sum(rateMtx'))) -propConst = 1./sum(rateMtx,2); -end +end \ No newline at end of file From 45b96ea6c91c9ce6a79a7cca95df1c512b3e59f8 Mon Sep 17 00:00:00 2001 From: gabe Date: Fri, 2 Aug 2019 12:25:54 -0700 Subject: [PATCH 018/133] Update BayesPFCbsPROTO.m --- Analyses/Prototypes/BayesPFCbsPROTO.m | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/Analyses/Prototypes/BayesPFCbsPROTO.m b/Analyses/Prototypes/BayesPFCbsPROTO.m index 2471f4f..08e393f 100644 --- a/Analyses/Prototypes/BayesPFCbsPROTO.m +++ b/Analyses/Prototypes/BayesPFCbsPROTO.m @@ -111,16 +111,9 @@ nonAIStrials = spkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog); nonAISodors = [trialPeriodTD(perfLog & inSeqLog & ~fullInSeqLog).Odor]; corrAisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorAlog),3); % All A InSeq Correct Trials Prior -[~, aPriorAllPostRaw] = CalculatePostProb(corrAisMtx, nonAIStrials, binSize); - corrBisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorBlog),3); % All B InSeq Correct Trials Prior -[~, bPriorAllPostRaw] = CalculatePostProb(corrBisMtx, nonAIStrials, binSize); - corrCisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorClog),3); % All C InSeq Correct Trials Prior -[~, cPriorAllPostRaw] = CalculatePostProb(corrCisMtx, nonAIStrials, binSize); - corrDisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorDlog),3); % All D InSeq Correct Trials Prior -[~, dPriorAllPostRaw] = CalculatePostProb(corrDisMtx, nonAIStrials, binSize); figure; oAfr = subplot(4,4,1); @@ -174,6 +167,11 @@ orient(gcf, 'landscape'); drawnow; + +[~, aPriorAllPostRaw] = CalculatePostProb(corrAisMtx, nonAIStrials, binSize); +[~, bPriorAllPostRaw] = CalculatePostProb(corrBisMtx, nonAIStrials, binSize); +[~, cPriorAllPostRaw] = CalculatePostProb(corrCisMtx, nonAIStrials, binSize); +[~, dPriorAllPostRaw] = CalculatePostProb(corrDisMtx, nonAIStrials, binSize); nonFISaLog = perfLog & inSeqLog & odorAlog & ~fullInSeqLog; nonFISbLog = perfLog & inSeqLog & odorBlog & ~fullInSeqLog; nonFIScLog = perfLog & inSeqLog & odorClog & ~fullInSeqLog; From 60f42b9edecc96bb296be4b48be3b307926a2ec1 Mon Sep 17 00:00:00 2001 From: gaelias Date: Fri, 2 Aug 2019 15:18:54 -0700 Subject: [PATCH 019/133] Update BayesPFCbsPROTO.m --- Analyses/Prototypes/BayesPFCbsPROTO.m | 125 ++++++++++++++++++-------- 1 file changed, 88 insertions(+), 37 deletions(-) diff --git a/Analyses/Prototypes/BayesPFCbsPROTO.m b/Analyses/Prototypes/BayesPFCbsPROTO.m index 08e393f..7dbaac7 100644 --- a/Analyses/Prototypes/BayesPFCbsPROTO.m +++ b/Analyses/Prototypes/BayesPFCbsPROTO.m @@ -3,7 +3,7 @@ clear all %% Runtime variables -binSize = 250; +binSize = 100; dsRate = 5; %% @@ -22,7 +22,7 @@ %% Extract Behavioral Periods % Taking 1/2 the binSize on either end to get rid of edge effects. trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.5-(binSize/2/1000) 1.5+(binSize/2/1000)], 'PokeIn'); -% trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-1.5 0.5], 'PokeOut'); +% trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-1.5-(binSize/2/1000) 0.5+(binSize/2/1000)], 'PokeOut'); trialEnsemble = ExtractTrialData_SM(trialPeriodTD, ensembleMatrix(:,2:end)); %#ok<*NODEF> trialEnsembleMtx = cell2mat(reshape(trialEnsemble, [1 1 length(trialEnsemble)])); @@ -72,44 +72,45 @@ spkMtx(:,uniFRthreshLog,:) = []; goodUniNames = {ensembleUnitSummaries(~uniFRthreshLog).UnitName}; %% Decode Trial Time Across Odors -% figure; -% corrAisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorAlog),3); % All A InSeq Correct Trials -% [postAnorm, postAraw] = CalculatePostProb(corrAisMtx, spkMtx(:,:,perfLog & inSeqLog & odorAlog & ~fullInSeqLog), binSize); -% subplot(2,2,1); -% aCaxis = PlotPostMtx(trialTimes, postAnorm, 'Odor A'); -% -% corrBisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorBlog),3); % All B InSeq Correct Trials -% [postBnorm, postBraw] = CalculatePostProb(corrBisMtx, spkMtx(:,:,perfLog & inSeqLog & odorBlog & ~fullInSeqLog), binSize); -% subplot(2,2,2); -% bCaxis = PlotPostMtx(trialTimes, postBnorm, 'Odor B'); -% -% corrCisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorClog),3); % All C InSeq Correct Trials -% [postCnorm, postCraw] = CalculatePostProb(corrCisMtx, spkMtx(:,:,perfLog & inSeqLog & odorClog & ~fullInSeqLog), binSize); -% subplot(2,2,3); -% cCaxis = PlotPostMtx(trialTimes, postCnorm, 'Odor C'); -% -% corrDisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorDlog),3); % All D InSeq Correct Trials -% [postDnorm, postDraw] = CalculatePostProb(corrDisMtx, spkMtx(:,:,perfLog & inSeqLog & odorDlog & ~fullInSeqLog), binSize); -% subplot(2,2,4); -% dCaxis = PlotPostMtx(trialTimes, postDnorm, 'Odor D'); -% -% cAx = [min([aCaxis, bCaxis, cCaxis, dCaxis]), max([aCaxis, bCaxis, cCaxis, dCaxis])]; -% -% annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... -% 'linestyle', 'none', 'horizontalalignment', 'right'); -% curDir = cd; -% annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... -% 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); -% colormap jet -% axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); -% axis(axesHandles,'square') -% set(axesHandles, 'clim', cAx); -% orient(gcf, 'tall'); -% orient(gcf, 'landscape'); +figure; +corrAisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorAlog),3); % All A InSeq Correct Trials +[postAnorm, postAraw] = CalculatePostProb(corrAisMtx, spkMtx(:,:,perfLog & inSeqLog & odorAlog & ~fullInSeqLog), binSize); +subplot(2,2,1); +aCaxis = PlotPostMtx(trialTimes, postAnorm, 'Odor A'); + +corrBisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorBlog),3); % All B InSeq Correct Trials +[postBnorm, postBraw] = CalculatePostProb(corrBisMtx, spkMtx(:,:,perfLog & inSeqLog & odorBlog & ~fullInSeqLog), binSize); +subplot(2,2,2); +bCaxis = PlotPostMtx(trialTimes, postBnorm, 'Odor B'); + +corrCisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorClog),3); % All C InSeq Correct Trials +[postCnorm, postCraw] = CalculatePostProb(corrCisMtx, spkMtx(:,:,perfLog & inSeqLog & odorClog & ~fullInSeqLog), binSize); +subplot(2,2,3); +cCaxis = PlotPostMtx(trialTimes, postCnorm, 'Odor C'); + +corrDisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorDlog),3); % All D InSeq Correct Trials +[postDnorm, postDraw] = CalculatePostProb(corrDisMtx, spkMtx(:,:,perfLog & inSeqLog & odorDlog & ~fullInSeqLog), binSize); +subplot(2,2,4); +dCaxis = PlotPostMtx(trialTimes, postDnorm, 'Odor D'); + +cAx = [min([aCaxis, bCaxis, cCaxis, dCaxis]), max([aCaxis, bCaxis, cCaxis, dCaxis])]; + +annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... + 'linestyle', 'none', 'horizontalalignment', 'right'); +curDir = cd; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +colormap jet +axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +axis(axesHandles,'square') +set(axesHandles, 'clim', cAx); +orient(gcf, 'tall'); +orient(gcf, 'landscape'); %% Decode Trial Time and Odor Across Odors nonAIStrials = spkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog); -nonAISodors = [trialPeriodTD(perfLog & inSeqLog & ~fullInSeqLog).Odor]; +nonAIStrialData = trialPeriodTD(perfLog & inSeqLog & ~fullInSeqLog); +nonAISodors = [nonAIStrialData.Odor]; corrAisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorAlog),3); % All A InSeq Correct Trials Prior corrBisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorBlog),3); % All B InSeq Correct Trials Prior corrCisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorClog),3); % All C InSeq Correct Trials Prior @@ -242,6 +243,56 @@ orient(gcf, 'landscape'); drawnow; +%% Compare Trial Before with Trial After OutSeq Trial +nonAIStrialNums = [nonAIStrialData.TrialNum]; +preOStrial = false(size(nonAIStrialNums)); +postOStrial = false(size(nonAIStrialNums)); +for trl = 1:length(nonAIStrialNums) + if nonAIStrialNums(trl)~=1 && nonAIStrialNums(trl) ~= length(trialPeriodTD) + prevTrial = trialPeriodTD(nonAIStrialNums(trl)-1); + curTrl = trialPeriodTD(nonAIStrialNums(trl)); + nextTrial = trialPeriodTD(nonAIStrialNums(trl)+1); + if curTrl.Position ~= 1 + if curTrl.Position - prevTrial.Position == 1 && prevTrial.TranspositionDistance ~= 0 + postOStrial(trl) = true; + preOStrial(trl-2) = true; +% elseif nextTrial.Position - curTrl.Position == 1 && nextTrial.TranspositionDistance ~=0 && nextTrial.Performance == 1 +% preOStrial(trl) = true; + end + end + end +end + +figure; +subplot(1,2,1) +PlotPostMtx(trialTimes, post(:,:,preOStrial), 'Trial Before OutSeq'); + +subplot(1,2,2) +PlotPostMtx(trialTimes, post(:,:,postOStrial), 'Trial After OutSeq'); + +annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... + 'linestyle', 'none', 'horizontalalignment', 'right'); +curDir = cd; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +colormap jet +axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +axis(axesHandles,'square') +orient(gcf, 'tall'); +orient(gcf, 'landscape'); +drawnow; + + + +%% +figure; +for tr = 1:size(post,3) + imagesc(trialTimes, trialTimes, post(:,:,tr), [0 1]); + title(num2str(nonAISodors(tr))); + set(gca, 'ydir', 'normal'); + pause(1); +end + %% function cAx = PlotPostMtx(trialTimes, postMtx, id) imagesc(trialTimes, trialTimes, nanmean(postMtx,3)); From 6729c9bfb6cc07b5ae5f4d785821f4bc7abd4f37 Mon Sep 17 00:00:00 2001 From: gaelias Date: Mon, 5 Aug 2019 15:40:53 -0700 Subject: [PATCH 020/133] Update BayesPFCbsPROTO.m --- Analyses/Prototypes/BayesPFCbsPROTO.m | 328 +++++++++++++------------- 1 file changed, 165 insertions(+), 163 deletions(-) diff --git a/Analyses/Prototypes/BayesPFCbsPROTO.m b/Analyses/Prototypes/BayesPFCbsPROTO.m index 7dbaac7..0bb5e7f 100644 --- a/Analyses/Prototypes/BayesPFCbsPROTO.m +++ b/Analyses/Prototypes/BayesPFCbsPROTO.m @@ -3,7 +3,7 @@ clear all %% Runtime variables -binSize = 100; +binSize = 250; dsRate = 5; %% @@ -21,13 +21,13 @@ %% Extract Behavioral Periods % Taking 1/2 the binSize on either end to get rid of edge effects. -trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.5-(binSize/2/1000) 1.5+(binSize/2/1000)], 'PokeIn'); -% trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-1.5-(binSize/2/1000) 0.5+(binSize/2/1000)], 'PokeOut'); +% trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.5-(binSize/2/1000) 1.5+(binSize/2/1000)], 'PokeIn'); +trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-1.5-(binSize/2/1000) 0.5+(binSize/2/1000)], 'PokeOut'); trialEnsemble = ExtractTrialData_SM(trialPeriodTD, ensembleMatrix(:,2:end)); %#ok<*NODEF> trialEnsembleMtx = cell2mat(reshape(trialEnsemble, [1 1 length(trialEnsemble)])); -trialTimes = behavMatrix(trialPeriodTD(1).TrialLogVect,1) - behavMatrix(trialPeriodTD(1).PokeInIndex,1); -% trialTimes = behavMatrix(trialPeriodTD(1).TrialLogVect,1) - behavMatrix(trialPeriodTD(1).PokeOutIndex,1); +% trialTimes = behavMatrix(trialPeriodTD(1).TrialLogVect,1) - behavMatrix(trialPeriodTD(1).PokeInIndex,1); +trialTimes = behavMatrix(trialPeriodTD(1).TrialLogVect,1) - behavMatrix(trialPeriodTD(1).PokeOutIndex,1); %% Bin the spiking data @@ -72,158 +72,158 @@ spkMtx(:,uniFRthreshLog,:) = []; goodUniNames = {ensembleUnitSummaries(~uniFRthreshLog).UnitName}; %% Decode Trial Time Across Odors -figure; -corrAisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorAlog),3); % All A InSeq Correct Trials -[postAnorm, postAraw] = CalculatePostProb(corrAisMtx, spkMtx(:,:,perfLog & inSeqLog & odorAlog & ~fullInSeqLog), binSize); -subplot(2,2,1); -aCaxis = PlotPostMtx(trialTimes, postAnorm, 'Odor A'); - -corrBisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorBlog),3); % All B InSeq Correct Trials -[postBnorm, postBraw] = CalculatePostProb(corrBisMtx, spkMtx(:,:,perfLog & inSeqLog & odorBlog & ~fullInSeqLog), binSize); -subplot(2,2,2); -bCaxis = PlotPostMtx(trialTimes, postBnorm, 'Odor B'); - -corrCisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorClog),3); % All C InSeq Correct Trials -[postCnorm, postCraw] = CalculatePostProb(corrCisMtx, spkMtx(:,:,perfLog & inSeqLog & odorClog & ~fullInSeqLog), binSize); -subplot(2,2,3); -cCaxis = PlotPostMtx(trialTimes, postCnorm, 'Odor C'); - -corrDisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorDlog),3); % All D InSeq Correct Trials -[postDnorm, postDraw] = CalculatePostProb(corrDisMtx, spkMtx(:,:,perfLog & inSeqLog & odorDlog & ~fullInSeqLog), binSize); -subplot(2,2,4); -dCaxis = PlotPostMtx(trialTimes, postDnorm, 'Odor D'); - -cAx = [min([aCaxis, bCaxis, cCaxis, dCaxis]), max([aCaxis, bCaxis, cCaxis, dCaxis])]; - -annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... - 'linestyle', 'none', 'horizontalalignment', 'right'); -curDir = cd; -annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... - 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); -colormap jet -axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); -axis(axesHandles,'square') -set(axesHandles, 'clim', cAx); -orient(gcf, 'tall'); -orient(gcf, 'landscape'); +% figure; +% corrAisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorAlog),3); % All A InSeq Correct Trials +% [postAnorm, postAraw] = CalculatePostProb(corrAisMtx, spkMtx(:,:,perfLog & inSeqLog & odorAlog & ~fullInSeqLog), binSize); +% subplot(2,2,1); +% aCaxis = PlotPostMtx(trialTimes, postAnorm, 'Odor A'); +% +% corrBisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorBlog),3); % All B InSeq Correct Trials +% [postBnorm, postBraw] = CalculatePostProb(corrBisMtx, spkMtx(:,:,perfLog & inSeqLog & odorBlog & ~fullInSeqLog), binSize); +% subplot(2,2,2); +% bCaxis = PlotPostMtx(trialTimes, postBnorm, 'Odor B'); +% +% corrCisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorClog),3); % All C InSeq Correct Trials +% [postCnorm, postCraw] = CalculatePostProb(corrCisMtx, spkMtx(:,:,perfLog & inSeqLog & odorClog & ~fullInSeqLog), binSize); +% subplot(2,2,3); +% cCaxis = PlotPostMtx(trialTimes, postCnorm, 'Odor C'); +% +% corrDisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorDlog),3); % All D InSeq Correct Trials +% [postDnorm, postDraw] = CalculatePostProb(corrDisMtx, spkMtx(:,:,perfLog & inSeqLog & odorDlog & ~fullInSeqLog), binSize); +% subplot(2,2,4); +% dCaxis = PlotPostMtx(trialTimes, postDnorm, 'Odor D'); +% +% cAx = [min([aCaxis, bCaxis, cCaxis, dCaxis]), max([aCaxis, bCaxis, cCaxis, dCaxis])]; +% +% annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... +% 'linestyle', 'none', 'horizontalalignment', 'right'); +% curDir = cd; +% annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... +% 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +% colormap jet +% axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +% axis(axesHandles,'square') +% set(axesHandles, 'clim', cAx); +% orient(gcf, 'tall'); +% orient(gcf, 'landscape'); %% Decode Trial Time and Odor Across Odors -nonAIStrials = spkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog); -nonAIStrialData = trialPeriodTD(perfLog & inSeqLog & ~fullInSeqLog); -nonAISodors = [nonAIStrialData.Odor]; -corrAisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorAlog),3); % All A InSeq Correct Trials Prior -corrBisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorBlog),3); % All B InSeq Correct Trials Prior -corrCisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorClog),3); % All C InSeq Correct Trials Prior -corrDisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorDlog),3); % All D InSeq Correct Trials Prior - -figure; -oAfr = subplot(4,4,1); -imagesc(trialTimes, 1:size(spkMtx,2), corrAisMtx'); -title('Odor A'); -abCorr = subplot(4,4,2); -corrScatPlot(corrAisMtx(:),corrBisMtx(:), 'A','B', 'markerStyle', '.', 'markerColor', 'k'); -acCorr = subplot(4,4,3); -corrScatPlot(corrAisMtx(:),corrCisMtx(:), 'A','C', 'markerStyle', '.', 'markerColor', 'k'); -adCorr = subplot(4,4,4); -corrScatPlot(corrAisMtx(:),corrDisMtx(:), 'A','B', 'markerStyle', '.', 'markerColor', 'k'); - -oBfr = subplot(4,4,6); -imagesc(trialTimes, 1:size(spkMtx,2), corrBisMtx'); -title('Odor B'); -bcCorr = subplot(4,4,7); -corrScatPlot(corrBisMtx(:),corrCisMtx(:), 'B','C', 'markerStyle', '.', 'markerColor', 'k'); -bdCorr = subplot(4,4,8); -corrScatPlot(corrBisMtx(:),corrDisMtx(:), 'B','D', 'markerStyle', '.', 'markerColor', 'k'); - -oCfr = subplot(4,4,11); -imagesc(trialTimes, 1:size(spkMtx,2), corrCisMtx'); -title('Odor C'); -cdCorr = subplot(4,4,12); -corrScatPlot(corrCisMtx(:),corrDisMtx(:), 'C','D', 'markerStyle', '.', 'markerColor', 'k'); - -oDfr = subplot(4,4,16); -imagesc(trialTimes, 1:size(spkMtx,2), corrDisMtx'); -title('Odor D'); - -frMapSPs = [oAfr, oBfr, oCfr, oDfr]; -curCL = cell2mat(get(frMapSPs, 'clim')); -set(frMapSPs, 'clim', [min(curCL(:,1)), max(curCL(:,2))], 'ydir', 'normal'); -corrSPs = [abCorr, acCorr, adCorr, bcCorr, bdCorr, cdCorr]; -curXL = cell2mat(get(corrSPs, 'xlim')); -curYL = cell2mat(get(corrSPs, 'ylim')); -set(corrSPs, 'xlim', [0 max(curXL(:,2))], 'ylim', [0 max(curYL(:,2))]); - -axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); -axis(axesHandles,'square'); - -annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... - 'linestyle', 'none', 'horizontalalignment', 'right'); -annotation('textbox', 'position', [0.025 0.935 0.5 0.05], 'String', '\bf\fontsize{14}Priors',... - 'linestyle', 'none', 'horizontalalignment', 'left'); -curDir = cd; -annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... - 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); -colormap jet -orient(gcf, 'tall'); -orient(gcf, 'landscape'); -drawnow; - - -[~, aPriorAllPostRaw] = CalculatePostProb(corrAisMtx, nonAIStrials, binSize); -[~, bPriorAllPostRaw] = CalculatePostProb(corrBisMtx, nonAIStrials, binSize); -[~, cPriorAllPostRaw] = CalculatePostProb(corrCisMtx, nonAIStrials, binSize); -[~, dPriorAllPostRaw] = CalculatePostProb(corrDisMtx, nonAIStrials, binSize); -nonFISaLog = perfLog & inSeqLog & odorAlog & ~fullInSeqLog; -nonFISbLog = perfLog & inSeqLog & odorBlog & ~fullInSeqLog; -nonFIScLog = perfLog & inSeqLog & odorClog & ~fullInSeqLog; -nonFISdLog = perfLog & inSeqLog & odorDlog & ~fullInSeqLog; - -allNonFISpost = [aPriorAllPostRaw, bPriorAllPostRaw, cPriorAllPostRaw, dPriorAllPostRaw]; -for c = 1:size(allNonFISpost,1) - allNonFISpost(c,:,:) = allNonFISpost(c,:,:)./max(allNonFISpost(c,:,:)); -end - -aPriorAllPostNorm = allNonFISpost(:,1:size(aPriorAllPostRaw,1),:); -bPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)+1:size(aPriorAllPostRaw,1)*2,:); -cPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)*2+1:size(aPriorAllPostRaw,1)*3,:); -dPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)*3+1:end,:); - -figure; -cAx = nan(4,4,2); -for prior = 1:4 - switch prior - case 1 - curPrior = aPriorAllPostNorm; - case 2 - curPrior = bPriorAllPostNorm; - case 3 - curPrior = cPriorAllPostNorm; - case 4 - curPrior = dPriorAllPostNorm; - end - for post = 1:4 - curPostLog = nonAISodors == post; - curDecode = curPrior(:,:,curPostLog); - subplot(4,4,sub2ind([4 4], post, prior)) - cAx(prior,post,:) = PlotPostMtx(trialTimes, curDecode, sprintf('Prior%i; Decode%i', prior, post)); - end -end -cAxs = [min(min(cAx(:,:,1))), max(max(cAx(:,:,2)))/3]; - -annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... - 'linestyle', 'none', 'horizontalalignment', 'right'); -annotation('textbox', 'position', [0.025 0.935 0.5 0.05], 'String', '\bf\fontsize{14}Decoding Trial and Odor Across Odors',... - 'linestyle', 'none', 'horizontalalignment', 'left'); -curDir = cd; -annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... - 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); -colormap jet -axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); -axis(axesHandles,'square') -set(axesHandles, 'clim', cAxs); -orient(gcf, 'tall'); -orient(gcf, 'landscape'); -drawnow; +% nonAIStrials = spkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog); +% nonAIStrialData = trialPeriodTD(perfLog & inSeqLog & ~fullInSeqLog); +% nonAISodors = [nonAIStrialData.Odor]; +% corrAisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorAlog),3); % All A InSeq Correct Trials Prior +% corrBisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorBlog),3); % All B InSeq Correct Trials Prior +% corrCisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorClog),3); % All C InSeq Correct Trials Prior +% corrDisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorDlog),3); % All D InSeq Correct Trials Prior +% +% figure; +% oAfr = subplot(4,4,1); +% imagesc(trialTimes, 1:size(spkMtx,2), corrAisMtx'); +% title('Odor A'); +% abCorr = subplot(4,4,2); +% corrScatPlot(corrAisMtx(:),corrBisMtx(:), 'A','B', 'markerStyle', '.', 'markerColor', 'k'); +% acCorr = subplot(4,4,3); +% corrScatPlot(corrAisMtx(:),corrCisMtx(:), 'A','C', 'markerStyle', '.', 'markerColor', 'k'); +% adCorr = subplot(4,4,4); +% corrScatPlot(corrAisMtx(:),corrDisMtx(:), 'A','B', 'markerStyle', '.', 'markerColor', 'k'); +% +% oBfr = subplot(4,4,6); +% imagesc(trialTimes, 1:size(spkMtx,2), corrBisMtx'); +% title('Odor B'); +% bcCorr = subplot(4,4,7); +% corrScatPlot(corrBisMtx(:),corrCisMtx(:), 'B','C', 'markerStyle', '.', 'markerColor', 'k'); +% bdCorr = subplot(4,4,8); +% corrScatPlot(corrBisMtx(:),corrDisMtx(:), 'B','D', 'markerStyle', '.', 'markerColor', 'k'); +% +% oCfr = subplot(4,4,11); +% imagesc(trialTimes, 1:size(spkMtx,2), corrCisMtx'); +% title('Odor C'); +% cdCorr = subplot(4,4,12); +% corrScatPlot(corrCisMtx(:),corrDisMtx(:), 'C','D', 'markerStyle', '.', 'markerColor', 'k'); +% +% oDfr = subplot(4,4,16); +% imagesc(trialTimes, 1:size(spkMtx,2), corrDisMtx'); +% title('Odor D'); +% +% frMapSPs = [oAfr, oBfr, oCfr, oDfr]; +% curCL = cell2mat(get(frMapSPs, 'clim')); +% set(frMapSPs, 'clim', [min(curCL(:,1)), max(curCL(:,2))], 'ydir', 'normal'); +% corrSPs = [abCorr, acCorr, adCorr, bcCorr, bdCorr, cdCorr]; +% curXL = cell2mat(get(corrSPs, 'xlim')); +% curYL = cell2mat(get(corrSPs, 'ylim')); +% set(corrSPs, 'xlim', [0 max(curXL(:,2))], 'ylim', [0 max(curYL(:,2))]); +% +% axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +% axis(axesHandles,'square'); +% +% annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... +% 'linestyle', 'none', 'horizontalalignment', 'right'); +% annotation('textbox', 'position', [0.025 0.935 0.5 0.05], 'String', '\bf\fontsize{14}Priors',... +% 'linestyle', 'none', 'horizontalalignment', 'left'); +% curDir = cd; +% annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... +% 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +% colormap jet +% orient(gcf, 'tall'); +% orient(gcf, 'landscape'); +% drawnow; + +% +% [~, aPriorAllPostRaw] = CalculatePostProb(corrAisMtx, nonAIStrials, binSize); +% [~, bPriorAllPostRaw] = CalculatePostProb(corrBisMtx, nonAIStrials, binSize); +% [~, cPriorAllPostRaw] = CalculatePostProb(corrCisMtx, nonAIStrials, binSize); +% [~, dPriorAllPostRaw] = CalculatePostProb(corrDisMtx, nonAIStrials, binSize); +% nonFISaLog = perfLog & inSeqLog & odorAlog & ~fullInSeqLog; +% nonFISbLog = perfLog & inSeqLog & odorBlog & ~fullInSeqLog; +% nonFIScLog = perfLog & inSeqLog & odorClog & ~fullInSeqLog; +% nonFISdLog = perfLog & inSeqLog & odorDlog & ~fullInSeqLog; +% +% allNonFISpost = [aPriorAllPostRaw, bPriorAllPostRaw, cPriorAllPostRaw, dPriorAllPostRaw]; +% for c = 1:size(allNonFISpost,1) +% allNonFISpost(c,:,:) = allNonFISpost(c,:,:)./max(allNonFISpost(c,:,:)); +% end +% +% aPriorAllPostNorm = allNonFISpost(:,1:size(aPriorAllPostRaw,1),:); +% bPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)+1:size(aPriorAllPostRaw,1)*2,:); +% cPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)*2+1:size(aPriorAllPostRaw,1)*3,:); +% dPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)*3+1:end,:); +% +% figure; +% cAx = nan(4,4,2); +% for prior = 1:4 +% switch prior +% case 1 +% curPrior = aPriorAllPostNorm; +% case 2 +% curPrior = bPriorAllPostNorm; +% case 3 +% curPrior = cPriorAllPostNorm; +% case 4 +% curPrior = dPriorAllPostNorm; +% end +% for post = 1:4 +% curPostLog = nonAISodors == post; +% curDecode = curPrior(:,:,curPostLog); +% subplot(4,4,sub2ind([4 4], post, prior)) +% cAx(prior,post,:) = PlotPostMtx(trialTimes, curDecode, sprintf('Prior%i; Decode%i', prior, post)); +% end +% end +% cAxs = [min(min(cAx(:,:,1))), max(max(cAx(:,:,2)))/3]; +% +% annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... +% 'linestyle', 'none', 'horizontalalignment', 'right'); +% annotation('textbox', 'position', [0.025 0.935 0.5 0.05], 'String', '\bf\fontsize{14}Decoding Trial and Odor Across Odors',... +% 'linestyle', 'none', 'horizontalalignment', 'left'); +% curDir = cd; +% annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... +% 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +% colormap jet +% axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +% axis(axesHandles,'square') +% set(axesHandles, 'clim', cAxs); +% orient(gcf, 'tall'); +% orient(gcf, 'landscape'); +% drawnow; %% figure @@ -278,20 +278,22 @@ colormap jet axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); axis(axesHandles,'square') +cLims = cell2mat(get(axesHandles, 'cLim')); +set(axesHandles, 'cLim', [min(cLims(:,1)), max(cLims(:,2))]); orient(gcf, 'tall'); orient(gcf, 'landscape'); drawnow; -%% -figure; -for tr = 1:size(post,3) - imagesc(trialTimes, trialTimes, post(:,:,tr), [0 1]); - title(num2str(nonAISodors(tr))); - set(gca, 'ydir', 'normal'); - pause(1); -end +%% Comment in if you want to display each trial individually +% figure; +% for tr = 1:size(post,3) +% imagesc(trialTimes, trialTimes, post(:,:,tr), [0 1]); +% title(num2str(nonAISodors(tr))); +% set(gca, 'ydir', 'normal'); +% pause(1); +% end %% function cAx = PlotPostMtx(trialTimes, postMtx, id) From 92e6d7584f4daf9d90eabade9174ae87489aeeb9 Mon Sep 17 00:00:00 2001 From: gaelias Date: Mon, 5 Aug 2019 15:59:59 -0700 Subject: [PATCH 021/133] Update BayesPFCbsPROTO.m --- Analyses/Prototypes/BayesPFCbsPROTO.m | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Analyses/Prototypes/BayesPFCbsPROTO.m b/Analyses/Prototypes/BayesPFCbsPROTO.m index 0bb5e7f..69da1bc 100644 --- a/Analyses/Prototypes/BayesPFCbsPROTO.m +++ b/Analyses/Prototypes/BayesPFCbsPROTO.m @@ -244,7 +244,11 @@ drawnow; %% Compare Trial Before with Trial After OutSeq Trial +nonAIStrials = spkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog); +nonAIStrialData = trialPeriodTD(perfLog & inSeqLog & ~fullInSeqLog); +nonAISodors = [nonAIStrialData.Odor]; nonAIStrialNums = [nonAIStrialData.TrialNum]; + preOStrial = false(size(nonAIStrialNums)); postOStrial = false(size(nonAIStrialNums)); for trl = 1:length(nonAIStrialNums) @@ -256,19 +260,19 @@ if curTrl.Position - prevTrial.Position == 1 && prevTrial.TranspositionDistance ~= 0 postOStrial(trl) = true; preOStrial(trl-2) = true; -% elseif nextTrial.Position - curTrl.Position == 1 && nextTrial.TranspositionDistance ~=0 && nextTrial.Performance == 1 -% preOStrial(trl) = true; end end end end +postPostOS = post(:,:,postOStrial); +postPreOS = post(:,:,preOStrial); figure; subplot(1,2,1) -PlotPostMtx(trialTimes, post(:,:,preOStrial), 'Trial Before OutSeq'); +PlotPostMtx(trialTimes, postPreOS, 'Trial Before OutSeq'); subplot(1,2,2) -PlotPostMtx(trialTimes, post(:,:,postOStrial), 'Trial After OutSeq'); +PlotPostMtx(trialTimes, postPostOS, 'Trial After OutSeq'); annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... 'linestyle', 'none', 'horizontalalignment', 'right'); From b8bdbfd526db09f7c2a482bdac33ac1d5f930c20 Mon Sep 17 00:00:00 2001 From: gaelias Date: Mon, 5 Aug 2019 17:21:42 -0700 Subject: [PATCH 022/133] Create BayesPFCbsPROTO_2.m --- Analyses/Prototypes/BayesPFCbsPROTO_2.m | 198 ++++++++++++++++++++++++ 1 file changed, 198 insertions(+) create mode 100644 Analyses/Prototypes/BayesPFCbsPROTO_2.m diff --git a/Analyses/Prototypes/BayesPFCbsPROTO_2.m b/Analyses/Prototypes/BayesPFCbsPROTO_2.m new file mode 100644 index 0000000..d8952d9 --- /dev/null +++ b/Analyses/Prototypes/BayesPFCbsPROTO_2.m @@ -0,0 +1,198 @@ +%% +clc +clear all + +%% Runtime variables +binSize = 250; +dsRate = 5; + +%% +smPath = uigetdir; +cd(smPath); +files = dir(smPath); +fileNames = {files.name}; +% Load the behavior matrix file for poke events plots +behMatFile = fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'BehaviorMatrix'))}; +nsmblMatFile = fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'EnsembleMatrix'))}; +load([smPath '\' behMatFile]); +load([smPath '\' nsmblMatFile]); +% Identify list of all statMatrix files +smFileList = fileNames(cellfun(@(a)~isempty(a), regexp(fileNames, '_SM\>')))'; + +%% Extract Behavioral Periods +% Taking 1/2 the binSize on either end to get rid of edge effects. +pokeInTrialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.5-(binSize/2/1000) 1.5+(binSize/2/1000)], 'PokeIn'); +pokeInTimes = behavMatrix(pokeInTrialPeriodTD(1).TrialLogVect,1) - behavMatrix(pokeInTrialPeriodTD(1).PokeInIndex,1); +pokeOutTrialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-1.5-(binSize/2/1000) 0.5+(binSize/2/1000)], 'PokeOut'); +pokeOutTimes = behavMatrix(pokeOutTrialPeriodTD(1).TrialLogVect,1) - behavMatrix(pokeOutTrialPeriodTD(1).PokeOutIndex,1); + +pokeInEnsemble = ExtractTrialData_SM(pokeInTrialPeriodTD, ensembleMatrix(:,2:end)); %#ok<*NODEF> +pokeInEnsembleMtx = cell2mat(reshape(pokeInEnsemble, [1 1 length(pokeInEnsemble)])); +pokeOutEnsemble = ExtractTrialData_SM(pokeOutTrialPeriodTD, ensembleMatrix(:,2:end)); %#ok<*NODEF> +pokeOutEnsembleMtx = cell2mat(reshape(pokeOutEnsemble, [1 1 length(pokeOutEnsemble)])); + + +%% Bin the spiking data +% First convolve the entire trialEnsembleMtx with a square to bin the +% spikes +pokeInBinnedEnsembleMtx = nan(size(pokeInEnsembleMtx)); +pokeOutBinnedEnsembleMtx = nan(size(pokeOutEnsembleMtx)); +for t = 1:size(pokeInEnsembleMtx,3) + for u = 1:size(pokeInEnsembleMtx,2) + pokeInBinnedEnsembleMtx(:,u,t) = conv(pokeInEnsembleMtx(:,u,t), ones(1,binSize)./(binSize/1000), 'same'); + pokeOutBinnedEnsembleMtx(:,u,t) = conv(pokeOutEnsembleMtx(:,u,t), ones(1,binSize)./(binSize/1000), 'same'); + end +end +% Now remove the binSize/2 padding +unPaddedPokeInBinnedEnsembleMtx = pokeInBinnedEnsembleMtx((binSize/2)+1:end-(binSize/2),:,:); +pokeInTimesUnpadded = pokeInTimes((binSize/2)+1:end-(binSize/2)); +unPaddedPokeOutBinnedEnsembleMtx = pokeOutBinnedEnsembleMtx((binSize/2)+1:end-(binSize/2),:,:); +pokeOutTimesUnpadded = pokeOutTimes((binSize/2)+1:end-(binSize/2)); +% Now downsample the binned matrix +dsVect = downsample(1:size(unPaddedPokeInBinnedEnsembleMtx,1), dsRate); +pokeInSpikeMatrix = unPaddedPokeInBinnedEnsembleMtx(dsVect,:,:); +pokeOutSpikeMatrix = unPaddedPokeOutBinnedEnsembleMtx(dsVect,:,:); +trialTimePI = pokeInTimesUnpadded(dsVect); +trialTimePO = pokeOutTimesUnpadded(dsVect); + +%% Create Logical Vectors +perfLog = [pokeInTrialPeriodTD.Performance]; +inSeqLog = [pokeInTrialPeriodTD.TranspositionDistance]==0; +outSeqLog = [pokeInTrialPeriodTD.TranspositionDistance]~=0 & abs([pokeInTrialPeriodTD.TranspositionDistance])<10; +odorAlog = [pokeInTrialPeriodTD.Odor] == 1; +odorBlog = [pokeInTrialPeriodTD.Odor] == 2; +odorClog = [pokeInTrialPeriodTD.Odor] == 3; +odorDlog = [pokeInTrialPeriodTD.Odor] == 4; + +fullInSeqSeqsStart = find(conv([pokeInTrialPeriodTD.Odor], 1:4, 'valid')==20 & conv([pokeInTrialPeriodTD.Position], 1:4, 'valid')==20 & conv([pokeInTrialPeriodTD.Performance]*1, ones(1,4), 'valid')==4); +inSeqSeqs = nan(3,length(fullInSeqSeqsStart)); +for iS = 1:length(fullInSeqSeqsStart) + inSeqSeqs(1,iS) = fullInSeqSeqsStart(iS); + inSeqSeqs(2,iS) = fullInSeqSeqsStart(iS) + 1; + inSeqSeqs(3,iS) = fullInSeqSeqsStart(iS) + 2; + inSeqSeqs(4,iS) = fullInSeqSeqsStart(iS) + 3; +end +fullInSeqLog = false(1,length(pokeInTrialPeriodTD)); +fullInSeqLog(inSeqSeqs(:)) = true; + +%% +uniFRthreshLog = max([mean(pokeInSpikeMatrix,3); mean(pokeOutSpikeMatrix,3)])<1; +pokeInSpkMtx = pokeInSpikeMatrix; +pokeInSpkMtx(:,uniFRthreshLog,:) = []; +pokeOutSpkMtx = pokeOutSpikeMatrix; +pokeOutSpkMtx(:,uniFRthreshLog,:) = []; +goodUniNames = {ensembleUnitSummaries(~uniFRthreshLog).UnitName}; + +%% +figure +corrISmtxPI = mean(pokeInSpkMtx(:,:,perfLog & fullInSeqLog),3); % All InSeq Correct Trials +[postPI] = CalculatePostProb(corrISmtxPI, pokeInSpkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog), binSize); +subplot(4,4,[1,2,5,6]) +PlotPostMtx(trialTimePI, postPI, 'InSeq Correct Trials (Poke In)'); +set(gca, 'clim', [0 0.5]); + +corrISmtxPO = mean(pokeOutSpkMtx(:,:,perfLog & fullInSeqLog),3); +[postPO] = CalculatePostProb(corrISmtxPO, pokeOutSpkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog), binSize); +subplot(4,4,[9,10,13,14]) +PlotPostMtx(trialTimePO, postPO, 'InSeq Correct Trials (Poke Out)'); +set(gca, 'clim', [0 0.5]); + + +nonAIStrialData = pokeInTrialPeriodTD(perfLog & inSeqLog & ~fullInSeqLog); +nonAISodors = [nonAIStrialData.Odor]; +nonAIStrialNums = [nonAIStrialData.TrialNum]; + +preOStrialLog = false(size(nonAIStrialNums)); +postOStrialLog = false(size(nonAIStrialNums)); +for trl = 1:length(nonAIStrialNums) + if nonAIStrialNums(trl)~=1 && nonAIStrialNums(trl) ~= length(pokeInTrialPeriodTD) + prevTrial = pokeInTrialPeriodTD(nonAIStrialNums(trl)-1); + curTrl = pokeInTrialPeriodTD(nonAIStrialNums(trl)); + nextTrial = pokeInTrialPeriodTD(nonAIStrialNums(trl)+1); + if curTrl.Position ~= 1 + if curTrl.Position - prevTrial.Position == 1 && prevTrial.TranspositionDistance ~= 0 + postOStrialLog(trl) = true; + preOStrialLog(trl-2) = true; + end + end + end +end + +preOSpi = postPI(:,:,preOStrialLog); +subplot(4,4,3) +PlotPostMtx(trialTimePI, preOSpi, 'Before OutSeq'); +set(gca, 'clim', [0 0.5]); +postOSpi = postPI(:,:,postOStrialLog); +subplot(4,4,7) +PlotPostMtx(trialTimePI, postOSpi, 'After OutSeq'); +set(gca, 'clim', [0 0.5]); +postPreDiffPI = postOSpi - preOSpi; +subplot(4,4,4) +PlotPostMtx(trialTimePI, postPreDiffPI, 'After-Before'); +set(gca, 'clim', [-0.25 0.25]); + + +preOSpo = postPO(:,:,preOStrialLog); +subplot(4,4,11) +PlotPostMtx(trialTimePO, preOSpo, 'Before OutSeq'); +set(gca, 'clim', [0 0.5]); +postOSpo = postPO(:,:,postOStrialLog); +subplot(4,4,15) +PlotPostMtx(trialTimePO, postOSpo, 'After OutSeq'); +set(gca, 'clim', [0 0.5]); +postPreDiffPO = postOSpo - preOSpo; +subplot(4,4,12) +PlotPostMtx(trialTimePO, postPreDiffPO, 'After-Before'); +set(gca, 'clim', [-0.25 0.25]); + +annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... + 'linestyle', 'none', 'horizontalalignment', 'right'); +curDir = cd; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +colormap jet +axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +axis(axesHandles,'square') +orient(gcf, 'tall'); +orient(gcf, 'landscape'); +drawnow; + + + + +%% +function cAx = PlotPostMtx(trialTimes, postMtx, id) +imagesc(trialTimes, trialTimes, nanmean(postMtx,3)); +set(gca, 'ydir', 'normal') +title(id); +xlabel('True Time'); +ylabel('Decoded Time'); +drawnow +cAx = get(gca, 'clim'); +end + +%% +function [postNorm, postRaw] = CalculatePostProb(prior, obsv, binSize) +propVect = 1./sum(prior,2); +postNorm = nan(size(obsv,1), size(obsv,1), size(obsv,3)); +postRaw = nan(size(obsv,1), size(obsv,1), size(obsv,3)); +for trl = 1:size(obsv,3) + for t = 1:size(prior,1) + p = nan(size(prior)); +% e = nan(size(meanFR)); % I think this is wrong + curPopVect = obsv(t,:,trl); + curPopFact = factorial(curPopVect); + for u = 1:size(prior,2) + curAvgUniFR = prior(:,u); + p(:,u) = (((binSize/1000)*curAvgUniFR).^curPopVect(u))./curPopFact(u); +% e(:,u) = exp(-(binSize/1000*curAvgUniFR)); % I think this is wrong + end + pp = prod(p,2); +% ee = sum(e,2); % I think this is wrong + ee = exp(-(binSize/1000*sum(prior,2))); + tempPost = propVect.*pp.*ee; + postRaw(t,:,trl) = tempPost; + postNorm(t,:,trl) = tempPost./max(tempPost); + end +end +end \ No newline at end of file From 684f88557bec48173e94efff25a361045ede0867 Mon Sep 17 00:00:00 2001 From: gaelias Date: Mon, 5 Aug 2019 17:21:51 -0700 Subject: [PATCH 023/133] Update BayesPFCbsPROTO.m --- Analyses/Prototypes/BayesPFCbsPROTO.m | 1 + 1 file changed, 1 insertion(+) diff --git a/Analyses/Prototypes/BayesPFCbsPROTO.m b/Analyses/Prototypes/BayesPFCbsPROTO.m index 69da1bc..a0a7ee8 100644 --- a/Analyses/Prototypes/BayesPFCbsPROTO.m +++ b/Analyses/Prototypes/BayesPFCbsPROTO.m @@ -41,6 +41,7 @@ end % Now remove the binSize/2 padding unPaddedBinnedEnsembleMtx = binnedEnsembleMtx((binSize/2)+1:end-(binSize/2),:,:); +trialTimes = trialTimes((binSize/2)+1:end-(binSize/2)); % Now downsample the binned matrix dsVect = downsample(1:size(unPaddedBinnedEnsembleMtx,1), dsRate); spikeMatrix = unPaddedBinnedEnsembleMtx(dsVect,:,:); From a61e087384f260de3b4deb7a2652bf220fe11227 Mon Sep 17 00:00:00 2001 From: gaelias Date: Mon, 5 Aug 2019 17:59:54 -0700 Subject: [PATCH 024/133] Update BayesPFCbsPROTO_2.m --- Analyses/Prototypes/BayesPFCbsPROTO_2.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Analyses/Prototypes/BayesPFCbsPROTO_2.m b/Analyses/Prototypes/BayesPFCbsPROTO_2.m index d8952d9..8a631f4 100644 --- a/Analyses/Prototypes/BayesPFCbsPROTO_2.m +++ b/Analyses/Prototypes/BayesPFCbsPROTO_2.m @@ -86,13 +86,13 @@ %% figure corrISmtxPI = mean(pokeInSpkMtx(:,:,perfLog & fullInSeqLog),3); % All InSeq Correct Trials -[postPI] = CalculatePostProb(corrISmtxPI, pokeInSpkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog), binSize); +[postPI] = CalculatePostProb(corrISmtxPI, pokeInSpkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog & ~odorAlog), binSize); subplot(4,4,[1,2,5,6]) PlotPostMtx(trialTimePI, postPI, 'InSeq Correct Trials (Poke In)'); set(gca, 'clim', [0 0.5]); corrISmtxPO = mean(pokeOutSpkMtx(:,:,perfLog & fullInSeqLog),3); -[postPO] = CalculatePostProb(corrISmtxPO, pokeOutSpkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog), binSize); +[postPO] = CalculatePostProb(corrISmtxPO, pokeOutSpkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog & ~odorAlog), binSize); subplot(4,4,[9,10,13,14]) PlotPostMtx(trialTimePO, postPO, 'InSeq Correct Trials (Poke Out)'); set(gca, 'clim', [0 0.5]); From 895bcd6405ee0a6d435c8b0814774868d8971490 Mon Sep 17 00:00:00 2001 From: gaelias Date: Mon, 5 Aug 2019 18:00:06 -0700 Subject: [PATCH 025/133] Update BayesPFCbsPROTO.m --- Analyses/Prototypes/BayesPFCbsPROTO.m | 230 +++++++++++++------------- 1 file changed, 115 insertions(+), 115 deletions(-) diff --git a/Analyses/Prototypes/BayesPFCbsPROTO.m b/Analyses/Prototypes/BayesPFCbsPROTO.m index a0a7ee8..7c0ce9e 100644 --- a/Analyses/Prototypes/BayesPFCbsPROTO.m +++ b/Analyses/Prototypes/BayesPFCbsPROTO.m @@ -109,122 +109,122 @@ % orient(gcf, 'landscape'); %% Decode Trial Time and Odor Across Odors -% nonAIStrials = spkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog); -% nonAIStrialData = trialPeriodTD(perfLog & inSeqLog & ~fullInSeqLog); -% nonAISodors = [nonAIStrialData.Odor]; -% corrAisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorAlog),3); % All A InSeq Correct Trials Prior -% corrBisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorBlog),3); % All B InSeq Correct Trials Prior -% corrCisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorClog),3); % All C InSeq Correct Trials Prior -% corrDisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorDlog),3); % All D InSeq Correct Trials Prior -% -% figure; -% oAfr = subplot(4,4,1); -% imagesc(trialTimes, 1:size(spkMtx,2), corrAisMtx'); -% title('Odor A'); -% abCorr = subplot(4,4,2); -% corrScatPlot(corrAisMtx(:),corrBisMtx(:), 'A','B', 'markerStyle', '.', 'markerColor', 'k'); -% acCorr = subplot(4,4,3); -% corrScatPlot(corrAisMtx(:),corrCisMtx(:), 'A','C', 'markerStyle', '.', 'markerColor', 'k'); -% adCorr = subplot(4,4,4); -% corrScatPlot(corrAisMtx(:),corrDisMtx(:), 'A','B', 'markerStyle', '.', 'markerColor', 'k'); -% -% oBfr = subplot(4,4,6); -% imagesc(trialTimes, 1:size(spkMtx,2), corrBisMtx'); -% title('Odor B'); -% bcCorr = subplot(4,4,7); -% corrScatPlot(corrBisMtx(:),corrCisMtx(:), 'B','C', 'markerStyle', '.', 'markerColor', 'k'); -% bdCorr = subplot(4,4,8); -% corrScatPlot(corrBisMtx(:),corrDisMtx(:), 'B','D', 'markerStyle', '.', 'markerColor', 'k'); -% -% oCfr = subplot(4,4,11); -% imagesc(trialTimes, 1:size(spkMtx,2), corrCisMtx'); -% title('Odor C'); -% cdCorr = subplot(4,4,12); -% corrScatPlot(corrCisMtx(:),corrDisMtx(:), 'C','D', 'markerStyle', '.', 'markerColor', 'k'); -% -% oDfr = subplot(4,4,16); -% imagesc(trialTimes, 1:size(spkMtx,2), corrDisMtx'); -% title('Odor D'); -% -% frMapSPs = [oAfr, oBfr, oCfr, oDfr]; -% curCL = cell2mat(get(frMapSPs, 'clim')); -% set(frMapSPs, 'clim', [min(curCL(:,1)), max(curCL(:,2))], 'ydir', 'normal'); -% corrSPs = [abCorr, acCorr, adCorr, bcCorr, bdCorr, cdCorr]; -% curXL = cell2mat(get(corrSPs, 'xlim')); -% curYL = cell2mat(get(corrSPs, 'ylim')); -% set(corrSPs, 'xlim', [0 max(curXL(:,2))], 'ylim', [0 max(curYL(:,2))]); -% -% axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); -% axis(axesHandles,'square'); -% -% annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... -% 'linestyle', 'none', 'horizontalalignment', 'right'); -% annotation('textbox', 'position', [0.025 0.935 0.5 0.05], 'String', '\bf\fontsize{14}Priors',... -% 'linestyle', 'none', 'horizontalalignment', 'left'); -% curDir = cd; -% annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... -% 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); -% colormap jet -% orient(gcf, 'tall'); -% orient(gcf, 'landscape'); -% drawnow; +nonAIStrials = spkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog); +nonAIStrialData = trialPeriodTD(perfLog & inSeqLog & ~fullInSeqLog); +nonAISodors = [nonAIStrialData.Odor]; +corrAisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorAlog),3); % All A InSeq Correct Trials Prior +corrBisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorBlog),3); % All B InSeq Correct Trials Prior +corrCisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorClog),3); % All C InSeq Correct Trials Prior +corrDisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorDlog),3); % All D InSeq Correct Trials Prior -% -% [~, aPriorAllPostRaw] = CalculatePostProb(corrAisMtx, nonAIStrials, binSize); -% [~, bPriorAllPostRaw] = CalculatePostProb(corrBisMtx, nonAIStrials, binSize); -% [~, cPriorAllPostRaw] = CalculatePostProb(corrCisMtx, nonAIStrials, binSize); -% [~, dPriorAllPostRaw] = CalculatePostProb(corrDisMtx, nonAIStrials, binSize); -% nonFISaLog = perfLog & inSeqLog & odorAlog & ~fullInSeqLog; -% nonFISbLog = perfLog & inSeqLog & odorBlog & ~fullInSeqLog; -% nonFIScLog = perfLog & inSeqLog & odorClog & ~fullInSeqLog; -% nonFISdLog = perfLog & inSeqLog & odorDlog & ~fullInSeqLog; -% -% allNonFISpost = [aPriorAllPostRaw, bPriorAllPostRaw, cPriorAllPostRaw, dPriorAllPostRaw]; -% for c = 1:size(allNonFISpost,1) -% allNonFISpost(c,:,:) = allNonFISpost(c,:,:)./max(allNonFISpost(c,:,:)); -% end -% -% aPriorAllPostNorm = allNonFISpost(:,1:size(aPriorAllPostRaw,1),:); -% bPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)+1:size(aPriorAllPostRaw,1)*2,:); -% cPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)*2+1:size(aPriorAllPostRaw,1)*3,:); -% dPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)*3+1:end,:); -% -% figure; -% cAx = nan(4,4,2); -% for prior = 1:4 -% switch prior -% case 1 -% curPrior = aPriorAllPostNorm; -% case 2 -% curPrior = bPriorAllPostNorm; -% case 3 -% curPrior = cPriorAllPostNorm; -% case 4 -% curPrior = dPriorAllPostNorm; -% end -% for post = 1:4 -% curPostLog = nonAISodors == post; -% curDecode = curPrior(:,:,curPostLog); -% subplot(4,4,sub2ind([4 4], post, prior)) -% cAx(prior,post,:) = PlotPostMtx(trialTimes, curDecode, sprintf('Prior%i; Decode%i', prior, post)); -% end -% end -% cAxs = [min(min(cAx(:,:,1))), max(max(cAx(:,:,2)))/3]; -% -% annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... -% 'linestyle', 'none', 'horizontalalignment', 'right'); -% annotation('textbox', 'position', [0.025 0.935 0.5 0.05], 'String', '\bf\fontsize{14}Decoding Trial and Odor Across Odors',... -% 'linestyle', 'none', 'horizontalalignment', 'left'); -% curDir = cd; -% annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... -% 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); -% colormap jet -% axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); -% axis(axesHandles,'square') -% set(axesHandles, 'clim', cAxs); -% orient(gcf, 'tall'); -% orient(gcf, 'landscape'); -% drawnow; +figure; +oAfr = subplot(4,4,1); +imagesc(trialTimes, 1:size(spkMtx,2), corrAisMtx'); +title('Odor A'); +abCorr = subplot(4,4,2); +corrScatPlot(corrAisMtx(:),corrBisMtx(:), 'A','B', 'markerStyle', '.', 'markerColor', 'k'); +acCorr = subplot(4,4,3); +corrScatPlot(corrAisMtx(:),corrCisMtx(:), 'A','C', 'markerStyle', '.', 'markerColor', 'k'); +adCorr = subplot(4,4,4); +corrScatPlot(corrAisMtx(:),corrDisMtx(:), 'A','B', 'markerStyle', '.', 'markerColor', 'k'); + +oBfr = subplot(4,4,6); +imagesc(trialTimes, 1:size(spkMtx,2), corrBisMtx'); +title('Odor B'); +bcCorr = subplot(4,4,7); +corrScatPlot(corrBisMtx(:),corrCisMtx(:), 'B','C', 'markerStyle', '.', 'markerColor', 'k'); +bdCorr = subplot(4,4,8); +corrScatPlot(corrBisMtx(:),corrDisMtx(:), 'B','D', 'markerStyle', '.', 'markerColor', 'k'); + +oCfr = subplot(4,4,11); +imagesc(trialTimes, 1:size(spkMtx,2), corrCisMtx'); +title('Odor C'); +cdCorr = subplot(4,4,12); +corrScatPlot(corrCisMtx(:),corrDisMtx(:), 'C','D', 'markerStyle', '.', 'markerColor', 'k'); + +oDfr = subplot(4,4,16); +imagesc(trialTimes, 1:size(spkMtx,2), corrDisMtx'); +title('Odor D'); + +frMapSPs = [oAfr, oBfr, oCfr, oDfr]; +curCL = cell2mat(get(frMapSPs, 'clim')); +set(frMapSPs, 'clim', [min(curCL(:,1)), max(curCL(:,2))], 'ydir', 'normal'); +corrSPs = [abCorr, acCorr, adCorr, bcCorr, bdCorr, cdCorr]; +curXL = cell2mat(get(corrSPs, 'xlim')); +curYL = cell2mat(get(corrSPs, 'ylim')); +set(corrSPs, 'xlim', [0 max(curXL(:,2))], 'ylim', [0 max(curYL(:,2))]); + +axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +axis(axesHandles,'square'); + +annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... + 'linestyle', 'none', 'horizontalalignment', 'right'); +annotation('textbox', 'position', [0.025 0.935 0.5 0.05], 'String', '\bf\fontsize{14}Priors',... + 'linestyle', 'none', 'horizontalalignment', 'left'); +curDir = cd; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +colormap jet +orient(gcf, 'tall'); +orient(gcf, 'landscape'); +drawnow; + + +[~, aPriorAllPostRaw] = CalculatePostProb(corrAisMtx, nonAIStrials, binSize); +[~, bPriorAllPostRaw] = CalculatePostProb(corrBisMtx, nonAIStrials, binSize); +[~, cPriorAllPostRaw] = CalculatePostProb(corrCisMtx, nonAIStrials, binSize); +[~, dPriorAllPostRaw] = CalculatePostProb(corrDisMtx, nonAIStrials, binSize); +nonFISaLog = perfLog & inSeqLog & odorAlog & ~fullInSeqLog; +nonFISbLog = perfLog & inSeqLog & odorBlog & ~fullInSeqLog; +nonFIScLog = perfLog & inSeqLog & odorClog & ~fullInSeqLog; +nonFISdLog = perfLog & inSeqLog & odorDlog & ~fullInSeqLog; + +allNonFISpost = [aPriorAllPostRaw, bPriorAllPostRaw, cPriorAllPostRaw, dPriorAllPostRaw]; +for c = 1:size(allNonFISpost,1) + allNonFISpost(c,:,:) = allNonFISpost(c,:,:)./max(allNonFISpost(c,:,:)); +end + +aPriorAllPostNorm = allNonFISpost(:,1:size(aPriorAllPostRaw,1),:); +bPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)+1:size(aPriorAllPostRaw,1)*2,:); +cPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)*2+1:size(aPriorAllPostRaw,1)*3,:); +dPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)*3+1:end,:); + +figure; +cAx = nan(4,4,2); +for prior = 1:4 + switch prior + case 1 + curPrior = aPriorAllPostNorm; + case 2 + curPrior = bPriorAllPostNorm; + case 3 + curPrior = cPriorAllPostNorm; + case 4 + curPrior = dPriorAllPostNorm; + end + for post = 1:4 + curPostLog = nonAISodors == post; + curDecode = curPrior(:,:,curPostLog); + subplot(4,4,sub2ind([4 4], post, prior)) + cAx(prior,post,:) = PlotPostMtx(trialTimes, curDecode, sprintf('Prior%i; Decode%i', prior, post)); + end +end +cAxs = [min(min(cAx(:,:,1))), max(max(cAx(:,:,2)))/3]; + +annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... + 'linestyle', 'none', 'horizontalalignment', 'right'); +annotation('textbox', 'position', [0.025 0.935 0.5 0.05], 'String', '\bf\fontsize{14}Decoding Trial and Odor Across Odors',... + 'linestyle', 'none', 'horizontalalignment', 'left'); +curDir = cd; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +colormap jet +axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +axis(axesHandles,'square') +set(axesHandles, 'clim', cAxs); +orient(gcf, 'tall'); +orient(gcf, 'landscape'); +drawnow; %% figure From ddb53a18628e9edfc9c2dcb2ded3bb74fb1e4e7a Mon Sep 17 00:00:00 2001 From: gabe Date: Mon, 5 Aug 2019 18:36:01 -0700 Subject: [PATCH 026/133] Update BayesPFCbsPROTO_2.m --- Analyses/Prototypes/BayesPFCbsPROTO_2.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Analyses/Prototypes/BayesPFCbsPROTO_2.m b/Analyses/Prototypes/BayesPFCbsPROTO_2.m index 8a631f4..d8952d9 100644 --- a/Analyses/Prototypes/BayesPFCbsPROTO_2.m +++ b/Analyses/Prototypes/BayesPFCbsPROTO_2.m @@ -86,13 +86,13 @@ %% figure corrISmtxPI = mean(pokeInSpkMtx(:,:,perfLog & fullInSeqLog),3); % All InSeq Correct Trials -[postPI] = CalculatePostProb(corrISmtxPI, pokeInSpkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog & ~odorAlog), binSize); +[postPI] = CalculatePostProb(corrISmtxPI, pokeInSpkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog), binSize); subplot(4,4,[1,2,5,6]) PlotPostMtx(trialTimePI, postPI, 'InSeq Correct Trials (Poke In)'); set(gca, 'clim', [0 0.5]); corrISmtxPO = mean(pokeOutSpkMtx(:,:,perfLog & fullInSeqLog),3); -[postPO] = CalculatePostProb(corrISmtxPO, pokeOutSpkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog & ~odorAlog), binSize); +[postPO] = CalculatePostProb(corrISmtxPO, pokeOutSpkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog), binSize); subplot(4,4,[9,10,13,14]) PlotPostMtx(trialTimePO, postPO, 'InSeq Correct Trials (Poke Out)'); set(gca, 'clim', [0 0.5]); From d7619f3612987a2bdb318a10a27dc6f2886b3c1f Mon Sep 17 00:00:00 2001 From: gabe Date: Mon, 5 Aug 2019 18:50:47 -0700 Subject: [PATCH 027/133] Update BayesPFCbsPROTO.m --- Analyses/Prototypes/BayesPFCbsPROTO.m | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Analyses/Prototypes/BayesPFCbsPROTO.m b/Analyses/Prototypes/BayesPFCbsPROTO.m index 7c0ce9e..a43ae31 100644 --- a/Analyses/Prototypes/BayesPFCbsPROTO.m +++ b/Analyses/Prototypes/BayesPFCbsPROTO.m @@ -303,8 +303,10 @@ %% function cAx = PlotPostMtx(trialTimes, postMtx, id) imagesc(trialTimes, trialTimes, nanmean(postMtx,3)); -set(gca, 'ydir', 'normal') title(id); +set(gca, 'ydir', 'normal') +hold on; +line(trialTimes, trialTimes, 'linestyle', ':', 'color', 'w', 'linewidth', 2); xlabel('True Time'); ylabel('Decoded Time'); drawnow From aa2cda6e585f487cc7f385a43f586d3c979d7ed3 Mon Sep 17 00:00:00 2001 From: gabe Date: Mon, 5 Aug 2019 18:50:53 -0700 Subject: [PATCH 028/133] Update BayesPFCbsPROTO_2.m --- Analyses/Prototypes/BayesPFCbsPROTO_2.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Analyses/Prototypes/BayesPFCbsPROTO_2.m b/Analyses/Prototypes/BayesPFCbsPROTO_2.m index d8952d9..5c15b39 100644 --- a/Analyses/Prototypes/BayesPFCbsPROTO_2.m +++ b/Analyses/Prototypes/BayesPFCbsPROTO_2.m @@ -164,6 +164,8 @@ function cAx = PlotPostMtx(trialTimes, postMtx, id) imagesc(trialTimes, trialTimes, nanmean(postMtx,3)); set(gca, 'ydir', 'normal') +hold on; +line(trialTimes, trialTimes, 'linestyle', ':', 'color', 'w', 'linewidth', 2); title(id); xlabel('True Time'); ylabel('Decoded Time'); From 743e01adffbef1e5b2e77e78b23d29ef7aa4c048 Mon Sep 17 00:00:00 2001 From: gaelias Date: Tue, 6 Aug 2019 16:02:38 -0700 Subject: [PATCH 029/133] Update BayesPFCbsPROTO_2.m --- Analyses/Prototypes/BayesPFCbsPROTO_2.m | 78 +++++++++++++++++-------- 1 file changed, 54 insertions(+), 24 deletions(-) diff --git a/Analyses/Prototypes/BayesPFCbsPROTO_2.m b/Analyses/Prototypes/BayesPFCbsPROTO_2.m index 5c15b39..0198d14 100644 --- a/Analyses/Prototypes/BayesPFCbsPROTO_2.m +++ b/Analyses/Prototypes/BayesPFCbsPROTO_2.m @@ -5,6 +5,8 @@ %% Runtime variables binSize = 250; dsRate = 5; +pokeInWindows = [-0.5 1.5]; +pokeOutWindows = [-1.5 0.5]; %% smPath = uigetdir; @@ -21,9 +23,14 @@ %% Extract Behavioral Periods % Taking 1/2 the binSize on either end to get rid of edge effects. -pokeInTrialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.5-(binSize/2/1000) 1.5+(binSize/2/1000)], 'PokeIn'); +% pokeInTrialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [pokeInWindows(1)-(binSize/2/1000) pokeInWindows(2)+(binSize/2/1000)], 'PokeIn'); +% pokeInTimes = behavMatrix(pokeInTrialPeriodTD(1).TrialLogVect,1) - behavMatrix(pokeInTrialPeriodTD(1).PokeInIndex,1); +% pokeOutTrialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [pokeOutWindows(1)-(binSize/2/1000) pokeOutWindows(2)+(binSize/2/1000)], 'PokeOut'); +% pokeOutTimes = behavMatrix(pokeOutTrialPeriodTD(1).TrialLogVect,1) - behavMatrix(pokeOutTrialPeriodTD(1).PokeOutIndex,1); + +pokeInTrialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [pokeInWindows(1)-(binSize/1000) pokeInWindows(2)+(binSize/1000)], 'PokeIn'); pokeInTimes = behavMatrix(pokeInTrialPeriodTD(1).TrialLogVect,1) - behavMatrix(pokeInTrialPeriodTD(1).PokeInIndex,1); -pokeOutTrialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-1.5-(binSize/2/1000) 0.5+(binSize/2/1000)], 'PokeOut'); +pokeOutTrialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [pokeOutWindows(1)-(binSize/1000) pokeOutWindows(2)+(binSize/1000)], 'PokeOut'); pokeOutTimes = behavMatrix(pokeOutTrialPeriodTD(1).TrialLogVect,1) - behavMatrix(pokeOutTrialPeriodTD(1).PokeOutIndex,1); pokeInEnsemble = ExtractTrialData_SM(pokeInTrialPeriodTD, ensembleMatrix(:,2:end)); %#ok<*NODEF> @@ -33,27 +40,53 @@ %% Bin the spiking data -% First convolve the entire trialEnsembleMtx with a square to bin the -% spikes -pokeInBinnedEnsembleMtx = nan(size(pokeInEnsembleMtx)); -pokeOutBinnedEnsembleMtx = nan(size(pokeOutEnsembleMtx)); -for t = 1:size(pokeInEnsembleMtx,3) - for u = 1:size(pokeInEnsembleMtx,2) - pokeInBinnedEnsembleMtx(:,u,t) = conv(pokeInEnsembleMtx(:,u,t), ones(1,binSize)./(binSize/1000), 'same'); - pokeOutBinnedEnsembleMtx(:,u,t) = conv(pokeOutEnsembleMtx(:,u,t), ones(1,binSize)./(binSize/1000), 'same'); +% % First convolve the entire trialEnsembleMtx with a square to bin the +% % spikes +% pokeInBinnedEnsembleMtx = nan(size(pokeInEnsembleMtx)); +% pokeOutBinnedEnsembleMtx = nan(size(pokeOutEnsembleMtx)); +% for t = 1:size(pokeInEnsembleMtx,3) +% for u = 1:size(pokeInEnsembleMtx,2) +% pokeInBinnedEnsembleMtx(:,u,t) = conv(pokeInEnsembleMtx(:,u,t), ones(1,binSize)./(binSize/1000), 'same'); +% pokeOutBinnedEnsembleMtx(:,u,t) = conv(pokeOutEnsembleMtx(:,u,t), ones(1,binSize)./(binSize/1000), 'same'); +% end +% end +% % Now remove the binSize/2 padding +% unPaddedPokeInBinnedEnsembleMtx = pokeInBinnedEnsembleMtx((binSize/2)+1:end-(binSize/2),:,:); +% pokeInTimesUnpadded = pokeInTimes((binSize/2)+1:end-(binSize/2)); +% unPaddedPokeOutBinnedEnsembleMtx = pokeOutBinnedEnsembleMtx((binSize/2)+1:end-(binSize/2),:,:); +% pokeOutTimesUnpadded = pokeOutTimes((binSize/2)+1:end-(binSize/2)); +% % Now downsample the binned matrix +% dsVect = downsample(1:size(unPaddedPokeInBinnedEnsembleMtx,1), dsRate); +% pokeInSpikeMatrix = unPaddedPokeInBinnedEnsembleMtx(dsVect,:,:); +% pokeOutSpikeMatrix = unPaddedPokeOutBinnedEnsembleMtx(dsVect,:,:); +% trialTimePI = pokeInTimesUnpadded(dsVect); +% trialTimePO = pokeOutTimesUnpadded(dsVect); + +%% Alternative way to bin the spike data +mansWin1 = 1:dsRate:size(pokeInEnsembleMtx,1)-binSize; +mansWin2 = mansWin1+binSize; + +pokeInBinnedEnsembleMtx = nan(length(mansWin1), size(pokeInEnsembleMtx,2), size(pokeInEnsembleMtx,3)); +pokeOutBinnedEnsembleMtx = nan(length(mansWin1), size(pokeOutEnsembleMtx,2), size(pokeOutEnsembleMtx,3)); +for trl = 1:size(pokeInEnsembleMtx,3) + for u = 1:size(pokeInEnsembleMtx,2) + curUniPI = pokeInEnsembleMtx(:,u,trl); + pokeInBinnedEnsembleMtx(:,u,trl) = cell2mat(arrayfun(@(a,b)sum(curUniPI(a:b)), mansWin1, mansWin2, 'uniformoutput', 0)); + curUniPO = pokeOutEnsembleMtx(:,u,trl); + pokeOutBinnedEnsembleMtx(:,u,trl) = cell2mat(arrayfun(@(a,b)sum(curUniPO(a:b)), mansWin1, mansWin2, 'uniformoutput', 0)); end end -% Now remove the binSize/2 padding -unPaddedPokeInBinnedEnsembleMtx = pokeInBinnedEnsembleMtx((binSize/2)+1:end-(binSize/2),:,:); -pokeInTimesUnpadded = pokeInTimes((binSize/2)+1:end-(binSize/2)); -unPaddedPokeOutBinnedEnsembleMtx = pokeOutBinnedEnsembleMtx((binSize/2)+1:end-(binSize/2),:,:); -pokeOutTimesUnpadded = pokeOutTimes((binSize/2)+1:end-(binSize/2)); -% Now downsample the binned matrix -dsVect = downsample(1:size(unPaddedPokeInBinnedEnsembleMtx,1), dsRate); -pokeInSpikeMatrix = unPaddedPokeInBinnedEnsembleMtx(dsVect,:,:); -pokeOutSpikeMatrix = unPaddedPokeOutBinnedEnsembleMtx(dsVect,:,:); -trialTimePI = pokeInTimesUnpadded(dsVect); -trialTimePO = pokeOutTimesUnpadded(dsVect); +pokeInBinnedEnsembleMtx = pokeInBinnedEnsembleMtx./(binSize/1000); +pokeOutBinnedEnsembleMtx = pokeOutBinnedEnsembleMtx./(binSize/1000); +pokeInTimesDS = pokeInTimes(mansWin1); +pokeInUnPadLog = pokeInTimesDS>=pokeInWindows(1) & pokeInTimesDS<=pokeInWindows(2); +trialTimePI = pokeInTimesDS(pokeInUnPadLog); +pokeInSpikeMatrix = pokeInBinnedEnsembleMtx(pokeInUnPadLog,:,:); +pokeOutTimesDS = pokeOutTimes(mansWin1); +pokeOutUnPadLog = pokeOutTimesDS>=pokeOutWindows(1) & pokeOutTimesDS<=pokeOutWindows(2); +trialTimePO = pokeOutTimesDS(pokeOutUnPadLog); +pokeOutSpikeMatrix = pokeOutBinnedEnsembleMtx(pokeOutUnPadLog,:,:); + %% Create Logical Vectors perfLog = [pokeInTrialPeriodTD.Performance]; @@ -181,16 +214,13 @@ for trl = 1:size(obsv,3) for t = 1:size(prior,1) p = nan(size(prior)); -% e = nan(size(meanFR)); % I think this is wrong curPopVect = obsv(t,:,trl); curPopFact = factorial(curPopVect); for u = 1:size(prior,2) curAvgUniFR = prior(:,u); p(:,u) = (((binSize/1000)*curAvgUniFR).^curPopVect(u))./curPopFact(u); -% e(:,u) = exp(-(binSize/1000*curAvgUniFR)); % I think this is wrong end pp = prod(p,2); -% ee = sum(e,2); % I think this is wrong ee = exp(-(binSize/1000*sum(prior,2))); tempPost = propVect.*pp.*ee; postRaw(t,:,trl) = tempPost; From 96fdda32a05ba52ed6e028ca81336344a825cd10 Mon Sep 17 00:00:00 2001 From: gaelias Date: Tue, 6 Aug 2019 16:27:44 -0700 Subject: [PATCH 030/133] Update BayesPFCbsPROTO_2.m --- Analyses/Prototypes/BayesPFCbsPROTO_2.m | 102 ++++++++++++------------ 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/Analyses/Prototypes/BayesPFCbsPROTO_2.m b/Analyses/Prototypes/BayesPFCbsPROTO_2.m index 0198d14..1856b64 100644 --- a/Analyses/Prototypes/BayesPFCbsPROTO_2.m +++ b/Analyses/Prototypes/BayesPFCbsPROTO_2.m @@ -23,16 +23,16 @@ %% Extract Behavioral Periods % Taking 1/2 the binSize on either end to get rid of edge effects. -% pokeInTrialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [pokeInWindows(1)-(binSize/2/1000) pokeInWindows(2)+(binSize/2/1000)], 'PokeIn'); -% pokeInTimes = behavMatrix(pokeInTrialPeriodTD(1).TrialLogVect,1) - behavMatrix(pokeInTrialPeriodTD(1).PokeInIndex,1); -% pokeOutTrialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [pokeOutWindows(1)-(binSize/2/1000) pokeOutWindows(2)+(binSize/2/1000)], 'PokeOut'); -% pokeOutTimes = behavMatrix(pokeOutTrialPeriodTD(1).TrialLogVect,1) - behavMatrix(pokeOutTrialPeriodTD(1).PokeOutIndex,1); - -pokeInTrialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [pokeInWindows(1)-(binSize/1000) pokeInWindows(2)+(binSize/1000)], 'PokeIn'); +pokeInTrialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [pokeInWindows(1)-(binSize/2/1000) pokeInWindows(2)+(binSize/2/1000)], 'PokeIn'); pokeInTimes = behavMatrix(pokeInTrialPeriodTD(1).TrialLogVect,1) - behavMatrix(pokeInTrialPeriodTD(1).PokeInIndex,1); -pokeOutTrialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [pokeOutWindows(1)-(binSize/1000) pokeOutWindows(2)+(binSize/1000)], 'PokeOut'); +pokeOutTrialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [pokeOutWindows(1)-(binSize/2/1000) pokeOutWindows(2)+(binSize/2/1000)], 'PokeOut'); pokeOutTimes = behavMatrix(pokeOutTrialPeriodTD(1).TrialLogVect,1) - behavMatrix(pokeOutTrialPeriodTD(1).PokeOutIndex,1); +% pokeInTrialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [pokeInWindows(1)-(binSize/1000) pokeInWindows(2)+(binSize/1000)], 'PokeIn'); +% pokeInTimes = behavMatrix(pokeInTrialPeriodTD(1).TrialLogVect,1) - behavMatrix(pokeInTrialPeriodTD(1).PokeInIndex,1); +% pokeOutTrialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [pokeOutWindows(1)-(binSize/1000) pokeOutWindows(2)+(binSize/1000)], 'PokeOut'); +% pokeOutTimes = behavMatrix(pokeOutTrialPeriodTD(1).TrialLogVect,1) - behavMatrix(pokeOutTrialPeriodTD(1).PokeOutIndex,1); + pokeInEnsemble = ExtractTrialData_SM(pokeInTrialPeriodTD, ensembleMatrix(:,2:end)); %#ok<*NODEF> pokeInEnsembleMtx = cell2mat(reshape(pokeInEnsemble, [1 1 length(pokeInEnsemble)])); pokeOutEnsemble = ExtractTrialData_SM(pokeOutTrialPeriodTD, ensembleMatrix(:,2:end)); %#ok<*NODEF> @@ -40,52 +40,52 @@ %% Bin the spiking data -% % First convolve the entire trialEnsembleMtx with a square to bin the -% % spikes -% pokeInBinnedEnsembleMtx = nan(size(pokeInEnsembleMtx)); -% pokeOutBinnedEnsembleMtx = nan(size(pokeOutEnsembleMtx)); -% for t = 1:size(pokeInEnsembleMtx,3) -% for u = 1:size(pokeInEnsembleMtx,2) -% pokeInBinnedEnsembleMtx(:,u,t) = conv(pokeInEnsembleMtx(:,u,t), ones(1,binSize)./(binSize/1000), 'same'); -% pokeOutBinnedEnsembleMtx(:,u,t) = conv(pokeOutEnsembleMtx(:,u,t), ones(1,binSize)./(binSize/1000), 'same'); -% end -% end -% % Now remove the binSize/2 padding -% unPaddedPokeInBinnedEnsembleMtx = pokeInBinnedEnsembleMtx((binSize/2)+1:end-(binSize/2),:,:); -% pokeInTimesUnpadded = pokeInTimes((binSize/2)+1:end-(binSize/2)); -% unPaddedPokeOutBinnedEnsembleMtx = pokeOutBinnedEnsembleMtx((binSize/2)+1:end-(binSize/2),:,:); -% pokeOutTimesUnpadded = pokeOutTimes((binSize/2)+1:end-(binSize/2)); -% % Now downsample the binned matrix -% dsVect = downsample(1:size(unPaddedPokeInBinnedEnsembleMtx,1), dsRate); -% pokeInSpikeMatrix = unPaddedPokeInBinnedEnsembleMtx(dsVect,:,:); -% pokeOutSpikeMatrix = unPaddedPokeOutBinnedEnsembleMtx(dsVect,:,:); -% trialTimePI = pokeInTimesUnpadded(dsVect); -% trialTimePO = pokeOutTimesUnpadded(dsVect); - -%% Alternative way to bin the spike data -mansWin1 = 1:dsRate:size(pokeInEnsembleMtx,1)-binSize; -mansWin2 = mansWin1+binSize; - -pokeInBinnedEnsembleMtx = nan(length(mansWin1), size(pokeInEnsembleMtx,2), size(pokeInEnsembleMtx,3)); -pokeOutBinnedEnsembleMtx = nan(length(mansWin1), size(pokeOutEnsembleMtx,2), size(pokeOutEnsembleMtx,3)); -for trl = 1:size(pokeInEnsembleMtx,3) - for u = 1:size(pokeInEnsembleMtx,2) - curUniPI = pokeInEnsembleMtx(:,u,trl); - pokeInBinnedEnsembleMtx(:,u,trl) = cell2mat(arrayfun(@(a,b)sum(curUniPI(a:b)), mansWin1, mansWin2, 'uniformoutput', 0)); - curUniPO = pokeOutEnsembleMtx(:,u,trl); - pokeOutBinnedEnsembleMtx(:,u,trl) = cell2mat(arrayfun(@(a,b)sum(curUniPO(a:b)), mansWin1, mansWin2, 'uniformoutput', 0)); +% First convolve the entire trialEnsembleMtx with a square to bin the +% spikes +pokeInBinnedEnsembleMtx = nan(size(pokeInEnsembleMtx)); +pokeOutBinnedEnsembleMtx = nan(size(pokeOutEnsembleMtx)); +for t = 1:size(pokeInEnsembleMtx,3) + for u = 1:size(pokeInEnsembleMtx,2) + pokeInBinnedEnsembleMtx(:,u,t) = conv(pokeInEnsembleMtx(:,u,t), ones(1,binSize)./(binSize/1000), 'same'); + pokeOutBinnedEnsembleMtx(:,u,t) = conv(pokeOutEnsembleMtx(:,u,t), ones(1,binSize)./(binSize/1000), 'same'); end end -pokeInBinnedEnsembleMtx = pokeInBinnedEnsembleMtx./(binSize/1000); -pokeOutBinnedEnsembleMtx = pokeOutBinnedEnsembleMtx./(binSize/1000); -pokeInTimesDS = pokeInTimes(mansWin1); -pokeInUnPadLog = pokeInTimesDS>=pokeInWindows(1) & pokeInTimesDS<=pokeInWindows(2); -trialTimePI = pokeInTimesDS(pokeInUnPadLog); -pokeInSpikeMatrix = pokeInBinnedEnsembleMtx(pokeInUnPadLog,:,:); -pokeOutTimesDS = pokeOutTimes(mansWin1); -pokeOutUnPadLog = pokeOutTimesDS>=pokeOutWindows(1) & pokeOutTimesDS<=pokeOutWindows(2); -trialTimePO = pokeOutTimesDS(pokeOutUnPadLog); -pokeOutSpikeMatrix = pokeOutBinnedEnsembleMtx(pokeOutUnPadLog,:,:); +% Now remove the binSize/2 padding +unPaddedPokeInBinnedEnsembleMtx = pokeInBinnedEnsembleMtx((binSize/2)+1:end-(binSize/2),:,:); +pokeInTimesUnpadded = pokeInTimes((binSize/2)+1:end-(binSize/2)); +unPaddedPokeOutBinnedEnsembleMtx = pokeOutBinnedEnsembleMtx((binSize/2)+1:end-(binSize/2),:,:); +pokeOutTimesUnpadded = pokeOutTimes((binSize/2)+1:end-(binSize/2)); +% Now downsample the binned matrix +dsVect = downsample(1:size(unPaddedPokeInBinnedEnsembleMtx,1), dsRate); +pokeInSpikeMatrix = unPaddedPokeInBinnedEnsembleMtx(dsVect,:,:); +pokeOutSpikeMatrix = unPaddedPokeOutBinnedEnsembleMtx(dsVect,:,:); +trialTimePI = pokeInTimesUnpadded(dsVect); +trialTimePO = pokeOutTimesUnpadded(dsVect); + +%% Alternative way to bin the spike data +% mansWin1 = 1:dsRate:size(pokeInEnsembleMtx,1)-binSize; +% mansWin2 = mansWin1+binSize; +% +% pokeInBinnedEnsembleMtx = nan(length(mansWin1), size(pokeInEnsembleMtx,2), size(pokeInEnsembleMtx,3)); +% pokeOutBinnedEnsembleMtx = nan(length(mansWin1), size(pokeOutEnsembleMtx,2), size(pokeOutEnsembleMtx,3)); +% for trl = 1:size(pokeInEnsembleMtx,3) +% for u = 1:size(pokeInEnsembleMtx,2) +% curUniPI = pokeInEnsembleMtx(:,u,trl); +% pokeInBinnedEnsembleMtx(:,u,trl) = cell2mat(arrayfun(@(a,b)sum(curUniPI(a:b)), mansWin1, mansWin2, 'uniformoutput', 0)); +% curUniPO = pokeOutEnsembleMtx(:,u,trl); +% pokeOutBinnedEnsembleMtx(:,u,trl) = cell2mat(arrayfun(@(a,b)sum(curUniPO(a:b)), mansWin1, mansWin2, 'uniformoutput', 0)); +% end +% end +% pokeInBinnedEnsembleMtx = pokeInBinnedEnsembleMtx./(binSize/1000); +% pokeOutBinnedEnsembleMtx = pokeOutBinnedEnsembleMtx./(binSize/1000); +% pokeInTimesDS = pokeInTimes(mansWin1); +% pokeInUnPadLog = pokeInTimesDS>=pokeInWindows(1) & pokeInTimesDS<=pokeInWindows(2); +% trialTimePI = pokeInTimesDS(pokeInUnPadLog); +% pokeInSpikeMatrix = pokeInBinnedEnsembleMtx(pokeInUnPadLog,:,:); +% pokeOutTimesDS = pokeOutTimes(mansWin1); +% pokeOutUnPadLog = pokeOutTimesDS>=pokeOutWindows(1) & pokeOutTimesDS<=pokeOutWindows(2); +% trialTimePO = pokeOutTimesDS(pokeOutUnPadLog); +% pokeOutSpikeMatrix = pokeOutBinnedEnsembleMtx(pokeOutUnPadLog,:,:); %% Create Logical Vectors From 295196e6fe744fd837bd2413874adc9a9ce7c735 Mon Sep 17 00:00:00 2001 From: gabe Date: Wed, 7 Aug 2019 14:13:28 -0700 Subject: [PATCH 031/133] Update BayesPFCbsPROTO_2.m --- Analyses/Prototypes/BayesPFCbsPROTO_2.m | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Analyses/Prototypes/BayesPFCbsPROTO_2.m b/Analyses/Prototypes/BayesPFCbsPROTO_2.m index 1856b64..7f51b55 100644 --- a/Analyses/Prototypes/BayesPFCbsPROTO_2.m +++ b/Analyses/Prototypes/BayesPFCbsPROTO_2.m @@ -3,10 +3,10 @@ clear all %% Runtime variables -binSize = 250; +binSize = 20; dsRate = 5; -pokeInWindows = [-0.5 1.5]; -pokeOutWindows = [-1.5 0.5]; +pokeInWindows = [0 1.2]; +pokeOutWindows = [-1.2 0]; %% smPath = uigetdir; @@ -198,7 +198,7 @@ imagesc(trialTimes, trialTimes, nanmean(postMtx,3)); set(gca, 'ydir', 'normal') hold on; -line(trialTimes, trialTimes, 'linestyle', ':', 'color', 'w', 'linewidth', 2); +% line(trialTimes, trialTimes, 'linestyle', ':', 'color', 'w', 'linewidth', 2); title(id); xlabel('True Time'); ylabel('Decoded Time'); @@ -224,7 +224,8 @@ ee = exp(-(binSize/1000*sum(prior,2))); tempPost = propVect.*pp.*ee; postRaw(t,:,trl) = tempPost; - postNorm(t,:,trl) = tempPost./max(tempPost); +% postNorm(t,:,trl) = tempPost./max(tempPost); % Probably wrong + postNorm(t,:,trl) = tempPost==max(tempPost); end end end \ No newline at end of file From 7081324db9275e61b82b7be4e9c6a3dae025cce1 Mon Sep 17 00:00:00 2001 From: gabe Date: Wed, 7 Aug 2019 14:13:34 -0700 Subject: [PATCH 032/133] Update BayesPFCbsPROTO.m --- Analyses/Prototypes/BayesPFCbsPROTO.m | 327 +++++++++++++------------- 1 file changed, 163 insertions(+), 164 deletions(-) diff --git a/Analyses/Prototypes/BayesPFCbsPROTO.m b/Analyses/Prototypes/BayesPFCbsPROTO.m index a43ae31..fc83efe 100644 --- a/Analyses/Prototypes/BayesPFCbsPROTO.m +++ b/Analyses/Prototypes/BayesPFCbsPROTO.m @@ -3,7 +3,7 @@ clear all %% Runtime variables -binSize = 250; +binSize = 20; dsRate = 5; %% @@ -21,13 +21,13 @@ %% Extract Behavioral Periods % Taking 1/2 the binSize on either end to get rid of edge effects. -% trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.5-(binSize/2/1000) 1.5+(binSize/2/1000)], 'PokeIn'); -trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-1.5-(binSize/2/1000) 0.5+(binSize/2/1000)], 'PokeOut'); +trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [0-(binSize/2/1000) 1.2+(binSize/2/1000)], 'PokeIn'); +% trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-1.5-(binSize/2/1000) 0.5+(binSize/2/1000)], 'PokeOut'); trialEnsemble = ExtractTrialData_SM(trialPeriodTD, ensembleMatrix(:,2:end)); %#ok<*NODEF> trialEnsembleMtx = cell2mat(reshape(trialEnsemble, [1 1 length(trialEnsemble)])); -% trialTimes = behavMatrix(trialPeriodTD(1).TrialLogVect,1) - behavMatrix(trialPeriodTD(1).PokeInIndex,1); -trialTimes = behavMatrix(trialPeriodTD(1).TrialLogVect,1) - behavMatrix(trialPeriodTD(1).PokeOutIndex,1); +trialTimes = behavMatrix(trialPeriodTD(1).TrialLogVect,1) - behavMatrix(trialPeriodTD(1).PokeInIndex,1); +% trialTimes = behavMatrix(trialPeriodTD(1).TrialLogVect,1) - behavMatrix(trialPeriodTD(1).PokeOutIndex,1); %% Bin the spiking data @@ -70,43 +70,43 @@ %% uniFRthreshLog = max(mean(spikeMatrix,3))<1; spkMtx = spikeMatrix; -spkMtx(:,uniFRthreshLog,:) = []; +% spkMtx(:,uniFRthreshLog,:) = []; goodUniNames = {ensembleUnitSummaries(~uniFRthreshLog).UnitName}; %% Decode Trial Time Across Odors -% figure; -% corrAisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorAlog),3); % All A InSeq Correct Trials -% [postAnorm, postAraw] = CalculatePostProb(corrAisMtx, spkMtx(:,:,perfLog & inSeqLog & odorAlog & ~fullInSeqLog), binSize); -% subplot(2,2,1); -% aCaxis = PlotPostMtx(trialTimes, postAnorm, 'Odor A'); -% -% corrBisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorBlog),3); % All B InSeq Correct Trials -% [postBnorm, postBraw] = CalculatePostProb(corrBisMtx, spkMtx(:,:,perfLog & inSeqLog & odorBlog & ~fullInSeqLog), binSize); -% subplot(2,2,2); -% bCaxis = PlotPostMtx(trialTimes, postBnorm, 'Odor B'); -% -% corrCisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorClog),3); % All C InSeq Correct Trials -% [postCnorm, postCraw] = CalculatePostProb(corrCisMtx, spkMtx(:,:,perfLog & inSeqLog & odorClog & ~fullInSeqLog), binSize); -% subplot(2,2,3); -% cCaxis = PlotPostMtx(trialTimes, postCnorm, 'Odor C'); -% -% corrDisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorDlog),3); % All D InSeq Correct Trials -% [postDnorm, postDraw] = CalculatePostProb(corrDisMtx, spkMtx(:,:,perfLog & inSeqLog & odorDlog & ~fullInSeqLog), binSize); -% subplot(2,2,4); -% dCaxis = PlotPostMtx(trialTimes, postDnorm, 'Odor D'); -% -% cAx = [min([aCaxis, bCaxis, cCaxis, dCaxis]), max([aCaxis, bCaxis, cCaxis, dCaxis])]; -% -% annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... -% 'linestyle', 'none', 'horizontalalignment', 'right'); -% curDir = cd; -% annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... -% 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); -% colormap jet -% axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); -% axis(axesHandles,'square') -% set(axesHandles, 'clim', cAx); -% orient(gcf, 'tall'); -% orient(gcf, 'landscape'); +figure; +corrAisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorAlog),3); % All A InSeq Correct Trials +[postAnorm, postAraw] = CalculatePostProb(corrAisMtx, spkMtx(:,:,perfLog & inSeqLog & odorAlog & ~fullInSeqLog), binSize); +subplot(2,2,1); +aCaxis = PlotPostMtx(trialTimes, postAraw, 'Odor A'); + +corrBisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorBlog),3); % All B InSeq Correct Trials +[postBnorm, postBraw] = CalculatePostProb(corrBisMtx, spkMtx(:,:,perfLog & inSeqLog & odorBlog & ~fullInSeqLog), binSize); +subplot(2,2,2); +bCaxis = PlotPostMtx(trialTimes, postBraw, 'Odor B'); + +corrCisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorClog),3); % All C InSeq Correct Trials +[postCnorm, postCraw] = CalculatePostProb(corrCisMtx, spkMtx(:,:,perfLog & inSeqLog & odorClog & ~fullInSeqLog), binSize); +subplot(2,2,3); +cCaxis = PlotPostMtx(trialTimes, postCraw, 'Odor C'); + +corrDisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorDlog),3); % All D InSeq Correct Trials +[postDnorm, postDraw] = CalculatePostProb(corrDisMtx, spkMtx(:,:,perfLog & inSeqLog & odorDlog & ~fullInSeqLog), binSize); +subplot(2,2,4); +dCaxis = PlotPostMtx(trialTimes, postDraw, 'Odor D'); + +cAx = [min([aCaxis, bCaxis, cCaxis, dCaxis]), max([aCaxis, bCaxis, cCaxis, dCaxis])]; + +annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... + 'linestyle', 'none', 'horizontalalignment', 'right'); +curDir = cd; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +colormap jet +axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +axis(axesHandles,'square') +set(axesHandles, 'clim', cAx); +orient(gcf, 'tall'); +orient(gcf, 'landscape'); %% Decode Trial Time and Odor Across Odors nonAIStrials = spkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog); @@ -168,126 +168,126 @@ orient(gcf, 'tall'); orient(gcf, 'landscape'); drawnow; - - -[~, aPriorAllPostRaw] = CalculatePostProb(corrAisMtx, nonAIStrials, binSize); -[~, bPriorAllPostRaw] = CalculatePostProb(corrBisMtx, nonAIStrials, binSize); -[~, cPriorAllPostRaw] = CalculatePostProb(corrCisMtx, nonAIStrials, binSize); -[~, dPriorAllPostRaw] = CalculatePostProb(corrDisMtx, nonAIStrials, binSize); -nonFISaLog = perfLog & inSeqLog & odorAlog & ~fullInSeqLog; -nonFISbLog = perfLog & inSeqLog & odorBlog & ~fullInSeqLog; -nonFIScLog = perfLog & inSeqLog & odorClog & ~fullInSeqLog; -nonFISdLog = perfLog & inSeqLog & odorDlog & ~fullInSeqLog; - -allNonFISpost = [aPriorAllPostRaw, bPriorAllPostRaw, cPriorAllPostRaw, dPriorAllPostRaw]; -for c = 1:size(allNonFISpost,1) - allNonFISpost(c,:,:) = allNonFISpost(c,:,:)./max(allNonFISpost(c,:,:)); -end - -aPriorAllPostNorm = allNonFISpost(:,1:size(aPriorAllPostRaw,1),:); -bPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)+1:size(aPriorAllPostRaw,1)*2,:); -cPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)*2+1:size(aPriorAllPostRaw,1)*3,:); -dPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)*3+1:end,:); - -figure; -cAx = nan(4,4,2); -for prior = 1:4 - switch prior - case 1 - curPrior = aPriorAllPostNorm; - case 2 - curPrior = bPriorAllPostNorm; - case 3 - curPrior = cPriorAllPostNorm; - case 4 - curPrior = dPriorAllPostNorm; - end - for post = 1:4 - curPostLog = nonAISodors == post; - curDecode = curPrior(:,:,curPostLog); - subplot(4,4,sub2ind([4 4], post, prior)) - cAx(prior,post,:) = PlotPostMtx(trialTimes, curDecode, sprintf('Prior%i; Decode%i', prior, post)); - end -end -cAxs = [min(min(cAx(:,:,1))), max(max(cAx(:,:,2)))/3]; - -annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... - 'linestyle', 'none', 'horizontalalignment', 'right'); -annotation('textbox', 'position', [0.025 0.935 0.5 0.05], 'String', '\bf\fontsize{14}Decoding Trial and Odor Across Odors',... - 'linestyle', 'none', 'horizontalalignment', 'left'); -curDir = cd; -annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... - 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); -colormap jet -axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); -axis(axesHandles,'square') -set(axesHandles, 'clim', cAxs); -orient(gcf, 'tall'); -orient(gcf, 'landscape'); -drawnow; +% +% +% [~, aPriorAllPostRaw] = CalculatePostProb(corrAisMtx, nonAIStrials, binSize); +% [~, bPriorAllPostRaw] = CalculatePostProb(corrBisMtx, nonAIStrials, binSize); +% [~, cPriorAllPostRaw] = CalculatePostProb(corrCisMtx, nonAIStrials, binSize); +% [~, dPriorAllPostRaw] = CalculatePostProb(corrDisMtx, nonAIStrials, binSize); +% nonFISaLog = perfLog & inSeqLog & odorAlog & ~fullInSeqLog; +% nonFISbLog = perfLog & inSeqLog & odorBlog & ~fullInSeqLog; +% nonFIScLog = perfLog & inSeqLog & odorClog & ~fullInSeqLog; +% nonFISdLog = perfLog & inSeqLog & odorDlog & ~fullInSeqLog; +% +% allNonFISpost = [aPriorAllPostRaw, bPriorAllPostRaw, cPriorAllPostRaw, dPriorAllPostRaw]; +% for c = 1:size(allNonFISpost,1) +% allNonFISpost(c,:,:) = allNonFISpost(c,:,:)./max(allNonFISpost(c,:,:)); +% end +% +% aPriorAllPostNorm = allNonFISpost(:,1:size(aPriorAllPostRaw,1),:); +% bPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)+1:size(aPriorAllPostRaw,1)*2,:); +% cPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)*2+1:size(aPriorAllPostRaw,1)*3,:); +% dPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)*3+1:end,:); +% +% figure; +% cAx = nan(4,4,2); +% for prior = 1:4 +% switch prior +% case 1 +% curPrior = aPriorAllPostNorm; +% case 2 +% curPrior = bPriorAllPostNorm; +% case 3 +% curPrior = cPriorAllPostNorm; +% case 4 +% curPrior = dPriorAllPostNorm; +% end +% for post = 1:4 +% curPostLog = nonAISodors == post; +% curDecode = curPrior(:,:,curPostLog); +% subplot(4,4,sub2ind([4 4], post, prior)) +% cAx(prior,post,:) = PlotPostMtx(trialTimes, curDecode, sprintf('Prior%i; Decode%i', prior, post)); +% end +% end +% cAxs = [min(min(cAx(:,:,1))), max(max(cAx(:,:,2)))/3]; +% +% annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... +% 'linestyle', 'none', 'horizontalalignment', 'right'); +% annotation('textbox', 'position', [0.025 0.935 0.5 0.05], 'String', '\bf\fontsize{14}Decoding Trial and Odor Across Odors',... +% 'linestyle', 'none', 'horizontalalignment', 'left'); +% curDir = cd; +% annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... +% 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +% colormap jet +% axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +% axis(axesHandles,'square') +% set(axesHandles, 'clim', cAxs); +% orient(gcf, 'tall'); +% orient(gcf, 'landscape'); +% drawnow; %% -figure -corrISmtx = mean(spkMtx(:,:,perfLog & fullInSeqLog),3); % All InSeq Correct Trials -[post] = CalculatePostProb(corrISmtx, spkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog), binSize); -PlotPostMtx(trialTimes, post, 'InSeq Correct Trials'); - -annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... - 'linestyle', 'none', 'horizontalalignment', 'right'); -curDir = cd; -annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... - 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); -colormap jet -axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); -axis(axesHandles,'square') -orient(gcf, 'tall'); -orient(gcf, 'landscape'); -drawnow; +% figure +% corrISmtx = mean(spkMtx(:,:,perfLog & fullInSeqLog),3); % All InSeq Correct Trials +% [post] = CalculatePostProb(corrISmtx, spkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog), binSize); +% PlotPostMtx(trialTimes, post, 'InSeq Correct Trials'); +% +% annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... +% 'linestyle', 'none', 'horizontalalignment', 'right'); +% curDir = cd; +% annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... +% 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +% colormap jet +% axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +% axis(axesHandles,'square') +% orient(gcf, 'tall'); +% orient(gcf, 'landscape'); +% drawnow; %% Compare Trial Before with Trial After OutSeq Trial -nonAIStrials = spkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog); -nonAIStrialData = trialPeriodTD(perfLog & inSeqLog & ~fullInSeqLog); -nonAISodors = [nonAIStrialData.Odor]; -nonAIStrialNums = [nonAIStrialData.TrialNum]; - -preOStrial = false(size(nonAIStrialNums)); -postOStrial = false(size(nonAIStrialNums)); -for trl = 1:length(nonAIStrialNums) - if nonAIStrialNums(trl)~=1 && nonAIStrialNums(trl) ~= length(trialPeriodTD) - prevTrial = trialPeriodTD(nonAIStrialNums(trl)-1); - curTrl = trialPeriodTD(nonAIStrialNums(trl)); - nextTrial = trialPeriodTD(nonAIStrialNums(trl)+1); - if curTrl.Position ~= 1 - if curTrl.Position - prevTrial.Position == 1 && prevTrial.TranspositionDistance ~= 0 - postOStrial(trl) = true; - preOStrial(trl-2) = true; - end - end - end -end -postPostOS = post(:,:,postOStrial); -postPreOS = post(:,:,preOStrial); - -figure; -subplot(1,2,1) -PlotPostMtx(trialTimes, postPreOS, 'Trial Before OutSeq'); - -subplot(1,2,2) -PlotPostMtx(trialTimes, postPostOS, 'Trial After OutSeq'); - -annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... - 'linestyle', 'none', 'horizontalalignment', 'right'); -curDir = cd; -annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... - 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); -colormap jet -axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); -axis(axesHandles,'square') -cLims = cell2mat(get(axesHandles, 'cLim')); -set(axesHandles, 'cLim', [min(cLims(:,1)), max(cLims(:,2))]); -orient(gcf, 'tall'); -orient(gcf, 'landscape'); -drawnow; +% nonAIStrials = spkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog); +% nonAIStrialData = trialPeriodTD(perfLog & inSeqLog & ~fullInSeqLog); +% nonAISodors = [nonAIStrialData.Odor]; +% nonAIStrialNums = [nonAIStrialData.TrialNum]; +% +% preOStrial = false(size(nonAIStrialNums)); +% postOStrial = false(size(nonAIStrialNums)); +% for trl = 1:length(nonAIStrialNums) +% if nonAIStrialNums(trl)~=1 && nonAIStrialNums(trl) ~= length(trialPeriodTD) +% prevTrial = trialPeriodTD(nonAIStrialNums(trl)-1); +% curTrl = trialPeriodTD(nonAIStrialNums(trl)); +% nextTrial = trialPeriodTD(nonAIStrialNums(trl)+1); +% if curTrl.Position ~= 1 +% if curTrl.Position - prevTrial.Position == 1 && prevTrial.TranspositionDistance ~= 0 +% postOStrial(trl) = true; +% preOStrial(trl-2) = true; +% end +% end +% end +% end +% postPostOS = post(:,:,postOStrial); +% postPreOS = post(:,:,preOStrial); +% +% figure; +% subplot(1,2,1) +% PlotPostMtx(trialTimes, postPreOS, 'Trial Before OutSeq'); +% +% subplot(1,2,2) +% PlotPostMtx(trialTimes, postPostOS, 'Trial After OutSeq'); +% +% annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... +% 'linestyle', 'none', 'horizontalalignment', 'right'); +% curDir = cd; +% annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... +% 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +% colormap jet +% axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +% axis(axesHandles,'square') +% cLims = cell2mat(get(axesHandles, 'cLim')); +% set(axesHandles, 'cLim', [min(cLims(:,1)), max(cLims(:,2))]); +% orient(gcf, 'tall'); +% orient(gcf, 'landscape'); +% drawnow; @@ -306,7 +306,7 @@ title(id); set(gca, 'ydir', 'normal') hold on; -line(trialTimes, trialTimes, 'linestyle', ':', 'color', 'w', 'linewidth', 2); +% line(trialTimes, trialTimes, 'linestyle', ':', 'color', 'w', 'linewidth', 2); xlabel('True Time'); ylabel('Decoded Time'); drawnow @@ -316,26 +316,25 @@ %% function [postNorm, postRaw] = CalculatePostProb(prior, obsv, binSize) -propVect = 1./sum(prior,2); +% propVect = 1./sum(prior,2); % Probably wrong postNorm = nan(size(obsv,1), size(obsv,1), size(obsv,3)); postRaw = nan(size(obsv,1), size(obsv,1), size(obsv,3)); for trl = 1:size(obsv,3) for t = 1:size(prior,1) p = nan(size(prior)); -% e = nan(size(meanFR)); % I think this is wrong curPopVect = obsv(t,:,trl); curPopFact = factorial(curPopVect); for u = 1:size(prior,2) curAvgUniFR = prior(:,u); p(:,u) = (((binSize/1000)*curAvgUniFR).^curPopVect(u))./curPopFact(u); -% e(:,u) = exp(-(binSize/1000*curAvgUniFR)); % I think this is wrong end pp = prod(p,2); -% ee = sum(e,2); % I think this is wrong ee = exp(-(binSize/1000*sum(prior,2))); - tempPost = propVect.*pp.*ee; - postRaw(t,:,trl) = tempPost; - postNorm(t,:,trl) = tempPost./max(tempPost); +% tempPost = propVect.*pp.*ee; % Probably wrong + tempPost = pp.*ee; + postRaw(t,:,trl) = tempPost.*sum(tempPost); +% postNorm(t,:,trl) = tempPost./max(tempPost); % Probably wrong + postNorm(t,:,trl) = tempPost==max(tempPost); end end end \ No newline at end of file From 9a8df9d08d5c2699d0da30f4a2749082dbfcdc1b Mon Sep 17 00:00:00 2001 From: gabe Date: Wed, 7 Aug 2019 14:55:22 -0700 Subject: [PATCH 033/133] Update BayesPFCbsPROTO.m --- Analyses/Prototypes/BayesPFCbsPROTO.m | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/Analyses/Prototypes/BayesPFCbsPROTO.m b/Analyses/Prototypes/BayesPFCbsPROTO.m index fc83efe..390436a 100644 --- a/Analyses/Prototypes/BayesPFCbsPROTO.m +++ b/Analyses/Prototypes/BayesPFCbsPROTO.m @@ -37,6 +37,7 @@ for t = 1:size(trialEnsembleMtx,3) for u = 1:size(trialEnsembleMtx,2) binnedEnsembleMtx(:,u,t) = conv(trialEnsembleMtx(:,u,t), ones(1,binSize)./(binSize/1000), 'same'); +% binnedEnsembleMtx(:,u,t) = conv(trialEnsembleMtx(:,u,t), ones(1,binSize), 'same'); end end % Now remove the binSize/2 padding @@ -70,31 +71,31 @@ %% uniFRthreshLog = max(mean(spikeMatrix,3))<1; spkMtx = spikeMatrix; -% spkMtx(:,uniFRthreshLog,:) = []; +spkMtx(:,uniFRthreshLog,:) = []; goodUniNames = {ensembleUnitSummaries(~uniFRthreshLog).UnitName}; %% Decode Trial Time Across Odors figure; corrAisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorAlog),3); % All A InSeq Correct Trials [postAnorm, postAraw] = CalculatePostProb(corrAisMtx, spkMtx(:,:,perfLog & inSeqLog & odorAlog & ~fullInSeqLog), binSize); subplot(2,2,1); -aCaxis = PlotPostMtx(trialTimes, postAraw, 'Odor A'); +aCaxis = PlotPostMtx(trialTimes, postAnorm, 'Odor A'); corrBisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorBlog),3); % All B InSeq Correct Trials [postBnorm, postBraw] = CalculatePostProb(corrBisMtx, spkMtx(:,:,perfLog & inSeqLog & odorBlog & ~fullInSeqLog), binSize); subplot(2,2,2); -bCaxis = PlotPostMtx(trialTimes, postBraw, 'Odor B'); +bCaxis = PlotPostMtx(trialTimes, postBnorm, 'Odor B'); corrCisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorClog),3); % All C InSeq Correct Trials [postCnorm, postCraw] = CalculatePostProb(corrCisMtx, spkMtx(:,:,perfLog & inSeqLog & odorClog & ~fullInSeqLog), binSize); subplot(2,2,3); -cCaxis = PlotPostMtx(trialTimes, postCraw, 'Odor C'); +cCaxis = PlotPostMtx(trialTimes, postCnorm, 'Odor C'); corrDisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorDlog),3); % All D InSeq Correct Trials [postDnorm, postDraw] = CalculatePostProb(corrDisMtx, spkMtx(:,:,perfLog & inSeqLog & odorDlog & ~fullInSeqLog), binSize); subplot(2,2,4); -dCaxis = PlotPostMtx(trialTimes, postDraw, 'Odor D'); +dCaxis = PlotPostMtx(trialTimes, postDnorm, 'Odor D'); -cAx = [min([aCaxis, bCaxis, cCaxis, dCaxis]), max([aCaxis, bCaxis, cCaxis, dCaxis])]; +cAx = [min([aCaxis, bCaxis, cCaxis, dCaxis]), max([aCaxis, bCaxis, cCaxis, dCaxis])/3]; annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... 'linestyle', 'none', 'horizontalalignment', 'right'); @@ -322,19 +323,21 @@ for trl = 1:size(obsv,3) for t = 1:size(prior,1) p = nan(size(prior)); - curPopVect = obsv(t,:,trl); + curPopVect = obsv(t,:,trl)./(1000/binSize); curPopFact = factorial(curPopVect); for u = 1:size(prior,2) curAvgUniFR = prior(:,u); - p(:,u) = (((binSize/1000)*curAvgUniFR).^curPopVect(u))./curPopFact(u); + p(:,u) = (((binSize/1000).*curAvgUniFR).^curPopVect(u))./curPopFact(u); +% p(:,u) = ((curAvgUniFR).^curPopVect(u))./curPopFact(u); end pp = prod(p,2); ee = exp(-(binSize/1000*sum(prior,2))); +% ee = exp(-(sum(prior,2))); % tempPost = propVect.*pp.*ee; % Probably wrong tempPost = pp.*ee; - postRaw(t,:,trl) = tempPost.*sum(tempPost); + postRaw(t,:,trl) = tempPost; % postNorm(t,:,trl) = tempPost./max(tempPost); % Probably wrong - postNorm(t,:,trl) = tempPost==max(tempPost); + postNorm(t,:,trl) = tempPost./sum(tempPost); end end end \ No newline at end of file From 437d36e050c91dca0b888fa69f8aaeb97a845250 Mon Sep 17 00:00:00 2001 From: gaelias Date: Wed, 7 Aug 2019 18:29:28 -0700 Subject: [PATCH 034/133] Update BayesPFCbsPROTO_2.m --- Analyses/Prototypes/BayesPFCbsPROTO_2.m | 61 +++++++++++++------------ 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/Analyses/Prototypes/BayesPFCbsPROTO_2.m b/Analyses/Prototypes/BayesPFCbsPROTO_2.m index 7f51b55..318293b 100644 --- a/Analyses/Prototypes/BayesPFCbsPROTO_2.m +++ b/Analyses/Prototypes/BayesPFCbsPROTO_2.m @@ -3,10 +3,10 @@ clear all %% Runtime variables -binSize = 20; +binSize = 200; dsRate = 5; -pokeInWindows = [0 1.2]; -pokeOutWindows = [-1.2 0]; +pokeInWindows = [-0.5 1.5]; +pokeOutWindows = [-1.5 0.5]; %% smPath = uigetdir; @@ -119,17 +119,14 @@ %% figure corrISmtxPI = mean(pokeInSpkMtx(:,:,perfLog & fullInSeqLog),3); % All InSeq Correct Trials -[postPI] = CalculatePostProb(corrISmtxPI, pokeInSpkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog), binSize); -subplot(4,4,[1,2,5,6]) +[postPI, ~] = CalculatePostProb(corrISmtxPI, pokeInSpkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog), binSize); +piPlot = subplot(4,4,[1,2,5,6]); PlotPostMtx(trialTimePI, postPI, 'InSeq Correct Trials (Poke In)'); -set(gca, 'clim', [0 0.5]); corrISmtxPO = mean(pokeOutSpkMtx(:,:,perfLog & fullInSeqLog),3); -[postPO] = CalculatePostProb(corrISmtxPO, pokeOutSpkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog), binSize); -subplot(4,4,[9,10,13,14]) +[postPO, ~] = CalculatePostProb(corrISmtxPO, pokeOutSpkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog), binSize); +poPlot = subplot(4,4,[9,10,13,14]); PlotPostMtx(trialTimePO, postPO, 'InSeq Correct Trials (Poke Out)'); -set(gca, 'clim', [0 0.5]); - nonAIStrialData = pokeInTrialPeriodTD(perfLog & inSeqLog & ~fullInSeqLog); nonAISodors = [nonAIStrialData.Odor]; @@ -152,31 +149,36 @@ end preOSpi = postPI(:,:,preOStrialLog); -subplot(4,4,3) +prOsPIplt = subplot(4,4,3); PlotPostMtx(trialTimePI, preOSpi, 'Before OutSeq'); -set(gca, 'clim', [0 0.5]); + postOSpi = postPI(:,:,postOStrialLog); -subplot(4,4,7) +poOsPIplt = subplot(4,4,7); PlotPostMtx(trialTimePI, postOSpi, 'After OutSeq'); -set(gca, 'clim', [0 0.5]); + postPreDiffPI = postOSpi - preOSpi; -subplot(4,4,4) +piDiffPlt = subplot(4,4,4); PlotPostMtx(trialTimePI, postPreDiffPI, 'After-Before'); -set(gca, 'clim', [-0.25 0.25]); preOSpo = postPO(:,:,preOStrialLog); -subplot(4,4,11) +prOsPOplt = subplot(4,4,11); PlotPostMtx(trialTimePO, preOSpo, 'Before OutSeq'); -set(gca, 'clim', [0 0.5]); + postOSpo = postPO(:,:,postOStrialLog); -subplot(4,4,15) +poOsPOplt = subplot(4,4,15); PlotPostMtx(trialTimePO, postOSpo, 'After OutSeq'); -set(gca, 'clim', [0 0.5]); + postPreDiffPO = postOSpo - preOSpo; -subplot(4,4,12) +poDiffPlt = subplot(4,4,12); PlotPostMtx(trialTimePO, postPreDiffPO, 'After-Before'); -set(gca, 'clim', [-0.25 0.25]); + +cLimsDecode = get([piPlot, poPlot, prOsPIplt, poOsPIplt, prOsPOplt, poOsPOplt], 'clim'); +cLimsDiff = get([piDiffPlt, poDiffPlt], 'clim'); + +set([piPlot, poPlot, prOsPIplt, poOsPIplt, prOsPOplt, poOsPOplt], 'clim', [0 max(max(cell2mat(cLimsDecode)))]); +set([piDiffPlt, poDiffPlt], 'clim', [0 max(max(cell2mat(cLimsDiff)))]); +colormap jet annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... 'linestyle', 'none', 'horizontalalignment', 'right'); @@ -208,24 +210,27 @@ %% function [postNorm, postRaw] = CalculatePostProb(prior, obsv, binSize) -propVect = 1./sum(prior,2); +% propVect = 1./sum(prior,2); % Probably wrong postNorm = nan(size(obsv,1), size(obsv,1), size(obsv,3)); postRaw = nan(size(obsv,1), size(obsv,1), size(obsv,3)); for trl = 1:size(obsv,3) for t = 1:size(prior,1) p = nan(size(prior)); - curPopVect = obsv(t,:,trl); + curPopVect = obsv(t,:,trl)*(binSize/1000); curPopFact = factorial(curPopVect); for u = 1:size(prior,2) curAvgUniFR = prior(:,u); - p(:,u) = (((binSize/1000)*curAvgUniFR).^curPopVect(u))./curPopFact(u); + p(:,u) = (((binSize/1000).*curAvgUniFR).^curPopVect(u))./curPopFact(u); +% p(:,u) = ((curAvgUniFR).^curPopVect(u))./curPopFact(u); end pp = prod(p,2); - ee = exp(-(binSize/1000*sum(prior,2))); - tempPost = propVect.*pp.*ee; + ee = exp(-((binSize/1000)*sum(prior,2))); +% ee = exp(-(sum(prior,2))); +% tempPost = propVect.*pp.*ee; % Probably wrong + tempPost = pp.*ee; postRaw(t,:,trl) = tempPost; % postNorm(t,:,trl) = tempPost./max(tempPost); % Probably wrong - postNorm(t,:,trl) = tempPost==max(tempPost); + postNorm(t,:,trl) = tempPost./sum(tempPost); end end end \ No newline at end of file From 50fd209d7935d26e40e9ee901ddd72375ae55b28 Mon Sep 17 00:00:00 2001 From: gaelias Date: Wed, 7 Aug 2019 18:30:27 -0700 Subject: [PATCH 035/133] Update BayesPFCbsPROTO.m --- Analyses/Prototypes/BayesPFCbsPROTO.m | 244 +++++++++++++------------- 1 file changed, 122 insertions(+), 122 deletions(-) diff --git a/Analyses/Prototypes/BayesPFCbsPROTO.m b/Analyses/Prototypes/BayesPFCbsPROTO.m index 390436a..86fb502 100644 --- a/Analyses/Prototypes/BayesPFCbsPROTO.m +++ b/Analyses/Prototypes/BayesPFCbsPROTO.m @@ -3,7 +3,7 @@ clear all %% Runtime variables -binSize = 20; +binSize = 100; dsRate = 5; %% @@ -21,7 +21,7 @@ %% Extract Behavioral Periods % Taking 1/2 the binSize on either end to get rid of edge effects. -trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [0-(binSize/2/1000) 1.2+(binSize/2/1000)], 'PokeIn'); +trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.5-(binSize/2/1000) 1.5+(binSize/2/1000)], 'PokeIn'); % trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-1.5-(binSize/2/1000) 0.5+(binSize/2/1000)], 'PokeOut'); trialEnsemble = ExtractTrialData_SM(trialPeriodTD, ensembleMatrix(:,2:end)); %#ok<*NODEF> trialEnsembleMtx = cell2mat(reshape(trialEnsemble, [1 1 length(trialEnsemble)])); @@ -95,7 +95,7 @@ subplot(2,2,4); dCaxis = PlotPostMtx(trialTimes, postDnorm, 'Odor D'); -cAx = [min([aCaxis, bCaxis, cCaxis, dCaxis]), max([aCaxis, bCaxis, cCaxis, dCaxis])/3]; +cAx = [min([aCaxis, bCaxis, cCaxis, dCaxis]), max([aCaxis, bCaxis, cCaxis, dCaxis])*.5]; annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... 'linestyle', 'none', 'horizontalalignment', 'right'); @@ -149,7 +149,7 @@ frMapSPs = [oAfr, oBfr, oCfr, oDfr]; curCL = cell2mat(get(frMapSPs, 'clim')); -set(frMapSPs, 'clim', [min(curCL(:,1)), max(curCL(:,2))], 'ydir', 'normal'); +set(frMapSPs, 'clim', [min(curCL(:,1)), max(curCL(:,2))*.5], 'ydir', 'normal'); corrSPs = [abCorr, acCorr, adCorr, bcCorr, bdCorr, cdCorr]; curXL = cell2mat(get(corrSPs, 'xlim')); curYL = cell2mat(get(corrSPs, 'ylim')); @@ -169,126 +169,126 @@ orient(gcf, 'tall'); orient(gcf, 'landscape'); drawnow; -% -% -% [~, aPriorAllPostRaw] = CalculatePostProb(corrAisMtx, nonAIStrials, binSize); -% [~, bPriorAllPostRaw] = CalculatePostProb(corrBisMtx, nonAIStrials, binSize); -% [~, cPriorAllPostRaw] = CalculatePostProb(corrCisMtx, nonAIStrials, binSize); -% [~, dPriorAllPostRaw] = CalculatePostProb(corrDisMtx, nonAIStrials, binSize); -% nonFISaLog = perfLog & inSeqLog & odorAlog & ~fullInSeqLog; -% nonFISbLog = perfLog & inSeqLog & odorBlog & ~fullInSeqLog; -% nonFIScLog = perfLog & inSeqLog & odorClog & ~fullInSeqLog; -% nonFISdLog = perfLog & inSeqLog & odorDlog & ~fullInSeqLog; -% -% allNonFISpost = [aPriorAllPostRaw, bPriorAllPostRaw, cPriorAllPostRaw, dPriorAllPostRaw]; -% for c = 1:size(allNonFISpost,1) -% allNonFISpost(c,:,:) = allNonFISpost(c,:,:)./max(allNonFISpost(c,:,:)); -% end -% -% aPriorAllPostNorm = allNonFISpost(:,1:size(aPriorAllPostRaw,1),:); -% bPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)+1:size(aPriorAllPostRaw,1)*2,:); -% cPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)*2+1:size(aPriorAllPostRaw,1)*3,:); -% dPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)*3+1:end,:); -% -% figure; -% cAx = nan(4,4,2); -% for prior = 1:4 -% switch prior -% case 1 -% curPrior = aPriorAllPostNorm; -% case 2 -% curPrior = bPriorAllPostNorm; -% case 3 -% curPrior = cPriorAllPostNorm; -% case 4 -% curPrior = dPriorAllPostNorm; -% end -% for post = 1:4 -% curPostLog = nonAISodors == post; -% curDecode = curPrior(:,:,curPostLog); -% subplot(4,4,sub2ind([4 4], post, prior)) -% cAx(prior,post,:) = PlotPostMtx(trialTimes, curDecode, sprintf('Prior%i; Decode%i', prior, post)); -% end -% end -% cAxs = [min(min(cAx(:,:,1))), max(max(cAx(:,:,2)))/3]; -% -% annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... -% 'linestyle', 'none', 'horizontalalignment', 'right'); -% annotation('textbox', 'position', [0.025 0.935 0.5 0.05], 'String', '\bf\fontsize{14}Decoding Trial and Odor Across Odors',... -% 'linestyle', 'none', 'horizontalalignment', 'left'); -% curDir = cd; -% annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... -% 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); -% colormap jet -% axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); -% axis(axesHandles,'square') -% set(axesHandles, 'clim', cAxs); -% orient(gcf, 'tall'); -% orient(gcf, 'landscape'); -% drawnow; + + +[~, aPriorAllPostRaw] = CalculatePostProb(corrAisMtx, nonAIStrials, binSize); +[~, bPriorAllPostRaw] = CalculatePostProb(corrBisMtx, nonAIStrials, binSize); +[~, cPriorAllPostRaw] = CalculatePostProb(corrCisMtx, nonAIStrials, binSize); +[~, dPriorAllPostRaw] = CalculatePostProb(corrDisMtx, nonAIStrials, binSize); +nonFISaLog = perfLog & inSeqLog & odorAlog & ~fullInSeqLog; +nonFISbLog = perfLog & inSeqLog & odorBlog & ~fullInSeqLog; +nonFIScLog = perfLog & inSeqLog & odorClog & ~fullInSeqLog; +nonFISdLog = perfLog & inSeqLog & odorDlog & ~fullInSeqLog; + +allNonFISpost = [aPriorAllPostRaw, bPriorAllPostRaw, cPriorAllPostRaw, dPriorAllPostRaw]; +for c = 1:size(allNonFISpost,1) + allNonFISpost(c,:,:) = allNonFISpost(c,:,:)./sum(allNonFISpost(c,:,:)); +end + +aPriorAllPostNorm = allNonFISpost(:,1:size(aPriorAllPostRaw,1),:); +bPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)+1:size(aPriorAllPostRaw,1)*2,:); +cPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)*2+1:size(aPriorAllPostRaw,1)*3,:); +dPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)*3+1:end,:); + +figure; +cAx = nan(4,4,2); +for prior = 1:4 + switch prior + case 1 + curPrior = aPriorAllPostNorm; + case 2 + curPrior = bPriorAllPostNorm; + case 3 + curPrior = cPriorAllPostNorm; + case 4 + curPrior = dPriorAllPostNorm; + end + for post = 1:4 + curPostLog = nonAISodors == post; + curDecode = curPrior(:,:,curPostLog); + subplot(4,4,sub2ind([4 4], post, prior)) + cAx(prior,post,:) = PlotPostMtx(trialTimes, curDecode, sprintf('Prior%i; Decode%i', prior, post)); + end +end +cAxs = [min(min(cAx(:,:,1))), max(max(cAx(:,:,2)))*.5]; + +annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... + 'linestyle', 'none', 'horizontalalignment', 'right'); +annotation('textbox', 'position', [0.025 0.935 0.5 0.05], 'String', '\bf\fontsize{14}Decoding Trial and Odor Across Odors',... + 'linestyle', 'none', 'horizontalalignment', 'left'); +curDir = cd; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +colormap jet +axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +axis(axesHandles,'square') +set(axesHandles, 'clim', cAxs); +orient(gcf, 'tall'); +orient(gcf, 'landscape'); +drawnow; %% -% figure -% corrISmtx = mean(spkMtx(:,:,perfLog & fullInSeqLog),3); % All InSeq Correct Trials -% [post] = CalculatePostProb(corrISmtx, spkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog), binSize); -% PlotPostMtx(trialTimes, post, 'InSeq Correct Trials'); -% -% annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... -% 'linestyle', 'none', 'horizontalalignment', 'right'); -% curDir = cd; -% annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... -% 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); -% colormap jet -% axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); -% axis(axesHandles,'square') -% orient(gcf, 'tall'); -% orient(gcf, 'landscape'); -% drawnow; +figure +corrISmtx = mean(spkMtx(:,:,perfLog & fullInSeqLog),3); % All InSeq Correct Trials +[post,~] = CalculatePostProb(corrISmtx, spkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog), binSize); +PlotPostMtx(trialTimes, post, 'InSeq Correct Trials'); + +annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... + 'linestyle', 'none', 'horizontalalignment', 'right'); +curDir = cd; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +colormap jet +axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +axis(axesHandles,'square') +orient(gcf, 'tall'); +orient(gcf, 'landscape'); +drawnow; %% Compare Trial Before with Trial After OutSeq Trial -% nonAIStrials = spkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog); -% nonAIStrialData = trialPeriodTD(perfLog & inSeqLog & ~fullInSeqLog); -% nonAISodors = [nonAIStrialData.Odor]; -% nonAIStrialNums = [nonAIStrialData.TrialNum]; -% -% preOStrial = false(size(nonAIStrialNums)); -% postOStrial = false(size(nonAIStrialNums)); -% for trl = 1:length(nonAIStrialNums) -% if nonAIStrialNums(trl)~=1 && nonAIStrialNums(trl) ~= length(trialPeriodTD) -% prevTrial = trialPeriodTD(nonAIStrialNums(trl)-1); -% curTrl = trialPeriodTD(nonAIStrialNums(trl)); -% nextTrial = trialPeriodTD(nonAIStrialNums(trl)+1); -% if curTrl.Position ~= 1 -% if curTrl.Position - prevTrial.Position == 1 && prevTrial.TranspositionDistance ~= 0 -% postOStrial(trl) = true; -% preOStrial(trl-2) = true; -% end -% end -% end -% end -% postPostOS = post(:,:,postOStrial); -% postPreOS = post(:,:,preOStrial); -% -% figure; -% subplot(1,2,1) -% PlotPostMtx(trialTimes, postPreOS, 'Trial Before OutSeq'); -% -% subplot(1,2,2) -% PlotPostMtx(trialTimes, postPostOS, 'Trial After OutSeq'); -% -% annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... -% 'linestyle', 'none', 'horizontalalignment', 'right'); -% curDir = cd; -% annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... -% 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); -% colormap jet -% axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); -% axis(axesHandles,'square') -% cLims = cell2mat(get(axesHandles, 'cLim')); -% set(axesHandles, 'cLim', [min(cLims(:,1)), max(cLims(:,2))]); -% orient(gcf, 'tall'); -% orient(gcf, 'landscape'); -% drawnow; +nonAIStrials = spkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog); +nonAIStrialData = trialPeriodTD(perfLog & inSeqLog & ~fullInSeqLog); +nonAISodors = [nonAIStrialData.Odor]; +nonAIStrialNums = [nonAIStrialData.TrialNum]; + +preOStrial = false(size(nonAIStrialNums)); +postOStrial = false(size(nonAIStrialNums)); +for trl = 1:length(nonAIStrialNums) + if nonAIStrialNums(trl)~=1 && nonAIStrialNums(trl) ~= length(trialPeriodTD) + prevTrial = trialPeriodTD(nonAIStrialNums(trl)-1); + curTrl = trialPeriodTD(nonAIStrialNums(trl)); + nextTrial = trialPeriodTD(nonAIStrialNums(trl)+1); + if curTrl.Position ~= 1 + if curTrl.Position - prevTrial.Position == 1 && prevTrial.TranspositionDistance ~= 0 + postOStrial(trl) = true; + preOStrial(trl-2) = true; + end + end + end +end +postPostOS = post(:,:,postOStrial); +postPreOS = post(:,:,preOStrial); + +figure; +subplot(1,2,1) +PlotPostMtx(trialTimes, postPreOS, 'Trial Before OutSeq'); + +subplot(1,2,2) +PlotPostMtx(trialTimes, postPostOS, 'Trial After OutSeq'); + +annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... + 'linestyle', 'none', 'horizontalalignment', 'right'); +curDir = cd; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +colormap jet +axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +axis(axesHandles,'square') +cLims = cell2mat(get(axesHandles, 'cLim')); +set(axesHandles, 'cLim', [min(cLims(:,1)), max(cLims(:,2))]); +orient(gcf, 'tall'); +orient(gcf, 'landscape'); +drawnow; @@ -323,7 +323,7 @@ for trl = 1:size(obsv,3) for t = 1:size(prior,1) p = nan(size(prior)); - curPopVect = obsv(t,:,trl)./(1000/binSize); + curPopVect = obsv(t,:,trl)*(binSize/1000); curPopFact = factorial(curPopVect); for u = 1:size(prior,2) curAvgUniFR = prior(:,u); @@ -331,7 +331,7 @@ % p(:,u) = ((curAvgUniFR).^curPopVect(u))./curPopFact(u); end pp = prod(p,2); - ee = exp(-(binSize/1000*sum(prior,2))); + ee = exp(-((binSize/1000)*sum(prior,2))); % ee = exp(-(sum(prior,2))); % tempPost = propVect.*pp.*ee; % Probably wrong tempPost = pp.*ee; From f203b8d963176c3013e3abfa7447f572a62d914a Mon Sep 17 00:00:00 2001 From: gabe Date: Tue, 12 Nov 2019 12:29:13 -0800 Subject: [PATCH 036/133] Update BayesPFCbsPROTO.m --- Analyses/Prototypes/BayesPFCbsPROTO.m | 29 +++++++++++++++------------ 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/Analyses/Prototypes/BayesPFCbsPROTO.m b/Analyses/Prototypes/BayesPFCbsPROTO.m index 390436a..db8007f 100644 --- a/Analyses/Prototypes/BayesPFCbsPROTO.m +++ b/Analyses/Prototypes/BayesPFCbsPROTO.m @@ -3,7 +3,7 @@ clear all %% Runtime variables -binSize = 20; +binSize = 100; dsRate = 5; %% @@ -21,7 +21,8 @@ %% Extract Behavioral Periods % Taking 1/2 the binSize on either end to get rid of edge effects. -trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [0-(binSize/2/1000) 1.2+(binSize/2/1000)], 'PokeIn'); +trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [0-(binSize/2/1000) 0.5+(binSize/2/1000)], 'PokeIn'); +trialPeriodTD = trialPeriodTD(1:end-1); % trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-1.5-(binSize/2/1000) 0.5+(binSize/2/1000)], 'PokeOut'); trialEnsemble = ExtractTrialData_SM(trialPeriodTD, ensembleMatrix(:,2:end)); %#ok<*NODEF> trialEnsembleMtx = cell2mat(reshape(trialEnsemble, [1 1 length(trialEnsemble)])); @@ -29,7 +30,7 @@ trialTimes = behavMatrix(trialPeriodTD(1).TrialLogVect,1) - behavMatrix(trialPeriodTD(1).PokeInIndex,1); % trialTimes = behavMatrix(trialPeriodTD(1).TrialLogVect,1) - behavMatrix(trialPeriodTD(1).PokeOutIndex,1); - +clear behavMatrix ensembleMatrix %% Bin the spiking data % First convolve the entire trialEnsembleMtx with a square to bin the % spikes @@ -48,6 +49,7 @@ spikeMatrix = unPaddedBinnedEnsembleMtx(dsVect,:,:); trialTime = trialTimes(dsVect); +clear trialEnsembleMtx binnedEnsembleMtx unPaddedBinnedEnsembleMtx %% Create Logical Vectors perfLog = [trialPeriodTD.Performance]; inSeqLog = [trialPeriodTD.TranspositionDistance]==0; @@ -68,11 +70,14 @@ fullInSeqLog = false(1,length(trialPeriodTD)); fullInSeqLog(inSeqSeqs(:)) = true; +nonAIStrialData = trialPeriodTD(perfLog & inSeqLog & ~fullInSeqLog); %% uniFRthreshLog = max(mean(spikeMatrix,3))<1; spkMtx = spikeMatrix; spkMtx(:,uniFRthreshLog,:) = []; goodUniNames = {ensembleUnitSummaries(~uniFRthreshLog).UnitName}; + +clear spikeMatrix %% Decode Trial Time Across Odors figure; corrAisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorAlog),3); % All A InSeq Correct Trials @@ -111,7 +116,6 @@ %% Decode Trial Time and Odor Across Odors nonAIStrials = spkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog); -nonAIStrialData = trialPeriodTD(perfLog & inSeqLog & ~fullInSeqLog); nonAISodors = [nonAIStrialData.Odor]; corrAisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorAlog),3); % All A InSeq Correct Trials Prior corrBisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorBlog),3); % All B InSeq Correct Trials Prior @@ -247,7 +251,6 @@ %% Compare Trial Before with Trial After OutSeq Trial % nonAIStrials = spkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog); -% nonAIStrialData = trialPeriodTD(perfLog & inSeqLog & ~fullInSeqLog); % nonAISodors = [nonAIStrialData.Odor]; % nonAIStrialNums = [nonAIStrialData.TrialNum]; % @@ -316,22 +319,22 @@ %% -function [postNorm, postRaw] = CalculatePostProb(prior, obsv, binSize) +function [postNorm, postRaw] = CalculatePostProb(likely, obsv, binSize) % propVect = 1./sum(prior,2); % Probably wrong -postNorm = nan(size(obsv,1), size(obsv,1), size(obsv,3)); -postRaw = nan(size(obsv,1), size(obsv,1), size(obsv,3)); +postNorm = nan(size(obsv,1), size(likely,1), size(obsv,3)); +postRaw = nan(size(obsv,1), size(likely,1), size(obsv,3)); for trl = 1:size(obsv,3) - for t = 1:size(prior,1) - p = nan(size(prior)); + for t = 1:size(obsv,1) + p = nan(size(likely)); curPopVect = obsv(t,:,trl)./(1000/binSize); curPopFact = factorial(curPopVect); - for u = 1:size(prior,2) - curAvgUniFR = prior(:,u); + for u = 1:size(likely,2) + curAvgUniFR = likely(:,u); p(:,u) = (((binSize/1000).*curAvgUniFR).^curPopVect(u))./curPopFact(u); % p(:,u) = ((curAvgUniFR).^curPopVect(u))./curPopFact(u); end pp = prod(p,2); - ee = exp(-(binSize/1000*sum(prior,2))); + ee = exp(-(binSize/1000*sum(likely,2))); % ee = exp(-(sum(prior,2))); % tempPost = propVect.*pp.*ee; % Probably wrong tempPost = pp.*ee; From 3158f0fafca1239e23b050af6f14eb90b8b29c3d Mon Sep 17 00:00:00 2001 From: gabe Date: Tue, 12 Nov 2019 12:30:26 -0800 Subject: [PATCH 037/133] Update BayesPFCbsPROTO.m --- Analyses/Prototypes/BayesPFCbsPROTO.m | 1 + 1 file changed, 1 insertion(+) diff --git a/Analyses/Prototypes/BayesPFCbsPROTO.m b/Analyses/Prototypes/BayesPFCbsPROTO.m index db8007f..fdd5071 100644 --- a/Analyses/Prototypes/BayesPFCbsPROTO.m +++ b/Analyses/Prototypes/BayesPFCbsPROTO.m @@ -100,6 +100,7 @@ subplot(2,2,4); dCaxis = PlotPostMtx(trialTimes, postDnorm, 'Odor D'); + cAx = [min([aCaxis, bCaxis, cCaxis, dCaxis]), max([aCaxis, bCaxis, cCaxis, dCaxis])/3]; annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... From 0263ffe8d871194ebb40b670dd9f2b70ec978a0f Mon Sep 17 00:00:00 2001 From: gabe Date: Tue, 12 Nov 2019 13:59:53 -0800 Subject: [PATCH 038/133] Update BayesPFCbsPROTO.m --- Analyses/Prototypes/BayesPFCbsPROTO.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Analyses/Prototypes/BayesPFCbsPROTO.m b/Analyses/Prototypes/BayesPFCbsPROTO.m index fdd5071..41fafa5 100644 --- a/Analyses/Prototypes/BayesPFCbsPROTO.m +++ b/Analyses/Prototypes/BayesPFCbsPROTO.m @@ -21,9 +21,9 @@ %% Extract Behavioral Periods % Taking 1/2 the binSize on either end to get rid of edge effects. -trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [0-(binSize/2/1000) 0.5+(binSize/2/1000)], 'PokeIn'); -trialPeriodTD = trialPeriodTD(1:end-1); -% trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-1.5-(binSize/2/1000) 0.5+(binSize/2/1000)], 'PokeOut'); +% trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [0-(binSize/2/1000) 1.5+(binSize/2/1000)], 'PokeIn'); +% trialPeriodTD = trialPeriodTD(1:end-1); +trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-1.5-(binSize/2/1000) 0.5+(binSize/2/1000)], 'PokeOut'); trialEnsemble = ExtractTrialData_SM(trialPeriodTD, ensembleMatrix(:,2:end)); %#ok<*NODEF> trialEnsembleMtx = cell2mat(reshape(trialEnsemble, [1 1 length(trialEnsemble)])); From 6b03a740c0933db862219e24dfefb59ba4b4abb5 Mon Sep 17 00:00:00 2001 From: gaelias Date: Wed, 11 Dec 2019 14:58:01 -0800 Subject: [PATCH 039/133] Local changes --- Analyses/Prototypes/BayesPFCbsPROTO.m | 357 +++++++------- Analyses/Prototypes/BayesPFCbsPROTO_2.m | 2 +- Analyses/SequenceNeuralViewer_SM.m | 6 +- Analyses/SequenceViewerNeural_SM.m | 627 ++++++++++++++++++++++++ 4 files changed, 810 insertions(+), 182 deletions(-) create mode 100644 Analyses/SequenceViewerNeural_SM.m diff --git a/Analyses/Prototypes/BayesPFCbsPROTO.m b/Analyses/Prototypes/BayesPFCbsPROTO.m index 86fb502..f768357 100644 --- a/Analyses/Prototypes/BayesPFCbsPROTO.m +++ b/Analyses/Prototypes/BayesPFCbsPROTO.m @@ -3,7 +3,7 @@ clear all %% Runtime variables -binSize = 100; +binSize = 50; dsRate = 5; %% @@ -21,7 +21,7 @@ %% Extract Behavioral Periods % Taking 1/2 the binSize on either end to get rid of edge effects. -trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.5-(binSize/2/1000) 1.5+(binSize/2/1000)], 'PokeIn'); +trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [0-(binSize/2/1000) 1.2+(binSize/2/1000)], 'PokeIn'); % trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-1.5-(binSize/2/1000) 0.5+(binSize/2/1000)], 'PokeOut'); trialEnsemble = ExtractTrialData_SM(trialPeriodTD, ensembleMatrix(:,2:end)); %#ok<*NODEF> trialEnsembleMtx = cell2mat(reshape(trialEnsemble, [1 1 length(trialEnsemble)])); @@ -110,186 +110,186 @@ orient(gcf, 'landscape'); %% Decode Trial Time and Odor Across Odors -nonAIStrials = spkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog); -nonAIStrialData = trialPeriodTD(perfLog & inSeqLog & ~fullInSeqLog); -nonAISodors = [nonAIStrialData.Odor]; -corrAisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorAlog),3); % All A InSeq Correct Trials Prior -corrBisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorBlog),3); % All B InSeq Correct Trials Prior -corrCisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorClog),3); % All C InSeq Correct Trials Prior -corrDisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorDlog),3); % All D InSeq Correct Trials Prior - -figure; -oAfr = subplot(4,4,1); -imagesc(trialTimes, 1:size(spkMtx,2), corrAisMtx'); -title('Odor A'); -abCorr = subplot(4,4,2); -corrScatPlot(corrAisMtx(:),corrBisMtx(:), 'A','B', 'markerStyle', '.', 'markerColor', 'k'); -acCorr = subplot(4,4,3); -corrScatPlot(corrAisMtx(:),corrCisMtx(:), 'A','C', 'markerStyle', '.', 'markerColor', 'k'); -adCorr = subplot(4,4,4); -corrScatPlot(corrAisMtx(:),corrDisMtx(:), 'A','B', 'markerStyle', '.', 'markerColor', 'k'); - -oBfr = subplot(4,4,6); -imagesc(trialTimes, 1:size(spkMtx,2), corrBisMtx'); -title('Odor B'); -bcCorr = subplot(4,4,7); -corrScatPlot(corrBisMtx(:),corrCisMtx(:), 'B','C', 'markerStyle', '.', 'markerColor', 'k'); -bdCorr = subplot(4,4,8); -corrScatPlot(corrBisMtx(:),corrDisMtx(:), 'B','D', 'markerStyle', '.', 'markerColor', 'k'); - -oCfr = subplot(4,4,11); -imagesc(trialTimes, 1:size(spkMtx,2), corrCisMtx'); -title('Odor C'); -cdCorr = subplot(4,4,12); -corrScatPlot(corrCisMtx(:),corrDisMtx(:), 'C','D', 'markerStyle', '.', 'markerColor', 'k'); - -oDfr = subplot(4,4,16); -imagesc(trialTimes, 1:size(spkMtx,2), corrDisMtx'); -title('Odor D'); - -frMapSPs = [oAfr, oBfr, oCfr, oDfr]; -curCL = cell2mat(get(frMapSPs, 'clim')); -set(frMapSPs, 'clim', [min(curCL(:,1)), max(curCL(:,2))*.5], 'ydir', 'normal'); -corrSPs = [abCorr, acCorr, adCorr, bcCorr, bdCorr, cdCorr]; -curXL = cell2mat(get(corrSPs, 'xlim')); -curYL = cell2mat(get(corrSPs, 'ylim')); -set(corrSPs, 'xlim', [0 max(curXL(:,2))], 'ylim', [0 max(curYL(:,2))]); - -axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); -axis(axesHandles,'square'); - -annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... - 'linestyle', 'none', 'horizontalalignment', 'right'); -annotation('textbox', 'position', [0.025 0.935 0.5 0.05], 'String', '\bf\fontsize{14}Priors',... - 'linestyle', 'none', 'horizontalalignment', 'left'); -curDir = cd; -annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... - 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); -colormap jet -orient(gcf, 'tall'); -orient(gcf, 'landscape'); -drawnow; - - -[~, aPriorAllPostRaw] = CalculatePostProb(corrAisMtx, nonAIStrials, binSize); -[~, bPriorAllPostRaw] = CalculatePostProb(corrBisMtx, nonAIStrials, binSize); -[~, cPriorAllPostRaw] = CalculatePostProb(corrCisMtx, nonAIStrials, binSize); -[~, dPriorAllPostRaw] = CalculatePostProb(corrDisMtx, nonAIStrials, binSize); -nonFISaLog = perfLog & inSeqLog & odorAlog & ~fullInSeqLog; -nonFISbLog = perfLog & inSeqLog & odorBlog & ~fullInSeqLog; -nonFIScLog = perfLog & inSeqLog & odorClog & ~fullInSeqLog; -nonFISdLog = perfLog & inSeqLog & odorDlog & ~fullInSeqLog; - -allNonFISpost = [aPriorAllPostRaw, bPriorAllPostRaw, cPriorAllPostRaw, dPriorAllPostRaw]; -for c = 1:size(allNonFISpost,1) - allNonFISpost(c,:,:) = allNonFISpost(c,:,:)./sum(allNonFISpost(c,:,:)); -end - -aPriorAllPostNorm = allNonFISpost(:,1:size(aPriorAllPostRaw,1),:); -bPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)+1:size(aPriorAllPostRaw,1)*2,:); -cPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)*2+1:size(aPriorAllPostRaw,1)*3,:); -dPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)*3+1:end,:); - -figure; -cAx = nan(4,4,2); -for prior = 1:4 - switch prior - case 1 - curPrior = aPriorAllPostNorm; - case 2 - curPrior = bPriorAllPostNorm; - case 3 - curPrior = cPriorAllPostNorm; - case 4 - curPrior = dPriorAllPostNorm; - end - for post = 1:4 - curPostLog = nonAISodors == post; - curDecode = curPrior(:,:,curPostLog); - subplot(4,4,sub2ind([4 4], post, prior)) - cAx(prior,post,:) = PlotPostMtx(trialTimes, curDecode, sprintf('Prior%i; Decode%i', prior, post)); - end -end -cAxs = [min(min(cAx(:,:,1))), max(max(cAx(:,:,2)))*.5]; - -annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... - 'linestyle', 'none', 'horizontalalignment', 'right'); -annotation('textbox', 'position', [0.025 0.935 0.5 0.05], 'String', '\bf\fontsize{14}Decoding Trial and Odor Across Odors',... - 'linestyle', 'none', 'horizontalalignment', 'left'); -curDir = cd; -annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... - 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); -colormap jet -axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); -axis(axesHandles,'square') -set(axesHandles, 'clim', cAxs); -orient(gcf, 'tall'); -orient(gcf, 'landscape'); -drawnow; +% nonAIStrials = spkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog); +% nonAIStrialData = trialPeriodTD(perfLog & inSeqLog & ~fullInSeqLog); +% nonAISodors = [nonAIStrialData.Odor]; +% corrAisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorAlog),3); % All A InSeq Correct Trials Prior +% corrBisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorBlog),3); % All B InSeq Correct Trials Prior +% corrCisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorClog),3); % All C InSeq Correct Trials Prior +% corrDisMtx = mean(spkMtx(:,:,perfLog & fullInSeqLog & odorDlog),3); % All D InSeq Correct Trials Prior +% +% figure; +% oAfr = subplot(4,4,1); +% imagesc(trialTimes, 1:size(spkMtx,2), corrAisMtx'); +% title('Odor A'); +% abCorr = subplot(4,4,2); +% corrScatPlot(corrAisMtx(:),corrBisMtx(:), 'A','B', 'markerStyle', '.', 'markerColor', 'k'); +% acCorr = subplot(4,4,3); +% corrScatPlot(corrAisMtx(:),corrCisMtx(:), 'A','C', 'markerStyle', '.', 'markerColor', 'k'); +% adCorr = subplot(4,4,4); +% corrScatPlot(corrAisMtx(:),corrDisMtx(:), 'A','B', 'markerStyle', '.', 'markerColor', 'k'); +% +% oBfr = subplot(4,4,6); +% imagesc(trialTimes, 1:size(spkMtx,2), corrBisMtx'); +% title('Odor B'); +% bcCorr = subplot(4,4,7); +% corrScatPlot(corrBisMtx(:),corrCisMtx(:), 'B','C', 'markerStyle', '.', 'markerColor', 'k'); +% bdCorr = subplot(4,4,8); +% corrScatPlot(corrBisMtx(:),corrDisMtx(:), 'B','D', 'markerStyle', '.', 'markerColor', 'k'); +% +% oCfr = subplot(4,4,11); +% imagesc(trialTimes, 1:size(spkMtx,2), corrCisMtx'); +% title('Odor C'); +% cdCorr = subplot(4,4,12); +% corrScatPlot(corrCisMtx(:),corrDisMtx(:), 'C','D', 'markerStyle', '.', 'markerColor', 'k'); +% +% oDfr = subplot(4,4,16); +% imagesc(trialTimes, 1:size(spkMtx,2), corrDisMtx'); +% title('Odor D'); +% +% frMapSPs = [oAfr, oBfr, oCfr, oDfr]; +% curCL = cell2mat(get(frMapSPs, 'clim')); +% set(frMapSPs, 'clim', [min(curCL(:,1)), max(curCL(:,2))*.5], 'ydir', 'normal'); +% corrSPs = [abCorr, acCorr, adCorr, bcCorr, bdCorr, cdCorr]; +% curXL = cell2mat(get(corrSPs, 'xlim')); +% curYL = cell2mat(get(corrSPs, 'ylim')); +% set(corrSPs, 'xlim', [0 max(curXL(:,2))], 'ylim', [0 max(curYL(:,2))]); +% +% axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +% axis(axesHandles,'square'); +% +% annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... +% 'linestyle', 'none', 'horizontalalignment', 'right'); +% annotation('textbox', 'position', [0.025 0.935 0.5 0.05], 'String', '\bf\fontsize{14}Priors',... +% 'linestyle', 'none', 'horizontalalignment', 'left'); +% curDir = cd; +% annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... +% 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +% colormap jet +% orient(gcf, 'tall'); +% orient(gcf, 'landscape'); +% drawnow; +% +% +% [~, aPriorAllPostRaw] = CalculatePostProb(corrAisMtx, nonAIStrials, binSize); +% [~, bPriorAllPostRaw] = CalculatePostProb(corrBisMtx, nonAIStrials, binSize); +% [~, cPriorAllPostRaw] = CalculatePostProb(corrCisMtx, nonAIStrials, binSize); +% [~, dPriorAllPostRaw] = CalculatePostProb(corrDisMtx, nonAIStrials, binSize); +% nonFISaLog = perfLog & inSeqLog & odorAlog & ~fullInSeqLog; +% nonFISbLog = perfLog & inSeqLog & odorBlog & ~fullInSeqLog; +% nonFIScLog = perfLog & inSeqLog & odorClog & ~fullInSeqLog; +% nonFISdLog = perfLog & inSeqLog & odorDlog & ~fullInSeqLog; +% +% allNonFISpost = [aPriorAllPostRaw, bPriorAllPostRaw, cPriorAllPostRaw, dPriorAllPostRaw]; +% for c = 1:size(allNonFISpost,1) +% allNonFISpost(c,:,:) = allNonFISpost(c,:,:)./sum(allNonFISpost(c,:,:)); +% end +% +% aPriorAllPostNorm = allNonFISpost(:,1:size(aPriorAllPostRaw,1),:); +% bPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)+1:size(aPriorAllPostRaw,1)*2,:); +% cPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)*2+1:size(aPriorAllPostRaw,1)*3,:); +% dPriorAllPostNorm = allNonFISpost(:,size(aPriorAllPostRaw,1)*3+1:end,:); +% +% figure; +% cAx = nan(4,4,2); +% for prior = 1:4 +% switch prior +% case 1 +% curPrior = aPriorAllPostNorm; +% case 2 +% curPrior = bPriorAllPostNorm; +% case 3 +% curPrior = cPriorAllPostNorm; +% case 4 +% curPrior = dPriorAllPostNorm; +% end +% for post = 1:4 +% curPostLog = nonAISodors == post; +% curDecode = curPrior(:,:,curPostLog); +% subplot(4,4,sub2ind([4 4], post, prior)) +% cAx(prior,post,:) = PlotPostMtx(trialTimes, curDecode, sprintf('Prior%i; Decode%i', prior, post)); +% end +% end +% cAxs = [min(min(cAx(:,:,1))), max(max(cAx(:,:,2)))*.5]; +% +% annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... +% 'linestyle', 'none', 'horizontalalignment', 'right'); +% annotation('textbox', 'position', [0.025 0.935 0.5 0.05], 'String', '\bf\fontsize{14}Decoding Trial and Odor Across Odors',... +% 'linestyle', 'none', 'horizontalalignment', 'left'); +% curDir = cd; +% annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... +% 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +% colormap jet +% axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +% axis(axesHandles,'square') +% set(axesHandles, 'clim', cAxs); +% orient(gcf, 'tall'); +% orient(gcf, 'landscape'); +% drawnow; %% -figure -corrISmtx = mean(spkMtx(:,:,perfLog & fullInSeqLog),3); % All InSeq Correct Trials -[post,~] = CalculatePostProb(corrISmtx, spkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog), binSize); -PlotPostMtx(trialTimes, post, 'InSeq Correct Trials'); - -annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... - 'linestyle', 'none', 'horizontalalignment', 'right'); -curDir = cd; -annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... - 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); -colormap jet -axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); -axis(axesHandles,'square') -orient(gcf, 'tall'); -orient(gcf, 'landscape'); -drawnow; +% figure +% corrISmtx = mean(spkMtx(:,:,perfLog & fullInSeqLog),3); % All InSeq Correct Trials +% [post,~] = CalculatePostProb(corrISmtx, spkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog), binSize); +% PlotPostMtx(trialTimes, post, 'InSeq Correct Trials'); +% +% annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... +% 'linestyle', 'none', 'horizontalalignment', 'right'); +% curDir = cd; +% annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... +% 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +% colormap jet +% axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +% axis(axesHandles,'square') +% orient(gcf, 'tall'); +% orient(gcf, 'landscape'); +% drawnow; %% Compare Trial Before with Trial After OutSeq Trial -nonAIStrials = spkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog); -nonAIStrialData = trialPeriodTD(perfLog & inSeqLog & ~fullInSeqLog); -nonAISodors = [nonAIStrialData.Odor]; -nonAIStrialNums = [nonAIStrialData.TrialNum]; - -preOStrial = false(size(nonAIStrialNums)); -postOStrial = false(size(nonAIStrialNums)); -for trl = 1:length(nonAIStrialNums) - if nonAIStrialNums(trl)~=1 && nonAIStrialNums(trl) ~= length(trialPeriodTD) - prevTrial = trialPeriodTD(nonAIStrialNums(trl)-1); - curTrl = trialPeriodTD(nonAIStrialNums(trl)); - nextTrial = trialPeriodTD(nonAIStrialNums(trl)+1); - if curTrl.Position ~= 1 - if curTrl.Position - prevTrial.Position == 1 && prevTrial.TranspositionDistance ~= 0 - postOStrial(trl) = true; - preOStrial(trl-2) = true; - end - end - end -end -postPostOS = post(:,:,postOStrial); -postPreOS = post(:,:,preOStrial); - -figure; -subplot(1,2,1) -PlotPostMtx(trialTimes, postPreOS, 'Trial Before OutSeq'); - -subplot(1,2,2) -PlotPostMtx(trialTimes, postPostOS, 'Trial After OutSeq'); - -annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... - 'linestyle', 'none', 'horizontalalignment', 'right'); -curDir = cd; -annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... - 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); -colormap jet -axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); -axis(axesHandles,'square') -cLims = cell2mat(get(axesHandles, 'cLim')); -set(axesHandles, 'cLim', [min(cLims(:,1)), max(cLims(:,2))]); -orient(gcf, 'tall'); -orient(gcf, 'landscape'); -drawnow; - +% nonAIStrials = spkMtx(:,:,perfLog & inSeqLog & ~fullInSeqLog); +% nonAIStrialData = trialPeriodTD(perfLog & inSeqLog & ~fullInSeqLog); +% nonAISodors = [nonAIStrialData.Odor]; +% nonAIStrialNums = [nonAIStrialData.TrialNum]; +% +% preOStrial = false(size(nonAIStrialNums)); +% postOStrial = false(size(nonAIStrialNums)); +% for trl = 1:length(nonAIStrialNums) +% if nonAIStrialNums(trl)~=1 && nonAIStrialNums(trl) ~= length(trialPeriodTD) +% prevTrial = trialPeriodTD(nonAIStrialNums(trl)-1); +% curTrl = trialPeriodTD(nonAIStrialNums(trl)); +% nextTrial = trialPeriodTD(nonAIStrialNums(trl)+1); +% if curTrl.Position ~= 1 +% if curTrl.Position - prevTrial.Position == 1 && prevTrial.TranspositionDistance ~= 0 +% postOStrial(trl) = true; +% preOStrial(trl-2) = true; +% end +% end +% end +% end +% postPostOS = post(:,:,postOStrial); +% postPreOS = post(:,:,preOStrial); +% +% figure; +% subplot(1,2,1) +% PlotPostMtx(trialTimes, postPreOS, 'Trial Before OutSeq'); +% +% subplot(1,2,2) +% PlotPostMtx(trialTimes, postPostOS, 'Trial After OutSeq'); +% +% annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... +% 'linestyle', 'none', 'horizontalalignment', 'right'); +% curDir = cd; +% annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... +% 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +% colormap jet +% axesHandles = findobj(get(gcf,'Children'), 'flat','Type','axes'); +% axis(axesHandles,'square') +% cLims = cell2mat(get(axesHandles, 'cLim')); +% set(axesHandles, 'cLim', [min(cLims(:,1)), max(cLims(:,2))]); +% orient(gcf, 'tall'); +% orient(gcf, 'landscape'); +% drawnow; +% %% Comment in if you want to display each trial individually @@ -303,6 +303,7 @@ %% function cAx = PlotPostMtx(trialTimes, postMtx, id) +postMtx(isnan(postMtx)) = 0; imagesc(trialTimes, trialTimes, nanmean(postMtx,3)); title(id); set(gca, 'ydir', 'normal') diff --git a/Analyses/Prototypes/BayesPFCbsPROTO_2.m b/Analyses/Prototypes/BayesPFCbsPROTO_2.m index 318293b..9a8a44d 100644 --- a/Analyses/Prototypes/BayesPFCbsPROTO_2.m +++ b/Analyses/Prototypes/BayesPFCbsPROTO_2.m @@ -176,7 +176,7 @@ cLimsDecode = get([piPlot, poPlot, prOsPIplt, poOsPIplt, prOsPOplt, poOsPOplt], 'clim'); cLimsDiff = get([piDiffPlt, poDiffPlt], 'clim'); -set([piPlot, poPlot, prOsPIplt, poOsPIplt, prOsPOplt, poOsPOplt], 'clim', [0 max(max(cell2mat(cLimsDecode)))]); +set([piPlot, poPlot, prOsPIplt, poOsPIplt, prOsPOplt, poOsPOplt], 'clim', [0 max(max(cell2mat(cLimsDecode)))\2]); set([piDiffPlt, poDiffPlt], 'clim', [0 max(max(cell2mat(cLimsDiff)))]); colormap jet diff --git a/Analyses/SequenceNeuralViewer_SM.m b/Analyses/SequenceNeuralViewer_SM.m index c673869..dfb37e5 100644 --- a/Analyses/SequenceNeuralViewer_SM.m +++ b/Analyses/SequenceNeuralViewer_SM.m @@ -4,11 +4,11 @@ % Histogram bins for peak FR latency decision histBins = 0:0.1:1.2; % FR threshold for trial period firing for inclusion in plot -trialFRthresh = 1; +trialFRthresh = 0.1; % Spike width threshold for PC determination -widthThresh = 0.45; +widthThresh = 0.6; % Spike rate threshold for PC determination -spkRtThresh = 5; +spkRtThresh = 4; % LFP Band to plot band2plot = 'Raw'; %% Load data diff --git a/Analyses/SequenceViewerNeural_SM.m b/Analyses/SequenceViewerNeural_SM.m new file mode 100644 index 0000000..6c726fe --- /dev/null +++ b/Analyses/SequenceViewerNeural_SM.m @@ -0,0 +1,627 @@ +% SequenceViewerNeural_SM +close all +clear all %#ok +clc +%% Initialize The Data For the Figure +global svnFig figAxes lfpPlotHandle lfpBand2Plot rasterHandles pCellList intCellList numSeqs seqWindows curSeqNumInput behavMatrixTrialStruct smFile +% Get the file +[smFile,smPath] = uigetfile('*.mat'); +if smPath==0 + return; +end +% Change directory +cd(smPath); +% Load the file +load(smFile); +% Identify the LFP columns +lfpNdxNd = cellfun(@(a)regexp(a,'_LFP_', 'end'), statMatrixColIDs, 'uniformoutput', 0); +% Identify the hilbert transform columns... +hilbCols = cell2mat(cellfun(@(a)~isempty(a), strfind(statMatrixColIDs, 'HilbVals'), 'uniformoutput', 0)); +lfpColLog = cellfun(@(a)~isempty(a), lfpNdxNd); +% ... so they can be removed from the selection +lfpColLog(hilbCols) = false; +lfpData = statMatrix(:,lfpColLog); +% Now identify the band names so they can be put in the list +lfpNdxNd(~lfpColLog) = []; +lfpColIDs = statMatrixColIDs(lfpColLog); +lfpBandNames = cellfun(@(a,b)a(b+1:end),lfpColIDs, lfpNdxNd, 'uniformoutput', 0)'; + +% Load the behavior and ensemble matrices +files = dir(smPath); +fileNames = {files.name}; +behMatFile = fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'BehaviorMatrix'))}; +nsmblMatFile = fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'EnsembleMatrix'))}; +load([smPath behMatFile]); +load([smPath nsmblMatFile]); +% Extract spike times and IDs for each unit +unitSpikeTimes = cell(size(ensembleMatrix,2)-1,1); +for uni = 2:size(ensembleMatrix,2) + unitSpikeTimes{uni-1} = ensembleMatrix(ensembleMatrix(:,uni)~=0,1); +end +unitIDs = ensembleMatrixColIDs(:,2:end)'; + +% To find the time window to use we're going to identify how long the rat +% stayed in the port on average for InSeq correct trials and use that +% window as our timewindow for alinging things to. +% First pull out the poke in indices +trlStruct = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [0 0], 'PokeIn'); +% Pull out relevant logicals +seqLog = [trlStruct.TranspositionDistance] == 0; +perfLog = [trlStruct.Performance] == 1; +% Extract sequence beginning and end points with 1s padding on either side +numSeqs = length(unique([trlStruct.SequenceNum])); +seqWindows = nan(numSeqs,2); +for seq = 1:numSeqs + curSeq = trlStruct([trlStruct.SequenceNum]==seq); + seqWindows(seq,1) = behavMatrix(curSeq(1).PokeInIndex-5000,1); + seqWindows(seq,2) = behavMatrix(curSeq(end).PokeOutIndex+5000,1); +end +% Now determine the mean poke duration +pokeDur = [trlStruct.PokeOutIndex] - [trlStruct.PokeInIndex]; +meanPokeDurISC = mean(pokeDur(seqLog & perfLog))/1000; +trialWindows = [0 ceil(meanPokeDurISC*10)/10]; +% Use the Organize/Extract functions to pull out and organize the trial data +behavMatrixTrialStruct = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, trialWindows, 'PokeIn'); +ensembleTrialData = ExtractTrialData_SM(behavMatrixTrialStruct, ensembleMatrix(:,2:end)); %#ok<*NODEF> + +% Make a timestamp vector for use with the trial relative spiking data +trialTSvect = behavMatrix(behavMatrixTrialStruct(1).TrialLogVect,1)-behavMatrix(behavMatrixTrialStruct(1).PokeInIndex,1); +% Create a timestamp vector +histBins = trialWindows(1):0.1:trialWindows(2); +trialEnsembleDataBinned = nan(length(histBins)-1, length(unitIDs), length(ensembleTrialData)); +for tr = 1:length(ensembleTrialData) + for uni = 1:length(unitIDs) + trialEnsembleDataBinned(:,uni,tr) = histcounts(trialTSvect(logical(ensembleTrialData{tr}(:,uni))), histBins, 'Normalization', 'CountDensity'); + end +end + +plotData = struct('TrialEnsembleBinned', {trialEnsembleDataBinned},... + 'UnitIDs', {unitIDs},... + 'UnitSpikeTimes', {unitSpikeTimes},... + 'SessionTime', {statMatrix(:,1)},... + 'InSeqLog', {[trlStruct.TranspositionDistance]==0},... + 'TrialOdor', {[trlStruct.Odor]},... + 'TrialPosition', {[trlStruct.Position]},... + 'Performance', {[trlStruct.Performance]},... + 'UnitInfo', {ensembleUnitSummaries}); +%% +svnFig = figure; +figAxes = axes(svnFig, 'position', [0.3, 0.2, 0.65, 0.75]); +set(svnFig, 'userData', plotData); + +ensembleData = DetermineEnsembleOrganization; + +[pcData, intData] = SeparatePCandINTs; + +%% Make the initial Figure +lfpVals = statMatrix(:,2); +lfpVals = (lfpVals/(max(abs(lfpVals))))*2; +lfpPlotHandle = plot(figAxes, statMatrix(:,1), lfpVals, 'color', [0.4 0.4 0.4]); +hold on; +rasterHandles = nan(length(unitIDs)+3,1); +for r = 1:length(rasterHandles) + rasterHandles(r) = scatter(figAxes,1,(r+1)*-1, 'markerfacecolor', 'k', 'markeredgecolor', 'none'); +end +UpdateRasters(pcData); +UpdateRasters(intData); + +for trl = 1:length(behavMatrixTrialStruct) + switch behavMatrixTrialStruct(trl).Odor + case 1 + patchColor = [44/255 168/255 224/255]; + case 2 + patchColor = [154/255 133/255 122/255]; + case 3 + patchColor = [9/255 161/255 74/255]; + case 4 + patchColor = [128/255 66/255 151/255]; + case 5 + patchColor = 'k'; + end + patch(gca, 'XData', [behavMatrix(behavMatrixTrialStruct(trl).PokeInIndex,1), behavMatrix(behavMatrixTrialStruct(trl).PokeOutIndex,1), behavMatrix(behavMatrixTrialStruct(trl).PokeOutIndex,1),behavMatrix(behavMatrixTrialStruct(trl).PokeInIndex,1)],... + 'YData', [ones(1,2), repmat(intData{end,1}+1,[1,2])*-1],... + 'FaceColor', patchColor, 'FaceAlpha', 0.15,... + 'EdgeColor', patchColor); +end +refreshdata(svnFig); + +axis tight +title(figAxes, smFile, 'interpreter', 'none'); +set(figAxes, 'yticklabel', []); +set(figAxes, 'xlim', seqWindows(1,:)); + +%% Create the Rest of the Figure +% Choose LFP File Button +chooseLFPfileBtn = uicontrol(svnFig, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', 'Choose LFP File',... + 'Position', [0.05,0.9,0.2,0.05], 'Tag', 'chooseLFPfile_Btn', 'Callback', @ChooseLFPfile); +% LFP Band Listbox +lfpBandList = axes(svnFig, 'position', [0.05, 0.875, 0.08, 0.05]); +set(lfpBandList, 'xlim', [-0.5 0.5], 'ylim', [0 0.5]); +text(lfpBandList, 0,0, 'LFP Data Options', 'horizontalalignment', 'center', 'verticalalignment', 'bottom'); +axis(lfpBandList, 'off'); +lfpBand2Plot = uicontrol(svnFig, 'Units', 'Normalized', 'Style', 'listbox', 'String', lfpBandNames,... + 'Position', [0.05,0.6,0.08,0.275], 'Tag', 'bandToPlot', 'Callback', @ChooseLFPbandToPlot, 'userData', lfpData); + +% Temporal Organization Button Group +tempOrgMethod = uibuttongroup(svnFig, 'Units', 'Normalized', 'Position', [0.05, 0.43, 0.085, 0.15],... + 'Title', 'Organization Method', 'TitlePosition', 'centertop', 'Tag', 'orgMthd_BtnGrp',... + 'SelectionChangedFcn', @ChangeTempOrgMethod); +% Temp Org by Mean A +meanAorg = uicontrol(tempOrgMethod, 'Units', 'Normalized', 'Style', 'radiobutton', 'String', 'Mean Odor A',... + 'Position', [0.05, 0.7, 0.9, 0.2], 'Tag', 'orgMeanA_PB'); +% Temp Org by Mean InSeq +meanISorg = uicontrol(tempOrgMethod, 'Units', 'Normalized', 'Style', 'radiobutton', 'String', 'Mean InSeq',... + 'Position', [0.05, 0.4, 0.9, 0.2], 'Tag', 'orgMeanIS_PB'); +% Temp Org by Non A InSeq +meanISsfpOrg = uicontrol(tempOrgMethod, 'Units', 'Normalized', 'Style', 'radiobutton', 'String', 'IS-SFP',... + 'Position', [0.05, 0.1, 0.9, 0.2], 'Tag', 'orgMeanISsfp_PB'); + +% Criteria for PC/IN Classification +cellTypeWidthClassify = uicontrol(svnFig, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', 'Spike Width Org',... + 'Position', [0.05, 0.35, 0.052, 0.06], 'Tag', 'classCellWidth', 'Callback', @SpikeWidthOrganize); +cellTypeWidthThresh = uicontrol(svnFig, 'Units', 'Normalized', 'Style', 'edit', 'String', '0.6',... + 'Position', [0.105, 0.35, 0.025, 0.06]); + +cellTypeRateClassify = uicontrol(svnFig, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', 'Spike Rate Org',... + 'Position', [0.05 0.29, 0.052, 0.06], 'Tag', 'classCellRate', 'Callback', @SpikeRateOrganize); +cellTypeRateThresh = uicontrol(svnFig, 'Units', 'Normalized', 'Style', 'edit', 'String', '5',... + 'Position', [0.105, 0.29, 0.025, 0.06]); + +% Principal Cells List +pcListTitleAx = axes(svnFig, 'position', [0.14, 0.875, 0.11, 0.05]); +set(pcListTitleAx, 'xlim', [-0.5 0.5], 'ylim', [0 0.5]); +text(pcListTitleAx, 0,0, 'Principal Cells', 'horizontalalignment', 'center', 'verticalalignment', 'bottom'); +axis(pcListTitleAx, 'off'); +pCellList = uicontrol(svnFig, 'Units', 'Normalized', 'Style', 'listbox', 'String', pcData(:,3),... + 'Position', [0.14, 0.575, 0.11, 0.3], 'Tag', 'pcCells_Lst', 'Callback', @SelectPC, 'userData', pcData); + +% Move PC to Int list +movePCtoInt = uicontrol(svnFig, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', 'PC>>Int',... + 'Position', [0.14, 0.5175, 0.05, 0.05], 'Callback', @PCtoINT); +% Move Int to PC list +moveInttoPC = uicontrol(svnFig, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', 'PC<>',... + 'Position', [0.75, 0.05, 0.2, 0.1], 'Callback', @NextSeq); + +% Sequence Number +curSeqNumInput = uicontrol(svnFig, 'Units', 'Normalized', 'Style', 'edit', 'String', '1',... + 'Position', [0.575, 0.075, 0.05, 0.05], 'Callback', @SelectSeq); +totalSeqNumAxs = axes('position', [0.625, 0.075, 0.05, 0.05]); +set(totalSeqNumAxs, 'xlim', [0 0.5], 'ylim', [-0.5 0.5]); +seqText = text(totalSeqNumAxs, 0,0, sprintf('/%i', numSeqs), 'horizontalalignment', 'left', 'verticalalignment', 'middle'); +axis(totalSeqNumAxs, 'off'); + +%% +function [spikeData] = DetermineEnsembleOrganization(orgType) +global svnFig +plotData = get(svnFig, 'userData'); +spikeTimes = plotData.UnitSpikeTimes; +unitIDs = plotData.UnitIDs; +unitInfo = arrayfun(@(a){a}, plotData.UnitInfo); +if nargin==0 + orgType = 1; +end +switch orgType + case 1 + trialLog = plotData.TrialOdor==1 & plotData.TrialPosition==1 & plotData.Performance==1; + case 2 + trialLog = plotData.InSeqLog==1 & plotData.Performance==1; + case 3 + trialLog = plotData.InSeqLog==1 & plotData.Performance==1 & plotData.TrialOdor~=1; +end +trialHist = mean(plotData.TrialEnsembleBinned(:,:,trialLog),3)'; +normHist = trialHist./max(trialHist,[],2); +silentCellLog = sum(isnan(normHist),2)==size(normHist,2); +lowFRlog = sum(trialHist<=1,2)==size(trialHist,2); silentCellLog = silentCellLog | lowFRlog; +spikeTimes(silentCellLog) = []; +unitIDs(silentCellLog) = []; +normHist(silentCellLog,:) = []; +unitInfo(:,silentCellLog) = []; +latency = cell(sum(~silentCellLog),1); +for u = 1:size(normHist,1) + latency{u} = find(normHist(u,:)==1,1,'first'); +end +spikeData = [latency, unitIDs, spikeTimes, unitInfo']; +plotData.SpikeData = spikeData; +set(svnFig, 'userData', plotData); +end +%% +function [pcData, intData] = SeparatePCandINTs(orgType, orgVal) +global svnFig +plotData = get(svnFig, 'userData'); +spikeData = plotData.SpikeData; +uniFR = cellfun(@(a)a.Mean_SpikeRate, spikeData(:,4)); +uniSW = cellfun(@(a)a.Spike_Width, spikeData(:,4))*40; +if nargin==0 + orgType = 'Width'; + orgVal = 0.6; +end + +switch orgType + case 'Width' + pcLog = uniSW>=orgVal; + case 'Rate' + pcLog = uniFR<=orgVal; +end + +% Select and Organize Putative Principal Cells +pcData = spikeData(pcLog,:); +pcSortVect = sortrows([cell2mat(pcData(:,1)), [1:sum(pcLog)]']); %#ok +pcLines = 2:size(pcData,1)+1; +pcData = [num2cell(pcLines'), pcData(pcSortVect(:,2),:)]; + +% Select and Organize Putative Interneurons +intData = spikeData(~pcLog,:); +intSortVect = sortrows([cell2mat(intData(:,1)), [1:sum(~pcLog)]']); %#ok +intLines = pcLines(end)+3:pcLines(end)+2+size(intData,1); +intData = [num2cell(intLines'), intData(intSortVect(:,2),:)]; +end +%% +function UpdateRasters(dataMtx) +global rasterHandles +for u = 1:size(dataMtx,1) + set(rasterHandles(dataMtx{u,1}), 'Xdata', dataMtx{u,4}, 'Ydata', ones(1,length(dataMtx{u,4})).*(dataMtx{u,1}*-1)); +end +end +%% +function ChooseLFPfile(source,event) +global svnFig figAxes lfpPlotHandle lfpBand2Plot rasterHandles pCellList intCellList numSeqs seqWindows curSeqNumInput +[smFile,smPath] = uigetfile('*.mat'); +if smPath==0 + return; +end + +if strcmp(smPath, [cd '\']) + load(smFile); + lfpNdxNd = cellfun(@(a)regexp(a,'_LFP_', 'end'), statMatrixColIDs, 'uniformoutput', 0); + hilbCols = cell2mat(cellfun(@(a)~isempty(a), strfind(statMatrixColIDs, 'HilbVals'), 'uniformoutput', 0)); + lfpColLog = cellfun(@(a)~isempty(a), lfpNdxNd); + lfpColLog(hilbCols) = false; + lfpData = statMatrix(:,lfpColLog); + lfpNdxNd(~lfpColLog) = []; + lfpColIDs = statMatrixColIDs(lfpColLog); + lfpBandNames = cellfun(@(a,b)a(b+1:end),lfpColIDs, lfpNdxNd, 'uniformoutput', 0)'; + set(lfpBand2Plot, 'Value', 1); + set(lfpBand2Plot, 'String', lfpBandNames); + set(lfpBand2Plot, 'userData', lfpData); + ChooseLFPbandToPlot + title(figAxes, smFile); +end + +end + +%% +function ChooseLFPbandToPlot(source,event) +global lfpPlotHandle lfpBand2Plot +lfpData = get(lfpBand2Plot, 'userData'); +band2Plot = get(lfpBand2Plot, 'value'); +lfpData2Plot = lfpData(:,band2Plot); +lfpData2Plot = (lfpData2Plot/(max(abs(lfpData2Plot))))*2; +set(lfpPlotHandle, 'Ydata', lfpData2Plot*-1); +end + +%% +function SpikeWidthOrganize(source,event) +error('Make me!') +end +%% +function SpikeRateOrganize(source,event) +error('Make me!') +end +%% +function SelectPC(source,event) +global rasterHandles pCellList +pData = get(pCellList, 'userData'); +curListPos = get(pCellList, 'Value'); +if curListPos>size(pData,1) + curListPos = size(pData,1); +end +set(rasterHandles(cell2mat(pData(:,1))), 'MarkerEdgeColor', 'none', 'MarkerFaceColor', 'k'); +set(rasterHandles(pData{curListPos,1}), 'MarkerFaceColor', 'r'); +end +%% +function RemovePC(source,event) +global rasterHandles pCellList intCellList +pData = get(pCellList, 'userData'); +pStart = pData{1,1}; +iData = get(intCellList, 'userData'); +curListPos = get(pCellList, 'Value'); + +% Remove Index +pData(curListPos,:) = []; +% Resort PCs +pcSortMtx = sortrows([cell2mat(pData(:,2)), [1:size(pData,1)]']); %#ok +pData = pData(pcSortMtx(:,2),:); +pData(:,1) = num2cell(pStart:size(pData,1)+1); +% Resort Ints +intSortMtx = sortrows([cell2mat(iData(:,2)), [1:size(iData,1)]']); %#ok +iData = iData(intSortMtx(:,2),:); +iData(:,1) = num2cell(pData{end,1}+3:pData{end,1}+2+size(iData,1)); + +UpdateRasters(pData); +UpdateRasters(iData); +set(pCellList, 'userData', pData); +set(intCellList, 'userData', iData); +set(pCellList, 'String', pData(:,3)); +if curListPos<=size(pData,1) + set(pCellList, 'Value', curListPos-1); +elseif curListPos > size(pData,1) + set(pCellList, 'Value', size(pData,1)); +end + +set(rasterHandles(~ismember(1:length(rasterHandles), cell2mat([pData(:,1);iData(:,1)]))), 'Xdata', 1, 'Ydata', 1); +SelectPC +end +%% +function SelectInt(source,event) +global rasterHandles intCellList +intData = get(intCellList, 'userData'); +curListPos = get(intCellList, 'Value'); +if curListPos>size(intData,1) || isempty(curListPos) + curListPos = size(intData,1); +end + +set(rasterHandles(cell2mat(intData(:,1))), 'MarkerEdgeColor', 'none', 'MarkerFaceColor', 'k'); +set(rasterHandles(intData{curListPos,1}), 'MarkerFaceColor', [0.1 0.8 0.1]); +end +%% +function RemoveINT(source,event) +global intCellList pCellList rasterHandles +pData = get(pCellList, 'userData'); +pStart = pData{1,1}; +iData = get(intCellList, 'userData'); +curListPos = get(intCellList, 'Value'); + +iData(curListPos,:) = []; +% Resort PCs +pcSortMtx = sortrows([cell2mat(pData(:,2)), [1:size(pData,1)]']); %#ok +pData = pData(pcSortMtx(:,2),:); +pData(:,1) = num2cell(pStart:size(pData,1)+1); +% Resort Ints +intSortMtx = sortrows([cell2mat(iData(:,2)), [1:size(iData,1)]']); %#ok +iData = iData(intSortMtx(:,2),:); +iData(:,1) = num2cell(pData{end,1}+3:pData{end,1}+2+size(iData,1)); +UpdateRasters(iData); +UpdateRasters(pData); +set(pCellList, 'userData', pData); +set(intCellList, 'userData', iData); +set(intCellList, 'String', iData(:,3)); +if curListPos<=length(pData) + set(intCellList, 'Value', curListPos-1); +elseif curListPos > size(pData,1) + set(intCellList, 'Value', size(pData,1)); +end +set(rasterHandles(~ismember(1:length(rasterHandles), cell2mat([pData(:,1);iData(:,1)]))), 'Xdata', 1, 'Ydata', 1); +SelectInt +end +%% +function PCtoINT(source, event) +global rasterHandles pCellList intCellList +pData = get(pCellList, 'userData'); +pStart = pData{1,1}; +curPC = get(pCellList, 'Value'); +pStr = get(pCellList, 'String'); +cell2MoveStr = pStr(curPC); +iData = get(intCellList, 'userData'); + + +% Modify Lists +iData = [iData;pData(curPC,:)]; +pData(curPC,:) = []; + +% Reorganize PCs +pcSortMtx = sortrows([cell2mat(pData(:,2)), [1:size(pData,1)]']); %#ok +pData = pData(pcSortMtx(:,2),:); +pData(:,1) = num2cell(pStart:size(pData,1)+1); +for p = 1:size(pData,1) + set(rasterHandles(pData{p,1}), 'Xdata', pData{p,4}, 'Ydata', ones(1,length(pData{p,4})).*(pData{p,1}*-1)); +end +set(pCellList, 'String', pData(:,3)); + +% Reorganize INTs +intSortMtx = sortrows([cell2mat(iData(:,2)), [1:size(iData,1)]']); %#ok +iData = iData(intSortMtx(:,2),:); +iData(:,1) = num2cell(pData{end,1}+3:pData{end,1}+2+size(iData,1)); +for i = 1:size(iData,1) + set(rasterHandles(iData{i,1}), 'Xdata', iData{i,4}, 'Ydata', ones(1,length(iData{i,4})).*(iData{i,1}*-1)); +end +set(intCellList, 'String', iData(:,3)); + +set(pCellList, 'userData', pData); +set(intCellList, 'userData', iData); +set(intCellList, 'Value', find(strcmp(iData(:,3), cell2MoveStr))); +if curPC > 1 + set(pCellList, 'Value', curPC-1); +end + +nonDataLog = ~ismember(1:length(rasterHandles), cell2mat([pData(:,1); iData(:,1)])); +set(rasterHandles(nonDataLog), 'Xdata', 1, 'Ydata', 1); +SelectInt +SelectPC +end +%% +function INTtoPC(source, event) +global rasterHandles pCellList intCellList +pData = get(pCellList, 'userData'); +iData = get(intCellList, 'userData'); +curInt = get(intCellList, 'Value'); +iStr = get(intCellList, 'String'); +cell2MoveStr = iStr(curInt); + +% Modify Lists +pData = [pData;iData(curInt,:)]; +iData(curInt,:) = []; + +% Reorganize PCs +pcSortMtx = sortrows([cell2mat(pData(:,2)), [1:size(pData,1)]']); %#ok +pData = pData(pcSortMtx(:,2),:); +pData(:,1) = num2cell(pData{1,1}:size(pData,1)+1); +for p = 1:size(pData,1) + set(rasterHandles(pData{p,1}), 'Xdata', pData{p,4}, 'Ydata', ones(1,length(pData{p,4})).*(pData{p,1}*-1)); +end + +% Reorganize INTs +intSortMtx = sortrows([cell2mat(iData(:,2)), [1:size(iData,1)]']); %#ok +iData = iData(intSortMtx(:,2),:); +iData(:,1) = num2cell(pData{end,1}+3:pData{end,1}+2+size(iData,1)); +for i = 1:size(iData,1) + set(rasterHandles(iData{i,1}), 'Xdata', iData{i,4}, 'Ydata', ones(1,length(iData{i,4})).*(iData{i,1}*-1)); +end + +set(pCellList, 'userData', pData); +set(pCellList, 'String', pData(:,3)); +set(intCellList, 'userData', iData); +set(intCellList, 'String', iData(:,3)); +set(pCellList, 'Value', find(strcmp(pData(:,3), cell2MoveStr))); +if curInt > 1 + set(intCellList, 'Value', curInt-1); +end + +nonDataLog = ~ismember(1:length(rasterHandles), cell2mat([pData(:,1); iData(:,1)])); +set(rasterHandles(nonDataLog), 'Xdata', 1, 'Ydata', 1); +SelectPC +SelectInt +end +%% +function ChangeTempOrgMethod(source,event) +global figAxes pCellList intCellList rasterHandles +orgMethodVal = logical(cell2mat(get(get(source, 'Children'), 'Value'))); +orgMethodTag = get(get(source, 'Children'), 'Tag'); +curMethod = orgMethodTag{orgMethodVal}; +switch curMethod + case 'orgMeanA_PB' + [spikeData] = DetermineEnsembleOrganization(1); + case 'orgMeanIS_PB' + [spikeData] = DetermineEnsembleOrganization(2); + case 'orgMeanISsfp_PB' + [spikeData] = DetermineEnsembleOrganization(3); +end +iData = get(intCellList, 'userData'); +intRmvLog = ismember(spikeData(:,2), iData(:,3)); +pData = spikeData; +pData(intRmvLog,:) = []; +pSortVect = sortrows([cell2mat(pData(:,1)), [1:size(pData,1)]']); %#ok +pLines = 2:size(pData,1)+1; +pData = [num2cell(pLines'), pData(pSortVect(:,2),:)]; +UpdateRasters(pData); +set(pCellList, 'userData', pData); +set(pCellList, 'String', pData(:,3)); + +for i = 1:size(iData,1) + newSpikeData = spikeData(strcmp(iData{i,3}, spikeData(:,2)),:); + if isempty(newSpikeData) + iData{i,1} = nan; + else + iData(i,2:end) = newSpikeData; + end +end +nanCheck = isnan(cell2mat(iData(:,1))); +iData(nanCheck,:) = []; +iData = iData(:,2:end); +iSortVect = sortrows([cell2mat(iData(:,1)), [1:size(iData,1)]']); %#ok +iLines = pLines(end)+3:pLines(end)+2+size(iData,1); +iData = [num2cell(iLines'), iData(iSortVect(:,2),:)]; +UpdateRasters(iData); +set(figAxes, 'ylim', [(iData{end,1}+1)*-1 1]); +set(intCellList, 'userData', iData); +set(intCellList, 'String', iData(:,3)); +SelectPC +SelectInt + +set(rasterHandles(~ismember(1:length(rasterHandles), cell2mat([pData(:,1);iData(:,1)]))), 'Xdata', 1, 'Ydata', 1); +end + +%% +function PrevSeq(source,event) +global seqWindows curSeqNumInput figAxes +curSeqNum = str2double(get(curSeqNumInput, 'String')); +if curSeqNum - 1 >= 1 + set(curSeqNumInput, 'String', num2str(curSeqNum-1)); + set(figAxes, 'xlim', seqWindows(curSeqNum-1,:)); +end +end +%% +function NextSeq(source,event) +global numSeqs seqWindows curSeqNumInput figAxes +curSeqNum = str2double(get(curSeqNumInput, 'String')); +if curSeqNum + 1 <= numSeqs + set(curSeqNumInput, 'String', num2str(curSeqNum+1)); + set(figAxes, 'xlim', seqWindows(curSeqNum+1,:)); +end +end +%% +function SelectSeq(source,event) +global numSeqs seqWindows curSeqNumInput figAxes +curSeqNum = str2double(get(curSeqNumInput, 'String')); +if curSeqNum <= numSeqs + set(curSeqNumInput, 'String', num2str(curSeqNum)); + set(figAxes, 'xlim', seqWindows(curSeqNum,:)); +end +end +%% +function PopoutPlot(source,event) +global svnFig lfpBand2Plot pCellList intCellList seqWindows curSeqNumInput behavMatrixTrialStruct smFile +plotData = get(svnFig, 'userData'); +poFig = figure; +lfpData = get(lfpBand2Plot, 'userData'); +curLFP = lfpData(:,get(lfpBand2Plot, 'Value')); +curLFP = (curLFP/(max(abs(curLFP))))*2; +plot(plotData.SessionTime, curLFP, 'color', [0.4 0.4 0.4]); +hold on; +pData = get(pCellList, 'userData'); +iData = get(intCellList, 'userData'); + +scatterData = [pData(:,1), pData(:,4); iData(:,1), iData(:,4)]; +for u = 1:size(scatterData,1) + scatter(scatterData{u,2}, ones(size(scatterData{u,2})).*scatterData{u,1}*-1, 'markerfacecolor', 'k', 'markeredgecolor', 'none'); +end + +for trl = 1:length(behavMatrixTrialStruct) + switch behavMatrixTrialStruct(trl).Odor + case 1 + patchColor = [44/255 168/255 224/255]; + case 2 + patchColor = [154/255 133/255 122/255]; + case 3 + patchColor = [9/255 161/255 74/255]; + case 4 + patchColor = [128/255 66/255 151/255]; + case 5 + patchColor = 'k'; + end + patch(gca, 'XData', [plotData.SessionTime(behavMatrixTrialStruct(trl).PokeInIndex,1), plotData.SessionTime(behavMatrixTrialStruct(trl).PokeOutIndex,1), plotData.SessionTime(behavMatrixTrialStruct(trl).PokeOutIndex,1),plotData.SessionTime(behavMatrixTrialStruct(trl).PokeInIndex,1)],... + 'YData', [ones(1,2), repmat(iData{end,1}+1,[1,2])*-1],... + 'FaceColor', patchColor, 'FaceAlpha', 0.15,... + 'EdgeColor', patchColor); +end +set(gca, 'xlim', seqWindows(str2double(get(curSeqNumInput, 'String')),:), 'ylim', [-1*iData{end,1}-1, 2]); +title(sprintf('%s; Sequence %s', smFile, get(curSeqNumInput, 'String')), 'interpreter', 'none'); +orient(gcf, 'tall'); +orient(gcf, 'landscape'); +end \ No newline at end of file From e5e66d9b56f4d0d37fbd77bf5d59db9117d3c1d2 Mon Sep 17 00:00:00 2001 From: gabe Date: Wed, 11 Dec 2019 17:16:30 -0800 Subject: [PATCH 040/133] Create BayesPFCproto_VALID.m --- Analyses/Prototypes/BayesPFCproto_VALID.m | 243 ++++++++++++++++++++++ 1 file changed, 243 insertions(+) create mode 100644 Analyses/Prototypes/BayesPFCproto_VALID.m diff --git a/Analyses/Prototypes/BayesPFCproto_VALID.m b/Analyses/Prototypes/BayesPFCproto_VALID.m new file mode 100644 index 0000000..8b4142b --- /dev/null +++ b/Analyses/Prototypes/BayesPFCproto_VALID.m @@ -0,0 +1,243 @@ +%% +clc +clear all + +%% Runtime variables +binSize = 50; +dsRate = 5; + +%% +smPath = uigetdir; +cd(smPath); +files = dir(smPath); +fileNames = {files.name}; +% Load the behavior matrix file for poke events plots +behMatFile = fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'BehaviorMatrix'))}; +nsmblMatFile = fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'EnsembleMatrix'))}; +load([smPath '\' behMatFile]); +load([smPath '\' nsmblMatFile]); +% Identify list of all statMatrix files +smFileList = fileNames(cellfun(@(a)~isempty(a), regexp(fileNames, '_SM\>')))'; + +withdrawTime = 1.1; +%% Extract Behavioral Periods +% Taking 1/2 the binSize on either end to get rid of edge effects. +trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.4-(binSize/2/1000) withdrawTime+0.5+(binSize/2/1000)], 'PokeIn'); +% trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-1.5-(binSize/2/1000) 0.5+(binSize/2/1000)], 'PokeOut'); +trialEnsemble = ExtractTrialData_SM(trialPeriodTD, ensembleMatrix(:,2:end)); %#ok<*NODEF> +trialEnsembleMtx = cell2mat(reshape(trialEnsemble, [1 1 length(trialEnsemble)])); + +trialTimes = behavMatrix(trialPeriodTD(1).TrialLogVect,1) - behavMatrix(trialPeriodTD(1).PokeInIndex,1); +% trialTimes = behavMatrix(trialPeriodTD(1).TrialLogVect,1) - behavMatrix(trialPeriodTD(1).PokeOutIndex,1); + +%% Bin the spiking data +% First convolve the entire trialEnsembleMtx with a square to bin the +% spikes +binnedEnsembleMtx = nan(size(trialEnsembleMtx)); +for t = 1:size(trialEnsembleMtx,3) + for u = 1:size(trialEnsembleMtx,2) + binnedEnsembleMtx(:,u,t) = conv(trialEnsembleMtx(:,u,t), ones(1,binSize)./(binSize/1000), 'same'); +% binnedEnsembleMtx(:,u,t) = conv(trialEnsembleMtx(:,u,t), ones(1,binSize), 'same'); + end +end +% Now remove the binSize/2 padding +unPaddedBinnedEnsembleMtx = binnedEnsembleMtx((binSize/2)+1:end-(binSize/2),:,:); +trialTimes = trialTimes((binSize/2)+1:end-(binSize/2)); +% Now downsample the binned matrix +dsVect = downsample(1:size(unPaddedBinnedEnsembleMtx,1), dsRate); +spikeMatrix = unPaddedBinnedEnsembleMtx(dsVect,:,:); +trialTime = trialTimes(dsVect); + +%% Create Logical Vectors +perfLog = [trialPeriodTD.Performance]; +inSeqLog = [trialPeriodTD.TranspositionDistance]==0; +outSeqLog = [trialPeriodTD.TranspositionDistance]~=0 & abs([trialPeriodTD.TranspositionDistance])<10; +odorAlog = [trialPeriodTD.Odor] == 1; +odorBlog = [trialPeriodTD.Odor] == 2; +odorClog = [trialPeriodTD.Odor] == 3; +odorDlog = [trialPeriodTD.Odor] == 4; + +fullInSeqSeqsStart = find(conv([trialPeriodTD.Odor], 1:4, 'valid')==20 & conv([trialPeriodTD.Position], 1:4, 'valid')==20 & conv([trialPeriodTD.Performance]*1, ones(1,4), 'valid')==4); +inSeqSeqs = nan(3,length(fullInSeqSeqsStart)); +for iS = 1:length(fullInSeqSeqsStart) + inSeqSeqs(1,iS) = fullInSeqSeqsStart(iS); + inSeqSeqs(2,iS) = fullInSeqSeqsStart(iS) + 1; + inSeqSeqs(3,iS) = fullInSeqSeqsStart(iS) + 2; + inSeqSeqs(4,iS) = fullInSeqSeqsStart(iS) + 3; +end +fullInSeqLog = false(1,length(trialPeriodTD)); +fullInSeqLog(inSeqSeqs(:)) = true; + +%% Create Standard Likelihood Vector +fullyISlikelyCell = repmat({nan(size(spikeMatrix,1), size(spikeMatrix,2))}, 4,1); +odorCell = cell(4,1); +for p = 1:4 + fullyISlikelyCell{p} = mean(spikeMatrix(:,:,inSeqSeqs(p,:)),3); + odorCell{p} = ones(size(fullyISlikelyCell{p},1),1)*p; +end +fullyISlikely = cell2mat(fullyISlikelyCell); +timeIndexes = repmat(trialTime,[4,1]); +odorIndexes = cell2mat(odorCell); + +xTickNdxs = find(timeIndexes==0 | timeIndexes==min(timeIndexes) | timeIndexes==min(trialTime(trialTime>=withdrawTime))); +odorDivs = find(timeIndexes==min(timeIndexes)); +odorTargs = find(timeIndexes==min(trialTime(trialTime>=withdrawTime))); +odorStarts = find(timeIndexes==0); + +%% +figure +subplot(4,5,1:4); +aPost = CalcStaticBayesPost(fullyISlikely, spikeMatrix(:,:,[trialPeriodTD.Odor]==1 & ~fullInSeqLog & perfLog & inSeqLog), binSize); +imagesc(1:length(timeIndexes), trialTime, nanmean(aPost,3),[0 0.0025]); +hold on; +set(gca, 'ydir', 'normal', 'xtick', xTickNdxs, 'xticklabel', repmat([-0.4 0 withdrawTime],[1,4])); +for p = 1:4 + plot([odorDivs(p) odorDivs(p)], get(gca, 'ylim'), '-w', 'linewidth', 2); + plot([odorTargs(p) odorTargs(p)], get(gca, 'ylim'), ':w', 'linewidth', 1.5); + plot([odorStarts(p) odorStarts(p)], get(gca, 'ylim'), '--w', 'linewidth', 1.5); +end +aDecode = DecodeBayesPost(aPost, odorIndexes); +aDecodePrcnts = nan(size(aDecode,1),4); +for t = 1:size(aDecode,1) + aDecodePrcnts(t,1) = sum(aDecode(t,:)==1)/sum(~isnan(aDecode(t,:))); + aDecodePrcnts(t,2) = sum(aDecode(t,:)==2)/sum(~isnan(aDecode(t,:))); + aDecodePrcnts(t,3) = sum(aDecode(t,:)==3)/sum(~isnan(aDecode(t,:))); + aDecodePrcnts(t,4) = sum(aDecode(t,:)==4)/sum(~isnan(aDecode(t,:))); +end +subplot(4,5,5); +curBar = barh(trialTime,aDecodePrcnts, 1, 'stacked'); +curBar(1).FaceColor = [44/255 168/255 224/255]; +curBar(2).FaceColor = [154/255 133/255 122/255]; +curBar(3).FaceColor = [9/255 161/255 74/255]; +curBar(4).FaceColor = [128/255 66/255 151/255]; +set(gca, 'xlim', [0 1], 'xticklabel', []); +hold on +plot(get(gca, 'xlim'), [0 0], '--w'); +plot(get(gca, 'xlim'), [withdrawTime withdrawTime], ':w'); +box off +legend('A', 'B', 'C', 'D'); +drawnow; +subplot(4,5,6:9); +bPost = CalcStaticBayesPost(fullyISlikely, spikeMatrix(:,:,[trialPeriodTD.Odor]==2 & ~fullInSeqLog & perfLog & inSeqLog), binSize); +imagesc(1:length(timeIndexes), trialTime, nanmean(bPost,3),[0 0.0025]); +hold on; +set(gca, 'ydir', 'normal', 'xtick', xTickNdxs, 'xticklabel', repmat([-0.4 0 withdrawTime],[1,4])); +for p = 1:4 + plot([odorDivs(p) odorDivs(p)], get(gca, 'ylim'), '-w', 'linewidth', 2); + plot([odorTargs(p) odorTargs(p)], get(gca, 'ylim'), ':w', 'linewidth', 1.5); + plot([odorStarts(p) odorStarts(p)], get(gca, 'ylim'), '--w', 'linewidth', 1.5); +end +bDecode = DecodeBayesPost(bPost, odorIndexes); +bDecodePrcnts = nan(size(bDecode,1),4); +for t = 1:size(aDecode,1) + bDecodePrcnts(t,1) = sum(bDecode(t,:)==1)/sum(~isnan(bDecode(t,:))); + bDecodePrcnts(t,2) = sum(bDecode(t,:)==2)/sum(~isnan(bDecode(t,:))); + bDecodePrcnts(t,3) = sum(bDecode(t,:)==3)/sum(~isnan(bDecode(t,:))); + bDecodePrcnts(t,4) = sum(bDecode(t,:)==4)/sum(~isnan(bDecode(t,:))); +end +subplot(4,5,10); +curBar = barh(trialTime,bDecodePrcnts, 1, 'stacked'); +curBar(1).FaceColor = [44/255 168/255 224/255]; +curBar(2).FaceColor = [154/255 133/255 122/255]; +curBar(3).FaceColor = [9/255 161/255 74/255]; +curBar(4).FaceColor = [128/255 66/255 151/255]; +set(gca, 'xlim', [0 1], 'xticklabel', []); +hold on +plot(get(gca, 'xlim'), [0 0], '--w'); +plot(get(gca, 'xlim'), [withdrawTime withdrawTime], ':w'); +box off +drawnow; +subplot(4,5,11:14); +cPost = CalcStaticBayesPost(fullyISlikely, spikeMatrix(:,:,[trialPeriodTD.Odor]==3 & ~fullInSeqLog & perfLog & inSeqLog), binSize); +imagesc(1:length(timeIndexes), trialTime, nanmean(cPost,3),[0 0.0025]); +hold on; +set(gca, 'ydir', 'normal', 'xtick', xTickNdxs, 'xticklabel', repmat([-0.4 0 withdrawTime],[1,4])); +for p = 1:4 + plot([odorDivs(p) odorDivs(p)], get(gca, 'ylim'), '-w', 'linewidth', 2); + plot([odorTargs(p) odorTargs(p)], get(gca, 'ylim'), ':w', 'linewidth', 1.5); + plot([odorStarts(p) odorStarts(p)], get(gca, 'ylim'), '--w', 'linewidth', 1.5); +end +cDecode = DecodeBayesPost(cPost, odorIndexes); +cDecodePrcnts = nan(size(cDecode,1),4); +for t = 1:size(aDecode,1) + cDecodePrcnts(t,1) = sum(cDecode(t,:)==1)/sum(~isnan(cDecode(t,:))); + cDecodePrcnts(t,2) = sum(cDecode(t,:)==2)/sum(~isnan(cDecode(t,:))); + cDecodePrcnts(t,3) = sum(cDecode(t,:)==3)/sum(~isnan(cDecode(t,:))); + cDecodePrcnts(t,4) = sum(cDecode(t,:)==4)/sum(~isnan(cDecode(t,:))); +end +subplot(4,5,15); +curBar = barh(trialTime,cDecodePrcnts, 1, 'stacked'); +curBar(1).FaceColor = [44/255 168/255 224/255]; +curBar(2).FaceColor = [154/255 133/255 122/255]; +curBar(3).FaceColor = [9/255 161/255 74/255]; +curBar(4).FaceColor = [128/255 66/255 151/255]; +set(gca, 'xlim', [0 1], 'xticklabel', []); +hold on +plot(get(gca, 'xlim'), [0 0], '--w'); +plot(get(gca, 'xlim'), [withdrawTime withdrawTime], ':w'); +box off +drawnow; +subplot(4,5,16:19); +dPost = CalcStaticBayesPost(fullyISlikely, spikeMatrix(:,:,[trialPeriodTD.Odor]==4 & ~fullInSeqLog & perfLog & inSeqLog), binSize); +imagesc(1:length(timeIndexes), trialTime, nanmean(dPost,3),[0 0.0025]); +hold on; +set(gca, 'ydir', 'normal', 'xtick', xTickNdxs, 'xticklabel', repmat([-0.4 0 withdrawTime],[1,4])); +for p = 1:4 + plot([odorDivs(p) odorDivs(p)], get(gca, 'ylim'), '-w', 'linewidth', 2); + plot([odorTargs(p) odorTargs(p)], get(gca, 'ylim'), ':w', 'linewidth', 1.5); + plot([odorStarts(p) odorStarts(p)], get(gca, 'ylim'), '--w', 'linewidth', 1.5); +end +dDecode = DecodeBayesPost(dPost, odorIndexes); +dDecodePrcnts = nan(size(dDecode,1),4); +for t = 1:size(aDecode,1) + dDecodePrcnts(t,1) = sum(dDecode(t,:)==1)/sum(~isnan(dDecode(t,:))); + dDecodePrcnts(t,2) = sum(dDecode(t,:)==2)/sum(~isnan(dDecode(t,:))); + dDecodePrcnts(t,3) = sum(dDecode(t,:)==3)/sum(~isnan(dDecode(t,:))); + dDecodePrcnts(t,4) = sum(dDecode(t,:)==4)/sum(~isnan(dDecode(t,:))); +end +subplot(4,5,20); +curBar = barh(trialTime,dDecodePrcnts, 1, 'stacked'); +curBar(1).FaceColor = [44/255 168/255 224/255]; +curBar(2).FaceColor = [154/255 133/255 122/255]; +curBar(3).FaceColor = [9/255 161/255 74/255]; +curBar(4).FaceColor = [128/255 66/255 151/255]; +set(gca, 'xlim', [0 1], 'xticklabel', []); +hold on +plot(get(gca, 'xlim'), [0 0], '--w'); +plot(get(gca, 'xlim'), [withdrawTime withdrawTime], ':w'); +box off +drawnow; + +colormap jet +%% +function decode = DecodeBayesPost(post, id) +% Assumes post is in the structure of ObservTime X LikelyTime X Trial +decode = nan(size(post,1),size(post,3)); +for o = 1:size(post,3) + for d = 1:size(post,1) + if ~isnan(post(d,1,o)) + decode(d,o) = id(find(post(d,:,o)==max(post(d,:,o)),1,'first')); + end + end +end +end + +%% +function post = CalcStaticBayesPost(likely, obsv, binSize) +post = nan(size(obsv,1), size(likely,1), size(obsv,3)); +for trl = 1:size(obsv,3) + for t = 1:size(obsv,1) + p = nan(size(likely)); + curPopVect = obsv(t,:,trl)*(binSize/1000); + curPopFact = factorial(curPopVect); + for u = 1:size(likely,2) + curAvgUniFR = likely(:,u); + p(:,u) = (((binSize/1000).*curAvgUniFR).^curPopVect(u))./curPopFact(u); + end + pp = prod(p,2); + ee = exp(-((binSize/1000)*sum(likely,2))); + tempPost = pp.*ee; + post(t,:,trl) = tempPost./sum(tempPost); + end +end +end \ No newline at end of file From e5393055872005a1467fe3d54ad434ef7fa521d3 Mon Sep 17 00:00:00 2001 From: gabe Date: Thu, 12 Dec 2019 14:31:57 -0800 Subject: [PATCH 041/133] Update BayesPFCproto_VALID.m --- Analyses/Prototypes/BayesPFCproto_VALID.m | 74 +++++++++++++---------- 1 file changed, 42 insertions(+), 32 deletions(-) diff --git a/Analyses/Prototypes/BayesPFCproto_VALID.m b/Analyses/Prototypes/BayesPFCproto_VALID.m index 8b4142b..f5d3911 100644 --- a/Analyses/Prototypes/BayesPFCproto_VALID.m +++ b/Analyses/Prototypes/BayesPFCproto_VALID.m @@ -3,9 +3,11 @@ clear all %% Runtime variables -binSize = 50; -dsRate = 5; - +window = [-10 10]; +binSize = 200; +dsRate = 10; +cLims = [0 0.0025]; +withdrawTime = 1; %% smPath = uigetdir; cd(smPath); @@ -19,11 +21,9 @@ % Identify list of all statMatrix files smFileList = fileNames(cellfun(@(a)~isempty(a), regexp(fileNames, '_SM\>')))'; -withdrawTime = 1.1; %% Extract Behavioral Periods % Taking 1/2 the binSize on either end to get rid of edge effects. -trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.4-(binSize/2/1000) withdrawTime+0.5+(binSize/2/1000)], 'PokeIn'); -% trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-1.5-(binSize/2/1000) 0.5+(binSize/2/1000)], 'PokeOut'); +trialPeriodTD = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [window(1)-(binSize/2/1000) withdrawTime+window(2)+(binSize/2/1000)], 'PokeIn'); trialEnsemble = ExtractTrialData_SM(trialPeriodTD, ensembleMatrix(:,2:end)); %#ok<*NODEF> trialEnsembleMtx = cell2mat(reshape(trialEnsemble, [1 1 length(trialEnsemble)])); @@ -79,8 +79,8 @@ timeIndexes = repmat(trialTime,[4,1]); odorIndexes = cell2mat(odorCell); -xTickNdxs = find(timeIndexes==0 | timeIndexes==min(timeIndexes) | timeIndexes==min(trialTime(trialTime>=withdrawTime))); -odorDivs = find(timeIndexes==min(timeIndexes)); +xTickNdxs = find(timeIndexes==0 | timeIndexes==min(trialTime(trialTime>=window(1))) | timeIndexes==min(trialTime(trialTime>=withdrawTime))); +odorDivs = find(timeIndexes==min(trialTime(trialTime>=window(1)))); odorTargs = find(timeIndexes==min(trialTime(trialTime>=withdrawTime))); odorStarts = find(timeIndexes==0); @@ -88,14 +88,15 @@ figure subplot(4,5,1:4); aPost = CalcStaticBayesPost(fullyISlikely, spikeMatrix(:,:,[trialPeriodTD.Odor]==1 & ~fullInSeqLog & perfLog & inSeqLog), binSize); -imagesc(1:length(timeIndexes), trialTime, nanmean(aPost,3),[0 0.0025]); +imagesc(1:length(timeIndexes), trialTime, nanmean(aPost,3),cLims); hold on; -set(gca, 'ydir', 'normal', 'xtick', xTickNdxs, 'xticklabel', repmat([-0.4 0 withdrawTime],[1,4])); +set(gca, 'ydir', 'normal', 'xtick', [], 'xticklabel', [], 'TickDir', 'out'); for p = 1:4 plot([odorDivs(p) odorDivs(p)], get(gca, 'ylim'), '-w', 'linewidth', 2); plot([odorTargs(p) odorTargs(p)], get(gca, 'ylim'), ':w', 'linewidth', 1.5); plot([odorStarts(p) odorStarts(p)], get(gca, 'ylim'), '--w', 'linewidth', 1.5); end +plot([0 length(timeIndexes)], [0 0], '-w', 'linewidth', 1); aDecode = DecodeBayesPost(aPost, odorIndexes); aDecodePrcnts = nan(size(aDecode,1),4); for t = 1:size(aDecode,1) @@ -106,10 +107,10 @@ end subplot(4,5,5); curBar = barh(trialTime,aDecodePrcnts, 1, 'stacked'); -curBar(1).FaceColor = [44/255 168/255 224/255]; -curBar(2).FaceColor = [154/255 133/255 122/255]; -curBar(3).FaceColor = [9/255 161/255 74/255]; -curBar(4).FaceColor = [128/255 66/255 151/255]; +curBar(1).FaceColor = [44/255 168/255 224/255]; curBar(1).EdgeColor = 'none'; +curBar(2).FaceColor = [154/255 133/255 122/255]; curBar(2).EdgeColor = 'none'; +curBar(3).FaceColor = [9/255 161/255 74/255]; curBar(3).EdgeColor = 'none'; +curBar(4).FaceColor = [128/255 66/255 151/255]; curBar(4).EdgeColor = 'none'; set(gca, 'xlim', [0 1], 'xticklabel', []); hold on plot(get(gca, 'xlim'), [0 0], '--w'); @@ -119,14 +120,15 @@ drawnow; subplot(4,5,6:9); bPost = CalcStaticBayesPost(fullyISlikely, spikeMatrix(:,:,[trialPeriodTD.Odor]==2 & ~fullInSeqLog & perfLog & inSeqLog), binSize); -imagesc(1:length(timeIndexes), trialTime, nanmean(bPost,3),[0 0.0025]); +imagesc(1:length(timeIndexes), trialTime, nanmean(bPost,3),cLims); hold on; -set(gca, 'ydir', 'normal', 'xtick', xTickNdxs, 'xticklabel', repmat([-0.4 0 withdrawTime],[1,4])); +set(gca, 'ydir', 'normal', 'xtick', [], 'xticklabel', [], 'TickDir', 'out'); for p = 1:4 plot([odorDivs(p) odorDivs(p)], get(gca, 'ylim'), '-w', 'linewidth', 2); plot([odorTargs(p) odorTargs(p)], get(gca, 'ylim'), ':w', 'linewidth', 1.5); plot([odorStarts(p) odorStarts(p)], get(gca, 'ylim'), '--w', 'linewidth', 1.5); end +plot([0 length(timeIndexes)], [0 0], '-w', 'linewidth', 1); bDecode = DecodeBayesPost(bPost, odorIndexes); bDecodePrcnts = nan(size(bDecode,1),4); for t = 1:size(aDecode,1) @@ -137,10 +139,10 @@ end subplot(4,5,10); curBar = barh(trialTime,bDecodePrcnts, 1, 'stacked'); -curBar(1).FaceColor = [44/255 168/255 224/255]; -curBar(2).FaceColor = [154/255 133/255 122/255]; -curBar(3).FaceColor = [9/255 161/255 74/255]; -curBar(4).FaceColor = [128/255 66/255 151/255]; +curBar(1).FaceColor = [44/255 168/255 224/255]; curBar(1).EdgeColor = 'none'; +curBar(2).FaceColor = [154/255 133/255 122/255]; curBar(2).EdgeColor = 'none'; +curBar(3).FaceColor = [9/255 161/255 74/255]; curBar(3).EdgeColor = 'none'; +curBar(4).FaceColor = [128/255 66/255 151/255]; curBar(4).EdgeColor = 'none'; set(gca, 'xlim', [0 1], 'xticklabel', []); hold on plot(get(gca, 'xlim'), [0 0], '--w'); @@ -149,14 +151,15 @@ drawnow; subplot(4,5,11:14); cPost = CalcStaticBayesPost(fullyISlikely, spikeMatrix(:,:,[trialPeriodTD.Odor]==3 & ~fullInSeqLog & perfLog & inSeqLog), binSize); -imagesc(1:length(timeIndexes), trialTime, nanmean(cPost,3),[0 0.0025]); +imagesc(1:length(timeIndexes), trialTime, nanmean(cPost,3),cLims); hold on; -set(gca, 'ydir', 'normal', 'xtick', xTickNdxs, 'xticklabel', repmat([-0.4 0 withdrawTime],[1,4])); +set(gca, 'ydir', 'normal', 'xtick', [], 'xticklabel', [], 'TickDir', 'out'); for p = 1:4 plot([odorDivs(p) odorDivs(p)], get(gca, 'ylim'), '-w', 'linewidth', 2); plot([odorTargs(p) odorTargs(p)], get(gca, 'ylim'), ':w', 'linewidth', 1.5); plot([odorStarts(p) odorStarts(p)], get(gca, 'ylim'), '--w', 'linewidth', 1.5); end +plot([0 length(timeIndexes)], [0 0], '-w', 'linewidth', 1); cDecode = DecodeBayesPost(cPost, odorIndexes); cDecodePrcnts = nan(size(cDecode,1),4); for t = 1:size(aDecode,1) @@ -167,10 +170,10 @@ end subplot(4,5,15); curBar = barh(trialTime,cDecodePrcnts, 1, 'stacked'); -curBar(1).FaceColor = [44/255 168/255 224/255]; -curBar(2).FaceColor = [154/255 133/255 122/255]; -curBar(3).FaceColor = [9/255 161/255 74/255]; -curBar(4).FaceColor = [128/255 66/255 151/255]; +curBar(1).FaceColor = [44/255 168/255 224/255]; curBar(1).EdgeColor = 'none'; +curBar(2).FaceColor = [154/255 133/255 122/255]; curBar(2).EdgeColor = 'none'; +curBar(3).FaceColor = [9/255 161/255 74/255]; curBar(3).EdgeColor = 'none'; +curBar(4).FaceColor = [128/255 66/255 151/255]; curBar(4).EdgeColor = 'none'; set(gca, 'xlim', [0 1], 'xticklabel', []); hold on plot(get(gca, 'xlim'), [0 0], '--w'); @@ -179,14 +182,15 @@ drawnow; subplot(4,5,16:19); dPost = CalcStaticBayesPost(fullyISlikely, spikeMatrix(:,:,[trialPeriodTD.Odor]==4 & ~fullInSeqLog & perfLog & inSeqLog), binSize); -imagesc(1:length(timeIndexes), trialTime, nanmean(dPost,3),[0 0.0025]); +imagesc(1:length(timeIndexes), trialTime, nanmean(dPost,3),cLims); hold on; -set(gca, 'ydir', 'normal', 'xtick', xTickNdxs, 'xticklabel', repmat([-0.4 0 withdrawTime],[1,4])); +set(gca, 'ydir', 'normal', 'xtick', xTickNdxs, 'xticklabel', repmat([window(1) 0 withdrawTime],[1,4]), 'TickDir', 'out'); for p = 1:4 plot([odorDivs(p) odorDivs(p)], get(gca, 'ylim'), '-w', 'linewidth', 2); plot([odorTargs(p) odorTargs(p)], get(gca, 'ylim'), ':w', 'linewidth', 1.5); plot([odorStarts(p) odorStarts(p)], get(gca, 'ylim'), '--w', 'linewidth', 1.5); end +plot([0 length(timeIndexes)], [0 0], '-w', 'linewidth', 1); dDecode = DecodeBayesPost(dPost, odorIndexes); dDecodePrcnts = nan(size(dDecode,1),4); for t = 1:size(aDecode,1) @@ -197,10 +201,10 @@ end subplot(4,5,20); curBar = barh(trialTime,dDecodePrcnts, 1, 'stacked'); -curBar(1).FaceColor = [44/255 168/255 224/255]; -curBar(2).FaceColor = [154/255 133/255 122/255]; -curBar(3).FaceColor = [9/255 161/255 74/255]; -curBar(4).FaceColor = [128/255 66/255 151/255]; +curBar(1).FaceColor = [44/255 168/255 224/255]; curBar(1).EdgeColor = 'none'; +curBar(2).FaceColor = [154/255 133/255 122/255]; curBar(2).EdgeColor = 'none'; +curBar(3).FaceColor = [9/255 161/255 74/255]; curBar(3).EdgeColor = 'none'; +curBar(4).FaceColor = [128/255 66/255 151/255]; curBar(4).EdgeColor = 'none'; set(gca, 'xlim', [0 1], 'xticklabel', []); hold on plot(get(gca, 'xlim'), [0 0], '--w'); @@ -209,6 +213,12 @@ drawnow; colormap jet + +annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... + 'linestyle', 'none', 'horizontalalignment', 'right'); +curDir = cd; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); %% function decode = DecodeBayesPost(post, id) % Assumes post is in the structure of ObservTime X LikelyTime X Trial From 720f092d076d3cd41dadf3850a8c82d12ced23b2 Mon Sep 17 00:00:00 2001 From: gabe Date: Fri, 24 Jan 2020 11:08:53 -0800 Subject: [PATCH 042/133] Update SummarizePLXevents_SD.m --- .../Supporting Code/SummarizePLXevents_SD.m | 40 ++++++++++++++----- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m b/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m index 9efc931..68d8911 100644 --- a/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m +++ b/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m @@ -89,6 +89,11 @@ %%% event timestamps. % error('This data was collected with an old version of the code that didn''t allow declaration of buffer duration, please use an older plxAnalysis script for it'); end +if isfield(ssnData(1).Settings, 'GracePeriodDur') + graceDur = arrayfun(@(a)a.Settings.GracePeriodDur, ssnData); +else + graceDur = repmat(0, size(ssnData)); +end sequenceLength = ssnData(1).Settings.SequenceLength; % Now extract event data from the .plx file [numChans, chanNames] = plx_event_names(plxFile); @@ -506,32 +511,47 @@ if plxSession(trl).TranspositionDistance == 0 && plxSession(trl).Performance == 0 break elseif plxSession(trl).TranspositionDistance == 0 && plxSession(trl).Performance == 1 - msgbox(sprintf('Trial #%i: InSeq trial where buffer was triggered and duration elapsed but it was counted as correct\n\n Re-run the analysis with the error line commented in place of this line', trl), 'Poke Buffer Warning', 'warn'); -% error('Trial #%i: InSeq trial where buffer was triggered and duration elapsed but it was counted as correct', trl); - fprintf(outfile, 'Trial #%i: InSeq trial where buffer was triggered and duration elapsed but it was counted as correct\n', trl); - tempPokeNum = tempPokeNum+1; - tempPokeDur = tempPokeDur + trialInterPokeIntervals(tempPokeNum-1)+trialPokeDurations(tempPokeNum); + if plxSession(trl).TargetDuration - tempPokeDur < graceDur(trl) + break + else +% msgbox(sprintf('Trial #%i: InSeq trial where buffer was triggered and duration elapsed but it was counted as correct\n\n Re-run the analysis with the error line commented in place of this line', trl), 'Poke Buffer Warning', 'warn'); +% error('Trial #%i: InSeq trial where buffer was triggered and duration elapsed but it was counted as correct', trl); +% fprintf(outfile, 'Trial #%i: InSeq trial where buffer was triggered and duration elapsed but it was counted as correct\n', trl); +% tempPokeNum = tempPokeNum+1; +% tempPokeDur = tempPokeDur + trialInterPokeIntervals(tempPokeNum-1)+trialPokeDurations(tempPokeNum); + plxData.Summary.Errors = [plxSummary.Errors; + ['Trial #' num2str(trl) ': InSeq trial where buffer was triggered and buffer duration elapsed but it was counted as correct']]; + break + end elseif ~(plxSession(trl).TranspositionDistance == 0) && plxSession(trl).Performance == 1 break elseif ~(plxSession(trl).TranspositionDistance == 0) && plxSession(trl).Performance == 0 if tempPokeDur < 0.2 tempPokeNum = tempPokeNum+1; tempPokeDur = tempPokeDur + trialInterPokeIntervals(tempPokeNum-1)+trialPokeDurations(tempPokeNum); + elseif plxSession(trl).TargetDuration - tempPokeDur < graceDur(trl) + break else - msgbox(sprintf('Trial #%i: OutSeq trial where buffer was triggered and duration elapsed but it was counted as incorrect\n\n Re-run the analysis with the error line commented in place of this line', trl), 'Poke Buffer Warning', 'warn'); - error('Trial #%i: OutSeq trial where buffer was triggered and duration elapsed but it was counted as incorrect', trl); +% msgbox(sprintf('Trial #%i: OutSeq trial where buffer was triggered and duration elapsed but it was counted as incorrect\n\n Re-run the analysis with the error line commented in place of this line', trl), 'Poke Buffer Warning', 'warn'); +% error('Trial #%i: OutSeq trial where buffer was triggered and duration elapsed but it was counted as incorrect', trl); % fprintf('Trial #%i: OutSeq trial where buffer was triggered and duration elapsed but it was counted as incorrect\n', trl); - tempPokeNum = tempPokeNum+1; - tempPokeDur = tempPokeDur + trialInterPokeIntervals(tempPokeNum-1)+trialPokeDurations(tempPokeNum); +% tempPokeNum = tempPokeNum+1; +% tempPokeDur = tempPokeDur + trialInterPokeIntervals(tempPokeNum-1)+trialPokeDurations(tempPokeNum); + plxData.Summary.Errors = [plxSummary.Errors; + ['Trial #' num2str(trl) ': OutSeq trial where buffer was triggered and buffer duration elapsed but it was counted as incorrect']]; + break end end - elseif tempPokeNum > sum(trialPokesLog) + elseif tempPokeNum > sum(trialPokesLog) || plxSession(trl).TargetDuration - tempPokeDur < graceDur(trl) break else tempPokeNum = tempPokeNum+1; tempPokeDur = tempPokeDur + trialInterPokeIntervals(tempPokeNum-1)+trialPokeDurations(tempPokeNum); end end + if sum(trialPokeDurations) + sum(trialInterPokeIntervals(1:end-1)) == ssnData(trl).PokeDuration + tempPokeDur = sum(trialPokeDurations) + sum(trialInterPokeIntervals(1:end-1)); + end plxSession(trl).PokeDuration = tempPokeDur; elseif sum(trialPokesLog)==0 error('Trial #%i: No pokes detected', trl); From 62b5baaa327c3bed72caa6cb9ddc6fb6f3a58d26 Mon Sep 17 00:00:00 2001 From: gabe Date: Fri, 24 Jan 2020 11:09:11 -0800 Subject: [PATCH 043/133] Create CreateBehaviorMatrixPFCabbr.m --- .../CreateBehaviorMatrixPFCabbr.m | 143 ++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 statMatrix Creation/CreateBehaviorMatrixPFCabbr.m diff --git a/statMatrix Creation/CreateBehaviorMatrixPFCabbr.m b/statMatrix Creation/CreateBehaviorMatrixPFCabbr.m new file mode 100644 index 0000000..ead3038 --- /dev/null +++ b/statMatrix Creation/CreateBehaviorMatrixPFCabbr.m @@ -0,0 +1,143 @@ +function [behavMatrix, behavMatrixColIDs] = CreateBehaviorMatrixPLXabbr +[plxData] = SummarizePLXevents_SD; +file = plxData.PLXfile; + +chan = 1; +[samp, ~, tetTS, fn, ~] = plx_ad_v(file, chan); +while tetTS == -1 + chan = chan + 1; + [samp, ~, tetTS, fn, ~] = plx_ad_v(file, chan); +end + +% Create timestamp vector +tsVect = ((0:(fn(1)-1))/samp)+tetTS(1); +% Sometimes the file is composed of multiple fragments, in which case these +% should be combined into a single file +for fragNum = 2:length(tetTS) + tsVect = [tsVect, ((0:(fn(fragNum)-1))/samp)+tetTS(fragNum)]; %#ok +end +tsVect(end+1) = tsVect(end)+(1/samp); + +seqLength = summary.SequenceLength; +maxSeqLength = max([behaviorData.OrdinalPosition]); +behPad = seqLength + maxSeqLength; +if isfield(summary, 'DualListLog') && summary.DualListLog + behPad = behPad + seqLength; +end +behVals = nan(length(tsVect)-1, behPad + 5); +behDataHeaders = cell(1,behPad + 7); +% Step through each sequence item/position and identify when odors were +% presented +% Do position first because it doesn't change with multiple lists +% fprintf(outfile, 'Position Counts\n'); +for pos = 1:maxSeqLength + posPresTimes = [behaviorData([behaviorData.OrdinalPosition]==pos).ItemPresentationTime]; + behVals(:,pos) = histcounts(posPresTimes, tsVect)'; + behDataHeaders{pos} = ['Position' num2str(pos)]; +% fprintf(outfile, ' Position #%i = %i trials\n', pos, length(posPresTimes)); +end +% fprintf(outfile, 'Odor Counts\n'); +for seq = 1:seqLength + itemPresTimes = [behaviorData([behaviorData.SequenceItem]==seq).ItemPresentationTime]; + behVals(:,seq+maxSeqLength) = histcounts(itemPresTimes, tsVect)'; + behDataHeaders{seq+maxSeqLength} = ['Odor' num2str(seq)]; +% fprintf(outfile, ' Odor #%i = %i trials\n', seq, length(itemPresTimes)); +end +if isfield(summary, 'DualListLog') && summary.DualListLog + for seq = 11:seqLength+10 + itemPresTimes = [behaviorData([behaviorData.SequenceItem]==seq).ItemPresentationTime]; + behVals(:,seq+seqLength+maxSeqLength-10) = histcounts(itemPresTimes, tsVect)'; + behDataHeaders{seq+seqLength+maxSeqLength-10} = ['Odor' num2str(seq)]; +% fprintf(outfile, ' Odor #%i = %i trials\n', seq, length(itemPresTimes)); + end +end + +inSeqOdorPres = [behaviorData([behaviorData.TranspositionDistance]==0).ItemPresentationTime]; +outSeqOdorPres = [behaviorData(~([behaviorData.TranspositionDistance]==0)).ItemPresentationTime]; +behVals(:,behPad+1) = histcounts(inSeqOdorPres, tsVect)' - histcounts(outSeqOdorPres,tsVect)'; +behDataHeaders{behPad+1} = 'InSeqLog'; +% fprintf(outfile, '\nCompiling InSeq trials.....\n %i trials were InSeq (%i%%)\n', length(inSeqOdorPres), round(length(inSeqOdorPres)/length(behaviorData),2)*100); +itmPresTimes = [behaviorData.ItemPresentationTime]; +trialPerformance = [behaviorData.Performance]; +corrTrials = itmPresTimes(logical(trialPerformance)); +corTrlHistCounts = histcounts(corrTrials, tsVect)'; +inCorrTrials = itmPresTimes(~logical(trialPerformance)); +inCorTrlHistCounts = histcounts(inCorrTrials, tsVect)'; +behVals(:,behPad+2) = corTrlHistCounts + (inCorTrlHistCounts*-1); +behDataHeaders{behPad+2} = 'PerformanceLog'; +% fprintf(outfile, 'Compiling Performance.....\n %i trials were correct (%i%%)\n', sum(trialPerformance), round(mean(trialPerformance),2)*100); + +behVals(:,behPad+3) = histcounts([behaviorData.OdorTrigPokeTime], tsVect)' - histcounts([behaviorData.OdorPokeWithdrawTime], tsVect)'; +behDataHeaders{behPad+3} = 'PokeEvents'; + +behVals(:,behPad+4) = histcounts([behaviorData.FrontRewardTime], tsVect)'; +behDataHeaders{behPad+4} = 'FrontReward'; + +behVals(:,behPad+5) = histcounts([behaviorData.BackRewardTime], tsVect)'; +behDataHeaders{behPad+5} = 'BackReward'; + +if isfield(behaviorData, 'ErrorSignalTime') + behVals(:,behPad+6) = histcounts([behaviorData.ErrorSignalTime], tsVect)'; + behDataHeaders{behPad+6} = 'ErrorSignal'; +end + +[numChans, chanNames] = plx_event_names(file); +findingStrobed = 1; +while findingStrobed + for chan = 1:numChans + curChan = chanNames(chan,:); + intVals = double(curChan); + valLim = find(~(intVals==0), 1, 'last'); + strobedChanLog = strcmp(curChan(1:valLim), 'Strobed'); + if strobedChanLog + [~, strobedTS, strobedSV] = plx_event_ts(file, curChan); + [~, ~, ~, aniPosition] = plx_vt_interpret(strobedTS, strobedSV); + aniPosition(aniPosition(:,1)=5 + for t = 1:size(aniPosition,1) + if (aniPosition(t,2)==0 && aniPosition(t,3)==0) &&... + (aniPosition(t,4)>0 && aniPosition(t,5)>0) + aniX(t) = aniPosition(t,4); + aniY(t) = aniPosition(t,5); + elseif (aniPosition(t,4)==0 && aniPosition(t,5)==0) &&... + (aniPosition(t,2)>0 && aniPosition(t,3)>0) + aniX(t) = aniPosition(t,2); + aniY(t) = aniPosition(t,3); + elseif (aniPosition(t,2)>0 && aniPosition(t,3)>0) &&... + (aniPosition(t,3)>0 && aniPosition(t,5)>0) + aniX(t) = mean([aniPosition(t,2) aniPosition(t,4)]); + aniY(t) = mean([aniPosition(t,3) aniPosition(t,5)]); + elseif (aniPosition(t,2)==0 && aniPosition(t,3)==0) &&... + (aniPosition(t,3)==0 && aniPosition(t,5)==0) + aniX(t) = 0; + aniY(t) = 0; + end + end + end + aniPosHistBins = find(histcounts(aniPosition(:,1), tsVect)); + if isfield(behaviorData, 'ErrorSignalTime') + behVals(aniPosHistBins,behPad+7) = aniX'; + behVals(aniPosHistBins,behPad+8) = aniY'; + else + behVals(aniPosHistBins,behPad+6) = aniX; + behVals(aniPosHistBins,behPad+7) = aniY; + end + findingStrobed = 0; + end + end +end +if isfield(behaviorData, 'ErrorSignalTime') + behDataHeaders{behPad+7} = 'XvalRatMazePosition'; + behDataHeaders{behPad+8} = 'YvalRatMazePosition'; +else + behDataHeaders{behPad+6} = 'XvalRatMazePosition'; + behDataHeaders{behPad+7} = 'YvalRatMazePosition'; +end +behavMatrix = [tsVect(1:end-1)', behVals]; +behavMatrixColIDs = [{'TimeBin'}, behDataHeaders]; +save([outputFileName{1} '_BehaviorMatrix.mat'], 'behavMatrix', 'behavMatrixColIDs'); +disp('Behavior data saved.'); +% fprintf(outfile, 'Behavior Matrix saved as %s_BehaviorMatrix.mat\n', outputFileName{1}); +end \ No newline at end of file From a7aee62c87ea500ac5f2365f5852c319ff8feb32 Mon Sep 17 00:00:00 2001 From: gabe Date: Fri, 24 Jan 2020 11:09:20 -0800 Subject: [PATCH 044/133] Update IdentifySWRs_SM.m --- Analyses/IdentifySWRs_SM.m | 72 +++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/Analyses/IdentifySWRs_SM.m b/Analyses/IdentifySWRs_SM.m index e89dd52..033f5e6 100644 --- a/Analyses/IdentifySWRs_SM.m +++ b/Analyses/IdentifySWRs_SM.m @@ -93,7 +93,7 @@ subplot(2,2,[2 4]); corrScatPlot(epocDur(1:end-1), interEpocInterval, 'Duration', 'Latency',[]); -26.7%% Format Output +%% Format Output tsVect = statMatrix(:,1); swrIndices = tsVect(epocWindows); swrVectLog = false(size(tsVect)); @@ -129,41 +129,41 @@ b = gca; linkaxes([a b], 'x') -%% -peakLoc = nan(size(epocWindows,1),1); -peakPwr = nan(size(epocWindows,1),1); -phaseMtx = cell(size(epocWindows,1),1); -phaseCorr = nan(size(epocWindows,1),1); -parfor epoc = 1:size(epocWindows,1) - curEpocWindow = epocWindows(epoc,1):epocWindows(epoc,2); - [tmpPks, tmpLocs] = findpeaks(aggPower(curEpocWindow), 'MinPeakHeight', rmsThresh, 'MinPeakProminence', rmsThresh); - if isempty(tmpPks) - [tmpPks, tmpLocs] = findpeaks(aggPower(curEpocWindow)); - % if isempty(tmpPks) - % disp('EMPTY PEAKS') - % continue - % % tmpPks = aggPower(curEpocWindow); - % % tmpLocs = 1:length(tmpPks); - % end - end - if ~isempty(tmpPks) - curEpocPhaseMtx = nan(size(bandPhase,2)); - for tetA = 1:size(bandPhase,2) - for tetB = 1:size(bandPhase,2) - curEpocPhaseMtx(tetA,tetB) = circ_corrcc(bandPhase(curEpocWindow,tetA), bandPhase(curEpocWindow,tetB)); - end - end - phaseMtx{epoc} = curEpocPhaseMtx; - phaseCorr(epoc) = mean(curEpocPhaseMtx(triu(true(size(curEpocPhaseMtx)),1))); - peakPwr(epoc) = (max(tmpPks)-mean(aggPower))/std(aggPower); % z-norm peak power; - tmpEpocPkLoc = tmpLocs(tmpPks==max(tmpPks)); - if size(tmpEpocPkLoc)>1 - tmpEpocPkLoc = tmpEpocPkLoc(1); - end - tempEpocPkLoc = epocWindows(epoc,1)+tmpEpocPkLoc; - peakLoc(epoc) = tempEpocPkLoc; - end -end +%% SOMETHING'S BROKE HERE +% peakLoc = nan(size(epocWindows,1),1); +% peakPwr = nan(size(epocWindows,1),1); +% phaseMtx = cell(size(epocWindows,1),1); +% phaseCorr = nan(size(epocWindows,1),1); +% parfor epoc = 1:size(epocWindows,1) +% curEpocWindow = epocWindows(epoc,1):epocWindows(epoc,2); +% [tmpPks, tmpLocs] = findpeaks(aggPower(curEpocWindow), 'MinPeakHeight', rmsThresh, 'MinPeakProminence', rmsThresh); +% if isempty(tmpPks) +% [tmpPks, tmpLocs] = findpeaks(aggPower(curEpocWindow)); +% % if isempty(tmpPks) +% % disp('EMPTY PEAKS') +% % continue +% % % tmpPks = aggPower(curEpocWindow); +% % % tmpLocs = 1:length(tmpPks); +% % end +% end +% if ~isempty(tmpPks) +% curEpocPhaseMtx = nan(size(bandPhase,2)); +% for tetA = 1:size(bandPhase,2) +% for tetB = 1:size(bandPhase,2) +% curEpocPhaseMtx(tetA,tetB) = circ_corrcc(bandPhase(curEpocWindow,tetA), bandPhase(curEpocWindow,tetB)); +% end +% end +% phaseMtx{epoc} = curEpocPhaseMtx; +% phaseCorr(epoc) = mean(curEpocPhaseMtx(triu(true(size(curEpocPhaseMtx)),1))); +% peakPwr(epoc) = (max(tmpPks)-mean(aggPower))/std(aggPower); % z-norm peak power; +% tmpEpocPkLoc = tmpLocs(tmpPks==max(tmpPks)); +% if size(tmpEpocPkLoc)>1 +% tmpEpocPkLoc = tmpEpocPkLoc(1); +% end +% tempEpocPkLoc = epocWindows(epoc,1)+tmpEpocPkLoc; +% peakLoc(epoc) = tempEpocPkLoc; +% end +% end % %% Edit the SWRs % % Combine short SWRs From 84ed614e96f9df528a5633c7b233822fbf56d304 Mon Sep 17 00:00:00 2001 From: gabe Date: Fri, 24 Jan 2020 11:09:31 -0800 Subject: [PATCH 045/133] Update BayesPFCproto_VALID.m --- Analyses/Prototypes/BayesPFCproto_VALID.m | 88 +++++++++++++++++++---- 1 file changed, 75 insertions(+), 13 deletions(-) diff --git a/Analyses/Prototypes/BayesPFCproto_VALID.m b/Analyses/Prototypes/BayesPFCproto_VALID.m index f5d3911..9940d55 100644 --- a/Analyses/Prototypes/BayesPFCproto_VALID.m +++ b/Analyses/Prototypes/BayesPFCproto_VALID.m @@ -3,10 +3,10 @@ clear all %% Runtime variables -window = [-10 10]; -binSize = 200; -dsRate = 10; -cLims = [0 0.0025]; +window = [-0.4 0.75]; +binSize = 20; +dsRate = 20; +cLims = [0 0.005]; withdrawTime = 1; %% smPath = uigetdir; @@ -86,7 +86,7 @@ %% figure -subplot(4,5,1:4); +subplot(4,5,1:3); aPost = CalcStaticBayesPost(fullyISlikely, spikeMatrix(:,:,[trialPeriodTD.Odor]==1 & ~fullInSeqLog & perfLog & inSeqLog), binSize); imagesc(1:length(timeIndexes), trialTime, nanmean(aPost,3),cLims); hold on; @@ -96,7 +96,8 @@ plot([odorTargs(p) odorTargs(p)], get(gca, 'ylim'), ':w', 'linewidth', 1.5); plot([odorStarts(p) odorStarts(p)], get(gca, 'ylim'), '--w', 'linewidth', 1.5); end -plot([0 length(timeIndexes)], [0 0], '-w', 'linewidth', 1); +plot([0 length(timeIndexes)], [withdrawTime, withdrawTime], ':w', 'linewidth', 1.5); +plot([0 length(timeIndexes)], [0 0], '--w', 'linewidth', 1); aDecode = DecodeBayesPost(aPost, odorIndexes); aDecodePrcnts = nan(size(aDecode,1),4); for t = 1:size(aDecode,1) @@ -105,7 +106,8 @@ aDecodePrcnts(t,3) = sum(aDecode(t,:)==3)/sum(~isnan(aDecode(t,:))); aDecodePrcnts(t,4) = sum(aDecode(t,:)==4)/sum(~isnan(aDecode(t,:))); end -subplot(4,5,5); +title('Probability Density'); +subplot(4,5,4); curBar = barh(trialTime,aDecodePrcnts, 1, 'stacked'); curBar(1).FaceColor = [44/255 168/255 224/255]; curBar(1).EdgeColor = 'none'; curBar(2).FaceColor = [154/255 133/255 122/255]; curBar(2).EdgeColor = 'none'; @@ -117,8 +119,24 @@ plot(get(gca, 'xlim'), [withdrawTime withdrawTime], ':w'); box off legend('A', 'B', 'C', 'D'); +aDecodeTime = DecodeBayesPost(aPost, timeIndexes); +decodeLag = aDecodeTime - repmat(trialTime, [1,size(aDecodeTime,2)]); +title('Odor Decoding'); +subplot(4,5,5); +lagMean = nanmean(decodeLag,2); +lagVar = nanstd(decodeLag,0,2); +plot(lagMean, trialTime, '-k', 'linewidth', 1); +patch('XData', [lagMean+lagVar; flipud(lagMean-lagVar)],... + 'YData', [trialTime; flipud(trialTime)], 'FaceColor', 'k', 'FaceAlpha', .3, 'edgecolor', 'none'); +axis tight; +box off; +set(gca, 'tickdir', 'out', 'xlim', [-2 2]); +hold on; +plot([0 0], get(gca, 'ylim'), ':k', 'linewidth', 1.5); +title('Time Decoding'); drawnow; -subplot(4,5,6:9); + +subplot(4,5,6:8); bPost = CalcStaticBayesPost(fullyISlikely, spikeMatrix(:,:,[trialPeriodTD.Odor]==2 & ~fullInSeqLog & perfLog & inSeqLog), binSize); imagesc(1:length(timeIndexes), trialTime, nanmean(bPost,3),cLims); hold on; @@ -128,6 +146,7 @@ plot([odorTargs(p) odorTargs(p)], get(gca, 'ylim'), ':w', 'linewidth', 1.5); plot([odorStarts(p) odorStarts(p)], get(gca, 'ylim'), '--w', 'linewidth', 1.5); end +plot([0 length(timeIndexes)], [withdrawTime, withdrawTime], ':w', 'linewidth', 1.5); plot([0 length(timeIndexes)], [0 0], '-w', 'linewidth', 1); bDecode = DecodeBayesPost(bPost, odorIndexes); bDecodePrcnts = nan(size(bDecode,1),4); @@ -137,7 +156,7 @@ bDecodePrcnts(t,3) = sum(bDecode(t,:)==3)/sum(~isnan(bDecode(t,:))); bDecodePrcnts(t,4) = sum(bDecode(t,:)==4)/sum(~isnan(bDecode(t,:))); end -subplot(4,5,10); +subplot(4,5,9); curBar = barh(trialTime,bDecodePrcnts, 1, 'stacked'); curBar(1).FaceColor = [44/255 168/255 224/255]; curBar(1).EdgeColor = 'none'; curBar(2).FaceColor = [154/255 133/255 122/255]; curBar(2).EdgeColor = 'none'; @@ -148,8 +167,22 @@ plot(get(gca, 'xlim'), [0 0], '--w'); plot(get(gca, 'xlim'), [withdrawTime withdrawTime], ':w'); box off +bDecodeTime = DecodeBayesPost(bPost, timeIndexes); +decodeLag = bDecodeTime - repmat(trialTime, [1,size(bDecodeTime,2)]); +subplot(4,5,10); +lagMean = nanmean(decodeLag,2); +lagVar = nanstd(decodeLag,0,2); +plot(lagMean, trialTime, '-k', 'linewidth', 1); +patch('XData', [lagMean+lagVar; flipud(lagMean-lagVar)],... + 'YData', [trialTime; flipud(trialTime)], 'FaceColor', 'k', 'FaceAlpha', .3, 'edgecolor', 'none'); +axis tight; +box off; +set(gca, 'tickdir', 'out', 'xlim', [-2 2]); +hold on; +plot([0 0], get(gca, 'ylim'), ':k', 'linewidth', 1.5); drawnow; -subplot(4,5,11:14); + +subplot(4,5,11:13); cPost = CalcStaticBayesPost(fullyISlikely, spikeMatrix(:,:,[trialPeriodTD.Odor]==3 & ~fullInSeqLog & perfLog & inSeqLog), binSize); imagesc(1:length(timeIndexes), trialTime, nanmean(cPost,3),cLims); hold on; @@ -159,6 +192,7 @@ plot([odorTargs(p) odorTargs(p)], get(gca, 'ylim'), ':w', 'linewidth', 1.5); plot([odorStarts(p) odorStarts(p)], get(gca, 'ylim'), '--w', 'linewidth', 1.5); end +plot([0 length(timeIndexes)], [withdrawTime, withdrawTime], ':w', 'linewidth', 1.5); plot([0 length(timeIndexes)], [0 0], '-w', 'linewidth', 1); cDecode = DecodeBayesPost(cPost, odorIndexes); cDecodePrcnts = nan(size(cDecode,1),4); @@ -168,7 +202,7 @@ cDecodePrcnts(t,3) = sum(cDecode(t,:)==3)/sum(~isnan(cDecode(t,:))); cDecodePrcnts(t,4) = sum(cDecode(t,:)==4)/sum(~isnan(cDecode(t,:))); end -subplot(4,5,15); +subplot(4,5,14); curBar = barh(trialTime,cDecodePrcnts, 1, 'stacked'); curBar(1).FaceColor = [44/255 168/255 224/255]; curBar(1).EdgeColor = 'none'; curBar(2).FaceColor = [154/255 133/255 122/255]; curBar(2).EdgeColor = 'none'; @@ -179,8 +213,22 @@ plot(get(gca, 'xlim'), [0 0], '--w'); plot(get(gca, 'xlim'), [withdrawTime withdrawTime], ':w'); box off +cDecodeTime = DecodeBayesPost(cPost, timeIndexes); +decodeLag = cDecodeTime - repmat(trialTime, [1,size(cDecodeTime,2)]); +subplot(4,5,15); +lagMean = nanmean(decodeLag,2); +lagVar = nanstd(decodeLag,0,2); +plot(lagMean, trialTime, '-k', 'linewidth', 1); +patch('XData', [lagMean+lagVar; flipud(lagMean-lagVar)],... + 'YData', [trialTime; flipud(trialTime)], 'FaceColor', 'k', 'FaceAlpha', .3, 'edgecolor', 'none'); +axis tight; +box off; +set(gca, 'tickdir', 'out', 'xlim', [-2 2]); +hold on; +plot([0 0], get(gca, 'ylim'), ':k', 'linewidth', 1.5); drawnow; -subplot(4,5,16:19); + +subplot(4,5,16:18); dPost = CalcStaticBayesPost(fullyISlikely, spikeMatrix(:,:,[trialPeriodTD.Odor]==4 & ~fullInSeqLog & perfLog & inSeqLog), binSize); imagesc(1:length(timeIndexes), trialTime, nanmean(dPost,3),cLims); hold on; @@ -190,6 +238,7 @@ plot([odorTargs(p) odorTargs(p)], get(gca, 'ylim'), ':w', 'linewidth', 1.5); plot([odorStarts(p) odorStarts(p)], get(gca, 'ylim'), '--w', 'linewidth', 1.5); end +plot([0 length(timeIndexes)], [withdrawTime, withdrawTime], ':w', 'linewidth', 1.5); plot([0 length(timeIndexes)], [0 0], '-w', 'linewidth', 1); dDecode = DecodeBayesPost(dPost, odorIndexes); dDecodePrcnts = nan(size(dDecode,1),4); @@ -199,7 +248,7 @@ dDecodePrcnts(t,3) = sum(dDecode(t,:)==3)/sum(~isnan(dDecode(t,:))); dDecodePrcnts(t,4) = sum(dDecode(t,:)==4)/sum(~isnan(dDecode(t,:))); end -subplot(4,5,20); +subplot(4,5,19); curBar = barh(trialTime,dDecodePrcnts, 1, 'stacked'); curBar(1).FaceColor = [44/255 168/255 224/255]; curBar(1).EdgeColor = 'none'; curBar(2).FaceColor = [154/255 133/255 122/255]; curBar(2).EdgeColor = 'none'; @@ -210,6 +259,19 @@ plot(get(gca, 'xlim'), [0 0], '--w'); plot(get(gca, 'xlim'), [withdrawTime withdrawTime], ':w'); box off +dDecodeTime = DecodeBayesPost(dPost, timeIndexes); +decodeLag = dDecodeTime - repmat(trialTime, [1,size(dDecodeTime,2)]); +subplot(4,5,20); +lagMean = nanmean(decodeLag,2); +lagVar = nanstd(decodeLag,0,2); +plot(lagMean, trialTime, '-k', 'linewidth', 1); +patch('XData', [lagMean+lagVar; flipud(lagMean-lagVar)],... + 'YData', [trialTime; flipud(trialTime)], 'FaceColor', 'k', 'FaceAlpha', .3, 'edgecolor', 'none'); +axis tight; +box off; +set(gca, 'tickdir', 'out', 'xlim', [-2 2]); +hold on; +plot([0 0], get(gca, 'ylim'), ':k', 'linewidth', 1.5); drawnow; colormap jet From 8f9620cf54677bd16ac55dee164c69a8ec1687ab Mon Sep 17 00:00:00 2001 From: gabe Date: Fri, 24 Jan 2020 11:23:16 -0800 Subject: [PATCH 046/133] Update SummarizePLXevents_SD.m --- statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m b/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m index 68d8911..f5cce59 100644 --- a/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m +++ b/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m @@ -17,6 +17,7 @@ plxFile = [path fileName]; [path, fileName] = fileparts(plxFile); path = [path '\']; + cd(path); flContents = dir(path); fileNames = {flContents.name}; matFileLog = cellfun(@(a)~isempty(a), regexp(fileNames, [fileName '_([0-9]*)-([A-Z | a-z]*)-([0-9]*).mat'])); @@ -59,6 +60,7 @@ end plxSummary.MATfile = matFileName; plxSummary.PLXfile = [fileName '.plx']; +plxSummary.Dir = path; fprintf(outfile, '\n****Beginning behavioral analysis using SummarizePLXevents_SD.m. ****\n Mat File = %s\n Plx File = %s\n', plxSummary.MATfile, plxSummary.PLXfile); %% Load Data From 1c0364dd6361ea5bcd13ca415064f05d5484116c Mon Sep 17 00:00:00 2001 From: gabe Date: Fri, 24 Jan 2020 11:23:19 -0800 Subject: [PATCH 047/133] Update CreateBehaviorMatrixPFCabbr.m --- .../CreateBehaviorMatrixPFCabbr.m | 51 +++++++++---------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/statMatrix Creation/CreateBehaviorMatrixPFCabbr.m b/statMatrix Creation/CreateBehaviorMatrixPFCabbr.m index ead3038..4aa7afd 100644 --- a/statMatrix Creation/CreateBehaviorMatrixPFCabbr.m +++ b/statMatrix Creation/CreateBehaviorMatrixPFCabbr.m @@ -1,12 +1,11 @@ -function [behavMatrix, behavMatrixColIDs] = CreateBehaviorMatrixPLXabbr +function CreateBehaviorMatrixPFCabbr [plxData] = SummarizePLXevents_SD; -file = plxData.PLXfile; chan = 1; -[samp, ~, tetTS, fn, ~] = plx_ad_v(file, chan); +[samp, ~, tetTS, fn, ~] = plx_ad_v(plxData.Summary.PLXfile, chan); while tetTS == -1 chan = chan + 1; - [samp, ~, tetTS, fn, ~] = plx_ad_v(file, chan); + [samp, ~, tetTS, fn, ~] = plx_ad_v(plxData.Summary.PLXfile, chan); end % Create timestamp vector @@ -18,10 +17,10 @@ end tsVect(end+1) = tsVect(end)+(1/samp); -seqLength = summary.SequenceLength; -maxSeqLength = max([behaviorData.OrdinalPosition]); +seqLength = plxData.Summary.SequenceLength; +maxSeqLength = max([plxData.Raw.OrdinalPosition]); behPad = seqLength + maxSeqLength; -if isfield(summary, 'DualListLog') && summary.DualListLog +if isfield(plxData.Summary, 'DualListLog') && plxData.Summary.DualListLog behPad = behPad + seqLength; end behVals = nan(length(tsVect)-1, behPad + 5); @@ -31,34 +30,34 @@ % Do position first because it doesn't change with multiple lists % fprintf(outfile, 'Position Counts\n'); for pos = 1:maxSeqLength - posPresTimes = [behaviorData([behaviorData.OrdinalPosition]==pos).ItemPresentationTime]; + posPresTimes = [plxData.Raw([plxData.Raw.OrdinalPosition]==pos).ItemPresentationTime]; behVals(:,pos) = histcounts(posPresTimes, tsVect)'; behDataHeaders{pos} = ['Position' num2str(pos)]; % fprintf(outfile, ' Position #%i = %i trials\n', pos, length(posPresTimes)); end % fprintf(outfile, 'Odor Counts\n'); for seq = 1:seqLength - itemPresTimes = [behaviorData([behaviorData.SequenceItem]==seq).ItemPresentationTime]; + itemPresTimes = [plxData.Raw([plxData.Raw.SequenceItem]==seq).ItemPresentationTime]; behVals(:,seq+maxSeqLength) = histcounts(itemPresTimes, tsVect)'; behDataHeaders{seq+maxSeqLength} = ['Odor' num2str(seq)]; % fprintf(outfile, ' Odor #%i = %i trials\n', seq, length(itemPresTimes)); end -if isfield(summary, 'DualListLog') && summary.DualListLog +if isfield(plxData.Summary, 'DualListLog') && plxData.Summary.DualListLog for seq = 11:seqLength+10 - itemPresTimes = [behaviorData([behaviorData.SequenceItem]==seq).ItemPresentationTime]; + itemPresTimes = [plxData.Raw([plxData.Raw.SequenceItem]==seq).ItemPresentationTime]; behVals(:,seq+seqLength+maxSeqLength-10) = histcounts(itemPresTimes, tsVect)'; behDataHeaders{seq+seqLength+maxSeqLength-10} = ['Odor' num2str(seq)]; % fprintf(outfile, ' Odor #%i = %i trials\n', seq, length(itemPresTimes)); end end -inSeqOdorPres = [behaviorData([behaviorData.TranspositionDistance]==0).ItemPresentationTime]; -outSeqOdorPres = [behaviorData(~([behaviorData.TranspositionDistance]==0)).ItemPresentationTime]; +inSeqOdorPres = [plxData.Raw([plxData.Raw.TranspositionDistance]==0).ItemPresentationTime]; +outSeqOdorPres = [plxData.Raw(~([plxData.Raw.TranspositionDistance]==0)).ItemPresentationTime]; behVals(:,behPad+1) = histcounts(inSeqOdorPres, tsVect)' - histcounts(outSeqOdorPres,tsVect)'; behDataHeaders{behPad+1} = 'InSeqLog'; -% fprintf(outfile, '\nCompiling InSeq trials.....\n %i trials were InSeq (%i%%)\n', length(inSeqOdorPres), round(length(inSeqOdorPres)/length(behaviorData),2)*100); -itmPresTimes = [behaviorData.ItemPresentationTime]; -trialPerformance = [behaviorData.Performance]; +% fprintf(outfile, '\nCompiling InSeq trials.....\n %i trials were InSeq (%i%%)\n', length(inSeqOdorPres), round(length(inSeqOdorPres)/length(plxData.Raw),2)*100); +itmPresTimes = [plxData.Raw.ItemPresentationTime]; +trialPerformance = [plxData.Raw.Performance]; corrTrials = itmPresTimes(logical(trialPerformance)); corTrlHistCounts = histcounts(corrTrials, tsVect)'; inCorrTrials = itmPresTimes(~logical(trialPerformance)); @@ -67,21 +66,21 @@ behDataHeaders{behPad+2} = 'PerformanceLog'; % fprintf(outfile, 'Compiling Performance.....\n %i trials were correct (%i%%)\n', sum(trialPerformance), round(mean(trialPerformance),2)*100); -behVals(:,behPad+3) = histcounts([behaviorData.OdorTrigPokeTime], tsVect)' - histcounts([behaviorData.OdorPokeWithdrawTime], tsVect)'; +behVals(:,behPad+3) = histcounts([plxData.Raw.OdorTrigPokeTime], tsVect)' - histcounts([plxData.Raw.OdorPokeWithdrawTime], tsVect)'; behDataHeaders{behPad+3} = 'PokeEvents'; -behVals(:,behPad+4) = histcounts([behaviorData.FrontRewardTime], tsVect)'; +behVals(:,behPad+4) = histcounts([plxData.Raw.FrontRewardTime], tsVect)'; behDataHeaders{behPad+4} = 'FrontReward'; -behVals(:,behPad+5) = histcounts([behaviorData.BackRewardTime], tsVect)'; +behVals(:,behPad+5) = histcounts([plxData.Raw.BackRewardTime], tsVect)'; behDataHeaders{behPad+5} = 'BackReward'; -if isfield(behaviorData, 'ErrorSignalTime') - behVals(:,behPad+6) = histcounts([behaviorData.ErrorSignalTime], tsVect)'; +if isfield(plxData.Raw, 'ErrorSignalTime') + behVals(:,behPad+6) = histcounts([plxData.Raw.ErrorSignalTime], tsVect)'; behDataHeaders{behPad+6} = 'ErrorSignal'; end -[numChans, chanNames] = plx_event_names(file); +[numChans, chanNames] = plx_event_names(plxData.Summary.PLXfile); findingStrobed = 1; while findingStrobed for chan = 1:numChans @@ -90,7 +89,7 @@ valLim = find(~(intVals==0), 1, 'last'); strobedChanLog = strcmp(curChan(1:valLim), 'Strobed'); if strobedChanLog - [~, strobedTS, strobedSV] = plx_event_ts(file, curChan); + [~, strobedTS, strobedSV] = plx_event_ts(plxData.Summary.PLXfile, curChan); [~, ~, ~, aniPosition] = plx_vt_interpret(strobedTS, strobedSV); aniPosition(aniPosition(:,1) Date: Fri, 24 Jan 2020 11:24:44 -0800 Subject: [PATCH 048/133] Update PositionInformationContent_SM.m --- Analyses/PositionInformationContent_SM.m | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Analyses/PositionInformationContent_SM.m b/Analyses/PositionInformationContent_SM.m index b7e25d8..0f7adcf 100644 --- a/Analyses/PositionInformationContent_SM.m +++ b/Analyses/PositionInformationContent_SM.m @@ -144,16 +144,16 @@ allOrientData = cell2mat(cellfun(@(a)a(~isnan(a(:,2)),:), orientationTrialData, 'uniformoutput', 0)'); % Head Position: Head X/Y headXcolLog = strcmp(orientMatrixColIDs, 'HeadX'); -headXbins = 30:spatialBinSize:90; +headXbins = floor(min(allOrientData(:,headXcolLog))/10)*10:spatialBinSize:ceil(max(allOrientData(:,headXcolLog))/10)*10; headYcolLog = strcmp(orientMatrixColIDs, 'HeadY'); -headYbins = 240:spatialBinSize:300; +headYbins = floor(min(allOrientData(:,headYcolLog))/10)*10:spatialBinSize:ceil(max(allOrientData(:,headYcolLog))/10)*10; headOccupancyMatrix = histcounts2(allOrientData(:,headXcolLog), allOrientData(:,headYcolLog), headXbins, headYbins, 'Normalization', 'probability')'; headOccupancyMatrix(headOccupancyMatrix==0) = nan; % Tail Position: Tail X/Y tailXcolLog = strcmp(orientMatrixColIDs, 'TailX'); -tailXbins = 60:spatialBinSize:180; +tailXbins = floor(min(allOrientData(:,tailXcolLog))/10)*10:spatialBinSize:ceil(max(allOrientData(:,tailXcolLog))/10)*10; tailYcolLog = strcmp(orientMatrixColIDs, 'TailY'); -tailYbins = 180:spatialBinSize:264; +tailYbins = floor(min(allOrientData(:,tailYcolLog))/10)*10:spatialBinSize:ceil(max(allOrientData(:,tailYcolLog))/10)*10; tailOccupancyMatrix = histcounts2(allOrientData(:,tailXcolLog), allOrientData(:,tailYcolLog), tailXbins, tailYbins, 'Normalization', 'probability')'; tailOccupancyMatrix(tailOccupancyMatrix==0) = nan; % Head Angle: Angle at the head between the port and the tail From e15d20cfb8f74704849bea974ff5c7cb60edcb01 Mon Sep 17 00:00:00 2001 From: gabe Date: Fri, 24 Jan 2020 11:24:46 -0800 Subject: [PATCH 049/133] Update PositionInformationFval_SM.m --- Analyses/PositionInformationFval_SM.m | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Analyses/PositionInformationFval_SM.m b/Analyses/PositionInformationFval_SM.m index a2a805e..7b097bf 100644 --- a/Analyses/PositionInformationFval_SM.m +++ b/Analyses/PositionInformationFval_SM.m @@ -13,7 +13,7 @@ %% Define Standard Variables slideWindowSize = 100; spatialBinSize = 5; -numPerms = 50; +numPerms = 100; %% Load Relevant Data load(fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'BehaviorMatrix'))}); @@ -115,9 +115,9 @@ %% Organize Trial Data % Create Extraction Matrices -pokeInTrialMatrix = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.2 0.5], 'PokeIn'); +pokeInTrialMatrix = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.5 0.5], 'PokeIn'); pokeInTSs = behavMatrix(pokeInTrialMatrix(1).TrialLogVect,1)-behavMatrix(pokeInTrialMatrix(1).PokeInIndex,1); -pokeOutTrialMatrix = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.5 0.2], 'PokeOut'); +pokeOutTrialMatrix = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [-0.5 0.5], 'PokeOut'); pokeOutTSs = behavMatrix(pokeOutTrialMatrix(1).TrialLogVect,1)-behavMatrix(pokeOutTrialMatrix(1).PokeOutIndex,1); % Create Trial Logical Vectors @@ -236,7 +236,7 @@ sp1 = subplot(1,2,1); imagesc(tempHeadFRmap) set(gca, 'ydir', 'normal'); - title('Head Occupancy'); + title('Head Firing Rate Map'); cb = colorbar('southoutside'); cb.Label.String = 'Mean Firing Rate (spk/s)'; z = colormap('jet'); @@ -246,7 +246,7 @@ sp2 = subplot(1,2,2); imagesc(tempTailFRmap); set(gca, 'ydir', 'normal'); - title('Head Position FR'); + title('Tail Firing Rate Map'); cb = colorbar('southoutside'); cb.Label.String = 'Mean Firing Rate (spk/s)'; colormap(z); From ba9e80767ca0836f5b2aa49546746585884bd9dc Mon Sep 17 00:00:00 2001 From: gabe Date: Wed, 5 Feb 2020 16:33:47 -0800 Subject: [PATCH 050/133] Update SummarizePLXevents_SD.m --- statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m b/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m index f5cce59..02d2a5c 100644 --- a/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m +++ b/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m @@ -309,11 +309,11 @@ elseif ~isempty(pokeChanRep) if allPokes(pokeChanRep,2)==2 pokeOutChanNumLog = pokeEndTimes==allPokes(pokeChanRep+1,1); - plxSummary.Errors = [plxSummary.Errors; {'More pokes ended than initiated @' num2str(pokeEndTimes(pokeOutChanNumLog))}]; + plxSummary.Errors = [plxSummary.Errors; sprintf('More pokes ended than initiated @%f', pokeEndTimes(pokeOutChanNumLog))]; pokeEndTimes(pokeOutChanNumLog) = []; elseif allPokes(pokeChanRep,2)==1 pokeInitiationTimeLog = pokeInitiationTimes==allPokes(pokeChanRep,1); - plxSummary.Errors = [plxSummary.Errors; {'More pokes initiated than ended @' num2str(pokeInitiationTimes(pokeInitiationTimeLog))}]; + plxSummary.Errors = [plxSummary.Errors; sprintf('More pokes initiated than ended @%f', pokeInitiationTimes(pokeInitiationTimeLog))]; pokeInitiationTimes(pokeInitiationTimeLog) = []; end end From 3051791f302f5edfda00055f4c4e87a4b6ba3b0c Mon Sep 17 00:00:00 2001 From: gabe Date: Thu, 6 Feb 2020 10:08:22 -0800 Subject: [PATCH 051/133] Update SummarizePLXevents_SD.m --- .../Supporting Code/SummarizePLXevents_SD.m | 55 +++++++++---------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m b/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m index 02d2a5c..2b5cb46 100644 --- a/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m +++ b/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m @@ -58,11 +58,11 @@ else outfile = fopen(sprintf('%s_PLXeventSummary.txt', matFileName), 'At'); %#ok end -plxSummary.MATfile = matFileName; -plxSummary.PLXfile = [fileName '.plx']; -plxSummary.Dir = path; +plxData.Summary.MATfile = matFileName; +plxData.Summary.PLXfile = [fileName '.plx']; +plxData.Summary.Dir = path; -fprintf(outfile, '\n****Beginning behavioral analysis using SummarizePLXevents_SD.m. ****\n Mat File = %s\n Plx File = %s\n', plxSummary.MATfile, plxSummary.PLXfile); +fprintf(outfile, '\n****Beginning behavioral analysis using SummarizePLXevents_SD.m. ****\n Mat File = %s\n Plx File = %s\n', plxData.Summary.MATfile, plxData.Summary.PLXfile); %% Load Data % Load the ssnData file first load(matFile); @@ -94,7 +94,7 @@ if isfield(ssnData(1).Settings, 'GracePeriodDur') graceDur = arrayfun(@(a)a.Settings.GracePeriodDur, ssnData); else - graceDur = repmat(0, size(ssnData)); + graceDur = zeros(size(ssnData)); end sequenceLength = ssnData(1).Settings.SequenceLength; % Now extract event data from the .plx file @@ -122,13 +122,13 @@ % terminateChannelLog = strcmp(channels, 'Terminate'); % if ~(sum(terminateChannelLog)==0) && ~(plxStruct(terminateChannelLog).n == 0) % term = 1; -% plxSummary.Terminate = 1; +% plxData.Summary.Terminate = 1; % terminateTime = plxStruct(terminateChannelLog).ts(find(plxStruct(terminateChannelLog).ts>0, 1, 'first')); % else term = 0; - plxSummary.Terminate = 0; + plxData.Summary.Terminate = 0; % end -plxSummary.Errors = []; +plxData.Summary.Errors = []; %% Pull Information from Buzzer channel (Sequence Block, Trial and Error Identifiers) % The easiest to parse the session is using the buzzer channel as it % has unique ways/times of activating during the trial. @@ -251,17 +251,17 @@ if length(odorPresTime{1}) < length(odorPresTime{2}) fprintf(outfile, 'More Bs than As\n'); - plxSummary.Errors = {'More Bs than As'}; + plxData.Summary.Errors = {'More Bs than As'}; % error('More Bs than As') plxData.Session = plxSession; %#ok - plxData.Summary = plxSummary; + plxData.Summary = plxData.Summary; return end if ~exist('sequenceLength', 'var') sequenceLength = sum(cellfun(@(a)~isempty(a), odorPresTime)); end -plxSummary.SequenceLength = sequenceLength; +plxData.Summary.SequenceLength = sequenceLength; odorPresSsn = odorPresTime; for opt = 1:length(odorPresSsn) @@ -282,9 +282,9 @@ end if sum(unique(odorPresSsn(:,1)) > 5)>=1 - plxSummary.DualListLog = true; + plxData.Summary.DualListLog = true; else - plxSummary.DualListLog = false; + plxData.Summary.DualListLog = false; end %% Identify Poke Times % The key information used here is when the poke was initiated and how @@ -301,19 +301,19 @@ pokeChanDiff = diff(allPokes(:,2)); pokeChanRep = find(pokeChanDiff==0); if isempty(pokeChanRep) && (length(pokeInitiationTimes) > length(pokeEndTimes)) - plxSummary.Errors = [plxSummary.Errors; {'More pokes initiated than ended'}]; + plxData.Summary.Errors = [plxData.Summary.Errors; {'More pokes initiated than ended'}]; pokeInitiationTimes(end) = []; elseif isempty(pokeChanRep) && (length(pokeInitiationTimes) < length(pokeEndTimes)) - plxSummary.Errors = [plxSummary.Errors; {'More pokes ended than initiated'}]; + plxData.Summary.Errors = [plxData.Summary.Errors; {'More pokes ended than initiated'}]; pokeEndTimes(1) = []; elseif ~isempty(pokeChanRep) if allPokes(pokeChanRep,2)==2 pokeOutChanNumLog = pokeEndTimes==allPokes(pokeChanRep+1,1); - plxSummary.Errors = [plxSummary.Errors; sprintf('More pokes ended than initiated @%f', pokeEndTimes(pokeOutChanNumLog))]; + plxData.Summary.Errors = [plxData.Summary.Errors; sprintf('More pokes ended than initiated @%f', pokeEndTimes(pokeOutChanNumLog))]; pokeEndTimes(pokeOutChanNumLog) = []; elseif allPokes(pokeChanRep,2)==1 pokeInitiationTimeLog = pokeInitiationTimes==allPokes(pokeChanRep,1); - plxSummary.Errors = [plxSummary.Errors; sprintf('More pokes initiated than ended @%f', pokeInitiationTimes(pokeInitiationTimeLog))]; + plxData.Summary.Errors = [plxData.Summary.Errors; sprintf('More pokes initiated than ended @%f', pokeInitiationTimes(pokeInitiationTimeLog))]; pokeInitiationTimes(pokeInitiationTimeLog) = []; end end @@ -417,7 +417,7 @@ % Convert the strobed channel into x-y coordinates using plexon % functions posChanNum = strcmp(channels, 'Strobed'); -[~, ~, plxSummary.PositionVTmode, aniPosition] = plx_vt_interpret(plxStruct(posChanNum).ts, plxStruct(posChanNum).sv); +[~, ~, plxData.Summary.PositionVTmode, aniPosition] = plx_vt_interpret(plxStruct(posChanNum).ts, plxStruct(posChanNum).sv); % clean position data of non-values nonPositionLog = (aniPosition(:,2) + aniPosition(:,3))==0; @@ -431,7 +431,7 @@ odorPresTime = cellfun(@(a,b)a(~b), odorPresTime, taintedOdorPresLog, 'uniformoutput', 0); pokeInitiationTimes(pokeInitiationTimes sum(trialPokesLog) || plxSession(trl).TargetDuration - tempPokeDur < graceDur(trl) + elseif tempPokeNum > sum(trialPokesLog) || plxSession(trl).TargetDuration - tempPokeDur < graceDur(trl) || isnan(trialInterPokeIntervals(tempPokeNum)) break else tempPokeNum = tempPokeNum+1; @@ -592,7 +592,7 @@ if plxSession(trl).PokeDuration - ssnData(trl).PokeDuration > 0.00001 % error('Trial #%i: Trial poke durations for .PLX and .MAT don''t match', trl); fprintf(outfile, 'Trial #%i: Poke Duration discrepancy of %i\n', trl, plxSession(trl).PokeDuration - ssnData(trl).PokeDuration); - plxSummary.Errors = [plxSummary.Errors; ['Trial #' num2str(trl) ': Poke Duration discrepancy of ' num2str(plxSession(trl).PokeDuration - ssnData(trl).PokeDuration)]]; + plxData.Summary.Errors = [plxData.Summary.Errors; ['Trial #' num2str(trl) ': Poke Duration discrepancy of ' num2str(plxSession(trl).PokeDuration - ssnData(trl).PokeDuration)]]; end % Check to ensure trial start came before trial end @@ -633,7 +633,6 @@ %% Compile Outputs plxData.Raw = plxSession; -plxData.Summary = plxSummary; fprintf(outfile, 'Behavioral Analysis Completed\n\n********************************************************\n'); From d1fad5471185ce6f0e6da8cae7cf21b44b70cf04 Mon Sep 17 00:00:00 2001 From: gabe Date: Wed, 26 Feb 2020 13:34:38 -0800 Subject: [PATCH 052/133] Update SummarizeUnits_SM.m --- Analyses/SummarizeUnits_SM.m | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/Analyses/SummarizeUnits_SM.m b/Analyses/SummarizeUnits_SM.m index bec277a..d1e09b9 100644 --- a/Analyses/SummarizeUnits_SM.m +++ b/Analyses/SummarizeUnits_SM.m @@ -285,8 +285,10 @@ curEpochCorrTable(1,5) = prR(2); curEpochSigTable(1,5) = prS(2); [peR, peS] = corrcoef(curUniPreTrialActivity(~correctTrialLog & inSeqTrialLog), curUniErrTrlActivity(~correctTrialLog & inSeqTrialLog)); - curEpochCorrTable(1,6) = peR(2); - curEpochSigTable(1,6) = peS(2); + if ~isnan(peR) && length(peR)>1 + curEpochCorrTable(1,6) = peR(2); + curEpochSigTable(1,6) = peS(2); + end % Early-Trial lead [elR, elS] = corrcoef(curUniErlyTrialActivity(correctTrialLog & inSeqTrialLog), curUniLtTrialActivity(correctTrialLog & inSeqTrialLog)); curEpochCorrTable(2,3) = elR(2); @@ -298,8 +300,10 @@ curEpochCorrTable(2,5) = erR(2); curEpochSigTable(2,5) = erS(2); [eeR, eeS] = corrcoef(curUniErlyTrialActivity(~correctTrialLog & inSeqTrialLog), curUniErrTrlActivity(~correctTrialLog & inSeqTrialLog)); - curEpochCorrTable(2,6) = eeR(2); - curEpochSigTable(2,6) = eeS(2); + if ~isnan(eeR) && length(eeR)>1 + curEpochCorrTable(2,6) = eeR(2); + curEpochSigTable(2,6) = eeS(2); + end % Late-Trial lead [lpR, lpS] = corrcoef(curUniLtTrialActivity(correctTrialLog & inSeqTrialLog), curUniPstTrlActivity(correctTrialLog & inSeqTrialLog)); curEpochCorrTable(3,4) = lpR(2); @@ -308,15 +312,19 @@ curEpochCorrTable(3,5) = lrR(2); curEpochSigTable(3,5) = lrS(2); [leR, leS] = corrcoef(curUniLtTrialActivity(~correctTrialLog & inSeqTrialLog), curUniErrTrlActivity(~correctTrialLog & inSeqTrialLog)); - curEpochCorrTable(3,6) = leR(2); - curEpochSigTable(3,6) = leS(2); + if ~isnan(leR) && length(leR)>1 + curEpochCorrTable(3,6) = leR(2); + curEpochSigTable(3,6) = leS(2); + end % Post-Trial lead [prR, prS] = corrcoef(curUniPstTrlActivity(correctTrialLog & inSeqTrialLog), curUniRwdTrlActivity(correctTrialLog & inSeqTrialLog)); curEpochCorrTable(4,5) = prR(2); curEpochSigTable(4,5) = prS(2); [peR, peS] = corrcoef(curUniPstTrlActivity(~correctTrialLog & inSeqTrialLog), curUniErrTrlActivity(~correctTrialLog & inSeqTrialLog)); - curEpochCorrTable(4,6) = peR(2); - curEpochSigTable(4,6) = peS(2); + if ~isnan(peR) && length(peR)>1 + curEpochCorrTable(4,6) = peR(2); + curEpochSigTable(4,6) = peS(2); + end unitInfo(u).TrialEpochStats.EpochCorrelations.R = curEpochCorrTable; unitInfo(u).TrialEpochStats.EpochCorrelations.P = curEpochSigTable; From a0dcce375e33d313438023dd26bda6f5403ca3bf Mon Sep 17 00:00:00 2001 From: gabe Date: Wed, 26 Feb 2020 13:34:47 -0800 Subject: [PATCH 053/133] Create RippleDetection_SM.m --- Analyses/RippleDetection_SM.m | 162 ++++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 Analyses/RippleDetection_SM.m diff --git a/Analyses/RippleDetection_SM.m b/Analyses/RippleDetection_SM.m new file mode 100644 index 0000000..b180256 --- /dev/null +++ b/Analyses/RippleDetection_SM.m @@ -0,0 +1,162 @@ +function [rips] = RippleDetection_SM(envProc, powThresh, durThresh, durThreshMrg, syncThresh, syncWin, smoothWin) +%% RippleDetection_SM +% Inputs: +% - envProc: Enveloping procedure +% - 'RMS' : Use the root mean squared approach to enveloping +% - 'HILB' : Use hilbert transform envelope +% - powThresh: Thresholds used for defining the ripple. +% - First value is the threshold for defining a ripple epoc. +% - Second value is the threshold needed to for an epoc to be +% considered a ripple. +% - durThresh: Threshold used to select ripples of only a particular +% duration. **CURRENTLY NOT IMPLEMENTED** +% - durThreshMrg: Threshold of the inter-ripple-interval used to +% merge together potential ripples that happen close in time +% - syncThresh: Synchrony threshold used to select ripples of a +% particular coherence. **CURRENTLY NOT IMPLEMENTED** +% - syncWin: Window size used to compute sliding window coherence +% across the session. **CURRENTLY NOT IMPLEMENTED** +% - smoothWin: Window size used for gaussian smoothing. + + +%#ok<*IDISVAR,*NODEF,*USENS,*NASGU,*COLND> +%% +if nargin == 0 + envProc = 'RMS'; % Enable for RMS determination of envelope + % envProc = 'HILB'; % Enable for abs(Hilbert) determinination of envelope + powThresh = [0 4]; + durThresh = 15; % Duration Threshold + durThreshMrg = 15; + syncThresh = 0; + syncWin = 10; + smoothWin = 21; +end +%% +%% Define Analysis Features +w = gausswin(smoothWin); +w = w/sum(w); +%% Load Data +origDir = cd; +[fileDir] = uigetdir(origDir); +if fileDir==0 + disp('Analysis Cancelled') + return +else + cd(fileDir) +end +dirContents = dir(fileDir); +fileNames = {dirContents.name}; +tetFileLog = cellfun(@(a)~isempty(a), regexp(fileNames, '_T([0-9]*)_SM.mat')); +tetFiles = fileNames(tetFileLog)'; +behavFileLog = cellfun(@(a)~isempty(a), regexp(fileNames, '_BehaviorMatrix')); +ensembleFileLog = cellfun(@(a)~isempty(a), regexp(fileNames, '_EnsembleMatrix')); +load(fileNames{behavFileLog}); +load(fileNames{ensembleFileLog}); +behavMatrixTrialStruct = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [0 0], 'PokeIn'); + +%% Extract Raw Values & Compute RMS Power +ripBPF = nan(size(behavMatrix,1),size(tetFiles,1)); +ripVolts = nan(size(behavMatrix,1),size(tetFiles,1)); +ripRMS = nan(size(behavMatrix,1),size(tetFiles,1)); +ripHilb = nan(size(behavMatrix,1),size(tetFiles,1)); +ripTetIDs = cell(size(tetFiles)); +for fl = 1:length(tetFiles) + load(tetFiles{fl}) + samp = mode(diff(statMatrix(:,1))); + wIndx = round((1/200)*(1/samp)); + fprintf('%s......', tetFiles{fl}); + ripCol = find(cellfun(@(a)~isempty(a), regexp(statMatrixColIDs, 'LFP_Ripple$'))); + ripVolts(:,fl) = statMatrix(:,2); + if strcmp(envProc, 'RMS') + ripRMS(:,fl) = conv(sqrt(conv(statMatrix(:,ripCol).^2, ones(wIndx,1)/wIndx, 'same')), w, 'same'); + elseif strcmp(envProc, 'HILB') + ripRMS(:,fl) = conv(abs(hilbert(statMatrix(:,ripCol))),w,'same'); + end + ripBPF(:,fl) = statMatrix(:,ripCol); + ripHilb(:,fl) = statMatrix(:,ripCol+1); + ripTetIDs{fl} = statMatrixColIDs{ripCol}; + fprintf('done\n'); +end + +%% Calculate Thresholds +% Aggregate Power +aggPower = mean(ripRMS,2); % Mean envelope +% aggPower = zscore(mean(ripRMS,2)); % Z-Score Mean envelope +% aggPower = mean(zscore(ripRMS),2); % Mean Z-Score envelope + +% Threshold Based on Mean +/- STD Aggregate Power +rmsThresh1 = (mean(aggPower) + (powThresh(1)*std(aggPower))); +rmsThresh2 = (mean(aggPower) + (powThresh(2)*std(aggPower))); + +%% Identify Ripples +% Define putative ripple periods +abvThresh1 = aggPower>rmsThresh1; +epocWindows = [find(diff(abvThresh1)==1), find(diff(abvThresh1)==-1)]; + +% Apply secondary (peak power) threshold +abvThresh2 = aggPower>rmsThresh2; +dualThreshLog = false(size(epocWindows,1),1); +for epoc = 1:size(epocWindows,1) + if sum(abvThresh2(epocWindows(epoc,1):epocWindows(epoc,2))) >=1 + dualThreshLog(epoc) = true; + end +end +epocWindows(~dualThreshLog,:) = []; % Comment out if not using dual thresholds + +% Merge short latency ripples +interEpocInterval = epocWindows(2:end,1) - epocWindows(1:end-1,2); +slrLog = interEpocInterval Date: Wed, 26 Feb 2020 13:34:54 -0800 Subject: [PATCH 054/133] Create RippleAnalysis_SM.m --- Analyses/RippleAnalysis_SM.m | 502 +++++++++++++++++++++++++++++++++++ 1 file changed, 502 insertions(+) create mode 100644 Analyses/RippleAnalysis_SM.m diff --git a/Analyses/RippleAnalysis_SM.m b/Analyses/RippleAnalysis_SM.m new file mode 100644 index 0000000..d87fa25 --- /dev/null +++ b/Analyses/RippleAnalysis_SM.m @@ -0,0 +1,502 @@ +% SWR Detection +%% Define Analysis Features +envProc = 'RMS'; % Enable for RMS determination of envelope +% envProc = 'HILB'; % Enable for abs(Hilbert) determinination of envelope +% %% Thresholds %% % +% Power Threshold % +powThresh1 = 0; % Threshold 1 +powThresh2 = 4; % Threshold 2 +% Duration Thresholds % +durThresh = 15; % Duration Threshold +durThreshMrg = 15; % Merge Duration Threshold +% Synchrony Threshold % +syncThresh = 0; % Synchrony Threshold + +% %% Parameters %% % +% Synchrony Duration % % Not going to worry about this for now... as it will likely be computationally inefficient +syncWin = 10; +% Smoothing Vector % +w = gausswin(21); +w = w/sum(w); + +% %% Trial Windows %% % +% Pre-Trial Window +preTrlWin = 500; +% Post-Trial Window +pstTrlWin = 700; +%% Load Data +origDir = cd; +[fileDir] = uigetdir(origDir); +if fileDir==0 + disp('Analysis Cancelled') + return +else + cd(fileDir) +end +dirContents = dir(fileDir); +fileNames = {dirContents.name}; +tetFileLog = cellfun(@(a)~isempty(a), regexp(fileNames, '_T([0-9]*)_SM.mat')); +tetFiles = fileNames(tetFileLog)'; +behavFileLog = cellfun(@(a)~isempty(a), regexp(fileNames, '_BehaviorMatrix')); +ensembleFileLog = cellfun(@(a)~isempty(a), regexp(fileNames, '_EnsembleMatrix')); +load(fileNames{behavFileLog}); +load(fileNames{ensembleFileLog}); +behavMatrixTrialStruct = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [0 0], 'PokeIn'); + +% poi = [behavMatrixTrialStruct.PokeOutIndex]; +% ri = [behavMatrixTrialStruct.RewardIndex]; +% if length(poi)~=length(ri) +% ri(end:end+(length(poi)-length(ri)))=nan; +% end +% dif = ri-poi; +% perfLog = [behavMatrixTrialStruct.Performance]==1; +% isLog = [behavMatrixTrialStruct.TranspositionDistance]==0; +% iscLog = perfLog & isLog; +% isiLog = ~perfLog & isLog; +% oscLog = perfLog & ~isLog; +% osiLog = ~perfLog & ~isLog; +% +% figure; +% subplot(2,2,1); +% histogram(dif(iscLog)); +% title('InSeq Correct'); +% subplot(2,2,2); +% histogram(dif(isiLog)); +% title('InSeq Incorrect'); +% subplot(2,2,3); +% histogram(dif(osiLog)); +% title('OutSeq Incorrect'); +% subplot(2,2,4); +% histogram(dif(oscLog)); +% title('OutSeq Correct'); +% +% annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string',... +% sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); +% +% annotation('textbox', 'position', [0.1 0.95 0.8 0.05], 'string',... +% '\bfReward Delivery Latency (relative to port withdrawal)', 'horizontalalignment', 'center',... +% 'linestyle', 'none', 'fontsize', 15); +%% Extract Raw Values & Compute RMS Power +ripBPF = nan(size(behavMatrix,1),size(tetFiles,1)); +ripVolts = nan(size(behavMatrix,1),size(tetFiles,1)); +ripRMS = nan(size(behavMatrix,1),size(tetFiles,1)); +ripHilb = nan(size(behavMatrix,1),size(tetFiles,1)); +ripTetIDs = cell(size(tetFiles)); +for fl = 1:length(tetFiles) + load(tetFiles{fl}) + samp = mode(diff(statMatrix(:,1))); + wIndx = round((1/200)*(1/samp)); + fprintf('%s......', tetFiles{fl}); + ripCol = find(cellfun(@(a)~isempty(a), regexp(statMatrixColIDs, 'LFP_Ripple$'))); + ripVolts(:,fl) = statMatrix(:,2); + if strcmp(envProc, 'RMS') + ripRMS(:,fl) = conv(sqrt(conv(statMatrix(:,ripCol).^2, ones(wIndx,1)/wIndx, 'same')), w, 'same'); + elseif strcmp(envProc, 'HILB') + ripRMS(:,fl) = conv(abs(hilbert(statMatrix(:,ripCol))),w,'same'); + end + ripBPF(:,fl) = statMatrix(:,ripCol); + ripHilb(:,fl) = statMatrix(:,ripCol+1); + ripTetIDs{fl} = statMatrixColIDs{ripCol}; + fprintf('done\n'); +end +%% Calculate Thresholds +% Aggregate Power +aggPower = mean(ripRMS,2); aggType = 'Mean'; % Mean envelope +% aggPower = zscore(mean(ripRMS,2)); aggType = 'zMean'; % Z-Score Mean envelope +% aggPower = mean(zscore(ripRMS),2); aggType = 'MeanZ'; % Mean Z-Score envelope + +% Threshold Based on Mean +/- STD Aggregate Power +rmsThresh1 = (mean(aggPower) + (powThresh1*std(aggPower))); +rmsThresh2 = (mean(aggPower) + (powThresh2*std(aggPower))); + +%% Identify Ripples +% Define putative ripple periods +abvThresh1 = aggPower>rmsThresh1; +epocWindows = [find(diff(abvThresh1)==1), find(diff(abvThresh1)==-1)]; + +% Apply secondary (peak power) threshold +abvThresh2 = aggPower>rmsThresh2; +dualThreshLog = false(size(epocWindows,1),1); +for epoc = 1:size(epocWindows,1) + if sum(abvThresh2(epocWindows(epoc,1):epocWindows(epoc,2))) >=1 + dualThreshLog(epoc) = true; + end +end +epocWindows(~dualThreshLog,:) = []; % Comment out if not using dual thresholds + +% Merge short latency ripples +interEpocInterval = epocWindows(2:end,1) - epocWindows(1:end-1,2); +slrLog = interEpocIntervalmedian(epocDur) + set(gca, 'ytick', [median(epocDur), mean(epocDur)], 'yticklabel', [{'Median'}, {'Mean'}]); +else + set(gca, 'ytick', [mean(epocDur), median(epocDur)], 'yticklabel', [{'Mean'}, {'Median'}]); +end +xlabel('Count'); +ylabel([{'Ripple Duration'}; sprintf('Mean: %0.2f; Median: %0.2f', mean(epocDur), median(epocDur))]); +corre = subplot(4,4,[2:4,6:8,10:12]); +corrScatPlot(epocSync, epocDur, [], [], []); +title('Session Wide Ripple Duration vs Mean Synchrony'); +set(gca, 'xticklabel', [], 'yticklabel', []); +sync = subplot(4,4,14:16); +histogram(epocSync, 30); +xlabel('Synchrony (r)'); +ylabel('Count'); + +linkaxes([dur, corre], 'y'); +linkaxes([sync, corre], 'x'); +annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string',... + sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); +annotation('textbox', 'position', [0.01 0.95 0.9 0.05], 'string',... + sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfAggregate = \\rm%s; \\bfMerge \\bfThreshold = \\rm%i ms', powThresh1 ,powThresh2, envProc, aggType, durThreshMrg),... + 'linestyle', 'none'); + +%% Extract Near-Trial Epocs +trialPokeTimes = [[behavMatrixTrialStruct.PokeInIndex]', [behavMatrixTrialStruct.PokeOutIndex]']; +trialRips = cell(size(trialPokeTimes,1),3); +trialRipLat = cell(size(trialPokeTimes,1),3); +trialRipsDur = cell(size(trialPokeTimes,1),3); +trialRipsSync = cell(size(trialPokeTimes,1),3); +for trl = 1:size(trialPokeTimes,1) + preTrlLog = epocWindows(:,1)>(trialPokeTimes(trl,1)-preTrlWin) & epocWindows(:,1)trialPokeTimes(trl,1) & epocWindows(:,1)trialPokeTimes(trl,2) & (epocWindows(:,1) Date: Wed, 26 Feb 2020 13:34:57 -0800 Subject: [PATCH 055/133] Create IdentifySWRs_SM.m --- Analyses/Old/IdentifySWRs_SM.m | 223 +++++++++++++++++++++++++++++++++ 1 file changed, 223 insertions(+) create mode 100644 Analyses/Old/IdentifySWRs_SM.m diff --git a/Analyses/Old/IdentifySWRs_SM.m b/Analyses/Old/IdentifySWRs_SM.m new file mode 100644 index 0000000..4d46d35 --- /dev/null +++ b/Analyses/Old/IdentifySWRs_SM.m @@ -0,0 +1,223 @@ +function IdentifySWRs_SM +%% +%% +origDir = cd; +[fileDir] = uigetdir(origDir); +if fileDir==0 + disp('Analysis Cancelled') + return +else + cd(fileDir) +end +dirContents = dir(fileDir); +fileNames = {dirContents.name}; +tetFileLog = cellfun(@(a)~isempty(a), regexp(fileNames, '_T([0-9]*)_SM.mat')); +tetFiles = fileNames(tetFileLog)'; +behavFileLog = cellfun(@(a)~isempty(a), regexp(fileNames, '_BehaviorMatrix')); +load(fileNames{behavFileLog}); + +%% Create Smoothing Vector +w = gausswin(21); +w = w/sum(w); +%% Extract Raw Values & Compute RMS Power +ripBPF = cell(size(tetFiles))'; +ripVolts = cell(size(tetFiles))'; +ripRMS = cell(size(tetFiles))'; +ripHilb = cell(size(tetFiles))'; +ripTetIDs = cell(size(tetFiles)); +for fl = 1:length(tetFiles) + load(tetFiles{fl}) + samp = mode(diff(statMatrix(:,1))); + wIndx = round((1/250)*(1/samp)); + fprintf('%s......', tetFiles{fl}); + ripCol = find(cellfun(@(a)~isempty(a), regexp(statMatrixColIDs, 'LFP_Ripple$'))); + ripVolts{fl} = statMatrix(:,2); + ripRMS{fl} = conv(sqrt(conv(statMatrix(:,ripCol).^2, ones(wIndx,1)/wIndx, 'same')), w, 'same'); + ripBPF{fl} = statMatrix(:,ripCol); + ripHilb{fl} = statMatrix(:,ripCol+1); + ripTetIDs{fl} = statMatrixColIDs{ripCol}; + fprintf('done\n'); +end + +%% +aggPower = mean(cell2mat(ripRMS),2); +bandPhase = cell2mat(ripHilb); +rmsThresh1 = (mean(aggPower) + (2*std(aggPower))); +rmsThresh2 = (mean(aggPower) + (4*std(aggPower))); +% aggPower = conv(aggPower, w, 'same'); +abvThresh1 = aggPower>rmsThresh1; +abvThresh2 = aggPower>rmsThresh2; +epocWindows = [find(diff(abvThresh1)==1), find(diff(abvThresh1)==-1)]; +dualThreshLog = false(size(epocWindows,1),1); +for epoc = 1:size(epocWindows,1) + if sum(abvThresh2(epocWindows(epoc,1):epocWindows(epoc,2))) >=1 + dualThreshLog(epoc) = true; + end +end +epocWindows(~dualThreshLog,:) = []; % Comment out if not using dual thresholds +epocDur = diff(epocWindows,1,2); +interEpocInterval = epocWindows(2:end,1) - epocWindows(1:end-1,2); +% +% Merge Ripples that Occur within 15ms of eachother +shortLatRipls = find(interEpocInterval<15); +for slr = 1:length(shortLatRipls) + epocWindows(shortLatRipls(slr),2) = epocWindows(shortLatRipls(slr)+1,2); +end +epocWindows(shortLatRipls+1,:) = []; + +epocDur = diff(epocWindows,1,2); +interEpocInterval = epocWindows(2:end,1) - epocWindows(1:end-1,2); + +% epocWindows(epocDur<15,:) = []; % Comment out if not using duration threshold + +epocDur = diff(epocWindows,1,2); +interEpocInterval = epocWindows(2:end,1) - epocWindows(1:end-1,2); + +figure; +subplot(2,2,1) +histogram(epocDur,0:1:100); +hold on; +line(repmat(mean(epocDur), [1,2]), get(gca, 'ylim'), 'color', 'r', 'linewidth', 2); +line(repmat(median(epocDur), [1,2]), get(gca, 'ylim'), 'color', 'r', 'linestyle', '--', 'linewidth', 2); +if mean(epocDur)>median(epocDur) + set(gca, 'xtick', [median(epocDur), mean(epocDur)], 'xticklabel', [{'Median'}, {'Mean'}], 'xticklabelrotation', 90); +else + set(gca, 'xtick', [mean(epocDur), median(epocDur)], 'xticklabel', [{'Mean'}, {'Median'}], 'xticklabelrotation', 90); +end +xlabel('Duration (ms)'); +title([{'Ripple Duration'}; sprintf('Mean: %0.2f; Median: %0.2f', mean(epocDur), median(epocDur))]); +subplot(2,2,3) +histogram(interEpocInterval,0:10:500); +xlabel('Latency (ms)'); +title('Inter-Ripple-Interval'); +subplot(2,2,[2 4]); +corrScatPlot(epocDur(1:end-1), interEpocInterval, 'Duration', 'Latency',[]); + +%% Format Output +tsVect = statMatrix(:,1); +swrIndices = tsVect(epocWindows); +swrVectLog = false(size(tsVect)); +for rpl = 1:size(epocWindows,1) + swrVectLog(epocWindows(rpl,1):epocWindows(rpl,2)) = true; +end +save('RippleEvents.mat', 'swrIndices', 'swrVectLog'); +%% +chan = 2; + +pokeCol = strcmp(behavMatrixColIDs, 'PokeEvents'); +pokeEvents = [find(behavMatrix(:,pokeCol)==1), find(behavMatrix(:,pokeCol)==-1)]; + +scrsz = get(groot,'ScreenSize'); +figure('position', [1 scrsz(4)*.45 scrsz(3) scrsz(4)*.47]); +plot(ripRMS{chan}, 'linewidth', 2); +a = gca; +hold on; +plot(aggPower, 'linewidth', 2, 'color', 'g'); +plot(a, ripBPF{chan}, 'color', 'r'); +line(a,[0 length(ripRMS{chan})], repmat(mean(ripRMS{chan})+ std(ripRMS{chan})*2, [1 2]), 'linestyle', '-', 'color', 'black', 'linewidth', 2); +line(a,[0 length(ripRMS{chan})], repmat(mean(ripRMS{chan}) + std(ripRMS{chan})*4, [1 2]), 'linestyle', '--', 'color', 'black', 'linewidth', 2); +for pk = 1:length(pokeEvents) + line(a, pokeEvents(pk,:), ones(1,2)*0.2, 'marker', '*', 'color', 'k'); +end + +for swr = 1:length(epocWindows) + line(a,epocWindows(swr,:), ones(1,2)*0.25, 'marker', 'o', 'color', 'k'); +end + +figure('position', [1 1 scrsz(3) scrsz(4)/2]); +plot(ripVolts{chan}); +b = gca; +linkaxes([a b], 'x') + +%% SOMETHING'S BROKE HERE +% peakLoc = nan(size(epocWindows,1),1); +% peakPwr = nan(size(epocWindows,1),1); +% phaseMtx = cell(size(epocWindows,1),1); +% phaseCorr = nan(size(epocWindows,1),1); +% parfor epoc = 1:size(epocWindows,1) +% curEpocWindow = epocWindows(epoc,1):epocWindows(epoc,2); +% [tmpPks, tmpLocs] = findpeaks(aggPower(curEpocWindow), 'MinPeakHeight', rmsThresh, 'MinPeakProminence', rmsThresh); +% if isempty(tmpPks) +% [tmpPks, tmpLocs] = findpeaks(aggPower(curEpocWindow)); +% % if isempty(tmpPks) +% % disp('EMPTY PEAKS') +% % continue +% % % tmpPks = aggPower(curEpocWindow); +% % % tmpLocs = 1:length(tmpPks); +% % end +% end +% if ~isempty(tmpPks) +% curEpocPhaseMtx = nan(size(bandPhase,2)); +% for tetA = 1:size(bandPhase,2) +% for tetB = 1:size(bandPhase,2) +% curEpocPhaseMtx(tetA,tetB) = circ_corrcc(bandPhase(curEpocWindow,tetA), bandPhase(curEpocWindow,tetB)); +% end +% end +% phaseMtx{epoc} = curEpocPhaseMtx; +% phaseCorr(epoc) = mean(curEpocPhaseMtx(triu(true(size(curEpocPhaseMtx)),1))); +% peakPwr(epoc) = (max(tmpPks)-mean(aggPower))/std(aggPower); % z-norm peak power; +% tmpEpocPkLoc = tmpLocs(tmpPks==max(tmpPks)); +% if size(tmpEpocPkLoc)>1 +% tmpEpocPkLoc = tmpEpocPkLoc(1); +% end +% tempEpocPkLoc = epocWindows(epoc,1)+tmpEpocPkLoc; +% peakLoc(epoc) = tempEpocPkLoc; +% end +% end + +% %% Edit the SWRs +% % Combine short SWRs +% +% % Threshold SWRs with >=12.5ms duration +% +% %... may need to throw this into the loop above... check into this later +% %on. +% +% %% +% ripDur = diff(epocWindows,1,2)*samp; +% epocWindows(ripDur<0.0125,:) = []; +% peakPwr(ripDur<0.0125,:) = []; +% phaseCorr(ripDur<0.0125,:) = []; +% %% +% pokeCol = strcmp(behavMatrixColIDs, 'PokeEvents'); +% +% pokeEvents = [find(behavMatrix(:,pokeCol)==1), find(behavMatrix(:,pokeCol)==-1)]; +% figure +% for pk = 1:length(pokeEvents) +% line(pokeEvents(pk,:), ones(1,2), 'marker', '*', 'color', 'r'); +% hold on; +% end +% for swr = 1:length(epocWindows) +% line(epocWindows(swr,:), ones(1,2)*2, 'marker', 'o', 'color', 'k'); +% end +% +% set(gca, 'ylim', [-1 4]); +% +% %% +% figure; +% subplot(2,3,1) +% histogram(diff(epocWindows,1,2)*samp); +% +% subplot(2,3,2) +% corrScatPlot(diff(epocWindows,1,2)*samp, peakPwr, 'SWR Dur', 'SWR Peak Power', []); +% +% subplot(2,3,4) +% histogram(phaseCorr); +% +% subplot(2,3,5) +% corrScatPlot(diff(epocWindows,1,2)*samp, phaseCorr, 'SWR Dur', 'Mean Coherence', []); +% +% subplot(2,3,6) +% corrScatPlot(peakPwr, phaseCorr, 'SWR Peak Power', 'Mean Coherence', []); +% +% +% %% +% figure +% plot(repmat(statMatrix(:,1), [1, length(ripBPF)]), cell2mat(ripBPF)) +% hold on; +% for epoc = 1:size(epocWindows,1) +% line([statMatrix(epocWindows(epoc,1),1), statMatrix(epocWindows(epoc,2),1)], [0.2 0.2], 'marker', '*', 'color','k') +% end +% + + From 271f230d69b5195ce6d0ec25e620e9ab0ac980b9 Mon Sep 17 00:00:00 2001 From: gabe Date: Wed, 26 Feb 2020 13:35:00 -0800 Subject: [PATCH 056/133] Delete IdentifySWRs_SM.m --- Analyses/IdentifySWRs_SM.m | 223 ------------------------------------- 1 file changed, 223 deletions(-) delete mode 100644 Analyses/IdentifySWRs_SM.m diff --git a/Analyses/IdentifySWRs_SM.m b/Analyses/IdentifySWRs_SM.m deleted file mode 100644 index 033f5e6..0000000 --- a/Analyses/IdentifySWRs_SM.m +++ /dev/null @@ -1,223 +0,0 @@ -function IdentifySWRs_SM -%% -%% -origDir = cd; -[fileDir] = uigetdir(origDir); -if fileDir==0 - disp('Analysis Cancelled') - return -else - cd(fileDir) -end -dirContents = dir(fileDir); -fileNames = {dirContents.name}; -tetFileLog = cellfun(@(a)~isempty(a), regexp(fileNames, '_T([0-9]*)_SM.mat')); -tetFiles = fileNames(tetFileLog)'; -behavFileLog = cellfun(@(a)~isempty(a), regexp(fileNames, '_BehaviorMatrix')); -load(fileNames{behavFileLog}); - -%% Create Smoothing Vector -w = gausswin(21); -w = w/sum(w); -%% Extract Raw Values & Compute RMS Power -ripBPF = cell(size(tetFiles))'; -ripVolts = cell(size(tetFiles))'; -ripRMS = cell(size(tetFiles))'; -ripHilb = cell(size(tetFiles))'; -ripTetIDs = cell(size(tetFiles)); -for fl = 1:length(tetFiles) - load(tetFiles{fl}) - samp = mode(diff(statMatrix(:,1))); - wIndx = round((1/250)*(1/samp)); - fprintf('%s......', tetFiles{fl}); - ripCol = find(cellfun(@(a)~isempty(a), regexp(statMatrixColIDs, 'LFP_Ripple$'))); - ripVolts{fl} = statMatrix(:,2); - ripRMS{fl} = conv(sqrt(conv(statMatrix(:,ripCol).^2, ones(wIndx,1)/wIndx, 'same')), w, 'same'); - ripBPF{fl} = statMatrix(:,ripCol); - ripHilb{fl} = statMatrix(:,ripCol+1); - ripTetIDs{fl} = statMatrixColIDs{ripCol}; - fprintf('done\n'); -end - -%% -aggPower = mean(cell2mat(ripRMS),2); -bandPhase = cell2mat(ripHilb); -rmsThresh1 = (mean(aggPower) + (2*std(aggPower))); -rmsThresh2 = (mean(aggPower) + (4*std(aggPower))); -% aggPower = conv(aggPower, w, 'same'); -abvThresh1 = aggPower>rmsThresh1; -abvThresh2 = aggPower>rmsThresh2; -epocWindows = [find(diff(abvThresh1)==1), find(diff(abvThresh1)==-1)]; -dualThreshLog = false(size(epocWindows,1),1); -for epoc = 1:size(epocWindows,1) - if sum(abvThresh2(epocWindows(epoc,1):epocWindows(epoc,2))) >=1 - dualThreshLog(epoc) = true; - end -end -epocWindows(~dualThreshLog,:) = []; % Comment out if not using dual thresholds -epocDur = diff(epocWindows,1,2); -interEpocInterval = epocWindows(2:end,1) - epocWindows(1:end-1,2); -% -% Merge Ripples that Occur within 15ms of eachother -shortLatRipls = find(interEpocInterval<10); -for slr = 1:length(shortLatRipls) - epocWindows(shortLatRipls(slr),2) = epocWindows(shortLatRipls(slr)+1,2); -end -epocWindows(shortLatRipls+1,:) = []; - -epocDur = diff(epocWindows,1,2); -interEpocInterval = epocWindows(2:end,1) - epocWindows(1:end-1,2); - -% epocWindows(epocDur<15,:) = []; % Comment out if not using duration threshold - -epocDur = diff(epocWindows,1,2); -interEpocInterval = epocWindows(2:end,1) - epocWindows(1:end-1,2); - -figure; -subplot(2,2,1) -histogram(epocDur,0:1:100); -hold on; -line(repmat(mean(epocDur), [1,2]), get(gca, 'ylim'), 'color', 'r', 'linewidth', 2); -line(repmat(median(epocDur), [1,2]), get(gca, 'ylim'), 'color', 'r', 'linestyle', '--', 'linewidth', 2); -if mean(epocDur)>median(epocDur) - set(gca, 'xtick', [median(epocDur), mean(epocDur)], 'xticklabel', [{'Median'}, {'Mean'}], 'xticklabelrotation', 90); -else - set(gca, 'xtick', [mean(epocDur), median(epocDur)], 'xticklabel', [{'Mean'}, {'Median'}], 'xticklabelrotation', 90); -end -xlabel('Duration (ms)'); -title([{'Ripple Duration'}; sprintf('Mean: %0.2f; Median: %0.2f', mean(epocDur), median(epocDur))]); -subplot(2,2,3) -histogram(interEpocInterval,0:10:500); -xlabel('Latency (ms)'); -title('Inter-Ripple-Interval'); -subplot(2,2,[2 4]); -corrScatPlot(epocDur(1:end-1), interEpocInterval, 'Duration', 'Latency',[]); - -%% Format Output -tsVect = statMatrix(:,1); -swrIndices = tsVect(epocWindows); -swrVectLog = false(size(tsVect)); -for rpl = 1:size(epocWindows,1) - swrVectLog(epocWindows(rpl,1):epocWindows(rpl,2)) = true; -end -save('RippleEvents.mat', 'swrIndices', 'swrVectLog'); -%% -chan = 2; - -pokeCol = strcmp(behavMatrixColIDs, 'PokeEvents'); -pokeEvents = [find(behavMatrix(:,pokeCol)==1), find(behavMatrix(:,pokeCol)==-1)]; - -scrsz = get(groot,'ScreenSize'); -figure('position', [1 scrsz(4)*.45 scrsz(3) scrsz(4)*.47]); -plot(ripRMS{chan}, 'linewidth', 2); -a = gca; -hold on; -plot(aggPower, 'linewidth', 2, 'color', 'g'); -plot(a, ripBPF{chan}, 'color', 'r'); -line(a,[0 length(ripRMS{chan})], repmat(mean(ripRMS{chan})+ std(ripRMS{chan})*2, [1 2]), 'linestyle', '-', 'color', 'black', 'linewidth', 2); -line(a,[0 length(ripRMS{chan})], repmat(mean(ripRMS{chan}) + std(ripRMS{chan})*4, [1 2]), 'linestyle', '--', 'color', 'black', 'linewidth', 2); -for pk = 1:length(pokeEvents) - line(a, pokeEvents(pk,:), ones(1,2)*0.2, 'marker', '*', 'color', 'k'); -end - -for swr = 1:length(epocWindows) - line(a,epocWindows(swr,:), ones(1,2)*0.25, 'marker', 'o', 'color', 'k'); -end - -figure('position', [1 1 scrsz(3) scrsz(4)/2]); -plot(ripVolts{chan}); -b = gca; -linkaxes([a b], 'x') - -%% SOMETHING'S BROKE HERE -% peakLoc = nan(size(epocWindows,1),1); -% peakPwr = nan(size(epocWindows,1),1); -% phaseMtx = cell(size(epocWindows,1),1); -% phaseCorr = nan(size(epocWindows,1),1); -% parfor epoc = 1:size(epocWindows,1) -% curEpocWindow = epocWindows(epoc,1):epocWindows(epoc,2); -% [tmpPks, tmpLocs] = findpeaks(aggPower(curEpocWindow), 'MinPeakHeight', rmsThresh, 'MinPeakProminence', rmsThresh); -% if isempty(tmpPks) -% [tmpPks, tmpLocs] = findpeaks(aggPower(curEpocWindow)); -% % if isempty(tmpPks) -% % disp('EMPTY PEAKS') -% % continue -% % % tmpPks = aggPower(curEpocWindow); -% % % tmpLocs = 1:length(tmpPks); -% % end -% end -% if ~isempty(tmpPks) -% curEpocPhaseMtx = nan(size(bandPhase,2)); -% for tetA = 1:size(bandPhase,2) -% for tetB = 1:size(bandPhase,2) -% curEpocPhaseMtx(tetA,tetB) = circ_corrcc(bandPhase(curEpocWindow,tetA), bandPhase(curEpocWindow,tetB)); -% end -% end -% phaseMtx{epoc} = curEpocPhaseMtx; -% phaseCorr(epoc) = mean(curEpocPhaseMtx(triu(true(size(curEpocPhaseMtx)),1))); -% peakPwr(epoc) = (max(tmpPks)-mean(aggPower))/std(aggPower); % z-norm peak power; -% tmpEpocPkLoc = tmpLocs(tmpPks==max(tmpPks)); -% if size(tmpEpocPkLoc)>1 -% tmpEpocPkLoc = tmpEpocPkLoc(1); -% end -% tempEpocPkLoc = epocWindows(epoc,1)+tmpEpocPkLoc; -% peakLoc(epoc) = tempEpocPkLoc; -% end -% end - -% %% Edit the SWRs -% % Combine short SWRs -% -% % Threshold SWRs with >=12.5ms duration -% -% %... may need to throw this into the loop above... check into this later -% %on. -% -% %% -% ripDur = diff(epocWindows,1,2)*samp; -% epocWindows(ripDur<0.0125,:) = []; -% peakPwr(ripDur<0.0125,:) = []; -% phaseCorr(ripDur<0.0125,:) = []; -% %% -% pokeCol = strcmp(behavMatrixColIDs, 'PokeEvents'); -% -% pokeEvents = [find(behavMatrix(:,pokeCol)==1), find(behavMatrix(:,pokeCol)==-1)]; -% figure -% for pk = 1:length(pokeEvents) -% line(pokeEvents(pk,:), ones(1,2), 'marker', '*', 'color', 'r'); -% hold on; -% end -% for swr = 1:length(epocWindows) -% line(epocWindows(swr,:), ones(1,2)*2, 'marker', 'o', 'color', 'k'); -% end -% -% set(gca, 'ylim', [-1 4]); -% -% %% -% figure; -% subplot(2,3,1) -% histogram(diff(epocWindows,1,2)*samp); -% -% subplot(2,3,2) -% corrScatPlot(diff(epocWindows,1,2)*samp, peakPwr, 'SWR Dur', 'SWR Peak Power', []); -% -% subplot(2,3,4) -% histogram(phaseCorr); -% -% subplot(2,3,5) -% corrScatPlot(diff(epocWindows,1,2)*samp, phaseCorr, 'SWR Dur', 'Mean Coherence', []); -% -% subplot(2,3,6) -% corrScatPlot(peakPwr, phaseCorr, 'SWR Peak Power', 'Mean Coherence', []); -% -% -% %% -% figure -% plot(repmat(statMatrix(:,1), [1, length(ripBPF)]), cell2mat(ripBPF)) -% hold on; -% for epoc = 1:size(epocWindows,1) -% line([statMatrix(epocWindows(epoc,1),1), statMatrix(epocWindows(epoc,2),1)], [0.2 0.2], 'marker', '*', 'color','k') -% end -% - - From faa741f4a3abea79e4752332bf4d36374d0e98a0 Mon Sep 17 00:00:00 2001 From: gabe Date: Wed, 26 Feb 2020 13:35:03 -0800 Subject: [PATCH 057/133] Create ExtractTrialEventRips_SM.m --- Analyses/ExtractTrialEventRips_SM.m | 40 +++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Analyses/ExtractTrialEventRips_SM.m diff --git a/Analyses/ExtractTrialEventRips_SM.m b/Analyses/ExtractTrialEventRips_SM.m new file mode 100644 index 0000000..ab69315 --- /dev/null +++ b/Analyses/ExtractTrialEventRips_SM.m @@ -0,0 +1,40 @@ +function [trialRipStruct] = ExtractTrialEventRips_SM(rips, trialWin) +% ExtractTrialEventRips_SM +% Extracts and organizes ripple data relative to the trial periods +% +% Inputs: +% - rips: Ripple data structure created by RippleDetection_SM +% - trialWin: Periods used for delineation of trial event related +% SWR. +% - First value is period prior to poke initiation considered +% as the "pre-trial" period +% - Second value is the period after poke withdrawal +% considered as the "post-trial" period + +%% Extract Near-Trial Epocs +trialPokeTimes = rips.TrialInfo.TrialPokes; +trialRips = cell(size(trialPokeTimes,1),3); +trialRipLat = cell(size(trialPokeTimes,1),3); +trialRipsDur = cell(size(trialPokeTimes,1),3); +trialRipsSync = cell(size(trialPokeTimes,1),3); +for trl = 1:size(trialPokeTimes,1) + preTrlLog = rips.Ripples.Events(:,1)>(trialPokeTimes(trl,1)-trialWin(1)) & rips.Ripples.Events(:,1)trialPokeTimes(trl,1) & rips.Ripples.Events(:,1)trialPokeTimes(trl,2) & (rips.Ripples.Events(:,1) Date: Wed, 26 Feb 2020 13:35:06 -0800 Subject: [PATCH 058/133] Create CurateRipples_SM.m --- Analyses/CurateRipples_SM.m | 116 ++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 Analyses/CurateRipples_SM.m diff --git a/Analyses/CurateRipples_SM.m b/Analyses/CurateRipples_SM.m new file mode 100644 index 0000000..278772f --- /dev/null +++ b/Analyses/CurateRipples_SM.m @@ -0,0 +1,116 @@ +%% CurateRipples(rips, trialRips) + +%% +rips = RippleDetection_SM; +[trialRips] = ExtractTrialEventRips_SM(rips, [500 700]); +allTrialRips = sortrows(cell2mat([trialRips.Events(:,1); trialRips.Events(:,2); trialRips.Events(:,3)])); +%% Create Figure +ripCure = figure; +rawAxes = axes(ripCure, 'position', [0.1, 0.75, 0.7, 0.2]); +bpfAxes = axes(ripCure, 'position', [0.1, 0.55, 0.7, 0.2]); +spkAxes = axes(ripCure, 'position', [0.1, 0.2, 0.7, 0.3]); +linkaxes([spkAxes, bpfAxes, rawAxes], 'x'); + +% Overall Ripple List +ssnRipTitleAx = axes(ripCure, 'position', [0.825, 0.925, 0.11, 0.05]); +set(ssnRipTitleAx, 'xlim', [-0.5 0.5], 'ylim', [0 0.5]); +text(ssnRipTitleAx, 0,0, 'Session Rips', 'horizontalalignment', 'center', 'verticalalignment', 'bottom'); +axis(ssnRipTitleAx, 'off'); +ssnRipList = uicontrol(ripCure, 'Units', 'Normalized', 'Style', 'listbox', 'String', 1:size(rips.Ripples.Events,1),... + 'Position', [0.825, 0.55, 0.15, 0.375], 'Tag', 'ssnRip_Lst', 'Callback', @SelectSsnRip, 'userData', rips.TimeStamps(rips.Ripples.Events)); + +% Trial Ripple List +trlRipTitleAx = axes(ripCure, 'position', [0.825, 0.5, 0.11, 0.05]); +set(trlRipTitleAx, 'xlim', [-0.5 0.5], 'ylim', [0 0.5]); +text(trlRipTitleAx, 0,0, 'Trial Rips', 'horizontalalignment', 'center', 'verticalalignment', 'bottom'); +axis(trlRipTitleAx, 'off'); +trlRipList = uicontrol(ripCure, 'Units', 'Normalized', 'Style', 'listbox', 'String', 1:size(allTrialRips,1),... + 'Position', [0.825, 0.2, 0.15, 0.300], 'Tag', 'trlRip_Lst', 'Callback', @SelectTrlRip, 'userData', rips.TimeStamps(allTrialRips)); + +% Zoom In +zoomOutBtn = uicontrol(ripCure, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', 'Zm-',... + 'Position', [0.35, 0.06, 0.05, 0.075], 'Callback', @ZoomOut); +% Zoom Out +zoomInBtn = uicontrol(ripCure, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', 'Zm+',... + 'Position', [0.65, 0.06, 0.05, 0.075], 'Callback', @ZoomIn); + +% Previous Ripple +prevRipBtn = uicontrol(ripCure, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', '<< Previous Ripple',... + 'Position', [0.05, 0.05, 0.2, 0.1], 'Callback', @PrevRip); +% Next Ripple +nextRipBtn = uicontrol(ripCure, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', 'Next Ripple >>',... + 'Position', [0.75, 0.05, 0.2, 0.1], 'Callback', @NextRip); + +% Remove Ripple +removeRipBtn = uicontrol(ripCure, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', 'Remove Ripple',... + 'Position', [0.425, 0.075, 0.2, 0.05], 'Callback', @RmvRip); + +%% Plot Stuff +% Plot Raw LFP Traces +raw = plot(rawAxes, rips.TimeStamps, rips.SessionData.RawLFP, 'color', 'k'); +for r = 1:length(raw) + raw(r).Color(4) = 0.2; +end +% Plot Ripple Band LFP Traces +bpf = plot(bpfAxes, rips.TimeStamps, rips.SessionData.RipBPF, 'color', 'k'); +for b = 1:length(bpf) + bpf(b).Color(4) = 0.2; +end +% Plot Spiking Activity +[spkX, spkY] = find(rips.SessionData.Spikes~=0); +scatter(spkAxes, rips.TimeStamps(spkX),spkY, '*k'); +xlabel(spkAxes, 'Time (m)'); + +% Fiddle with Axes +set(rawAxes, 'color', 'none', 'ycolor', 'none', 'xcolor', 'none', 'xticklabel', [], 'box', 'off'); +set(bpfAxes,'color', 'none', 'ycolor', 'none', 'xticklabel', [], 'box', 'off'); +set(spkAxes, 'color', 'none', 'ycolor', 'none', 'box', 'off'); +axis([rawAxes, bpfAxes,spkAxes], 'tight'); +rawLim = repmat(get(rawAxes,'ylim'),[2,1]); +bpfLim = repmat(get(bpfAxes,'ylim'),[2,1]); +spkLim = repmat(get(spkAxes,'ylim'),[2,1]); +spkLim(:,1) = spkLim(:,1)-1; +spkLim(:,2) = spkLim(:,2)+1; +% Plot Trial Periods +for trl = 1:size(rips.TrialInfo.TrialPokes,1) + switch rips.TrialInfo.OdorVect(trl) + case 1 + patchColor = [44/255 168/255 224/255]; + case 2 + patchColor = [154/255 133/255 122/255]; + case 3 + patchColor = [9/255 161/255 74/255]; + case 4 + patchColor = [128/255 66/255 151/255]; + case 5 + patchColor = [241/255 103/255 36/255]; + end + patch(rawAxes, 'XData', [rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)),rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1))],... + 'YData', rawLim(:),... + 'FaceColor', patchColor, 'FaceAlpha', 0.5,... + 'EdgeColor', patchColor, 'EdgeAlpha', 0.5); + patch(bpfAxes, 'XData', [rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)),rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1))],... + 'YData', bpfLim(:),... + 'FaceColor', patchColor, 'FaceAlpha', 0.5,... + 'EdgeColor', patchColor, 'EdgeAlpha', 0.5); + patch(spkAxes, 'XData', [rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)),rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1))],... + 'YData', spkLim(:),... + 'FaceColor', patchColor, 'FaceAlpha', 0.5,... + 'EdgeColor', patchColor, 'EdgeAlpha', 0.5); +end + +for rip = 1:size(rips.Ripples.Events,1) + curRipX = [rips.TimeStamps(rips.Ripples.Events(rip,:)); flipud(rips.TimeStamps(rips.Ripples.Events(rip,:)))]'; + patch(rawAxes, 'XData', curRipX,... + 'YData', rawLim(:),... + 'FaceColor', 'y', 'FaceAlpha', 0.15,... + 'EdgeColor', 'y', 'EdgeAlpha', 0.5); + patch(bpfAxes, 'XData', curRipX,... + 'YData', bpfLim(:),... + 'FaceColor', 'y', 'FaceAlpha', 0.15,... + 'EdgeColor', 'y', 'EdgeAlpha', 0.5); + patch(spkAxes, 'XData', curRipX,... + 'YData', spkLim(:),... + 'FaceColor', 'y', 'FaceAlpha', 0.15,... + 'EdgeColor', 'y', 'EdgeAlpha', 0.5); +end From af81eb4475794a9a1709b770729098ecb74b927e Mon Sep 17 00:00:00 2001 From: gaelias Date: Wed, 26 Feb 2020 16:15:00 -0800 Subject: [PATCH 059/133] Update RippleDetection_SM.m --- Analyses/RippleDetection_SM.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Analyses/RippleDetection_SM.m b/Analyses/RippleDetection_SM.m index b180256..2bbd68f 100644 --- a/Analyses/RippleDetection_SM.m +++ b/Analyses/RippleDetection_SM.m @@ -142,7 +142,7 @@ %% Organize Data Output rips = struct(... - 'TimeStamps', statMatrix(:,1)/60,... + 'TimeStamps', statMatrix(:,1),... 'Ripples', struct('Events', epocWindows, 'Duration', epocDur,... 'Synchrony', epocSync),... 'SessionData', struct('RawLFP', ripVolts, 'RipBPF', ripBPF,... From 527065ba906c11372260aa210b600a3ec7b27d88 Mon Sep 17 00:00:00 2001 From: gaelias Date: Wed, 26 Feb 2020 16:15:04 -0800 Subject: [PATCH 060/133] Update CurateRipples_SM.m --- Analyses/CurateRipples_SM.m | 79 ++++++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/Analyses/CurateRipples_SM.m b/Analyses/CurateRipples_SM.m index 278772f..0ea72f0 100644 --- a/Analyses/CurateRipples_SM.m +++ b/Analyses/CurateRipples_SM.m @@ -1,5 +1,6 @@ %% CurateRipples(rips, trialRips) - +global listSel +listSel = 1; % Used to keep track of which list is being selected from for ripple viewing %% rips = RippleDetection_SM; [trialRips] = ExtractTrialEventRips_SM(rips, [500 700]); @@ -51,15 +52,19 @@ for r = 1:length(raw) raw(r).Color(4) = 0.2; end +set(rawAxes, 'Tag', 'Raw_Axes'); % Plot Ripple Band LFP Traces bpf = plot(bpfAxes, rips.TimeStamps, rips.SessionData.RipBPF, 'color', 'k'); for b = 1:length(bpf) bpf(b).Color(4) = 0.2; end +set(bpfAxes, 'Tag', 'Bpf_Axes'); % Plot Spiking Activity [spkX, spkY] = find(rips.SessionData.Spikes~=0); scatter(spkAxes, rips.TimeStamps(spkX),spkY, '*k'); +% line(spkAxes, rips.TimeStamps(spkX),spkY, 'marker', '*', 'linestyle', 'none', 'color', 'k'); xlabel(spkAxes, 'Time (m)'); +set(spkAxes, 'Tag', 'Spk_Axes'); % Fiddle with Axes set(rawAxes, 'color', 'none', 'ycolor', 'none', 'xcolor', 'none', 'xticklabel', [], 'box', 'off'); @@ -114,3 +119,75 @@ 'FaceColor', 'y', 'FaceAlpha', 0.15,... 'EdgeColor', 'y', 'EdgeAlpha', 0.5); end + +clear rips +%% Callbacks +function SelectSsnRip(source,event) +global listSel +listSel = 1; +kids = get(get(source, 'Parent'), 'Children'); +tags = arrayfun(@(a)a.Tag, kids, 'uniformoutput',0); +axTag = strcmp(tags, 'Raw_Axes'); +set(kids(axTag), 'xLim', [source.UserData(source.Value,1)-(50/60000),... + source.UserData(source.Value,2)+(50/60000)]); +end + +function SelectTrlRip(source,event) +global listSel +listSel = 2; +kids = get(get(source, 'Parent'), 'Children'); +tags = arrayfun(@(a)a.Tag, kids, 'uniformoutput',0); +axTag = strcmp(tags, 'Raw_Axes'); +set(kids(axTag), 'xLim', [source.UserData(source.Value,1)-(50/60000),... + source.UserData(source.Value,2)+(50/60000)]); +end + +function NextRip(source,event) +global listSel +if listSel == 1 + lstTarg = 'ssnRip_Lst'; +else + lstTarg = 'trlRip_Lst'; +end +kids = get(get(source, 'Parent'), 'Children'); +tags = arrayfun(@(a)a.Tag, kids, 'uniformoutput',0); +lstTag = strcmp(tags, lstTarg); +kids(lstTag).Value = kids(lstTag).Value+1; +axTag = strcmp(tags, 'Raw_Axes'); +set(kids(axTag), 'xLim', [kids(lstTag).UserData(kids(lstTag).Value,1)-(50),... + kids(lstTag).UserData(kids(lstTag).Value,2)+(50)]); +end + +function PrevRip(source,event) +global listSel +if listSel == 1 + lstTarg = 'ssnRip_Lst'; +else + lstTarg = 'trlRip_Lst'; +end +kids = get(get(source, 'Parent'), 'Children'); +tags = arrayfun(@(a)a.Tag, kids, 'uniformoutput',0); +lstTag = strcmp(tags, lstTarg); +kids(lstTag).Value = kids(lstTag).Value-1; +axTag = strcmp(tags, 'Raw_Axes'); +set(kids(axTag), 'xLim', [kids(lstTag).UserData(kids(lstTag).Value,1)-(50),... + kids(lstTag).UserData(kids(lstTag).Value,2)+(50)]); +end + +function ZoomOut(source,event) +kids = get(get(source, 'Parent'), 'Children'); +tags = arrayfun(@(a)a.Tag, kids, 'uniformoutput',0); +axTag = strcmp(tags, 'Raw_Axes'); +curX = get(kids(axTag), 'xLim'); +set(kids(axTag), 'xLim', [curX(1)-(10),... + curX(2)+(10)]); +end + +function ZoomIn(source,event) +kids = get(get(source, 'Parent'), 'Children'); +tags = arrayfun(@(a)a.Tag, kids, 'uniformoutput',0); +axTag = strcmp(tags, 'Raw_Axes'); +curX = get(kids(axTag), 'xLim'); +set(kids(axTag), 'xLim', [curX(1)+(10),... + curX(2)-(10)]); +end \ No newline at end of file From a7ccf3dfeb84035067d7e362af96c3b68fe651f6 Mon Sep 17 00:00:00 2001 From: gaelias Date: Wed, 26 Feb 2020 16:15:23 -0800 Subject: [PATCH 061/133] Update CurateRipples_SM.m --- Analyses/CurateRipples_SM.m | 1 - 1 file changed, 1 deletion(-) diff --git a/Analyses/CurateRipples_SM.m b/Analyses/CurateRipples_SM.m index 0ea72f0..156f10b 100644 --- a/Analyses/CurateRipples_SM.m +++ b/Analyses/CurateRipples_SM.m @@ -120,7 +120,6 @@ 'EdgeColor', 'y', 'EdgeAlpha', 0.5); end -clear rips %% Callbacks function SelectSsnRip(source,event) global listSel From ed2cf989392b50c8cb7108d31f9b84145df09922 Mon Sep 17 00:00:00 2001 From: gaelias Date: Wed, 26 Feb 2020 16:17:57 -0800 Subject: [PATCH 062/133] Update CurateRipples_SM.m --- Analyses/CurateRipples_SM.m | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Analyses/CurateRipples_SM.m b/Analyses/CurateRipples_SM.m index 156f10b..382cf37 100644 --- a/Analyses/CurateRipples_SM.m +++ b/Analyses/CurateRipples_SM.m @@ -127,8 +127,8 @@ function SelectSsnRip(source,event) kids = get(get(source, 'Parent'), 'Children'); tags = arrayfun(@(a)a.Tag, kids, 'uniformoutput',0); axTag = strcmp(tags, 'Raw_Axes'); -set(kids(axTag), 'xLim', [source.UserData(source.Value,1)-(50/60000),... - source.UserData(source.Value,2)+(50/60000)]); +set(kids(axTag), 'xLim', [source.UserData(source.Value,1)-(50),... + source.UserData(source.Value,2)+(50)]); end function SelectTrlRip(source,event) @@ -137,8 +137,8 @@ function SelectTrlRip(source,event) kids = get(get(source, 'Parent'), 'Children'); tags = arrayfun(@(a)a.Tag, kids, 'uniformoutput',0); axTag = strcmp(tags, 'Raw_Axes'); -set(kids(axTag), 'xLim', [source.UserData(source.Value,1)-(50/60000),... - source.UserData(source.Value,2)+(50/60000)]); +set(kids(axTag), 'xLim', [source.UserData(source.Value,1)-(50),... + source.UserData(source.Value,2)+(50)]); end function NextRip(source,event) From afaf5372659b7b55aada5421fba85bcdfcf1ebe2 Mon Sep 17 00:00:00 2001 From: gaelias Date: Wed, 26 Feb 2020 16:18:32 -0800 Subject: [PATCH 063/133] Update CurateRipples_SM.m --- Analyses/CurateRipples_SM.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Analyses/CurateRipples_SM.m b/Analyses/CurateRipples_SM.m index 382cf37..1153a80 100644 --- a/Analyses/CurateRipples_SM.m +++ b/Analyses/CurateRipples_SM.m @@ -63,7 +63,7 @@ [spkX, spkY] = find(rips.SessionData.Spikes~=0); scatter(spkAxes, rips.TimeStamps(spkX),spkY, '*k'); % line(spkAxes, rips.TimeStamps(spkX),spkY, 'marker', '*', 'linestyle', 'none', 'color', 'k'); -xlabel(spkAxes, 'Time (m)'); +xlabel(spkAxes, 'Time (ms)'); set(spkAxes, 'Tag', 'Spk_Axes'); % Fiddle with Axes From 5b9d2d389cd74d3408a30b8e4a133e4cd317c3c2 Mon Sep 17 00:00:00 2001 From: gaelias Date: Wed, 26 Feb 2020 16:21:33 -0800 Subject: [PATCH 064/133] Update CurateRipples_SM.m --- Analyses/CurateRipples_SM.m | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Analyses/CurateRipples_SM.m b/Analyses/CurateRipples_SM.m index 1153a80..3165a80 100644 --- a/Analyses/CurateRipples_SM.m +++ b/Analyses/CurateRipples_SM.m @@ -127,8 +127,8 @@ function SelectSsnRip(source,event) kids = get(get(source, 'Parent'), 'Children'); tags = arrayfun(@(a)a.Tag, kids, 'uniformoutput',0); axTag = strcmp(tags, 'Raw_Axes'); -set(kids(axTag), 'xLim', [source.UserData(source.Value,1)-(50),... - source.UserData(source.Value,2)+(50)]); +set(kids(axTag), 'xLim', [source.UserData(source.Value,1)-(0.05),... + source.UserData(source.Value,2)+(0.05)]); end function SelectTrlRip(source,event) @@ -137,8 +137,8 @@ function SelectTrlRip(source,event) kids = get(get(source, 'Parent'), 'Children'); tags = arrayfun(@(a)a.Tag, kids, 'uniformoutput',0); axTag = strcmp(tags, 'Raw_Axes'); -set(kids(axTag), 'xLim', [source.UserData(source.Value,1)-(50),... - source.UserData(source.Value,2)+(50)]); +set(kids(axTag), 'xLim', [source.UserData(source.Value,1)-(0.05),... + source.UserData(source.Value,2)+(0.05)]); end function NextRip(source,event) @@ -153,8 +153,8 @@ function NextRip(source,event) lstTag = strcmp(tags, lstTarg); kids(lstTag).Value = kids(lstTag).Value+1; axTag = strcmp(tags, 'Raw_Axes'); -set(kids(axTag), 'xLim', [kids(lstTag).UserData(kids(lstTag).Value,1)-(50),... - kids(lstTag).UserData(kids(lstTag).Value,2)+(50)]); +set(kids(axTag), 'xLim', [kids(lstTag).UserData(kids(lstTag).Value,1)-(0.05),... + kids(lstTag).UserData(kids(lstTag).Value,2)+(0.05)]); end function PrevRip(source,event) @@ -169,8 +169,8 @@ function PrevRip(source,event) lstTag = strcmp(tags, lstTarg); kids(lstTag).Value = kids(lstTag).Value-1; axTag = strcmp(tags, 'Raw_Axes'); -set(kids(axTag), 'xLim', [kids(lstTag).UserData(kids(lstTag).Value,1)-(50),... - kids(lstTag).UserData(kids(lstTag).Value,2)+(50)]); +set(kids(axTag), 'xLim', [kids(lstTag).UserData(kids(lstTag).Value,1)-(0.05),... + kids(lstTag).UserData(kids(lstTag).Value,2)+(0.05)]); end function ZoomOut(source,event) @@ -178,8 +178,8 @@ function ZoomOut(source,event) tags = arrayfun(@(a)a.Tag, kids, 'uniformoutput',0); axTag = strcmp(tags, 'Raw_Axes'); curX = get(kids(axTag), 'xLim'); -set(kids(axTag), 'xLim', [curX(1)-(10),... - curX(2)+(10)]); +set(kids(axTag), 'xLim', [curX(1)-(0.01),... + curX(2)+(0.01)]); end function ZoomIn(source,event) @@ -187,6 +187,6 @@ function ZoomIn(source,event) tags = arrayfun(@(a)a.Tag, kids, 'uniformoutput',0); axTag = strcmp(tags, 'Raw_Axes'); curX = get(kids(axTag), 'xLim'); -set(kids(axTag), 'xLim', [curX(1)+(10),... - curX(2)-(10)]); +set(kids(axTag), 'xLim', [curX(1)+(0.01),... + curX(2)-(0.01)]); end \ No newline at end of file From 954fa6ca815592840439b1208882ed6fe8165061 Mon Sep 17 00:00:00 2001 From: gaelias Date: Wed, 26 Feb 2020 16:24:19 -0800 Subject: [PATCH 065/133] Create CurateRipples2_SM.m --- Analyses/CurateRipples2_SM.m | 197 +++++++++++++++++++++++++++++++++++ 1 file changed, 197 insertions(+) create mode 100644 Analyses/CurateRipples2_SM.m diff --git a/Analyses/CurateRipples2_SM.m b/Analyses/CurateRipples2_SM.m new file mode 100644 index 0000000..2de5a63 --- /dev/null +++ b/Analyses/CurateRipples2_SM.m @@ -0,0 +1,197 @@ +%% CurateRipples(rips, trialRips) +global listSel rawPlot bpfPlot spkRasts +listSel = 1; % Used to keep track of which list is being selected from for ripple viewing +%% +rips = RippleDetection_SM; +[trialRips] = ExtractTrialEventRips_SM(rips, [500 700]); +allTrialRips = sortrows(cell2mat([trialRips.Events(:,1); trialRips.Events(:,2); trialRips.Events(:,3)])); +%% Create Figure +ripCure = figure; +rawAxes = axes(ripCure, 'position', [0.1, 0.75, 0.7, 0.2]); +bpfAxes = axes(ripCure, 'position', [0.1, 0.55, 0.7, 0.2]); +spkAxes = axes(ripCure, 'position', [0.1, 0.2, 0.7, 0.3]); +linkaxes([spkAxes, bpfAxes, rawAxes], 'x'); + +% Overall Ripple List +ssnRipTitleAx = axes(ripCure, 'position', [0.825, 0.925, 0.11, 0.05]); +set(ssnRipTitleAx, 'xlim', [-0.5 0.5], 'ylim', [0 0.5]); +text(ssnRipTitleAx, 0,0, 'Session Rips', 'horizontalalignment', 'center', 'verticalalignment', 'bottom'); +axis(ssnRipTitleAx, 'off'); +ssnRipList = uicontrol(ripCure, 'Units', 'Normalized', 'Style', 'listbox', 'String', 1:size(rips.Ripples.Events,1),... + 'Position', [0.825, 0.55, 0.15, 0.375], 'Tag', 'ssnRip_Lst', 'Callback', @SelectSsnRip, 'userData', rips.TimeStamps(rips.Ripples.Events)); + +% Trial Ripple List +trlRipTitleAx = axes(ripCure, 'position', [0.825, 0.5, 0.11, 0.05]); +set(trlRipTitleAx, 'xlim', [-0.5 0.5], 'ylim', [0 0.5]); +text(trlRipTitleAx, 0,0, 'Trial Rips', 'horizontalalignment', 'center', 'verticalalignment', 'bottom'); +axis(trlRipTitleAx, 'off'); +trlRipList = uicontrol(ripCure, 'Units', 'Normalized', 'Style', 'listbox', 'String', 1:size(allTrialRips,1),... + 'Position', [0.825, 0.2, 0.15, 0.300], 'Tag', 'trlRip_Lst', 'Callback', @SelectTrlRip, 'userData', rips.TimeStamps(allTrialRips)); + +% Zoom In +zoomOutBtn = uicontrol(ripCure, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', 'Zm-',... + 'Position', [0.35, 0.06, 0.05, 0.075], 'Callback', @ZoomOut); +% Zoom Out +zoomInBtn = uicontrol(ripCure, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', 'Zm+',... + 'Position', [0.65, 0.06, 0.05, 0.075], 'Callback', @ZoomIn); + +% Previous Ripple +prevRipBtn = uicontrol(ripCure, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', '<< Previous Ripple',... + 'Position', [0.05, 0.05, 0.2, 0.1], 'Callback', @PrevRip); +% Next Ripple +nextRipBtn = uicontrol(ripCure, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', 'Next Ripple >>',... + 'Position', [0.75, 0.05, 0.2, 0.1], 'Callback', @NextRip); + +% Remove Ripple +removeRipBtn = uicontrol(ripCure, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', 'Remove Ripple',... + 'Position', [0.425, 0.075, 0.2, 0.05], 'Callback', @RmvRip); + +%% Plot Stuff +% Plot Raw LFP Traces +[xVals, yVals] = GetVals(rips.TimeStamps, rips.SessionData.RawLFP, rips.Ripples.Events(1,:)); +rawPlot = plot(rawAxes, xVals, yVals, 'color', 'k'); +for r = 1:length(rawPlot) + rawPlot(r).Color(4) = 0.2; +end +set(rawAxes, 'Tag', 'Raw_Axes'); +% Plot Ripple Band LFP Traces +bpfPlot = plot(bpfAxes, rips.TimeStamps, rips.SessionData.RipBPF, 'color', 'k'); +for b = 1:length(bpfPlot) + bpfPlot(b).Color(4) = 0.2; +end +set(bpfAxes, 'Tag', 'Bpf_Axes'); +% Plot Spiking Activity +[spkX, spkY] = find(rips.SessionData.Spikes~=0); +spkRasts = scatter(spkAxes, rips.TimeStamps(spkX),spkY, '*k'); +xlabel(spkAxes, 'Time (m)'); +set(spkAxes, 'Tag', 'Spk_Axes'); + +% Fiddle with Axes +set(rawAxes, 'color', 'none', 'ycolor', 'none', 'xcolor', 'none', 'xticklabel', [], 'box', 'off'); +set(bpfAxes,'color', 'none', 'ycolor', 'none', 'xticklabel', [], 'box', 'off'); +set(spkAxes, 'color', 'none', 'ycolor', 'none', 'box', 'off'); +axis([rawAxes, bpfAxes,spkAxes], 'tight'); +rawLim = repmat(get(rawAxes,'ylim'),[2,1]); +bpfLim = repmat(get(bpfAxes,'ylim'),[2,1]); +spkLim = repmat(get(spkAxes,'ylim'),[2,1]); +spkLim(:,1) = spkLim(:,1)-1; +spkLim(:,2) = spkLim(:,2)+1; +% Plot Trial Periods +for trl = 1:size(rips.TrialInfo.TrialPokes,1) + switch rips.TrialInfo.OdorVect(trl) + case 1 + patchColor = [44/255 168/255 224/255]; + case 2 + patchColor = [154/255 133/255 122/255]; + case 3 + patchColor = [9/255 161/255 74/255]; + case 4 + patchColor = [128/255 66/255 151/255]; + case 5 + patchColor = [241/255 103/255 36/255]; + end + patch(rawAxes, 'XData', [rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)),rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1))],... + 'YData', rawLim(:),... + 'FaceColor', patchColor, 'FaceAlpha', 0.5,... + 'EdgeColor', patchColor, 'EdgeAlpha', 0.5); + patch(bpfAxes, 'XData', [rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)),rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1))],... + 'YData', bpfLim(:),... + 'FaceColor', patchColor, 'FaceAlpha', 0.5,... + 'EdgeColor', patchColor, 'EdgeAlpha', 0.5); + patch(spkAxes, 'XData', [rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)),rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1))],... + 'YData', spkLim(:),... + 'FaceColor', patchColor, 'FaceAlpha', 0.5,... + 'EdgeColor', patchColor, 'EdgeAlpha', 0.5); +end + +for rip = 1:size(rips.Ripples.Events,1) + curRipX = [rips.TimeStamps(rips.Ripples.Events(rip,:)); flipud(rips.TimeStamps(rips.Ripples.Events(rip,:)))]'; + patch(rawAxes, 'XData', curRipX,... + 'YData', rawLim(:),... + 'FaceColor', 'y', 'FaceAlpha', 0.15,... + 'EdgeColor', 'y', 'EdgeAlpha', 0.5); + patch(bpfAxes, 'XData', curRipX,... + 'YData', bpfLim(:),... + 'FaceColor', 'y', 'FaceAlpha', 0.15,... + 'EdgeColor', 'y', 'EdgeAlpha', 0.5); + patch(spkAxes, 'XData', curRipX,... + 'YData', spkLim(:),... + 'FaceColor', 'y', 'FaceAlpha', 0.15,... + 'EdgeColor', 'y', 'EdgeAlpha', 0.5); +end + +%% Callbacks/Functions +function [xData, yData] = GetVals(ts, data, windows) +xData = ts(windows(1)-50:windows(2)+50); +yData = data(windows(1)-50:windows(2)+50,:); +end + +function SelectSsnRip(source,event) +global listSel +listSel = 1; +kids = get(get(source, 'Parent'), 'Children'); +tags = arrayfun(@(a)a.Tag, kids, 'uniformoutput',0); +axTag = strcmp(tags, 'Raw_Axes'); +set(kids(axTag), 'xLim', [source.UserData(source.Value,1)-(50/60000),... + source.UserData(source.Value,2)+(50/60000)]); +end + +function SelectTrlRip(source,event) +global listSel +listSel = 2; +kids = get(get(source, 'Parent'), 'Children'); +tags = arrayfun(@(a)a.Tag, kids, 'uniformoutput',0); +axTag = strcmp(tags, 'Raw_Axes'); +set(kids(axTag), 'xLim', [source.UserData(source.Value,1)-(50/60000),... + source.UserData(source.Value,2)+(50/60000)]); +end + +function NextRip(source,event) +global listSel +if listSel == 1 + lstTarg = 'ssnRip_Lst'; +else + lstTarg = 'trlRip_Lst'; +end +kids = get(get(source, 'Parent'), 'Children'); +tags = arrayfun(@(a)a.Tag, kids, 'uniformoutput',0); +lstTag = strcmp(tags, lstTarg); +kids(lstTag).Value = kids(lstTag).Value+1; +axTag = strcmp(tags, 'Raw_Axes'); +set(kids(axTag), 'xLim', [kids(lstTag).UserData(kids(lstTag).Value,1)-(50/60000),... + kids(lstTag).UserData(kids(lstTag).Value,2)+(50/60000)]); +end + +function PrevRip(source,event) +global listSel +if listSel == 1 + lstTarg = 'ssnRip_Lst'; +else + lstTarg = 'trlRip_Lst'; +end +kids = get(get(source, 'Parent'), 'Children'); +tags = arrayfun(@(a)a.Tag, kids, 'uniformoutput',0); +lstTag = strcmp(tags, lstTarg); +kids(lstTag).Value = kids(lstTag).Value-1; +axTag = strcmp(tags, 'Raw_Axes'); +set(kids(axTag), 'xLim', [kids(lstTag).UserData(kids(lstTag).Value,1)-(50/60000),... + kids(lstTag).UserData(kids(lstTag).Value,2)+(50/60000)]); +end + +function ZoomOut(source,event) +kids = get(get(source, 'Parent'), 'Children'); +tags = arrayfun(@(a)a.Tag, kids, 'uniformoutput',0); +axTag = strcmp(tags, 'Raw_Axes'); +curX = get(kids(axTag), 'xLim'); +set(kids(axTag), 'xLim', [curX(1)-(10/60000),... + curX(2)+(10/60000)]); +end + +function ZoomIn(source,event) +kids = get(get(source, 'Parent'), 'Children'); +tags = arrayfun(@(a)a.Tag, kids, 'uniformoutput',0); +axTag = strcmp(tags, 'Raw_Axes'); +curX = get(kids(axTag), 'xLim'); +set(kids(axTag), 'xLim', [curX(1)+(10/60000),... + curX(2)-(10/60000)]); +end \ No newline at end of file From 3c3c49f179e7ff5b03d9195b15411c370723b9a1 Mon Sep 17 00:00:00 2001 From: gabe Date: Wed, 26 Feb 2020 17:47:23 -0800 Subject: [PATCH 066/133] Update CurateRipples2_SM.m --- Analyses/CurateRipples2_SM.m | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Analyses/CurateRipples2_SM.m b/Analyses/CurateRipples2_SM.m index 2de5a63..c5884ff 100644 --- a/Analyses/CurateRipples2_SM.m +++ b/Analyses/CurateRipples2_SM.m @@ -7,9 +7,13 @@ allTrialRips = sortrows(cell2mat([trialRips.Events(:,1); trialRips.Events(:,2); trialRips.Events(:,3)])); %% Create Figure ripCure = figure; +set(ripCure, 'UserData', rips.TimeStamps); rawAxes = axes(ripCure, 'position', [0.1, 0.75, 0.7, 0.2]); +set(rawAxes, 'UserData', rips.SessionData.RawLFP); bpfAxes = axes(ripCure, 'position', [0.1, 0.55, 0.7, 0.2]); +set(bpfAxes, 'UserData', rips.SessionData.RipBPF); spkAxes = axes(ripCure, 'position', [0.1, 0.2, 0.7, 0.3]); +set(spkAxes, 'UserData', rips.SessionData.Spikes); linkaxes([spkAxes, bpfAxes, rawAxes], 'x'); % Overall Ripple List @@ -48,14 +52,19 @@ %% Plot Stuff % Plot Raw LFP Traces -[xVals, yVals] = GetVals(rips.TimeStamps, rips.SessionData.RawLFP, rips.Ripples.Events(1,:)); -rawPlot = plot(rawAxes, xVals, yVals, 'color', 'k'); +rawPlaceHolder = nan(size(rips.SessionData.RawLFP)); +rawPlaceHolder(1,1) = max(max(rips.SessionData.RawLFP)); +rawPlaceHolder(end,end) = min(min(rips.SessionData.RawLFP)); +rawPlot = plot(rawAxes, rips.TimeStamps, rawPlaceHolder, 'color', 'k'); for r = 1:length(rawPlot) rawPlot(r).Color(4) = 0.2; end set(rawAxes, 'Tag', 'Raw_Axes'); % Plot Ripple Band LFP Traces -bpfPlot = plot(bpfAxes, rips.TimeStamps, rips.SessionData.RipBPF, 'color', 'k'); +bpfPlaceHolder = nan(size(rips.SessionData.RipBPF)); +bpfPlaceHolder(1,1) = max(max(rips.SessionData.RipBPF)); +bpfPlaceHolder(end,end) = max(max(rips.SessionData.RipBPF)); +bpfPlot = plot(bpfAxes, rips.TimeStamps, bpfPlaceHolder, 'color', 'k'); for b = 1:length(bpfPlot) bpfPlot(b).Color(4) = 0.2; end From 72cc674d7ca422c53db9d80fb4556b9370f579ac Mon Sep 17 00:00:00 2001 From: gabe Date: Thu, 27 Feb 2020 12:21:49 -0800 Subject: [PATCH 067/133] Update CurateRipples2_SM.m --- Analyses/CurateRipples2_SM.m | 423 ++++++++++++++++++++++++++--------- 1 file changed, 312 insertions(+), 111 deletions(-) diff --git a/Analyses/CurateRipples2_SM.m b/Analyses/CurateRipples2_SM.m index c5884ff..c726310 100644 --- a/Analyses/CurateRipples2_SM.m +++ b/Analyses/CurateRipples2_SM.m @@ -1,143 +1,139 @@ %% CurateRipples(rips, trialRips) -global listSel rawPlot bpfPlot spkRasts -listSel = 1; % Used to keep track of which list is being selected from for ripple viewing +global plotData +plotData.listSel = 1; % Used to keep track of which list is being selected from for ripple viewing %% rips = RippleDetection_SM; [trialRips] = ExtractTrialEventRips_SM(rips, [500 700]); allTrialRips = sortrows(cell2mat([trialRips.Events(:,1); trialRips.Events(:,2); trialRips.Events(:,3)])); %% Create Figure -ripCure = figure; -set(ripCure, 'UserData', rips.TimeStamps); -rawAxes = axes(ripCure, 'position', [0.1, 0.75, 0.7, 0.2]); -set(rawAxes, 'UserData', rips.SessionData.RawLFP); -bpfAxes = axes(ripCure, 'position', [0.1, 0.55, 0.7, 0.2]); -set(bpfAxes, 'UserData', rips.SessionData.RipBPF); -spkAxes = axes(ripCure, 'position', [0.1, 0.2, 0.7, 0.3]); -set(spkAxes, 'UserData', rips.SessionData.Spikes); -linkaxes([spkAxes, bpfAxes, rawAxes], 'x'); +plotData.ripCure = figure; +set(plotData.ripCure, 'UserData', rips.TimeStamps); +plotData.rawAxes = axes(plotData.ripCure, 'position', [0.1, 0.75, 0.7, 0.2]); +set(plotData.rawAxes, 'UserData', rips.SessionData.RawLFP); +plotData.bpfAxes = axes(plotData.ripCure, 'position', [0.1, 0.55, 0.7, 0.2]); +set(plotData.bpfAxes, 'UserData', rips.SessionData.RipBPF); +plotData.spkAxes = axes(plotData.ripCure, 'position', [0.1, 0.2, 0.7, 0.3]); +set(plotData.spkAxes, 'UserData', rips.SessionData.Spikes); % Overall Ripple List -ssnRipTitleAx = axes(ripCure, 'position', [0.825, 0.925, 0.11, 0.05]); +ssnRipTitleAx = axes(plotData.ripCure, 'position', [0.825, 0.925, 0.11, 0.05]); set(ssnRipTitleAx, 'xlim', [-0.5 0.5], 'ylim', [0 0.5]); text(ssnRipTitleAx, 0,0, 'Session Rips', 'horizontalalignment', 'center', 'verticalalignment', 'bottom'); axis(ssnRipTitleAx, 'off'); -ssnRipList = uicontrol(ripCure, 'Units', 'Normalized', 'Style', 'listbox', 'String', 1:size(rips.Ripples.Events,1),... - 'Position', [0.825, 0.55, 0.15, 0.375], 'Tag', 'ssnRip_Lst', 'Callback', @SelectSsnRip, 'userData', rips.TimeStamps(rips.Ripples.Events)); +plotData.ssnRipList = uicontrol(plotData.ripCure, 'Units', 'Normalized', 'Style', 'listbox', 'String', 1:size(rips.Ripples.Events,1),... + 'Position', [0.825, 0.55, 0.15, 0.375], 'Callback', @SelectSsnRip, 'userData', rips.Ripples.Events); % Trial Ripple List -trlRipTitleAx = axes(ripCure, 'position', [0.825, 0.5, 0.11, 0.05]); +trlRipTitleAx = axes(plotData.ripCure, 'position', [0.825, 0.5, 0.11, 0.05]); set(trlRipTitleAx, 'xlim', [-0.5 0.5], 'ylim', [0 0.5]); text(trlRipTitleAx, 0,0, 'Trial Rips', 'horizontalalignment', 'center', 'verticalalignment', 'bottom'); axis(trlRipTitleAx, 'off'); -trlRipList = uicontrol(ripCure, 'Units', 'Normalized', 'Style', 'listbox', 'String', 1:size(allTrialRips,1),... - 'Position', [0.825, 0.2, 0.15, 0.300], 'Tag', 'trlRip_Lst', 'Callback', @SelectTrlRip, 'userData', rips.TimeStamps(allTrialRips)); +plotData.trlRipList = uicontrol(plotData.ripCure, 'Units', 'Normalized', 'Style', 'listbox', 'String', 1:size(allTrialRips,1),... + 'Position', [0.825, 0.2, 0.15, 0.300], 'Callback', @SelectTrlRip, 'userData', allTrialRips); % Zoom In -zoomOutBtn = uicontrol(ripCure, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', 'Zm-',... +zoomOutBtn = uicontrol(plotData.ripCure, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', 'Zm-',... 'Position', [0.35, 0.06, 0.05, 0.075], 'Callback', @ZoomOut); % Zoom Out -zoomInBtn = uicontrol(ripCure, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', 'Zm+',... +zoomInBtn = uicontrol(plotData.ripCure, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', 'Zm+',... 'Position', [0.65, 0.06, 0.05, 0.075], 'Callback', @ZoomIn); % Previous Ripple -prevRipBtn = uicontrol(ripCure, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', '<< Previous Ripple',... +prevRipBtn = uicontrol(plotData.ripCure, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', '<< Previous Ripple',... 'Position', [0.05, 0.05, 0.2, 0.1], 'Callback', @PrevRip); % Next Ripple -nextRipBtn = uicontrol(ripCure, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', 'Next Ripple >>',... +nextRipBtn = uicontrol(plotData.ripCure, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', 'Next Ripple >>',... 'Position', [0.75, 0.05, 0.2, 0.1], 'Callback', @NextRip); % Remove Ripple -removeRipBtn = uicontrol(ripCure, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', 'Remove Ripple',... +removeRipBtn = uicontrol(plotData.ripCure, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', 'Remove Ripple',... 'Position', [0.425, 0.075, 0.2, 0.05], 'Callback', @RmvRip); -%% Plot Stuff -% Plot Raw LFP Traces -rawPlaceHolder = nan(size(rips.SessionData.RawLFP)); -rawPlaceHolder(1,1) = max(max(rips.SessionData.RawLFP)); -rawPlaceHolder(end,end) = min(min(rips.SessionData.RawLFP)); -rawPlot = plot(rawAxes, rips.TimeStamps, rawPlaceHolder, 'color', 'k'); -for r = 1:length(rawPlot) - rawPlot(r).Color(4) = 0.2; -end -set(rawAxes, 'Tag', 'Raw_Axes'); -% Plot Ripple Band LFP Traces -bpfPlaceHolder = nan(size(rips.SessionData.RipBPF)); -bpfPlaceHolder(1,1) = max(max(rips.SessionData.RipBPF)); -bpfPlaceHolder(end,end) = max(max(rips.SessionData.RipBPF)); -bpfPlot = plot(bpfAxes, rips.TimeStamps, bpfPlaceHolder, 'color', 'k'); -for b = 1:length(bpfPlot) - bpfPlot(b).Color(4) = 0.2; -end -set(bpfAxes, 'Tag', 'Bpf_Axes'); -% Plot Spiking Activity -[spkX, spkY] = find(rips.SessionData.Spikes~=0); -spkRasts = scatter(spkAxes, rips.TimeStamps(spkX),spkY, '*k'); -xlabel(spkAxes, 'Time (m)'); -set(spkAxes, 'Tag', 'Spk_Axes'); - -% Fiddle with Axes -set(rawAxes, 'color', 'none', 'ycolor', 'none', 'xcolor', 'none', 'xticklabel', [], 'box', 'off'); -set(bpfAxes,'color', 'none', 'ycolor', 'none', 'xticklabel', [], 'box', 'off'); -set(spkAxes, 'color', 'none', 'ycolor', 'none', 'box', 'off'); -axis([rawAxes, bpfAxes,spkAxes], 'tight'); -rawLim = repmat(get(rawAxes,'ylim'),[2,1]); -bpfLim = repmat(get(bpfAxes,'ylim'),[2,1]); -spkLim = repmat(get(spkAxes,'ylim'),[2,1]); -spkLim(:,1) = spkLim(:,1)-1; -spkLim(:,2) = spkLim(:,2)+1; -% Plot Trial Periods -for trl = 1:size(rips.TrialInfo.TrialPokes,1) - switch rips.TrialInfo.OdorVect(trl) - case 1 - patchColor = [44/255 168/255 224/255]; - case 2 - patchColor = [154/255 133/255 122/255]; - case 3 - patchColor = [9/255 161/255 74/255]; - case 4 - patchColor = [128/255 66/255 151/255]; - case 5 - patchColor = [241/255 103/255 36/255]; - end - patch(rawAxes, 'XData', [rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)),rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1))],... - 'YData', rawLim(:),... - 'FaceColor', patchColor, 'FaceAlpha', 0.5,... - 'EdgeColor', patchColor, 'EdgeAlpha', 0.5); - patch(bpfAxes, 'XData', [rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)),rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1))],... - 'YData', bpfLim(:),... - 'FaceColor', patchColor, 'FaceAlpha', 0.5,... - 'EdgeColor', patchColor, 'EdgeAlpha', 0.5); - patch(spkAxes, 'XData', [rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)),rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1))],... - 'YData', spkLim(:),... - 'FaceColor', patchColor, 'FaceAlpha', 0.5,... - 'EdgeColor', patchColor, 'EdgeAlpha', 0.5); -end - -for rip = 1:size(rips.Ripples.Events,1) - curRipX = [rips.TimeStamps(rips.Ripples.Events(rip,:)); flipud(rips.TimeStamps(rips.Ripples.Events(rip,:)))]'; - patch(rawAxes, 'XData', curRipX,... - 'YData', rawLim(:),... - 'FaceColor', 'y', 'FaceAlpha', 0.15,... - 'EdgeColor', 'y', 'EdgeAlpha', 0.5); - patch(bpfAxes, 'XData', curRipX,... - 'YData', bpfLim(:),... - 'FaceColor', 'y', 'FaceAlpha', 0.15,... - 'EdgeColor', 'y', 'EdgeAlpha', 0.5); - patch(spkAxes, 'XData', curRipX,... - 'YData', spkLim(:),... - 'FaceColor', 'y', 'FaceAlpha', 0.15,... - 'EdgeColor', 'y', 'EdgeAlpha', 0.5); -end +%% Initialize Things +SetPlots; + +%% Plot Trial Periods +% for trl = 1:size(rips.TrialInfo.TrialPokes,1) +% switch rips.TrialInfo.OdorVect(trl) +% case 1 +% patchColor = [44/255 168/255 224/255]; +% case 2 +% patchColor = [154/255 133/255 122/255]; +% case 3 +% patchColor = [9/255 161/255 74/255]; +% case 4 +% patchColor = [128/255 66/255 151/255]; +% case 5 +% patchColor = [241/255 103/255 36/255]; +% end +% patch(rawAxes, 'XData', [rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)),rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1))],... +% 'YData', plotData.FigLims.Raw(:),... +% 'FaceColor', patchColor, 'FaceAlpha', 0.5,... +% 'EdgeColor', patchColor, 'EdgeAlpha', 0.5); +% patch(bpfAxes, 'XData', [rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)),rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1))],... +% 'YData', plotData.FigLims.BPF(:),... +% 'FaceColor', patchColor, 'FaceAlpha', 0.5,... +% 'EdgeColor', patchColor, 'EdgeAlpha', 0.5); +% patch(spkAxes, 'XData', [rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)),rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1))],... +% 'YData', plotData.FigLims.Spk(:),... +% 'FaceColor', patchColor, 'FaceAlpha', 0.5,... +% 'EdgeColor', patchColor, 'EdgeAlpha', 0.5); +% end + %% Callbacks/Functions -function [xData, yData] = GetVals(ts, data, windows) -xData = ts(windows(1)-50:windows(2)+50); -yData = data(windows(1)-50:windows(2)+50,:); +function SetPlots +global plotData +if plotData.listSel == 1 + curNdx = plotData.ssnRipList.UserData(plotData.ssnRipList.Value,:); +else + curNdx = plotData.trlRipList.UserData(plotData.trlRipList.Value,:); +end +rawData = plotData.rawAxes.UserData(curNdx(1)-50:curNdx(2)+50,:); +bpfData = plotData.bpfAxes.UserData(curNdx(1)-50:curNdx(2)+50,:); +spkData = plotData.spkAxes.UserData(curNdx(1)-50:curNdx(2)+50,:); +[spkX, spkY] = find(spkData~=0); +curTS = plotData.ripCure.UserData(curNdx(1)-50:curNdx(2)+50,:); + +if ~isfield('rawPlot', plotData) + plotData.rawPlot = plot(plotData.rawAxes, curTS, rawData, 'color', 'k'); + for r = 1:length(plotData.rawPlot) + plotData.rawPlot(r).Color(4) = 0.2; + end + plotData.bpfPlot = plot(plotData.bpfAxes, curTS, bpfData, 'color', 'k'); + for b = 1:length(plotData.bpfPlot) + plotData.bpfPlot(b).Color(4) = 0.2; + end + plotData.spkRasts = scatter(plotData.spkAxes, curTS(spkX),spkY, '*k'); + xlabel(plotData.spkAxes, 'Time (s)'); + + linkaxes([plotData.spkAxes, plotData.bpfAxes, plotData.rawAxes], 'x'); + set(plotData.rawAxes, 'color', 'none', 'ycolor', 'none', 'xcolor', 'none', 'xticklabel', [], 'box', 'off'); + set(plotData.bpfAxes,'color', 'none', 'ycolor', 'none', 'xticklabel', [], 'box', 'off'); + set(plotData.spkAxes, 'color', 'none', 'ycolor', 'none', 'box', 'off'); + axis([plotData.rawAxes, plotData.bpfAxes, plotData.spkAxes], 'tight'); + + plotData.FigLims.Raw = repmat(get(plotData.rawAxes,'ylim'),[2,1]); + plotData.FigLims.BPF = repmat(get(plotData.bpfAxes,'ylim'),[2,1]); + spkLim = repmat(get(plotData.spkAxes,'ylim'),[2,1]); + plotData.FigLims.Spk = [spkLim(:,1)-1, spkLim(:,2)+1]; +else + for r = 1:length(plotData.rawPlot) + plotData.rawPlot(r).XData = curTS; + plotData.rawPlot(r).YData = rawData(r,:); + end + for b = 1:length(plotData.bpfPlot) + plotData.bpfPlot(b).XData = curTS; + plotData.bpfPlot(b).YData = bpfData(r,:); + end + set(plotData.spkRasts, 'XData', curTS(spkX), 'YData', spkY); +end +refreshdata(plotData.ripCure); end function SelectSsnRip(source,event) -global listSel -listSel = 1; +global plotData +plotData.listSel = 1; kids = get(get(source, 'Parent'), 'Children'); tags = arrayfun(@(a)a.Tag, kids, 'uniformoutput',0); axTag = strcmp(tags, 'Raw_Axes'); @@ -146,8 +142,8 @@ function SelectSsnRip(source,event) end function SelectTrlRip(source,event) -global listSel -listSel = 2; +global plotData +plotData.listSel = 2; kids = get(get(source, 'Parent'), 'Children'); tags = arrayfun(@(a)a.Tag, kids, 'uniformoutput',0); axTag = strcmp(tags, 'Raw_Axes'); @@ -156,8 +152,8 @@ function SelectTrlRip(source,event) end function NextRip(source,event) -global listSel -if listSel == 1 +global plotData +if plotData.listSel == 1 lstTarg = 'ssnRip_Lst'; else lstTarg = 'trlRip_Lst'; @@ -172,8 +168,8 @@ function NextRip(source,event) end function PrevRip(source,event) -global listSel -if listSel == 1 +global plotData +if plotData.listSel == 1 lstTarg = 'ssnRip_Lst'; else lstTarg = 'trlRip_Lst'; @@ -203,4 +199,209 @@ function ZoomIn(source,event) curX = get(kids(axTag), 'xLim'); set(kids(axTag), 'xLim', [curX(1)+(10/60000),... curX(2)-(10/60000)]); +end + +function [rips] = RippleDetection_SM(envProc, powThresh, durThresh, durThreshMrg, syncThresh, syncWin, smoothWin) +%% RippleDetection_SM +% Inputs: +% - envProc: Enveloping procedure +% - 'RMS' : Use the root mean squared approach to enveloping +% - 'HILB' : Use hilbert transform envelope +% - powThresh: Thresholds used for defining the ripple. +% - First value is the threshold for defining a ripple epoc. +% - Second value is the threshold needed to for an epoc to be +% considered a ripple. +% - durThresh: Threshold used to select ripples of only a particular +% duration. **CURRENTLY NOT IMPLEMENTED** +% - durThreshMrg: Threshold of the inter-ripple-interval used to +% merge together potential ripples that happen close in time +% - syncThresh: Synchrony threshold used to select ripples of a +% particular coherence. **CURRENTLY NOT IMPLEMENTED** +% - syncWin: Window size used to compute sliding window coherence +% across the session. **CURRENTLY NOT IMPLEMENTED** +% - smoothWin: Window size used for gaussian smoothing. + + +%#ok<*IDISVAR,*NODEF,*USENS,*NASGU,*COLND> +%% +if nargin == 0 + envProc = 'RMS'; % Enable for RMS determination of envelope + % envProc = 'HILB'; % Enable for abs(Hilbert) determinination of envelope + powThresh = [0 4]; + durThresh = 15; % Duration Threshold + durThreshMrg = 15; + syncThresh = 0; + syncWin = 10; + smoothWin = 21; +end +%% +%% Define Analysis Features +w = gausswin(smoothWin); +w = w/sum(w); +%% Load Data +origDir = cd; +[fileDir] = uigetdir(origDir); +if fileDir==0 + disp('Analysis Cancelled') + return +else + cd(fileDir) +end +dirContents = dir(fileDir); +fileNames = {dirContents.name}; +tetFileLog = cellfun(@(a)~isempty(a), regexp(fileNames, '_T([0-9]*)_SM.mat')); +tetFiles = fileNames(tetFileLog)'; +behavFileLog = cellfun(@(a)~isempty(a), regexp(fileNames, '_BehaviorMatrix')); +ensembleFileLog = cellfun(@(a)~isempty(a), regexp(fileNames, '_EnsembleMatrix')); +load(fileNames{behavFileLog}); +load(fileNames{ensembleFileLog}); +behavMatrixTrialStruct = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [0 0], 'PokeIn'); + +%% Extract Raw Values & Compute RMS Power +ripBPF = nan(size(behavMatrix,1),size(tetFiles,1)); +ripVolts = nan(size(behavMatrix,1),size(tetFiles,1)); +ripRMS = nan(size(behavMatrix,1),size(tetFiles,1)); +ripHilb = nan(size(behavMatrix,1),size(tetFiles,1)); +ripTetIDs = cell(size(tetFiles)); +for fl = 1:length(tetFiles) + load(tetFiles{fl}) + samp = mode(diff(statMatrix(:,1))); + wIndx = round((1/200)*(1/samp)); + fprintf('%s......', tetFiles{fl}); + ripCol = find(cellfun(@(a)~isempty(a), regexp(statMatrixColIDs, 'LFP_Ripple$'))); + ripVolts(:,fl) = statMatrix(:,2); + if strcmp(envProc, 'RMS') + ripRMS(:,fl) = conv(sqrt(conv(statMatrix(:,ripCol).^2, ones(wIndx,1)/wIndx, 'same')), w, 'same'); + elseif strcmp(envProc, 'HILB') + ripRMS(:,fl) = conv(abs(hilbert(statMatrix(:,ripCol))),w,'same'); + end + ripBPF(:,fl) = statMatrix(:,ripCol); + ripHilb(:,fl) = statMatrix(:,ripCol+1); + ripTetIDs{fl} = statMatrixColIDs{ripCol}; + fprintf('done\n'); +end + +%% Calculate Thresholds +% Aggregate Power +aggPower = mean(ripRMS,2); % Mean envelope +% aggPower = zscore(mean(ripRMS,2)); % Z-Score Mean envelope +% aggPower = mean(zscore(ripRMS),2); % Mean Z-Score envelope + +% Threshold Based on Mean +/- STD Aggregate Power +rmsThresh1 = (mean(aggPower) + (powThresh(1)*std(aggPower))); +rmsThresh2 = (mean(aggPower) + (powThresh(2)*std(aggPower))); + +%% Identify Ripples +% Define putative ripple periods +abvThresh1 = aggPower>rmsThresh1; +epocWindows = [find(diff(abvThresh1)==1), find(diff(abvThresh1)==-1)]; + +% Apply secondary (peak power) threshold +abvThresh2 = aggPower>rmsThresh2; +dualThreshLog = false(size(epocWindows,1),1); +for epoc = 1:size(epocWindows,1) + if sum(abvThresh2(epocWindows(epoc,1):epocWindows(epoc,2))) >=1 + dualThreshLog(epoc) = true; + end +end +epocWindows(~dualThreshLog,:) = []; % Comment out if not using dual thresholds + +% Merge short latency ripples +interEpocInterval = epocWindows(2:end,1) - epocWindows(1:end-1,2); +slrLog = interEpocInterval(trialPokeTimes(trl,1)-trialWin(1)) & rips.Ripples.Events(:,1)trialPokeTimes(trl,1) & rips.Ripples.Events(:,1)trialPokeTimes(trl,2) & (rips.Ripples.Events(:,1) Date: Thu, 27 Feb 2020 13:13:25 -0800 Subject: [PATCH 068/133] Update CurateRipples2_SM.m --- Analyses/CurateRipples2_SM.m | 163 ++++++++++++++++++++++------------- 1 file changed, 105 insertions(+), 58 deletions(-) diff --git a/Analyses/CurateRipples2_SM.m b/Analyses/CurateRipples2_SM.m index c726310..6a9f0f6 100644 --- a/Analyses/CurateRipples2_SM.m +++ b/Analyses/CurateRipples2_SM.m @@ -1,10 +1,11 @@ -%% CurateRipples(rips, trialRips) +%% CurateRipples global plotData plotData.listSel = 1; % Used to keep track of which list is being selected from for ripple viewing +plotData.Window = 50; %% -rips = RippleDetection_SM; -[trialRips] = ExtractTrialEventRips_SM(rips, [500 700]); -allTrialRips = sortrows(cell2mat([trialRips.Events(:,1); trialRips.Events(:,2); trialRips.Events(:,3)])); +% rips = RippleDetection_SM('HILB', [0 3], 0, 15, 0, 10, 21); +% [trialRips] = ExtractTrialEventRips_SM(rips, [500 700]); +% allTrialRips = sortrows(cell2mat([trialRips.Events(:,1); trialRips.Events(:,2); trialRips.Events(:,3)])); %% Create Figure plotData.ripCure = figure; set(plotData.ripCure, 'UserData', rips.TimeStamps); @@ -21,7 +22,9 @@ text(ssnRipTitleAx, 0,0, 'Session Rips', 'horizontalalignment', 'center', 'verticalalignment', 'bottom'); axis(ssnRipTitleAx, 'off'); plotData.ssnRipList = uicontrol(plotData.ripCure, 'Units', 'Normalized', 'Style', 'listbox', 'String', 1:size(rips.Ripples.Events,1),... - 'Position', [0.825, 0.55, 0.15, 0.375], 'Callback', @SelectSsnRip, 'userData', rips.Ripples.Events); + 'Position', [0.825, 0.62, 0.15, 0.3], 'Callback', @SelectSsnRip, 'userData', rips.Ripples.Events); +plotData.ssnRipExport = uicontrol(plotData.ripCure, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', 'Export Session Rips',... + 'Position', [0.825, 0.55, 0.15, 0.05], 'Callback', @ExportSsnRips); % Trial Ripple List trlRipTitleAx = axes(plotData.ripCure, 'position', [0.825, 0.5, 0.11, 0.05]); @@ -29,7 +32,9 @@ text(trlRipTitleAx, 0,0, 'Trial Rips', 'horizontalalignment', 'center', 'verticalalignment', 'bottom'); axis(trlRipTitleAx, 'off'); plotData.trlRipList = uicontrol(plotData.ripCure, 'Units', 'Normalized', 'Style', 'listbox', 'String', 1:size(allTrialRips,1),... - 'Position', [0.825, 0.2, 0.15, 0.300], 'Callback', @SelectTrlRip, 'userData', allTrialRips); + 'Position', [0.825, 0.25, 0.15, 0.25], 'Callback', @SelectTrlRip, 'userData', allTrialRips); +plotData.trlRipExport = uicontrol(plotData.ripCure, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', 'Export Trial Rips',... + 'Position', [0.825, 0.195, 0.15, 0.05], 'Callback', @ExportTrlRips); % Zoom In zoomOutBtn = uicontrol(plotData.ripCure, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', 'Zm-',... @@ -89,22 +94,26 @@ else curNdx = plotData.trlRipList.UserData(plotData.trlRipList.Value,:); end -rawData = plotData.rawAxes.UserData(curNdx(1)-50:curNdx(2)+50,:); -bpfData = plotData.bpfAxes.UserData(curNdx(1)-50:curNdx(2)+50,:); -spkData = plotData.spkAxes.UserData(curNdx(1)-50:curNdx(2)+50,:); -[spkX, spkY] = find(spkData~=0); -curTS = plotData.ripCure.UserData(curNdx(1)-50:curNdx(2)+50,:); +rawData = plotData.rawAxes.UserData; +bpfData = plotData.bpfAxes.UserData; +spkData = plotData.spkAxes.UserData; +tmpSpkData = spkData(curNdx(1)-plotData.Window:curNdx(2)+plotData.Window,:); +[spkX, spkY] = find(tmpSpkData~=0); +curTS = plotData.ripCure.UserData(curNdx(1)-plotData.Window:curNdx(2)+plotData.Window,:); if ~isfield('rawPlot', plotData) - plotData.rawPlot = plot(plotData.rawAxes, curTS, rawData, 'color', 'k'); + plotData.rawPlot = plot(plotData.rawAxes, curTS, rawData(curNdx(1)-plotData.Window:curNdx(2)+plotData.Window,:), 'color', 'k'); + plotData.rawAxes.UserData = rawData; for r = 1:length(plotData.rawPlot) plotData.rawPlot(r).Color(4) = 0.2; end - plotData.bpfPlot = plot(plotData.bpfAxes, curTS, bpfData, 'color', 'k'); + plotData.bpfPlot = plot(plotData.bpfAxes, curTS, bpfData(curNdx(1)-plotData.Window:curNdx(2)+plotData.Window,:), 'color', 'k'); + plotData.bpfAxes.UserData = bpfData; for b = 1:length(plotData.bpfPlot) plotData.bpfPlot(b).Color(4) = 0.2; end plotData.spkRasts = scatter(plotData.spkAxes, curTS(spkX),spkY, '*k'); + plotData.spkAxes.UserData = spkData; xlabel(plotData.spkAxes, 'Time (s)'); linkaxes([plotData.spkAxes, plotData.bpfAxes, plotData.rawAxes], 'x'); @@ -117,90 +126,128 @@ plotData.FigLims.BPF = repmat(get(plotData.bpfAxes,'ylim'),[2,1]); spkLim = repmat(get(plotData.spkAxes,'ylim'),[2,1]); plotData.FigLims.Spk = [spkLim(:,1)-1, spkLim(:,2)+1]; + + curRipX = [plotData.ripCure.UserData(curNdx); flipud(plotData.ripCure.UserData(curNdx))]'; + plotData.RipPatch.Raw = patch(plotData.rawAxes, 'XData', curRipX,... + 'YData', plotData.FigLims.Raw(:),... + 'FaceColor', 'y', 'FaceAlpha', 0.15,... + 'EdgeColor', 'y', 'EdgeAlpha', 0.5); + plotData.RipPatch.BPF = patch(plotData.bpfAxes, 'XData', curRipX,... + 'YData', plotData.FigLims.BPF(:),... + 'FaceColor', 'y', 'FaceAlpha', 0.15,... + 'EdgeColor', 'y', 'EdgeAlpha', 0.5); + plotData.RipPatch.Spk = patch(plotData.spkAxes, 'XData', curRipX,... + 'YData', plotData.FigLims.Spk(:),... + 'FaceColor', 'y', 'FaceAlpha', 0.15,... + 'EdgeColor', 'y', 'EdgeAlpha', 0.5); else for r = 1:length(plotData.rawPlot) - plotData.rawPlot(r).XData = curTS; - plotData.rawPlot(r).YData = rawData(r,:); + set(plotData.rawPlot(r), 'XData', curTS, 'YData', rawData(r,:)); end for b = 1:length(plotData.bpfPlot) - plotData.bpfPlot(b).XData = curTS; - plotData.bpfPlot(b).YData = bpfData(r,:); + set(plotData.bpfPlot(b), 'XData', curTS, 'YData', bpfData(r,:)); end set(plotData.spkRasts, 'XData', curTS(spkX), 'YData', spkY); end -refreshdata(plotData.ripCure); +title(plotData.spkAxes, sprintf('Duration = %i(ms)', diff(curNdx))); +% refreshdata(plotData.ripCure); end function SelectSsnRip(source,event) global plotData plotData.listSel = 1; -kids = get(get(source, 'Parent'), 'Children'); -tags = arrayfun(@(a)a.Tag, kids, 'uniformoutput',0); -axTag = strcmp(tags, 'Raw_Axes'); -set(kids(axTag), 'xLim', [source.UserData(source.Value,1)-(50/60000),... - source.UserData(source.Value,2)+(50/60000)]); +SetPlots end function SelectTrlRip(source,event) global plotData plotData.listSel = 2; -kids = get(get(source, 'Parent'), 'Children'); -tags = arrayfun(@(a)a.Tag, kids, 'uniformoutput',0); -axTag = strcmp(tags, 'Raw_Axes'); -set(kids(axTag), 'xLim', [source.UserData(source.Value,1)-(50/60000),... - source.UserData(source.Value,2)+(50/60000)]); +SetPlots end function NextRip(source,event) global plotData if plotData.listSel == 1 - lstTarg = 'ssnRip_Lst'; + plotData.ssnRipList.Value = plotData.ssnRipList.Value + 1; else - lstTarg = 'trlRip_Lst'; + plotData.trlRipList.Value = plotData.trlRipList.Value + 1; end -kids = get(get(source, 'Parent'), 'Children'); -tags = arrayfun(@(a)a.Tag, kids, 'uniformoutput',0); -lstTag = strcmp(tags, lstTarg); -kids(lstTag).Value = kids(lstTag).Value+1; -axTag = strcmp(tags, 'Raw_Axes'); -set(kids(axTag), 'xLim', [kids(lstTag).UserData(kids(lstTag).Value,1)-(50/60000),... - kids(lstTag).UserData(kids(lstTag).Value,2)+(50/60000)]); +SetPlots end function PrevRip(source,event) global plotData if plotData.listSel == 1 - lstTarg = 'ssnRip_Lst'; + plotData.ssnRipList.Value = plotData.ssnRipList.Value - 1; else - lstTarg = 'trlRip_Lst'; + plotData.trlRipList.Value = plotData.trlRipList.Value - 1; end -kids = get(get(source, 'Parent'), 'Children'); -tags = arrayfun(@(a)a.Tag, kids, 'uniformoutput',0); -lstTag = strcmp(tags, lstTarg); -kids(lstTag).Value = kids(lstTag).Value-1; -axTag = strcmp(tags, 'Raw_Axes'); -set(kids(axTag), 'xLim', [kids(lstTag).UserData(kids(lstTag).Value,1)-(50/60000),... - kids(lstTag).UserData(kids(lstTag).Value,2)+(50/60000)]); +SetPlots end function ZoomOut(source,event) -kids = get(get(source, 'Parent'), 'Children'); -tags = arrayfun(@(a)a.Tag, kids, 'uniformoutput',0); -axTag = strcmp(tags, 'Raw_Axes'); -curX = get(kids(axTag), 'xLim'); -set(kids(axTag), 'xLim', [curX(1)-(10/60000),... - curX(2)+(10/60000)]); +global plotData +plotData.Window = plotData.Window+100; +SetPlots end function ZoomIn(source,event) -kids = get(get(source, 'Parent'), 'Children'); -tags = arrayfun(@(a)a.Tag, kids, 'uniformoutput',0); -axTag = strcmp(tags, 'Raw_Axes'); -curX = get(kids(axTag), 'xLim'); -set(kids(axTag), 'xLim', [curX(1)+(10/60000),... - curX(2)-(10/60000)]); +global plotData +plotData.Window = plotData.Window-100; +if plotData.Window < 50 + plotData.Window = 50; +end +SetPlots +end + +function RmvRip(source,event) +global plotData +if plotData.listSel == 1 + curNdx = plotData.ssnRipList.UserData(plotData.ssnRipList.Value,:); +else + curNdx = plotData.trlRipList.UserData(plotData.trlRipList.Value,:); +end +ssnList = plotData.ssnRipList.UserData; +ssnList(ssnList(:,1)==curNdx(1),:) = []; +trlList = plotData.ssnRipList.UserData; +trlList(trlList(:,1)==curNdx(1),:) = []; + +plotData.ssnRipList.UserData = ssnList; +plotData.ssnRipList.String = 1:size(ssnList,1); +plotData.trlRipList.UserData = trlList; +plotData.trlRipList.String = 1:size(trlList,1); + +if plotData.listSel == 1 + if plotData.ssnRipList.Value == 1 + plotData.ssnRipList.Value = 1; + elseif plotData.ssnRipList.Value == size(ssnList,1)+1 + plotData.ssnRipList.Value = size(ssnList,1); + end +else + if plotData.trlRipList.Value == 1 + plotData.trlRipList.Value = 1; + elseif plotData.trlRipList.Value == size(ssnList,1)+1 + plotData.trlRipList.Value = size(ssnList,1); + end +end +SetPlots +end + +function ExportSsnRips(source, event) +global plotData +exportData = plotData.ssnRipList.UserData; +save('SessionRips.mat', 'exportData'); +msgbox('Session Ripple Indices Saved'); +end + +function ExportTrlRips(source, event) +global plotData +exportData = plotData.trlRipList.UserData; +save('TrialRips.mat', 'exportData'); +msgbox('Trial Ripple Indices Saved'); end +%% Data Organization Functions function [rips] = RippleDetection_SM(envProc, powThresh, durThresh, durThreshMrg, syncThresh, syncWin, smoothWin) %% RippleDetection_SM % Inputs: From bf5b47cb3874ab55c37e63f01e84fd44408a33cb Mon Sep 17 00:00:00 2001 From: gaelias Date: Thu, 27 Feb 2020 15:35:43 -0800 Subject: [PATCH 069/133] Update CurateRipples2_SM.m --- Analyses/CurateRipples2_SM.m | 93 ++++++++++++++++++++++++++++-------- 1 file changed, 74 insertions(+), 19 deletions(-) diff --git a/Analyses/CurateRipples2_SM.m b/Analyses/CurateRipples2_SM.m index 6a9f0f6..218e11b 100644 --- a/Analyses/CurateRipples2_SM.m +++ b/Analyses/CurateRipples2_SM.m @@ -2,13 +2,34 @@ global plotData plotData.listSel = 1; % Used to keep track of which list is being selected from for ripple viewing plotData.Window = 50; +%% Parameters +% envProc = 'RMS'; +envProc = 'HILB'; +powThresh = [0 4]; +durThresh = 15; +durThreshMrg = 15; +syncThresh = 0; +syncWin = 10; +smoothWin = 21; + %% -% rips = RippleDetection_SM('HILB', [0 3], 0, 15, 0, 10, 21); -% [trialRips] = ExtractTrialEventRips_SM(rips, [500 700]); -% allTrialRips = sortrows(cell2mat([trialRips.Events(:,1); trialRips.Events(:,2); trialRips.Events(:,3)])); +rips = RippleDetection_SM(envProc, powThresh, durThresh, durThreshMrg, syncThresh, syncWin, smoothWin); +[trialRips] = ExtractTrialEventRips_SM(rips, [500 700]); +allTrialRips = sortrows(cell2mat([trialRips.Events(:,1); trialRips.Events(:,2); trialRips.Events(:,3)])); + +%% Plot Descriptives +PlotNearTrialRipStats(trialRips) +annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string',... + sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); +annotation('textbox', 'position', [0.01 0.95 0.9 0.05], 'string',... + sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge \\bfThreshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg),... + 'linestyle', 'none'); + + %% Create Figure +plotData.PowThresh = rips.FileInfo.PowerThreshold; plotData.ripCure = figure; -set(plotData.ripCure, 'UserData', rips.TimeStamps); +set(plotData.ripCure, 'UserData', [rips.TimeStamps, mean(rips.SessionData.RipEnv,2)]); plotData.rawAxes = axes(plotData.ripCure, 'position', [0.1, 0.75, 0.7, 0.2]); set(plotData.rawAxes, 'UserData', rips.SessionData.RawLFP); plotData.bpfAxes = axes(plotData.ripCure, 'position', [0.1, 0.55, 0.7, 0.2]); @@ -54,6 +75,12 @@ removeRipBtn = uicontrol(plotData.ripCure, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', 'Remove Ripple',... 'Position', [0.425, 0.075, 0.2, 0.05], 'Callback', @RmvRip); + +annotation('textbox', 'position', [0.01 0.005 0.9 0.05], 'string',... + sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); +annotation('textbox', 'position', [0.01 0.95 0.9 0.05], 'string',... + sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge \\bfThreshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg),... + 'linestyle', 'none'); %% Initialize Things SetPlots; @@ -97,9 +124,10 @@ rawData = plotData.rawAxes.UserData; bpfData = plotData.bpfAxes.UserData; spkData = plotData.spkAxes.UserData; +envData = plotData.ripCure.UserData(:,2); tmpSpkData = spkData(curNdx(1)-plotData.Window:curNdx(2)+plotData.Window,:); [spkX, spkY] = find(tmpSpkData~=0); -curTS = plotData.ripCure.UserData(curNdx(1)-plotData.Window:curNdx(2)+plotData.Window,:); +curTS = plotData.ripCure.UserData(curNdx(1)-plotData.Window:curNdx(2)+plotData.Window,1); if ~isfield('rawPlot', plotData) plotData.rawPlot = plot(plotData.rawAxes, curTS, rawData(curNdx(1)-plotData.Window:curNdx(2)+plotData.Window,:), 'color', 'k'); @@ -108,6 +136,14 @@ plotData.rawPlot(r).Color(4) = 0.2; end plotData.bpfPlot = plot(plotData.bpfAxes, curTS, bpfData(curNdx(1)-plotData.Window:curNdx(2)+plotData.Window,:), 'color', 'k'); + hold(plotData.bpfAxes, 'on'); + plotData.envPlot = plot(plotData.bpfAxes, curTS, envData(curNdx(1)-plotData.Window:curNdx(2)+plotData.Window,:), 'color', 'r', 'linewidth', 2); + plotData.envTH1 = plot(plotData.bpfAxes, curTS,... + ones(1,length(curNdx(1)-plotData.Window:curNdx(2)+plotData.Window))*(mean(plotData.ripCure.UserData(:,2)) + (plotData.PowThresh (1)*std(plotData.ripCure.UserData(:,2)))),... + 'color', 'k', 'linestyle','--', 'linewidth', 2); + plotData.envTH2 = plot(plotData.bpfAxes, curTS,... + ones(1,length(curNdx(1)-plotData.Window:curNdx(2)+plotData.Window))*(mean(plotData.ripCure.UserData(:,2)) + (plotData.PowThresh (2)*std(plotData.ripCure.UserData(:,2)))),... + 'color', 'k', 'linestyle','-', 'linewidth', 2); plotData.bpfAxes.UserData = bpfData; for b = 1:length(plotData.bpfPlot) plotData.bpfPlot(b).Color(4) = 0.2; @@ -127,7 +163,7 @@ spkLim = repmat(get(plotData.spkAxes,'ylim'),[2,1]); plotData.FigLims.Spk = [spkLim(:,1)-1, spkLim(:,2)+1]; - curRipX = [plotData.ripCure.UserData(curNdx); flipud(plotData.ripCure.UserData(curNdx))]'; + curRipX = [plotData.ripCure.UserData(curNdx,1); flipud(plotData.ripCure.UserData(curNdx,1))]'; plotData.RipPatch.Raw = patch(plotData.rawAxes, 'XData', curRipX,... 'YData', plotData.FigLims.Raw(:),... 'FaceColor', 'y', 'FaceAlpha', 0.15,... @@ -148,6 +184,13 @@ set(plotData.bpfPlot(b), 'XData', curTS, 'YData', bpfData(r,:)); end set(plotData.spkRasts, 'XData', curTS(spkX), 'YData', spkY); + set(plotData.envPlot, 'XData', curTS, 'YData', envData(curNdx(1)-plotData.Window:curNdx(2)+plotData.Window,:)); + set(plotData.envTH1, 'XData', curTS, 'YData', ones(1,length(curNdx(1)-plotData.Window:curNdx(2)+plotData.Window))*(mean(plotData.ripCure.UserData(:,2)) + (plotData.PowThresh (1)*std(plotData.ripCure.UserData(:,2))))); + set(plotData.envTH2, 'XData', curTS, 'YData', ones(1,length(curNdx(1)-plotData.Window:curNdx(2)+plotData.Window))*(mean(plotData.ripCure.UserData(:,2)) + (plotData.PowThresh (2)*std(plotData.ripCure.UserData(:,2))))); + curRipX = [plotData.ripCure.UserData(curNdx,1); flipud(plotData.ripCure.UserData(curNdx,1))]'; + set(plotData.RipPatch.Raw, 'XData', curRipX); + set(plotData.RipPatch.BPF, 'XData', curRipX); + set(plotData.RipPatch.Spk, 'XData', curRipX); end title(plotData.spkAxes, sprintf('Duration = %i(ms)', diff(curNdx))); % refreshdata(plotData.ripCure); @@ -389,26 +432,32 @@ function ExportTrlRips(source, event) sortedSpkCountsAndIndices = sortrows([sum(spkMtx); 1:size(spkMtx,2)]'); spkMtxSorted = spkMtx(:,sortedSpkCountsAndIndices(:,2)); +%% Evaluate Ensemble Activity +epocNsmblAct = nan(size(epocWindows,1),1); +for epoc = 1:size(epocWindows,1) + tempSpkMtx = ensembleMatrix(epocWindows(epoc,1):epocWindows(epoc,2),2:end); + epocNsmblAct(epoc) = mean(sum(tempSpkMtx)>=1); +end %% Organize Data Output rips = struct(... 'TimeStamps', statMatrix(:,1),... 'Ripples', struct('Events', epocWindows, 'Duration', epocDur,... - 'Synchrony', epocSync),... + 'Synchrony', epocSync, 'EnsembleActivity', epocNsmblAct),... 'SessionData', struct('RawLFP', ripVolts, 'RipBPF', ripBPF,... - 'RipEnv', ripRMS, 'RipPhase', ripHilb, 'TetIDs', {ripTetIDs},... - 'Spikes', spkMtxSorted),... + 'RipEnv', ripRMS, 'RipPhase', ripHilb, 'TetIDs', {ripTetIDs},... + 'Spikes', spkMtxSorted),... 'TrialInfo', struct('Perf', [behavMatrixTrialStruct.Performance]==1,... - 'TransDist', [behavMatrixTrialStruct.TranspositionDistance],... - 'OdorVect', [behavMatrixTrialStruct.Odor],... - 'PositionVect', [behavMatrixTrialStruct.Position],... - 'TrialPokes', [[behavMatrixTrialStruct.PokeInIndex]', [behavMatrixTrialStruct.PokeOutIndex]'],... - 'TrialRewards', [behavMatrixTrialStruct.RewardIndex]),... + 'TransDist', [behavMatrixTrialStruct.TranspositionDistance],... + 'OdorVect', [behavMatrixTrialStruct.Odor],... + 'PositionVect', [behavMatrixTrialStruct.Position],... + 'TrialPokes', [[behavMatrixTrialStruct.PokeInIndex]', [behavMatrixTrialStruct.PokeOutIndex]'],... + 'TrialRewards', [behavMatrixTrialStruct.RewardIndex]),... 'FileInfo', struct('Directory', cd, 'EnvelopeProcedure', envProc,... - 'PowerThreshold', powThresh, 'DurationThreshold', durThresh,... - 'DurationMergeThreshold', durThreshMrg,... - 'SynchronyThreshold', syncThresh, 'SynchronyWindow', syncWin,... - 'GaussianDuration', smoothWin)); + 'PowerThreshold', powThresh, 'DurationThreshold', durThresh,... + 'DurationMergeThreshold', durThreshMrg,... + 'SynchronyThreshold', syncThresh, 'SynchronyWindow', syncWin,... + 'GaussianDuration', smoothWin)); end function [trialRipStruct] = ExtractTrialEventRips_SM(rips, trialWin) @@ -430,25 +479,31 @@ function ExportTrlRips(source, event) trialRipLat = cell(size(trialPokeTimes,1),3); trialRipsDur = cell(size(trialPokeTimes,1),3); trialRipsSync = cell(size(trialPokeTimes,1),3); +trialRipsNsmblAct = cell(size(trialPokeTimes,1),3); for trl = 1:size(trialPokeTimes,1) preTrlLog = rips.Ripples.Events(:,1)>(trialPokeTimes(trl,1)-trialWin(1)) & rips.Ripples.Events(:,1)trialPokeTimes(trl,1) & rips.Ripples.Events(:,1)trialPokeTimes(trl,2) & (rips.Ripples.Events(:,1) Date: Thu, 27 Feb 2020 15:35:50 -0800 Subject: [PATCH 070/133] Create PlotNearTrialRipStats.m --- Analyses/PlotNearTrialRipStats.m | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Analyses/PlotNearTrialRipStats.m diff --git a/Analyses/PlotNearTrialRipStats.m b/Analyses/PlotNearTrialRipStats.m new file mode 100644 index 0000000..428d81a --- /dev/null +++ b/Analyses/PlotNearTrialRipStats.m @@ -0,0 +1,28 @@ +function PlotNearTrialRipStats(tRips) +trialRips = tRips.Events; +trialRipsDur = tRips.Duration; +trialRipsSync = tRips.Synchrony; +trialRipsNsmbl = tRips.EnsembleAct; + +figure; +prcntTrlsWrips = mean(cellfun(@(a)~isempty(a),trialRips)); +subplot(4,1,1); +bar(prcntTrlsWrips); +set(gca, 'xticklabels', [], 'box', 'off'); +ylabel('% Trials w/Ripple'); +subplot(4,1,2); +BarPlotErrorbars([mean(cell2mat(trialRipsDur(:,1))),mean(cell2mat(trialRipsDur(:,2))),mean(cell2mat(trialRipsDur(:,3)))],... + [std(cell2mat(trialRipsDur(:,1))),std(cell2mat(trialRipsDur(:,2))),std(cell2mat(trialRipsDur(:,3)))],[],[]); +set(gca, 'xticklabels', [], 'box', 'off'); +ylabel('Duration (ms)'); +subplot(4,1,3); +BarPlotErrorbars([mean(cell2mat(trialRipsSync(:,1))),mean(cell2mat(trialRipsSync(:,2))),mean(cell2mat(trialRipsSync(:,3)))],... + [std(cell2mat(trialRipsSync(:,1))),std(cell2mat(trialRipsSync(:,2))),std(cell2mat(trialRipsSync(:,3)))],[],[]); +set(gca, 'xticklabels', [{'Pre-Trial'}, {'Trial'}, {'Post-Trial'}], 'box', 'off'); +ylabel('Synchrony'); +subplot(4,1,4); +BarPlotErrorbars([mean(cell2mat(trialRipsNsmbl(:,1))),mean(cell2mat(trialRipsNsmbl(:,2))),mean(cell2mat(trialRipsNsmbl(:,3)))],... + [std(cell2mat(trialRipsNsmbl(:,1))),std(cell2mat(trialRipsNsmbl(:,2))),std(cell2mat(trialRipsNsmbl(:,3)))],[],[]); +set(gca, 'xticklabels', [{'Pre-Trial'}, {'Trial'}, {'Post-Trial'}], 'box', 'off'); +ylabel('% Active Units'); +end \ No newline at end of file From f539981060f864e29a5ed1857ca6486c1a6db7c2 Mon Sep 17 00:00:00 2001 From: gaelias Date: Thu, 27 Feb 2020 15:35:56 -0800 Subject: [PATCH 071/133] Create PlotRipFeatsByTrlType.m --- Analyses/PlotRipFeatsByTrlType.m | 88 ++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 Analyses/PlotRipFeatsByTrlType.m diff --git a/Analyses/PlotRipFeatsByTrlType.m b/Analyses/PlotRipFeatsByTrlType.m new file mode 100644 index 0000000..9e00a7f --- /dev/null +++ b/Analyses/PlotRipFeatsByTrlType.m @@ -0,0 +1,88 @@ +function PlotRipFeatsByTrlType(rips, trialRips) + +%% Extract Logicals +perfLog = rips.TrialInfo.Perf; +isLog = rips.TrialInfo.TransDist==0; + +%% Plot Data +% Plot InSeq Correct +iscLog = perfLog & isLog; +figure; +iscP = subplot(4,4,1); + RipLikelyPlot(trialRips.Events, iscLog); + ylabel(iscP, '% Trials w/Ripple'); + title(iscP, 'InSeq Correct Trials'); +iscD = subplot(4,4,5); + RipPlotVar(trialRips.Duration, iscLog); + ylabel(iscD, 'Duration'); +iscS = subplot(4,4,9); + RipPlotVar(trialRips.Synchrony, iscLog); + ylabel(iscS, 'Synchrony'); +iscE = subplot(4,4,13); + RipPlotVar(trialRips.EnsembleAct, iscLog); + ylabel(iscE, '% Units Active'); + set(gca, 'xticklabels', [{'Pre-Trial'}, {'Trial'}, {'Post-Trial'}], 'box', 'off'); + +% Plot InSeq Incorrect +isiLog = ~perfLog & isLog; +isiP = subplot(4,4,2); + RipLikelyPlot(trialRips.Events, isiLog); + ylabel(isiP, '% Trials w/Ripple'); + title(isiP, 'InSeq Incorrect Trials'); +isiD = subplot(4,4,6); + RipPlotVar(trialRips.Duration, isiLog); + ylabel(isiD, 'Duration'); +isiS = subplot(4,4,10); + RipPlotVar(trialRips.Synchrony, isiLog); + ylabel(isiS, 'Synchrony'); +isiE = subplot(4,4,14); + RipPlotVar(trialRips.EnsembleAct, isiLog); + ylabel(isiE, '% Units Active'); + set(gca, 'xticklabels', [{'Pre-Trial'}, {'Trial'}, {'Post-Trial'}], 'box', 'off'); + +% Plot OutSeq Incorrect +osiLog = ~perfLog & ~isLog; +osiP = subplot(4,4,3); + RipLikelyPlot(trialRips.Events, osiLog); + title(osiP, 'OutSeq Incorrect Trials'); +osiD = subplot(4,4,7); + RipPlotVar(trialRips.Duration, osiLog); +osiS = subplot(4,4,11); + RipPlotVar(trialRips.Synchrony, osiLog); +osiE = subplot(4,4,15); + RipPlotVar(trialRips.EnsembleAct, osiLog); + set(gca, 'xticklabels', [{'Pre-Trial'}, {'Trial'}, {'Post-Trial'}], 'box', 'off'); + +% Plot OutSeq Correct +oscLog = perfLog & ~isLog; +oscP = subplot(4,4,4); + RipLikelyPlot(trialRips.Events, oscLog); + title(oscP, 'OutSeq Correct Trials'); +oscD = subplot(4,4,8); + RipPlotVar(trialRips.Duration, oscLog); +oscS = subplot(4,4,12); + RipPlotVar(trialRips.Synchrony, oscLog); +oscE = subplot(4,4,16); + RipPlotVar(trialRips.EnsembleAct, oscLog); + set(gca, 'xticklabels', [{'Pre-Trial'}, {'Trial'}, {'Post-Trial'}], 'box', 'off'); + + +linkaxes([iscP isiP oscP osiP], 'xy'); +linkaxes([iscD isiD oscD osiD], 'xy'); +linkaxes([iscS isiS oscS osiS], 'xy'); +linkaxes([iscE isiE oscE osiE], 'xy'); +end + + +%% Plotting Functions +function RipLikelyPlot(trialRips, log) + prcntTrlsWrips = mean(cellfun(@(a)~isempty(a),trialRips(log,:))); + bar(prcntTrlsWrips); + set(gca, 'xticklabels', [], 'box', 'off'); +end + +function RipPlotVar(trialRipsParam, log) + BarPlotErrorbars([mean(cell2mat(trialRipsParam(log,1))),mean(cell2mat(trialRipsParam(log,2))),mean(cell2mat(trialRipsParam(log,3)))],... + [std(cell2mat(trialRipsParam(log,1))),std(cell2mat(trialRipsParam(log,2))),std(cell2mat(trialRipsParam(log,3)))],[],[]); + set(gca, 'xticklabels', [], 'box', 'off'); +end \ No newline at end of file From b4ff88dc7e9dc66c5058ab0327fa2aeb989fd5aa Mon Sep 17 00:00:00 2001 From: gaelias Date: Thu, 27 Feb 2020 15:36:10 -0800 Subject: [PATCH 072/133] Update RippleDetection_SM.m --- Analyses/RippleDetection_SM.m | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Analyses/RippleDetection_SM.m b/Analyses/RippleDetection_SM.m index 2bbd68f..56ec465 100644 --- a/Analyses/RippleDetection_SM.m +++ b/Analyses/RippleDetection_SM.m @@ -139,12 +139,18 @@ sortedSpkCountsAndIndices = sortrows([sum(spkMtx); 1:size(spkMtx,2)]'); spkMtxSorted = spkMtx(:,sortedSpkCountsAndIndices(:,2)); +%% Evaluate Ensemble Activity +epocNsmblAct = nan(size(epocWindows,1),1); +for epoc = 1:size(epocWindows,1) + tempSpkMtx = ensembleMatrix(epocWindows(epoc,1):epocWindows(epoc,2),2:end); + epocNsmblAct(epoc) = mean(sum(tempSpkMtx)>=1); +end %% Organize Data Output rips = struct(... 'TimeStamps', statMatrix(:,1),... 'Ripples', struct('Events', epocWindows, 'Duration', epocDur,... - 'Synchrony', epocSync),... + 'Synchrony', epocSync, 'EnsembleActivity', epocNsmblAct),... 'SessionData', struct('RawLFP', ripVolts, 'RipBPF', ripBPF,... 'RipEnv', ripRMS, 'RipPhase', ripHilb, 'TetIDs', {ripTetIDs},... 'Spikes', spkMtxSorted),... From 1000ff98f0ba00a9b8e380d1243b7f1fde564e9e Mon Sep 17 00:00:00 2001 From: gabe Date: Thu, 27 Feb 2020 15:45:14 -0800 Subject: [PATCH 073/133] Create PlotRipFeatsByOdor.m --- Analyses/PlotRipFeatsByOdor.m | 113 ++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 Analyses/PlotRipFeatsByOdor.m diff --git a/Analyses/PlotRipFeatsByOdor.m b/Analyses/PlotRipFeatsByOdor.m new file mode 100644 index 0000000..98fd41e --- /dev/null +++ b/Analyses/PlotRipFeatsByOdor.m @@ -0,0 +1,113 @@ +function PlotRipFeatsByOdor(rips, trialRips) + +%% Extract Logicals +perfLog = rips.TrialInfo.Perf; +isLog = rips.TrialInfo.TransDist==0; + +%% Plot Data +figure; +% Plot Odor A +aLog = perfLog & isLog & rips.TrialInfo.OdorVect==1; +aP = subplot(4,5,1); + RipLikelyPlot(trialRips.Events, aLog); + ylabel(aP, '% Trials w/Ripple'); + title(aP, 'Odor A'); +aD = subplot(4,5,6); + RipPlotVar(trialRips.Duration, aLog); + ylabel(aD, 'Duration'); +aS = subplot(4,5,11); + RipPlotVar(trialRips.Synchrony, aLog); + ylabel(aS, 'Synchrony'); +aE = subplot(4,5,16); + RipPlotVar(trialRips.EnsembleAct, aLog); + ylabel(aE, '% Units Active'); + set(aE, 'xticklabels', [{'Pre-Trial'}, {'Trial'}, {'Post-Trial'}], 'box', 'off'); + +% Plot Odor B +bLog = perfLog & isLog & rips.TrialInfo.OdorVect==2; +bP = subplot(4,5,2); + RipLikelyPlot(trialRips.Events, bLog); + ylabel(bP, '% Trials w/Ripple'); + title(bP, 'Odor B'); +bD = subplot(4,5,7); + RipPlotVar(trialRips.Duration, bLog); + ylabel(bD, 'Duration'); +bS = subplot(4,5,12); + RipPlotVar(trialRips.Synchrony, bLog); + ylabel(bS, 'Synchrony'); +bE = subplot(4,5,17); + RipPlotVar(trialRips.EnsembleAct, bLog); + ylabel(bE, '% Units Active'); + set(bE, 'xticklabels', [{'Pre-Trial'}, {'Trial'}, {'Post-Trial'}], 'box', 'off'); + +% Plot Odor C +cLog = perfLog & isLog & rips.TrialInfo.OdorVect==3; +cP = subplot(4,5,3); + RipLikelyPlot(trialRips.Events, cLog); + ylabel(cP, '% Trials w/Ripple'); + title(cP, 'Odor C'); +cD = subplot(4,5,8); + RipPlotVar(trialRips.Duration, cLog); + ylabel(cD, 'Duration'); +cS = subplot(4,5,13); + RipPlotVar(trialRips.Synchrony, cLog); + ylabel(cS, 'Synchrony'); +cE = subplot(4,5,18); + RipPlotVar(trialRips.EnsembleAct, cLog); + ylabel(cE, '% Units Active'); + set(cE, 'xticklabels', [{'Pre-Trial'}, {'Trial'}, {'Post-Trial'}], 'box', 'off'); + +% Plot Odor D +dLog = perfLog & isLog & rips.TrialInfo.OdorVect==4; +dP = subplot(4,5,4); + RipLikelyPlot(trialRips.Events, dLog); + ylabel(dP, '% Trials w/Ripple'); + title(dP, 'Odor D'); +dD = subplot(4,5,9); + RipPlotVar(trialRips.Duration, dLog); + ylabel(dD, 'Duration'); +dS = subplot(4,5,14); + RipPlotVar(trialRips.Synchrony, dLog); + ylabel(dS, 'Synchrony'); +dE = subplot(4,5,19); + RipPlotVar(trialRips.EnsembleAct, dLog); + ylabel(dE, '% Units Active'); + set(dE, 'xticklabels', [{'Pre-Trial'}, {'Trial'}, {'Post-Trial'}], 'box', 'off'); + +% Plot Odor E +eLog = perfLog & isLog & rips.TrialInfo.OdorVect==5; +eP = subplot(4,5,5); + RipLikelyPlot(trialRips.Events, eLog); + ylabel(eP, '% Trials w/Ripple'); + title(eP, 'Odor E'); +eD = subplot(4,5,10); + RipPlotVar(trialRips.Duration, eLog); + ylabel(eD, 'Duration'); +eS = subplot(4,5,15); + RipPlotVar(trialRips.Synchrony, eLog); + ylabel(eS, 'Synchrony'); +eE = subplot(4,5,20); + RipPlotVar(trialRips.EnsembleAct, eLog); + ylabel(eE, '% Units Active'); + set(eE, 'xticklabels', [{'Pre-Trial'}, {'Trial'}, {'Post-Trial'}], 'box', 'off'); + +linkaxes([aP bP cP dP eP], 'xy'); +linkaxes([aD bD cD dD eD], 'xy'); +linkaxes([aS bS cS dS eS], 'xy'); +linkaxes([aE bE cE dE eE], 'xy'); +end + + + +%% Plotting Functions +function RipLikelyPlot(trialRips, log) + prcntTrlsWrips = mean(cellfun(@(a)~isempty(a),trialRips(log,:))); + bar(prcntTrlsWrips); + set(gca, 'xticklabels', [], 'box', 'off'); +end + +function RipPlotVar(trialRipsParam, log) + BarPlotErrorbars([mean(cell2mat(trialRipsParam(log,1))),mean(cell2mat(trialRipsParam(log,2))),mean(cell2mat(trialRipsParam(log,3)))],... + [std(cell2mat(trialRipsParam(log,1))),std(cell2mat(trialRipsParam(log,2))),std(cell2mat(trialRipsParam(log,3)))],[],[]); + set(gca, 'xticklabels', [], 'box', 'off'); +end From 1265b413acde9a6cee0afa6d058a2c23cdc577b1 Mon Sep 17 00:00:00 2001 From: gabe Date: Thu, 27 Feb 2020 15:45:18 -0800 Subject: [PATCH 074/133] Update CurateRipples2_SM.m --- Analyses/CurateRipples2_SM.m | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Analyses/CurateRipples2_SM.m b/Analyses/CurateRipples2_SM.m index 218e11b..cf35240 100644 --- a/Analyses/CurateRipples2_SM.m +++ b/Analyses/CurateRipples2_SM.m @@ -3,7 +3,7 @@ plotData.listSel = 1; % Used to keep track of which list is being selected from for ripple viewing plotData.Window = 50; %% Parameters -% envProc = 'RMS'; +% envProc = 'RMS'; envProc = 'HILB'; powThresh = [0 4]; durThresh = 15; @@ -25,6 +25,19 @@ sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge \\bfThreshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg),... 'linestyle', 'none'); +PlotRipFeatsByTrlType(rips, trialRips) +annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string',... + sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); +annotation('textbox', 'position', [0.01 0.95 0.9 0.05], 'string',... + sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge \\bfThreshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg),... + 'linestyle', 'none'); + +PlotRipFeatsByOdor(rips, trialRips) +annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string',... + sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); +annotation('textbox', 'position', [0.01 0.95 0.9 0.05], 'string',... + sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge \\bfThreshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg),... + 'linestyle', 'none'); %% Create Figure plotData.PowThresh = rips.FileInfo.PowerThreshold; From 8076ee2ce5347cda813127adeee4646b52c11e13 Mon Sep 17 00:00:00 2001 From: gabe Date: Thu, 27 Feb 2020 15:56:50 -0800 Subject: [PATCH 075/133] Create PlotRipFeatCorr.m --- Analyses/PlotRipFeatCorr.m | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Analyses/PlotRipFeatCorr.m diff --git a/Analyses/PlotRipFeatCorr.m b/Analyses/PlotRipFeatCorr.m new file mode 100644 index 0000000..3000cb6 --- /dev/null +++ b/Analyses/PlotRipFeatCorr.m @@ -0,0 +1,28 @@ +function PlotRipFeatCorr(param1, param2, param1Title, param2Title) +%% Plot Duration vs Synchrony Correlation +figure; +p1Hist = subplot(4,4,[1,5,9]); +histogram(param1, 30, 'Orientation', 'Horizontal'); +set(gca, 'XDir', 'reverse'); +line(get(gca, 'xlim'), repmat(mean(param1), [1,2]), 'color', 'r', 'linewidth', 2); +line(get(gca, 'xlim'), repmat(median(param1), [1,2]),'color', 'r', 'linestyle', '--', 'linewidth', 2); +if mean(param1)>median(param1) + set(gca, 'ytick', [median(param1), mean(param1)], 'yticklabel', [{'Median'}, {'Mean'}]); +else + set(gca, 'ytick', [mean(param1), median(param1)], 'yticklabel', [{'Mean'}, {'Median'}]); +end +xlabel('Count'); +ylabel([{['\bf' param1Title '\rm']}; sprintf('Mean: %0.2f; Median: %0.2f', mean(param1), median(param1))]); + +corre = subplot(4,4,[2:4,6:8,10:12]); +corrScatPlot(param2, param1, [], [], []); +title(sprintf('Session Wide Ripple %s vs %s', param1Title, param2Title)); +set(gca, 'xticklabel', [], 'yticklabel', []); + +p2Hist = subplot(4,4,14:16); +histogram(param2, 30); +title([{['\bf' param2Title '\rm']}; sprintf('Mean: %0.2f; Median: %0.2f', mean(param2), median(param2))]); +ylabel('Count'); + +linkaxes([p1Hist, corre], 'y'); +linkaxes([p2Hist, corre], 'x'); \ No newline at end of file From d5e402b3f5288bfdc3eb96550ef8cf824e2d611d Mon Sep 17 00:00:00 2001 From: gabe Date: Thu, 27 Feb 2020 15:56:53 -0800 Subject: [PATCH 076/133] Update CurateRipples2_SM.m --- Analyses/CurateRipples2_SM.m | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Analyses/CurateRipples2_SM.m b/Analyses/CurateRipples2_SM.m index cf35240..fc5af7e 100644 --- a/Analyses/CurateRipples2_SM.m +++ b/Analyses/CurateRipples2_SM.m @@ -25,6 +25,27 @@ sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge \\bfThreshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg),... 'linestyle', 'none'); +PlotRipFeatCorr(rips.Ripples.Duration, rips.Ripples.Synchrony, 'Duration', 'Synchrony') +annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string',... + sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); +annotation('textbox', 'position', [0.01 0.95 0.9 0.05], 'string',... + sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge \\bfThreshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg),... + 'linestyle', 'none'); + +PlotRipFeatCorr(rips.Ripples.Duration, rips.Ripples.EnsembleActivity, 'Duration', 'Ensemble Activity') +annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string',... + sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); +annotation('textbox', 'position', [0.01 0.95 0.9 0.05], 'string',... + sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge \\bfThreshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg),... + 'linestyle', 'none'); + +PlotRipFeatCorr(rips.Ripples.Synchrony, rips.Ripples.EnsembleActivity, 'Synchrony', 'Ensemble Activity') +annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string',... + sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); +annotation('textbox', 'position', [0.01 0.95 0.9 0.05], 'string',... + sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge \\bfThreshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg),... + 'linestyle', 'none'); + PlotRipFeatsByTrlType(rips, trialRips) annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string',... sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); From 2eef8ad8c164bc43d73bae1244326b4ac759fbed Mon Sep 17 00:00:00 2001 From: gabe Date: Thu, 27 Feb 2020 17:09:40 -0800 Subject: [PATCH 077/133] Create PlotRipCountsByEvent.m --- Analyses/PlotRipCountsByEvent.m | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Analyses/PlotRipCountsByEvent.m diff --git a/Analyses/PlotRipCountsByEvent.m b/Analyses/PlotRipCountsByEvent.m new file mode 100644 index 0000000..e75da94 --- /dev/null +++ b/Analyses/PlotRipCountsByEvent.m @@ -0,0 +1,36 @@ +function PlotRipCountsByEvent(rips) +%% +pokeInNdxs = rips.TrialInfo.TrialPokes(:,1); +pokeOutNdxs = rips.TrialInfo.TrialPokes(:,2); +rwdNdxs = rips.TrialInfo.TrialRewards; + +if length(pokeInNdxs)~=length(rwdNdxs) + rwdNdxs(end:end+(length(pokeInNdxs)-length(rwdNdxs)))=nan; +end +%% +piDiffs = cell(length(pokeInNdxs),1); +poDiffs = piDiffs; +rdDiffs = piDiffs; +for trl = 1:size(rips.TrialInfo.TrialPokes,1) + curPIdiff = rips.Ripples.Events(:,1) - pokeInNdxs(trl); + piDiffs{trl} = curPIdiff(curPIdiff>=-500 & curPIdiff<=500); + + curPOdiff = rips.Ripples.Events(:,1) - pokeOutNdxs(trl); + poDiffs{trl} = curPOdiff(curPOdiff>=-500 & curPOdiff<=500); + + curRDdiff = rips.Ripples.Events(:,1) - rwdNdxs(trl); + rdDiffs{trl} = curRDdiff(curRDdiff>=-500 & curRDdiff<=500); +end + +%% +figure; +subplot(3,1,1) +histogram(cell2mat(piDiffs), -500:10:500); +title('Ripples Relative to Poke In'); +subplot(3,1,2) +histogram(cell2mat(poDiffs), -500:10:500); +title('Ripples Relative to Poke Out'); +subplot(3,1,3) +histogram(cell2mat(rdDiffs), -500:10:500); +title('Ripples Relative to Reward Delivery'); + From b6332978e0d2a9c82a76fd8d9bc030d68890768c Mon Sep 17 00:00:00 2001 From: gabe Date: Thu, 27 Feb 2020 17:09:44 -0800 Subject: [PATCH 078/133] Update CurateRipples2_SM.m --- Analyses/CurateRipples2_SM.m | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Analyses/CurateRipples2_SM.m b/Analyses/CurateRipples2_SM.m index fc5af7e..0f34a45 100644 --- a/Analyses/CurateRipples2_SM.m +++ b/Analyses/CurateRipples2_SM.m @@ -60,6 +60,13 @@ sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge \\bfThreshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg),... 'linestyle', 'none'); +PlotRipCountsByEvent(rips) +annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string',... + sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); +annotation('textbox', 'position', [0.01 0.95 0.9 0.05], 'string',... + sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge \\bfThreshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg),... + 'linestyle', 'none'); + %% Create Figure plotData.PowThresh = rips.FileInfo.PowerThreshold; plotData.ripCure = figure; From caf8d5dece8ef96eb773c4eae3ab215b345b9c87 Mon Sep 17 00:00:00 2001 From: gabe Date: Thu, 27 Feb 2020 17:52:19 -0800 Subject: [PATCH 079/133] Update CurateRipples2_SM.m --- Analyses/CurateRipples2_SM.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Analyses/CurateRipples2_SM.m b/Analyses/CurateRipples2_SM.m index 0f34a45..4445cd9 100644 --- a/Analyses/CurateRipples2_SM.m +++ b/Analyses/CurateRipples2_SM.m @@ -1,4 +1,6 @@ %% CurateRipples +clear all +close all global plotData plotData.listSel = 1; % Used to keep track of which list is being selected from for ripple viewing plotData.Window = 50; From fce843768faa42abd6d60cef0d3863cbfe6e6f82 Mon Sep 17 00:00:00 2001 From: gaelias Date: Fri, 28 Feb 2020 12:41:57 -0800 Subject: [PATCH 080/133] Update CurateRipples2_SM.m --- Analyses/CurateRipples2_SM.m | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Analyses/CurateRipples2_SM.m b/Analyses/CurateRipples2_SM.m index 0f34a45..1f05304 100644 --- a/Analyses/CurateRipples2_SM.m +++ b/Analyses/CurateRipples2_SM.m @@ -1,4 +1,6 @@ %% CurateRipples +close all +clear all global plotData plotData.listSel = 1; % Used to keep track of which list is being selected from for ripple viewing plotData.Window = 50; @@ -14,8 +16,9 @@ %% rips = RippleDetection_SM(envProc, powThresh, durThresh, durThreshMrg, syncThresh, syncWin, smoothWin); -[trialRips] = ExtractTrialEventRips_SM(rips, [500 700]); -allTrialRips = sortrows(cell2mat([trialRips.Events(:,1); trialRips.Events(:,2); trialRips.Events(:,3)])); +[trialRips] = ExtractTrialEventRips_SM(rips, [500 500]); +% allTrialRips = sortrows(cell2mat([trialRips.Events(:,1); trialRips.Events(:,2); trialRips.Events(:,3)])); % Use for ALL (pre-trial, trial and post-trial) Trial Rips +allTrialRips = trialRips.Events(:,3); % Use for ONLY Post-Trial Rips %% Plot Descriptives PlotNearTrialRipStats(trialRips) From 2ee21788472402b6c68a9a9cf93667d931c4b0df Mon Sep 17 00:00:00 2001 From: gaelias Date: Fri, 28 Feb 2020 12:42:02 -0800 Subject: [PATCH 081/133] Update RippleAnalysis_SM.m --- Analyses/RippleAnalysis_SM.m | 66 ++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/Analyses/RippleAnalysis_SM.m b/Analyses/RippleAnalysis_SM.m index d87fa25..5cb15e2 100644 --- a/Analyses/RippleAnalysis_SM.m +++ b/Analyses/RippleAnalysis_SM.m @@ -43,39 +43,39 @@ load(fileNames{ensembleFileLog}); behavMatrixTrialStruct = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [0 0], 'PokeIn'); -% poi = [behavMatrixTrialStruct.PokeOutIndex]; -% ri = [behavMatrixTrialStruct.RewardIndex]; -% if length(poi)~=length(ri) -% ri(end:end+(length(poi)-length(ri)))=nan; -% end -% dif = ri-poi; -% perfLog = [behavMatrixTrialStruct.Performance]==1; -% isLog = [behavMatrixTrialStruct.TranspositionDistance]==0; -% iscLog = perfLog & isLog; -% isiLog = ~perfLog & isLog; -% oscLog = perfLog & ~isLog; -% osiLog = ~perfLog & ~isLog; -% -% figure; -% subplot(2,2,1); -% histogram(dif(iscLog)); -% title('InSeq Correct'); -% subplot(2,2,2); -% histogram(dif(isiLog)); -% title('InSeq Incorrect'); -% subplot(2,2,3); -% histogram(dif(osiLog)); -% title('OutSeq Incorrect'); -% subplot(2,2,4); -% histogram(dif(oscLog)); -% title('OutSeq Correct'); -% -% annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string',... -% sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); -% -% annotation('textbox', 'position', [0.1 0.95 0.8 0.05], 'string',... -% '\bfReward Delivery Latency (relative to port withdrawal)', 'horizontalalignment', 'center',... -% 'linestyle', 'none', 'fontsize', 15); +poi = [behavMatrixTrialStruct.PokeOutIndex]; +ri = [behavMatrixTrialStruct.RewardIndex]; +if length(poi)~=length(ri) + ri(end:end+(length(poi)-length(ri)))=nan; +end +dif = ri-poi; +perfLog = [behavMatrixTrialStruct.Performance]==1; +isLog = [behavMatrixTrialStruct.TranspositionDistance]==0; +iscLog = perfLog & isLog; +isiLog = ~perfLog & isLog; +oscLog = perfLog & ~isLog; +osiLog = ~perfLog & ~isLog; + +figure; +subplot(2,2,1); +histogram(dif(iscLog)); +title('InSeq Correct'); +subplot(2,2,2); +histogram(dif(isiLog)); +title('InSeq Incorrect'); +subplot(2,2,3); +histogram(dif(osiLog)); +title('OutSeq Incorrect'); +subplot(2,2,4); +histogram(dif(oscLog)); +title('OutSeq Correct'); + +annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string',... + sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); + +annotation('textbox', 'position', [0.1 0.95 0.8 0.05], 'string',... + '\bfReward Delivery Latency (relative to port withdrawal)', 'horizontalalignment', 'center',... + 'linestyle', 'none', 'fontsize', 15); %% Extract Raw Values & Compute RMS Power ripBPF = nan(size(behavMatrix,1),size(tetFiles,1)); ripVolts = nan(size(behavMatrix,1),size(tetFiles,1)); From e9b9c25eb20a1628381d6ddd47722776ef86aebc Mon Sep 17 00:00:00 2001 From: gaelias Date: Fri, 28 Feb 2020 13:02:13 -0800 Subject: [PATCH 082/133] Update CurateRipples2_SM.m --- Analyses/CurateRipples2_SM.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Analyses/CurateRipples2_SM.m b/Analyses/CurateRipples2_SM.m index ca14d8e..aa35176 100644 --- a/Analyses/CurateRipples2_SM.m +++ b/Analyses/CurateRipples2_SM.m @@ -2,12 +2,12 @@ clear all close all global plotData -plotData.listSel = 1; % Used to keep track of which list is being selected from for ripple viewing +plotData.listSel = 2; % Used to keep track of which list is being selected from for ripple viewing plotData.Window = 50; %% Parameters % envProc = 'RMS'; envProc = 'HILB'; -powThresh = [0 4]; +powThresh = [0 3]; durThresh = 15; durThreshMrg = 15; syncThresh = 0; @@ -16,7 +16,7 @@ %% rips = RippleDetection_SM(envProc, powThresh, durThresh, durThreshMrg, syncThresh, syncWin, smoothWin); -[trialRips] = ExtractTrialEventRips_SM(rips, [500 500]); +[trialRips] = ExtractTrialEventRips_SM(rips, [0 500]); % allTrialRips = sortrows(cell2mat([trialRips.Events(:,1); trialRips.Events(:,2); trialRips.Events(:,3)])); % Use for ALL (pre-trial, trial and post-trial) Trial Rips allTrialRips = trialRips.Events(:,3); % Use for ONLY Post-Trial Rips From cff7856786eef2d9145ca415c9cf1bd9e39aea21 Mon Sep 17 00:00:00 2001 From: gaelias Date: Fri, 28 Feb 2020 13:02:27 -0800 Subject: [PATCH 083/133] Update CurateRipples2_SM.m --- Analyses/CurateRipples2_SM.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Analyses/CurateRipples2_SM.m b/Analyses/CurateRipples2_SM.m index aa35176..3e95440 100644 --- a/Analyses/CurateRipples2_SM.m +++ b/Analyses/CurateRipples2_SM.m @@ -1,6 +1,6 @@ %% CurateRipples -clear all -close all +% clear all +% close all global plotData plotData.listSel = 2; % Used to keep track of which list is being selected from for ripple viewing plotData.Window = 50; From 53668d8f3b0c6fe56ddf8b01df43d87d077dd291 Mon Sep 17 00:00:00 2001 From: gaelias Date: Fri, 28 Feb 2020 13:02:57 -0800 Subject: [PATCH 084/133] Update CurateRipples2_SM.m --- Analyses/CurateRipples2_SM.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Analyses/CurateRipples2_SM.m b/Analyses/CurateRipples2_SM.m index 3e95440..67d9131 100644 --- a/Analyses/CurateRipples2_SM.m +++ b/Analyses/CurateRipples2_SM.m @@ -18,7 +18,7 @@ rips = RippleDetection_SM(envProc, powThresh, durThresh, durThreshMrg, syncThresh, syncWin, smoothWin); [trialRips] = ExtractTrialEventRips_SM(rips, [0 500]); % allTrialRips = sortrows(cell2mat([trialRips.Events(:,1); trialRips.Events(:,2); trialRips.Events(:,3)])); % Use for ALL (pre-trial, trial and post-trial) Trial Rips -allTrialRips = trialRips.Events(:,3); % Use for ONLY Post-Trial Rips +allTrialRips = cell2mat(trialRips.Events(:,3)); % Use for ONLY Post-Trial Rips %% Plot Descriptives PlotNearTrialRipStats(trialRips) From 9e2e41766e3c18c4e76cc00df67e7344b033cada Mon Sep 17 00:00:00 2001 From: gaelias Date: Fri, 28 Feb 2020 13:22:56 -0800 Subject: [PATCH 085/133] Update CurateRipples2_SM.m --- Analyses/CurateRipples2_SM.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Analyses/CurateRipples2_SM.m b/Analyses/CurateRipples2_SM.m index 67d9131..ef90ef6 100644 --- a/Analyses/CurateRipples2_SM.m +++ b/Analyses/CurateRipples2_SM.m @@ -15,8 +15,8 @@ smoothWin = 21; %% -rips = RippleDetection_SM(envProc, powThresh, durThresh, durThreshMrg, syncThresh, syncWin, smoothWin); -[trialRips] = ExtractTrialEventRips_SM(rips, [0 500]); +% rips = RippleDetection_SM(envProc, powThresh, durThresh, durThreshMrg, syncThresh, syncWin, smoothWin); +% [trialRips] = ExtractTrialEventRips_SM(rips, [0 500]); % allTrialRips = sortrows(cell2mat([trialRips.Events(:,1); trialRips.Events(:,2); trialRips.Events(:,3)])); % Use for ALL (pre-trial, trial and post-trial) Trial Rips allTrialRips = cell2mat(trialRips.Events(:,3)); % Use for ONLY Post-Trial Rips From c350e1163144b76d2264cee0a78e929a6be20d12 Mon Sep 17 00:00:00 2001 From: gaelias Date: Fri, 28 Feb 2020 13:56:47 -0800 Subject: [PATCH 086/133] Update CurateRipples2_SM.m --- Analyses/CurateRipples2_SM.m | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Analyses/CurateRipples2_SM.m b/Analyses/CurateRipples2_SM.m index ef90ef6..c9279f8 100644 --- a/Analyses/CurateRipples2_SM.m +++ b/Analyses/CurateRipples2_SM.m @@ -15,8 +15,8 @@ smoothWin = 21; %% -% rips = RippleDetection_SM(envProc, powThresh, durThresh, durThreshMrg, syncThresh, syncWin, smoothWin); -% [trialRips] = ExtractTrialEventRips_SM(rips, [0 500]); +rips = RippleDetection_SM(envProc, powThresh, durThresh, durThreshMrg, syncThresh, syncWin, smoothWin); +[trialRips] = ExtractTrialEventRips_SM(rips, [0 500]); % allTrialRips = sortrows(cell2mat([trialRips.Events(:,1); trialRips.Events(:,2); trialRips.Events(:,3)])); % Use for ALL (pre-trial, trial and post-trial) Trial Rips allTrialRips = cell2mat(trialRips.Events(:,3)); % Use for ONLY Post-Trial Rips @@ -225,12 +225,12 @@ set(plotData.rawPlot(r), 'XData', curTS, 'YData', rawData(r,:)); end for b = 1:length(plotData.bpfPlot) - set(plotData.bpfPlot(b), 'XData', curTS, 'YData', bpfData(r,:)); + set(plotData.bpfPlot(b), 'XData', curTS, 'YData', bpfData(b,:)); end set(plotData.spkRasts, 'XData', curTS(spkX), 'YData', spkY); set(plotData.envPlot, 'XData', curTS, 'YData', envData(curNdx(1)-plotData.Window:curNdx(2)+plotData.Window,:)); - set(plotData.envTH1, 'XData', curTS, 'YData', ones(1,length(curNdx(1)-plotData.Window:curNdx(2)+plotData.Window))*(mean(plotData.ripCure.UserData(:,2)) + (plotData.PowThresh (1)*std(plotData.ripCure.UserData(:,2))))); - set(plotData.envTH2, 'XData', curTS, 'YData', ones(1,length(curNdx(1)-plotData.Window:curNdx(2)+plotData.Window))*(mean(plotData.ripCure.UserData(:,2)) + (plotData.PowThresh (2)*std(plotData.ripCure.UserData(:,2))))); + set(plotData.envTH1, 'XData', curTS, 'YData', ones(1,length(curNdx(1)-plotData.Window:curNdx(2)+plotData.Window))*(mean(plotData.ripCure.UserData(:,2)) + (plotData.PowThresh(1)*std(plotData.ripCure.UserData(:,2))))); + set(plotData.envTH2, 'XData', curTS, 'YData', ones(1,length(curNdx(1)-plotData.Window:curNdx(2)+plotData.Window))*(mean(plotData.ripCure.UserData(:,2)) + (plotData.PowThresh(2)*std(plotData.ripCure.UserData(:,2))))); curRipX = [plotData.ripCure.UserData(curNdx,1); flipud(plotData.ripCure.UserData(curNdx,1))]'; set(plotData.RipPatch.Raw, 'XData', curRipX); set(plotData.RipPatch.BPF, 'XData', curRipX); From a8d3a610f0ccb9fd43deef7acf855d9569181cf5 Mon Sep 17 00:00:00 2001 From: gaelias Date: Fri, 28 Feb 2020 14:09:40 -0800 Subject: [PATCH 087/133] Update CurateRipples2_SM.m --- Analyses/CurateRipples2_SM.m | 1 + 1 file changed, 1 insertion(+) diff --git a/Analyses/CurateRipples2_SM.m b/Analyses/CurateRipples2_SM.m index c9279f8..4c65ae2 100644 --- a/Analyses/CurateRipples2_SM.m +++ b/Analyses/CurateRipples2_SM.m @@ -188,6 +188,7 @@ plotData.envTH2 = plot(plotData.bpfAxes, curTS,... ones(1,length(curNdx(1)-plotData.Window:curNdx(2)+plotData.Window))*(mean(plotData.ripCure.UserData(:,2)) + (plotData.PowThresh (2)*std(plotData.ripCure.UserData(:,2)))),... 'color', 'k', 'linestyle','-', 'linewidth', 2); + hold(plotData.bpfAxes, 'off'); plotData.bpfAxes.UserData = bpfData; for b = 1:length(plotData.bpfPlot) plotData.bpfPlot(b).Color(4) = 0.2; From 2356c965e37a5d8a4ddd8791e94eba594c78c299 Mon Sep 17 00:00:00 2001 From: gaelias Date: Fri, 28 Feb 2020 15:26:54 -0800 Subject: [PATCH 088/133] Update CurateRipples2_SM.m --- Analyses/CurateRipples2_SM.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Analyses/CurateRipples2_SM.m b/Analyses/CurateRipples2_SM.m index 4c65ae2..1eb567d 100644 --- a/Analyses/CurateRipples2_SM.m +++ b/Analyses/CurateRipples2_SM.m @@ -1,13 +1,13 @@ %% CurateRipples -% clear all -% close all +clear all +close all global plotData plotData.listSel = 2; % Used to keep track of which list is being selected from for ripple viewing plotData.Window = 50; %% Parameters % envProc = 'RMS'; envProc = 'HILB'; -powThresh = [0 3]; +powThresh = [1 3]; durThresh = 15; durThreshMrg = 15; syncThresh = 0; From 3335a156a88afd74ae2b7f9e9309c4587da68dfc Mon Sep 17 00:00:00 2001 From: gaelias Date: Mon, 2 Mar 2020 16:25:13 -0800 Subject: [PATCH 089/133] Update CurateRipples2_SM.m --- Analyses/CurateRipples2_SM.m | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Analyses/CurateRipples2_SM.m b/Analyses/CurateRipples2_SM.m index 1eb567d..2bfbaf6 100644 --- a/Analyses/CurateRipples2_SM.m +++ b/Analyses/CurateRipples2_SM.m @@ -7,7 +7,7 @@ %% Parameters % envProc = 'RMS'; envProc = 'HILB'; -powThresh = [1 3]; +powThresh = [1 5]; durThresh = 15; durThreshMrg = 15; syncThresh = 0; @@ -484,6 +484,16 @@ function ExportTrlRips(source, event) epocNsmblAct(epoc) = mean(sum(tempSpkMtx)>=1); end +%% Evaluate Spectrogram +ripSpect = cell(size(epocWindows,1),1); +for e = 1:size(epocWindows,1) + tempSpect = nan(diff(epocWindows(e,:))+1,500,size(ripBPF,2)); + for t = 1:size(ripBPF,2) + tempSpect(:,:,t) = MorletAG(ripBPF(epocWindows(e,1):epocWindows(e,2),t), 1/samp, 1, 500)'; + end + ripSpect{e} = tempSpect; +end + %% Organize Data Output rips = struct(... 'TimeStamps', statMatrix(:,1),... From 862e629b54241aabea0880530938e63f5f3af017 Mon Sep 17 00:00:00 2001 From: gaelias Date: Mon, 2 Mar 2020 16:25:22 -0800 Subject: [PATCH 090/133] Update MorletAG.m --- Analyses/MorletAG.m | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Analyses/MorletAG.m b/Analyses/MorletAG.m index 4966d0d..4e71156 100644 --- a/Analyses/MorletAG.m +++ b/Analyses/MorletAG.m @@ -25,8 +25,12 @@ freq = minFreq:maxFreq; %% Frequency of Morlet Wavelet %% Bandstop 60Hz signal -[b1, a1] = butter(2, [59/500 61/500], 'stop'); %% Remove 60hz Harmonic (noise) -bsData = filtfilt(b1, a1, lfp); +if minFreq<=60 %% Remove 60hz Harmonic (noise) if necessary + [b1, a1] = butter(2, [59/500 61/500], 'stop'); + bsData = filtfilt(b1, a1, lfp); +else + bsData = lfp; +end % bsData = lfp; freqPower = nan(length(freq),length(time)); %% Create the data matrix dataX = fft(bsData', length(time)*2-1); %% Fast Fourier Transform of Data (correct length) @@ -49,5 +53,5 @@ power = abs(analytic_signal).^2; %% Extract Power power = (10*log10(power)); %% Convert to Logarithmic Scale - freqPower(fi, :) = power; %% Add Data to Matrix + freqPower(fi, 1:length(power)) = power; %% Add Data to Matrix end \ No newline at end of file From 8d7a34732cc0492cb03bf2d8dffd6370fe262a62 Mon Sep 17 00:00:00 2001 From: gaelias Date: Mon, 2 Mar 2020 17:31:12 -0800 Subject: [PATCH 091/133] Update CurateRipples2_SM.m --- Analyses/CurateRipples2_SM.m | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/Analyses/CurateRipples2_SM.m b/Analyses/CurateRipples2_SM.m index 2bfbaf6..4d6ced0 100644 --- a/Analyses/CurateRipples2_SM.m +++ b/Analyses/CurateRipples2_SM.m @@ -486,19 +486,29 @@ function ExportTrlRips(source, event) %% Evaluate Spectrogram ripSpect = cell(size(epocWindows,1),1); +ripFreq = nan(size(epocWindows,1),1); +ripMaxFreq = nan(size(epocWindows,1),1); +freqs = [150 250]; +freqsVect = freqs(1):freqs(2); for e = 1:size(epocWindows,1) - tempSpect = nan(diff(epocWindows(e,:))+1,500,size(ripBPF,2)); + tempSpect = nan(diff(epocWindows(e,:))+101,freqs(2)-freqs(1)+1,size(ripBPF,2)); for t = 1:size(ripBPF,2) - tempSpect(:,:,t) = MorletAG(ripBPF(epocWindows(e,1):epocWindows(e,2),t), 1/samp, 1, 500)'; + tempSpect(:,:,t) = MorletAG(ripVolts(epocWindows(e,1)-50:epocWindows(e,2)+50,t), 1/samp, freqs(1), freqs(2))'; end ripSpect{e} = tempSpect; + tempMax = mean(tempSpect(51:end-50,:,:),3); + [~,c] = find((tempMax./repmat(max(tempMax,[],2), [1,size(tempMax,2)]))==1); + ripFreq(e) = mean(freqsVect(c)); + [~, cM] = find((tempMax./max(max(tempMax)))==1); + ripMaxFreq(e) = freqsVect(cM); end %% Organize Data Output rips = struct(... 'TimeStamps', statMatrix(:,1),... 'Ripples', struct('Events', epocWindows, 'Duration', epocDur,... - 'Synchrony', epocSync, 'EnsembleActivity', epocNsmblAct),... + 'Synchrony', epocSync, 'EnsembleActivity', epocNsmblAct,... + 'MeanFrequency', ripFreq, 'MaxPowerFrequency', ripMaxFreq),... 'SessionData', struct('RawLFP', ripVolts, 'RipBPF', ripBPF,... 'RipEnv', ripRMS, 'RipPhase', ripHilb, 'TetIDs', {ripTetIDs},... 'Spikes', spkMtxSorted),... From bc256dcf3c54b43af1de0edce7e5cbbeb0b27022 Mon Sep 17 00:00:00 2001 From: gaelias Date: Thu, 5 Mar 2020 10:35:05 -0800 Subject: [PATCH 092/133] Update CurateRipples2_SM.m --- Analyses/CurateRipples2_SM.m | 248 +++++++++++++++++++++++++++++------ 1 file changed, 208 insertions(+), 40 deletions(-) diff --git a/Analyses/CurateRipples2_SM.m b/Analyses/CurateRipples2_SM.m index 4d6ced0..a75f226 100644 --- a/Analyses/CurateRipples2_SM.m +++ b/Analyses/CurateRipples2_SM.m @@ -7,8 +7,8 @@ %% Parameters % envProc = 'RMS'; envProc = 'HILB'; -powThresh = [1 5]; -durThresh = 15; +powThresh = [1 3]; +durThresh = 25; durThreshMrg = 15; syncThresh = 0; syncWin = 10; @@ -16,60 +16,112 @@ %% rips = RippleDetection_SM(envProc, powThresh, durThresh, durThreshMrg, syncThresh, syncWin, smoothWin); -[trialRips] = ExtractTrialEventRips_SM(rips, [0 500]); +[trialRips] = ExtractTrialEventRips_SM(rips, [500 500]); % allTrialRips = sortrows(cell2mat([trialRips.Events(:,1); trialRips.Events(:,2); trialRips.Events(:,3)])); % Use for ALL (pre-trial, trial and post-trial) Trial Rips -allTrialRips = cell2mat(trialRips.Events(:,3)); % Use for ONLY Post-Trial Rips +allTrialRips = cell2mat(trialRips.Events(:,3)); % Use for ONLY Post-Trial Rips + +%% Toss Ripple Features into PlotData +% Need to update the selection for TrialRips if not using only Post-Trial +% Rips +plotData.Rips = rips; +plotData.SessionRips.Events = rips.Ripples.Events; +plotData.SessionRips.Synchrony = rips.Ripples.Synchrony; +plotData.SessionRips.EnsembleAct = rips.Ripples.EnsembleActivity; +plotData.SessionRips.MaxPower = rips.Ripples.MaxPower; +plotData.SessionRips.RipFreq = rips.Ripples.MaxPowerFrequency; +plotData.TrialRips.Events = allTrialRips; +plotData.TrialRips.Synchrony = cell2mat(trialRips.Synchrony(:,3)); % Use for ONLY Post-Trial Rips +plotData.TrialRips.EnsembleAct = cell2mat(trialRips.EnsembleAct(:,3)); % Use for ONLY Post-Trial Rips +plotData.TrialRips.MaxPower = cell2mat(trialRips.MaxPower(:,3)); % Use for ONLY Post-Trial Rips +plotData.TrialRips.RipFreq = cell2mat(trialRips.RipFreq(:,3)); % Use for ONLY Post-Trial Rips %% Plot Descriptives PlotNearTrialRipStats(trialRips) annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string',... sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); annotation('textbox', 'position', [0.01 0.95 0.9 0.05], 'string',... - sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge \\bfThreshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg),... + sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge Threshold = \\rm%i ms; \\bfDuration Threshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg, durThresh),... 'linestyle', 'none'); PlotRipFeatCorr(rips.Ripples.Duration, rips.Ripples.Synchrony, 'Duration', 'Synchrony') annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string',... sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); annotation('textbox', 'position', [0.01 0.95 0.9 0.05], 'string',... - sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge \\bfThreshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg),... + sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge Threshold = \\rm%i ms; \\bfDuration Threshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg, durThresh),... 'linestyle', 'none'); PlotRipFeatCorr(rips.Ripples.Duration, rips.Ripples.EnsembleActivity, 'Duration', 'Ensemble Activity') annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string',... sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); annotation('textbox', 'position', [0.01 0.95 0.9 0.05], 'string',... - sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge \\bfThreshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg),... + sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge Threshold = \\rm%i ms; \\bfDuration Threshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg, durThresh),... 'linestyle', 'none'); PlotRipFeatCorr(rips.Ripples.Synchrony, rips.Ripples.EnsembleActivity, 'Synchrony', 'Ensemble Activity') annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string',... sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); annotation('textbox', 'position', [0.01 0.95 0.9 0.05], 'string',... - sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge \\bfThreshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg),... + sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge Threshold = \\rm%i ms; \\bfDuration Threshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg, durThresh),... 'linestyle', 'none'); PlotRipFeatsByTrlType(rips, trialRips) annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string',... sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); annotation('textbox', 'position', [0.01 0.95 0.9 0.05], 'string',... - sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge \\bfThreshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg),... + sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge Threshold = \\rm%i ms; \\bfDuration Threshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg, durThresh),... 'linestyle', 'none'); PlotRipFeatsByOdor(rips, trialRips) annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string',... sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); annotation('textbox', 'position', [0.01 0.95 0.9 0.05], 'string',... - sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge \\bfThreshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg),... + sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge Threshold = \\rm%i ms; \\bfDuration Threshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg, durThresh),... 'linestyle', 'none'); PlotRipCountsByEvent(rips) annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string',... sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); annotation('textbox', 'position', [0.01 0.95 0.9 0.05], 'string',... - sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge \\bfThreshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg),... + sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge Threshold = \\rm%i ms; \\bfDuration Threshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg, durThresh),... 'linestyle', 'none'); +PlotRipFeatsByEvent(rips, 'Power') +annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string',... + sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); +annotation('textbox', 'position', [0.01 0.95 0.9 0.05], 'string',... + sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge Threshold = \\rm%i ms; \\bfDuration Threshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg, durThresh),... + 'linestyle', 'none'); + +PlotRipFeatsByEvent(rips, 'Synchrony') +annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string',... + sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); +annotation('textbox', 'position', [0.01 0.95 0.9 0.05], 'string',... + sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge Threshold = \\rm%i ms; \\bfDuration Threshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg, durThresh),... + 'linestyle', 'none'); + +PlotRipFeatsByEvent(rips, 'Spiking') +annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string',... + sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); +annotation('textbox', 'position', [0.01 0.95 0.9 0.05], 'string',... + sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge Threshold = \\rm%i ms; \\bfDuration Threshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg, durThresh),... + 'linestyle', 'none'); + +PlotRipFeatsByEvent(rips, 'Duration') +annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string',... + sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); +annotation('textbox', 'position', [0.01 0.95 0.9 0.05], 'string',... + sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge Threshold = \\rm%i ms; \\bfDuration Threshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg, durThresh),... + 'linestyle', 'none'); + +PlotRipFeatsByEvent(rips, 'MaxFreq') +annotation('textbox', 'position', [0.01 0.01 0.9 0.05], 'string',... + sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); +annotation('textbox', 'position', [0.01 0.95 0.9 0.05], 'string',... + sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge Threshold = \\rm%i ms; \\bfDuration Threshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg, durThresh),... + 'linestyle', 'none'); + + + %% Create Figure plotData.PowThresh = rips.FileInfo.PowerThreshold; plotData.ripCure = figure; @@ -119,11 +171,19 @@ removeRipBtn = uicontrol(plotData.ripCure, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', 'Remove Ripple',... 'Position', [0.425, 0.075, 0.2, 0.05], 'Callback', @RmvRip); +% Rip Playback +playRipBtn = uicontrol(plotData.ripCure, 'Units', 'Normalized', 'Style', 'pushbutton', 'String', 'Play Ripple',... + 'Position', [0.01, 0.9, 0.08, 0.05], 'Callback', @PlayRip); +plotData.playRipLst = uicontrol(plotData.ripCure, 'Units', 'Normalized', 'Style', 'listbox', 'String', rips.SessionData.TetIDs,... + 'Position', [0.01, 0.75, 0.08, 0.145], 'Callback', @HighlightTrace); +plotData.dcOffsetRadio = uicontrol(plotData.ripCure, 'Units', 'Normalized', 'Style', 'radiobutton', 'String', 'DC Offset',... + 'Position', [0.01, 0.7, 0.08, 0.05]); + annotation('textbox', 'position', [0.01 0.005 0.9 0.05], 'string',... sprintf('%s', cd), 'linestyle', 'none', 'interpreter', 'none'); annotation('textbox', 'position', [0.01 0.95 0.9 0.05], 'string',... - sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge \\bfThreshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg),... + sprintf('\\bfThreshold: \\rm+%i(+%i)SD; \\bfEnvelope = \\rm%s; \\bfMerge Threshold = \\rm%i ms; \\bfDuration Threshold = \\rm%i ms', powThresh(1), powThresh(2), envProc, durThreshMrg, durThresh),... 'linestyle', 'none'); %% Initialize Things SetPlots; @@ -179,6 +239,9 @@ for r = 1:length(plotData.rawPlot) plotData.rawPlot(r).Color(4) = 0.2; end + hold(plotData.rawAxes, 'on'); + plotData.audMrk = line(plotData.rawAxes, [curTS(1) curTS(1)], get(plotData.rawAxes, 'ylim'), 'color', 'k', 'visible', 'off'); + hold(plotData.rawAxes, 'off'); plotData.bpfPlot = plot(plotData.bpfAxes, curTS, bpfData(curNdx(1)-plotData.Window:curNdx(2)+plotData.Window,:), 'color', 'k'); hold(plotData.bpfAxes, 'on'); plotData.envPlot = plot(plotData.bpfAxes, curTS, envData(curNdx(1)-plotData.Window:curNdx(2)+plotData.Window,:), 'color', 'r', 'linewidth', 2); @@ -237,6 +300,7 @@ set(plotData.RipPatch.BPF, 'XData', curRipX); set(plotData.RipPatch.Spk, 'XData', curRipX); end +HighlightTrace title(plotData.spkAxes, sprintf('Duration = %i(ms)', diff(curNdx))); % refreshdata(plotData.ripCure); end @@ -256,21 +320,67 @@ function SelectTrlRip(source,event) function NextRip(source,event) global plotData if plotData.listSel == 1 - plotData.ssnRipList.Value = plotData.ssnRipList.Value + 1; + if plotData.ssnRipList.Value < size(plotData.ssnRipList.String,1) + plotData.ssnRipList.Value = plotData.ssnRipList.Value + 1; + SetPlots + else + plotData.ssnRipList.Value = size(plotData.ssnRipList.String,1); + end else - plotData.trlRipList.Value = plotData.trlRipList.Value + 1; + if plotData.trlRipList.Value < size(plotData.trlRipList.String,1) + plotData.trlRipList.Value = plotData.trlRipList.Value + 1; + SetPlots + else + plotData.trlRipList.Value = size(plotData.trlRipList.String,1); + end end -SetPlots end function PrevRip(source,event) global plotData if plotData.listSel == 1 - plotData.ssnRipList.Value = plotData.ssnRipList.Value - 1; + if plotData.ssnRipList.Value ~= 1 + plotData.ssnRipList.Value = plotData.ssnRipList.Value - 1; + SetPlots + end else - plotData.trlRipList.Value = plotData.trlRipList.Value - 1; + if plotData.trlRipList.Value ~= 1 + plotData.trlRipList.Value = plotData.trlRipList.Value - 1; + SetPlots + end +end +end + +function PlayRip(source, event) +global plotData +tetID = plotData.playRipLst.Value; +curWave = plotData.rawPlot(tetID).YData; +if plotData.dcOffsetRadio.Value == 1 + curWave = curWave-curWave(1); % To reduce initial popping due to DC offset. +end +% if plotData.detrend.Value ==1 +% curWave = detrend(curWave); +% end +curSound = audioplayer(curWave, 1000); +playblocking(curSound); +% curWaveTime = plotData.rawPlot(tetID).XData; +% for t = 1:length(curWaveTime) +% set(plotData.audMrk, 'XData', [curWaveTime(t) curWaveTime(t)]); +% % pause(1/1000); +% drawnow +% end +end + +function HighlightTrace(source, event) +global plotData +tetID = plotData.playRipLst.Value; +for t = 1:size(plotData.playRipLst.String,1) + if t==tetID + plotData.rawPlot(t).Color = [1 0 0 1]; + else + plotData.rawPlot(t).Color = [0 0 0 0.2]; + end end -SetPlots end function ZoomOut(source,event) @@ -296,9 +406,22 @@ function RmvRip(source,event) curNdx = plotData.trlRipList.UserData(plotData.trlRipList.Value,:); end ssnList = plotData.ssnRipList.UserData; -ssnList(ssnList(:,1)==curNdx(1),:) = []; -trlList = plotData.ssnRipList.UserData; -trlList(trlList(:,1)==curNdx(1),:) = []; +ssnListNdx = ssnList(:,1)==curNdx(1); +ssnList(ssnListNdx,:) = []; +plotData.SessionRips.Events(ssnListNdx,:) = []; +plotData.SessionRips.Synchrony(ssnListNdx,:) = []; +plotData.SessionRips.EnsembleAct(ssnListNdx,:) = []; +plotData.SessionRips.MaxPower(ssnListNdx,:) = []; +plotData.SessionRips.RipFreq(ssnListNdx,:) = []; + +trlList = plotData.trlRipList.UserData; +trlListNdx = trlList(:,1)==curNdx(1); +trlList(trlListNdx,:) = []; +plotData.TrialRips.Events(trlListNdx,:) = []; +plotData.TrialRips.Synchrony(trlListNdx,:) = []; +plotData.TrialRips.EnsembleAct(trlListNdx,:) = []; +plotData.TrialRips.MaxPower(trlListNdx,:) = []; +plotData.TrialRips.RipFreq(trlListNdx,:) = []; plotData.ssnRipList.UserData = ssnList; plotData.ssnRipList.String = 1:size(ssnList,1); @@ -314,8 +437,8 @@ function RmvRip(source,event) else if plotData.trlRipList.Value == 1 plotData.trlRipList.Value = 1; - elseif plotData.trlRipList.Value == size(ssnList,1)+1 - plotData.trlRipList.Value = size(ssnList,1); + elseif plotData.trlRipList.Value == size(trlList,1)+1 + plotData.trlRipList.Value = size(trlList,1); end end SetPlots @@ -323,15 +446,37 @@ function RmvRip(source,event) function ExportSsnRips(source, event) global plotData -exportData = plotData.ssnRipList.UserData; -save('SessionRips.mat', 'exportData'); +ripMatrix(:,1) = plotData.ripCure.UserData(:,1); +ripMatrix(:,2) = false(size(ripMatrix,1),1); +ripMatrixColIDs = [{'TimeBin'}, {'Session_Ripple_Log'}]; +ripFeats = [plotData.ssnRipList.UserData, plotData.SessionRips.Synchrony,... + plotData.SessionRips.EnsembleAct, plotData.SessionRips.MaxPower,... + plotData.SessionRips.RipFreq]; +ripFeatsColIDs = [{'RippleStartIndex'}, {'RippleEndIndex'},... + {'RippleSynchrony'}, {'RipplePercentActiveCells'}, {'RipplePower'},... + {'RippleFrequency'}]; +for e = 1:size(ripFeats, 1) + ripMatrix(ripFeats(e,1):ripFeats(e,2),2) = true; +end +save('SessionRipples.mat', 'ripMatrix', 'ripMatrixColIDs', 'ripFeats', 'ripFeatsColIDs'); msgbox('Session Ripple Indices Saved'); end function ExportTrlRips(source, event) global plotData -exportData = plotData.trlRipList.UserData; -save('TrialRips.mat', 'exportData'); +ripMatrix(:,1) = plotData.ripCure.UserData(:,1); +ripMatrix(:,2) = false(size(ripMatrix,1),1); +ripMatrixColIDs = [{'TimeBin'}, {'Trial_Ripple_Log'}]; +ripFeats = [plotData.trlRipList.UserData, plotData.TrialRips.Synchrony,... + plotData.TrialRips.EnsembleAct, plotData.TrialRips.MaxPower,... + plotData.TrialRips.RipFreq]; +ripFeatsColIDs = [{'RippleStartIndex'}, {'RippleEndIndex'},... + {'RippleSynchrony'}, {'RipplePercentActiveCells'}, {'RipplePower'},... + {'RippleFrequency'}]; +for e = 1:size(ripFeats, 1) + ripMatrix(ripFeats(e,1):ripFeats(e,2),2) = true; +end +save('TrialRipples.mat', 'ripMatrix', 'ripMatrixColIDs', 'ripFeats', 'ripFeatsColIDs'); msgbox('Trial Ripple Indices Saved'); end @@ -419,6 +564,7 @@ function ExportTrlRips(source, event) %% Calculate Thresholds % Aggregate Power aggPower = mean(ripRMS,2); % Mean envelope +zAgg = zscore(aggPower); % aggPower = zscore(mean(ripRMS,2)); % Z-Score Mean envelope % aggPower = mean(zscore(ripRMS),2); % Mean Z-Score envelope @@ -449,27 +595,34 @@ function ExportTrlRips(source, event) for slr = 1:length(shortLatRipls) nxtNdx = shortLatRipls(slr)+find(slrLog(shortLatRipls(slr)+1:end)==0,1,'first'); epocWindows(shortLatRipls(slr),2) = epocWindows(nxtNdx,2); - mrgdNdxs(nxtNdx) = true; + mrgdNdxs(shortLatRipls(slr)+1:nxtNdx) = true; end epocWindows(mrgdNdxs,:) = []; -%% Quantify & Extract Ripples +% Threshold ripples based on duration % Determine the duration/timing of each event epocDur = diff(epocWindows,1,2); +durThreshLog = epocDur<=durThresh; +epocWindows(durThreshLog,:) = []; +epocDur(durThreshLog,:) = []; +%% Quantify & Extract Ripples % Determine the Synchrony of each event epocSync = nan(size(epocWindows,1),1); epocSyncConfMtx = cell(size(epocWindows,1),1); + for epoc = 1:size(epocWindows,1) tempConfMtx = nan(size(ripHilb,2)); for e1 = 1:size(ripHilb,2) for e2 = 1:size(ripHilb,2) - [tempConfMtx(e1,e2),~] = circ_corrcc(ripHilb(epocWindows(epoc,1):epocWindows(epoc,2),e1),... - ripHilb(epocWindows(epoc,1):epocWindows(epoc,2),e2)); + if e2>e1 + [tempConfMtx(e1,e2),~] = circ_corrcc(ripHilb(epocWindows(epoc,1):epocWindows(epoc,2),e1),... + ripHilb(epocWindows(epoc,1):epocWindows(epoc,2),e2)); + end end end epocSyncConfMtx{epoc} = tempConfMtx; - epocSync(epoc) = mean(tempConfMtx(triu(true(size(ripHilb,2))))); + epocSync(epoc) = mean(tempConfMtx(triu(true(size(ripHilb,2)),1))); end %% Organize Spiking Data Based on Total # Spikes @@ -484,23 +637,28 @@ function ExportTrlRips(source, event) epocNsmblAct(epoc) = mean(sum(tempSpkMtx)>=1); end -%% Evaluate Spectrogram +%% Evaluate Spectrogram & Extract Max Power + ripSpect = cell(size(epocWindows,1),1); ripFreq = nan(size(epocWindows,1),1); ripMaxFreq = nan(size(epocWindows,1),1); +epocPow = nan(size(epocWindows,1),1); freqs = [150 250]; +padSize = 25; freqsVect = freqs(1):freqs(2); for e = 1:size(epocWindows,1) - tempSpect = nan(diff(epocWindows(e,:))+101,freqs(2)-freqs(1)+1,size(ripBPF,2)); + curZagg = zAgg(epocWindows(e,1):epocWindows(e,2)); + epocPow(e) = max(curZagg); + tempSpect = nan(diff(epocWindows(e,:))+(padSize*2+1),freqs(2)-freqs(1)+1,size(ripBPF,2)); for t = 1:size(ripBPF,2) - tempSpect(:,:,t) = MorletAG(ripVolts(epocWindows(e,1)-50:epocWindows(e,2)+50,t), 1/samp, freqs(1), freqs(2))'; + tempSpect(:,:,t) = MorletAG(ripVolts(epocWindows(e,1)-padSize:epocWindows(e,2)+padSize,t), 1/samp, freqs(1), freqs(2))'; end - ripSpect{e} = tempSpect; - tempMax = mean(tempSpect(51:end-50,:,:),3); + ripSpect{e} = tempSpect(padSize+1:end-padSize,:,:); + tempMax = mean(ripSpect{e},3); [~,c] = find((tempMax./repmat(max(tempMax,[],2), [1,size(tempMax,2)]))==1); ripFreq(e) = mean(freqsVect(c)); - [~, cM] = find((tempMax./max(max(tempMax)))==1); - ripMaxFreq(e) = freqsVect(cM); + maxPowSpect = tempMax(curZagg==max(curZagg),:); + ripMaxFreq(e) = freqsVect(maxPowSpect==max(maxPowSpect)); end %% Organize Data Output @@ -508,6 +666,7 @@ function ExportTrlRips(source, event) 'TimeStamps', statMatrix(:,1),... 'Ripples', struct('Events', epocWindows, 'Duration', epocDur,... 'Synchrony', epocSync, 'EnsembleActivity', epocNsmblAct,... + 'MaxPower', epocPow,... 'MeanFrequency', ripFreq, 'MaxPowerFrequency', ripMaxFreq),... 'SessionData', struct('RawLFP', ripVolts, 'RipBPF', ripBPF,... 'RipEnv', ripRMS, 'RipPhase', ripHilb, 'TetIDs', {ripTetIDs},... @@ -545,6 +704,8 @@ function ExportTrlRips(source, event) trialRipsDur = cell(size(trialPokeTimes,1),3); trialRipsSync = cell(size(trialPokeTimes,1),3); trialRipsNsmblAct = cell(size(trialPokeTimes,1),3); +trialRipsMaxPow = cell(size(trialPokeTimes,1),3); +trialRipsMaxFreq = cell(size(trialPokeTimes,1),3); for trl = 1:size(trialPokeTimes,1) preTrlLog = rips.Ripples.Events(:,1)>(trialPokeTimes(trl,1)-trialWin(1)) & rips.Ripples.Events(:,1)trialPokeTimes(trl,1) & rips.Ripples.Events(:,1)trialPokeTimes(trl,2) & (rips.Ripples.Events(:,1) Date: Thu, 5 Mar 2020 10:35:08 -0800 Subject: [PATCH 093/133] Create PlotRipFeatsByEvent.m --- Analyses/PlotRipFeatsByEvent.m | 61 ++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Analyses/PlotRipFeatsByEvent.m diff --git a/Analyses/PlotRipFeatsByEvent.m b/Analyses/PlotRipFeatsByEvent.m new file mode 100644 index 0000000..5144eb0 --- /dev/null +++ b/Analyses/PlotRipFeatsByEvent.m @@ -0,0 +1,61 @@ +function PlotRipFeatsByEvent(rips, param) +%% +pokeInNdxs = rips.TrialInfo.TrialPokes(:,1); +pokeOutNdxs = rips.TrialInfo.TrialPokes(:,2); +rwdNdxs = rips.TrialInfo.TrialRewards; + +if length(pokeInNdxs)~=length(rwdNdxs) + rwdNdxs(end:end+(length(pokeInNdxs)-length(rwdNdxs)))=nan; +end + +%% +switch param + case 'Duration' + feat = rips.Ripples.Duration; + case 'Synchrony' + feat = rips.Ripples.Synchrony; + case 'Spiking' + feat = rips.Ripples.EnsembleActivity; + case 'Power' + feat = rips.Ripples.MaxPower; + case 'MaxFreq' + feat = rips.Ripples.MaxPowerFrequency; +end + +%% +piDiffs = cell(length(pokeInNdxs),1); +piFeat = piDiffs; +poDiffs = piDiffs; +poFeat = piDiffs; +rdDiffs = piDiffs; +rdFeat = piDiffs; +for trl = 1:size(rips.TrialInfo.TrialPokes,1) + curPIdiff = rips.Ripples.Events(:,1) - pokeInNdxs(trl); + piLog = curPIdiff>=-500 & curPIdiff<=500; + piDiffs{trl} = curPIdiff(piLog); + piFeat{trl} = feat(piLog); + + curPOdiff = rips.Ripples.Events(:,1) - pokeOutNdxs(trl); + poLog = curPOdiff>=-500 & curPOdiff<=500; + poDiffs{trl} = curPOdiff(poLog); + poFeat{trl} = feat(poLog); + + curRDdiff = rips.Ripples.Events(:,1) - rwdNdxs(trl); + rdLog = curRDdiff>=-500 & curRDdiff<=500; + rdDiffs{trl} = curRDdiff(rdLog); + rdFeat{trl} = feat(rdLog); +end + +%% +figure; +sp1 = subplot(3,1,1); +scatter(cell2mat(piDiffs), cell2mat(piFeat), '*k'); +title(sprintf('Ripple %s Relative to Poke In', param)); +sp2 = subplot(3,1,2); +scatter(cell2mat(poDiffs), cell2mat(poFeat), '*k'); +title(sprintf('Ripple %s Relative to Poke Out', param)); +sp3 = subplot(3,1,3); +scatter(cell2mat(rdDiffs), cell2mat(rdFeat), '*k'); +title(sprintf('Ripple %s Relative to Reward Delivery', param)); +linkaxes([sp1 sp2 sp3], 'xy'); +set(sp3, 'xlim', [-500 500]); \ No newline at end of file From d529888e77c6b0bf4b7a15d5ddea5f0bb2d09942 Mon Sep 17 00:00:00 2001 From: gaelias Date: Thu, 5 Mar 2020 17:05:24 -0800 Subject: [PATCH 094/133] Update CurateRipples2_SM.m --- Analyses/CurateRipples2_SM.m | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Analyses/CurateRipples2_SM.m b/Analyses/CurateRipples2_SM.m index a75f226..e27917c 100644 --- a/Analyses/CurateRipples2_SM.m +++ b/Analyses/CurateRipples2_SM.m @@ -1,6 +1,6 @@ %% CurateRipples clear all -close all +% close all global plotData plotData.listSel = 2; % Used to keep track of which list is being selected from for ripple viewing plotData.Window = 50; @@ -594,6 +594,9 @@ function ExportTrlRips(source, event) mrgdNdxs = false(size(interEpocInterval)); for slr = 1:length(shortLatRipls) nxtNdx = shortLatRipls(slr)+find(slrLog(shortLatRipls(slr)+1:end)==0,1,'first'); + if isempty(nxtNdx) + nxtNdx = shortLatRipls(slr) + 1; + end epocWindows(shortLatRipls(slr),2) = epocWindows(nxtNdx,2); mrgdNdxs(shortLatRipls(slr)+1:nxtNdx) = true; end From 7d32f58c09f2ee6f4b6a482e352018387c4b0d44 Mon Sep 17 00:00:00 2001 From: gabe Date: Mon, 9 Mar 2020 16:08:22 -0700 Subject: [PATCH 095/133] Update CurateRipples2_SM.m --- Analyses/CurateRipples2_SM.m | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Analyses/CurateRipples2_SM.m b/Analyses/CurateRipples2_SM.m index e27917c..ffa8dce 100644 --- a/Analyses/CurateRipples2_SM.m +++ b/Analyses/CurateRipples2_SM.m @@ -532,9 +532,13 @@ function ExportTrlRips(source, event) tetFileLog = cellfun(@(a)~isempty(a), regexp(fileNames, '_T([0-9]*)_SM.mat')); tetFiles = fileNames(tetFileLog)'; behavFileLog = cellfun(@(a)~isempty(a), regexp(fileNames, '_BehaviorMatrix')); -ensembleFileLog = cellfun(@(a)~isempty(a), regexp(fileNames, '_EnsembleMatrix')); load(fileNames{behavFileLog}); -load(fileNames{ensembleFileLog}); +ensembleFileLog = cellfun(@(a)~isempty(a), regexp(fileNames, '_EnsembleMatrix')); +if sum(ensembleFileLog)==0 + ensembleMatrix = zeros(size(behavMatrix,1),2); +else + load(fileNames{ensembleFileLog}); +end behavMatrixTrialStruct = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [0 0], 'PokeIn'); %% Extract Raw Values & Compute RMS Power From 786ab28afca7f5721927c20bd56add6b9d316552 Mon Sep 17 00:00:00 2001 From: gabe Date: Wed, 11 Mar 2020 11:59:31 -0700 Subject: [PATCH 096/133] Update CurateRipples2_SM.m --- Analyses/CurateRipples2_SM.m | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Analyses/CurateRipples2_SM.m b/Analyses/CurateRipples2_SM.m index ffa8dce..5aff6bb 100644 --- a/Analyses/CurateRipples2_SM.m +++ b/Analyses/CurateRipples2_SM.m @@ -653,20 +653,20 @@ function ExportTrlRips(source, event) freqs = [150 250]; padSize = 25; freqsVect = freqs(1):freqs(2); -for e = 1:size(epocWindows,1) - curZagg = zAgg(epocWindows(e,1):epocWindows(e,2)); - epocPow(e) = max(curZagg); - tempSpect = nan(diff(epocWindows(e,:))+(padSize*2+1),freqs(2)-freqs(1)+1,size(ripBPF,2)); - for t = 1:size(ripBPF,2) - tempSpect(:,:,t) = MorletAG(ripVolts(epocWindows(e,1)-padSize:epocWindows(e,2)+padSize,t), 1/samp, freqs(1), freqs(2))'; - end - ripSpect{e} = tempSpect(padSize+1:end-padSize,:,:); - tempMax = mean(ripSpect{e},3); - [~,c] = find((tempMax./repmat(max(tempMax,[],2), [1,size(tempMax,2)]))==1); - ripFreq(e) = mean(freqsVect(c)); - maxPowSpect = tempMax(curZagg==max(curZagg),:); - ripMaxFreq(e) = freqsVect(maxPowSpect==max(maxPowSpect)); -end +% for e = 1:size(epocWindows,1) +% curZagg = zAgg(epocWindows(e,1):epocWindows(e,2)); +% epocPow(e) = max(curZagg); +% tempSpect = nan(diff(epocWindows(e,:))+(padSize*2+1),freqs(2)-freqs(1)+1,size(ripBPF,2)); +% for t = 1:size(ripBPF,2) +% tempSpect(:,:,t) = MorletAG(ripVolts(epocWindows(e,1)-padSize:epocWindows(e,2)+padSize,t), 1/samp, freqs(1), freqs(2))'; +% end +% ripSpect{e} = tempSpect(padSize+1:end-padSize,:,:); +% tempMax = mean(ripSpect{e},3); +% [~,c] = find((tempMax./repmat(max(tempMax,[],2), [1,size(tempMax,2)]))==1); +% ripFreq(e) = mean(freqsVect(c)); +% maxPowSpect = tempMax(curZagg==max(curZagg),:); +% ripMaxFreq(e) = freqsVect(maxPowSpect==max(maxPowSpect)); +% end %% Organize Data Output rips = struct(... From 4c5ff58323fa5d2903217bfd2a6e8bf5181a414a Mon Sep 17 00:00:00 2001 From: gaelias Date: Wed, 11 Mar 2020 13:19:53 -0700 Subject: [PATCH 097/133] Update CurateRipples2_SM.m --- Analyses/CurateRipples2_SM.m | 119 ++++++++++++++++------------------- 1 file changed, 53 insertions(+), 66 deletions(-) diff --git a/Analyses/CurateRipples2_SM.m b/Analyses/CurateRipples2_SM.m index 5aff6bb..36fcf2f 100644 --- a/Analyses/CurateRipples2_SM.m +++ b/Analyses/CurateRipples2_SM.m @@ -1,6 +1,6 @@ %% CurateRipples clear all -% close all +close all global plotData plotData.listSel = 2; % Used to keep track of which list is being selected from for ripple viewing plotData.Window = 50; @@ -17,8 +17,8 @@ %% rips = RippleDetection_SM(envProc, powThresh, durThresh, durThreshMrg, syncThresh, syncWin, smoothWin); [trialRips] = ExtractTrialEventRips_SM(rips, [500 500]); -% allTrialRips = sortrows(cell2mat([trialRips.Events(:,1); trialRips.Events(:,2); trialRips.Events(:,3)])); % Use for ALL (pre-trial, trial and post-trial) Trial Rips -allTrialRips = cell2mat(trialRips.Events(:,3)); % Use for ONLY Post-Trial Rips +trlRipIndices = cell2mat([trialRips.Events(:,1); trialRips.Events(:,2); trialRips.Events(:,3)]); +allTrialRips = sortrows(trlRipIndices); % Use for ALL (pre-trial, trial and post-trial) Trial Rips %% Toss Ripple Features into PlotData % Need to update the selection for TrialRips if not using only Post-Trial @@ -29,11 +29,18 @@ plotData.SessionRips.EnsembleAct = rips.Ripples.EnsembleActivity; plotData.SessionRips.MaxPower = rips.Ripples.MaxPower; plotData.SessionRips.RipFreq = rips.Ripples.MaxPowerFrequency; +ripsPrTrl = sum(cellfun(@(a)size(a,1), trialRips.Events)); +trlPrdID = sortrows([trlRipIndices(:,1), [ones(ripsPrTrl(1),1); ones(ripsPrTrl(2),1)*2; ones(ripsPrTrl(3),1)*3]]); +plotData.TrialRips.TrialPeriodID = trlPrdID(:,2); plotData.TrialRips.Events = allTrialRips; -plotData.TrialRips.Synchrony = cell2mat(trialRips.Synchrony(:,3)); % Use for ONLY Post-Trial Rips -plotData.TrialRips.EnsembleAct = cell2mat(trialRips.EnsembleAct(:,3)); % Use for ONLY Post-Trial Rips -plotData.TrialRips.MaxPower = cell2mat(trialRips.MaxPower(:,3)); % Use for ONLY Post-Trial Rips -plotData.TrialRips.RipFreq = cell2mat(trialRips.RipFreq(:,3)); % Use for ONLY Post-Trial Rips +trlSynch = sortrows([trlRipIndices(:,1), cell2mat([trialRips.Synchrony(:,1); trialRips.Synchrony(:,2); trialRips.Synchrony(:,3)])]); +plotData.TrialRips.Synchrony = trlSynch(:,2); +trlNsmbl = sortrows([trlRipIndices(:,1), cell2mat([trialRips.EnsembleAct(:,1); trialRips.EnsembleAct(:,2); trialRips.EnsembleAct(:,3)])]); +plotData.TrialRips.EnsembleAct = trlNsmbl(:,2); +trlPower = sortrows([trlRipIndices(:,1), cell2mat([trialRips.MaxPower(:,1); trialRips.MaxPower(:,2); trialRips.MaxPower(:,3)])]); +plotData.TrialRips.MaxPower = trlPower(:,2); +trlFreq = sortrows([trlRipIndices(:,1), cell2mat([trialRips.RipFreq(:,1); trialRips.RipFreq(:,2); trialRips.RipFreq(:,3)])]); +plotData.TrialRips.RipFreq = trlFreq(:,2); %% Plot Descriptives PlotNearTrialRipStats(trialRips) @@ -188,35 +195,6 @@ %% Initialize Things SetPlots; -%% Plot Trial Periods -% for trl = 1:size(rips.TrialInfo.TrialPokes,1) -% switch rips.TrialInfo.OdorVect(trl) -% case 1 -% patchColor = [44/255 168/255 224/255]; -% case 2 -% patchColor = [154/255 133/255 122/255]; -% case 3 -% patchColor = [9/255 161/255 74/255]; -% case 4 -% patchColor = [128/255 66/255 151/255]; -% case 5 -% patchColor = [241/255 103/255 36/255]; -% end -% patch(rawAxes, 'XData', [rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)),rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1))],... -% 'YData', plotData.FigLims.Raw(:),... -% 'FaceColor', patchColor, 'FaceAlpha', 0.5,... -% 'EdgeColor', patchColor, 'EdgeAlpha', 0.5); -% patch(bpfAxes, 'XData', [rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)),rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1))],... -% 'YData', plotData.FigLims.BPF(:),... -% 'FaceColor', patchColor, 'FaceAlpha', 0.5,... -% 'EdgeColor', patchColor, 'EdgeAlpha', 0.5); -% patch(spkAxes, 'XData', [rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)), rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,2)),rips.TimeStamps(rips.TrialInfo.TrialPokes(trl,1))],... -% 'YData', plotData.FigLims.Spk(:),... -% 'FaceColor', patchColor, 'FaceAlpha', 0.5,... -% 'EdgeColor', patchColor, 'EdgeAlpha', 0.5); -% end - - %% Callbacks/Functions function SetPlots global plotData @@ -363,6 +341,8 @@ function PlayRip(source, event) % end curSound = audioplayer(curWave, 1000); playblocking(curSound); +% Below code is an attempt to visualize what's being played... it didn't +% work % curWaveTime = plotData.rawPlot(tetID).XData; % for t = 1:length(curWaveTime) % set(plotData.audMrk, 'XData', [curWaveTime(t) curWaveTime(t)]); @@ -464,19 +444,28 @@ function ExportSsnRips(source, event) function ExportTrlRips(source, event) global plotData -ripMatrix(:,1) = plotData.ripCure.UserData(:,1); -ripMatrix(:,2) = false(size(ripMatrix,1),1); -ripMatrixColIDs = [{'TimeBin'}, {'Trial_Ripple_Log'}]; -ripFeats = [plotData.trlRipList.UserData, plotData.TrialRips.Synchrony,... - plotData.TrialRips.EnsembleAct, plotData.TrialRips.MaxPower,... - plotData.TrialRips.RipFreq]; -ripFeatsColIDs = [{'RippleStartIndex'}, {'RippleEndIndex'},... - {'RippleSynchrony'}, {'RipplePercentActiveCells'}, {'RipplePower'},... - {'RippleFrequency'}]; -for e = 1:size(ripFeats, 1) - ripMatrix(ripFeats(e,1):ripFeats(e,2),2) = true; +for t = 1:3 + curTrialRipLog = plotData.TrialRips.TrialPeriodID==t; + ripMatrixColIDs = [{'TimeBin'}, {'Trial_Ripple_Log'}]; + ripFeats = [plotData.trlRipList.UserData(curTrialRipLog,:), plotData.TrialRips.Synchrony(curTrialRipLog,:),... + plotData.TrialRips.EnsembleAct(curTrialRipLog,:), plotData.TrialRips.MaxPower(curTrialRipLog,:),... + plotData.TrialRips.RipFreq(curTrialRipLog,:)]; + ripFeatsColIDs = [{'RippleStartIndex'}, {'RippleEndIndex'},... + {'RippleSynchrony'}, {'RipplePercentActiveCells'}, {'RipplePower'},... + {'RippleFrequency'}]; + ripMatrix(:,1) = plotData.ripCure.UserData(:,1); + ripMatrix(:,2) = false(size(ripMatrix,1),1); + for e = 1:size(ripFeats, 1) + ripMatrix(ripFeats(e,1):ripFeats(e,2),2) = true; + end + if t==1 + save('Pre-TrialRipples.mat', 'ripMatrix', 'ripMatrixColIDs', 'ripFeats', 'ripFeatsColIDs'); + elseif t==2 + save('In-TrialRipples.mat', 'ripMatrix', 'ripMatrixColIDs', 'ripFeats', 'ripFeatsColIDs'); + elseif t==3 + save('Post-TrialRipples.mat', 'ripMatrix', 'ripMatrixColIDs', 'ripFeats', 'ripFeatsColIDs'); + end end -save('TrialRipples.mat', 'ripMatrix', 'ripMatrixColIDs', 'ripFeats', 'ripFeatsColIDs'); msgbox('Trial Ripple Indices Saved'); end @@ -505,8 +494,8 @@ function ExportTrlRips(source, event) %#ok<*IDISVAR,*NODEF,*USENS,*NASGU,*COLND> %% if nargin == 0 - envProc = 'RMS'; % Enable for RMS determination of envelope - % envProc = 'HILB'; % Enable for abs(Hilbert) determinination of envelope +% envProc = 'RMS'; % Enable for RMS determination of envelope + envProc = 'HILB'; % Enable for abs(Hilbert) determinination of envelope powThresh = [0 4]; durThresh = 15; % Duration Threshold durThreshMrg = 15; @@ -569,8 +558,6 @@ function ExportTrlRips(source, event) % Aggregate Power aggPower = mean(ripRMS,2); % Mean envelope zAgg = zscore(aggPower); -% aggPower = zscore(mean(ripRMS,2)); % Z-Score Mean envelope -% aggPower = mean(zscore(ripRMS),2); % Mean Z-Score envelope % Threshold Based on Mean +/- STD Aggregate Power rmsThresh1 = (mean(aggPower) + (powThresh(1)*std(aggPower))); @@ -653,20 +640,20 @@ function ExportTrlRips(source, event) freqs = [150 250]; padSize = 25; freqsVect = freqs(1):freqs(2); -% for e = 1:size(epocWindows,1) -% curZagg = zAgg(epocWindows(e,1):epocWindows(e,2)); -% epocPow(e) = max(curZagg); -% tempSpect = nan(diff(epocWindows(e,:))+(padSize*2+1),freqs(2)-freqs(1)+1,size(ripBPF,2)); -% for t = 1:size(ripBPF,2) -% tempSpect(:,:,t) = MorletAG(ripVolts(epocWindows(e,1)-padSize:epocWindows(e,2)+padSize,t), 1/samp, freqs(1), freqs(2))'; -% end -% ripSpect{e} = tempSpect(padSize+1:end-padSize,:,:); -% tempMax = mean(ripSpect{e},3); -% [~,c] = find((tempMax./repmat(max(tempMax,[],2), [1,size(tempMax,2)]))==1); -% ripFreq(e) = mean(freqsVect(c)); -% maxPowSpect = tempMax(curZagg==max(curZagg),:); -% ripMaxFreq(e) = freqsVect(maxPowSpect==max(maxPowSpect)); -% end +for e = 1:size(epocWindows,1) + curZagg = zAgg(epocWindows(e,1):epocWindows(e,2)); + epocPow(e) = max(curZagg); + tempSpect = nan(diff(epocWindows(e,:))+(padSize*2+1),freqs(2)-freqs(1)+1,size(ripBPF,2)); + for t = 1:size(ripBPF,2) + tempSpect(:,:,t) = MorletAG(ripVolts(epocWindows(e,1)-padSize:epocWindows(e,2)+padSize,t), 1/samp, freqs(1), freqs(2))'; + end + ripSpect{e} = tempSpect(padSize+1:end-padSize,:,:); + tempMax = mean(ripSpect{e},3); + [~,c] = find((tempMax./repmat(max(tempMax,[],2), [1,size(tempMax,2)]))==1); + ripFreq(e) = mean(freqsVect(c)); + maxPowSpect = tempMax(curZagg==max(curZagg),:); + ripMaxFreq(e) = freqsVect(maxPowSpect==max(maxPowSpect)); +end %% Organize Data Output rips = struct(... From 861b0079906cdb7b95b00286da6f9ea389d9f919 Mon Sep 17 00:00:00 2001 From: gaelias Date: Wed, 11 Mar 2020 13:19:57 -0700 Subject: [PATCH 098/133] Update BayesPFCproto_VALID.m --- Analyses/Prototypes/BayesPFCproto_VALID.m | 103 +++++++++++++++++++--- 1 file changed, 89 insertions(+), 14 deletions(-) diff --git a/Analyses/Prototypes/BayesPFCproto_VALID.m b/Analyses/Prototypes/BayesPFCproto_VALID.m index 9940d55..13e4a79 100644 --- a/Analyses/Prototypes/BayesPFCproto_VALID.m +++ b/Analyses/Prototypes/BayesPFCproto_VALID.m @@ -3,11 +3,11 @@ clear all %% Runtime variables -window = [-0.4 0.75]; -binSize = 20; -dsRate = 20; +window = [-0.2 0.2]; +binSize = 50; +dsRate = 5; cLims = [0 0.005]; -withdrawTime = 1; +withdrawTime = 1.2; %% smPath = uigetdir; cd(smPath); @@ -79,14 +79,37 @@ timeIndexes = repmat(trialTime,[4,1]); odorIndexes = cell2mat(odorCell); -xTickNdxs = find(timeIndexes==0 | timeIndexes==min(trialTime(trialTime>=window(1))) | timeIndexes==min(trialTime(trialTime>=withdrawTime))); +xTickNdxs = find(timeIndexes==trialTime(find(trialTime>max(trialTime)/2,1,'first'))); odorDivs = find(timeIndexes==min(trialTime(trialTime>=window(1)))); odorTargs = find(timeIndexes==min(trialTime(trialTime>=withdrawTime))); odorStarts = find(timeIndexes==0); +noSpikeLog = sum(fullyISlikely)==0; +spikeMatrix(:,noSpikeLog,:) = []; +fullyISlikely(:,noSpikeLog) = []; + +%% +figure; +imagesc(1:length(timeIndexes), 1:size(fullyISlikely,2), fullyISlikely'); +hold on; +set(gca, 'ydir', 'normal', 'xTick', find(timeIndexes==trialTime(find(trialTime>max(trialTime)/2,1,'first'))),... + 'xticklabel', [{'A'}, {'B'}, {'C'}, {'D'}], 'TickDir', 'out'); +for p = 1:4 + plot([odorDivs(p) odorDivs(p)], get(gca, 'ylim'), '-w', 'linewidth', 2); + plot([odorTargs(p) odorTargs(p)], get(gca, 'ylim'), ':w', 'linewidth', 1.5); + plot([odorStarts(p) odorStarts(p)], get(gca, 'ylim'), '--w', 'linewidth', 1.5); +end +title('Likelihoods (PSTH)'); + +annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... + 'linestyle', 'none', 'horizontalalignment', 'right'); +curDir = cd; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); + %% figure -subplot(4,5,1:3); +spA = subplot(4,5,1:3); aPost = CalcStaticBayesPost(fullyISlikely, spikeMatrix(:,:,[trialPeriodTD.Odor]==1 & ~fullInSeqLog & perfLog & inSeqLog), binSize); imagesc(1:length(timeIndexes), trialTime, nanmean(aPost,3),cLims); hold on; @@ -107,7 +130,7 @@ aDecodePrcnts(t,4) = sum(aDecode(t,:)==4)/sum(~isnan(aDecode(t,:))); end title('Probability Density'); -subplot(4,5,4); +curBarA = subplot(4,5,4); curBar = barh(trialTime,aDecodePrcnts, 1, 'stacked'); curBar(1).FaceColor = [44/255 168/255 224/255]; curBar(1).EdgeColor = 'none'; curBar(2).FaceColor = [154/255 133/255 122/255]; curBar(2).EdgeColor = 'none'; @@ -136,7 +159,7 @@ title('Time Decoding'); drawnow; -subplot(4,5,6:8); +spB = subplot(4,5,6:8); bPost = CalcStaticBayesPost(fullyISlikely, spikeMatrix(:,:,[trialPeriodTD.Odor]==2 & ~fullInSeqLog & perfLog & inSeqLog), binSize); imagesc(1:length(timeIndexes), trialTime, nanmean(bPost,3),cLims); hold on; @@ -156,7 +179,7 @@ bDecodePrcnts(t,3) = sum(bDecode(t,:)==3)/sum(~isnan(bDecode(t,:))); bDecodePrcnts(t,4) = sum(bDecode(t,:)==4)/sum(~isnan(bDecode(t,:))); end -subplot(4,5,9); +curBarB = subplot(4,5,9); curBar = barh(trialTime,bDecodePrcnts, 1, 'stacked'); curBar(1).FaceColor = [44/255 168/255 224/255]; curBar(1).EdgeColor = 'none'; curBar(2).FaceColor = [154/255 133/255 122/255]; curBar(2).EdgeColor = 'none'; @@ -182,7 +205,7 @@ plot([0 0], get(gca, 'ylim'), ':k', 'linewidth', 1.5); drawnow; -subplot(4,5,11:13); +spC = subplot(4,5,11:13); cPost = CalcStaticBayesPost(fullyISlikely, spikeMatrix(:,:,[trialPeriodTD.Odor]==3 & ~fullInSeqLog & perfLog & inSeqLog), binSize); imagesc(1:length(timeIndexes), trialTime, nanmean(cPost,3),cLims); hold on; @@ -202,7 +225,7 @@ cDecodePrcnts(t,3) = sum(cDecode(t,:)==3)/sum(~isnan(cDecode(t,:))); cDecodePrcnts(t,4) = sum(cDecode(t,:)==4)/sum(~isnan(cDecode(t,:))); end -subplot(4,5,14); +curBarC = subplot(4,5,14); curBar = barh(trialTime,cDecodePrcnts, 1, 'stacked'); curBar(1).FaceColor = [44/255 168/255 224/255]; curBar(1).EdgeColor = 'none'; curBar(2).FaceColor = [154/255 133/255 122/255]; curBar(2).EdgeColor = 'none'; @@ -228,11 +251,11 @@ plot([0 0], get(gca, 'ylim'), ':k', 'linewidth', 1.5); drawnow; -subplot(4,5,16:18); +spD = subplot(4,5,16:18); dPost = CalcStaticBayesPost(fullyISlikely, spikeMatrix(:,:,[trialPeriodTD.Odor]==4 & ~fullInSeqLog & perfLog & inSeqLog), binSize); imagesc(1:length(timeIndexes), trialTime, nanmean(dPost,3),cLims); hold on; -set(gca, 'ydir', 'normal', 'xtick', xTickNdxs, 'xticklabel', repmat([window(1) 0 withdrawTime],[1,4]), 'TickDir', 'out'); +set(gca, 'ydir', 'normal', 'xtick', xTickNdxs, 'xticklabel', [{'A'}, {'B'}, {'C'}, {'D'}], 'TickDir', 'out'); for p = 1:4 plot([odorDivs(p) odorDivs(p)], get(gca, 'ylim'), '-w', 'linewidth', 2); plot([odorTargs(p) odorTargs(p)], get(gca, 'ylim'), ':w', 'linewidth', 1.5); @@ -248,7 +271,7 @@ dDecodePrcnts(t,3) = sum(dDecode(t,:)==3)/sum(~isnan(dDecode(t,:))); dDecodePrcnts(t,4) = sum(dDecode(t,:)==4)/sum(~isnan(dDecode(t,:))); end -subplot(4,5,19); +curBarD = subplot(4,5,19); curBar = barh(trialTime,dDecodePrcnts, 1, 'stacked'); curBar(1).FaceColor = [44/255 168/255 224/255]; curBar(1).EdgeColor = 'none'; curBar(2).FaceColor = [154/255 133/255 122/255]; curBar(2).EdgeColor = 'none'; @@ -276,6 +299,58 @@ colormap jet +linkaxes([curBarA, curBarB, curBarC, curBarD], 'xy'); +linkaxes([spA, spB, spC, spD], 'xy'); +linkaxes([curBarA, spA], 'y'); +axis(curBarA, 'tight'); + +annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... + 'linestyle', 'none', 'horizontalalignment', 'right'); +curDir = cd; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); + +aDecodeSmooth = aDecodePrcnts; +bDecodeSmooth = bDecodePrcnts; +cDecodeSmooth = cDecodePrcnts; +dDecodeSmooth = dDecodePrcnts; +for c = 1:4 + aDecodeSmooth(:,c) = smooth(aDecodePrcnts(:,c),10); + bDecodeSmooth(:,c) = smooth(bDecodePrcnts(:,c),10); + cDecodeSmooth(:,c) = smooth(cDecodePrcnts(:,c),10); + dDecodeSmooth(:,c) = smooth(dDecodePrcnts(:,c),10); +end +figure; +sp1 = subplot(4,1,1); +aPlots = plot(trialTime,aDecodeSmooth); +aPlots(1).Color = [44/255 168/255 224/255]; +aPlots(1).LineWidth = 2; +aPlots(2).Color = [154/255 133/255 122/255]; +aPlots(3).Color = [9/255 161/255 74/255]; +aPlots(4).Color = [128/255 66/255 151/255]; +sp2 = subplot(4,1,2); +bPlots = plot(trialTime,bDecodeSmooth); +bPlots(1).Color = [44/255 168/255 224/255]; +bPlots(2).Color = [154/255 133/255 122/255]; +bPlots(2).LineWidth = 2; +bPlots(3).Color = [9/255 161/255 74/255]; +bPlots(4).Color = [128/255 66/255 151/255]; +sp3 = subplot(4,1,3); +cPlots = plot(trialTime,cDecodeSmooth); +cPlots(1).Color = [44/255 168/255 224/255]; +cPlots(2).Color = [154/255 133/255 122/255]; +cPlots(3).Color = [9/255 161/255 74/255]; +cPlots(3).LineWidth = 2; +cPlots(4).Color = [128/255 66/255 151/255]; +sp4 = subplot(4,1,4); +dPlots = plot(trialTime,dDecodeSmooth); +dPlots(1).Color = [44/255 168/255 224/255]; +dPlots(2).Color = [154/255 133/255 122/255]; +dPlots(3).Color = [9/255 161/255 74/255]; +dPlots(4).Color = [128/255 66/255 151/255]; +dPlots(4).LineWidth = 2; + +linkaxes([sp1 sp2 sp3 sp4], 'xy'); annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... 'linestyle', 'none', 'horizontalalignment', 'right'); curDir = cd; From 2168791d6752c937b4e4eced55508b0ce8a084eb Mon Sep 17 00:00:00 2001 From: gaelias Date: Thu, 23 Apr 2020 14:19:45 -0700 Subject: [PATCH 099/133] Update CreateBehaviorMatrixPFCabbr.m --- .../CreateBehaviorMatrixPFCabbr.m | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/statMatrix Creation/CreateBehaviorMatrixPFCabbr.m b/statMatrix Creation/CreateBehaviorMatrixPFCabbr.m index 4aa7afd..791fe28 100644 --- a/statMatrix Creation/CreateBehaviorMatrixPFCabbr.m +++ b/statMatrix Creation/CreateBehaviorMatrixPFCabbr.m @@ -1,5 +1,16 @@ function CreateBehaviorMatrixPFCabbr -[plxData] = SummarizePLXevents_SD; +[fileName, path] = uigetfile('.plx','Identify .PLX File'); +if fileName == 0 + disp('No file selected, analysis cancelled') + return +end +plxFile = [path fileName]; +[path, fileName] = fileparts(plxFile); +path = [path '\']; +cd(path); +outfile = fopen(sprintf('%s_PLXeventSummary.txt', fileName), 'a+'); + +[plxData] = SummarizePLXevents_SD(plxFile, [], outfile); chan = 1; [samp, ~, tetTS, fn, ~] = plx_ad_v(plxData.Summary.PLXfile, chan); @@ -33,21 +44,21 @@ posPresTimes = [plxData.Raw([plxData.Raw.OrdinalPosition]==pos).ItemPresentationTime]; behVals(:,pos) = histcounts(posPresTimes, tsVect)'; behDataHeaders{pos} = ['Position' num2str(pos)]; -% fprintf(outfile, ' Position #%i = %i trials\n', pos, length(posPresTimes)); + fprintf(outfile, ' Position #%i = %i trials\n', pos, length(posPresTimes)); end % fprintf(outfile, 'Odor Counts\n'); for seq = 1:seqLength itemPresTimes = [plxData.Raw([plxData.Raw.SequenceItem]==seq).ItemPresentationTime]; behVals(:,seq+maxSeqLength) = histcounts(itemPresTimes, tsVect)'; behDataHeaders{seq+maxSeqLength} = ['Odor' num2str(seq)]; -% fprintf(outfile, ' Odor #%i = %i trials\n', seq, length(itemPresTimes)); + fprintf(outfile, ' Odor #%i = %i trials\n', seq, length(itemPresTimes)); end if isfield(plxData.Summary, 'DualListLog') && plxData.Summary.DualListLog for seq = 11:seqLength+10 itemPresTimes = [plxData.Raw([plxData.Raw.SequenceItem]==seq).ItemPresentationTime]; behVals(:,seq+seqLength+maxSeqLength-10) = histcounts(itemPresTimes, tsVect)'; behDataHeaders{seq+seqLength+maxSeqLength-10} = ['Odor' num2str(seq)]; -% fprintf(outfile, ' Odor #%i = %i trials\n', seq, length(itemPresTimes)); + fprintf(outfile, ' Odor #%i = %i trials\n', seq, length(itemPresTimes)); end end @@ -55,7 +66,7 @@ outSeqOdorPres = [plxData.Raw(~([plxData.Raw.TranspositionDistance]==0)).ItemPresentationTime]; behVals(:,behPad+1) = histcounts(inSeqOdorPres, tsVect)' - histcounts(outSeqOdorPres,tsVect)'; behDataHeaders{behPad+1} = 'InSeqLog'; -% fprintf(outfile, '\nCompiling InSeq trials.....\n %i trials were InSeq (%i%%)\n', length(inSeqOdorPres), round(length(inSeqOdorPres)/length(plxData.Raw),2)*100); +fprintf(outfile, '\nCompiling InSeq trials.....\n %i trials were InSeq (%i%%)\n', length(inSeqOdorPres), round(length(inSeqOdorPres)/length(plxData.Raw),2)*100); itmPresTimes = [plxData.Raw.ItemPresentationTime]; trialPerformance = [plxData.Raw.Performance]; corrTrials = itmPresTimes(logical(trialPerformance)); @@ -64,7 +75,7 @@ inCorTrlHistCounts = histcounts(inCorrTrials, tsVect)'; behVals(:,behPad+2) = corTrlHistCounts + (inCorTrlHistCounts*-1); behDataHeaders{behPad+2} = 'PerformanceLog'; -% fprintf(outfile, 'Compiling Performance.....\n %i trials were correct (%i%%)\n', sum(trialPerformance), round(mean(trialPerformance),2)*100); +fprintf(outfile, 'Compiling Performance.....\n %i trials were correct (%i%%)\n', sum(trialPerformance), round(mean(trialPerformance),2)*100); behVals(:,behPad+3) = histcounts([plxData.Raw.OdorTrigPokeTime], tsVect)' - histcounts([plxData.Raw.OdorPokeWithdrawTime], tsVect)'; behDataHeaders{behPad+3} = 'PokeEvents'; @@ -138,5 +149,5 @@ behavMatrixColIDs = [{'TimeBin'}, behDataHeaders]; save([plxData.Summary.PLXfile(1:end-4) '_BehaviorMatrix.mat'], 'behavMatrix', 'behavMatrixColIDs', 'plxData'); disp('Behavior data saved.'); -% fprintf(outfile, 'Behavior Matrix saved as %s_BehaviorMatrix.mat\n', outputFileName{1}); +fprintf(outfile, 'Behavior Matrix saved as %s_BehaviorMatrix.mat\n', plxData.Summary.PLXfile(1:end-4)); end \ No newline at end of file From b947f312a952fb742c8c61851540444733c77048 Mon Sep 17 00:00:00 2001 From: gaelias Date: Thu, 23 Apr 2020 14:19:50 -0700 Subject: [PATCH 100/133] Update SummarizePLXevents_SD.m --- .../Supporting Code/SummarizePLXevents_SD.m | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m b/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m index 2b5cb46..e392609 100644 --- a/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m +++ b/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m @@ -32,7 +32,7 @@ matFile = [path fileNames{matFileLog}]; [~, matFileName] = fileparts(matFile); end - outfile = fopen(sprintf('%s_PLXeventSummary.txt', matFileName), 'At'); + outfile = fopen(sprintf('%s_PLXeventSummary.txt', matFileName), 'a+'); end if isempty(matFile) [path, fileName] = fileparts(plxFile); @@ -53,10 +53,10 @@ end end if isempty(outfile) - outfile = fopen(sprintf('%s_PLXeventSummary.txt', matFileName), 'At'); + outfile = fopen(sprintf('%s_PLXeventSummary.txt', matFileName), 'a+'); end else - outfile = fopen(sprintf('%s_PLXeventSummary.txt', matFileName), 'At'); %#ok + outfile = fopen(sprintf('%s_PLXeventSummary.txt', matFileName), 'a+'); %#ok end plxData.Summary.MATfile = matFileName; plxData.Summary.PLXfile = [fileName '.plx']; @@ -424,14 +424,27 @@ aniPosition(nonPositionLog,:) = []; %% Identify and correct issues caused by asynchronous starts of Plexon and Matlab -if odorPresTime{1}(1) plxSession(trl).TargetDuration) if isfield(ssnData(1).Settings, 'GracePeriodDur') && plxSession(trl).TargetDuration - plxSession(trl).PokeDuration > ssnData(trl).Settings.GracePeriodDur fprintf(outfile, 'Trial #%i: Reward signal time occurred before target duration elapsed\n', trl); - error('Trial #%i: Reward signal time occurred before target duration elapsed', trl); +% error('Trial #%i: Reward signal time occurred before target duration elapsed', trl); end end % Check to ensure reward was presented AFTER the reward signal From 9890aa41f56397162ad0f56acd6840ceed29e3b3 Mon Sep 17 00:00:00 2001 From: gaelias Date: Fri, 22 May 2020 10:58:18 -0700 Subject: [PATCH 101/133] Create DualListMeanPlots_PROTO.m --- Analyses/Prototypes/DualListMeanPlots_PROTO.m | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Analyses/Prototypes/DualListMeanPlots_PROTO.m diff --git a/Analyses/Prototypes/DualListMeanPlots_PROTO.m b/Analyses/Prototypes/DualListMeanPlots_PROTO.m new file mode 100644 index 0000000..1476758 --- /dev/null +++ b/Analyses/Prototypes/DualListMeanPlots_PROTO.m @@ -0,0 +1,31 @@ +odor = [bmts.Odor]; +pos = [bmts.Position]; +performance = [bmts.Performance]; +isLog = [bmts.TranspositionDistance]==0; + +for t = 1:length(ensembleMatrixColIDs)-1 + figure; + sp = nan(1,4); + for o = 1:4 + curOdorFam = trialOrgData(odor==o & isLog==1 & performance==1); + curFam = mean(cell2mat(cellfun(@(a)conv(a(:,t),gausswin(200)./max(gausswin(200)),'same'),curOdorFam, 'uniformoutput',0)),2); + curOdorNov = trialOrgData(odor==o+10 & isLog==1 & performance==1); + curNov = mean(cell2mat(cellfun(@(a)conv(a(:,t),gausswin(200)./max(gausswin(200)),'same'),curOdorNov, 'uniformoutput',0)),2); + sp(o) = subplot(1,4,o); + plot(-500:1750, curFam, '-k'); + hold on; + plot(-500:1750, curNov, '--k'); + title(sprintf('Position %i', o)); + xlabel('Time From Poke In (ms)') + end + linkaxes(sp, 'xy'); + legend('Familiar', 'Novel') + for o = 1:4 + plot(sp(o), [0 0], get(sp(o), 'ylim'), '-k'); + end + axis tight; + annotation(gcf,'textbox', [0 0.93 1 0.05],'String', ensembleMatrixColIDs(t+1),... + 'FontWeight','bold', 'FontSize',12, 'edgecolor', 'none', 'horizontalalignment', 'left'); +end + + From 98ef23067ee9994f68e01ee159f0f6fb912d9b42 Mon Sep 17 00:00:00 2001 From: gaelias Date: Wed, 1 Jul 2020 22:10:03 -0700 Subject: [PATCH 102/133] Delete EnsembleCompilation_SM.m --- Data Management/EnsembleCompilation_SM.m | 47 ------------------------ 1 file changed, 47 deletions(-) delete mode 100644 Data Management/EnsembleCompilation_SM.m diff --git a/Data Management/EnsembleCompilation_SM.m b/Data Management/EnsembleCompilation_SM.m deleted file mode 100644 index 0ff0c87..0000000 --- a/Data Management/EnsembleCompilation_SM.m +++ /dev/null @@ -1,47 +0,0 @@ -function EnsembleCompilation_SM -%% EnsembleCompilation_SM -% Code to extract all the unit activity from a set of files in the -% statMatrix format FROM THE SAME SESSION and compile them into a common -% variable -% -% 02/14/2018 - Created by GE -% -%% Extract Info and bands and shit idk -origDir = cd; -[fileDir] = uigetdir(origDir); -if fileDir==0 - disp('Analysis Cancelled') - return -else - cd(fileDir) -end -dirContents = dir(fileDir); -fileNames = {dirContents.name}; -tetFileLog = cellfun(@(a)~isempty(a), regexp(fileNames, '_T([0-9]*).mat')); -tetFiles = fileNames(tetFileLog)'; -load(tetFiles{1}, 'statMatrixColIDs'); - -%% Compile everything! -ensembleMatrix = cell(1,length(tetFiles)); -ensembleMatrixColIDs = cell(1,length(tetFiles)); -ensembleUnitSummaries = cell(1,length(tetFiles)); -for tet = 1:length(tetFiles) - load(tetFiles{tet}); - ensembleUnitSummaries{tet} = unitSummary; - if tet == 1 - tsVect = statMatrix(:,1); %#ok - end - statMatrixColIDs = statMatrixColIDs(cellfun(@(a)~isempty(a), statMatrixColIDs)); - uniColLog = cellfun(@(a)~isempty(a), regexp(statMatrixColIDs, '-U([0-9]*)')); - ensembleMatrix{tet} = statMatrix(:,uniColLog); - ensembleMatrixColIDs{tet} = statMatrixColIDs(uniColLog); - fprintf('%s Complete\n', tetFiles{tet}); -end -%% -ensembleMatrix = [tsVect, cell2mat(ensembleMatrix)]; %#ok -ensembleMatrixColIDs = [{'TimeBin'}, ensembleMatrixColIDs{:}]; %#ok -ensembleUnitSummaries = cell2mat(ensembleUnitSummaries); %#ok - -save('EnsembleMatrix.mat', 'ensembleMatrix', 'ensembleMatrixColIDs', 'ensembleUnitSummaries', '-v7.3'); - - From 52f99d80905b6c16ddaea73a0ab4a71f131a37f0 Mon Sep 17 00:00:00 2001 From: gaelias Date: Wed, 1 Jul 2020 22:10:16 -0700 Subject: [PATCH 103/133] Create EnsembleCompilation_SM.m Moved to "old" --- Data Management/Old/EnsembleCompilation_SM.m | 47 ++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Data Management/Old/EnsembleCompilation_SM.m diff --git a/Data Management/Old/EnsembleCompilation_SM.m b/Data Management/Old/EnsembleCompilation_SM.m new file mode 100644 index 0000000..231849a --- /dev/null +++ b/Data Management/Old/EnsembleCompilation_SM.m @@ -0,0 +1,47 @@ +function EnsembleCompilation_SM +%% EnsembleCompilation_SM +% Code to extract all the unit activity from a set of files in the +% statMatrix format FROM THE SAME SESSION and compile them into a common +% variable +% +% 02/14/2018 - Created by GE +% +%% Extract Info and bands and shit idk +origDir = cd; +[fileDir] = uigetdir(origDir); +if fileDir==0 + disp('Analysis Cancelled') + return +else + cd(fileDir) +end +dirContents = dir(fileDir); +fileNames = {dirContents.name}; +tetFileLog = cellfun(@(a)~isempty(a), regexp(fileNames, '_T([0-9]*).mat')); +tetFiles = fileNames(tetFileLog)'; +load(tetFiles{1}, 'statMatrixColIDs'); + +%% Compile everything! +ensembleMatrix = cell(1,length(tetFiles)); +ensembleMatrixColIDs = cell(1,length(tetFiles)); +ensembleUnitSummaries = cell(1,length(tetFiles)); +for tet = 1:length(tetFiles) + load(tetFiles{tet}); + ensembleUnitSummaries{tet} = unitSummary; + if tet == 1 + tsVect = statMatrix(:,1); %#ok + end + statMatrixColIDs = statMatrixColIDs(cellfun(@(a)~isempty(a), statMatrixColIDs)); + uniColLog = cellfun(@(a)~isempty(a), regexp(statMatrixColIDs, '-U([0-9]*)')); + ensembleMatrix{tet} = statMatrix(:,uniColLog); + ensembleMatrixColIDs{tet} = statMatrixColIDs(uniColLog); + fprintf('%s Complete\n', tetFiles{tet}); +end +%% +ensembleMatrix = [tsVect, cell2mat(ensembleMatrix)]; %#ok +ensembleMatrixColIDs = [{'TimeBin'}, ensembleMatrixColIDs{:}]; %#ok +ensembleUnitSummaries = cell2mat(ensembleUnitSummaries); %#ok + +save('EnsembleMatrix.mat', 'ensembleMatrix', 'ensembleMatrixColIDs', 'ensembleUnitSummaries', '-v7.3'); + + From b608a200b7577a10a8e9636c84446428c481a4ae Mon Sep 17 00:00:00 2001 From: gaelias Date: Wed, 1 Jul 2020 22:11:07 -0700 Subject: [PATCH 104/133] Create OrganizeExtractTrialPeriod_SM.m New compact data management script --- Data Management/OrganizeExtractTrialPeriod_SM.m | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Data Management/OrganizeExtractTrialPeriod_SM.m diff --git a/Data Management/OrganizeExtractTrialPeriod_SM.m b/Data Management/OrganizeExtractTrialPeriod_SM.m new file mode 100644 index 0000000..7281c5a --- /dev/null +++ b/Data Management/OrganizeExtractTrialPeriod_SM.m @@ -0,0 +1,14 @@ +function behavTrialMtx = OrganizeExtractTrialPeriod_SM(behavMatrix, behavMatrixColIDs, statMatrix, preStartDur, postEndDur) +% Combines the Organize and Extract functions to pull out trial data from +% the statMatrix input within the window specified by preStartDur and +% postEndDur reflecting the amount of time prior to trial initiation and +% after trial end you want extracted. + +sampleRate = 1/mode(diff(behavMatrix(:,1))); +trlWindow = [round(preStartDur*sampleRate) round(postEndDur*sampleRate)]; + +behavMatrixTrialStruct = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [0 0], 'PokeIn'); +behavTrialMtx = rmfield(behavMatrixTrialStruct, 'TrialLogVect'); +for t = 1:length(behavMatrixTrialStruct) + behavTrialMtx(t).StatMatrixData = statMatrix(behavMatrixTrialStruct(t).PokeInIndex - trlWindow(1) : behavMatrixTrialStruct(t).PokeOutIndex + trlWindow(2),:); +end \ No newline at end of file From afe83d1841771ec0f99417429483243d10bd1392 Mon Sep 17 00:00:00 2001 From: gaelias Date: Wed, 1 Jul 2020 22:11:39 -0700 Subject: [PATCH 105/133] Create CreateOrientationMatrix.m Thought I had this previously... apparently I never made it? --- statMatrix Creation/CreateOrientationMatrix.m | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 statMatrix Creation/CreateOrientationMatrix.m diff --git a/statMatrix Creation/CreateOrientationMatrix.m b/statMatrix Creation/CreateOrientationMatrix.m new file mode 100644 index 0000000..8790485 --- /dev/null +++ b/statMatrix Creation/CreateOrientationMatrix.m @@ -0,0 +1,114 @@ +function CreateOrientationMatrix + +origDir = cd; +[fileDir] = uigetdir(origDir); +if fileDir==0 + disp('Analysis Cancelled') + return +else + cd(fileDir) +end +dirContents = dir(fileDir); +fileNames = {dirContents.name}; + +%% Load BehaviorMatrix and Orientation_Data Files +load(fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'BehaviorMatrix'))}); +load(fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'Orientation_Data'))}); + +%% Compare the timestamps from the behavMatrix and orientation_data files. +% Since the timestamps used in the statMatrix files is constructed based +% on the LFP sample rate they don't match up identically with the frame +% indices taken from the .AVI file. Therefore we are expecting some +% discrepancy to exist between the video timestamps ('FrameTimestamp' +% column) and the behavMatrix timestamps ('TimeBin' column), but that +% discrepancy should be very small. +frameIndices = [orientData.FileIndices{:,strcmp(orientData.FileIndicesColIDs, 'FrameIndex')}]; +frameTimestamps = [orientData.FileIndices{:,strcmp(orientData.FileIndicesColIDs, 'FrameTimestamp')}]; + +% To assess whether they are from the same data we first check to make sure +% the indices expected in the orientData file can be found within the +% behavMatrix. Then we compare the mean and median for the difference +% between the orientData and behavMatrix and as long as it's below a +% floating point threshold odds are they're from the same data and we can +% assume the frameIndices are correct. +if max(frameIndices) > size(behavMatrix,1) + error('More frameIndices than actual indices in the behavMatrix. Double check files'); +elseif mean(behavMatrix(frameIndices,1) - frameTimestamps') - median(behavMatrix(frameIndices,1) - frameTimestamps') >= 1.0e-10 + error('Difference between mean/median for behavior and orientation timestamp differences is larget than 1.0e-10. Double check files or change threhsold for match violations'); +end + +%% Create the orientMatrix and orientMatrixColIDs variables +orientMatrixColIDs = [{'TimeBin'}, {'PortX'}, {'PortY'}, {'PortAngle'},... + {'HeadX'}, {'HeadY'}, {'HeadAngle'},... + {'TailX'}, {'TailY'}, {'TailAngle'},... + {'HeadTailLength'}, {'HeadPortLength'}, {'PortTailLength'}]; +orientMatrix = [behavMatrix(:,1), nan(size(behavMatrix,1), 12)]; + +%% Fill in the orientation values from the Orientation_Data file +orientMatrix(frameIndices,strcmp(orientMatrixColIDs, 'PortX')) = [orientData.FileIndices{:,strcmp(orientData.FileIndicesColIDs, 'PortX')}]; +orientMatrix(frameIndices,strcmp(orientMatrixColIDs, 'PortY')) = [orientData.FileIndices{:,strcmp(orientData.FileIndicesColIDs, 'PortY')}]; +orientMatrix(frameIndices,strcmp(orientMatrixColIDs, 'HeadX')) = [orientData.FileIndices{:,strcmp(orientData.FileIndicesColIDs, 'HeadX')}]; +orientMatrix(frameIndices,strcmp(orientMatrixColIDs, 'HeadY')) = [orientData.FileIndices{:,strcmp(orientData.FileIndicesColIDs, 'HeadY')}]; +orientMatrix(frameIndices,strcmp(orientMatrixColIDs, 'TailX')) = [orientData.FileIndices{:,strcmp(orientData.FileIndicesColIDs, 'TailX')}]; +orientMatrix(frameIndices,strcmp(orientMatrixColIDs, 'TailY')) = [orientData.FileIndices{:,strcmp(orientData.FileIndicesColIDs, 'TailY')}]; + +%% Calculate the angle and length values +posIndices = find(~isnan(orientMatrix(:,2))); + +for pos = 1:length(posIndices) + curPosNdx = posIndices(pos); + curPortX = orientMatrix(curPosNdx, strcmp(orientMatrixColIDs, 'PortX')); + curPortY = orientMatrix(curPosNdx, strcmp(orientMatrixColIDs, 'PortY')); + curHeadX = orientMatrix(curPosNdx, strcmp(orientMatrixColIDs, 'HeadX')); + curHeadY = orientMatrix(curPosNdx, strcmp(orientMatrixColIDs, 'HeadY')); + curTailX = orientMatrix(curPosNdx, strcmp(orientMatrixColIDs, 'TailX')); + curTailY = orientMatrix(curPosNdx, strcmp(orientMatrixColIDs, 'TailY')); + + htVal = sqrt((curTailX - curHeadX)^2 + (curTailY - curHeadY)^2); + orientMatrix(curPosNdx, strcmp(orientMatrixColIDs, 'HeadTailLength')) = htVal; + hpVal = sqrt((curPortX - curHeadX)^2 + (curPortY - curHeadY)^2); + orientMatrix(curPosNdx, strcmp(orientMatrixColIDs, 'HeadPortLength')) = hpVal; + ptVal = sqrt((curTailX - curPortX)^2 + (curTailY - curPortY)^2); + orientMatrix(curPosNdx, strcmp(orientMatrixColIDs, 'PortTailLength')) = ptVal; + + orientMatrix(curPosNdx, strcmp(orientMatrixColIDs, 'PortAngle')) = rad2deg(acos((ptVal^2 + hpVal^2 - htVal^2)/(2*ptVal*hpVal))); + orientMatrix(curPosNdx, strcmp(orientMatrixColIDs, 'HeadAngle')) = rad2deg(acos((htVal^2 + hpVal^2 - ptVal^2)/(2*htVal*hpVal))); + orientMatrix(curPosNdx, strcmp(orientMatrixColIDs, 'TailAngle')) = rad2deg(acos((htVal^2 + ptVal^2 - hpVal^2)/(2*htVal*ptVal))); +end + +%% Save the orientationMatrix File +fileParts = strsplit(fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'BehaviorMatrix'))}, '_'); +save(sprintf('%s_%s_OrientationMatrix', fileParts{1}, fileParts{2}), 'orientMatrix', 'orientMatrixColIDs'); + +%% Create a summary figure +figure; +scatAll = subplot(2,2,1); +scatter(orientMatrix(:,strcmp(orientMatrixColIDs, 'HeadX')), orientMatrix(:,strcmp(orientMatrixColIDs, 'HeadY')), 15, 'markeredgecolor', 'none', 'markerfacecolor', 'b', 'markerfacealpha', 0.25); +hold on; +scatter(orientMatrix(:,strcmp(orientMatrixColIDs, 'TailX')), orientMatrix(:,strcmp(orientMatrixColIDs, 'TailY')), 15, 'markeredgecolor', 'none', 'markerfacecolor', 'r', 'markerfacealpha', 0.25); +scatter(orientMatrix(:,strcmp(orientMatrixColIDs, 'PortX')), orientMatrix(:,strcmp(orientMatrixColIDs, 'PortY')), 100, 'markeredgecolor', 'k', 'markerfacecolor', 'k') + +scatter(nanmean(orientMatrix(:,strcmp(orientMatrixColIDs, 'HeadX'))), nanmean(orientMatrix(:,strcmp(orientMatrixColIDs, 'HeadY'))), 100, 'x', 'markeredgecolor', 'k', 'markerfacecolor', 'b', 'linewidth', 2); +scatter(nanmean(orientMatrix(:,strcmp(orientMatrixColIDs, 'TailX'))), nanmean(orientMatrix(:,strcmp(orientMatrixColIDs, 'TailY'))), 100, 'x', 'markeredgecolor', 'k', 'markerfacecolor', 'r', 'linewidth', 2); +title(sprintf('%s %s PreTrial = %i ms, PostTrial = %i ms', fileParts{1}, fileParts{2}, orientData.Params.PreTrialDuration*1000, orientData.Params.PostTrialDuration*1000)); + +trlStruct = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [0 0], 'PokeIn'); +headSP = subplot(2,2,3); +tailSP = subplot(2,2,4); +linkaxes([scatAll, headSP, tailSP], 'xy'); +for trl = 1:length(trlStruct) + winStart = trlStruct(trl).PokeInIndex - (orientData.Params.PreTrialDuration*1000); + winEnd = trlStruct(trl).PokeOutIndex + (orientData.Params.PostTrialDuration*1000); + trlOrient = orientMatrix(winStart:winEnd,:); + omx = trlOrient(sum(~isnan(trlOrient),2)==size(trlOrient,2),:); + tempHead = plot(headSP, omx(:,strcmp(orientMatrixColIDs, 'HeadX')), omx(:,strcmp(orientMatrixColIDs, 'HeadY')), 'k'); + tempHead.Color(4) = 0.25; + hold(headSP, 'on'); + tempTail = plot(tailSP, omx(:,strcmp(orientMatrixColIDs, 'TailX')), omx(:,strcmp(orientMatrixColIDs, 'TailY')), 'k'); + tempTail.Color(4) = 0.25; + hold(tailSP, 'on'); + drawnow; + pause(1) +end + +' \ No newline at end of file From 63beb72652ad48894da161cdb929f9b476a470ba Mon Sep 17 00:00:00 2001 From: gaelias Date: Thu, 2 Jul 2020 02:44:49 -0700 Subject: [PATCH 106/133] Create PFC_MLB.m --- Analyses/PFC/PFC_MLB.m | 261 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 261 insertions(+) create mode 100644 Analyses/PFC/PFC_MLB.m diff --git a/Analyses/PFC/PFC_MLB.m b/Analyses/PFC/PFC_MLB.m new file mode 100644 index 0000000..41126a3 --- /dev/null +++ b/Analyses/PFC/PFC_MLB.m @@ -0,0 +1,261 @@ +%% +clc +clear all + +%% Runtime variables +piWindow = [-0.5 0.5]; +poWindow = [-0.5 0.5]; +binSize = 200; +dsRate = 5; +cLims = [0 0.005]; + +%% +smPath = uigetdir; +cd(smPath); +files = dir(smPath); +fileNames = {files.name}; +% Load the behavior matrix file for poke events plots +behMatFile = fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'BehaviorMatrix'))}; +nsmblMatFile = fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'EnsembleMatrix'))}; +load([smPath '\' behMatFile]); +load([smPath '\' nsmblMatFile]); +% Identify list of all statMatrix files +smFileList = fileNames(cellfun(@(a)~isempty(a), regexp(fileNames, '_SM\>')))'; + +%% Extract Behavioral Periods +% trialInfo = [Trial#, Sequence#, Position, Odor, Performance, tDist, PokeDur, WithdrawLat] +[piSpkMtx, piTrialTime, trialInfo] = OrganizeAndBinSpikes(ensembleMatrix, behavMatrix, behavMatrixColIDs, 'PokeIn', piWindow, binSize, dsRate); +[poSpkMtx, poTrialTime, ~] = OrganizeAndBinSpikes(ensembleMatrix, behavMatrix, behavMatrixColIDs, 'PokeOut', poWindow, binSize, dsRate); + +clear ensembleMatrix ensembleMatrixColIDs behavMatrix behavMatrixColIDs +%% Identify Fully InSeq Trials & Calculate FIS Liklihoods (PSTH) +fullInSeqSeqsStart = find(conv(trialInfo(:,4), 1:4, 'valid')==20 & conv(trialInfo(:,3), 1:4, 'valid')==20 & conv(trialInfo(:,5), ones(1,4), 'valid')==4); +inSeqSeqs = nan(3,length(fullInSeqSeqsStart)); +for iS = 1:length(fullInSeqSeqsStart) + inSeqSeqs(1,iS) = fullInSeqSeqsStart(iS); + inSeqSeqs(2,iS) = fullInSeqSeqsStart(iS) + 1; + inSeqSeqs(3,iS) = fullInSeqSeqsStart(iS) + 2; + inSeqSeqs(4,iS) = fullInSeqSeqsStart(iS) + 3; +end + +piFISlikes = repmat({nan(size(piSpkMtx,1), size(piSpkMtx,2))}, 4,1); +poFISlikes = repmat({nan(size(poSpkMtx,1), size(poSpkMtx,2))}, 4,1); +for p = 1:4 + piFISlikes{p} = mean(piSpkMtx(:,:,inSeqSeqs(p,:)),3); + poFISlikes{p} = mean(poSpkMtx(:,:,inSeqSeqs(p,:)),3); +end + +% Plot Likelihoods +figure; +figLims = [0 max(max([cell2mat(piFISlikes); cell2mat(poFISlikes)]))]; +subplot(4,2,1); imagesc(piTrialTime, 1:size(piFISlikes{1},2), piFISlikes{1}', figLims); title(gca, 'Poke In'); ylabel('Odor A'); hold on; line([0 0], get(gca, 'ylim'), 'color', 'w'); +subplot(4,2,2); imagesc(poTrialTime, 1:size(poFISlikes{1},2), poFISlikes{1}', figLims); title(gca, 'Poke Out'); hold on; line([0 0], get(gca, 'ylim'), 'color', 'w'); +subplot(4,2,3); imagesc(piTrialTime, 1:size(piFISlikes{1},2), piFISlikes{2}', figLims); ylabel('Odor B'); hold on; line([0 0], get(gca, 'ylim'), 'color', 'w'); +subplot(4,2,4); imagesc(poTrialTime, 1:size(poFISlikes{1},2), poFISlikes{2}', figLims); hold on; line([0 0], get(gca, 'ylim'), 'color', 'w'); +subplot(4,2,5); imagesc(piTrialTime, 1:size(piFISlikes{1},2), piFISlikes{3}', figLims); ylabel('Odor C'); hold on; line([0 0], get(gca, 'ylim'), 'color', 'w'); +subplot(4,2,6); imagesc(poTrialTime, 1:size(poFISlikes{1},2), poFISlikes{3}', figLims); hold on; line([0 0], get(gca, 'ylim'), 'color', 'w'); +subplot(4,2,7); imagesc(piTrialTime, 1:size(piFISlikes{1},2), piFISlikes{4}', figLims); ylabel('Odor D'); hold on; line([0 0], get(gca, 'ylim'), 'color', 'w'); +subplot(4,2,8); imagesc(poTrialTime, 1:size(poFISlikes{1},2), poFISlikes{4}', figLims); hold on; line([0 0], get(gca, 'ylim'), 'color', 'w'); + +annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... + 'linestyle', 'none', 'horizontalalignment', 'right'); +curDir = cd; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); + +%% Decode via Leave 1 out +issPosts = nan((size(piSpkMtx,1) + size(poSpkMtx,1))*4, (size(piSpkMtx,1) + size(poSpkMtx,1))*4,size(inSeqSeqs,2)); +for s = 1:size(inSeqSeqs,2) + tempISSs = inSeqSeqs; + tempISSs(:,s) = []; + + tempFISlikes = repmat({nan(size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,2) + size(poSpkMtx,2))}, 4,1); + tempObsv = tempFISlikes; + for p = 1:4 + tempFISlikes{p} = [mean(piSpkMtx(:,:,tempISSs(p,:)),3); mean(poSpkMtx(:,:,tempISSs(p,:)),3)]; + tempObsv{p} = [piSpkMtx(:,:,inSeqSeqs(p,s)); poSpkMtx(:,:,inSeqSeqs(p,s))]; + end + + issPosts(:,:,s) = CalcStaticBayesPost(cell2mat(tempFISlikes), cell2mat(tempObsv), binSize); +end +odorLog = [ones(1,size(piSpkMtx,1)+size(poSpkMtx,1))*1,... + ones(1,size(piSpkMtx,1)+size(poSpkMtx,1))*2,... + ones(1,size(piSpkMtx,1)+size(poSpkMtx,1))*3,... + ones(1,size(piSpkMtx,1)+size(poSpkMtx,1))*4]; +timeLog = [piTrialTime', poTrialTime'+1+dsRate/1000,... + piTrialTime', poTrialTime'+1+dsRate/1000,... + piTrialTime', poTrialTime'+1+dsRate/1000,... + piTrialTime', poTrialTime'+1+dsRate/1000]; + +figure; +subplot(6,4,1:16); +imagesc(nanmedian(issPosts,3), [0 0.01]); set(gca, 'ydir', 'normal'); colormap jet +% imagesc(nanmean(issPosts,3), [0 0.01]); set(gca, 'ydir', 'normal'); colormap jet +set(gca, 'xtick', [], 'ytick', []); +annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... + 'linestyle', 'none', 'horizontalalignment', 'right'); +curDir = cd; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', '-', 'color', 'white'); +line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 2); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line(get(gca, 'xlim'), [size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)], 'linestyle', '-', 'color', 'white'); +line(get(gca, 'xlim'), [size(piSpkMtx,1)/2 size(piSpkMtx,1)/2], 'linestyle', ':', 'color', 'white', 'linewidth', 2); +line(get(gca, 'xlim'), [size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)], 'linestyle', ':', 'color', 'white', 'linewidth', 1); + +line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*2, get(gca, 'ylim'), 'linestyle', '-', 'color', 'white'); +line([size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1) size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line(get(gca, 'xlim'), [size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*2, 'linestyle', '-', 'color', 'white'); +line(get(gca, 'xlim'), [size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1) size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1)], 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line(get(gca, 'xlim'), [size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)], 'linestyle', ':', 'color', 'white', 'linewidth', 1); + +line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*3, get(gca, 'ylim'), 'linestyle', '-', 'color', 'white'); +line([size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)*2 size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)*2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*2 size(poSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line(get(gca, 'xlim'), [size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*3, 'linestyle', '-', 'color', 'white'); +line(get(gca, 'xlim'), [size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)*2 size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)*2], 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line(get(gca, 'xlim'), [size(poSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*2 size(poSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*2], 'linestyle', ':', 'color', 'white', 'linewidth', 1); + +line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*4, get(gca, 'ylim'), 'linestyle', '-', 'color', 'white'); +line([size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3 size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3 size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line(get(gca, 'xlim'), [size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*4, 'linestyle', '-', 'color', 'white'); +line(get(gca, 'xlim'), [size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3 size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3], 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line(get(gca, 'xlim'), [size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3 size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3], 'linestyle', ':', 'color', 'white', 'linewidth', 1); + +% Odor Decoding +decodeOdor = DecodeBayesPost(issPosts, odorLog); +subplot(6,4,17:20) +plot(1:size(decodeOdor,1), mean(decodeOdor==1,2), 'color', [44/255 168/255 224/255], 'linewidth', 1); +hold on; +plot(1:size(decodeOdor,1), mean(decodeOdor==2,2), 'color', [154/255 133/255 122/255], 'linewidth', 1); +plot(1:size(decodeOdor,1), mean(decodeOdor==3,2), 'color', [9/255 161/255 74/255], 'linewidth', 1); +plot(1:size(decodeOdor,1), mean(decodeOdor==4,2), 'color', [128/255 66/255 151/255], 'linewidth', 1); +legend('A', 'B', 'C','D', 'location', 'southoutside', 'orientation', 'horizontal'); +axis tight +set(gca, 'ylim', [0 1], 'xtick', []); +line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*2, get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*3, get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*4, get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1) size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)*2 size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)*2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*2 size(poSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3 size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3 size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +ylabel([{'Decoding'};{'(% Trials)'}]); + +% Temporal Decoding +decodeTime = DecodeBayesPost(issPosts, timeLog); +decodeLag = nan(size(decodeTime)); +for c = 1:size(decodeTime,2) + decodeLag(:,c) = decodeTime(:,c)-timeLog'; +end +lagMean = nanmean(decodeLag,2); +lagVar = nanstd(decodeLag,1,2); +subplot(6,4,21:24) +plot(lagMean, '-k', 'linewidth', 1); +patch('YData', [lagMean+lagVar; flipud(lagMean-lagVar)],... + 'XData', [1:length(lagMean), length(lagMean):-1:1], 'FaceColor', 'k', 'FaceAlpha', .3, 'edgecolor', 'none'); +axis tight +line([0 length(lagMean)], [0 0], 'linestyle', '--', 'color', 'k', 'linewidth', 1); +line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*2, get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*3, get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*4, get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1) size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)*2 size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)*2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*2 size(poSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3 size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3 size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +set(gca, 'xtick', []); +ylabel Lag (s) + +%% Step through and decode each OutSeq Trial +outSeqTrials = find(trialInfo(:,6)); + + +%% +%%*******************************************************%% +%%********************** Functions **********************%% +%%*******************************************************%% +function [binnedMtx, trialTime, trialInfo] = OrganizeAndBinSpikes(ensembleMatrix, behavMatrix, behavMatrixColIDs, alignment, window, binSize, dsRate) +% First check to make sure reward signal is present within the +% behaviorMatrix +if sum(strcmp(behavMatrixColIDs, 'RewardSignal'))==0 + warning('Reward signal missing from behavior matrix, update the behavior matrix. OR ignore this if reward signal wasn''t recorded as plx flag'); +end +% Organize behavior data and Extract Ensemble Spiking +% Taking 1/2 the binSize on either end to get rid of edge effects. +td = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [window(1)-(binSize/2/1000) window(2)+(binSize/2/1000)], alignment); +trialEnsemble = ExtractTrialData_SM(td, ensembleMatrix(:,2:end)); %#ok<*NODEF> +ensembleMtx = cell2mat(reshape(trialEnsemble, [1 1 length(trialEnsemble)])); +if strcmp(alignment, 'PokeIn') + trialTimes = behavMatrix(td(1).TrialLogVect,1) - behavMatrix(td(1).PokeInIndex,1); +else + trialTimes = behavMatrix(td(1).TrialLogVect,1) - behavMatrix(td(1).PokeOutIndex,1); +end + +%% Bin the spiking data +% First convolve the entire trialEnsembleMtx with a square to bin the +% spikes +binnedEnsembleMtx = nan(size(ensembleMtx)); +for t = 1:size(ensembleMtx,3) + for u = 1:size(ensembleMtx,2) + binnedEnsembleMtx(:,u,t) = conv(ensembleMtx(:,u,t), ones(1,binSize)./(binSize/1000), 'same'); +% binnedEnsembleMtx(:,u,t) = conv(trialEnsembleMtx(:,u,t), ones(1,binSize), 'same'); + end +end +% Now remove the binSize/2 padding +unPaddedBinnedEnsembleMtx = binnedEnsembleMtx((binSize/2)+1:end-(binSize/2),:,:); +trialTimes = trialTimes((binSize/2)+1:end-(binSize/2)); +% Now downsample the binned matrix +dsVect = downsample(1:size(unPaddedBinnedEnsembleMtx,1), dsRate); +binnedMtx = unPaddedBinnedEnsembleMtx(dsVect,:,:); +trialTime = trialTimes(dsVect); + +trialInfo = [[td.TrialNum]', [td.SequenceNum]', [td.Position]', [td.Odor]', ... + [td.Performance]',[td.TranspositionDistance]', [td.PokeDuration]', [td.WithdrawLatency]']; + +end + +%% +function post = CalcStaticBayesPost(likely, obsv, binSize) +post = nan(size(obsv,1), size(likely,1), size(obsv,3)); +for trl = 1:size(obsv,3) + for t = 1:size(obsv,1) + p = nan(size(likely)); + curPopVect = obsv(t,:,trl)*(binSize/1000); + curPopFact = factorial(curPopVect); + for u = 1:size(likely,2) + curAvgUniFR = likely(:,u); + p(:,u) = (((binSize/1000).*curAvgUniFR).^curPopVect(u))./curPopFact(u); + end + pp = prod(p,2); + ee = exp(-((binSize/1000)*sum(likely,2))); + tempPost = pp.*ee; + post(t,:,trl) = tempPost./sum(tempPost); + end +end +end + +%% +function decode = DecodeBayesPost(post, id) +% Assumes post is in the structure of ObservTime X LikelyTime X Trial +decode = nan(size(post,1),size(post,3)); +for o = 1:size(post,3) + for d = 1:size(post,1) + if ~isnan(post(d,1,o)) + decode(d,o) = id(find(post(d,:,o)==max(post(d,:,o)),1,'first')); + end + end +end +end \ No newline at end of file From ad0016ea7fb26a93f75bafb63324e13358956843 Mon Sep 17 00:00:00 2001 From: gaelias Date: Thu, 2 Jul 2020 02:44:54 -0700 Subject: [PATCH 107/133] Update OrganizeTrialData_SM.m --- Data Management/OrganizeTrialData_SM.m | 32 ++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/Data Management/OrganizeTrialData_SM.m b/Data Management/OrganizeTrialData_SM.m index 6e82b33..1e1b30d 100644 --- a/Data Management/OrganizeTrialData_SM.m +++ b/Data Management/OrganizeTrialData_SM.m @@ -50,6 +50,9 @@ % Pull out Error Indices errorSigVect = behavMatrix(:, cellfun(@(a)~isempty(a), strfind(behavMatrixColIDs, 'ErrorSignal'))); errorSigNdxs = find(errorSigVect); +% Pull out Reward Signal Indices +rwdSigVect = behavMatrix(:, cellfun(@(a)~isempty(a), strfind(behavMatrixColIDs, 'RewardSignal'))); +rewardSigNdxs = find(rwdSigVect); % Pull out Sequence Length seqLength = size(positionTrlMtx,2); % Identify trial performance @@ -69,9 +72,11 @@ trialPokeOutNdx = repmat({nan}, [1, numTrials]); trialRewardNdx = repmat({nan}, [1, numTrials]); trialErrorNdx = repmat({nan}, [1, numTrials]); +trialRwdSigNdx = repmat({nan}, [1, numTrials]); trialLogVect = cell(1,numTrials); trialNum = cell(1,numTrials); pokeDuration = cell(1,numTrials); +withdrawLat = cell(1,numTrials); seq = 0; %% Go through each trial and pull out trial information and create a logical vector for that trial's time periods specified by the input trialLims for trl = 1:numTrials @@ -112,23 +117,39 @@ tempLogVect = false(size(behavMatrix,1),1); curPokeIn = pokeInNdxs(find(pokeInNdxstrialIndices(trl)==1,1, 'first')); - pokeDuration{trl} = (curPokeOut-curPokeIn)/sampleRate; - + trialPokeInNdx{trl} = curPokeIn; trialOdorNdx{trl} = trialIndices(trl); trialPokeOutNdx{trl} = curPokeOut; curFrontRwrdNdx = frontRwrdNdxs(find(frontRwrdNdxs>trialIndices(trl)==1,1, 'first')); if isempty(curFrontRwrdNdx) || trl==numTrials || curFrontRwrdNdxtrialIndices(trl)==1,1,'first')); if isempty(curErrSigNdx) || trl==numTrials || curErrSigNdxtrialIndices(trl)==1,1,'first')); + if isempty(curRwdSigNdx) || trl==numTrials || curRwdSigNdx Date: Thu, 2 Jul 2020 02:44:59 -0700 Subject: [PATCH 108/133] Update CreateBehaviorMatrixPFCabbr.m --- .../CreateBehaviorMatrixPFCabbr.m | 32 +++++++------------ 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/statMatrix Creation/CreateBehaviorMatrixPFCabbr.m b/statMatrix Creation/CreateBehaviorMatrixPFCabbr.m index 791fe28..4249aca 100644 --- a/statMatrix Creation/CreateBehaviorMatrixPFCabbr.m +++ b/statMatrix Creation/CreateBehaviorMatrixPFCabbr.m @@ -34,8 +34,8 @@ if isfield(plxData.Summary, 'DualListLog') && plxData.Summary.DualListLog behPad = behPad + seqLength; end -behVals = nan(length(tsVect)-1, behPad + 5); -behDataHeaders = cell(1,behPad + 7); +behVals = nan(length(tsVect)-1, behPad + 6); +behDataHeaders = cell(1,behPad + 8); % Step through each sequence item/position and identify when odors were % presented % Do position first because it doesn't change with multiple lists @@ -86,10 +86,11 @@ behVals(:,behPad+5) = histcounts([plxData.Raw.BackRewardTime], tsVect)'; behDataHeaders{behPad+5} = 'BackReward'; -if isfield(plxData.Raw, 'ErrorSignalTime') - behVals(:,behPad+6) = histcounts([plxData.Raw.ErrorSignalTime], tsVect)'; - behDataHeaders{behPad+6} = 'ErrorSignal'; -end +behVals(:,behPad+6) = histcounts([plxData.Raw.ErrorSignalTime], tsVect)'; +behDataHeaders{behPad+6} = 'ErrorSignal'; + +behVals(:,behPad+7) = histcounts([plxData.Raw.RewardSignalTime], tsVect)'; +behDataHeaders{behPad+7} = 'RewardSignal'; [numChans, chanNames] = plx_event_names(plxData.Summary.PLXfile); findingStrobed = 1; @@ -127,24 +128,15 @@ end end aniPosHistBins = find(histcounts(aniPosition(:,1), tsVect)); - if isfield(plxData.Raw, 'ErrorSignalTime') - behVals(aniPosHistBins,behPad+7) = aniX'; - behVals(aniPosHistBins,behPad+8) = aniY'; - else - behVals(aniPosHistBins,behPad+6) = aniX; - behVals(aniPosHistBins,behPad+7) = aniY; - end + behVals(aniPosHistBins,behPad+8) = aniX'; + behVals(aniPosHistBins,behPad+9) = aniY'; findingStrobed = 0; end end end -if isfield(plxData.Raw, 'ErrorSignalTime') - behDataHeaders{behPad+7} = 'XvalRatMazePosition'; - behDataHeaders{behPad+8} = 'YvalRatMazePosition'; -else - behDataHeaders{behPad+6} = 'XvalRatMazePosition'; - behDataHeaders{behPad+7} = 'YvalRatMazePosition'; -end +behDataHeaders{behPad+8} = 'XvalRatMazePosition'; +behDataHeaders{behPad+9} = 'YvalRatMazePosition'; + behavMatrix = [tsVect(1:end-1)', behVals]; behavMatrixColIDs = [{'TimeBin'}, behDataHeaders]; save([plxData.Summary.PLXfile(1:end-4) '_BehaviorMatrix.mat'], 'behavMatrix', 'behavMatrixColIDs', 'plxData'); From 8ce6b3dc3444cc115af2b670a58dbe13aef04366 Mon Sep 17 00:00:00 2001 From: gaelias Date: Thu, 2 Jul 2020 02:45:06 -0700 Subject: [PATCH 109/133] Update SummarizePLXevents_SD.m --- statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m b/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m index e392609..48db825 100644 --- a/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m +++ b/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m @@ -22,7 +22,7 @@ fileNames = {flContents.name}; matFileLog = cellfun(@(a)~isempty(a), regexp(fileNames, [fileName '_([0-9]*)-([A-Z | a-z]*)-([0-9]*).mat'])); if sum(matFileLog)==0 - [matFileName, matFilePath] = uigetfile('.mat', 'No .MAT file found in the folder with the .PLX file, select the ssnData file'); + [matFileName, matFilePath] = uigetfile('.mat', 'No matching .MAT file found in the folder with the .PLX file, select the ssnData file'); if matFileName == 0 disp('No .MAT file selected, analysis cancelled') return From e443605fea20bac2242d52855e6071203cbc595c Mon Sep 17 00:00:00 2001 From: gabe Date: Thu, 2 Jul 2020 12:14:06 -0700 Subject: [PATCH 110/133] Update PFC_MLB.m --- Analyses/PFC/PFC_MLB.m | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/Analyses/PFC/PFC_MLB.m b/Analyses/PFC/PFC_MLB.m index 41126a3..db46ac9 100644 --- a/Analyses/PFC/PFC_MLB.m +++ b/Analyses/PFC/PFC_MLB.m @@ -89,39 +89,39 @@ figure; subplot(6,4,1:16); -imagesc(nanmedian(issPosts,3), [0 0.01]); set(gca, 'ydir', 'normal'); colormap jet -% imagesc(nanmean(issPosts,3), [0 0.01]); set(gca, 'ydir', 'normal'); colormap jet +% imagesc(nanmedian(issPosts,3), [0 0.01]); set(gca, 'ydir', 'normal'); colormap jet +imagesc(nanmean(issPosts,3), [0 0.01]); set(gca, 'ydir', 'normal'); colormap jet set(gca, 'xtick', [], 'ytick', []); annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... 'linestyle', 'none', 'horizontalalignment', 'right'); curDir = cd; annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); -line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', '-', 'color', 'white'); -line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 2); +line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', '-', 'color', 'white', 'linewidth', 2); +line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); line([size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); -line(get(gca, 'xlim'), [size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)], 'linestyle', '-', 'color', 'white'); -line(get(gca, 'xlim'), [size(piSpkMtx,1)/2 size(piSpkMtx,1)/2], 'linestyle', ':', 'color', 'white', 'linewidth', 2); +line(get(gca, 'xlim'), [size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)], 'linestyle', '-', 'color', 'white', 'linewidth', 2); +line(get(gca, 'xlim'), [size(piSpkMtx,1)/2 size(piSpkMtx,1)/2], 'linestyle', ':', 'color', 'white', 'linewidth', 1); line(get(gca, 'xlim'), [size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)], 'linestyle', ':', 'color', 'white', 'linewidth', 1); -line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*2, get(gca, 'ylim'), 'linestyle', '-', 'color', 'white'); +line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*2, get(gca, 'ylim'), 'linestyle', '-', 'color', 'white', 'linewidth', 2); line([size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1) size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); -line(get(gca, 'xlim'), [size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*2, 'linestyle', '-', 'color', 'white'); +line(get(gca, 'xlim'), [size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*2, 'linestyle', '-', 'color', 'white', 'linewidth', 2); line(get(gca, 'xlim'), [size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1) size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1)], 'linestyle', ':', 'color', 'white', 'linewidth', 1); line(get(gca, 'xlim'), [size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)], 'linestyle', ':', 'color', 'white', 'linewidth', 1); -line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*3, get(gca, 'ylim'), 'linestyle', '-', 'color', 'white'); +line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*3, get(gca, 'ylim'), 'linestyle', '-', 'color', 'white', 'linewidth', 2); line([size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)*2 size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)*2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*2 size(poSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); -line(get(gca, 'xlim'), [size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*3, 'linestyle', '-', 'color', 'white'); +line(get(gca, 'xlim'), [size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*3, 'linestyle', '-', 'color', 'white', 'linewidth', 2); line(get(gca, 'xlim'), [size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)*2 size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)*2], 'linestyle', ':', 'color', 'white', 'linewidth', 1); line(get(gca, 'xlim'), [size(poSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*2 size(poSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*2], 'linestyle', ':', 'color', 'white', 'linewidth', 1); -line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*4, get(gca, 'ylim'), 'linestyle', '-', 'color', 'white'); +line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*4, get(gca, 'ylim'), 'linestyle', '-', 'color', 'white', 'linewidth', 2); line([size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3 size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3 size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); -line(get(gca, 'xlim'), [size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*4, 'linestyle', '-', 'color', 'white'); +line(get(gca, 'xlim'), [size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*4, 'linestyle', '-', 'color', 'white', 'linewidth', 2); line(get(gca, 'xlim'), [size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3 size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3], 'linestyle', ':', 'color', 'white', 'linewidth', 1); line(get(gca, 'xlim'), [size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3 size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3], 'linestyle', ':', 'color', 'white', 'linewidth', 1); @@ -177,7 +177,12 @@ line([size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3 size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3 size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); set(gca, 'xtick', []); -ylabel Lag (s) +ylabel(gca,'Lag (s)'); + +%% Investigate decoding more.... possibly do rank order quantification for each index... tabulate at each index which odor is 1st, 2nd, 3rd, 4th +% Possibly the indexing will demonstrate a lag effect, i.e. 1st = current odor, 2nd = next odor. +% Alternatively, examine decoding of peaks? +% Alternatively (and I like this one the most), calculate total posterior for different odors, i.e. calculate sum or average posterior value for each odor at each index %% Step through and decode each OutSeq Trial outSeqTrials = find(trialInfo(:,6)); From 7c52acced38a8870f6d41f0bf4ee8d08f6d0561e Mon Sep 17 00:00:00 2001 From: gabe Date: Thu, 2 Jul 2020 14:43:57 -0700 Subject: [PATCH 111/133] Update PFC_MLB.m --- Analyses/PFC/PFC_MLB.m | 93 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 86 insertions(+), 7 deletions(-) diff --git a/Analyses/PFC/PFC_MLB.m b/Analyses/PFC/PFC_MLB.m index db46ac9..eaeb508 100644 --- a/Analyses/PFC/PFC_MLB.m +++ b/Analyses/PFC/PFC_MLB.m @@ -57,7 +57,7 @@ subplot(4,2,7); imagesc(piTrialTime, 1:size(piFISlikes{1},2), piFISlikes{4}', figLims); ylabel('Odor D'); hold on; line([0 0], get(gca, 'ylim'), 'color', 'w'); subplot(4,2,8); imagesc(poTrialTime, 1:size(poFISlikes{1},2), poFISlikes{4}', figLims); hold on; line([0 0], get(gca, 'ylim'), 'color', 'w'); -annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... +annotation('textbox', 'position', [0 0.935 1 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms; PokeInWindow = [%ims +%ims], PokeOutWindow = [%ims +%ims]', binSize, dsRate, piWindow(1)*1000,piWindow(2)*1000,poWindow(1)*1000, poWindow(2)*1000)],... 'linestyle', 'none', 'horizontalalignment', 'right'); curDir = cd; annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... @@ -92,11 +92,6 @@ % imagesc(nanmedian(issPosts,3), [0 0.01]); set(gca, 'ydir', 'normal'); colormap jet imagesc(nanmean(issPosts,3), [0 0.01]); set(gca, 'ydir', 'normal'); colormap jet set(gca, 'xtick', [], 'ytick', []); -annotation('textbox', 'position', [0.5 0.935 0.5 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms', binSize, dsRate)],... - 'linestyle', 'none', 'horizontalalignment', 'right'); -curDir = cd; -annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... - 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', '-', 'color', 'white', 'linewidth', 2); line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); line([size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); @@ -179,15 +174,99 @@ set(gca, 'xtick', []); ylabel(gca,'Lag (s)'); +annotation('textbox', 'position', [0 0.935 1 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms; PokeInWindow = [%ims +%ims], PokeOutWindow = [%ims +%ims]', binSize, dsRate, piWindow(1)*1000,piWindow(2)*1000,poWindow(1)*1000, poWindow(2)*1000)],... + 'linestyle', 'none', 'horizontalalignment', 'right'); +curDir = cd; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); + %% Investigate decoding more.... possibly do rank order quantification for each index... tabulate at each index which odor is 1st, 2nd, 3rd, 4th % Possibly the indexing will demonstrate a lag effect, i.e. 1st = current odor, 2nd = next odor. % Alternatively, examine decoding of peaks? % Alternatively (and I like this one the most), calculate total posterior for different odors, i.e. calculate sum or average posterior value for each odor at each index %% Step through and decode each OutSeq Trial -outSeqTrials = find(trialInfo(:,6)); +outSeqTrials = find(trialInfo(:,6)~=0 & trialInfo(:,5)==1 & trialInfo(:,3)<=4 & trialInfo(:,4)<=4); + +osPosts = nan(size(piSpkMtx,1)+size(poSpkMtx,1),(size(piSpkMtx,1)+size(poSpkMtx,1))*2,length(outSeqTrials)); +for osT = 1:length(outSeqTrials) + tempObsv = [piSpkMtx(:,:,outSeqTrials(osT)); poSpkMtx(:,:,outSeqTrials(osT))]; + osPos = trialInfo(outSeqTrials(osT),3); + osOdr = trialInfo(outSeqTrials(osT),4); + + tempPosLikes = [mean(piSpkMtx(:,:,tempISSs(osPos,:)),3); mean(poSpkMtx(:,:,tempISSs(osPos,:)),3)]; + tempOdrLikes = [mean(piSpkMtx(:,:,tempISSs(osOdr,:)),3); mean(poSpkMtx(:,:,tempISSs(osOdr,:)),3)]; + tempLikes = [tempPosLikes; tempOdrLikes]; + + osPosts(:,:,osT) = CalcStaticBayesPost(tempLikes, tempObsv, binSize); +end +odorLog = [ones(1,size(piSpkMtx,1)+size(poSpkMtx,1))*1,... + ones(1,size(piSpkMtx,1)+size(poSpkMtx,1))*2]; +timeLog = [piTrialTime', poTrialTime'+1+dsRate/1000,... + piTrialTime', poTrialTime'+1+dsRate/1000]; + +figure; +subplot(6,4,1:16) +% imagesc(nanmedian(osPosts,3), [0 0.01]); set(gca, 'ydir', 'normal'); colormap jet +imagesc(nanmean(osPosts,3)', [0 0.01]); set(gca, 'ydir', 'normal'); colormap jet +xlabel('Observed'); +ylabel('Decodeded'); +set(gca,... + 'xtick', [size(piSpkMtx,1)/2, size(poSpkMtx,1)/2+size(piSpkMtx,1)], 'xticklabel', [{'PokeIn'}, {'PokeOut'}],... + 'ytick', [size(piSpkMtx,1)/2 size(piSpkMtx,1) size(piSpkMtx,1)+size(poSpkMtx,1)/2 size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1) size(piSpkMtx,1)*2+size(poSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)],... + 'yticklabel', [{'PokeIn'}, {'\bf\fontsize{14}Position'}, {'PokeOut'}, {'PokeIn'}, {'\bf\fontsize{14}Odor'}, {'PokeOut'}]); +line(get(gca, 'xlim'), [size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)], 'linestyle', '-', 'color', 'white', 'linewidth', 2); +line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); + +line(get(gca, 'xlim'), [size(piSpkMtx,1)/2 size(piSpkMtx,1)/2], 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line(get(gca, 'xlim'), [size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)], 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line(get(gca, 'xlim'), [size(piSpkMtx,1)/2+size(piSpkMtx,1)*2 size(piSpkMtx,1)/2+size(piSpkMtx,1)*2], 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line(get(gca, 'xlim'), [size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1) size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)], 'linestyle', ':', 'color', 'white', 'linewidth', 1); +% Odor Decoding +decodeOdor = DecodeBayesPost(osPosts, odorLog); +posDecode = mean(decodeOdor==1,2); +odrDecode = mean(decodeOdor==2,2); +subplot(6,4,17:20) +plot(1:size(decodeOdor,1), posDecode-odrDecode, 'color', 'k', 'linewidth', 1); +axis tight +set(gca, 'ylim', [-1 1], 'xtick', [size(piSpkMtx,1)/2, size(poSpkMtx,1)/2+size(piSpkMtx,1)], 'xticklabel', [{'PokeIn'}, {'PokeOut'}]); +ylabel([{'Decoded Difference'};{'(Pos-Odor)'}]); +line(get(gca, 'xlim'), [0 0], 'color', 'k', 'linestyle', '--'); +line([size(piSpkMtx,1) size(piSpkMtx,1)], get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); + +% Temporal Decoding +decodeTime = DecodeBayesPost(osPosts, timeLog); +decodeLag = nan(size(decodeTime)); +for c = 1:size(decodeTime,2) + decodeLag(:,c) = decodeTime(:,c)-[piTrialTime', poTrialTime'+1+dsRate/1000]'; +end +lagMean = nanmean(decodeLag,2); +lagVar = nanstd(decodeLag,1,2); +subplot(6,4,21:24) +plot(lagMean, '-k', 'linewidth', 1); +patch('YData', [lagMean+lagVar; flipud(lagMean-lagVar)],... + 'XData', [1:length(lagMean), length(lagMean):-1:1], 'FaceColor', 'k', 'FaceAlpha', .3, 'edgecolor', 'none'); +axis tight +set(gca, 'xtick', [size(piSpkMtx,1)/2, size(poSpkMtx,1)/2+size(piSpkMtx,1)], 'xticklabel', [{'PokeIn'}, {'PokeOut'}]); +line(get(gca, 'xlim'), [0 0], 'color', 'k', 'linestyle', '--'); +line([size(piSpkMtx,1) size(piSpkMtx,1)], get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); + +annotation('textbox', 'position', [0 0.935 1 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms; PokeInWindow = [%ims +%ims], PokeOutWindow = [%ims +%ims]', binSize, dsRate, piWindow(1)*1000,piWindow(2)*1000,poWindow(1)*1000, poWindow(2)*1000)],... + 'linestyle', 'none', 'horizontalalignment', 'right'); +curDir = cd; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +%% Trial After OutSeq +% For TAO, use FIS as likelihoods and TAO as observations +% How to compare TAO with FIS decoding... Compare both posteriors as well +% as the likelihood of decoding the correct odor. %% %%*******************************************************%% %%********************** Functions **********************%% From 89f8d3f99e120cfac8c05e08cb4ba116412bf26d Mon Sep 17 00:00:00 2001 From: gaelias Date: Fri, 3 Jul 2020 13:28:52 -0700 Subject: [PATCH 112/133] Rename PFC_MLB to slPFC_MLB PFC_MLB as written was written for single list sessions so to distinguish it from the analysis that will ultimately be made for the dual list sessions (which will be dlPFC_MLB most likely) I renamed it. --- Analyses/PFC/{PFC_MLB.m => slPFC_MLB.m} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Analyses/PFC/{PFC_MLB.m => slPFC_MLB.m} (100%) diff --git a/Analyses/PFC/PFC_MLB.m b/Analyses/PFC/slPFC_MLB.m similarity index 100% rename from Analyses/PFC/PFC_MLB.m rename to Analyses/PFC/slPFC_MLB.m From 66fc0eaa995dd47533863f8546ef0407106e513f Mon Sep 17 00:00:00 2001 From: gaelias Date: Fri, 3 Jul 2020 16:09:42 -0700 Subject: [PATCH 113/133] Create PFC_MLB.m --- Analyses/PFC/PFC_MLB.m | 386 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 386 insertions(+) create mode 100644 Analyses/PFC/PFC_MLB.m diff --git a/Analyses/PFC/PFC_MLB.m b/Analyses/PFC/PFC_MLB.m new file mode 100644 index 0000000..d3c65f2 --- /dev/null +++ b/Analyses/PFC/PFC_MLB.m @@ -0,0 +1,386 @@ +%% +clc +clear all + +%% Runtime variables +piWindow = [-0.5 0.5]; +poWindow = [-0.5 0.5]; +binSize = 200; +dsRate = 5; +cLims = [0 0.005]; + +%% +smPath = uigetdir; +cd(smPath); +files = dir(smPath); +fileNames = {files.name}; +% Load the behavior matrix file for poke events plots +behMatFile = fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'BehaviorMatrix'))}; +nsmblMatFile = fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'EnsembleMatrix'))}; +load([smPath '\' behMatFile]); +load([smPath '\' nsmblMatFile]); +% Identify list of all statMatrix files +smFileList = fileNames(cellfun(@(a)~isempty(a), regexp(fileNames, '_SM\>')))'; + +%% Extract Behavioral Periods +% trialInfo = [Trial#, Sequence#, Position, Odor, Performance, tDist, PokeDur, WithdrawLat] +[piSpkMtx, piTrialTime, trialInfo] = OrganizeAndBinSpikes(ensembleMatrix, behavMatrix, behavMatrixColIDs, 'PokeIn', piWindow, binSize, dsRate); +[poSpkMtx, poTrialTime, ~] = OrganizeAndBinSpikes(ensembleMatrix, behavMatrix, behavMatrixColIDs, 'PokeOut', poWindow, binSize, dsRate); + +clear ensembleMatrix ensembleMatrixColIDs behavMatrix behavMatrixColIDs +%% Identify Fully InSeq Trials & Calculate FIS Liklihoods (PSTH) +fullInSeqSeqsStart = find(conv(trialInfo(:,4), 1:4, 'valid')==20 & conv(trialInfo(:,3), 1:4, 'valid')==20 & conv(trialInfo(:,5), ones(1,4), 'valid')==4); +inSeqSeqs = nan(3,length(fullInSeqSeqsStart)); +for iS = 1:length(fullInSeqSeqsStart) + inSeqSeqs(1,iS) = fullInSeqSeqsStart(iS); + inSeqSeqs(2,iS) = fullInSeqSeqsStart(iS) + 1; + inSeqSeqs(3,iS) = fullInSeqSeqsStart(iS) + 2; + inSeqSeqs(4,iS) = fullInSeqSeqsStart(iS) + 3; +end + +piFISlikes = repmat({nan(size(piSpkMtx,1), size(piSpkMtx,2))}, 4,1); +poFISlikes = repmat({nan(size(poSpkMtx,1), size(poSpkMtx,2))}, 4,1); +for p = 1:4 + piFISlikes{p} = mean(piSpkMtx(:,:,inSeqSeqs(p,:)),3); + poFISlikes{p} = mean(poSpkMtx(:,:,inSeqSeqs(p,:)),3); +end + +% Plot Likelihoods +figure; +figLims = [0 max(max([cell2mat(piFISlikes); cell2mat(poFISlikes)]))]; +subplot(4,2,1); imagesc(piTrialTime, 1:size(piFISlikes{1},2), piFISlikes{1}', figLims); title(gca, 'Poke In'); ylabel('Odor A'); hold on; line([0 0], get(gca, 'ylim'), 'color', 'w'); +subplot(4,2,2); imagesc(poTrialTime, 1:size(poFISlikes{1},2), poFISlikes{1}', figLims); title(gca, 'Poke Out'); hold on; line([0 0], get(gca, 'ylim'), 'color', 'w'); +subplot(4,2,3); imagesc(piTrialTime, 1:size(piFISlikes{1},2), piFISlikes{2}', figLims); ylabel('Odor B'); hold on; line([0 0], get(gca, 'ylim'), 'color', 'w'); +subplot(4,2,4); imagesc(poTrialTime, 1:size(poFISlikes{1},2), poFISlikes{2}', figLims); hold on; line([0 0], get(gca, 'ylim'), 'color', 'w'); +subplot(4,2,5); imagesc(piTrialTime, 1:size(piFISlikes{1},2), piFISlikes{3}', figLims); ylabel('Odor C'); hold on; line([0 0], get(gca, 'ylim'), 'color', 'w'); +subplot(4,2,6); imagesc(poTrialTime, 1:size(poFISlikes{1},2), poFISlikes{3}', figLims); hold on; line([0 0], get(gca, 'ylim'), 'color', 'w'); +subplot(4,2,7); imagesc(piTrialTime, 1:size(piFISlikes{1},2), piFISlikes{4}', figLims); ylabel('Odor D'); hold on; line([0 0], get(gca, 'ylim'), 'color', 'w'); +subplot(4,2,8); imagesc(poTrialTime, 1:size(poFISlikes{1},2), poFISlikes{4}', figLims); hold on; line([0 0], get(gca, 'ylim'), 'color', 'w'); + +annotation('textbox', 'position', [0 0.935 1 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms; PokeInWindow = [%ims +%ims], PokeOutWindow = [%ims +%ims]', binSize, dsRate, piWindow(1)*1000,piWindow(2)*1000,poWindow(1)*1000, poWindow(2)*1000)],... + 'linestyle', 'none', 'horizontalalignment', 'right'); +curDir = cd; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); + +%% Decode via Leave 1 out +issPosts = nan((size(piSpkMtx,1) + size(poSpkMtx,1))*4, (size(piSpkMtx,1) + size(poSpkMtx,1))*4,size(inSeqSeqs,2)); +for s = 1:size(inSeqSeqs,2) + tempISSs = inSeqSeqs; + tempISSs(:,s) = []; + + tempFISlikes = repmat({nan(size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,2) + size(poSpkMtx,2))}, 4,1); + tempObsv = tempFISlikes; + for p = 1:4 + tempFISlikes{p} = [mean(piSpkMtx(:,:,tempISSs(p,:)),3); mean(poSpkMtx(:,:,tempISSs(p,:)),3)]; + tempObsv{p} = [piSpkMtx(:,:,inSeqSeqs(p,s)); poSpkMtx(:,:,inSeqSeqs(p,s))]; + end + + issPosts(:,:,s) = CalcStaticBayesPost(cell2mat(tempFISlikes), cell2mat(tempObsv), binSize); +end +odorLog = [ones(1,size(piSpkMtx,1)+size(poSpkMtx,1))*1,... + ones(1,size(piSpkMtx,1)+size(poSpkMtx,1))*2,... + ones(1,size(piSpkMtx,1)+size(poSpkMtx,1))*3,... + ones(1,size(piSpkMtx,1)+size(poSpkMtx,1))*4]; +timeLog = [piTrialTime', poTrialTime'+1+dsRate/1000,... + piTrialTime', poTrialTime'+1+dsRate/1000,... + piTrialTime', poTrialTime'+1+dsRate/1000,... + piTrialTime', poTrialTime'+1+dsRate/1000]; + +figure; +subplot(6,4,1:16); +% imagesc(nanmedian(issPosts,3)', [0 0.01]); set(gca, 'ydir', 'normal'); colormap jet +imagesc(nanmean(issPosts,3)', [0 0.01]); set(gca, 'ydir', 'normal'); colormap jet +set(gca, 'xtick', [], 'ytick', []); +line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', '-', 'color', 'white', 'linewidth', 2); +line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line(get(gca, 'xlim'), [size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)], 'linestyle', '-', 'color', 'white', 'linewidth', 2); +line(get(gca, 'xlim'), [size(piSpkMtx,1)/2 size(piSpkMtx,1)/2], 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line(get(gca, 'xlim'), [size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)], 'linestyle', ':', 'color', 'white', 'linewidth', 1); + +line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*2, get(gca, 'ylim'), 'linestyle', '-', 'color', 'white', 'linewidth', 2); +line([size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1) size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line(get(gca, 'xlim'), [size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*2, 'linestyle', '-', 'color', 'white', 'linewidth', 2); +line(get(gca, 'xlim'), [size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1) size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1)], 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line(get(gca, 'xlim'), [size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)], 'linestyle', ':', 'color', 'white', 'linewidth', 1); + +line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*3, get(gca, 'ylim'), 'linestyle', '-', 'color', 'white', 'linewidth', 2); +line([size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)*2 size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)*2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*2 size(poSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line(get(gca, 'xlim'), [size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*3, 'linestyle', '-', 'color', 'white', 'linewidth', 2); +line(get(gca, 'xlim'), [size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)*2 size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)*2], 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line(get(gca, 'xlim'), [size(poSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*2 size(poSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*2], 'linestyle', ':', 'color', 'white', 'linewidth', 1); + +line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*4, get(gca, 'ylim'), 'linestyle', '-', 'color', 'white', 'linewidth', 2); +line([size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3 size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3 size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line(get(gca, 'xlim'), [size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*4, 'linestyle', '-', 'color', 'white', 'linewidth', 2); +line(get(gca, 'xlim'), [size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3 size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3], 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line(get(gca, 'xlim'), [size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3 size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3], 'linestyle', ':', 'color', 'white', 'linewidth', 1); + +% % Odor Decoding (average posteriors) +% post = TabulateBayesPost(issPosts, odorLog); +% subplot(6,4,17:20) +% plot(1:size(post,1), nanmean(post(:,:,1),2), 'color', [44/255 168/255 224/255], 'linewidth', 1); +% hold on; +% plot(1:size(post,1), nanmean(post(:,:,2),2), 'color', [154/255 133/255 122/255], 'linewidth', 1); +% plot(1:size(post,1), nanmean(post(:,:,3),2), 'color', [9/255 161/255 74/255], 'linewidth', 1); +% plot(1:size(post,1), nanmean(post(:,:,4),2), 'color', [128/255 66/255 151/255], 'linewidth', 1); +% legend('A', 'B', 'C','D', 'location', 'southoutside', 'orientation', 'horizontal'); +% ylabel([{'Odor Posterior'};{'(Mean +/- SEM)'}]); +% for o = 1:4 +% tempPostMean = nanmean(post(:,:,o),2); +% % tempPostVar = nanstd(post(:,:,o),1,2); +% tempPostVar = SEMcalc(post(:,:,o)')'; +% tp = patch('YData', [tempPostMean+tempPostVar; flipud(tempPostMean-tempPostVar)],... +% 'XData', [1:length(tempPostMean), length(tempPostMean):-1:1], 'FaceAlpha', .3); +% if o==1 +% set(tp, 'FaceColor', [44/255 168/255 224/255], 'edgecolor', [44/255 168/255 224/255], 'EdgeAlpha', .5); +% elseif o==2 +% set(tp, 'FaceColor', [154/255 133/255 122/255], 'edgecolor', [154/255 133/255 122/255], 'EdgeAlpha', .5); +% elseif o==3 +% set(tp, 'FaceColor', [9/255 161/255 74/255], 'edgecolor', [9/255 161/255 74/255], 'EdgeAlpha', .5); +% else +% set(tp, 'FaceColor', [128/255 66/255 151/255], 'edgecolor', [128/255 66/255 151/255], 'EdgeAlpha', .5); +% end +% end + +% Odor Decoding (true decoding) +decodeOdor = DecodeBayesPost(issPosts, odorLog); +subplot(6,4,17:20) +plot(1:size(decodeOdor,1), mean(decodeOdor==1,2), 'color', [44/255 168/255 224/255], 'linewidth', 1); +hold on; +plot(1:size(decodeOdor,1), mean(decodeOdor==2,2), 'color', [154/255 133/255 122/255], 'linewidth', 1); +plot(1:size(decodeOdor,1), mean(decodeOdor==3,2), 'color', [9/255 161/255 74/255], 'linewidth', 1); +plot(1:size(decodeOdor,1), mean(decodeOdor==4,2), 'color', [128/255 66/255 151/255], 'linewidth', 1); +legend('A', 'B', 'C','D', 'location', 'southoutside', 'orientation', 'horizontal'); +ylabel([{'Decoding'};{'(% Trials)'}]); + +axis tight +set(gca, 'ylim', [0 1], 'xtick', []); +line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*2, get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*3, get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*4, get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1) size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)*2 size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)*2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*2 size(poSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3 size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3 size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); + +% Temporal Decoding +decodeTime = DecodeBayesPost(issPosts, timeLog); +decodeLag = nan(size(decodeTime)); +for c = 1:size(decodeTime,2) + decodeLag(:,c) = decodeTime(:,c)-timeLog'; +end +lagMean = nanmean(decodeLag,2); +lagVar = nanstd(decodeLag,1,2); +subplot(6,4,21:24) +plot(lagMean, '-k', 'linewidth', 1); +patch('YData', [lagMean+lagVar; flipud(lagMean-lagVar)],... + 'XData', [1:length(lagMean), length(lagMean):-1:1], 'FaceColor', 'k', 'FaceAlpha', .3, 'edgecolor', 'none'); +axis tight +line([0 length(lagMean)], [0 0], 'linestyle', '--', 'color', 'k', 'linewidth', 1); +line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*2, get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*3, get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*4, get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1) size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)*2 size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)*2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*2 size(poSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3 size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3 size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +set(gca, 'xtick', []); +ylabel(gca,'Lag (s)'); + +annotation('textbox', 'position', [0 0.935 1 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms; PokeInWindow = [%ims +%ims], PokeOutWindow = [%ims +%ims]', binSize, dsRate, piWindow(1)*1000,piWindow(2)*1000,poWindow(1)*1000, poWindow(2)*1000)],... + 'linestyle', 'none', 'horizontalalignment', 'right'); +curDir = cd; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); + +%% Investigate decoding more.... possibly do rank order quantification for each index... tabulate at each index which odor is 1st, 2nd, 3rd, 4th +% Possibly the indexing will demonstrate a lag effect, i.e. 1st = current odor, 2nd = next odor. +% Alternatively, examine decoding of peaks? +% Alternatively (and I like this one the most), calculate total posterior for different odors, i.e. calculate sum or average posterior value for each odor at each index + +%% Step through and decode each OutSeq Trial +outSeqTrials = find(trialInfo(:,6)~=0 & trialInfo(:,5)==1 & trialInfo(:,3)<=4 & trialInfo(:,4)<=4); + +osPosts = nan(size(piSpkMtx,1)+size(poSpkMtx,1),(size(piSpkMtx,1)+size(poSpkMtx,1))*2,length(outSeqTrials)); +for osT = 1:length(outSeqTrials) + tempObsv = [piSpkMtx(:,:,outSeqTrials(osT)); poSpkMtx(:,:,outSeqTrials(osT))]; + osPos = trialInfo(outSeqTrials(osT),3); + osOdr = trialInfo(outSeqTrials(osT),4); + + tempPosLikes = [mean(piSpkMtx(:,:,tempISSs(osPos,:)),3); mean(poSpkMtx(:,:,tempISSs(osPos,:)),3)]; + tempOdrLikes = [mean(piSpkMtx(:,:,tempISSs(osOdr,:)),3); mean(poSpkMtx(:,:,tempISSs(osOdr,:)),3)]; + tempLikes = [tempPosLikes; tempOdrLikes]; + + osPosts(:,:,osT) = CalcStaticBayesPost(tempLikes, tempObsv, binSize); +end +odorLog = [ones(1,size(piSpkMtx,1)+size(poSpkMtx,1))*1,... + ones(1,size(piSpkMtx,1)+size(poSpkMtx,1))*2]; +timeLog = [piTrialTime', poTrialTime'+1+dsRate/1000,... + piTrialTime', poTrialTime'+1+dsRate/1000]; + +figure; +subplot(6,4,1:16) +% imagesc(nanmedian(osPosts,3), [0 0.01]); set(gca, 'ydir', 'normal'); colormap jet +imagesc(nanmean(osPosts,3)', [0 0.01]); set(gca, 'ydir', 'normal'); colormap jet +xlabel('Observed'); +ylabel('Decodeded'); +set(gca,... + 'xtick', [size(piSpkMtx,1)/2, size(poSpkMtx,1)/2+size(piSpkMtx,1)], 'xticklabel', [{'PokeIn'}, {'PokeOut'}],... + 'ytick', [size(piSpkMtx,1)/2 size(piSpkMtx,1) size(piSpkMtx,1)+size(poSpkMtx,1)/2 size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1) size(piSpkMtx,1)*2+size(poSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)],... + 'yticklabel', [{'PokeIn'}, {'\bf\fontsize{14}Position'}, {'PokeOut'}, {'PokeIn'}, {'\bf\fontsize{14}Odor'}, {'PokeOut'}]); +line(get(gca, 'xlim'), [size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)], 'linestyle', '-', 'color', 'white', 'linewidth', 2); +line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); + +line(get(gca, 'xlim'), [size(piSpkMtx,1)/2 size(piSpkMtx,1)/2], 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line(get(gca, 'xlim'), [size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)], 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line(get(gca, 'xlim'), [size(piSpkMtx,1)/2+size(piSpkMtx,1)*2 size(piSpkMtx,1)/2+size(piSpkMtx,1)*2], 'linestyle', ':', 'color', 'white', 'linewidth', 1); +line(get(gca, 'xlim'), [size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1) size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)], 'linestyle', ':', 'color', 'white', 'linewidth', 1); + +% Odor Decoding +decodeOdor = DecodeBayesPost(osPosts, odorLog); +posDecode = mean(decodeOdor==1,2); +odrDecode = mean(decodeOdor==2,2); +subplot(6,4,17:20) +plot(1:size(decodeOdor,1), posDecode-odrDecode, 'color', 'k', 'linewidth', 1); +axis tight +set(gca, 'ylim', [-1 1], 'xtick', [size(piSpkMtx,1)/2, size(poSpkMtx,1)/2+size(piSpkMtx,1)], 'xticklabel', [{'PokeIn'}, {'PokeOut'}]); +ylabel([{'Decoded Difference'};{'(Pos-Odor)'}]); +line(get(gca, 'xlim'), [0 0], 'color', 'k', 'linestyle', '--'); +line([size(piSpkMtx,1) size(piSpkMtx,1)], get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); + +% Temporal Decoding +decodeTime = DecodeBayesPost(osPosts, timeLog); +decodeLag = nan(size(decodeTime)); +for c = 1:size(decodeTime,2) + decodeLag(:,c) = decodeTime(:,c)-[piTrialTime', poTrialTime'+1+dsRate/1000]'; +end +lagMean = nanmean(decodeLag,2); +lagVar = nanstd(decodeLag,1,2); +subplot(6,4,21:24) +plot(lagMean, '-k', 'linewidth', 1); +patch('YData', [lagMean+lagVar; flipud(lagMean-lagVar)],... + 'XData', [1:length(lagMean), length(lagMean):-1:1], 'FaceColor', 'k', 'FaceAlpha', .3, 'edgecolor', 'none'); +axis tight +set(gca, 'xtick', [size(piSpkMtx,1)/2, size(poSpkMtx,1)/2+size(piSpkMtx,1)], 'xticklabel', [{'PokeIn'}, {'PokeOut'}]); +line(get(gca, 'xlim'), [0 0], 'color', 'k', 'linestyle', '--'); +line([size(piSpkMtx,1) size(piSpkMtx,1)], get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); + +annotation('textbox', 'position', [0 0.935 1 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms; PokeInWindow = [%ims +%ims], PokeOutWindow = [%ims +%ims]', binSize, dsRate, piWindow(1)*1000,piWindow(2)*1000,poWindow(1)*1000, poWindow(2)*1000)],... + 'linestyle', 'none', 'horizontalalignment', 'right'); +curDir = cd; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); + +%% Trial After OutSeq +% For TAO, use FIS as likelihoods and TAO as observations +% How to compare TAO with FIS decoding... Compare both posteriors as well +% as the likelihood of decoding the correct odor. +%% +%%*******************************************************%% +%%********************** Functions **********************%% +%%*******************************************************%% +function [binnedMtx, trialTime, trialInfo] = OrganizeAndBinSpikes(ensembleMatrix, behavMatrix, behavMatrixColIDs, alignment, window, binSize, dsRate) +% First check to make sure reward signal is present within the +% behaviorMatrix +if sum(strcmp(behavMatrixColIDs, 'RewardSignal'))==0 + warning('Reward signal missing from behavior matrix, update the behavior matrix. OR ignore this if reward signal wasn''t recorded as plx flag'); +end +% Organize behavior data and Extract Ensemble Spiking +% Taking 1/2 the binSize on either end to get rid of edge effects. +td = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [window(1)-(binSize/2/1000) window(2)+(binSize/2/1000)], alignment); +trialEnsemble = ExtractTrialData_SM(td, ensembleMatrix(:,2:end)); %#ok<*NODEF> +ensembleMtx = cell2mat(reshape(trialEnsemble, [1 1 length(trialEnsemble)])); +if strcmp(alignment, 'PokeIn') + trialTimes = behavMatrix(td(1).TrialLogVect,1) - behavMatrix(td(1).PokeInIndex,1); +else + trialTimes = behavMatrix(td(1).TrialLogVect,1) - behavMatrix(td(1).PokeOutIndex,1); +end + +%% Bin the spiking data +% First convolve the entire trialEnsembleMtx with a square to bin the +% spikes +binnedEnsembleMtx = nan(size(ensembleMtx)); +for t = 1:size(ensembleMtx,3) + for u = 1:size(ensembleMtx,2) + binnedEnsembleMtx(:,u,t) = conv(ensembleMtx(:,u,t), ones(1,binSize)./(binSize/1000), 'same'); +% binnedEnsembleMtx(:,u,t) = conv(trialEnsembleMtx(:,u,t), ones(1,binSize), 'same'); + end +end +% Now remove the binSize/2 padding +unPaddedBinnedEnsembleMtx = binnedEnsembleMtx((binSize/2)+1:end-(binSize/2),:,:); +trialTimes = trialTimes((binSize/2)+1:end-(binSize/2)); +% Now downsample the binned matrix +dsVect = downsample(1:size(unPaddedBinnedEnsembleMtx,1), dsRate); +binnedMtx = unPaddedBinnedEnsembleMtx(dsVect,:,:); +trialTime = trialTimes(dsVect); + +trialInfo = [[td.TrialNum]', [td.SequenceNum]', [td.Position]', [td.Odor]', ... + [td.Performance]',[td.TranspositionDistance]', [td.PokeDuration]', [td.WithdrawLatency]']; + +end + +%% +function post = CalcStaticBayesPost(likely, obsv, binSize) +post = nan(size(obsv,1), size(likely,1), size(obsv,3)); +for trl = 1:size(obsv,3) + for t = 1:size(obsv,1) + p = nan(size(likely)); + curPopVect = obsv(t,:,trl)*(binSize/1000); + curPopFact = factorial(curPopVect); + for u = 1:size(likely,2) + curAvgUniFR = likely(:,u); + p(:,u) = (((binSize/1000).*curAvgUniFR).^curPopVect(u))./curPopFact(u); + end + pp = prod(p,2); + ee = exp(-((binSize/1000)*sum(likely,2))); + tempPost = pp.*ee; + post(t,:,trl) = tempPost./sum(tempPost); + end +end +end + +%% +function decode = DecodeBayesPost(post, id) +% Assumes post is in the structure of ObservTime X LikelyTime X Trial +decode = nan(size(post,1),size(post,3)); +for o = 1:size(post,3) + for d = 1:size(post,1) + if ~isnan(post(d,1,o)) + decode(d,o) = id(find(post(d,:,o)==max(post(d,:,o)),1,'first')); + end + end +end +end + +%% +function decode = TabulateBayesPost(post, id) +idS = unique(id); +decode = nan(size(post,1), size(post,3), length(idS)); +for trl = 1:size(post,3) + for t = 1:size(post,1) + for iD = 1:length(idS) + decode(t,trl,iD) = sum(post(t,id==idS(iD),trl)); + end + end +end +end \ No newline at end of file From 6dc166607fd5e0f1c26ec2a0c99ea11cb97ee0d4 Mon Sep 17 00:00:00 2001 From: gaelias Date: Mon, 27 Jul 2020 14:25:51 -0700 Subject: [PATCH 114/133] Update PFC_MLB.m --- Analyses/PFC/PFC_MLB.m | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/Analyses/PFC/PFC_MLB.m b/Analyses/PFC/PFC_MLB.m index d3c65f2..69512dc 100644 --- a/Analyses/PFC/PFC_MLB.m +++ b/Analyses/PFC/PFC_MLB.m @@ -7,7 +7,7 @@ poWindow = [-0.5 0.5]; binSize = 200; dsRate = 5; -cLims = [0 0.005]; +cLim = [0 0.01]; %% smPath = uigetdir; @@ -27,6 +27,9 @@ [piSpkMtx, piTrialTime, trialInfo] = OrganizeAndBinSpikes(ensembleMatrix, behavMatrix, behavMatrixColIDs, 'PokeIn', piWindow, binSize, dsRate); [poSpkMtx, poTrialTime, ~] = OrganizeAndBinSpikes(ensembleMatrix, behavMatrix, behavMatrixColIDs, 'PokeOut', poWindow, binSize, dsRate); +decodings.PItime = piTrialTime; +decodings.POtime = poTrialTime; + clear ensembleMatrix ensembleMatrixColIDs behavMatrix behavMatrixColIDs %% Identify Fully InSeq Trials & Calculate FIS Liklihoods (PSTH) fullInSeqSeqsStart = find(conv(trialInfo(:,4), 1:4, 'valid')==20 & conv(trialInfo(:,3), 1:4, 'valid')==20 & conv(trialInfo(:,5), ones(1,4), 'valid')==4); @@ -89,8 +92,8 @@ figure; subplot(6,4,1:16); -% imagesc(nanmedian(issPosts,3)', [0 0.01]); set(gca, 'ydir', 'normal'); colormap jet -imagesc(nanmean(issPosts,3)', [0 0.01]); set(gca, 'ydir', 'normal'); colormap jet +% imagesc(nanmedian(issPosts,3)', cLim); set(gca, 'ydir', 'normal'); colormap jet +imagesc(nanmean(issPosts,3)', cLim); set(gca, 'ydir', 'normal'); colormap jet set(gca, 'xtick', [], 'ytick', []); line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', '-', 'color', 'white', 'linewidth', 2); line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); @@ -149,12 +152,13 @@ % Odor Decoding (true decoding) decodeOdor = DecodeBayesPost(issPosts, odorLog); +decodings.Odor = [mean(decodeOdor==1,2), mean(decodeOdor==2,2), mean(decodeOdor==3,2), mean(decodeOdor==4,2)]; subplot(6,4,17:20) -plot(1:size(decodeOdor,1), mean(decodeOdor==1,2), 'color', [44/255 168/255 224/255], 'linewidth', 1); +plot(1:size(decodeOdor,1), decodings.Odor(:,1), 'color', [44/255 168/255 224/255], 'linewidth', 1); hold on; -plot(1:size(decodeOdor,1), mean(decodeOdor==2,2), 'color', [154/255 133/255 122/255], 'linewidth', 1); -plot(1:size(decodeOdor,1), mean(decodeOdor==3,2), 'color', [9/255 161/255 74/255], 'linewidth', 1); -plot(1:size(decodeOdor,1), mean(decodeOdor==4,2), 'color', [128/255 66/255 151/255], 'linewidth', 1); +plot(1:size(decodeOdor,1), decodings.Odor(:,2), 'color', [154/255 133/255 122/255], 'linewidth', 1); +plot(1:size(decodeOdor,1), decodings.Odor(:,3), 'color', [9/255 161/255 74/255], 'linewidth', 1); +plot(1:size(decodeOdor,1), decodings.Odor(:,4), 'color', [128/255 66/255 151/255], 'linewidth', 1); legend('A', 'B', 'C','D', 'location', 'southoutside', 'orientation', 'horizontal'); ylabel([{'Decoding'};{'(% Trials)'}]); @@ -181,6 +185,7 @@ end lagMean = nanmean(decodeLag,2); lagVar = nanstd(decodeLag,1,2); +decodings.Time = lagMean; subplot(6,4,21:24) plot(lagMean, '-k', 'linewidth', 1); patch('YData', [lagMean+lagVar; flipud(lagMean-lagVar)],... @@ -235,8 +240,8 @@ figure; subplot(6,4,1:16) -% imagesc(nanmedian(osPosts,3), [0 0.01]); set(gca, 'ydir', 'normal'); colormap jet -imagesc(nanmean(osPosts,3)', [0 0.01]); set(gca, 'ydir', 'normal'); colormap jet +% imagesc(nanmedian(osPosts,3), cLim); set(gca, 'ydir', 'normal'); colormap jet +imagesc(nanmean(osPosts,3)', cLim); set(gca, 'ydir', 'normal'); colormap jet xlabel('Observed'); ylabel('Decodeded'); set(gca,... @@ -256,6 +261,7 @@ decodeOdor = DecodeBayesPost(osPosts, odorLog); posDecode = mean(decodeOdor==1,2); odrDecode = mean(decodeOdor==2,2); +decodings.OSdiff = posDecode-odrDecode; subplot(6,4,17:20) plot(1:size(decodeOdor,1), posDecode-odrDecode, 'color', 'k', 'linewidth', 1); axis tight From ca65642c5a2f94dbf8679dd1dc4d4d35580c4019 Mon Sep 17 00:00:00 2001 From: gaelias Date: Mon, 27 Jul 2020 14:25:56 -0700 Subject: [PATCH 115/133] Create SimpleBetaViewer_Prototype.m --- Analyses/PFC/SimpleBetaViewer_Prototype.m | 256 ++++++++++++++++++++++ 1 file changed, 256 insertions(+) create mode 100644 Analyses/PFC/SimpleBetaViewer_Prototype.m diff --git a/Analyses/PFC/SimpleBetaViewer_Prototype.m b/Analyses/PFC/SimpleBetaViewer_Prototype.m new file mode 100644 index 0000000..7ce59c3 --- /dev/null +++ b/Analyses/PFC/SimpleBetaViewer_Prototype.m @@ -0,0 +1,256 @@ +clc +clear all + +%% +piWindow = [-0.5 1.5]; +poWindow = [-1.5 1.5]; +%% +smPath = uigetdir; +cd(smPath); +files = dir(smPath); +fileNames = {files.name}; +% Load the behavior matrix file for poke events plots +behMatFile = fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'BehaviorMatrix'))}; +% nsmblMatFile = fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'EnsembleMatrix'))}; +load([smPath '\' behMatFile]); +% load([smPath '\' nsmblMatFile]); +% Identify list of all statMatrix files +smFileList = fileNames(cellfun(@(a)~isempty(a), regexp(fileNames, '_SM\>')))'; +% Organize Trial Data and Define Trial Logicals +tdPI = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, piWindow, 'PokeIn'); +tdPO = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, poWindow, 'PokeOut'); +tdRS = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, poWindow, 'RewardSignal'); +isLog = [tdPI.TranspositionDistance]==0; +perfLog = [tdPI.Performance]==1; +p1Log = [tdPI.Position]==1; +p2Log = [tdPI.Position]==2; +p3Log = [tdPI.Position]==3; +p4Log = [tdPI.Position]==4; +pokeDur = [tdPI.PokeDuration]; +wdLat = [tdPI.WithdrawLatency]; + +targDur = nanmean([tdPI.RewardSignalIndex]-[tdPI.PokeInIndex])/1000; +targVar = nanstd([tdPI.RewardSignalIndex]-[tdPI.PokeInIndex])/1000; + +%% +for f = 1:length(smFileList) + % Load File + load([smPath '\' smFileList{f}]); + % Identify Beta, get envelope and z-score + betaCol = cellfun(@(a)~isempty(a), regexp(statMatrixColIDs, 'Beta\>')); + env = abs(hilbert(statMatrix(:,betaCol))); + zEnv = zscore(env); + + % InSeq v OutSeq Beta Power Overall + iscBetaPI = cell2mat(ExtractTrialData_SM(tdPI(isLog & perfLog), zEnv)); + oscBetaPI = cell2mat(ExtractTrialData_SM(tdPI(~isLog & perfLog), zEnv)); + iscBetaPO = cell2mat(ExtractTrialData_SM(tdPO(isLog & perfLog), zEnv)); + oscBetaPO = cell2mat(ExtractTrialData_SM(tdPO(~isLog & perfLog), zEnv)); + figure; + piSP = subplot(6,1,1:2); + line(piWindow, [0 0], 'linestyle', ':', 'color', 'k'); + hold on; + plot(piWindow(1):1/1000:piWindow(2), mean(iscBetaPI,2), 'color', 'blue'); + patch('YData', [mean(iscBetaPI,2)+std(iscBetaPI,1,2)/sqrt(size(iscBetaPI,2)-1); flipud(mean(iscBetaPI,2)-std(iscBetaPI,1,2)/sqrt(size(iscBetaPI,2)-1))],... + 'XData', [piWindow(1):1/1000:piWindow(2), piWindow(2):-1/1000:piWindow(1)],... + 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); + plot(piWindow(1):1/1000:piWindow(2), mean(oscBetaPI,2), 'color', 'red'); + patch('YData', [mean(oscBetaPI,2)+std(oscBetaPI,1,2)/sqrt(size(oscBetaPI,2)-1); flipud(mean(oscBetaPI,2)-std(oscBetaPI,1,2)/sqrt(size(oscBetaPI,2)-1))],... + 'XData', [piWindow(1):1/1000:piWindow(2), piWindow(2):-1/1000:piWindow(1)],... + 'FaceColor', 'red', 'FaceAlpha', .3, 'EdgeColor', 'red', 'EdgeAlpha', 0.5); + title('Poke In Aligned'); + subplot(6,1,3) + histogram(pokeDur(isLog & perfLog),piWindow(1):0.01:piWindow(2), 'normalization', 'count'); + hold on; + histogram(pokeDur(~isLog & perfLog),piWindow(1):0.01:piWindow(2), 'normalization', 'count'); + axis tight + set(gca, 'xlim', get(piSP, 'xlim')); + poSP = subplot(6,1,4:5); + line(poWindow, [0 0], 'linestyle', ':', 'color', 'k'); + hold on; + plot(poWindow(1):1/1000:poWindow(2), mean(iscBetaPO,2), 'color', 'blue'); + patch('YData', [mean(iscBetaPO,2)+std(iscBetaPO,1,2)/sqrt(size(iscBetaPO,2)-1); flipud(mean(iscBetaPO,2)-std(iscBetaPO,1,2)/sqrt(size(iscBetaPO,2)-1))],... + 'XData', [poWindow(1):1/1000:poWindow(2), poWindow(2):-1/1000:poWindow(1)],... + 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); + plot(poWindow(1):1/1000:poWindow(2), mean(oscBetaPO,2), 'color', 'red'); + patch('YData', [mean(oscBetaPO,2)+std(oscBetaPO,1,2)/sqrt(size(oscBetaPO,2)-1); flipud(mean(oscBetaPO,2)-std(oscBetaPO,1,2)/sqrt(size(oscBetaPO,2)-1))],... + 'XData', [poWindow(1):1/1000:poWindow(2), poWindow(2):-1/1000:poWindow(1)],... + 'FaceColor', 'red', 'FaceAlpha', .3, 'EdgeColor', 'red', 'EdgeAlpha', 0.5); + title('Poke Out Aligned'); + subplot(6,1,6) + histogram(wdLat(isLog & perfLog)*-1, poWindow(1):0.01:poWindow(2)); + hold on; + histogram(pokeDur(~isLog & perfLog)*-1, poWindow(1):0.01:poWindow(2)); + axis tight + set(gca, 'xlim', get(poSP, 'xlim')); + annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', smFileList{f},... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); + drawnow; + + % InSeq & OutSeq Beta Broken down by Position + figure; + sp(1) = subplot(2,4,1); + iscBetaPI1 = cell2mat(ExtractTrialData_SM(tdPI(isLog & perfLog & p1Log), zEnv)); + line(piWindow, [0 0], 'linestyle', ':', 'color', 'k'); + hold on; + plot(piWindow(1):1/1000:piWindow(2), mean(iscBetaPI1,2), 'color', 'blue'); + patch('YData', [mean(iscBetaPI1,2)+std(iscBetaPI1,1,2)/sqrt(size(iscBetaPI1,2)-1); flipud(mean(iscBetaPI1,2)-std(iscBetaPI1,1,2)/sqrt(size(iscBetaPI1,2)-1))],... + 'XData', [piWindow(1):1/1000:piWindow(2), piWindow(2):-1/1000:piWindow(1)],... + 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); + ylabel('Poke In Aligned'); + title('Position 1'); + sp(2) = subplot(2,4,2); + iscBetaPI2 = cell2mat(ExtractTrialData_SM(tdPI(isLog & perfLog & p2Log), zEnv)); + oscBetaPI2 = cell2mat(ExtractTrialData_SM(tdPI(~isLog & perfLog & p2Log), zEnv)); + line(piWindow, [0 0], 'linestyle', ':', 'color', 'k'); + hold on; + plot(piWindow(1):1/1000:piWindow(2), mean(iscBetaPI2,2), 'color', 'blue'); + patch('YData', [mean(iscBetaPI2,2)+std(iscBetaPI2,1,2)/sqrt(size(iscBetaPI2,2)-1); flipud(mean(iscBetaPI2,2)-std(iscBetaPI2,1,2)/sqrt(size(iscBetaPI2,2)-1))],... + 'XData', [piWindow(1):1/1000:piWindow(2), piWindow(2):-1/1000:piWindow(1)],... + 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); + plot(piWindow(1):1/1000:piWindow(2), mean(oscBetaPI2,2), 'color', 'red'); + patch('YData', [mean(oscBetaPI2,2)+std(oscBetaPI2,1,2)/sqrt(size(oscBetaPI2,2)-1); flipud(mean(oscBetaPI2,2)-std(oscBetaPI2,1,2)/sqrt(size(oscBetaPI2,2)-1))],... + 'XData', [piWindow(1):1/1000:piWindow(2), piWindow(2):-1/1000:piWindow(1)],... + 'FaceColor', 'red', 'FaceAlpha', .3, 'EdgeColor', 'red', 'EdgeAlpha', 0.5); + title('Position 2'); + sp(3) = subplot(2,4,3); + iscBetaPI3 = cell2mat(ExtractTrialData_SM(tdPI(isLog & perfLog & p3Log), zEnv)); + oscBetaPI3 = cell2mat(ExtractTrialData_SM(tdPI(~isLog & perfLog & p3Log), zEnv)); + line(piWindow, [0 0], 'linestyle', ':', 'color', 'k'); + hold on; + plot(piWindow(1):1/1000:piWindow(2), mean(iscBetaPI3,2), 'color', 'blue'); + patch('YData', [mean(iscBetaPI3,2)+std(iscBetaPI3,1,2)/sqrt(size(iscBetaPI3,2)-1); flipud(mean(iscBetaPI3,2)-std(iscBetaPI3,1,2)/sqrt(size(iscBetaPI3,2)-1))],... + 'XData', [piWindow(1):1/1000:piWindow(2), piWindow(2):-1/1000:piWindow(1)],... + 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); + plot(piWindow(1):1/1000:piWindow(2), mean(oscBetaPI3,2), 'color', 'red'); + patch('YData', [mean(oscBetaPI3,2)+std(oscBetaPI3,1,2)/sqrt(size(oscBetaPI3,2)-1); flipud(mean(oscBetaPI3,2)-std(oscBetaPI3,1,2)/sqrt(size(oscBetaPI3,2)-1))],... + 'XData', [piWindow(1):1/1000:piWindow(2), piWindow(2):-1/1000:piWindow(1)],... + 'FaceColor', 'red', 'FaceAlpha', .3, 'EdgeColor', 'red', 'EdgeAlpha', 0.5); + title('Position 3'); + sp(4) = subplot(2,4,4); + iscBetaPI4 = cell2mat(ExtractTrialData_SM(tdPI(isLog & perfLog & p4Log), zEnv)); + oscBetaPI4 = cell2mat(ExtractTrialData_SM(tdPI(~isLog & perfLog & p4Log), zEnv)); + line(piWindow, [0 0], 'linestyle', ':', 'color', 'k'); + hold on; + plot(piWindow(1):1/1000:piWindow(2), mean(iscBetaPI4,2), 'color', 'blue'); + patch('YData', [mean(iscBetaPI4,2)+std(iscBetaPI4,1,2)/sqrt(size(iscBetaPI4,2)-1); flipud(mean(iscBetaPI4,2)-std(iscBetaPI4,1,2)/sqrt(size(iscBetaPI4,2)-1))],... + 'XData', [piWindow(1):1/1000:piWindow(2), piWindow(2):-1/1000:piWindow(1)],... + 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); + plot(piWindow(1):1/1000:piWindow(2), mean(oscBetaPI4,2), 'color', 'red'); + patch('YData', [mean(oscBetaPI4,2)+std(oscBetaPI4,1,2)/sqrt(size(oscBetaPI4,2)-1); flipud(mean(oscBetaPI4,2)-std(oscBetaPI4,1,2)/sqrt(size(oscBetaPI4,2)-1))],... + 'XData', [piWindow(1):1/1000:piWindow(2), piWindow(2):-1/1000:piWindow(1)],... + 'FaceColor', 'red', 'FaceAlpha', .3, 'EdgeColor', 'red', 'EdgeAlpha', 0.5); + title('Position 4'); + sp(5) = subplot(2,4,5); + iscBetaPO1 = cell2mat(ExtractTrialData_SM(tdPO(isLog & perfLog & p1Log), zEnv)); + line(poWindow, [0 0], 'linestyle', ':', 'color', 'k'); + hold on; + plot(poWindow(1):1/1000:poWindow(2), mean(iscBetaPO1,2), 'color', 'blue'); + patch('YData', [mean(iscBetaPO1,2)+std(iscBetaPO1,1,2)/sqrt(size(iscBetaPO1,2)-1); flipud(mean(iscBetaPO1,2)-std(iscBetaPO1,1,2)/sqrt(size(iscBetaPO1,2)-1))],... + 'XData', [poWindow(1):1/1000:poWindow(2), poWindow(2):-1/1000:poWindow(1)],... + 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); + ylabel('Poke Out Aligned'); + sp(6) = subplot(2,4,6); + iscBetaPO2 = cell2mat(ExtractTrialData_SM(tdPO(isLog & perfLog & p2Log), zEnv)); + oscBetaPO2 = cell2mat(ExtractTrialData_SM(tdPO(~isLog & perfLog & p2Log), zEnv)); + line(poWindow, [0 0], 'linestyle', ':', 'color', 'k'); + hold on; + plot(poWindow(1):1/1000:poWindow(2), mean(iscBetaPO2,2), 'color', 'blue'); + patch('YData', [mean(iscBetaPO2,2)+std(iscBetaPO2,1,2)/sqrt(size(iscBetaPO2,2)-1); flipud(mean(iscBetaPO2,2)-std(iscBetaPO2,1,2)/sqrt(size(iscBetaPO2,2)-1))],... + 'XData', [poWindow(1):1/1000:poWindow(2), poWindow(2):-1/1000:poWindow(1)],... + 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); + plot(poWindow(1):1/1000:poWindow(2), mean(oscBetaPO2,2), 'color', 'red'); + patch('YData', [mean(oscBetaPO2,2)+std(oscBetaPO2,1,2)/sqrt(size(oscBetaPO2,2)-1); flipud(mean(oscBetaPO2,2)-std(oscBetaPO2,1,2)/sqrt(size(oscBetaPO2,2)-1))],... + 'XData', [poWindow(1):1/1000:poWindow(2), poWindow(2):-1/1000:poWindow(1)],... + 'FaceColor', 'red', 'FaceAlpha', .3, 'EdgeColor', 'red', 'EdgeAlpha', 0.5); + sp(7) = subplot(2,4,7); + iscBetaPO3 = cell2mat(ExtractTrialData_SM(tdPO(isLog & perfLog & p3Log), zEnv)); + oscBetaPO3 = cell2mat(ExtractTrialData_SM(tdPO(~isLog & perfLog & p3Log), zEnv)); + line(poWindow, [0 0], 'linestyle', ':', 'color', 'k'); + hold on; + plot(poWindow(1):1/1000:poWindow(2), mean(iscBetaPO3,2), 'color', 'blue'); + patch('YData', [mean(iscBetaPO3,2)+std(iscBetaPO3,1,2)/sqrt(size(iscBetaPO3,2)-1); flipud(mean(iscBetaPO3,2)-std(iscBetaPO3,1,2)/sqrt(size(iscBetaPO3,2)-1))],... + 'XData', [poWindow(1):1/1000:poWindow(2), poWindow(2):-1/1000:poWindow(1)],... + 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); + plot(poWindow(1):1/1000:poWindow(2), mean(oscBetaPO3,2), 'color', 'red'); + patch('YData', [mean(oscBetaPO3,2)+std(oscBetaPO3,1,2)/sqrt(size(oscBetaPO3,2)-1); flipud(mean(oscBetaPO3,2)-std(oscBetaPO3,1,2)/sqrt(size(oscBetaPO3,2)-1))],... + 'XData', [poWindow(1):1/1000:poWindow(2), poWindow(2):-1/1000:poWindow(1)],... + 'FaceColor', 'red', 'FaceAlpha', .3, 'EdgeColor', 'red', 'EdgeAlpha', 0.5); + sp(8) = subplot(2,4,8); + iscBetaPO4 = cell2mat(ExtractTrialData_SM(tdPO(isLog & perfLog & p4Log), zEnv)); + oscBetaPO4 = cell2mat(ExtractTrialData_SM(tdPO(~isLog & perfLog & p4Log), zEnv)); + line(poWindow, [0 0], 'linestyle', ':', 'color', 'k'); + hold on; + plot(poWindow(1):1/1000:poWindow(2), mean(iscBetaPO4,2), 'color', 'blue'); + patch('YData', [mean(iscBetaPO4,2)+std(iscBetaPO4,1,2)/sqrt(size(iscBetaPO4,2)-1); flipud(mean(iscBetaPO4,2)-std(iscBetaPO4,1,2)/sqrt(size(iscBetaPO4,2)-1))],... + 'XData', [poWindow(1):1/1000:poWindow(2), poWindow(2):-1/1000:poWindow(1)],... + 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); + plot(poWindow(1):1/1000:poWindow(2), mean(oscBetaPO4,2), 'color', 'red'); + patch('YData', [mean(oscBetaPO4,2)+std(oscBetaPO4,1,2)/sqrt(size(oscBetaPO4,2)-1); flipud(mean(oscBetaPO4,2)-std(oscBetaPO4,1,2)/sqrt(size(oscBetaPO4,2)-1))],... + 'XData', [poWindow(1):1/1000:poWindow(2), poWindow(2):-1/1000:poWindow(1)],... + 'FaceColor', 'red', 'FaceAlpha', .3, 'EdgeColor', 'red', 'EdgeAlpha', 0.5); + linkaxes(sp(1:4), 'x'); + linkaxes(sp(5:8), 'x'); + set(sp(1), 'xlim', piWindow); + set(sp(8), 'xlim', poWindow); + linkaxes(sp, 'y'); + annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', smFileList{f},... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); + drawnow; + + % Examine Relationship between Withdrawal Latency and Beta Power + iscBetaRS = cell2mat(ExtractTrialData_SM(tdRS(isLog & perfLog & ~p1Log), zEnv)); +% iscBetaRS = cell2mat(ExtractTrialData_SM(tdPO(isLog & perfLog), zEnv)); + pdTrls = pokeDur(isLog & perfLog & ~p1Log); + wlTrls = wdLat(isLog & perfLog & ~p1Log); + figure; + + % Plot Hold Durations + subplot(2,4,[1,5]); + scatter(ones(1,length(pdTrls))+normrnd(0,0.1, [1,length(pdTrls)]), pdTrls, 'marker', '*', 'markeredgecolor', 'k'); + hold on; + line(get(gca, 'xlim'), [targDur targDur], 'color', 'k', 'linewidth', 2); + line(get(gca, 'xlim'), [targDur+targVar targDur+targVar], 'color', 'k', 'linestyle', ':'); + line(get(gca, 'xlim'), [targDur-targVar targDur-targVar], 'color', 'k', 'linestyle', ':'); + set(gca, 'ylim', [0 max(pdTrls)+0.2]); + title ('Poke Dur'); + + % Plot Reaction Times + subplot(2,4,[2,6]); + scatter(ones(1,length(wlTrls))+normrnd(0,0.1, [1,length(wlTrls)]), wlTrls, 'marker', '*', 'markeredgecolor', 'k'); + hold on; + line(get(gca, 'xlim'), [0 0], 'color', 'k', 'linewidth', 2); + title('WD Lat'); + + % Plot Beta Power relative to Reward Signal + subplot(2,4,3:4) + line(poWindow, [0 0], 'linestyle', ':', 'color', 'k'); + hold on; + plot(poWindow(1):1/1000:poWindow(2), mean(iscBetaRS,2), 'color', 'blue'); + patch('YData', [mean(iscBetaRS,2)+std(iscBetaRS,1,2)/sqrt(size(iscBetaRS,2)-1); flipud(mean(iscBetaRS,2)-std(iscBetaRS,1,2)/sqrt(size(iscBetaRS,2)-1))],... + 'XData', [poWindow(1):1/1000:poWindow(2), poWindow(2):-1/1000:poWindow(1)],... + 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); + + % Plot Correlation between Beta Power and behavior (overall Poke Duration and Reward Latency) over time + pdCorr = nan(size(iscBetaRS,1),1); + wlCorr = nan(size(iscBetaRS,1),1); + for t = 1:size(iscBetaRS,1) + tempCorrPD = corrcoef(iscBetaRS(t,:), pdTrls); + pdCorr(t) = tempCorrPD(2); + tempCorrWL = corrcoef(iscBetaRS(t,:), wlTrls); + wlCorr(t) = tempCorrWL(2); + end + subplot(2,4,7:8); + plot(poWindow(1):1/1000:poWindow(2), pdCorr, 'color', 'k'); + hold on; + plot(poWindow(1):1/1000:poWindow(2), wlCorr, 'color', 'k', 'linestyle', '-.'); + legend('Poke Duration', 'Withdrawal Latency', 'location', 'northwest'); + line(poWindow, [0 0], 'color', 'k', 'linestyle', ':'); + annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', smFileList{f},... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); + drawnow; +end + +figure; imagesc(piWindow(1):1/1000:piWindow(2), 1:size(iscBetaPI,2),iscBetaPI', [-2 5]); colormap jet; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', smFileList{f},... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); From 54b609267601a447ef925a436e841dfd86684c83 Mon Sep 17 00:00:00 2001 From: gaelias Date: Mon, 27 Jul 2020 14:26:01 -0700 Subject: [PATCH 116/133] Create slPFC_MLB_GroupPlot.m --- Analyses/PFC/slPFC_MLB_GroupPlot.m | 78 ++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 Analyses/PFC/slPFC_MLB_GroupPlot.m diff --git a/Analyses/PFC/slPFC_MLB_GroupPlot.m b/Analyses/PFC/slPFC_MLB_GroupPlot.m new file mode 100644 index 0000000..b7a1dae --- /dev/null +++ b/Analyses/PFC/slPFC_MLB_GroupPlot.m @@ -0,0 +1,78 @@ +smPath = uigetdir; +cd(smPath); +files = dir(smPath); +fileNames = {files.name}; +% Identify list of all statMatrix files +decodingFiles = fileNames(cellfun(@(a)~isempty(a), regexp(fileNames, 'Decodings')))'; + +%% +decodes = cell(1,length(decodingFiles),4); +osDiffs = cell(1,length(decodingFiles)); +for f = 1:length(decodingFiles) + load(decodingFiles{f}); + decodes{1,f,1} = decodings.Odor(:,1); + decodes{1,f,2} = decodings.Odor(:,2); + decodes{1,f,3} = decodings.Odor(:,3); + decodes{1,f,4} = decodings.Odor(:,4); + osDiffs{f} = decodings.OSdiff; +end +decodes = cell2mat(decodes); +osDiffs = cell2mat(osDiffs); + +figure; +plot(1:size(decodes,1), nanmean(decodes(:,:,1),2), 'color', [44/255 168/255 224/255], 'linewidth', 1); +hold on; +plot(1:size(decodes,1), nanmean(decodes(:,:,2),2), 'color', [154/255 133/255 122/255], 'linewidth', 1); +plot(1:size(decodes,1), nanmean(decodes(:,:,3),2), 'color', [9/255 161/255 74/255], 'linewidth', 1); +plot(1:size(decodes,1), nanmean(decodes(:,:,4),2), 'color', [128/255 66/255 151/255], 'linewidth', 1); +legend('A', 'B', 'C','D', 'location', 'southoutside', 'orientation', 'horizontal'); +for o = 1:4 + tempPostMean = nanmean(decodes(:,:,o),2); +% tempPostVar = nanstd(decodes(:,:,o),1,2); + tempPostVar = SEMcalc(decodes(:,:,o)')'; + tp = patch('YData', [tempPostMean+tempPostVar; flipud(tempPostMean-tempPostVar)],... + 'XData', [1:length(tempPostMean), length(tempPostMean):-1:1], 'FaceAlpha', .3); + if o==1 + set(tp, 'FaceColor', [44/255 168/255 224/255], 'edgecolor', [44/255 168/255 224/255], 'EdgeAlpha', .5); + elseif o==2 + set(tp, 'FaceColor', [154/255 133/255 122/255], 'edgecolor', [154/255 133/255 122/255], 'EdgeAlpha', .5); + elseif o==3 + set(tp, 'FaceColor', [9/255 161/255 74/255], 'edgecolor', [9/255 161/255 74/255], 'EdgeAlpha', .5); + else + set(tp, 'FaceColor', [128/255 66/255 151/255], 'edgecolor', [128/255 66/255 151/255], 'EdgeAlpha', .5); + end +end + +axis tight +set(gca, 'ylim', [0 1], 'xtick', []); +line([size(decodings.PItime,1) + size(decodings.POtime,1), size(decodings.PItime,1) + size(decodings.POtime,1)], get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); +line([size(decodings.PItime,1) + size(decodings.POtime,1), size(decodings.PItime,1) + size(decodings.POtime,1)]*2, get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); +line([size(decodings.PItime,1) + size(decodings.POtime,1), size(decodings.PItime,1) + size(decodings.POtime,1)]*3, get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); +line([size(decodings.PItime,1) + size(decodings.POtime,1), size(decodings.PItime,1) + size(decodings.POtime,1)]*4, get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); +line([size(decodings.PItime,1)/2 size(decodings.PItime,1)/2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(decodings.POtime,1)/2+size(decodings.PItime,1) size(decodings.POtime,1)/2+size(decodings.PItime,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(decodings.PItime,1)/2+size(decodings.PItime,1)+size(decodings.POtime,1) size(decodings.PItime,1)/2+size(decodings.PItime,1)+size(decodings.POtime,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(decodings.POtime,1)/2+size(decodings.PItime,1)*2+size(decodings.POtime,1) size(decodings.POtime,1)/2+size(decodings.PItime,1)*2+size(decodings.POtime,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(decodings.PItime,1)/2+size(decodings.PItime,1)*2+size(decodings.POtime,1)*2 size(decodings.PItime,1)/2+size(decodings.PItime,1)*2+size(decodings.POtime,1)*2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(decodings.POtime,1)/2+size(decodings.PItime,1)*3+size(decodings.POtime,1)*2 size(decodings.POtime,1)/2+size(decodings.PItime,1)*3+size(decodings.POtime,1)*2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(decodings.PItime,1)/2+size(decodings.PItime,1)*3+size(decodings.POtime,1)*3 size(decodings.PItime,1)/2+size(decodings.PItime,1)*3+size(decodings.POtime,1)*3], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(decodings.POtime,1)/2+size(decodings.PItime,1)*4+size(decodings.POtime,1)*3 size(decodings.POtime,1)/2+size(decodings.PItime,1)*4+size(decodings.POtime,1)*3], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +ylabel([{'Decoded Odor'};{'% Trials'}]); +xlabel('Sequence Time'); +title('GE11 Decodings (All InSeq, Leave-1-Out; Mean +/- SEM; 5xSessions)'); + +%% +figure; +plot(1:size(osDiffs,1), nanmean(osDiffs,2), 'color', 'k', 'linewidth', 1); +axis tight +patch('YData', [nanmean(osDiffs,2)+SEMcalc(osDiffs')'; flipud(nanmean(osDiffs,2)-SEMcalc(osDiffs')')],... + 'XData', [1:length(nanmean(osDiffs,2)), length(nanmean(osDiffs,2)):-1:1], 'FaceAlpha', .3); +set(gca, 'ylim', [-1 1], 'xtick', [size(decodings.PItime,1)/2, size(decodings.POtime,1)/2+size(decodings.PItime,1)], 'xticklabel', [{'PokeIn'}, {'PokeOut'}]); +ylabel([{'Decoded Difference'};{'(Pos-Odor)'}]); +line(get(gca, 'xlim'), [0 0], 'color', 'k', 'linestyle', '--'); +line([size(decodings.PItime,1) size(decodings.PItime,1)], get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); +line([size(decodings.PItime,1)/2 size(decodings.PItime,1)/2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(decodings.POtime,1)/2+size(decodings.POtime,1) size(decodings.POtime,1)/2+size(decodings.PItime,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +title('GE11 OutSeq Decoding Diff (Position-Odor; Mean +/- SEM; 5xSessions)'); + + From 6cbed410dab2994ab4bd55770113c2daef4e8f8b Mon Sep 17 00:00:00 2001 From: gaelias Date: Mon, 27 Jul 2020 14:26:07 -0700 Subject: [PATCH 117/133] Update SummarizePLXevents_SD.m --- .../Supporting Code/SummarizePLXevents_SD.m | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m b/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m index 48db825..1969172 100644 --- a/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m +++ b/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m @@ -479,7 +479,7 @@ 'AniReturnToOrigin', repmat({nan}, [1,length(ssnData)]),... 'FrontRewardTime', repmat({nan}, [1,length(ssnData)]),... 'BackRewardTime', repmat({nan}, [1,length(ssnData)]),... - 'MultiOdorPokeLog', repmat({nan}, [1,length(ssnData)])); + 'MultiPokeCounts', repmat({nan}, [1,length(ssnData)])); %% Extract Session Timestamps @@ -532,8 +532,9 @@ % msgbox(sprintf('Trial #%i: InSeq trial where buffer was triggered and duration elapsed but it was counted as correct\n\n Re-run the analysis with the error line commented in place of this line', trl), 'Poke Buffer Warning', 'warn'); % error('Trial #%i: InSeq trial where buffer was triggered and duration elapsed but it was counted as correct', trl); % fprintf(outfile, 'Trial #%i: InSeq trial where buffer was triggered and duration elapsed but it was counted as correct\n', trl); -% tempPokeNum = tempPokeNum+1; -% tempPokeDur = tempPokeDur + trialInterPokeIntervals(tempPokeNum-1)+trialPokeDurations(tempPokeNum); + tempPokeNum = tempPokeNum+1; + tempPokeDur = tempPokeDur + trialInterPokeIntervals(tempPokeNum-1)+trialPokeDurations(tempPokeNum); + fprintf(outfile, 'Trial #%i: InSeq trial where buffer was triggered and buffer duration elapsed but it was counted as correct\n', trl); plxData.Summary.Errors = [plxData.Summary.Errors; {['Trial #' num2str(trl) ': InSeq trial where buffer was triggered and buffer duration elapsed but it was counted as correct']}]; break @@ -550,8 +551,9 @@ % msgbox(sprintf('Trial #%i: OutSeq trial where buffer was triggered and duration elapsed but it was counted as incorrect\n\n Re-run the analysis with the error line commented in place of this line', trl), 'Poke Buffer Warning', 'warn'); % error('Trial #%i: OutSeq trial where buffer was triggered and duration elapsed but it was counted as incorrect', trl); % fprintf('Trial #%i: OutSeq trial where buffer was triggered and duration elapsed but it was counted as incorrect\n', trl); -% tempPokeNum = tempPokeNum+1; -% tempPokeDur = tempPokeDur + trialInterPokeIntervals(tempPokeNum-1)+trialPokeDurations(tempPokeNum); + tempPokeNum = tempPokeNum+1; + tempPokeDur = tempPokeDur + trialInterPokeIntervals(tempPokeNum-1)+trialPokeDurations(tempPokeNum); + fprintf(outfile, 'Trial #%i: OutSeq trial where buffer was triggered and buffer duration elapsed but it was counted as incorrect\n', trl); plxData.Summary.Errors = [plxData.Summary.Errors; {['Trial #' num2str(trl) ': OutSeq trial where buffer was triggered and buffer duration elapsed but it was counted as incorrect']}]; break @@ -589,7 +591,7 @@ fprintf(outfile, 'Trial #%i: Abberant performance value\n', trl); error('Trial #%i: Abberant performance value', trl); end - + plxSession(trl).MultiPokeCounts = tempPokeNum; %% ******Logical Checks****** % If these trigger they indicate a need for debugging. Either % there's an issue with the assumptions of this code, @@ -602,10 +604,10 @@ % Check to ensure poke durations match well between the .PLX ans .MAT % files. The only differences should be floating point, hence the % threshold of 0.00001 - if plxSession(trl).PokeDuration - ssnData(trl).PokeDuration > 0.00001 + if abs(plxSession(trl).PokeDuration - ssnData(trl).PokeDuration) > 0.00001 % error('Trial #%i: Trial poke durations for .PLX and .MAT don''t match', trl); fprintf(outfile, 'Trial #%i: Poke Duration discrepancy of %i\n', trl, plxSession(trl).PokeDuration - ssnData(trl).PokeDuration); - plxData.Summary.Errors = [plxData.Summary.Errors; ['Trial #' num2str(trl) ': Poke Duration discrepancy of ' num2str(plxSession(trl).PokeDuration - ssnData(trl).PokeDuration)]]; + plxData.Summary.Errors = [plxData.Summary.Errors; {['Trial #' num2str(trl) ': Poke Duration discrepancy of ' num2str(plxSession(trl).PokeDuration - ssnData(trl).PokeDuration)]}]; end % Check to ensure trial start came before trial end From 744427a422d7dfbc94dcc6bcb1d2a8a8508bf8f6 Mon Sep 17 00:00:00 2001 From: gaelias Date: Mon, 17 Aug 2020 13:27:17 -0700 Subject: [PATCH 118/133] Update CurateRipples2_SM.m --- Analyses/CurateRipples2_SM.m | 1 + 1 file changed, 1 insertion(+) diff --git a/Analyses/CurateRipples2_SM.m b/Analyses/CurateRipples2_SM.m index 36fcf2f..814f217 100644 --- a/Analyses/CurateRipples2_SM.m +++ b/Analyses/CurateRipples2_SM.m @@ -397,6 +397,7 @@ function RmvRip(source,event) trlList = plotData.trlRipList.UserData; trlListNdx = trlList(:,1)==curNdx(1); trlList(trlListNdx,:) = []; +plotData.TrialRips.TrialPeriodID(trlListNdx,:) = []; plotData.TrialRips.Events(trlListNdx,:) = []; plotData.TrialRips.Synchrony(trlListNdx,:) = []; plotData.TrialRips.EnsembleAct(trlListNdx,:) = []; From dc8815c9521e89d0aef9be2f9378677ad64ba153 Mon Sep 17 00:00:00 2001 From: gaelias Date: Thu, 5 Nov 2020 13:24:36 -0800 Subject: [PATCH 119/133] Updates 2020.11.05 --- Analyses/CalculateDprime.m | 26 + Analyses/PFC/PFC_MLB.m | 217 +++++---- Analyses/PFC/PFCdualList_MLB.m | 446 ++++++++++++++++++ Analyses/PFC/SimpleBetaViewer_Prototype.m | 345 +++++++------- Analyses/PFC/slPFC_MLB_GroupPlot.m | 134 +++++- Data Management/OrganizeTrialData_SM.m | 19 +- .../Supporting Code/SummarizePLXevents_SD.m | 1 + 7 files changed, 931 insertions(+), 257 deletions(-) create mode 100644 Analyses/CalculateDprime.m create mode 100644 Analyses/PFC/PFCdualList_MLB.m diff --git a/Analyses/CalculateDprime.m b/Analyses/CalculateDprime.m new file mode 100644 index 0000000..65beac9 --- /dev/null +++ b/Analyses/CalculateDprime.m @@ -0,0 +1,26 @@ +function [dPrm, h, fa] = CalculateDprime(responseMatrix) +%% +if sum(responseMatrix(1,:)) == 1 || sum(responseMatrix(2,:)) == 1 + dPrm = nan; + h = nan; + fa = nan; +else + h = responseMatrix(1,1)/sum(responseMatrix(1,:)); + if h == 1 + h = (sum(responseMatrix(1,:))-1)/sum(responseMatrix(1,:)); + elseif h == 0 + h = 1/sum(responseMatrix(1,:)); + end + fa = responseMatrix(2,1)/sum(responseMatrix(2,:)); + if fa == 1 + fa = (sum(responseMatrix(2,:))-1)/sum(responseMatrix(2,:)); + elseif fa == 0 + fa = 1/sum(responseMatrix(2,:)); + end + + dPrm = norminv(h)-norminv(fa); +end +% +% beta = exp((norminv(fa)^2 - norminv(h)^2)/2); +% +% c = -.5 * (norminv(h) + (norminv(fa))); diff --git a/Analyses/PFC/PFC_MLB.m b/Analyses/PFC/PFC_MLB.m index 69512dc..1c7a65e 100644 --- a/Analyses/PFC/PFC_MLB.m +++ b/Analyses/PFC/PFC_MLB.m @@ -1,4 +1,4 @@ -%% +%% PFC_MLB clc clear all @@ -23,10 +23,13 @@ smFileList = fileNames(cellfun(@(a)~isempty(a), regexp(fileNames, '_SM\>')))'; %% Extract Behavioral Periods -% trialInfo = [Trial#, Sequence#, Position, Odor, Performance, tDist, PokeDur, WithdrawLat] +% trialInfo = [1:Trial#, 2:Sequence#, 3:Position, 4:Odor, 5:Performance, 6:tDist, 7:PokeDur, 8:WithdrawLat] [piSpkMtx, piTrialTime, trialInfo] = OrganizeAndBinSpikes(ensembleMatrix, behavMatrix, behavMatrixColIDs, 'PokeIn', piWindow, binSize, dsRate); [poSpkMtx, poTrialTime, ~] = OrganizeAndBinSpikes(ensembleMatrix, behavMatrix, behavMatrixColIDs, 'PokeOut', poWindow, binSize, dsRate); +decodings.SMIbehav = CalculateSMI([sum(trialInfo(trialInfo(:,6)==0,5)), sum(~trialInfo(trialInfo(:,6)==0,5)); sum(~trialInfo(trialInfo(:,6)~=0,5)), sum(trialInfo(trialInfo(:,6)~=0,5))]); +decodings.SMIbehavSFP = CalculateSMI([sum(trialInfo(trialInfo(:,6)==0 & trialInfo(:,3)~=1,5)), sum(~trialInfo(trialInfo(:,6)==0 & trialInfo(:,3)~=1,5)); sum(~trialInfo(trialInfo(:,6)~=0,5)), sum(trialInfo(trialInfo(:,6)~=0,5))]); + decodings.PItime = piTrialTime; decodings.POtime = poTrialTime; @@ -65,6 +68,30 @@ curDir = cd; annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +%% Organized Ensemble Templates +% time = [piTrialTime', poTrialTime'+1+dsRate/100]; +% +% spks = cellfun(@(a,b)[a',b'], piFISlikes, poFISlikes, 'uniformoutput', 0); +% +% grandMean = mean(cell2mat(reshape(spks, [1,1,4])),3); +% maxSpkNdx = [nan(size(grandMean,1),1), (1:size(grandMean,1))']; +% for c = 1:size(grandMean,1) +% maxSpkNdx(c,1) = find(grandMean(c,:)==max(grandMean(c,:)), 1, 'first'); +% end +% sortedMaxSpk = sortrows(maxSpkNdx); +% +% sortedSpks = cellfun(@(a)a(sortedMaxSpk(:,2),:),spks, 'uniformoutput',0); +% maxSpk = max(cell2mat(cellfun(@(a)max(max(a)),spks, 'uniformoutput', 0))); +% +% figure; +% sp1 = subplot(1,4,1); +% imagesc(time, 1:size(grandMean,1),sortedSpks{1}, [0 maxSpk/2]); +% sp2 = subplot(1,4,2); +% imagesc(time, 1:size(grandMean,1),sortedSpks{2}, [0 maxSpk/2]); +% sp3 = subplot(1,4,3); +% imagesc(time, 1:size(grandMean,1),sortedSpks{3}, [0 maxSpk/2]); +% sp4 = subplot(1,4,4); +% imagesc(time, 1:size(grandMean,1),sortedSpks{4}, [0 maxSpk/2]); %% Decode via Leave 1 out issPosts = nan((size(piSpkMtx,1) + size(poSpkMtx,1))*4, (size(piSpkMtx,1) + size(poSpkMtx,1))*4,size(inSeqSeqs,2)); @@ -95,60 +122,14 @@ % imagesc(nanmedian(issPosts,3)', cLim); set(gca, 'ydir', 'normal'); colormap jet imagesc(nanmean(issPosts,3)', cLim); set(gca, 'ydir', 'normal'); colormap jet set(gca, 'xtick', [], 'ytick', []); -line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', '-', 'color', 'white', 'linewidth', 2); -line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); -line([size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); -line(get(gca, 'xlim'), [size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)], 'linestyle', '-', 'color', 'white', 'linewidth', 2); -line(get(gca, 'xlim'), [size(piSpkMtx,1)/2 size(piSpkMtx,1)/2], 'linestyle', ':', 'color', 'white', 'linewidth', 1); -line(get(gca, 'xlim'), [size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)], 'linestyle', ':', 'color', 'white', 'linewidth', 1); - -line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*2, get(gca, 'ylim'), 'linestyle', '-', 'color', 'white', 'linewidth', 2); -line([size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1) size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); -line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); -line(get(gca, 'xlim'), [size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*2, 'linestyle', '-', 'color', 'white', 'linewidth', 2); -line(get(gca, 'xlim'), [size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1) size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1)], 'linestyle', ':', 'color', 'white', 'linewidth', 1); -line(get(gca, 'xlim'), [size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)], 'linestyle', ':', 'color', 'white', 'linewidth', 1); - -line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*3, get(gca, 'ylim'), 'linestyle', '-', 'color', 'white', 'linewidth', 2); -line([size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)*2 size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)*2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); -line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*2 size(poSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); -line(get(gca, 'xlim'), [size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*3, 'linestyle', '-', 'color', 'white', 'linewidth', 2); -line(get(gca, 'xlim'), [size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)*2 size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)*2], 'linestyle', ':', 'color', 'white', 'linewidth', 1); -line(get(gca, 'xlim'), [size(poSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*2 size(poSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*2], 'linestyle', ':', 'color', 'white', 'linewidth', 1); - -line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*4, get(gca, 'ylim'), 'linestyle', '-', 'color', 'white', 'linewidth', 2); -line([size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3 size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); -line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3 size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); -line(get(gca, 'xlim'), [size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*4, 'linestyle', '-', 'color', 'white', 'linewidth', 2); -line(get(gca, 'xlim'), [size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3 size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3], 'linestyle', ':', 'color', 'white', 'linewidth', 1); -line(get(gca, 'xlim'), [size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3 size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3], 'linestyle', ':', 'color', 'white', 'linewidth', 1); - -% % Odor Decoding (average posteriors) -% post = TabulateBayesPost(issPosts, odorLog); -% subplot(6,4,17:20) -% plot(1:size(post,1), nanmean(post(:,:,1),2), 'color', [44/255 168/255 224/255], 'linewidth', 1); -% hold on; -% plot(1:size(post,1), nanmean(post(:,:,2),2), 'color', [154/255 133/255 122/255], 'linewidth', 1); -% plot(1:size(post,1), nanmean(post(:,:,3),2), 'color', [9/255 161/255 74/255], 'linewidth', 1); -% plot(1:size(post,1), nanmean(post(:,:,4),2), 'color', [128/255 66/255 151/255], 'linewidth', 1); -% legend('A', 'B', 'C','D', 'location', 'southoutside', 'orientation', 'horizontal'); -% ylabel([{'Odor Posterior'};{'(Mean +/- SEM)'}]); -% for o = 1:4 -% tempPostMean = nanmean(post(:,:,o),2); -% % tempPostVar = nanstd(post(:,:,o),1,2); -% tempPostVar = SEMcalc(post(:,:,o)')'; -% tp = patch('YData', [tempPostMean+tempPostVar; flipud(tempPostMean-tempPostVar)],... -% 'XData', [1:length(tempPostMean), length(tempPostMean):-1:1], 'FaceAlpha', .3); -% if o==1 -% set(tp, 'FaceColor', [44/255 168/255 224/255], 'edgecolor', [44/255 168/255 224/255], 'EdgeAlpha', .5); -% elseif o==2 -% set(tp, 'FaceColor', [154/255 133/255 122/255], 'edgecolor', [154/255 133/255 122/255], 'EdgeAlpha', .5); -% elseif o==3 -% set(tp, 'FaceColor', [9/255 161/255 74/255], 'edgecolor', [9/255 161/255 74/255], 'EdgeAlpha', .5); -% else -% set(tp, 'FaceColor', [128/255 66/255 151/255], 'edgecolor', [128/255 66/255 151/255], 'EdgeAlpha', .5); -% end -% end +for op = 1:4 + line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*op, get(gca, 'ylim'), 'linestyle', '-', 'color', 'white', 'linewidth', 2); + line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2] + repmat(size(piSpkMtx,1) + size(poSpkMtx,1),1,2)*(op-1), get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); + line([size(poSpkMtx,1)/2 + size(piSpkMtx,1)*op + size(poSpkMtx,1)*(op-1) size(poSpkMtx,1)/2 + size(piSpkMtx,1)*op + size(poSpkMtx,1)*(op-1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); + line(get(gca, 'xlim'), [size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*op, 'linestyle', '-', 'color', 'white', 'linewidth', 2); + line(get(gca, 'xlim'), [size(piSpkMtx,1)/2 size(piSpkMtx,1)/2] + repmat(size(piSpkMtx,1) + size(poSpkMtx,1),1,2)*(op-1), 'linestyle', ':', 'color', 'white', 'linewidth', 1); + line(get(gca, 'xlim'), [size(poSpkMtx,1)/2 + size(piSpkMtx,1)*op + size(poSpkMtx,1)*(op-1) size(poSpkMtx,1)/2 + size(piSpkMtx,1)*op + size(poSpkMtx,1)*(op-1)], 'linestyle', ':', 'color', 'white', 'linewidth', 1); +end % Odor Decoding (true decoding) decodeOdor = DecodeBayesPost(issPosts, odorLog); @@ -164,18 +145,11 @@ axis tight set(gca, 'ylim', [0 1], 'xtick', []); -line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); -line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*2, get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); -line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*3, get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); -line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*4, get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); -line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); -line([size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); -line([size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1) size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); -line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); -line([size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)*2 size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)*2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); -line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*2 size(poSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); -line([size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3 size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); -line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3 size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +for op = 1:4 + line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*op, get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); + line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2]+(size(piSpkMtx,1)+size(poSpkMtx,1))*(op-1), get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); + line([size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)]+(size(piSpkMtx,1)+size(poSpkMtx,1))*(op-1), get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +end % Temporal Decoding decodeTime = DecodeBayesPost(issPosts, timeLog); @@ -192,18 +166,12 @@ 'XData', [1:length(lagMean), length(lagMean):-1:1], 'FaceColor', 'k', 'FaceAlpha', .3, 'edgecolor', 'none'); axis tight line([0 length(lagMean)], [0 0], 'linestyle', '--', 'color', 'k', 'linewidth', 1); -line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); -line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*2, get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); -line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*3, get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); -line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*4, get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); -line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); -line([size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); -line([size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1) size(piSpkMtx,1)/2+size(piSpkMtx,1)+size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); -line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); -line([size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)*2 size(piSpkMtx,1)/2+size(piSpkMtx,1)*2+size(poSpkMtx,1)*2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); -line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*2 size(poSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); -line([size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3 size(piSpkMtx,1)/2+size(piSpkMtx,1)*3+size(poSpkMtx,1)*3], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); -line([size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3 size(poSpkMtx,1)/2+size(piSpkMtx,1)*4+size(poSpkMtx,1)*3], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); + +for op = 1:4 + line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*op, get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); + line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2]+(size(piSpkMtx,1)+size(poSpkMtx,1))*(op-1), get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); + line([size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)]+(size(piSpkMtx,1)+size(poSpkMtx,1))*(op-1), get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +end set(gca, 'xtick', []); ylabel(gca,'Lag (s)'); @@ -213,13 +181,58 @@ annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +%% Investigate the Posteriors... tabulate across odors and break down by trial periods +% Define trial periods +trlPrdLog(:,1) = [piTrialTime<=0; false(size(poTrialTime))]; % Pre-Trial Log +trlPrdLog(:,2) = [piTrialTime>0; false(size(poTrialTime))]; % Early-Trial Log +trlPrdLog(:,3) = [false(size(piTrialTime)); poTrialTime<=0]; % Late-Trial Log +trlPrdLog(:,4) = [false(size(piTrialTime)); poTrialTime>0]; % Post-Trial Log +trlPrdLog = repmat(trlPrdLog, [4,1]); +post = TabulateBayesPost(issPosts, odorLog); + +trlPrdDecodeMean = nan(4,4,4); +trlPrdDprm = nan(1,4); +trlPrdDecodeVar = nan(4,4,4); +for trlPrd = 1:4 + for opObs = 1:4 + for opDec = 1:4 + tempPosts = post(odorLog'==opObs & trlPrdLog(:,trlPrd),:,opDec); + trlPrdDecodeMean(opDec, opObs, trlPrd) = nanmean(tempPosts(:)); + trlPrdDecodeVar(opDec,opObs,trlPrd) = nanstd(nanmean(tempPosts)); + end + end + tempTrlPrdPost = trlPrdDecodeMean(:,:,trlPrd); + trlPrdDprm(trlPrd) = norminv(mean(tempTrlPrdPost(logical(eye(4)))))-norminv(mean(tempTrlPrdPost(~logical(eye(4))))); +end +figure; +subplot(2,2,1) +imagesc(trlPrdDecodeMean(:,:,1), [0 0.5]); +title('Pre-Trial Period'); +xlabel('True Position'); +subplot(2,2,2) +imagesc(trlPrdDecodeMean(:,:,2), [0 0.5]); +title('Early-Trial Period'); +subplot(2,2,3) +imagesc(trlPrdDecodeMean(:,:,3), [0 0.5]); +title('Late-Trial Period'); +xlabel('True Position'); +ylabel('Decoded Position'); +subplot(2,2,4) +imagesc(trlPrdDecodeMean(:,:,4), [0 0.5]); +title('Post-Trial Period'); +xlabel('True Position'); +colormap jet + +decodings.TrialPeriods = trlPrdDecodeMean; +decodings.TrialPeriodsDprm = trlPrdDprm; + %% Investigate decoding more.... possibly do rank order quantification for each index... tabulate at each index which odor is 1st, 2nd, 3rd, 4th % Possibly the indexing will demonstrate a lag effect, i.e. 1st = current odor, 2nd = next odor. % Alternatively, examine decoding of peaks? % Alternatively (and I like this one the most), calculate total posterior for different odors, i.e. calculate sum or average posterior value for each odor at each index %% Step through and decode each OutSeq Trial -outSeqTrials = find(trialInfo(:,6)~=0 & trialInfo(:,5)==1 & trialInfo(:,3)<=4 & trialInfo(:,4)<=4); +outSeqTrials = find(trialInfo(:,6)~=0 & trialInfo(:,5)==1 & trialInfo(:,3)<=4 & trialInfo(:,4)<=4 & trialInfo(:,4)~=1); osPosts = nan(size(piSpkMtx,1)+size(poSpkMtx,1),(size(piSpkMtx,1)+size(poSpkMtx,1))*2,length(outSeqTrials)); for osT = 1:length(outSeqTrials) @@ -297,6 +310,48 @@ annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +% Split into Skips and Repeats +tDistVals = trialInfo(outSeqTrials,6); +skps = osPosts(:,:,tDistVals<0); +reps = osPosts(:,:,tDistVals>0); + +figure; +repSP = subplot(6,8,[1:4,9:12,17:20,25:28]); +imagesc(nanmean(reps, 3)', cLim); set(gca, 'ydir', 'normal'); colormap jet; +title('Repeats'); +decodeRep = DecodeBayesPost(reps, odorLog); +posDecode = mean(decodeRep==1,2); +odrDecode = mean(decodeRep==2,2); +decodings.OSdiffREP = posDecode-odrDecode; +subplot(6,8,33:36) +plot(1:size(decodeRep,1), posDecode-odrDecode, 'color', 'k', 'linewidth', 1); +axis tight +set(gca, 'ylim', [-1 1], 'xtick', [size(piSpkMtx,1)/2, size(poSpkMtx,1)/2+size(piSpkMtx,1)], 'xticklabel', [{'PokeIn'}, {'PokeOut'}]); +ylabel([{'Decoded Difference'};{'(Pos-Odor)'}]); +line(get(gca, 'xlim'), [0 0], 'color', 'k', 'linestyle', '--'); +line([size(piSpkMtx,1) size(piSpkMtx,1)], get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); + + +skpSP = subplot(6,8,[5:8,13:16,21:24,29:32]); +imagesc(nanmean(skps, 3)', cLim); set(gca, 'ydir', 'normal'); colormap jet; +title('Skips'); +decodeSkp = DecodeBayesPost(skps, odorLog); +posDecode = mean(decodeSkp==1,2); +odrDecode = mean(decodeSkp==2,2); +decodings.OSdiffSKP = posDecode-odrDecode; +subplot(6,8,37:40) +plot(1:size(decodeSkp,1), posDecode-odrDecode, 'color', 'k', 'linewidth', 1); +axis tight +set(gca, 'ylim', [-1 1], 'xtick', [size(piSpkMtx,1)/2, size(poSpkMtx,1)/2+size(piSpkMtx,1)], 'xticklabel', [{'PokeIn'}, {'PokeOut'}]); +ylabel([{'Decoded Difference'};{'(Pos-Odor)'}]); +line(get(gca, 'xlim'), [0 0], 'color', 'k', 'linestyle', '--'); +line([size(piSpkMtx,1) size(piSpkMtx,1)], get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); +line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); + + %% Trial After OutSeq % For TAO, use FIS as likelihoods and TAO as observations % How to compare TAO with FIS decoding... Compare both posteriors as well diff --git a/Analyses/PFC/PFCdualList_MLB.m b/Analyses/PFC/PFCdualList_MLB.m new file mode 100644 index 0000000..41597f0 --- /dev/null +++ b/Analyses/PFC/PFCdualList_MLB.m @@ -0,0 +1,446 @@ +%% PFCdualList_MLB +clc +clear all + +%% Runtime variables +piWindow = [-0.5 0.5]; +poWindow = [-0.5 0.5]; +binSize = 200; +dsRate = 5; +cLim = [0 0.01]; + +%% +smPath = uigetdir; +cd(smPath); +files = dir(smPath); +fileNames = {files.name}; +% Load the behavior matrix file for poke events plots +behMatFile = fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'BehaviorMatrix'))}; +nsmblMatFile = fileNames{cellfun(@(a)~isempty(a), strfind(fileNames, 'EnsembleMatrix'))}; +load([smPath '\' behMatFile]); +load([smPath '\' nsmblMatFile]); +% Identify list of all statMatrix files +smFileList = fileNames(cellfun(@(a)~isempty(a), regexp(fileNames, '_SM\>')))'; + +%% Extract Behavioral Periods +% trialInfo = [1:Trial#, 2:Sequence#, 3:Position, 4:Odor, 5:Performance, 6:tDist, 7:PokeDur, 8:WithdrawLat] +[piSpkMtx, piTrialTime, trialInfo] = OrganizeAndBinSpikes(ensembleMatrix, behavMatrix, behavMatrixColIDs, 'PokeIn', piWindow, binSize, dsRate); +[poSpkMtx, poTrialTime, ~] = OrganizeAndBinSpikes(ensembleMatrix, behavMatrix, behavMatrixColIDs, 'PokeOut', poWindow, binSize, dsRate); + +decodings.PItime = piTrialTime; +decodings.POtime = poTrialTime; + +clear ensembleMatrix ensembleMatrixColIDs behavMatrix behavMatrixColIDs + +%% Organize Data into Sequences & Identify Novel and Familiar Sequences +seqNums = unique(trialInfo(:,2)); +seqCells = cell(size(seqNums)); +seqTdist = zeros(size(seqNums)); +for obsSeq = 1:length(seqNums) + curSeq = trialInfo(trialInfo(:,2)==seqNums(obsSeq),:); + seqCells{obsSeq} = curSeq; + seqTdist(obsSeq) = sum(curSeq(:,6)); +end +seqLength = cellfun(@(a)size(a,1),seqCells); +listID = cellfun(@(a)a(1,4),seqCells); + +famFISs = seqCells(listID==1 & seqLength==4 & seqTdist==0)'; +novFISs = seqCells(listID==11 & seqLength==4 & seqTdist==0)'; + +famFIStrls = cell2mat(cellfun(@(a)a(:,1), famFISs, 'uniformoutput', 0)); +novFIStrls = cell2mat(cellfun(@(a)a(:,1), novFISs, 'uniformoutput', 0)); + +if ~isempty(intersect(famFIStrls, novFIStrls)) + error('Non-unique Familiar and Novel sequences, check code and files!'); +end + +isSeqs = [famFIStrls, novFIStrls]; +issIDlog = [false(1,size(famFIStrls,2)), true(1,size(novFIStrls,2))]; +%% Run decoding using leave-one-out +issPosts = nan((size(piSpkMtx,1) + size(poSpkMtx,1))*4, (size(piSpkMtx,1) + size(poSpkMtx,1))*8,size([famFIStrls, novFIStrls],2)); +for s = 1:size(isSeqs,2) + tempISSs = isSeqs; + tempISSs(:,s) = []; + tempISSidLog = issIDlog; + tempISSidLog(s) = []; + + tempFAMfisLikes = cell(4,1); + tempNOVfisLikes = cell(4,1); + tempObsv = cell(4,1); + for p = 1:4 + tempFAMfisLikes{p} = [mean(piSpkMtx(:,:,tempISSs(p,~tempISSidLog)),3); mean(poSpkMtx(:,:,tempISSs(p,~tempISSidLog)),3)]; + tempNOVfisLikes{p} = [mean(piSpkMtx(:,:,tempISSs(p,tempISSidLog)),3); mean(poSpkMtx(:,:,tempISSs(p,tempISSidLog)),3)]; + tempObsv{p} = [piSpkMtx(:,:,isSeqs(p,s)); poSpkMtx(:,:,isSeqs(p,s))]; + end + + issPosts(:,:,s) = CalcStaticBayesPost(cell2mat([tempFAMfisLikes;tempNOVfisLikes]), cell2mat(tempObsv), binSize); +end + +%% Plot LOO Data +famISSposts = issPosts(:,:,~issIDlog); +novISSposts = issPosts(:,:,issIDlog); +figure; +famSP = subplot(6,8,[1:4,9:12,17:20,25:28]); +imagesc(nanmean(famISSposts,3)', cLim); set(gca, 'ydir', 'normal'); colormap jet +set(gca, 'xtick', [], 'ytick', []); +for op = 1:8 + line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*op, get(gca, 'ylim'), 'linestyle', '-', 'color', 'white', 'linewidth', 2); + line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2] + repmat(size(piSpkMtx,1) + size(poSpkMtx,1),1,2)*(op-1), get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); + line([size(poSpkMtx,1)/2 + size(piSpkMtx,1)*op + size(poSpkMtx,1)*(op-1) size(poSpkMtx,1)/2 + size(piSpkMtx,1)*op + size(poSpkMtx,1)*(op-1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); + line(get(gca, 'xlim'), [size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*op, 'linestyle', '-', 'color', 'white', 'linewidth', 2); + line(get(gca, 'xlim'), [size(piSpkMtx,1)/2 size(piSpkMtx,1)/2] + repmat(size(piSpkMtx,1) + size(poSpkMtx,1),1,2)*(op-1), 'linestyle', ':', 'color', 'white', 'linewidth', 1); + line(get(gca, 'xlim'), [size(poSpkMtx,1)/2 + size(piSpkMtx,1)*op + size(poSpkMtx,1)*(op-1) size(poSpkMtx,1)/2 + size(piSpkMtx,1)*op + size(poSpkMtx,1)*(op-1)], 'linestyle', ':', 'color', 'white', 'linewidth', 1); +end +line(get(gca,'xlim'), [size(piSpkMtx,1)+size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*4, 'linestyle', '--', 'color', 'k', 'linewidth', 2); + +novSP = subplot(6,8,[5:8,13:16,21:24,29:32]); +imagesc(nanmean(novISSposts,3)', cLim); set(gca, 'ydir', 'normal'); colormap jet +set(gca, 'xtick', [], 'ytick', []); +for op = 1:8 + line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*op, get(gca, 'ylim'), 'linestyle', '-', 'color', 'white', 'linewidth', 2); + line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2] + repmat(size(piSpkMtx,1) + size(poSpkMtx,1),1,2)*(op-1), get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); + line([size(poSpkMtx,1)/2 + size(piSpkMtx,1)*op + size(poSpkMtx,1)*(op-1) size(poSpkMtx,1)/2 + size(piSpkMtx,1)*op + size(poSpkMtx,1)*(op-1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'white', 'linewidth', 1); + line(get(gca, 'xlim'), [size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*op, 'linestyle', '-', 'color', 'white', 'linewidth', 2); + line(get(gca, 'xlim'), [size(piSpkMtx,1)/2 size(piSpkMtx,1)/2] + repmat(size(piSpkMtx,1) + size(poSpkMtx,1),1,2)*(op-1), 'linestyle', ':', 'color', 'white', 'linewidth', 1); + line(get(gca, 'xlim'), [size(poSpkMtx,1)/2 + size(piSpkMtx,1)*op + size(poSpkMtx,1)*(op-1) size(poSpkMtx,1)/2 + size(piSpkMtx,1)*op + size(poSpkMtx,1)*(op-1)], 'linestyle', ':', 'color', 'white', 'linewidth', 1); +end +line(get(gca,'xlim'), [size(piSpkMtx,1)+size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*4, 'linestyle', '--', 'color', 'k', 'linewidth', 2); + +%% Decode LOO data +trialTemplate = ones(1,size(piSpkMtx,1)+size(poSpkMtx,1)); +odorLog = [trialTemplate*1,... + trialTemplate*2,... + trialTemplate*3,... + trialTemplate*4,... + trialTemplate*11,... + trialTemplate*12,... + trialTemplate*13,... + trialTemplate*14]; +posLog = [trialTemplate*1,... + trialTemplate*2,... + trialTemplate*3,... + trialTemplate*4,... + trialTemplate*1,... + trialTemplate*2,... + trialTemplate*3,... + trialTemplate*4]; +seqLog = [repmat(trialTemplate*1,1,4),repmat(trialTemplate*2,1,4)]; +timeLog = repmat([piTrialTime', poTrialTime'+1+dsRate/1000], 1,8); + +% Decode Odor ************************************************************* +odorDecode = DecodeBayesPost(issPosts, odorLog); + +famOdorDecode = odorDecode(:,~issIDlog); +decodings.FamOdor = [mean(famOdorDecode==1,2), mean(famOdorDecode==2,2), mean(famOdorDecode==3,2), mean(famOdorDecode==4,2),... + mean(famOdorDecode==11,2), mean(famOdorDecode==12,2), mean(famOdorDecode==13,2), mean(famOdorDecode==14,2)]; +famOdrSP = subplot(6,8,33:36); +plot(1:size(odorDecode,1), smooth(decodings.FamOdor(:,1)-decodings.FamOdor(:,5)), 'color', [44/255 168/255 224/255], 'linewidth', 1); +hold on; +plot(1:size(odorDecode,1), smooth(decodings.FamOdor(:,2)-decodings.FamOdor(:,6)), 'color', [154/255 133/255 122/255], 'linewidth', 1); +plot(1:size(odorDecode,1), smooth(decodings.FamOdor(:,3)-decodings.FamOdor(:,7)), 'color', [9/255 161/255 74/255], 'linewidth', 1); +plot(1:size(odorDecode,1), smooth(decodings.FamOdor(:,4)-decodings.FamOdor(:,8)), 'color', [128/255 66/255 151/255], 'linewidth', 1); +legend('A-W', 'B-X', 'C-Y','D-Z', 'location', 'southoutside', 'orientation', 'horizontal'); +ylabel([{'List Difference'};{'(Fam-Nov)'}]); +axis tight +set(gca, 'ylim', [-1 1], 'xtick', []); +for op = 1:4 + line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*op, get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); + line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2]+(size(piSpkMtx,1)+size(poSpkMtx,1))*(op-1), get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); + line([size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)]+(size(piSpkMtx,1)+size(poSpkMtx,1))*(op-1), get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +end + +novOdorDecode = odorDecode(:,issIDlog); +decodings.NovOdor = [mean(novOdorDecode==1,2), mean(novOdorDecode==2,2), mean(novOdorDecode==3,2), mean(novOdorDecode==4,2),... + mean(novOdorDecode==11,2), mean(novOdorDecode==12,2), mean(novOdorDecode==13,2), mean(novOdorDecode==14,2)]; +novOdrSP = subplot(6,8,37:40); +plot(1:size(odorDecode,1), smooth(decodings.NovOdor(:,1)-decodings.NovOdor(:,5)), 'color', [44/255 168/255 224/255], 'linewidth', 1); +hold on; +plot(1:size(odorDecode,1), smooth(decodings.NovOdor(:,2)-decodings.NovOdor(:,6)), 'color', [154/255 133/255 122/255], 'linewidth', 1); +plot(1:size(odorDecode,1), smooth(decodings.NovOdor(:,3)-decodings.NovOdor(:,7)), 'color', [9/255 161/255 74/255], 'linewidth', 1); +plot(1:size(odorDecode,1), smooth(decodings.NovOdor(:,4)-decodings.NovOdor(:,8)), 'color', [128/255 66/255 151/255], 'linewidth', 1); +legend('A-W', 'B-X', 'C-Y','D-Z', 'location', 'southoutside', 'orientation', 'horizontal'); +axis tight +set(gca, 'ylim', [-1 1], 'xtick', []); +for op = 1:4 + line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*op, get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); + line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2]+(size(piSpkMtx,1)+size(poSpkMtx,1))*(op-1), get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); + line([size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)]+(size(piSpkMtx,1)+size(poSpkMtx,1))*(op-1), get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +end + +% Decode Position ********************************************************* +posDecode = DecodeBayesPost(issPosts, posLog); + +famPosDecode = posDecode(:,~issIDlog); +decodings.FamPos = [mean(famPosDecode==1,2), mean(famPosDecode==2,2), mean(famPosDecode==3,2), mean(famPosDecode==4,2)]; +famPosSP = subplot(6,8,41:44); +plot(1:size(odorDecode,1), smooth(decodings.FamPos(:,1)), 'color', [44/255 168/255 224/255], 'linewidth', 1); +hold on; +plot(1:size(odorDecode,1), smooth(decodings.FamPos(:,2)), 'color', [154/255 133/255 122/255], 'linewidth', 1); +plot(1:size(odorDecode,1), smooth(decodings.FamPos(:,3)), 'color', [9/255 161/255 74/255], 'linewidth', 1); +plot(1:size(odorDecode,1), smooth(decodings.FamPos(:,4)), 'color', [128/255 66/255 151/255], 'linewidth', 1); +legend('1', '2', '3','4', 'location', 'southoutside', 'orientation', 'horizontal'); +ylabel([{'Decoding'};{'(% Trials)'}]); +axis tight +set(gca, 'ylim', [0 1], 'xtick', []); +for op = 1:4 + line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*op, get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); + line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2]+(size(piSpkMtx,1)+size(poSpkMtx,1))*(op-1), get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); + line([size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)]+(size(piSpkMtx,1)+size(poSpkMtx,1))*(op-1), get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +end + +novPosDecode = posDecode(:,issIDlog); +decodings.NovPos = [mean(novPosDecode==1,2), mean(novPosDecode==2,2), mean(novPosDecode==3,2), mean(novPosDecode==4,2)]; +novPosSP = subplot(6,8,45:48); +plot(1:size(odorDecode,1), smooth(decodings.NovPos(:,1)), 'color', [44/255 168/255 224/255], 'linewidth', 1); +hold on; +plot(1:size(odorDecode,1), smooth(decodings.NovPos(:,2)), 'color', [154/255 133/255 122/255], 'linewidth', 1); +plot(1:size(odorDecode,1), smooth(decodings.NovPos(:,3)), 'color', [9/255 161/255 74/255], 'linewidth', 1); +plot(1:size(odorDecode,1), smooth(decodings.NovPos(:,4)), 'color', [128/255 66/255 151/255], 'linewidth', 1); +legend('1', '2', '3','4', 'location', 'southoutside', 'orientation', 'horizontal'); +axis tight +set(gca, 'ylim', [0 1], 'xtick', []); +for op = 1:4 + line([size(piSpkMtx,1) + size(poSpkMtx,1), size(piSpkMtx,1) + size(poSpkMtx,1)]*op, get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); + line([size(piSpkMtx,1)/2 size(piSpkMtx,1)/2]+(size(piSpkMtx,1)+size(poSpkMtx,1))*(op-1), get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); + line([size(poSpkMtx,1)/2+size(piSpkMtx,1) size(poSpkMtx,1)/2+size(piSpkMtx,1)]+(size(piSpkMtx,1)+size(poSpkMtx,1))*(op-1), get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +end + +annotation('textbox', 'position', [0 0.935 1 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms; PokeInWindow = [%ims +%ims], PokeOutWindow = [%ims +%ims]', binSize, dsRate, piWindow(1)*1000,piWindow(2)*1000,poWindow(1)*1000, poWindow(2)*1000)],... + 'linestyle', 'none', 'horizontalalignment', 'right'); +curDir = cd; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); + +figure; +subplot(1,2,1) +novDiff = decodings.NovOdor(:,1:4)-decodings.NovOdor(:,5:8); +ndCounts = histcounts(novDiff(:), -1:0.1:1); +famDiff = decodings.FamOdor(:,1:4)-decodings.FamOdor(:,5:8); +fdCounts = histcounts(famDiff(:), -1:0.1:1); +fB = bar(-0.95:0.1:0.95, fdCounts,1); +fB.FaceAlpha = 0.5; +hold on; +nB = bar(-0.95:0.1:0.95, ndCounts, 1, 'r'); +nB.FaceAlpha = 0.5; +legend('Seq1', 'Seq2'); +xlabel([{'Decoding Difference'}; {'Seq1-Seq2'}]); +subplot(1,2,2) +fB = plot(-0.95:0.1:0.95, cumsum(fdCounts)./sum(fdCounts)); +hold on; +nB = plot(-0.95:0.1:0.95, cumsum(ndCounts)./sum(ndCounts), 'color', 'r'); +xlabel([{'Decoding Difference'}; {'Seq1-Seq2'}]); +ylabel('Cumulative Proportion'); + +annotation('textbox', 'position', [0 0.935 1 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms; PokeInWindow = [%ims +%ims], PokeOutWindow = [%ims +%ims]', binSize, dsRate, piWindow(1)*1000,piWindow(2)*1000,poWindow(1)*1000, poWindow(2)*1000)],... + 'linestyle', 'none', 'horizontalalignment', 'right'); +curDir = cd; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); + +% Decode Time ************************************************************* +timeDecode = DecodeBayesPost(issPosts, timeLog); + +% Decode Sequence ********************************************************* +seqDecode = DecodeBayesPost(issPosts, seqLog); + +%% Examine Difference in Decoding Using pdfs: Nov X Fam +famFamPost = nanmean(issPosts(:,seqLog==1,~issIDlog),3); +famNovPost = nanmean(issPosts(:,seqLog==2,~issIDlog),3); +novFamPost = nanmean(issPosts(:,seqLog==1,issIDlog),3); +novNovPost = nanmean(issPosts(:,seqLog==2,issIDlog),3); + +figure; +subplot(3,4,2) +corrScatPlot(famFamPost(:), famNovPost(:), 'Fam-Fam', 'Fam-Nov', 'rmvZro'); +subplot(3,4,3) +corrScatPlot(famFamPost(:), novFamPost(:), 'Fam-Fam', 'Nov-Fam', 'rmvZro'); +subplot(3,4,4) +corrScatPlot(famFamPost(:), novNovPost(:), 'Fam-Fam', 'Nov-Nov', 'rmvZro'); +subplot(3,4,7) +corrScatPlot(famNovPost(:), novFamPost(:), 'Fam-Nov', 'Nov-Fam', 'rmvZro'); +subplot(3,4,8) +corrScatPlot(famNovPost(:), novNovPost(:), 'Fam-Nov', 'Nov-Nov', 'rmvZro'); +subplot(3,4,12) +corrScatPlot(novFamPost(:), novNovPost(:), 'Nov-Fam', 'Nov-Nov', 'rmvZro'); + +annotation('textbox', 'position', [0 0.935 1 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms; PokeInWindow = [%ims +%ims], PokeOutWindow = [%ims +%ims]', binSize, dsRate, piWindow(1)*1000,piWindow(2)*1000,poWindow(1)*1000, poWindow(2)*1000)],... + 'linestyle', 'none', 'horizontalalignment', 'right'); +curDir = cd; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); + +figure; +subplot(2,2,1) +famDiff = famFamPost - famNovPost; +imagesc(famDiff, [-0.01, 0.01]); set(gca, 'ydir', 'normal'); colormap jet +title([{'Familiar Template'};{'Decode Fam - Decode Nov'}]); +subplot(2,2,2) +novDiff = novFamPost - novNovPost; +imagesc(novDiff, [-0.01, 0.01]); set(gca, 'ydir', 'normal'); colormap jet +title([{'Novel Template'};{'Decode Fam - Decode Nov'}]); +subplot(2,2,3) +corrScatPlot(famDiff(:), novDiff(:), 'FamDiff', 'NovDiff', 'rmvZro'); +title('Difference Similarity'); + +annotation('textbox', 'position', [0 0.935 1 0.05], 'String', ['\bf\fontsize{10}' sprintf('Bin = %i ms; Step = %i ms; PokeInWindow = [%ims +%ims], PokeOutWindow = [%ims +%ims]', binSize, dsRate, piWindow(1)*1000,piWindow(2)*1000,poWindow(1)*1000, poWindow(2)*1000)],... + 'linestyle', 'none', 'horizontalalignment', 'right'); +curDir = cd; +annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', curDir,... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); + + +%% Collapse Across Odor +odorCollapseDecode = zeros(4,8,2); +odors = unique(odorLog); +osbPosLog = [trialTemplate*1,... + trialTemplate*2,... + trialTemplate*3,... + trialTemplate*4]; +for obsSeq = 1:2 + curPosts = nanmean(issPosts(:,:,issIDlog==(obsSeq-1)),3); + for obsPos = 1:4 + curObsPosLog = osbPosLog==obsPos; + for decOdr = 1:length(odors) + curDecOdrLog = odorLog==odors(decOdr); + odorCollapseDecode(obsPos,decOdr,obsSeq) = mean(sum(curPosts(curObsPosLog,curDecOdrLog),2)); + end + end +end + +figure; +ffSP = subplot(3,3,1); +imagesc(odorCollapseDecode(:,1:4,1), [0 0.5]); +set(gca, 'xtick', 1:4, 'ytick', 1:4, 'xticklabel', [{'A'}, {'B'}, {'C'}, {'D'}], 'yticklabel', [{'A'}, {'B'}, {'C'}, {'D'}]); +colormap(ffSP, 'jet'); +title('Fam Decode Fam'); +fnSP = subplot(3,3,2); +imagesc(odorCollapseDecode(:,5:8,1), [0 0.5]); +set(gca, 'xtick', 1:4, 'ytick', 1:4, 'xticklabel', [{'A'}, {'B'}, {'C'}, {'D'}], 'yticklabel', [{'W'}, {'X'}, {'Y'}, {'X'}]); +colormap(fnSP, 'jet'); +title('Fam Decode Nov'); +fDiffSP = subplot(3,3,3); +famDecodeDiff = odorCollapseDecode(:,1:4,1) - odorCollapseDecode(:,5:8,1); +imagesc(famDecodeDiff, [-0.1 0.1]); +set(gca, 'xtick', 1:4, 'ytick', 1:4, 'xticklabel', [{'A-W'}, {'B-X'}, {'C-Y'}, {'D-Z'}], 'yticklabel', [{'W-A'}, {'X-B'}, {'Y-C'}, {'Z-D'}]); +colormap(fDiffSP, 'gray'); +title('Fam-Nov Difference') +nfSP = subplot(3,3,4); +imagesc(odorCollapseDecode(:,1:4,2), [0 0.5]); +set(gca, 'xtick', 1:4, 'ytick', 1:4, 'xticklabel', [{'W'}, {'X'}, {'Y'}, {'X'}], 'yticklabel', [{'A'}, {'B'}, {'C'}, {'D'}]); +colormap(nfSP, 'jet'); +title('Nov Decode Fam'); +nnSP = subplot(3,3,5); +imagesc(odorCollapseDecode(:,5:8,2), [0 0.5]); +set(gca, 'xtick', 1:4, 'ytick', 1:4, 'xticklabel', [{'W'}, {'X'}, {'Y'}, {'X'}], 'yticklabel', [{'W'}, {'X'}, {'Y'}, {'X'}]); +colormap(nnSP, 'jet'); +title('Nov Decode Nov'); +nDiffSP = subplot(3,3,6); +novDecodeDiff = odorCollapseDecode(:,1:4,2)-odorCollapseDecode(:,5:8,2); +imagesc(novDecodeDiff, [-0.1 0.1]); +set(gca, 'xtick', 1:4, 'ytick', 1:4, 'xticklabel', [{'W-A'}, {'X-B'}, {'Y-C'}, {'Z-D'}], 'yticklabel', [{'A-W'}, {'B-X'}, {'C-Y'}, {'D-Z'}]); +colormap(nDiffSP, 'gray'); + +subplot(3,3,7) +tempFD = famDecodeDiff(2:4,2:4); +tempND = novDecodeDiff(2:4,2:4); +corrScatPlot(tempFD(:), tempND(:), 'Familiar Difference', 'Novel Difference'); +title('Correlation Sans Pos1'); +subplot(3,3,8) +tempFD = famDecodeDiff(:); +tempND = novDecodeDiff(:); +corrScatPlot(tempFD(2:end), tempND(2:end), 'Familiar Difference', 'Novel Difference'); +title('Correlation Sans 1-1'); +subplot(3,3,9) +corrScatPlot(tempFD(:), tempND(:), 'Familiar Difference', 'Novel Difference'); +title('Correlation w/All'); + + +%% +%%*******************************************************%% +%%********************** Functions **********************%% +%%*******************************************************%% +function [binnedMtx, trialTime, trialInfo] = OrganizeAndBinSpikes(ensembleMatrix, behavMatrix, behavMatrixColIDs, alignment, window, binSize, dsRate) +% First check to make sure reward signal is present within the +% behaviorMatrix +if sum(strcmp(behavMatrixColIDs, 'RewardSignal'))==0 + warning('Reward signal missing from behavior matrix, update the behavior matrix. OR ignore this if reward signal wasn''t recorded as plx flag'); +end +% Organize behavior data and Extract Ensemble Spiking +% Taking 1/2 the binSize on either end to get rid of edge effects. +td = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, [window(1)-(binSize/2/1000) window(2)+(binSize/2/1000)], alignment); +trialEnsemble = ExtractTrialData_SM(td, ensembleMatrix(:,2:end)); %#ok<*NODEF> +ensembleMtx = cell2mat(reshape(trialEnsemble, [1 1 length(trialEnsemble)])); +if strcmp(alignment, 'PokeIn') + trialTimes = behavMatrix(td(1).TrialLogVect,1) - behavMatrix(td(1).PokeInIndex,1); +else + trialTimes = behavMatrix(td(1).TrialLogVect,1) - behavMatrix(td(1).PokeOutIndex,1); +end + +%% Bin the spiking data +% First convolve the entire trialEnsembleMtx with a square to bin the +% spikes +binnedEnsembleMtx = nan(size(ensembleMtx)); +for t = 1:size(ensembleMtx,3) + for u = 1:size(ensembleMtx,2) + binnedEnsembleMtx(:,u,t) = conv(ensembleMtx(:,u,t), ones(1,binSize)./(binSize/1000), 'same'); % Spike Rate +% binnedEnsembleMtx(:,u,t) = conv(trialEnsembleMtx(:,u,t), ones(1,binSize), 'same'); % Spike Count + end +end +% Now remove the binSize/2 padding +unPaddedBinnedEnsembleMtx = binnedEnsembleMtx((binSize/2)+1:end-(binSize/2),:,:); +trialTimes = trialTimes((binSize/2)+1:end-(binSize/2)); +% Now downsample the binned matrix +dsVect = downsample(1:size(unPaddedBinnedEnsembleMtx,1), dsRate); +binnedMtx = unPaddedBinnedEnsembleMtx(dsVect,:,:); +trialTime = trialTimes(dsVect); + +trialInfo = [[td.TrialNum]', [td.SequenceNum]', [td.Position]', [td.Odor]', ... + [td.Performance]',[td.TranspositionDistance]', [td.PokeDuration]', [td.WithdrawLatency]']; + +end + +%% +function post = CalcStaticBayesPost(likely, obsv, binSize) +post = nan(size(obsv,1), size(likely,1), size(obsv,3)); +for trl = 1:size(obsv,3) + for t = 1:size(obsv,1) + p = nan(size(likely)); + curPopVect = obsv(t,:,trl)*(binSize/1000); + curPopFact = factorial(curPopVect); + for u = 1:size(likely,2) + curAvgUniFR = likely(:,u); + p(:,u) = (((binSize/1000).*curAvgUniFR).^curPopVect(u))./curPopFact(u); + end + pp = prod(p,2); + ee = exp(-((binSize/1000)*sum(likely,2))); + tempPost = pp.*ee; + post(t,:,trl) = tempPost./sum(tempPost); + end +end +end + +%% +function decode = DecodeBayesPost(post, id) +% Assumes post is in the structure of ObservTime X LikelyTime X Trial +decode = nan(size(post,1),size(post,3)); +for o = 1:size(post,3) + for d = 1:size(post,1) + if ~isnan(post(d,1,o)) + decode(d,o) = id(find(post(d,:,o)==max(post(d,:,o)),1,'first')); + end + end +end +end + +%% +function decode = TabulateBayesPost(post, id) +idS = unique(id); +decode = nan(size(post,1), size(post,3), length(idS)); +for trl = 1:size(post,3) + for t = 1:size(post,1) + for iD = 1:length(idS) + decode(t,trl,iD) = sum(post(t,id==idS(iD),trl)); + end + end +end +end \ No newline at end of file diff --git a/Analyses/PFC/SimpleBetaViewer_Prototype.m b/Analyses/PFC/SimpleBetaViewer_Prototype.m index 7ce59c3..b76f17a 100644 --- a/Analyses/PFC/SimpleBetaViewer_Prototype.m +++ b/Analyses/PFC/SimpleBetaViewer_Prototype.m @@ -32,171 +32,183 @@ targDur = nanmean([tdPI.RewardSignalIndex]-[tdPI.PokeInIndex])/1000; targVar = nanstd([tdPI.RewardSignalIndex]-[tdPI.PokeInIndex])/1000; +% Define time period of the session based on first and last trial to remove +% sleep periods from the z-score calculation +ssnLog = false(size(behavMatrix,1),1); +ssnLog(tdPI(1).PokeInIndex-500:max([tdPI.PokeOutIndex])+500) = true; + %% +wlCorrVals = cell(size(smFileList)); +tetIDs = cell(size(smFileList)); for f = 1:length(smFileList) % Load File load([smPath '\' smFileList{f}]); % Identify Beta, get envelope and z-score - betaCol = cellfun(@(a)~isempty(a), regexp(statMatrixColIDs, 'Beta\>')); - env = abs(hilbert(statMatrix(:,betaCol))); - zEnv = zscore(env); + betaCol = cellfun(@(a)~isempty(a), regexp(statMatrixColIDs, '_Beta\>')); + tet = strsplit(statMatrixColIDs{betaCol}, '_'); + tetIDs(f) = tet(1); +% env = abs(hilbert(statMatrix(:,betaCol))); + env = abs(hilbert(statMatrix(:,betaCol))).^2; + zEnv = zeros(size(statMatrix,1),1); + zEnv(ssnLog) = zscore(env(ssnLog)); % InSeq v OutSeq Beta Power Overall iscBetaPI = cell2mat(ExtractTrialData_SM(tdPI(isLog & perfLog), zEnv)); oscBetaPI = cell2mat(ExtractTrialData_SM(tdPI(~isLog & perfLog), zEnv)); iscBetaPO = cell2mat(ExtractTrialData_SM(tdPO(isLog & perfLog), zEnv)); oscBetaPO = cell2mat(ExtractTrialData_SM(tdPO(~isLog & perfLog), zEnv)); - figure; - piSP = subplot(6,1,1:2); - line(piWindow, [0 0], 'linestyle', ':', 'color', 'k'); - hold on; - plot(piWindow(1):1/1000:piWindow(2), mean(iscBetaPI,2), 'color', 'blue'); - patch('YData', [mean(iscBetaPI,2)+std(iscBetaPI,1,2)/sqrt(size(iscBetaPI,2)-1); flipud(mean(iscBetaPI,2)-std(iscBetaPI,1,2)/sqrt(size(iscBetaPI,2)-1))],... - 'XData', [piWindow(1):1/1000:piWindow(2), piWindow(2):-1/1000:piWindow(1)],... - 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); - plot(piWindow(1):1/1000:piWindow(2), mean(oscBetaPI,2), 'color', 'red'); - patch('YData', [mean(oscBetaPI,2)+std(oscBetaPI,1,2)/sqrt(size(oscBetaPI,2)-1); flipud(mean(oscBetaPI,2)-std(oscBetaPI,1,2)/sqrt(size(oscBetaPI,2)-1))],... - 'XData', [piWindow(1):1/1000:piWindow(2), piWindow(2):-1/1000:piWindow(1)],... - 'FaceColor', 'red', 'FaceAlpha', .3, 'EdgeColor', 'red', 'EdgeAlpha', 0.5); - title('Poke In Aligned'); - subplot(6,1,3) - histogram(pokeDur(isLog & perfLog),piWindow(1):0.01:piWindow(2), 'normalization', 'count'); - hold on; - histogram(pokeDur(~isLog & perfLog),piWindow(1):0.01:piWindow(2), 'normalization', 'count'); - axis tight - set(gca, 'xlim', get(piSP, 'xlim')); - poSP = subplot(6,1,4:5); - line(poWindow, [0 0], 'linestyle', ':', 'color', 'k'); - hold on; - plot(poWindow(1):1/1000:poWindow(2), mean(iscBetaPO,2), 'color', 'blue'); - patch('YData', [mean(iscBetaPO,2)+std(iscBetaPO,1,2)/sqrt(size(iscBetaPO,2)-1); flipud(mean(iscBetaPO,2)-std(iscBetaPO,1,2)/sqrt(size(iscBetaPO,2)-1))],... - 'XData', [poWindow(1):1/1000:poWindow(2), poWindow(2):-1/1000:poWindow(1)],... - 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); - plot(poWindow(1):1/1000:poWindow(2), mean(oscBetaPO,2), 'color', 'red'); - patch('YData', [mean(oscBetaPO,2)+std(oscBetaPO,1,2)/sqrt(size(oscBetaPO,2)-1); flipud(mean(oscBetaPO,2)-std(oscBetaPO,1,2)/sqrt(size(oscBetaPO,2)-1))],... - 'XData', [poWindow(1):1/1000:poWindow(2), poWindow(2):-1/1000:poWindow(1)],... - 'FaceColor', 'red', 'FaceAlpha', .3, 'EdgeColor', 'red', 'EdgeAlpha', 0.5); - title('Poke Out Aligned'); - subplot(6,1,6) - histogram(wdLat(isLog & perfLog)*-1, poWindow(1):0.01:poWindow(2)); - hold on; - histogram(pokeDur(~isLog & perfLog)*-1, poWindow(1):0.01:poWindow(2)); - axis tight - set(gca, 'xlim', get(poSP, 'xlim')); - annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', smFileList{f},... - 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); - drawnow; - - % InSeq & OutSeq Beta Broken down by Position - figure; - sp(1) = subplot(2,4,1); - iscBetaPI1 = cell2mat(ExtractTrialData_SM(tdPI(isLog & perfLog & p1Log), zEnv)); - line(piWindow, [0 0], 'linestyle', ':', 'color', 'k'); - hold on; - plot(piWindow(1):1/1000:piWindow(2), mean(iscBetaPI1,2), 'color', 'blue'); - patch('YData', [mean(iscBetaPI1,2)+std(iscBetaPI1,1,2)/sqrt(size(iscBetaPI1,2)-1); flipud(mean(iscBetaPI1,2)-std(iscBetaPI1,1,2)/sqrt(size(iscBetaPI1,2)-1))],... - 'XData', [piWindow(1):1/1000:piWindow(2), piWindow(2):-1/1000:piWindow(1)],... - 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); - ylabel('Poke In Aligned'); - title('Position 1'); - sp(2) = subplot(2,4,2); - iscBetaPI2 = cell2mat(ExtractTrialData_SM(tdPI(isLog & perfLog & p2Log), zEnv)); - oscBetaPI2 = cell2mat(ExtractTrialData_SM(tdPI(~isLog & perfLog & p2Log), zEnv)); - line(piWindow, [0 0], 'linestyle', ':', 'color', 'k'); - hold on; - plot(piWindow(1):1/1000:piWindow(2), mean(iscBetaPI2,2), 'color', 'blue'); - patch('YData', [mean(iscBetaPI2,2)+std(iscBetaPI2,1,2)/sqrt(size(iscBetaPI2,2)-1); flipud(mean(iscBetaPI2,2)-std(iscBetaPI2,1,2)/sqrt(size(iscBetaPI2,2)-1))],... - 'XData', [piWindow(1):1/1000:piWindow(2), piWindow(2):-1/1000:piWindow(1)],... - 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); - plot(piWindow(1):1/1000:piWindow(2), mean(oscBetaPI2,2), 'color', 'red'); - patch('YData', [mean(oscBetaPI2,2)+std(oscBetaPI2,1,2)/sqrt(size(oscBetaPI2,2)-1); flipud(mean(oscBetaPI2,2)-std(oscBetaPI2,1,2)/sqrt(size(oscBetaPI2,2)-1))],... - 'XData', [piWindow(1):1/1000:piWindow(2), piWindow(2):-1/1000:piWindow(1)],... - 'FaceColor', 'red', 'FaceAlpha', .3, 'EdgeColor', 'red', 'EdgeAlpha', 0.5); - title('Position 2'); - sp(3) = subplot(2,4,3); - iscBetaPI3 = cell2mat(ExtractTrialData_SM(tdPI(isLog & perfLog & p3Log), zEnv)); - oscBetaPI3 = cell2mat(ExtractTrialData_SM(tdPI(~isLog & perfLog & p3Log), zEnv)); - line(piWindow, [0 0], 'linestyle', ':', 'color', 'k'); - hold on; - plot(piWindow(1):1/1000:piWindow(2), mean(iscBetaPI3,2), 'color', 'blue'); - patch('YData', [mean(iscBetaPI3,2)+std(iscBetaPI3,1,2)/sqrt(size(iscBetaPI3,2)-1); flipud(mean(iscBetaPI3,2)-std(iscBetaPI3,1,2)/sqrt(size(iscBetaPI3,2)-1))],... - 'XData', [piWindow(1):1/1000:piWindow(2), piWindow(2):-1/1000:piWindow(1)],... - 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); - plot(piWindow(1):1/1000:piWindow(2), mean(oscBetaPI3,2), 'color', 'red'); - patch('YData', [mean(oscBetaPI3,2)+std(oscBetaPI3,1,2)/sqrt(size(oscBetaPI3,2)-1); flipud(mean(oscBetaPI3,2)-std(oscBetaPI3,1,2)/sqrt(size(oscBetaPI3,2)-1))],... - 'XData', [piWindow(1):1/1000:piWindow(2), piWindow(2):-1/1000:piWindow(1)],... - 'FaceColor', 'red', 'FaceAlpha', .3, 'EdgeColor', 'red', 'EdgeAlpha', 0.5); - title('Position 3'); - sp(4) = subplot(2,4,4); - iscBetaPI4 = cell2mat(ExtractTrialData_SM(tdPI(isLog & perfLog & p4Log), zEnv)); - oscBetaPI4 = cell2mat(ExtractTrialData_SM(tdPI(~isLog & perfLog & p4Log), zEnv)); - line(piWindow, [0 0], 'linestyle', ':', 'color', 'k'); - hold on; - plot(piWindow(1):1/1000:piWindow(2), mean(iscBetaPI4,2), 'color', 'blue'); - patch('YData', [mean(iscBetaPI4,2)+std(iscBetaPI4,1,2)/sqrt(size(iscBetaPI4,2)-1); flipud(mean(iscBetaPI4,2)-std(iscBetaPI4,1,2)/sqrt(size(iscBetaPI4,2)-1))],... - 'XData', [piWindow(1):1/1000:piWindow(2), piWindow(2):-1/1000:piWindow(1)],... - 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); - plot(piWindow(1):1/1000:piWindow(2), mean(oscBetaPI4,2), 'color', 'red'); - patch('YData', [mean(oscBetaPI4,2)+std(oscBetaPI4,1,2)/sqrt(size(oscBetaPI4,2)-1); flipud(mean(oscBetaPI4,2)-std(oscBetaPI4,1,2)/sqrt(size(oscBetaPI4,2)-1))],... - 'XData', [piWindow(1):1/1000:piWindow(2), piWindow(2):-1/1000:piWindow(1)],... - 'FaceColor', 'red', 'FaceAlpha', .3, 'EdgeColor', 'red', 'EdgeAlpha', 0.5); - title('Position 4'); - sp(5) = subplot(2,4,5); - iscBetaPO1 = cell2mat(ExtractTrialData_SM(tdPO(isLog & perfLog & p1Log), zEnv)); - line(poWindow, [0 0], 'linestyle', ':', 'color', 'k'); - hold on; - plot(poWindow(1):1/1000:poWindow(2), mean(iscBetaPO1,2), 'color', 'blue'); - patch('YData', [mean(iscBetaPO1,2)+std(iscBetaPO1,1,2)/sqrt(size(iscBetaPO1,2)-1); flipud(mean(iscBetaPO1,2)-std(iscBetaPO1,1,2)/sqrt(size(iscBetaPO1,2)-1))],... - 'XData', [poWindow(1):1/1000:poWindow(2), poWindow(2):-1/1000:poWindow(1)],... - 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); - ylabel('Poke Out Aligned'); - sp(6) = subplot(2,4,6); - iscBetaPO2 = cell2mat(ExtractTrialData_SM(tdPO(isLog & perfLog & p2Log), zEnv)); - oscBetaPO2 = cell2mat(ExtractTrialData_SM(tdPO(~isLog & perfLog & p2Log), zEnv)); - line(poWindow, [0 0], 'linestyle', ':', 'color', 'k'); - hold on; - plot(poWindow(1):1/1000:poWindow(2), mean(iscBetaPO2,2), 'color', 'blue'); - patch('YData', [mean(iscBetaPO2,2)+std(iscBetaPO2,1,2)/sqrt(size(iscBetaPO2,2)-1); flipud(mean(iscBetaPO2,2)-std(iscBetaPO2,1,2)/sqrt(size(iscBetaPO2,2)-1))],... - 'XData', [poWindow(1):1/1000:poWindow(2), poWindow(2):-1/1000:poWindow(1)],... - 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); - plot(poWindow(1):1/1000:poWindow(2), mean(oscBetaPO2,2), 'color', 'red'); - patch('YData', [mean(oscBetaPO2,2)+std(oscBetaPO2,1,2)/sqrt(size(oscBetaPO2,2)-1); flipud(mean(oscBetaPO2,2)-std(oscBetaPO2,1,2)/sqrt(size(oscBetaPO2,2)-1))],... - 'XData', [poWindow(1):1/1000:poWindow(2), poWindow(2):-1/1000:poWindow(1)],... - 'FaceColor', 'red', 'FaceAlpha', .3, 'EdgeColor', 'red', 'EdgeAlpha', 0.5); - sp(7) = subplot(2,4,7); - iscBetaPO3 = cell2mat(ExtractTrialData_SM(tdPO(isLog & perfLog & p3Log), zEnv)); - oscBetaPO3 = cell2mat(ExtractTrialData_SM(tdPO(~isLog & perfLog & p3Log), zEnv)); - line(poWindow, [0 0], 'linestyle', ':', 'color', 'k'); - hold on; - plot(poWindow(1):1/1000:poWindow(2), mean(iscBetaPO3,2), 'color', 'blue'); - patch('YData', [mean(iscBetaPO3,2)+std(iscBetaPO3,1,2)/sqrt(size(iscBetaPO3,2)-1); flipud(mean(iscBetaPO3,2)-std(iscBetaPO3,1,2)/sqrt(size(iscBetaPO3,2)-1))],... - 'XData', [poWindow(1):1/1000:poWindow(2), poWindow(2):-1/1000:poWindow(1)],... - 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); - plot(poWindow(1):1/1000:poWindow(2), mean(oscBetaPO3,2), 'color', 'red'); - patch('YData', [mean(oscBetaPO3,2)+std(oscBetaPO3,1,2)/sqrt(size(oscBetaPO3,2)-1); flipud(mean(oscBetaPO3,2)-std(oscBetaPO3,1,2)/sqrt(size(oscBetaPO3,2)-1))],... - 'XData', [poWindow(1):1/1000:poWindow(2), poWindow(2):-1/1000:poWindow(1)],... - 'FaceColor', 'red', 'FaceAlpha', .3, 'EdgeColor', 'red', 'EdgeAlpha', 0.5); - sp(8) = subplot(2,4,8); - iscBetaPO4 = cell2mat(ExtractTrialData_SM(tdPO(isLog & perfLog & p4Log), zEnv)); - oscBetaPO4 = cell2mat(ExtractTrialData_SM(tdPO(~isLog & perfLog & p4Log), zEnv)); - line(poWindow, [0 0], 'linestyle', ':', 'color', 'k'); - hold on; - plot(poWindow(1):1/1000:poWindow(2), mean(iscBetaPO4,2), 'color', 'blue'); - patch('YData', [mean(iscBetaPO4,2)+std(iscBetaPO4,1,2)/sqrt(size(iscBetaPO4,2)-1); flipud(mean(iscBetaPO4,2)-std(iscBetaPO4,1,2)/sqrt(size(iscBetaPO4,2)-1))],... - 'XData', [poWindow(1):1/1000:poWindow(2), poWindow(2):-1/1000:poWindow(1)],... - 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); - plot(poWindow(1):1/1000:poWindow(2), mean(oscBetaPO4,2), 'color', 'red'); - patch('YData', [mean(oscBetaPO4,2)+std(oscBetaPO4,1,2)/sqrt(size(oscBetaPO4,2)-1); flipud(mean(oscBetaPO4,2)-std(oscBetaPO4,1,2)/sqrt(size(oscBetaPO4,2)-1))],... - 'XData', [poWindow(1):1/1000:poWindow(2), poWindow(2):-1/1000:poWindow(1)],... - 'FaceColor', 'red', 'FaceAlpha', .3, 'EdgeColor', 'red', 'EdgeAlpha', 0.5); - linkaxes(sp(1:4), 'x'); - linkaxes(sp(5:8), 'x'); - set(sp(1), 'xlim', piWindow); - set(sp(8), 'xlim', poWindow); - linkaxes(sp, 'y'); - annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', smFileList{f},... - 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); - drawnow; +% figure; +% piSP = subplot(6,1,1:2); +% line(piWindow, [0 0], 'linestyle', ':', 'color', 'k'); +% hold on; +% plot(piWindow(1):1/1000:piWindow(2), mean(iscBetaPI,2), 'color', 'blue'); +% patch('YData', [mean(iscBetaPI,2)+std(iscBetaPI,1,2)/sqrt(size(iscBetaPI,2)-1); flipud(mean(iscBetaPI,2)-std(iscBetaPI,1,2)/sqrt(size(iscBetaPI,2)-1))],... +% 'XData', [piWindow(1):1/1000:piWindow(2), piWindow(2):-1/1000:piWindow(1)],... +% 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); +% plot(piWindow(1):1/1000:piWindow(2), mean(oscBetaPI,2), 'color', 'red'); +% patch('YData', [mean(oscBetaPI,2)+std(oscBetaPI,1,2)/sqrt(size(oscBetaPI,2)-1); flipud(mean(oscBetaPI,2)-std(oscBetaPI,1,2)/sqrt(size(oscBetaPI,2)-1))],... +% 'XData', [piWindow(1):1/1000:piWindow(2), piWindow(2):-1/1000:piWindow(1)],... +% 'FaceColor', 'red', 'FaceAlpha', .3, 'EdgeColor', 'red', 'EdgeAlpha', 0.5); +% title('Poke In Aligned'); +% subplot(6,1,3) +% histogram(pokeDur(isLog & perfLog),piWindow(1):0.01:piWindow(2), 'normalization', 'count'); +% hold on; +% histogram(pokeDur(~isLog & perfLog),piWindow(1):0.01:piWindow(2), 'normalization', 'count'); +% axis tight +% set(gca, 'xlim', get(piSP, 'xlim')); +% poSP = subplot(6,1,4:5); +% line(poWindow, [0 0], 'linestyle', ':', 'color', 'k'); +% hold on; +% plot(poWindow(1):1/1000:poWindow(2), mean(iscBetaPO,2), 'color', 'blue'); +% patch('YData', [mean(iscBetaPO,2)+std(iscBetaPO,1,2)/sqrt(size(iscBetaPO,2)-1); flipud(mean(iscBetaPO,2)-std(iscBetaPO,1,2)/sqrt(size(iscBetaPO,2)-1))],... +% 'XData', [poWindow(1):1/1000:poWindow(2), poWindow(2):-1/1000:poWindow(1)],... +% 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); +% plot(poWindow(1):1/1000:poWindow(2), mean(oscBetaPO,2), 'color', 'red'); +% patch('YData', [mean(oscBetaPO,2)+std(oscBetaPO,1,2)/sqrt(size(oscBetaPO,2)-1); flipud(mean(oscBetaPO,2)-std(oscBetaPO,1,2)/sqrt(size(oscBetaPO,2)-1))],... +% 'XData', [poWindow(1):1/1000:poWindow(2), poWindow(2):-1/1000:poWindow(1)],... +% 'FaceColor', 'red', 'FaceAlpha', .3, 'EdgeColor', 'red', 'EdgeAlpha', 0.5); +% title('Poke Out Aligned'); +% subplot(6,1,6) +% histogram(wdLat(isLog & perfLog)*-1, poWindow(1):0.01:poWindow(2)); +% hold on; +% histogram(pokeDur(~isLog & perfLog)*-1, poWindow(1):0.01:poWindow(2)); +% axis tight +% set(gca, 'xlim', get(poSP, 'xlim')); +% annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', smFileList{f},... +% 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +% drawnow; +% close gcf +% +% % InSeq & OutSeq Beta Broken down by Position +% figure; +% sp(1) = subplot(2,4,1); +% iscBetaPI1 = cell2mat(ExtractTrialData_SM(tdPI(isLog & perfLog & p1Log), zEnv)); +% line(piWindow, [0 0], 'linestyle', ':', 'color', 'k'); +% hold on; +% plot(piWindow(1):1/1000:piWindow(2), mean(iscBetaPI1,2), 'color', 'blue'); +% patch('YData', [mean(iscBetaPI1,2)+std(iscBetaPI1,1,2)/sqrt(size(iscBetaPI1,2)-1); flipud(mean(iscBetaPI1,2)-std(iscBetaPI1,1,2)/sqrt(size(iscBetaPI1,2)-1))],... +% 'XData', [piWindow(1):1/1000:piWindow(2), piWindow(2):-1/1000:piWindow(1)],... +% 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); +% ylabel('Poke In Aligned'); +% title('Position 1'); +% sp(2) = subplot(2,4,2); +% iscBetaPI2 = cell2mat(ExtractTrialData_SM(tdPI(isLog & perfLog & p2Log), zEnv)); +% oscBetaPI2 = cell2mat(ExtractTrialData_SM(tdPI(~isLog & perfLog & p2Log), zEnv)); +% line(piWindow, [0 0], 'linestyle', ':', 'color', 'k'); +% hold on; +% plot(piWindow(1):1/1000:piWindow(2), mean(iscBetaPI2,2), 'color', 'blue'); +% patch('YData', [mean(iscBetaPI2,2)+std(iscBetaPI2,1,2)/sqrt(size(iscBetaPI2,2)-1); flipud(mean(iscBetaPI2,2)-std(iscBetaPI2,1,2)/sqrt(size(iscBetaPI2,2)-1))],... +% 'XData', [piWindow(1):1/1000:piWindow(2), piWindow(2):-1/1000:piWindow(1)],... +% 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); +% plot(piWindow(1):1/1000:piWindow(2), mean(oscBetaPI2,2), 'color', 'red'); +% patch('YData', [mean(oscBetaPI2,2)+std(oscBetaPI2,1,2)/sqrt(size(oscBetaPI2,2)-1); flipud(mean(oscBetaPI2,2)-std(oscBetaPI2,1,2)/sqrt(size(oscBetaPI2,2)-1))],... +% 'XData', [piWindow(1):1/1000:piWindow(2), piWindow(2):-1/1000:piWindow(1)],... +% 'FaceColor', 'red', 'FaceAlpha', .3, 'EdgeColor', 'red', 'EdgeAlpha', 0.5); +% title('Position 2'); +% sp(3) = subplot(2,4,3); +% iscBetaPI3 = cell2mat(ExtractTrialData_SM(tdPI(isLog & perfLog & p3Log), zEnv)); +% oscBetaPI3 = cell2mat(ExtractTrialData_SM(tdPI(~isLog & perfLog & p3Log), zEnv)); +% line(piWindow, [0 0], 'linestyle', ':', 'color', 'k'); +% hold on; +% plot(piWindow(1):1/1000:piWindow(2), mean(iscBetaPI3,2), 'color', 'blue'); +% patch('YData', [mean(iscBetaPI3,2)+std(iscBetaPI3,1,2)/sqrt(size(iscBetaPI3,2)-1); flipud(mean(iscBetaPI3,2)-std(iscBetaPI3,1,2)/sqrt(size(iscBetaPI3,2)-1))],... +% 'XData', [piWindow(1):1/1000:piWindow(2), piWindow(2):-1/1000:piWindow(1)],... +% 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); +% plot(piWindow(1):1/1000:piWindow(2), mean(oscBetaPI3,2), 'color', 'red'); +% patch('YData', [mean(oscBetaPI3,2)+std(oscBetaPI3,1,2)/sqrt(size(oscBetaPI3,2)-1); flipud(mean(oscBetaPI3,2)-std(oscBetaPI3,1,2)/sqrt(size(oscBetaPI3,2)-1))],... +% 'XData', [piWindow(1):1/1000:piWindow(2), piWindow(2):-1/1000:piWindow(1)],... +% 'FaceColor', 'red', 'FaceAlpha', .3, 'EdgeColor', 'red', 'EdgeAlpha', 0.5); +% title('Position 3'); +% sp(4) = subplot(2,4,4); +% iscBetaPI4 = cell2mat(ExtractTrialData_SM(tdPI(isLog & perfLog & p4Log), zEnv)); +% oscBetaPI4 = cell2mat(ExtractTrialData_SM(tdPI(~isLog & perfLog & p4Log), zEnv)); +% line(piWindow, [0 0], 'linestyle', ':', 'color', 'k'); +% hold on; +% plot(piWindow(1):1/1000:piWindow(2), mean(iscBetaPI4,2), 'color', 'blue'); +% patch('YData', [mean(iscBetaPI4,2)+std(iscBetaPI4,1,2)/sqrt(size(iscBetaPI4,2)-1); flipud(mean(iscBetaPI4,2)-std(iscBetaPI4,1,2)/sqrt(size(iscBetaPI4,2)-1))],... +% 'XData', [piWindow(1):1/1000:piWindow(2), piWindow(2):-1/1000:piWindow(1)],... +% 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); +% plot(piWindow(1):1/1000:piWindow(2), mean(oscBetaPI4,2), 'color', 'red'); +% patch('YData', [mean(oscBetaPI4,2)+std(oscBetaPI4,1,2)/sqrt(size(oscBetaPI4,2)-1); flipud(mean(oscBetaPI4,2)-std(oscBetaPI4,1,2)/sqrt(size(oscBetaPI4,2)-1))],... +% 'XData', [piWindow(1):1/1000:piWindow(2), piWindow(2):-1/1000:piWindow(1)],... +% 'FaceColor', 'red', 'FaceAlpha', .3, 'EdgeColor', 'red', 'EdgeAlpha', 0.5); +% title('Position 4'); +% sp(5) = subplot(2,4,5); +% iscBetaPO1 = cell2mat(ExtractTrialData_SM(tdPO(isLog & perfLog & p1Log), zEnv)); +% line(poWindow, [0 0], 'linestyle', ':', 'color', 'k'); +% hold on; +% plot(poWindow(1):1/1000:poWindow(2), mean(iscBetaPO1,2), 'color', 'blue'); +% patch('YData', [mean(iscBetaPO1,2)+std(iscBetaPO1,1,2)/sqrt(size(iscBetaPO1,2)-1); flipud(mean(iscBetaPO1,2)-std(iscBetaPO1,1,2)/sqrt(size(iscBetaPO1,2)-1))],... +% 'XData', [poWindow(1):1/1000:poWindow(2), poWindow(2):-1/1000:poWindow(1)],... +% 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); +% ylabel('Poke Out Aligned'); +% sp(6) = subplot(2,4,6); +% iscBetaPO2 = cell2mat(ExtractTrialData_SM(tdPO(isLog & perfLog & p2Log), zEnv)); +% oscBetaPO2 = cell2mat(ExtractTrialData_SM(tdPO(~isLog & perfLog & p2Log), zEnv)); +% line(poWindow, [0 0], 'linestyle', ':', 'color', 'k'); +% hold on; +% plot(poWindow(1):1/1000:poWindow(2), mean(iscBetaPO2,2), 'color', 'blue'); +% patch('YData', [mean(iscBetaPO2,2)+std(iscBetaPO2,1,2)/sqrt(size(iscBetaPO2,2)-1); flipud(mean(iscBetaPO2,2)-std(iscBetaPO2,1,2)/sqrt(size(iscBetaPO2,2)-1))],... +% 'XData', [poWindow(1):1/1000:poWindow(2), poWindow(2):-1/1000:poWindow(1)],... +% 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); +% plot(poWindow(1):1/1000:poWindow(2), mean(oscBetaPO2,2), 'color', 'red'); +% patch('YData', [mean(oscBetaPO2,2)+std(oscBetaPO2,1,2)/sqrt(size(oscBetaPO2,2)-1); flipud(mean(oscBetaPO2,2)-std(oscBetaPO2,1,2)/sqrt(size(oscBetaPO2,2)-1))],... +% 'XData', [poWindow(1):1/1000:poWindow(2), poWindow(2):-1/1000:poWindow(1)],... +% 'FaceColor', 'red', 'FaceAlpha', .3, 'EdgeColor', 'red', 'EdgeAlpha', 0.5); +% sp(7) = subplot(2,4,7); +% iscBetaPO3 = cell2mat(ExtractTrialData_SM(tdPO(isLog & perfLog & p3Log), zEnv)); +% oscBetaPO3 = cell2mat(ExtractTrialData_SM(tdPO(~isLog & perfLog & p3Log), zEnv)); +% line(poWindow, [0 0], 'linestyle', ':', 'color', 'k'); +% hold on; +% plot(poWindow(1):1/1000:poWindow(2), mean(iscBetaPO3,2), 'color', 'blue'); +% patch('YData', [mean(iscBetaPO3,2)+std(iscBetaPO3,1,2)/sqrt(size(iscBetaPO3,2)-1); flipud(mean(iscBetaPO3,2)-std(iscBetaPO3,1,2)/sqrt(size(iscBetaPO3,2)-1))],... +% 'XData', [poWindow(1):1/1000:poWindow(2), poWindow(2):-1/1000:poWindow(1)],... +% 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); +% plot(poWindow(1):1/1000:poWindow(2), mean(oscBetaPO3,2), 'color', 'red'); +% patch('YData', [mean(oscBetaPO3,2)+std(oscBetaPO3,1,2)/sqrt(size(oscBetaPO3,2)-1); flipud(mean(oscBetaPO3,2)-std(oscBetaPO3,1,2)/sqrt(size(oscBetaPO3,2)-1))],... +% 'XData', [poWindow(1):1/1000:poWindow(2), poWindow(2):-1/1000:poWindow(1)],... +% 'FaceColor', 'red', 'FaceAlpha', .3, 'EdgeColor', 'red', 'EdgeAlpha', 0.5); +% sp(8) = subplot(2,4,8); +% iscBetaPO4 = cell2mat(ExtractTrialData_SM(tdPO(isLog & perfLog & p4Log), zEnv)); +% oscBetaPO4 = cell2mat(ExtractTrialData_SM(tdPO(~isLog & perfLog & p4Log), zEnv)); +% line(poWindow, [0 0], 'linestyle', ':', 'color', 'k'); +% hold on; +% plot(poWindow(1):1/1000:poWindow(2), mean(iscBetaPO4,2), 'color', 'blue'); +% patch('YData', [mean(iscBetaPO4,2)+std(iscBetaPO4,1,2)/sqrt(size(iscBetaPO4,2)-1); flipud(mean(iscBetaPO4,2)-std(iscBetaPO4,1,2)/sqrt(size(iscBetaPO4,2)-1))],... +% 'XData', [poWindow(1):1/1000:poWindow(2), poWindow(2):-1/1000:poWindow(1)],... +% 'FaceColor', 'blue', 'FaceAlpha', .3, 'EdgeColor', 'blue', 'EdgeAlpha', 0.5); +% plot(poWindow(1):1/1000:poWindow(2), mean(oscBetaPO4,2), 'color', 'red'); +% patch('YData', [mean(oscBetaPO4,2)+std(oscBetaPO4,1,2)/sqrt(size(oscBetaPO4,2)-1); flipud(mean(oscBetaPO4,2)-std(oscBetaPO4,1,2)/sqrt(size(oscBetaPO4,2)-1))],... +% 'XData', [poWindow(1):1/1000:poWindow(2), poWindow(2):-1/1000:poWindow(1)],... +% 'FaceColor', 'red', 'FaceAlpha', .3, 'EdgeColor', 'red', 'EdgeAlpha', 0.5); +% linkaxes(sp(1:4), 'x'); +% linkaxes(sp(5:8), 'x'); +% set(sp(1), 'xlim', piWindow); +% set(sp(8), 'xlim', poWindow); +% linkaxes(sp, 'y'); +% annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', smFileList{f},... +% 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +% drawnow; % Examine Relationship between Withdrawal Latency and Beta Power iscBetaRS = cell2mat(ExtractTrialData_SM(tdRS(isLog & perfLog & ~p1Log), zEnv)); @@ -235,22 +247,31 @@ pdCorr = nan(size(iscBetaRS,1),1); wlCorr = nan(size(iscBetaRS,1),1); for t = 1:size(iscBetaRS,1) - tempCorrPD = corrcoef(iscBetaRS(t,:), pdTrls); - pdCorr(t) = tempCorrPD(2); - tempCorrWL = corrcoef(iscBetaRS(t,:), wlTrls); - wlCorr(t) = tempCorrWL(2); +% pdCorr(t) = corr(iscBetaRS(t,:)', pdTrls'); + pdCorr(t) = corr(iscBetaRS(t,:)', pdTrls', 'Type', 'Spearman'); +% wlCorr(t) = corr(iscBetaRS(t,:)', wlTrls'); + wlCorr(t) = corr(iscBetaRS(t,:)', wlTrls', 'Type', 'Spearman'); end + wlCorrVals{f} = wlCorr; subplot(2,4,7:8); plot(poWindow(1):1/1000:poWindow(2), pdCorr, 'color', 'k'); hold on; plot(poWindow(1):1/1000:poWindow(2), wlCorr, 'color', 'k', 'linestyle', '-.'); - legend('Poke Duration', 'Withdrawal Latency', 'location', 'northwest'); + legend('Poke Duration', 'Withdrawal Latency', 'location', 'southoutside'); line(poWindow, [0 0], 'color', 'k', 'linestyle', ':'); annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', smFileList{f},... 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); drawnow; + + figure; imagesc(piWindow(1):1/1000:piWindow(2), 1:size(iscBetaPI,2),iscBetaPI', [-2 5]); colormap jet; + annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', smFileList{f},... + 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); end -figure; imagesc(piWindow(1):1/1000:piWindow(2), 1:size(iscBetaPI,2),iscBetaPI', [-2 5]); colormap jet; -annotation('textbox', 'position', [0.025 0.025 0.7 0.05], 'String', smFileList{f},... - 'linestyle', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); +pkMtx = [([tdPI.PokeInIndex])', ([tdPI.PokeOutIndex])']; +figure; sp1 = subplot(3,1,1); plot(statMatrix(:,2)); sp2 = subplot(3,1,2); plot(statMatrix(:,betaCol)); sp3 = subplot(3,1,3); plot(zEnv); linkaxes([sp1 sp2 sp3], 'x'); hold on; plot(pkMtx', (ones(size(pkMtx))')*5, 'marker', '*', 'color', 'k'); + +%% +wlCorrVals = cell2mat(wlCorrVals'); +tetIDs = tetIDs'; +save('BetaWithdrawLatCorrSQR.mat', 'wlCorrVals', 'tetIDs'); \ No newline at end of file diff --git a/Analyses/PFC/slPFC_MLB_GroupPlot.m b/Analyses/PFC/slPFC_MLB_GroupPlot.m index b7a1dae..2e79705 100644 --- a/Analyses/PFC/slPFC_MLB_GroupPlot.m +++ b/Analyses/PFC/slPFC_MLB_GroupPlot.m @@ -8,6 +8,12 @@ %% decodes = cell(1,length(decodingFiles),4); osDiffs = cell(1,length(decodingFiles)); +skpDiffs = cell(1,length(decodingFiles)); +repDiffs = cell(1,length(decodingFiles)); +trlPrdDecodes = cell(1,length(decodingFiles)); +ssnSMI = nan(1,length(decodingFiles)); +ssnSMIsfp = nan(1,length(decodingFiles)); +trlPrdDprms = nan(4,length(decodingFiles)); for f = 1:length(decodingFiles) load(decodingFiles{f}); decodes{1,f,1} = decodings.Odor(:,1); @@ -15,21 +21,30 @@ decodes{1,f,3} = decodings.Odor(:,3); decodes{1,f,4} = decodings.Odor(:,4); osDiffs{f} = decodings.OSdiff; + skpDiffs{f} = decodings.OSdiffSKP; + repDiffs{f} = decodings.OSdiffREP; + trlPrdDecodes{f} = decodings.TrialPeriods; + ssnSMI(f) = decodings.SMIbehav; + ssnSMIsfp(f) = decodings.SMIbehavSFP; + trlPrdDprms(:,f) = decodings.TrialPeriodsDprm; end decodes = cell2mat(decodes); osDiffs = cell2mat(osDiffs); +skpDiffs = cell2mat(skpDiffs); +repDiffs = cell2mat(repDiffs); +%% figure; -plot(1:size(decodes,1), nanmean(decodes(:,:,1),2), 'color', [44/255 168/255 224/255], 'linewidth', 1); +plot(1:size(decodes,1), smooth(nanmean(decodes(:,:,1),2)), 'color', [44/255 168/255 224/255], 'linewidth', 1); hold on; -plot(1:size(decodes,1), nanmean(decodes(:,:,2),2), 'color', [154/255 133/255 122/255], 'linewidth', 1); -plot(1:size(decodes,1), nanmean(decodes(:,:,3),2), 'color', [9/255 161/255 74/255], 'linewidth', 1); -plot(1:size(decodes,1), nanmean(decodes(:,:,4),2), 'color', [128/255 66/255 151/255], 'linewidth', 1); -legend('A', 'B', 'C','D', 'location', 'southoutside', 'orientation', 'horizontal'); +plot(1:size(decodes,1), smooth(nanmean(decodes(:,:,2),2)), 'color', [154/255 133/255 122/255], 'linewidth', 1); +plot(1:size(decodes,1), smooth(nanmean(decodes(:,:,3),2)), 'color', [9/255 161/255 74/255], 'linewidth', 1); +plot(1:size(decodes,1), smooth(nanmean(decodes(:,:,4),2)), 'color', [128/255 66/255 151/255], 'linewidth', 1); +legend('1', '2', '3','4', 'location', 'southoutside', 'orientation', 'horizontal'); for o = 1:4 - tempPostMean = nanmean(decodes(:,:,o),2); + tempPostMean = smooth(nanmean(decodes(:,:,o),2)); % tempPostVar = nanstd(decodes(:,:,o),1,2); - tempPostVar = SEMcalc(decodes(:,:,o)')'; + tempPostVar = smooth(SEMcalc(decodes(:,:,o)')'); tp = patch('YData', [tempPostMean+tempPostVar; flipud(tempPostMean-tempPostVar)],... 'XData', [1:length(tempPostMean), length(tempPostMean):-1:1], 'FaceAlpha', .3); if o==1 @@ -57,22 +72,115 @@ line([size(decodings.POtime,1)/2+size(decodings.PItime,1)*3+size(decodings.POtime,1)*2 size(decodings.POtime,1)/2+size(decodings.PItime,1)*3+size(decodings.POtime,1)*2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); line([size(decodings.PItime,1)/2+size(decodings.PItime,1)*3+size(decodings.POtime,1)*3 size(decodings.PItime,1)/2+size(decodings.PItime,1)*3+size(decodings.POtime,1)*3], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); line([size(decodings.POtime,1)/2+size(decodings.PItime,1)*4+size(decodings.POtime,1)*3 size(decodings.POtime,1)/2+size(decodings.PItime,1)*4+size(decodings.POtime,1)*3], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); -ylabel([{'Decoded Odor'};{'% Trials'}]); +ylabel([{'Decoded Position'};{'% Trials'}]); xlabel('Sequence Time'); -title('GE11 Decodings (All InSeq, Leave-1-Out; Mean +/- SEM; 5xSessions)'); +title(sprintf('GE11 Decodings (All InSeq, Leave-1-Out; Mean +/- SEM; %ixSessions)', length(decodingFiles))); + +%% +odorLog = [ones(1,size(decodings.PItime,1)+size(decodings.POtime,1))*1,... + ones(1,size(decodings.PItime,1)+size(decodings.POtime,1))*2,... + ones(1,size(decodings.PItime,1)+size(decodings.POtime,1))*3,... + ones(1,size(decodings.PItime,1)+size(decodings.POtime,1))*4]; + +figure; +sps = nan(1,4); +for p = 1:4 + decLog = odorLog==p; + sps(p) = subplot(1,4,p); + plot(1:sum(decLog), smooth(nanmean(decodes(decLog,:,1),2)), 'color', [44/255 168/255 224/255], 'linewidth', 1); + hold on; + plot(1:sum(decLog), smooth(nanmean(decodes(decLog,:,2),2)), 'color', [154/255 133/255 122/255], 'linewidth', 1); + plot(1:sum(decLog), smooth(nanmean(decodes(decLog,:,3),2)), 'color', [9/255 161/255 74/255], 'linewidth', 1); + plot(1:sum(decLog), smooth(nanmean(decodes(decLog,:,4),2)), 'color', [128/255 66/255 151/255], 'linewidth', 1); + for o = 1:4 + tempPostMean = smooth(nanmean(decodes(decLog,:,o),2)); + tempPostVar = smooth(SEMcalc(decodes(decLog,:,o)')'); + tp = patch('YData', [tempPostMean+tempPostVar; flipud(tempPostMean-tempPostVar)],... + 'XData', [1:length(tempPostMean), length(tempPostMean):-1:1], 'FaceAlpha', .3); + if o==1 + set(tp, 'FaceColor', [44/255 168/255 224/255], 'edgecolor', [44/255 168/255 224/255], 'EdgeAlpha', .5); + elseif o==2 + set(tp, 'FaceColor', [154/255 133/255 122/255], 'edgecolor', [154/255 133/255 122/255], 'EdgeAlpha', .5); + elseif o==3 + set(tp, 'FaceColor', [9/255 161/255 74/255], 'edgecolor', [9/255 161/255 74/255], 'EdgeAlpha', .5); + else + set(tp, 'FaceColor', [128/255 66/255 151/255], 'edgecolor', [128/255 66/255 151/255], 'EdgeAlpha', .5); + end + end + axis(sps(p), 'tight'); + set(sps(p), 'ylim', [0 1], 'xticklabels', []); + line([size(decodings.PItime,1)/2 size(decodings.PItime,1)/2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); + line([size(decodings.POtime,1)/2+size(decodings.PItime,1) size(decodings.POtime,1)/2+size(decodings.PItime,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +end + legend('1', '2', '3','4', 'location', 'southoutside', 'orientation', 'horizontal'); %% figure; -plot(1:size(osDiffs,1), nanmean(osDiffs,2), 'color', 'k', 'linewidth', 1); +plot(1:size(osDiffs,1), smooth(nanmean(osDiffs,2)), 'color', 'k', 'linewidth', 1); axis tight -patch('YData', [nanmean(osDiffs,2)+SEMcalc(osDiffs')'; flipud(nanmean(osDiffs,2)-SEMcalc(osDiffs')')],... +patch('YData', [smooth(nanmean(osDiffs,2)+SEMcalc(osDiffs')'); flipud(smooth(nanmean(osDiffs,2)-SEMcalc(osDiffs')'))],... 'XData', [1:length(nanmean(osDiffs,2)), length(nanmean(osDiffs,2)):-1:1], 'FaceAlpha', .3); set(gca, 'ylim', [-1 1], 'xtick', [size(decodings.PItime,1)/2, size(decodings.POtime,1)/2+size(decodings.PItime,1)], 'xticklabel', [{'PokeIn'}, {'PokeOut'}]); ylabel([{'Decoded Difference'};{'(Pos-Odor)'}]); line(get(gca, 'xlim'), [0 0], 'color', 'k', 'linestyle', '--'); line([size(decodings.PItime,1) size(decodings.PItime,1)], get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); line([size(decodings.PItime,1)/2 size(decodings.PItime,1)/2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); -line([size(decodings.POtime,1)/2+size(decodings.POtime,1) size(decodings.POtime,1)/2+size(decodings.PItime,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); -title('GE11 OutSeq Decoding Diff (Position-Odor; Mean +/- SEM; 5xSessions)'); +line([size(decodings.POtime,1)/2+size(decodings.PItime,1) size(decodings.POtime,1)/2+size(decodings.PItime,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +title(sprintf('GE11 OutSeq Decoding Diff (Position-Odor; Mean +/- SEM; %ixSessions)', length(decodingFiles))); + +%% +figure; +skpPlot = plot(1:size(skpDiffs,1), smooth(nanmean(skpDiffs,2)), 'color', 'k', 'linewidth', 1); +patch('YData', [smooth(nanmean(skpDiffs,2)+SEMcalc(skpDiffs')'); smooth(flipud(nanmean(skpDiffs,2)-SEMcalc(skpDiffs')'))],... + 'XData', [1:length(nanmean(skpDiffs,2)), length(nanmean(skpDiffs,2)):-1:1], 'FaceAlpha', .3); +axis tight +hold on; +repPlot = plot(1:size(repDiffs,1), smooth(nanmean(repDiffs,2)), 'color', 'r', 'linewidth', 1); +patch('YData', [smooth(nanmean(repDiffs,2)+SEMcalc(repDiffs')'); smooth(flipud(nanmean(repDiffs,2)-SEMcalc(repDiffs')'))],... + 'XData', [1:length(nanmean(repDiffs,2)), length(nanmean(repDiffs,2)):-1:1], 'FaceColor', 'r', 'EdgeColor', 'r', 'FaceAlpha', .3); +set(gca, 'ylim', [-1 1], 'xtick', [size(decodings.PItime,1)/2, size(decodings.POtime,1)/2+size(decodings.PItime,1)], 'xticklabel', [{'PokeIn'}, {'PokeOut'}]); +legend([skpPlot repPlot], 'Skips', 'Repeats'); +ylabel([{'Decoded Difference'};{'(Pos-Odor)'}]); +line(get(gca, 'xlim'), [0 0], 'color', 'k', 'linestyle', '--'); +line([size(decodings.PItime,1) size(decodings.PItime,1)], get(gca, 'ylim'), 'linestyle', '-', 'color', 'k', 'linewidth', 2); +line([size(decodings.PItime,1)/2 size(decodings.PItime,1)/2], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +line([size(decodings.POtime,1)/2+size(decodings.PItime,1) size(decodings.POtime,1)/2+size(decodings.PItime,1)], get(gca, 'ylim'), 'linestyle', ':', 'color', 'k', 'linewidth', 2); +title('OutSeq Diff by Type'); +%% +meanTrialDecodes = nan(4,4,4); +for p = 1:4 + meanTrialDecodes(:,:,p) = mean(cell2mat(reshape(cellfun(@(a)a(:,:,p),trlPrdDecodes, 'uniformoutput',0), [1,1,length(trlPrdDecodes)])),3); +end + +figure; +subplot(2,4,1) +imagesc(meanTrialDecodes(:,:,1), [0 0.5]); +set(gca, 'xtick', 1:4, 'ytick', 1:4); +title('Pre-Trial Period'); +ylabel('Decoded Position'); +subplot(2,4,2) +imagesc(meanTrialDecodes(:,:,2), [0 0.5]); +set(gca, 'xtick', 1:4, 'ytick', 1:4); +title('Early-Trial Period'); +subplot(2,4,3) +imagesc(meanTrialDecodes(:,:,3), [0 0.5]); +set(gca, 'xtick', 1:4, 'ytick', 1:4); +title('Late-Trial Period'); +xlabel('True Position'); +ylabel('Decoded Position'); +subplot(2,4,4) +imagesc(meanTrialDecodes(:,:,4), [0 0.5]); +set(gca, 'xtick', 1:4, 'ytick', 1:4); +title('Post-Trial Period'); +xlabel('True Position'); +colormap jet +subplot(2,4,5) +corrScatPlot(ssnSMI', trlPrdDprms(1,:)', 'SMI', 'dPrm'); +subplot(2,4,6) +corrScatPlot(ssnSMI', trlPrdDprms(2,:)', 'SMI', 'dPrm'); +subplot(2,4,7) +corrScatPlot(ssnSMI', trlPrdDprms(3,:)', 'SMI', 'dPrm'); +subplot(2,4,8) +corrScatPlot(ssnSMI', trlPrdDprms(4,:)', 'SMI', 'dPrm'); \ No newline at end of file diff --git a/Data Management/OrganizeTrialData_SM.m b/Data Management/OrganizeTrialData_SM.m index 1e1b30d..0dfb4cd 100644 --- a/Data Management/OrganizeTrialData_SM.m +++ b/Data Management/OrganizeTrialData_SM.m @@ -60,6 +60,23 @@ % Identify InSeq logical inSeqLog = behavMatrix(:, cellfun(@(a)~isempty(a), strfind(behavMatrixColIDs, 'InSeqLog'))); + +% COMMENT IN IF RUNNING BOSTON-CA1 DATA; OUT IF RUNNING PFC +% if isempty(rewardSigNdxs) +% odorNdxs = find(sum(odorTrlMtx,2)); +% for t = 1:length(odorNdxs) +% if t~=length(odorNdxs) +% if sum(frontRwrdNdxs>odorNdxs(t) & frontRwrdNdxs=1 +% rewardSigNdxs = [rewardSigNdxs; odorNdxs(t)+1200]; %#ok +% end +% elseif t==length(odorNdxs) +% if sum(frontRwrdNdxs>odorNdxs(t))>=1 +% rewardSigNdxs = [rewardSigNdxs; odorNdxs(t)+1200]; %#ok +% end +% end +% end +% end + %% Create Data input structures seqNum = cell(1,numTrials); trialOdor = cell(1,numTrials); @@ -115,7 +132,7 @@ % Create trial logical vector tempLogVect = false(size(behavMatrix,1),1); - curPokeIn = pokeInNdxs(find(pokeInNdxstrialIndices(trl)==1,1, 'first')); trialPokeInNdx{trl} = curPokeIn; diff --git a/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m b/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m index 1969172..22ddfe6 100644 --- a/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m +++ b/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m @@ -518,6 +518,7 @@ if sum(trialPokesLog) == 1 % i.e. if there is only one poke that occurred during the trial period plxSession(trl).PokeDuration = trialPokeDurations; + tempPokeNum = nan; elseif sum(trialPokesLog) > 1 tempPokeDur = trialPokeDurations(1); tempPokeNum = 1; From 08b5a539fe3cf768474184e062467d47562b7ea1 Mon Sep 17 00:00:00 2001 From: gaelias Date: Fri, 18 Dec 2020 16:32:09 -0800 Subject: [PATCH 120/133] Create CurateSWR_SM.m --- Analyses/CurateSWR_SM.m | 505 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 505 insertions(+) create mode 100644 Analyses/CurateSWR_SM.m diff --git a/Analyses/CurateSWR_SM.m b/Analyses/CurateSWR_SM.m new file mode 100644 index 0000000..bc0fdde --- /dev/null +++ b/Analyses/CurateSWR_SM.m @@ -0,0 +1,505 @@ +aniIDs = [{'Barat'},... + {'Buchanan'},... + {'Mitt'},... + {'Stella'},... + {'SuperChris'}]; +dataDir = 'D:\WorkBigDataFiles\CA1 Data\1. WellTrained session\'; +aniDirs = cellfun(@(a)sprintf('%s%s\\SWR Tets\\',dataDir, a), aniIDs, 'uniformoutput', 0); +swChans = [{16},... + {21},... + {2},... + {18},... + {12}]; +ripChans = [{21},... + {18},... + {18},... + {14},... + {15}]; +aniInfo = struct('ID', aniIDs, 'Directory', aniDirs, 'SWchan', swChans, 'RIPchan', ripChans,... + 'SWepocs', [], 'SWtrace', [], 'SWpower', [],... + 'RIPepocs', [], 'RIPtrace', [], 'RIPpower', [],... + 'SWRepocs', []); + +swThresh = [0 3]; +ripThresh = [0 3]; +mergeThresh = 15; + +odorColors = [44/255, 168/255, 224/255;... + 154/255, 133/255, 122/255;... + 9/255, 161/255, 74/255;... + 128/255, 66/255, 151/255;... + 241/255, 103/255, 36/255]; + +%% +for a = 1:length(aniInfo) + %% Load Files & Load Behavior/Ensemble Data + aniFiles = dir(aniInfo(a).Directory); + behMatFile = aniFiles(cellfun(@(a)~isempty(a), strfind({aniFiles.name}, 'BehaviorMatrix'))).name; + behav = load([aniInfo(a).Directory behMatFile]); + behavMat = OrganizeTrialData_SM(behav.behavMatrix, behav.behavMatrixColIDs, [0 0], 'PokeIn'); + sessionMatrix = [[behavMat.PokeInIndex]', [behavMat.PokeOutIndex]', [behavMat.Performance]', [behavMat.Position]', [behavMat.Odor]']; + clear behav behavMat + nsmblMatFile = aniFiles(cellfun(@(a)~isempty(a), strfind({aniFiles.name}, 'EnsembleMatrix'))).name; + ensemble = load([aniInfo(a).Directory nsmblMatFile]); + + %% Evaluate Sharpwave channel & events + swFile = aniFiles(cell2mat(cellfun(@(a)~isempty(a),regexp({aniFiles.name}, ['\w*T' mat2str(aniInfo(a).SWchan) '_\w*SM.mat\>']),'uniformoutput', 0))).name; + sw = load([aniInfo(a).Directory swFile]); + [swEpocs, swLFP, swPOW] = SharpwaveDetection(sw.statMatrix(:,2), 1/mode(diff(sw.statMatrix(:,1))), swThresh, mergeThresh); + swLog = false(size(sw.statMatrix(:,1))); + for sws = 1:size(swEpocs,1) + swLog(swEpocs(sws,1):swEpocs(sws,2)) = true; + end + swTrace = nan(size(sw.statMatrix(:,1))); + swTrace(swLog) = sw.statMatrix(swLog,2); + aniInfo(a).SWepocs = swEpocs; + aniInfo(a).SWtrace = swLFP; + aniInfo(a).SWpower = swPOW; + + swPV = nan(size(ensemble.ensembleMatrix,2)-1, size(swEpocs,1)); + for sws = 1:size(swEpocs,1) + swPV(:,sws) = mean(ensemble.ensembleMatrix(swEpocs(sws,1):swEpocs(sws,2),2:end),1); + end + + %% Evaluate Ripple channel & events + ripFile = aniFiles(cell2mat(cellfun(@(a)~isempty(a),regexp({aniFiles.name}, ['\w*T' mat2str(aniInfo(a).RIPchan) '_\w*SM.mat\>']),'uniformoutput', 0))).name; + rip = load([aniInfo(a).Directory ripFile]); + [ripEpocs, ripLFP, ripPOW] = RippleDetection(rip.statMatrix(:,2), 1/mode(diff(rip.statMatrix(:,1))), ripThresh, mergeThresh); + ripLog = false(size(rip.statMatrix(:,1))); + for rips = 1:size(ripEpocs,1) + ripLog(ripEpocs(rips,1):ripEpocs(rips,2)) = true; + end + ripTrace = nan(size(rip.statMatrix(:,1))); + ripTrace(ripLog) = rip.statMatrix(ripLog,2); + aniInfo(a).RIPepocs = ripEpocs; + aniInfo(a).RIPtrace = ripLFP; + aniInfo(a).RIPpower = ripPOW; + + ripPV = nan(size(ensemble.ensembleMatrix,2)-1, size(ripEpocs,1)); + for rips = 1:size(ripEpocs,1) + ripPV(:,rips) = mean(ensemble.ensembleMatrix(ripEpocs(rips,1):ripEpocs(rips,2),2:end),1); + end + + %% Identify Sharpwave Ripple events + swrLog = swLog + ripLog; + swrStart = find(diff(swrLog==2)==1); + swrEnd = find(diff(swrLog==2)==-1); + swrWindows = [swrStart nan(length(swrStart),1)]; + for swr = 1:length(swrStart) + tempSWRstart = swrStart(swr); + swrWindows(swr,1) = min([swEpocs(tempSWRstart>=swEpocs(:,1) & tempSWRstart<=swEpocs(:,2),1), ripEpocs(tempSWRstart>=ripEpocs(:,1) & tempSWRstart<=ripEpocs(:,2),1), tempSWRstart]); + + tempSWRend = swrEnd(find(swrEnd>tempSWRstart,1,'first')); + swrWindows(swr,2) = max([swEpocs(tempSWRend>=swEpocs(:,1) & tempSWRend<=swEpocs(:,2),2), ripEpocs(tempSWRend>=ripEpocs(:,1) & tempSWRend<=ripEpocs(:,2),2), tempSWRend]); + end + for swr = 2:size(swrWindows,1) + if ~isnan(swrWindows(swr,1)) && ~isnan(swrWindows(swr-1,1)) + if swrWindows(swr,1) - swrWindows(swr-1,2) <= mergeThresh + swrWindows(swr,1) = swrWindows(swr-1,1); + swrWindows(swr-1,:) = nan; + end + end + end + swrWindows(isnan(swrWindows(:,1)),:) = []; + aniInfo(a).SWRepocs = swrWindows; + + newSWtrace = nan(size(sw.statMatrix(:,1))); + newRIPtrace = nan(size(rip.statMatrix(:,1))); + for swr = 1:size(swrWindows,1) + newSWtrace(swrWindows(swr,1):swrWindows(swr,2)) = sw.statMatrix(swrWindows(swr,1):swrWindows(swr,2),2); + newRIPtrace(swrWindows(swr,1):swrWindows(swr,2)) = rip.statMatrix(swrWindows(swr,1):swrWindows(swr,2),2); + end + + swrPV = nan(size(ensemble.ensembleMatrix,2)-1, size(swrWindows,1)); + swrActv = nan(1,size(swrWindows,1)); + for swrs = 1:size(swrWindows,1) + swrPV(:,swrs) = mean(ensemble.ensembleMatrix(swrWindows(swrs,1):swrWindows(swrs,2),2:end),1); + swrActv(swrs) = mean(swrPV(:,swrs)~=0); + end + + %% Summarize SWR Events + % Tabulate SWR vs Trial Info + trialRIPlog = false(size(swrWindows,1),1); + trialRIPlogPRE = false(size(swrWindows,1),1); + trialRIPlogTRIAL = false(size(swrWindows,1),1); + trialRIPlogPOST = false(size(swrWindows,1),1); + trialsWithRips = nan(size(sessionMatrix,1),1); + trialSWRlat = cell(size(sessionMatrix,1),1); + trialSWRlatPRE = cell(size(sessionMatrix,1),1); + trialSWRlatPOST = cell(size(sessionMatrix,1),1); + for trl = 1:size(sessionMatrix,1) + preTrlSWRlat = swrWindows(:,1) - sessionMatrix(trl,1); + if trl == 1 || sessionMatrix(trl,4)==1 + preTrialSWRlog = (preTrlSWRlat<0) & (preTrlSWRlat>-1000); + else + preTrialSWRlog = (preTrlSWRlat<0) & (preTrlSWRlat>(sessionMatrix(trl-1,2)-sessionMatrix(trl,1))/2); + end + trialSWRlatPRE{trl} = preTrlSWRlat(preTrialSWRlog); + trialRIPlogPRE(preTrialSWRlog) = true; + + trlSWRlog = (swrWindows(:,1) >= sessionMatrix(trl,1)) & (swrWindows(:,1) <= sessionMatrix(trl,2)); + trialSWRlat{trl} = swrWindows(trlSWRlog,1)-sessionMatrix(trl,1); + trialsWithRips(trl) = sum(trlSWRlog) ~= 0; + trialRIPlogTRIAL(trlSWRlog) = true; + + postTrlSWRlat = swrWindows(:,1) - sessionMatrix(trl,2); + if trl == size(sessionMatrix,1) || sessionMatrix(trl,3)==0 || sessionMatrix(trl,4)==max(sessionMatrix(:,4)) + postTrialSWRlog = (postTrlSWRlat>0) & (postTrlSWRlat<1000); + else + postTrialSWRlog = (postTrlSWRlat>0) & (postTrlSWRlat<(sessionMatrix(trl+1,1)-sessionMatrix(trl,2))/2); + end + trialSWRlatPOST{trl} = postTrlSWRlat(postTrialSWRlog); + trialRIPlogPOST(postTrialSWRlog) = true; + + trialRIPlog(preTrialSWRlog | trlSWRlog | postTrialSWRlog) = true; + end + + % Non-Trial SWRs + figure; + spDur = subplot(2,2,1); + swrDurs = swrWindows(:,2)-swrWindows(:,1); + histogram(swrDurs(~trialRIPlog), 0:10:max(swrDurs)+10, 'orientation', 'horizontal'); + set(spDur, 'XDir', 'reverse', 'YAxisLocation', 'right'); + title('SWR Duration'); + spPop = subplot(2,2,4); + histogram(swrActv(~trialRIPlog), 0:0.01:max(swrActv)+0.1); + title('Proportion Active Neurons'); + spDurPopCorr = subplot(2,2,2); + corrScatPlot(swrActv(~trialRIPlog)', swrDurs(~trialRIPlog), '% Active', 'Duration', [], []) + title('Non-Trial SWRs'); + linkaxes([spDur spDurPopCorr], 'y'); + linkaxes([spPop spDurPopCorr], 'x'); + supIRI = subplot(2,2,3); + swrIRI = swrWindows(2:end,2)-swrWindows(1:end-1,1); + histogram(swrIRI); + title('Inter-SWR-Interval'); + annotation(gcf,'textbox', [0 0.95 1 0.05],'String', sprintf('%s Non-Trial SWRs', aniInfo(a).ID),... + 'FontWeight', 'Bold', 'FontSize',10, 'edgecolor', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); + + % Trial Period SWRs + figure('toolbar', 'none'); + spDurPRE = subplot(6,2,1); + histogram(swrDurs(trialRIPlogPRE), 0:50:max(swrDurs)+50, 'orientation', 'horizontal'); + set(spDurPRE, 'XDir', 'reverse', 'YAxisLocation', 'right'); + title('SWR Duration'); + spDurPopCorrPRE = subplot(6,2,2); + corrScatPlot(swrActv(trialRIPlogPRE)', swrDurs(trialRIPlogPRE), '% Active', 'Duration', [], []); + title('Pre Trial SWRs'); + spPRElat = subplot(6,2,3); + histogram(cell2mat(trialSWRlatPRE), -4000:200:0); + title('Pre Trial Latency'); + spPopPRE = subplot(6,2,4); + histogram(swrActv(trialRIPlogPRE), 0:0.05:max(swrActv)+0.05); + title('Proportion Active Neurons'); + + spDurTRL = subplot(6,2,5); + histogram(swrDurs(trialRIPlogTRIAL), 0:50:max(swrDurs)+50, 'orientation', 'horizontal'); + set(spDurTRL, 'XDir', 'reverse', 'YAxisLocation', 'right'); + title('SWR Duration'); + spDurPopCorrTRL = subplot(6,2,6); + corrScatPlot(swrActv(trialRIPlogTRIAL)', swrDurs(trialRIPlogTRIAL), '% Active', 'Duration', [], []); + title('Trial Period SWRs'); + spTRLlat = subplot(6,2,7); + histogram(cell2mat(trialSWRlat), 0:200:max(cell2mat(trialSWRlat))); + title('Trial Latency'); + spPopTRL = subplot(6,2,8); + histogram(swrActv(trialRIPlogTRIAL), 0:0.05:max(swrActv)+0.05); + title('Proportion Active Neurons'); + + spDurPOST = subplot(6,2,9); + histogram(swrDurs(trialRIPlogPOST), 0:50:max(swrDurs)+50, 'orientation', 'horizontal'); + set(spDurPOST, 'XDir', 'reverse', 'YAxisLocation', 'right'); + title('SWR Duration'); + spDurPopCorrPOST = subplot(6,2,10); + corrScatPlot(swrActv(trialRIPlogPOST)', swrDurs(trialRIPlogPOST), '% Active', 'Duration', [], []); + title('Post Trial SWRs'); + spPOSTlat = subplot(6,2,11); + histogram(cell2mat(trialSWRlatPOST), 0:200:4000); + title('Post Trial Latency'); + spPopPOST = subplot(6,2,12); + histogram(swrActv(trialRIPlogPOST), 0:0.05:max(swrActv)+0.05); + title('Proportion Active Neurons'); + + linkaxes([spDurPRE spDurTRL spDurPOST spDurPopCorrPRE spDurPopCorrTRL spDurPopCorrPOST], 'y'); + linkaxes([spPopPRE spPopTRL spPopPOST spDurPopCorrPRE spDurPopCorrTRL spDurPopCorrPOST], 'x'); + + linkaxes([spPRElat, spTRLlat, spPOSTlat], 'y'); + annotation(gcf,'textbox', [0 0.95 1 0.05],'String', sprintf('%s Trial SWRs', aniInfo(a).ID),... + 'FontWeight', 'Bold', 'FontSize',10, 'edgecolor', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); + + %% Evaluate behavioral relations of SWR events + perfLog = sessionMatrix(:,3)==1; + isLog = sessionMatrix(:,4)==sessionMatrix(:,5); + + templates = cell(1,max(sessionMatrix(:,5))); + for o = 1:max(sessionMatrix(:,5)) + trlLog = sessionMatrix(:,5) == o & perfLog & isLog; + tempSsnMtx = sessionMatrix(trlLog,:); + tempTemplate = nan(240,size(ensemble.ensembleMatrix,2)-1, size(tempSsnMtx,1)); + for trl = 1:size(tempSsnMtx,1) + tempTrial = ensemble.ensembleMatrix(tempSsnMtx(trl,1):tempSsnMtx(trl,1)+1199,2:end); + for uni = 1:size(tempTrial,2) + tempConv = conv(tempTrial(:,uni), ones(1,10)./(10/1000),'same'); + tempTemplate(:,uni,trl) = downsample(tempConv,5); + end + end + templates{o} = mean(tempTemplate,3); + end + catTemplate = cell2mat(templates'); + timeNdx = downsample(repmat(1:1200, [1,5]), 5); + odrNdx = downsample([ones(1,1200), ones(1,1200)+1, ones(1,1200)+2, ones(1,1200)+3, ones(1,1200)+4], 5); + + ripDecodeODR = cell(size(aniInfo(a).SWRepocs,1),1); + ripDecodeTIME = cell(size(aniInfo(a).SWRepocs,1),1); + for rip = 1:size(aniInfo(a).SWRepocs,1) + tempEnsemble = ensemble.ensembleMatrix(aniInfo(a).SWRepocs(rip,1):aniInfo(a).SWRepocs(rip,2),2:end); + tempEnsConv = nan(size(tempEnsemble)); + for uni = 1:size(tempEnsemble,2) + tempEnsConv(:,uni) = conv(tempEnsemble(:,uni), ones(1,10)./(10/1000), 'same'); + end + post = CalcStaticBayesPost(catTemplate, downsample(tempEnsConv,10), 10); + ripDecodeTIME{rip} = DecodeBayesPost(post, timeNdx); + ripDecodeODR{rip} = DecodeBayesPost(post, odrNdx); + end + + lagBins = 0:1:100; + odrLag = zeros(max(sessionMatrix(:,4)), length(lagBins)-1, max(sessionMatrix(:,4))); + tempHistBins = -1200:100:1200; + timeLag = zeros(1,length(tempHistBins)-1); + odrLagTrans = zeros(max(sessionMatrix(:,4))); + for rip = 1:size(ripDecodeODR) + tempRipODR = ripDecodeODR{rip}; + tempRipTIME = ripDecodeTIME{rip}; + tempRipNdx = 1:length(tempRipODR); + for t = 1:length(tempRipODR) + curOdr = tempRipODR(t); + if ~isnan(curOdr) + for o = 1:max(sessionMatrix(:,4)) + odrTargLog = tempRipODR==o; + odrTargLag = tempRipNdx(odrTargLog) - t; + odrLag(o,:,curOdr) = odrLag(o,:,curOdr) + histcounts(odrTargLag, lagBins); + end + end + if t + ripCol = cellfun(@(a)~isempty(a), regexp(statMatrixColIDs, 'LFP_Ripple$')); + lfp = statMatrix(:,ripCol); %#ok + clear statMatrix statMatrixColIDs file path + powThresh = [0 4]; + mergeThresh = 15; +end +%% +lfpFilt = SimpleFilt(lfp, fs, 150, 250); +zPow = zscore(abs(hilbert(lfpFilt))); + +%% Identify Potential Sharpwave Periods +abvThresh1 = zPow > powThresh(1); +epocStart = find(diff(abvThresh1)==1); +epocEnd = find(diff(abvThresh1)==-1); +epocWindows = [epocStart, nan(size(epocStart))]; +for epoc = 1:size(epocStart,1) + tempEpocEnd = epocEnd(find(epocEnd>epocStart(epoc), 1, 'first')); + if sum(zPow(epocStart(epoc):tempEpocEnd)>=powThresh(2))>=1 + epocWindows(epoc,2) = tempEpocEnd; + else + epocWindows(epoc,1) = nan; + end +end +epocWindows(isnan(epocWindows(:,1)),:) = []; +% Merge short latency sharpwaves +for epoc = 2:size(epocWindows,1) + if ~isnan(epocWindows(epoc,1)) && ~isnan(epocWindows(epoc-1,1)) + if epocWindows(epoc,1) - epocWindows(epoc-1,2) <= mergeThresh + epocWindows(epoc,1) = epocWindows(epoc-1,1); + epocWindows(epoc-1,:) = nan; + end + end +end +epocWindows(isnan(epocWindows(:,1)),:) = []; +%% +ripEpocs = epocWindows; +end + +function [swEpocs, hpf, zPow] = SharpwaveDetection(lfp, fs, powThresh, mergeThresh) +%% SharpwaveDetection +% Code for detection of sharpwaves on tetrodes presumed to be in the +% stratum radiatum of CA1 +% Inputs: +% lfp: Nx1 input of the raw LFP +% powThresh: 1x2 input with threshold parameters for detection of peak +% event. Initial value is event boundaries, second is peak required +% mergeThresh: threshold for merging two sharpwave events if they happen +% in close temporal proximity +%% +if nargin == 0 + [file,path] = uigetfile('*.mat'); + load([path file]); %#ok + lfp = statMatrix(:,2); %#ok + fs = 1/mode(diff(statMatrix(:,1))); + clear statMatrix statMatrixColIDs file path + powThresh = [0 2]; + mergeThresh = 15; +end +%% Filter & Z-Score Envelope +hpf = highpass(lfp, 4, fs); +zPow = zscore(abs(hilbert(hpf))); + +%% Identify Potential Sharpwave Periods +abvThresh1 = zPow > powThresh(1); +epocStart = find(diff(abvThresh1)==1); +epocEnd = find(diff(abvThresh1)==-1); +epocWindows = [epocStart, nan(size(epocStart))]; +for epoc = 1:size(epocStart,1) + tempEpocEnd = epocEnd(find(epocEnd>epocStart(epoc), 1, 'first')); + if sum(zPow(epocStart(epoc):tempEpocEnd)>=powThresh(2))>=1 + epocWindows(epoc,2) = tempEpocEnd; + else + epocWindows(epoc,1) = nan; + end +end +epocWindows(isnan(epocWindows(:,1)),:) = []; +% Merge short latency sharpwaves +for epoc = 2:size(epocWindows,1) + if ~isnan(epocWindows(epoc,1)) && ~isnan(epocWindows(epoc-1,1)) + if epocWindows(epoc,1) - epocWindows(epoc-1,2) <= mergeThresh + epocWindows(epoc,1) = epocWindows(epoc-1,1); + epocWindows(epoc-1,:) = nan; + end + end +end +epocWindows(isnan(epocWindows(:,1)),:) = []; +%% +swEpocs = epocWindows; +%% +end + +%% Simple Filtering +function lfpFilt = SimpleFilt(trace, samp, low, high) +Wn_FRange = [low/(samp/2) high/(samp/2)]; % normalized by the nyquist frequency +[bFRange, aFRange] = butter(3,Wn_FRange); +lfpFilt = filtfilt(bFRange,aFRange,trace); +end + +%% +function post = CalcStaticBayesPost(likely, obsv, binSize) +post = nan(size(obsv,1), size(likely,1), size(obsv,3)); +for trl = 1:size(obsv,3) + for t = 1:size(obsv,1) + p = nan(size(likely)); + curPopVect = obsv(t,:,trl)*(binSize/1000); + curPopFact = factorial(curPopVect); + for u = 1:size(likely,2) + curAvgUniFR = likely(:,u); + p(:,u) = (((binSize/1000).*curAvgUniFR).^curPopVect(u))./curPopFact(u); + end + pp = prod(p,2); + ee = exp(-((binSize/1000)*sum(likely,2))); + tempPost = pp.*ee; + post(t,:,trl) = tempPost./sum(tempPost); + end +end +end + +%% +function decode = DecodeBayesPost(post, id) +% Assumes post is in the structure of ObservTime X LikelyTime X Trial +decode = nan(size(post,1),size(post,3)); +for o = 1:size(post,3) + for d = 1:size(post,1) + if ~isnan(post(d,1,o)) + decode(d,o) = id(find(post(d,:,o)==max(post(d,:,o)),1,'first')); + end + end +end +end \ No newline at end of file From b042bdabbc252a0f1c7b2627b3eee99d2f717435 Mon Sep 17 00:00:00 2001 From: gaelias Date: Fri, 18 Dec 2020 16:32:23 -0800 Subject: [PATCH 121/133] Update SequenceNeuralViewer_SM.m --- Analyses/SequenceNeuralViewer_SM.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Analyses/SequenceNeuralViewer_SM.m b/Analyses/SequenceNeuralViewer_SM.m index dfb37e5..5e52811 100644 --- a/Analyses/SequenceNeuralViewer_SM.m +++ b/Analyses/SequenceNeuralViewer_SM.m @@ -119,10 +119,10 @@ load(smFile) lfpColIDs = find(cellfun(@(a)~isempty(a), regexp(statMatrixColIDs, '_LFP_[(A-Z)|(a-z)]*\>'))); lfpBands = cellfun(@(a,b)a(b:end), statMatrixColIDs(lfpColIDs), regexp(statMatrixColIDs(lfpColIDs), '[(A-Z)|(a-z)]*\>'), 'uniformoutput', 0); -lfpVals = statMatrix(:,lfpColIDs(strcmp(lfpBands, band2plot))); +lfpVals = statMatrix(:,lfpColIDs(strcmp(lfpBands, band2plot))); %#ok<*USENS> lfpVals = lfpVals/(max(abs(lfpVals))); -plot(statMatrix(:,1), lfpVals-1, 'color', [0.4 0.4 0.4]); +plot(statMatrix(:,1), (lfpVals+2)*-1, 'color', [0.4 0.4 0.4]); % Plot Spikes hold on; From 3a5df51e2a0ea0c97ec5d8cd238abc7f99c481fc Mon Sep 17 00:00:00 2001 From: gaelias Date: Fri, 18 Dec 2020 16:32:27 -0800 Subject: [PATCH 122/133] Update SequenceViewerNeural_SM.m --- Analyses/SequenceViewerNeural_SM.m | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Analyses/SequenceViewerNeural_SM.m b/Analyses/SequenceViewerNeural_SM.m index 6c726fe..4328842 100644 --- a/Analyses/SequenceViewerNeural_SM.m +++ b/Analyses/SequenceViewerNeural_SM.m @@ -238,7 +238,7 @@ trialHist = mean(plotData.TrialEnsembleBinned(:,:,trialLog),3)'; normHist = trialHist./max(trialHist,[],2); silentCellLog = sum(isnan(normHist),2)==size(normHist,2); -lowFRlog = sum(trialHist<=1,2)==size(trialHist,2); silentCellLog = silentCellLog | lowFRlog; +% lowFRlog = sum(trialHist<=1,2)==size(trialHist,2); silentCellLog = silentCellLog | lowFRlog; spikeTimes(silentCellLog) = []; unitIDs(silentCellLog) = []; normHist(silentCellLog,:) = []; @@ -323,7 +323,8 @@ function ChooseLFPbandToPlot(source,event) band2Plot = get(lfpBand2Plot, 'value'); lfpData2Plot = lfpData(:,band2Plot); lfpData2Plot = (lfpData2Plot/(max(abs(lfpData2Plot))))*2; -set(lfpPlotHandle, 'Ydata', lfpData2Plot*-1); +% set(lfpPlotHandle, 'Ydata', lfpData2Plot*-1); +set(lfpPlotHandle, 'Ydata', lfpData2Plot); end %% From 64045a6b0c1e5775474cd5af36ad3c3ff5a0073b Mon Sep 17 00:00:00 2001 From: gaelias Date: Fri, 18 Dec 2020 16:32:30 -0800 Subject: [PATCH 123/133] Create CARcleaner_SM.m --- Data Management/CARcleaner_SM.m | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Data Management/CARcleaner_SM.m diff --git a/Data Management/CARcleaner_SM.m b/Data Management/CARcleaner_SM.m new file mode 100644 index 0000000..a57059f --- /dev/null +++ b/Data Management/CARcleaner_SM.m @@ -0,0 +1,25 @@ +function CARcleaner_SM +smPath = uigetdir; +cd(smPath); +files = dir(smPath); +fileNames = {files.name}; +smFileList = fileNames(cellfun(@(a)~isempty(a), regexp(fileNames, '_SM\>')))'; +lfps = cell(size(smFileList))'; +for f = 1:length(smFileList) + temp = load(smFileList{f}); + lfps{f} = temp.statMatrix(:,2); +end +lfps = cell2mat(lfps); +gMn = mean(lfps,2); + +%% +for f = 1:length(smFileList) + temp = load(smFileList{f}); + statMatrix = [temp.statMatrix(:,1), temp.statMatrix(:,2)-gMn]; + statMatrixColIDs = temp.statMatrixColIDs(1:2); + fName = [smFileList{f}(1:end-6) 'CARcleaned_SM.mat']; + save(fName, 'statMatrix', 'statMatrixColIDs'); + fprintf('%s Saved\n', fName); +end +fprintf('All files saved\n'); + From dfb02c5a50b4f226b91d1c6887bd3281a6037375 Mon Sep 17 00:00:00 2001 From: gaelias Date: Fri, 18 Dec 2020 16:34:34 -0800 Subject: [PATCH 124/133] Update manualArtifactRemoval.m --- manualArtifactRemoval.m | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/manualArtifactRemoval.m b/manualArtifactRemoval.m index 75ceb54..88484a4 100644 --- a/manualArtifactRemoval.m +++ b/manualArtifactRemoval.m @@ -219,11 +219,13 @@ function ChangeCH(source,event) function SaveFile(source,event) global statMatrix smFile badIndxs - -statMatrix_edited = statMatrix; -statMatrix_edited(badIndxs,:) = nan; -outputfilename = strcat('Edited_',smFile); -uisave('statMatrix_edited',outputfilename); +badIndexLog = [statMatrix(:,1), false(size(statMatrix,1),1)]; +badIndexLog(badIndxs,2) = true; +uisave('badIndexLog',sprintf('%s_BadIndices.mat', smFile(1:end-4))); +% statMatrix_edited = statMatrix; +% statMatrix_edited(badIndxs,:) = nan; +% outputfilename = strcat('Edited_',smFile); +% uisave('statMatrix_edited',outputfilename); end @@ -253,10 +255,10 @@ function UpdatePokePlotVals(rmsThresh) end load(smFile,'statMatrix') -goodDataTrace = statMatrix(:,10); +goodDataTrace = statMatrix(:,2); % Enable the following to filter the data -[b1, a1] = butter(2, [59/500 61/500], 'stop'); %% Remove 60hz Harmonic (noise) -goodDataTrace = filtfilt(b1, a1, goodDataTrace); %% Apply Filter +% [b1, a1] = butter(2, [59/500 61/500], 'stop'); %% Remove 60hz Harmonic (noise) +% goodDataTrace = filtfilt(b1, a1, goodDataTrace); %% Apply Filter assignin('base', 'goodDataTrace', goodDataTrace); badDataTrace = nan(size(goodDataTrace)); assignin('base', 'badDataTrace', badDataTrace); From 0204bc2f2c5fc2fde27c8a9863d2be0df2c936ab Mon Sep 17 00:00:00 2001 From: gaelias Date: Mon, 21 Dec 2020 11:47:53 -0800 Subject: [PATCH 125/133] Update CurateSWR_SM.m --- Analyses/CurateSWR_SM.m | 260 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 242 insertions(+), 18 deletions(-) diff --git a/Analyses/CurateSWR_SM.m b/Analyses/CurateSWR_SM.m index bc0fdde..08f702a 100644 --- a/Analyses/CurateSWR_SM.m +++ b/Analyses/CurateSWR_SM.m @@ -227,7 +227,7 @@ annotation(gcf,'textbox', [0 0.95 1 0.05],'String', sprintf('%s Trial SWRs', aniInfo(a).ID),... 'FontWeight', 'Bold', 'FontSize',10, 'edgecolor', 'none', 'horizontalalignment', 'left', 'interpreter', 'none'); - %% Evaluate behavioral relations of SWR events + %% Calculate Trial Templates to evaluate behavioral relations of SWR events perfLog = sessionMatrix(:,3)==1; isLog = sessionMatrix(:,4)==sessionMatrix(:,5); @@ -249,35 +249,39 @@ timeNdx = downsample(repmat(1:1200, [1,5]), 5); odrNdx = downsample([ones(1,1200), ones(1,1200)+1, ones(1,1200)+2, ones(1,1200)+3, ones(1,1200)+4], 5); - ripDecodeODR = cell(size(aniInfo(a).SWRepocs,1),1); - ripDecodeTIME = cell(size(aniInfo(a).SWRepocs,1),1); - for rip = 1:size(aniInfo(a).SWRepocs,1) - tempEnsemble = ensemble.ensembleMatrix(aniInfo(a).SWRepocs(rip,1):aniInfo(a).SWRepocs(rip,2),2:end); + %% All SWRs + trlRelRips = swrWindows; + ripDecodeODR = cell(size(trlRelRips,1),1); + ripDecodeTIME = cell(size(trlRelRips,1),1); + for rip = 1:size(trlRelRips,1) + tempEnsemble = ensemble.ensembleMatrix(trlRelRips(rip,1):trlRelRips(rip,2),2:end); tempEnsConv = nan(size(tempEnsemble)); for uni = 1:size(tempEnsemble,2) tempEnsConv(:,uni) = conv(tempEnsemble(:,uni), ones(1,10)./(10/1000), 'same'); end - post = CalcStaticBayesPost(catTemplate, downsample(tempEnsConv,10), 10); + post = CalcStaticBayesPost(catTemplate, downsample(tempEnsConv,10), 10); % <---------------- SWR Observation Rate ripDecodeTIME{rip} = DecodeBayesPost(post, timeNdx); ripDecodeODR{rip} = DecodeBayesPost(post, odrNdx); end lagBins = 0:1:100; - odrLag = zeros(max(sessionMatrix(:,4)), length(lagBins)-1, max(sessionMatrix(:,4))); + odrLagDecodeMtx = repmat({zeros(max(sessionMatrix(:,4)), length(lagBins)-1)}, [1, max(sessionMatrix(:,4))]); + odrLagRips = cell(length(ripDecodeODR), max(sessionMatrix(:,4))); tempHistBins = -1200:100:1200; timeLag = zeros(1,length(tempHistBins)-1); odrLagTrans = zeros(max(sessionMatrix(:,4))); - for rip = 1:size(ripDecodeODR) + for rip = 1:length(ripDecodeODR) tempRipODR = ripDecodeODR{rip}; tempRipTIME = ripDecodeTIME{rip}; tempRipNdx = 1:length(tempRipODR); + tempODRlagDecodeMtx = odrLagDecodeMtx; for t = 1:length(tempRipODR) curOdr = tempRipODR(t); if ~isnan(curOdr) for o = 1:max(sessionMatrix(:,4)) odrTargLog = tempRipODR==o; odrTargLag = tempRipNdx(odrTargLog) - t; - odrLag(o,:,curOdr) = odrLag(o,:,curOdr) + histcounts(odrTargLag, lagBins); + tempODRlagDecodeMtx{curOdr}(o,:) = tempODRlagDecodeMtx{curOdr}(o,:) + histcounts(odrTargLag, lagBins); end end if t Date: Mon, 21 Dec 2020 15:04:52 -0800 Subject: [PATCH 126/133] Update CurateSWR_SM.m --- Analyses/CurateSWR_SM.m | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/Analyses/CurateSWR_SM.m b/Analyses/CurateSWR_SM.m index 08f702a..38a65b7 100644 --- a/Analyses/CurateSWR_SM.m +++ b/Analyses/CurateSWR_SM.m @@ -38,6 +38,15 @@ behav = load([aniInfo(a).Directory behMatFile]); behavMat = OrganizeTrialData_SM(behav.behavMatrix, behav.behavMatrixColIDs, [0 0], 'PokeIn'); sessionMatrix = [[behavMat.PokeInIndex]', [behavMat.PokeOutIndex]', [behavMat.Performance]', [behavMat.Position]', [behavMat.Odor]']; + % Extract position and derive velocity + locNdxs = find(sum(posVals~=0,2)>=1); + locVals = behav.behavMatrix(locNdxs,end-1:end)*1.5/100; + timeVals = behav.behavMatrix(locNdxs,1); + instV = nan(size(locVals,1),1); + for v = 2:size(locVals,1) + instV(v) = sqrt((locVals(v,1)-locVals(v-1,1))^2 - (locVals(v,2)-locVals(v,2))^2)/diff(timeVals(v-1:v)); + end + smoothInstV = conv(instV, ones(1,20)/20, 'same'); clear behav behavMat nsmblMatFile = aniFiles(cellfun(@(a)~isempty(a), strfind({aniFiles.name}, 'EnsembleMatrix'))).name; ensemble = load([aniInfo(a).Directory nsmblMatFile]); @@ -235,19 +244,19 @@ for o = 1:max(sessionMatrix(:,5)) trlLog = sessionMatrix(:,5) == o & perfLog & isLog; tempSsnMtx = sessionMatrix(trlLog,:); - tempTemplate = nan(240,size(ensemble.ensembleMatrix,2)-1, size(tempSsnMtx,1)); + tempTemplate = nan(120,size(ensemble.ensembleMatrix,2)-1, size(tempSsnMtx,1)); for trl = 1:size(tempSsnMtx,1) tempTrial = ensemble.ensembleMatrix(tempSsnMtx(trl,1):tempSsnMtx(trl,1)+1199,2:end); for uni = 1:size(tempTrial,2) tempConv = conv(tempTrial(:,uni), ones(1,10)./(10/1000),'same'); - tempTemplate(:,uni,trl) = downsample(tempConv,5); + tempTemplate(:,uni,trl) = downsample(tempConv,10); end end templates{o} = mean(tempTemplate,3); end catTemplate = cell2mat(templates'); - timeNdx = downsample(repmat(1:1200, [1,5]), 5); - odrNdx = downsample([ones(1,1200), ones(1,1200)+1, ones(1,1200)+2, ones(1,1200)+3, ones(1,1200)+4], 5); + timeNdx = downsample(repmat(1:1200, [1,5]), 10); + odrNdx = downsample([ones(1,1200), ones(1,1200)+1, ones(1,1200)+2, ones(1,1200)+3, ones(1,1200)+4], 10); %% All SWRs trlRelRips = swrWindows; From 4b83f68d7b24db07a35594895757612af87b08da Mon Sep 17 00:00:00 2001 From: gaelias Date: Fri, 15 Jan 2021 09:32:46 -0800 Subject: [PATCH 127/133] Create IdentifySWR_SM.m --- Analyses/IdentifySWR_SM.m | 278 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 278 insertions(+) create mode 100644 Analyses/IdentifySWR_SM.m diff --git a/Analyses/IdentifySWR_SM.m b/Analyses/IdentifySWR_SM.m new file mode 100644 index 0000000..426211b --- /dev/null +++ b/Analyses/IdentifySWR_SM.m @@ -0,0 +1,278 @@ +% IdentifySWR_SM +aniIDs = [{'Barat'},... + {'Buchanan'},... + {'Mitt'},... + {'Stella'},... + {'SuperChris'}]; +dataDir = 'D:\WorkBigDataFiles\CA1 Data\1. WellTrained session\'; +aniDirs = cellfun(@(a)sprintf('%s%s\\SWR Tets\\',dataDir, a), aniIDs, 'uniformoutput', 0); +swChans = [{16},... + {21},... + {2},... + {18},... + {12}]; +ripChans = [{21},... + {18},... + {18},... + {14},... + {15}]; +aniInfo = struct('ID', aniIDs, 'Directory', aniDirs, 'SWchan', swChans, 'RIPchan', ripChans,... + 'SWepocs', [], 'SWtrace', [], 'SWpower', [],... + 'RIPepocs', [], 'RIPtrace', [], 'RIPpower', [],... + 'SWRepocs', []); + +swThresh = [0 3]; +ripThresh = [0 3]; +mergeThresh = 15; + +odorColors = [44/255, 168/255, 224/255;... + 154/255, 133/255, 122/255;... + 9/255, 161/255, 74/255;... + 128/255, 66/255, 151/255;... + 241/255, 103/255, 36/255]; + +%% Define Output Data Structure +aniSWRmat = struct('AniID', aniIDs, 'SessionMatrix', [], 'SessionMatrixColIDs', [],... + 'SWRmatrix', [], 'SWRmatrixColIDs', []); +%% +for a = 1:length(aniInfo) + %% Load Files & Load Behavior Data + aniFiles = dir(aniInfo(a).Directory); + behMatFile = aniFiles(cellfun(@(a)~isempty(a), strfind({aniFiles.name}, 'BehaviorMatrix'))).name; + behav = load([aniInfo(a).Directory behMatFile]); + behavMat = OrganizeTrialData_SM(behav.behavMatrix, behav.behavMatrixColIDs, [0 0], 'PokeIn'); + sessionMatrix = [[behavMat.PokeInIndex]', [behavMat.PokeOutIndex]', [behavMat.Performance]', [behavMat.Position]', [behavMat.Odor]']; + aniSWRmat(a).SessionMatrix = sessionMatrix; + aniSWRmat(a).SessionMatrixColIDs = [{'PokeInIndex'}, {'PokeOutIndex'}, {'Performance'}, {'Trial_Position'}, {'Trial_Odor'}]; + % Extract position and derive velocity + posVals = behav.behavMatrix(:,end-1:end); + locNdxs = find(sum(posVals~=0,2)>=1); + locVals = behav.behavMatrix(locNdxs,end-1:end)*1.5/100; + timeVals = behav.behavMatrix(locNdxs,1); + instV = nan(size(locVals,1),1); + for v = 2:size(locVals,1) + instV(v) = sqrt((locVals(v,1)-locVals(v-1,1))^2 - (locVals(v,2)-locVals(v,2))^2)/diff(timeVals(v-1:v)); + end + smoothInstV = conv(instV, ones(1,20)/20, 'same'); + clear behav behavMat + + %% Evaluate Sharpwave channel & events + swFile = aniFiles(cell2mat(cellfun(@(a)~isempty(a),regexp({aniFiles.name}, ['\w*T' mat2str(aniInfo(a).SWchan) '_\w*SM.mat\>']),'uniformoutput', 0))).name; + sw = load([aniInfo(a).Directory swFile]); + [swEpocs, swLFP, swPOW] = SharpwaveDetection(sw.statMatrix(:,2), 1/mode(diff(sw.statMatrix(:,1))), swThresh, mergeThresh); + swLog = false(size(sw.statMatrix(:,1))); + for sws = 1:size(swEpocs,1) + swLog(swEpocs(sws,1):swEpocs(sws,2)) = true; + end + swTrace = nan(size(sw.statMatrix(:,1))); + swTrace(swLog) = sw.statMatrix(swLog,2); + aniInfo(a).SWepocs = swEpocs; + aniInfo(a).SWtrace = swLFP; + aniInfo(a).SWpower = swPOW; + + %% Evaluate Ripple channel & events + ripFile = aniFiles(cell2mat(cellfun(@(a)~isempty(a),regexp({aniFiles.name}, ['\w*T' mat2str(aniInfo(a).RIPchan) '_\w*SM.mat\>']),'uniformoutput', 0))).name; + rip = load([aniInfo(a).Directory ripFile]); + [ripEpocs, ripLFP, ripPOW] = RippleDetection(rip.statMatrix(:,2), 1/mode(diff(rip.statMatrix(:,1))), ripThresh, mergeThresh); + ripLog = false(size(rip.statMatrix(:,1))); + for rips = 1:size(ripEpocs,1) + ripLog(ripEpocs(rips,1):ripEpocs(rips,2)) = true; + end + ripTrace = nan(size(rip.statMatrix(:,1))); + ripTrace(ripLog) = rip.statMatrix(ripLog,2); + aniInfo(a).RIPepocs = ripEpocs; + aniInfo(a).RIPtrace = ripLFP; + aniInfo(a).RIPpower = ripPOW; + + %% Identify Sharpwave Ripple events + swrLog = swLog + ripLog; + swrStart = find(diff(swrLog==2)==1); + swrEnd = find(diff(swrLog==2)==-1); + swrWindows = [swrStart nan(length(swrStart),1)]; + for swr = 1:length(swrStart) + tempSWRstart = swrStart(swr); + swrWindows(swr,1) = min([swEpocs(tempSWRstart>=swEpocs(:,1) & tempSWRstart<=swEpocs(:,2),1), ripEpocs(tempSWRstart>=ripEpocs(:,1) & tempSWRstart<=ripEpocs(:,2),1), tempSWRstart]); + + tempSWRend = swrEnd(find(swrEnd>tempSWRstart,1,'first')); + swrWindows(swr,2) = max([swEpocs(tempSWRend>=swEpocs(:,1) & tempSWRend<=swEpocs(:,2),2), ripEpocs(tempSWRend>=ripEpocs(:,1) & tempSWRend<=ripEpocs(:,2),2), tempSWRend]); + end + for swr = 2:size(swrWindows,1) + if ~isnan(swrWindows(swr,1)) && ~isnan(swrWindows(swr-1,1)) + if swrWindows(swr,1) - swrWindows(swr-1,2) <= mergeThresh + swrWindows(swr,1) = swrWindows(swr-1,1); + swrWindows(swr-1,:) = nan; + end + end + end + swrWindows(isnan(swrWindows(:,1)),:) = []; + aniInfo(a).SWRepocs = swrWindows; + + newSWtrace = nan(size(sw.statMatrix(:,1))); + newRIPtrace = nan(size(rip.statMatrix(:,1))); + for swr = 1:size(swrWindows,1) + newSWtrace(swrWindows(swr,1):swrWindows(swr,2)) = sw.statMatrix(swrWindows(swr,1):swrWindows(swr,2),2); + newRIPtrace(swrWindows(swr,1):swrWindows(swr,2)) = rip.statMatrix(swrWindows(swr,1):swrWindows(swr,2),2); + end + + %% Identify Sequence & Non-Sequence SWRs + intSeqSWRlog = false(size(swrWindows,1),1); + betSeqsSWRlog = false(size(swrWindows,1),1); + for swr = 1:size(swrWindows,1) + preSWRtrlNdx = find(swrWindows(swr,1)>sessionMatrix(1:end-1,2) & swrWindows(swr,1) + ripCol = cellfun(@(a)~isempty(a), regexp(statMatrixColIDs, 'LFP_Ripple$')); + lfp = statMatrix(:,ripCol); %#ok + clear statMatrix statMatrixColIDs file path + powThresh = [0 4]; + mergeThresh = 15; +end +%% +lfpFilt = SimpleFilt(lfp, fs, 150, 250); +zPow = zscore(abs(hilbert(lfpFilt))); + +%% Identify Potential Sharpwave Periods +abvThresh1 = zPow > powThresh(1); +epocStart = find(diff(abvThresh1)==1); +epocEnd = find(diff(abvThresh1)==-1); +epocWindows = [epocStart, nan(size(epocStart))]; +for epoc = 1:size(epocStart,1) + tempEpocEnd = epocEnd(find(epocEnd>epocStart(epoc), 1, 'first')); + if sum(zPow(epocStart(epoc):tempEpocEnd)>=powThresh(2))>=1 + epocWindows(epoc,2) = tempEpocEnd; + else + epocWindows(epoc,1) = nan; + end +end +epocWindows(isnan(epocWindows(:,1)),:) = []; +% Merge short latency sharpwaves +for epoc = 2:size(epocWindows,1) + if ~isnan(epocWindows(epoc,1)) && ~isnan(epocWindows(epoc-1,1)) + if epocWindows(epoc,1) - epocWindows(epoc-1,2) <= mergeThresh + epocWindows(epoc,1) = epocWindows(epoc-1,1); + epocWindows(epoc-1,:) = nan; + end + end +end +epocWindows(isnan(epocWindows(:,1)),:) = []; +%% +ripEpocs = epocWindows; +end + +function [swEpocs, hpf, zPow] = SharpwaveDetection(lfp, fs, powThresh, mergeThresh) +%% SharpwaveDetection +% Code for detection of sharpwaves on tetrodes presumed to be in the +% stratum radiatum of CA1 +% Inputs: +% lfp: Nx1 input of the raw LFP +% powThresh: 1x2 input with threshold parameters for detection of peak +% event. Initial value is event boundaries, second is peak required +% mergeThresh: threshold for merging two sharpwave events if they happen +% in close temporal proximity +%% +if nargin == 0 + [file,path] = uigetfile('*.mat'); + load([path file]); %#ok + lfp = statMatrix(:,2); %#ok + fs = 1/mode(diff(statMatrix(:,1))); + clear statMatrix statMatrixColIDs file path + powThresh = [0 2]; + mergeThresh = 15; +end +%% Filter & Z-Score Envelope +hpf = highpass(lfp, 4, fs); +zPow = zscore(abs(hilbert(hpf))); + +%% Identify Potential Sharpwave Periods +abvThresh1 = zPow > powThresh(1); +epocStart = find(diff(abvThresh1)==1); +epocEnd = find(diff(abvThresh1)==-1); +epocWindows = [epocStart, nan(size(epocStart))]; +for epoc = 1:size(epocStart,1) + tempEpocEnd = epocEnd(find(epocEnd>epocStart(epoc), 1, 'first')); + if sum(zPow(epocStart(epoc):tempEpocEnd)>=powThresh(2))>=1 + epocWindows(epoc,2) = tempEpocEnd; + else + epocWindows(epoc,1) = nan; + end +end +epocWindows(isnan(epocWindows(:,1)),:) = []; +% Merge short latency sharpwaves +for epoc = 2:size(epocWindows,1) + if ~isnan(epocWindows(epoc,1)) && ~isnan(epocWindows(epoc-1,1)) + if epocWindows(epoc,1) - epocWindows(epoc-1,2) <= mergeThresh + epocWindows(epoc,1) = epocWindows(epoc-1,1); + epocWindows(epoc-1,:) = nan; + end + end +end +epocWindows(isnan(epocWindows(:,1)),:) = []; +%% +swEpocs = epocWindows; +%% +end + +%% Simple Filtering +function lfpFilt = SimpleFilt(trace, samp, low, high) +Wn_FRange = [low/(samp/2) high/(samp/2)]; % normalized by the nyquist frequency +[bFRange, aFRange] = butter(3,Wn_FRange); +lfpFilt = filtfilt(bFRange,aFRange,trace); +end \ No newline at end of file From 4ff751abe81878c9c5f7af72dc0b712ef054e777 Mon Sep 17 00:00:00 2001 From: gaelias Date: Mon, 25 Oct 2021 15:51:54 -0700 Subject: [PATCH 128/133] Bunch o' stuff --- Analyses/PFC/PFC_MLB.m | 16 +- Analyses/PlotUniSum_SM.m | 8 +- Analyses/SummarizeUnits_SM.m | 8 +- Data Management/SimpleHilbFilt.m | 8 + Examples/SimpleExampleUI.m | 93 ++++++++ OSMdata Code/CalcStaticBayesPost.m | 18 ++ OSMdata Code/DecodeBayesPost.m | 11 + OSMdata Code/SimpleHilbFilt.m | 9 + OSMdata Code/SimpleOSMdataCreator.m | 100 ++++++++ OSMdata Code/SimpleOSMsessionSpectrogram.m | 37 +++ OSMdata Code/TEMPosmMLB.m | 167 +++++++++++++ OSMdata Code/TEMPosmMLBphaseCraziness.m | 225 ++++++++++++++++++ OSMdata Code/TEMPosmTrialSpectCoher.m | 187 +++++++++++++++ .../Supporting Code/SummarizePLXevents_SD.m | 12 +- 14 files changed, 880 insertions(+), 19 deletions(-) create mode 100644 Data Management/SimpleHilbFilt.m create mode 100644 Examples/SimpleExampleUI.m create mode 100644 OSMdata Code/CalcStaticBayesPost.m create mode 100644 OSMdata Code/DecodeBayesPost.m create mode 100644 OSMdata Code/SimpleHilbFilt.m create mode 100644 OSMdata Code/SimpleOSMdataCreator.m create mode 100644 OSMdata Code/SimpleOSMsessionSpectrogram.m create mode 100644 OSMdata Code/TEMPosmMLB.m create mode 100644 OSMdata Code/TEMPosmMLBphaseCraziness.m create mode 100644 OSMdata Code/TEMPosmTrialSpectCoher.m diff --git a/Analyses/PFC/PFC_MLB.m b/Analyses/PFC/PFC_MLB.m index 1c7a65e..e56b524 100644 --- a/Analyses/PFC/PFC_MLB.m +++ b/Analyses/PFC/PFC_MLB.m @@ -1,16 +1,18 @@ + %% PFC_MLB clc clear all %% Runtime variables -piWindow = [-0.5 0.5]; -poWindow = [-0.5 0.5]; -binSize = 200; -dsRate = 5; -cLim = [0 0.01]; - +% if nargin==0 + piWindow = [-0.5 0.5]; + poWindow = [-0.5 0.5]; + binSize = 200; + dsRate = 5; + cLim = [0 0.01]; + smPath = uigetdir; +% end %% -smPath = uigetdir; cd(smPath); files = dir(smPath); fileNames = {files.name}; diff --git a/Analyses/PlotUniSum_SM.m b/Analyses/PlotUniSum_SM.m index a2641d4..2881221 100644 --- a/Analyses/PlotUniSum_SM.m +++ b/Analyses/PlotUniSum_SM.m @@ -274,9 +274,9 @@ if uniSum.TrialEpochStats.Error.Odor(2)1 + if ~isnan(peR(2)) && length(peR)>1 curEpochCorrTable(1,6) = peR(2); curEpochSigTable(1,6) = peS(2); end @@ -300,7 +300,7 @@ curEpochCorrTable(2,5) = erR(2); curEpochSigTable(2,5) = erS(2); [eeR, eeS] = corrcoef(curUniErlyTrialActivity(~correctTrialLog & inSeqTrialLog), curUniErrTrlActivity(~correctTrialLog & inSeqTrialLog)); - if ~isnan(eeR) && length(eeR)>1 + if ~isnan(eeR(2)) && length(eeR)>1 curEpochCorrTable(2,6) = eeR(2); curEpochSigTable(2,6) = eeS(2); end @@ -312,7 +312,7 @@ curEpochCorrTable(3,5) = lrR(2); curEpochSigTable(3,5) = lrS(2); [leR, leS] = corrcoef(curUniLtTrialActivity(~correctTrialLog & inSeqTrialLog), curUniErrTrlActivity(~correctTrialLog & inSeqTrialLog)); - if ~isnan(leR) && length(leR)>1 + if ~isnan(leR(2)) && length(leR)>1 curEpochCorrTable(3,6) = leR(2); curEpochSigTable(3,6) = leS(2); end @@ -321,7 +321,7 @@ curEpochCorrTable(4,5) = prR(2); curEpochSigTable(4,5) = prS(2); [peR, peS] = corrcoef(curUniPstTrlActivity(~correctTrialLog & inSeqTrialLog), curUniErrTrlActivity(~correctTrialLog & inSeqTrialLog)); - if ~isnan(peR) && length(peR)>1 + if ~isnan(peR(2)) && length(peR)>1 curEpochCorrTable(4,6) = peR(2); curEpochSigTable(4,6) = peS(2); end diff --git a/Data Management/SimpleHilbFilt.m b/Data Management/SimpleHilbFilt.m new file mode 100644 index 0000000..d15e466 --- /dev/null +++ b/Data Management/SimpleHilbFilt.m @@ -0,0 +1,8 @@ +function [zeroTroughPhase, zPow] = SimpleHilbFilt(data, samp, window) + Wn_FRange = [window(1)/(samp/2) window(2)/(samp/2)]; % normalized by the nyquist frequency + [bFRange, aFRange] = butter(3,Wn_FRange); + filtered = filtfilt(bFRange,aFRange,data); + + zeroTroughPhase = atan2(imag(hilbert(filtered*-1)), real(hilbert(filtered*-1))); + zPow = zscore(abs(hilbert(filtered))); +end \ No newline at end of file diff --git a/Examples/SimpleExampleUI.m b/Examples/SimpleExampleUI.m new file mode 100644 index 0000000..8552d14 --- /dev/null +++ b/Examples/SimpleExampleUI.m @@ -0,0 +1,93 @@ +function SimpleExampleUI +%% +% uiFig = figure('Name', 'Simple UI', 'toolbar', 'none'); +uiFig = figure('Name', 'Simple UI', 'DeleteFcn', @WindowClose); + +annotation(uiFig, 'textbox', 'Position',[0.25 0.94 0.5 0.05], 'String', 'Simple UI',... + 'FontSize', 12, 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom',... + 'FitHeightToText','off','LineStyle','none'); + +uiData.leftButton = uicontrol(uiFig, 'Units', 'Normalized', 'Position', [0.05 0.2 0.4 0.75],... + 'Style', 'pushbutton', 'String', 'Left', 'FontSize',12, 'FontWeight', 'bold',... + 'Callback', @ActivateLeft); +uiData.rightButton = uicontrol(uiFig, 'Units', 'Normalized', 'Position', [0.55 0.2 0.4 0.75],... + 'Style', 'pushbutton', 'String', 'Right', 'FontSize',12, 'FontWeight', 'bold',... + 'Callback', @ActivateRight); + +annotation(uiFig, 'textbox', 'position', [0.05 0.14 0.4 0.05], 'String', 'Left Duration',... + 'FontSize', 12, 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom',... + 'FitHeightToText', 'off', 'LineStyle', 'none'); +annotation(uiFig, 'textbox', 'position', [0.55 0.14 0.4 0.05], 'String', 'Right Duration',... + 'FontSize', 12, 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom',... + 'FitHeightToText', 'off', 'LineStyle', 'none'); +uiData.leftDuration = uicontrol(uiFig, 'Units', 'Normalized', 'Position', [0.05 0.05 0.4 0.1],... + 'Style', 'edit', 'String', '100', 'Callback', @UpdateLeftDuration); +uiData.rightDuration = uicontrol(uiFig, 'Units', 'Normalized', 'Position', [0.55 0.05 0.4 0.1],... + 'Style', 'edit', 'String', '100', 'Callback', @UpdateRightDuration); + +% Read up on the timer object in the matlab documentation +uiData.leftRewardTimer = timer('StartFcn', {@WaterCallback, 0, 1}, 'TimerFcn', {@WaterCallback, 0, 0}, 'StartDelay', 0.1); +uiData.rightRewardTimer = timer('StartFcn', {@WaterCallback, 1, 1}, 'TimerFcn', {@WaterCallback, 1, 0}, 'StartDelay', 0.1); + +uiData.sessionStart = tic; +uiData.leftRewardTimes = nan(1,10); +uiData.rightRewardTimes = nan(1,10); +uiFig.UserData = uiData; + +end + +function ActivateLeft(hObject, evt) +fig = get(hObject, 'parent'); +handles = get(fig, 'UserData'); +start(handles.leftRewardTimer); +lastLR = find(~isnan(handles.leftRewardTimes),1,'last'); +if lastLR == size(handles.leftRewardTimes,2) + handles.leftRewardTimes = [handles.leftRewardTimes, nan(1,10)]; +end +handles.leftRewardTimes(lastLR+1) = toc(handles.sessionStart); +end + +function ActivateRight(hObject,evt) +fig = get(hObject, 'parent'); +handles = get(fig, 'UserData'); +start(handles.rightRewardTimer); +end + +function UpdateLeftDuration(hObject, evt) +fig = get(hObject, 'parent'); +handles = get(fig, 'UserData'); +handles.leftRewardTimer.StartDelay = str2double(handles.leftDuration.String)/1000; +end + +function UpdateRightDuration(hObject, evt) +fig = get(hObject, 'parent'); +handles = get(fig, 'UserData'); +handles.rightRewardTimer.StartDelay = str2double(handles.rightDuration.String)/1000; +end + +function WaterCallback(obj, event, pinID, setting) +if pinID == 0 + if setting == 1 + % Replace this with code to set Arduino pin for left water high + disp('Left Water On'); + elseif setting == 0 + % Replace this with code to set Arduino pin for left water low + disp('Left Water Off'); + end +elseif pinID == 1 + if setting == 1 + % Replace this with code to set Arduino pin for right water high + disp('Right Water On'); + elseif setting == 0 + % Replace this with code to set Arduino pin for right water low + disp('Right Water Off'); + end +end +end + +function WindowClose(hObject, evt) +% Write code here to save whatever you want from the file. +fig = get(hObject, 'parent'); +handles = get(fig, 'UserData'); +disp('Window Closed'); +end \ No newline at end of file diff --git a/OSMdata Code/CalcStaticBayesPost.m b/OSMdata Code/CalcStaticBayesPost.m new file mode 100644 index 0000000..0450d7a --- /dev/null +++ b/OSMdata Code/CalcStaticBayesPost.m @@ -0,0 +1,18 @@ +function post = CalcStaticBayesPost(likely, obsv, binSize) +post = nan(size(obsv,1), size(likely,1), size(obsv,3)); +for trl = 1:size(obsv,3) + for t = 1:size(obsv,1) + p = nan(size(likely)); + curPopVect = obsv(t,:,trl)*(binSize/1000); + curPopFact = factorial(curPopVect); + for u = 1:size(likely,2) + curAvgUniFR = likely(:,u); + p(:,u) = (((binSize/1000).*curAvgUniFR).^curPopVect(u))./curPopFact(u); + end + pp = prod(p,2); + ee = exp(-((binSize/1000)*sum(likely,2))); + tempPost = pp.*ee; + post(t,:,trl) = tempPost./sum(tempPost); + end +end +end \ No newline at end of file diff --git a/OSMdata Code/DecodeBayesPost.m b/OSMdata Code/DecodeBayesPost.m new file mode 100644 index 0000000..5c016cf --- /dev/null +++ b/OSMdata Code/DecodeBayesPost.m @@ -0,0 +1,11 @@ +function decode = DecodeBayesPost(post, id) +% Assumes post is in the structure of ObservTime X LikelyTime X Trial +decode = nan(size(post,1),size(post,3)); +for o = 1:size(post,3) + for d = 1:size(post,1) + if ~isnan(post(d,1,o)) + decode(d,o) = id(find(post(d,:,o)==max(post(d,:,o)),1,'first')); + end + end +end +end \ No newline at end of file diff --git a/OSMdata Code/SimpleHilbFilt.m b/OSMdata Code/SimpleHilbFilt.m new file mode 100644 index 0000000..4615791 --- /dev/null +++ b/OSMdata Code/SimpleHilbFilt.m @@ -0,0 +1,9 @@ +function [zeroTroughPhase, rawPow, zPow] = SimpleHilbFilt(data, samp, window) + Wn_FRange = [window(1)/(samp/2) window(2)/(samp/2)]; % normalized by the nyquist frequency + [bFRange, aFRange] = butter(3,Wn_FRange); + filtered = filtfilt(bFRange,aFRange,data); + + zeroTroughPhase = atan2(imag(hilbert(filtered*-1)), real(hilbert(filtered*-1))); + rawPow = abs(hilbert(filtered)); + zPow = zscore(rawPow); +end \ No newline at end of file diff --git a/OSMdata Code/SimpleOSMdataCreator.m b/OSMdata Code/SimpleOSMdataCreator.m new file mode 100644 index 0000000..25f625d --- /dev/null +++ b/OSMdata Code/SimpleOSMdataCreator.m @@ -0,0 +1,100 @@ +%% Grab and process the behavioral Data +[plxData] = SummarizePLXevents_SD; +load(plxData.Summary.MATfile); + +% Pull together the behavior data into a structure organized by trial +osmBehavior = struct('TrialNum', repmat({nan}, [1,length(ssnData)]),... + 'TrialPosition', repmat({nan}, [1,length(ssnData)]),... + 'TrialOdor', repmat({nan}, [1,length(ssnData)]),... + 'PortEntryTime', repmat({nan}, [1,length(ssnData)]),... + 'OdorDeliveryTime', repmat({nan}, [1,length(ssnData)]),... + 'PortExitTime', repmat({nan}, [1,length(ssnData)]),... + 'PerformanceLog', repmat({false}, [1,length(ssnData)]),... + 'RewardSignalTime', repmat({false}, [1,length(ssnData)]),... + 'ErrorSignalTime', repmat({false}, [1,length(ssnData)]),... + 'QuestionableTrialLog', repmat({false}, [1,length(ssnData)]),... + 'BehaviorPLX', cell(1,length(ssnData)),... + 'BehaviorMAT', cell(1,length(ssnData))); + +for trl = 1:length(ssnData) + % Pull out and verify Behavior Data + curPLX = plxData.Raw(trl); + curMAT = ssnData(trl); + osmBehavior(trl).TrialNum = trl; + if curPLX.OrdinalPosition == curMAT.TrialPosition + osmBehavior(trl).TrialPosition = curPLX.OrdinalPosition; + else + error('Trial position values do not match'); + end + if curPLX.SequenceItem == curMAT.Odor + osmBehavior(trl).TrialOdor = curPLX.SequenceItem; + else + error('Trial odor values do not match'); + end + if curPLX.Performance == curMAT.Performance + osmBehavior(trl).PerformanceLog = curPLX.Performance; + else + error('Trial performance values do not match'); + end + osmBehavior(trl).PortEntryTime = curPLX.OdorTrigPokeTime; + osmBehavior(trl).OdorDeliveryTime = curPLX.ItemPresentationTime; + osmBehavior(trl).PortExitTime = curPLX.OdorPokeWithdrawTime; + osmBehavior(trl).RewardSignalTime = curPLX.RewardSignalTime; + osmBehavior(trl).ErrorSignalTime = curPLX.ErrorSignalTime; + osmBehavior(trl).FrontRewardTime = curPLX.FrontRewardTime; + osmBehavior(trl).BackRewardTime = curPLX.BackRewardTime; + osmBehavior(trl).QuestionableTrialLog = curPLX.QuestionableTrialLog; + osmBehavior(trl).BehaviorPLX = curPLX; + osmBehavior(trl).BehaviorMAT = curMAT; +end + +%% Grab Neural data +% First pull out the LFP channel IDs +[~, plxADchanNames] = plx_adchan_names(plxData.Summary.PLXfile); +tetLFPchanNames = cell(size(plxADchanNames,1),1); +for tet = 1:size(plxADchanNames,1) + tetLFPchanNames{tet} = deblank(plxADchanNames(tet,:)); +end +% Now pull out the spike channel IDs +[~,plxTETchanNames] = plx_chan_names(plxData.Summary.PLXfile); +tetSPKchanNames = cell(size(plxTETchanNames,1),1); +for chan = 1:size(plxTETchanNames,1) + tetSPKchanNames{chan} = deblank(plxTETchanNames(chan,:)); +end +% Now extract the counts of spiking & LFP data then identify tetrode IDs +[~, wfCountFl, ~, contCountFl] = plx_info(plxData.Summary.PLXfile, 1); +lfpDataLog = contCountFl ~= 0; +tetLFPchanNames(~lfpDataLog) = []; +tetLFPchanNames(cellfun(@(a)isempty(a), regexp(tetLFPchanNames, '^T([0-9]*)'))) = []; +tetNames = cellfun(@(a)a(1:end-2), tetLFPchanNames','uniformoutput', 0); + +% Pull the LFP and spiking data from each tetrode and put them into a +% common structure variable +osmNeural = struct('TetName', tetNames, 'LFP', cell(1,length(tetNames)),... + 'Spikes', cell(1,length(tetNames))); +for tet = 1:length(tetLFPchanNames) + [samp, ~, tetTS, fn, osmNeural(tet).LFP] = plx_ad_v(plxData.Summary.PLXfile, tetLFPchanNames{tet}); + tetChan = find(strcmp(tetSPKchanNames, tetLFPchanNames{tet})); + spkCounts = wfCountFl(:,tetChan+1); + uni = 0; + for u = 1:sum(spkCounts>0) + if uni == 0 + [~, osmNeural(tet).Spikes.Unsorted] = plx_ts(plxData.Summary.PLXfile, tetChan, uni); + else + [~, osmNeural(tet).Spikes.(sprintf('U%i', uni))] = plx_ts(plxData.Summary.PLXfile, tetChan, uni); + end + uni = uni+1; + end +end + +% Create Timestamp Vector +tsVect = ((0:(fn(1)-1))/samp)+tetTS(1); +% Sometimes the file is composed of multiple fragments, in which case these +% should be combined into a single file +for fragNum = 2:length(tetTS) + tsVect = [tsVect, ((0:(fn(fragNum)-1))/samp)+tetTS(fragNum)]; %#ok +end + +%% Save it all! +save([plxData.Summary.PLXfile(1:end-4), '_OSMdata.mat'], 'osmNeural', 'osmBehavior', 'tsVect'); +fprintf('%s saved!\n', [plxData.Summary.PLXfile(1:end-4), '_OSMdata.mat']); \ No newline at end of file diff --git a/OSMdata Code/SimpleOSMsessionSpectrogram.m b/OSMdata Code/SimpleOSMsessionSpectrogram.m new file mode 100644 index 0000000..b130d11 --- /dev/null +++ b/OSMdata Code/SimpleOSMsessionSpectrogram.m @@ -0,0 +1,37 @@ +freqs = 1:100; +for tet = 1:length(osmNeural) + zData = zscore(osmNeural(tet).LFP); +% zData = zscore(osmLFP(:,tet) - mean(osmLFP(:,2:end),2)); +% zData = osmLFP(:,tet) - mean(osmLFP(:,2:end),2); + [s,fs,t] = spectrogram(zData, 1000, 900, freqs, 1000); + ss = 10*log10(abs(s)); + zSS = ss; + for f = 1:size(ss,1) + zSS(f,:) = zscore(ss(f,:)); + end + figure; + sp1 = subplot(2,3,[1 2]); + imagesc(t,fs,ss); + hold on; + scatter([osmBehavior.PortEntryTime], ones(1,length(osmBehavior))*20, '*k'); + scatter([osmBehavior.PortExitTime], ones(1,length(osmBehavior))*20, 'ok'); + set(gca, 'ydir', 'normal'); + title(osmNeural(tet).TetName); + colorbar; + + sp2 = subplot(2,3,[4 5]); + imagesc(t,fs,zSS, [-4 4]); + hold on; + scatter([osmBehavior.PortEntryTime], ones(1,length(osmBehavior))*20, '*k'); + scatter([osmBehavior.PortExitTime], ones(1,length(osmBehavior))*20, 'ok'); + set(gca, 'ydir', 'normal'); + colormap jet; + linkaxes([sp1 sp2], 'xy'); + colorbar; + + subplot(2,3,3); + periodogram(zData, [], freqs, 1000); + + subplot(2,3,6); + plot(fs,median(zSS,2)) +end diff --git a/OSMdata Code/TEMPosmMLB.m b/OSMdata Code/TEMPosmMLB.m new file mode 100644 index 0000000..e5d8f10 --- /dev/null +++ b/OSMdata Code/TEMPosmMLB.m @@ -0,0 +1,167 @@ +%% Runtime variables +piWindow = [-0.5 0.5]; +poWindow = [-0.5 0.5]; +binSize = 200; +dsRate = 5; +cLim = [0 0.01]; + +piWindowTS = piWindow(1):(dsRate*mode(diff(tsVect))):piWindow(2); +poWindowTS = poWindow(1):(dsRate*mode(diff(tsVect))):poWindow(2); + +%% Create the spikeMtx: the multiunit signal from every tetrode by aggregating the cut and uncut units together. +tempTSvect = tsVect; +tempTSvect(end+1) = tsVect(end)+(mode(diff(tsVect))); +spikeMtx = nan(length(tsVect), length(osmNeural)); +for t = 1:length(osmNeural) + tempTetSpks = osmNeural(t).Spikes; + tempSpikes = []; + if ~isempty(tempTetSpks) + uniNms = fieldnames(tempTetSpks); + for u = 1:length(uniNms) + tempSpikes = [tempSpikes; tempTetSpks.(uniNms{u})]; %#ok + end + spikeMtx(:,t) = histcounts(tempSpikes, tempTSvect); + end +end +tetNames = {osmNeural.TetName}; +tetNames(isnan(spikeMtx(1,:))) = []; +spikeMtx(:,isnan(spikeMtx(1,:))) = []; + +%% Bin spiking on a per trial basis +piSpikeMtx = nan(length(piWindowTS)-1, length(tetNames), length(osmBehavior)); +poSpikeMtx = nan(length(poWindowTS)-1, length(tetNames), length(osmBehavior)); +trlSpikeMtx = nan(length(piWindowTS) + length(poWindowTS) -2, length(tetNames), length(osmBehavior)); +for trl = 1:length(osmBehavior) + pokeInWindowLog = tsVect>osmBehavior(trl).PortEntryTime + piWindow(1) - (binSize/2*mode(diff(tsVect))) & tsVect<=osmBehavior(trl).PortEntryTime + piWindow(2) + (binSize/2*mode(diff(tsVect))); + pokeOutWindowLog = tsVect>osmBehavior(trl).PortExitTime + poWindow(1) - (binSize/2*mode(diff(tsVect))) & tsVect<=osmBehavior(trl).PortExitTime + poWindow(2) + (binSize/2*mode(diff(tsVect))); + + for tet = 1:length(tetNames) + tempSpksPI = conv(spikeMtx(pokeInWindowLog,tet), ones(1,binSize)./(binSize/1000), 'same'); + tempSpksPI = tempSpksPI((binSize/2)+1:end-(binSize/2)); + piSpikeMtx(:,tet,trl) = downsample(tempSpksPI,dsRate); + + tempSpksPO = conv(spikeMtx(pokeOutWindowLog,tet), ones(1,binSize)./(binSize/1000), 'same'); + tempSpksPO = tempSpksPO((binSize/2)+1:end-(binSize/2)); + poSpikeMtx(:,tet,trl) = downsample(tempSpksPO,dsRate); + + trlSpikeMtx(:,tet,trl) = [piSpikeMtx(:,tet,trl);poSpikeMtx(:,tet,trl)]; + end + fprintf('Trial %i\n', trl) +end + +%% Create logical masks +% Use these to select trials +trlPos = [osmBehavior.TrialPosition]; +trlOdr = [osmBehavior.TrialOdor]; +isTrlLog = (trlPos - trlOdr)==0; +perfTrlLog = [osmBehavior.PerformanceLog]==1; + +fisLog = false(1,length(osmBehavior)); +for t = 4:length(osmBehavior) + fisLog(t) = sum([osmBehavior(t-3:t).TrialPosition] == 1:4)==4 &&... + sum([osmBehavior(t-3:t).TrialOdor] == 1:4)==4 &&... + sum([osmBehavior(t-3:t).PerformanceLog])==4; + if fisLog(t) == true + fisLog(t-3:t) = true; + end +end + +% Use these to navigate the FIS trial spike matrix (created below) +piIDvect = repmat([true(1,length(piWindowTS)-1), false(1,length(poWindowTS)-1)], [1,4]); +poIDvect = repmat([false(1,length(piWindowTS)-1), true(1,length(poWindowTS)-1)], [1,4]); +odrIDvect = zeros(1,(length(piWindowTS)-1 + length(poWindowTS)-1)*4); +for o = 1:4 + odrIDvect((o-1)*(length(piWindowTS)+length(poWindowTS)-2)+1:(o*(length(piWindowTS)+length(poWindowTS)-2))) = o; +end +timIDvect = repmat(1:length(piWindowTS)+length(poWindowTS)-2, [1,4]); + +%% Extract FIS Trials to create likelihood matrix +fisSpikeMtx = nan(length(odrIDvect), length(tetNames), sum(fisLog)/4); +for odr = 1:4 + tempFISodrVect = find(fisLog & trlOdr==odr); + for tet = 1:length(tetNames) + for trl = 1:length(tempFISodrVect) + fisSpikeMtx(odrIDvect==odr,tet,trl) = trlSpikeMtx(:,tet,tempFISodrVect(trl)); + end + end +end + +%% Perform leave 1 out decoding on FIS data +fisPost = nan(size(fisSpikeMtx,1), size(fisSpikeMtx,1), size(fisSpikeMtx,3)); +odrDecode = nan(size(fisSpikeMtx,1),size(fisSpikeMtx,3)); +for seq = 1:size(fisSpikeMtx,3) + curObsv = fisSpikeMtx(:,:,seq); + tempLikely = fisSpikeMtx; + tempLikely(:,:,seq) = []; + fisPost(:,:,seq) = CalcStaticBayesPost(mean(tempLikely,3), curObsv, binSize); +end +fisODRdecode = DecodeBayesPost(fisPost, odrIDvect); +fisODRdecoded = nan(4,size(fisODRdecode,1)); +for o = 1:4 + fisODRdecoded(o,:) = smooth(mean(fisODRdecode == o,2),10); +end +fisLATdecode = DecodeBayesPost(fisPost, timIDvect); +for seq = 1:size(fisLATdecode,2) + fisLATdecode(:,seq) = fisLATdecode(:,seq) - timIDvect'; +end +lagMean = smooth(mean(fisLATdecode,2),10); +lagVar = std(fisLATdecode,0,2); + + +%% Plot the MLB summary +figure; +% Plot the probability density +subplot(5,5,[1:4, 6:9, 11:14, 16:19]); +imagesc(mean(fisPost,3), [0 0.01]); +set(gca, 'ydir', 'normal'); +colormap jet; +% Plot odor decoding through the sequence +subplot(5,5,21:24); +plot(fisODRdecoded(1,:), 'color', [44/255 168/255 224/255], 'linewidth', 1); +hold on; +plot(fisODRdecoded(2,:), 'color', [154/255 133/255 122/255], 'linewidth', 1); +plot(fisODRdecoded(3,:), 'color', [9/255 161/255 74/255], 'linewidth', 1); +plot(fisODRdecoded(4,:), 'color', [128/255 66/255 151/255], 'linewidth', 1); +% Plot temporal decoding through the sequence +subplot(5,5,5:5:20) +plot(lagMean, 1:size(fisLATdecode,1), 'k'); +hold on; +plot([0 0], get(gca, 'ylim'), ':k'); +patch('XData', [lagMean+lagVar; flipud(lagMean-lagVar)],... + 'YData', [1:length(lagMean), length(lagMean):-1:1], 'FaceColor', 'k', 'FaceAlpha', .3, 'edgecolor', 'none'); + + +%% Functions %% + +%% +function post = CalcStaticBayesPost(likely, obsv, binSize) +post = nan(size(obsv,1), size(likely,1), size(obsv,3)); +for trl = 1:size(obsv,3) + for t = 1:size(obsv,1) + p = nan(size(likely)); + curPopVect = obsv(t,:,trl)*(binSize/1000); + curPopFact = factorial(curPopVect); + for u = 1:size(likely,2) + curAvgUniFR = likely(:,u); + p(:,u) = (((binSize/1000).*curAvgUniFR).^curPopVect(u))./curPopFact(u); + end + pp = prod(p,2); + ee = exp(-((binSize/1000)*sum(likely,2))); + tempPost = pp.*ee; + post(t,:,trl) = tempPost./sum(tempPost); + end +end +end + +%% +function decode = DecodeBayesPost(post, id) +% Assumes post is in the structure of ObservTime X LikelyTime X Trial +decode = nan(size(post,1),size(post,3)); +for o = 1:size(post,3) + for d = 1:size(post,1) + if ~isnan(post(d,1,o)) + decode(d,o) = id(find(post(d,:,o)==max(post(d,:,o)),1,'first')); + end + end +end +end \ No newline at end of file diff --git a/OSMdata Code/TEMPosmMLBphaseCraziness.m b/OSMdata Code/TEMPosmMLBphaseCraziness.m new file mode 100644 index 0000000..4f826ca --- /dev/null +++ b/OSMdata Code/TEMPosmMLBphaseCraziness.m @@ -0,0 +1,225 @@ +%% Runtime variables +piWindow = [-0.5 0.5]; +poWindow = [-0.5 0.5]; +slideWin = 200; +dsRate = 5; +phaseBins = -pi:pi/3.5:pi; +cLim = [0 0.01]; +spectRanges = [[4,12];... + [16, 32];... + [40 59];... + [61 100]]; + +piWindowTS = piWindow(1):(dsRate*mode(diff(tsVect))):piWindow(2); +poWindowTS = poWindow(1):(dsRate*mode(diff(tsVect))):poWindow(2); + +%% Create the spikeMtx: the multiunit signal from every tetrode by aggregating the cut and uncut units together. +tempTSvect = tsVect; +tempTSvect(end+1) = tsVect(end)+(mode(diff(tsVect))); +spikeMtx = nan(length(tsVect), length(osmNeural)); +lfpInstPhase = nan(length(tsVect), length(osmNeural), size(spectRanges,1)); + +for t = 1:length(osmNeural) + tempTetSpks = osmNeural(t).Spikes; + tempSpikes = []; + if ~isempty(tempTetSpks) + uniNms = fieldnames(tempTetSpks); + if length(uniNms)~=1 + tempTetLFP = osmNeural(t).LFP; + for f = 1:size(spectRanges,1) + lfpInstPhase(:,t,f) = SimpleHilbFilt(tempTetLFP, 1/mode(diff(tsVect)), spectRanges(f,:)); + end + for u = 2:length(uniNms) + tempSpikes = [tempSpikes; tempTetSpks.(uniNms{u})]; %#ok + end + spikeMtx(:,t) = histcounts(tempSpikes, tempTSvect); + end + end +end +tetNames = {osmNeural.TetName}; +tetNames(isnan(spikeMtx(1,:))) = []; +lfpInstPhase(:,isnan(spikeMtx(1,:)),:) = []; +for ph = 1:size(lfpInstPhase,3) + lfpInstPhase(:,:,ph) = [lfpInstPhase(:,end,ph), lfpInstPhase(:,1:end-1, ph)]; % Rotate LFP to other tetrodes to break LFP/spiking relationship +% lfpInstPhase(:,:,ph) = repmat(lfpInstPhase(:,1,ph), [1,size(lfpInstPhase,2)]); % Or... use a common tetrode's LFP signal +end +spikeMtx(:,isnan(spikeMtx(1,:))) = []; + +%% Bin spiking on a per trial basis +piSpikeMtx = nan(length(piWindowTS)-1, length(tetNames), length(osmBehavior)); +poSpikeMtx = nan(length(poWindowTS)-1, length(tetNames), length(osmBehavior)); +trlSpikeMtx = nan(length(piWindowTS) + length(poWindowTS) -2, length(tetNames), length(osmBehavior)); + +piSpikeMtxPhase = repmat({piSpikeMtx}, [size(spectRanges,1), length(phaseBins)-1]); +poSpikeMtxPhase = repmat({piSpikeMtx}, [size(spectRanges,1), length(phaseBins)-1]); +trlSpikeMtxPhase = repmat({[piSpikeMtx;poSpikeMtx]}, [size(spectRanges,1), length(phaseBins)-1]); + +lfpInstPhaseTrlMtx = repmat({nan(length(piWindowTS) + length(poWindowTS) -2, length(tetNames), length(osmBehavior))}, [1 size(spectRanges,1)]); + +for trl = 1:length(osmBehavior) + pokeInWindowLog = tsVect>osmBehavior(trl).PortEntryTime + piWindow(1) - (slideWin/2*mode(diff(tsVect))) & tsVect<=osmBehavior(trl).PortEntryTime + piWindow(2) + (slideWin/2*mode(diff(tsVect))); + pokeOutWindowLog = tsVect>osmBehavior(trl).PortExitTime + poWindow(1) - (slideWin/2*mode(diff(tsVect))) & tsVect<=osmBehavior(trl).PortExitTime + poWindow(2) + (slideWin/2*mode(diff(tsVect))); + + for tet = 1:length(tetNames) + curPIspikes = spikeMtx(pokeInWindowLog,tet); + tempSpksPI = conv(curPIspikes, ones(1,slideWin)./(slideWin/1000), 'same'); + piSpikeMtx(:,tet,trl) = downsample(tempSpksPI((slideWin/2)+1:end-(slideWin/2)),dsRate); + + curPOspikes = spikeMtx(pokeOutWindowLog,tet); + tempSpksPO = conv(curPOspikes, ones(1,slideWin)./(slideWin/1000), 'same'); + poSpikeMtx(:,tet,trl) = downsample(tempSpksPO((slideWin/2)+1:end-(slideWin/2)),dsRate); + + trlSpikeMtx(:,tet,trl) = [piSpikeMtx(:,tet,trl);poSpikeMtx(:,tet,trl)]; + for freq = 1:size(spectRanges,1) + curTetFreqHilbPI = lfpInstPhase(pokeInWindowLog,tet,freq); + curTetFreqHilbPO = lfpInstPhase(pokeOutWindowLog,tet,freq); + lfpInstPhaseTrlMtx{freq}(:,tet,trl) = [downsample(curTetFreqHilbPI((slideWin/2)+1:end-(slideWin/2)), dsRate);... + downsample(curTetFreqHilbPO((slideWin/2)+1:end-(slideWin/2)), dsRate)]; + for phase = 2:length(phaseBins) + tempPIspks = curPIspikes; + curPhaseLogPI = curTetFreqHilbPI>=phaseBins(phase-1) & curTetFreqHilbPI=phaseBins(phase-1) & curTetFreqHilbPOosmBehavior(trl).PortEntryTime + piWindow(1) - (spectWin/2000) & tsVect<=osmBehavior(trl).PortEntryTime + piWindow(2) + (spectWin/2000); + pokeOutWindowLog = tsVect>osmBehavior(trl).PortExitTime + poWindow(1) - (spectWin/2000) & tsVect<=osmBehavior(trl).PortExitTime + poWindow(2) + (spectWin/2000); + + betaHilbPI = nan(sum(pokeInWindowLog), length(osmNeural)); + betaHilbPO = nan(sum(pokeOutWindowLog), length(osmNeural)); + osmBehavior(trl).ThetaPhaseBinPI = nan(length(0:spectWin-spectOverlap:diff(piWindow)*1000),length(osmNeural)); + osmBehavior(trl).ThetaPhaseBinPO = nan(length(0:spectWin-spectOverlap:diff(poWindow)*1000),length(osmNeural)); + + + osmBehavior(trl).PIspect = nan(length(freqs), length(0:spectWin-spectOverlap:diff(piWindow)*1000), length(osmNeural)); %#ok<*SAGROW> + osmBehavior(trl).POspect = nan(length(freqs), length(0:spectWin-spectOverlap:diff(poWindow)*1000), length(osmNeural)); + for tet = 1:length(osmNeural) + tetPokeInLFP = osmNeural(tet).LFP(pokeInWindowLog); + tetPokeOutLFP = osmNeural(tet).LFP(pokeOutWindowLog); + if tet == 1 + [tempPIspect, trlFreqs{trl}, ts] = spectrogram(tetPokeInLFP, spectWin, spectOverlap, freqs, 1000); + tempTs = ts - (spectWin/2000); + [tempPOspect, ~, ~] = spectrogram(tetPokeOutLFP, spectWin, spectOverlap, freqs, 1000); + else + [tempPIspect, ~, ~] = spectrogram(tetPokeInLFP, spectWin, spectOverlap, freqs, 1000); + [tempPOspect, ~, ~] = spectrogram(tetPokeOutLFP, spectWin, spectOverlap, freqs, 1000); + end + osmBehavior(trl).PIspect(:,:,tet) = 10*log10(abs(tempPIspect)); + for t = 1:size(osmBehavior(trl).PIspect,2) + osmBehavior(trl).PIspect(:,t,tet) = (osmBehavior(trl).PIspect(:,t,tet)-spectNormMean(:,tet))./spectNormVar(:,tet); + end + osmBehavior(trl).POspect(:,:,tet) = 10*log10(abs(tempPOspect)); + for t = 1:size(osmBehavior(trl).POspect,2) + osmBehavior(trl).POspect(:,t,tet) = (osmBehavior(trl).POspect(:,t,tet)-spectNormMean(:,tet))./spectNormVar(:,tet); + end + [betaHilbPI(:,tet), ~] = PhaseFreqDetectAbbr(tetPokeInLFP, tsVect(pokeInWindowLog), 16, 32); + [betaHilbPO(:,tet), ~] = PhaseFreqDetectAbbr(tetPokeOutLFP, tsVect(pokeOutWindowLog), 16, 32); + [thetaHilbPI, ~] = PhaseFreqDetectAbbr(tetPokeInLFP, tsVect(pokeInWindowLog), 4, 12); + piNndx = 0; + for tBin = 0:spectWin-spectOverlap:diff(piWindow)*1000 + piNndx = piNndx+1; + osmBehavior(trl).ThetaPhaseBinPI(piNndx,tet) = circ_mean(thetaHilbPI(tBin+1:tBin+(spectWin-spectOverlap))); + end + [thetaHilbPO, ~] = PhaseFreqDetectAbbr(tetPokeInLFP, tsVect(pokeOutWindowLog), 4, 12); + poNndx = 0; + for tBin = 0:spectWin-spectOverlap:diff(poWindow)*1000 + poNndx = poNndx+1; + osmBehavior(trl).ThetaPhaseBinPO(poNndx,tet) = circ_mean(thetaHilbPO(tBin+1:tBin+(spectWin-spectOverlap))); + end + end + tempTrlCoherPI = nan(length(osmNeural), length(osmNeural), length(0:spectWin-spectOverlap:diff(piWindow)*1000)); + tempTrlCoherPO = nan(length(osmNeural), length(osmNeural), length(0:spectWin-spectOverlap:diff(piWindow)*1000)); + for tet1 = 1:length(osmNeural) + for tet2 = 1:length(osmNeural) + if tet1 == tet2 + continue; + else + tsNdx = 1; + for ts = 1:(spectWin-spectOverlap):size(betaHilbPI,1)-(spectWin)+1 + [tempTrlCoherPI(tet1, tet2, tsNdx), ~] = circ_corrcc(betaHilbPI(ts:ts+spectWin-1,tet1), betaHilbPI(ts:ts+spectWin-1,tet2)); + [tempTrlCoherPO(tet1, tet2, tsNdx), ~] = circ_corrcc(betaHilbPO(ts:ts+spectWin-1,tet1), betaHilbPO(ts:ts+spectWin-1,tet2)); + tsNdx = tsNdx+1; + end + end + end + end + osmBehavior(trl).PIcoher = tempTrlCoherPI; + osmBehavior(trl).PIcoherMean = nanmean(tempTrlCoherPI,3); + osmBehavior(trl).POcoher = tempTrlCoherPO; + osmBehavior(trl).POcoherMean = nanmean(tempTrlCoherPO,3); +end + +%% +isTrlLog = ([osmBehavior.TrialPosition] - [osmBehavior.TrialOdor])==0; +perfTrlLog = [osmBehavior.PerformanceLog]==1; +iscTrials = osmBehavior(isTrlLog & perfTrlLog); +oscTrials = osmBehavior(~isTrlLog & perfTrlLog); + +for tet = 1:length(osmNeural) + iscPIspect = median(cell2mat(reshape(cellfun(@(a)a(:,:,tet), {iscTrials.PIspect}, 'uniformoutput', 0), [1,1,length(iscTrials)])),3); + oscPIspect = median(cell2mat(reshape(cellfun(@(a)a(:,:,tet), {oscTrials.PIspect}, 'uniformoutput', 0), [1,1,length(oscTrials)])),3); + iscPOspect = median(cell2mat(reshape(cellfun(@(a)a(:,:,tet), {iscTrials.POspect}, 'uniformoutput', 0), [1,1,length(iscTrials)])),3); + oscPOspect = median(cell2mat(reshape(cellfun(@(a)a(:,:,tet), {oscTrials.POspect}, 'uniformoutput', 0), [1,1,length(oscTrials)])),3); + figure; + annotation('textbox', 'Position',[0.05 0.95 0.9 0.05],... + 'String', osmNeural(tet).TetName, 'HorizontalAlignment', 'Left',... + 'Fontweight', 'bold',... + 'VerticalAlignment', 'bottom', 'FitHeightToText','off',... + 'LineStyle','none', 'Interpreter', 'none'); + subplot(2,2,1); + imagesc((0:spectWin-spectOverlap:diff(piWindow)*1000)-500, freqs, iscPIspect, [-2 2]); + title('ISC Poke In Aligned'); + set(gca, 'ydir', 'normal'); + subplot(2,2,2); + imagesc((0:spectWin-spectOverlap:diff(poWindow)*1000)-500, freqs, iscPOspect, [-2 2]); + title('ISC Poke Out Aligned'); + set(gca, 'ydir', 'normal'); + subplot(2,2,3); + imagesc((0:spectWin-spectOverlap:diff(piWindow)*1000)-500, freqs, oscPIspect, [-2 2]); + title('ISC Poke In Aligned'); + set(gca, 'ydir', 'normal'); + subplot(2,2,4); + imagesc((0:spectWin-spectOverlap:diff(poWindow)*1000)-500, freqs, oscPOspect, [-2 2]); + title('ISC Poke Out Aligned'); + set(gca, 'ydir', 'normal'); +end + +figure; +iscPIcoher = median(cell2mat(reshape({iscTrials.PIcoherMean}, [1,1,length(iscTrials)])),3); +oscPIcoher = median(cell2mat(reshape({oscTrials.PIcoherMean}, [1,1,length(oscTrials)])),3); +iscPOcoher = median(cell2mat(reshape({iscTrials.POcoherMean}, [1,1,length(iscTrials)])),3); +oscPOcoher = median(cell2mat(reshape({oscTrials.POcoherMean}, [1,1,length(oscTrials)])),3); +subplot(2,2,1); +imagesc(1:length(osmNeural), 1:length(osmNeural), iscPIcoher, [-1 1]); +set(gca, 'xtick', 1:length(osmNeural), 'xticklabel', {osmNeural.TetName}, 'ytick', 1:length(osmNeural), 'yticklabel', {osmNeural.TetName}); +title('ISC Beta Coherence (PI)'); + +subplot(2,2,2); +imagesc(1:length(osmNeural), 1:length(osmNeural), iscPOcoher, [-1 1]); +set(gca, 'xtick', 1:length(osmNeural), 'xticklabel', {osmNeural.TetName}, 'ytick', 1:length(osmNeural), 'yticklabel', {osmNeural.TetName}); +title('ISC Beta Coherence (PO)'); + +subplot(2,2,3); +imagesc(1:length(osmNeural), 1:length(osmNeural), oscPIcoher, [-1 1]); +set(gca, 'xtick', 1:length(osmNeural), 'xticklabel', {osmNeural.TetName}, 'ytick', 1:length(osmNeural), 'yticklabel', {osmNeural.TetName}); +title('OSC Beta Coherence (PI)'); + +subplot(2,2,4); +imagesc(1:length(osmNeural), 1:length(osmNeural), oscPOcoher, [-1 1]); +set(gca, 'xtick', 1:length(osmNeural), 'xticklabel', {osmNeural.TetName}, 'ytick', 1:length(osmNeural), 'yticklabel', {osmNeural.TetName}); +title('OSC Beta Coherence (PI)'); +colormap jet + +%% +phaseBins = -pi:pi/5:pi; +for tet = 1:length(osmNeural) + tpbPI = cell2mat(cellfun(@(a)a(:,tet),{iscTrials.ThetaPhaseBinPI}, 'uniformoutput', 0)); + tpbPO = cell2mat(cellfun(@(a)a(:,tet),{iscTrials.ThetaPhaseBinPO}, 'uniformoutput', 0)); + betaPowerPI = cell2mat(cellfun(@(a)mean(a(16:32,:,tet),1)', {iscTrials.PIspect}, 'uniformoutput', 0)); + betaPowerPO = cell2mat(cellfun(@(a)mean(a(16:32,:,tet),1)', {iscTrials.POspect}, 'uniformoutput', 0)); + + piThetaBetaMod = nan(length(phaseBins)-1, length(0:spectWin-spectOverlap:diff(piWindow)*1000)); + for t = 1:length(0:spectWin-spectOverlap:diff(piWindow)*1000) + curTPB = tpbPI(t,:); + for p = 1:length(phaseBins)-1 + tpbLog = curTPB>=phaseBins(p) & curTPB=phaseBins(p) & curTPB Date: Wed, 8 Dec 2021 09:39:05 -0800 Subject: [PATCH 129/133] Update SummarizePLXevents_SD.m --- statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m | 3 +++ 1 file changed, 3 insertions(+) diff --git a/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m b/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m index 1cadd9c..89aba1a 100644 --- a/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m +++ b/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m @@ -485,6 +485,7 @@ %% Extract Session Timestamps for trl = 1:size(odorPresSsn,1) + trl % Fill in timestamp for when the sequence started plxSession(trl).SessionBlockStartTime = sequenceBlockInitiationTimes(plxSession(trl).SessionBlockNumber); @@ -563,6 +564,8 @@ plxSession(trl).QuestionableTrialLog = true; break end + elseif abs(plxSession(trl).TranspositionDistance) == 10 + break end elseif tempPokeNum > sum(trialPokesLog) || plxSession(trl).TargetDuration - tempPokeDur < graceDur(trl) || isnan(trialInterPokeIntervals(tempPokeNum)) break From 5a5732b09d6b5c1b07e0cc4a66fbbfcdda6b2778 Mon Sep 17 00:00:00 2001 From: gaelias Date: Wed, 9 Aug 2023 13:40:43 -0700 Subject: [PATCH 130/133] unpushed updates --- Analyses/CalculateRI.m | 20 +++++++++++++++++++ Analyses/SequenceNeuralViewer_SM.m | 2 +- Analyses/SequenceViewerNeural_SM.m | 29 +++++++++++++++++++--------- Data Management/EpochExtraction_SM.m | 2 +- 4 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 Analyses/CalculateRI.m diff --git a/Analyses/CalculateRI.m b/Analyses/CalculateRI.m new file mode 100644 index 0000000..6830631 --- /dev/null +++ b/Analyses/CalculateRI.m @@ -0,0 +1,20 @@ +function riVal = CalculateRI(responseMatrix) +if sum(responseMatrix(1,:)) == 1 || sum(responseMatrix(2,:)) == 1 + riVal = nan; +else + h = responseMatrix(1,1)/sum(responseMatrix(1,:)); + if h == 1 + h = (sum(responseMatrix(1,:))-1)/sum(responseMatrix(1,:)); + elseif h == 0 + h = 1/sum(responseMatrix(1,:)); + end + fa = responseMatrix(2,1)/sum(responseMatrix(2,:)); + if fa == 1 + fa = (sum(responseMatrix(2,:))-1)/sum(responseMatrix(2,:)); + elseif fa == 0 + fa = 1/sum(responseMatrix(2,:)); + end + riVal = (h+fa-1)/(1-((h-fa)^2)); +end + +end \ No newline at end of file diff --git a/Analyses/SequenceNeuralViewer_SM.m b/Analyses/SequenceNeuralViewer_SM.m index 5e52811..f2dcec4 100644 --- a/Analyses/SequenceNeuralViewer_SM.m +++ b/Analyses/SequenceNeuralViewer_SM.m @@ -2,7 +2,7 @@ %% Declare Runtime Variables % Histogram bins for peak FR latency decision -histBins = 0:0.1:1.2; +histBins = -1.2:0.1:1.2; % FR threshold for trial period firing for inclusion in plot trialFRthresh = 0.1; % Spike width threshold for PC determination diff --git a/Analyses/SequenceViewerNeural_SM.m b/Analyses/SequenceViewerNeural_SM.m index 4328842..d7b6ea0 100644 --- a/Analyses/SequenceViewerNeural_SM.m +++ b/Analyses/SequenceViewerNeural_SM.m @@ -53,13 +53,13 @@ seqWindows = nan(numSeqs,2); for seq = 1:numSeqs curSeq = trlStruct([trlStruct.SequenceNum]==seq); - seqWindows(seq,1) = behavMatrix(curSeq(1).PokeInIndex-5000,1); - seqWindows(seq,2) = behavMatrix(curSeq(end).PokeOutIndex+5000,1); + seqWindows(seq,1) = behavMatrix(curSeq(1).PokeInIndex-1500,1); + seqWindows(seq,2) = behavMatrix(curSeq(end).PokeOutIndex+1500,1); end % Now determine the mean poke duration pokeDur = [trlStruct.PokeOutIndex] - [trlStruct.PokeInIndex]; meanPokeDurISC = mean(pokeDur(seqLog & perfLog))/1000; -trialWindows = [0 ceil(meanPokeDurISC*10)/10]; +trialWindows = [-1.2 ceil(meanPokeDurISC*10)/10]; % Use the Organize/Extract functions to pull out and organize the trial data behavMatrixTrialStruct = OrganizeTrialData_SM(behavMatrix, behavMatrixColIDs, trialWindows, 'PokeIn'); ensembleTrialData = ExtractTrialData_SM(behavMatrixTrialStruct, ensembleMatrix(:,2:end)); %#ok<*NODEF> @@ -119,7 +119,7 @@ patchColor = 'k'; end patch(gca, 'XData', [behavMatrix(behavMatrixTrialStruct(trl).PokeInIndex,1), behavMatrix(behavMatrixTrialStruct(trl).PokeOutIndex,1), behavMatrix(behavMatrixTrialStruct(trl).PokeOutIndex,1),behavMatrix(behavMatrixTrialStruct(trl).PokeInIndex,1)],... - 'YData', [ones(1,2), repmat(intData{end,1}+1,[1,2])*-1],... + 'YData', [ones(1,2), repmat(length(plotData.UnitIDs)+1,[1,2])*-1],... 'FaceColor', patchColor, 'FaceAlpha', 0.15,... 'EdgeColor', patchColor); end @@ -147,7 +147,7 @@ 'Title', 'Organization Method', 'TitlePosition', 'centertop', 'Tag', 'orgMthd_BtnGrp',... 'SelectionChangedFcn', @ChangeTempOrgMethod); % Temp Org by Mean A -meanAorg = uicontrol(tempOrgMethod, 'Units', 'Normalized', 'Style', 'radiobutton', 'String', 'Mean Odor A',... +meanAorg = uicontrol(tempOrgMethod, 'Units', 'Normalized', 'Style', 'radiobutton', 'String', 'Peak Seq FR (reset with new seq)',... 'Position', [0.05, 0.7, 0.9, 0.2], 'Tag', 'orgMeanA_PB'); % Temp Org by Mean InSeq meanISorg = uicontrol(tempOrgMethod, 'Units', 'Normalized', 'Style', 'radiobutton', 'String', 'Mean InSeq',... @@ -219,7 +219,8 @@ %% function [spikeData] = DetermineEnsembleOrganization(orgType) -global svnFig +global svnFig figAxes +seqWin = round(get(figAxes, 'xlim')); plotData = get(svnFig, 'userData'); spikeTimes = plotData.UnitSpikeTimes; unitIDs = plotData.UnitIDs; @@ -235,8 +236,16 @@ case 3 trialLog = plotData.InSeqLog==1 & plotData.Performance==1 & plotData.TrialOdor~=1; end -trialHist = mean(plotData.TrialEnsembleBinned(:,:,trialLog),3)'; -normHist = trialHist./max(trialHist,[],2); +if orgType == 2 || orgType == 3 + trialHist = mean(plotData.TrialEnsembleBinned(:,:,trialLog),3)'; + normHist = trialHist./max(trialHist,[],2); +else + instFRgauss = (gausswin(200))/0.2; + normHist = cell2mat(cellfun(@(a){conv(histcounts(a(a>min(seqWin) & amin(seqWin) & plotData.SessionTime pData = pData(pcSortMtx(:,2),:); -pData(:,1) = num2cell(pData{1,1}:size(pData,1)+1); +pData(:,1) = num2cell(ndxStart:size(pData,1)+1); for p = 1:size(pData,1) set(rasterHandles(pData{p,1}), 'Xdata', pData{p,4}, 'Ydata', ones(1,length(pData{p,4})).*(pData{p,1}*-1)); end @@ -625,4 +635,5 @@ function PopoutPlot(source,event) title(sprintf('%s; Sequence %s', smFile, get(curSeqNumInput, 'String')), 'interpreter', 'none'); orient(gcf, 'tall'); orient(gcf, 'landscape'); +set(gcf, 'Renderer', 'Painters'); end \ No newline at end of file diff --git a/Data Management/EpochExtraction_SM.m b/Data Management/EpochExtraction_SM.m index c3b4d84..a969d9d 100644 --- a/Data Management/EpochExtraction_SM.m +++ b/Data Management/EpochExtraction_SM.m @@ -178,7 +178,7 @@ lfpCol = cellfun(@(a)~isempty(a), regexp(statMatrixColIDs, lfpColRegXprsnString)); lfpIDs = [lfpIDs, statMatrixColIDs(lfpCol)]; %#ok - lfpEventData = ExtractTrialData_SM(eventAlignedMatrix, statMatrix(:,lfpCol)); %#ok + lfpEventData = ExtractTrialData_SM(eventAlignedMatrix, statMatrix(:,lfpCol)); mptLogLFP = cellfun(@(a)isempty(a), lfpEventData); lfpEventData(mptLogLFP) = {nan(size(eventTimeBins))}; lfpEventData = cellfun(@(a)reshape(a, [1, size(a,1), size(a,2)]), lfpEventData, 'uniformoutput',0); From b8e6f20ac2fb89654d0dde0c2bc5a7fcba17fb09 Mon Sep 17 00:00:00 2001 From: Gabriel Elias Date: Wed, 9 Aug 2023 17:06:33 -0400 Subject: [PATCH 131/133] Update SummarizePLXevents_SD.m --- statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m | 3 +++ 1 file changed, 3 insertions(+) diff --git a/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m b/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m index 89aba1a..23c80f7 100644 --- a/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m +++ b/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m @@ -571,6 +571,9 @@ break else tempPokeNum = tempPokeNum+1; + if tempPokeNum > length(trialInterPokeIntervals) + break + end tempPokeDur = tempPokeDur + trialInterPokeIntervals(tempPokeNum-1)+trialPokeDurations(tempPokeNum); end end From 852b851f00da89a5b2cbbd90d200d53010078e2a Mon Sep 17 00:00:00 2001 From: Gabriel Elias Date: Wed, 9 Aug 2023 21:42:02 -0400 Subject: [PATCH 132/133] Updates to include port light flag --- .../CreateBehaviorMatrixPFCabbr.m | 71 ++++++++++--------- .../Supporting Code/SummarizePLXevents_SD.m | 13 +++- 2 files changed, 48 insertions(+), 36 deletions(-) diff --git a/statMatrix Creation/CreateBehaviorMatrixPFCabbr.m b/statMatrix Creation/CreateBehaviorMatrixPFCabbr.m index 4249aca..bdf8a0b 100644 --- a/statMatrix Creation/CreateBehaviorMatrixPFCabbr.m +++ b/statMatrix Creation/CreateBehaviorMatrixPFCabbr.m @@ -11,7 +11,7 @@ outfile = fopen(sprintf('%s_PLXeventSummary.txt', fileName), 'a+'); [plxData] = SummarizePLXevents_SD(plxFile, [], outfile); - +%% chan = 1; [samp, ~, tetTS, fn, ~] = plx_ad_v(plxData.Summary.PLXfile, chan); while tetTS == -1 @@ -19,7 +19,7 @@ [samp, ~, tetTS, fn, ~] = plx_ad_v(plxData.Summary.PLXfile, chan); end -% Create timestamp vector +%% Create timestamp vector tsVect = ((0:(fn(1)-1))/samp)+tetTS(1); % Sometimes the file is composed of multiple fragments, in which case these % should be combined into a single file @@ -30,70 +30,70 @@ seqLength = plxData.Summary.SequenceLength; maxSeqLength = max([plxData.Raw.OrdinalPosition]); -behPad = seqLength + maxSeqLength; -if isfield(plxData.Summary, 'DualListLog') && plxData.Summary.DualListLog - behPad = behPad + seqLength; -end -behVals = nan(length(tsVect)-1, behPad + 6); -behDataHeaders = cell(1,behPad + 8); +%% % Step through each sequence item/position and identify when odors were % presented % Do position first because it doesn't change with multiple lists % fprintf(outfile, 'Position Counts\n'); +posVals = nan(length(tsVect)-1, seqLength); +posHeaders = cell(1,seqLength); for pos = 1:maxSeqLength posPresTimes = [plxData.Raw([plxData.Raw.OrdinalPosition]==pos).ItemPresentationTime]; - behVals(:,pos) = histcounts(posPresTimes, tsVect)'; - behDataHeaders{pos} = ['Position' num2str(pos)]; + posVals(:,pos) = histcounts(posPresTimes, tsVect)'; + posHeaders{pos} = ['Position' num2str(pos)]; fprintf(outfile, ' Position #%i = %i trials\n', pos, length(posPresTimes)); end % fprintf(outfile, 'Odor Counts\n'); +seqVals = nan(length(tsVect)-1, seqLength); +seqHeaders = cell(1,seqLength); for seq = 1:seqLength itemPresTimes = [plxData.Raw([plxData.Raw.SequenceItem]==seq).ItemPresentationTime]; - behVals(:,seq+maxSeqLength) = histcounts(itemPresTimes, tsVect)'; - behDataHeaders{seq+maxSeqLength} = ['Odor' num2str(seq)]; + seqVals(:,seq) = histcounts(itemPresTimes, tsVect)'; + seqHeaders{seq} = ['Odor' num2str(seq)]; fprintf(outfile, ' Odor #%i = %i trials\n', seq, length(itemPresTimes)); end if isfield(plxData.Summary, 'DualListLog') && plxData.Summary.DualListLog + dlSeqVals = nan(length(tsVect)-1,seqLength); + dlSeqHeaders = cell(1,seqLength); for seq = 11:seqLength+10 itemPresTimes = [plxData.Raw([plxData.Raw.SequenceItem]==seq).ItemPresentationTime]; - behVals(:,seq+seqLength+maxSeqLength-10) = histcounts(itemPresTimes, tsVect)'; - behDataHeaders{seq+seqLength+maxSeqLength-10} = ['Odor' num2str(seq)]; + dlSeqVals(:,seq) = histcounts(itemPresTimes, tsVect)'; + dlSeqHeaders{seq} = ['Odor' num2str(seq)]; fprintf(outfile, ' Odor #%i = %i trials\n', seq, length(itemPresTimes)); end + seqVals = [seqVals, dlSeqVals]; + seqHeaders = [seqHeaders, dlSeqHeaders]; end - +% In Seq Vect inSeqOdorPres = [plxData.Raw([plxData.Raw.TranspositionDistance]==0).ItemPresentationTime]; outSeqOdorPres = [plxData.Raw(~([plxData.Raw.TranspositionDistance]==0)).ItemPresentationTime]; -behVals(:,behPad+1) = histcounts(inSeqOdorPres, tsVect)' - histcounts(outSeqOdorPres,tsVect)'; -behDataHeaders{behPad+1} = 'InSeqLog'; +isVals = histcounts(inSeqOdorPres, tsVect)' - histcounts(outSeqOdorPres,tsVect)'; fprintf(outfile, '\nCompiling InSeq trials.....\n %i trials were InSeq (%i%%)\n', length(inSeqOdorPres), round(length(inSeqOdorPres)/length(plxData.Raw),2)*100); + itmPresTimes = [plxData.Raw.ItemPresentationTime]; trialPerformance = [plxData.Raw.Performance]; corrTrials = itmPresTimes(logical(trialPerformance)); corTrlHistCounts = histcounts(corrTrials, tsVect)'; inCorrTrials = itmPresTimes(~logical(trialPerformance)); inCorTrlHistCounts = histcounts(inCorrTrials, tsVect)'; -behVals(:,behPad+2) = corTrlHistCounts + (inCorTrlHistCounts*-1); -behDataHeaders{behPad+2} = 'PerformanceLog'; +perfVals = corTrlHistCounts + (inCorTrlHistCounts*-1); fprintf(outfile, 'Compiling Performance.....\n %i trials were correct (%i%%)\n', sum(trialPerformance), round(mean(trialPerformance),2)*100); -behVals(:,behPad+3) = histcounts([plxData.Raw.OdorTrigPokeTime], tsVect)' - histcounts([plxData.Raw.OdorPokeWithdrawTime], tsVect)'; -behDataHeaders{behPad+3} = 'PokeEvents'; +lightVals = histcounts([plxData.Raw.TrialLightTime], tsVect)'; + +pokeVals = histcounts([plxData.Raw.OdorTrigPokeTime], tsVect)' - histcounts([plxData.Raw.OdorPokeWithdrawTime], tsVect)'; -behVals(:,behPad+4) = histcounts([plxData.Raw.FrontRewardTime], tsVect)'; -behDataHeaders{behPad+4} = 'FrontReward'; +rwdVals = histcounts([plxData.Raw.FrontRewardTime], tsVect)'; -behVals(:,behPad+5) = histcounts([plxData.Raw.BackRewardTime], tsVect)'; -behDataHeaders{behPad+5} = 'BackReward'; +backRwdVals = histcounts([plxData.Raw.BackRewardTime], tsVect)'; -behVals(:,behPad+6) = histcounts([plxData.Raw.ErrorSignalTime], tsVect)'; -behDataHeaders{behPad+6} = 'ErrorSignal'; +errVals = histcounts([plxData.Raw.ErrorSignalTime], tsVect)'; -behVals(:,behPad+7) = histcounts([plxData.Raw.RewardSignalTime], tsVect)'; -behDataHeaders{behPad+7} = 'RewardSignal'; +rwdSigVals = histcounts([plxData.Raw.RewardSignalTime], tsVect)'; [numChans, chanNames] = plx_event_names(plxData.Summary.PLXfile); findingStrobed = 1; +aniPosVals = nan(length(tsVect)-1, 2); while findingStrobed for chan = 1:numChans curChan = chanNames(chan,:); @@ -128,17 +128,18 @@ end end aniPosHistBins = find(histcounts(aniPosition(:,1), tsVect)); - behVals(aniPosHistBins,behPad+8) = aniX'; - behVals(aniPosHistBins,behPad+9) = aniY'; + aniPosVals(aniPosHistBins,1) = aniX'; + aniPosVals(aniPosHistBins,2) = aniY'; findingStrobed = 0; end end end -behDataHeaders{behPad+8} = 'XvalRatMazePosition'; -behDataHeaders{behPad+9} = 'YvalRatMazePosition'; -behavMatrix = [tsVect(1:end-1)', behVals]; -behavMatrixColIDs = [{'TimeBin'}, behDataHeaders]; +behavMatrix = [tsVect(1:end-1)', posVals, seqVals, isVals, perfVals, lightVals, pokeVals,... + rwdVals, backRwdVals, errVals, rwdSigVals, aniPosVals]; +behavMatrixColIDs = ['TimeBin', posHeaders, seqHeaders, 'InSeqLog', 'PerformanceLog', 'PortLight', 'PokeEvents',... + 'FrontReward', 'BackReward', 'ErrorSignal', 'RewardSignal', 'XvalRatMazePosition', 'YvalRatMazePosition']; + save([plxData.Summary.PLXfile(1:end-4) '_BehaviorMatrix.mat'], 'behavMatrix', 'behavMatrixColIDs', 'plxData'); disp('Behavior data saved.'); fprintf(outfile, 'Behavior Matrix saved as %s_BehaviorMatrix.mat\n', plxData.Summary.PLXfile(1:end-4)); diff --git a/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m b/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m index 23c80f7..b32da5e 100644 --- a/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m +++ b/statMatrix Creation/Supporting Code/SummarizePLXevents_SD.m @@ -192,6 +192,13 @@ fprintf(outfile, 'Number of Buzzer activations don''t match the number of trials, check files and code for source of discrepancy\n PLX Count = %i\n MAT Count = %i\n', length(nonDoubleBuzzBuzzer)+length(sequenceBlockInitiationTimes)-sum([ssnData.Performance]==0), length(ssnData)); warning('Number of Buzzer activations don''t match the number of trials, check files and code for source of discrepancy'); end +%% Identify Port Light times +portLightChan = strcmp(channels, 'Port Light A (front)'); +lightOnTimes = plxStruct(portLightChan).ts; + +if term + lightOnTimes(lightOnTimes>terminateTime) = []; +end %% Identify Beep (Reward Signal) Times beepChanNum = strcmp(channels, 'Tone'); beepTimes = plxStruct(beepChanNum).ts; @@ -472,6 +479,7 @@ 'OdorPokeWithdrawTime', repmat({nan}, [1,length(ssnData)]),... 'OdorPokesDurations', repmat({nan}, [1,length(ssnData)]),... 'RewardSignalTime', repmat({nan}, [1,length(ssnData)]),... + 'TrialLightTime', repmat({nan}, [1,length(ssnData)]),... 'TrialAvailTime', repmat({nan}, [1,length(ssnData)]),... 'TrialEndTime', repmat({nan}, [1,length(ssnData)]),... 'ErrorSignalTime', repmat({nan}, [1,length(ssnData)]),... @@ -485,12 +493,15 @@ %% Extract Session Timestamps for trl = 1:size(odorPresSsn,1) - trl +% trl % Fill in timestamp for when the sequence started plxSession(trl).SessionBlockStartTime = sequenceBlockInitiationTimes(plxSession(trl).SessionBlockNumber); % Fill in timestamp for when the trial was available to start plxSession(trl).TrialAvailTime = trialEndTimes(find(trialEndTimesplxSession(trl).ItemPresentationTime,1,'first')); From ca3d5f04f75631a8dbc170809da2887cea7eff43 Mon Sep 17 00:00:00 2001 From: Gabriel Elias Date: Thu, 10 Aug 2023 00:36:58 -0400 Subject: [PATCH 133/133] Update CreateBehaviorMatrixPFCabbr.m --- statMatrix Creation/CreateBehaviorMatrixPFCabbr.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/statMatrix Creation/CreateBehaviorMatrixPFCabbr.m b/statMatrix Creation/CreateBehaviorMatrixPFCabbr.m index bdf8a0b..412b9b0 100644 --- a/statMatrix Creation/CreateBehaviorMatrixPFCabbr.m +++ b/statMatrix Creation/CreateBehaviorMatrixPFCabbr.m @@ -57,8 +57,8 @@ dlSeqHeaders = cell(1,seqLength); for seq = 11:seqLength+10 itemPresTimes = [plxData.Raw([plxData.Raw.SequenceItem]==seq).ItemPresentationTime]; - dlSeqVals(:,seq) = histcounts(itemPresTimes, tsVect)'; - dlSeqHeaders{seq} = ['Odor' num2str(seq)]; + dlSeqVals(:,seq-10) = histcounts(itemPresTimes, tsVect)'; + dlSeqHeaders{seq-10} = ['Odor' num2str(seq)]; fprintf(outfile, ' Odor #%i = %i trials\n', seq, length(itemPresTimes)); end seqVals = [seqVals, dlSeqVals];