user2052251
user2052251

Reputation: 161

removing number of edges in python

I was wondering if there is pythonic way to do this.

Suppose I list of dictionary:

{'source': 338, 'target': 343, 'value': 0.667693}
{'source': 339, 'target': 342, 'value': 0.628195}
{'source': 340, 'target': 346, 'value': 0.529861}
{'source': 340, 'target': 342, 'value': 0.470139}
{'source': 341, 'target': 342, 'value': 0.762871}
{'source': 342, 'target': 349, 'value': 0.664869}
{'source': 343, 'target': 347, 'value': 0.513025}
{'source': 343, 'target': 344, 'value': 0.486975}
{'source': 344, 'target': 347, 'value': 0.536706}
{'source': 344, 'target': 349, 'value': 0.463294}
{'source': 345, 'target': 349, 'value': 0.546326}
{'source': 345, 'target': 346, 'value': 0.453674}

basically its an undirected graph. but its very messy. i want to clean it a bit.

So, I want to leave top 2 nodes which have most edges like in original format..

and for rest of the nodes... have atmost 5 edges attached to it.

I am just maintaining a dict with counts... reverse sorting it..

then saving top 2 and going thru list again.. and removing the edges but checking for top 2 ones..

Is there a cleaner way to do this.

My buggy.. messy sample code:

import json
from pprint import pprint
import operator
json_data=open('topics350_1.json')

data = json.load(json_data)
edges = data["links"]
node_count_dict = {}
super_nodes = 3
min_nodes = 5

for edge in edges:
    keys = [edge['source'], edge['target']]
    for key in keys:
        if key in node_count_dict:
            node_count_dict[key] +=1
        else:
            node_count_dict[key] = 1

sorted_nodes = sorted(node_count_dict.iteritems(), key=operator.itemgetter(1),reverse = True)           
#print sorted_nodes 
top_nodes = sorted_nodes[super_nodes]
final_node_count = {}
for key in sorted_nodes:
    final_node_count[key[0]] = 0
print final_node_count
link_list = []
for edge in edges:
    keys = [edge['source'], edge['target']]
    for key in keys:
        if key not in top_nodes:
            if final_node_count[key] < min_nodes:
                link_list.append(edge)
print link_list




#print data['links']

Upvotes: 0

Views: 242

Answers (1)

Yueyoum
Yueyoum

Reputation: 3073

I strongly recommend you using networkx to work with Graph.

import networkx as nx
G = nx.Graph()
# build your Graph
# G.add_node(), G.add_nodes_from(), G.add_edge(), G.add_edges_from()...

nodes = [(g, G.degree(g)) for g in G.nodes()]
# nodes like this: [(338, 4), (340, 7)...]
# item one is the node, and item two is the edges connected with this node

nodes.sort(key=lambda n: n[1], reverse=True)

# you wanna delete the third node and other nodes which edges at most 5, right?
G.remove_node(nodes[2][1])
for n, e in nodes:
    if e > 5:
        G.remove_node(n)

But, Just Your Code above, I will make it like below:

from collections import Counter

sources = []
for edge in edges:
    source.append(edge['source'])
    source.append(edge['target'])

sources_count = Counter(sources)
sources_count = sorted(source_count.items(), key=lambda s: s[1], reverse=True)

sources_count.pop(2)
valid_nodes = filter(lambda s: s[1] <= 5, sources_count)

link_list = filter(
    lambda e: e['source'] not in valid_nodes and e['target'] not in valid_nodes, 
    edges
)

Upvotes: 1

Related Questions