Skip to content

Commit

Permalink
Fix/improve normal dist block (#238)
Browse files Browse the repository at this point in the history
* encapsulate normal sample in its own function

* add normal dist block to download
  • Loading branch information
kohlros authored Sep 5, 2023
1 parent f5fa7d5 commit b85b2cc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
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

0 comments on commit b85b2cc

Please sign in to comment.