# How to save a KDTree properly

**URL:** <https://discourse.julialang.org/t/how-to-save-a-kdtree-properly/27890>\
**Category:** General Usage\
**Created:** [August 23, 2019, 12:50pm UTC](https://discourse.julialang.org/t/how-to-save-a-kdtree-properly/27890 "2019-08-23T12:50:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xiucheng\_Li](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiucheng_li/32/9773_2.png) [@Xiucheng\_Li](https://discourse.julialang.org/u/Xiucheng_Li)\
**Post date:** [August 23, 2019, 12:50pm UTC](https://discourse.julialang.org/t/how-to-save-a-kdtree-properly/27890/1 "2019-08-23T12:50:38Z")

</div>

I want to save a KDTree into the disk and reuse it again with JLD2, but ran into problem after loading it,

```julia
using NearestNeighbors
using FileIO, JLD2

kdtree = KDTree(rand(2, 100))
save("/tmp/test.jld2", "kdtree", kdtree) # it works

```

but I got warning when loading the saved one,

```julia
julia> kdt = load("/tmp/test.jld2", "kdtree");
┌ Warning: type StaticArrays.SArray{Tuple{2},Float64,1,2} does not exist in workspace; reconstructing
└ @ JLD2 ~/.julia/packages/JLD2/KjBIK/src/data.jl:1153
┌ Warning: type Distances.Euclidean does not exist in workspace; reconstructing
└ @ JLD2 ~/.julia/packages/JLD2/KjBIK/src/data.jl:1153
┌ Warning: type parameters for KDTree{getfield(JLD2.ReconstructedTypes, Symbol("##StaticArrays.SArray{Tuple{2},Float64,1,2}#366")),getfield(JLD2.ReconstructedTypes, Symbol("##Distances.Euclidean#367")),Float64} do not match type KDTree in workspace; reconstructing
└ @ JLD2 ~/.julia/packages/JLD2/KjBIK/src/data.jl:1133

```

and got an error when attempting to query with the loaded tree

```julia
julia> knn(kdt, [1, 2.0], 3)
ERROR: MethodError: no method matching knn(::getfield(JLD2.ReconstructedTypes, Symbol("##KDTree{getfield(JLD2.ReconstructedTypes, Symbol("##StaticArrays.SArray{Tuple{2},Float64,1,2}#366")),getfield(JLD2.ReconstructedTypes, Symbol("##Distances.Euclidean#367")),Float64}#368")), ::Array{Float64,1}, ::Int64)
Closest candidates are:
  knn(::NNTree{V,P} where P<:Distances.Metric, ::AbstractArray{T<:Number,1}, ::Int64) where {V, T<:Number} at /Users/fineday/.julia/packages/NearestNeighbors/N7lgR/src/knn.jl:40
  knn(::NNTree{V,P} where P<:Distances.Metric, ::AbstractArray{T<:Number,1}, ::Int64, ::Any) where {V, T<:Number} at /Users/fineday/.julia/packages/NearestNeighbors/N7lgR/src/knn.jl:40
  knn(::NNTree{V,P} where P<:Distances.Metric, ::AbstractArray{T<:Number,1}, ::Int64, ::Any, ::Function) where {V, T<:Number} at /Users/fineday/.julia/packages/NearestNeighbors/N7lgR/src/knn.jl:40
  ...
Stacktrace:
 [1] top-level scope at none:0

```

so what’s the proper way to save it?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [August 23, 2019, 12:58pm UTC](https://discourse.julialang.org/t/how-to-save-a-kdtree-properly/27890/2 "2019-08-23T12:58:22Z")

</div>

For saving arbitrary Julia data I never find anything that works well so I always fall back to `serialize`:

```julia
julia> using Serialization

julia> serialize("/tmp/test", kdtree)

julia> deserialize("/tmp/test")
KDTree{StaticArrays.SArray{Tuple{2},Float64,1,2},Euclidean,Float64}
  Number of points: 100
  Dimensions: 2
  Metric: Euclidean(0.0)
  Reordered: true

```

Note that you might not be able to load serialized data from previous julia versions but so far it has been pretty stable in practice.

---

<div class="post-metadata">

**Author:** ![Xiucheng\_Li](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiucheng_li/32/9773_2.png) [@Xiucheng\_Li](https://discourse.julialang.org/u/Xiucheng_Li)\
**Post date:** [August 23, 2019, 1:18pm UTC](https://discourse.julialang.org/t/how-to-save-a-kdtree-properly/27890/3 "2019-08-23T13:18:53Z")

</div>

It works perfectly, thanks so much.
