def test_attribute_mixing_dict_multigraph(self):
     d = nx.attribute_mixing_dict(self.M, "fish")
     d_result = {
         "one": {"one": 4},
         "two": {"two": 2},
     }
     assert d == d_result
示例#2
0
def get_assortativity(network):
    assortativity_dict = nx.attribute_mixing_dict(network,
                                                  'sex',
                                                  normalized=True)

    if 'female' in assortativity_dict:
        f2f = assortativity_dict['female']['female'] if 'female' in assortativity_dict['female']\
                                                     else 0
        f2f = round(f2f, 3)

        f2m = assortativity_dict['female']['male'] if 'male' in assortativity_dict['female']\
                                                    else 0
        f2m = round(f2m, 3)
    else:
        f2f = 0
        f2m = 0

    if 'male' in assortativity_dict:
        m2m = assortativity_dict['male']['male'] if 'male' in assortativity_dict['male']\
                                                            else 0
        m2m = round(m2m, 3)

        m2f = assortativity_dict['male']['female'] if 'female' in assortativity_dict['male']\
                                                                else 0
        m2f = round(m2f, 3)
    else:
        m2m = 0
        m2f = 0

    return (f2f, m2m, f2m, m2f)
示例#3
0
 def test_attribute_mixing_dict_undirected(self):
     d=nx.attribute_mixing_dict(self.G,'fish')
     d_result={'one':{'one':2,'red':1},
               'two':{'two':2,'blue':1},
               'red':{'one':1},
               'blue':{'two':1}
               }
     assert_equal(d,d_result)
 def test_attribute_mixing_dict_undirected(self):
     d = nx.attribute_mixing_dict(self.G, "fish")
     d_result = {
         "one": {"one": 2, "red": 1},
         "two": {"two": 2, "blue": 1},
         "red": {"one": 1},
         "blue": {"two": 1},
     }
     assert d == d_result
示例#5
0
 def test_attribute_mixing_dict_multigraph(self):
     d = nx.attribute_mixing_dict(self.M, 'fish')
     d_result = {
         'one': {
             'one': 4
         },
         'two': {
             'two': 2
         },
     }
     assert d == d_result
    def plotMixPattern(self, attribute, attribute_value):
        mix_dict = nx.attribute_mixing_dict(self.graph, attribute)
        norm_mix = self.mixingDictingToProb(mix_dict)

        # O atributo abaixo define o valor de atributo que desejamos analisar o padrão de mixagem
        # por exemplo, mixagem para o partido PT ou para o sexo Masculino
        D = norm_mix[attribute_value]

        plt.bar(range(len(D)), list(D.values()), align='center')
        plt.xticks(range(len(D)), list(D.keys()), rotation='vertical')

        plt.show()
def atribute_mixing(graph, probability):
    d = nx.attribute_mixing_dict(graph, attribute,  normalized=probability)
    od = collections.OrderedDict(sorted(d.items()))
    for key, value in d.items():
        print("------------------------------------")
        print("Atribute1: " + str(key))
        print("------------------------------------")
        ovalue = collections.OrderedDict(sorted(value.items()))
        for k, v in value.items():
            print("Atribute2: " + str(k), end='\t')
            if probability == True:
                print("Probability: " + str(v))
            else:
                print("Count: " + str(v))
示例#8
0
def attribute_info():
    c_list, m_list, p_list = util.get_lists()
    cmp_list = util.append_arrays(c_list, m_list, p_list)

    interaction_types = ['mentions', 'replies', 'retweets']
    for interaction_type in interaction_types:
        edge_list = util.get_edge_list(interaction_type)
        g = create_graph_edge_weights(edge_list)
        cmp_g = create_graph_subset(g, cmp_list)
        add_types(cmp_g)
        print('{} Assortativity: '.format(interaction_type),
              nx.attribute_assortativity_coefficient(cmp_g, 'type'))
        print('{} Mixing: '.format(interaction_type),
              nx.attribute_mixing_dict(cmp_g, 'type', normalized=True))
示例#9
0
 def test_attribute_mixing_dict_directed(self):
     d = nx.attribute_mixing_dict(self.D, 'fish')
     d_result = {
         'one': {
             'one': 1,
             'red': 1
         },
         'two': {
             'two': 1,
             'blue': 1
         },
         'red': {},
         'blue': {}
     }
     assert d == d_result
def attribute_assortativity_coefficient(G, nodal_attribute):
    mixing_dict = nx.attribute_mixing_dict(G, nodal_attribute)
    total_sum_of_degree = float(2 * G.size())

    sum_e_ii = 0
    for row in mixing_dict:
        column = row
        sum_e_ii += mixing_dict[row][column] / total_sum_of_degree

    sum_e_ij = 0
    for row in mixing_dict:
        sum_e_ij += sum(
            np.asarray(mixing_dict[row].values()) / total_sum_of_degree
        )**2  # (\sum_i e_ij)**2 as a_i == b_i for undirected graph.

    return (sum_e_ii - sum_e_ij) / (1 - sum_e_ij)
示例#11
0
def avrg_runner(link_pred_obj: LinkPrediction, count: int, mu: int,
                basedir: str) -> np.array:
    """
    Runs AVRG on the input graph and returns the combined adjacency matrix
    """

    extract_type = 'mu_random'
    mu = 5
    clustering = 'leiden'

    link_pred.set_method(method=f'AVRG_link_{clustering}_{mu}')
    train_g: nx.Graph = nx.from_scipy_sparse_matrix(link_pred_obj.adj_train,
                                                    create_using=nx.Graph)
    train_g.add_edges_from(
        link_pred_obj.val_edges.tolist())  # add the validation edges too
    nx.set_node_attributes(train_g,
                           name='value',
                           values=link_pred_obj.vals_dict)

    list_of_list_clusters = get_clustering(g=train_g,
                                           outdir=basedir,
                                           clustering=clustering,
                                           use_pickle=False,
                                           filename='na',
                                           write_pickle=False)

    root = create_tree(list_of_list_clusters) if isinstance(
        list_of_list_clusters, list) else list_of_list_clusters
    train_lmg = nx_to_lmg(nx_g=train_g)
    extractor = AVRGLinkExtractor(g=train_lmg,
                                  attr_name=att_name,
                                  clustering=clustering,
                                  mu=mu,
                                  extract_type=extract_type,
                                  root=root)

    avrg_link = extractor.extract()

    mix_dict = nx.attribute_mixing_dict(train_g, 'value')
    gen = AttributedEnsureAllNodesGenerator(grammar=avrg_link,
                                            attr_name='value',
                                            mixing_dict=mix_dict,
                                            use_fancy_rewiring=True)
    gen_graphs = gen.generate(10)
    return gen_graphs
示例#12
0
 def test_attribute_mixing_dict_undirected(self):
     d = nx.attribute_mixing_dict(self.G, 'fish')
     d_result = {
         'one': {
             'one': 2,
             'red': 1
         },
         'two': {
             'two': 2,
             'blue': 1
         },
         'red': {
             'one': 1
         },
         'blue': {
             'two': 1
         }
     }
     assert_equal(d, d_result)
示例#13
0
 def test_attribute_mixing_dict_multigraph(self):
     d=nx.attribute_mixing_dict(self.M,'fish')
     d_result={'one':{'one':4},
               'two':{'two':2},
               }
     assert_equal(d,d_result)