From a427ea5d105ef12c907b81e263e9587172a5f0ea Mon Sep 17 00:00:00 2001 From: Roberto <37729096+RobertoDF@users.noreply.github.com> Date: Mon, 5 Dec 2022 13:32:29 +0100 Subject: [PATCH 1/4] Add s1 and s2 variables s1 and s2 variables missing, definition taken from https://github.com/MouseLand/stringer-pachitariu-et-al-2018a/blob/8dce0d516bb8a4b16e245b897403d76cd1d25ae7/multiDactivity/SVCA.m#L24 --- multiDactivity/peerPC_cov.m | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/multiDactivity/peerPC_cov.m b/multiDactivity/peerPC_cov.m index 282e34f..cadc128 100644 --- a/multiDactivity/peerPC_cov.m +++ b/multiDactivity/peerPC_cov.m @@ -72,6 +72,10 @@ function peerPC_cov(dataroot,matroot,useGPU,dex) semilogx(sneur./varneur) [u2,~,~] = svdecon(Ff(ntrain,itrain)); + + s1 = u' * Ff(ntrain,itest); + s2 = v' * Ff(ntest,itest); + cpc = corr(gather_try(s1(1,:)'), gather_try(Ff(ntrain,itest)' * u2(:,1))); disp(cpc); corrPC1(d) = cpc; @@ -88,4 +92,4 @@ function peerPC_cov(dataroot,matroot,useGPU,dex) %% -save(fullfile(matroot,'PCpredict.mat'),'cov_neur','var_neur','exampleV1','exampleV2', 'corrPC1'); \ No newline at end of file +save(fullfile(matroot,'PCpredict.mat'),'cov_neur','var_neur','exampleV1','exampleV2', 'corrPC1'); From d65afb0b09d5824dfe9167d3e26342379a2b5317 Mon Sep 17 00:00:00 2001 From: Roberto <37729096+RobertoDF@users.noreply.github.com> Date: Mon, 5 Dec 2022 14:10:40 +0100 Subject: [PATCH 2/4] Adapt diretories to figshare folder organization --- ephys/alignFaces30Hz.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ephys/alignFaces30Hz.m b/ephys/alignFaces30Hz.m index 429a2f7..dfbd124 100644 --- a/ephys/alignFaces30Hz.m +++ b/ephys/alignFaces30Hz.m @@ -21,11 +21,11 @@ function alignFaces30Hz(ephysroot, matroot) % load files - beh = load(fullfile(ephysroot, sprintf('%s_face_proc.mat',mouse_name))); + beh = load(fullfile(ephysroot, "faces", sprintf('%s_face_proc.mat',mouse_name))); motSVD = beh.motionSVD; tVid = beh.times; % times of movie frames in spike reference frame - load(fullfile(ephysroot, sprintf('spks%s_Feb18.mat',mouse_name))); + load(fullfile(ephysroot, "spks", sprintf('spks%s_Feb18.mat',mouse_name))); %% extract spikes stall = zeros(5e3,5500,'uint8'); @@ -93,4 +93,4 @@ function alignFaces30Hz(ephysroot, matroot) save(fullfile(matroot, sprintf('%swithFaces_KS2.mat',mouse_name)), 'stall','Wh','iprobe',... 'motSVD','tspont','tVid','srate','brainLoc','areaLabels'); -end \ No newline at end of file +end From 831557d9eebb23549ce9c662475844fc3a974dde Mon Sep 17 00:00:00 2001 From: Roberto <37729096+RobertoDF@users.noreply.github.com> Date: Mon, 12 Dec 2022 18:52:57 +0100 Subject: [PATCH 3/4] addd missing ndims0 --- behavior/predictNeuronsFromFacePositions.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/behavior/predictNeuronsFromFacePositions.m b/behavior/predictNeuronsFromFacePositions.m index 265a664..fd2bd86 100644 --- a/behavior/predictNeuronsFromFacePositions.m +++ b/behavior/predictNeuronsFromFacePositions.m @@ -55,6 +55,8 @@ function predictNeuronsFromFacePositions(dataroot,matroot) x = reshape(x, [], np); x = x'; + ndims0 = [1 2 3 4 8 16 32 64 128]; + ndims1 = ndims0(ndims0<=size(x,1) & ndims0<=size(v,1)); %% low rank regression From 1ad154d1777572a36e0257a318cbfe0f243cb533 Mon Sep 17 00:00:00 2001 From: RobertoDF Date: Fri, 22 Mar 2024 21:19:58 +0100 Subject: [PATCH 4/4] test prediction across animals --- behavior/predictNeuronsFromAllBeh.m | 53 ++++++++++++++++++++++----- behavior/predictPCsFromAllBeh.m | 57 ++++++++++++++++++++++------- ephys/masterAnalysis.m | 2 +- mainfigs/fig2new.m | 6 +-- make_figs.m | 2 +- multiDactivity/peerPC_cov.m | 2 +- process_data.m | 6 +-- 7 files changed, 95 insertions(+), 33 deletions(-) diff --git a/behavior/predictNeuronsFromAllBeh.m b/behavior/predictNeuronsFromAllBeh.m index 57f12f6..0dcfbdd 100644 --- a/behavior/predictNeuronsFromAllBeh.m +++ b/behavior/predictNeuronsFromAllBeh.m @@ -23,6 +23,39 @@ %% dat = load(fullfile(dataroot,sprintf('spont_%s_%s.mat',dall.db(d).mouse_name,dall.db(d).date))); + % Check if d+1 exists + if d+1 <= length(dall.db) + % If d+1 exists, load data for d+1 + dat_2 = load(fullfile(dataroot, sprintf('spont_%s_%s.mat', dall.db(d+1).mouse_name, dall.db(d+1).date))); + else + % If d+1 does not exist, consider loading data for d-1 if d is not the first index + if d > 1 + dat_2 = load(fullfile(dataroot, sprintf('spont_%s_%s.mat', dall.db(d-1).mouse_name, dall.db(d-1).date))); + end + end + + % Determine the minimum number of rows across the relevant fields + minRows = min([size(dat.Fsp, 1), size(dat_2.Fsp, 1)]); + + % Determine the minimum number of columns for Fsp + minColsFsp = min(size(dat.Fsp, 2), size(dat_2.Fsp, 2)); + + % Adjust the Fsp fields in both structures + dat.Fsp = dat.Fsp(1:minRows, 1:minColsFsp); + dat_2.Fsp = dat_2.Fsp(1:minRows, 1:minColsFsp); + + % Since med and stat have the same row dimension as Fsp, adjust their rows to match + % (Assuming med does not need column adjustment as it is ?x3, and stat is likely a 1D array) + + dat.med = dat.med(1:minRows, :); + dat_2.med = dat_2.med(1:minRows, :); + + % Assuming 'stat' is an array of structures and needs to be resized based on rows + dat.stat = dat.stat(1:minRows); + dat_2.stat = dat_2.stat(1:minRows); + + % Now, dat and dat_2 have their 'Fsp', 'med', and 'stat' fields adjusted to match in size + if isfield(dat.stat,'redcell') Ff = dat.Fsp(~logical([dat.stat(:).redcell]), :); med = dat.med(~logical([dat.stat(:).redcell]),:); @@ -70,27 +103,27 @@ switch btype %wmot = wmot * sign case 1 - x = zscore([dat.beh.runSpeed(:)],1,1); + x = zscore([dat_2.beh.runSpeed(:)],1,1); case 2 - x = zscore([dat.beh.pupil.area(:)],1,1); + x = zscore([dat_2.beh.pupil.area(:)],1,1); case 3 - x = zscore([dat.beh.whisker.motionSVD(:,1)],1,1); + x = zscore([dat_2.beh.whisker.motionSVD(:,1)],1,1); case 4 - x = zscore([dat.beh.runSpeed(:) dat.beh.pupil.area(:)],1,1); + x = zscore([dat_2.beh.runSpeed(:) dat.beh.pupil.area(:)],1,1); case 5 - x = zscore([dat.beh.runSpeed(:) wmot],1,1); + x = zscore([dat_2.beh.runSpeed(:) wmot],1,1); case 6 - x = zscore([dat.beh.pupil.area(:) wmot],1,1); + x = zscore([dat_2.beh.pupil.area(:) wmot],1,1); case 7 - x = zscore([dat.beh.runSpeed(:) dat.beh.pupil.area(:) wmot],1,1); + x = zscore([dat_2.beh.runSpeed(:) dat.beh.pupil.area(:) wmot],1,1); case 8 - x = zscore(dat.beh.face.motionSVD(:,1),1,1); + x = zscore(dat_2.beh.face.motionSVD(:,1),1,1); case 9 - x = dat.beh.face.motionSVD; + x = dat_2.beh.face.motionSVD; x = x - mean(x,1); x = x / std(x(:,1)); case 10 - x = dat.beh.face.motionSVD; + x = dat_2.beh.face.motionSVD; x = x - mean(x,1); x = x / std(x(:,1)); x = [x zscore([dat.beh.runSpeed(:) dat.beh.pupil.area(:) wmot],1,1)]; diff --git a/behavior/predictPCsFromAllBeh.m b/behavior/predictPCsFromAllBeh.m index 4528edb..5f0a313 100644 --- a/behavior/predictPCsFromAllBeh.m +++ b/behavior/predictPCsFromAllBeh.m @@ -15,6 +15,35 @@ %% dat = load(fullfile(dataroot,sprintf('spont_%s_%s.mat',dall.db(d).mouse_name,dall.db(d).date))); + % Check if d+1 exists + if d+1 <= length(dall.db) + % If d+1 exists, load data for d+1 + dat_2 = load(fullfile(dataroot, sprintf('spont_%s_%s.mat', dall.db(d+1).mouse_name, dall.db(d+1).date))); + else + % If d+1 does not exist, consider loading data for d-1 if d is not the first index + if d > 1 + dat_2 = load(fullfile(dataroot, sprintf('spont_%s_%s.mat', dall.db(d-1).mouse_name, dall.db(d-1).date))); + end + end + + % Determine the minimum number of columns for Fsp + minColsFsp = min(size(dat.Fsp, 2), size(dat_2.Fsp, 2)); + + % Adjust the Fsp fields in both structures + dat.Fsp = dat.Fsp(:, 1:minColsFsp); + dat_2.Fsp = dat_2.Fsp(:, 1:minColsFsp); + + % Since med and stat have the same row dimension as Fsp, adjust their rows to match + % (Assuming med does not need column adjustment as it is ?x3, and stat is likely a 1D array) + dat.beh.runSpeed = dat.beh.runSpeed(1:minColsFsp) + dat_2.beh.runSpeed = dat_2.beh.runSpeed(1:minColsFsp) + dat_2.beh.pupil.area =dat_2.beh.pupil.area(1:minColsFsp) + dat.beh.pupil.area =dat.beh.pupil.area(1:minColsFsp) + dat_2.beh.whisker.motionSVD = dat_2.beh.whisker.motionSVD(1:minColsFsp,:) + dat.beh.whisker.motionSVD = dat.beh.whisker.motionSVD(1:minColsFsp,:) + dat_2.beh.face.motionSVD = dat_2.beh.face.motionSVD(1:minColsFsp,:) + dat.beh.face.motionSVD = dat.beh.face.motionSVD(1:minColsFsp,:) + if isfield(dat.stat,'redcell') Ff = dat.Fsp(~logical([dat.stat(:).redcell]), :); med = dat.med(~logical([dat.stat(:).redcell]),:); @@ -75,37 +104,37 @@ %% loop over behavioral predictors for btype = [1:10] - wmot = dat.beh.whisker.motionSVD(:,1); - wmot = wmot * sign(mean(mean(dat.beh.whisker.motionMask(:,:,1)))); + wmot = dat_2.beh.whisker.motionSVD(:,1); + wmot = wmot * sign(mean(mean(dat_2.beh.whisker.motionMask(:,:,1)))); switch btype %wmot = wmot * sign case 1 - x = zscore([dat.beh.runSpeed(:)],1,1); + x = zscore([dat_2.beh.runSpeed(:)],1,1); case 2 - x = zscore([dat.beh.pupil.area(:)],1,1); + x = zscore([dat_2.beh.pupil.area(:)],1,1); case 3 - x = zscore([dat.beh.whisker.motionSVD(:,1)],1,1); + x = zscore([dat_2.beh.whisker.motionSVD(:,1)],1,1); case 4 - x = zscore([dat.beh.runSpeed(:) dat.beh.pupil.area(:)],1,1); + x = zscore([dat_2.beh.runSpeed(:) dat_2.beh.pupil.area(:)],1,1); case 5 - x = zscore([dat.beh.runSpeed(:) wmot],1,1); + x = zscore([dat_2.beh.runSpeed(:) wmot],1,1); case 6 - x = zscore([dat.beh.pupil.area(:) wmot],1,1); + x = zscore([dat_2.beh.pupil.area(:) wmot],1,1); case 7 - x = zscore([dat.beh.runSpeed(:) dat.beh.pupil.area(:) wmot],1,1); + x = zscore([dat_2.beh.runSpeed(:) dat_2.beh.pupil.area(:) wmot],1,1); case 8 - x = zscore(dat.beh.face.motionSVD(:,1),1,1); + x = zscore(dat_2.beh.face.motionSVD(:,1),1,1); case 9 - x = dat.beh.face.motionSVD; + x = dat_2.beh.face.motionSVD; x = x - mean(x,1); x = x / std(x(:,1)); case 10 - x = dat.beh.face.motionSVD; + x = dat_2.beh.face.motionSVD; x = x - mean(x,1); x = x / std(x(:,1)); - x = [x zscore([dat.beh.runSpeed(:) dat.beh.pupil.area(:) wmot],1,1)]; + x = [x zscore([dat_2.beh.runSpeed(:) dat_2.beh.pupil.area(:) wmot],1,1)]; end - + x = x * 10; x = x(1:end-(tdelay),:); % apply time delay x = bin2d(x, tbin, 1); diff --git a/ephys/masterAnalysis.m b/ephys/masterAnalysis.m index d307d3f..16fb140 100644 --- a/ephys/masterAnalysis.m +++ b/ephys/masterAnalysis.m @@ -1,4 +1,4 @@ -% runs all the analyses for the EPHYS recordings! +1% runs all the analyses for the EPHYS recordings! function masterAnalysis(matroot) mstr = {'Krebs','Waksman','Robbins'}; diff --git a/mainfigs/fig2new.m b/mainfigs/fig2new.m index a227b41..d2eac84 100644 --- a/mainfigs/fig2new.m +++ b/mainfigs/fig2new.m @@ -195,11 +195,11 @@ function fig2newnew(matroot) hs{i}.Position(2) = hs{i}.Position(2)+.06; hold all; shadedErrorBar([1:1024],100*mean((cov_neur-squeeze(cov_res_beh(:,6,:,9)))./var_neur,2),... - std(100*(cov_neur-squeeze(cov_res_beh(:,6,:,9)))./var_neur,1,2)/sqrt(ndat-1),{'color','b','linewidth',1}); + std(100*(cov_neur-squeeze(cov_res_beh(:,6,:,9)))./var_neur,1,2)/sqrt(ndat-1),'lineProps',{'color','b','linewidth',1}); shadedErrorBar([1:1024],100*mean(cov_neur./var_neur,2),... - std(100*cov_neur./var_neur,1,2)/sqrt(ndat-1),{'color',.8*[1 1 1],'linewidth',1}); + std(100*cov_neur./var_neur,1,2)/sqrt(ndat-1),'lineProps',{'color',.8*[1 1 1],'linewidth',1}); shadedErrorBar([1:1024],100*mean((cov_neur-squeeze(cov_res_beh(:,3,:,7)))./var_neur,2),... - std(100*(cov_neur-squeeze(cov_res_beh(:,3,:,7)))./var_neur,1,2)/sqrt(ndat-1),{'color',[0 0.2 0],'linewidth',1}); + std(100*(cov_neur-squeeze(cov_res_beh(:,3,:,7)))./var_neur,1,2)/sqrt(ndat-1),'lineProps',{'color',[0 0.2 0],'linewidth',1}); %axis([0 .06 0 .06]); set(gca,'xtick',10.^[0 1 2]) text(.55,.9,{'max explainable'},'color',.6*[1 1 1],'fontsize',6) diff --git a/make_figs.m b/make_figs.m index 36fb2ce..f95cd70 100644 --- a/make_figs.m +++ b/make_figs.m @@ -1,7 +1,7 @@ % where data is stored (that you download from figshare) dataroot = '/media/carsen/DATA2/grive/10krecordings/spont_paper/'; % where processed data and results are saved -matroot = '/media/carsen/DATA2/grive/10krecordings/spontResults/'; +matroot = '/alzheimer/Roberto/Data_papers/spontResults/'; %% this will produce the figures diff --git a/multiDactivity/peerPC_cov.m b/multiDactivity/peerPC_cov.m index cadc128..4ab5ff7 100644 --- a/multiDactivity/peerPC_cov.m +++ b/multiDactivity/peerPC_cov.m @@ -59,7 +59,7 @@ function peerPC_cov(dataroot,matroot,useGPU,dex) %Ff = randn(size(Ff)); Lblock = 60; fractrain = 0.5; - [itrain, itest] = splitInterleaved(NT, Lblock, fractrain, 1); + [itrain, itest] = splitInterleaved(NT, Lbleock, fractrain, 1); tic; if useGPU Ff = gpuArray(single(Ff)); diff --git a/process_data.m b/process_data.m index 749e546..249567e 100644 --- a/process_data.m +++ b/process_data.m @@ -2,13 +2,13 @@ % you should change this to your local data paths % where you downloaded the 2P data -dataroot = '/media/carsen/DATA2/grive/10krecordings/spontData'; +dataroot = '/alzheimer/Roberto/Data_papers/6163622/'; % where ephys data is ephysroot = '/home/carsen/dm11/data/Spikes/eightprobes/'; % give a local folder for saving intermediate data (around 4-5GB) -matroot = '/media/carsen/DATA2/grive/10krecordings/spontResults'; +matroot = '/alzheimer/Roberto/Data_papers/spontResults/'; mkdir(matroot) @@ -16,7 +16,7 @@ useGPU = 1; % should be in github folder -addpath(genpath('.')); +%addpath(genpath('.')); % also download rastermap % https://github.com/MouseLand/rastermap/