Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 353: Use map to assign prefixes in modifier models #356

Merged
merged 3 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions EpiAware/src/EpiLatentModels/manipulators/CombineLatentModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@ latent_model()
P <: AbstractVector{<:String}}
@assert length(models)>1 "At least two models are required"
@assert length(models)==length(prefixes) "The number of models and prefixes must be equal"
for i in eachindex(models)
if (prefixes[i] != "")
models[i] = PrefixLatentModel(models[i], prefixes[i])
end
end
prefix_models = [prefixes[i] == "" ? models[i] :
seabbs marked this conversation as resolved.
Show resolved Hide resolved
PrefixLatentModel(models[i], prefixes[i])
for i in eachindex(models)]
return new{AbstractVector{<:AbstractTuringLatentModel}, AbstractVector{<:String}}(
models, prefixes)
prefix_models, prefixes)
end

function CombineLatentModels(models::M) where {
Expand Down
10 changes: 4 additions & 6 deletions EpiAware/src/EpiLatentModels/manipulators/ConcatLatentModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@ struct ConcatLatentModels{
@assert typeof(check_dim)<:AbstractVector{Int} "Output of dimension_adaptor must be a vector of integers"
@assert length(check_dim)==no_models "The vector of dimensions must have the same length as the number of models"
@assert length(prefixes)==no_models "The number of models and prefixes must be equal"
for i in eachindex(models)
if (prefixes[i] != "")
models[i] = PrefixLatentModel(models[i], prefixes[i])
end
end
prefix_models = [prefixes[i] == "" ? models[i] :
PrefixLatentModel(models[i], prefixes[i])
for i in eachindex(models)]
return new{
AbstractVector{<:AbstractTuringLatentModel}, Int, Function,
AbstractVector{<:String}}(
models, no_models, dimension_adaptor, prefixes)
prefix_models, no_models, dimension_adaptor, prefixes)
end

function ConcatLatentModels(models::M, dimension_adaptor::Function;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ struct Ascertainment{
latent_prefix::P) where {
M <: AbstractTuringObservationModel, T <: AbstractTuringLatentModel,
F <: Function, P <: String}
if (latent_prefix != "")
latent_model = PrefixLatentModel(latent_model, latent_prefix)
end
prefix_model = latent_prefix == "" ? latent_model :
PrefixLatentModel(latent_model, latent_prefix)
return new{M, AbstractTuringLatentModel, F, P}(
model, latent_model, link, latent_prefix)
model, prefix_model, link, latent_prefix)
end

function Ascertainment(model::M,
Expand Down
11 changes: 11 additions & 0 deletions EpiAware/test/EpiLatentModels/manipulators/CombineLatentModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@
@test comb.prefixes == ["Int", "AR"]
end

@testitem "CombineLatentModels constructor handles duplicate models" begin
using Distributions: Normal
comb = CombineLatentModels([Intercept(Normal(0, 1)), Intercept(Normal(0, 2))])
prefix_1 = PrefixLatentModel(Intercept(Normal(0, 1)), "Combine.1")
prefix_2 = PrefixLatentModel(Intercept(Normal(0, 2)), "Combine.2")

@test typeof(comb) <: AbstractTuringLatentModel
@test comb.models == [prefix_1, prefix_2]
@test comb.prefixes == ["Combine.1", "Combine.2"]
end

@testitem "CombineLatentModels generate_latent method works as expected: FixedIntecept + custom" begin
using Turing

Expand Down
13 changes: 13 additions & 0 deletions EpiAware/test/EpiLatentModels/manipulators/ConcatLatentModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@
@test concat_prefix.prefixes == ["Int", "AR"]
end

@testitem "ConcatLatentmodels constructor works with duplicate models" begin
using Distributions: Normal
concat = ConcatLatentModels([Intercept(Normal(0, 1)), Intercept(Normal(0, 2))])
prefix_1 = PrefixLatentModel(Intercept(Normal(0, 1)), "Concat.1")
prefix_2 = PrefixLatentModel(Intercept(Normal(0, 2)), "Concat.2")

@test typeof(concat) <: AbstractTuringLatentModel
@test concat.models == [prefix_1, prefix_2]
@test concat.no_models == 2
@test concat.dimension_adaptor == equal_dimensions
@test concat.prefixes == ["Concat.1", "Concat.2"]
end

@testitem "ConcatLatentModels generate_latent method works as expected: FixedIntecept + custom" begin
using Turing

Expand Down
Loading