Skip to content

Instantly share code, notes, and snippets.

@MahShaaban
Last active September 24, 2018 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MahShaaban/3f075e00237896b40e2bb09cc77f298b to your computer and use it in GitHub Desktop.
Save MahShaaban/3f075e00237896b40e2bb09cc77f298b to your computer and use it in GitHub Desktop.
Reproduce the output of colocr using only imager.
library(imager)
# download data
if(!file.exists('./stats.csv')) {
download.file('https://raw.githubusercontent.com/MahShaaban/colocr/master/inst/colocr_app/stats_18.09.02_05.15.01.csv',
destfile = './stats.csv')
}
if(!file.exists('./inputs.csv')) {
download.file('https://raw.githubusercontent.com/MahShaaban/colocr/master/inst/colocr_app/inputs_18.09.02_05.15.08.csv',
destfile = './inputs.csv')
}
if(!file.exists('./Image0001_.jpg')) {
download.file('https://github.com/MahShaaban/colocr/blob/master/inst/extdata/Image0001_.jpg?raw=true',
destfile = './Image0001_.jpg')
}
if(!file.exists('./Image0003_.jpg')) {
download.file('https://github.com/MahShaaban/colocr/blob/master/inst/extdata/Image0003_.jpg?raw=true',
destfile = './Image0003_.jpg')
}
# read input and output from the app
stats <- read.csv('./stats.csv')
inputs <- read.csv('./inputs.csv')
# read images
lst <- as.list(inputs)
num <- list()
for(i in 1:2) {
fl <- paste0('./', lst$image[i])
img <- load.image(fl)
# transform to gray scale
img.g <- grayscale(img)
# apply threshold
img.t <- threshold(img.g, paste0(lst$threshold[i], '%'))
# change to pixset
px <- as.pixset(1-img.t)
# apply shrink
px.m <- shrink(px, lst$shrink[i])
# apply grow
px.m <- grow(px.m, lst$grow[i])
# apply fill
px.m <- fill(px.m, lst$fill[i])
# apply clean
px <- clean(px.m, lst$clean[i])
# add labels
px.labs <- label(px, tolerance = lst$tolerance[i])
value <- as.data.frame(px.labs)$value
ids <- reorder(value, value, length)
k <- levels(ids)
k <- k[(length(k)-1):(length(k)-lst$roi_num[i])]
new.ids <- ifelse(value %in% as.numeric(k), value, 0)
f <- as.numeric(factor(new.ids))
new.px <- as.data.frame(px.labs)
new.px$value <- f - 1
new.px <- as.cimg(new.px, dim = dim(px))
labs <- as.data.frame(new.px)$value
# extract first two channels
img1.g <- channel(img, ind = 1)
img2.g <- channel(img, ind = 2)
# subset and change images to numeric
img1.num <- as.numeric(img1.g[as.pixset(new.px)])
img2.num <- as.numeric(img2.g[as.pixset(new.px)])
num[[i]] <- list(img1.num,
img2.num,
labs[labs != 0])
}
# test pcc
corr <- list()
for(i in 1:2) {
lab <- (num[[i]][[3]])
res <- list()
for(j in unique(lab)) {
c1 <- num[[i]][[1]][lab == j]
c2 <- num[[i]][[2]][lab == j]
rx <- c1 - mean(c1)
gx <- c2 - mean(c2)
nom <- sum(rx * gx)
den <- sqrt(sum(rx**2) * sum(gx**2))
res[[j]] <-nom/den
}
corr[[i]] <- res
}
print(
all.equal(round(stats$pcc, 5), round(unlist(corr), 5))
)
# test moc
corr <- list()
for(i in 1:2) {
lab <- (num[[i]][[3]])
res <- list()
for(j in unique(lab)) {
c1 <- num[[i]][[1]][lab == j]
c2 <- num[[i]][[2]][lab == j]
nom <- sum(c1 * c2)
den <- sqrt(sum(c1**2) * sum(c2**2))
res[[j]] <- nom/den
}
corr[[i]] <- res
}
print(
all.equal(round(stats$moc, 5), round(unlist(corr), 5))
)
unlink('./*.csv')
unlink('./*.jpg')
@maelle
Copy link

maelle commented Sep 24, 2018

library(imager)
#> Loading required package: magrittr
#> 
#> Attaching package: 'imager'
#> The following object is masked from 'package:magrittr':
#> 
#>     add
#> The following objects are masked from 'package:stats':
#> 
#>     convolve, spectrum
#> The following object is masked from 'package:graphics':
#> 
#>     frame
#> The following object is masked from 'package:base':
#> 
#>     save.image

# download data
if(!file.exists('./stats.csv')) {
  download.file('https://raw.githubusercontent.com/MahShaaban/colocr/master/inst/colocr_app/stats_18.09.02_05.15.01.csv',
                destfile = './stats.csv')
}
if(!file.exists('./inputs.csv')) {
  download.file('https://raw.githubusercontent.com/MahShaaban/colocr/master/inst/colocr_app/inputs_18.09.02_05.15.08.csv',
                destfile = './inputs.csv')
}
if(!file.exists('./Image0001_.jpg')) {
  download.file('https://github.com/MahShaaban/colocr/blob/master/inst/extdata/Image0001_.jpg?raw=true',
                destfile = './Image0001_.jpg')
}
if(!file.exists('./Image0003_.jpg')) {
  download.file('https://github.com/MahShaaban/colocr/blob/master/inst/extdata/Image0003_.jpg?raw=true',
                destfile = './Image0003_.jpg')
}

  # read input and output from the app
  stats <- read.csv('./stats.csv')
  inputs <- read.csv('./inputs.csv')

  # read images
  lst <- as.list(inputs)
  num <- list()
  for(i in 1:2) {
    fl <- paste0('./', lst$image[i])
    img <- load.image(fl)

    # transform to gray scale
    img.g <- grayscale(img)

    # apply threshold
    img.t <- threshold(img.g, paste0(lst$threshold[i], '%'))

    # change to pixset
    px <- as.pixset(1-img.t)

    # apply shrink
    px.m <- shrink(px, lst$shrink[i])

    # apply grow
    px.m <- grow(px.m, lst$grow[i])

    # apply fill
    px.m <- fill(px.m, lst$fill[i])

    # apply clean
    px <- clean(px.m, lst$clean[i])

    # add labels
    px.labs <- label(px, tolerance = lst$tolerance[i])
    value <- as.data.frame(px.labs)$value

    ids <- reorder(value, value, length)

    k <- levels(ids)

    k <- k[(length(k)-1):(length(k)-lst$roi_num[i])]

    new.ids <- ifelse(value %in% as.numeric(k), value, 0)
    f <- as.numeric(factor(new.ids))

    new.px <- as.data.frame(px.labs)
    new.px$value <- f - 1
    new.px <- as.cimg(new.px, dim = dim(px))
    labs <- as.data.frame(new.px)$value

    # extract first two channels
    img1.g <- channel(img, ind = 1)
    img2.g <- channel(img, ind = 2)

    # subset and change images to numeric
    img1.num <- as.numeric(img1.g[as.pixset(new.px)])
    img2.num <- as.numeric(img2.g[as.pixset(new.px)])
    num[[i]] <- list(img1.num,
                     img2.num,
                     labs[labs != 0])
  }

  # test pcc
  corr <- list()
  for(i in 1:2) {
    lab <- (num[[i]][[3]])
    res <- list()
    for(j in unique(lab)) {
      c1 <- num[[i]][[1]][lab == j]
      c2 <- num[[i]][[2]][lab == j]

      rx <- c1 - mean(c1)
      gx <- c2 - mean(c2)

      nom <- sum(rx * gx)
      den <- sqrt(sum(rx**2) * sum(gx**2))

      res[[j]] <-nom/den
    }
    corr[[i]] <- res
  }
  print(
    all.equal(round(stats$pcc, 5), round(unlist(corr), 5))
  )
#> [1] "Numeric: lengths (5, 3) differ"

  # test moc
  corr <- list()
  for(i in 1:2) {
    lab <- (num[[i]][[3]])
    res <- list()
    for(j in unique(lab)) {
      c1 <- num[[i]][[1]][lab == j]
      c2 <- num[[i]][[2]][lab == j]

      nom <- sum(c1 * c2)
      den <- sqrt(sum(c1**2) * sum(c2**2))

      res[[j]] <- nom/den
    }
    corr[[i]] <- res
  }
  
  print(
    all.equal(round(stats$moc, 5), round(unlist(corr), 5))
  )
#> [1] "Numeric: lengths (5, 3) differ"
unlink('./*.csv')
unlink('./*.jpg')

Created on 2018-09-24 by the reprex package (v0.2.0).

Session info
devtools::session_info()
#> Session info -------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.5.0 (2018-04-23)
#>  system   x86_64, mingw32             
#>  ui       RTerm                       
#>  language (EN)                        
#>  collate  English_United States.1252  
#>  tz       Europe/Paris                
#>  date     2018-09-24
#> Packages -----------------------------------------------------------------
#>  package    * version date       source        
#>  backports    1.1.2   2017-12-13 CRAN (R 3.5.0)
#>  base       * 3.5.0   2018-04-23 local         
#>  bmp          0.3     2017-09-11 CRAN (R 3.5.0)
#>  compiler     3.5.0   2018-04-23 local         
#>  datasets   * 3.5.0   2018-04-23 local         
#>  devtools     1.13.6  2018-06-27 CRAN (R 3.5.1)
#>  digest       0.6.17  2018-09-12 CRAN (R 3.5.1)
#>  evaluate     0.11    2018-07-17 CRAN (R 3.5.1)
#>  graphics   * 3.5.0   2018-04-23 local         
#>  grDevices  * 3.5.0   2018-04-23 local         
#>  htmltools    0.3.6   2017-04-28 CRAN (R 3.5.1)
#>  igraph       1.2.2   2018-07-27 CRAN (R 3.5.1)
#>  imager     * 0.41.1  2018-05-30 CRAN (R 3.5.0)
#>  jpeg         0.1-8   2014-01-23 CRAN (R 3.5.0)
#>  knitr        1.20    2018-02-20 CRAN (R 3.5.0)
#>  magrittr   * 1.5     2014-11-22 CRAN (R 3.5.0)
#>  memoise      1.1.0   2017-04-21 CRAN (R 3.5.0)
#>  methods    * 3.5.0   2018-04-23 local         
#>  pkgconfig    2.0.2   2018-08-16 CRAN (R 3.5.1)
#>  plyr         1.8.4   2016-06-08 CRAN (R 3.5.0)
#>  png          0.1-7   2013-12-03 CRAN (R 3.5.0)
#>  purrr        0.2.5   2018-05-29 CRAN (R 3.5.0)
#>  Rcpp         0.12.18 2018-07-23 CRAN (R 3.5.0)
#>  readbitmap   0.1-4   2014-09-05 CRAN (R 3.4.3)
#>  rlang        0.2.2   2018-08-16 CRAN (R 3.5.1)
#>  rmarkdown    1.10    2018-06-11 CRAN (R 3.5.0)
#>  rprojroot    1.3-2   2018-01-03 CRAN (R 3.4.3)
#>  stats      * 3.5.0   2018-04-23 local         
#>  stringi      1.2.4   2018-07-23 local         
#>  stringr      1.3.1   2018-05-10 CRAN (R 3.5.0)
#>  tools        3.5.0   2018-04-23 local         
#>  utils      * 3.5.0   2018-04-23 local         
#>  withr        2.1.2   2018-03-15 CRAN (R 3.4.4)
#>  yaml         2.2.0   2018-07-25 CRAN (R 3.5.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment