JuliaRobotics / KernelDensityEstimate.jl

Kernel Density Estimate with product approximation using multiscale Gibbs sampling
GNU Lesser General Public License v2.1
23 stars 7 forks source link

`bt` not defined in changeWeights! #101

Open AntonOresten opened 6 months ago

AntonOresten commented 6 months ago

Hi! I was trying to change the weights of a BallTreeDensity when I encountered this error.

Here's the relevant source code:

getIndexOf(bd::BallTreeDensity, i::Int) = getIndexOf(bd.bt, i)

function changeWeights!(bd::BallTreeDensity, newWeights::Array{Float64,1})
  for i in (bd.bt.num_points+1):bd.bt.num_points*2
    bd.bt.weights[i] = newWeights[ getIndexOf(bt, i) ]; # <--- error here
  end

  for i in bd.bt.num_points:-1:1
    calcStats!(bd.bt.data,i)
  end
  calcStats!(bd.bt.data, root());
  return nothing
end

It seems like maybe the getIndexOf(bt, i) call should instead be getIndexOf(bd, i)?

EDIT: I tried fixing it but got this:

BoundsError: attempt to access Tuple{typeof(+)} at index [2]

Stacktrace:
  ...
 [2] getMiniMaxi(bt::BallTree, leftI::Int64, rightI::Int64, d::Int64, addop::Tuple{typeof(+)}, diffop::Tuple{typeof(-)})
   @ KernelDensityEstimate [C:\Users\anton\.julia\dev\KernelDensityEstimate.jl\src\BallTree01.jl:261](file:///C:/Users/anton/.julia/dev/KernelDensityEstimate.jl/src/BallTree01.jl:261)
 [3] calcStatsBall!(bt::BallTree, root::Int64, addop::Tuple{typeof(+)}, diffop::Tuple{typeof(-)})
   @ KernelDensityEstimate [C:\Users\anton\.julia\dev\KernelDensityEstimate.jl\src\BallTree01.jl:306](file:///C:/Users/anton/.julia/dev/KernelDensityEstimate.jl/src/BallTree01.jl:306)
 [4] calcStatsDensity!(bd::BallTreeDensity, root::Int64, addop::Tuple{typeof(+)}, diffop::Tuple{typeof(-)})
   @ KernelDensityEstimate [C:\Users\anton\.julia\dev\KernelDensityEstimate.jl\src\BallTreeDensity01.jl:143](file:///C:/Users/anton/.julia/dev/KernelDensityEstimate.jl/src/BallTreeDensity01.jl:143)
 [5] calcStats!(data::BallTreeDensity, root::Int64, addop::Tuple{typeof(+)}, diffop::Tuple{typeof(-)})
   @ KernelDensityEstimate [C:\Users\anton\.julia\dev\KernelDensityEstimate.jl\src\BallTree01.jl:101](file:///C:/Users/anton/.julia/dev/KernelDensityEstimate.jl/src/BallTree01.jl:101)
 [6] changeWeights!(bd::BallTreeDensity, newWeights::Vector{Float64})
   @ KernelDensityEstimate [C:\Users\anton\.julia\dev\KernelDensityEstimate.jl\src\BallTreeDensity01.jl:305](file:///C:/Users/anton/.julia/dev/KernelDensityEstimate.jl/src/BallTreeDensity01.jl:305)

This might be a separate issue. I'm working with trivariate data. Could that be why? Should there be an addop/diffop for each dimension?

EDIT 2: It looks like this works, maybe?

function changeWeights!(bd::BallTreeDensity, newWeights::Array{Float64,1})
  for i in (bd.bt.num_points+1):bd.bt.num_points*2
    bd.bt.weights[i] = newWeights[ getIndexOf(bd, i) ];
  end

  addop = Tuple(fill(+, bd.bt.dims))
  diffop = Tuple(fill(-, bd.bt.dims))
  for i in bd.bt.num_points:-1:1
    calcStats!(bd.bt.data, i, addop, diffop)
  end
  calcStats!(bd.bt.data, root(), addop, diffop);
  return nothing
end
Affie commented 5 months ago

Hi, thanks for opening an issue. It looks like some functions might not be fully upgraded to use addop and doffp yet. @dehann will be the best person to help you on this one.