-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExample_RippleDetectorMacro_public.m
156 lines (125 loc) · 6.55 KB
/
Example_RippleDetectorMacro_public.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
%the struct runData holds data about patients and where the different event
%types are stored
% ---- UPDATE this part -
% the main path for extracted data here -
data_p_path = 'C:\Users\mgeva\Documents\GitHub\rippleDetection_IEEG\example\';
% the code assumes extracted data will be found under
% runData(iPatient).DataFolder = [data_p_path,patients{iPatient},'\',expNames{iPatient},'\MACRO','CSCX.mat'];
outputFigureFolder = fullfile(data_p_path,'Figures');
% subject name
patients = {'p1'};
% session name
expNames = {'EXP1'};
% sleep-scoring vector ('1' for NREM epochs to analyze)
sleepScoreFileName = {'sleepScore_p1'};
% macroMontageFileName contains channel ids and area names per subject
% ----------------------
%channels on which detections will be performed (just an example)
channelsPerPatient = {[9 ],... % p1
};
%for bipolar ripple detection - in every row the first index is the channel in which ripple
%detection is required and the second is the reference channel
biPolarCouplesPerPatient = {[9 12],... % p1
};
bipolarDet = 0; % determines if uni-polar or bi-polar detection is used
%building the run data, not that for all file names of detections the
%methods assume the name is <provided name according to runData><channel
%num>, e.g. if runData(iPatient).SWStaresinaFileName='c:\slow_wave', then
%the slow waves file for channel 1 is 'c:\slow_wave1.mat'.
runData = [];
nPatients = length(patients);
for iPatient = 1:nPatients
runData(iPatient).patientName = patients{iPatient};
%The folder where the raw data is stored - you will need to change it
runData(iPatient).DataFolder = [data_p_path,patients{iPatient},'\',expNames{iPatient},'\MACRO'];
%The folder+filename from which spindles are going to be loaded
%The folder+filename from which spindles are going to be loaded (should be the same
%as the line above if the detections are first run and saved into SpindlesStaresinaFileNames
spindleFolder = [data_p_path,patients{iPatient},'\',expNames{iPatient},'\MACRO\spindleResults'];
runData(iPatient).SpindlesFileNames = fullfile(spindleFolder,'spindleTimes');
if isempty(dir(spindleFolder))
mkdir(spindleFolder)
end
%The folder+filename into which the bipolar ripples detections results is going
%to be stored
rippleFolder = [data_p_path,patients{iPatient},'\',expNames{iPatient},'\MACRO\rippleBipolarResults'];
runData(iPatient).RipplesBipolarFileNames = fullfile(rippleFolder,'rippleTimes');
if isempty(dir(rippleFolder))
mkdir(rippleFolder)
end
%The folder+filename from which ripples are going to be loaded (should be the same
%as the line above if the bipolar detections are first run and saved into
%RipplesBipolarFileNames)
rippleFolder = [data_p_path,patients{iPatient},'\',expNames{iPatient},'\MACRO\rippleBipolarResults'];
runData(iPatient).RipplesFileNames = fullfile(rippleFolder,'rippleTimes');
if isempty(dir(rippleFolder))
mkdir(rippleFolder)
end
%list of couples for bipolar ripple detection - where each row has the channel
%in which the detection is performed in the first index, and the
%reference channel in the second
runData(iPatient).biPolarCouples = biPolarCouplesPerPatient{iPatient};
%The folder+filename into which the spikes results is going to be stored or is
%already stored if the spikes detection was already run (the folder should
%exist)
runData(iPatient).SpikesFileNames = fullfile(runData(iPatient).DataFolder, ...
sprintf('MacroInterictalSpikeTimesFor_%s_%s_',patients{iPatient},expNames{iPatient}));
%name of the EXP data for the patient
runData(iPatient).ExpDataFileName = [data_p_path,patients{iPatient},'\',expNames{iPatient},'\',patients{iPatient},'_',expNames{iPatient},'_dataset.mat'];
%name of the sleep scoring mat file for the patient
runData(iPatient).sleepScoringFileName = [runData(iPatient).DataFolder,'\',sleepScoreFileName{iPatient},'.mat'];
%channels that the ripples analyses will be performed on
% For ripple detection
if ~bipolarDet
runData(iPatient).channelsToRunOn = biPolarCouplesPerPatient{iPatient}(:,1);
else
% For analysis
runData(iPatient).channelsToRunOn = biPolarCouplesPerPatient{iPatient};
end
%macromontage file name per patient
runData(iPatient).macroMontageFileName = [data_p_path,'MACRO_MONTAGE','\',patients{iPatient},'\',expNames{iPatient},'\MacroMontage.mat'];
%name of file with single units info
runData(iPatient).spikeData = [data_p_path,patients{iPatient},'\',expNames{iPatient},'\averagedRef\',patients{iPatient},'_spike_timestamps_post_processing.mat'];
end
%% an example for saving ripples using the wrapper AnalyzeSleepOsc.saveDetectionResults
as = AnalyzeSleepOsc;
%setting which detections to run -
whatToRun.runSpikes = false;
whatToRun.runRipples = false;
whatToRun.runRipplesBiPolar = true;
whatToRun.runSpindles = false;
whatToRun.runSpindlesStaresina = false;
whatToRun.runSWStaresina = false;
whatToRun.runSWMaingret = false;
whatToRun.HighFreqSpindles = false;
%saving detections (in this example, bipolar ripples detection)
parfor ii = 1:length(runData)
as.saveDetectionResults(runData(ii), whatToRun);
end
%plotting the single ripples and saving the figures
currData = load([runData(iPatient).DataFolder,'\CSC',num2str(currChan),'.mat']);
currData = currData.data;
currChan = channelsPerPatient{1}(1); % example
mm = matfile([runData(iPatient).RipplesBipolarFileNames,num2str(currChan),'.mat']);
rd.plotRipples(currData,mm.ripplesTimes,outputFigureFolder);
%% an example for detecting ripples directly using RippleDetector (it's the same thing the wrapper does inside)
rd = RippleDetector_class;
%an example of using the ripple detection directly and not with the wrapper
%(on the first channel of the first patient for this example)
currChan = runData(iPatient).channelsToRunOn(1);
%loading - sleep scoring, IED detection times, data
sleepScoring = load(runData(1).sleepScoringFileName);
sleepScoring = sleepScoring.sleep_score_vec;
% load or perform interictal Spikes Detection
try
peakTimes = load([runData(iPatient).SpikesFileNames,num2str(currChan),'.mat']);
peakTimes = peakTimes.peakTimes;
catch
peakTimes = [];
end
currData = load([runData(iPatient).DataFolder,'\CSC',num2str(currChan),'.mat']);
currData = currData.data;
%detecting the ripples
[ripplesTimes, ripplesStartEnd] = rd.detectRipple(currData, sleepScoring, peakTimes);
%plotting the single ripples and saving the figures
rd.plotRipples(currData,ripplesTimes,outputFigureFolder);