Merge pull request #16 from foretold-app/improvements/1104

Improvements/1104
This commit is contained in:
Ozzie Gooen 2020-03-04 10:05:38 +00:00 committed by GitHub
commit 25eea46f0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 194 additions and 182 deletions

View File

@ -33,7 +33,7 @@
"antd": "3.17.0", "antd": "3.17.0",
"autoprefixer": "9.7.4", "autoprefixer": "9.7.4",
"babel-jest": "25.1.0", "babel-jest": "25.1.0",
"bs-ant-design-alt": "2.0.0-alpha.31", "bs-ant-design-alt": "2.0.0-alpha.33",
"bs-css": "11.0.0", "bs-css": "11.0.0",
"bs-moment": "0.4.4", "bs-moment": "0.4.4",
"bs-platform": "7.0.1", "bs-platform": "7.0.1",

View File

@ -6,21 +6,27 @@ module FormConfig = [%lenses
guesstimatorString: string, guesstimatorString: string,
// //
domainType: string, // Complete, LeftLimited(...), RightLimited(...), LeftAndRightLimited(..., ...) domainType: string, // Complete, LeftLimited(...), RightLimited(...), LeftAndRightLimited(..., ...)
xPoint: float, xPoint: string,
xPoint2: float, xPoint2: string,
excludingProbabilityMass: float, excludingProbabilityMass: string,
excludingProbabilityMass2: float, excludingProbabilityMass2: string,
// //
unitType: string, // UnspecifiedDistribution, TimeDistribution(zero, unit) unitType: string, // UnspecifiedDistribution, TimeDistribution(zero, unit)
zero: MomentRe.Moment.t, zero: MomentRe.Moment.t,
unit: string, unit: string,
// //
sampleCount: int, sampleCount: string,
outputXYPoints: int, outputXYPoints: string,
truncateTo: int, truncateTo: string,
} }
]; ];
type options = {
sampleCount: int,
outputXYPoints: int,
truncateTo: option(int),
};
module Form = ReForm.Make(FormConfig); module Form = ReForm.Make(FormConfig);
let schema = Form.Validation.Schema([||]); let schema = Form.Validation.Schema([||]);
@ -43,47 +49,18 @@ module FieldString = {
}; };
}; };
module FieldNumber = {
[@react.component]
let make = (~field, ~label, ~min=0) => {
<Form.Field
field
render={({handleChange, error, value, validate}) =>
<Antd.Form.Item label={label |> E.ste}>
<Antd.InputNumber
value
onChange=handleChange
min
onBlur={_ => validate()}
parser={str => {
let a = str |> Js.Float.fromString |> int_of_float;
a < min ? min : a;
}}
/>
</Antd.Form.Item>
}
/>;
};
};
module FieldFloat = { module FieldFloat = {
[@react.component] [@react.component]
let make = let make = (~field, ~label, ~className=Css.style([])) => {
(~field, ~label, ~className=Css.style([]), ~min=0., ~precision=2) => {
<Form.Field <Form.Field
field field
render={({handleChange, error, value, validate}) => render={({handleChange, error, value, validate}) =>
<Antd.Form.Item label={label |> E.ste}> <Antd.Form.Item label={label |> E.ste}>
<Antd.InputFloat <Antd.Input
value value
precision onChange={BsReform.Helpers.handleChange(handleChange)}
onChange=handleChange
onBlur={_ => validate()} onBlur={_ => validate()}
className className
parser={str => {
let a = str |> Js.Float.fromString;
Js.Float.isNaN(a) ? min : a;
}}
/> />
</Antd.Form.Item> </Antd.Form.Item>
} }
@ -134,27 +111,29 @@ module Styles = {
module DemoDist = { module DemoDist = {
[@react.component] [@react.component]
let make = let make = (~guesstimatorString, ~domain, ~unit, ~options) => {
(
~guesstimatorString,
~domain,
~unit,
~sampleCount,
~outputXYPoints,
~truncateTo,
) => {
<Antd.Card title={"Distribution" |> E.ste}> <Antd.Card title={"Distribution" |> E.ste}>
<div className=Styles.spacer /> <div className=Styles.spacer />
<div> <div>
<div> {switch (domain, unit, options) {
{DistPlusIngredients.make(~guesstimatorString, ~domain, ~unit, ()) | (Some(domain), Some(unit), Some(options)) =>
let distPlus =
DistPlusIngredients.make(~guesstimatorString, ~domain, ~unit, ())
|> DistPlusIngredients.toDistPlus( |> DistPlusIngredients.toDistPlus(
~sampleCount, ~sampleCount=options.sampleCount,
~outputXYPoints, ~outputXYPoints=options.outputXYPoints,
~truncateTo, ~truncateTo=options.truncateTo,
) );
|> E.O.React.fmapOrNull(distPlus => <DistPlusPlot distPlus />)} switch (distPlus) {
</div> | Some(distPlus) => <DistPlusPlot distPlus />
| _ =>
"Correct Guesstimator string input to show a distribution."
|> E.ste
};
| _ =>
"Nothing to show. Try to change the distribution description."
|> E.ste
}}
</div> </div>
</Antd.Card>; </Antd.Card>;
}; };
@ -171,16 +150,16 @@ let make = () => {
~initialState={ ~initialState={
guesstimatorString: "mm(5 to 20, floor(normal(20,2)), [.5, .5])", guesstimatorString: "mm(5 to 20, floor(normal(20,2)), [.5, .5])",
domainType: "Complete", domainType: "Complete",
xPoint: 50.0, xPoint: "50.0",
xPoint2: 60.0, xPoint2: "60.0",
excludingProbabilityMass2: 0.5, excludingProbabilityMass2: "0.5",
excludingProbabilityMass: 0.3, excludingProbabilityMass: "0.3",
unitType: "UnspecifiedDistribution", unitType: "UnspecifiedDistribution",
zero: MomentRe.momentNow(), zero: MomentRe.momentNow(),
unit: "days", unit: "days",
sampleCount: 1000, sampleCount: "1000",
outputXYPoints: 2000, outputXYPoints: "2000",
truncateTo: 500, truncateTo: "500",
}, },
(), (),
); );
@ -190,78 +169,98 @@ let make = () => {
reform.submit(); reform.submit();
}; };
let xPoint = reform.state.values.xPoint |> Js.Float.fromString;
let xPoint2 = reform.state.values.xPoint2 |> Js.Float.fromString;
let excludingProbabilityMass =
reform.state.values.excludingProbabilityMass |> Js.Float.fromString;
let excludingProbabilityMass2 =
reform.state.values.excludingProbabilityMass2 |> Js.Float.fromString;
let zero = reform.state.values.zero;
let unit = reform.state.values.unit;
let domainType = reform.state.values.domainType;
let unitType = reform.state.values.unitType;
let guesstimatorString = reform.state.values.guesstimatorString;
let sampleCount = reform.state.values.sampleCount |> Js.Float.fromString;
let outputXYPoints =
reform.state.values.outputXYPoints |> Js.Float.fromString;
let truncateTo = reform.state.values.truncateTo |> Js.Float.fromString;
let domain = let domain =
switch (reform.state.values.domainType) { switch (domainType) {
| "Complete" => DistTypes.Complete | "Complete" => Some(DistTypes.Complete)
| "LeftLimited" => | "LeftLimited"
LeftLimited({ when
xPoint: reform.state.values.xPoint, !Js.Float.isNaN(xPoint)
excludingProbabilityMass: reform.state.values.excludingProbabilityMass, && !Js.Float.isNaN(excludingProbabilityMass) =>
}) Some(LeftLimited({xPoint, excludingProbabilityMass}))
| "RightLimited" => | "RightLimited"
RightLimited({ when
xPoint: reform.state.values.xPoint2, !Js.Float.isNaN(xPoint2)
excludingProbabilityMass: && !Js.Float.isNaN(excludingProbabilityMass2) =>
reform.state.values.excludingProbabilityMass2, Some(RightLimited({xPoint, excludingProbabilityMass}))
}) | "LeftAndRightLimited"
| "LeftAndRightLimited" => when
!Js.Float.isNaN(xPoint)
&& !Js.Float.isNaN(excludingProbabilityMass)
&& !Js.Float.isNaN(xPoint2)
&& !Js.Float.isNaN(excludingProbabilityMass2) =>
Some(
LeftAndRightLimited( LeftAndRightLimited(
{ {xPoint, excludingProbabilityMass},
xPoint: reform.state.values.xPoint, {xPoint, excludingProbabilityMass},
excludingProbabilityMass: ),
reform.state.values.excludingProbabilityMass,
},
{
xPoint: reform.state.values.xPoint2,
excludingProbabilityMass:
reform.state.values.excludingProbabilityMass2,
},
) )
| _ => Js.Exn.raiseError("domain is unknown") | _ => None
}; };
let unit = let unit =
switch (reform.state.values.unitType) { switch (unitType) {
| "UnspecifiedDistribution" => DistTypes.UnspecifiedDistribution | "UnspecifiedDistribution" => Some(DistTypes.UnspecifiedDistribution)
| "TimeDistribution" => | "TimeDistribution" =>
TimeDistribution({ Some(
zero: reform.state.values.zero, TimeDistribution({zero, unit: unit |> TimeTypes.TimeUnit.ofString}),
unit: reform.state.values.unit |> TimeTypes.TimeUnit.ofString, )
}) | _ => None
| _ => Js.Exn.raiseError("unit is unknown")
}; };
let guesstimatorString = reform.state.values.guesstimatorString; let options =
let sampleCount = reform.state.values.sampleCount; switch (sampleCount, outputXYPoints, truncateTo) {
let outputXYPoints = reform.state.values.outputXYPoints; | (_, _, _)
let truncateTo = reform.state.values.truncateTo |> E.O.some; when
!Js.Float.isNaN(sampleCount)
&& !Js.Float.isNaN(outputXYPoints)
&& !Js.Float.isNaN(truncateTo)
&& sampleCount > 10.
&& outputXYPoints > 10.
&& truncateTo > 10. =>
Some({
sampleCount: sampleCount |> int_of_float,
outputXYPoints: outputXYPoints |> int_of_float,
truncateTo: truncateTo |> int_of_float |> E.O.some,
})
| _ => None
};
let demoDist = let demoDist =
React.useMemo1( React.useMemo1(
() => { () => <DemoDist guesstimatorString domain unit options />,
<DemoDist
guesstimatorString
domain
unit
sampleCount
outputXYPoints
truncateTo
/>
},
[| [|
reform.state.values.guesstimatorString, reform.state.values.guesstimatorString,
reform.state.values.domainType, reform.state.values.domainType,
reform.state.values.xPoint |> string_of_float, reform.state.values.xPoint,
reform.state.values.xPoint2 |> string_of_float, reform.state.values.xPoint2,
reform.state.values.xPoint2 |> string_of_float, reform.state.values.xPoint2,
reform.state.values.excludingProbabilityMass |> string_of_float, reform.state.values.excludingProbabilityMass,
reform.state.values.excludingProbabilityMass2 |> string_of_float, reform.state.values.excludingProbabilityMass2,
reform.state.values.unitType, reform.state.values.unitType,
reform.state.values.zero |> E.M.format(E.M.format_standard), reform.state.values.zero |> E.M.format(E.M.format_standard),
reform.state.values.unit, reform.state.values.unit,
reform.state.values.sampleCount |> string_of_int, reform.state.values.sampleCount,
reform.state.values.outputXYPoints |> string_of_int, reform.state.values.outputXYPoints,
reform.state.values.truncateTo |> string_of_int, reform.state.values.truncateTo,
reloader |> string_of_int, reloader |> string_of_int,
|], |],
); );
@ -445,25 +444,16 @@ let make = () => {
</Row> </Row>
<Row _type=`flex className=Styles.rows> <Row _type=`flex className=Styles.rows>
<Col span=4> <Col span=4>
<FieldNumber <FieldFloat field=FormConfig.SampleCount label="Sample Count" />
field=FormConfig.SampleCount
label="Sample Count"
min=100
/>
</Col> </Col>
<Col span=4> <Col span=4>
<FieldNumber <FieldFloat
field=FormConfig.OutputXYPoints field=FormConfig.OutputXYPoints
label="Output XY-points" label="Output XY-points"
min=100
/> />
</Col> </Col>
<Col span=4> <Col span=4>
<FieldNumber <FieldFloat field=FormConfig.TruncateTo label="Truncate To" />
field=FormConfig.TruncateTo
label="Truncate To"
min=10
/>
</Col> </Col>
</Row> </Row>
<Antd.Button <Antd.Button

View File

@ -87,13 +87,11 @@ export class CdfChartD3 {
render() { render() {
this._container = d3.select(this.attrs.container); this._container = d3.select(this.attrs.container);
if (this._container.node() === null) { if (this._container.node() === null) {
console.error('Container for D3 is not defined.'); throw new Error('Container for D3 is not defined.');
return;
} }
if (!['log', 'linear'].includes(this.attrs.scale)) { if (!['log', 'linear'].includes(this.attrs.scale)) {
console.error('Scale should be either "log" or "linear".'); throw new Error('Scale should be either "log" or "linear".');
return;
} }
// Log Scale. // Log Scale.
@ -121,8 +119,7 @@ export class CdfChartD3 {
]; ];
for (const field of fields) { for (const field of fields) {
if (!_.isNumber(this.attrs[field])) { if (!_.isNumber(this.attrs[field])) {
console.error(`${field} should be a number.`); throw new Error(`${field} should be a number.`);
return;
} }
} }
@ -157,6 +154,7 @@ export class CdfChartD3 {
`translate(${this.calc.chartLeftMargin}, ${this.calc.chartTopMargin})`, `translate(${this.calc.chartLeftMargin}, ${this.calc.chartTopMargin})`,
); );
try {
const common = this.getCommonThings(); const common = this.getCommonThings();
if (this.hasDate('continuous')) { if (this.hasDate('continuous')) {
this.addDistributionChart(common); this.addDistributionChart(common);
@ -164,6 +162,10 @@ export class CdfChartD3 {
if (this.hasDate('discrete')) { if (this.hasDate('discrete')) {
this.addLollipopsChart(common); this.addLollipopsChart(common);
} }
} catch (e) {
this._container.selectAll("*").remove();
throw e;
}
return this; return this;
} }
@ -183,10 +185,10 @@ export class CdfChartD3 {
const yMax = d3.max(this.attrs.data.continuous.ys); const yMax = d3.max(this.attrs.data.continuous.ys);
// Errors. // Errors.
if (!_.isFinite(xMin)) return console.error('xMin is undefined'); if (!_.isFinite(xMin)) throw new Error('xMin is undefined');
if (!_.isFinite(xMax)) return console.error('xMax is undefined'); if (!_.isFinite(xMax)) throw new Error('xMax is undefined');
if (!_.isFinite(yMin)) return console.error('yMin is undefined'); if (!_.isFinite(yMin)) throw new Error('yMin is undefined');
if (!_.isFinite(yMax)) return console.error('yMax is undefined'); if (!_.isFinite(yMax)) throw new Error('yMax is undefined');
// X-domains. // X-domains.
const yMaxDomainFactor = _.get(this.attrs, 'yMaxContinuousDomainFactor', 1); const yMaxDomainFactor = _.get(this.attrs, 'yMaxContinuousDomainFactor', 1);

View File

@ -41,7 +41,7 @@ function CdfChartReact(props) {
.set('maxX', props.maxX) .set('maxX', props.maxX)
.set('minX', props.minX) .set('minX', props.minX)
.set('onHover', props.onHover) .set('onHover', props.onHover)
.set('marginBottom',props.marginBottom || 15) .set('marginBottom', props.marginBottom || 15)
.set('marginLeft', 30) .set('marginLeft', 30)
.set('marginRight', 30) .set('marginRight', 30)
.set('marginTop', 5) .set('marginTop', 5)
@ -59,8 +59,7 @@ function CdfChartReact(props) {
discrete: props.discrete, discrete: props.discrete,
}) })
.render(); .render();
} } catch (e) {
catch(e) {
console.error("distPlotD3 Error: ", e) console.error("distPlotD3 Error: ", e)
} }
}); });

View File

@ -135,6 +135,7 @@ function get_final_pdf(pdf_vals, bst_pts_and_idxs, output_grid) {
var active_intervals = new Map(); var active_intervals = new Map();
var active_endpoints = new bst.AVLTree(); var active_endpoints = new bst.AVLTree();
var final_pdf_vals = []; var final_pdf_vals = [];
for ( for (
let out_grid_idx = 0; let out_grid_idx = 0;
out_grid_idx < output_grid.length; out_grid_idx < output_grid.length;
@ -179,9 +180,19 @@ function get_final_pdf(pdf_vals, bst_pts_and_idxs, output_grid) {
active_intervals.delete(interval_idx); active_intervals.delete(interval_idx);
} }
} }
return final_pdf_vals; return final_pdf_vals;
} }
/**
* @param {string} str
* @param {string} char
* @returns {number}
*/
function get_count_of_chars(str, char) {
return str.split(char).length - 1;
}
/** /**
* Entrypoint. Pass user input strings to this function, * Entrypoint. Pass user input strings to this function,
* get the corresponding pdf values and input points back. * get the corresponding pdf values and input points back.
@ -194,12 +205,19 @@ function get_final_pdf(pdf_vals, bst_pts_and_idxs, output_grid) {
* @returns {([]|*[])[]} * @returns {([]|*[])[]}
*/ */
function get_pdf_from_user_input(user_input_string) { function get_pdf_from_user_input(user_input_string) {
try{ try {
const count_opened_bracket = get_count_of_chars(user_input_string, '(');
const count_closed_bracket = get_count_of_chars(user_input_string, ')');
if (count_opened_bracket !== count_closed_bracket) {
throw new Error('Count of brackets are not equal.');
}
let parsed = parse.parse_initial_string(user_input_string); let parsed = parse.parse_initial_string(user_input_string);
let mm_args = parse.separate_mm_args(parsed.mm_args_string); let mm_args = parse.separate_mm_args(parsed.mm_args_string);
const is_mm = mm_args.distrs.length > 0; const is_mm = mm_args.distrs.length > 0;
if (!parsed.outer_string) return [[], [], true]; if (!parsed.outer_string) {
throw new Error('Parse string is empty.');
}
let tree = new bst.AVLTree(); let tree = new bst.AVLTree();
let possible_start_pts = []; let possible_start_pts = [];
@ -232,6 +250,7 @@ function get_pdf_from_user_input(user_input_string) {
let output_grid = evenly_spaced_grid(start_pt, end_pt, OUTPUT_GRID_NUMEL); let output_grid = evenly_spaced_grid(start_pt, end_pt, OUTPUT_GRID_NUMEL);
let final_pdf_vals = get_final_pdf(all_vals, tree, output_grid); let final_pdf_vals = get_final_pdf(all_vals, tree, output_grid);
return [final_pdf_vals, output_grid, false]; return [final_pdf_vals, output_grid, false];
} catch (e) { } catch (e) {
return [[], [], true]; return [[], [], true];

View File

@ -3,6 +3,7 @@ const math = _math.create(_math.all);
// Functions for parsing/processing user input strings are here // Functions for parsing/processing user input strings are here
// @todo: Do not use objects.
const DISTR_REGEXS = [ const DISTR_REGEXS = [
/beta\(/g, /beta\(/g,
/(log)?normal\(/g, /(log)?normal\(/g,
@ -12,7 +13,6 @@ const DISTR_REGEXS = [
]; ];
/** /**
*
* @param user_input_string * @param user_input_string
* @returns {{mm_args_string: string, outer_string: string}} * @returns {{mm_args_string: string, outer_string: string}}
*/ */
@ -20,6 +20,7 @@ function parse_initial_string(user_input_string) {
let outer_output_string = ""; let outer_output_string = "";
let mm_args_string = ""; let mm_args_string = "";
let idx = 0; let idx = 0;
while (idx < user_input_string.length) { while (idx < user_input_string.length) {
if ( if (
user_input_string.substring(idx - 11, idx) === "multimodal(" || user_input_string.substring(idx - 11, idx) === "multimodal(" ||
@ -42,6 +43,7 @@ function parse_initial_string(user_input_string) {
idx += 1; idx += 1;
} }
} }
return { return {
outer_string: outer_output_string, outer_string: outer_output_string,
mm_args_string: mm_args_string mm_args_string: mm_args_string

View File

@ -2488,10 +2488,10 @@ browserslist@^4.0.0, browserslist@^4.1.0, browserslist@^4.3.4, browserslist@^4.8
electron-to-chromium "^1.3.341" electron-to-chromium "^1.3.341"
node-releases "^1.1.47" node-releases "^1.1.47"
bs-ant-design-alt@2.0.0-alpha.31: bs-ant-design-alt@2.0.0-alpha.33:
version "2.0.0-alpha.31" version "2.0.0-alpha.33"
resolved "https://registry.yarnpkg.com/bs-ant-design-alt/-/bs-ant-design-alt-2.0.0-alpha.31.tgz#425003babaf47eefdc81cb392d9f224bc18c651a" resolved "https://registry.yarnpkg.com/bs-ant-design-alt/-/bs-ant-design-alt-2.0.0-alpha.33.tgz#13dc94f4fe4e7525485515cca2d2eae298940045"
integrity sha512-N0wmBksjgyQ4ioLi6L/gTt8852ghrmdYOBvs7onckEQ/O86eYRbKSMGL8cqMvUYJLB3IxKXSS89cssApqzGpTw== integrity sha512-C+REAjuxExzQnPP5KySwNd8Pizs0VGnrrF/6YlraErWr/Bjf3fqn6and3UXyHqi+DAj15IxY6e7lkaghQgO0eA==
dependencies: dependencies:
antd "3.17.0" antd "3.17.0"
bs-moment "^0.4.4" bs-moment "^0.4.4"