Sort AFM Images

tutorial
AFM
code
Sorting AFM images quickly from RStudio
Author

Thomas Gredig

Published

November 20, 2024

This code helps you sort AFM images and categorize them into high- and low-quality images for analysis later on. You can run this code within RStudio. Make sure that the nanoAFMr package is installed. The user is prompted for a path that contains AFM images, then each AFM image is displayed and the user can rate it high, medium or low according to its quality.

The result is saved in out.csv file.

library(nanoAFMr)
p = NULL
while(is.null(p)) {
  cat("Please enter the file path with AFM image (searches recursively: ")
  # Read the user input
  p <- readline()
  if(!dir.exists(p)) p=NULL
}

file_list = c(dir(p, pattern="tiff$", full.names = TRUE, recursive = TRUE),
              dir(p, pattern = "ibw$", full.names = TRUE, recursive = TRUE),
              dir(p, pattern = "nid$", full.names = TRUE, recursive = TRUE))

cat("Found",length(file_list),"AFM files.\n\n")
cat("Enter Q to quit.\n\n")

r=data.frame()
for(f in file_list) {
  a = AFM.import(f)
  print(a)
  a <- AFM.flatten(a)
  print(plot(a))
  cat("Enter rating (Q=quit, 1=high,2=mid,3=low:")
  rating <- readline()
  if(rating=="q" || rating=="Q") break
  rating = as.numeric(rating)
  
  # set default rating if needed
  if (is.na(rating) || rating == "") rating <- 3
  r = rbind(r,
            data.frame(
              filename = f,
              rating = rating
            ))
}
write.csv(r, "out.csv", row.names = FALSE)
cat("Ratings saved in file out.csv and variable r.\n")

Citation

BibTeX citation:
@misc{gredig2024,
  author = {Gredig, Thomas and Gredig, Thomas},
  title = {Sort {AFM} {Images}},
  date = {2024-11-20},
  url = {https://www.csulb.edu/~tgredig/posts/sortAFM_images/sortAFM_images.html},
  langid = {en}
}
For attribution, please cite this work as:
Gredig, Thomas, and Thomas Gredig. 2024. “Sort AFM Images.” https://www.csulb.edu/~tgredig/posts/sortAFM_images/sortAFM_images.html.