Skip to content

Commit

Permalink
Flip xy coordinates in ENVI files (#156)
Browse files Browse the repository at this point in the history
* Test flip
* Update tests

---------

Co-authored-by: Win Cowger, PhD <[email protected]>
  • Loading branch information
zsteinmetz and wincowgerDEV authored Oct 31, 2023
1 parent 90e780c commit 3812966
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
4 changes: 2 additions & 2 deletions R/read_envi.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ read_envi <- function(file, header = NULL, share = NULL,
arr <- read.ENVI(file, header)
dt <- as.data.table(arr)
md <- hdr[names(hdr) != "wavelength"]
names(dt) <- c("x", "y", "z", "value")
names(dt) <- c("y", "x", "z", "value")
dt[, 1:2] <- dt[, 1:2] -1

os <- as_OpenSpecy(x = hdr$wavelength,
spectra = dcast(dt, z ~ x + y)[, -1],
spectra = dcast(dt, z ~ y + x)[, -1],
metadata = c(metadata, md),
coords = dt[, 1:2] |> unique(),
session_id = T)
Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-adj_range.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ test_that("flatten_range() function test", {
expect_false(all.equal(flat_map$spectra, tiny_map$spectra) |> isTRUE())
expect_equal(flat_map$spectra[1:20], tiny_map$spectra[1:20])

expect_equal(flat_map$spectra[40:60, 1:5] |> unique() |> round(4)
|> as.numeric(),
c(-0.8694, -1.246, -0.8304, -1.1909, -0.7857))
flat_map$spectra[40:60, 1:5] |> unique() |> round(2) |> as.numeric() |>
expect_equal(c(-0.87, -1.25, -0.83, -1.19, -0.79))
})
46 changes: 29 additions & 17 deletions tests/testthat/test-def_features.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,27 @@ test_that("collapse_spec() handles input errors correctly", {
test_that("features are identified when given logical", {
map$metadata$particles <- map$metadata$y == 0
id_map <- def_features(map, map$metadata$particles)
expect_true(check_OpenSpecy(id_map))
expect_length(unique(id_map$metadata$feature_id), 2)
expect_equal(max(id_map$metadata$area, na.rm = T), 13)
expect_equal(max(id_map$metadata$feret_max, na.rm = T), 13)
expect_equal(max(id_map$metadata$feret_min, na.rm = T), 1)
expect_equal(max(id_map$metadata$perimeter, na.rm = T), 24)
check_OpenSpecy(id_map) |> expect_true()
unique(id_map$metadata$feature_id) |> expect_length(2)
max(id_map$metadata$area, na.rm = T) |> expect_equal(16)
max(id_map$metadata$feret_max, na.rm = T) |> round(2) |>
expect_equal(13.04)
max(id_map$metadata$feret_min, na.rm = T) |> round(2) |>
expect_equal(1.23)
max(id_map$metadata$perimeter, na.rm = T) |> round(2) |>
expect_equal(25.05)
})

test_that("particles are identified when given character", {
map$metadata$particles <- ifelse(map$metadata$y == 1, "particle", "not_particle")
id_map <- def_features(map, map$metadata$particles)
expect_true(check_OpenSpecy(id_map))
expect_length(unique(id_map$metadata$feature_id), 3)
expect_equal(max(id_map$metadata$area, na.rm = T), 182)
expect_equal(round(max(id_map$metadata$feret_max, na.rm = T)), 19)
unique(id_map$metadata$feature_id) |>
expect_length(3)
max(id_map$metadata$area, na.rm = T) |>
expect_equal(176)
max(id_map$metadata$feret_max, na.rm = T) |> round(2) |>
expect_equal(18.69)
})

test_that("an error is thrown for invalid feature input", {
Expand Down Expand Up @@ -72,20 +78,26 @@ test_that("collapse particles returns expected values", {

expect_true(check_OpenSpecy(test_collapsed))

expect_equal(test_collapsed$metadata |> nrow(), 3)
expect_equal(test_collapsed$metadata$feret_max |> round(2), c(13, 13, 18.69))
expect_equal(test_collapsed$metadata$centroid_x |> unique(), 6)
test_collapsed$metadata |> nrow() |>
expect_equal(3)
test_collapsed$metadata$feret_max |> round(2) |>
expect_equal(c(13.04, 13.04, 18.69))
test_collapsed$metadata$centroid_x |> unique() |>
expect_equal(7.5)

particles <- map$metadata$y == 1
id_map <- def_features(map, particles)
expect_true(check_OpenSpecy(id_map))

test_collapsed <- collapse_spec(id_map)
expect_true(check_OpenSpecy(test_collapsed))

expect_equal(test_collapsed$metadata |> nrow(), 2)
expect_equal(test_collapsed$metadata$feret_max |> round(2), c(NA, 13))
expect_equal(test_collapsed$metadata$centroid_x |> unique(), 6)
check_OpenSpecy(test_collapsed) |> expect_true()

test_collapsed$metadata |> nrow() |>
expect_equal(2)
test_collapsed$metadata$feret_max |> round(2) |>
expect_equal(c(NA, 13.04))
test_collapsed$metadata$centroid_x |> unique() |>
expect_equal(7.5)

expect_contains(names(test_collapsed$metadata),
c("feature_id", "area", "feret_max", "centroid_y",
Expand Down

0 comments on commit 3812966

Please sign in to comment.