1

The nodes of my graphs are instances that have some methods. I have correctly added the nodes, but now I want to check, for those nodes if they have the same group_id, add_edges. I am using igraph

import igraph as ig

def making_graph(researchers):
    g = ig.Graph()
    for each in researchers:
        for i in range(len(each)):
            g.add_vertex(each[i])
            g.vs[i]['researcher_id'] = each[i].get_researcher_id()
            g.vs[i]['name'] = each[i].get_name()
            g.vs[i]['sex'] = each[i].get_sex()

    g.add_edges() # if researchers have the same group_id, add edge
    return g

The class for researcher is

class Researcher:

    def __init__(self, group_id, research_id, name, tit, sex,
             tot_nac_2011, tot_nac_2014, tot_int_2011, tot_int_2014, tot_bbl_2011, tot_bbl_2014):

        self.group_id = group_id
        self.research_id = research_id
        self.name = name
        self.tit = tit
        self.sex = sex
        self.producao = [tot_nac_2011, tot_nac_2014, tot_int_2011, tot_int_2014, tot_bbl_2011, tot_bbl_2014]

    def get_group_id(self):
        return self.group_id

    def get_researcher_id(self):
        return self.research_id

    def get_sex(self):
        return self.sex

and when I call making_graph I pass a list with a list of researchers

EDITED. This also does not work. Why?

def making_graph(researchers):
    g = ig.Graph()
    for each in researchers:
        for i in range(len(each)):
            g.add_vertex(each[i])
            g.vs[i]['researcher_id'] = each[i].get_researcher_id()
            g.vs[i]['name'] = each[i].get_name()
            g.vs[i]['sex'] = each[i].get_sex()
            for other in researchers:
                for j in range(len(other)):
                    if each[i].get_group_id() == other[j].get_group_id():
                        g.add_edge([(g.vs[i], g.vs[j])])
return g
Akshat Mahajan
  • 9,543
  • 4
  • 35
  • 44
B Furtado
  • 1,488
  • 3
  • 20
  • 34
  • Do you mean if _all_ researchers have the same `group_id`? – Akshat Mahajan Apr 23 '16 at 23:51
  • For this specific case, yes. But when I gather more graphs (106,000), I will need to access the nodes method researcher.get_group_id() and if it is the same of another node, then, add_edge(researcher1, researcher2) – B Furtado Apr 23 '16 at 23:53
  • One step at a time. You may wish to check out [this question](http://stackoverflow.com/questions/3844801/check-if-all-elements-in-a-list-are-identical). – Akshat Mahajan Apr 23 '16 at 23:53
  • I would also recommend posting your _full_ code, including the definition of your `researcher` nodes. Please see [Minimal, Complete Viable Example](http://stackoverflow.com/help/mcve). – Akshat Mahajan Apr 23 '16 at 23:57
  • researchers is a list. In the first element researchers[0], there are 11 instances of object class Researcher. They have 4 variables, and the methods to get them... – B Furtado Apr 24 '16 at 00:01
  • All useful information which should be part of the original question, _including_ the class definition for `Researcher`. – Akshat Mahajan Apr 24 '16 at 00:01

1 Answers1

0

Now, it seems this does work:

def making_graph(researchers):
    g = ig.Graph()
    for each in researchers:
        for i in range(len(each)):
            g.add_vertex(each[i])
            g.vs[i]['group_id'] = each[i].get_group_id()
            g.vs[i]['researcher_id'] = each[i].get_researcher_id()
            g.vs[i]['name'] = each[i].get_name()
            g.vs[i]['sex'] = each[i].get_sex()
    for v in g.vs:
        for w in g.vs:
            if v['group_id'] == w['group_id'] and v != w and g.are_connected(v, w) is False:
                g.add_edge(v, w)
    return g
B Furtado
  • 1,488
  • 3
  • 20
  • 34