From 1f61746aa1ff6b724dd3078a840d1258c76f4802 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Tue, 8 Apr 2025 18:54:07 +0200 Subject: [PATCH 1/3] Add triangle to bidirectional visualization --- docs/src/vis.md | 8 +++++++- ext/SparseMatrixColoringsColorsExt.jl | 18 +++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/docs/src/vis.md b/docs/src/vis.md index 242fee4d..597139ff 100644 --- a/docs/src/vis.md +++ b/docs/src/vis.md @@ -74,7 +74,7 @@ problem_bi = ColoringProblem(; structure=:nonsymmetric, partition=:bidirectional algo_bi = GreedyColoringAlgorithm(RandomOrder(StableRNG(0)); postprocessing=true, decompression=:direct) result_bi = coloring(S, problem_bi, algo_bi) -Ar_img, Ac_img, Br_img, Bc_img = show_colors( +A_rc_img, Ar_img, Ac_img, Br_img, Bc_img = show_colors( result_bi; colorscheme=ColorSchemes.progress, background_color=RGB(1, 1, 1), # white @@ -94,6 +94,12 @@ Ar_img Ac_img ``` +Together, this yields: + +```@example img +Arc_img +``` + And there are two associated compression results, one by row and one by column: ```@example img diff --git a/ext/SparseMatrixColoringsColorsExt.jl b/ext/SparseMatrixColoringsColorsExt.jl index eb0e4321..c934b5e9 100644 --- a/ext/SparseMatrixColoringsColorsExt.jl +++ b/ext/SparseMatrixColoringsColorsExt.jl @@ -142,6 +142,7 @@ function allocate_outputs( hA, wA = size(A) .* (scale + 2border + pad) .+ (pad) hBr, wBr = size(Br) .* (scale + 2border + pad) .+ (pad) hBc, wBc = size(Bc) .* (scale + 2border + pad) .+ (pad) + Arc_img = fill(background_color, hA, wA) Ar_img = fill(background_color, hA, wA) Ac_img = fill(background_color, hA, wA) Br_img = fill(background_color, hBr, wBr) @@ -150,8 +151,10 @@ function allocate_outputs( if !iszero(A[I]) area = matrix_entry_area(I, scale, border, pad) barea = matrix_entry_plus_border_area(I, scale, border, pad) + Arc_img[barea] .= border_color Ar_img[barea] .= border_color Ac_img[barea] .= border_color + Arc_img[area] .= background_color Ar_img[area] .= background_color Ac_img[area] .= background_color end @@ -172,7 +175,7 @@ function allocate_outputs( Bc_img[area] .= background_color end end - return Ar_img, Ac_img, Br_img, Bc_img + return Arc_img, Ar_img, Ac_img, Br_img, Bc_img end ## Implementations for different AbstractColoringResult types start here @@ -247,7 +250,11 @@ function show_colors!( return A_img, B_img end +mytriu(area) = [area[i, j] for i in axes(area, 1) for j in axes(area, 2) if i < j] +mytril(area) = [area[i, j] for i in axes(area, 1) for j in axes(area, 2) if i > j] + function show_colors!( + Arc_img::AbstractMatrix{<:Colorant}, Ar_img::AbstractMatrix{<:Colorant}, Ac_img::AbstractMatrix{<:Colorant}, Br_img::AbstractMatrix{<:Colorant}, @@ -264,9 +271,8 @@ function show_colors!( A_ccolor_indices = mod1.(column_colors(res), length(colorscheme)) A_rcolor_indices = mod1.(row_shift .+ row_colors(res), length(colorscheme)) B_ccolor_indices = mod1.(1:maximum(column_colors(res)), length(colorscheme)) - B_rcolor_indices = mod1.( - (row_shift + 1):(row_shift + maximum(row_colors(res))), length(colorscheme) - ) + B_rcolor_indices = + mod1.((row_shift + 1):(row_shift + maximum(row_colors(res))), length(colorscheme)) A_ccolors = colorscheme[A_ccolor_indices] A_rcolors = colorscheme[A_rcolor_indices] B_ccolors = colorscheme[B_ccolor_indices] @@ -278,9 +284,11 @@ function show_colors!( r, c = Tuple(I) area = matrix_entry_area(I, scale, border, pad) if column_colors(res)[c] > 0 + Arc_img[mytriu(area)] .= A_ccolors[c] Ac_img[area] .= A_ccolors[c] end if row_colors(res)[r] > 0 + Arc_img[mytril(area)] .= A_rcolors[r] Ar_img[area] .= A_rcolors[r] end end @@ -299,7 +307,7 @@ function show_colors!( Bc_img[area] .= B_ccolors[c] end end - return Ar_img, Ac_img, Br_img, Bc_img + return Arc_img, Ar_img, Ac_img, Br_img, Bc_img end end # module From e233e74f25793b6dc44a19a83ea3897c2e40ed57 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Wed, 9 Apr 2025 07:54:16 +0200 Subject: [PATCH 2/3] Fix --- docs/src/vis.md | 2 +- test/show_colors.jl | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/src/vis.md b/docs/src/vis.md index 597139ff..75085162 100644 --- a/docs/src/vis.md +++ b/docs/src/vis.md @@ -74,7 +74,7 @@ problem_bi = ColoringProblem(; structure=:nonsymmetric, partition=:bidirectional algo_bi = GreedyColoringAlgorithm(RandomOrder(StableRNG(0)); postprocessing=true, decompression=:direct) result_bi = coloring(S, problem_bi, algo_bi) -A_rc_img, Ar_img, Ac_img, Br_img, Bc_img = show_colors( +Arc_img, Ar_img, Ac_img, Br_img, Bc_img = show_colors( result_bi; colorscheme=ColorSchemes.progress, background_color=RGB(1, 1, 1), # white diff --git a/test/show_colors.jl b/test/show_colors.jl index 347b15ba..cad07088 100644 --- a/test/show_colors.jl +++ b/test/show_colors.jl @@ -52,11 +52,13 @@ algo = GreedyColoringAlgorithm(; decompression=:direct) Br, Bc = compress(A, result) scale = 3 - Ar_img, Ac_img, Br_img, Bc_img = show_colors(result; scale=scale) + Arc_img, Ar_img, Ac_img, Br_img, Bc_img = show_colors(result; scale=scale) + @test size(Arc_img) == size(A) .* scale @test size(Ar_img) == size(A) .* scale @test size(Ac_img) == size(A) .* scale @test size(Br_img) == size(Br) .* scale @test size(Bc_img) == size(Bc) .* scale + @test Arc_img isa Matrix{<:Colorant} @test Ar_img isa Matrix{<:Colorant} @test Ac_img isa Matrix{<:Colorant} @test Br_img isa Matrix{<:Colorant} From 343f8692e7d5e3825067d41d8f6aab992842ad80 Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Wed, 9 Apr 2025 08:19:12 +0200 Subject: [PATCH 3/3] format --- ext/SparseMatrixColoringsColorsExt.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ext/SparseMatrixColoringsColorsExt.jl b/ext/SparseMatrixColoringsColorsExt.jl index c934b5e9..7f844e89 100644 --- a/ext/SparseMatrixColoringsColorsExt.jl +++ b/ext/SparseMatrixColoringsColorsExt.jl @@ -271,8 +271,9 @@ function show_colors!( A_ccolor_indices = mod1.(column_colors(res), length(colorscheme)) A_rcolor_indices = mod1.(row_shift .+ row_colors(res), length(colorscheme)) B_ccolor_indices = mod1.(1:maximum(column_colors(res)), length(colorscheme)) - B_rcolor_indices = - mod1.((row_shift + 1):(row_shift + maximum(row_colors(res))), length(colorscheme)) + B_rcolor_indices = mod1.( + (row_shift + 1):(row_shift + maximum(row_colors(res))), length(colorscheme) + ) A_ccolors = colorscheme[A_ccolor_indices] A_rcolors = colorscheme[A_rcolor_indices] B_ccolors = colorscheme[B_ccolor_indices]