Simple optional functions

This commit is contained in:
Ozzie Gooen 2020-08-07 14:24:15 +01:00
parent 04d606d9c6
commit 6887da59b3
5 changed files with 73 additions and 43 deletions

View File

@ -149,7 +149,8 @@ module DemoDist = {
sampleCount: Some(options.sampleCount), sampleCount: Some(options.sampleCount),
outputXYPoints: Some(options.outputXYPoints), outputXYPoints: Some(options.outputXYPoints),
kernelWidth: options.kernelWidth, kernelWidth: options.kernelWidth,
shapeLength: Some(options.downsampleTo |> E.O.default(1000)) shapeLength:
Some(options.downsampleTo |> E.O.default(1000)),
}, },
~distPlusIngredients, ~distPlusIngredients,
~environment= ~environment=
@ -160,12 +161,22 @@ module DemoDist = {
let response1 = DistPlusRenderer.run2(inputs1); let response1 = DistPlusRenderer.run2(inputs1);
switch (response1) { switch (response1) {
| (Ok(`DistPlus(distPlus1))) => | Ok(`DistPlus(distPlus1)) =>
<DistPlusPlot distPlus={DistPlus.T.normalize(distPlus1)} />
| Ok(`Function(f, a)) =>
let result1 = DistPlusRenderer.runFunction(inputs1, (f, a), [|`SymbolicDist(`Float(5.0))|],);
let result2 = DistPlusRenderer.runFunction(inputs1, (f, a), [|`SymbolicDist(`Float(20.0))|],);
let result3 = DistPlusRenderer.runFunction(inputs1, (f, a), [|`SymbolicDist(`Float(40.0))|],);
switch (result1, result2, result3) {
| (Ok(`DistPlus(distPlus1)),Ok(`DistPlus(distPlus2)),Ok(`DistPlus(distPlus3))) =>
<> <>
<DistPlusPlot distPlus={DistPlus.T.normalize(distPlus1)} /> <DistPlusPlot distPlus={DistPlus.T.normalize(distPlus1)} />
<DistPlusPlot distPlus={DistPlus.T.normalize(distPlus2)} />
<DistPlusPlot distPlus={DistPlus.T.normalize(distPlus3)} />
</> </>
| (Ok(`Function(f,a))) => "Function!!!" |> R.ste | _ => "Failure " |> R.ste
| (Error(r)) => r |> R.ste };
| Error(r) => r |> R.ste
}; };
| _ => | _ =>
"Nothing to show. Try to change the distribution description." "Nothing to show. Try to change the distribution description."
@ -329,10 +340,7 @@ let make = () => {
<Antd.Form onSubmit> <Antd.Form onSubmit>
<Row _type=`flex className=Styles.rows> <Row _type=`flex className=Styles.rows>
<Col span=24> <Col span=24>
<FieldText <FieldText field=FormConfig.GuesstimatorString label="Program" />
field=FormConfig.GuesstimatorString
label="Program"
/>
</Col> </Col>
</Row> </Row>
<Row _type=`flex className=Styles.rows> <Row _type=`flex className=Styles.rows>

View File

@ -104,7 +104,7 @@ let reducer = (state: state, action: action) =>
let init = { let init = {
showStats: false, showStats: false,
showParams: false, showParams: false,
showPercentiles: true, showPercentiles: false,
distributions: [ distributions: [
{yLog: false, xLog: false, isCumulative: false, height: 1}, {yLog: false, xLog: false, isCumulative: false, height: 1},
], ],

View File

@ -1,13 +1,10 @@
open ExpressionTypes.ExpressionTree; open ExpressionTypes.ExpressionTree;
let toLeaf = (samplingInputs, environment, node: node) => { let envs = (samplingInputs, environment) => {
node {samplingInputs, environment, evaluateNode: ExpressionTreeEvaluator.toLeaf};
|> ExpressionTreeEvaluator.toLeaf({
samplingInputs,
environment,
evaluateNode: ExpressionTreeEvaluator.toLeaf,
});
}; };
let toLeaf = (samplingInputs, environment, node: node) =>
ExpressionTreeEvaluator.toLeaf(envs(samplingInputs, environment), node);
let rec toString: node => string = let rec toString: node => string =
fun fun
@ -27,7 +24,7 @@ let rec toString: node => string =
| `Render(t) => toString(t) | `Render(t) => toString(t)
| `Symbol(t) => "Symbol: " ++ t | `Symbol(t) => "Symbol: " ++ t
| `FunctionCall(name, args) => | `FunctionCall(name, args) =>
"[Fuction call: (" "[Function call: ("
++ name ++ name
++ (args |> E.A.fmap(toString) |> Js.String.concatMany(_, ",")) ++ (args |> E.A.fmap(toString) |> Js.String.concatMany(_, ","))
++ ")]" ++ ")]"
@ -37,16 +34,6 @@ let rec toString: node => string =
++ toString(internal) ++ toString(internal)
++ ")]"; ++ ")]";
let toLeaf = (samplingInputs, environment, node: node) => {
switch(toLeaf(samplingInputs, environment, node)){
| Ok(`SymbolicDist(n)) => `Render(`SymbolicDist(n)) |> toLeaf(samplingInputs, environment);
| Ok(`Function(n)) => Ok(`Function(n))
| Ok(`RenderedDist(n)) => `Normalize(`RenderedDist(n))|> toLeaf(samplingInputs, environment)
| Error(e) => Error(e)
| _ => Error("Wrong type returned")
}
}
let toShape = (samplingInputs, environment, node: node) => { let toShape = (samplingInputs, environment, node: node) => {
switch (toLeaf(samplingInputs, environment, node)) { switch (toLeaf(samplingInputs, environment, node)) {
| Ok(`RenderedDist(shape)) => Ok(shape) | Ok(`RenderedDist(shape)) => Ok(shape)
@ -54,3 +41,8 @@ let toShape = (samplingInputs, environment, node: node) => {
| Error(e) => Error(e) | Error(e) => Error(e)
}; };
}; };
let runFunction = (samplingInputs, environment, inputs, fn: PTypes.Function.t) => {
let params = envs(samplingInputs, environment);
PTypes.Function.run(params, inputs, fn)
}

View File

@ -31,7 +31,7 @@ module Function = {
}; };
evaluationParams.evaluateNode(newEvaluationParams, internals(t)); evaluationParams.evaluateNode(newEvaluationParams, internals(t));
} else { } else {
Error("Failure"); Error("Wrong number of variables");
}; };
}; };

View File

@ -123,10 +123,7 @@ module Internals = {
let inputsToLeaf = (inputs: inputs) => { let inputsToLeaf = (inputs: inputs) => {
MathJsParser.fromString(inputs.guesstimatorString) MathJsParser.fromString(inputs.guesstimatorString)
|> E.R.bind(_, g => runProgram(inputs, g)) |> E.R.bind(_, g => runProgram(inputs, g))
|> E.R.bind(_, r => |> E.R.bind(_, r => E.A.last(r) |> E.O.toResult("No rendered lines"));
E.A.last(r)
|> E.O.toResult("No rendered lines")
);
}; };
let outputToDistPlus = (inputs: Inputs.inputs, shape: DistTypes.shape) => { let outputToDistPlus = (inputs: Inputs.inputs, shape: DistTypes.shape) => {
@ -144,21 +141,54 @@ let run = (inputs: Inputs.inputs) => {
inputs inputs
|> Internals.distPlusRenderInputsToInputs |> Internals.distPlusRenderInputsToInputs
|> Internals.inputsToLeaf |> Internals.inputsToLeaf
|> E.R.bind(_,r => switch(r){ |> E.R.bind(_, r =>
| `RenderedDist(n) => Ok(n) switch (r) {
| _ => Error("Didn't output renderedDist") | `RenderedDist(n) => Ok(n)
}) | _ => Error("Didn't output renderedDist")
}
)
|> E.R.fmap(Internals.outputToDistPlus(inputs)); |> E.R.fmap(Internals.outputToDistPlus(inputs));
}; };
let run2 = (inputs: Inputs.inputs) => { let run2 = (inputs: Inputs.inputs) => {
inputs inputs
|> Internals.distPlusRenderInputsToInputs |> Internals.distPlusRenderInputsToInputs
|> Internals.inputsToLeaf |> Internals.inputsToLeaf
|> E.R.bind(_,r => switch(r){ |> E.R.bind(_, r =>
| `RenderedDist(n) => Ok(`DistPlus(Internals.outputToDistPlus(inputs,n))) switch (r) {
| `Function(n) => Ok(`Function(n)) | `RenderedDist(n) =>
| _ => Error("Didn't output renderedDist") Ok(`DistPlus(Internals.outputToDistPlus(inputs, n)))
}) | `Function(n) => Ok(`Function(n))
| _ => Error("Didn't output renderedDist")
}
);
};
let runFunction =
(
ins: Inputs.inputs,
fn: (array(string), ExpressionTypes.ExpressionTree.node),
fnInputs,
) => {
let (_, fns) = fn;
let inputs = ins |> Internals.distPlusRenderInputsToInputs;
let output =
ExpressionTree.runFunction(
{
sampleCount: inputs.samplingInputs.sampleCount |> E.O.default(10000),
outputXYPoints:
inputs.samplingInputs.outputXYPoints |> E.O.default(10000),
kernelWidth: inputs.samplingInputs.kernelWidth,
shapeLength: inputs.samplingInputs.shapeLength |> E.O.default(10000),
},
inputs.environment,
fnInputs,
fn,
);
Js.log2("GOt output", output);
switch (output) {
| Ok(`RenderedDist(n)) => Ok(`DistPlus(Internals.outputToDistPlus(ins, n)))
| Ok(`Function(n)) => Ok(`Function(n))
| _ => Error("Didn't output renderedDist")
};
}; };