Skip to content

Commit

Permalink
mapping networks and occurrence
Browse files Browse the repository at this point in the history
  • Loading branch information
gottacatchenall committed Jun 23, 2024
1 parent 44b66c4 commit 9922331
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 77 deletions.
22 changes: 22 additions & 0 deletions src/map.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
Base.map(fcn, net::Network{ST,<:Global}) where {ST}
Overload of the `map` method for `Global` networks.
"""
function Base.map(fcn, net::Network{ST,<:Global}) where {ST}
Global(fcn(network(net)))
end

function Base.map(fcn, net::Network{ST,SC}) where {ST,SC}
_sc = SC.name.wrapper
_sc(Union{SpeciesInteractionNetwork,Nothing}[fcn(localnet) for localnet in network(net)])
end

function Base.map(fcn, occ::Occurrence{T}) where {T}
_sc = T <: Range ? Spatial : Temporal
_sc(Union{SpeciesInteractionNetwork,Nothing}[fcn(x) for x in eachindex(occ)])
end

function Base.map(fcn, ranges::Occurrence{<:Range}, phen::Occurrence{<:Phenology}) where {T}
return Spatiotemporal(Union{SpeciesInteractionNetwork,Nothing}[fcn(x, t) for x in eachindex(ranges), t in eachindex(phen)])
end
68 changes: 29 additions & 39 deletions src/possible.jl
Original file line number Diff line number Diff line change
@@ -1,70 +1,60 @@
function possible(::Network{ST,SC}, args...) where {ST,SC}
tprint("{red}Possible must be called on a {bold}Feasible{/bold} network{/red}\n")
function possible(args...)
tprint("{red}{bold}ERROR:{/bold} The arguments to `possible` must be a `Feasible` network, and optionally `Occurrence` {red}data for each species.{/red}\n")
throw(ArgumentError)
end

function possible(net::Network{Feasible,G}, occ::Occurrence{T}) where {G<:Global,T<:Union{Range,Phenology}}
#nets = [Base.copy(network(net)) for _ in eachindex(occ)]

# TODO refactor so all empty networks a dropped and replaced in the nets datastructure
function _get_local_net(idx)
pres = present(occ, idx)
localnet = Base.copy(network(net))
localnet = SpeciesInteractionNetworks.subgraph(localnet, pres)
function _local_net(idx, net, occ)
pres = present(occ, idx)
localnet = subgraph(network(net), pres)

if length(SpeciesInteractionNetworks.interactions(localnet)) > 0
return localnet
end
return nothing
if length(interactions(localnet)) > 0
return localnet
end
nets = Union{SpeciesInteractionNetworks.SpeciesInteractionNetwork,Nothing}[_get_local_net(x) for x in eachindex(occ)]

SC = T <: Range ? Spatial : Temporal
Network{Possible}(species(net), SC(nets))
return nothing
end

function possible(net::Network{Feasible,G}, occ::Occurrence{T}) where {G<:Global,T<:Union{Range,Phenology}}
_scale = map(x -> _local_net(x, net, occ), occ)
Network{Possible}(species(net), _scale)
end

possible(
net::Network{Feasible,G},
phenologies::Occurrence{P},
ranges::Occurrence{R}
) where {G,R<:Range,P<:Phenology} = possible(net, ranges, phenologies)

function _spatiotemporal_local_net(x, t, adj, spnames, spat_pres, temp_pres)
cooc_onehot = vec(spat_pres[x] .& temp_pres[t])
if sum(cooc_onehot) > 0
cooc_idx = findall(cooc_onehot)
return SpeciesInteractionNetwork(
Unipartite(spnames[cooc_idx]),
Binary(adj[cooc_idx, cooc_idx])
)
end
return nothing
end

function possible(
net::Network{Feasible,G},
ranges::Occurrence{R},
phenologies::Occurrence{P}
) where {G<:Global,R<:Range,P<:Phenology}
spnames = species(net).names
onehot(localspecies) = Bool.(sum(permutedims(localspecies) .== spnames, dims=2))

adj = net.scale.network.edges.edges

onehot(localspecies) = Bool.(sum(permutedims(localspecies) .== spnames, dims=2))
findidx(sp) = findfirst(isequal(sp), spnames)
spnames = species(net).names

spat_pres = [onehot(present(ranges, x)) for x in eachindex(ranges)]
temp_pres = [onehot(present(phenologies, t)) for t in eachindex(phenologies)]


function local_net(x, t)
cooc_onehot = vec(spat_pres[x] .& temp_pres[t])

if sum(cooc_onehot) > 0

cooc_idx = findall(cooc_onehot)
return SpeciesInteractionNetwork(
SpeciesInteractionNetworks.Unipartite(spnames[cooc_idx]),
SpeciesInteractionNetworks.Binary(adj[cooc_idx, cooc_idx])
)
end
return nothing
end

ST = Spatiotemporal(Union{SpeciesInteractionNetworks.SpeciesInteractionNetwork,Nothing}[local_net(x, t) for x in eachindex(ranges), t in eachindex(phenologies)])
_func = (x, t) -> _spatiotemporal_local_net(x, t, adj, spnames, spat_pres, temp_pres)
st = map(_func, ranges, phenologies)

Network{Possible}(
species(net),
ST
st
)
end

Expand Down
47 changes: 25 additions & 22 deletions src/realizable.jl
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@

struct NeutrallyForbiddenLinks <: RealizationModel
relative_abundance::Abundance{RelativeAbundance}
energy
end
struct Equal <: RealizationModel end

function _rate_matrix(localnet, relabd, energy)
if !isnothing(localnet)
rates = Quantitative(zeros(Float32, size(localnet)))
rate_network = SpeciesInteractionNetwork(localnet.nodes, rates)
ra_sum = 0.0
ints = interactions(localnet)
for int in ints
spᵢ, spⱼ, θᵢⱼ = int
rᵢ, rⱼ = relabd[spᵢ], relabd[spⱼ]
θᵢⱼ = rᵢ * rⱼ
rate_network[spᵢ, spⱼ] = θᵢⱼ
ra_sum += θᵢⱼ
end
return (energy .* rate_network) ./ ra_sum
end
end


function realizable(
net::Network{ST,SC},
rm::NeutrallyForbiddenLinks
) where {ST<:Union{Feasible,Possible},SC}
relabd = rm.relative_abundance

function _rate_matrix(localnet)
if !isnothing(localnet)
rates = SpeciesInteractionNetworks.Quantitative(zeros(Float32, size(localnet)))
rate_network = SpeciesInteractionNetworks.SpeciesInteractionNetwork(localnet.nodes, rates)
ra_sum = 0.0
ints = SpeciesInteractionNetworks.interactions(localnet)
for int in ints
spᵢ, spⱼ, θᵢⱼ = int
rᵢ, rⱼ = relabd[spᵢ], relabd[spⱼ]
θᵢⱼ = rᵢ * rⱼ
rate_network[spᵢ, spⱼ] = θᵢⱼ
ra_sum += θᵢⱼ
end
return rate_network ./ ra_sum
end
end
relabd, energy = rm.relative_abundance, rm.energy

_sc = SC.name.wrapper
localnets = scale(net).network
Network{Realizable}(net.species, _sc(Union{SpeciesInteractionNetworks.SpeciesInteractionNetwork,Nothing}[_rate_matrix(localnet) for localnet in localnets]))
_scale = map(x -> _rate_matrix(x, relabd, energy), net)
Network{Realizable}(
net.species,
_scale,
)
end
33 changes: 17 additions & 16 deletions src/realize.jl
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@

function realize(net::Network{Realizable,SC}) where {SC}

function _realize(localnet)
if !isnothing(localnet)
counts = SpeciesInteractionNetworks.Quantitative(zeros(Int, size(localnet)))
realized_net = SpeciesInteractionNetworks.SpeciesInteractionNetwork(localnet.nodes, counts)
for int in SpeciesInteractionNetworks.interactions(localnet)
i, j, θ = int
if θ > 0
realized_net[i, j] = rand(Poisson(θ))
end
function _realize(localnet)
if !isnothing(localnet)
counts = Quantitative(zeros(Int, size(localnet)))
realized_net = SpeciesInteractionNetwork(localnet.nodes, counts)
for int in interactions(localnet)
i, j, θ = int
if θ > 0
realized_net[i, j] = rand(Poisson(θ))
end
return realized_net
end
return realized_net
end
end


_sc = SC.name.wrapper
localnets = scale(net).network
Network{Realized}(net.species, _sc(Union{SpeciesInteractionNetworks.SpeciesInteractionNetwork,Nothing}[_realize(localnet) for localnet in localnets]))
function realize(net::Network{Realizable,SC}) where {SC}
_scale = map(_realize, net)
Network{Realized}(
net.species,
_scale,
)
end

0 comments on commit 9922331

Please sign in to comment.