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)) => | Ok(`DistPlus(distPlus1)) =>
<DistPlusPlot distPlus={DistPlus.T.normalize(distPlus1)} /> <DistPlusPlot distPlus={DistPlus.T.normalize(distPlus1)} />
| Ok(`Function(f, a)) => | Ok(`Function(f, a)) =>
let result1 = let results = E.A.Floats.range(0.0, 10.0, 100)
DistPlusRenderer.runFunction( |> E.A.fmap(r =>
inputs1, DistPlusRenderer.runFunction(
(f, a), inputs1,
[|`SymbolicDist(`Float(0.0))|], (f, a),
); [|`SymbolicDist(`Float(r))|],
let result2 = )
DistPlusRenderer.runFunction( |> E.R.bind(_, a =>
inputs1, switch (a) {
(f, a), | `DistPlus(d) => Ok((r, d))
[|`SymbolicDist(`Float(2.0))|], | _ => Error("")
); }
let result3 = )
DistPlusRenderer.runFunction( )
inputs1, |> E.A.R.firstErrorOrOpen;
(f, a), switch (results) {
[|`SymbolicDist(`Float(4.0))|], | Ok(dists) => <> <PercentilesChart dists /> </>
); | Error(r) => r |> R.ste
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
}; };
| Error(r) => r |> R.ste | Error(r) => r |> R.ste
}; };
@ -236,7 +196,7 @@ module DemoDist = {
[@react.component] [@react.component]
let make = () => { let make = () => {
let (reloader, setRealoader) = React.useState(() => 1); let (reloader, setReloader) = React.useState(() => 1);
let reform = let reform =
Form.use( Form.use(
~validationStrategy=OnDemand, ~validationStrategy=OnDemand,
@ -366,8 +326,8 @@ let make = () => {
|], |],
); );
let onRealod = _ => { let onReload = _ => {
setRealoader(_ => reloader + 1); setReloader(_ => reloader + 1);
}; };
<div className=Styles.parent> <div className=Styles.parent>
@ -380,7 +340,7 @@ let make = () => {
<Antd.Button <Antd.Button
icon=Antd.IconName.reload icon=Antd.IconName.reload
shape=`circle shape=`circle
onClick=onRealod onClick=onReload
/> />
}> }>
<Form.Provider value=reform> <Form.Provider value=reform>
@ -411,7 +371,7 @@ let make = () => {
</Col> </Col>
</Row> </Row>
<Antd.Button <Antd.Button
_type=`primary icon=Antd.IconName.reload onClick=onRealod> _type=`primary icon=Antd.IconName.reload onClick=onReload>
{"Update Distribution" |> R.ste} {"Update Distribution" |> R.ste}
</Antd.Button> </Antd.Button>
</Antd.Form> </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 // 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. // 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! // TODO: This should work for symbolic distributions too!
@ -176,8 +177,8 @@ module PointwiseCombination = {
) => { ) => {
switch (pointwiseOp) { switch (pointwiseOp) {
| `Add => pointwiseAdd(evaluationParams, t1, t2) | `Add => pointwiseAdd(evaluationParams, t1, t2)
| `Multiply => pointwiseCombine(( *. ),evaluationParams, t1, t2) | `Multiply => pointwiseCombine(( *. ), evaluationParams, t1, t2)
| `Exponentiate => pointwiseCombine(( ** ),evaluationParams, t1, t2) | `Exponentiate => pointwiseCombine(( ** ), evaluationParams, t1, t2)
}; };
}; };
}; };
@ -262,14 +263,12 @@ module FloatFromDist = {
// TODO: This forces things to be floats // TODO: This forces things to be floats
let callableFunction = (evaluationParams, name, args) => { let callableFunction = (evaluationParams, name, args) => {
let b = args
args |> E.A.fmap(a =>
|> 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
) |> E.R.bind(_, Functions.fnn(evaluationParams, name));
|> E.A.R.firstErrorOrOpen;
b |> E.R.bind(_, Functions.fnn(evaluationParams, name));
}; };
module Render = { module Render = {
@ -280,7 +279,10 @@ module Render = {
| `SymbolicDist(d) => | `SymbolicDist(d) =>
Ok( Ok(
`RenderedDist( `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 | `RenderedDist(_) as t => Ok(t) // already a rendered shape, we're done here
@ -329,7 +331,12 @@ let toLeaf =
FloatFromDist.operationToLeaf(evaluationParams, distToFloatOp, t) FloatFromDist.operationToLeaf(evaluationParams, distToFloatOp, t)
| `Normalize(t) => Normalize.operationToLeaf(evaluationParams, t) | `Normalize(t) => Normalize.operationToLeaf(evaluationParams, t)
| `Render(t) => Render.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) => | `FunctionCall(name, args) =>
callableFunction(evaluationParams, 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") | _ => Error("Needs 3 args")
}; };
let to_: array(node) => result(node, string) = let to_: (float, float) => result(node, string) = (low, high) => switch(low,high){
fun | (low,high)
| [|`SymbolicDist(`Float(low)), `SymbolicDist(`Float(high))|]
when low <= 0.0 && low < high => { when low <= 0.0 && low < high => {
Ok(`SymbolicDist(SymbolicDist.Normal.from90PercentCI(low, high))); Ok(`SymbolicDist(SymbolicDist.Normal.from90PercentCI(low, high)));
} }
| [|`SymbolicDist(`Float(low)), `SymbolicDist(`Float(high))|] | (low,high)
when low < high => { when low < high => {
Ok(`SymbolicDist(SymbolicDist.Lognormal.from90PercentCI(low, high))); Ok(`SymbolicDist(SymbolicDist.Lognormal.from90PercentCI(low, high)));
} }
| [|`SymbolicDist(`Float(_)), `SymbolicDist(_)|] => | (low,high) =>
Error("Low value must be less than high value.") Error("Low value must be less than high value.")
| _ => Error("Requires 2 variables"); }
// Possible setup: // Possible setup:
// let normal = {"inputs": [`float, `float], "outputs": [`float]}; // let normal = {"inputs": [`float, `float], "outputs": [`float]};
@ -97,6 +96,6 @@ let fnn =
|> E.R.fmap(r => `SymbolicDist(r)) |> E.R.fmap(r => `SymbolicDist(r))
| _ => Error("Needs 3 valid arguments") | _ => Error("Needs 3 valid arguments")
} }
| ("to", _) => to_(args) | ("to", _) => apply2(twoFloats(to_), args)
| _ => Error("Function " ++ name ++ " not found") | _ => 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 = let renderIfNeeded =
(inputs, node: ExpressionTypes.ExpressionTree.node) (inputs, node: ExpressionTypes.ExpressionTree.node)
: result(ExpressionTypes.ExpressionTree.node, string) => : result(ExpressionTypes.ExpressionTree.node, string) =>
@ -171,6 +154,27 @@ let renderIfNeeded =
| n => Ok(n) | 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) => let exportDistPlus = (inputs, node: ExpressionTypes.ExpressionTree.node) =>
node node
|> renderIfNeeded(inputs) |> renderIfNeeded(inputs)