Skip to content

linting, new funs, exception handling, mixed test cases

Andrew Schwenke requested to merge linting into main

Exception handling:

  • New function EC_check_computed
    • added after biomod2::BIOMOD_Modelling calls
    • biomod2 has internal error handling, which means model failures are silent
    • Therefore, this function checks the model object, and if all models failed the experiment will stop with a warning.
    • This allows early termination of the experiment rather than an error appearing later on
  • New function EC_check_new_levels
    • Discovered in GLM using mixed input
    • This is a type of edge case handling for categorical layers
    • When projecting with categorical layers, new levels (in the raster), that weren't used to fit the model, can cause an error
    • EC_check_new_levels will check the levels of the model object and the raster, and drop levels from the raster if needed
    • Only called within the GLM model for now

New function EC_plot_response:

  • Custom response plots, which removes dependency on the biomod2 bm_PlotResponseCurves function, which was not working for categorical predictors
  • EC_plot_response uses default mean for continuous predictors and mode (terra::modal) for categorical predictors, to fix the n-1 predictor values
  • Creates bar charts for categorical predictor response and line for continuous
  • Continuous includes rug plots of pres/abs
  • Adapted from the plotResponse.R function in the SDMtune package for our workflow and to work with biomod2 model objects
  • All cals to EC_plot_response now have a tryCatch
    • The advantage of this is that a failure in generating this plot will not disrupt the experiment
    • However the resilience of this function outside of local scope remains to be seen so attention to failed plots should be monitored so the function can be improved

Function changes:

  • EC_save_pdf -> EC_save_png
  • Documenting internal functions
  • Circles model:
    • temp override to sub-sample 1000 points (if n > 1000)
    • trying to run circles with large number of points (e.g. 40k) crashes due to memory or takes an extremely long time (even on prod)
    • investigate better output plots for circles in progress

Testing changes:

  • Some test cases updated and extended (e.g. EC_save: new test: able to save objects, import of saved .rds file works and is identical)
  • Test case for deprecated functions removed
  • Mixed input test cases added for all most models (not including sptraits, CC as these are still in prog with other changes)
    • Mixed input is 1 x categorical and 1 x continuous raster, using reset_json_paths_mixed function
    • Mixed input useful for finding minor bugs that weren't present in other cases
  • test-EC_modelling_gbm with multi species input changed:
    • This would fail or pass randomly due to small spatial distribution of test species "Nigroporus vinosus"
    • Added test species Litoria jungguy to the MSDM_dataset.csv to use for this test, which has wider distribution
    • modelling region also updated to be smaller (QLD focused) to match the distribution of L. jungguy

Other:

  • Categorical outputs change re: ticket https://griffith.atlassian.net/browse/ECO-2454
    • range change raster (produced in CC function) will now output as categorical with metadata (.xml)
    • this will allow front end to render with correct legend etc once handling is added for this
    • scope to add this to other categorical raster outputs (TBD)

Merge request reports