Skip to content

Commit 2b5f40a

Browse files
authored
Merge pull request #757 from JuliaAI/show-tweaks
Improve short version of `show` for measures
2 parents 8d0bd52 + 699098a commit 2b5f40a

File tree

5 files changed

+47
-24
lines changed

5 files changed

+47
-24
lines changed

src/MLJBase.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ const COLUMN_WIDTH = 24
110110
# how deep to display fields of `MLJType` objects:
111111
const DEFAULT_SHOW_DEPTH = 0
112112
const DEFAULT_AS_CONSTRUCTED_SHOW_DEPTH = 2
113-
const INDENT = 4
113+
const INDENT = 2
114114

115115
const Arr = AbstractArray
116116
const Vec = AbstractVector

src/machines.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,16 +375,18 @@ machines(::Source) = Machine[]
375375
_cache_status(::Machine{<:Any,true}) = " caches data"
376376
_cache_status(::Machine{<:Any,false}) = " does not cache data"
377377

378+
Base.show(io::IO, mach::Machine) = print(io, "machine($(mach.model), …)")
378379
function Base.show(io::IO, ::MIME"text/plain", mach::Machine{M}) where M
379-
show(io, mach)
380+
#show(io, mach)
381+
print(io, "Machine")
380382
print(io, " trained $(mach.state) time")
381383
if mach.state == 1
382384
print(io, ";")
383385
else
384386
print(io, "s;")
385387
end
386388
println(io, _cache_status(mach))
387-
println(io, " model: $M")
389+
println(io, " model: $(mach.model)")
388390
println(io, " args: ")
389391
for i in eachindex(mach.args)
390392
arg = mach.args[i]

src/measures/measures.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,6 @@ end
149149

150150
# display:
151151
show_as_constructed(::Type{<:Measure}) = true
152-
show_compact(::Type{<:Measure}) = true
153-
Base.show(io::IO, m::Measure) = show(io, MIME("text/plain"), m)
154152

155153
# info
156154
function StatisticalTraits.info(M::Type{<:Measure})

src/resampling.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,13 @@ _short(v::Vector) = string("[", join(_short.(v), ", "), "]")
533533
_short(::Missing) = missing
534534

535535
function Base.show(io::IO, ::MIME"text/plain", e::PerformanceEvaluation)
536-
data = hcat(e.measure, round3.(e.measurement), e.operation,
537-
[round3.(v) for v in e.per_fold])
536+
_measure = map(e.measure) do m
537+
repr(MIME("text/plain"), m)
538+
end
539+
_measurement = round3.(e.measurement)
540+
_per_fold = [round3.(v) for v in e.per_fold]
541+
542+
data = hcat(_measure, _measurement, e.operation, _per_fold)
538543
header = ["measure", "measurement", "operation", "per_fold"]
539544
println(io, "PerformanceEvaluation object "*
540545
"with these fields:")
@@ -546,7 +551,8 @@ function Base.show(io::IO, ::MIME"text/plain", e::PerformanceEvaluation)
546551
color_off()
547552
PrettyTables.pretty_table(io, data, header;
548553
header_crayon=PrettyTables.Crayon(bold=false),
549-
alignment=:l)
554+
alignment=:l,
555+
linebreaks=true)
550556
show_color ? color_on() : color_off()
551557
end
552558

src/show.jl

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,25 +120,38 @@ show_handle(object) = false
120120
function simple_repr(T)
121121
repr = string(T.name.name)
122122
parameters = T.parameters
123-
p_string = ""
124-
if length(parameters) > 0
125-
p = parameters[1]
126-
if p isa DataType
127-
p_string = simple_repr(p)
128-
elseif p isa Symbol
129-
p_string = string(":", p)
130-
end
131-
if length(parameters) > 1
132-
p_string *= ",…"
133-
end
134-
end
135-
isempty(p_string) || (repr *= "{"*p_string*"}")
123+
124+
# # add abbreviated type parameters:
125+
# p_string = ""
126+
# if length(parameters) > 0
127+
# p = parameters[1]
128+
# if p isa DataType
129+
# p_string = simple_repr(p)
130+
# elseif p isa Symbol
131+
# p_string = string(":", p)
132+
# end
133+
# if length(parameters) > 1
134+
# p_string *= ",…"
135+
# end
136+
# end
137+
# isempty(p_string) || (repr *= "{"*p_string*"}")
138+
136139
return repr
137140
end
138141

139142
# short version of showing a `MLJType` object:
140143
function Base.show(stream::IO, object::MLJType)
141144
str = simple_repr(typeof(object))
145+
L = length(propertynames(object))
146+
if L > 0
147+
first_name = propertynames(object) |> first
148+
value = getproperty(object, first_name)
149+
str *= "($first_name = $value"
150+
L > 1 && (str *= ", …")
151+
str *= ")"
152+
else
153+
str *= "()"
154+
end
142155
show_handle(object) && (str *= " $(handle(object))")
143156
if false # !isempty(propertynames(object))
144157
printstyled(IOContext(stream, :color=> SHOW_COLOR),
@@ -185,9 +198,13 @@ function fancy(stream, object::MLJType, current_depth, depth, n)
185198
show_compact(object) ||
186199
print(stream, crind(n + length(prefix) - anti))
187200
print(stream, "$(names[k]) = ")
188-
fancy(stream, value, current_depth + 1, depth, n + length(prefix)
189-
- anti + length("$k = "))
190-
k == n_names || print(stream, ",")
201+
if show_compact(object)
202+
show(stream, value)
203+
else
204+
fancy(stream, value, current_depth + 1, depth, n + length(prefix)
205+
- anti + length("$k = "))
206+
end
207+
k == n_names || print(stream, ", ")
191208
end
192209
print(stream, ")")
193210
if current_depth == 0 && show_handle(object)

0 commit comments

Comments
 (0)