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

Fix/improve normal dist block #238

Merged
merged 4 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions static/scripts/modules/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ async function downloadWorkspaceAsJS() {
let jszip = await readFile("./scripts/jszip.js");
let fileutils = await prepareFileUtils();
let plotHandler = await preparePlotting();
let gaussian = await prepareGaussian();
// Check if everything worked out
if (
![
Expand Down Expand Up @@ -63,6 +64,7 @@ async function downloadWorkspaceAsJS() {
zip.file("PlotHandler.mjs", plotHandler);
zip.folder("modules");
zip.file("modules/fileUtils.mjs", fileutils);
zip.file("gaussian.js", gaussian);
let zipFile = await zip.generateAsync({ type: "blob" });
saveFile(zipFile, "elea.zip");
}
Expand Down Expand Up @@ -102,6 +104,7 @@ function prepareAlgorithm() {
`const {parentPort, Worker} = require("worker_threads");\n` +
`const {Handler, consolelog, saveInCSV, plot, consoleerror, Message, RecvRequest} = require("./MessageHandler.js");\n` +
`const {cpus} = require("os");\n` +
`const {gaussian} = require("./gaussian.js");\n` +
`Handler.setParentPort(parentPort);\n`;

let js = getCode();
Expand Down Expand Up @@ -162,6 +165,13 @@ async function preparePlotting() {
return code;
}

async function prepareGaussian() {
let file, code;
if (!(file = await readFile("./scripts/gaussian.js"))) return false;
code = file + `module.exports = {\n` + `gaussian \n` + `};`;
return code;
}

function removeFirstLine(file) {
let lines = file.split("\n");
lines.shift();
Expand Down
28 changes: 19 additions & 9 deletions static/scripts/normalBlockBehaviour.js
Original file line number Diff line number Diff line change
Expand Up @@ -595,21 +595,31 @@ Blockly.JavaScript["sample_normal_positive"] = function (block) {
let variableMean = Blockly.JavaScript.valueToCode(
block,
"mean",
Blockly.JavaScript.ORDER_ATOMIC,
Blockly.JavaScript.ORDER_ATOMIC
);
let variableVariance = Blockly.JavaScript.valueToCode(
block,
"variance",
Blockly.JavaScript.ORDER_ATOMIC,
Blockly.JavaScript.ORDER_ATOMIC
);

let code = `(function() {
const distribution = gaussian(${variableMean}, ${variableVariance});
let sample = 0;
while (sample <= 0) sample = Math.round(distribution.ppf(Math.random()));
return sample;
})()`;
return [code, Blockly.JavaScript.ORDER_NONE];
var functionName = Blockly.JavaScript.provideFunction_(
"sampleNormalPositive",
[
"function " + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + "() {",
" const distribution = gaussian(" +
variableMean +
"," +
variableVariance +
");",
" let sample = 0;",
" while (sample <= 0) sample = Math.round(distribution.ppf(Math.random()));",
" return sample;",
"}",
]
);
var code = functionName + "()";
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
};

Blockly.JavaScript["ea_debug_all"] = function () {
Expand Down
Loading