-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.m
More file actions
229 lines (180 loc) · 7.63 KB
/
Copy pathanalysis.m
File metadata and controls
229 lines (180 loc) · 7.63 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
function analysis
%ANALYSIS Add bed logs to data that is already cropped
% Detailed explanation goes here
% Enable dependecies
initializedependencies;
% Construct project paths
Paths = initializepaths;
[cdfFileArray,cdfPathArray] = searchdir(Paths.editedData,'cdf');
[bedLogFileNameArray,bedLogPathArray] = searchdir(Paths.logs,'xlsx');
% Import the index
[indexSubjectArray,indexWeekArray,indexFileArray] = importindex(Paths.index);
% Extract subject from bed logs
bedSubject = str2double(regexprep(bedLogFileNameArray,'.*(\d\d\d).*','$1'));
idxNaN = isnan(bedSubject);
bedSubject(idxNaN) = [];
bedLogFileNameArray(idxNaN) = [];
bedLogPathArray(idxNaN) = [];
% Preallocate and intialize resources
[hFigure,~,~,units] = initializefigure1(1,'on');
nFiles = numel(cdfPathArray);
templateCell = cell(nFiles,1);
Output = dataset;
Output.subject = templateCell;
Output.week = templateCell;
% sleep
Output.nightsAveraged = templateCell;
Output.actualSleepTimeMins = templateCell;
Output.actualSleepPercent = templateCell;
Output.actualWakeTimeMins = templateCell;
Output.actualWakePercent = templateCell;
Output.sleepEfficiency = templateCell;
Output.sleepOnsetLatencyMins = templateCell;
Output.sleepBouts = templateCell;
Output.wakeBouts = templateCell;
Output.meanSleepBoutTimeMins = templateCell;
Output.meanWakeBoutTimeMins = templateCell;
% phasor, IS, IV
Output.phasorMagnitude = templateCell;
Output.phasorAngleHrs = templateCell;
Output.interdailyStability = templateCell;
Output.intradailyVariability = templateCell;
% averages
Output.meanNonzeroCs = templateCell;
Output.logmeanNonzeroLux = templateCell;
Output.meanNonzeroActivity = templateCell;
for i1 = 1:nFiles
% Import data
Data = ProcessCDF(cdfPathArray{i1});
subject = Data.GlobalAttributes.subjectID{1};
logicalArray = logical(Data.Variables.logicalArray);
complianceArray = logical(Data.Variables.complianceArray(logicalArray));
bedArray = logical(Data.Variables.bedArray(logicalArray));
timeArray = Data.Variables.time(logicalArray);
activityArray = Data.Variables.activity(logicalArray);
csArray = Data.Variables.CS(logicalArray);
illuminanceArray = Data.Variables.illuminance(logicalArray);
% Match index entry
idxIndex = strcmpi(cdfFileArray{i1},indexFileArray);
week = num2str(indexWeekArray(idxIndex));
% Set subject and week
Output.subject{i1,1} = subject;
Output.week{i1,1} = week;
% Check useable data
if numel(timeArray(complianceArray)) < 24
continue
end
% Match and import bed log
bedIdx = bedSubject == str2double(subject);
[bedTimeArray,riseTimeArray] = importbedlog(bedLogPathArray{bedIdx});
% Daysigram
sheetTitle = ['NIH Alzheimer''s Light Table - Subject ',subject,' Week ',week];
daysigramFileID = ['subject',subject,'_week',week];
generatedaysigram(sheetTitle,timeArray(complianceArray),...
activityArray(complianceArray),csArray(complianceArray),...
'cs',[0,1],12,Paths.plots,daysigramFileID)
% Light and Health Report/ Phasor Analysis
figTitle = 'NIH Alzheimer''s Light Table';
Phasor = phasorprep(subject,week,figTitle,hFigure,units,Paths,...
complianceArray,bedArray,timeArray,csArray,activityArray,...
illuminanceArray);
% Averages
Average = prepaverages(timeArray,csArray,activityArray,...
illuminanceArray,complianceArray,bedArray);
% Sleep Analysis
[Sleep,nIntervalsAveraged] = sleepprep(timeArray,activityArray,...
bedTimeArray,riseTimeArray,complianceArray);
% Assign output
% sleep
Output.nightsAveraged{i1,1} = nIntervalsAveraged;
Output.actualSleepTimeMins{i1,1} = Sleep.actualSleepTime;
Output.actualSleepPercent{i1,1} = Sleep.actualSleepPercent;
Output.actualWakeTimeMins{i1,1} = Sleep.actualWakeTime;
Output.actualWakePercent{i1,1} = Sleep.actualWakePercent;
Output.sleepEfficiency{i1,1} = Sleep.sleepEfficiency;
Output.sleepOnsetLatencyMins{i1,1} = Sleep.sleepLatency;
Output.sleepBouts{i1,1} = Sleep.sleepBouts;
Output.wakeBouts{i1,1} = Sleep.wakeBouts;
Output.meanSleepBoutTimeMins{i1,1} = Sleep.meanSleepBoutTime;
Output.meanWakeBoutTimeMins{i1,1} = Sleep.meanWakeBoutTime;
% phasor, IS, IV
Output.phasorMagnitude{i1,1} = Phasor.phasorMagnitude;
Output.phasorAngleHrs{i1,1} = Phasor.phasorAngleHrs;
Output.interdailyStability{i1,1} = Phasor.interdailyStability;
Output.intradailyVariability{i1,1} = Phasor.intradailyVariability;
% averages
Output.meanNonzeroCs{i1,1} = Average.cs;
Output.logmeanNonzeroLux{i1,1} = Average.illuminance;
Output.meanNonzeroActivity{i1,1} = Average.activity;
end
close all
runtime = datestr(now,'yyyy-mm-dd_HHMM');
resultsPath = fullfile(Paths.results,['results_',runtime,'_NIHAlzheimers-LightTable.xlsx']);
organizeanalysis(Output,resultsPath);
end
function Output = phasorprep(subject,week,figTitle,hFigure,units,Paths,complianceArray,bedArray,timeArray,csArray,activityArray,illuminanceArray)
clf;
% replace in bed time
csArray(bedArray) = 0;
activityArray(bedArray) = 0;
illuminanceArray(bedArray) = 0;
% remove only large noncompliance while awake
complianceArray = adjustcrop(timeArray,complianceArray,bedArray);
timeArray(~complianceArray) = [];
csArray(~complianceArray) = [];
activityArray(~complianceArray) = [];
illuminanceArray(~complianceArray) = [];
subject = [subject,' Week ',week];
Output = generatereport(Paths.plots,timeArray,csArray,activityArray,...
illuminanceArray,subject,hFigure,units,figTitle);
end
function [workIdx,postWorkIdx] = createworkday(timeArray,bedTimeArray)
workStart = 8/24;
workEnd = 17/24;
dayArray = unique(floor(timeArray));
dayOfWeekArray = weekday(dayArray); % Sunday = 1, Monday = 2, etc.
workDaysIdx = dayOfWeekArray >= 2 & dayOfWeekArray <= 6;
workDayArray = dayArray(workDaysIdx);
workStartArray = workDayArray + workStart;
workEndArray = workDayArray + workEnd;
workIdx = false(size(timeArray));
postWorkIdx = false(size(timeArray));
for j1 = 1:numel(workStartArray)
tempWorkIdx = timeArray > workStartArray(j1) & timeArray <= workEndArray(j1);
workIdx = workIdx | tempWorkIdx;
diffBedTime = bedTimeArray - workEndArray(j1);
currentBedTime = bedTimeArray(diffBedTime<1 & diffBedTime>0);
if numel(currentBedTime) == 1
tempPostWorkIdx = timeArray > workEndArray(j1) & timeArray <=currentBedTime;
postWorkIdx = postWorkIdx | tempPostWorkIdx;
end
end
end
function Average = prepaverages(timeArray,csArray,activityArray,illuminanceArray,complianceArray,bedArray)
validIdx = complianceArray & ~bedArray;
timeArray(~validIdx) = [];
csArray(~validIdx) = [];
activityArray(~validIdx) = [];
illuminanceArray(~validIdx) = [];
Average = daysimeteraverages(csArray,illuminanceArray,activityArray);
end
function [Sleep,nIntervalsAveraged] = sleepprep(timeArray,activityArray,bedTimeArray,riseTimeArray,complianceArray)
timeArray(~complianceArray) = [];
activityArray(~complianceArray) = [];
nIntervals = numel(bedTimeArray);
dailySleep = cell(nIntervals,1);
analysisStartTimeArray = bedTimeArray - 20/(60*24);
analysisEndTimeArray = riseTimeArray + 20/(60*24);
for i1 = 1:nIntervals
% Perform analysis
try
dailySleep{i1} = sleepAnalysis(timeArray,activityArray,...
analysisStartTimeArray(i1),analysisEndTimeArray(i1),...
bedTimeArray(i1),riseTimeArray(i1),'auto');
catch err
continue
end
end
% Average results
[Sleep,nIntervalsAveraged] = averageanalysis(dailySleep);
end