Skip to content

Commit

Permalink
Update set_factor_bounds function name to use ! suffix and update tes…
Browse files Browse the repository at this point in the history
…ts accordingly.
  • Loading branch information
Pascal Omondiagbe committed Feb 24, 2025
1 parent 705c65e commit 58819c4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
16 changes: 10 additions & 6 deletions src/io/sampling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,8 @@ set_factor_bounds(dom, :wave_stress, (0.1, 0.2))
```
"""
function set_factor_bounds(dom::Domain, factor::Symbol, new_dist_params::Tuple)::Domain
Base.@warn "set_factor_bounds is deprecated, use set_factor_bounds! instead" maxlog=1 _category=:deprecation
Base.@warn "set_factor_bounds is deprecated, use set_factor_bounds! instead" maxlog = 1
_category = :deprecation

set_factor_bounds!(dom, factor, new_dist_params)
return dom
Expand All @@ -528,16 +529,17 @@ function set_factor_bounds!(dom::Domain, factor::Symbol, new_dist_params::Tuple)
ms[!, :is_constant] .= (ms[!, :lower_bound] .== ms[!, :upper_bound])

update!(dom, ms)
return nothing
return dom
end

function set_factor_bounds(dom::Domain; factors...)::Domain
Base.@warn "set_factor_bounds is deprecated, use set_factor_bounds! instead" maxlog=1 _category=:deprecation
Base.@warn "set_factor_bounds is deprecated, use set_factor_bounds! instead" maxlog = 1
_category = :deprecation

set_factor_bounds!(dom; factors...)
return dom
end
function set_factor_bounds!(dom::Domain; factors...)::Nothing
function set_factor_bounds!(dom::Domain; factors...)::Domain
ms = model_spec(dom)
factor_symbols = collect(keys(factors))
factor_rows = findall(in(factor_symbols), ms.fieldname)
Expand All @@ -549,13 +551,15 @@ function set_factor_bounds!(dom::Domain; factors...)::Nothing
# Calculate new values preserving types
old_vals = ms[factor_rows, :val]
new_vals = mean.(zip(first.(new_params), last.(new_params)))
ms[factor_rows, :val] .= [v isa Int ? round(n) : n for (v, n) in zip(old_vals, new_vals)]
ms[factor_rows, :val] .= [
v isa Int ? round(n) : n for (v, n) in zip(old_vals, new_vals)
]

# Update `is_constant` column
ms[!, :is_constant] .= (ms[!, :lower_bound] .== ms[!, :upper_bound])

update!(dom, ms)
return nothing
return dom
end

"""
Expand Down
32 changes: 16 additions & 16 deletions test/sampling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ end
end

@testset "Set new sampling bounds" begin
set_factor_bounds = ADRIA.set_factor_bounds
set_factor_bounds! = ADRIA.set_factor_bounds!

dom = ADRIA.load_domain(TEST_DOMAIN_PATH)
num_samples = 32
Expand All @@ -223,10 +223,10 @@ end
factors = ms[factor_mask, :]
factor_fieldnames = (factors.fieldname...,)

@testset "set_factor_bounds" begin
@testset "set_factor_bounds!" begin
bounds_ranges = [range(b[1], b[2], 5) for b in factors.default_dist_params]
new_bounds = Tuple.(sort.(rand.(bounds_ranges, 2)))
dom = set_factor_bounds(dom; NamedTuple{factor_fieldnames}(new_bounds)...)
dom = set_factor_bounds!(dom; NamedTuple{factor_fieldnames}(new_bounds)...)
scens = ADRIA.sample_guided(dom, num_samples)

_test_bounds(scens, factor_mask, bounds_ranges)
Expand All @@ -235,7 +235,7 @@ end
@testset "set to default bounds" begin
new_bounds =
ADRIA.get_attr.([dom], factor_fieldnames, [:default_dist_params])
dom = set_factor_bounds(dom; NamedTuple{factor_fieldnames}(new_bounds)...)
dom = set_factor_bounds!(dom; NamedTuple{factor_fieldnames}(new_bounds)...)

factor_params = dom.model[ms.fieldname .∈ [factor_fieldnames]][1]
@test all(factor_params.dist_params .== factor_params.default_dist_params)
Expand All @@ -250,10 +250,10 @@ end
factors = ms[factor_mask, :]
factor_fieldnames = (factors.fieldname...,)

@testset "set_factor_bounds" begin
@testset "set_factor_bounds!" begin
bounds_ranges = [b[1]:b[2] for b in factors.default_dist_params]
new_bounds = Tuple.(sort.(rand.(bounds_ranges, 2)))
dom = set_factor_bounds(dom; NamedTuple{factor_fieldnames}(new_bounds)...)
dom = set_factor_bounds!(dom; NamedTuple{factor_fieldnames}(new_bounds)...)
scens = ADRIA.sample_guided(dom, num_samples)

_test_bounds(scens, factor_mask, bounds_ranges)
Expand All @@ -262,7 +262,7 @@ end
@testset "get_default_dist_params" begin
new_bounds =
ADRIA.get_attr.([dom], factor_fieldnames, [:default_dist_params])
dom = set_factor_bounds(dom; NamedTuple{factor_fieldnames}(new_bounds)...)
dom = set_factor_bounds!(dom; NamedTuple{factor_fieldnames}(new_bounds)...)

factor_params = dom.model[ms.fieldname .∈ [factor_fieldnames]][1]
@test all(factor_params.dist_params .== factor_params.default_dist_params)
Expand All @@ -279,12 +279,12 @@ end
ms.dist .== ADRIA.DiscreteOrderedUniformDist
factors = ms[factor_mask, :]
factor_fieldnames = (factors.fieldname...,)
@testset "set_factor_bounds" begin
@testset "set_factor_bounds!" begin
bounds_ranges = [b[1]:b[2] for b in factors.default_dist_params]
new_bounds = Tuple.(sort.(rand.(bounds_ranges, 2)))
new_steps = [ceil((nb[2] - nb[1]) / 10) for nb in new_bounds]
new_dist_params = [(b[1], b[2], s) for (b, s) in zip(new_bounds, new_steps)]
dom = set_factor_bounds(
dom = set_factor_bounds!(
dom; NamedTuple{factor_fieldnames}(new_dist_params)...
)
scens = ADRIA.sample_guided(dom, num_samples)
Expand All @@ -295,7 +295,7 @@ end
@testset "get_default_dist_params" begin
new_bounds =
ADRIA.get_attr.([dom], factor_fieldnames, [:default_dist_params])
dom = set_factor_bounds(dom; NamedTuple{factor_fieldnames}(new_bounds)...)
dom = set_factor_bounds!(dom; NamedTuple{factor_fieldnames}(new_bounds)...)

factor_params = dom.model[ms.fieldname .∈ [factor_fieldnames]][1]
@test all(factor_params.dist_params .== factor_params.default_dist_params)
Expand All @@ -309,13 +309,13 @@ end
factor_mask = ms.component .∈ [test_components] .&& ms.dist .== TriangularDist
factors = ms[factor_mask, :]
factor_fieldnames = (factors.fieldname...,)
@testset "set_factor_bounds" begin
@testset "set_factor_bounds!" begin
bounds_ranges = [range(b[1], b[2], 5) for b in factors.default_dist_params]
new_bounds = Tuple.(sort.(rand.(bounds_ranges, 2)))
mode_ranges = [range(nb[1], nb[2], 5) for nb in new_bounds]
new_modes = (rand.(mode_ranges))
new_dist_params = [(b[1], b[2], p) for (b, p) in zip(new_bounds, new_modes)]
dom = set_factor_bounds(
dom = set_factor_bounds!(
dom; NamedTuple{factor_fieldnames}(new_dist_params)...
)
scens = ADRIA.sample_guided(dom, num_samples)
Expand All @@ -326,7 +326,7 @@ end
@testset "get_default_dist_params" begin
new_bounds =
ADRIA.get_attr.([dom], factor_fieldnames, [:default_dist_params])
dom = set_factor_bounds(dom; NamedTuple{factor_fieldnames}(new_bounds)...)
dom = set_factor_bounds!(dom; NamedTuple{factor_fieldnames}(new_bounds)...)

factor_params = dom.model[ms.fieldname .∈ [factor_fieldnames]][1]
@test all(factor_params.dist_params .== factor_params.default_dist_params)
Expand All @@ -342,13 +342,13 @@ end
ms.dist .== ADRIA.DiscreteTriangularDist
factors = ms[factor_mask, :]
factor_fieldnames = (factors.fieldname...,)
@testset "set_factor_bounds" begin
@testset "set_factor_bounds!" begin
bounds_ranges = [b[1]:b[2] for b in factors.default_dist_params]
new_bounds = Tuple.(sort.(rand.(bounds_ranges, 2)))
new_mode_ranges = [nb[1]:nb[2] for nb in new_bounds]
new_peaks = (rand.(new_mode_ranges))
new_dist_params = [(b[1], b[2], p) for (b, p) in zip(new_bounds, new_peaks)]
dom = set_factor_bounds(
dom = set_factor_bounds!(
dom; NamedTuple{factor_fieldnames}(new_dist_params)...
)
scens = ADRIA.sample_guided(dom, num_samples)
Expand All @@ -359,7 +359,7 @@ end
@testset "get_default_dist_params" begin
new_bounds =
ADRIA.get_attr.([dom], factor_fieldnames, [:default_dist_params])
dom = set_factor_bounds(dom; NamedTuple{factor_fieldnames}(new_bounds)...)
dom = set_factor_bounds!(dom; NamedTuple{factor_fieldnames}(new_bounds)...)

factor_params = dom.model[ms.fieldname .∈ [factor_fieldnames]][1]
@test all(factor_params.dist_params .== factor_params.default_dist_params)
Expand Down

0 comments on commit 58819c4

Please sign in to comment.