Merge pull request #441 from quantified-uncertainty/fromsamples-fix

Small cleanup to fromSamples
This commit is contained in:
Quinn 2022-04-30 09:39:40 -04:00 committed by GitHub
commit d5d00efc75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 17 deletions

View File

@ -189,10 +189,9 @@ let rec run = (~env, functionCallInfo: functionCallInfo): outputType => {
->GenericDist.mixture(~scaleMultiplyFn=scaleMultiply, ~pointwiseAddFn=pointwiseAdd) ->GenericDist.mixture(~scaleMultiplyFn=scaleMultiply, ~pointwiseAddFn=pointwiseAdd)
->E.R2.fmap(r => Dist(r)) ->E.R2.fmap(r => Dist(r))
->OutputLocal.fromResult ->OutputLocal.fromResult
| FromArray(xs) => | FromSamples(xs) => xs
xs
->SampleSetDist.make ->SampleSetDist.make
->E.R2.fmap2(x => DistributionTypes.SampleSetError(x)) ->E.R2.errMap(x => DistributionTypes.SampleSetError(x))
->E.R2.fmap(x => x->DistributionTypes.SampleSet->Dist) ->E.R2.fmap(x => x->DistributionTypes.SampleSet->Dist)
->OutputLocal.fromResult ->OutputLocal.fromResult
} }

View File

@ -97,8 +97,8 @@ module DistributionOperation = {
type genericFunctionCallInfo = type genericFunctionCallInfo =
| FromDist(fromDist, genericDist) | FromDist(fromDist, genericDist)
| FromFloat(fromDist, float) | FromFloat(fromDist, float)
| FromSamples(array<float>)
| Mixture(array<(genericDist, float)>) | Mixture(array<(genericDist, float)>)
| FromArray(SampleSetDist.t)
let distCallToString = (distFunction: fromDist): string => let distCallToString = (distFunction: fromDist): string =>
switch distFunction { switch distFunction {
@ -123,7 +123,7 @@ module DistributionOperation = {
switch d { switch d {
| FromDist(f, _) | FromFloat(f, _) => distCallToString(f) | FromDist(f, _) | FromFloat(f, _) => distCallToString(f)
| Mixture(_) => `mixture` | Mixture(_) => `mixture`
| FromArray(_) => `samples` | FromSamples(_) => `fromSamples`
} }
} }
module Constructors = { module Constructors = {
@ -140,7 +140,7 @@ module Constructors = {
let isNormalized = (dist): t => FromDist(ToBool(IsNormalized), dist) let isNormalized = (dist): t => FromDist(ToBool(IsNormalized), dist)
let toPointSet = (dist): t => FromDist(ToDist(ToPointSet), dist) let toPointSet = (dist): t => FromDist(ToDist(ToPointSet), dist)
let toSampleSet = (dist, r): t => FromDist(ToDist(ToSampleSet(r)), dist) let toSampleSet = (dist, r): t => FromDist(ToDist(ToSampleSet(r)), dist)
let fromSamples = (xs): t => FromArray(xs) let fromSamples = (xs): t => FromSamples(xs)
let truncate = (dist, left, right): t => FromDist(ToDist(Truncate(left, right)), dist) let truncate = (dist, left, right): t => FromDist(ToDist(Truncate(left, right)), dist)
let inspect = (dist): t => FromDist(ToDist(Inspect), dist) let inspect = (dist): t => FromDist(ToDist(Inspect), dist)
let toString = (dist): t => FromDist(ToString(ToString), dist) let toString = (dist): t => FromDist(ToString(ToString), dist)

View File

@ -218,17 +218,14 @@ let dispatchToGenericOutput = (call: ExpressionValue.functionCall): option<
Helpers.toDistFn(ToSampleSet(Belt.Int.fromFloat(float)), dist) Helpers.toDistFn(ToSampleSet(Belt.Int.fromFloat(float)), dist)
| ("toSampleSet", [EvDistribution(dist)]) => | ("toSampleSet", [EvDistribution(dist)]) =>
Helpers.toDistFn(ToSampleSet(MagicNumbers.Environment.defaultSampleCount), dist) Helpers.toDistFn(ToSampleSet(MagicNumbers.Environment.defaultSampleCount), dist)
| ("fromSamples", [EvArray(arr)]) => | ("fromSamples", [EvArray(inputArray)]) => {
Helpers.toDistFn( let _wrapInputErrors = x => SampleSetDist.NonNumericInput(x)
ToSampleSet(MagicNumbers.Environment.defaultSampleCount), let parsedArray = Helpers.parseNumberArray(inputArray)->E.R2.errMap(_wrapInputErrors)
arr switch parsedArray {
->Helpers.parseNumberArray | Ok(array) => runGenericOperation(FromSamples(array))
->E.R2.fmap2(x => SampleSetDist.NonNumericInput(x)) | Error(e) => GenDistError(SampleSetError(e))
->E.R.bind(SampleSetDist.make) }->Some
->E.R2.fmap(x => DistributionTypes.SampleSet(x)) }
// Raising here isn't ideal. This: GenDistError(SampleSetError(NonNumericInput("Something wasn't a number"))) would be proper.
->E.R2.toExn("Something in the input wasn't a number"),
)
| ("inspect", [EvDistribution(dist)]) => Helpers.toDistFn(Inspect, dist) | ("inspect", [EvDistribution(dist)]) => Helpers.toDistFn(Inspect, dist)
| ("truncateLeft", [EvDistribution(dist), EvNumber(float)]) => | ("truncateLeft", [EvDistribution(dist), EvNumber(float)]) =>
Helpers.toDistFn(Truncate(Some(float), None), dist) Helpers.toDistFn(Truncate(Some(float), None), dist)

View File

@ -289,6 +289,13 @@ module R = {
| Ok(r) => r->Ok | Ok(r) => r->Ok
| Error(x) => x->f->Error | Error(x) => x->f->Error
} }
//I'm not sure what to call this.
let unify = (a: result<'a, 'b>, c: 'b => 'a): 'a =>
switch a {
| Ok(x) => x
| Error(x) => c(x)
}
} }
module R2 = { module R2 = {