-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_cleaning.Rmd
343 lines (271 loc) · 9.23 KB
/
data_cleaning.Rmd
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
---
title: "R Notebook"
output:
html_document: default
html_notebook: default
---
Utility functions
```{r}
# select and rename columns
# detect boundary conditions
# diffmax: maximum tolerated difference between t and t+1 to detect border crossing, eg c(velocity_xmax, velocity_ymax)
# field_size: to offsed boundary crossing
save_clean_data <- function(df, target_columns, output_path, real_data, diffmax, field_size) {
# add row id as "time"
clean_data = data.frame(c(0:(length(df[[target_columns[1]]])-1)), df[[target_columns[1]]], df[[target_columns[2]]])
colnames(clean_data) = c("t", "x", "y")
clean_data[, colnames(clean_data)] <- lapply(colnames(clean_data), function(x) as.numeric(clean_data[[x]]))
print(length(clean_data$t))
# deal with boundary conditions
if(!real_data){
nrows = length(clean_data$t)
#boundary conditions
diff = clean_data
diff$dx = diff$x - c(0, diff$x[1:(nrows-1)])
index <- diff$dx > diffmax[1]
# offset all data from there
time_indices = clean_data$t[index]
for ( time in time_indices) {
clean_data$x[(time+1):nrows] = clean_data$x[(time+1):nrows] - field_size[1]
}
index = diff$dx < (-diffmax[1])
time_indices = clean_data$t[index]
for ( time in time_indices) {
clean_data$x[(time+1):nrows] = clean_data$x[(time+1):nrows] + field_size[1]
}
diff$dy = diff$y - c(0, diff$y[1:(nrows-1)])
index <- diff$dy > diffmax[2]
time_indices = clean_data$t[index]
for ( time in time_indices) {
clean_data$y[(time+1):nrows] = clean_data$y[(time+1):nrows] - field_size[2]
}
index = diff$dy < (-diffmax[2])
time_indices = clean_data$t[index]
for ( time in time_indices) {
clean_data$y[(time+1):nrows] = clean_data$y[(time+1):nrows] + field_size[2]
}
}
# plot(x = clean_data$x, y = clean_data$y,
# type = "l", main = output_path)
# finally, normalize the data and give precision to 4 decimals
precision = 4
factor = max(clean_data$x) - min(clean_data$x)
off = min(clean_data$x)
clean_data$x = round( (clean_data$x-off)/factor, digits = precision)
factor = max(clean_data$y) - min(clean_data$y)
off = min(clean_data$y)
clean_data$y = round( (clean_data$y - off)/factor, digits = precision)
plot(x = clean_data$x, y = clean_data$y,
type = "l", main = output_path)
write.csv(clean_data, paste(base_path, output_path, sep = ""), row.names=FALSE)
print(output_path)
}
# cut files in a directory into smaller bits
# offset: starting point, everything before this step will be ignored
# dataset_dir: path where the files are stored
# output_dir: path where the output will be stored
# steps: size of each output file
# header: do the data files have headers or not
# column_names: columns to be selected
# total_cuts: number of output files
# real_data: true if real data, false if fake
cut_data = function(dataset_dir, output_dir, offset, steps, header, target_columns,
total_cuts, real_data, diffmax, field_size){
dir.create(paste(base_path, output_dir, sep = ""), showWarnings = FALSE)
all_files = list.files(path = dataset_dir, full.names = TRUE, recursive = FALSE)
# read files in order, plot and save by 2000 time steps increments
file_id = 1
run = 0
dataset_name = ""
for(i in c(0:(total_cuts-1))){
print(i)
if(dataset_name != all_files[file_id]){
dataset_name = all_files[file_id]
print(dataset_name)
df = read.csv(dataset_name, check.names = FALSE, header = header, stringsAsFactors = FALSE)
}
start = offset + run*steps + 1
sub_df = df[start:(start + steps -1),]
output_path = paste(output_dir, i , ".csv", sep = "")
print(output_path)
save_clean_data(sub_df, target_columns, output_path, real_data, diffmax, field_size)
file_id = file_id + 1
if (file_id >= length(all_files) ){
file_id = 1
run = run + 1
}
}
}
```
Path variables
```{r}
# dataset base path
base_path = "/Users/lana/Desktop/prgm/CSL/FLR_contest/data/"
```
Bird data.
Selected: 0, 1, 4, 7, 13
```{r}
dataset_dir = paste(base_path, "real_life/Annotated20GPS20tracking20data/data/", sep = "")
output_dir = "real_ready/0/"
offset = 5000
steps = 2000
header = TRUE
column_names = c("latitude", "longitude")
total_cuts = 20
real_data = TRUE
cut_data(dataset_dir, output_dir, offset, steps, header, column_names, total_cuts, real_data, NULL, NULL)
```
Read ants data
Selected: 0,1,2,3,4
```{r}
dataset_dir = paste(base_path, "real_life/Ant20exploration20trajectories/data/", sep = "")
output_dir = "real_ready/1/"
offset = 10000
steps = 2000
header = FALSE
column_names = c("V2", "V3")
total_cuts = 5
real_data = TRUE
cut_data(dataset_dir, output_dir, offset, steps, header, column_names, total_cuts, real_data, NULL, NULL)
```
Read shark data
Select 0-4
```{r}
dataset_dir = paste(base_path, "real_life/sharks/data/", sep = "")
output_dir = "real_ready/2/"
offset = 10000
steps = 2000
header = TRUE
column_names = c("lat", "long")
total_cuts = 5
real_data = TRUE
cut_data(dataset_dir, output_dir, offset, steps, header, column_names, total_cuts, real_data, NULL, NULL)
# dataset_name = paste(dataset_dir, "BT_Palmyra_telemetry_hourly_through2015.csv", sep = "")
# df = read.csv(dataset_name, check.names = FALSE, header = header, stringsAsFactors = FALSE)
#
# plot(df$lat[1:50], df$long[1:50], type = "l")
```
Read spiders data
Data is divided in small trials....
Selected 0, 1, 6, 7, 14
```{r}
dataset_dir = paste(base_path, "real_life/spiders/data/", sep = "")
output_dir = "real_ready/3/"
offset = 50000
steps = 2000
header = TRUE
column_names = c("x location mm", "y location mm")
total_cuts = 15
real_data = TRUE
cut_data(dataset_dir, output_dir, offset, steps, header, column_names, total_cuts, real_data, NULL, NULL)
```
Read jaguar data
Select 0, 2, 3, 5, 6
```{r}
dataset_dir = paste(base_path, "real_life/jaguars/data/", sep = "")
output_dir = "real_ready/4/"
offset = 5000
steps = 2000
header = TRUE
column_names = c("location.long", "location.lat")
total_cuts = 10
real_data = TRUE
cut_data(dataset_dir, output_dir, offset, steps, header, column_names, total_cuts, real_data, NULL, NULL)
```
Read boids data
```{r}
dataset_dir = paste(base_path, "fake_life/boids", sep = "")
output_dir = "fake_ready/0/"
offset = 0
steps = 2000
header = FALSE
column_names = c("V2", "V3")
total_cuts = 5
real_data = FALSE
diffmax = c(0.4,0.4) #
field_size = c(1,1)
cut_data(dataset_dir, output_dir, offset, steps, header, column_names, total_cuts, real_data, diffmax, field_size)
```
Clean artificial chemistry data
Data is already perfectly clean
```{r}
dataset_dir = paste(base_path, "fake_life/artificial_chemistry/", sep = "")
output_dir = "fake_ready/1/"
offset = 0
steps = 2000
header = FALSE
column_names = c("V2", "V3")
total_cuts = 5
real_data = TRUE # no cleaning needed
diffmax = NULL
field_size = NULL
cut_data(dataset_dir, output_dir, offset, steps, header, column_names, total_cuts, real_data, diffmax, field_size)
```
Read Olaf data 1
```{r}
dataset_dir = paste(base_path, "fake_life/olaf_1/", sep = "")
output_dir = "fake_ready/2/"
offset = 0
steps = 2000
header = TRUE
column_names = c("x", "y")
total_cuts = 5
diffmax = c(6,6)
field_size = c(8,8)
cut_data(dataset_dir, output_dir, offset, steps, header, column_names, total_cuts, FALSE, diffmax, field_size)
```
Read Olaf data 2
Data is clean
```{r}
dataset_dir = paste(base_path, "fake_life/olaf_2/", sep = "")
output_dir = "fake_ready/3/"
offset = 0
steps = 2000
header = TRUE
column_names = c("x", "y")
total_cuts = 5
real_data = TRUE # no cleaning needed
diffmax = NULL
field_size = NULL
cut_data(dataset_dir, output_dir, offset, steps, header, column_names, total_cuts, real_data, diffmax, field_size)
```
Read robot arm data
```{r}
dataset_dir = paste(base_path, "fake_life/robot_arm/", sep = "")
output_dir = "fake_ready/4/"
offset = 0
steps = 2000
header = TRUE
column_names = c("x", "y")
total_cuts = 5
real_data = TRUE # no cleaning needed
diffmax = NULL
field_size = NULL
cut_data(dataset_dir, output_dir, offset, steps, header, column_names, total_cuts, real_data, diffmax, field_size)
```
Finally, plot all selected data
```{r}
dataset_dir = paste(base_path, "real_ready/", sep = "")
all_files = list.files(path = dataset_dir, full.names = TRUE, recursive = TRUE)
for(dataset_name in all_files){
df = read.csv(dataset_name, check.names = FALSE, header = header, stringsAsFactors = FALSE)
plot(x = df$x, y = df$y, type = "l", main = dataset_name)
}
dataset_dir = paste(base_path, "fake_ready/", sep = "")
all_files = list.files(path = dataset_dir, full.names = TRUE, recursive = TRUE)
for(dataset_name in all_files){
df = read.csv(dataset_name, check.names = FALSE, header = header, stringsAsFactors = FALSE)
plot(x = df$x, y = df$y, type = "l", main = dataset_name)
}
```
Plot the 1st chunk of final data for verification
```{r}
dataset_dir = paste(base_path, "all_data_ready/", sep = "")
all_dirs = list.dirs(path = dataset_dir, full.names = TRUE, recursive = FALSE)
#all_files = list.files(path = dataset_dir, full.names = TRUE, recursive = TRUE)
for(dataset_dir in all_dirs){
dataset_name = paste(dataset_dir,"/0.csv", sep="")
df = read.csv(dataset_name, check.names = FALSE, header = TRUE, stringsAsFactors = FALSE)
plot(x = df$x, y = df$y, type = "l", main = dataset_name)
}
```