From f6e5ef6046fb0ad7522ce943d45d846973f24077 Mon Sep 17 00:00:00 2001 From: dehann Date: Fri, 13 Jul 2018 18:55:17 -0400 Subject: [PATCH 1/2] adding new string conversion for easier downstream use --- src/DualTree01.jl | 69 ++++++++++++++++-------------------- src/KernelDensityEstimate.jl | 7 +++- test/runtests.jl | 51 +++++++------------------- 3 files changed, 50 insertions(+), 77 deletions(-) diff --git a/src/DualTree01.jl b/src/DualTree01.jl index aa166b6..d7fb407 100644 --- a/src/DualTree01.jl +++ b/src/DualTree01.jl @@ -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 diff --git a/src/KernelDensityEstimate.jl b/src/KernelDensityEstimate.jl index 5bfeea0..4b6a888 100644 --- a/src/KernelDensityEstimate.jl +++ b/src/KernelDensityEstimate.jl @@ -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, diff --git a/test/runtests.jl b/test/runtests.jl index 27b3017..b0649ea 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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) @@ -267,3 +230,15 @@ 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 + +end From 120ac8f7b9198f527f22560e9a77bb825258a170 Mon Sep 17 00:00:00 2001 From: dehann Date: Fri, 13 Jul 2018 19:01:10 -0400 Subject: [PATCH 2/2] expanding tests slightly --- test/runtests.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/runtests.jl b/test/runtests.jl index b0649ea..015c124 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -240,5 +240,6 @@ ps = string(p) pp = convert(BallTreeDensity, ps) @test norm(getPoints(pp)-getPoints(p)) < 1e-5 +@test norm(getBW(pp)-getBW(p)) < 1e-5 end