Attempt to simplify Render code

This commit is contained in:
Ozzie Gooen 2020-07-19 21:19:57 +01:00
parent 6bf350039b
commit 95b8ba2036
10 changed files with 125 additions and 89 deletions

View File

@ -19,8 +19,9 @@ let timeDist ={
let setup = dist => let setup = dist =>
RenderTypes.DistPlusRenderer.make(~distPlusIngredients=dist,()) RenderTypes.DistPlusRenderer.make(~distPlusIngredients=dist,())
|> DistPlusRenderer.run |> DistPlusRenderer.run
|> RenderTypes.DistPlusRenderer.Outputs.distplus |> E.R.fmap(distPlus => <DistPlusPlot distPlus />)
|> R.O.fmapOrNull(distPlus => <DistPlusPlot distPlus />); |> E.R.toOption
|> E.O.toExn("")
let simpleExample = (name, guesstimatorString) => let simpleExample = (name, guesstimatorString) =>
<> <>

View File

@ -1,8 +1,9 @@
let setup = dist => let setup = dist =>
RenderTypes.DistPlusRenderer.make(~distPlusIngredients=dist, ()) RenderTypes.DistPlusRenderer.make(~distPlusIngredients=dist, ())
|> DistPlusRenderer.run |> DistPlusRenderer.run
|> RenderTypes.DistPlusRenderer.Outputs.distplus |> E.R.fmap(distPlus => <DistPlusPlot distPlus />)
|> R.O.fmapOrNull(distPlus => <DistPlusPlot distPlus />); |> E.R.toOption
|> E.O.toExn("")
let simpleExample = (guesstimatorString, ~problem="", ()) => let simpleExample = (guesstimatorString, ~problem="", ()) =>
<> <>

View File

@ -146,14 +146,11 @@ module DemoDist = {
(), (),
); );
let response = DistPlusRenderer.run(inputs); let response = DistPlusRenderer.run(inputs);
switch (RenderTypes.DistPlusRenderer.Outputs.distplus(response)) { switch (response) {
| Some(distPlus) => { | Ok(distPlus) =>
let normalizedDistPlus = DistPlus.T.normalize(distPlus); let normalizedDistPlus = DistPlus.T.normalize(distPlus);
<DistPlusPlot distPlus={normalizedDistPlus} />; <DistPlusPlot distPlus=normalizedDistPlus />;
} | Error(r) => r |> R.ste
| _ =>
"Correct Guesstimator string input to show a distribution."
|> R.ste
}; };
| _ => | _ =>
"Nothing to show. Try to change the distribution description." "Nothing to show. Try to change the distribution description."
@ -484,7 +481,10 @@ let make = () => {
/> />
</Col> </Col>
<Col span=4> <Col span=4>
<FieldFloat field=FormConfig.DownsampleTo label="Downsample To" /> <FieldFloat
field=FormConfig.DownsampleTo
label="Downsample To"
/>
</Col> </Col>
<Col span=4> <Col span=4>
<FieldFloat field=FormConfig.KernelWidth label="Kernel Width" /> <FieldFloat field=FormConfig.KernelWidth label="Kernel Width" />

View File

@ -32,6 +32,50 @@ module AlgebraicCombination = {
); );
}; };
let choose = (t1: node, t2: node) => {
let dLength = (r: DistTypes.discreteShape) =>
r.xyShape |> XYShape.T.length;
switch (t1, t2) {
| (`RenderedDist(Continuous(_)), `RenderedDist(Continuous(_))) => `Sampling
| (`RenderedDist(Discrete(m1)), `RenderedDist(Discrete(m2)))
when dLength(m1) * dLength(m2) > 1000 => `Analytical
| (`RenderedDist(Discrete(_)), `RenderedDist(Discrete(_))) => `Sampling
| (`RenderedDist(Discrete(d)), `SymbolicDist(_))
| (`SymbolicDist(_), `RenderedDist(Discrete(d)))
| (`RenderedDist(Discrete(d)), `RenderedDist(Continuous(_)))
| (`RenderedDist(Continuous(_)), `RenderedDist(Discrete(d))) =>
dLength(d) > 10 ? `Sampling : `Analytical
| _ => `Sampling
};
};
let foo =
(evaluationParams, algebraicOp, t1: node, t2: node)
: result(node, string) => {
E.R.merge(
SamplingDistribution.renderIfIsNotSamplingDistribution(
evaluationParams,
t1,
),
SamplingDistribution.renderIfIsNotSamplingDistribution(
evaluationParams,
t2,
),
)
|> E.R.bind(_, ((a, b)) =>
switch (choose(a, b)) {
| `Sampling =>
SamplingDistribution.combineShapesUsingSampling(
evaluationParams,
algebraicOp,
a,
b,
)
| `Analytical =>
combinationByRendering(evaluationParams, algebraicOp, a, b)
}
);
};
let operationToLeaf = let operationToLeaf =
( (
evaluationParams: evaluationParams, evaluationParams: evaluationParams,
@ -45,8 +89,8 @@ module AlgebraicCombination = {
|> E.R.bind( |> E.R.bind(
_, _,
fun fun
| `SymbolicDist(d) as t => Ok(t) | `SymbolicDist(_) as t => Ok(t)
| _ => combinationByRendering(evaluationParams, algebraicOp, t1, t2), | _ => foo(evaluationParams, algebraicOp, t1, t2),
); );
}; };

View File

@ -6,7 +6,7 @@ let isSamplingDistribution: node => bool =
| `RenderedDist(_) => true | `RenderedDist(_) => true
| _ => false; | _ => false;
let renderIfIsNotSamplingDistribution = (params, t) => let renderIfIsNotSamplingDistribution = (params, t): result(node, string) =>
!isSamplingDistribution(t) !isSamplingDistribution(t)
? switch (Render.render(params, t)) { ? switch (Render.render(params, t)) {
| Ok(r) => Ok(r) | Ok(r) => Ok(r)
@ -70,9 +70,7 @@ let combineShapesUsingSampling =
}, },
), ),
) )
|> E.O.bind(_, (r: RenderTypes.ShapeRenderer.Sampling.outputs) => |> E.O.bind(_, (r) => r.shape)
r.shape
)
|> E.O.toResult("No response"); |> E.O.toResult("No response");
shape |> E.R.fmap(r => `Normalize(`RenderedDist(r))); shape |> E.R.fmap(r => `Normalize(`RenderedDist(r)));
}, },

View File

@ -1,18 +1,4 @@
let downsampleIfShould = let run = (inputs: RenderTypes.DistPlusRenderer.inputs) => {
(
{recommendedLength, shouldDownsample}: RenderTypes.DistPlusRenderer.inputs,
outputs: RenderTypes.ShapeRenderer.Combined.outputs,
dist,
) => {
let willDownsample =
shouldDownsample
&& RenderTypes.ShapeRenderer.Combined.methodUsed(outputs) == `Sampling;
willDownsample ? dist |> DistPlus.T.downsample(recommendedLength) : dist;
};
let run =
(inputs: RenderTypes.DistPlusRenderer.inputs)
: RenderTypes.DistPlusRenderer.outputs => {
let toDist = shape => let toDist = shape =>
DistPlus.make( DistPlus.make(
~shape, ~shape,
@ -22,7 +8,7 @@ let run =
(), (),
) )
|> DistPlus.T.normalize; |> DistPlus.T.normalize;
let outputs = let output =
ShapeRenderer.run({ ShapeRenderer.run({
samplingInputs: inputs.samplingInputs, samplingInputs: inputs.samplingInputs,
guesstimatorString: inputs.distPlusIngredients.guesstimatorString, guesstimatorString: inputs.distPlusIngredients.guesstimatorString,
@ -30,8 +16,8 @@ let run =
length: inputs.recommendedLength, length: inputs.recommendedLength,
}, },
}); });
let shape = outputs |> RenderTypes.ShapeRenderer.Combined.getShape; output
let dist = |> E.R.fmap((o: RenderTypes.ShapeRenderer.Symbolic.outputs) =>
shape |> E.O.fmap(toDist) |> E.O.fmap(downsampleIfShould(inputs, outputs)); toDist(o.shape)
RenderTypes.DistPlusRenderer.Outputs.make(outputs, dist); );
}; };

View File

@ -36,9 +36,6 @@ let runSymbolic = (inputs: RenderTypes.ShapeRenderer.Combined.inputs) => {
); );
}; };
let run = let run = (inputs: RenderTypes.ShapeRenderer.Combined.inputs) => {
(inputs: RenderTypes.ShapeRenderer.Combined.inputs) runSymbolic(inputs);
: RenderTypes.ShapeRenderer.Combined.outputs => {
let symbolic = runSymbolic(inputs);
{symbolic: Some(symbolic), sampling: None};
}; };

View File

@ -1,3 +1,42 @@
module Types = {
let defaultSampleCount = 5000;
let defaultOutputXYPoints = 10000;
type inputs = {
sampleCount: option(int),
outputXYPoints: option(int),
kernelWidth: option(float),
};
type samplingStats = {
sampleCount: int,
outputXYPoints: int,
bandwidthXSuggested: float,
bandwidthUnitSuggested: float,
bandwidthXImplemented: float,
bandwidthUnitImplemented: float,
};
type outputs = {
continuousParseParams: option(samplingStats),
shape: option(DistTypes.shape),
};
let empty = {sampleCount: None, outputXYPoints: None, kernelWidth: None};
type fInputs = {
sampleCount: int,
outputXYPoints: int,
kernelWidth: option(float),
};
let toF = (i: inputs): fInputs => {
sampleCount: i.sampleCount |> E.O.default(defaultSampleCount),
outputXYPoints: i.outputXYPoints |> E.O.default(defaultOutputXYPoints),
kernelWidth: i.kernelWidth,
};
};
module JS = { module JS = {
[@bs.deriving abstract] [@bs.deriving abstract]
type distJs = { type distJs = {
@ -21,39 +60,6 @@ module KDE = {
|> JS.samplesToContinuousPdf(_, outputXYPoints, kernelWidth) |> JS.samplesToContinuousPdf(_, outputXYPoints, kernelWidth)
|> JS.jsToDist; |> JS.jsToDist;
}; };
// Note: This was an experiment, but it didn't actually work that well.
let inGroups = (samples, outputXYPoints, kernelWidth, ~cuttoff=0.9, ()) => {
let partitionAt =
samples
|> E.A.length
|> float_of_int
|> (e => e *. cuttoff)
|> int_of_float;
let part1XYPoints =
outputXYPoints |> float_of_int |> (e => e *. cuttoff) |> int_of_float;
let part2XYPoints = outputXYPoints - part1XYPoints |> Js.Math.max_int(30);
let part1Data =
samples |> Belt.Array.slice(_, ~offset=0, ~len=partitionAt);
let part2DataLength = (samples |> E.A.length) - partitionAt;
let part2Data =
samples
|> Belt.Array.slice(
_,
~offset=(-1) * part2DataLength,
~len=part2DataLength,
);
let part1 =
part1Data
|> JS.samplesToContinuousPdf(_, part1XYPoints, kernelWidth)
|> JS.jsToDist;
let part2 =
part2Data
|> JS.samplesToContinuousPdf(_, part2XYPoints, 3)
|> JS.jsToDist;
let opp = 1.0 -. cuttoff;
part1;
};
}; };
module T = { module T = {
@ -109,7 +115,7 @@ module T = {
let toShape = let toShape =
( (
~samples: t, ~samples: t,
~samplingInputs: RenderTypes.ShapeRenderer.Sampling.Inputs.fInputs, ~samplingInputs: Types.fInputs,
(), (),
) => { ) => {
Array.fast_sort(compare, samples); Array.fast_sort(compare, samples);
@ -126,6 +132,7 @@ module T = {
continuousPart |> E.A.length > 5 continuousPart |> E.A.length > 5
? { ? {
let _suggestedXWidth = Bandwidth.nrd0(continuousPart); let _suggestedXWidth = Bandwidth.nrd0(continuousPart);
// todo: This does some recalculating from the last step.
let _suggestedUnitWidth = let _suggestedUnitWidth =
suggestedUnitWidth(continuousPart, samplingInputs.outputXYPoints); suggestedUnitWidth(continuousPart, samplingInputs.outputXYPoints);
let usedWidth = let usedWidth =
@ -136,7 +143,7 @@ module T = {
samplingInputs.outputXYPoints, samplingInputs.outputXYPoints,
usedWidth, usedWidth,
); );
let foo: RenderTypes.ShapeRenderer.Sampling.samplingStats = { let foo: Types.samplingStats = {
sampleCount: samplingInputs.sampleCount, sampleCount: samplingInputs.sampleCount,
outputXYPoints: samplingInputs.outputXYPoints, outputXYPoints: samplingInputs.outputXYPoints,
bandwidthXSuggested: _suggestedXWidth, bandwidthXSuggested: _suggestedXWidth,
@ -159,7 +166,7 @@ module T = {
~continuous=pdf |> E.O.fmap(fst), ~continuous=pdf |> E.O.fmap(fst),
~discrete=Some(discrete), ~discrete=Some(discrete),
); );
let samplesParse: RenderTypes.ShapeRenderer.Sampling.outputs = { let samplesParse: Types.outputs = {
continuousParseParams: pdf |> E.O.fmap(snd), continuousParseParams: pdf |> E.O.fmap(snd),
shape, shape,
}; };
@ -168,11 +175,11 @@ module T = {
let fromSamples = let fromSamples =
( (
~samplingInputs=RenderTypes.ShapeRenderer.Sampling.Inputs.empty, ~samplingInputs=Types.empty,
samples, samples,
) => { ) => {
let samplingInputs = let samplingInputs =
RenderTypes.ShapeRenderer.Sampling.Inputs.toF(samplingInputs); Types.toF(samplingInputs);
toShape(~samples, ~samplingInputs, ()); toShape(~samples, ~samplingInputs, ());
}; };
}; };

View File

@ -25,11 +25,9 @@ let propValue = (t: Prop.Value.t) => {
~shouldDownsample=true, ~shouldDownsample=true,
(), (),
) )
|> DistPlusRenderer.run |> DistPlusRenderer.run;
|> RenderTypes.DistPlusRenderer.Outputs.distplus;
switch (newDistribution) { switch (newDistribution) {
| Some(distribution) => | Ok(distribution) => <div> <DistPlusPlot distPlus=distribution /> </div>
<div> <DistPlusPlot distPlus=distribution /> </div>
// <input // <input
// readOnly=true // readOnly=true
// className="shadow appearance-none border w-1/3 rounded py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" // className="shadow appearance-none border w-1/3 rounded py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
@ -45,7 +43,7 @@ let propValue = (t: Prop.Value.t) => {
// className="w-1/3 border w-1/2 rounded py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline bg-white"> // className="w-1/3 border w-1/2 rounded py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline bg-white">
// {"30 to infinity, 80% mass" |> ReasonReact.string} // {"30 to infinity, 80% mass" |> ReasonReact.string}
// </div> // </div>
| None => "Something went wrong" |> ReasonReact.string | Error(e) => e |> ReasonReact.string
}; };
| FloatCdf(_) => <div /> | FloatCdf(_) => <div />
| Probability(r) => | Probability(r) =>

View File

@ -112,8 +112,12 @@ module Model = {
GlobalCatastrophe.makeI(MomentRe.momentNow()) GlobalCatastrophe.makeI(MomentRe.momentNow())
|> RenderTypes.DistPlusRenderer.make(~distPlusIngredients=_, ()) |> RenderTypes.DistPlusRenderer.make(~distPlusIngredients=_, ())
|> DistPlusRenderer.run |> DistPlusRenderer.run
|> RenderTypes.DistPlusRenderer.Outputs.distplus |> E.R.bind(_, r =>
|> E.O.bind(_, DistPlusTime.Integral.xToY(Time(dateTime))); r
|> DistPlusTime.Integral.xToY(Time(dateTime))
|> E.O.toResult("error")
)
|> E.R.toOption;
}; };
let make = let make =
@ -299,4 +303,4 @@ module Interface = {
outputTypes: [||], outputTypes: [||],
run, run,
}; };
}; };