-
Notifications
You must be signed in to change notification settings - Fork 1
/
runExampleLad.R
262 lines (147 loc) · 5.76 KB
/
runExampleLad.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
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
######### Examples of using methods from this package ###########
source("init.R") # Load user-specific settings, e.g. file names for analysis, etc.
source("data.R") # Load data methods
source("plot.R") # Load plot methods
source("farmLad.R") # Farm-specific functions
# Read FA data
FAdata <- read.FAData(FAfile)
# Print FA data dimensions
getInfo(FAdata)
# Get the time interval
timeRange <- getTimeRange(FAdata)
start <- timeRange[1]
end <- timeRange[2]
data <- apply(FAdata[, c(2, 4, 5, 6)], 2, as.numeric) # Keep essential columns (id, time, x, y)
ids <- sort(unique(FAdata$id)) # Get tag IDs
# Choose if the layout should be rotated or not
bRot <- T
bRot <- F
# Plot trajectory for a selected cow
plotBarn(barn, bRot, axes = FALSE, main = ids[15])
addPoints(FAdata, ids[15], start, end, color = 3, bRot)
addBarnFeatures(barn)
# Remove performance tags
ids <- ids[which(is.na(match(ids, perfTags$tag_id)))]
# Exclude this code form execution
if (FALSE) {
# For each cow, calculate area of rectangular enclosing cow's locations
rArea <- c() # Will store rectangular area for each tag
for (id in ids) {
Ex1.ID1 <- getIndividual(FAdata, id)
Ex1.ID1.Interval <- getInterval(Ex1.ID1, start = start, end = end)
xMin <- min(Ex1.ID1.Interval$x)
xMax <- max(Ex1.ID1.Interval$x)
yMin <- min(Ex1.ID1.Interval$y)
yMax <- max(Ex1.ID1.Interval$y)
if (bRot) {
rect(yMin, -xMin, yMax, -xMax)
text((yMin + yMax) / 2, -(xMin + xMax) / 2, id)
} else {
rect(xMin, yMin, xMax, yMax)
text((xMin + xMax) / 2, (yMin + yMax) / 2, id)
}
print(paste0(id, ": ", (xMax - xMin) * (yMax - yMin)))
rArea <- c(rArea, (xMax - xMin) * (yMax - yMin))
}
# Save tag IDs that don't move a lot
# write.table(ids[which(rArea < 5000000)], file = "stillIDs.csv", sep = ";", col.names = F)
# Plot trajectories of tags that don't move a lot
plotBarn(barn, bRot)
for (id in ids[which(rArea < 5000000)])
addPoints(FAdata, id, "darkred", bRot)
for (id in ids[which(rArea > 5000000 & rArea < 10000000)])
addPoints(FAdata, id, "blue", bRot)
# Output PDF with tags that don't move a lot
# pdf("stillIDs.pdf")
#
# for (id in ids[which(rArea < 5000000)]) {
# plotBarn(barn, bRot)
# title(id)
# addPoints(FAdata, id, "darkred", bRot)
# }
#
# dev.off()
}
# End of excluded code
if (FALSE) {
# Example: read cow data
res <- readCowData(KoInfoFile)
cowData <- res[[1]]
dryData <- res[[2]]
# Example: matching PAA data with cow data by id
PAAdata <- read.PAAData(PAAfile)
id <- 2427958
i <- getTagID(PAAdata, id, cowData)
print(cowData[i, ])
}
# Select tag ID
id <- 2428289 # Small area
id <- 2428864
id <- 2427958
# Get the grid for the whole barn
grid <- getGrid(c(barn$x1[1], barn$x3[1]), c(barn$y1[1], barn$y3[1]), ncol = 50, nrow = 100, bRot)
pal <- adjustcolor(rev(heat.colors(100)), alpha.f = 0.8)
pal <- adjustcolor(cm.colors(100), alpha.f = 0.6)
# Plot location heatmap for selected cow
plotBarn(barn, bRot, axes = F, main = id)
addPoints(FAdata, id, "red", bRot)
r <- rasterizePoints(FAdata, id, start, end, grid, bRot)
image(r, add = T, col = pal)
# Draw heatmap separately
plot(r, axes = F, box = F, main = id)
# For each cow, plot its heatmap of locations
if (F) {
# pdf("plot.pdf")
for (id in ids[which(rArea > 5000000)]) {
plotBarn(barn, bRot, axes = F, main = id)
addPoints(FAdata, id, "red", bRot)
r <- rasterizePoints(FAdata, id, grid, bRot)
image(r, add = T, col = pal)
}
# dev.off()
}
# Rotated raster for all cows
grid <- getGrid(c(barn$x1[1], barn$x3[1]), c(barn$y1[1], barn$y3[1]), nrow = 50, ncol = 100, bRotated = TRUE)
r <- rasterize(cbind(FAdata$y, -FAdata$x), grid, fun = 'count')
plotBarn(barn, bRot = T, main = "All cows", axes = F)
image(r, add = T, col = adjustcolor(rev(topo.colors(100)), alpha.f = 0.9))
plotBarn(barn, bRot = T, bAdd = TRUE) # Add barn wireframe on top
# Standalone heatmap for all cows
plot(r, axes = F, box = F, main = "All cows")
date <- as.Date("2020-09-20")
# Read FA data
FAdata <- read.FAData(paste0(dataFolder, "/CowDataLad/FA_",
as.character(as.Date(date, origin = "1970-01-01"), format = "%Y%m%d"),
"T000000UTC.csv"))
# Print FA data dimensions
getInfo(FAdata)
# Get the time interval
timeRange <- getTimeRange(FAdata)
start <- timeRange[1]
end <- timeRange[2]
ids <- sort(unique(FAdata$id)) # Get tag IDs
id <- ids[24]
PAdata <- read.PAData(paste0(dataFolder, "/CowDataLad/PA_", as.character(as.Date(date, origin = "1970-01-01"),
format = "%Y%m%d"), "T000000UTC.csv"))
pdf(paste0(outputFolder, "/FA-PA.pdf"), width = 11, height = 6)
opar <- par(mfrow = c(1, 2), mar = c(1,1,1,1))
plotBarn(barn, bRot, axes = FALSE, main = "FA data")
addPoints(FAdata, id, start, end, color = 3, bRot, cex = 1.2)
addBarnFeatures(barn)
# Plot trajectory for a selected cow
plotBarn(barn, axes = FALSE, bText = FALSE, main = "PA data")
data <- PAdata[which(PAdata$id == id), ]
points(data$x, data$y, pch = 19, col = data$activity + 1, cex = 0.5)
addBarnFeatures(barn)
legend("bottomright", legend = c("Unknown", "Standing", "Walking", "In cubicle",
"At feed", "At drinker", "Out def", "Outside"),
title = "Activity", col = c(0, 1, 2, 3, 4, 5, 998, 999) + 1, pch = 19, bg = NA)
par(opar)
dev.off()
png(paste0(outputFolder, "/LadMap.png"), width = 600, height = 1000, res = 200)
opar <- par(mar = c(1,1,1,1))
plotBarn(barn, axes = FALSE, bText = FALSE, main = "", xlim = c(0, 3300), ylim = c(2500, 8000))
addCubicleHeatmap(hm, factor = 1 / 9, bLegend = F)
addBarnFeatures(barn, textSize = 0.8)
par(opar)
dev.off()