Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use IOContext properly for styled printing. #146

Merged
merged 4 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
name = "SpectralFitting"
uuid = "f2c56810-742e-4b72-8bf4-27af3bb81a12"
authors = ["Fergus Baker <[email protected]>"]
version = "0.6.2"
version = "0.6.3"

[deps]
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
Crayons = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f"
DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
Expand Down
1 change: 0 additions & 1 deletion src/SpectralFitting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ using SpecialFunctions
using PreallocationTools
using EnumX

import Crayons
# fitting backends
import ForwardDiff
import LsqFit
Expand Down
4 changes: 2 additions & 2 deletions src/abstract-models.jl
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,12 @@ end

function Base.show(io::IO, ::MIME"text/plain", @nospecialize(model::AbstractSpectralModel))
buff = IOBuffer()
_printinfo(buff, model)
_printinfo(IOContext(buff, io), model)
s = String(take!(buff))
print(io, encapsulate(s))
end

# todo: this function could be cleaned up with some generated hackery
# todo: this function could be cleaned up with some generated hackery
function remake_with_number_type(model::AbstractSpectralModel{P}, T::Type) where {P}
params = parameter_tuple(model)
new_params = if P <: FitParam
Expand Down
64 changes: 18 additions & 46 deletions src/composite-models.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,9 @@ function Base.show(io::IO, @nospecialize(model::CompositeModel))
name = "$(Base.typename(typeof(model)).name)"
expr = replace(expr, s => name)
end
print(
io,
"CompositeModel[",
Crayons.Crayon(foreground = :cyan),
"$(expr)",
Crayons.Crayon(reset = true),
"]",
)
print(io, "CompositeModel[")
printstyled(io, "$(expr)", color = :cyan)
print(io, "]")
end

function _print_param(io, free, name, val, q0, q1, q2, q3, q4; binding = nothing)
Expand All @@ -168,28 +163,13 @@ function _print_param(io, free, name, val, q0, q1, q2, q3, q4; binding = nothing
end

if !isnothing(binding)
print(
io,
" ",
Crayons.Crayon(foreground = :magenta),
lpad(binding, 7),
Crayons.Crayon(reset = true),
)
print(io, " ")
printstyled(io, lpad(binding, 7), color = :magenta)
elseif free
print(
io,
Crayons.Crayon(foreground = :green),
lpad("FREE", 7),
Crayons.Crayon(reset = true),
)
printstyled(io, lpad("FREE", 7), color = :green)
else
print(
io,
" ",
Crayons.Crayon(foreground = :cyan),
lpad("FROZEN", 15 + q1 + q2 + q3 + q4),
Crayons.Crayon(reset = true),
)
print(io, " ")
printstyled(io, lpad("FROZEN", 15 + q1 + q2 + q3 + q4), color = :cyan)
end
end
println(io)
Expand All @@ -210,30 +190,22 @@ function _printinfo(io::IO, @nospecialize(model::CompositeModel); bindings = not
length("$s")
end

print(io, "CompositeModel with $(length(destructed.model_map)) model components:\n")
println(
io,
Crayons.Crayon(foreground = :cyan),
" "^expr_buffer,
destructed.expression,
Crayons.Crayon(reset = true),
)
println(io, "CompositeModel with $(length(destructed.model_map)) model components:")
print(io, " "^expr_buffer)
printstyled(io, destructed.expression, color = :cyan)
println(io)
println(io, "Model key and parameters:")

param_index = 1
for (sym, m) in destructed.model_map
param_syms = destructed.parameter_symbols[sym]
basename = Base.typename(typeof(m)).name
println(
io,
Crayons.Crayon(foreground = :cyan),
lpad("$sym", sym_buffer),
Crayons.Crayon(reset = true),
" => ",
Crayons.Crayon(foreground = :cyan),
"$basename",
Crayons.Crayon(reset = true),
)

printstyled(io, lpad("$sym", sym_buffer), color = :cyan)
print(io, " => ")
printstyled(io, "$basename", color = :cyan)
println(io)

for ps in param_syms
param = destructed.parameter_map[ps]
free = param isa FitParam ? !isfrozen(param) : true
Expand Down
16 changes: 5 additions & 11 deletions src/datasets/binneddata.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,11 @@ end
make_label(dataset::BinnedData) = dataset.name

function _printinfo(io::IO, data::BinnedData)
println(
io,
Crayons.Crayon(foreground = :cyan),
"BinnedData",
Crayons.Crayon(reset = true),
" with ",
Crayons.Crayon(foreground = :cyan),
length(data.domain),
Crayons.Crayon(reset = true),
" data points:",
)
printstyled(io, "BinnedData", color = :cyan)
print(io, " with ")
printstyled(io, length(data.domain), color = :cyan)
println(io, " data points:")

dmin, dmax = prettyfloat.(extrema(data.domain))
comin, comax = prettyfloat.(extrema(data.codomain))
descr = """ Name : $(data.name)
Expand Down
2 changes: 1 addition & 1 deletion src/datasets/datasets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ abstract type AbstractDataset end

function Base.show(io::IO, ::MIME"text/plain", @nospecialize(data::AbstractDataset))
buff = IOBuffer()
_printinfo(buff, data)
_printinfo(IOContext(buff, io), data)
s = String(take!(buff))
print(io, encapsulate(s))
end
Expand Down
16 changes: 5 additions & 11 deletions src/datasets/injectivedata.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,11 @@ end
make_label(dataset::InjectiveData) = dataset.name

function _printinfo(io::IO, data::InjectiveData)
println(
io,
Crayons.Crayon(foreground = :cyan),
"InjectiveData",
Crayons.Crayon(reset = true),
" with ",
Crayons.Crayon(foreground = :cyan),
length(data.domain),
Crayons.Crayon(reset = true),
" data points:",
)
printstyled(io, "InjectiveData", color = :cyan)
print(io, " with ")
printstyled(io, length(data.domain), color = :cyan)
println(io, " data points:")

dmin, dmax = prettyfloat.(extrema(data.domain))
comin, comax = prettyfloat.(extrema(data.codomain))
descr = """ Name : $(data.name)
Expand Down
13 changes: 5 additions & 8 deletions src/datasets/response.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,11 @@ function _printinfo(io, resp::ResponseMatrix{T}) where {T}
c_emin, c_emax = prettyfloat(minimum(resp.channel_bins_low)),
prettyfloat(maximum(resp.channel_bins_high))
ranks, files = size(resp.matrix)
println(
io,
"Response Matrix ",
Crayons.Crayon(foreground = :cyan),
"($ranks x $files)",
Crayons.Crayon(reset = true),
" channels:",
)

print(io, "Response Matrix")
printstyled(io, "($ranks x $files)", color = :cyan)
println(io, "channels:")

descr = """ . Chn. E (min/max) : ($c_emin, $c_emax)
. Domain E (min/max) : ($emin, $emax)
"""
Expand Down
71 changes: 18 additions & 53 deletions src/datasets/spectraldata.jl
Original file line number Diff line number Diff line change
Expand Up @@ -604,78 +604,43 @@ function _printinfo(io, data::SpectralData{T}) where {T}
ce_min = @views prettyfloat.(minimum(data.energy_low[data.data_mask]))
ce_max = @views prettyfloat.(maximum(data.energy_high[data.data_mask]))
dom_min, dom_max = @views prettyfloat.(extrema(domain))
@views println(
io,
Crayons.Crayon(foreground = :cyan),
"SpectralData",
Crayons.Crayon(reset = true),
" with ",
Crayons.Crayon(foreground = :cyan),
length(data.energy_low[data.data_mask]),
Crayons.Crayon(reset = true),
" active channels:",
)

printstyled(io, "SpectralData", color = :cyan)
print(io, " with ")
@views printstyled(io, length(data.energy_low[data.data_mask]), color = :cyan)
println(io, " active channels: ")

descr = """ . Chn. E (min/max) : ($ce_min, $ce_max)
. Masked channels : $(count(==(false), data.data_mask)) / $(length(data.data_mask))
. Model domain size : $(length(domain))
. Domain (min/max) : ($dom_min, $dom_max)
"""
print(io, descr)

print(
io,
Crayons.Crayon(foreground = :cyan),
"Primary Spectrum:",
Crayons.Crayon(reset = true),
"\n ",
)
printstyled(io, "Primary Spectrum:", color = :cyan)
println(io, "")
_printinfo(io, data.spectrum)

print(
io,
Crayons.Crayon(foreground = :cyan),
"Response:",
Crayons.Crayon(reset = true),
"\n ",
)
printstyled(io, "Response:", color = :cyan)
println(io, "")
_printinfo(io, data.response)

if has_background(data)
print(
io,
Crayons.Crayon(foreground = :cyan),
"Background: ",
Crayons.Crayon(reset = true),
"\n ",
)
printstyled(io, "Background:", color = :cyan)
println(io, "")
_printinfo(io, data.background)
else
print(
io,
Crayons.Crayon(foreground = :dark_gray),
"Background: nothing",
Crayons.Crayon(reset = true),
"\n",
)
printstyled(io, "Background: nothing", color = :gray)
println(io, "")
end

if has_ancillary(data)
print(
io,
Crayons.Crayon(foreground = :cyan),
"Ancillary:",
Crayons.Crayon(reset = true),
"\n ",
)
printstyled(io, "Ancillary:", color = :cyan)
println(io, "")
_printinfo(io, data.ancillary)
else
print(
io,
Crayons.Crayon(foreground = :dark_gray),
"Ancillary: nothing",
Crayons.Crayon(reset = true),
"\n",
)
printstyled(io, "Ancillary: nothing", color = :gray)
println(io, "")
end
end

Expand Down
Loading
Loading