Skip to content

Commit

Permalink
Extend AxisKeys.named_axiskeys (#45)
Browse files Browse the repository at this point in the history
* Add erroring test

* Extend `AxisKeys.named_axiskeys`

* Bump version

* Use `named_axiskeys` in `_rand!` and `canonform`

* Bump patch version
  • Loading branch information
mjp98 authored Nov 15, 2022
1 parent 2813aa1 commit 0d8a52d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "KeyedDistributions"
uuid = "2576fb08-064d-4cab-b15d-8dda7fcb9a6d"
authors = ["Invenia Technical Computing Corporation"]
version = "0.1.13"
version = "0.1.14"

[deps]
AutoHashEquals = "15f4f7f2-30c1-5605-9d31-71845cf9641f"
Expand Down
9 changes: 7 additions & 2 deletions src/KeyedDistributions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ AxisKeys.haskeys(d::KeyedDistOrSampleable) = true
AxisKeys.keyless(d::KeyedDistOrSampleable) = distribution(d)
AxisKeys.keyless_unname(d::KeyedDistOrSampleable) = distribution(d)

function AxisKeys.named_axiskeys(d::KeyedDistOrSampleable)
NT = NamedTuple{dimnames(d)}
return NT(axiskeys(d))
end

# NamedDims functionality
for T in (:Distribution, :Sampleable)
KeyedT = Symbol(:Keyed, T)
Expand Down Expand Up @@ -175,7 +180,7 @@ function Distributions._rand!(
x::AbstractVector{<:Real}
)
sample = Distributions._rand!(rng, distribution(d), x)
named_keys = NamedTuple{dimnames(d)}(axiskeys(d))
named_keys = named_axiskeys(d)
return KeyedArray(sample; named_keys...)
end

Expand Down Expand Up @@ -250,7 +255,7 @@ Distributions.entropy(d::KeyedDistribution) = entropy(distribution(d))
Distributions.entropy(d::KeyedDistribution, b::Real) = entropy(distribution(d), b)

function Distributions.canonform(d::KeyedDistribution)
named_keys = NamedTuple{dimnames(d)}(axiskeys(d)) # Define/exists a shorthand for this?
named_keys = named_axiskeys(d)
return KeyedDistribution(canonform(distribution(d)); named_keys...)
end

Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ using Test
@test distribution(kd) == d
@test axiskeys(kd) == keys
@test dimnames(kd) == names
@test named_axiskeys(kd) == named_keys
@test length(kd) == length(d) == 3
@test isequal(kd, T(d; id=[:a, :b, :c]))
@test params(kd) == params(d) == (m, s)
Expand Down

2 comments on commit 0d8a52d

@mjp98
Copy link
Member Author

@mjp98 mjp98 commented on 0d8a52d Nov 15, 2022

Choose a reason for hiding this comment

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

@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/72241

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.1.14 -m "<description of version>" 0d8a52d19fb4e4ae1583752230d71f2cb468f4e9
git push origin v0.1.14

Please sign in to comment.