This repository was archived by the owner on Nov 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathplotPatterns.m
170 lines (153 loc) · 6.42 KB
/
plotPatterns.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
function plotPatterns(config, MuseStruct)
% remove empty structs
MuseStruct = MuseStruct(~cellfun('isempty',MuseStruct));
MuseStruct_append = []; %rmfield(MuseStruct{1},'filenames');
MuseStruct_append.markers = [];
% adding next directories
for idir = 1 : size(MuseStruct,2)
fprintf('Working on directory %d of %d\n',idir,size(MuseStruct,2));
try % some might be empty
mrknames = fieldnames(MuseStruct{idir}.markers);
for imarker = 1 : numel(mrknames)
if ~isfield(MuseStruct_append.markers,(mrknames{imarker}))
MuseStruct_append.markers.(mrknames{imarker}) = MuseStruct{idir}.markers.(mrknames{imarker});
end
if ~isfield(MuseStruct_append.markers.(mrknames{imarker}),'clock')
MuseStruct_append.markers.(mrknames{imarker}).clock = [];
end
if isfield(MuseStruct{idir}.markers.(mrknames{imarker}),'clock')
MuseStruct_append.markers.(mrknames{imarker}).clock = ...
[MuseStruct_append.markers.(mrknames{imarker}).clock, ...
MuseStruct{idir}.markers.(mrknames{imarker}).clock];
end
if ~isfield(MuseStruct_append.markers.(mrknames{imarker}),'samples')
MuseStruct_append.markers.(mrknames{imarker}).sample = [];
end
if isfield(MuseStruct{idir}.markers.(mrknames{imarker}).events,'sample')
for ievent = 1 : length(MuseStruct{idir}.markers.(mrknames{imarker}).events)
MuseStruct_append.markers.(mrknames{imarker}).sample = ...
[MuseStruct_append.markers.(mrknames{imarker}).sample, ...
MuseStruct{idir}.markers.(mrknames{imarker}).events(ievent).sample];
end
end
if ~isfield(MuseStruct_append.markers.(mrknames{imarker}),'sec')
MuseStruct_append.markers.(mrknames{imarker}).sec = [];
end
if isfield(MuseStruct{idir}.markers.(mrknames{imarker}).events,'time')
for ievent = 1 : length(MuseStruct{idir}.markers.(mrknames{imarker}).events)
MuseStruct_append.markers.(mrknames{imarker}).sec = ...
[MuseStruct_append.markers.(mrknames{imarker}).sec, ...
MuseStruct{idir}.markers.(mrknames{imarker}).events(ievent).time];
end
end
end
catch
end
end
% remove empty markers
fn = fieldnames(MuseStruct_append.markers);
for imarker = 1 : numel(fn)
if isempty(MuseStruct_append.markers.(fn{imarker}).sample)
MuseStruct_append.markers = rmfield(MuseStruct_append.markers,fn{imarker});
end
end
% concatinate markers
fn = fieldnames(MuseStruct_append.markers);
markerlabel = [];
starttime = [];
for imarker = 1 : numel(fn)
markerlabel = [markerlabel; repmat(convertCharsToStrings(fn{imarker}),numel(MuseStruct_append.markers.(fn{imarker}).clock),1)];
starttime = [starttime; MuseStruct_append.markers.(fn{imarker}).clock'];
end
endtime = starttime; % same time if cant find end marker
t = table(markerlabel,starttime,endtime);
t = unique(t,'rows');
t = sortrows(t,2);
i = 1;
while i < height(t)
if contains(t.markerlabel(i),'__START__')
t.markerlabel(i) = t.markerlabel{i}(1:end-9);
endsindx = find(contains(t.markerlabel,strcat(t.markerlabel(i),'__END__')));
endsindx = endsindx(endsindx > i);
endindx = endsindx(1);
t.endtime(i) = t.starttime(endindx);
t(endindx,:) = [];
end
i = i + 1;
end
% t(contains(t.markerlabel,config.hyp.notcontains),:) = [];
t = t(contains(t.markerlabel,config.hyp.contains),:);
%
% startsindx = find(contains(t.markerlabel,'CriseStart'));
% endsindx = find(contains(t.markerlabel,'CriseEnd'));
%
% startsindx = find(contains(t.markerlabel,config.pattern.startmarker));
% endsindx = find(contains(t.markerlabel,config.pattern.endmarker));
% fprintf('\n%d Patterns found\n',numel(startsindx));
startsindx = [1; find(diff(t.endtime) > hours(2))+1];
endsindx = [find(diff(t.endtime) > hours(2)); length(endsindx)];
ii = 1;
for i = 1 : numel(startsindx)
if endsindx(i) - startsindx(i) > 1
s{ii} = t(startsindx(i)+1:endsindx(i)-1,:);
Cstart(ii) = t.starttime(startsindx(i));
Cend(ii) = t.starttime(endsindx(i));
ii = ii + 1;
end
end
t(contains(t.markerlabel,config.pattern.startmarker) | contains(t.markerlabel,config.pattern.endmarker),:) = [];
maxlength = max(Cend - Cstart);
%% plotting
colortable.label = unique(t.markerlabel);
colortable.color = linspecer(numel(unique(t.markerlabel)));
h = figure;
for is = 1 : size(s,2)
subplot(size(s,2)+1,1,is); hold;
fill([s{is}.starttime(1), Cstart(is) + maxlength, Cstart(is) + maxlength, s{is}.starttime(1)],[0 0 1 1],[1 1 1],'EdgeColor',[1 1 1]);
X = [];
Y = [];
for im = 1 : height(s{is})
c = colortable.color(strcmp(colortable.label,s{is}.markerlabel(im)),:);
if ~isempty(X)
if s{is}.starttime(im) ~= X(end)
X = [X, X(end) s{is}.starttime(im)];
Y = [Y, 0 0];
end
end
X = [X, s{is}.starttime(im), s{is}.endtime(im)];
y = find(contains(config.hyp.contains,t.markerlabel(im)));
Y = [Y, y, y];
end
for i = 1 : length(X)-1
if Y(i) ~= 0 && Y(i+1) ~= 0
if strcmp(config.hyp.contains(Y(i)),'REM') && strcmp(config.hyp.contains(Y(i+1)),'REM')
plot([X(i) X(i+1)],[Y(i) Y(i+1)],'k','LineWidth',3);
else
plot([X(i) X(i+1)],[Y(i) Y(i+1)],'k');
end
end
end
set(gca,'Layer','top');
set(gca,'Ytick', 1 : length(config.hyp.contains),'Yticklabels',strrep(config.hyp.contains,'_',' '),'TickDir','out');
axis tight;
end
%
% subplot(size(s,2)+1,1,size(s,2)+1); hold;
% for marker = 1 : numel(colortable.label)
% c = colortable.color(marker,:);
% x1 = marker*2;
% x2 = marker*2+1.5;
% fill([x1,x2,x2,x1],[0 0 1 1],c,'EdgeColor',c,'facealpha',1);
% text((x1+x2)/2,0.1,colortable.label(marker),'Rotation',90,'Fontsize',10);
% set(gca,'Layer','top');
% end
% set(gca,'XTick',[]);
% axis tight
% print to file
set(h,'PaperOrientation','landscape');
set(h,'PaperUnits','normalized');
set(h,'PaperPosition', [0 0 1 1]);
set(h,'Renderer','Painters');
print(h, '-dpdf', fullfile(config.imagesavedir,[config.prefix,'overview_patterns.pdf']),'-r600');
% writetable(t,fullfile(config.imagesavedir,'seg_labels'));
disp('Done');