forked from pramitghosh/statvis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.R
148 lines (105 loc) · 4.29 KB
/
import.R
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
#Note: Some comments in this code are OUTDATED!!
library('jsonlite')
#initialize a list of 3 lists
#each sub-lists for each camera
json_data = list(c1 = list(), c2 = list(), c4 = list())
files = list.files(path = "Experiments/", pattern = "*.json", recursive = TRUE, full.names = TRUE)
for(filename_w_path in files)
{
#import json values as text from json file
json_value = readLines(filename_w_path, n = -1, warn = FALSE)
# ============ The following lines for each camera ==========================
#read meta information from JSON char string with position numbers.
#Can be made dynamic to get the position numbers using grep - but can also result in pitfalls!
#For now since the JSON structure is consistent, this is enough.
cid = as.numeric(substr(json_value, 12, 13))
# height = as.numeric(substr(json_value, 104, 107))
# global_x = as.numeric(substr(json_value, 125, <end position here>))
# global_y = as.numeric(substr(json_value, 147, <end position here>))
# global_z = as.numeric(substr(json_value, 169, <end position here>))
# tilt = as.numeric(substr(json_value, 185, <end position here>))
#Combine camera information into a dataframe
#The other variables mentioned above have to be added too.
# cinfo = data.frame(cid, height)
# ==============================================================================
#remove the extra header in the data to make it compatible with the JSON format
#replace the 1st occurence of square brackets and anything inside the square brackets with an empty string ("") effectively deleting it
sample_json = sub("*\\[.*?\\] *", "", json_value)
#read the variable as a valid json
sample_json = jsonlite::fromJSON(sample_json)
sample_json = sample_json$bodies_data
#Since there is no camera 3; a makeshift solution
if(cid == 4) cid = 3
# columns = c()
# startpos = 0
#Note: going through each directory on the disk: c1, c2 and c4 might have to be implemented to import data recursively
#go through each of the data frames in sample_json and store them in the correct sub-list of json_data according to the camera
for(i in 1:length(sample_json))
{
# columns = c(columns, names(sample_json[i]))
tailpos = length(json_data[[cid]]) + 1
# if(i == 1) startpos = tailpos
#In the next line: "json_data$c1" might have to be made dynamic by changing it to json_data[1], for example
#This can be done after implementing recursive directory traversal (see "Note" above)
json_data[[cid]][tailpos] = sample_json[i]
#access by: json_data$c1[[i]]$x, json_data$c1[[i]]$time etc.
#converting "time" to POSIX time
# op <- options(digits.secs=6)
json_data[[cid]][[tailpos]]$time = strptime(json_data[[cid]][[tailpos]]$time, format = "%H:%M:%OS")#, format = "%H:%M:%OS")
json_data[[cid]][[tailpos]]$camera = cid
#Find starting & ending times
print("Start time: ")
start = head(json_data[[cid]][[tailpos]]$time, 1)
print(start)
print("End time: ")
end = tail(json_data[[cid]][[tailpos]]$time, 1)
print(end)
#Find the duration
print("Duration: ")
print(end-start)
}
# c_name = paste("c", cid, sep = "")
# names(json_data[[cid]][startpos:length(json_data[[cid]])]) = c(columns)
# columns = NA
}
source("loc2glob.R")
globalized_json = loc2glob(json_data)
source("trajectory.R")
globalized_tracks = create_trajectories(globalized_json)
ti=0
for(tri in 1:(length(globalized_json)-1))
{
for(trj in 1:length(globalized_json[[tri]]))
{
ti=ti+1
if(ti==2 || ti==9)
next
egtrack = globalized_tracks[[ti]]
source("find_stops.R")
if(dim(bpts_df)[1]==0){
next
}
globalized_json[[tri]][[trj]]$stop = rep(1,dim(globalized_json[[tri]][[trj]])[1])
for(k in 1:(dim(bpts_df)[1]))
{
globalized_json[[tri]][[trj]]$stop[bpts_df[k,1]:bpts_df[k,2]]=0
}
print( paste("aafsesef: ",toString(trj)))
}
}
l = c()
trackCount = 1
for(i in 1:(length(globalized_json)-1))
{
for(j in 1:length(globalized_json[[i]]))
{
l[paste("t0",trackCount,sep="")] = globalized_json[[i]][j]
trackCount=trackCount+1
}
}
for(i in 1:(length(l)-1))
{
l[[i]] = subset( l[[i]], select = c(1,2,4,21,22) )
}
newd = toJSON(l)
writeLines(newd, paste("statvis","/data/test.json",sep="") )