Skip to content

Commit ef23961

Browse files
committed
rework valuehistogram to store the encoding directly
1 parent 673965b commit ef23961

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

src/probabilities_estimators/histograms/rectangular_binning.jl

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
export RectangularBinning, FixedRectangularBinning
22
export RectangularBinEncoding
33

4+
abstract type AbstractBinning end
5+
abstract type HistogramEncoding <: Encoding end
6+
47
##################################################################
58
# Structs and docstrings
69
##################################################################
@@ -79,7 +82,7 @@ information as `ϵmin/max` is already an `NTuple`.
7982
8083
See also: [`RectangularBinning`](@ref), [`FixedRectangularBinning`](@ref).
8184
"""
82-
struct RectangularBinEncoding{B, V, E, C, L} <: Encoding
85+
struct RectangularBinEncoding{B, V, E, C, L} <: HistogramEncoding
8386
binning::B # either RectangularBinning or FixedRectangularBinning
8487
mini::V # fields are either static vectors or numbers
8588
edgelengths::E

src/probabilities_estimators/histograms/value_histogram.jl

+20-23
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
export ValueHistogram, VisitationFrequency, AbstractBinning
2-
3-
"""
4-
AbstractBinning
5-
6-
The supertype of all binning schemes.
7-
"""
8-
abstract type AbstractBinning end
9-
1+
export ValueHistogram, VisitationFrequency
102
# We need binning to be defined first to add it as a field to a struct
113
include("rectangular_binning.jl")
124
include("fasthist.jl")
@@ -42,10 +34,19 @@ are returned as `SVector`s.
4234
4335
See also: [`RectangularBinning`](@ref).
4436
"""
45-
struct ValueHistogram{B<:AbstractBinning} <: ProbabilitiesEstimator
46-
binning::B
37+
struct ValueHistogram{H<:HistogramEncoding} <: ProbabilitiesEstimator
38+
encoding::H
4739
end
4840
ValueHistogram::Union{Real,Vector}) = ValueHistogram(RectangularBinning(ϵ))
41+
function ValueHistogram(x, b::AbstractBinning)
42+
encoding = RectangularBinEncoding(x, b)
43+
return ValueHistogram(encoding)
44+
end
45+
function ValueHistogram(b::FixedRectangularBinning)
46+
encoding = RectangularBinEncoding(b)
47+
return ValueHistogram(encoding)
48+
end
49+
4950

5051
"""
5152
VisitationFrequency
@@ -54,24 +55,20 @@ An alias for [`ValueHistogram`](@ref).
5455
"""
5556
const VisitationFrequency = ValueHistogram
5657

57-
# For organizational outcomes we extend methods here. However, their
58-
# source code in truth is in the binnings file using the bin encoding
59-
60-
# This method is only valid for rectangular binnings, as `fasthist`
61-
# is only valid for rectangular binnings. For more binnings, it needs to be extended.
58+
# The source code of `ValueHistogram` operates as rather simple calls to
59+
# the underlying encoding and the `fasthist` function and extensions.
60+
# See the `rectangular_binning.jl` file for more.
6261
function probabilities(x::Array_or_Dataset, est::ValueHistogram)
63-
# and the `fasthist` actually just makes an encoding,
64-
# this function is in `rectangular_binning.jl`
65-
Probabilities(fasthist(x, est.binning)[1])
62+
Probabilities(fasthist(x, est.encoding)[1])
6663
end
6764

6865
function probabilities_and_outcomes(x::Array_or_Dataset, est::ValueHistogram)
69-
probs, bins, encoder = fasthist(x, est.binning)
66+
probs, bins = fasthist(x, est.encoding) # bins are integers here
7067
unique!(bins) # `bins` is already sorted from `fasthist!`
7168
# Here we transfor the cartesian coordinate based bins into data unit bins:
72-
outcomes = map(b -> decode_from_bin(b, encoder), bins)
69+
outcomes = map(b -> decode(b, encoder), bins)
7370
return Probabilities(probs), vec(outcomes)
7471
end
7572

76-
outcome_space(x, est::ValueHistogram) = outcome_space(x, est.binning)
77-
total_outcomes(x, est::ValueHistogram) = total_outcomes(x, est.binning)
73+
outcome_space(x, est::ValueHistogram) = outcome_space(x, est.encoding)
74+
total_outcomes(x, est::ValueHistogram) = total_outcomes(x, est.encoding)

0 commit comments

Comments
 (0)