Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

features/75-WITec-unittest #82

Merged
merged 9 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 38 additions & 67 deletions R/read_dat_WITec_.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,86 +42,57 @@ read_dat_WITec <- function(filex = stop("filename or connection needed"),
# Unit tests -----------------------------------------------------------------

hySpc.testthat::test(read_dat_WITec) <- function() {
local_edition(2)

context("read_dat_WITec")

tmpdir <- paste0(tempdir(), "/test_Witec_dat")
on.exit(unlink(tmpdir, force = TRUE))

untar("testfiles_Witec.tar.gz",
files = c(
"Witec-Map-x.dat",
"Witec-Map-y.dat",
"Witec-timeseries-x.dat",
"Witec-timeseries-y.dat"
),
exdir = tmpdir
local_edition(3)

filename <- system.file(
"extdata",
"txt.Witec/Witec-timeseries-x.dat",
package = "hySpc.read.txt"
)

test_that("-y file guessing", {
spc <- read_dat_WITec(paste0(tmpdir, "/Witec-timeseries-x.dat"))
spc$filename <- gsub("^.*/", "", spc$filename)
expect_known_hash(spc, "1977fe5997")
})
expect_silent(spc <- read_dat_WITec(filename))

test_that("encoding", {
spc <- read_dat_WITec(
paste0(tmpdir, "/Witec-timeseries-x.dat"),
encoding = "ascii"
)
spc$filename <- gsub("^.*/", "", spc$filename)
n_wl <- nwl(spc)
n_rows <- nrow(spc)
n_clos <- ncol(spc)

expect_known_hash(spc, "1977fe5997")
test_that("WITec .dat: hyperSpec obj. dimensions are correct", {
expect_equal(n_wl, 1024)
expect_equal(n_rows, 100)
expect_equal(n_clos, 2)
})

test_that("Time series", {
spc <- read_dat_WITec(
paste0(tmpdir, "/Witec-timeseries-x.dat"),
paste0(tmpdir, "/Witec-timeseries-y.dat")
)
spc$filename <- gsub("^.*/", "", spc$filename)
test_that("WITec .dat: extra data are correct", {
# @data colnames
expect_equal(colnames(spc), c("spc", "filename"))
})

expect_known_hash(spc, "1977fe5997")
test_that("WITec .dat: labels are correct", {
expect_equal(spc@label$.wavelength, NULL)
expect_equal(spc@label$spc, NULL)
expect_equal(spc@label$filename, "filename")
})

test_that("Map: .dat does not have spatial information", {
spc <- read_dat_WITec(
paste0(tmpdir, "/Witec-Map-x.dat"),
paste0(tmpdir, "/Witec-Map-y.dat")
)
expect_null(spc$x)
expect_null(spc$y)
spc$filename <- gsub("^.*/", "", spc$filename)
test_that("WITec .dat: spectra are correct", {
# Dimensions of spectra matrix (@data$spc)
expect_equal(dim(spc@data$spc), c(100, 1024))

# Column names of spectra matrix
expect_equal(colnames(spc@data$spc)[1], "87.7367")
expect_equal(colnames(spc@data$spc)[10], "117.333")
expect_equal(colnames(spc@data$spc)[n_wl], "2821.31") # last name

expect_known_hash(spc, "b523735004")
# Values of spectra matrix
expect_equal(unname(spc@data$spc[1, 1]), 995)
expect_equal(unname(spc@data$spc[2, 10]), 1002)
expect_equal(unname(spc@data$spc[n_rows, n_wl]), 985) # last spc value
})

test_that("Map", {
expect_warning(
read_dat_WITec(
paste0(tmpdir, "/Witec-Map-x.dat"),
paste0(tmpdir, "/Witec-Map-y.dat"),
points.per.line = 5, lines.per.image = 5
),
"points.per.line != 1 given for single spectrum"
)
expect_warning(
read_dat_WITec(
paste0(tmpdir, "/Witec-Map-x.dat"),
paste0(tmpdir, "/Witec-Map-y.dat"),
points.per.line = 5, lines.per.image = 5
),
"lines.per.image != 1 are defined for single spectrum"
)

spc <- read_dat_WITec(
paste0(tmpdir, "/Witec-Map-x.dat"),
paste0(tmpdir, "/Witec-Map-y.dat"),
type = "map", points.per.line = 5, lines.per.image = 5
)
spc$filename <- gsub("^.*/", "", spc$filename)

expect_known_hash(spc, "efc28c0d45")
test_that("WITec .dat: wavelengths are correct", {
expect_equal(spc@wavelength[1], 87.736672)
expect_equal(spc@wavelength[10], 117.332683)
expect_equal(spc@wavelength[n_wl], 2821.3062)
})
}
245 changes: 151 additions & 94 deletions R/read_txt_Graph_WITec_.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,115 +60,172 @@ read_txt_WITec_Graph <- function(headerfile = stop("filename or connection neede
# Unit tests -----------------------------------------------------------------

hySpc.testthat::test(read_txt_WITec_Graph) <- function() {
local_edition(2)

context("read_txt_WITec_Graph")

tmpdir <- paste0(tempdir(), "/test_Witec_txt_Graph")
untar("testfiles_Witec.tar.gz",
files = c(
"nofilename (Header).txt",
"nofilename (X-Axis).txt",
"nofilename (Y-Axis).txt",
"timeseries3x_GraphASCII.Data 1 (Header).txt",
"timeseries3x_GraphASCII.Data 1 (X-Axis).txt",
"timeseries3x_GraphASCII.Data 1 (Y-Axis).txt",
"image2x3_GraphASCII.Data 1_F (Header).txt",
"image2x3_GraphASCII.Data 1_F (X-Axis).txt",
"image2x3_GraphASCII.Data 1_F (Y-Axis).txt"
),
exdir = tmpdir
)

on.exit(unlink(tmpdir))
local_edition(3)

test_that("defaults and (X-Axis)/(Y-Axis) file guessing", {
spc <- read_txt_WITec_Graph(
paste0(tmpdir, "/timeseries3x_GraphASCII.Data 1 (Header).txt")
)

expect_equal(dim(spc), c(nrow = 3L, ncol = 4L, nwl = 1024L))

expect_equal(
spc$filename,
rep(paste0(tmpdir, "/timeseries3x_GraphASCII.Data 1 (Y-Axis).txt"), 3)
)
filename <- system.file(
"extdata",
"txt.Witec/nofilename_Header.txt",
package = "hySpc.read.txt"
)

expect_equal(
spc$WIPname,
rep("F:\\Acetamidophenol samples_20200703.wip", 3)
)
expect_silent(spc <- read_txt_WITec_Graph(filename))

expect_equal(
spc$spcname,
rep("4Acetamidophenol_timeseries3x_20200703_001_Spec.Data 1", 3)
)
n_wl <- nwl(spc)
n_rows <- nrow(spc)
n_clos <- ncol(spc)

expect_equivalent(spc[[2, , 1650]], 2427)
test_that("WITec Graph .txt: hyperSpec obj. dimensions are correct", {
expect_equal(n_wl, 63)
expect_equal(n_rows, 5)
expect_equal(n_clos, 2)
})

test_that("encoding", {
spc <- read_txt_WITec_Graph(
paste0(tmpdir, "/nofilename (Header).txt"),
encoding = "latin1"
)
spc$filename <- gsub("^.*/", "", spc$filename)

expect_known_hash(spc, "b273847b9c")
test_that("WITec Graph .txt: extra data are correct", {
# @data colnames
expect_equal(colnames(spc), c("spc", "filename"))
})

test_that("Time Series", {
spc <- read_txt_WITec_Graph(
paste0(tmpdir, "/timeseries3x_GraphASCII.Data 1 (Header).txt"),
type = "single"
)
spc$filename <- gsub("^.*/", "", spc$filename)

expect_known_hash(spc, "a5ff28fc8b")
test_that("WITec Graph .txt: labels are correct", {
expect_equal(spc@label$.wavelength, NULL)
expect_equal(spc@label$spc, NULL)
expect_equal(spc@label$filename, "filename")
})

test_that("Map", {
expect_warning(
read_txt_WITec_Graph(
paste0(tmpdir, "/image2x3_GraphASCII.Data 1_F (Header).txt"),
encoding = "latin1"
),
"header provides spatial information in y direction for single spectra"
)

expect_warning(
read_txt_WITec_Graph(
paste0(tmpdir, "/image2x3_GraphASCII.Data 1_F (Header).txt"),
encoding = "latin1", type = "single"
),
"header provides spatial information in y direction for single spectra"
)

spc <- read_txt_WITec_Graph(
paste0(tmpdir, "/image2x3_GraphASCII.Data 1_F (Header).txt"),
type = "map", encoding = "latin1"
)
spc$filename <- gsub("^.*/", "", spc$filename)

expect_known_hash(spc, "1e92447516")
})
test_that("WITec Graph .txt: spectra are correct", {
# Dimensions of spectra matrix (@data$spc)
expect_equal(dim(spc@data$spc), c(5, 63))

test_that("missing filename", {
spc <- read_txt_WITec_Graph(paste0(tmpdir, "/nofilename (Header).txt"),
encoding = "latin1"
)
spc$filename <- gsub("^.*/", "", spc$filename)
# Column names of spectra matrix
expect_equal(colnames(spc@data$spc)[1], "161.408")
expect_equal(colnames(spc@data$spc)[10], "200.184")
expect_equal(colnames(spc@data$spc)[n_wl], "423.651") # last name

expect_known_hash(spc, "b273847b9c")
# Values of spectra matrix
expect_equal(unname(spc@data$spc[1, 1]), 3404)
expect_equal(unname(spc@data$spc[2, 10]), 3405)
expect_equal(unname(spc@data$spc[n_rows, n_wl]), 3415) # last spc value
})

test_that("wrong combination of file names", {
expect_error(
read_txt_WITec_Graph(
paste0(tmpdir, "/timeseries3x_GraphASCII.Data 1 (Header).txt"),
paste0(tmpdir, "/timeseries3x_GraphASCII.Data 1 (Y-Axis).txt")
),
"length of wavelength axis .* differs from 'SizeGraph' in header"
)
test_that("WITec Graph .txt: wavelengths are correct", {
expect_equal(spc@wavelength[1], 161.40845)
expect_equal(spc@wavelength[10], 200.18387)
expect_equal(spc@wavelength[n_wl], 423.65106)
})
}


# hySpc.testthat::test(read_txt_WITec_Graph) <- function() {
# local_edition(2)
#
# context("read_txt_WITec_Graph")
#
# tmpdir <- paste0(tempdir(), "/test_Witec_txt_Graph")
# untar("testfiles_Witec.tar.gz",
# files = c(
# "nofilename (Header).txt",
# "nofilename (X-Axis).txt",
# "nofilename (Y-Axis).txt",
# "timeseries3x_GraphASCII.Data 1 (Header).txt",
# "timeseries3x_GraphASCII.Data 1 (X-Axis).txt",
# "timeseries3x_GraphASCII.Data 1 (Y-Axis).txt",
# "image2x3_GraphASCII.Data 1_F (Header).txt",
# "image2x3_GraphASCII.Data 1_F (X-Axis).txt",
# "image2x3_GraphASCII.Data 1_F (Y-Axis).txt"
# ),
# exdir = tmpdir
# )
#
# on.exit(unlink(tmpdir))
#
# test_that("defaults and (X-Axis)/(Y-Axis) file guessing", {
# spc <- read_txt_WITec_Graph(
# paste0(tmpdir, "/timeseries3x_GraphASCII.Data 1 (Header).txt")
# )
#
# expect_equal(dim(spc), c(nrow = 3L, ncol = 4L, nwl = 1024L))
#
# expect_equal(
# spc$filename,
# rep(paste0(tmpdir, "/timeseries3x_GraphASCII.Data 1 (Y-Axis).txt"), 3)
# )
#
# expect_equal(
# spc$WIPname,
# rep("F:\\Acetamidophenol samples_20200703.wip", 3)
# )
#
# expect_equal(
# spc$spcname,
# rep("4Acetamidophenol_timeseries3x_20200703_001_Spec.Data 1", 3)
# )
#
# expect_equivalent(spc[[2, , 1650]], 2427)
# })
#
# test_that("encoding", {
# spc <- read_txt_WITec_Graph(
# paste0(tmpdir, "/nofilename (Header).txt"),
# encoding = "latin1"
# )
# spc$filename <- gsub("^.*/", "", spc$filename)
#
# expect_known_hash(spc, "b273847b9c")
# })
#
# test_that("Time Series", {
# spc <- read_txt_WITec_Graph(
# paste0(tmpdir, "/timeseries3x_GraphASCII.Data 1 (Header).txt"),
# type = "single"
# )
# spc$filename <- gsub("^.*/", "", spc$filename)
#
# expect_known_hash(spc, "a5ff28fc8b")
# })
#
# test_that("Map", {
# expect_warning(
# read_txt_WITec_Graph(
# paste0(tmpdir, "/image2x3_GraphASCII.Data 1_F (Header).txt"),
# encoding = "latin1"
# ),
# "header provides spatial information in y direction for single spectra"
# )
#
# expect_warning(
# read_txt_WITec_Graph(
# paste0(tmpdir, "/image2x3_GraphASCII.Data 1_F (Header).txt"),
# encoding = "latin1", type = "single"
# ),
# "header provides spatial information in y direction for single spectra"
# )
#
# spc <- read_txt_WITec_Graph(
# paste0(tmpdir, "/image2x3_GraphASCII.Data 1_F (Header).txt"),
# type = "map", encoding = "latin1"
# )
# spc$filename <- gsub("^.*/", "", spc$filename)
#
# expect_known_hash(spc, "1e92447516")
# })
#
# test_that("missing filename", {
# spc <- read_txt_WITec_Graph(paste0(tmpdir, "/nofilename (Header).txt"),
# encoding = "latin1"
# )
# spc$filename <- gsub("^.*/", "", spc$filename)
#
# expect_known_hash(spc, "b273847b9c")
# })
#
# test_that("wrong combination of file names", {
# expect_error(
# read_txt_WITec_Graph(
# paste0(tmpdir, "/timeseries3x_GraphASCII.Data 1 (Header).txt"),
# paste0(tmpdir, "/timeseries3x_GraphASCII.Data 1 (Y-Axis).txt")
# ),
# "length of wavelength axis .* differs from 'SizeGraph' in header"
# )
# })
# }
Loading