rmaia / pavo

tools for the analysis of color data in R
http://pavo.colrverse.com
GNU General Public License v2.0
69 stars 17 forks source link

Make colspace() work when columns are not in expected order #192

Open Bisaloo opened 4 years ago

Bisaloo commented 4 years ago
library(pavo)
#> Welcome to pavo 2! Take a look at the latest features (and update your bibliography) in our recent publication: Maia R., Gruson H., Endler J. A., White T. E. (2019) pavo 2: new tools for the spectral and spatial analysis of colour in R. Methods in Ecology and Evolution, 10, 1097-1107.
data(flowers)

di <- sensmodel(c(440, 330))
names(di) <- c("wl", "l", "s")

di_vis <- as.data.frame(vismodel(flowers, visual = di))

head(di_vis)
#>                               l            s lum
#> Goodenia_heterophylla 0.9730821 0.0269178704  NA
#> Goodenia_geniculata   0.9507922 0.0492078038  NA
#> Goodenia_gracilis     0.9247331 0.0752668919  NA
#> Xyris_operculata      0.9997937 0.0002062707  NA
#> Eucalyptus_sp         0.9796303 0.0203697463  NA
#> Faradaya_splendida    0.8829416 0.1170583601  NA

head(colspace(di_vis))
#> Warning: Input data is not a "vismodel" object *and* has more than two columns;
#> treating the first two columns as standardized quantum catch for "s", and "l"
#> receptors, respectively
#>                               s            l          x     r.vec lum
#> Goodenia_heterophylla 0.9730821 0.0269178704 -0.6690392 0.6690392  NA
#> Goodenia_geniculata   0.9507922 0.0492078038 -0.6375164 0.6375164  NA
#> Goodenia_gracilis     0.9247331 0.0752668919 -0.6006633 0.6006633  NA
#> Xyris_operculata      0.9997937 0.0002062707 -0.7068151 0.7068151  NA
#> Eucalyptus_sp         0.9796303 0.0203697463 -0.6782996 0.6782996  NA
#> Faradaya_splendida    0.8829416 0.1170583601 -0.5415613 0.5415613  NA

Created on 2020-02-18 by the reprex package (v0.3.0)

In the colspace object, the s and l columns are swapped. This is consistent with the warning but it would be nice if pavo could work with this out of the box.

thomased commented 4 years ago

Yeah I started to chip away at this as part of a more general attempt at #102, but got tangled up. I suspect it'd be simplest to first refactor out all those data-checks that are duplicated across the colspace functions (hexagon, trispace etc.), then just fix it in the one spot. That change is simple enough — we just need to be smarter about checking the structure of the data — but might take a little effort to get there.