More simple cleanup

This commit is contained in:
Ozzie Gooen 2020-08-09 09:51:47 +01:00
parent b064001a51
commit e55a981be7
4 changed files with 70 additions and 100 deletions

View File

@ -164,64 +164,24 @@ module DemoDist = {
| Ok(`DistPlus(distPlus1)) =>
<DistPlusPlot distPlus={DistPlus.T.normalize(distPlus1)} />
| Ok(`Function(f, a)) =>
let result1 =
let results = E.A.Floats.range(0.0, 10.0, 100)
|> E.A.fmap(r =>
DistPlusRenderer.runFunction(
inputs1,
(f, a),
[|`SymbolicDist(`Float(0.0))|],
);
let result2 =
DistPlusRenderer.runFunction(
inputs1,
(f, a),
[|`SymbolicDist(`Float(2.0))|],
);
let result3 =
DistPlusRenderer.runFunction(
inputs1,
(f, a),
[|`SymbolicDist(`Float(4.0))|],
);
let result4 =
DistPlusRenderer.runFunction(
inputs1,
(f, a),
[|`SymbolicDist(`Float(6.0))|],
);
let result5 =
DistPlusRenderer.runFunction(
inputs1,
(f, a),
[|`SymbolicDist(`Float(8.0))|],
);
let result6 =
DistPlusRenderer.runFunction(
inputs1,
(f, a),
[|`SymbolicDist(`Float(10.0))|],
);
switch (result1, result2, result3, result4, result5, result6) {
| (
Ok(`DistPlus(distPlus1)),
Ok(`DistPlus(distPlus2)),
Ok(`DistPlus(distPlus3)),
Ok(`DistPlus(distPlus4)),
Ok(`DistPlus(distPlus5)),
Ok(`DistPlus(distPlus6)),
) =>
<>
<PercentilesChart
dists=[|
(0.0, distPlus1),
(2.0, distPlus2),
(4.0, distPlus3),
(6.0, distPlus4),
(8.0, distPlus5),
(10.0, distPlus6),
|]
/>
</>
| _ => "Failure " |> R.ste
[|`SymbolicDist(`Float(r))|],
)
|> E.R.bind(_, a =>
switch (a) {
| `DistPlus(d) => Ok((r, d))
| _ => Error("")
}
)
)
|> E.A.R.firstErrorOrOpen;
switch (results) {
| Ok(dists) => <> <PercentilesChart dists /> </>
| Error(r) => r |> R.ste
};
| Error(r) => r |> R.ste
};
@ -236,7 +196,7 @@ module DemoDist = {
[@react.component]
let make = () => {
let (reloader, setRealoader) = React.useState(() => 1);
let (reloader, setReloader) = React.useState(() => 1);
let reform =
Form.use(
~validationStrategy=OnDemand,
@ -366,8 +326,8 @@ let make = () => {
|],
);
let onRealod = _ => {
setRealoader(_ => reloader + 1);
let onReload = _ => {
setReloader(_ => reloader + 1);
};
<div className=Styles.parent>
@ -380,7 +340,7 @@ let make = () => {
<Antd.Button
icon=Antd.IconName.reload
shape=`circle
onClick=onRealod
onClick=onReload
/>
}>
<Form.Provider value=reform>
@ -411,7 +371,7 @@ let make = () => {
</Col>
</Row>
<Antd.Button
_type=`primary icon=Antd.IconName.reload onClick=onRealod>
_type=`primary icon=Antd.IconName.reload onClick=onReload>
{"Update Distribution" |> R.ste}
</Antd.Button>
</Antd.Form>

View File

@ -151,7 +151,8 @@ module PointwiseCombination = {
};
};
let pointwiseCombine = (fn, evaluationParams: evaluationParams, t1: t, t2: t) => {
let pointwiseCombine =
(fn, evaluationParams: evaluationParams, t1: t, t2: t) => {
// TODO: construct a function that we can easily sample from, to construct
// a RenderedDist. Use the xMin and xMax of the rendered shapes to tell the sampling function where to look.
// TODO: This should work for symbolic distributions too!
@ -176,8 +177,8 @@ module PointwiseCombination = {
) => {
switch (pointwiseOp) {
| `Add => pointwiseAdd(evaluationParams, t1, t2)
| `Multiply => pointwiseCombine(( *. ),evaluationParams, t1, t2)
| `Exponentiate => pointwiseCombine(( ** ),evaluationParams, t1, t2)
| `Multiply => pointwiseCombine(( *. ), evaluationParams, t1, t2)
| `Exponentiate => pointwiseCombine(( ** ), evaluationParams, t1, t2)
};
};
};
@ -262,14 +263,12 @@ module FloatFromDist = {
// TODO: This forces things to be floats
let callableFunction = (evaluationParams, name, args) => {
let b =
args
|> E.A.fmap(a =>
Render.render(evaluationParams, a)
|> E.R.bind(_, Render.toFloat)
Render.render(evaluationParams, a) |> E.R.bind(_, Render.toFloat)
)
|> E.A.R.firstErrorOrOpen;
b |> E.R.bind(_, Functions.fnn(evaluationParams, name));
|> E.A.R.firstErrorOrOpen
|> E.R.bind(_, Functions.fnn(evaluationParams, name));
};
module Render = {
@ -280,7 +279,10 @@ module Render = {
| `SymbolicDist(d) =>
Ok(
`RenderedDist(
SymbolicDist.T.toShape(evaluationParams.samplingInputs.shapeLength, d),
SymbolicDist.T.toShape(
evaluationParams.samplingInputs.shapeLength,
d,
),
),
)
| `RenderedDist(_) as t => Ok(t) // already a rendered shape, we're done here
@ -329,7 +331,12 @@ let toLeaf =
FloatFromDist.operationToLeaf(evaluationParams, distToFloatOp, t)
| `Normalize(t) => Normalize.operationToLeaf(evaluationParams, t)
| `Render(t) => Render.operationToLeaf(evaluationParams, t)
| `Symbol(r) => ExpressionTypes.ExpressionTree.Environment.get(evaluationParams.environment, r) |> E.O.toResult("Undeclared variable " ++ r)
| `Symbol(r) =>
ExpressionTypes.ExpressionTree.Environment.get(
evaluationParams.environment,
r,
)
|> E.O.toResult("Undeclared variable " ++ r)
| `FunctionCall(name, args) =>
callableFunction(evaluationParams, name, args)
};

View File

@ -35,19 +35,18 @@ let apply3 = (fn, args: array(node)): result(node, string) =>
| _ => Error("Needs 3 args")
};
let to_: array(node) => result(node, string) =
fun
| [|`SymbolicDist(`Float(low)), `SymbolicDist(`Float(high))|]
let to_: (float, float) => result(node, string) = (low, high) => switch(low,high){
| (low,high)
when low <= 0.0 && low < high => {
Ok(`SymbolicDist(SymbolicDist.Normal.from90PercentCI(low, high)));
}
| [|`SymbolicDist(`Float(low)), `SymbolicDist(`Float(high))|]
| (low,high)
when low < high => {
Ok(`SymbolicDist(SymbolicDist.Lognormal.from90PercentCI(low, high)));
}
| [|`SymbolicDist(`Float(_)), `SymbolicDist(_)|] =>
| (low,high) =>
Error("Low value must be less than high value.")
| _ => Error("Requires 2 variables");
}
// Possible setup:
// let normal = {"inputs": [`float, `float], "outputs": [`float]};
@ -97,6 +96,6 @@ let fnn =
|> E.R.fmap(r => `SymbolicDist(r))
| _ => Error("Needs 3 valid arguments")
}
| ("to", _) => to_(args)
| ("to", _) => apply2(twoFloats(to_), args)
| _ => Error("Function " ++ name ++ " not found")
};

View File

@ -135,23 +135,6 @@ module Internals = {
};
};
let run = (inputs: Inputs.inputs) => {
inputs
|> Internals.distPlusRenderInputsToInputs
|> Internals.inputsToLeaf
|> E.R.bind(_, r =>
switch (r) {
| `RenderedDist(n) => Ok(n)
| n =>
Error(
"Didn't output a rendered distribution. Format:"
++ ExpressionTree.toString(n),
)
}
)
|> E.R.fmap(Internals.outputToDistPlus(inputs));
};
let renderIfNeeded =
(inputs, node: ExpressionTypes.ExpressionTree.node)
: result(ExpressionTypes.ExpressionTree.node, string) =>
@ -171,6 +154,27 @@ let renderIfNeeded =
| n => Ok(n)
);
let run = (inputs: Inputs.inputs) => {
inputs
|> Internals.distPlusRenderInputsToInputs
|> Internals.inputsToLeaf
|> E.R.bind(_, r =>
r
|> renderIfNeeded(inputs)
|> (
fun
| Ok(`RenderedDist(n)) => Ok(n)
| Ok(n) =>
Error(
"Didn't output a rendered distribution. Format:"
++ ExpressionTree.toString(n),
)
| Error(r) => Error(r)
)
)
|> E.R.fmap(Internals.outputToDistPlus(inputs));
};
let exportDistPlus = (inputs, node: ExpressionTypes.ExpressionTree.node) =>
node
|> renderIfNeeded(inputs)