Skip to content

Commit bd6999e

Browse files
Merge pull request #355 from abhro/kwargs-naming
Remove repeated naming for keyword arguments
2 parents 2caa033 + 9c3f039 commit bd6999e

11 files changed

Lines changed: 47 additions & 65 deletions

docs/src/examples/DiffEqFlux.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ cb(θ, loss_n_ode(θ)...)
8383

8484
data = Iterators.repeated((), 1000)
8585

86-
res1 = sciml_train(loss_n_ode, θ, ADAM(0.05); cb = cb, maxiters = 100)
86+
res1 = sciml_train(loss_n_ode, θ, ADAM(0.05); cb, maxiters = 100)
8787
cb(res1.minimizer, loss_n_ode(res1.minimizer)...; doplot = true)
8888

89-
res2 = sciml_train(loss_n_ode, res1.minimizer, LBFGS(); cb = cb)
89+
res2 = sciml_train(loss_n_ode, res1.minimizer, LBFGS(); cb)
9090
cb(res2.minimizer, loss_n_ode(res2.minimizer)...; doplot = true)
9191
```
9292

docs/src/examples/adaptive_control.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ plot!(step_p, solve(ODEProblem(apply_inputs(nominal_sim!; u = 1), nominal_ic, (0
107107
vars = 1, label = "nominal model")
108108

109109
u = (x, p, t) -> sin(3t)
110-
sin_p = plot(solve(ODEProblem(apply_inputs(truth_sim!; u = u), truth_ic, (0.0, 10.0)));
110+
sin_p = plot(solve(ODEProblem(apply_inputs(truth_sim!; u), truth_ic, (0.0, 10.0)));
111111
vars = 1, label = "truth model")
112-
plot!(sin_p, solve(ODEProblem(apply_inputs(nominal_sim!; u = u), nominal_ic, (0.0, 10.0)));
112+
plot!(sin_p, solve(ODEProblem(apply_inputs(nominal_sim!; u), nominal_ic, (0.0, 10.0)));
113113
vars = 1, label = "nominal model")
114114

115115
plot(step_p, sin_p; layout = (2, 1), size = (800, 800))
@@ -155,11 +155,11 @@ function feedback_sys!(D, vars, p, t; ym, r, n)
155155
regressor = [r, plant_model[1]]
156156

157157
u = control(parameter_estimates, regressor)
158-
yp = p.plant_fun(D.plant_model, plant_model, (), t; u = u)
158+
yp = p.plant_fun(D.plant_model, plant_model, (), t; u)
159159
= sensor_sim!(D.sensor, sensor, (), t; u = yp[1]) + n
160160
e =.- ym
161161
regressor[2] =
162-
adapt!(D.parameter_estimates, parameter_estimates, γ, t; e = e, w = regressor)
162+
adapt!(D.parameter_estimates, parameter_estimates, γ, t; e, w = regressor)
163163
return yp
164164
end
165165
```
@@ -172,7 +172,7 @@ function system!(D, vars, p, t; r = 0.0, n = 0.0)
172172
@unpack reference_model, feedback_loop = vars
173173

174174
ym = ref_sim!(D.reference_model, reference_model, (), t; u = r)
175-
yp = feedback_sys!(D.feedback_loop, feedback_loop, p, t; ym = ym, r = r, n = n)
175+
yp = feedback_sys!(D.feedback_loop, feedback_loop, p, t; ym, r, n)
176176
return yp
177177
end
178178
```
@@ -219,10 +219,7 @@ function simulate(plant_fun, plant_ic;
219219
)
220220

221221
# Model parameters
222-
p = (
223-
gamma = adapt_gain,
224-
plant_fun = plant_fun
225-
)
222+
p = (; gamma = adapt_gain, plant_fun)
226223

227224
sim_fun = apply_inputs(system!; r = input_signal, n = deterministic_noise)
228225

docs/src/examples/coulomb_control.md

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function feedback_sys!(D, components, p, t; ref = 0.0)
6969
@unpack ctrl, plant = components
7070

7171
u = p.ctrl.fun(D.ctrl, ctrl, p.ctrl.params, t; err = ref-plant.x, v = -plant.v)
72-
return p.plant.fun(D.plant, plant, p.plant.params, t; u = u)
72+
return p.plant.fun(D.plant, plant, p.plant.params, t; u)
7373
end
7474

7575
step_input(; time = 1.0, mag = 1.0) = (x, p, t) -> t>time ? mag : 0
@@ -91,7 +91,7 @@ const m = 50.0
9191
const μ = 0.1
9292
const k = 50.0
9393

94-
p = (m = m, μ = μ, k = k)
94+
p = (; m, μ, k)
9595
ic = ComponentArray(v = 0, x = 0)
9696

9797
ODEProblem(simulator(coulomb_block!, u = 5), ic, tspan, p) |> solve |> plot
@@ -147,7 +147,7 @@ plot(sol, vars = 3)
147147
# plant_fun = coulomb_block!
148148

149149
ref = if reference==sine_input
150-
reference(period = period, mag = magnitude)
150+
reference(; period, mag = magnitude)
151151
else
152152
reference(mag = magnitude)
153153
end
@@ -158,25 +158,19 @@ plot(sol, vars = 3)
158158
c = 4*μ*m*g/*ω*magnitude) # Viscous equivalent damping
159159
k = 50.0
160160

161-
plant_p = (m = m, μ = μ, c = c, k = k) # We'll just put everything for both models in here
162-
ctrl_p = (kp = kp, ki = ki, kd = kd)
161+
plant_p = (; m, μ, c, k) # We'll just put everything for both models in here
162+
ctrl_p = (; kp, ki, kd)
163163

164164
plant_ic = (v = 0, x = 0)
165165
ctrl_ic = (; x = 0)
166166

167167
# Set up and solve
168168
sys_p = (
169-
ctrl = (
170-
params = ctrl_p,
171-
fun = ctrl_fun
172-
),
173-
plant = (
174-
params = plant_p,
175-
fun = damping
176-
)
169+
ctrl = (params = ctrl_p, fun = ctrl_fun),
170+
plant = (params = plant_p, fun = damping)
177171
)
178172
sys_ic = ComponentArray(ctrl = ctrl_ic, plant = plant_ic)
179-
sys_fun = ODEFunction(simulator(feedback_sys!, ref = ref), syms = [:u, :v, :x])
173+
sys_fun = ODEFunction(simulator(feedback_sys!; ref), syms = [:u, :v, :x])
180174
sys_prob = ODEProblem(sys_fun, sys_ic, tspan, sys_p)
181175

182176
sol = solve(sys_prob, Tsit5())

examples/DiffEqFlux_example.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ cb(θ, loss_n_ode(θ)...)
104104

105105
data = Iterators.repeated((), 1000)
106106

107-
res1 = sciml_train(loss_n_ode, θ, ADAM(0.05); maxiters = 100, save_best = true, cb = cb)
107+
res1 = sciml_train(loss_n_ode, θ, ADAM(0.05); maxiters = 100, save_best = true, cb)
108108
cb(res1.minimizer, loss_n_ode(res1.minimizer)...; doplot = true)
109109

110-
res2 = sciml_train(loss_n_ode, res1.minimizer, LBFGS(), cb = cb)
110+
res2 = sciml_train(loss_n_ode, res1.minimizer, LBFGS(), cb)
111111
cb(res2.minimizer, loss_n_ode(res2.minimizer)...; doplot = true)
112112

113113
# gif(anim, "DiffEqFlux.gif", fps=15)

examples/adaptive_control_example.jl

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ function feedback_sys!(D, vars, p, t; ym, r, n)
9191
regressor = [r, plant_model[1]]
9292

9393
u = control(parameter_estimates, regressor)
94-
yp = p.plant_fun(D.plant_model, plant_model, (), t; u = u)
94+
yp = p.plant_fun(D.plant_model, plant_model, (), t; u)
9595
= sensor_sim!(D.sensor, sensor, (), t; u = yp[1]) + n
9696
e =.- ym
9797
regressor[2] =
98-
adapt!(D.parameter_estimates, parameter_estimates, γ, t; e = e, w = regressor)
98+
adapt!(D.parameter_estimates, parameter_estimates, γ, t; e, w = regressor)
9999
return yp
100100
end
101101
# Now the full system takes in an input signal `r`, feeds it through the reference model,
@@ -104,7 +104,7 @@ function system!(D, vars, p, t; r = 0.0, n = 0.0)
104104
@unpack reference_model, feedback_loop = vars
105105

106106
ym = ref_sim!(D.reference_model, reference_model, (), t; u = r)
107-
yp = feedback_sys!(D.feedback_loop, feedback_loop, p, t; ym = ym, r = r, n = n)
107+
yp = feedback_sys!(D.feedback_loop, feedback_loop, p, t; ym, r, n)
108108
return yp
109109
end
110110

@@ -147,10 +147,7 @@ function simulate(
147147
)
148148

149149
# Model parameters
150-
p = (
151-
gamma = adapt_gain,
152-
plant_fun = plant_fun,
153-
)
150+
p = (; gamma = adapt_gain, plant_fun)
154151

155152
sim_fun = apply_inputs(system!; r = input_signal, n = deterministic_noise)
156153

examples/coulomb_control.ipynb

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
" @unpack ctrl, plant = components\n",
9393
"\n",
9494
" u = p.ctrl.fun(D.ctrl, ctrl, p.ctrl.params, t; err=ref-plant.x, v=-plant.v)\n",
95-
" return p.plant.fun(D.plant, plant, p.plant.params, t; u=u)\n",
95+
" return p.plant.fun(D.plant, plant, p.plant.params, t; u)\n",
9696
"end\n",
9797
"\n",
9898
"step_input(;time=1.0, mag=1.0) = (x,p,t) -> t>time ? mag : 0\n",
@@ -1727,7 +1727,7 @@
17271727
" # plant_fun = coulomb_block!\n",
17281728
" \n",
17291729
" ref = if reference==sine_input\n",
1730-
" reference(period=period, mag=magnitude)\n",
1730+
" reference(; period, mag=magnitude)\n",
17311731
" else\n",
17321732
" reference(mag=magnitude)\n",
17331733
" end\n",
@@ -1738,8 +1738,8 @@
17381738
" c = 4*μ*m*g/(π*ω*magnitude) # Viscous equivalent damping\n",
17391739
" k = 50.0\n",
17401740
"\n",
1741-
" plant_p = (m=m, μ, c=c, k=k)\n",
1742-
" ctrl_p = (kp=kp, ki=ki, kd=kd)\n",
1741+
" plant_p = (; m, μ, c, k)\n",
1742+
" ctrl_p = (; kp, ki, kd)\n",
17431743
"\n",
17441744
" plant_ic = (v=0, x=0)\n",
17451745
" ctrl_ic = (;x=0)\n",
@@ -1748,17 +1748,11 @@
17481748
"\n",
17491749
" # Set up and solve\n",
17501750
" sys_p = (\n",
1751-
" ctrl = (\n",
1752-
" params = ctrl_p,\n",
1753-
" fun = ctrl_fun,\n",
1754-
" ),\n",
1755-
" plant = (\n",
1756-
" params = plant_p,\n",
1757-
" fun = damping,\n",
1758-
" ),\n",
1751+
" ctrl = (params = ctrl_p, fun = ctrl_fun),\n",
1752+
" plant = (params = plant_p, fun = damping),\n",
17591753
" )\n",
17601754
" sys_ic = ComponentArray(ctrl=ctrl_ic, plant=plant_ic)\n",
1761-
" sys_fun = ODEFunction(simulator(feedback_sys!, ref=ref), syms=[:u, :v, :x])\n",
1755+
" sys_fun = ODEFunction(simulator(feedback_sys!; ref), syms=[:u, :v, :x])\n",
17621756
" sys_prob = ODEProblem(sys_fun, sys_ic, tspan, sys_p)\n",
17631757
"\n",
17641758
" sol = solve(sys_prob, Tsit5())\n",

ext/ComponentArraysGPUArraysExt.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ function Base.mapreduce(
6868
return mapreduce(f, op, getdata(x), map(getdata, args)...; kwargs...)
6969
end
7070

71-
# These are all stolen from GPUArrays.j;
71+
# These are all stolen from GPUArrays.jl
7272
Base.any(A::GPUComponentArray{Bool}) = mapreduce(identity, |, getdata(A))
7373
Base.all(A::GPUComponentArray{Bool}) = mapreduce(identity, &, getdata(A))
7474

7575
Base.any(f::Function, A::GPUComponentArray) = mapreduce(f, |, getdata(A))
7676
Base.all(f::Function, A::GPUComponentArray) = mapreduce(f, &, getdata(A))
7777

7878
function Base.count(pred::Function, A::GPUComponentArray; dims = :, init = 0)
79-
return mapreduce(pred, Base.add_sum, getdata(A); init = init, dims = dims)
79+
return mapreduce(pred, Base.add_sum, getdata(A); init, dims)
8080
end
8181

8282
# avoid calling into `initarray!`

src/show.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,30 +41,30 @@ Base.show(io::IO, ci::ComponentIndex) = print(io, "ComponentIndex($(ci.idx), $(c
4141

4242
# Show ComponentArrays
4343
function _print_type_short(io, ca; color = :normal)
44-
return _print_type_short(io, typeof(ca); color = color)
44+
return _print_type_short(io, typeof(ca); color)
4545
end
46-
_print_type_short(io, T::Type; color = :normal) = printstyled(io, T; color = color)
46+
_print_type_short(io, T::Type; color = :normal) = printstyled(io, T; color)
4747
function _print_type_short(io, ::Type{<:ComponentArray{T, N, <:Array}}; color = :normal) where {
4848
T, N,
4949
}
50-
return printstyled(io, "ComponentArray{$T,$N}"; color = color)
50+
return printstyled(io, "ComponentArray{$T,$N}"; color)
5151
end # do not pollute the stacktrace with verbose type printing
5252
function _print_type_short(io, ::Type{<:ComponentArray{T, 1, <:Array}}; color = :normal) where {T}
53-
return printstyled(io, "ComponentVector{$T}"; color = color)
53+
return printstyled(io, "ComponentVector{$T}"; color)
5454
end
5555
function _print_type_short(io, ::Type{<:ComponentArray{T, 2, <:Array}}; color = :normal) where {T}
56-
return printstyled(io, "ComponentMatrix{$T}"; color = color)
56+
return printstyled(io, "ComponentMatrix{$T}"; color)
5757
end
5858
function _print_type_short(io, ::Type{<:ComponentArray{T, N, <:SubArray}}; color = :normal) where {
5959
T, N,
6060
}
61-
return printstyled(io, "ComponentArray{$T,$N,SubArray...}"; color = color)
61+
return printstyled(io, "ComponentArray{$T,$N,SubArray...}"; color)
6262
end # do not pollute the stacktrace with verbose type printing
6363
function _print_type_short(io, ::Type{<:ComponentArray{T, 1, <:SubArray}}; color = :normal) where {T}
64-
return printstyled(io, "ComponentVector{$T,SubArray...}"; color = color)
64+
return printstyled(io, "ComponentVector{$T,SubArray...}"; color)
6565
end
6666
function _print_type_short(io, ::Type{<:ComponentArray{T, 2, <:SubArray}}; color = :normal) where {T}
67-
return printstyled(io, "ComponentMatrix{$T,SubArray...}"; color = color)
67+
return printstyled(io, "ComponentMatrix{$T,SubArray...}"; color)
6868
end
6969

7070
function Base.show(io::IO, x::ComponentVector)

test/autodiff/autodiff_tests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ end
109109

110110
@testset "Issues" begin
111111
function mysum(x::AbstractVector)
112-
y = ComponentVector(x = x)
112+
y = ComponentVector(; x)
113113
z = ComponentVector(; z = x .^ 2)
114114
return sum(y) + sum(abs2, z)
115115
end

test/core_tests.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ r2v(r::AbstractUnitRange) = ViewAxis(r, ShapedAxis(size(r)))
1515

1616
## Test setup
1717
c = (a = (a = 1, b = [1.0, 4.4]), b = [0.4, 2, 1, 45])
18-
nt = (a = 100, b = [4, 1.3], c = c)
18+
nt = (; a = 100, b = [4, 1.3], c)
1919
nt2 = (
2020
a = 5, b = [(a = (a = 20, b = 1), b = 0), (a = (a = 33, b = 1), b = 0)],
2121
c = (a = (a = 2, b = [1, 2]), b = [1.0 2.0; 5 6]),
@@ -605,10 +605,10 @@ end
605605
@test ldiv!(getdata(tempmat), lu(cmat + I), cmat) isa AbstractMatrix
606606

607607
c = (a = 2, b = [1, 2])
608-
x = ComponentArray(
609-
a = 5, b = [
610-
(a = 20.0, b = 3.0), (a = 33.0, b = 2.0), (a = 44.0, b = 3.0),
611-
], c = c
608+
x = ComponentArray(;
609+
a = 5,
610+
b = [(a = 20.0, b = 3.0), (a = 33.0, b = 2.0), (a = 44.0, b = 3.0)],
611+
c,
612612
)
613613
@test ldiv!(rand(10), Diagonal(x), x) isa Vector
614614

@@ -924,7 +924,7 @@ end
924924
b = range(0.0, 1.0; length = 2) |> collect
925925
c = range(0.0, 1.0, length = 3) |> collect
926926
d = range(0.0, 1.0; length = 0) |> collect
927-
u = ComponentVector(a = a, b = b, c = c, d = d)
927+
u = ComponentVector(; a, b, c, d)
928928

929929
function get_state_index(
930930
idx::Int,

0 commit comments

Comments
 (0)