Ejemplo n.º 1
0
def makeGexf():
    raw_file_name = "gexf_format_test.csv"
    #raw_file_name = "HKALL_tags_RT10count.csv"
    gexf = Gexf("Ching-Ya Lin", "Keyword relation by hours.")
    graph = gexf.addGraph("undirected", "dynamic", "graph!", "datetime")
    wid = graph.addNodeAttribute("Weight", "1", "integer", "dynamic")
    ewid = graph.addEdgeAttribute("EdgeWeight", "1", "integer", "dynamic")

    if (1):
        f = codecs.open(raw_file_name, "r", "utf-8")
        #with open(raw_file_name, 'r') as f:
        reader = csv.reader(f)
        w_d = defaultdict()  #weight_dictionary for all keywords
        e_w_d = defaultdict()  #edge
        for row in reader:
            #row = [x.decode('utf8') for x in rowx]
            tt = time.strptime(row[0], "%Y-%m-%d %H")
            rtt = time.strftime("%Y-%m-%d %H:00:00", tt)
            words = row[1].split('/')
            templist = []  #one row list to help addEdge
            for w in words:
                # useless : w = w.encode('utf8')
                #print w
                #print chardet.detect(w)
                templist.append(w)
                if graph.nodeExists(w):
                    n = graph.nodes[w]
                    v = w_d[w] + 1
                    #v = int(n.attributes[int(wid)]["value"])
                    #n.attributes will get the initial value which we don't want.
                    n.addAttribute(wid, str(v), rtt)
                    w_d[w] = v
                else:
                    new = graph.addNode(w, w, rtt)
                    new.addAttribute(wid, "1", rtt)
                    w_d[w] = 1

            for w1 in templist:
                for w2 in templist:
                    if w1 != w2:
                        s1 = w1 + w2
                        s2 = w2 + w1
                        if graph.edgeExists(s1):
                            e = graph.edges[s1]
                            val = e_w_d[s1] + 1
                            e.addAttribute(ewid, str(val), rtt)
                            e_w_d[s1] = val
                        elif graph.edgeExists(s2):
                            e = graph.edges[s2]
                            val = e_w_d[s2] + 1
                            e.addAttribute(ewid, str(val), rtt)
                            e_w_d[s2] = val
                        else:
                            enew = graph.addEdge(s1, w1, w2, 1, rtt)
                            enew.addAttribute(ewid, "1", rtt)
                            e_w_d[s1] = 1
                templist.remove(w1)

    output_file = open("eventhr.gexf", "w")
    gexf.write(output_file)
Ejemplo n.º 2
0
        for bankID in bankList:
            graph.nodes[bankID].addAttribute('state',value = bankList[bankID][1], start =str(bankList[bankID][0]), end=str(EndTime))
    for bankID in diff(range(len(graph.nodes.keys())),bankList.keys()):
        graph.nodes[bankID].addAttribute('state',value = 1, start ='0', end=str(EndTime))
    return graph

# Creating toy example 
# state = 1 => solvant
# state = 2 => exposed
# state = 3 => feail
# state = 4 => dead
List = [] # It is important that the list is ordered by time
List.append((1,0,1)) #(time, bankID, state)
List.append((1,1,4))
List.append((2,0,4))
List.append((3,2,2))
List.append((4,2,3))
List.append((5,2,4))
# Creating list of edges to be cut at a specific time
EdgeList = {}
EdgeList[(2,20)]=3
EdgeList[(2,21)]=4
EdgeList[(2,6)]=5

gexf = Gexf("Financial Contagion","A dynamic network model")
G0 = pickle.load(open('/Users/linnlii/Documents/GitHub/pygexf/test/ToyGraph.p', 'rb')) 
graph = convertGraphs(G0,EdgeList)
graph = addAttributes(graph,List)
output_file=open("DynamicNetworks.gexf","w")
gexf.write(output_file)
Ejemplo n.º 3
0
import sys, pprint

sys.path.append('../gexf')
from _gexf import Gexf, GexfImport

# test helloworld.gexf
gexf = Gexf("Paul Girard", "A hello world! file")
graph = gexf.addGraph("directed", "static", "a hello world graph")

graph.addNode("0", "hello")
graph.addNode("1", "World")
graph.addEdge("0", "0", "1")

output_file = open("helloworld.gexf", "w")
gexf.write(output_file)

# test GexfImport
f = open("gexf.net.dynamics_openintervals.gexf")
gexf_import = Gexf.importXML(f)
f.close()
f = open("gexf.net.dynamics_openintervals.gexf")
gexf_import2 = Gexf.importXML(f)
f.close()
print "test gexf comparision " + str(gexf_import == gexf_import2)

graph = gexf_import.graphs[0]
# display nodes list
for node_id, node in graph.nodes.iteritems():
    print node.label
    pprint.pprint(node.getAttributes(), indent=1, width=1)