Skip to content

Commit

Permalink
Merge pull request #232 from JuliaRobotics/23Q2/perf/bitstypes
Browse files Browse the repository at this point in the history
Support bitstypes
  • Loading branch information
Affie committed May 12, 2023
2 parents 10aab94 + f0604f5 commit 0905b82
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
39 changes: 38 additions & 1 deletion src/Interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,43 @@ function setPointPartial!(Mdest::AbstractManifold,
return dest
end

function setPointPartial!(
Mdest::AbstractManifold,
dest::AbstractArray{T},
Msrc::AbstractManifold,
src::AbstractArray{U},
partial::AbstractVector{<:Integer},
destIdx,
srcIdx=destIdx,
asPartial::Bool=true
) where {T<:AbstractArray,U<:AbstractArray}

if isbitstype(T)
#TODO needs cleanup, this is copied from setPointPartial! above with index changes
if length(partial) == 0
return dest[destIdx]
end
dest_coords = collect(AMP.makeCoordsFromPoint(Mdest, dest[destIdx]))
src_coords = AMP.makeCoordsFromPoint(Msrc, src[srcIdx])
dest_coords[partial] .= asPartial ? src_coords : view(src_coords, partial)
return dest[destIdx] = makePointFromCoords(Mdest, dest_coords, dest[destIdx])

else
return setPointPartial!(Mdest, dest[destIdx], Msrc, src[srcIdx], partial, asPartial)
end

end

#TODO workaround for supporting bitstypes, need rewrite, can consider `PowerManifoldNestedReplacing` or similar, maybe copyto!
function setPointsMani!(dest::AbstractArray{T}, src::AbstractArray{U}, destIdx, srcIdx=destIdx) where {T<:AbstractArray,U<:AbstractArray}
if isbitstype(T)
dest[destIdx] = src[srcIdx]
else
setPointsMani!(dest[destIdx],src[srcIdx])
end
end


#TODO ArrayPartition should work for now as it's an AbstractVector, but it won't remain mutable
setPointsMani!(dest::AbstractVector, src::AbstractVector) = (dest .= src)
setPointsMani!(dest::AbstractMatrix, src::AbstractMatrix) = (dest .= src)
Expand Down Expand Up @@ -165,7 +202,7 @@ function Base.replace( dest::ManifoldKernelDensity{M,<:BallTreeDensity,Nothing},
ipc[pl] .= src.infoPerCoord[pl]

# and _u0 point is a bit more tricky
c0 = vee(dest.manifold, dest._u0, log(dest.manifold, dest._u0, dest._u0))
c0 = collect(vee(dest.manifold, dest._u0, log(dest.manifold, dest._u0, dest._u0)))
c_ = vee(dest.manifold, dest._u0, log(dest.manifold, dest._u0, src._u0))
c0[pl] .= c_[pl]
u0 = exp(dest.manifold, dest._u0, hat(dest.manifold, dest._u0, c0))
Expand Down
18 changes: 16 additions & 2 deletions src/services/ManifoldKernelDensity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,20 @@ end
# pts = getPoints(x)


"""
$SIGNATURES
If a marginal (statistics) of a probability reduce the dimensions (i.e. casts a shadow, or projects); then an
antimarginal aims to increase dimension of the probability within reason.
Notes
- Marginalization is a integration of for higher dimension to lower dimension, so antimarginal likely involve
differentiation (anti-integral) instead.
- In manifold language, this is an embedding into a higher dimensional space.
- See structure from motion in machine vision, or stereo disparity for building depth clouds from 2D images.
- Imagine combining three different partial embedding A=[1, nan, nan, 1.4], B=[nan,2.1,nan,4], C=[nan, nan, 3, nan]
which should equal A+B+C = [notnan, notnan, notnan, notnan]
"""
function antimarginal(newM::AbstractManifold,
u0,
mkd::ManifoldKernelDensity,
Expand All @@ -270,9 +283,10 @@ function antimarginal(newM::AbstractManifold,

# convert to antimarginal by copying user provided example point for bigger manifold
pts = getPoints(mkd, false)
# new coord partials must be placed into a full dimension point, thats why we use u0
nPts = Vector{typeof(u0)}(undef, length(pts))
for (i,pt) in enumerate(pts)
nPts[i] = setPointPartial!(newM, deepcopy(u0), mkd.manifold, pt, newpartial)
for i in eachindex(pts)
setPointPartial!(newM, nPts, mkd.manifold, pts, newpartial, i)
end

# also update metadata elements
Expand Down

0 comments on commit 0905b82

Please sign in to comment.