@@ -7,7 +7,7 @@ export RectangularBinEncoding
7
7
# Notice that the binning types are intermediate structs that are NOT retained
8
8
# in the source code. Their only purpose is instructions of how to create a
9
9
# `RectangularBinEncoder`. All actual source code functionality of `ValueHistogram`
10
- # is implemented based on `RectangularBinEncoder` and
10
+ # is implemented based on `RectangularBinEncoder`.
11
11
12
12
"""
13
13
RectangularBinning(ϵ) <: AbstractBinning
@@ -79,10 +79,12 @@ information as `ϵmin/max` is already an `NTuple`.
79
79
80
80
See also: [`RectangularBinning`](@ref), [`FixedRectangularBinning`](@ref).
81
81
"""
82
- struct RectangularBinEncoding{B, V, E} <: Encoding
82
+ struct RectangularBinEncoding{B, V, E, C, L } <: Encoding
83
83
binning:: B # either RectangularBinning or FixedRectangularBinning
84
84
mini:: V # fields are either static vectors or numbers
85
85
edgelengths:: E
86
+ ci:: C # cartesian indices
87
+ li:: L # linear indices
86
88
end
87
89
88
90
function Base. show (io:: IO , x:: RectangularBinEncoding )
@@ -93,17 +95,20 @@ function Base.show(io::IO, x::RectangularBinEncoding)
93
95
)
94
96
end
95
97
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))]
100
103
end
101
104
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
104
108
# 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
106
110
end
111
+
107
112
function decode_from_bin (bin, b:: RectangularBinEncoding{B, T} ) where {B, T<: Real }
108
113
(; mini, edgelengths) = b
109
114
return (T (Tuple (bin)[1 ]) - 1 )* edgelengths + mini
@@ -130,7 +135,10 @@ function RectangularBinEncoding(x::AbstractDataset{D,T}, b::RectangularBinning;
130
135
error (" Invalid ϵ for binning of a dataset" )
131
136
end
132
137
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)
134
142
end
135
143
136
144
function RectangularBinEncoding (x:: AbstractVector{<:Real} , b:: RectangularBinning ; n_eps = 2 )
0 commit comments