Skip to content

Commit

Permalink
feat: simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
tpoisot committed Jun 18, 2023
1 parent 6e6dc64 commit 7e9c394
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
8 changes: 7 additions & 1 deletion docs/src/macro_level/degeneracy.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@
```@docs
isdisconnected
isdegenerate
```
```

## Simplification of networks with disconnected species

```@docs
simplify
```
1 change: 1 addition & 0 deletions src/SpeciesInteractionNetworks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export spectralradius

include("macro_level/degeneracy.jl")
export isdegenerate, isdisconnected
export simplify

include("meta_level/set.jl")
include("meta_level/partitions.jl")
Expand Down
3 changes: 3 additions & 0 deletions src/interfaces/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Base.axes(N::SpeciesInteractionNetwork, i::Integer) = axes(N.edges, i)
Base.size(E::Interactions) = size(E.edges)
Base.size(E::Interactions, i::Integer) = size(E.edges, i)

Base.size(E::Bipartite) = (length(E.top), length(E.bottom))
Base.size(E::Unipartite) = (length(E.margin), length(E.margin))

Base.size(N::SpeciesInteractionNetwork) = size(N.edges)
Base.size(N::SpeciesInteractionNetwork, i::Integer) = size(N.edges, i)

Expand Down
38 changes: 38 additions & 0 deletions src/macro_level/degeneracy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,42 @@ end
@test ~isdisconnected(N, :A, true)
@test isdisconnected(N, :B, false)
@test isdisconnected(N, :B, true)
end

"""
simplify(N::SpeciesInteractionNetwork{<:Bipartite, <:Interactions})
Returns a *copy* of a network containing only the species with interactions.
Internally, this calls [`isdisconnected`](@ref).
"""
function simplify(N::SpeciesInteractionNetwork{<:Bipartite, <:Interactions})
top_nodes = filter(sp -> !isdisconnected(N, sp), species(N, 1))
bot_nodes = filter(sp -> !isdisconnected(N, sp), species(N, 2))
return subgraph(N, top_nodes, bot_nodes)
end

"""
simplify(N::SpeciesInteractionNetwork{<:Unipartite, <:Interactions}, allow_self_interactions::Bool=true)
Returns a *copy* of a network containing only the species with interactions.
Internally, this calls [`isdisconnected`](@ref) -- see this documentation for
the consequences of `allow_self_interactions`.
"""
function simplify(N::SpeciesInteractionNetwork{<:Unipartite, <:Interactions}, allow_self_interactions::Bool=true)
nodes = filter(sp -> !isdisconnected(N, sp, allow_self_interactions), species(N))
return subgraph(N, nodes)
end

@testitem "We can simplify a bipartite network" begin
nodes = Bipartite([:A, :B, :C], [:a, :b])
edges = Binary(zeros(Bool, size(nodes)))
N = SpeciesInteractionNetwork(nodes, edges)
N[:A, :a] = true
N[:A, :b] = true
N[:C, :a] = true
S = simplify(N)
@test richness(S) == 4
S[:A, :a] = false
@test N[:A, :a] == true
@test S[:A, :a] == false
end

0 comments on commit 7e9c394

Please sign in to comment.