Skip to content

Commit

Permalink
add lazy compute operations
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaqz committed Dec 2, 2024
1 parent c18ca9b commit a0a17ea
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/grid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ function plot_indegrees(g::Grid; kwargs...)
heatmap(canvas, yflip=true, axis=nothing, border=:none; kwargs...)
end

function assess(g::Grid)
targetidx, targetnodes = _targetidx_and_nodes(g)
# Calculate memory use and expected flops for
# targetnodes, or something...
end

"""
is_strongly_connected(g::Grid)::Bool
Expand Down
143 changes: 143 additions & 0 deletions src/gridrsp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,60 @@ function Base.show(io::IO, ::MIME"text/html", grsp::GridRSP)
show(io, MIME"text/html"(), plot_outdegrees(grsp.g))
end

abstract type Operation end
abstract type RSPOperation <: Operation end

struct ComputationAssesment{O,Z,L,M,F}
op::O
zmax::Z
lumax::L
totalmem::T
flops::F
# Something else?
end

"""
compute(o::Operation, grsp::GridRSP)
Compute operation `o` on precomputed grid `grsp`.
"""
function compute end

"""
assess(o::Operation, g::Grid; grid_assessment)
Assess the memory and compute requirements of operation
`o` on grid `g`. This can be used to indicate memory
and time reequiremtents on a cluster
"""
function assess end

@kwdef struct Operations{O,T,A} <: Operation
ops::O
θ::T=nothing
allocs::A
end
Operations(ops::O) where O<:Tuple = Operations{O}(ops)
Operations(args...) = Operations(args)

function compute(o::Operations, grsp::Grid)
compute(o, GridRSP(g; allocs=o.allocs); θ=o.θ)
end
function compute(o::Operations, grsp::GridRSP)
map(compute, o.ops)
# Something else?
end

function assess(ops::Operations, g::Grid; grid_assessment=asses(g))
as = map(o -> asses(o, g), ops.ops)
# some code to combine
end

@kwdef struct BetweennessQ <: RSPResult end

compute(r::BetweennessQ, grsp::GridRSP) = betweenness_qweighted(grsp)
assess(r::BetweennessQ, grsp::Grid; grid_assessment=asses(g)) = nothing # TODO

"""
betweenness_qweighted(grsp::GridRSP)::Matrix{Float64}
Expand All @@ -65,7 +119,10 @@ function betweenness_qweighted(grsp::GridRSP)
return bet
end

@kwdef struct EdgeBetweenness <: RSPResult end

compute(r::EdgeBetweenness, grsp::GridRSP) = betweenness_qweighted(grsp)
assess(r::EdgeBetweenness, grsp::Grid; grid_assessment=asses(g)) = nothing # TODO

"""
edge_betweenness_qweighted(grsp::GridRSP)::Matrix{Float64}
Expand All @@ -87,6 +144,14 @@ function edge_betweenness_qweighted(grsp::GridRSP)
return betmatrix
end

@kwdef struct BetweennessK{CV,DT,DV} <: RSPResult
connectivity_function::CV=expected_cost
distance_transformation::DT=nothing
diagvalue::DV=nothing
end

compute(r::BetweennessK, grsp::GridRSP) = betweenness_kweighted(grsp; keywords(r)...)
assess(r::BetweennessK, grsp::Grid; grid_assessment=asses(g)) = nothing # TODO

"""
betweenness_kweighted(grsp::GridRSP;
Expand Down Expand Up @@ -142,7 +207,13 @@ function betweenness_kweighted(grsp::GridRSP;
return bet
end

@kwdef struct EdgeBetweennessK{CV,DT,DV} <: RSPResult
distance_transformation::DT=inv(grsp.g.costfunction)
diagvalue::DV=nothing
end

compute(r::EdgeBetweennessK, grsp::GridRSP) = edge_betweenness_kweighted(grsp; keywords(r)...)
assess(r::EdgeBetweennessK, grsp::Grid; grid_assessment=asses(g)) = nothing # TODO

"""
edge_betweenness_kweighted(grsp::GridRSP; [distance_transformation=inv(grsp.g.costfunction), diagvalue=nothing])::SparseMatrixCSC{Float64,Int}
Expand Down Expand Up @@ -177,7 +248,10 @@ function edge_betweenness_kweighted(grsp::GridRSP; distance_transformation=inv(g
return betmatrix
end

@kwdef struct ExpectedCost <: RSPResult end

compute(r::ExpectedCost, grsp::GridRSP) = expected_cost(grsp)
assess(r::ExpectedCost, grsp::Grid; grid_assessment=asses(g)) = nothing # TODO

"""
expected_cost(grsp::GridRSP)::Matrix{Float64}
Expand All @@ -189,23 +263,48 @@ function expected_cost(grsp::GridRSP)
return RSP_expected_cost(grsp.W, grsp.g.costmatrix, grsp.Z, targetnodes)
end

@kwdef struct FreeDnergyDistance <: RSPResult end

compute(r::FreeDnergyDistance, grsp::GridRSP) = free_energy_distance(grsp)
assess(r::FreeDnergyDistance, grsp::Grid; grid_assessment=asses(g)) = nothing # TODO

function free_energy_distance(grsp::GridRSP)
targetidx, targetnodes = _targetidx_and_nodes(grsp.g)
return RSP_free_energy_distance(grsp.Z, grsp.θ, targetnodes)
end

@kwdef struct SurvivalProbability <: RSPResult end

compute(r::SurvivalProbability, grsp::GridRSP) = survival_probability(grsp)
assess(r::SurvivalProbability, grsp::Grid; grid_assessment=asses(g)) = nothing # TODO

function survival_probability(grsp::GridRSP)
targetidx, targetnodes = _targetidx_and_nodes(grsp.g)
return RSP_survival_probability(grsp.Z, grsp.θ, targetnodes)
end

@kwdef struct PowerMeanProximity <: RSPResult end

compute(r::PowerMeanProximity, grsp::GridRSP) = power_mean_proximity(grsp)
assess(r::PowerMeanProximity, grsp::Grid; grid_assessment=asses(g)) = nothing # TODO

function power_mean_proximity(grsp::GridRSP)
targetidx, targetnodes = _targetidx_and_nodes(grsp.g)
return RSP_power_mean_proximity(grsp.Z, grsp.θ, targetnodes)
end

@kwdef struct LeastCostDistance <: RSPResult end

compute(r::LeastCostDistance, grsp::GridRSP) = least_cost_distance(grsp)
assess(r::LeastCostDistance, grsp::Grid; grid_assessment=asses(g)) = nothing # TODO

least_cost_distance(grsp::GridRSP) = least_cost_distance(grsp.g)

@kwdef struct MeanKullbackLeiblerDivergence <: RSPResult end

compute(r::MeanKullbackLeiblerDivergence, grsp::GridRSP) = mean_kl_divergence(grsp)
assess(r::MeanKullbackLeiblerDivergence, grsp::Grid; grid_assessment=asses(g)) = nothing # TODO

"""
mean_kl_divergence(grsp::GridRSP)::Float64
Expand All @@ -218,6 +317,11 @@ function mean_kl_divergence(grsp::GridRSP)
return qs'*(RSP_free_energy_distance(grsp.Z, grsp.θ, targetnodes) - expected_cost(grsp))*qt*grsp.θ
end

@kwdef struct MeanLeastCostKullbackLeiblerDivergence <: RSPResult end

compute(r::MeanLeastCostKullbackLeiblerDivergence, grsp::GridRSP) = mean_kl_divergence(grsp)
assess(r::MeanLeastCostKullbackLeiblerDivergence, grsp::Grid; grid_assessment=asses(g)) = nothing # TODO


"""
mean_lc_kl_divergence(grsp::GridRSP)::Float64
Expand Down Expand Up @@ -276,6 +380,11 @@ function least_cost_kl_divergence(C::SparseMatrixCSC, Pref::SparseMatrixCSC, tar
return kl_div
end

@kwdef struct LeastCostKullbackLeiblerDivergence <: RSPResult end

compute(r::LeastCostKullbackLeiblerDivergence, grsp::GridRSP) = least_cost_kl_divergence(grsp)
assess(r::LeastCostKullbackLeiblerDivergence, grsp::Grid; grid_assessment=asses(g)) = nothing # TODO

"""
least_cost_kl_divergence(grsp::GridRSP, target::Tuple{Int,Int})
Expand All @@ -294,6 +403,18 @@ function least_cost_kl_divergence(grsp::GridRSP, target::Tuple{Int,Int})
return reshape(div, grsp.g.nrows, grsp.g.ncols)
end

@kwdef struct ConnectedHabitat{CV,DT,DV} <: RSPResult
# TODO not sure which kw to use here
connectivity_function::CV=expected_cost
distance_transformation::DT=nothing
diagvalue::DV=nothing
θ::Union{Nothing,Real}=nothing
approx::Bool=false
end

compute(r::ConnectedHabitat, grsp::GridRSP) = eigmax(grsp; keywords(r)...)
assess(r::ConnectedHabitat, grsp::Grid; grid_assessment=asses(g)) = nothing # TODO

"""
connected_habitat(grsp::Union{Grid,GridRSP};
connectivity_function=expected_cost,
Expand Down Expand Up @@ -430,6 +551,16 @@ function connected_habitat(grsp::GridRSP,
return connected_habitat(newh; diagvalue=diagvalue, distance_transformation=distance_transformation)
end

@kwdef struct EigMax{F,DT,DV,T} <: RSPResult
connectivity_function::F=expected_cost
Tdistance_transformation::DT=nothing
diagvalue::DV=nothing
tol::T=1e-14
end

compute(r::EigMax, grsp::GridRSP) = eigmax(grsp; keywords(r)...)
assess(r::EigMax, grsp::Grid; grid_assessment=asses(g)) = nothing # TODO

"""
eigmax(grsp::GridRSP;
connectivity_function=expected_cost,
Expand Down Expand Up @@ -555,6 +686,17 @@ function LinearAlgebra.eigmax(grsp::GridRSP;
return vˡ, λ₀[1], vʳ
end

@kwdef struct Criticality{DT,DV,AV,QT,QS} <: RSPResult
distance_transformation::DT=inv(grsp.g.costfunction)
diagvalue::DV=nothing
avalue::AV=floatmin()
qˢvalue::QS=0.0
qᵗvalue::QT=0.0
end

compute(r::Criticality, grsp::GridRSP) = criticality(grsp; keywords(r)...)
assess(r::Criticality, grsp::Grid; grid_assessment=asses(g)) = nothing # TODO

"""
criticality(grsp::GridRSP[;
distance_transformation=inv(grsp.g.costfunction),
Expand Down Expand Up @@ -596,3 +738,4 @@ function criticality(grsp::GridRSP;

return landscape
end

0 comments on commit a0a17ea

Please sign in to comment.