-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
spy.jl
176 lines (155 loc) · 7.51 KB
/
spy.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""
spy(A; kw...)
# Description
Plots the sparsity pattern for the given matrix `A`. This means
that a scatterplot that resembles the matrix is drawn, in which
only the pixel for non-zero elements of the matrix are set.
If the parameters `width` and `height` are not explicitly
specified, then the function will attempt to preserve the aspect
ratio of the matrix, while also attempting to fit the resulting
plot withing the bounding box specified by `maxwidth` and `maxheight`.
# Usage
spy(A; $(keywords((maxwidth = 0, maxheight = 0, zeros = false); add=(:fix_ar, :canvas))))
# Arguments
$(arguments(
(
A = "matrix of interest for which non-zero elements should be drawn",
maxheight = "maximum number of character rows that should be used for plotting",
maxwidth = "maximum number of characters per row that should be used for plotting",
height = "exact number of character rows that should be used for plotting (`0` stands for automatic)",
width = "exact number of characters per row that should be used for plotting (`0` stands for automatic)",
show_zeros = "show zeros pattern instead of default nonzeros",
); add=(:fix_ar, :canvas),
))
# Author(s)
- Dominique Orban (github.com/dpo)
- Christof Stocker (github.com/Evizero)
- Jake Bolewski (github.com/jakebolewski)
# Examples
```julia-repl
julia> using SparseArrays
julia> spy(sprandn(50, 120, .05))
┌────────────────────────────────────────────────────────────┐
1 │⡀⠨⠂⠀⠠⠀⠠⠀⠀⠀⠂⠀⡀⠂⠀⠀⠰⠈⠈⠂⡀⠀⠀⠀⠀⠐⠀⡀⡀⠀⠀⢄⡀⠀⠀⠠⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢄⠀⠀⠀⠁⠀⠀⠀⠀⠀⠀⠐⠴⠄│ > 0
│⠀⠀⠀⢀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠄⠠⠄⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠐⢀⠀⡀⠀⠄⠘⠀⠀⡀⠀⠀⠀⠂⠠⠀⠀⠀⠀⠀⠈⠀⠀⠀⠀⠀⠀⠀⠀│ < 0
│⠂⠀⠀⠀⠐⠐⠀⠂⠀⡀⠐⢀⠀⠀⠀⠀⡀⠀⠈⠀⠄⠀⠀⠨⠀⠀⠀⠀⠀⠠⠀⢀⠀⠀⠉⠐⠄⠄⠀⠔⠀⠀⠂⠀⢐⠀⠀⠀⡀⠘⢀⠀⠁⠄⠀⠠⠀⠄⠀⠄│
│⠀⢀⠀⠀⠀⠀⠀⠠⠀⠂⠀⠄⠀⠘⠈⠌⢀⠀⠀⠀⠐⠀⠁⠀⠀⠀⢀⠀⠀⠠⠀⠁⠄⠀⠀⠀⠂⢀⠀⠀⠀⠀⢀⡁⠀⠀⠂⠠⠀⠀⠀⠀⠀⠊⠁⠀⠀⠀⠀⠄│
│⠀⠅⠀⠀⠀⠀⢄⠈⠄⠠⠈⠀⠀⠀⠀⡀⠀⢀⠠⠀⠀⠀⠁⠀⠀⡀⠃⠀⠀⠈⠈⠁⠀⠁⠠⢀⠀⢁⠀⠀⢀⠀⠀⠀⢀⠀⠠⠂⠀⠁⢁⠀⠂⠀⠀⠆⠌⠀⠀⠀│
│⠀⠀⠔⠀⠀⠀⠀⢀⠀⠁⢀⠀⠀⠀⠀⠀⡀⠀⠀⠀⠀⡀⠀⠀⠀⡀⠀⠈⠀⠀⠀⡁⠁⠀⠀⠀⠠⠀⠀⠀⠄⡀⠀⠀⠀⠊⠀⠀⠄⠀⠀⠀⠀⠀⠀⠠⠀⠀⠄⠀│
│⠀⠀⠀⠀⠐⠀⠀⠀⠀⠀⠁⠀⠀⠀⠀⠅⠄⡀⠀⠀⢂⠂⠄⠑⠀⠀⠀⢄⠀⠀⠠⠂⠁⡀⠀⢠⠈⠀⠂⠀⠀⠄⠀⠀⠀⠄⠀⠀⠃⠂⠀⠄⢀⠀⠀⠀⠀⠀⠀⠀│
│⠀⠐⠀⠀⠂⠀⢀⠀⠀⠀⠀⠀⠀⠀⠀⠌⠀⠂⠀⠀⠀⠀⡀⢁⠁⠨⠀⠀⠀⠂⠀⠀⠨⠀⠁⠀⠀⠀⠀⠀⠊⠀⠄⠀⠀⠁⠐⠠⠀⢀⠀⠀⠀⠈⠀⠀⠁⠐⠄⠄│
│⢉⠀⢀⠁⠀⠀⠀⠀⠈⠀⠀⠀⠀⠁⠀⠠⠀⠀⠁⠀⠀⠀⡠⠁⠀⠀⠀⠀⠉⠠⡀⠀⠀⠀⢀⡀⠄⠀⠀⠀⠄⠀⠀⠈⠄⠀⠑⠀⠀⠀⠀⠀⠀⠠⠀⡀⡀⠀⠀⠀│
│⠅⠈⠈⠀⠀⠀⠀⠄⠀⠀⠀⠀⠀⠀⠀⠂⠀⠀⡄⠀⠀⠄⠀⠀⠠⠀⠀⠠⠈⠀⠂⠢⠈⠀⠀⠀⠄⠀⠀⠐⠀⠀⠀⠀⠀⠀⠀⠠⠀⠀⠀⠀⠀⠀⢀⠤⠀⠀⠀⠀│
│⠀⠀⠀⠂⠁⠀⠀⠀⠀⠁⠀⠘⠀⠂⢠⠀⠀⠀⠀⠀⢃⠀⠐⠈⠄⠐⠀⠀⠀⢀⠀⠀⠁⠀⠀⠀⠀⠀⠀⠀⠄⠀⠀⠀⠀⡀⠀⡀⠀⠀⠁⠀⠁⠀⠁⠠⠔⠀⢁⡀│
50 │⠀⠀⠀⠀⠀⠀⡀⠀⠀⠀⠐⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠀⠀⠈⠀⠂⠠⠀⠀⠀⠖⠀⠀⠀⠈⠀⠀⠀⠀⠀⡀⠠⠀⢀⠀⠅⠀⠀⠐⠀⠀⠀⠀⠠⠀⠠⠀⠀⢀⠀│
└────────────────────────────────────────────────────────────┘
⠀1⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀120⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀315 ≠ 0⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
```
# See also
[`Plot`](@ref), [`scatterplot`](@ref),
[`BrailleCanvas`](@ref), [`BlockCanvas`](@ref),
[`AsciiCanvas`](@ref), [`DotCanvas`](@ref)
"""
function spy(A::AbstractMatrix; kw...)
rows, cols, vals = _strict_non_zeros(_findnz(A)...)
if get(kw, :show_zeros, false)
I = CartesianIndex.(zip(rows, cols)) # non zeros
mask = trues(size(A))
mask[I] .= false
Z = CartesianIndices(axes(A))[mask] # zeros
rows, cols = getindex.(Z, 1), getindex.(Z, 2)
vals = zeros(eltype(vals), length(rows))
end
spy(size(A)..., rows, cols, vals; kw...)
end
_strict_non_zeros(rows, cols, vals) =
let I = findall(!iszero, vals) # findnz(A) returns stored zeros, ignore those
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,
rows::AbstractArray{<:Integer},
cols::AbstractArray{<:Integer},
vals::AbstractArray;
maxwidth::Integer = 0,
maxheight::Integer = 0,
out_stream::Union{Nothing,IO} = nothing,
height::Union{Nothing,Integer} = nothing,
width::Union{Nothing,Integer} = nothing,
margin::Integer = KEYWORDS.margin,
padding::Integer = KEYWORDS.padding,
color::UserColorType = KEYWORDS.color,
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)
warn_on_lost_kw(okw)
height, width = get_canvas_dimensions_for_matrix(
canvas,
nrow,
ncol,
maxheight,
maxwidth,
height,
width,
margin,
padding,
out_stream,
fix_ar;
extra_rows = 9,
extra_cols = 6,
)
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, rows; color)
label!(plot, :r, 1, show_zeros ? "⩵ 0" : "≠ 0", color)
else
if show_zeros
points!(plot, cols, rows; color = :green)
label!(plot, :r, 1, "⩵ 0", :green)
else
pos_idx = vals .> 0
neg_idx = (!).(pos_idx)
pos_cols = cols[pos_idx]
pos_rows = rows[pos_idx]
neg_cols = cols[neg_idx]
neg_rows = rows[neg_idx]
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
isempty(xlabel(plot)) &&
xlabel!(plot, nice_repr(length(vals), plot) * (show_zeros ? " ⩵ 0" : " ≠ 0"))
plot
end