Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Sankey Diagram #2

Open
russelljjarvis opened this issue Feb 12, 2025 · 1 comment
Open

Create Sankey Diagram #2

russelljjarvis opened this issue Feb 12, 2025 · 1 comment
Labels
enhancement New feature or request

Comments

@russelljjarvis
Copy link

russelljjarvis commented Feb 12, 2025

PlotlyJS has a good sankey, but it tries to take over your whole plotting environment/backend.
Firstly not a registered project, so this line is required

import Pkg; Pkg.add(url="https://github.com/russelljjarvis/SankeyMakie.jl")
@russelljjarvis russelljjarvis added the enhancement New feature or request label Feb 12, 2025
@russelljjarvis
Copy link
Author

russelljjarvis commented Feb 12, 2025

Sankey Network diagrams with cyclic graphs (recursive connections) is only supported by plotly-JS Sankey diagrams.

I think this is a really good way for debugging connectivity issues, as you only want to see the proportions of connectivity between populations of cells, rather than wanting to view all one-to-one connections.

Image

For now the node labels are wrong, but at least the approach seems to produce a useable output. The graph is interactive too, with nice mouse over labels appearing.

Inside the code file test/multilayer_potjans-diesmann2015.jl

    connections = Dict()
    conn_map_pre_density = Dict()
    conn_map_post_density = Dict()

    for i in eachindex(pre_layer_names)
        for j in eachindex(post_layer_names)
            pre = pre_layer_names[i]
            post = post_layer_names[j]
            p = conn_probs[j, i]
            J = conn_j[j, i]
            sym = J>=0 ? :ge : :gi
            μ = abs(J)
            if J>=0      
                s = SNN.SpikingSynapse(neurons[pre], neurons[post], sym; μ = μ, p=p, σ=0, delay_dist=delay_dist_exc)
            else
                s = SNN.SpikingSynapse(neurons[pre], neurons[post], sym; μ = -μ, p=p, σ=0, delay_dist=delay_dist_inh)
            
            end
            keyed = Symbol(string(pre,post))
            if haskey(conn_map_pre_density, keyed)
                conn_map_pre_density[keyed] += length(s.I)
                conn_map_post_density[keyed] += length(s.J)

            else
                conn_map_pre_density[keyed] = length(s.I)  # or set to some default value
                conn_map_post_density[keyed] = length(s.J)

            end
            conn_map_pre_density[Symbol(string(pre,post))] += length(s.I)
            connections[Symbol(string(i,"_",pre,"_",j,"_",post))] = s
        end
    end
function sankey_applied(pre_layer_names,post_layer_names,conn_map_pre_density)
    labels = []
    connections = []
    for i in eachindex(pre_layer_names)
        for j in eachindex(post_layer_names)
            pre = pre_layer_names[i]
            post = post_layer_names[j]
            push!(labels,string(pre))
            push!(connections,(i,j,conn_map_pre_density[Symbol(string(pre,post))]))
        end
    end
    @save "sankey_data.jld" pre_layer_names post_layer_names connections labels
end

In its own seperate file called sankey_only.jl to stop imports from messing with the standard plotting backends of SNN.jl

using PlotlyJS # messes with Plots/Makie backends so compartmentalise by keeping in its own file.
using ElectronDisplay
using JLD2
function sankey_applied()
    @load "sankey_data.jld" pre_layer_names post_layer_names connections labels
    sankey_trace = sankey(
        arrangement = "snap",
        node = attr(
            label    = labels,
            pad      = 15,
            thickness = 20,
            line     = attr(color = "black", width = 0.5)
        ),
        link = attr(
            source = [i[1] for i in connections],
            target = [i[2] for i in connections],
            value  = [i[3] for i in connections]
        )
    )
    plt = plot(sankey_trace)
    ElectronDisplay.display(plt)  # opens a new Electron window
end
sankey_applied()

@aquaresima

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant