Creating a graph out of a string list?

Hello everyone,

This is my first post here, I hope I’m going to do it right
and it’s the right place to post it :slight_smile:
Here’s my problem, I have a list of string:

[" 'DataManager' ", " 'LauncherFrame' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'ToolBox' ", " 'ToolBox' ", " 'ToolBox' ", " 'ToolBox' ", " 'ToolBox' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'ToolBox' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' "]

This list corresponds to the names of all the windows opened during a log(session ?) on the software I am working on. The order of the names is important since it corresponds to the sequences between the windows for example the window “DataManager” was opened first then “LauncherFrame” and then again DataManager etc …

I’m trying to make a directed graph with this list where the nodes are the different types of windows and the edges are the possible transitions from one window to another (for example here there would be an arrow from DataManager to LauncherFrame etc…).
Here’s what I have done so far :

frame = countmap(WindowsType)

Dict{Any,Int64}(" ‘MonoWellInterpretationView’ " => 11," ‘DataManager’ " => 13," ‘LauncherFrame’ " => 1," ‘ToolBox’ " => 6)

FrameLabel=
for i in keys(frame)
FrameLabel = [FrameLabel;i]
end

A = [0 0 2 3
0 0 0 0
1 3 0 0
0 0 3 0]

  G = DiGraph(A)
  # save to png

draw(PNG(“graph.png”, 100, 100),gplot(G, nodelabel=FrameLabel))

graph

As you can see I managed to create a graph with arrows and with a node for each type of window, the problem is the matrix A of the edges. How could I “automate” it without having to create it “by hand” for each string list that I will have? How could I create this matrix A (for edges) directly from my list of string with inside the transitions between the windows and their frequency for each type of transitions?

I hope I have been clear and not asked a stupid question !

Thank you in advance for your answers,

Have a good day, Valentine
PS : Sorry if my english is bad :sweat_smile:

I only understand parts of your question as I am not too familiar with directed graphs.
Also, I is not clear to me how the values in the matrix A are defined.

But one thing you can do is the snippet below.
I though that countmap(transitions) would be similar (or equal) to A, but it is not…

list=[" 'DataManager' ", " 'LauncherFrame' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'ToolBox' ", " 'ToolBox' ", " 'ToolBox' ", " 'ToolBox' ", " 'ToolBox' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'ToolBox' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' "]

function get_transitions(list)
    transitions = Vector{Vector{String}}()
    for i=2:length(list)
        push!(transitions,list[i-1:i])
    end
    return transitions
end

transitions = get_transitions(list)

countmap(transitions)
unique(list) 

julia> countmap(transitions)
Dict{Array{String,1},Int64} with 9 entries:
  [" 'ToolBox' ", " 'ToolBox' "]                                       => 4
  [" 'LauncherFrame' ", " 'DataManager' "]                             => 1
  [" 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' "] => 10
  [" 'ToolBox' ", " 'MonoWellInterpretationView' "]                    => 1
  [" 'MonoWellInterpretationView' ", " 'ToolBox' "]                    => 1
  [" 'DataManager' ", " 'LauncherFrame' "]                             => 1
  [" 'DataManager' ", " 'ToolBox' "]                                   => 1
  [" 'DataManager' ", " 'DataManager' "]                               => 10
  [" 'ToolBox' ", " 'DataManager' "]                                   => 1

julia> unique(list)
4-element Array{String,1}:
 " 'DataManager' "
 " 'LauncherFrame' "
 " 'ToolBox' "
 " 'MonoWellInterpretationView' "

Thank you very much for your answer and sorry if it is not clear !
Each value in the matrix A corresponds to the number of times a type of transition occurs. These are the values you found with the “countmap(transitions)”.

ah ok. I was irritated as your Matrix A has values “0 0 2 3” on the first line. But my result does not show these values.

Yes, it’s my fault I didn’t specify that they were random values just to make a graph appear.

Here’s one way to get the adjacency matrix:

str = [" 'DataManager' ", " 'LauncherFrame' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'ToolBox' ", " 'ToolBox' ", " 'ToolBox' ", " 'ToolBox' ", " 'ToolBox' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'ToolBox' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' "]

d = Dict(unique(str) .=> 1:length(unique(str)))
A = zeros(Int, length(d), length(d))
for i in 1:length(str)-1
    A[d[str[i]], d[str[i+1]]] += 1
end
1 Like

Thank you very much , this is exactly what i needed !