source: orange/orange/OrangeWidgets/Unsupervised/OWNxInsideView.py @ 9259:56829909e839

Revision 9259:56829909e839, 2.4 KB checked in by miha <miha.stajdohar@…>, 2 years ago (diff)

Moved to Unsupervised.

Line 
1"""
2<name>Net Inside View</name>
3<description>Orange widget for community detection in networks</description>
4<icon>icons/Network.png</icon>
5<contact>Miha Stajdohar (miha.stajdohar(@at@)gmail.com)</contact> 
6<priority>6460</priority>
7"""
8
9import Orange
10import OWGUI
11
12from OWWidget import *
13
14class NxInsideView(Orange.network.NxView):
15    """Network Inside View
16   
17    """
18   
19    def __init__(self, nhops):
20        Orange.network.NxView.__init__(self)
21       
22        self._nhops = nhops
23        self._center_node = None
24       
25    def init_network(self, graph):
26        self._network = graph
27       
28        if graph is None:
29            return None
30       
31        self._center_node = graph.nodes_iter().next()
32        nodes = self._get_neighbors()
33        return Orange.network.nx.Graph.subgraph(self._network, nodes)
34   
35    def update_network(self):
36        nodes = self._get_neighbors()
37        subnet = Orange.network.nx.Graph.subgraph(self._network, nodes)
38
39        if self._nx_explorer is not None:
40            self._nx_explorer.change_graph(subnet)
41       
42    def set_nhops(self, nhops):
43        self._nhops = nhops
44       
45    def node_selection_changed(self):
46        selection = self._nx_explorer.networkCanvas.selected_nodes()
47        if len(selection) == 1:
48            self._center_node = selection[0]
49            self.update_network()
50       
51    def _get_neighbors(self):
52        nodes = set([self._center_node])
53        for n in range(self._nhops):
54            neighbors = set()
55            for node in nodes:
56                neighbors.update(self._network.neighbors(node))
57            nodes.update(neighbors)
58        return nodes
59       
60class OWNxInsideView(OWWidget):
61   
62    settingsList = ['_nhops']
63   
64    def __init__(self, parent=None, signalManager=None):
65        OWWidget.__init__(self, parent, signalManager, 'Net Inside View')
66       
67        self.inputs = []
68        self.outputs = [("Nx View", Orange.network.NxView)]
69       
70        self._nhops = 2
71       
72        self.loadSettings()
73       
74        ib = OWGUI.widgetBox(self.controlArea, "Preferences", orientation="vertical")
75        OWGUI.spin(ib, self, "_nhops", 1, 6, 1, label="Number of hops: ", callback=self.update_view)
76
77        self.inside_view = NxInsideView(self._nhops)
78        self.send("Nx View", self.inside_view)
79   
80    def update_view(self):
81        self.inside_view.set_nhops(self._nhops)
82       
83        self.inside_view.update_network()
84   
Note: See TracBrowser for help on using the repository browser.