source: orange/Orange/OrangeWidgets/Unsupervised/OWNxInsideView.py @ 10719:0b86e2af93e0

Revision 10719:0b86e2af93e0, 2.5 KB checked in by mstajdohar, 2 years ago (diff)

Fixed some bugs.

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        selected = self._nx_explorer.networkCanvas.selected_nodes()
32        if selected is None or len(selected) <= 0:
33            self._center_node = graph.nodes_iter().next()
34        else:
35            self._center_node = selected[0]
36
37        nodes = self._get_neighbors()
38        return Orange.network.nx.Graph.subgraph(self._network, nodes)
39
40    def update_network(self):
41        nodes = self._get_neighbors()
42        subnet = Orange.network.nx.Graph.subgraph(self._network, nodes)
43
44        if self._nx_explorer is not None:
45            self._nx_explorer.change_graph(subnet)
46
47    def set_nhops(self, nhops):
48        self._nhops = nhops
49
50    def node_selection_changed(self):
51        selection = self._nx_explorer.networkCanvas.selected_nodes()
52        if len(selection) == 1:
53            self._center_node = selection[0]
54            self.update_network()
55
56    def _get_neighbors(self):
57        nodes = set([self._center_node])
58        for n in range(self._nhops):
59            neighbors = set()
60            for node in nodes:
61                neighbors.update(self._network.neighbors(node))
62            nodes.update(neighbors)
63        return nodes
64
65class OWNxInsideView(OWWidget):
66
67    settingsList = ['_nhops']
68
69    def __init__(self, parent=None, signalManager=None):
70        OWWidget.__init__(self, parent, signalManager, 'Net Inside View')
71
72        self.inputs = []
73        self.outputs = [("Nx View", Orange.network.NxView)]
74
75        self._nhops = 2
76
77        self.loadSettings()
78
79        ib = OWGUI.widgetBox(self.controlArea, "Preferences", orientation="vertical")
80        OWGUI.spin(ib, self, "_nhops", 1, 6, 1, label="Number of hops: ", callback=self.update_view)
81
82        self.inside_view = NxInsideView(self._nhops)
83        self.send("Nx View", self.inside_view)
84
85    def update_view(self):
86        self.inside_view.set_nhops(self._nhops)
87
88        self.inside_view.update_network()
Note: See TracBrowser for help on using the repository browser.