Skip to content

Commit

Permalink
Return Result from wasm_fanova_calculate
Browse files Browse the repository at this point in the history
  • Loading branch information
ciffelia committed Sep 30, 2023
1 parent 0869e36 commit 8c48bde
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
21 changes: 13 additions & 8 deletions rustlib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
use fanova::{FanovaOptions, RandomForestOptions};
use js_sys::Array;
use serde_wasm_bindgen::from_value;
use fanova::{FanovaOptions, RandomForestOptions};
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn wasm_fanova_calculate(features: Array, targets: Array) -> Vec<f64> {
// TODO(c-bata): Fix error handling
pub fn wasm_fanova_calculate(features: Array, targets: Array) -> Result<Vec<f64>, JsError> {
let features_vec: Vec<Vec<f64>> = features
.iter()
.map(|x| from_value::<Vec<f64>>(x).unwrap())
.collect();
let targets_vec: Vec<f64> = targets.iter().map(|x| x.as_f64().unwrap()).collect();
.map(|x| from_value::<Vec<f64>>(x))
.collect::<Result<_, _>>()
.map_err(|_| JsError::new("features must be of type number[][]"))?;
let targets_vec: Vec<f64> = targets
.iter()
.map(|x| x.as_f64())
.collect::<Option<_>>()
.ok_or(JsError::new("targets must be of type number[]"))?;

let mut fanova = FanovaOptions::new()
.random_forest(RandomForestOptions::new().seed(0))
.fit(
features_vec.iter().map(|x| x.as_slice()).collect(),
&targets_vec,
)
.unwrap();
.map_err(|e| JsError::new(&format!("failed to build fANOVA model: {}", e)))?;
let importances = (0..features_vec.len())
.map(|i| fanova.quantify_importance(&[i]).mean)
.collect::<Vec<_>>();
return importances;

Ok(importances)
}
1 change: 1 addition & 0 deletions standalone_app/src/components/PlotImportance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const PlotImportance: FC<{ study: Study }> = ({ study }) => {
const values = filteredTrials.map(
(t) => t.values?.[objectiveId] as number
)
// TODO: handle errors thrown by wasm_fanova_calculate
const importance = wasm_fanova_calculate(features, values)
return study.intersection_search_space.map((s, i) => ({
name: s.name,
Expand Down

0 comments on commit 8c48bde

Please sign in to comment.