Skip to content

Commit a5ee8e9

Browse files
acaccacacc
authored andcommitted
fixed a few bugs related to SW-only and R-only detection options. Added new csv export files for LFP averages
1 parent 1b09b59 commit a5ee8e9

File tree

7 files changed

+196
-22
lines changed

7 files changed

+196
-22
lines changed

analyzeLFPBatch.m

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
function analyzeLFPBatch(param, dataFolder, saveFolder, expEvFolder, expDataFolder, stimFolder)
2-
%% analyzeLFPBatch(param, dataFolder, saveFolder, expEvFolder, expDataFolder, stimFolder)
1+
function analyzeLFPBatch(param, dataFolder, saveFolder, expEvFolder, expAveFolder, expDataFolder, stimFolder)
2+
%% analyzeLFPBatch(param, dataFolder, saveFolder, expEvFolder, expAveFolder, expDataFolder, stimFolder)
33
%
44
% Function to run analyzeLFPFile on batch of files
55
%
@@ -32,6 +32,7 @@ function analyzeLFPBatch(param, dataFolder, saveFolder, expEvFolder, expDataFold
3232
% param.swrType = Option to determine what qualifies as SWR (1: SW & R (default), 2: SW only, 3: R only)
3333
% param.swrWindow = +/- window around SWR peak events for swrData file [ms]
3434
% param.expSWREvOption = boolean flag to determine whether to export csv table of SWR events
35+
% param.expLFPAveOption = boolean flag to determine whether to export csv table of average statistics
3536
% param.expSWRDataOption = boolean flag to determine whether to export txt file of episodic SWR events for pClamp analysis
3637
% param.thetaOption = boolean flag to filter and analyze theta signal
3738
% param.thetaLim1 = lower theta band-pass lim (default = 4Hz)
@@ -56,12 +57,14 @@ function analyzeLFPBatch(param, dataFolder, saveFolder, expEvFolder, expDataFold
5657
% dataFolder = full path to folder containing raw data to be analysed (if not set, will prompt)
5758
% saveFolder = full path to folder of matlab files to save (if not set, will prompt)
5859
% expEvFolder = full path to folder of exported csv event table (if not set and expSWREvOption = 1, will prompt)
60+
% expAveFolder = full path to folder of exported csv table of averages (if not set and expLFPAveOption = 1, will prompt)
5961
% expDataFolder = full path to folder of exported txt data file (if not set and expSWRDataOption = 1, will prompt)
6062
% stimFolder = full path to folder of pClamp stim events (if not set and importStimOption = 1, will prompt)
6163

6264
%% Handle optional arguments
63-
if (nargin < 6) stimFolder = []; end
64-
if (nargin < 5) expDataFolder = []; end
65+
if (nargin < 7) stimFolder = []; end
66+
if (nargin < 6) expDataFolder = []; end
67+
if (nargin < 5) expAveFolder = []; end
6568
if (nargin < 4) expEvFolder = []; end
6669
if (nargin < 3) saveFolder = []; end
6770
if (nargin < 2) dataFolder = []; end
@@ -98,6 +101,7 @@ function analyzeLFPBatch(param, dataFolder, saveFolder, expEvFolder, expDataFold
98101
if ~isfield(param,'swrType') param.swrType = 1; end
99102
if ~isfield(param,'swrWindow') param.swrWindow = 100; end
100103
if ~isfield(param,'expSWREvOption') param.expSWREvOption = 1; end
104+
if ~isfield(param,'expLFPAveOption') param.expLFPAveOption = 1; end
101105
if ~isfield(param,'expSWRDataOption') param.expSWRDataOption = 1; end
102106
if ~isfield(param,'thetaOption') param.thetaOption = 0; end
103107
if ~isfield(param,'thetaLim1') param.thetaLim1 = 4; end
@@ -164,6 +168,16 @@ function analyzeLFPBatch(param, dataFolder, saveFolder, expEvFolder, expDataFold
164168
end
165169
end
166170

171+
% Select folder to export average files, if option selected
172+
if isempty(expAveFolder) && param.expLFPAveOption
173+
expAveFolder = uigetdir(parentPath, 'Select folder to export average statistics *.csv files');
174+
if (expAveFolder == 0)
175+
warning('No files to be exported - LFP average folder not selected');
176+
else
177+
[parentPath, ~, ~] = parsePath(expAveFolder);
178+
end
179+
end
180+
167181
% Select folder to export SWR event-locked episodic data files, if option selected
168182
if isempty(expDataFolder) && param.expSWRDataOption
169183
expDataFolder = uigetdir(parentPath, 'Select folder to export SWR event-locked episodic data *.txt files');
@@ -218,6 +232,7 @@ function analyzeLFPBatch(param, dataFolder, saveFolder, expEvFolder, expDataFold
218232
dataFile{nDataFiles} = [];
219233
saveFile{nDataFiles} = [];
220234
expEvFile{nDataFiles} = [];
235+
expAveFile{nDataFiles} = [];
221236
expDataFile{nDataFiles} = [];
222237
stimFile{nDataFiles} = [];
223238

@@ -240,6 +255,13 @@ function analyzeLFPBatch(param, dataFolder, saveFolder, expEvFolder, expDataFold
240255
end
241256
end
242257

258+
% Determine individual exported average *.csv file names (if selected)
259+
if ~isempty(expAveFolder) && param.expLFPAveOption
260+
if (expAveFolder ~= 0)
261+
expAveFile{i} = [expAveFolder slash dataFileName '_lfpAve.csv'];
262+
end
263+
end
264+
243265
% Determine individual exported SWR event-locked episodic data *.txt file names (if selected)
244266
if ~isempty(expDataFolder) && param.expSWRDataOption
245267
if (expDataFolder ~= 0)
@@ -257,9 +279,9 @@ function analyzeLFPBatch(param, dataFolder, saveFolder, expEvFolder, expDataFold
257279
parfor i = 1:nDataFiles
258280
if reAnalyzeOption
259281
data = load(dataFile{i});
260-
analyzeLFPFile(data, [], param, dataFile{i}, saveFile{i}, expEvFile{i}, expDataFile{i}, stimFile{i});
282+
analyzeLFPFile(data, [], param, dataFile{i}, saveFile{i}, expEvFile{i}, expAveFile{i}, expDataFile{i}, stimFile{i});
261283
else
262-
analyzeLFPFile([], [], param, dataFile{i}, saveFile{i}, expEvFile{i}, expDataFile{i}, stimFile{i});
284+
analyzeLFPFile([], [], param, dataFile{i}, saveFile{i}, expEvFile{i}, expAveFile{i}, expDataFile{i}, stimFile{i});
263285
end
264286
end
265287
fprintf('complete\n');

analyzeLFPFile.m

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function [data, hand] = analyzeLFPFile(data, hand, param, dataFile, saveFile, expEvFile, expDataFile, stimFile)
1+
function [data, hand] = analyzeLFPFile(data, hand, param, dataFile, saveFile, expEvFile, expAveFile, expDataFile, stimFile)
22
%% [data, hand] = analyzeLFPFile(data, hand, param, dataFile, saveFile, expEvFile, expDataFile, stimFile)
33
%
44
% Function to detect sharp wave ripple (SWR) events, theta, beta, and gamma analysis, time-frequency
@@ -40,6 +40,7 @@
4040
% param.swrType = Option to determine what qualifies as SWR (1: SW & R (default), 2: SW only, 3: R only)
4141
% param.swrWindow = +/- window around SWR peak events for swrData file [ms]
4242
% param.expSWREvOption = boolean flag to determine whether to export csv table of SWR events
43+
% param.expLFPAveOption = boolean flag to determine whether to export csv table of average statistics
4344
% param.expSWRDataOption = boolean flag to determine whether to export txt file of episodic SWR events for pClamp analysis
4445
% param.thetaOption = boolean flag to filter and analyze theta signal
4546
% param.thetaLim1 = lower theta band-pass lim (default = 4Hz)
@@ -64,6 +65,7 @@
6465
% dataFile = full path to file/folder containing data to be analysed (if not set, will prompt)
6566
% saveFile = full path to matlab file to save (if not set, will prompt)
6667
% expEvFile = full path to exported csv event table (if not set and expSWREvOption = 1, will prompt
68+
% expAveFile = full path to exported csv average table (if not set and expLFPAveOption = 1, will prompt
6769
% expDataFile = full path to exported txt data file (if not set and expSWRDataOption = 1, will prompt
6870
% stimFile = full path to pClamp stim event file (if not set and importStimOption = 1, will prompt
6971
%
@@ -72,8 +74,9 @@
7274
% hand = handle structure for figure
7375

7476
%% Handle input arguments - if not entered
75-
if (nargin < 8) stimFile = []; end
76-
if (nargin < 7) expDataFile = []; end
77+
if (nargin < 9) stimFile = []; end
78+
if (nargin < 8) expDataFile = []; end
79+
if (nargin < 7) expAveFile = []; end
7780
if (nargin < 6) expEvFile = []; end
7881
if (nargin < 5) saveFile = []; end
7982
if (nargin < 4) dataFile = []; end
@@ -115,6 +118,7 @@
115118
if ~isfield(param,'swrType') param.swrType = 1; end
116119
if ~isfield(param,'swrWindow') param.swrWindow = 100; end
117120
if ~isfield(param,'expSWREvOption') param.expSWREvOption = 1; end
121+
if ~isfield(param,'expLFPAveOption') param.expLFPAveOption = 1; end
118122
if ~isfield(param,'expSWRDataOption') param.expSWRDataOption = 1; end
119123
if ~isfield(param,'thetaOption') param.thetaOption = 0; end
120124
if ~isfield(param,'thetaLim1') param.thetaLim1 = 4; end
@@ -185,6 +189,18 @@
185189
end
186190
end
187191

192+
% Select export LFP average file, if option selected
193+
if isempty(expAveFile) && param.expLFPAveOption
194+
defaultPath = [parentPath dataFileName '_lfpAve.csv'];
195+
[exportName, exportPath] = uiputfile('.csv','Select *.csv file to export table of LFP averages', defaultPath);
196+
expAveFile = [exportPath exportName];
197+
if ~all(expAveFile)
198+
warning('No LFP averages to be exported - no file selected');
199+
else
200+
[parentPath, ~, ~] = parsePath(expAveFile);
201+
end
202+
end
203+
188204
% Select export SWR data file, if option selected
189205
if isempty(expDataFile) && param.expSWRDataOption
190206
defaultPath = [parentPath dataFileName '_swrData.txt'];
@@ -871,6 +887,14 @@
871887
fprintf('done\n');
872888
end
873889

890+
%% Export LFP average table
891+
if all(expAveFile) && param.expLFPAveOption
892+
fprintf(['exporting LFP averages (file ' dataFileName ')... ']);
893+
exportLFPAverages(data, saveFile, expAveFile);
894+
data.LFP.expAveFile = expAveFile;
895+
fprintf('done\n');
896+
end
897+
874898
%% Export SWR event-locked episodic data files
875899
if all(expDataFile) && param.expSWRDataOption
876900
fprintf(['exporting SWR event-locked data (file ' dataFileName ')... ']);

exportLFPAverages.m

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
function exportLFPAverages(data, saveFile, exportFile)
2+
%% exportLFPAverages(data, saveFile, exportFile)
3+
%
4+
% Function to export csv file of averages of all LFP data available
5+
6+
% Handle input arguments - if not entered
7+
if (nargin < 3) exportFile = []; end
8+
if (nargin < 2) saveFile = []; end
9+
if (nargin < 1) data = []; end
10+
11+
if isempty(data) || isempty(saveFile)
12+
error('Enter sufficient inputs to use function exportSpkEvents');
13+
end
14+
15+
if isempty(exportFile)
16+
[parentPath, saveFileName, ~] = parsePath(saveFile);
17+
defaultName = [parentPath saveFileName '_lfpAve.csv'];
18+
[exportName, exportPath] = uiputfile('.csv','Select *.csv file to export averages', defaultName);
19+
exportFile = [exportPath exportName];
20+
if ~all(exportFile) error('Please select valid file'); end
21+
end
22+
23+
% LFP SWR event data
24+
varNames = {'nSWRs', 'SWR_frequency_Hz', 'SWR_duration_ms', 'SWR_IEI_s', 'SWR_amplitude_uV'};
25+
outTable = table(length(data.SWR.amp), data.SWR.frequency, mean(data.SWR.duration,'omitnan'), mean(data.SWR.IEI,'omitnan'), 10^3 * mean(data.SWR.amp,'omitnan'), 'VariableNames', varNames);
26+
if isfield(data.SWR, 'area') outTable = [outTable table(mean(data.SWR.area,'omitnan'), 'VariableNames', {'SWR_area_uVs'})]; end
27+
if isfield(data.SWR, 'power') outTable = [outTable table(10^6 * mean(data.SWR.power,'omitnan'), 'VariableNames', {'SWR_power_uV2'})]; end
28+
29+
% LFP Sharp Wave
30+
if isfield(data, 'SW')
31+
if isfield(data.SW, 'tPower') outTable = [outTable table(10^6 * data.SW.tPower, 'VariableNames', {'SW_Total_Power_uV2'})]; end
32+
if isfield(data.SW, 'SWR')
33+
if isfield(data.SW.SWR, 'power') outTable = [outTable table(10^6 * mean(data.SW.SWR.power,'omitnan'), 'VariableNames', {'SW_SWR_Power_uV2'})]; end
34+
end
35+
end
36+
37+
% LFP Theta
38+
if isfield(data, 'theta')
39+
if isfield(data.theta, 'tPower') outTable = [outTable table(10^6 * data.theta.tPower, 'VariableNames', {'Theta_Total_Power_uV2'})]; end
40+
end
41+
42+
% LFP Theta
43+
if isfield(data, 'beta')
44+
if isfield(data.beta, 'tPower') outTable = [outTable table(10^6 * data.beta.tPower, 'VariableNames', {'Beta_Total_Power_uV2'})]; end
45+
end
46+
47+
% LFP Gamma
48+
if isfield(data, 'gamma')
49+
if isfield(data.gamma, 'tPower') outTable = [outTable table(10^6 * data.gamma.tPower, 'VariableNames', {'Gamma_Total_Power_uV2'})]; end
50+
if isfield(data.gamma, 'SWR')
51+
if isfield(data.gamma.SWR, 'power') outTable = [outTable table(10^6 * mean(data.gamma.SWR.power,'omitnan'), 'VariableNames', {'Gamma_SWR_Power_uV2'})]; end
52+
if isfield(data.gamma.SWR, 'FFT')
53+
if isfield(data.gamma.SWR.FFT, 'pkFreq') outTable = [outTable table(mean(data.gamma.SWR.FFT.pkFreq,'omitnan'), 'VariableNames', {'Gamma_SWR_pkFreq_Hz'})]; end
54+
end
55+
if isfield(data.gamma.SWR, 'phase')
56+
if isfield(data.gamma.SWR.phase, 'nCycle') outTable = [outTable table(mean(data.gamma.SWR.phase.nCycle,'omitnan'), 'VariableNames', {'Gamma_SWR_nCycle'})]; end
57+
if isfield(data.gamma.SWR.phase, 'phFreq') outTable = [outTable table(mean(data.gamma.SWR.phase.phFreq,'omitnan'), 'VariableNames', {'Gamma_SWR_phFreq_Hz'})]; end
58+
end
59+
end
60+
end
61+
62+
% LFP High Gamma
63+
if isfield(data, 'hgamma')
64+
if isfield(data.hgamma, 'tPower') outTable = [outTable table(10^6 * data.hgamma.tPower, 'VariableNames', {'HighGamma_Total_Power_uV2'})]; end
65+
end
66+
67+
% LFP Ripple
68+
if isfield(data, 'R')
69+
if isfield(data.R, 'tPower') outTable = [outTable table(10^6 * data.R.tPower, 'VariableNames', {'Ripple_Total_Power_uV2'})]; end
70+
if isfield(data.R, 'SWR')
71+
if isfield(data.R.SWR, 'power') outTable = [outTable table(10^6 * mean(data.R.SWR.power,'omitnan'), 'VariableNames', {'Ripple_SWR_Power_uV2'})]; end
72+
if isfield(data.R.SWR, 'FFT')
73+
if isfield(data.R.SWR.FFT, 'pkFreq') outTable = [outTable table(mean(data.R.SWR.FFT.pkFreq,'omitnan'), 'VariableNames', {'Ripple_SWR_pkFreq_Hz'})]; end
74+
end
75+
if isfield(data.R.SWR, 'phase')
76+
if isfield(data.R.SWR.phase, 'nCycle') outTable = [outTable table(mean(data.R.SWR.phase.nCycle,'omitnan'), 'VariableNames', {'Ripple_SWR_nCycle'})]; end
77+
if isfield(data.R.SWR.phase, 'phFreq') outTable = [outTable table(mean(data.R.SWR.phase.phFreq,'omitnan'), 'VariableNames', {'Ripple_SWR_phFreq_Hz'})]; end
78+
end
79+
end
80+
end
81+
82+
% LFP Fast Ripple
83+
if isfield(data, 'fR')
84+
if isfield(data.fR, 'tPower') outTable = [outTable table(10^6 * data.fR.tPower, 'VariableNames', {'fastRipple_Total_Power_uV2'})]; end
85+
if isfield(data.fR, 'SWR')
86+
if isfield(data.fR.SWR, 'power') outTable = [outTable table(10^6 * mean(data.fR.SWR.power,'omitnan'), 'VariableNames', {'fastRipple_SWR_Power_uV2'})]; end
87+
if isfield(data.fR.SWR, 'FFT')
88+
if isfield(data.fR.SWR.FFT, 'pkFreq') outTable = [outTable table(mean(data.fR.SWR.FFT.pkFreq,'omitnan'), 'VariableNames', {'fastRipple_SWR_pkFreq_Hz'})]; end
89+
end
90+
if isfield(data.fR.SWR, 'phase')
91+
if isfield(data.fR.SWR.phase, 'nCycle') outTable = [outTable table(mean(data.fR.SWR.phase.nCycle,'omitnan'), 'VariableNames', {'fastRipple_SWR_nCycle'})]; end
92+
if isfield(data.fR.SWR.phase, 'phFreq') outTable = [outTable table(mean(data.fR.SWR.phase.phFreq,'omitnan'), 'VariableNames', {'fastRipple_SWR_phFreq_Hz'})]; end
93+
end
94+
end
95+
end
96+
97+
% Replace NaN values with blanks
98+
tmp = table2cell(outTable);
99+
tmp(isnan(outTable.Variables)) = {[]};
100+
outTable = array2table(tmp,'VariableNames',outTable.Properties.VariableNames);
101+
102+
writetable(outTable, exportFile, 'Delimiter', ',');
103+
104+
end

exportSWRData.m

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function exportSWRData(data, param, expDataFile)
88
if (nargin < 2) param = struct; end
99
if (nargin < 1) error('Supply data structure to use exportSWRData'); end
1010

11-
% param = []; % comment out normally, quick fix if want to use defaults below instead of GUI params.
11+
param = []; % comment out normally, quick fix if want to use defaults below instead of GUI params.
1212

1313
% Select export file if not supplied
1414
if isempty(expDataFile)
@@ -22,11 +22,12 @@ function exportSWRData(data, param, expDataFile)
2222
% Set default parameters
2323
if ~isfield(param,'gammaOption') param.gammaOption = 1; end
2424
if ~isfield(param,'rOption') param.rOption = 1; end
25-
if ~isfield(param,'cellOption') param.cellOption = 1; end
25+
if ~isfield(param,'fROption') param.fROption = 1; end
26+
if ~isfield(param,'cellOption') param.cellOption = 0; end
2627
if ~isfield(param,'cellRawOption') param.cellRawOption = 0; end
27-
if ~isfield(param,'cellGammaOption') param.cellGammaOption = 1; end
28-
if ~isfield(param,'cellRippleOption') param.cellRippleOption = 1; end
29-
if ~isfield(param,'truncateEvs') param.truncateEvs = 0; end
28+
if ~isfield(param,'cellGammaOption') param.cellGammaOption = 0; end
29+
if ~isfield(param,'cellRippleOption') param.cellRippleOption = 0; end
30+
if ~isfield(param,'truncateEvs') param.truncateEvs = 1; end
3031
if ~isfield(param,'maxNumEvs') param.maxNumEvs = 50; end
3132
if ~isfield(param,'swrWindow') param.swrWindow = 100; end
3233

@@ -45,6 +46,7 @@ function exportSWRData(data, param, expDataFile)
4546
data.SWR.event(1) = [];
4647
if param.gammaOption data.gamma.SWR.event(1) = []; end
4748
if param.rOption data.R.SWR.event(1) = []; end
49+
if param.fROption data.fR.SWR.event(1) = []; end
4850
if param.cellOption
4951
if param.cellRawOption data.C.SWR.event(1) = []; end
5052
data.C.SWR.evNorm(1) = [];
@@ -59,6 +61,7 @@ function exportSWRData(data, param, expDataFile)
5961
data.SWR.event(nEvs) = [];
6062
if param.gammaOption data.gamma.SWR.event(nEvs) = []; end
6163
if param.rOption data.R.SWR.event(nEvs) = []; end
64+
if param.fROption data.fR.SWR.event(nEvs) = []; end
6265
if param.cellOption
6366
if param.cellRawOption data.C.SWR.event(nEvs) = []; end
6467
data.C.SWR.evNorm(nEvs) = [];
@@ -99,6 +102,13 @@ function exportSWRData(data, param, expDataFile)
99102
tableVarNames{nameInd} = ['R_' num2str(i)];
100103
nameInd = nameInd + 1;
101104
end
105+
106+
% Fast Ripple event-locked data:
107+
if param.fROption
108+
dataOut = horzcat(dataOut, data.fR.SWR.event{i});
109+
tableVarNames{nameInd} = ['fR_' num2str(i)];
110+
nameInd = nameInd + 1;
111+
end
102112

103113
if param.cellOption
104114

0 commit comments

Comments
 (0)