From 2b1485fbecbed667076e27ae6f6e2d99e860bb58 Mon Sep 17 00:00:00 2001 From: Quinn Dougherty Date: Wed, 17 Aug 2022 13:04:00 -0700 Subject: [PATCH] wrastling with bindgen on structures --- .prettierignore | 1 + .release-please-manifest.json | 2 +- packages/mc/Cargo.lock | 95 +++++++------ packages/mc/Cargo.nix | 130 +++++++++++------- packages/mc/Cargo.toml | 5 +- packages/mc/src/distribution/bandwidth/mod.rs | 4 +- packages/mc/src/distribution/mod.rs | 2 +- packages/mc/src/distribution/monte_carlo.rs | 32 +++-- packages/mc/src/lib.rs | 11 +- 9 files changed, 176 insertions(+), 106 deletions(-) diff --git a/.prettierignore b/.prettierignore index 8090b3f3..cdbdaa36 100644 --- a/.prettierignore +++ b/.prettierignore @@ -13,3 +13,4 @@ packages/squiggle-lang/.cache/ packages/website/build/ packages/squiggle-lang/src/rescript/Reducer/Reducer_Peggy/Reducer_Peggy_GeneratedParser.js packages/vscode-ext/media/vendor/ +packages/mc/pkg/ diff --git a/.release-please-manifest.json b/.release-please-manifest.json index e8ea53a6..4a46ecda 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -2,7 +2,7 @@ "packages/cli": "0.0.3", "packages/components": "0.3.1", "packages/squiggle-lang": "0.3.0", - "packages/squiggle-mc-cached-wasm": "0.0.1", + "packages/mc": "0.0.1", "packages/vscode-ext": "0.3.1", "packages/website": "0.3.0" } diff --git a/packages/mc/Cargo.lock b/packages/mc/Cargo.lock index 6179efa5..08aec2e1 100644 --- a/packages/mc/Cargo.lock +++ b/packages/mc/Cargo.lock @@ -269,6 +269,12 @@ dependencies = [ "web-sys", ] +[[package]] +name = "itoa" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754" + [[package]] name = "js-sys" version = "0.3.59" @@ -280,12 +286,11 @@ dependencies = [ [[package]] name = "kernel_density" -version = "0.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4650e4bc6bb55f57f16f669ddb3c643c8b2e99a477f2f1fa9162528322d2a1b1" +version = "0.0.2" +source = "git+https://github.com/daithiocrualaoich/kernel_density#98c5fb6f9989d9bb6db19adc134cdbd32512d41e" dependencies = [ - "ordered-float", "rand 0.3.23", + "special-fun", ] [[package]] @@ -327,15 +332,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3" -[[package]] -name = "num-traits" -version = "0.1.43" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31" -dependencies = [ - "num-traits 0.2.15", -] - [[package]] name = "num-traits" version = "0.2.15" @@ -362,16 +358,6 @@ version = "1.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "074864da206b4973b84eb91683020dbefd6a8c3f0f38e054d93954e891935e4e" -[[package]] -name = "ordered-float" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc511538298611a79d5a4ddfbb75315b866d942ed26a00bdc3590795c68b7279" -dependencies = [ - "num-traits 0.1.43", - "unreachable", -] - [[package]] name = "pin-project-lite" version = "0.2.9" @@ -462,6 +448,7 @@ dependencies = [ "quickcheck_macros", "rand 0.8.5", "rand_distr", + "serde", "statistics", "wasm-bindgen", "wasm-bindgen-futures", @@ -544,7 +531,7 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32cb0b9bc82b0a0876c2dd994a7e7a2683d3e7390ca40e6886785ef0c7e3ee31" dependencies = [ - "num-traits 0.2.15", + "num-traits", "rand 0.8.5", ] @@ -574,12 +561,55 @@ version = "0.6.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244" +[[package]] +name = "ryu" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" + [[package]] name = "scoped-tls" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2" +[[package]] +name = "serde" +version = "1.0.143" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53e8e5d5b70924f74ff5c6d64d9a5acd91422117c60f48c4e07855238a254553" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.143" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3d8e8de557aee63c26b85b947f5e59b690d0454c753f3adeb5cd7835ab88391" +dependencies = [ + "proc-macro2 1.0.43", + "quote 1.0.21", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38dd04e3c8279e75b31ef29dbdceebfe5ad89f4d0937213c53f7d49d01b3d5a7" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "special-fun" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cde43d93062bf8040a0d30f83a8b40b2756af55ba048e007ee7c81e02276b81" + [[package]] name = "statistics" version = "0.4.1" @@ -659,21 +689,6 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" -[[package]] -name = "unreachable" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f2ae5ddb18e1c92664717616dd9549dde73f539f01bd7b77c2edb2446bdff91" -dependencies = [ - "void", -] - -[[package]] -name = "void" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" - [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -687,6 +702,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc7652e3f6c4706c8d9cd54832c4a4ccb9b5336e2c3bd154d5cccfbf1c1f5f7d" dependencies = [ "cfg-if 1.0.0", + "serde", + "serde_json", "wasm-bindgen-macro", ] diff --git a/packages/mc/Cargo.nix b/packages/mc/Cargo.nix index 3bf90eef..43f931e3 100644 --- a/packages/mc/Cargo.nix +++ b/packages/mc/Cargo.nix @@ -404,6 +404,13 @@ in }; }); + "registry+https://github.com/rust-lang/crates.io-index".itoa."1.0.3" = overridableMkRustCrate (profileName: rec { + name = "itoa"; + version = "1.0.3"; + registry = "registry+https://github.com/rust-lang/crates.io-index"; + src = fetchCratesIo { inherit name version; sha256 = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754"; }; + }); + "registry+https://github.com/rust-lang/crates.io-index".js-sys."0.3.59" = overridableMkRustCrate (profileName: rec { name = "js-sys"; version = "0.3.59"; @@ -414,14 +421,18 @@ in }; }); - "registry+https://github.com/rust-lang/crates.io-index".kernel_density."0.0.1" = overridableMkRustCrate (profileName: rec { + "git+https://github.com/daithiocrualaoich/kernel_density".kernel_density."0.0.2" = overridableMkRustCrate (profileName: rec { name = "kernel_density"; - version = "0.0.1"; - registry = "registry+https://github.com/rust-lang/crates.io-index"; - src = fetchCratesIo { inherit name version; sha256 = "4650e4bc6bb55f57f16f669ddb3c643c8b2e99a477f2f1fa9162528322d2a1b1"; }; + version = "0.0.2"; + registry = "git+https://github.com/daithiocrualaoich/kernel_density"; + src = fetchCrateGit { + url = https://github.com/daithiocrualaoich/kernel_density; + name = "kernel_density"; + version = "0.0.2"; + rev = "98c5fb6f9989d9bb6db19adc134cdbd32512d41e";}; dependencies = { - ordered_float = rustPackages."registry+https://github.com/rust-lang/crates.io-index".ordered-float."0.2.3" { inherit profileName; }; rand = rustPackages."registry+https://github.com/rust-lang/crates.io-index".rand."0.3.23" { inherit profileName; }; + special_fun = rustPackages."registry+https://github.com/rust-lang/crates.io-index".special-fun."0.1.6" { inherit profileName; }; }; }); @@ -484,25 +495,13 @@ in src = fetchCratesIo { inherit name version; sha256 = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3"; }; }); - "registry+https://github.com/rust-lang/crates.io-index".num-traits."0.1.43" = overridableMkRustCrate (profileName: rec { - name = "num-traits"; - version = "0.1.43"; - registry = "registry+https://github.com/rust-lang/crates.io-index"; - src = fetchCratesIo { inherit name version; sha256 = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31"; }; - dependencies = { - num_traits = rustPackages."registry+https://github.com/rust-lang/crates.io-index".num-traits."0.2.15" { inherit profileName; }; - }; - }); - "registry+https://github.com/rust-lang/crates.io-index".num-traits."0.2.15" = overridableMkRustCrate (profileName: rec { name = "num-traits"; version = "0.2.15"; registry = "registry+https://github.com/rust-lang/crates.io-index"; src = fetchCratesIo { inherit name version; sha256 = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"; }; features = builtins.concatLists [ - [ "default" ] [ "libm" ] - [ "std" ] ]; dependencies = { libm = rustPackages."registry+https://github.com/rust-lang/crates.io-index".libm."0.2.5" { inherit profileName; }; @@ -536,17 +535,6 @@ in ]; }); - "registry+https://github.com/rust-lang/crates.io-index".ordered-float."0.2.3" = overridableMkRustCrate (profileName: rec { - name = "ordered-float"; - version = "0.2.3"; - registry = "registry+https://github.com/rust-lang/crates.io-index"; - src = fetchCratesIo { inherit name version; sha256 = "cc511538298611a79d5a4ddfbb75315b866d942ed26a00bdc3590795c68b7279"; }; - dependencies = { - num_traits = rustPackages."registry+https://github.com/rust-lang/crates.io-index".num-traits."0.1.43" { inherit profileName; }; - unreachable = rustPackages."registry+https://github.com/rust-lang/crates.io-index".unreachable."0.1.1" { inherit profileName; }; - }; - }); - "registry+https://github.com/rust-lang/crates.io-index".pin-project-lite."0.2.9" = overridableMkRustCrate (profileName: rec { name = "pin-project-lite"; version = "0.2.9"; @@ -672,9 +660,10 @@ in cached = rustPackages."registry+https://github.com/rust-lang/crates.io-index".cached."0.38.0" { inherit profileName; }; ${ if false then "console_error_panic_hook" else null } = rustPackages."registry+https://github.com/rust-lang/crates.io-index".console_error_panic_hook."0.1.7" { inherit profileName; }; getrandom = rustPackages."registry+https://github.com/rust-lang/crates.io-index".getrandom."0.2.7" { inherit profileName; }; - kernel_density = rustPackages."registry+https://github.com/rust-lang/crates.io-index".kernel_density."0.0.1" { inherit profileName; }; + kernel_density = rustPackages."git+https://github.com/daithiocrualaoich/kernel_density".kernel_density."0.0.2" { inherit profileName; }; rand = rustPackages."registry+https://github.com/rust-lang/crates.io-index".rand."0.8.5" { inherit profileName; }; rand_distr = rustPackages."registry+https://github.com/rust-lang/crates.io-index".rand_distr."0.4.3" { inherit profileName; }; + serde = rustPackages."registry+https://github.com/rust-lang/crates.io-index".serde."1.0.143" { inherit profileName; }; statistics = rustPackages."registry+https://github.com/rust-lang/crates.io-index".statistics."0.4.1" { inherit profileName; }; wasm_bindgen = rustPackages."registry+https://github.com/rust-lang/crates.io-index".wasm-bindgen."0.2.82" { inherit profileName; }; web_sys = rustPackages."registry+https://github.com/rust-lang/crates.io-index".web-sys."0.3.59" { inherit profileName; }; @@ -847,6 +836,13 @@ in src = fetchCratesIo { inherit name version; sha256 = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244"; }; }); + "registry+https://github.com/rust-lang/crates.io-index".ryu."1.0.11" = overridableMkRustCrate (profileName: rec { + name = "ryu"; + version = "1.0.11"; + registry = "registry+https://github.com/rust-lang/crates.io-index"; + src = fetchCratesIo { inherit name version; sha256 = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09"; }; + }); + "registry+https://github.com/rust-lang/crates.io-index".scoped-tls."1.0.0" = overridableMkRustCrate (profileName: rec { name = "scoped-tls"; version = "1.0.0"; @@ -854,6 +850,60 @@ in src = fetchCratesIo { inherit name version; sha256 = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2"; }; }); + "registry+https://github.com/rust-lang/crates.io-index".serde."1.0.143" = overridableMkRustCrate (profileName: rec { + name = "serde"; + version = "1.0.143"; + registry = "registry+https://github.com/rust-lang/crates.io-index"; + src = fetchCratesIo { inherit name version; sha256 = "53e8e5d5b70924f74ff5c6d64d9a5acd91422117c60f48c4e07855238a254553"; }; + features = builtins.concatLists [ + [ "default" ] + [ "derive" ] + [ "serde_derive" ] + [ "std" ] + ]; + dependencies = { + serde_derive = buildRustPackages."registry+https://github.com/rust-lang/crates.io-index".serde_derive."1.0.143" { profileName = "__noProfile"; }; + }; + }); + + "registry+https://github.com/rust-lang/crates.io-index".serde_derive."1.0.143" = overridableMkRustCrate (profileName: rec { + name = "serde_derive"; + version = "1.0.143"; + registry = "registry+https://github.com/rust-lang/crates.io-index"; + src = fetchCratesIo { inherit name version; sha256 = "d3d8e8de557aee63c26b85b947f5e59b690d0454c753f3adeb5cd7835ab88391"; }; + features = builtins.concatLists [ + [ "default" ] + ]; + dependencies = { + proc_macro2 = rustPackages."registry+https://github.com/rust-lang/crates.io-index".proc-macro2."1.0.43" { inherit profileName; }; + quote = rustPackages."registry+https://github.com/rust-lang/crates.io-index".quote."1.0.21" { inherit profileName; }; + syn = rustPackages."registry+https://github.com/rust-lang/crates.io-index".syn."1.0.99" { inherit profileName; }; + }; + }); + + "registry+https://github.com/rust-lang/crates.io-index".serde_json."1.0.83" = overridableMkRustCrate (profileName: rec { + name = "serde_json"; + version = "1.0.83"; + registry = "registry+https://github.com/rust-lang/crates.io-index"; + src = fetchCratesIo { inherit name version; sha256 = "38dd04e3c8279e75b31ef29dbdceebfe5ad89f4d0937213c53f7d49d01b3d5a7"; }; + features = builtins.concatLists [ + [ "default" ] + [ "std" ] + ]; + dependencies = { + itoa = rustPackages."registry+https://github.com/rust-lang/crates.io-index".itoa."1.0.3" { inherit profileName; }; + ryu = rustPackages."registry+https://github.com/rust-lang/crates.io-index".ryu."1.0.11" { inherit profileName; }; + serde = rustPackages."registry+https://github.com/rust-lang/crates.io-index".serde."1.0.143" { inherit profileName; }; + }; + }); + + "registry+https://github.com/rust-lang/crates.io-index".special-fun."0.1.6" = overridableMkRustCrate (profileName: rec { + name = "special-fun"; + version = "0.1.6"; + registry = "registry+https://github.com/rust-lang/crates.io-index"; + src = fetchCratesIo { inherit name version; sha256 = "5cde43d93062bf8040a0d30f83a8b40b2756af55ba048e007ee7c81e02276b81"; }; + }); + "registry+https://github.com/rust-lang/crates.io-index".statistics."0.4.1" = overridableMkRustCrate (profileName: rec { name = "statistics"; version = "0.4.1"; @@ -971,23 +1021,6 @@ in ]; }); - "registry+https://github.com/rust-lang/crates.io-index".unreachable."0.1.1" = overridableMkRustCrate (profileName: rec { - name = "unreachable"; - version = "0.1.1"; - registry = "registry+https://github.com/rust-lang/crates.io-index"; - src = fetchCratesIo { inherit name version; sha256 = "1f2ae5ddb18e1c92664717616dd9549dde73f539f01bd7b77c2edb2446bdff91"; }; - dependencies = { - void = rustPackages."registry+https://github.com/rust-lang/crates.io-index".void."1.0.2" { inherit profileName; }; - }; - }); - - "registry+https://github.com/rust-lang/crates.io-index".void."1.0.2" = overridableMkRustCrate (profileName: rec { - name = "void"; - version = "1.0.2"; - registry = "registry+https://github.com/rust-lang/crates.io-index"; - src = fetchCratesIo { inherit name version; sha256 = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"; }; - }); - "registry+https://github.com/rust-lang/crates.io-index".wasi."0.11.0+wasi-snapshot-preview1" = overridableMkRustCrate (profileName: rec { name = "wasi"; version = "0.11.0+wasi-snapshot-preview1"; @@ -1006,11 +1039,16 @@ in src = fetchCratesIo { inherit name version; sha256 = "fc7652e3f6c4706c8d9cd54832c4a4ccb9b5336e2c3bd154d5cccfbf1c1f5f7d"; }; features = builtins.concatLists [ [ "default" ] + [ "serde" ] + [ "serde-serialize" ] + [ "serde_json" ] [ "spans" ] [ "std" ] ]; dependencies = { cfg_if = rustPackages."registry+https://github.com/rust-lang/crates.io-index".cfg-if."1.0.0" { inherit profileName; }; + serde = rustPackages."registry+https://github.com/rust-lang/crates.io-index".serde."1.0.143" { inherit profileName; }; + serde_json = rustPackages."registry+https://github.com/rust-lang/crates.io-index".serde_json."1.0.83" { inherit profileName; }; wasm_bindgen_macro = buildRustPackages."registry+https://github.com/rust-lang/crates.io-index".wasm-bindgen-macro."0.2.82" { profileName = "__noProfile"; }; }; }); diff --git a/packages/mc/Cargo.toml b/packages/mc/Cargo.toml index 99e19566..253cf3aa 100644 --- a/packages/mc/Cargo.toml +++ b/packages/mc/Cargo.toml @@ -33,7 +33,8 @@ default = ["wee_alloc"] [dependencies] # The `wasm-bindgen` crate provides the bare minimum functionality needed # to interact with JavaScript. -wasm-bindgen = "0.2.82" +wasm-bindgen = { version = "0.2.82", features = ["serde-serialize"] } +serde = { version = "1", features = ["derive"] } # The `web-sys` crate allows you to interact with the various browser APIs, # like the DOM. @@ -49,7 +50,7 @@ rand = "0.8.5" getrandom = { version = "0.2", features = ["js"] } rand_distr = "0.4.3" statistics = "0.4.1" -kernel_density = "0.0.1" +kernel_density = { git = "https://github.com/daithiocrualaoich/kernel_density" } cached = { version = "0.38.0", features = ["wasm"] } # The `console_error_panic_hook` crate provides better debugging of panics by diff --git a/packages/mc/src/distribution/bandwidth/mod.rs b/packages/mc/src/distribution/bandwidth/mod.rs index 3c42eeed..7dea8dbc 100644 --- a/packages/mc/src/distribution/bandwidth/mod.rs +++ b/packages/mc/src/distribution/bandwidth/mod.rs @@ -34,7 +34,7 @@ pub fn iqr(x: &Vec) -> f64 { /** Silverman, B. W. (1986) Density Estimation. London: Chapman and Hall. */ -pub fn nrd0(x: Vec) -> f64 { +pub fn nrd0(x: &Vec) -> f64 { let _hi = variance(&x).sqrt(); let _lo = min([_hi, iqr(&x) / nrd0_lo_denominator()].to_vec()); let _e = x[1].abs(); @@ -52,7 +52,7 @@ pub fn nrd0(x: Vec) -> f64 { } /** Scott, D. W. (1992) Multivariate Density Estimation: Theory, Practice, and Visualization. Wiley. */ -pub fn nrd(x: Vec) -> f64 { +pub fn nrd(x: &Vec) -> f64 { let h = iqr(&x) / nrd0_lo_denominator(); return nrd_coef() * min([variance(&x).sqrt(), h].to_vec()) * lenf(&x).powf(nrd_fractional_power()); } diff --git a/packages/mc/src/distribution/mod.rs b/packages/mc/src/distribution/mod.rs index e463be7a..06aa9067 100644 --- a/packages/mc/src/distribution/mod.rs +++ b/packages/mc/src/distribution/mod.rs @@ -4,4 +4,4 @@ pub mod monte_carlo; pub use magic_numbers::bandwidth::Parameters as BandwidthParameters; pub use magic_numbers::environment::Parameters as EnvironmentParameters; -pub use monte_carlo::sampleN; +pub use monte_carlo::sample_n; diff --git a/packages/mc/src/distribution/monte_carlo.rs b/packages/mc/src/distribution/monte_carlo.rs index 4a205dc9..ef625fe9 100644 --- a/packages/mc/src/distribution/monte_carlo.rs +++ b/packages/mc/src/distribution/monte_carlo.rs @@ -1,32 +1,36 @@ -extern crate kernel_density; - +use wasm_bindgen::prelude::*; use rand::Rng; use rand::thread_rng; -// use kernel_density::kde; -// use crate::distribution::bandwidth::{nrd0, nrd}; +use kernel_density::kde; +use serde::Serialize; +use crate::distribution::bandwidth::{nrd0, nrd}; // TODO: impl a constructor such that xs.len() == ys.len(). +#[wasm_bindgen] +#[derive(Serialize)] pub struct PdfCurve { xs: Vec, ys: Vec, } -// pub fn samples_to_continuous_pdf(samples: Vec, bandwidth: f64) -> PdfCurve { -// let pdf = kde::normal(&samples, bandwidth); -// return PdfCurve { -// xs: samples, -// ys: samples.map(|x| pdf.density(x)), -// }; -// } +pub fn samples_to_continuous_pdf(samples: Vec, bandwidth: f64) -> JsValue { + let samples_to_map = samples.clone(); + let pdf = kde::normal(&samples, bandwidth); + let pdf_curve = PdfCurve { + xs: samples, + ys: samples_to_map.iter().map(|x| pdf.density(*x)).collect(), + }; + JsValue::from_serde::(&pdf_curve).unwrap() +} -// pub fn getCurve(xs: Vec, n: u64) -> Vec { -// let bandwidth = nrd(xs); +// pub fn get_curve(samples: Vec, n: u64) -> Vec { +// let bandwidth = nrd(&samples); // let curve = samples_to_continuous_pdf(samples, bandwidth); // return curve.ys; // } -pub fn sampleN(xs: Vec, n: i32) -> Vec { +pub fn sample_n(xs: Vec, n: i32) -> Vec { let m = xs.len(); let mut output = vec![]; for _ in 0..n { diff --git a/packages/mc/src/lib.rs b/packages/mc/src/lib.rs index e10df11a..201a2559 100644 --- a/packages/mc/src/lib.rs +++ b/packages/mc/src/lib.rs @@ -17,5 +17,14 @@ pub fn sample_n(samples: Box<[f32]>, num_samples: i32) -> Vec { console_error_panic_hook::set_once(); let samples_vec = Vec::from(samples); - monte_carlo::sampleN(samples_vec, num_samples) + return monte_carlo::sample_n(samples_vec, num_samples); +} + +#[wasm_bindgen] +pub fn samples_to_continuous_pdf(samples: Box<[f64]>, bandwidth: f64) -> JsValue { + #[cfg(debug_assertions)] + console_error_panic_hook::set_once(); + + let samples_vec = Vec::from(samples); + monte_carlo::samples_to_continuous_pdf(samples_vec, bandwidth) }