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

Plot unfolded data and model #111

Merged
merged 7 commits into from
Jun 18, 2024
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
15 changes: 15 additions & 0 deletions src/datasets/spectraldata.jl
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,21 @@ function rescale_background!(data::SpectralData)
data
end

# for invoking a model on the same domain as the spectral data
function invokemodel(
data::SpectralData,
model::AbstractSpectralModel,
new_free_params = nothing,
)
domain = make_output_domain(common_support(data, model), data)
cache = make_parameter_cache(model)
if !isnothing(new_free_params)
update_free_parameters!(cache, new_free_params)
end
output = allocate_model_output(model, domain)
invokemodel!(output, domain, model, cache)[data.data_mask]
end

macro _forward_SpectralData_api(args)
if args.head !== :.
error("Bad syntax")
Expand Down
65 changes: 65 additions & 0 deletions src/plots-recipes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,71 @@ end
end
end

# unfolded plots
@userplot UnfoldedPlot
@recipe function _plotting_fun(
r::UnfoldedPlot;
pow = 2,
datacolor = :auto,
modelcolor = :auto,
label = :auto,
)
if length(r.args) != 1 || !(typeof(r.args[1]) <: AbstractFittingResult)
error(
"Unfolded plots first argument must be `AbstractDataset` and second argument of type `AbstractFittingResult`.",
)
end

result = r.args[1] isa FittingResult ? r.args[1][1] : r.args[1]
data = get_dataset(result)
model = get_model(result)

x_plot = plotting_domain(data)

y_folded = invoke_result(result, result.u)
y_unfolded = invokemodel(data.data, model, result.u)
Δx = bin_widths(data)

unfolded_spectrum = @. x_plot^pow * result.objective * (y_unfolded / y_folded) / Δx
unfolded_std = @. sqrt(result.variance) * x_plot^pow * (y_unfolded / y_folded) / Δx
unfolded_model = @. x_plot^pow * y_unfolded / Δx

# TODO: use unitful units to automatically label the x and y axes - this would be a great feature
pow_prefix = pow == 0 ? "" : "E^$pow ("
pow_suffix = pow == 0 ? "" : ")"
ylabel --> pow_prefix * "Photons cm^-2 s^-1 keV^-1" * pow_suffix
xlabel --> "Energy (keV)"
minorgrid --> true

if (label == :auto)
label = make_label(data)
end

# unfolded data
@series begin
markerstrokecolor --> datacolor
label --> label
legend --> :none
seriestype --> :scatter
markershape --> :none
markersize --> 0.5
yerror --> unfolded_std
xerror --> SpectralFitting.bin_widths(data) ./ 2
x_plot, unfolded_spectrum
end

# unfolded model
@series begin
markerstrokecolor --> modelcolor
label --> label
legend --> :none
seriestype --> :line
markershape --> :none
markersize --> 0.5
x_plot, unfolded_model
end
end

"""
get_tickslogscale(lims; skiplog=false)

Expand Down