Skip to content

Commit

Permalink
Merge pull request #25 from JuliaRobotics/feature/addstringconversions
Browse files Browse the repository at this point in the history
adding new string conversion for easier downstream use
  • Loading branch information
dehann committed Jul 17, 2018
2 parents 9e27e18 + 120ac8f commit f20761c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 77 deletions.
69 changes: 31 additions & 38 deletions src/DualTree01.jl
Original file line number Diff line number Diff line change
@@ -1,43 +1,36 @@

function string(d::KernelDensityEstimate.BallTreeDensity)
# TODO only supports single bandwidth per dimension at this point
pts = getPoints(d)
return "KDE:$(size(pts,2)):$(getBW(d)[:,1]):$(pts)"
end

function parsestringvector(str::AS; dlim=',') where {AS <: AbstractString}
sstr = split(split(strip(str),'[')[end],']')[1]
ssstr = strip.(split(sstr,dlim))
parse.(Float64, ssstr)
end

function convert(::Type{BallTreeDensity}, str::AS) where {AS <: AbstractString}
@assert ismatch(r"KDE:", str)
sstr = strip.(split(str, ':'))
N = parse(Int, sstr[2])
bw = parsestringvector(sstr[3])
dims = length(bw)
ptrowstrs = split(sstr[4],';')
@assert dims == length(ptrowstrs)
pts = zeros(dims, N)
for i in 1:dims
pts[i,:] = parsestringvector(ptrowstrs[i], dlim=' ')
end
kde!(pts, bw)
end
# psubs = split(psubs, '[')[end]
# psubsub = split(psubs, ']')[1]
# pw = split(psubsub, ',')



# function OLDminDistGauss(bd::BallTreeDensity, dRoot::Int, atTree::BallTreeDensity, aRoot::Int)
# #@show "minDistGauss", dRoot, aRoot
# atCenter = center(atTree, aRoot)
# densCenter = center(bd, dRoot)
# bw = bwMax(bd, dRoot)
# result = 0.0
# tmp = 0.0
# for (k=1:Ndim(atTree))
# tmp = abs( atCenter[k] - densCenter[k] )
# tmp -= (rangeB(atTree, aRoot))[k] + (rangeB(bd, dRoot))[k];
# tmp = (tmp > 0) ? tmp : 0.0
# if ( bwUniform(bd) )
# result -= (tmp*tmp)/bw[k]
# else
# result -= (tmp*tmp)/bw[k] + log(bwMin(bd, dRoot)[k]) end
# end
# result = exp(result/2.0)
# return result
# end
#
# function OLDmaxDistGauss(bd::BallTreeDensity, dRoot::Int, atTree::BallTreeDensity, aRoot::Int)
# atCenter = center(atTree, aRoot)
# densCenter = center(bd, dRoot)
# bw = bwMin(bd, dRoot)
# tmp = 0.0
# result = 0.0
# for k in 1:Ndim(atTree.bt)
# tmp = abs( atCenter[k] - densCenter[k] )
# tmp+= rangeB(atTree, aRoot)[k] + rangeB(bd, dRoot)[k]
# if ( bwUniform(bd) )
# result -= (tmp*tmp)/bw[k]
# else
# result -= (tmp*tmp)/bw[k] + log(bwMax(bd, Root)[k])
# end
# end
# result = exp(result/2.0);
# return result
# end

function minDistGauss!(restmp::Array{Float64, 1}, bd::BallTreeDensity, dRoot::Int, atTree::BallTreeDensity, aRoot::Int)
#@show "minDistGauss", dRoot, aRoot
Expand Down
7 changes: 6 additions & 1 deletion src/KernelDensityEstimate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ module KernelDensityEstimate
using Distributions, Compat

import Distributions: sample
import Base: promote_rule, *, rand
import Base: promote_rule, *, rand, string, convert

export
# override Base
string,
convert,

# new stuff
MixtureDensity,
kde!,
getPoints,
Expand Down
52 changes: 14 additions & 38 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,43 +1,6 @@
using KernelDensityEstimate
using Base.Test
# include("BallTree01.jl")
# include("BallTreeDensity01.jl")
# include("KDE01.jl")
# include("DualTree01.jl")

# type BallTree
# dims::Int # dimension of data
# num_points::Int # of points
# centers::Array{Float64,1} # ball centers, dims numbers per ball
# ranges::Array{Float64,1} # bounding box ranges, dims per ball, dist from center to one side
# weights::Array{Float64,1} # total weight in each ball
#
# left_child::Array{Int,1}
# right_child::Array{Int,1} # left, right children; no parent indices
# lowest_leaf::Array{Int,1}
# highest_leaf::Array{Int,1} # lower & upper leaf indices for each ball
# permutation::Array{Int,1} # point's position in the original data
#
# next::Int # internal var for placing the non-leaf nodes
#
# swapHandle::Function
# calcStatsHandle::Function
# data
# end
# type BallTreeDensity
# bt::BallTree
#
# KernelType
# multibandwidth::Int # flag: is bandwidth uniform?
#
# means::Array{Float64,1} # Weighted mean of points from this level down
# bandwidth::Array{Float64,1} # Variance or other multiscale bandwidth
# bandwidthMin::Array{Float64,1}
# bandwidthMax::Array{Float64,1} # Bounds on BW in non-uniform case
#
# calcStatsHandle::Function
# swapHandle::Function
# end


# parse the output from matlab process
function parseMatPrintKDE(filename::String)
Expand Down Expand Up @@ -267,3 +230,16 @@ end
@test integralAppxUnitTests()

@test testRand()



@testset "test string and back conversion" begin

p = kde!(randn(2,3))
ps = string(p)
pp = convert(BallTreeDensity, ps)

@test norm(getPoints(pp)-getPoints(p)) < 1e-5
@test norm(getBW(pp)-getBW(p)) < 1e-5

end

0 comments on commit f20761c

Please sign in to comment.