Skip to content

Commit

Permalink
add bymap₊
Browse files Browse the repository at this point in the history
  • Loading branch information
aplavin committed Feb 27, 2025
1 parent 0455a9c commit 91cf8a1
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,23 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"

[weakdeps]
AccessorsExtra = "33016aad-b69d-45be-9359-82a41f556fd4"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
Measurements = "eff96d63-e80a-5855-80a2-b1b0885c5ab7"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[extensions]
AccessorsExtraExt = "AccessorsExtra"
ForwardDiffExt = "ForwardDiff"
MakieExt = "Makie"
MeasurementsExt = "Measurements"
RecipesBaseExt = "RecipesBase"
UnitfulExt = "Unitful"

[compat]
AccessorsExtra = "0.1.93"
Distributions = "0.17,0.18,0.19,0.20,0.21, 0.22, 0.23, 0.24, 0.25"
ForwardDiff = "0.10"
GenericSchur = "0.5"
Expand All @@ -53,4 +56,4 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[targets]
test = ["ControlSystemsBase", "ForwardDiff", "Makie", "Measurements", "Test", "Plots", "Unitful"]
test = ["AccessorsExtra", "ControlSystemsBase", "ForwardDiff", "Makie", "Measurements", "Test", "Plots", "Unitful"]
22 changes: 22 additions & 0 deletions ext/AccessorsExtraExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module AccessorsExtraExt

using MonteCarloMeasurements
using AccessorsExtra

function MonteCarloMeasurements.bymap₊(f, args...)
Ns = @getall args |> RecursiveOfType(AbstractParticles) |> length(_.particles)
allequal(Ns) || throw(ArgumentError("different number of particles within function arguments: $Ns"))
N = first(Ns)
vals = map(1:N) do i
curargs = @modify(args |> RecursiveOfType(AbstractParticles)) do p
p.particles[i]
end
f(curargs...)
end
v = first(vals)
numoptics = AccessorsExtra.flat_concatoptic(v, RecursiveOfType(Number))
valps = map(o -> Particles(o.(vals)), AccessorsExtra._optics(numoptics))
return setall(v, numoptics, valps)
end

end
2 changes: 1 addition & 1 deletion src/MonteCarloMeasurements.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export Normal, MvNormal, Cauchy, Beta, Exponential, Gamma, Laplace, Uniform, fit

export unsafe_comparisons, @unsafe, set_comparison_function

export bymap, bypmap, @bymap, @bypmap, @prob, Workspace, with_workspace, has_particles, mean_object
export bymap, bypmap, bymap₊, @bymap, @bypmap, @prob, Workspace, with_workspace, has_particles, mean_object

include("types.jl")
include("register_primitive.jl")
Expand Down
13 changes: 13 additions & 0 deletions src/bymap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,16 @@ macro prob(ex)
mean($op.(MonteCarloMeasurements.maybe_particles($(esc(a))), MonteCarloMeasurements.maybe_particles($(esc(b)))))
end
end


"""
bymap₊(f, args...)
Like `f(args...)`, but with uncertainty propagation.
Similar to `bymap`, but expands particles contained anywhere within `args`.
In addition, reassembles the result to be like the original `f` output with `Particles` instead of numbers.
This function is experimental and requires loading the `AccessorsExtra` package.
"""
function bymap₊ end
13 changes: 13 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,19 @@ Random.seed!(0)

end

@testset "bymap₊" begin
using AccessorsExtra

x = (1 ± 0.2, (a=1 ± 0.1, b=123))
y = bymap₊(x) do x
vals = (x[1], x[2].a)
(;x[2].b, i=argmax(vals))
end
@test y isa NamedTuple
@test y.b == 123.0 ± 0.0
@test y.i 1.52 ± 0.5
end

@testset "inference" begin
@inferred zero(Particles{Float64,1})
@inferred zeros(Particles{Float64,1}, 5)
Expand Down

0 comments on commit 91cf8a1

Please sign in to comment.