Skip to content

more general bymap (RFC) #162

@aplavin

Description

@aplavin

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions