aphalo / photobiology

Package ‘photobiology’ defines a system of classes for storing spectral data and accompanying methods and operators. This is the core of a suite of R packages for photobiological calculations.
4 stars 1 forks source link

fscale + autoplot mixup ? #28

Closed CdeMills closed 8 months ago

CdeMills commented 9 months ago

Hello

the following code:

myciev2.spct <- as_tibble(read.table("CIE_xyz_1931_2deg.csv", sep=","))
colnames(myciev2.spct) <- c("w.length", "xbar", "ybar", "zbar")
myciev2.spct <- myciev2.spct %>% filter(w.length <= 780)
myciev2.spct <- response_spct(w.length=myciev2.spct$w.length,
                              s.e.response = myciev2.spct$ybar)
myciev2.spct <- fscale(myciev2.spct, f="total", set.scaled=T)
allspecs <- response_mspct(list(CIEv2=myciev2.spct, photobio=ciev2.spct))
autoplot(allspecs, annotations=list(c("color.guide", "peaks")))

generates the warning:

In normalize_spct(spct = q2e(x, action = "replace.raw"), range = range, : Normalization not updated: action not supported for objects created with 'photobiology' (<= 0.10.9).

Which can be traced to spct.normalize.r, line 753:

753     if (norm == "update") {
 754       if (!has.normalization.metadata) {
 755         warning("Normalization not updated: action not supported for objects created with 'photobiology' (<= 0.10.9).")
 756         return(spct)
 757       } else {
 758         norm <- old.normalization.ls$norm.type
 759         if (norm == "wavelength") {
 760           norm <- old.normalization.ls$norm.wl
 761         }
 762       }
 763     }

Clearly, there is some discrepancy between "normalize" and fscale, as cie2.spct is normalized; while mycie2.spct is scaled. The generated image place both maxima at "1"; meaning mycie2.spct scaling is ignored. Any hint ?

Regards

Pascal

aphalo commented 9 months ago

Dear Pascal, Thanks for raising this issue. Indeed there is a bug affecting response_spct methods. I will try to fix this bug by the end of the day. I am sorry about this!

Meanwhile, below is a comparison between the source_spct methods, working as expected, and the response_spct method failing. The errors messages are different, I think, because I am using the latest version 0.11.1.

library(ggspectra)
#> Loading required package: photobiology
#> News at https://www.r4photobiology.info/
#> Loading required package: ggplot2
library(photobiology)

autoplot(sun.spct)

autoplot(fscale(sun.spct, f = e_irrad, target = 10))

autoplot(fscale(normalise(sun.spct), f = e_irrad, target = 10))


autoplot(ciev2.spct)

autoplot(fscale(ciev2.spct, f = e_response, target = 10))

autoplot(fscale(normalise(ciev2.spct), f = e_response, target = 10))

Created on 2024-02-28 with reprex v2.1.0

aphalo commented 9 months ago

The source of the problem is in the autoplot() method in 'ggspectra' . The reason for the apparent bug is a badly chosen default argument for parameter norm in the autoplot() method for response_spct'. Addingnorm = "skip"` to the call solves the problem, at least with the most recent version of 'photobiology' (0.11.1 now in CRAN).

(The error "Normalization not updated: action not supported for objects created with 'photobiology' (<= 0.10.9)." was caused in the previous version by a version mismatch between the data object and the function.)

library(ggspectra)
#> Loading required package: photobiology
#> News at https://www.r4photobiology.info/
#> Loading required package: ggplot2
library(photobiology)

autoplot(ciev2.spct, norm = "skip")

autoplot(fscale(ciev2.spct, f = e_response, target = 10), norm = "skip")

autoplot(fscale(normalise(ciev2.spct), f = e_response, target = 10), norm = "skip")

Created on 2024-02-28 with reprex v2.1.0

aphalo commented 8 months ago

This is fixed in the under development version.