Skip to content

Commit 7cd588e

Browse files
committed
DArray: Show array elements
1 parent 23c2bd7 commit 7cd588e

File tree

1 file changed

+89
-12
lines changed

1 file changed

+89
-12
lines changed

src/array/darray.jl

Lines changed: 89 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,6 @@ collect(x::Computation) = collect(fetch(x))
9696

9797
Base.fetch(x::Computation) = fetch(stage(Context(global_context()), x))
9898

99-
function Base.show(io::IO, ::MIME"text/plain", x::ArrayOp)
100-
write(io, string(typeof(x)))
101-
write(io, string(size(x)))
102-
end
103-
104-
function Base.show(io::IO, x::ArrayOp)
105-
m = MIME"text/plain"()
106-
show(io, m, x)
107-
end
108-
109-
export BlockPartition, Blocks
110-
11199
abstract type AbstractBlocks{N} end
112100

113101
abstract type AbstractMultiBlocks{N}<:AbstractBlocks{N} end
@@ -200,6 +188,95 @@ function Base.collect(d::DArray; tree=false)
200188
end
201189
end
202190

191+
### show
192+
193+
#= FIXME
194+
@static if isdefined(Base, :AnnotatedString)
195+
# FIXME: Import StyledStrings
196+
struct ColorElement{T}
197+
color::Symbol
198+
value::T
199+
end
200+
function Base.show(io::IO, ::MIME"text/plain", x::ColorElement)
201+
print(io, styled"{(foreground=$(x.color)):$(x.value)}")
202+
end
203+
else
204+
=#
205+
struct ColorElement{T}
206+
color::Symbol
207+
value::Union{Some{T},Nothing}
208+
end
209+
function Base.show(io::IO, ::MIME"text/plain", x::ColorElement)
210+
if x.value !== nothing
211+
printstyled(io, something(x.value); color=x.color)
212+
else
213+
printstyled(io, "..."; color=x.color)
214+
end
215+
end
216+
Base.alignment(io::IO, x::ColorElement) =
217+
Base.alignment(io, something(x.value, "..."))
218+
#end
219+
struct ColorArray{T,N} <: DenseArray{T,N}
220+
A::DArray{T,N}
221+
color_map::Vector{Symbol}
222+
seen_values::Dict{NTuple{N,Int},Union{Some{T},Nothing}}
223+
function ColorArray(A::DArray{T,N}) where {T,N}
224+
colors = [:red, :green, :yellow, :blue, :magenta, :cyan]
225+
color_map = [colors[mod1(idx, length(colors))] for idx in 1:length(A.chunks)]
226+
return new{T,N}(A, color_map, Dict{NTuple{N,Int},Union{Some{T},Nothing}}())
227+
end
228+
end
229+
Base.size(A::ColorArray) = size(A.A)
230+
Base.getindex(A::ColorArray, idx::Integer) = getindex(A, (idx,))
231+
Base.getindex(A::ColorArray, idxs::Integer...) = getindex(A, (idxs...,))
232+
function Base.getindex(A::ColorArray{T,N}, idxs::NTuple{N,Int}) where {T,N}
233+
sd_idx_tuple, _ = partition_for(A.A, idxs)
234+
sd_idx = CartesianIndex(sd_idx_tuple)
235+
sd_idx_linear = LinearIndices(A.A.chunks)[sd_idx]
236+
if !haskey(A.seen_values, idxs)
237+
chunk = A.A.chunks[sd_idx]
238+
if chunk isa Chunk || isready(chunk)
239+
value = A.seen_values[idxs] = Some(getindex(A.A, idxs))
240+
else
241+
# Show a placeholder instead
242+
value = A.seen_values[idxs] = nothing
243+
end
244+
else
245+
value = A.seen_values[idxs]
246+
end
247+
if value !== nothing
248+
color = A.color_map[sd_idx_linear]
249+
else
250+
color = :light_black
251+
end
252+
return ColorElement{T}(color, value)
253+
end
254+
function Base.getindex(A::ColorArray{T,N}, idxs::Dims{S}) where {T,N,S}
255+
if S > N
256+
if all(idxs[(N+1):end] .== 1)
257+
return getindex(A, idxs[1:N])
258+
else
259+
throw(BoundsError(A, idxs))
260+
end
261+
elseif S < N
262+
throw(BoundsError(A, idxs))
263+
end
264+
end
265+
function Base.show(io::IO, ::MIME"text/plain", A::DArray{T,N}) where {T,N}
266+
write(io, string(DArray{T,N}))
267+
write(io, string(size(A)))
268+
write(io, " with $(join(size(A.chunks), 'x')) partitions of size $(join(A.partitioning.blocksize, 'x')):")
269+
pct_complete = 100 * (sum(c->c isa Chunk ? true : isready(c), A.chunks) / length(A.chunks))
270+
if pct_complete < 100
271+
println(io)
272+
printstyled(io, "~$(round(Int, pct_complete))% completed"; color=:yellow)
273+
end
274+
println(io)
275+
with_index_caching(1) do
276+
Base.print_array(IOContext(io, :compact=>true), ColorArray(A))
277+
end
278+
end
279+
203280
function (==)(x::ArrayOp, y::ArrayOp)
204281
x === y || reduce((a,b)->a&&b, map(==, x, y))
205282
end

0 commit comments

Comments
 (0)