Skip to content

Commit 5f7d296

Browse files
authored
Make Clustering non-lazy dependency (#13)
1 parent da00991 commit 5f7d296

4 files changed

Lines changed: 15 additions & 25 deletions

File tree

Project.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ version = "0.1.1"
77
Clustering = "aaaa29a8-35af-508c-8bc3-b662a17a0fe5"
88
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
99
ImageBase = "c817782e-172a-44cc-b673-b171935fbb9e"
10-
LazyModules = "8cdb02fc-e678-4876-92c5-9defec4f444e"
1110
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1211

1312
[compat]
1413
Clustering = "0.14.3, 0.15"
1514
Colors = "0.12"
1615
ImageBase = "0.1"
17-
LazyModules = "0.3"
1816
julia = "1.6"

src/ColorQuantization.jl

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@ using Colors
44
using ImageBase: FixedPoint, floattype, FixedPointNumbers.rawtype
55
using ImageBase: channelview, colorview, restrict
66
using Random: AbstractRNG, GLOBAL_RNG
7-
using LazyModules: @lazy
8-
#! format: off
9-
@lazy import Clustering = "aaaa29a8-35af-508c-8bc3-b662a17a0fe5"
10-
#! format: on
7+
using Clustering: kmeans, _kmeans_default_init, _kmeans_default_maxiter, _kmeans_default_tol
118

129
abstract type AbstractColorQuantizer end
1310

1411
include("api.jl")
1512
include("utils.jl")
1613
include("uniform.jl")
17-
include("clustering.jl") # lazily loaded
14+
include("clustering.jl")
1815

1916
export AbstractColorQuantizer, quantize
2017
export UniformQuantization, KMeansQuantization

src/clustering.jl

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
# The following code is lazily loaded from Clustering.jl using LazyModules.jl
21
# Code adapted from @cormullion's ColorSchemeTools (https://github.com/JuliaGraphics/ColorSchemeTools.jl)
3-
4-
# The following type definition is taken from from Clustering.jl for the kwarg `init`:
5-
const ClusteringInitType = Union{
6-
Symbol,Clustering.SeedingAlgorithm,AbstractVector{<:Integer}
7-
}
8-
92
const KMEANS_DEFAULT_COLORSPACE = RGB{Float32}
103

114
"""
@@ -28,8 +21,7 @@ The default values are carried over from are imported from Clustering.jl.
2821
For more details, refer to the [documentation](https://juliastats.org/Clustering.jl/stable/)
2922
of Clustering.jl.
3023
"""
31-
struct KMeansQuantization{T<:Colorant,I<:ClusteringInitType,R<:AbstractRNG} <:
32-
AbstractColorQuantizer
24+
struct KMeansQuantization{T<:Colorant,I,R<:AbstractRNG} <: AbstractColorQuantizer
3325
ncolors::Int
3426
maxiter::Int
3527
tol::Float64
@@ -39,9 +31,9 @@ struct KMeansQuantization{T<:Colorant,I<:ClusteringInitType,R<:AbstractRNG} <:
3931
function KMeansQuantization(
4032
T::Type{<:Colorant},
4133
ncolors::Integer;
42-
init=Clustering._kmeans_default_init,
43-
maxiter=Clustering._kmeans_default_maxiter,
44-
tol=Clustering._kmeans_default_tol,
34+
init=_kmeans_default_init,
35+
maxiter=_kmeans_default_maxiter,
36+
tol=_kmeans_default_tol,
4537
rng=GLOBAL_RNG,
4638
)
4739
ncolors 2 ||
@@ -62,7 +54,7 @@ end
6254

6355
function _kmeans(alg::KMeansQuantization, cs::AbstractArray{<:Colorant{T,N}}) where {T,N}
6456
data = reshape(channelview(cs), N, :)
65-
R = Clustering.kmeans(
57+
R = kmeans(
6658
data, alg.ncolors; maxiter=alg.maxiter, tol=alg.tol, init=alg.init, rng=alg.rng
6759
)
6860
return colorview(eltype(cs), R.centers)

test/runtests.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ algs_deterministic = Dict(
2525
@testset "Reference tests" begin
2626
for (name, alg) in algs_deterministic
2727
@testset "$name" begin
28-
cs = quantize(img, alg)
28+
cs = @inferred quantize(img, alg)
2929
@test eltype(cs) == eltype(img)
3030
@test_reference "references/$(name).txt" cs
3131

32-
cs = quantize(HSV{Float16}, img, alg)
32+
cs = @inferred quantize(HSV{Float16}, img, alg)
3333
@test eltype(cs) == HSV{Float16}
3434
@test_reference "references/$(name)_HSV.txt" cs
3535
end
@@ -41,14 +41,14 @@ algs_deterministic = Dict(
4141
c3 = RGB(0.11, 0.52, 0.73)
4242
cs = [c1, c2, c3]
4343

44-
cs1 = quantize(cs, UniformQuantization(2))
44+
cs1 = @inferred quantize(cs, UniformQuantization(2))
4545
# Centers of cubes at: 0.25, 0.75
4646
# c1 and c2 get quantized to the same color
4747
@test length(cs1) == 2
4848
@test cs1[1] == RGB(0.25, 0.25, 0.75)
4949
@test cs1[2] == RGB(0.25, 0.75, 0.75)
5050

51-
cs2 = quantize(cs, UniformQuantization(4))
51+
cs2 = @inferred quantize(cs, UniformQuantization(4))
5252
# Centers of cubes at: 0.125, 0.375, 0.625, 0.875
5353

5454
# Remember that `round` defaults to `RoundNearest`,
@@ -58,7 +58,10 @@ algs_deterministic = Dict(
5858
@test cs2[2] == RGB(0.375, 0.625, 0.625)
5959
@test cs2[3] == RGB(0.125, 0.625, 0.625)
6060
end
61-
61+
@testset "Type stability" begin
62+
# @inferred UniformQuantization(4) # runtime inferrence due to {N} = 4
63+
@inferred KMeansQuantization(8)
64+
end
6265
@testset "Error messages" begin
6366
@test_throws ArgumentError UniformQuantization(0)
6467
@test_throws ArgumentError KMeansQuantization(0)

0 commit comments

Comments
 (0)