Skip to content

Commit 93df6b7

Browse files
committed
Using sleep portion of rrgen
1 parent d0666bd commit 93df6b7

File tree

4 files changed

+78
-34
lines changed

4 files changed

+78
-34
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
artificialDataAnalysis/rrGenerator/.DS_Store
33
.DS_Store
44
*.c~
5+
*.m~

artificialDataAnalysis/pipelineArtificialData.m

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function pipelineArtificialData(pathToRepo)
1+
%function pipelineArtificialData(pathToRepo)
22
%
33
% Overview:
44
% Loads artificially generated data and applies change point
@@ -31,8 +31,8 @@ function pipelineArtificialData(pathToRepo)
3131
settings.tolerance = 5; % tolerance in seconds for determining if change point is detected;
3232
% if estimated change point (ecp) is within true change point (tcp),
3333
% the change point is detected.
34-
settings.act = 1; % 1: work with actigraphy data
35-
settings.figs = 0; % 1: plot tcp & ecp on time series
34+
settings.act = 0; % 1: work with actigraphy data
35+
settings.figs = 1; % 1: plot tcp & ecp on time series
3636
settings.iterations = 1000;
3737

3838
% Choose parameter set according to each method
@@ -55,16 +55,34 @@ function pipelineArtificialData(pathToRepo)
5555

5656
% Create random seed
5757
seed = rand(1)*1e3;
58+
59+
% Generate artificial data
60+
if settings.act ~= 1
61+
% 24 hour RR time series
62+
vector_length = 86400;
5863

59-
% Generate a random vector length between 1e3 and 9e3
60-
vector_length = 1000;
61-
62-
% Create RR time series with 'rrgen_sys' exe (compiled from c file)
63-
[data, tcp] = rrgenV3_wrapper(seed, vector_length, 0, 0, pathToRepo);
64+
% Create RR time series with 'rrgen_sys' exe (compiled from c file)
65+
[data, tcp, sleepStart, sleepEnd] = rrgenV3_wrapper(seed, vector_length, 0, 0, pathToRepo);
66+
67+
% Calculate sleep length
68+
sleepLength = (sleepEnd - sleepStart) / 3600;
69+
70+
hold off;
71+
plot(data); hold on; plot(sleepStart,data(sleepStart),'ro'); hold on; plot(sleepEnd,data(sleepEnd),'ro');
6472

65-
% Can also specify probability of ectopy and noise: rrgen_2003(seed, vector_length, prob_ectopy, prob_noise);
66-
67-
time = cumsum(data); % Time of RR time series is found by cumulative summation
73+
% Extract sleep portion of data
74+
data = data(sleepStart:sleepEnd);
75+
76+
% Adjust changepoints for sleep portion
77+
tcp_idx = find((tcp > sleepStart) & (tcp < sleepEnd));
78+
tcp = tcp(tcp_idx) - sleepStart;
79+
80+
time = cumsum(data); % Time of RR time series is found by cumulative summation
81+
else
82+
vector_length = 24000;
83+
[data, tcp, sleepStart, sleepEnd] = rrgenV3_wrapper(seed, vector_length, 0, 0, pathToRepo);
84+
time = cumsum(data);
85+
end
6886

6987
% Converting rrgen data to actigraphy data
7088
if settings.act == 1
@@ -122,11 +140,7 @@ function pipelineArtificialData(pathToRepo)
122140

123141
%% Modified Bayesian Online Changepoint Detection
124142

125-
rl = mbocd(data, lambda, 'gamma');
126-
127-
% Locate estimated changepoints
128-
[~, ecp] = findpeaks(rl(:,1));
129-
ecp(end) = []; % last index is errenous
143+
ecp = msegWin(data, lambda, 30000);
130144

131145
% Plot true and estimated change points
132146
if settings.figs == 1
@@ -141,9 +155,7 @@ function pipelineArtificialData(pathToRepo)
141155

142156
%% BOCD Original Code
143157

144-
[~, maxes] = bocd(data, lambda2);
145-
146-
[~, ecp] = findpeaks(maxes(:,1));
158+
ecp = segWin(data,lambda2, 30000);
147159

148160
% Plot true and estimated change points
149161
if settings.figs == 1
@@ -367,4 +379,4 @@ function pipelineArtificialData(pathToRepo)
367379

368380
tightfig;
369381

370-
end
382+
%end

artificialDataAnalysis/rrGenerator/rrgenV3_wrapper.m

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
function [rr, tcp] = rrgenV3_wrapper(seed, vector_length, prob_ectopy, prob_noise, pathToRepo)
1+
function [rr, tcp, sleepStart, sleepEnd] = rrgenV3_wrapper(seed, vector_length, ...
2+
prob_ectopy, prob_noise, pathToRepo)
23
%
34
% OVERVIEW:
45
% Matlab wrapper for calling compiled rrgen executable
@@ -17,6 +18,8 @@
1718
% OUTPUT
1819
% rr - RR interval time series
1920
% tcp - True change points in time series
21+
% sleepStart - Index of sleep start in artificial RR interval code
22+
% sleepEnd - Index of sleep end in artificial RR interval code
2023
%
2124
% REPO
2225
% https://github.com/cliffordlab/rrgen
@@ -47,24 +50,37 @@
4750
vector_length = 200;
4851
end
4952

50-
command = [pathToRepo filesep 'artificialDataAnalysis' filesep 'rrGenerator' filesep 'rrgenV3' ' ' num2str(seed) ' ' num2str(vector_length) ' ' num2str(prob_ectopy) ' ' num2str(prob_noise)];
53+
generate = 1;
54+
while generate == 1
55+
command = [pathToRepo filesep 'artificialDataAnalysis' filesep 'rrGenerator' filesep 'rrgenV3' ' ' num2str(seed) ' ' num2str(vector_length) ' ' num2str(prob_ectopy) ' ' num2str(prob_noise)];
5156

52-
% Call rrgen2 via system and save output
53-
[~, systemout] = system(command);
57+
% Call rrgen2 via system and save output
58+
[~, systemout] = system(command);
5459

55-
% Read the system output into cells
56-
systemout_cell = textscan(systemout,'%f %f %f','Delimiter',',');
60+
% Read the system output into cells
61+
systemout_cell = textscan(systemout,'%f %f %f','Delimiter',',');
5762

58-
% Convert cells of doubles into matrix
59-
rrgen_mat = cell2mat(systemout_cell);
63+
% Convert cells of doubles into matrix
64+
rrgen_mat = cell2mat(systemout_cell);
6065

61-
% Isolate columns of matrix into output vectors
62-
trpeaks = rrgen_mat(rrgen_mat(:,3)==7,1);
63-
rr = rrgen_mat(rrgen_mat(:,3)~=7,1);
66+
% Isolate columns of matrix into output vectors
67+
trpeaks = rrgen_mat(rrgen_mat(:,3)==7,1);
68+
rr = rrgen_mat(rrgen_mat(:,3)~=7,1);
6469

65-
% Output true change points depending on noise probability
66-
tcp_idx = find(rrgen_mat(rrgen_mat(:,3)~=7,2)==1);
70+
% Output true change points depending on noise probability
71+
tcp_idx = find(rrgen_mat(rrgen_mat(:,3)~=7,2)==1);
6772

73+
% Find sleep start and end indexes
74+
sleep_idx = find(rrgen_mat(rrgen_mat(:,3)~=7,3)==0);
75+
76+
if isempty(sleep_idx)
77+
disp('Regenerating RR intervals...');
78+
else
79+
generate = 0; % Artificial data generation complete;
80+
end
81+
end
82+
83+
% Adjust for noise if necessary
6884
tcp = zeros(length(tcp_idx),1);
6985
if prob_noise==0
7086
tcp = tcp_idx;
@@ -77,4 +93,19 @@
7793
end
7894
end
7995

96+
sleep = zeros(length(sleep_idx),1);
97+
if prob_noise==0
98+
sleep = sleep_idx;
99+
else
100+
sleep_time = trpeaks(sleep_idx);
101+
t = cumsum(rr);
102+
for i=1:length(sleep)
103+
[~,I] = min(abs(t-sleep_time(i)));
104+
sleep(i) = I(1);
105+
end
106+
end
107+
108+
sleepStart = sleep(1);
109+
sleepEnd = sleep(end-1);
110+
80111
end

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Add where R is located to your MATLAB environment <br />
6868
setenv('PATH', [getenv('PATH'),':','/usr/local/bin']);
6969

7070
+ In artificial data analysis, no data is generated
71-
Make sure your rrgen_2003_v2 executable is suitable for your computer type.
71+
Make sure your rrgenV3 executable is suitable for your computer type.
7272
If not, delete this and re compile as decribed above.
7373

7474
## Useful Links

0 commit comments

Comments
 (0)