Skip to content

Commit

Permalink
Merge pull request #121 from tpapp/tp/remove-unpack
Browse files Browse the repository at this point in the history
Remove SimpleUnpack.@unpack, depend on Julia 1.10
  • Loading branch information
tpapp authored Jan 23, 2025
2 parents 227adfd + 84fa44f commit 95e5f29
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
fail-fast: false
matrix:
version:
- '1.6' # Replace this with the minimum Julia version that your package supports. E.g. if your package requires Julia 1.5 or higher, change this to '1.5'.
- '1.10' # Replace this with the minimum Julia version that your package supports. E.g. if your package requires Julia 1.5 or higher, change this to '1.5'.
- '1' # Leave this line unchanged. '1' will automatically expand to the latest stable 1.x release of Julia.
os:
- ubuntu-latest
Expand Down
6 changes: 2 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TransformVariables"
uuid = "84d833dd-6860-57f9-a1a7-6da5db126cff"
authors = ["Tamas K. Papp <[email protected]>"]
version = "0.8.11"
version = "0.9.0"

[deps]
ArgCheck = "dce04be8-c92d-5529-be00-80e4d2c0e197"
Expand All @@ -12,7 +12,6 @@ InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
LogExpFunctions = "2ab3a3ac-af41-5b50-aa03-7779005ae688"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SimpleUnPack = "ce78b400-467f-4804-87d8-8f486da07d0a"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[compat]
Expand All @@ -24,6 +23,5 @@ InverseFunctions = "0.1"
LinearAlgebra = "1.6"
LogExpFunctions = "0.3"
Random = "1.6"
SimpleUnPack = "1"
StaticArrays = "1"
julia = "1.6"
julia = "1.10"
1 change: 0 additions & 1 deletion src/TransformVariables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ using LogExpFunctions
using LinearAlgebra: UpperTriangular, logabsdet
using Random: AbstractRNG, GLOBAL_RNG
using StaticArrays: MMatrix, SMatrix, SArray, SVector, pushfirst
using SimpleUnPack: @unpack

import ChangesOfVariables
import InverseFunctions
Expand Down
24 changes: 12 additions & 12 deletions src/aggregation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct ArrayTransformation{T <: AbstractTransform,M} <: VectorTransform
end

function _summary_rows(transformation::ArrayTransformation, mime)
@unpack inner_transformation, dims = transformation
(; inner_transformation, dims) = transformation
_dims = foldr((a,b) -> "$(string(a))×$(string(b))", dims, init = "")
rows = _summary_row(transformation, _dims)
for row in _summary_rows(inner_transformation, mime)
Expand Down Expand Up @@ -71,7 +71,7 @@ function as(::Type{Matrix}, args...)
end

function transform_with(flag::LogJacFlag, transformation::ArrayTransformation, x, index::T) where {T}
@unpack inner_transformation, dims = transformation
(; inner_transformation, dims) = transformation
# NOTE not using index increments as that somehow breaks type inference
d = dimension(inner_transformation) # length of an element transformation
len = prod(dims) # number of elements
Expand Down Expand Up @@ -102,7 +102,7 @@ function _array_domain_label(inner_transformation, dims, index::Int)
end

function _domain_label(transformation::ArrayTransformation, index::Int)
@unpack inner_transformation, dims = transformation
(; inner_transformation, dims) = transformation
_array_domain_label(inner_transformation, dims, index)
end

Expand Down Expand Up @@ -139,7 +139,7 @@ function transform_with(flag::LogJacFlag, t::ViewTransformation, x, index)
end

function _domain_label(transformation::ViewTransformation, index::Int)
@unpack dims = transformation
(; dims) = transformation
_array_domain_label(asℝ, dims, index)
end

Expand Down Expand Up @@ -193,7 +193,7 @@ result_size(::StaticArrayTransformation{D,S}) where {D,S} = fieldtypes(S)

function transform_with(flag::LogJacFlag, transformation::StaticArrayTransformation{D,S},
x::AbstractVector{T}, index::Int) where {D,S,T}
@unpack inner_transformation = transformation
(; inner_transformation) = transformation
# NOTE this is a fix for #112, enforcing types taken from the transformation of the
# first element.
y1, ℓ1, index1 = transform_with(flag, inner_transformation, x, index)
Expand All @@ -219,7 +219,7 @@ end
function inverse_at!(x::AbstractVector, index,
transformation::Union{ArrayTransformation,StaticArrayTransformation},
y::AbstractArray)
@unpack inner_transformation = transformation
(; inner_transformation) = transformation
dims = result_size(transformation)
@argcheck size(y) == dims
for elt in vec(y)
Expand Down Expand Up @@ -267,7 +267,7 @@ struct TransformTuple{T} <: VectorTransform
end

function _summary_rows(transformation::TransformTuple, mime)
@unpack transformations = transformation
(; transformations) = transformation
repr1 = (transformations isa NamedTuple ? "NamedTuple" : "Tuple" ) * " of transformations"
rows = _summary_row(transformation, repr1)
_index = 0
Expand Down Expand Up @@ -369,13 +369,13 @@ function transform_with(flag::LogJacFlag, tt::TransformTuple{<:Tuple}, x, index)
end

function inverse_eltype(tt::TransformTuple{<:Tuple}, y::Tuple)
@unpack transformations = tt
(; transformations) = tt
@argcheck length(transformations) == length(y)
_inverse_eltype_tuple(transformations, y)
end

function inverse_at!(x::AbstractVector, index, tt::TransformTuple{<:Tuple}, y::Tuple)
@unpack transformations = tt
(; transformations) = tt
@argcheck length(transformations) == length(y)
_inverse!_tuple(x, index, tt.transformations, y)
end
Expand All @@ -384,19 +384,19 @@ as(transformations::NamedTuple{N,<:NTransforms}) where N =
TransformTuple(transformations)

function transform_with(flag::LogJacFlag, tt::TransformTuple{<:NamedTuple}, x, index)
@unpack transformations = tt
(; transformations) = tt
y, ℓ, index′ = transform_tuple(flag, values(transformations), x, index)
NamedTuple{keys(transformations)}(y), ℓ, index′
end

function inverse_eltype(tt::TransformTuple{<:NamedTuple}, y::NamedTuple)
@unpack transformations = tt
(; transformations) = tt
@argcheck _same_set_of_names(transformations, y)
_inverse_eltype_tuple(values(transformations), values(NamedTuple{keys(transformations)}(y)))
end

function inverse_at!(x::AbstractVector, index, tt::TransformTuple{<:NamedTuple}, y::NamedTuple)
@unpack transformations = tt
(; transformations) = tt
@argcheck _same_set_of_names(transformations, y)
_inverse!_tuple(x, index, values(transformations), values(NamedTuple{keys(transformations)}(y)))
end
Expand Down
4 changes: 2 additions & 2 deletions src/custom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ CustomTransform(n::Integer, f, flatten; kwargs...) =
dimension(t::CustomTransform) = dimension(t.g)

function transform_with(flag::NoLogJac, t::CustomTransform, x::AbstractVector, index)
@unpack g, f = t
(; g, f) = t
f(first(transform_with(flag, g, x, index))), flag, index + dimension(t)
end

function transform_with(flag::LogJac, t::CustomTransform, x::AbstractVector, index)
@unpack g, f, flatten, cfg = t
(; g, f, flatten, cfg) = t
index = firstindex(x)
index′ = index + dimension(g)
y, ℓ = value_and_logjac_forwarddiff(_custom_f(g, f), x[index:(index′ - 1)];
Expand Down
2 changes: 1 addition & 1 deletion src/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ function Base.show(io::IO, mime::MIME"text/plain", transformation::AbstractTran
print(io, only(rows).repr, " (dimension $(dimension(transformation)))")
else
for (i, row) in enumerate(rows)
@unpack level, indices, repr = row
(; level, indices, repr) = row
i > 1 && println(io)
print(io, ' '^(2 * (level - 1)))
indices nothing && print(io, '[', indices, "] ")
Expand Down
2 changes: 1 addition & 1 deletion src/scalar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ transform(t::ShiftedExp{D}, x::Real) where D =
transform_and_logjac(t::ShiftedExp, x::Real) = transform(t, x), x

function inverse(t::ShiftedExp{D}, x::Real) where D
@unpack shift = t
(; shift) = t
if D
@argcheck x > shift DomainError
log(x - shift)
Expand Down
12 changes: 6 additions & 6 deletions src/special_arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ but use `log(r)` for actual calculations so that large `y`s still give nonsingul
`ℓ` is the log Jacobian (whether it is evaluated depends on `flag`).
"""
@inline function l2_remainder_transform(flag::LogJacFlag, x, log_r)
@unpack logjac, log_l2_rem = tanh_helpers(x)
(; logjac, log_l2_rem) = tanh_helpers(x)
# note that 1-tanh(x)^2 = sech(x)^2
(tanh(x) * exp(log_r / 2),
log_r + log_l2_rem,
Expand Down Expand Up @@ -80,7 +80,7 @@ function _summary_rows(transformation::UnitVector, mime)
end

function transform_with(flag::LogJacFlag, t::UnitVector, x::AbstractVector, index)
@unpack n = t
(; n) = t
T = robust_eltype(x)
log_r = zero(T)
y = Vector{T}(undef, n)
Expand All @@ -98,7 +98,7 @@ end
inverse_eltype(t::UnitVector, y::AbstractVector) = robust_eltype(y)

function inverse_at!(x::AbstractVector, index, t::UnitVector, y::AbstractVector)
@unpack n = t
(; n) = t
@argcheck length(y) == n
log_r = zero(eltype(y))
@inbounds for yi in axes(y, 1)[1:(end-1)]
Expand Down Expand Up @@ -133,7 +133,7 @@ end
dimension(t::UnitSimplex) = t.n - 1

function transform_with(flag::LogJacFlag, t::UnitSimplex, x::AbstractVector, index)
@unpack n = t
(; n) = t
T = robust_eltype(x)

= logjac_zero(flag, T)
Expand All @@ -160,7 +160,7 @@ end
inverse_eltype(t::UnitSimplex, y::AbstractVector) = robust_eltype(y)

function inverse_at!(x::AbstractVector, index, t::UnitSimplex, y::AbstractVector)
@unpack n = t
(; n) = t
@argcheck length(y) == n

stick = one(eltype(y))
Expand Down Expand Up @@ -210,7 +210,7 @@ struct CorrCholeskyFactor <: VectorTransform
end

function _summary_rows(transformation::CorrCholeskyFactor, mime)
@unpack n = transformation
(; n) = transformation
_summary_row(transformation, "$(n)×$(n) correlation cholesky factor")
end

Expand Down
1 change: 0 additions & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ LogDensityProblemsAD = "996a588d-648d-4e1f-a8f0-a84b347e47b1"
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SimpleUnPack = "ce78b400-467f-4804-87d8-8f486da07d0a"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TransformedLogDensities = "f9bc47f6-f3f8-4f3b-ab21-f8bc73906f26"
11 changes: 5 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using DocStringExtensions, LinearAlgebra, LogDensityProblems, OffsetArrays, SimpleUnPack,
Random, Test, TransformVariables, StaticArrays, TransformedLogDensities,
LogDensityProblemsAD
using DocStringExtensions, LinearAlgebra, LogDensityProblems, OffsetArrays, Random, Test,
TransformVariables, StaticArrays, TransformedLogDensities, LogDensityProblemsAD
import ForwardDiff
using LogDensityProblems: logdensity, logdensity_and_gradient
using LogDensityProblemsAD
Expand Down Expand Up @@ -140,7 +139,7 @@ end
@testset "tanh helpers" begin
for _ in 1:10000
x = (rand() - 0.5) * 100
@unpack log_l2_rem, logjac = TransformVariables.tanh_helpers(x)
(; log_l2_rem, logjac) = TransformVariables.tanh_helpers(x)
@test Float64(AD_logjac(tanh, BigFloat(x))) logjac atol = 1e-4
@test Float64(log(sech(BigFloat(x))^2)) log_l2_rem atol = 1e-4
end
Expand Down Expand Up @@ -405,7 +404,7 @@ end
u = UnitVector(3), L = CorrCholeskyFactor(4),
δ = as((asℝ₋, as𝕀))))
function f(θ)
@unpack μ, σ, β, α, δ = θ
(; μ, σ, β, α, δ) = θ
-(abs2(μ) + abs2(σ) + abs2(β) + α + δ[1] + δ[2])
end
P = TransformedLogDensities.TransformedLogDensity(t, f)
Expand Down Expand Up @@ -452,7 +451,7 @@ end
# # NOTE tests simplified disabled as they currently fail
# t = as((μ = asℝ, ))
# function f(θ)
# @unpack μ = θ
# (; μ) = θ
# -(abs2(μ))
# end
# P = TransformedLogDensity(t, f)
Expand Down

2 comments on commit 95e5f29

@tpapp
Copy link
Owner Author

@tpapp tpapp commented on 95e5f29 Jan 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register

Release notes:

  • remove SimpleUnpack.@unpack, depend on Julia 1.10

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/123565

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.9.0 -m "<description of version>" 95e5f293abde34203347f76ab2956be616eb5008
git push origin v0.9.0

Please sign in to comment.