Learn R Programming

proximetricsR (version 0.6.5)

proxiscout_write_model: Write a calibration model to ProxiScout JSON format

Description

Serializes a model of class spectral_model (including its preprocessing recipe) into a JSON format that can be imported into the NeoSpectra NIR Hub and deployed on ProxiScout sensors (see Details).

Usage

proxiscout_write_model(object, file = NULL)

Value

If file = NULL (default), the JSON string is returned visibly so it can be inspected or assigned to a variable. If file

is specified, the JSON string is written to that file and returned invisibly (i.e. it is not printed to the console, following the standard R convention for functions called primarily for their side effect).

Arguments

object

an object of class spectral_model that contains the preprocessing recipe and final model to be serialized.

file

an optional character string with the path (including file name) where the JSON output should be written. If NULL (default), no file is written and the JSON string is returned. If a path is provided, the JSON is written to that file and returned invisibly.

Author

Leonardo Ramirez-Lopez and Claudio Orellano

Details

The JSON output produced by this function can be imported into the NeoSpectra NIR Hub and used within a ProxiScout application. Once imported, the NeoSpectra Scan mobile app linked to a ProxiScout sensor can access the model and use it to compute and display spectral predictions.

The JSON pipeline always begins with two hardware-specific steps that are added automatically, regardless of the preprocessing recipe in object: (1) scaling raw reflectance from the 0--100 range reported by the sensor to the 0--1 range, and (2) averaging repeated scans of the same sample. These steps precede any user-defined preprocessing.

Constraints and supported preprocessing steps:

  • The first step in the preprocessing recipe of object must be prep_resample, as wavenumber alignment with the ProxiScout hardware grid is required.

  • All predictor wavenumbers in object must match the hardware wavenumbers returned by get_proxiscout_wavenumbers within a tolerance of 0.1 cm^-1cm^-1.

  • prep_derivative and prep_smooth are supported only when algorithm = "savitzky-golay".

  • prep_transform is supported only with to = "absorbance"; using to = "reflectance" generates a warning and the step is skipped in the JSON output.

  • prep_wav_trim is handled implicitly through wavenumber selection and does not produce an explicit JSON step.

See Also

calibrate, get_proxiscout_wavenumbers, prep_resample

Examples

Run this code
# \donttest{
data("NIRcannabis")
control <- calibration_control(
  validation_type = "kfold", number = 3, folds = "sequential"
)
recipe <- preprocess_recipe(
  prep_resample(grid = "proxiscout"),
  prep_snv(),
  prep_derivative(m = 1, w = 11, p = 2, algorithm = "savitzky-golay"),
  device = "proxiscout"
)
model <- calibrate(
  THCA ~ spc,
  data = NIRcannabis, preprocess = recipe,
  method = fit_plsr(10), control = control, verbose = FALSE
)

json_model <- proxiscout_write_model(model)
json_model

proxiscout_write_model(model, file = file.path(tempdir(), "my_model.json"))
# }

Run the code above in your browser using DataLab