-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanalysis.R
More file actions
257 lines (177 loc) · 8.56 KB
/
analysis.R
File metadata and controls
257 lines (177 loc) · 8.56 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
## further data cleaning and SemiSupervised filling in of 10 initial data points
library(randomForest)
library(data.table)
library(TLBC)
library("upclass")
library(extraTrees)
#Window size in seconds
ws=60
#frequency of data
rate=50
if(Sys.info()[['sysname']]=="Darwin"){
cleanDataDirectory<-'/Users/Matthew/Documents/Oxford/Activity/Prototype_data/clean_data'
cleanBoutDirectory<-'/Users/Matthew/Documents/Oxford/Activity/Prototype_data/all_participants/clean_data'
dataDirectory<-'/Users/Matthew/Documents/Oxford/Activity/Prototype_data'
#Linux
} else if(Sys.info()[['sysname']]=='Linux'){
cleanDataDirectory<-'/data/rockptarmigan/willetts/Prototype_data/clean_data'
cleanBoutDirectory<-'/data/rockptarmigan/willetts/Prototype_data/all_participants/clean_data'
dataDirectory<-'/data/rockptarmigan/willetts/Prototype_data'
}
#Temporary directory
tempDirectory<-paste0(dataDirectory,'/temp')
#Directories for RF and HMM models
RFoutput<-paste0(tempDirectory,'/RFoutput')
HMMoutput<-paste0(tempDirectory,'/HMMoutput')
Predictions<-paste0(tempDirectory,'/Predictions')
#Temp data directories
trainingAccelDirectory<-paste0(tempDirectory,'/AccelTraining')
testingAccelDirectory<-paste0(tempDirectory,'/AccelTesting')
trainingBoutDirectory<-paste0(tempDirectory,'/BoutTraining')
testingBoutDirectory<-paste0(tempDirectory,'/BoutTesting')
trainingFeatureDirectory<-paste0(trainingAccelDirectory,'_Features_',ws)
trainingLabelDirectory<-paste0(trainingBoutDirectory,'_Labels_',ws)
testingFeatureDirectory<-paste0(testingAccelDirectory,'_Features_',ws)
testingLabelDirectory<-paste0(testingBoutDirectory,'_Labels_',ws)
outputLabelDirectory<-paste0(tempDirectory,'/Bout_Labels_',ws)
outputFeatureDirectory<-paste0(tempDirectory,'/Accel_Features_',ws)
#Semi Supervised Learning Output
semiSupervisedLabelDirectory<-paste0(dataDirectory,'/SemiSupLabels')
semiSupervisedFeatureDirectory<-paste0(dataDirectory,'/SemiSupFeatures')
#Cut Down to certainly correct output
certainlyTrueLabelDirectory<-paste0(dataDirectory,'/CertainlyTrueLabels')
certainlyTrueFeatureDirectory<-paste0(dataDirectory,'/CertainlyTrueFeatures')
## Train on first half of data for each individual, test on the second
listOfIndividuals<-list.files(cleanBoutDirectory)
listOfDataFiles<-list.files(cleanDataDirectory)
identifiers<-gsub(listOfIndividuals,pattern = '.csv',replacement = '')
InstanceData<-list()
FeatureData<-list()
IndexOfInstanceFiles<-list()
performance<-list()
trainingNoLabel<-list()
testingNoLabel<-list()
for (i in 1:length(listOfIndividuals)){
#create instance level information
boutFileAddress<-paste(cleanBoutDirectory,listOfIndividuals[i],sep='/')
if(file.exists(file.path(outputLabelDirectory,identifiers[i]))==FALSE){
extractLabelsSingleFile(inputFile = boutFileAddress,outputDir = outputLabelDirectory,winSize = ws)
}
#create features
accelFileAddress<-paste(cleanDataDirectory,listOfDataFiles[i],sep='/')
if(file.exists(file.path(outputFeatureDirectory,identifiers[i]))==FALSE){
extractAccFeatsFile(inputFile = accelFileAddress,outputPath = file.path(outputFeatureDirectory,identifiers[i]),winSize = 60)
}
}
for (i in 1:length(listOfIndividuals)){
#Step 1 - use semi supervised learning to fill in the blank behavior spaces
#Load in instance level labels
InstanceData[[i]]<-NULL
InstanceDir<-file.path(outputLabelDirectory,identifiers[i])
InstanceFiles<-list.files(InstanceDir)
tempInstanceData<-NULL
#Load in features
FeatureData[[i]]<-NULL
tempFeatureData<-NULL
FeatureDir<-file.path(outputFeatureDirectory,identifiers[i])
FeatureFiles<-list.files(FeatureDir)
for(k in 1:length(InstanceFiles)){
#load in instance data
temptempInstanceData<-read.csv(file=file.path(InstanceDir,InstanceFiles[k]),stringsAsFactors = F)
#load in feature data
temptempFeatureData<-read.csv(file=file.path(FeatureDir,FeatureFiles[k]),stringsAsFactors = F)
#discard all data before first labelled data point and after last labelled point
kx<-which(!temptempInstanceData$behavior=='nolabel')
if(length(kx)>0){
maxIndex<-min(kx[length(kx)],nrow(temptempFeatureData))
temptempInstanceData<-temptempInstanceData[kx[1]:maxIndex,]
tempInstanceData<-rbind(tempInstanceData,temptempInstanceData)
temptempFeatureData<-temptempFeatureData[kx[1]:maxIndex,]
tempFeatureData<-rbind(tempFeatureData,temptempFeatureData)
}
}
InstanceData[[i]]<-tempInstanceData
rm(tempInstanceData)
rm(temptempInstanceData)
FeatureData[[i]]<-tempFeatureData
rm(tempFeatureData)
rm(temptempFeatureData)
}
#cut down Instance data to size of Feature data, or vice versa
for (i in 1:length(listOfIndividuals)){
if(nrow(FeatureData[[i]])<nrow(InstanceData[[i]])){
InstanceData[[i]]<-InstanceData[[i]][seq(1,nrow(FeatureData[[i]])),]
} else {
FeatureData[[i]]<-FeatureData[[i]][seq(1,nrow(InstanceData[[i]])),]
}
}
#add participant labels to data
for (i in 1:length(listOfIndividuals)){
InstanceData[[i]]$name<-identifiers[i]
FeatureData[[i]]$name<-identifiers[i]
}
for (i in 1:length(listOfIndividuals)){
print(nrow(InstanceData[[i]]))
print(nrow(FeatureData[[i]]))
}
AllFeatureData<-do.call("rbind", FeatureData)
AllInstanceData<-do.call("rbind", InstanceData)
#reduce the number of different labels
AllInstanceData<-reduceLabels(data=AllInstanceData,labelsToReduce=list(c('gardening','standing'),c('in-vehicle')),overallLabel =c('sitting','driving'))
#which instances have missing labels
ix<-which(AllInstanceData$behavior=='nolabel')
unlabelledInstance<-AllInstanceData[ix,]
labelledInstance<-as.factor(as.vector(AllInstanceData[-ix,2]))
labelCode<-levels(labelledInstance)
levels(labelledInstance)<-1:length(levels(labelledInstance))
labelledInstance<-as.numeric(labelledInstance)
labelledFeature<-AllFeatureData[-ix,]
labelledFeature$timestamp<-NULL
labelledFeature<-as.matrix(labelledFeature)
unlabelledFeature<-AllFeatureData[ix,]
unlabelledFeature$timestamp<-NULL
unlabelledFeature<-as.matrix(unlabelledFeature)
semiSupOutputComplete<-upclassify(Xtrain = labelledFeature,cltrain = labelledInstance,Xtest = unlabelledFeature)
newLabels<-semiSupOutputComplete$Best$test$cl
newLabels<-as.factor(newLabels)
levels(newLabels)<-labelCode
newLabels<-as.character(newLabels)
OutputInstanceData<-AllInstanceData
OutputInstanceData$behavior[ix]=newLabels
semiSupCounter<-1
#Now Output semisupervise labels and features
#and also certainly true
for (i in 1:length(listOfIndividuals)){
#Load in instance level labels
InstanceDir<-file.path(outputLabelDirectory,identifiers[i])
InstanceFiles<-list.files(InstanceDir)
FeatureDir<-file.path(outputFeatureDirectory,identifiers[i])
FeatureFiles<-list.files(FeatureDir)
semiSupervisedLabelOutputDir<-file.path(semiSupervisedLabelDirectory,identifiers[i])
semiSupervisedFeatureOutputDir<-file.path(semiSupervisedFeatureDirectory,identifiers[i])
outputrow<-nrow(InstanceData[[i]])
labelOutputData<-OutputInstanceData[seq(semiSupCounter,semiSupCounter+outputrow-1),]
featureOutputData<-AllFeatureData[seq(semiSupCounter,semiSupCounter+outputrow-1),]
semiSupCounter<-semiSupCounter+outputrow
dir.create(semiSupervisedLabelOutputDir)
dir.create(semiSupervisedFeatureOutputDir)
if(file.exists(paste0(semiSupervisedLabelOutputDir,'/',InstanceFiles[1]))==FALSE){
write.csv(x=labelOutputData,file=paste0(semiSupervisedLabelOutputDir,'/',InstanceFiles[1]),row.names=FALSE,append = FALSE)
}
if(file.exists(paste0(semiSupervisedFeatureOutputDir,'/',FeatureFiles[1]))==FALSE){
write.csv(x=featureOutputData,file=paste0(semiSupervisedFeatureOutputDir,'/',FeatureFiles[1]),row.names=FALSE,append = FALSE)
}
certainlyTrueLabelOutputDir<-file.path(certainlyTrueLabelDirectory,identifiers[i])
certainlyTrueFeatureOutputDir<-file.path(certainlyTrueFeatureDirectory,identifiers[i])
ix<-which(!InstanceData[[i]]$behavior=='nolabel')
certainlyTrueLabels<-InstanceData[[i]][ix,]
certainlyTrueFeatures<-FeatureData[[i]][ix,]
dir.create(certainlyTrueLabelOutputDir)
dir.create(certainlyTrueFeatureOutputDir)
if(file.exists(paste0(certainlyTrueLabelOutputDir,'/',InstanceFiles[1]))==FALSE){
write.csv(x=certainlyTrueLabels,file=paste0(certainlyTrueLabelOutputDir,'/',InstanceFiles[1]),row.names=FALSE,append = FALSE)
}
if(file.exists(paste0(certainlyTrueFeatureOutputDir,'/',FeatureFiles[1]))==FALSE){
write.csv(x=certainlyTrueFeatures,file=paste0(certainlyTrueFeatureOutputDir,'/',FeatureFiles[1]),row.names=FALSE,append = FALSE)
}
}