Skip to content

Data binning #139

Open
Open
@phajy

Description

@phajy

We can add new grouping methods such as minimum signal-to-noise ratio.

Multiple datasets could be grouped in the same way so they can be summed for fitting or plotting purposes. In addition provide a method to copying the binning from a template spectrum.

Some existing relevant functions include the following.

function regroup!(dataset::SpectralData, grouping; safety_copy = false)
grp::typeof(grouping) = if safety_copy
copy(grouping)
else
grouping
end
itt = GroupingIterator(grp)
for i in itt
dataset.energy_low[i[1]] = dataset.energy_low[i[2]]
dataset.energy_high[i[1]] = dataset.energy_high[i[3]]
end
if has_background(dataset)
regroup!(dataset.background, grp)
end
regroup!(dataset.response, grp)
regroup!(dataset.spectrum, grp)
resize!(dataset.data_mask, length(itt))
resize!(dataset.energy_low, length(itt))
resize!(dataset.energy_high, length(itt))
# set everything to unmasked
dataset.data_mask .= 1
dataset
end
function regroup!(dataset::SpectralData; min_counts = nothing)
if !isnothing(min_counts)
group_min_counts!(dataset.spectrum, min_counts)
end
regroup!(dataset, dataset.spectrum.grouping)
end

function group_min_counts!(spectrum::Spectrum, min_counts::Int)
NEW_GRP = 1
CONTINUE_GRP = 0
function _counts(x)
if spectrum.units == u"counts"
convert(Int, x)
elseif spectrum.units == u"counts / s"
convert(Int, x * spectrum.exposure_time)
end
end
sum::Int = 0
for (i, f) in enumerate(spectrum.data)
c = _counts(f)
sum += c
if sum >= min_counts
spectrum.grouping[i] = NEW_GRP
sum = 0
else
spectrum.grouping[i] = CONTINUE_GRP
end
end
end

And the file grouping.jl

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions