Skip to content

Commit

Permalink
🚧🚧 Simplify exposed disconnection methods signatures.
Browse files Browse the repository at this point in the history
  • Loading branch information
iago-lito committed Jul 26, 2024
1 parent 87b3036 commit f16e4c9
Show file tree
Hide file tree
Showing 16 changed files with 588 additions and 239 deletions.
1 change: 1 addition & 0 deletions src/GraphDataInputs/GraphDataInputs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ using OrderedCollections
import ..SparseMatrix
import ..argerr
import ..checkfails
import ..join_elided

# Unhygienically define `loc` variable in macros to point to invocation line.
# Assumes __source__ is available.
Expand Down
2 changes: 1 addition & 1 deletion src/GraphDataInputs/check.jl
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ function outspace((i, j), (n, m))
end
either(symbols) =
length(symbols) == 1 ? "$(repr(first(symbols)))" :
"either " * join(repr.(sort(collect(symbols))), ", ", " or ")
"either " * join_elided(sort(collect(symbols)), ", ", " or "; max = 12)

#-------------------------------------------------------------------------------------------
# Assuming the above check passed, check references against a template.
Expand Down
74 changes: 72 additions & 2 deletions src/GraphDataInputs/convert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,31 @@ function graphdataconvert(::Type{BinMap{<:Any}}, input; expected_I = nothing)
res
end

# The binary case *can* accept boolean masks.
function graphdataconvert(
::Type{BinMap{<:Any}},
input::AbstractVector{Bool};
expected_I = Int64,
)
res = BinMap{expected_I}()
for (i, val) in enumerate(input)
val && push!(res, i)
end
res
end

function graphdataconvert(
::Type{BinMap{<:Any}},
input::AbstractSparseVector{Bool,I};
expected_I = I,
) where {I}
res = BinMap{expected_I}()
for i in findnz(input)[1]
push!(res, i)
end
res
end

#-------------------------------------------------------------------------------------------
# Similar, nested logic for adjacency maps.

Expand Down Expand Up @@ -193,6 +218,38 @@ function graphdataconvert(::Type{BinAdjacency{<:Any}}, input; expected_I = nothi
res
end

# The binary case *can* accept boolean matrices.
function graphdataconvert(
::Type{BinAdjacency{<:Any}},
input::AbstractMatrix{Bool},
expected_I = Int64,
)
res = BinAdjacency{expected_I}()
for (i, row) in enumerate(eachrow(input))
adj_line = BinMap(j for (j, val) in enumerate(row) if val)
isempty(adj_line) && continue
res[i] = adj_line
end
res
end

function graphdataconvert(
::Type{BinAdjacency{<:Any}},
input::AbstractSparseMatrix{Bool,I},
expected_I = I,
) where {I}
res = BinAdjacency{expected_I}()
nzi, nzj, _ = findnz(input)
for (i, j) in zip(nzi, nzj)
if haskey(res, i)
push!(res[i], j)
else
res[i] = BinMap([j])
end
end
res
end

# Alias if types matches exactly.
graphdataconvert(::Type{Map{<:Any,T}}, input::Map{Symbol,T}) where {T} = input
graphdataconvert(::Type{Map{<:Any,T}}, input::Map{Int64,T}) where {T} = input
Expand Down Expand Up @@ -273,9 +330,11 @@ end
# Example usage:
# @tographdata var {Sym, Scal, SpVec}{Float64}
# @tographdata var YSN{Float64}
macro tographdata(var, input)
macro tographdata(var::Symbol, input)
@defloc
var isa Symbol || argerr("Not a variable: $(repr(var)) at $loc.")
tographdata(loc, var, input)
end
function tographdata(loc, var, input)
@capture(input, types_{Target_} | types_{})
isnothing(types) && argerr("Invalid @tographdata target types at $loc.\n\
Expected @tographdata var {aliases...}{Target}. \
Expand Down Expand Up @@ -317,3 +376,14 @@ function _tographdata(vsym, var, targets)
The value received is $(repr(var)) ::$(typeof(var)).")
end
export @tographdata

# Convenience to re-bind in local scope, avoiding the akward following pattern:
# long_var_name = @tographdata long_var_name <...>
# In favour of:
# @tographdata! long_var_name <...>
macro tographdata!(var::Symbol, input)
@defloc
evar = esc(var)
:($evar = $(tographdata(loc, var, input)))
end
export @tographdata!
2 changes: 1 addition & 1 deletion src/Internals/model/model_parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mutable struct ModelParameters
# These don't exactly have an 'empty' variant.
network::Option{EcologicalNetwork}
biorates::BioRates # (this one does but all values are initially 'nothing' inside)
topology::Topology # This will actually be part of the future refactoring.
_topology::Topology # This will actually be part of the future refactoring.
functional_response::Option{FunctionalResponse}
producer_growth::Option{ProducerGrowth}
# Since 'foodweb' is still a mandatory input to construct interaction layers,
Expand Down
10 changes: 3 additions & 7 deletions src/Topologies/Topologies.jl
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,10 @@ end
export remove_node!

#-------------------------------------------------------------------------------------------
"""
disconnected_components(g::Topology)
# Iterate over disconnected components within the topology.
# Every component is yielded as a separate new topology,
# with tombstones in the right places.

Iterate over the disconnected component within the topology.
This create a collection of topologies
with all the same compartments and nodes indices,
but with different nodes marked as removed to constitute the various components.
"""
function disconnected_components(g::Topology)
# Construct a simpler graph representation
# with all nodes and edges compartments pooled together.
Expand Down
2 changes: 1 addition & 1 deletion src/Topologies/display.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function Base.show(io::IO, ::MIME"text/plain", g::Topology)
end
print(io, " $(repr(type)) => [$(join_elided(live, ", "; max =elision_limit))]")
if !isempty(tomb)
print(io, " <removed: [$(join_elided(tomb, ", "))]>")
print(io, " <removed: [$(join_elided(tomb, ", "; max=elision_limit))]>")
end
end
n_e > 0 && print(io, "\n Edges:")
Expand Down
Loading

0 comments on commit f16e4c9

Please sign in to comment.