Skip to content

Commit 673965b

Browse files
committed
finish encode/decode and linear indexing
1 parent 4e63ebe commit 673965b

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/probabilities_estimators/histograms/rectangular_binning.jl

+18-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export RectangularBinEncoding
77
# Notice that the binning types are intermediate structs that are NOT retained
88
# in the source code. Their only purpose is instructions of how to create a
99
# `RectangularBinEncoder`. All actual source code functionality of `ValueHistogram`
10-
# is implemented based on `RectangularBinEncoder` and
10+
# is implemented based on `RectangularBinEncoder`.
1111

1212
"""
1313
RectangularBinning(ϵ) <: AbstractBinning
@@ -79,10 +79,12 @@ information as `ϵmin/max` is already an `NTuple`.
7979
8080
See also: [`RectangularBinning`](@ref), [`FixedRectangularBinning`](@ref).
8181
"""
82-
struct RectangularBinEncoding{B, V, E} <: Encoding
82+
struct RectangularBinEncoding{B, V, E, C, L} <: Encoding
8383
binning::B # either RectangularBinning or FixedRectangularBinning
8484
mini::V # fields are either static vectors or numbers
8585
edgelengths::E
86+
ci::C # cartesian indices
87+
li::L # linear indices
8688
end
8789

8890
function Base.show(io::IO, x::RectangularBinEncoding)
@@ -93,17 +95,20 @@ function Base.show(io::IO, x::RectangularBinEncoding)
9395
)
9496
end
9597

96-
function encode_as_bin(point, b::RectangularBinEncoding)
97-
(; mini, edgelengths) = b
98-
# Map a data point to its bin edge
99-
return floor.(Int, (point .- mini) ./ edgelengths)
98+
function encode(point, e::RectangularBinEncoding)
99+
(; mini, edgelengths) = e
100+
# Map a data point to its bin edge (plus one because indexing starts from 1)
101+
bin = floor.(Int, (point .- mini) ./ edgelengths) .+ 1
102+
return e.li[CartesianIndex(Tuple(bin))]
100103
end
101104

102-
function decode_from_bin(bin, b::RectangularBinEncoding{B, V}) where {B, V}
103-
(; mini, edgelengths) = b
105+
function decode(bin::Int, e::RectangularBinEncoding{B, V}) where {B, V}
106+
cartesian = e.ci[bin]
107+
(; mini, edgelengths) = e
104108
# Remove one because we want lowest value corner, and we get indices starting from 1
105-
return (V(Tuple(bin)) .- 1) .* edgelengths .+ mini
109+
return (V(Tuple(cartesian)) .- 1) .* edgelengths .+ mini
106110
end
111+
107112
function decode_from_bin(bin, b::RectangularBinEncoding{B, T}) where {B, T<:Real}
108113
(; mini, edgelengths) = b
109114
return (T(Tuple(bin)[1]) - 1)*edgelengths + mini
@@ -130,7 +135,10 @@ function RectangularBinEncoding(x::AbstractDataset{D,T}, b::RectangularBinning;
130135
error("Invalid ϵ for binning of a dataset")
131136
end
132137

133-
RectangularBinEncoding(b, mini, edgelengths)
138+
# Cartesian indices of the underlying histogram
139+
ci = CartesianIndices(ntuple(i -> length(x), D))
140+
li = LinearIndices(ci)
141+
RectangularBinEncoding(b, mini, edgelengths, ci, li)
134142
end
135143

136144
function RectangularBinEncoding(x::AbstractVector{<:Real}, b::RectangularBinning; n_eps = 2)

0 commit comments

Comments
 (0)