Skip to content

Commit

Permalink
fix spy {x,y}flip (#374)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Mar 6, 2024
1 parent b379a32 commit 5ab274d
Show file tree
Hide file tree
Showing 46 changed files with 332 additions and 255 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -20,7 +20,7 @@ jobs:
version:
- '1.6' # latest LTS
- '1'
- '~1.10.0-0' # upcoming julia version, next `rc`
- '~1.11.0-0' # upcoming julia version, next `rc`
experimental:
- false
os: [ubuntu-latest]
Expand Down
1 change: 1 addition & 0 deletions src/interface/heatmap.jl
Expand Up @@ -178,6 +178,7 @@ function heatmap(
labels,
height,
width,
margin,
colorbar_lim = (mi, ma),
colormap = callback,
min_height = 1,
Expand Down
51 changes: 33 additions & 18 deletions src/interface/spy.jl
Expand Up @@ -82,6 +82,13 @@ _strict_non_zeros(rows, cols, vals) =
rows[I], cols[I], vals[I]
end

_findnz(A::AbstractMatrix) =
let I = findall(!iszero, A)
getindex.(I, 1), getindex.(I, 2), A[I]
end

_findnz(A::AbstractSparseMatrix) = findnz(A)

function spy(
nrow::Integer,
ncol::Integer,
Expand All @@ -99,6 +106,8 @@ function spy(
canvas::Type{<:Canvas} = KEYWORDS.canvas,
fix_ar::Bool = KEYWORDS.fix_ar,
show_zeros::Bool = false,
xflip::Bool = false,
yflip::Bool = true,
kw...,
)
pkw, okw = split_plot_kw(kw)
Expand All @@ -120,15 +129,33 @@ function spy(
extra_cols = 6,
)

can = canvas(height, width; height = 1.0 + nrow, width = 1.0 + ncol)
plot = Plot(can; margin, padding, pkw...)
ylim = [1, nrow]
xlim = [1, ncol]

plot = Plot(
xlim,
ylim,
nothing,
canvas;
ylim,
xlim,
yflip,
xflip,
height,
width,
margin,
padding,
grid = false,
canvas_kw = (; height = 1.0 + nrow, width = 1.0 + ncol),
pkw...,
)

if color :auto
points!(plot, cols, nrow + 1 .- rows; color)
points!(plot, cols, rows; color)
label!(plot, :r, 1, show_zeros ? "⩵ 0" : "≠ 0", color)
else
if show_zeros
points!(plot, cols, nrow + 1 .- rows; color = :green)
points!(plot, cols, rows; color = :green)
label!(plot, :r, 1, "⩵ 0", :green)
else
pos_idx = vals .> 0
Expand All @@ -137,25 +164,13 @@ function spy(
pos_rows = rows[pos_idx]
neg_cols = cols[neg_idx]
neg_rows = rows[neg_idx]
points!(plot, pos_cols, nrow + 1 .- pos_rows; color = :red)
points!(plot, neg_cols, nrow + 1 .- neg_rows; color = :blue)
points!(plot, pos_cols, pos_rows; color = :red)
points!(plot, neg_cols, neg_rows; color = :blue)
label!(plot, :r, 1, "> 0", :red)
label!(plot, :r, 2, "< 0", :blue)
end
end
bc = BORDER_COLOR[]
label!(plot, :l, 1, "1", bc)
label!(plot, :l, nrows(plot.graphics), nice_repr(nrow, plot), bc)
label!(plot, :bl, "1", bc)
label!(plot, :br, nice_repr(ncol, plot), bc)
isempty(xlabel(plot)) &&
xlabel!(plot, nice_repr(length(vals), plot) * (show_zeros ? " ⩵ 0" : " ≠ 0"))
plot
end

_findnz(A::AbstractMatrix) =
let I = findall(!iszero, A)
getindex.(I, 1), getindex.(I, 2), A[I]
end

_findnz(A::AbstractSparseMatrix) = findnz(A)
2 changes: 1 addition & 1 deletion src/plot.jl
Expand Up @@ -196,7 +196,7 @@ function Plot(
height = something(height :auto ? displaysize(stdout)[1] - 6 - (isempty(title) ? 0 : 1) : height, DEFAULT_HEIGHT[])
width = something(width :auto ? displaysize(stdout)[2] - 10 : width, DEFAULT_WIDTH[])

(visible = width > 0) && (width = max(width, min_width))
(visible = width 0) && (width = max(width, min_width))
height = max(height, min_height)

x, y, z = validate_input(x, y, z)
Expand Down
6 changes: 3 additions & 3 deletions test/references_24/spy/default_0x0.txt
@@ -1,6 +1,6 @@
┌─────┐
1 │⠀⠀⠀⠀⠀│ > 0
0 │⠀⠀⠀⠀⠀│ < 0
0 │⠀⠀⠀⠀⠀│ > 0
1 │⠀⠀⠀⠀⠀│ < 0
└─────┘
⠀1⠀⠀⠀0⠀
⠀0⠀⠀⠀1⠀
⠀0 ≠ 0⠀
4 changes: 2 additions & 2 deletions test/references_24/spy/default_10x10.txt
@@ -1,6 +1,6 @@
┌─────┐
1 │⠀│ > 0
10 ││ < 0
1 │⠀⠐⠁│ > 0
10 ││ < 0
└─────┘
⠀1⠀⠀10⠀
⠀14 ≠ 0
6 changes: 3 additions & 3 deletions test/references_24/spy/default_10x15.txt
@@ -1,7 +1,7 @@
┌────────┐
1 │⠀⠀│ > 0
 │⠀⠀⠀│ < 0
10 │⠂⠈⠈⠂⠐│ 
1 │⠀⠆⠀│ > 0
 │⡀⠀│ < 0
10 │⠈⠀⠀⠈│ 
└────────┘
⠀1⠀⠀⠀⠀⠀15⠀
⠀⠀26 ≠ 0⠀⠀
8 changes: 4 additions & 4 deletions test/references_24/spy/default_15x10.txt
@@ -1,8 +1,8 @@
┌─────┐
1 │⢄⠀│ > 0
 ││ < 0
 ││ 
15 ││ 
1 │⠄⠐⢄⠀│ > 0
 │⡁⡀⡄│ < 0
 │⢀⠂│ 
15 ││ 
└─────┘
⠀1⠀⠀10⠀
⠀26 ≠ 0
2 changes: 1 addition & 1 deletion test/references_24/spy/default_2000x200.txt
Expand Up @@ -2,7 +2,7 @@
1 │⠀⠁⠀⠂⠀│ > 0
 │⠀⠀⠐⠀⠀│ < 0
 │⠠⠈⠌⡀⠰│ 
 │⠀⠀⠄⠌│ 
 │⠀⠀⠄⠌│ 
 │⠀⠀⢉⠁⠀│ 
 │⢈⠀⠂⠀⢄│ 
 │⠀⠢⠀⠀⠂│ 
Expand Down
6 changes: 3 additions & 3 deletions test/references_24/spy/default_200x2000.txt
@@ -1,7 +1,7 @@
┌──────────────────────────────────────────────────────────────────┐
1 │⠀⠀⠀⢀⠀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠠⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠀⠀⠄⠀⠀⠀⠀⠀⠀⠀⠀⢀⠀⠀⠀⠈⠀⠀⠀⠀⠄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ > 0
 │⠀⠀⠀⠀⠀⠀⠀⠠⠀⠀⠈⠂⠀⠀⠀⠀⠀⠀⠀⠀⠑⠀⢀⠀⠀⠂⠀⠠⠀⠠⠀⠀⠀⠀⠈⡀⠀⠀⠀⠁⠀⠀⠁⢀⠀⠀⠀⠀⠀⠀⠀⠀⠃⠀⠀⠀⠀⠀⠀⠀⠀⢀⠀⢀│ < 0
192 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠐⠈⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡀⠈⠀⠀⠀⠀⠀⠈⠀⠀⠀⠀⠄⠁⠀⠀⠄⠀⠀⠄⠀⠀⠀⠀⢀⠀⠀⠀⠀⠉⠀⠀⠀⠀⠂⠀⠀⠑⠀│ 
1 │⠀⠀⠀⢀⠀⡀⠀⠀⠀⢀⠀⠀⠀⠀⠀⠂⠀⠀⠀⠀⠠⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠀⠀⠄⠀⠀⠀⠀⠀⠀⠀⠀⢀⠀⠀⠀⠈⠀⠀⠀⠀⠄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ > 0
 │⠀⠀⠀⠀⠀⠀⠀⠠⠀⠀⠂⠀⠀⠀⠀⠀⠀⠀⠀⠑⠀⢀⠀⠀⠂⠀⠠⠀⠠⠀⠀⠀⠀⠈⡀⠀⠀⠀⠁⠀⠀⠁⢀⠀⠀⠀⠀⠀⠀⠀⠀⠃⠀⠀⠀⠀⠀⠀⠀⠀⢀⠀⢀│ < 0
192 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠐⠈⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡀⠈⠀⠀⠀⠀⠀⠈⠀⠀⠀⠀⠄⠁⠀⠀⠀⠀⠀⠄⠀⠀⠀⠀⢀⠀⠀⠀⠀⠉⠀⠀⠀⠀⠂⠀⠀⠑⠀│ 
└──────────────────────────────────────────────────────────────────┘
⠀1⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀1 982⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀52 ≠ 0⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
26 changes: 13 additions & 13 deletions test/references_24/spy/default_200x200_normal.txt
@@ -1,19 +1,19 @@
┌──────────────────────────────┐
1 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠄⠀⠀⠀⠀⠀⠀⠀⠀⠀│ > 0
 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠐⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ < 0
 │⠀⠀⠀⠀⠀⠂⠀⠀⠀⠁⠀⠀⠀⠀⠐⠀⠈⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡠│ 
 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⡀⠀⠀⠀⠀⠀⠀⠠⠀⠀⠀⠀⠀⠀⠀⠀│ 
 │⠀⠀⠀⠀⢀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠁⠀⠀⠈⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠠⠀⠀⠀⠀│ 
 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⡀⠀⠀⠀⠀⠀⠀⠐⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ < 0
 │⠀⠀⠀⠀⠀⠂⠀⠀⠀⠀⠀⠀⠀⠐⠀⠈⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡠│ 
 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⡀⠀⠀⠀⠀⠀⠀⠠⠀⠀⠀⠀⠀⠀⠀⠀│ 
 │⠀⠀⠀⠀⢀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠁⠀⠀⠈⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠠⠀⠀⠀⠀│ 
 │⠀⠀⠀⠀⠀⠀⠀⠈⠀⠂⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠂⠀⠀⠀⠀⠀│ 
 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠠⠀⠀⠀⠂⠀⠀⠀⠀⠀⠀⠀⠀│ 
 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠁⠀⠀⠀⠀⠀⢀⠀⠀⠄⠀⠀⠀│ 
 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠁⠀⠀⠠⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠈⠀│ 
200 │⠀⠀⠀⠀⠀⠀⠀⢀⠀⠀⠀⠀⠀⠀⠀⠐⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠠⡀⠀⠀⠀⠂⠀⠀⠀⠀⠀⠀⠀⠀│ 
 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠀⠀⠄⠀⠀⠀│ 
 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠁⠀⠀⠠⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠀│ 
 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠀│ 
200 │⠀⠀⠀⠀⠀⠀⠀⢀⠀⠀⠀⠀⠀⠀⠀⠐⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
└──────────────────────────────┘
⠀1⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀199⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀52 ≠ 0⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀

0 comments on commit 5ab274d

Please sign in to comment.