Skip to content

Commit

Permalink
More passing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewIOM committed Apr 17, 2024
1 parent 2be244b commit ed1f6d1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Bristlecone/Language.fs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ module Language =
| Parameter name ->
match pool |> Parameter.Pool.tryGetRealValue name with
| Some est -> est
| None -> failwithf "The equation could not be calculated. The parameter '%s' has not been set up." name
| None -> failwithf "The equation could not be calculated. The parameter '%s' has not been set up (or has yet to be estimated)." name
| Constant n -> n
| Add list ->
match list with
Expand Down
8 changes: 8 additions & 0 deletions src/Bristlecone/Parameter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ module Parameter =
else
None

/// Determines if the parameter has been estimated or not.
let isEstimated parameter =
let _, _, estimate = parameter |> unwrap

match estimate with
| NotEstimated _ -> false
| Estimated v -> true

/// Retrieve the estimated parameter value for further analysis.
/// Will only be `Ok` if parameter has been estimated.
let getEstimate parameter =
Expand Down
56 changes: 32 additions & 24 deletions tests/Bristlecone.Tests/Language.fs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ let modelExpressions =

match pool |> Parameter.Pool.toList |> List.tryFind (fun (k, _) -> k.Value = code.Value) with
| Some p ->
Expect.equal
(f ())
(p |> snd |> Parameter.getTransformedValue)
"Did not fail when parameter was not present"
if p |> snd |> Parameter.isEstimated
then
Expect.equal
(f ())
(p |> snd |> Parameter.getTransformedValue)
"Did not fail when parameter was not present"
| None ->
Expect.throws (fun () -> f () |> ignore) "Parameter was not present"

Expand All @@ -74,12 +76,16 @@ let modelExpressions =
|> Gen.sample 1 1
|> List.head

let result = Parameter selectedCode.Value |> compute x t pool e
if not (pool |> Parameter.Pool.hasParameter selectedCode.Value |> Option.map Parameter.isEstimated).Value
then ()
else

let existingValue =
pool |> Parameter.Pool.tryGetRealValue selectedCode.Value |> Option.get

let existingValue =
pool |> Parameter.Pool.tryGetRealValue selectedCode.Value |> Option.get
let result = Parameter selectedCode.Value |> compute x t pool e

Expect.equal result existingValue "The parameter value was not correct"
Expect.equal result existingValue "The parameter value was not correct"

testPropertyWithConfig Config.config "Fails when environmental (aka time-varying) data is not present"
<| fun (code:ShortCode.ShortCode) x t pool e ->
Expand Down Expand Up @@ -161,23 +167,25 @@ let modelBuilder =
|> ignore

testPropertyWithConfig Config.config "Doesn't compile if duplicate keys exist"
<| fun likelihood (eqs: (ShortCode.ShortCode * ModelExpression) list) (measures: (ShortCode.ShortCode * ModelSystem.MeasureEquation) list) ->
let compile () =
eqs
|> Seq.fold
(fun mb (n, eq) -> mb |> Model.addEquation n.Value eq)
(Model.empty |> Model.useLikelihoodFunction likelihood)
|> fun mb ->
Seq.fold
(fun mb (n: ShortCode.ShortCode, eq) -> mb |> Model.includeMeasure n.Value eq)
mb measures
|> Model.compile

let keys = [ (eqs |> List.map fst); (measures |> List.map fst) ] |> List.concat
if keys |> Seq.hasDuplicates then
Expect.throws (compile >> ignore) "Duplicate keys existed"
<| fun likelihood (eqs: ShortCode.ShortCode list) (measures: (ShortCode.ShortCode * ModelSystem.MeasureEquation) list) ->
if eqs.IsEmpty then ()
else
compile () |> ignore
let compile () =
eqs
|> Seq.fold
(fun mb n -> mb |> Model.addEquation n.Value This)
(Model.empty |> Model.useLikelihoodFunction likelihood)
|> fun mb ->
Seq.fold
(fun mb (n: ShortCode.ShortCode, eq) -> mb |> Model.includeMeasure n.Value eq)
mb measures
|> Model.compile

let keys = [ eqs; (measures |> List.map fst) ] |> List.concat
if keys |> Seq.hasDuplicates then
Expect.throws (compile >> ignore) "Duplicate keys existed"
else
compile () |> ignore

// testProperty "Only compiles when all required parameters are specified" <| fun (pool:Parameter.Pool) ->

Expand Down

0 comments on commit ed1f6d1

Please sign in to comment.