Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more general bymap (RFC) #162

Open
aplavin opened this issue Jan 9, 2025 · 0 comments
Open

more general bymap (RFC) #162

aplavin opened this issue Jan 9, 2025 · 0 comments

Comments

@aplavin
Copy link
Contributor

aplavin commented Jan 9, 2025

I drafted a reasonably simple and more generic bymap version that works for arbitrary inputs that contain Particles inside.
It assumes (and asserts) all Particles have the same number of particles and iterates over them.
In the end, it gathers all results from applying f to arguments stripped of Particles, and transforms all numeric arguments there into Particles, keeping non-numbers from the first output. Assumes that all f results are consistent, have the same structure.

import MonteCarloMeasurements as MCM
using AccessorsExtra

function bymap₊(f, args...)
	Ns = @getall args |> RecursiveOfType(MCM.AbstractParticles) |> length(_.particles)
	N = uniqueonly(Ns)
	vals = map(1:N) do i
		curargs = @modify(args |> RecursiveOfType(MCM.AbstractParticles)) do p
			p.particles[i]
		end
		f(curargs...)
	end
	v = first(vals)
	numoptics = AccessorsExtra.flat_concatoptic(v, RecursiveOfType(Number))
	valps = map(o -> MCM.Particles(o.(vals)), AccessorsExtra._optics(numoptics))
	return setall(v, numoptics, valps)
end

x = (MCM.Particles(randn(1024)), (a=MCM.Particles(randn(1024)), b=123))
bymap₊(x) do x
	vals = (x[1], x[2].a)
	(;x[2].b, i=argmax(vals))
end
# returns:
# (b = 123.0 ± 0.0, i = 1.52 ± 0.5)

I'm not proposing to include it here, but wonder if you have some suggestions/recommendations on the specific semantics given your experience with bymap/Workspace. This bymap₊ is supposed to be much easier to use than the workspace thing, and applicable in many cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant