From 05debd13d9a3c13a2a96d02309225946f71cf30b Mon Sep 17 00:00:00 2001 From: georgejr Date: Tue, 2 Jul 2024 17:03:01 +0200 Subject: [PATCH 1/3] add GitHub action --- .Rbuildignore | 1 + .github/.gitignore | 1 + .github/workflows/R-CMD-check.yaml | 62 ++++++++++++++++++++++++++++++ .gitignore | 2 +- DESCRIPTION | 2 + README.md | 4 ++ tests/testthat/test_openASD.R | 14 ++++++- 7 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 .github/.gitignore create mode 100644 .github/workflows/R-CMD-check.yaml diff --git a/.Rbuildignore b/.Rbuildignore index c36cf1b..01b277f 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -1,3 +1,4 @@ .git ^.*\.Rproj$ ^\.Rproj\.user$ +^\.github$ diff --git a/.github/.gitignore b/.github/.gitignore new file mode 100644 index 0000000..2d19fc7 --- /dev/null +++ b/.github/.gitignore @@ -0,0 +1 @@ +*.html diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml new file mode 100644 index 0000000..389ecdd --- /dev/null +++ b/.github/workflows/R-CMD-check.yaml @@ -0,0 +1,62 @@ +# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples +# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help +on: + push: + branches: [main, master] + pull_request: + branches: [main, master] + +name: R-CMD-check + +permissions: read-all + +jobs: + R-CMD-check: + runs-on: ${{ matrix.config.os }} + + name: ${{ matrix.config.os }} (${{ matrix.config.r }}) + + strategy: + fail-fast: false + matrix: + config: + + - {os: ubuntu-latest, r: 'release'} +# - {os: macos-latest, r: 'release'} +# - {os: windows-latest, r: 'release'} +# - {os: ubuntu-latest, r: 'oldrel-1'} +# - {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'} + + env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + R_KEEP_PKG_SOURCE: yes + PKG_SYSREQS: false + + steps: + - uses: actions/checkout@v4 + + - uses: r-lib/actions/setup-pandoc@v2 + + - uses: r-lib/actions/setup-r@v2 + with: + r-version: ${{ matrix.config.r }} + http-user-agent: ${{ matrix.config.http-user-agent }} + use-public-rspm: true + extra-repositories: https://pecanproject.r-universe.dev + + - name: dependencies on Linux + if: runner.os == 'Linux' + run: sudo apt-get install -y make pandoc git libssl-dev libgdal-dev gdal-bin libgeos-dev libproj-dev libsqlite3-dev libicu-dev libudunits2-dev librdf0-dev libxml2-dev libfreetype6-dev libjpeg-dev libpng-dev libtiff-dev libfontconfig1-dev libfribidi-dev libharfbuzz-dev libcurl4-gnutls-dev pkg-config + + + - uses: r-lib/actions/setup-r-dependencies@v2 + with: + extra-packages: any::rcmdcheck, testthat, PEcAnRTM, rjags + needs: check + dependencies: '"hard"' + + - uses: r-lib/actions/check-r-package@v2 + with: + error-on: '"error"' + upload-snapshots: true + build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")' diff --git a/.gitignore b/.gitignore index ffe31f0..492b9c0 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,4 @@ settings.xml~ *.RData *.Rdata - +R-FieldSpectra.Rproj diff --git a/DESCRIPTION b/DESCRIPTION index 7308bae..ad0572f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -27,6 +27,8 @@ Suggests: spectrolab Depends: R (>= 2.10) +Remotes: + meireles/spectrolab NeedsCompilation: no SystemRequirements: OS_type: unix, mac License: GNU GENERAL PUBLIC LICENSE Version 3 diff --git a/README.md b/README.md index e25ee8c..62a49fe 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ + +[![R-CMD-check](https://github.com/georgejr45/R-FieldSpectra/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/georgejr45/R-FieldSpectra/actions/workflows/R-CMD-check.yaml) + + # R-FieldSpectra R library for importing and processing field spectroscopy data collected with ASD, Spectra Vista, and Spectral Evolution instruments diff --git a/tests/testthat/test_openASD.R b/tests/testthat/test_openASD.R index 595ab61..69e974f 100644 --- a/tests/testthat/test_openASD.R +++ b/tests/testthat/test_openASD.R @@ -1,9 +1,19 @@ context("opening spectra files") -test_that("open ASD spectra file", { +test_that("open single ASD spectra file", { file.dir <- system.file("extdata/PM01_TIAM_B_LC_REFL00005.asd",package="FieldSpectra") spec <- read.asd(file.dir, out.dir=file.path('~'), start.wave=350, end.wave=2500, step.size=1) expect_equal(round(sum(spec$Spectra)), 517) -}) \ No newline at end of file +}) + +# test_that("open multiple ASD spectra file", { +# +# file.dir <- system.file("extdata/",package="FieldSpectra") +# spec <- read.asd(file.dir, out.dir=file.path('~'), start.wave=350, end.wave=2500, step.size=1) +# expect_equal(round(sum(spec$Spectra)), 517) +# +# }) +# + From 500383b2cd683c1cfd23aecc35472ad1d83af087 Mon Sep 17 00:00:00 2001 From: georgejr Date: Tue, 20 Aug 2024 10:21:34 +0200 Subject: [PATCH 2/3] Initial support for reading SVC spectral data from sig files --- DESCRIPTION | 2 +- NAMESPACE | 1 + R/read.sig.R | 104 ++++++++++++++++++++++++++++++++++ man/read.sig.Rd | 48 ++++++++++++++++ tests/testthat/test_openSIG.R | 35 ++++++++++++ 5 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 R/read.sig.R create mode 100644 man/read.sig.Rd create mode 100644 tests/testthat/test_openSIG.R diff --git a/DESCRIPTION b/DESCRIPTION index ad0572f..79ec549 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -36,4 +36,4 @@ Copyright: Authors LazyLoad: yes LazyData: FALSE Encoding: UTF-8 -RoxygenNote: 7.1.2 +RoxygenNote: 7.3.2 diff --git a/NAMESPACE b/NAMESPACE index 447e3af..370cca3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -7,5 +7,6 @@ export(convolve.spectra) export(extract.metadata) export(jump.correction) export(read.asd) +export(read.sig) export(settings) export(smooth.spectra) diff --git a/R/read.sig.R b/R/read.sig.R new file mode 100644 index 0000000..d1b5a34 --- /dev/null +++ b/R/read.sig.R @@ -0,0 +1,104 @@ +#--------------------------------------------------------------------------------------------------------------# +##' Function to read .sig files and extract spectral data +##' +##'This function will read ".sig" files and extract the speczral data. The resulting data frame will +##'have the option for all data as well as selected data , which can be obtained by providing the +##'boolean parameter. +##' +##' @name read.sig +##' @title read .sig files from SVC +##' +##' @param path A character string specifying the path to the directory or a single sig file. +##' @param default A logical value indicating whether to include all columns from the `.sig` files +#' (`TRUE`) or only selected columns (`FALSE`). Default is `TRUE`. +#' +##' +##' @return A data data frame with the following columns : +##' \describe{ +##' \item{Wavelengths(nm)}{Numeric vector representing the wavelengths in nanometers.} +##' \item{file}{gives information on the file name} +##' \item{Reflectance(\%)}{Numeric vector of reflectance percentages.} +##' \item{Target Values (optional)}{Provides the targer values} +##' \item{Reference Values (optional)}{Provides information on the reference values} +##' } +##' +##' @details +##' The function can read a single file or multiple files in a directory. +##' The file is then read line by line and the spectral data is extracted from the line after "data=". +##' The extracted data is then converted into a data frame with appropriate names. +##' +##'@examples +##'\dontrun{ +##'path <- "/path/to/your/file.sig" +##'data <- read.sig(path, default = FALSE) +##'print(head(data)) +##'} +##' +##'@export +##' +##'@author Methun George, Steffen Neumann +##' + + + + + +read.sig <- function(path, default = TRUE) { + # Check if the path exists + if (!file.exists(path)) { + stop("The path does not exist: ", path) + } + + # Check if the path is a directory or a single file + path_info <- file.info(path) + if (path_info$isdir) { + sig_files <- list.files(path = path, pattern = "\\.sig$", full.names = TRUE) + } else { + sig_files <- path + } + + # An empty list to store the data frames + all_data <- list() + + # Read each file and then combine the data + for (file in sig_files) { + file_content <- readLines(file) + data_start <- grep("^data=", file_content) + data_lines <- file_content[(data_start + 1):length(file_content)] + data <- read.table(text = data_lines, header = FALSE, fill = TRUE) + + # Add the filename + name_line <- grep("^name=", file_content, value = TRUE) + if (length(name_line) > 0) { + file_name <- sub("^name=", "", name_line) + } else { + file_name <- basename(file) # Default to file name if not found in metadata + } + + if (default) { + # If parameter,'all' is TRUE, include all columns with original column names + data$file_name <- file_name + if (ncol(data) >= 4) { + colnames(data) <- c("Wavelengths(nm)", "Reference Values", "Target Values", "Reflectance(%)", "file_name") + } + } else { + # If 'all' is FALSE, only select the wavelength and reflectance columns + if (ncol(data) >= 4) { + data <- data[, c(1, 4), drop = FALSE] + colnames(data) <- c("Wavelengths(nm)", "Reflectance(%)") + } else { + stop("Data does not have enough columns to extract the required columns!!!") + } + data$file_name <- file_name + } + + # Store the data frame in the list + all_data[[length(all_data) + 1]] <- data + } + + # Combine all data frames + combined_data <- do.call(rbind, all_data) + return(combined_data) +} + + diff --git a/man/read.sig.Rd b/man/read.sig.Rd new file mode 100644 index 0000000..aafbf39 --- /dev/null +++ b/man/read.sig.Rd @@ -0,0 +1,48 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/read.sig.R +\name{read.sig} +\alias{read.sig} +\title{read .sig files from SVC} +\usage{ +read.sig(path, default = TRUE) +} +\arguments{ +\item{path}{A character string specifying the path to the directory or a single sig file.} + +\item{default}{A logical value indicating whether to include all columns from the `.sig` files +(`TRUE`) or only selected columns (`FALSE`). Default is `TRUE`.} +} +\value{ +A data data frame with the following columns : +\describe{ +\item{Wavelengths(nm)}{Numeric vector representing the wavelengths in nanometers.} +\item{file}{gives information on the file name} +\item{Reflectance(\%)}{Numeric vector of reflectance percentages.} +\item{Target Values (optional)}{Provides the targer values} +\item{Reference Values (optional)}{Provides information on the reference values} +} +} +\description{ +Function to read .sig files and extract spectral data +} +\details{ +This function will read ".sig" files and extract the speczral data. The resulting data frame will +have the option for all data as well as selected data , which can be obtained by providing the +boolean parameter. + + +The function can read a single file or multiple files in a directory. +The file is then read line by line and the spectral data is extracted from the line after "data=". +The extracted data is then converted into a data frame with appropriate names. +} +\examples{ +\dontrun{ +path <- "/path/to/your/file.sig" +data <- read.sig(path, default = FALSE) +print(head(data)) +} + +} +\author{ +Methun George, Steffen Neumann +} diff --git a/tests/testthat/test_openSIG.R b/tests/testthat/test_openSIG.R new file mode 100644 index 0000000..5a6bc45 --- /dev/null +++ b/tests/testthat/test_openSIG.R @@ -0,0 +1,35 @@ +# Load the testthat package +library(testthat) +source("/Users/methungeorge/Desktop/nearspectRa/R/read.sig.R") + +# Create test +test_that("read.sig function works correctly with default parameter", { + # Create a temporary directory and file for testing + temp_dir <- tempdir() + temp_file <- file.path(temp_dir, "test.sig") + + # Create a mock .sig file with some sample data + writeLines(c( + "name=test_file.sig", + "data=", + "400 1 2 10", + "500 1 2 20", + "600 1 2 30", + "700 1 2 40" + ), temp_file) + + # Test with default = TRUE (all columns) + data_all <- read.sig(temp_file, default = TRUE) + expect_equal(ncol(data_all), 5) # Should have 5 columns + expect_equal(colnames(data_all), c("Wavelengths(nm)", "Reference Values", "Target Values", "Reflectance(%)", "file_name")) + + # Test with default = FALSE (selected columns) + data_selected <- read.sig(temp_file, default = FALSE) + expect_equal(ncol(data_selected), 3) # Should have 3 columns: Wavelengths(nm), Reflectance(%), file_name + expect_equal(colnames(data_selected), c("Wavelengths(nm)", "Reflectance(%)", "file_name")) + + # Clean up + unlink(temp_file) +}) + + From 60c8e49e742fb3f8f77c08daf9edebf23b18625e Mon Sep 17 00:00:00 2001 From: georgejr Date: Tue, 20 Aug 2024 10:45:56 +0200 Subject: [PATCH 3/3] Fix SVC unit test --- tests/testthat/test_openSIG.R | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/testthat/test_openSIG.R b/tests/testthat/test_openSIG.R index 5a6bc45..f25b58e 100644 --- a/tests/testthat/test_openSIG.R +++ b/tests/testthat/test_openSIG.R @@ -1,7 +1,3 @@ -# Load the testthat package -library(testthat) -source("/Users/methungeorge/Desktop/nearspectRa/R/read.sig.R") - # Create test test_that("read.sig function works correctly with default parameter", { # Create a temporary directory and file for testing @@ -18,13 +14,15 @@ test_that("read.sig function works correctly with default parameter", { "700 1 2 40" ), temp_file) + filepath <- system.file("extdata/gr070214_003.sig",package="FieldSpectra") + # Test with default = TRUE (all columns) - data_all <- read.sig(temp_file, default = TRUE) + data_all <- read.sig(filepath, default = TRUE) expect_equal(ncol(data_all), 5) # Should have 5 columns expect_equal(colnames(data_all), c("Wavelengths(nm)", "Reference Values", "Target Values", "Reflectance(%)", "file_name")) # Test with default = FALSE (selected columns) - data_selected <- read.sig(temp_file, default = FALSE) + data_selected <- read.sig(filepath, default = FALSE) expect_equal(ncol(data_selected), 3) # Should have 3 columns: Wavelengths(nm), Reflectance(%), file_name expect_equal(colnames(data_selected), c("Wavelengths(nm)", "Reflectance(%)", "file_name"))