Updating weight information depending on repeat of edges with networkx
Asked Answered
Z

1

22

I have a JSON feed data with lots of user relation in it such as:

"subject_id = 1, object_id = 2, object = added 
subject_id = 1, object_id = 2, object = liked
subject_id = 1, object_id = 3, object = added
subject_id = 2, object_id = 1, object = added"

Now I've used following code to convert JSON to networkx Graph:

def load(fname):
G = nx.DiGraph()
d = simplejson.load(open(fname))
for item in d:
    for attribute, value in item.iteritems():
        G.add_edge(value['subject_id'],value['object_id'])
return G

And the result is something like:

[('12820', '80842'), ('12820', '81312'), ('12820', '81311'), ('13317', '29'), ('12144', '81169'), ('13140', '16687'), ('13140', '79092'), ('13140', '78384'), ('13140', '48715'), ('13140', '54151'), ('13140', '13718'), ('13140', '4060'), ('13140', '9914'), ('13140', '32877'), ('13140', '9918'), ('13140', '4740'), ('13140', '47847'), ('13140', '29632'), ('13140', '72395'), ('13140', '48658'), ('13140', '78394'), ('13140', '4324'), ('13140', '4776'), ('13140', '78209'), ('13140', '51624'), ('13140', '66274'), ('13140', '38009'), ('13140', '80606'), ('13140', '13762'), ('13140', '28402'), ('13140', '13720'), ('13140', '9922'), ('13303', '81199'), ('13130', '70835'), ('13130', '7936'), ('13130', '30839'), ('13130', '11558'), ('13130', '32157'), ('13130', '2785'), ('13130', '9914'), ('13130', '73597'), ('13130', '9918'), ('13130', '49879'), ('13130', '62303'), ('13130', '64275'), ('13130', '48123'), ('13130', '8722'), ('13130', '43303'), ('13130', '39316'), ('13130', '78368'), ('13130', '28328'), ('13130', '57386'), ('13130', '30739'), ('13130', '9922'), ('13130', '71464'), ('13130', '50296'), ('12032', '65338'), ('13351', '81316'), ('13351', '16926'), ('13351', '80435'), ('13351', '79086'), ('12107', '16811'), ('12107', '70310'), ('12107', '10008'), ('12107', '25466'), ('12107', '36625'), ('12107', '81320'), ('12107', '48912'), ('12107', '62209'), ('12816', '79526'), ('12816', '79189'), ('13180', '39769'), ('13180', '81319'), ('12293', '70918'), ('12293', '59403'), ('12293', '76348'), ('12293', '12253'), ('12293', '65078'), ('12293', '61126'), ('12293', '12243'), ('12293', '12676'), ('12293', '11693'), ('12293', '78387'), ('12293', '54788'), ('12293', '26113'), ('12293', '50472'), ('12293', '50365'), ('12293', '66431'), ('12293', '29781'), ('12293', '50435'), ('12293', '48145'), ('12293', '79170'), ('12293', '76730'), ('12293', '13260'), ('12673', '29'), ('12672', '29'), ('13559', '9327'), ('12583', '25462'), ('12252', '50754'), ('12252', '11778'), ('12252', '38306'), ('12252', '48170'), ('12252', '5488'), ('12325', '78635'), ('12325', '4459'), ('12325', '68699'), ('12559', '80285'), ('12559', '78273'), ('12020', '48291'), ('12020', '4498'), ('12746', '48916'), ('13463', '56785'), ('13463', '47821'), ('13461', '80790'), ('13461', '4425'), ('12550', '48353')]

What I want to do is I want to increase weight if there are more than 1 relation between these users. So, as I demonstrated in JSON relation, subject_id 1 has 3 relations with subject_id 2 therefore their weight should be 3 whereas user 3 has only 1 relation with subject_id 1 and so it should be 1 as weight.

Update:

I suppose I've solved my problem with using:

def load(fname):
G = nx.MultiDiGraph()
d = simplejson.load(open(fname))
for item in d:
    for attribute, value in item.iteritems():
        if (value['subject_id'], value['object_id']) in G.edges():
            data = G.get_edge_data(value['subject_id'], value['object_id'], key='edge')
            G.add_edge(value['subject_id'], value['object_id'], key='edge', weight=data['weight']+1)
        else:
            G.add_edge(value['subject_id'], value['object_id'], key='edge', weight=1)

print G.edges(data=True)

But still your help would be nice about improving.

Zen answered 19/3, 2012 at 3:21 Comment(2)
Well, your "solution" might not be exactly a solution. MultiDiGraph allows multiple edges between two nodes. You are are not modifying the weight of previous edge but adding a new edge every time you encounter a pair. And you might clarify one thing, are you looking for directed graphs? You explanation offers undirected graphs since you consider (1,2) relation to be with weight 3 where there was two 1->2 relations and one 2->1 relations.Mixer
You are correct, I completely forgot about directions between nodes. Would you propose some solution to solve both weight update and directed graph please?Zen
M
44

You can simply use the weight attribute store your weights. You can check if an edge exists with has_edge method. Combining these would give you:

def load(fname):
    G = nx.DiGraph()
    d = simplejson.load(open(fname))
    for item in d:
        for attribute, value in item.iteritems():
            subject_id, object_id = value['subject_id'], value['object_id']
            if G.has_edge(subject_id, object_id):
                # we added this one before, just increase the weight by one
                G[subject_id][object_id]['weight'] += 1
            else:
                # new edge. add with weight=1
                G.add_edge(subject_id, object_id, weight=1)
    return G
Mixer answered 24/3, 2012 at 16:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.