Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python friends network visualization

I have hundreds of lists (each list corresponds to 1 person). Each list contains 100 strings, which are the 100 friends of that person.

I want to 3D visualize this people network based on the number of common friends they have. Considering any 2 lists, the more same strings they have, the closer they should appear together in this 3D graph. I wanted to show each list as a dot on the 3D graph without nodes/connections between the dots.

For brevity, I have included only 3 people here.

person1 = ['mike', 'alex', 'arker','locke','dave','david','ross','rachel','anna','ann','darl','carl','karle']

person2 = ['mika', 'adlex', 'parker','ocke','ave','david','rosse','rachel','anna','ann','darla','carla','karle']

person3 = ['mika', 'alex', 'parker','ocke','ave','david','rosse','ross','anna','ann','darla','carla','karle', 'sasha', 'daria']
like image 302
J.A Avatar asked Jan 03 '23 23:01

J.A


1 Answers

Gephi Setup steps:

  • Install Gephi and then start it
  • You probably want to upgrade all the plugins now, see the button in the lower right corner.
  • Now create a new project.
  • Make sure the current workspace is Workspace1
  • Enable Graph Streaming plugin
  • In Streaming tab that then appears configure server to use http and port 8080
  • start the server (it will then have a green dot underneath it instead of a red dot).

Python steps:

  • install gephistreamer package (pip install gephistreamer)

Copy the following python cod to something like friends.py:

from gephistreamer import graph
from gephistreamer import streamer
import random as rn

stream = streamer.Streamer(streamer.GephiWS(hostname="localhost",port=8080,workspace="workspace1"))

szfak = 100  # this scales up everything - somehow it is needed
cdfak = 3000

nodedict = {}
def addfnode(fname):
  # grab the node out of the dictionary if it is there, otherwise make a newone
  if (fname in nodedict):
    nnode = nodedict[fname]
  else:
    nnode = graph.Node(fname,size=szfak,x=cdfak*rn.random(),y=cdfak*rn.random(),color="#8080ff",type="f")
    nodedict[fname] = nnode # new node into the dictionary
  return nnode

def addnodes(pname,fnodenamelist):
  pnode = graph.Node(pname,size=szfak,x=cdfak*rn.random(),y=cdfak*rn.random(),color="#ff8080",type="p")
  stream.add_node(pnode)
  for fname in fnodenamelist:
    print(pname+"-"+fname)
    fnode = addfnode(fname)
    stream.add_node(fnode)
    pfedge = graph.Edge(pnode,fnode,weight=rn.random())
    stream.add_edge(pfedge)

person1friends = ['mike','alex','arker','locke','dave','david','ross','rachel','anna','ann','darl','carl','karle']
person2friends = ['mika','adlex','parker','ocke','ave','david','rosse','rachel','anna','ann','darla','carla','karle']
person3friends = ['mika','alex','parker','ocke','ave','david','rosse','ross','anna','ann','darla','carla','karle','sasha','daria']

addnodes("p1",person1friends)
addnodes("p2",person2friends)
addnodes("p3",person3friends)

Run it with the command python friends.py You should see all the nodes appear. There are then lots of ways you can lay it out to make it look better, I am using the Force Atlas layouter here and you can see the parameters I am using on the left.

enter image description here

Some notes:

  • you can get the labels to show or disappear by clicking on the T on the bottom status/control bar.
  • View the data in the nodes and edges by opening Window/Data Table.
  • It is a very rich program, there are more options than you can shake a stick at.
  • You can set more properties on your nodes and edges in the python code and then they will show up in the data table view and can be used to filter, etc.
  • You want to pay attention to that update button in the bottom right corner of Gephi, there are a lot of bugs to fix.

This will get you started (as you asked), but for your particular problem:

  • you will also need to calculate weights for your persons (the "p" nodes), and link them to each other with those weights
  • Then you need to find a layouter and paramters that positions those nodes the way you want them based on the new weights.
  • So you don't really need to show the type="f" nodes, you need just the "p" nodes.
  • The weight between to "p" nodes should be based on the intersection of the sets of the friend names.
  • There are also Gephi plugins that can then display this in 3D, but that is actually a completely separate issue, you probably want to get it working in 2D first.

This is running on Windows 10 using Anaconda 4.4.1 and Python 3.5.2 and Gephi 0.9.1.

like image 60
Mike Wise Avatar answered Jan 13 '23 08:01

Mike Wise