Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use TypedArray as parameter? #32

Open
asukaminato0721 opened this issue Mar 8, 2025 · 2 comments
Open

use TypedArray as parameter? #32

asukaminato0721 opened this issue Mar 8, 2025 · 2 comments

Comments

@asukaminato0721
Copy link
Contributor

pub fn new(parameters: Option<Vec<JsNumber>>) -> Self {
let params: [f32; 19] = match parameters {
Some(parameters) => {
let mut array = [0.0; 19];
for (i, value) in parameters.iter().enumerate().take(19) {
array[i] = value.get_double().unwrap_or(0.0) as f32;
}
array

ref https://napi.rs/cn/docs/concepts/typed-array

well, currently the code work, so don't touch it.

@asukaminato0721
Copy link
Contributor Author

@ishiko732

@ishiko732
Copy link
Contributor

If using a TypedArray, I must wrap the parameter with Float32Array

+  pub fn new(parameters: Option<&[f32]>) -> Self {
    let params: Vec<f32> = match parameters {
+    Some(parameters) => parameters.into_iter().map(|p| *p as f32).collect(),
      None => DEFAULT_PARAMETERS.to_vec(),
    };
    Self(Arc::new(Mutex::new(
      fsrs::FSRS::new(Some(&params)).unwrap(),
    )))
  }
  async function computeParametersWrapper(enableShortTerm) {
    // create FSRS instance and optimize
    const fsrs = new FSRS(null)
    const optimizedParameters = await fsrs.computeParameters(
      fsrsItems,
      enableShortTerm,
      progress.bind(null, enableShortTerm),
      1000 /** 1s */,
    )
    console.log(`[enableShortTerm=${enableShortTerm}]optimized parameters:`, optimizedParameters)
-    const model = new FSRS(optimizedParameters) // Error: Get TypedArray info failed
+    const model = new FSRS(Float32Array.from(optimizedParameters))

    const metrics = model.evaluate(fsrsItems)
    console.log(`[enableShortTerm=${enableShortTerm}]metrics:`, metrics)
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants