Skip to content

Commit d303952

Browse files
fix subparts access
1 parent ade91e4 commit d303952

File tree

10 files changed

+39
-28
lines changed

10 files changed

+39
-28
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ReactiveDynamics"
22
uuid = "c7456e7d-545a-4b79-91ea-6e93d96dd4d4"
3-
version = "0.2.1"
3+
version = "0.2.2"
44

55
[deps]
66
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ using ReactiveDynamics
6767

6868
# model dynamics
6969
sir_acs = @ReactionNetwork begin
70-
α*S*I, S+I --> 2I, cycle_time=>0, name=>I2R
71-
β*I, I --> R, cycle_time=>0, name=>R2S
70+
α*S*I, S+I --> 2I, name=>I2R
71+
β*I, I --> R, name=>R2S
7272
end
7373

7474
# simulation parameters

src/ReactiveDynamics.jl

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ prettynames = Dict(
8383
)
8484

8585
defargs = Dict(
86-
:T => Dict{Symbol, Any}(:transPriority => 1, :transProbOfSuccess => 1, :transCapacity => Inf, :transCycleTime => 1,
86+
:T => Dict{Symbol, Any}(:transPriority => 1, :transProbOfSuccess => 1, :transCapacity => Inf, :transCycleTime => 0.,
8787
:transMaxLifeTime => Inf, :transMultiplier=>1, :transPostAction => :(), :transName => missing),
8888
:S => Dict{Symbol, Any}(:specInitUncertainty => .0, :specInitVal => .0, :specCost => .0, :specReward => .0, :specValuation => .0),
8989
:P => Dict{Symbol, Any}(:prmVal => missing),
@@ -96,14 +96,14 @@ species_modalities = [:nonblock, :conserved, :rate]
9696

9797
function assign_defaults!(acs::ReactionNetwork)
9898
for (_, v_) in defargs, (k, v) in v_
99-
for i in eachindex(acs.subparts[k])
100-
!isassigned(acs.subparts[k], i) && (acs.subparts[k][i] = v)
99+
for i in 1:length(subpart(acs, k))
100+
!isassigned(subpart(acs, k), i) && (subpart(acs, k)[i] = v)
101101
end
102102
end
103-
104-
foreach(i -> isassigned(acs.subparts.specModality, i) || (acs.subparts.specModality[i] = Set{Symbol}()), 1:nparts(acs, :S))
103+
104+
foreach(i -> isassigned(subpart(acs, :specModality), i) || (subpart(acs, :specModality)[i] = Set{Symbol}()), 1:nparts(acs, :S))
105105
k = [:specCost, :specReward, :specValuation]
106-
foreach(k -> foreach(i -> isassigned(getproperty(acs.subparts, k), i) || (getproperty(acs.subparts, k)[i] = .0), 1:nparts(acs, :S)), k)
106+
foreach(k -> foreach(i -> isassigned(subpart(acs, k), i) || (subpart(acs, k)[i] = .0), 1:nparts(acs, :S)), k)
107107

108108
acs
109109
end
@@ -144,4 +144,9 @@ include.(readdir(joinpath(@__DIR__, "operators"), join=true))
144144
include("solvers.jl"); include("optim.jl")
145145
include("loadsave.jl")
146146

147+
# Catlab.jl hack: bypass @isdefined check
148+
function Base.getindex(m::Catlab.ColumnImplementations.PartialVecMap, x::Int)
149+
m.v[x]
150+
end
151+
147152
end

src/compilers.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function compile_attrs(acs::ReactionNetwork)
8989
wrap_fun = ex -> wrap_expr(ex, species_names, prm_names, varmap)
9090
attrs = Dict{Symbol, Vector}(); transitions = Dict{Symbol, Vector}()
9191
for attr in propertynames(acs.subparts)
92-
attrs_ = getproperty(acs.subparts, attr)
92+
attrs_ = subpart(acs, attr)
9393
!contains(string(attr), "trans") ?
9494
(attrs[attr] = map(i -> skip_compile(attr) ? attrs_[i] : wrap_fun(attrs_[i]), 1:length(attrs_))) :
9595
(transitions[attr] = map(i -> skip_compile(attr) ? attrs_[i] : wrap_fun(attrs_[i]), 1:length(attrs_)))
@@ -104,7 +104,7 @@ end
104104
function remove_choose(acs::ReactionNetwork)
105105
acs = deepcopy(acs); pcs = []
106106
for attr in propertynames(acs.subparts)
107-
attrs_ = getproperty(acs.subparts, attr)
107+
attrs_ = subpart(acs, attr)
108108
foreach(i -> isassigned(attrs_, i) && attrs_[i] isa Expr && (attrs_[i] = normalize_pcs!(pcs, attrs_[i])), 1:length(attrs_))
109109
end
110110

src/interface/update.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ end
8181
function mode!(acs, dict)
8282
for (spex, mods) in dict
8383
i = if spex isa Regex
84-
incident_pattern(spex, getproperty(acs.subparts, :specName))
84+
incident_pattern(spex, acs[:, :specName])
8585
else incident(acs, Symbol(spex), :specName) end
8686

8787
for ix in i
88-
!isassigned(acs.subparts.specModality, ix) && (acs.subparts.specModality[ix] = Set{Symbol}())
88+
!isassigned(subpart(acs, specModality), ix) && (acs[ix, specModality] = Set{Symbol}())
8989
union!(acs[ix, :specModality], mods)
9090
end
9191
end
@@ -124,7 +124,7 @@ end
124124
function set_valuation!(acs, dict, valuation_type)
125125
for (spex, val) in dict
126126
i = if spex isa Regex
127-
incident_pattern(spex, getproperty(acs.subparts, :specName))
127+
incident_pattern(spex, subpart(acs, :specName))
128128
else incident(acs, Symbol(spex), :specName) end
129129

130130
foreach(ix -> acs[ix, Symbol(:spec, Symbol(uppercasefirst(string(valuation_type))))] = eval(val), i)
@@ -207,10 +207,10 @@ end
207207
macro prob_init_from_vec(acsex, vecex) :(init!($(esc(acsex)), $(esc(vecex)))) end
208208

209209
function init!(acs, inits)
210-
inits isa AbstractVector && length(inits) == nparts(acs, :S) && (acs.subparts.specInitVal .= inits; return)
210+
inits isa AbstractVector && length(inits) == nparts(acs, :S) && (subpart(acs, :specInitVal) .= inits; return)
211211
inits isa AbstractDict && for (k, init_val) in inits
212212
k isa Number ? acs[k, :specInitVal] = init_val : begin
213-
i = k isa Regex ? incident_pattern(k, getproperty(acs.subparts, :specName)) :
213+
i = k isa Regex ? incident_pattern(k, subpart(acs, :specName)) :
214214
incident(acs, k, :specName)
215215
foreach(ix -> (acs[ix, :specInitVal] = init_val), i)
216216
end
@@ -246,10 +246,10 @@ macro prob_uncertainty(acsex, exs...)
246246
end
247247

248248
function uncinit!(acs, inits)
249-
inits isa AbstractVector && length(inits) == nparts(acs, :S) && (acs.subparts.specInitUncertainty .= inits; return)
249+
inits isa AbstractVector && length(inits) == nparts(acs, :S) && (subpart(acs, :specInitUncertainty) .= inits; return)
250250
inits isa AbstractDict && for (k, init_val) in inits
251251
k isa Number ? acs[k, :specInitUncertainty] = init_val : begin
252-
i = k isa Regex ? incident_pattern(k, getproperty(acs.subparts, :specName)) :
252+
i = k isa Regex ? incident_pattern(k, subpart(acs, :specName)) :
253253
incident(acs, k, :specName)
254254
foreach(ix -> (acs[ix, :specInitUncertainty] = init_val), i)
255255
end
@@ -262,7 +262,7 @@ function set_params!(acs, params)
262262
params isa AbstractDict && for (k, init_val) in params
263263
k = get_pattern(k)
264264
if k isa Regex
265-
i = incident_pattern(k, getproperty(acs.subparts, :prmName))
265+
i = incident_pattern(k, subpart(acs, :prmName))
266266
else
267267
i = incident(acs, k, :prmName)
268268
isempty(i) && (i = add_part!(acs, :P, prmName=k))

src/operators/equalize.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function equalize!(acs::ReactionNetwork, eqs=[])
3333

3434
for attr in propertynames(acs.subparts)
3535
attr == :specName && continue
36-
attr_ = getproperty(acs.subparts, attr)
36+
attr_ = acs[:, attr]
3737
for i in 1:length(attr_)
3838
attr_[i] = escape_ref(attr_[i], collect(keys(specmap)))
3939
attr_[i] = recursively_substitute_vars!(specmap, attr_[i])

src/operators/joins.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ function union_acs!(acs1, acs2, name=gensym("acs_"), eqs=[])
1010
for i in 1:nparts(acs2, :S)
1111
inc = incident(acs1, acs2[i, :specName], :specName)
1212
isempty(inc) && (inc = add_part!(acs1, :S; specName=acs2[i, :specName]); assign_defaults!(acs1))
13+
return (acs1, acs2)
14+
println(first(inc)); println(acs1[first(inc), :specModality]); println(); println(acs2[:, :specModality]);
1315
union!(acs1[first(inc), :specModality], acs2[i, :specModality])
1416

1517
for attr in propertynames(acs1.subparts)
@@ -51,7 +53,7 @@ function prepend!(acs::ReactionNetwork, name=gensym("acs"), eqs=[])
5153

5254
for attr in propertynames(acs.subparts)
5355
attr == :specName && continue
54-
attr_ = getproperty(acs.subparts, attr)
56+
attr_ = acs[:, attr]
5557
for i in 1:length(attr_)
5658
attr_[i] = escape_ref(attr_[i], collect(keys(specmap)))
5759
attr_[i] = recursively_substitute_vars!(specmap, attr_[i])

src/optim.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ function get_free_vars(acs, args)
140140
u0_ = []; for (k, v) in u0;
141141
if k isa Number push!(u0_, Int(k) => v)
142142
else
143-
for i in 1:length(acs.subparts.specName)
143+
for i in 1:length(subpart(acs, :specName))
144144
(acs[i, :specName] == k) && (push!(u0_, i => v); break)
145145
end
146146
end
@@ -158,8 +158,8 @@ function get_vars(acs, args)
158158
arg = recursively_expand_dots(arg)
159159
if arg isa Number push!(args_, Int(arg))
160160
else
161-
for i in 1:length(acs.subparts.specName)
162-
isassigned(acs.subparts.specName, i) && (acs[i, :specName] == arg) &&
161+
for i in 1:length(subpart(acs, :specName))
162+
isassigned(subpart(acs, :specName), i) && (acs[i, :specName] == arg) &&
163163
(push!(args_, i); break)
164164
end
165165
end

src/solvers.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,11 @@ DiscreteProblem(acs, u0, p; tspan=(.0, 100.), schedule=schedule_weighted!)
237237
"""
238238
function DiffEqBase.DiscreteProblem(acs::ReactionNetwork, u0=Dict(), p=DiffEqBase.NullParameters(); kwargs...)
239239
assign_defaults!(acs)
240-
keywords = Dict{Symbol, Any}([acs[i, :metaKeyword] => acs[i, :metaVal] for i in 1:nparts(acs, :M) if isassigned(acs.subparts[:metaKeyword], i) && isassigned(acs.subparts[:metaVal], i)])
240+
keywords = Dict{Symbol, Any}(
241+
[
242+
acs[i, :metaKeyword] => acs[i, :metaVal] for i in 1:nparts(acs, :M) if isassigned(subpart(acs, :metaKeyword), i) && isassigned(subpart(acs, :metaVal), i)
243+
]
244+
)
241245
merge!(keywords, Dict(collect(kwargs))); merge!(keywords, Dict(:strategy => get(keywords, :alloc_strategy, :weighted)))
242246
keywords[:tspan], keywords[:tstep] = get_tcontrol(keywords[:tspan], keywords)
243247

@@ -247,7 +251,7 @@ function DiffEqBase.DiscreteProblem(acs::ReactionNetwork, u0=Dict(), p=DiffEqBas
247251
prob = transform(DiffEqBase.DiscreteProblem, state; kwargs...)
248252

249253
u0 isa Dict &&
250-
foreach(i -> prob.u0[i] = isassigned(acs.subparts[:specName], i) && haskey(u0, acs[i, :specName]) ? u0[acs[i, :specName]] : prob.u0[i], 1:nparts(state, :S))
254+
foreach(i -> prob.u0[i] = isassigned(subpart(acs, :specName), i) && haskey(u0, acs[i, :specName]) ? u0[acs[i, :specName]] : prob.u0[i], 1:nparts(state, :S))
251255
p_ = p == DiffEqBase.NullParameters() ? Dict() : Dict(k => v for (k, v) in p)
252256
prob = remake(prob; u0=prob.u0, tspan=keywords[:tspan], dt=get(keywords, :tstep, 1),
253257
p=merge(prob.p, p_, Dict(:tstep => get(keywords, :tstep, 1), :strategy => get(keywords, :alloc_strategy, :weighted)))
@@ -256,7 +260,7 @@ function DiffEqBase.DiscreteProblem(acs::ReactionNetwork, u0=Dict(), p=DiffEqBas
256260
prob
257261
end
258262

259-
fetch_params(acs::ReactionNetwork) = Dict{Symbol, Any}((acs[i, :prmName] => acs[i, :prmVal] for i in Iterators.filter(i -> isassigned(acs.subparts[:prmVal], i), 1:nparts(acs, :P))))
263+
fetch_params(acs::ReactionNetwork) = Dict{Symbol, Any}((acs[i, :prmName] => acs[i, :prmVal] for i in Iterators.filter(i -> isassigned(subpart(acs, :prmVal), i), 1:nparts(acs, :P))))
260264

261265
# EnsembleProblem's prob_func: sample initial values
262266
function get_prob_func(prob)

src/state.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ mutable struct ReactiveDynamicsState
4949
ongoing_transitions = Transition[]
5050
log = NamedTuple[]
5151
observables = compile_observables(acs)
52-
transitions_attrs = setdiff(filter(a -> contains(string(a), "trans"), keys(acs.subparts)), (:trans,))
52+
transitions_attrs = setdiff(filter(a -> contains(string(a), "trans"), propertynames(acs.subparts)), (:trans,))
5353
[:transLHS, :transRHS, :transToSpawn, :transHash]
5454
transitions = Dict{Symbol, Vector}(a => [] for a in transitions_attrs)
5555

0 commit comments

Comments
 (0)