Skip to content

Commit

Permalink
style: julia formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
fjebaker committed Sep 30, 2023
1 parent 3188baa commit 5662455
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 40 deletions.
20 changes: 15 additions & 5 deletions src/datasets/binning.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,21 @@ function downsample_rebin(input, current_bins, target_bins_high)
output
end

construct_objective_cache(model::AbstractSpectralModel, domain::AbstractVector) = construct_objective_cache(preferred_support(model), model, domain)
construct_objective_cache(T::Type, model::AbstractSpectralModel, domain::AbstractVector) = construct_objective_cache(preferred_support(model), T, model, domain)
construct_objective_cache(layout::AbstractDataLayout, model::AbstractSpectralModel, domain::AbstractVector{T}) where {T} =
construct_objective_cache(layout, T, model, domain)
function construct_objective_cache(layout::AbstractDataLayout, T::Type, model::AbstractSpectralModel, domain)
construct_objective_cache(model::AbstractSpectralModel, domain::AbstractVector) =
construct_objective_cache(preferred_support(model), model, domain)
construct_objective_cache(T::Type, model::AbstractSpectralModel, domain::AbstractVector) =
construct_objective_cache(preferred_support(model), T, model, domain)
construct_objective_cache(
layout::AbstractDataLayout,
model::AbstractSpectralModel,
domain::AbstractVector{T},
) where {T} = construct_objective_cache(layout, T, model, domain)
function construct_objective_cache(
layout::AbstractDataLayout,
T::Type,
model::AbstractSpectralModel,
domain,
)
N = if layout isa OneToOne
length(domain)
elseif layout isa ContiguouslyBinned
Expand Down
32 changes: 25 additions & 7 deletions src/datasets/injectivedata.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ struct InjectiveData{V} <: AbstractDataset
name::String
end

function InjectiveData(domain, codomain; domain_variance = nothing, codomain_variance = nothing, name = "[no-name]")
function InjectiveData(
domain,
codomain;
domain_variance = nothing,
codomain_variance = nothing,
name = "[no-name]",
)
InjectiveData(domain, codomain, domain_variance, codomain_variance, name)
end

Expand All @@ -26,7 +32,10 @@ make_objective(::ContiguouslyBinned, dataset::InjectiveData) = dataset.codomain
make_domain(::OneToOne, dataset::InjectiveData) = dataset.domain
make_objective(::OneToOne, dataset::InjectiveData) = dataset.codomain

function make_objective_variance(::AbstractDataLayout, dataset::InjectiveData{V})::V where {V}
function make_objective_variance(
::AbstractDataLayout,
dataset::InjectiveData{V},
)::V where {V}
if !isnothing(dataset.domain_variance)
dataset.codomain_variance
else
Expand All @@ -35,13 +44,22 @@ function make_objective_variance(::AbstractDataLayout, dataset::InjectiveData{V}
end
end

objective_transformer(::AbstractDataLayout, dataset::InjectiveData) =
_DEFAULT_TRANSFORMER()
objective_transformer(::AbstractDataLayout, dataset::InjectiveData) = _DEFAULT_TRANSFORMER()

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:")
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:",
)
dmin, dmax = prettyfloat.(extrema(data.domain))
comin, comax = prettyfloat.(extrema(data.codomain))
descr = """ Name : $(data.name)
Expand All @@ -51,4 +69,4 @@ function _printinfo(io::IO, data::InjectiveData)
print(io, descr)
end

export InjectiveData
export InjectiveData
2 changes: 1 addition & 1 deletion src/datasets/nustart.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ function _printinfo(io, data::NuStarData{T}) where {T}
end


export NuStarData
export NuStarData
24 changes: 20 additions & 4 deletions src/datasets/response.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,29 @@ function build_response_matrix!(
end
end

function Base.show(io::IO, ::MIME{Symbol("text/plain")}, @nospecialize(rm::ResponseMatrix{T})) where {T}
function Base.show(
io::IO,
::MIME{Symbol("text/plain")},
@nospecialize(rm::ResponseMatrix{T})
) where {T}
nchans = length(rm.channels)
println(io, "ResponseMatrix with $nchans channels:")
Base.print_array(io, rm.matrix)
end

function _printinfo(io, resp::ResponseMatrix{T}) where {T}
emin, emax = prettyfloat(minimum(resp.bins_low)), prettyfloat(maximum(resp.bins_high))
c_emin, c_emax = prettyfloat(minimum(resp.channel_bins_low)), prettyfloat(maximum(resp.channel_bins_high))
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:")
println(
io,
"Response Matrix ",
Crayons.Crayon(foreground = :cyan),
"($ranks x $files)",
Crayons.Crayon(reset = true),
" channels:",
)
descr = """ . Chn. E (min/max) : ($c_emin, $c_emax)
. Domain E (min/max) : ($emin, $emax)
"""
Expand Down Expand Up @@ -112,7 +124,11 @@ fold_ancillary(response::ResponseMatrix, ancillary::AncillaryResponse) =

fold_ancillary(response::ResponseMatrix, ::Missing) = response.matrix

function Base.show(io::IO, ::MIME{Symbol("text/plain")}, @nospecialize(resp::AncillaryResponse{T})) where {T}
function Base.show(
io::IO,
::MIME{Symbol("text/plain")},
@nospecialize(resp::AncillaryResponse{T})
) where {T}
_printinfo(io, resp)
end

Expand Down
71 changes: 60 additions & 11 deletions src/datasets/spectraldata.jl
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,10 @@ macro _forward_SpectralData_api(args)
layout::SpectralFitting.AbstractDataLayout,
t::$(T),
) = SpectralFitting.make_domain_variance(layout, getproperty(t, $(field)))
SpectralFitting.make_objective(layout::SpectralFitting.AbstractDataLayout, t::$(T)) =
SpectralFitting.make_objective(layout, getproperty(t, $(field)))
SpectralFitting.make_objective(
layout::SpectralFitting.AbstractDataLayout,
t::$(T),
) = SpectralFitting.make_objective(layout, getproperty(t, $(field)))
SpectralFitting.make_objective_variance(
layout::SpectralFitting.AbstractDataLayout,
t::$(T),
Expand Down Expand Up @@ -305,40 +307,87 @@ end

# printing utilities

function Base.show(io::IO, @nospecialize(data::SpectralData))
function Base.show(io::IO, @nospecialize(data::SpectralData))
print(io, "SpectralData[$(data.spectrum.telescope_name)]")
end

function _printinfo(io, data::SpectralData{T}) where {T}
domain = @views data.domain
ce_min, ce_max = @views prettyfloat.(extrema(data.channel_to_energy[1:end-1][data.data_mask]))
ce_min, ce_max =
@views prettyfloat.(extrema(data.channel_to_energy[1:end-1][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.channel_to_energy[1:end-1][data.data_mask]) - 1, Crayons.Crayon(reset = true), " active channels:")
@views println(
io,
Crayons.Crayon(foreground = :cyan),
"SpectralData",
Crayons.Crayon(reset = true),
" with ",
Crayons.Crayon(foreground = :cyan),
length(data.channel_to_energy[1:end-1][data.data_mask]) - 1,
Crayons.Crayon(reset = true),
" 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 ")
print(
io,
Crayons.Crayon(foreground = :cyan),
"Primary Spectrum:",
Crayons.Crayon(reset = true),
"\n ",
)
_printinfo(io, data.spectrum)

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/datasets/xmm-newton.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ function _printinfo(io, data::XmmData{T}) where {T}
_printinfo(io, data.data)
end

export XmmData, XmmEPIC
export XmmData, XmmEPIC
16 changes: 14 additions & 2 deletions src/fitting/cache.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ struct SpectralCache{M,O,T,TransformerType} <: AbstractFittingCache
model_output::O
calculated_objective::T
transfomer!!::TransformerType
function SpectralCache(layout::AbstractDataLayout, model::M, domain, objective, transformer::XfmT) where {M,XfmT}
function SpectralCache(
layout::AbstractDataLayout,
model::M,
domain,
objective,
transformer::XfmT,
) where {M,XfmT}
model_output = DiffCache(construct_objective_cache(layout, model, domain))
calc_obj = similar(objective)
calc_obj .= 0
Expand Down Expand Up @@ -71,7 +77,13 @@ function FittingConfig(model::AbstractSpectralModel, dataset::AbstractDataset)
objective = make_objective(layout, dataset)
variance = make_objective_variance(layout, dataset)
params = freeparameters(model)
cache = SpectralCache(layout, model, domain, objective, objective_transformer(layout, dataset))
cache = SpectralCache(
layout,
model,
domain,
objective,
objective_transformer(layout, dataset),
)
FittingConfig(implementation(model), cache, params, domain, objective, variance)
end

Expand Down
5 changes: 1 addition & 4 deletions src/plotting-recipes.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using RecipesBase

@recipe function _plotting_func(
dataset::InjectiveData;
data_layout = OneToOne(),
)
@recipe function _plotting_func(dataset::InjectiveData; data_layout = OneToOne())
seriestype --> :scatter
markersize --> 1.0
markershape --> :none
Expand Down
6 changes: 3 additions & 3 deletions test/datasets/test-ogip.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ bg_nustar = OGIP.read_background(nustar_background_path, nustar_config)
# test reading the associated paths
xmm_paths = OGIP.read_paths_from_spectrum(xmm_spec_path)

@test xmm_paths[1]== xmm_backgroud_path
@test xmm_paths[2]== xmm_rmf_path
@test xmm_paths[3]== xmm_arf_path
@test xmm_paths[1] == xmm_backgroud_path
@test xmm_paths[2] == xmm_rmf_path
@test xmm_paths[3] == xmm_arf_path
2 changes: 1 addition & 1 deletion test/fitting/test-sample-data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ end

# path to the data directory
data1 =
SpectralFitting.XMMData(
SpectralFitting.XmmData(
SpectralFitting.XmmEPIC(),
joinpath(testdir, "xmm/pn_spec_grp.fits"),
) |> _prepare_data
Expand Down
2 changes: 1 addition & 1 deletion test/io/test-printing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,4 @@ if false
└ Σχ² = 39.000"""
@test string == expected
end
end

0 comments on commit 5662455

Please sign in to comment.