---
title: "Finding Thresholds"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Finding Thresholds}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
if (utils::packageVersion("knitr") >= "1.20.15") {
  knitr::opts_chunk$set(
    collapse = TRUE,
    comment = "#>",
    fig.width = 7, fig.height = 6,
    tidy = "styler"
  )
} else {
  knitr::opts_chunk$set(
    collapse = TRUE,
    comment = "#>",
    fig.width = 7, fig.height = 6
  )
}
set.seed(1)
```

Most of the examples from this package are centred around thresholding images, but really the core function `auto_thresh()` can be used to find thresholds for any non-negative integer data. 


## Mostly high, some low

Let's create a vector of values, most of which are greater than 50, the rest of which are less than 10:

```{r create-high-low}
x <- c(sample.int(9, 2e5, replace = TRUE), sample(51:99, 8e5, replace = TRUE))
```

Now let's take a look at the distribution of `x`:

```{r plot-high-low, message=FALSE}
library(ggplot2)
library(dplyr)
tibble(x = x) %>%
  ggplot() +
  aes(x) +
  stat_density(bw = 3)
```

If you're trying to threshold this sort of data, you're probably looking for a method which will find a threshold that separates the larger values from the smaller ones. The available automatic thresholding methods are "IJDefault", "Huang", "Huang2", "Intermodes", "IsoData", "Li", "MaxEntropy", "Mean", "MinErrorI", "Minimum", "Moments", "Otsu", "Percentile", "RenyiEntropy", "Shanbhag", "Triangle" and "Yen". These are well demonstrated at https://imagej.net/plugins/auto-threshold. 


## Trying all methods

"MaxEntropy" and "Yen" often fail to find a threshold, so I generally avoid them. Let's try out all the rest.

```{r try-all}
library(autothresholdr)
thresh_methods <- c(
  "IJDefault", "Huang", "Huang2", "Intermodes", "IsoData",
  "Li", "Mean", "MinErrorI", "Minimum", "Moments", "Otsu",
  "Percentile", "RenyiEntropy", "Shanbhag", "Triangle"
)
thresholds <- purrr::map_chr(thresh_methods, ~ auto_thresh(x, .)) %>%
  tibble(method = thresh_methods, threshold = .)
print(thresholds)
```

Now, which of these selected a threshold between 10 and 49?

```{r successes}  
filter(thresholds, threshold >= 10, threshold <= 49)
```

The other methods aren't necessarily _wrong_, they're just more strict or more lax than these ones. For thresholding microscopy images to remove background, my favourite methods are "Huang" and "Triangle" because they are quite conservative in that anything even slightly above background is kept.


## Using one method

```{r tri}
auto_thresh(x, "huang")
auto_thresh(x, "tri")
auto_thresh(x, "otsu")
```


## Using a frequency table

Rather than an array of values, you may have a frequency table. I'll create one here from `x`. 

```{r freq1}
x_freqtab <- x %>% 
  table() %>% 
  as.data.frame() %>% 
  magrittr::set_names(c("value", "n"))
head(x_freqtab)
```

If you have the data in this format (a data frame with column names `value` and `n`) you can use the same function `auto_thresh()`.

```{r freq2}
auto_thresh(x_freqtab, "huang")
```
