def graph():
    fh = open ('data/jamia/data_out2.json','r')
    data = json.load(fh)
    nodelist = set()
    for d in data:
        nodelist.add(d['source'])
        nodelist.add(d['target'])


    gexf = Gexf("Yiye Zhang","Test graph")
    graph=gexf.addGraph("directed", "static","testing graph")

    for i in nodelist:
        graph.addNode(i,i)

    k=0
    for d in data:
        graph.addEdge(k,d['source'],d['target'], weight=d['weight'])
        k=k+1

    output_file=open("app/static/g.gexf","w")
    gexf.write(output_file)


    return render_template('graph.html')
Beispiel #2
0
def write_gexf(G, fpath):
    """Convert an igraph graph to a gexf file."""
    gexf = Gexf("QNATool", config.project_name)
    gexf_graph = gexf.addGraph(
            'directed', 'dynamic', config.project_name, timeformat='date')

    # add custom attributes to edges and nodes
    gexf_graph.addEdgeAttribute('article_path', 0,
            type='string',
            force_id='article_path')
    gexf_graph.addEdgeAttribute('sentiment', 0, force_id='sentiment')
    gexf_graph.addEdgeAttribute('sentences', 0,
            type='string',
            force_id='sentences')

    gexf_graph.addNodeAttribute('community', 0, force_id='community')
    gexf_graph.addNodeAttribute('kind', 0, type='string', force_id='kind')
    gexf_graph.addNodeAttribute('frequency', 0, force_id='frequency')

    for vertex in G.vs:
        add_gexf_node(vertex.index, G, gexf_graph)

    for source_id, target_id in G.get_edgelist():
        add_gexf_edge(source_id, target_id, G, gexf_graph)

    f = open(fpath, 'w')
    gexf.write(f)
Beispiel #3
0
def build_gexf(edges, out_name, p_sample = 1):
  if not Gexf_loaded:
    print 'Could not load Gexf from module gexf.'
    return
  gexf = Gexf("snikolov", out_name)
  graph = gexf.addGraph('directed', 'dynamic', out_name)
  end = str(max([edge[2] for edge in edges]))
  for (src, dst, time) in edges:
    if np.random.rand() > p_sample:
      continue
    # Assumes time is in epoch seconds
    #d = datetime.datetime.fromtimestamp(int(time))    
    #date = d.isoformat()
    start = str(time)
    if src != -1:
      if not graph.nodeExists(src) or start < graph._nodes[src].start:
        graph.addNode(id = src, label = '', start = start, end = end)
      if not graph.nodeExists(dst) or start < graph._nodes[dst].start:
        graph.addNode(id = dst, label = '', start = start, end = end)
      graph.addEdge(id = str(src) + ',' + str(dst), source = src,
                    target = dst, start = start, end = end)
    else:
      if not graph.nodeExists(dst):
        graph.addNode(id = dst, label = '', start = start, end = end)
      
  out = open('/Users/snikolov/projects/twesearch/data/graphs/' + out_name + '.gexf', 'w')
  gexf.write(out)
  out.close()
Beispiel #4
0
def main():
  gexf = Gexf("Politikon","Autoreferences graph")
  graph=gexf.addGraph("directed","static","a hello world graph")

  tree = ET.parse(INPUT_WP_XML)
  root = tree.getroot()

  orphan = []

  for item in root[0].findall('item'):
    if re.match("http://politikon.es/\d\d\d\d/\d\d/\d\d/([\w*-]*)/", item.find('link').text):    
      post_id = re.match("http://politikon.es/\d\d\d\d/\d\d/\d\d/([\w*-]*)/", item.find('link').text).group(1)
      post_link = item.find('link').text
      r,g,b = get_color(item.find('{http://purl.org/dc/elements/1.1/}creator').text)
      graph.addNode(id=str(post_id),label=str(post_link), r=str(r), g=str(g), b=str(b))
      links = get_post_links(item.find('{http://purl.org/rss/1.0/modules/content/}encoded').text)
      if links:
        for link in links:
          link_id = re.match("http://politikon.es/\d\d\d\d/\d\d/[\d\d/]*([\w*-]*)/", link).group(1)
          if link_id in orphan:
            orphan.remove(link_id)
          if graph.nodeExists(link_id):
            graph.addEdge(post_id+"->"+link_id, post_id, link_id)
      else:
        orphan.append(post_id)
        
  for o in orphan:
    del graph._nodes[o]
      
  output_file=open(GEXF_OUTPUT_FILE,"w")
  gexf.write(output_file)
    def get(self,query):
	gexf = Gexf("Test","abc")
	graph=gexf.addGraph("directed","static","a test graph")
        
        gdb = GraphDatabase("http://localhost:7474/db/data/")
        
        srcs=[]
        i=0
        l = (Q("name", contains=query))
        for node in gdb.nodes.filter(l):
                src=graph.addNode(node.properties['name'],node.properties['name'],r='255',g='0',b='0')
                srcs.append(node.properties['name'])
                related_nodes = node.traverse()
                for related_node in related_nodes:
                        dst=graph.addNode(related_node.properties['name'],related_node.properties['name'],r='0',g='255',b='255')
                        graph.addEdge(i,related_node.properties['name'],node.properties['name'],weight=.01)
                        i+=1

        for item in [comb for comb in combinations(srcs, 2)]:
                graph.addEdge(i,item[1],item[0],weight=0.1)
                i+=1

                        
	out=etree.tostring(gexf.getXML(),pretty_print=True,encoding='utf-8')
	self.write(out)
def main():

    if len(sys.argv) != 4:
        sys.exit("Format: parse_output.py infopath-output.txt timesteps.txt timestamp")

    infopath_output_file = sys.argv[1]
    timesteps_file = sys.argv[2]
    ts_to_gen = int(sys.argv[3])
    out_file = "%s-at-%d.gexf" % (infopath_output_file.split('.')[0], ts_to_gen)

    fin = open(infopath_output_file, 'r')
    fts = open(timesteps_file, 'r')
    fout = open(out_file, 'w')


    gexf = Gexf('tribhu_infopath','31-03-2014')
    graph = gexf.addGraph('directed','dynamic','31-03-2014')

    # Add all users as nodes
    for line in fin:
        if ',' not in line: break
        uid, uname = line.split(',')
        uid = int(uid)
        # graph.addNode(str(uid), uname[-1])

    timesteps = map(lambda x: float(x.strip()), fts.readlines())
    first_ts = timesteps[0]
    last_ts = timesteps[-1]

    #
    # Create a dict: timstamp -> [ (source, target, weight), ()...]
    #
    ts_to_edges_dct = defaultdict(list)

    for line in fin:
        vals = map(float, line.split(','))
        source, target = map(int, vals[0:2])
        # Obtain a list of pairs <timestamp, weight>
        edge_weights = [(int(vals[i]), float(vals[i+1])) for i in range(2, len(vals[2:])-1, 2)]

        for ts, weight in edge_weights:
            ts_to_edges_dct[ts] += [(source, target, weight), ]

    # Dump the required timestamps into an output file
    added_nodes = set()
    for source, target, weight in ts_to_edges_dct[ts_to_gen]:
        if source not in added_nodes:
            added_nodes.add(source)
            graph.addNode(str(source), str(source))
        if target not in added_nodes:
            added_nodes.add(target)
            graph.addNode(str(target), str(target))

        edge_id = "%d_to_%d_at_%f" % (source, target, ts)
        graph.addEdge(edge_id, str(source), str(target), weight=weight, label=edge_id)

    gexf.write(fout)
Beispiel #7
0
def generate_graph():
    gexf = Gexf("sna - meetup.com", "A meetup.com social network analysis")
    graph = gexf.addGraph("undirected", "static", "Meetup groups graph")

    # attributes
    organizer_attr = graph.addNodeAttribute('organizer', 'None', 'string')
    type_attr = graph.addNodeAttribute('type', 'None', 'string')
    category_attr = graph.addNodeAttribute('category', 'None', 'integer')

    groups = get_data('data/groups.json')
    members = get_data('data/members.json')

    # print groups[0]
    # print members[0]

    

    groups = list(filter(lambda x: int(x['members']) <= 200, groups))

    print "Total number of groups: %s" % len(groups)

    for index, group in enumerate(groups):
        node = graph.addNode(
            "G-%s" % group.get('id'),
            group.get('urlname')
        )
        if group.get('organizer'):
            node.addAttribute(organizer_attr, str(group.get('organizer').get('member_id')))
        if group.get('category'):
            node.addAttribute(category_attr, str(group.get('category').get('id')))
        node.addAttribute(type_attr, 'Group')

    

    members = list(filter(lambda x: (x['group_id']) in list(map(lambda x: x['id'] ,groups)),members))

    print "Total number of members: %s" % len(members)


    for index, member in enumerate(members):
        # print index + 1

        
        node = graph.addNode(
            "M-%s" % member.get('member_id'),
            get_member_name(member.get('name'))
        )
        node.addAttribute(type_attr, 'Member')

        graph.addEdge(
            index,
            "G-%s" % member.get('group_id'),
            "M-%s" % member.get('member_id'),
        )

    return gexf
def perm_to_app_graph_all(apps, p_maps, categories):
    print 'perm_to_app_graph start ', 'all'
    gexf_a = Gexf('Jianhua Shao', 'all')
    graph_a = gexf_a.addGraph('directed', 'static', 'all')
    attr_node_a = graph_a.addNodeAttribute('node_type', 'app', 'string')
    attr_edge_a = graph_a.addEdgeAttribute('edge_type', 'no', 'string')
    for p_id in p_maps:
        p_label = p_maps[p_id]
        n_a = graph_a.addNode(p_id, '%s_%s'%(str(p_id), p_label))
        n_a.addAttribute(attr_node_a, 'p')
    app_ids_a = {}
    i_t = len(apps)
    i_i = 0
    p = 0
    for app_id in apps:
        p, i_i = p_percent(p, i_i, i_t, 10)
        app = apps[app_id]
        if not app.has_key('developer'):
            continue
        developer_href = app['developer']
        if developer_href == '' or developer_href == None:
            continue
        if not app.has_key('perms'):
            continue
        installs = app['installs']
        installs, install_min, install_max, install_average = installs_c(installs)
        if int(install_max) <= 5:
            continue
        rating_average = app['rating_average']
        if float(rating_average) == 0:
            continue
        category = app['category'].lower().strip()
        category_id = categories[category]
        award_editor = 'no'
        award_developer = 'no'
        if app.has_key('awards'):
            for award in app['awards']:
                award = award.strip().lower()
                if award == 'top developer':
                    award_developer = 'yes'
                if award == "editors' choice":
                    award_editor = 'yes'
        if not app_ids_a.has_key(app_id):
            app_ids_a[app_id] = 1
            n_a = graph_a.addNode(app_id, app_id)
            n_a.addAttribute(attr_node_a, 'a')
        ps = app['perms']
        for p_id in ps:
            e_a = graph_a.addEdge('%s_%s'%(str(p_id), app_id), p_id, app_id)
            e_a.addAttribute(attr_edge_a, award_editor)
    output_file_a = open('./txt/graph/all.gexf', 'w')
    gexf_a.write(output_file_a)
    print 'perm_to_app_graph end all '
Beispiel #9
0
 def get_gexf(self):
     gexf = Gexf("Pallas", self.name)
     graph = gexf.addGraph("directed", "static", "Current site exploration")
     graph.addNode('start', 'start')
     for page in [page for page in self._pages if page != 'start']:
       graph.addNode(page, self._pages[page]._url)
     for id in [id for id in self._connections if self._connections[id]['from'] == "start"]:
       graph.addEdge(id, "start", self._connections[id]['to'])
     for page in self._pages:
         for id in [id for id in self._connections if self._connections[id]['from'] == page and self._connections[id]['explored']]:
           graph.addEdge(id, self._connections[id]['from'], self._connections[id]['to'])
     self._gexf_xml = gexf.getXML()
     return self._gexf_xml
Beispiel #10
0
def generateXml(G, layers):
    gexf = Gexf("lzu.edu", "network based on IPv6")
    graph = gexf.addGraph("undirected", "static", "network based on IPv6")

    # atr1 = graph.addNodeAttribute('ip address',type='string',defaultValue='true')
    atr1 = graph.addNodeAttribute(force_id='modularity_class',
                                  title='Modularity Class',
                                  type='integer')

    f = faker.Faker(locale='zh-CN')
    print('ipv6:{}'.format(f.ipv6()))

    nodes = list(G.nodes)
    edges = list(G.edges)
    print('nodes:{}'.format(nodes))
    print('edges:{}'.format(edges))

    activate_nodes = []
    for i in range(len(layers)):
        for j in range(len(layers[i])):
            activate_nodes.append(layers[i][j])

    for i in range(len(nodes)):
        tmp = graph.addNode(nodes[i], f.ipv6())
        attribute_flag = 0
        for j in range(len(layers)):
            if nodes[i] in layers[j]:
                attribute_flag = j + 1
                break
        tmp.addAttribute(atr1, str(attribute_flag))
    for i in range(len(edges)):
        graph.addEdge(str(i), str(edges[i][0]), str(edges[i][1]))
    # tmp = graph.addNode("0","Gephi")
    # tmp.addAttribute(atr1,"http://gephi.org")
    # tmp = graph.addNode("1","Webatlas")
    # tmp.addAttribute(atr1,"http://webatlas.fr")
    # tmp = graph.addNode("2","RTGI")
    # tmp.addAttribute(atr1,"http://rtgi.fr")
    # tmp = graph.addNode("3","BarabasiLab")
    # tmp.addAttribute(atr1,"http://barabasilab.com")

    # graph.addEdge("0","0","1",weight='1')
    # graph.addEdge("1","0","2",weight='1')
    # graph.addEdge("2","1","0",weight='1')
    # graph.addEdge("3","2","1",weight='1')
    # graph.addEdge("4","0","3",weight='1')

    xml_file = os.getcwd(
    ) + os.sep + 'app' + os.sep + 'static' + os.sep + 'data' + os.sep + 'xml' + os.sep + 'data.xml'
    output_file = open(xml_file, "wb")
    gexf.write(output_file)
Beispiel #11
0
def generate_graph():
    gexf = Gexf("sna - meetup.com", "A meetup.com social network analysis")
    graph = gexf.addGraph("undirected", "static", "Meetup groups graph")

    # attributes
    organizer_attr = graph.addNodeAttribute('organizer', 'None', 'string')
    type_attr = graph.addNodeAttribute('type', 'None', 'string')
    category_attr = graph.addNodeAttribute('category', 'None', 'integer')

    groups = get_data('data/groups.json')
    members = get_data('data/members.json')

    # print groups[0]
    # print members[0]

    groups = list(filter(lambda x: int(x['members']) <= 200, groups))

    print "Total number of groups: %s" % len(groups)

    for index, group in enumerate(groups):
        node = graph.addNode("G-%s" % group.get('id'), group.get('urlname'))
        if group.get('organizer'):
            node.addAttribute(organizer_attr,
                              str(group.get('organizer').get('member_id')))
        if group.get('category'):
            node.addAttribute(category_attr,
                              str(group.get('category').get('id')))
        node.addAttribute(type_attr, 'Group')

    members = list(
        filter(
            lambda x: (x['group_id']) in list(map(lambda x: x['id'], groups)),
            members))

    print "Total number of members: %s" % len(members)

    for index, member in enumerate(members):
        # print index + 1

        node = graph.addNode("M-%s" % member.get('member_id'),
                             get_member_name(member.get('name')))
        node.addAttribute(type_attr, 'Member')

        graph.addEdge(
            index,
            "G-%s" % member.get('group_id'),
            "M-%s" % member.get('member_id'),
        )

    return gexf
Beispiel #12
0
def tree2graph(tree_nodes):
    dt, max_depth, max_width = buchheim.buchheim(tree_nodes[0])

    gexf = Gexf('MOON_CLJ', 'simple')
    graph = gexf.addGraph('directed', 'static', 'weibo graph')
    graph.addNodeAttribute('img_url', type='URI', force_id='img_url')
    graph.addNodeAttribute('name', type='string', force_id='name')
    graph.addNodeAttribute('location', type='string', force_id='location')
    graph.addNodeAttribute('datetime', type='string', force_id='datetime')
    graph.addNodeAttribute('repost_num', type='integer', force_id='repost_num')
    graph.addNodeAttribute('weibo_url', type='URI', force_id='weibo_url')

    add_node_and_edge(dt, graph, Count(), max_width=max_width)

    return etree.tostring(gexf.getXML(), pretty_print=False, encoding='utf-8', xml_declaration=True), max_depth, max_width
def tree2graph(tree_nodes):
    tree_xml = ''
    dt, max_depth, max_width = buchheim_weibospread.buchheim(tree_nodes[0])

    gexf = Gexf('tree', 'simple')
    graph = gexf.addGraph('directed', 'static', 'weibo graph')
    graph.addNodeAttribute('photo_url', type='URI', force_id='photo_url')
    graph.addNodeAttribute('name', type='string', force_id='name')
    graph.addNodeAttribute('location', type='string', force_id='location')
    graph.addNodeAttribute('datetime', type='string', force_id='datetime')
    graph.addNodeAttribute('repost_num', type='string', force_id='repost_num')
    graph.addNodeAttribute('weibo_url', type='URI', force_id='weibo_url')
    
    add_node_and_edge(dt, graph, Count(), max_width=max_width)

    return etree.tostring(gexf.getXML(), pretty_print=False, encoding='utf-8', xml_declaration=True), max_depth, max_width
Beispiel #14
0
def getgexf(g):
    gexf = Gexf('hxq', 'test graph')
    graph = gexf.addGraph('undirected graph')

    node_counter = 0
    for node in g.nodes():
        graph.addNode(node_counter, node)
        node_couter += 1

    edge_counter = 0
    for edge in g.edges():
        start, end = edge
        graph.addEdge(edge_counter, start, end)

    output_file = open('graph.gexf', 'w')
    gexf.write(output_file)
Beispiel #15
0
def getgexf(g):
    gexf = Gexf('hxq', 'test graph')
    graph = gexf.addGraph('undirected graph')

    node_counter = 0
    for node in g.nodes():
        graph.addNode(node_counter, node)
        node_couter += 1
        
    edge_counter = 0
    for edge in g.edges():
        start, end = edge
        graph.addEdge(edge_counter, start, end)

    output_file = open('graph.gexf', 'w')
    gexf.write(output_file)
Beispiel #16
0
    def convert(self, nodes, ties):

        gexf = Gexf(config.CREATOR, config.DESCRIPTION)
        graph = gexf.addGraph(config.DEFAULTEDGETYPE, config.MODE, config.LABEL)

        # Percorre todos os nós
        for node in nodes.keys():
            for item in nodes[node]:
                graph.addNode(nodes[node][item]["id"], item)

        for i in xrange(len(ties)):
            graph.addEdge(i, ties[i][0], ties[i][1])

        output = open(config.OUTPUT_FILE, "w")
        gexf.write(output)

        output.close()
Beispiel #17
0
  def convert(self,nodes,ties):

    gexf = Gexf(config.CREATOR, config.DESCRIPTION)
    graph = gexf.addGraph(config.DEFAULTEDGETYPE, config.MODE, config.LABEL)

    # Percorre todos os nós
    for node in nodes.keys():
      for item in nodes[node]:
        graph.addNode(nodes[node][item]['id'], item)

    for i in xrange(len(ties)):
      graph.addEdge(i, ties[i][0], ties[i][1])

    output = open(config.OUTPUT_FILE,'w')
    gexf.write(output)

    output.close()
Beispiel #18
0
def build_gexf(path, out_name, p_sample = 1):
  if not Gexf_loaded:
    print 'Could not import Gexf from gexf module.'
    return
  graphs = {}
  gexfs = {}
  call(['mkdir', 'data/graphs/' + out_name])
  files = os.listdir(path)
  for (fi, file) in enumerate(files):
    print file
    if not re.match('part-.-[0-9]{5}',file):
      print 'Filename', file, 'not valid data. Skipping...'
      continue
    if os.path.isdir(file):
      print 'File', file, 'is directory. Skipping...'
      continue
    f = open(os.path.join(path,file), 'r')
    line = f.readline()
    while line:
      if np.random.rand() > p_sample:
        line = f.readline()
        continue
      if np.random.rand() < 0.0001:
        print sorted([ (len(graphs[t]._nodes), len(graphs[t]._edges), t) for t in graphs ])
      fields = line_to_fields(line)
      if len(fields) != 12:
        # print 'Bad line', line, '. Skipping...'
        line = f.readline()
        continue
      dst_status_id = fields[0]
      src_status_id = fields[4]
      time = fields[5]
      topic = fields[8]

      # Clean non ASCII chars from topic name, since XML complains.
      topic = ''.join(c for c in topic if ord(c) < 128)

      if topic not in gexfs:
        gexfs[topic] = Gexf("Stanislav Nikolov", topic)
        graphs[topic] = gexfs[topic].addGraph("directed", "dynamic", topic)
      if not graphs[topic].nodeExists(src_status_id):
        graphs[topic].addNode(id = str(src_status_id), label = "",
                              start = time, end = "Infinity")
      if not graphs[topic].nodeExists(dst_status_id):
        graphs[topic].addNode(id = str(dst_status_id), label = "",
                              start = time, end = "Infinity")
      graphs[topic].addEdge(id = "(" + str(src_status_id) + "," + str(dst_status_id) + ")",
                            source = src_status_id,
                            target = dst_status_id,
                            start = time,
                            end = "Infinity")
      line = f.readline()

    for topic in gexfs.keys():
      clean_topic = ''.join([ c for c in topic if (c != '#' and c != ' ') ])
      out = open("data/graphs/" + out_name + "/" + clean_topic + ".gexf", "w")
      gexfs[topic].write(out)
      out.close()
Beispiel #19
0
def derived_x_to_y():
    txt = open("./dot/derived_x_to_y.gv", "w")
    txt.write("//%s\n" % (str(datetime.now())))
    txt.write("digraph graphname {\n")
    gexf = Gexf("Jianhua Shao", "Thingiver derivation mapping")
    graph = gexf.addGraph("directed", "static", "derivation_x_y")
    # attr_node = graph.addNodeAttribute('url', '', 'string')
    attr_edge = graph.addEdgeAttribute("c_days", "", "string")
    # sql = sql_derived_x_to_y
    sql = sql_derived_x_to_y_created_time
    param = ()
    c = conn.cursor()
    c.execute(sql, param)
    for r in c.fetchall():
        print r
        x = r[1]
        y = r[2]
        x_ctime = r[3]
        y_ctime = r[4]
        x = x.strip("\n\t/thing:")
        # x = int(x)
        y = y.strip("\n\t/thing:")
        # y = int(y)
        # print type(x), type(y)
        # print x, y
        x_ctime = datetime.strptime(x_ctime, "%b %d, %Y")
        y_ctime = datetime.strptime(y_ctime, "%b %d, %Y")
        duration = (y_ctime - x_ctime).days
        # print duration
        dot_line = "\t{%s} -> {%s} [label=%s];\n" % (x, y, str(duration))
        print dot_line
        txt.write(dot_line)
        n_x = graph.addNode(str(x), str(x))
        n_y = graph.addNode(str(y), str(y))
        # n_x.addAttribute(attr_node, 'string')
        # n_y.addAttribute(attr_node, 'string')
        e = graph.addEdge("%s_%s" % (str(x), str(y)), x, y)
        e.addAttribute(attr_edge, str(duration))
        # print e
    c.close()
    txt.write("}")
    txt.close()
    gexf_file = open("./dot/derived_x_to_y.gexf", "w")
    gexf.write(gexf_file)
    print "finish"
Beispiel #20
0
def derived_x_to_y():
    txt = open('./dot/derived_x_to_y.gv', 'w')
    txt.write('//%s\n' % (str(datetime.now())))
    txt.write('digraph graphname {\n')
    gexf = Gexf('Jianhua Shao', 'Thingiver derivation mapping')
    graph = gexf.addGraph('directed', 'static', 'derivation_x_y')
    #attr_node = graph.addNodeAttribute('url', '', 'string')
    attr_edge = graph.addEdgeAttribute('c_days', '', 'string')
    #sql = sql_derived_x_to_y
    sql = sql_derived_x_to_y_created_time
    param = ()
    c = conn.cursor()
    c.execute(sql, param)
    for r in c.fetchall():
        print r
        x = r[1]
        y = r[2]
        x_ctime = r[3]
        y_ctime = r[4]
        x = x.strip('\n\t/thing:')
        #x = int(x)
        y = y.strip('\n\t/thing:')
        #y = int(y)
        #print type(x), type(y)
        #print x, y
        x_ctime = datetime.strptime(x_ctime, '%b %d, %Y')
        y_ctime = datetime.strptime(y_ctime, '%b %d, %Y')
        duration = (y_ctime - x_ctime).days
        #print duration
        dot_line = '\t{%s} -> {%s} [label=%s];\n' % (x, y, str(duration))
        print dot_line
        txt.write(dot_line)
        n_x = graph.addNode(str(x), str(x))
        n_y = graph.addNode(str(y), str(y))
        #n_x.addAttribute(attr_node, 'string')
        #n_y.addAttribute(attr_node, 'string')
        e = graph.addEdge('%s_%s' % (str(x), str(y)), x, y)
        e.addAttribute(attr_edge, str(duration))
        #print e
    c.close()
    txt.write('}')
    txt.close()
    gexf_file = open('./dot/derived_x_to_y.gexf', 'w')
    gexf.write(gexf_file)
    print "finish"
Beispiel #21
0
def sigma_test_gexf(request):
    r = Raffle.objects.get(id='58')
    tl = Ticket.objects.filter(raffle=r.id).order_by('-is_root_ticket')
    gexf = Gexf('Sample','Testting Sigma JS')
    graph = gexf.addGraph('directed','static','a test graph')
    graph.addNodeAttribute('xcoord','0',"float")
    graph.addNodeAttribute('ycoord','0',"float")
    graph.addNodeAttribute('label','',"string")
    coords = r.graph()
    for i,t in enumerate(tl):
        n = graph.addNode(t.id,t.hash)
        n.addAttribute(0,str(coords[i][0]))
        n.addAttribute(1,str(coords[i][1]))
        n.addAttribute(2,t.hash)
        if t.parent_ticket_id>0:
            graph.addEdge(t.id,t.id,t.parent_ticket_id)
    response = etree.tostring(gexf.getXML(),pretty_print=True,encoding='utf-8',xml_declaration=True)
    print response
    return HttpResponse(response, content_type="application/json")
def developer_to_api_o():
    global conn
    gexf = Gexf("Jianhua Shao", "programaleweb developer to api")
    graph = gexf.addGraph("directed", "static", "ecosystem")
    attr_node = graph.addNodeAttribute("n_type", "mashup", "string")
    gexf_node_developer(
        developer_to_api_distinct_developer, 
        (), 
        graph, 
        attr_node)
    gexf_node_api(
        developer_to_api_distinct_api, 
        (), 
        graph, 
        attr_node)
    gexf_edge_developer_to_api(
        developer_to_api, 
        (), 
        graph)
    output_file = open(gexf_path+"pgw_developer_to_api.gexf", 'w')
    gexf.write(output_file)
Beispiel #23
0
def gen_gexf(ofile='./../gexf/test2.gexf'):
    import sys, pprint
    from gexf import Gexf

    # test helloworld.gexf
    gexf = Gexf("Gephi.org", "A Web network")
    graph = gexf.addGraph("directed", "static", "A Web network")

    atr2 = graph.addNodeAttribute('modularity_class', 'Modularity Class',
                                  'integer')

    tmp = graph.addNode("0", "A")
    tmp.addAttribute(atr2, "0")

    tmp = graph.addNode("1", "B")
    tmp.addAttribute(atr2, "0")

    graph.addEdge("0", "0", "1", weight='1')

    output_file = open(ofile, "w")
    gexf.write(output_file)
Beispiel #24
0
def forest_main(keyword,topic_id):

    first_start_ts, dataset, flag = load_data(keyword,topic_id)

    if flag == 0:
        return 0

    height = 0
    counter = 0
    ts = []
    colors = []
    node_y_table = {}
    x_count = defaultdict(int)
    y_count = defaultdict(int)
    y_name = {}
    gexf = Gexf("Yang Han <*****@*****.**>", "Retweets evolution graph based on sail layout.")
    graph = gexf.addGraph("directed", "static", "retweets evolution graph")

    n = 0
    for reposts in dataset:
        if not len(reposts):
            continue
        
        root, start_ts, end_ts, count, counter = build_tree(reposts, counter)
        _color = random_color(colors)

        build_graph(graph, root, start_ts, end_ts, node_y_table, x_count, y_count, y_name, x=ts2x(start_ts-first_start_ts), y=height, color=_color)
        height += 1

        counter = build_x_chart(graph, x_count, counter, -SEG, first_start_ts)
        counter = build_y_chart(graph, y_count, y_name, counter, -SEG)

        n = n + 1
        print n

    graph = etree.tostring(gexf.getXML(), pretty_print=False, encoding='utf-8', xml_declaration=True)

    save_weibo_tree(str(topic_id), graph)

    return 1
Beispiel #25
0
def sigma_gexf(request, ticket_hash):
    x = Ticket.objects.get(hash=ticket_hash)
    r = Raffle.objects.get(id=x.raffle.id)
    tl = Ticket.objects.filter(raffle=r.id).order_by('-is_root_ticket')
    gexf = Gexf('Tree For '+ticket_hash,'Sigma JS Graph')
    graph = gexf.addGraph('directed','static',ticket_hash)
    graph.addNodeAttribute('xcoord','0',"float")
    graph.addNodeAttribute('ycoord','0',"float")
    graph.addNodeAttribute('label','',"string")
    graph.addNodeAttribute('completion_size','1',"float")
    coords = r.graph()
    for i,t in enumerate(tl):
        n = graph.addNode(t.id,t.hash)
        n.addAttribute(0,str(coords[i][0]))
        n.addAttribute(1,str(coords[i][1]*4))
        n.addAttribute(2,t.hash)
        cs = t.completion_count()
        n.addAttribute(3,str(cs))
    for t in tl:
        if t.parent_ticket_id>0:
            graph.addEdge(t.id,t.id,t.parent_ticket_id)
    response = etree.tostring(gexf.getXML(),pretty_print=True,encoding='utf-8',xml_declaration=True)
    return HttpResponse(response, content_type="application/json")
Beispiel #26
0
    def load_file(self, fn):
        """Load full corpus data from a GEXF file"""
        logger = logging.getLogger("web.load")

        #Loading gexf file
        with open(fn, 'r') as gf:
            gexf = Gexf.importXML(gf)

        # Parsing Graph
        graph = gexf.graphs[0]

        # breakpoint --> unicode
        self.sites = set(str(node) for node_id,node in graph.nodes.iteritems())
        return len(self.sites)
def perm_to_app_graph_each(apps_t, p_maps, categories):
    for category in apps_t:
        print 'perm_to_app_graph start ', category
        category_id = categories[category]
        apps = apps_t[category]
        gexf = Gexf('Jianhua Shao', category)
        graph = gexf.addGraph('directed', 'static', category)
        attr_node = graph.addNodeAttribute('node_type', 'app', 'string')
        attr_edge = graph.addEdgeAttribute('edge_type', 'no', 'string')
        #attr_node_app_award = graph.addNodeAttribute('app_award', 'no', 'string')
        for p_id in p_maps:
            p_label = p_maps[p_id]
            n = graph.addNode(p_id, '%s_%s'%(str(p_id), p_label))
            n.addAttribute(attr_node, 'p')
        app_ids = {}
        for app_id in apps:
            app = apps[app_id]
            award_editor = 'no'
            award_developer = 'no'
            if app.has_key('awards'):
                for award in app['awards']:
                    award = award.strip().lower()
                    if award == 'top developer':
                        award_developer = 'yes'
                    if award == "editors' choice":
                        award_editor = 'yes'
            if not app_ids.has_key(app_id):
                app_ids[app_id] = 1
                n = graph.addNode(app_id, app_id)
                n.addAttribute(attr_node, 'a')
            ps = app['perms']
            for p_id in ps:
                e = graph.addEdge('%s_%s'%(str(p_id), app_id), p_id, app_id)
                e.addAttribute(attr_edge, award_editor)
        output_file = open('./txt/graph/%s_%s.gexf'%(str(category_id), category), 'w')
        gexf.write(output_file)
        print 'perm_to_app_graph end ', category
 def __init__(self, fle_name, path=""):
     self.f = open(fle_name)
     self.gexf_import = Gexf.importXML(self.f)
     self.f.close()
     self.graph = self.gexf_import.graphs[0]
    if 'e+' in time:
        calctime = time.split('e+')
        time = str(float(calctime[0]) * pow(10, int(calctime[1])))
    elif 'e-' in time:
        calctime = time.split('e-')
        time = str(float(calctime[0]) * pow(10, int(calctime[1]) * (-1)))
    return time


lsArgs = sys.argv

trace_file = open(lsArgs[1], 'r')
trace_lines = trace_file.readlines()
trace_file.close()

gexf = Gexf("Pedro and David", "Dynamic graph")
graph = gexf.addGraph("directed", "dynamic", "Graph")
idTotalNodeDuration = graph.addNodeAttribute("Total Duration", "0.0",
                                             "float")  #name, default, type
idTotalEdgeDuration = graph.addEdgeAttribute("Total Duration", "0.0", "float")
idLinkType = graph.addEdgeAttribute("Type", "None", "string")
idNumMsgs = graph.addEdgeAttribute("Number of messages", "0", "integer")

dicEvents = {}

i = 0
while (trace_lines[i].startswith('%') == True):

    if ('%EventDef' in trace_lines[i]):

        lsLine = trace_lines[i].split(' ')
from gexf import Gexf
try:
    gexf = Gexf ("Jianhua Shao", "hello world description")
    graph = gexf.addGraph ("directed", "static", "hello world hints")
    attribute_node = graph.addNodeAttribute("name", "default_value", "type like string")
    attribute_edge = graph.addEdgeAttribute("name", "default_value", "type like boolean")
    n = graph.addNode("0", "node_name")
    n1=graph.addNode("1", "node_name1")
    n1=graph.addNode("2", "node_name2")
    n1=graph.addNode("3", "node_name3")
    #n.addAttribute(attribute_node, "mashup")
    e = graph.addEdge("name", "0", "1")
    
    e.addAttribute(attribute_edge, "true")
    e1 = graph.addEdge("name", "0", "2")
    e2 = graph.addEdge("name", "1", "2")
    e3 = graph.addEdge("name", "2", "3")
    output_file = open("D://programableweb.gexf", "w")
    gexf.write(output_file)
    #outputfile.close()
except:
    print "error"
    pass
"""import networkx as nx
import enchant
import matplotlib.pyplot as plt
g=nx.Graph()
g.add_node(1)
g.add_node(2)
g.add_edge(1,2)
#nx.draw(g)
Beispiel #31
0
from gexf import Gexf
from py2neo import cypher, neo4j

__author__ = 'Trevor Grant'


# test helloworld.gexf
gexf = Gexf(__author__,__title__)
graph=gexf.addGraph("directed","static",__desc__)
graph_db = neo4j.GraphDatabaseService()


query = neo4j.CypherQuery(graph_db, "MATCH (n) WHERE n.on_path=1 RETURN n" ) 
results = query.execute()
# Query for relationships where both nodes have .on_path
for r in results:
	quite =graph.addNode(str(r[0]._id),r[0]['KEYWORD_TEXT'])
#	if r[0]._id == 2418: print 'yup'


RESULTS_PER_PAGE= 1000
# Query for nodes with .on_path

paging = False
skip=0
query = neo4j.CypherQuery(graph_db, "MATCH (m {on_path:1})-[r]->(n {on_path:1}) RETURN r ORDER BY ID(r) SKIP %i LIMIT %i" % (skip,RESULTS_PER_PAGE ))
results = query.execute()
if len(results)==1000: 
	paging=True
	skip += RESULTS_PER_PAGE
# Query for relationships where both nodes have .on_path
Beispiel #32
0
    nodeSet = set()
    edgeSet = {}
    newNode = set(['color'])


    for i in range(3):
        edges = findBSiblings(newNode)
        currentSiblingSet = set()

        for edge in edges:
            currentSiblingSet.add(edge[0])
            currentSiblingSet.add(edge[1])
            if edgeSet.has_key(edge[0]+"_"+edge[1]) == False and edgeSet.has_key(edge[1]+"_"+edge[0]) == False:
                edgeSet[edge[0]+"_"+edge[1]] = edge

        newNode = currentSiblingSet - nodeSet
        nodeSet = nodeSet | newNode

    gexf = Gexf("Husonchen", "A hello world! file")
    graph = gexf.addGraph("undirected", "static", "categories")

    for node in nodeSet:
        graph.addNode(node, node)

    i = 0
    for edge in edgeSet.values():
        graph.addEdge(str(i),edge[0] , edge[1])
        i += 1

    output_file = open("helloworld.gexf", "w")
    gexf.write(output_file)
Beispiel #33
0
DB_NAME		= "picbrother"

try:
 conn = MySQLdb.connect (host = DB_HOST,
                         user = DB_USER,
                         passwd = DB_PSWD,
                         db = DB_NAME,
                         use_unicode=True)
except MySQLdb.Error, e:
 print "Error %d: %s" % (e.args[0], e.args[1])
 sys.exit (1)

cursor = conn.cursor ()

if __name__ == '__main__':
	gexf = Gexf("PIC BROTHER","Le graph du pic !")
	graph = gexf.addGraph("undirected","static","Picbrother graph")
	cursor.execute ("SELECT id, fb_id, first_name, last_name FROM  `T_USER` ")
	rows = cursor.fetchall ()
	for row in rows:
		print "%s, %s, %s, %s" % (row[0], row[1], row[2], row[3])
		#graph.addNode(row[0],u"#%s => %s %s" % (row[1],row[2],row[3])) #  ==>  Probléme d'accent... Comment faire ?
		graph.addNode(str(row[0]),unac.unac_string("%s %s" % (row[2],row[3]), "utf-8"))
	print "Nombre de user ajoute : %d" % cursor.rowcount
	cursor.execute ("SELECT U1.user_id, U2.user_id, COUNT( * ) weight FROM  `J_PHOTO_USER` U1,  `J_PHOTO_USER` U2 WHERE U1.photo_id = U2.photo_id AND U1.user_id != U2.user_id GROUP BY U1.user_id, U2.user_id")
	rows = cursor.fetchall ()
	edge_id = 0
	for row in rows:
		print "%s, %s, %s, %s" % (edge_id,row[0], row[1], row[2])
		graph.addEdge(str(edge_id),str(row[0]),str(row[1]),str(row[2]))
		edge_id+=1
# # 根据输入文件构建国家交互网络
# def graphBuild(nodeList, edgeList):
#     # for indexs in nodeList.index:
#     # graphCon.add_node(nodeList.node[indexs].strip())
#     # 上面注释了的代码是添加节点,不过也可以不需要,因为下面添加边的时候,边两端的节点会自动添加到网络中
#     for indexs in edgeList.index:
#         countryGraph.add_edge(edgeList.edgeNode1[indexs].strip(), edgeList.edgeNode2[indexs].strip(),
#                               weight=edgeList.numCount[indexs])  # 添加边与权重

# graphBuild(nodeList, edgeList)  # 运行网络构建函数

# # 输出网络
# nx.write_gexf(countryGraph, gexfPath + 'graph_' + dataTime + 'c1.gexf')  # 输出为Gephi格式
# print("ss")
# print(gexf)
gexf = Gexf("Gephi.org", "A Web network")
graph = gexf.addGraph("undirected", "static", "A Web network")
atr1 = graph.addNodeAttribute('Modularity_class',
                              force_id='modularity_class',
                              type='integer')
atr2 = graph.addNodeAttribute('country', force_id='country', type='string')
atr3 = graph.addNodeAttribute('nodenumber',
                              force_id='nodenumber',
                              type='integer')
# atr3 = graph.addNodeAttribute('frog',type='boolean',defaultValue='true')
eatr1 = graph.addEdgeAttribute('country1',
                               force_id='country1',
                               type='string',
                               defaultValue='country1')
eatr2 = graph.addEdgeAttribute('country2',
                               force_id='country2',
Beispiel #35
0
import sys
import pprint
from gexf import Gexf

# test helloworld.gexf
gexf = Gexf("Home With Kids", "A Demo Graph")
graph = gexf.addGraph("directed", "static", "A Demo Graph")

atr1 = graph.addNodeAttribute('ID', type='string')
atr2 = graph.addNodeAttribute('adult', type='boolean', defaultValue='true')

tmp = graph.addNode("0", "刘梅")
tmp.addAttribute(atr1, "123456")
tmp.addAttribute(atr2, 'true')

tmp = graph.addNode("1", "夏东海")
tmp.addAttribute(atr1, "534523")
tmp.addAttribute(atr2, 'true')

tmp = graph.addNode("2", "夏雪")
tmp.addAttribute(atr1, "2312311")
tmp.addAttribute(atr2, 'false')

tmp = graph.addNode("3", "刘星")
tmp.addAttribute(atr1, "1231231")
tmp.addAttribute(atr2, 'false')

tmp = graph.addNode("4", "夏雨")
tmp.addAttribute(atr1, "2314889")
tmp.addAttribute(atr2, 'false')
Beispiel #36
0
# -*- coding: UTF-8 -*-
__author__ = 'zy'
__time__ = '2019/10/31 13:45'
import sys, pprint
from gexf import Gexf

# test helloworld.gexf
gexf = Gexf("Gephi.org", "A Web network")
graph = gexf.addGraph("directed", "static", "A Web network")

atr1 = graph.addNodeAttribute('url', type='string')
atr2 = graph.addNodeAttribute('indegree', type='float')
atr3 = graph.addNodeAttribute('frog', type='boolean', defaultValue='true')

tmp = graph.addNode("0", "Gephi")
tmp.addAttribute(atr1, "http://gephi.org")
tmp.addAttribute(atr2, '1')

tmp = graph.addNode("1", "Webatlas")
tmp.addAttribute(atr1, "http://webatlas.fr")
tmp.addAttribute(atr2, '2')

tmp = graph.addNode("2", "RTGI")
tmp.addAttribute(atr1, "http://rtgi.fr")
tmp.addAttribute(atr2, '1')

tmp = graph.addNode("3", "BarabasiLab")
tmp.addAttribute(atr1, "http://barabasilab.com")
tmp.addAttribute(atr2, '1')
tmp.addAttribute(atr3, 'false')
Beispiel #37
0
#!/usr/bin/python

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("hellowrld.gexf", "w")
gexf.write(output_file)

# test GexfImport
f = open("exp.gexf")
o = open("exp_bootstrap_bootstrap.gexf", "w+")

gexf_import = GexfImport(f).gexf()
gexf_import.write(o)
Beispiel #38
0
def make_ds_gexf(gexf_name_1, gexf_name_2, G, node_degree, pr_key_users, all_uid_pr, ds_pr_data, partition, ds_new_attribute_dict):
    gexf = Gexf(gexf_name_1, gexf_name_2)

    node_id = {}
    graph = gexf.addGraph('directed', 'static', 'demp graph')
    graph.addNodeAttribute('name', type='string', force_id='name')
    graph.addNodeAttribute('location', type='string', force_id='location')
    graph.addNodeAttribute('timestamp', type='string', force_id='timestamp')
    graph.addNodeAttribute('pagerank', type='float', force_id='pagerank')
    #graph.addNodeAttribute('trendsetter_rank', type='float', force_id='trendsetter_rank')
    graph.addNodeAttribute('acategory', type='string', force_id='acategory')
    graph.addNodeAttribute('text', type='string', force_id='text')
    graph.addNodeAttribute('reposts_count', type='string', force_id='reposts_count')
    graph.addNodeAttribute('comments_count', type='string', force_id='comments_count')
    graph.addNodeAttribute('attitude_count', type='string', force_id='attitude_count')
    graph.addNodeAttribute('rank_pr', type='string', force_id='rank_pr')
    #graph.addNodeAttribute('rank_tr', type='string', force_id='rank_tr')

    pos = nx.spring_layout(G)

    node_counter = 0
    edge_counter = 0

    for node in G.nodes():
        x, y = pos[node]
        degree = node_degree[node]
        if node not in node_id: # 判断该节点是否已经加入到图中
            node_id[node] = node_counter
            node_counter += 1
        uid = node # 节点就是用户名
        if uid in pr_key_users:
            _node = graph.addNode(node_id[node], str(node), x=str(x), y=str(y), z='0', r='255', g='51', b='51', size=str(degree))
        else:
            _node = graph.addNode(node_id[node], str(node), x=str(x), y=str(y), z='0', r='0', g='204', b='204', size=str(degree))
        cluster_id = str(partition[node])
        _node.addAttribute('acategory', cluster_id)
        pr = str(all_uid_pr[str(uid)])
        _node.addAttribute('pagerank', pr)
        #print 'all_uid_tr:', all_uid_tr
        #print 'all_uid_pr:', all_uid_pr
        #tr = str(all_uid_tr[str(uid)])
        #_node.addAttribute('trendsetter_rank', tr)
        rank_pr = ds_pr_data[uid]
        _node.addAttribute('rank_pr', str(rank_pr))
        #rank_tr = ds_tr_data[uid]
        #_node.addAttribute('rank_tr', str(rank_tr))
        try:
            text_add = ds_new_attribute_dict[uid][0][0]
            _node.addAttribute('text', json.dumps(text_add))
            reposts_count_add = i2u(ds_new_attribute_dict[uid][0][1])
            _node.addAttribute('reposts_count', reposts_count_add)
            comment_count_add = i2u(ds_new_attribute_dict[uid][0][2])
            _node.addAttribute('comments_count', comment_count_add)
            attitude_count_add = i2u(ds_new_attribute_dict[uid][0][3])
            if attitude_count_add == None:
                attitude_count_add = u'未知'
            _node.addAttribute('attitude_count', i2u(attitude_count_add))
            timestamp_add = i2u(ds_new_attribute_dict[uid][0][4])
            _node.addAttribute('timestamp', timestamp_add)
        except KeyError:
            _node.addAttribute('text', u'未知')
            _node.addAttribute('reposts_count', u'未知')
            _node.addAttribute('comments_count', u'未知')
            _node.addAttribute('attitude_count', u'未知')
            _node.addAttribute('timestamp', u'未知')
        user_info = acquire_user_by_id(uid) # 获取对应的用户信息,添加属性
        if user_info:
            _node.addAttribute('name', user_info['name'])
            _node.addAttribute('location', user_info['location'])
        else:
            _node.addAttribute('name', u'未知')
            _node.addAttribute('location', u'未知')


    for edge in G.edges():
        start, end = edge # (repost_uid, source_uid)
        start_id = node_id[start]
        end_id = node_id[end]
        graph.addEdge(str(edge_counter), str(start_id), str(end_id))
        edge_counter += 1

    return gexf
        id = str(db_node._id)
        node = Node(graph, id, db_node['name'], datetime.utcfromtimestamp(db_node['timeNominated']).isoformat())

        if 'didDonate' in db_node.properties and db_node['didDonate']:
            node.addAttribute('challenge_status', 'donated', datetime.utcfromtimestamp(db_node['donationDate']).isoformat())
        elif 'timeCompleted' in db_node.properties:
            node.addAttribute('challenge_status', 'completed', datetime.utcfromtimestamp(db_node['timeCompleted']).isoformat())

        graph.nodes[id] = node

def get_nominations():
    return db.match(rel_type='NOMINATED')

def add_nominations_to_graph(graph):
    for db_nomination in get_nominations():
        id = str(db_nomination._id)
        source_id = str(db_nomination.start_node._id)
        target_id = str(db_nomination.end_node._id)

        graph.addEdge(id, source_id, target_id, start=datetime.utcfromtimestamp(db_nomination['timeNominated']).isoformat(), label='NOMINATED')

graph = init_graph()

gexf = Gexf('Brad Ross', 'A Network of Ice Bucket Challenges')
gexf.graphs.append(graph)

add_participants_to_graph(graph)
add_nominations_to_graph(graph)

output_file = open('nomination_graph.gexf', 'w')
gexf.write(output_file, print_stat=True)
Beispiel #40
0
import sys, pprint
from gexf import Gexf

# test helloworld.gexf
gexf = Gexf("course system", "neo4j graph")
graph = gexf.addGraph("directed", "static", "A Web network")

atr1 = graph.addNodeAttribute('url',
                              type='string',
                              defaultValue='123',
                              force_id='testid')
atr2 = graph.addNodeAttribute('indegree',
                              type='float',
                              defaultValue='true',
                              force_id='modularity_class')
atr3 = graph.addNodeAttribute('frog', type='boolean', defaultValue='true')

tmp = graph.addNode("0", "Gephi")
tmp.addAttribute(atr1, "http://gephi.org")
tmp.addAttribute(atr2, '1')

tmp = graph.addNode("1", "Webatlas")
tmp.addAttribute(atr1, "http://webatlas.fr")
tmp.addAttribute(atr2, '2')

tmp = graph.addNode("2", "RTGI")
tmp.addAttribute(atr1, "http://rtgi.fr")
tmp.addAttribute(atr2, '3')

tmp = graph.addNode("3", "BarabasiLab")
tmp.addAttribute(atr1, "http://barabasilab.com")
Beispiel #41
0
import sys

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

df = pd.read_csv('article_similarity_filtered.csv', delimiter=',', index_col=0)
dates = pd.read_csv('master_articles-scraped_filtered_aritcles.csv',
                    delimiter=',')

rows = list(df.index)
rows.remove('Article URL')
cols = list(df.columns.values)
num = len(rows)
cols.remove('Article URL.1')

gexf = Gexf("emCOMP", "Article Similarity")
graph = gexf.addGraph("undirected", "static", "Article Similarity")

#add nodes
for r in rows:
    graph.addNode(r, dates.loc[r, "updated_domain"])

#add edges
count = 0
count_r = 0
for r in rows:
    print("r" + str(count_r))
    for c in cols:
        #can be adapted for directed graph
        if (df.loc[r, c] > .85):
            r_date = datetime.datetime.strptime(str(dates.loc[r, "first_ts"]),
from gexf import Gexf, GexfImport


#gephx is xml, which does not support accents, so we need to remove them
def remove_accents(word):
    return ''.join(x for x in unicodedata.normalize('NFKD', word)
                   if x in string.ascii_letters)


df = pd.read_csv('authors_counts.csv', delimiter=',', index_col=0)

rows = list(df.index)
cols = list(df.columns.values)
num = len(rows)

gexf = Gexf("emCOMP", "Authors")
G = gexf.addGraph("directed", "static", "Authors")

unreadable_names = {}
#add nodes
for r in rows:
    #gephx needs unicode, make sure the string is unicode
    if isinstance(unicode(r, "utf-8"), str):
        print "ordinary string"
    elif isinstance(unicode(r, "utf-8"), unicode):
        print "unicode string"
    #Checking myself
    print(r)
    print(remove_accents(unicode(r, "utf-8")))
    new_string = remove_accents(unicode(r, "utf-8"))
    count = 0
Beispiel #43
0
def make_gexf(gexf_name_1, gexf_name_2, G, node_degree, key_users, all_uid_pr, pr_data, partition, new_attribute_dict):
    gexf = Gexf(gexf_name_1, gexf_name_2)

    node_id = {}
    graph = gexf.addGraph("directed", "static", "demp graph")
    graph.addNodeAttribute('name', type='string', force_id='name')
    graph.addNodeAttribute('location', type='string', force_id='location') # 添加地理位置属性
    graph.addNodeAttribute('timestamp', type='int', force_id='timestamp')
    graph.addNodeAttribute('pagerank', type='string', force_id='pagerank')
    graph.addNodeAttribute('acategory', type='string', force_id='acategory')
    graph.addNodeAttribute('text', type='string', force_id='text')
    graph.addNodeAttribute('reposts_count', type='string', force_id='reposts_count') # 新添加的属性
    graph.addNodeAttribute('comments_count', type='string', force_id='comments_count')
    graph.addNodeAttribute('attitude_count', type='string', force_id='attitude_count')
    graph.addNodeAttribute('rank_pr', type='string', force_id='rank_pr') # 用户的pagerank值对应的排名
    pos = nx.spring_layout(G) # 定义一个布局 pos={node:[v...]/(v...)}

    node_counter = 0
    edge_counter = 0

    for node in G.nodes():
        x, y = pos[node] # 返回位置(x,y)
        degree = node_degree[node]
        if node not in node_id: # {node:排名}
            node_id[node] = node_counter
            node_counter += 1
        uid = node # 节点就是用户名
        if uid in key_users: # 根据是否为关键用户添加不同的节点 
            _node = graph.addNode(node_id[node], str(node), x=str(x), y=str(y), z='0', r='255', g='51', b='51', size=str(degree))
        else:
            _node = graph.addNode(node_id[node], str(node), x=str(x), y=str(y), z='0', r='0', g='204', b='204', size=str(degree))
        cluster_id = str(partition[node])
        _node.addAttribute('acategory', cluster_id)
        #print 'al_uid_pr:', all_uid_pr
        pr = str(all_uid_pr[str(uid)])
        _node.addAttribute('pagerank', pr)
        rank = pr_data[uid]
        _node.addAttribute('rank_pr', str(rank))
        #print 'pagarank_uid:', uid
        try:
            text_add = new_attribute_dict[uid][0][0] # 添加节点属性--text
            _node.addAttribute('text', json.dumps(text_add))
            reposts_count_add = i2u(new_attribute_dict[uid][0][1])
            _node.addAttribute('reposts_count', reposts_count_add) # 添加节点属性--reposts_count
            comment_count_add = i2u(new_attribute_dict[uid][0][2])
            _node.addAttribute('comments_count', comment_count_add) # 添加节点属性--comment_count
            attitude_count_add = i2u(new_attribute_dict[uid][0][3])
            if attitude_count_add == None:
                attitude_count_add = u'未知'
            _node.addAttribute('attitude_count', i2u(attitude_count_add)) # 添加节点属性--attitude_count
        except KeyError:
            _node.addAttribute('text', u'未知')
            _node.addAttribute('reposts_count', u'未知')
            _node.addAttribute('comments_count', u'未知')
            _node.addAttribute('attitude_count', u'未知')
        user_info = acquire_user_by_id(uid) # 获取对应的用户信息,添加属性
        if user_info:
            _node.addAttribute('name', user_info['name'])
            _node.addAttribute('location', user_info['location'])
        else:
            _node.addAttribute('name', u'未知')
            _node.addAttribute('location', u'未知')
            #_node.addAttribute('timestamp', str(uid_ts[uid]))

    for edge in G.edges():
        start, end = edge # (repost_uid, source_uid)
        start_id = node_id[start]
        end_id = node_id[end]
        graph.addEdge(str(edge_counter), str(start_id), str(end_id))
        edge_counter += 1

    return gexf
Beispiel #44
0
from gexf import Gexf
from lxml import etree
from model.school_xuke import search_school, search_xueke, search_school_xuke, search_school_id

import faker
f = faker.Faker(locale='zh-CN')

school = search_school()
xuke = search_xueke()
school_xuke = search_school_xuke()
school_id = search_school_id()

gexf = Gexf("school", "xueke")
graph = gexf.addGraph("school", "jiaoyu", "xueke")

atr1 = graph.addNodeAttribute(force_id='modularity_class',
                              title='schoolce',
                              defaultValue='true',
                              type='integer')
atr2 = graph.addNodeAttribute(force_id='xueke',
                              title='xu_ke',
                              defaultValue='true',
                              type='string')

for node in school:
    node_type = 'school'
    tmp_node = graph.addNode(str(node[0]), str(node[1]))
    tmp_node.addAttribute(atr1, str(node[0] - 1))

for node in xuke:
    node_type = 'xuke'
Beispiel #45
0
def convert(team_name, channels_list, graph='mention_based_graph_info', user='******', pwd='FluoBySusTech',
            port=3306, host='10.20.13.209', dbname='rowdata'):
    from gexf import Gexf
    from textblob import TextBlob
    import random
    import pymysql
    import pandas as pd

    try:
        con = pymysql.Connect(
            host=host,  # 远程登录主机的ip地址
            port=port,  # mysql服务器的端口
            user=user,  # mysql的用户
            passwd=pwd,  # 密码
            db=dbname,  # 需要的数据库名称
        )
        # 获取本次的游标
        cur = con.cursor()
    except pymysql.Error as e:
        print("Error %d: %s" % (e.args[0], e.args[1]))

    cur.execute('select * from team_channel_relation ')
    team_to_channel = cur.fetchall()
    team_to_channel = list(map(list, zip(*team_to_channel)))
    team = team_to_channel[0][:]
    channel = team_to_channel[1][:]
    team_to_channel = {'team': team, 'channel': channel}
    team_to_channel = pd.DataFrame(team_to_channel)
    del team
    del channel

    for channel_file in channels_list:

        gexf = Gexf("Gephi.org", "A Web network")

        output = gexf.addGraph("directed", "static", "A Web network")

        cur.execute('select * from people_channel_relation where channel_id = \'' + channel_file + '\' ')
        person_and_channel = cur.fetchall()

        if len(person_and_channel) == 0:
            output_file = open(".\graphdata_of_" + channel_file + ".gexf", "wb")
            gexf.write(output_file)
            output_file.close()

        else:

            person_and_channel = list(map(list, zip(*person_and_channel)))
            person = person_and_channel[0][:]
            channel = person_and_channel[1][:]
            person_and_channel = {'person': person, 'channel': channel}
            person_and_channel = pd.DataFrame(person_and_channel)
            del person
            del channel

            person_list = person_and_channel['person']

            # print(person_and_channel)

            channel_node = output.addNodeAttribute(force_id="Channel", title="channel", type="String")
            team_node = output.addNodeAttribute(force_id="Team", title="team", type="String")
            weight_node = output.addNodeAttribute(force_id="Weight", title="weight", type="float")

            people_id = dict()
            #            id_list = ['people']
            #            for id in id_list:
            #                cur.execute('select * from ' + id)
            #                data = cur.fetchall()
            #                for i in data:
            #                    exec(id + '_id[\'' + i[0] + '\']=i[1]')
            #                    print('id', id)

            person_set = set(person_list)
            person_to_channel = []
            for tem_person in person_set:
                cur.execute('select * from people_channel_relation where people_id = \'' + tem_person + '\' ')

                person_to_channel = person_to_channel + list(cur.fetchall())

            person_to_channel = list(map(list, zip(*person_to_channel)))
            person = person_to_channel[0][:]
            channel = person_to_channel[1][:]
            person_to_channel = {'person': person, 'channel': channel}
            person_to_channel = pd.DataFrame(person_to_channel)

            # print(person_to_channel)

            cc = 0
            num2333 = len(person_set)
            for tem_id in person_set:
                #                print(cc / num2333)
                try:
                    tem_name = people_id[tem_id]
                except KeyError:
                    tem_name = "Null"

                tem_channel_list = set(person_to_channel[person_to_channel['person'] == tem_id]['channel'])

                tmp_node = output.addNode(tem_id, tem_name)
                tmp_node.addAttribute(weight_node, str(int(100 * random.random())))
                tem_team_list = set()
                for tem_channel in tem_channel_list:
                    # cur.execute('select team_id from team_channel_relation where channel_id = \'' + tem_channel + '\'')
                    # tem_team_list = cur.fetchall()
                    tem_team_list = tem_team_list | set(
                        team_to_channel[team_to_channel['channel'] == tem_channel]['team'])
                for tem_team in tem_team_list:
                    tmp_node.addAttribute(team_node, tem_team)

                for tem_channel in tem_channel_list:
                    tmp_node.addAttribute(channel_node, tem_channel)

                cc = cc + 1

            m = 'mention_based_graph_info'
            cur.execute('select * from ' + m + ' where channel_id = \'' + channel_file + '\' ')
            data = cur.fetchall()

            msg_att = output.addEdgeAttribute(force_id="Message", title="message", type='String', defaultValue='None')
            weight_att = output.addEdgeAttribute(force_id="Weight", title="weight", type='float', defaultValue='0')
            date_att = output.addEdgeAttribute(force_id="Date", title="date", type='float', defaultValue='None')
            channel_att = output.addEdgeAttribute(force_id="Channel", title="channel", type='String',
                                                  defaultValue='None')
            team_att = output.addEdgeAttribute(force_id="Team", title="team", type='String', defaultValue='None')
            cc = 0
            numhehe = len(data)
            for tem_m in data:
                #                print(cc / numhehe)
                sender, receiver, text, channel_id, team_id, ts = tem_m
                blob = TextBlob(text)
                weight = str(blob.sentiment.polarity)
                try:
                    tem_edge = output.addEdge(sender + receiver + str(cc), sender, receiver, weight=weight)
                except Exception:
                    try:
                        tmp_node = output.addNode(receiver, 'Null')
                        tem_edge = output.addEdge(sender + receiver + str(cc), sender, receiver, weight=weight)
                    except Exception:
                        tmp_node = output.addNode(sender, 'Null')
                        tem_edge = output.addEdge(sender + receiver + str(cc), sender, receiver, weight=weight)

                cc = cc + 1
                tem_edge.addAttribute(msg_att, text)
                tem_edge.addAttribute(weight_att, weight)
                tem_edge.addAttribute(date_att, str(ts))
                tem_edge.addAttribute(team_att, team_id)
                tem_edge.addAttribute(channel_att, channel_id)


            output_file = open(dir_path + "/mention_based/{}_{}.gexf".format(team_name, channel_file), "wb")
            gexf.write(output_file)
            output_file.close()
    )

    bts = {}
    temp = {}

    # CSV bts parsing
    for row in reader1:
        array = row[0].split(";")
        id = str(array[0])
        lat = array[1]
        lon = array[2]
        flux = array[3]
        bts[id] = (lat, lon, flux)

        # create graph
    gexf = Gexf("Quentin", "foreigner people")
    graph = gexf.addGraph("directed", "static", "world graph map")

    # add Attribute
    latitude = graph.addNodeAttribute("lat", "0", "double")
    longitude = graph.addNodeAttribute("lon", "0", "double")
    flux_interne = graph.addNodeAttribute("flux_interne", "0", "double")

    edgeid = 1
    for row in reader2:
        # CSV edge parsing
        array = row[0].split(";")
        source = array[0]
        target = array[1]
        weight = array[2]
Beispiel #47
0
#!/usr/bin/python

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("hellowrld.gexf","w")
gexf.write(output_file)

# test GexfImport
f = open("exp.gexf")
o = open("exp_bootstrap_bootstrap.gexf", "w+")

gexf_import = GexfImport(f).gexf()
gexf_import.write(o)
import sys, pprint
from gexf import Gexf
import codecs

input_file = codecs.open("pr-lpa2.txt", encoding="utf-8")
gexf = Gexf("2018st27", "LPA")
graph = gexf.addGraph("undirected", "static", "LPA")

class_attr = graph.addNodeAttribute("Class",
                                    defaultValue=0,
                                    type="integer",
                                    force_id="class")
pr_attr = graph.addNodeAttribute('PageRank',
                                 defaultValue=0.0,
                                 type="double",
                                 force_id="pageranks")

i = 0
library = {}
for line in input_file:
    strs = line.split("\t")
    label_name = strs[0].split("#")
    library[label_name[1]] = i
    node = graph.addNode(i, label_name[1])
    node.addAttribute(class_attr, label_name[0])
    node.addAttribute(pr_attr, label_name[2])
    i += 1

i = 0
input_file.seek(0)
Beispiel #49
0
DB_NAME = "picbrother"

try:
    conn = MySQLdb.connect(host=DB_HOST,
                           user=DB_USER,
                           passwd=DB_PSWD,
                           db=DB_NAME,
                           use_unicode=True)
except MySQLdb.Error, e:
    print "Error %d: %s" % (e.args[0], e.args[1])
    sys.exit(1)

cursor = conn.cursor()

if __name__ == '__main__':
    gexf = Gexf("PIC BROTHER", "Le graph du pic !")
    graph = gexf.addGraph("undirected", "static", "Picbrother graph")
    cursor.execute("SELECT id, fb_id, first_name, last_name FROM  `T_USER` ")
    rows = cursor.fetchall()
    for row in rows:
        print "%s, %s, %s, %s" % (row[0], row[1], row[2], row[3])
        #graph.addNode(row[0],u"#%s => %s %s" % (row[1],row[2],row[3])) #  ==>  Probléme d'accent... Comment faire ?
        graph.addNode(str(row[0]),
                      unac.unac_string("%s %s" % (row[2], row[3]), "utf-8"))
    print "Nombre de user ajoute : %d" % cursor.rowcount
    cursor.execute(
        "SELECT U1.user_id, U2.user_id, COUNT( * ) weight FROM  `J_PHOTO_USER` U1,  `J_PHOTO_USER` U2 WHERE U1.photo_id = U2.photo_id AND U1.user_id != U2.user_id GROUP BY U1.user_id, U2.user_id"
    )
    rows = cursor.fetchall()
    edge_id = 0
    for row in rows:
Beispiel #50
0
def make_network_graph(current_date, topic_id, topic, window_size, key_user_labeled=True):
    date = current_date

    if key_user_labeled:
        key_users = read_key_users(current_date, window_size, topic, top_n=10)
    else:
        key_users = []

    #topic = acquire_topic_name(topic_id)
    #if not topic:
    #    return None
              
    G = make_network(topic, date, window_size)

    N = len(G.nodes())

    if not N:
        return ''

    node_degree = nx.degree(G)

    G = cut_network(G, node_degree)
    
    gexf = Gexf("Yang Han", "Topic Network")

    node_id = {}
    graph = gexf.addGraph("directed", "static", "demp graph")
    graph.addNodeAttribute('name', type='string', force_id='name')
    graph.addNodeAttribute('location', type='string', force_id='location')
    graph.addNodeAttribute('timestamp', type='int', force_id='timestamp')

    pos = nx.spring_layout(G)

    node_counter = 0
    edge_counter = 0

    for node in G.nodes():
        x, y = pos[node]
        degree = node_degree[node]
        if node not in node_id:
            node_id[node] = node_counter
            node_counter += 1
        uid = node
        if uid in key_users:
            _node = graph.addNode(node_id[node], str(node), x=str(x), y=str(y), z='0', r='255', g='51', b='51', size=str(degree))
        else:
            _node = graph.addNode(node_id[node], str(node), x=str(x), y=str(y), z='0', r='0', g='204', b='204', size=str(degree))
        user_info = acquire_user_by_id(uid)
        if user_info:
            _node.addAttribute('name', user_info['name'])
            _node.addAttribute('location', user_info['location'])
        else:
            _node.addAttribute('name', 'Unknown')
            _node.addAttribute('location', 'Unknown')
        #_node.addAttribute('timestamp', str(uid_ts[uid]))

    for edge in G.edges():
        start, end = edge
        start_id = node_id[start]
        end_id = node_id[end]
        graph.addEdge(str(edge_counter), str(start_id), str(end_id))
        edge_counter += 1

    return etree.tostring(gexf.getXML(), pretty_print=True, encoding='utf-8', xml_declaration=True)
Beispiel #51
0
#!/usr/bin/python2.7

import pygeoip as geoip
from gexf import Gexf

db = geoip.Database('GeoIP.dat')
gexf = Gexf("Alexander Konovalov", "The internet map")
graph = gexf.addGraph("directed", "static", "The internet map")

attrid = graph.addNodeAttribute("country", "??", type="string")


def addIP(ip):
    if not graph.nodeExists(ip):
        info = db.lookup(ip)
        country = "??"
        if info.country:
            country = info.country
        n = graph.addNode(ip, ip)
        n.addAttribute(attrid, country)


n = 0
with open("out.csv") as f:
    for line in f:
        ips = tuple(line.strip().replace("-", ".").split('\t'))
        if len(ips) < 2:
            continue
        ip1, ip2 = ips
        addIP(ip1)
        addIP(ip2)
Beispiel #52
0

# Main
if __name__ == "__main__":
	# MongoDB connection
	client 		= MongoClient('localhost', 27017)
	db 			= client.ccl
	mails 		= db.mails
	threads 	= db.threads
	authors		= db.authors

	# Create a graph
	g = nx.Graph()

	# Create a graph
	gexf 		= Gexf("Anne L'Hote","A bipartite graph of threads vs. authors")
	graph 		= gexf.addGraph("directed","static","ccl")

	output_file	= open("data/ccl.gexf","w")

	for author in authors.find():
		# Add a node by author
		g.add_node(str(author['_id']), {
			'viz': {
				'color': {'r': authorColor['r'], 'g': authorColor['g'], 'b': authorColor['b']},
				'position': {'y': random.random(), 'x': random.random(), 'z': 0.0},
				'size': author['count']},
			'label': author['email']
		})

	for thread in threads.find({'count':{'$gte':3}}):
Beispiel #53
0
def status():
    user = login_user(session)
    if user is None:
        return ""

    client = Client(APP_KEY, APP_SECRET, CALLBACK_URL)
    client.set_token(user["access_token"])

    id = request.args.get('id', '')
    since_id = request.args.get('since_id', 0)

    reposts, source_weibo, since_id = load_reposts(app, weibo2db, r, client, id, since_id)
    if len(reposts) == 0:
        return ""

    #root
    tree_nodes = []
    node = source_weibo["user"]["name"]
    location = source_weibo["user"]["location"]
    datetime = source_weibo["created_at"]
    img_url = source_weibo["user"]["profile_image_url"]
    weibo_url = "http://weibo.com/" + \
        str(source_weibo["user"]["id"]) + \
        "/" + base62.mid_to_str(source_weibo["mid"])

    tree_nodes.append(Tree(node, location, datetime, int(id), img_url, weibo_url))

    for repost in reposts:
        try:
            node = repost["user"]["name"]
            wid = repost["id"]
            img_url = repost["user"]["profile_image_url"]
            location = repost["user"]["location"]
            datetime = repost['created_at']
            weibo_url = "http://weibo.com/" + \
                str(repost["user"]["id"]) + \
                "/" + base62.mid_to_str(repost["mid"])
            tree_nodes.append(Tree(node, location, datetime, wid, img_url, weibo_url))
        except:
            app.logger.error(repost)
            continue

        repost_users = re.findall(r'//@(\S+?):', repost["text"])
        if len(repost_users):
            flag = True
            for node in tree_nodes[::-1]:
                if node.node == repost_users[0]:
                    node.append_child(tree_nodes[-1])
                    flag = False
                    break

            if flag:
                tree_nodes[0].append_child(tree_nodes[-1])
        else:
            tree_nodes[0].append_child(tree_nodes[-1])

    dt, max_width = buchheim.buchheim(tree_nodes[0])

    gexf = Gexf("MOON_CLJ", "haha")
    graph = gexf.addGraph("directed", "static", "weibo graph")
    graph.addNodeAttribute("img_url", type="URI", force_id="img_url")
    graph.addNodeAttribute("name", type="string", force_id="name")
    graph.addNodeAttribute("location", type="string", force_id="location")
    graph.addNodeAttribute("datetime", type="string", force_id="datetime")
    graph.addNodeAttribute("repost_num", type="integer", force_id="repost_num")
    graph.addNodeAttribute("weibo_url", type="URI", force_id="weibo_url")

    rank = node_rank(tree_nodes[0])
    add_node_edge(dt, graph, rank, Count(), max_width=max_width)

    return etree.tostring(gexf.getXML(), pretty_print=True, encoding='utf-8', xml_declaration=True)
Beispiel #54
0
from gexf import Gexf
import cjdnsmap
import sys

if len(sys.argv) != 2:
	print "usage: " + sys.argv[0] + " output.gexf"
	sys.exit()

output_filename = sys.argv[1]


nodes, edges = cjdnsmap.get_map()

gexf = Gexf("cjdns mapper", "A map of cjdns network")
graph = gexf.addGraph("undirected", "static", "cjdns network graph")

attr_ip                 = graph.addNodeAttribute("IP",                 type="string")
attr_version            = graph.addNodeAttribute("Version",            type="integer")
attr_connections        = graph.addNodeAttribute("Connections",        type="integer")
attr_active_connections = graph.addNodeAttribute("Active connections", type="integer")

attr_quality            = graph.addEdgeAttribute("Quality", 0,         type="float")


for node in nodes:
	n = graph.addNode(node.ip, node.name)
	n.addAttribute(attr_ip, node.ip)
	n.addAttribute(attr_version, str(node.version))
	n.addAttribute(attr_connections, str(node.connections))
	n.addAttribute(attr_active_connections, str(node.active_connections))