---
title: "Data Summary"
output: html_document
---

```{r, echo=F, message=F}
library(png)
library(ggplot2)
library(dplyr)

train = read.table("data/HOwt/HOwt_transfer_train.txt", header=F, as.is=T)
test = read.table("data/HOwt/HOwt_transfer_test.txt", header=F, as.is=T)
val = read.table("data/HOwt/HOwt_transfer_val.txt", header=F, as.is=T)

label_doc = read.table("data/HOwt/HOwt_transfer_doc.txt", as.is=T, sep=";", header=T)

names(train) = c("src", "label")
names(test) = c("src", "label")
names(val) = c("src", "label")

for(i in 0:3){
  train$label[train$label == i] = label_doc$label[i + 1]
  test$label[test$label == i] = label_doc$label[i + 1]
  val$label[val$label == i] = label_doc$label[i + 1]
}

extract_code = function(x){
  # input: "plate01/001002000-0-5-1.jpg"
  # output: "plate01/001002000"
  return(strsplit(x, "-")[[1]][1])
}

train$code = sapply(train$src, extract_code)
test$code = sapply(test$src, extract_code)
val$code = sapply(val$src, extract_code)
```

### Class frequencies

```{r, echo=F, fig.width=10}
title = paste0("Training data (", nrow(train) , " images total)\n")
p1 = ggplot(train, aes(x=label)) +
  geom_histogram(fill="#53cfff") +
  theme_light(base_size=20) +
  xlab("") +
  ylab("count") + 
  ggtitle(title) +
  theme(plot.title=element_text(size=18),
        axis.text.x = element_text(angle = 90, hjust = 1))

title = paste0("Validation data (", nrow(val) , " images total)\n")
p2 = ggplot(val, aes(x=label)) +
  geom_histogram(fill="#53cfff") +
  theme_light(base_size=20) +
  xlab("") +
  ylab("count") + 
  ggtitle(title) +
  theme(plot.title=element_text(size=18),
        axis.text.x = element_text(angle = 90, hjust = 1))

title = paste0("Testing data (", nrow(test) , " images total)\n")
p3 = ggplot(test, aes(x=label)) +
  geom_histogram(fill="#53cfff") +
  theme_light(base_size=20) +
  xlab("") +
  ylab("count") + 
  ggtitle(title) +
  theme(plot.title=element_text(size=18),
        axis.text.x = element_text(angle = 90, hjust = 1))

print(p1)
print(p2)
print(p3)
```

```{r, echo=F, eval=F}
freq_tbl = cbind(table(train$label),
                 table(test$label),
                 table(val$label))
colnames(freq_tbl) = c("train", "test", "val")
rownames(freq_tbl) = names(table(train$label))
kable(freq_tbl)
```

### Protein counts per class

Each protein was imaged four times.

```{r, echo=F}
f_train = group_by(train, label) %>%
  summarise(count_train = length(unique(code)))
f_test = group_by(test, label) %>%
  summarise(count_test = length(unique(code)))
f_val = group_by(val, label) %>%
  summarise(count_val = length(unique(code)))

f = left_join(f_train, f_val, by="label")
f = left_join(f, f_test, by="label")
colnames(f) = c("Class", "Training images", "Validation images", "Test images")
kable(f)
```


### Sanity check for class overlap

```{r, echo=F}
print(paste0("# test imgs in training set: ", sum(test$src %in% train$src)))
print(paste0("# val imgs in training set: ", sum(val$src %in% train$src)))
print(paste0("# test imgs in validation set: ", sum(test$src %in% val$src)))

```


### Class examples (from training set)

```{r, echo=F, fig.width=10, fig.height=2}
DATA_PATH = "/storage/hpc_tanel/images-png-merged-patches/HOwt/"
set.seed(1235)

for(i in 1:4){
  class = label_doc$label[i]
  sub = train[train$label == class, ]
  sample_src = sample(sub$src, 20)
  
  print(class)
  par(mfrow=c(2, 10), mar=c(0.1, 0.1, 0.1, 0.1)/10)
  for(j in 1:length(sample_src)){
    path = paste0(DATA_PATH, sample_src[j])
    img = readPNG(path, native=TRUE)
    #plot(0, 0, type="n", ann=FALSE, axes=FALSE, asp=1)
    plot.new()
    rasterImage(img,-0.03,-0.03,1.03,1.03,interpolate=FALSE)
    #rasterImage(img,0,0,1,1)
  }
}
```

### Scaled class examples (from training set)

```{r, echo=F, fig.width=10, fig.height=2}
DATA_PATH = "/storage/hpc_tanel/images-png-merged-patches-scaled/HOwt/"
set.seed(1235)

for(i in 1:4){
  class = label_doc$label[i]
  sub = train[train$label == class, ]
  sample_src = sample(sub$src, 20)
  
  print(class)
  par(mfrow=c(2, 10), mar=c(0.1, 0.1, 0.1, 0.1)/10)
  for(j in 1:length(sample_src)){
    path = paste0(DATA_PATH, sample_src[j])
    img = readPNG(path, native=TRUE)
    #plot(0, 0, type="n", ann=FALSE, axes=FALSE, asp=1)
    plot.new()
    rasterImage(img,-0.03,-0.03,1.03,1.03,interpolate=FALSE)
    #rasterImage(img,0,0,1,1)
  }
}
```

