-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_Shapes_Load Data Functions.R
60 lines (48 loc) · 1.86 KB
/
3_Shapes_Load Data Functions.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
#this file loads the packages and creates the functions that will be used in the model
library(tidyverse)
library(png)
# This function loads images of animals.
# ... These images are big, so it will scale to the specified size
# ... Want all images to be square, so pad the short size with 0s
import_phylo <- function(phylo_name, facing = "L", side = 100){
phylo_raw <- readPNG(paste0("PhyloPic/", phylo_name, ".png"))
phylo <- phylo_raw[, , 4] #Only need the transparency matrix
phylo[phylo == min(phylo)] <- 0 #Some images don't have a complete 0 background
#Reduce resolution to a smaller matrix
width.to.height = nrow(phylo) / ncol(phylo)
divisor <- ceiling(max(ncol(phylo), nrow(phylo))/side)
phylo2 <- phylo %>%
as.tibble() %>%
mutate(y = row_number()) %>%
mutate(y = max(y)-y +1) %>%
select(y, everything()) %>%
gather(x, value, 2:ncol(.)) %>%
mutate(x = as.numeric(substr(x, 2, 8))) %>%
group_by(x = x %/% divisor, y= y %/% divisor) %>%
summarize(value = mean(value)) %>%
ungroup() %>%
do( #Want all animals facing left for now
if(facing == "R"){
mutate(., x = max(x) - x + 1)
} else {.}
) %>%
filter(value > 0) %>%
spread(x, value, fill = 0) %>%
select(-y) %>%
as.matrix()
if(nrow(phylo2) < ncol(phylo2)){
phylo2 <- rbind(phylo2, matrix(0, nrow = (side - nrow(phylo2)), ncol = ncol(phylo2)))
}
if(nrow(phylo2) > ncol(phylo2)){
phylo2 <- cbind(phylo2, matrix(0, nrow = nrow(phylo2), ncol = (side - ncol(phylo2))))
}
return(phylo2)
}
test <- import_phylo("Balaur", "L", side=80)
ceiling(test)
Matrix::image(-t(test))
kerasaur_list <- readr::read_csv("DatasaurList.csv")
train_ksaur <- kerasaur_list$Fauna %>%
map2(kerasaur_list$Direction,
import_phylo)
train_ksaur2 <- array(unlist(train_ksaur), dim = c(length(train_ksaur), 100, 100))