Skip to content

Commit 30252dc

Browse files
committed
Rename getindex_raw to getindex_internal
1 parent 45c89c4 commit 30252dc

File tree

3 files changed

+30
-32
lines changed

3 files changed

+30
-32
lines changed

src/varinfo.jl

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -637,9 +637,6 @@ getindex_internal(vi::VarInfo, vn::VarName) = getindex_internal(getmetadata(vi,
637637
# what a bijector would result in, even if the input is a view (`SubArray`).
638638
# TODO(torfjelde): An alternative is to implement `view` directly instead.
639639
getindex_internal(md::Metadata, vn::VarName) = getindex(md.vals, getrange(md, vn))
640-
# TODO(mhauru) Maybe rename getindex_raw to getindex_internal and obviate the need for this
641-
# method.
642-
getindex_internal(vnv::VarNamedVector, vn::VarName) = getindex_raw(vnv, vn)
643640

644641
function getindex_internal(vi::VarInfo, vns::Vector{<:VarName})
645642
return mapreduce(Base.Fix1(getindex_internal, vi), vcat, vns)
@@ -676,7 +673,7 @@ function getall(md::Metadata)
676673
Base.Fix1(getindex_internal, md), vcat, md.vns; init=similar(md.vals, 0)
677674
)
678675
end
679-
getall(vnv::VarNamedVector) = getindex_raw(vnv, Colon())
676+
getall(vnv::VarNamedVector) = getindex_internal(vnv, Colon())
680677

681678
"""
682679
setall!(vi::VarInfo, val)
@@ -1439,7 +1436,7 @@ function _link_metadata!!(
14391436
# First transform from however the variable is stored in vnv to the model
14401437
# representation.
14411438
transform_to_orig = gettransform(metadata, vn)
1442-
val_old = getindex_raw(metadata, vn)
1439+
val_old = getindex_internal(metadata, vn)
14431440
val_orig, logjac1 = with_logabsdet_jacobian(transform_to_orig, val_old)
14441441
# Then transform from the model representation to the linked representation.
14451442
transform_from_linked = from_linked_vec_transform(dists[vn])
@@ -1559,7 +1556,7 @@ function _invlink_metadata!!(
15591556
vns = target_vns === nothing ? keys(metadata) : target_vns
15601557
for vn in vns
15611558
transform = gettransform(metadata, vn)
1562-
old_val = getindex_raw(metadata, vn)
1559+
old_val = getindex_internal(metadata, vn)
15631560
new_val, logjac = with_logabsdet_jacobian(transform, old_val)
15641561
# TODO(mhauru) We are calling a !! function but ignoring the return value.
15651562
acclogp!!(varinfo, -logjac)

src/varnamedvector.jl

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,9 @@ Base.pairs(vnv::VarNamedVector) = (vn => vnv[vn] for vn in keys(vnv))
298298
Base.haskey(vnv::VarNamedVector, vn::VarName) = haskey(vnv.varname_to_index, vn)
299299

300300
# `getindex` & `setindex!`
301-
Base.getindex(vnv::VarNamedVector, i::Int) = getindex_raw(vnv, i)
301+
Base.getindex(vnv::VarNamedVector, i::Int) = getindex_internal(vnv, i)
302302
function Base.getindex(vnv::VarNamedVector, vn::VarName)
303-
x = getindex_raw(vnv, vn)
303+
x = getindex_internal(vnv, vn)
304304
f = gettransform(vnv, vn)
305305
return f(x)
306306
end
@@ -369,15 +369,15 @@ function index_to_vals_index(vnv::VarNamedVector, i::Int)
369369
end
370370

371371
"""
372-
getindex_raw(vnv::VarNamedVector, i::Int)
373-
getindex_raw(vnv::VarNamedVector, vn::VarName)
372+
getindex_internal(vnv::VarNamedVector, i::Int)
373+
getindex_internal(vnv::VarNamedVector, vn::VarName)
374374
375375
Like `getindex`, but returns the values as they are stored in `vnv` without transforming.
376376
377377
For integer indices this is the same as `getindex`, but for `VarName`s this is different.
378378
"""
379-
getindex_raw(vnv::VarNamedVector, i::Int) = vnv.vals[index_to_vals_index(vnv, i)]
380-
getindex_raw(vnv::VarNamedVector, vn::VarName) = vnv.vals[getrange(vnv, vn)]
379+
getindex_internal(vnv::VarNamedVector, i::Int) = vnv.vals[index_to_vals_index(vnv, i)]
380+
getindex_internal(vnv::VarNamedVector, vn::VarName) = vnv.vals[getrange(vnv, vn)]
381381

382382
# `getindex` for `Colon`
383383
function Base.getindex(vnv::VarNamedVector, ::Colon)
@@ -388,34 +388,34 @@ function Base.getindex(vnv::VarNamedVector, ::Colon)
388388
end
389389
end
390390

391-
getindex_raw(vnv::VarNamedVector, ::Colon) = getindex(vnv, Colon())
391+
getindex_internal(vnv::VarNamedVector, ::Colon) = getindex(vnv, Colon())
392392

393393
# TODO(mhauru): Remove this as soon as possible. Only needed because of the old Gibbs
394394
# sampler.
395395
function Base.getindex(vnv::VarNamedVector, spl::AbstractSampler)
396396
throw(ErrorException("Cannot index a VarNamedVector with a sampler."))
397397
end
398398

399-
Base.setindex!(vnv::VarNamedVector, val, i::Int) = setindex_raw!(vnv, val, i)
399+
Base.setindex!(vnv::VarNamedVector, val, i::Int) = setindex_internal!(vnv, val, i)
400400
function Base.setindex!(vnv::VarNamedVector, val, vn::VarName)
401401
# Since setindex! does not change the transform, we need to apply it to `val`.
402402
f = inverse(gettransform(vnv, vn))
403-
return setindex_raw!(vnv, f(val), vn)
403+
return setindex_internal!(vnv, f(val), vn)
404404
end
405405

406406
"""
407-
setindex_raw!(vnv::VarNamedVector, val, i::Int)
408-
setindex_raw!(vnv::VarNamedVector, val, vn::VarName)
407+
setindex_internal!(vnv::VarNamedVector, val, i::Int)
408+
setindex_internal!(vnv::VarNamedVector, val, vn::VarName)
409409
410410
Like `setindex!`, but sets the values as they are stored in `vnv` without transforming.
411411
412412
For integer indices this is the same as `setindex!`, but for `VarName`s this is different.
413413
"""
414-
function setindex_raw!(vnv::VarNamedVector, val, i::Int)
414+
function setindex_internal!(vnv::VarNamedVector, val, i::Int)
415415
return vnv.vals[index_to_vals_index(vnv, i)] = val
416416
end
417417

418-
function setindex_raw!(vnv::VarNamedVector, val::AbstractVector, vn::VarName)
418+
function setindex_internal!(vnv::VarNamedVector, val::AbstractVector, vn::VarName)
419419
return vnv.vals[getrange(vnv, vn)] = val
420420
end
421421

@@ -565,13 +565,13 @@ function Base.merge(left_vnv::VarNamedVector, right_vnv::VarNamedVector)
565565
# Extract the necessary information from `left` or `right`.
566566
if vn in vns_left && !(vn in vns_right)
567567
# `vn` is only in `left`.
568-
val = getindex_raw(left_vnv, vn)
568+
val = getindex_internal(left_vnv, vn)
569569
f = gettransform(left_vnv, vn)
570570
is_unconstrained[idx] = istrans(left_vnv, vn)
571571
else
572572
# `vn` is either in both or just `right`.
573573
# Note that in a `merge` the right value has precedence.
574-
val = getindex_raw(right_vnv, vn)
574+
val = getindex_internal(right_vnv, vn)
575575
f = gettransform(right_vnv, vn)
576576
is_unconstrained[idx] = istrans(right_vnv, vn)
577577
end
@@ -621,7 +621,7 @@ function subset(vnv::VarNamedVector, vns_given::AbstractVector{VN}) where {VN<:V
621621
isempty(vnv) && return vnv_new
622622

623623
for vn in vns
624-
push!(vnv_new, vn, getindex_raw(vnv, vn), gettransform(vnv, vn))
624+
push!(vnv_new, vn, getindex_internal(vnv, vn), gettransform(vnv, vn))
625625
settrans!(vnv_new, istrans(vnv, vn), vn)
626626
end
627627

@@ -977,7 +977,7 @@ end
977977
set!!(vnv::VarNamedVector, vn::VarName, val) = update!!(vnv, vn, val)
978978

979979
function setval!(vnv::VarNamedVector, val, vn::VarName)
980-
return setindex_raw!(vnv, tovec(val), vn)
980+
return setindex_internal!(vnv, tovec(val), vn)
981981
end
982982

983983
function recontiguify_ranges!(ranges::AbstractVector{<:AbstractRange})

test/varnamedvector.jl

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -228,25 +228,26 @@ end
228228
@test vnv[vn_right] == val_right .+ 100
229229
end
230230

231-
# `getindex_raw`
232-
@testset "getindex_raw" begin
231+
# `getindex_internal`
232+
@testset "getindex_internal" begin
233233
# With `VarName` index.
234-
@test DynamicPPL.getindex_raw(vnv_base, vn_left) == to_vec_left(val_left)
235-
@test DynamicPPL.getindex_raw(vnv_base, vn_right) == to_vec_right(val_right)
234+
@test DynamicPPL.getindex_internal(vnv_base, vn_left) == to_vec_left(val_left)
235+
@test DynamicPPL.getindex_internal(vnv_base, vn_right) ==
236+
to_vec_right(val_right)
236237
# With `Int` index.
237238
val_vec = vcat(to_vec_left(val_left), to_vec_right(val_right))
238239
@test all(
239-
DynamicPPL.getindex_raw(vnv_base, i) == val_vec[i] for
240+
DynamicPPL.getindex_internal(vnv_base, i) == val_vec[i] for
240241
i in 1:length(val_vec)
241242
)
242243
end
243244

244-
# `setindex_raw!`
245-
@testset "setindex_raw!" begin
245+
# `setindex_internal!`
246+
@testset "setindex_internal!" begin
246247
vnv = deepcopy(vnv_base)
247-
DynamicPPL.setindex_raw!(vnv, to_vec_left(val_left .+ 100), vn_left)
248+
DynamicPPL.setindex_internal!(vnv, to_vec_left(val_left .+ 100), vn_left)
248249
@test vnv[vn_left] == val_left .+ 100
249-
DynamicPPL.setindex_raw!(vnv, to_vec_right(val_right .+ 100), vn_right)
250+
DynamicPPL.setindex_internal!(vnv, to_vec_right(val_right .+ 100), vn_right)
250251
@test vnv[vn_right] == val_right .+ 100
251252
end
252253

0 commit comments

Comments
 (0)