* system/plots.py: Added NetworkPlot, which may or may not work correctly.
* system/dataset.py: Removed a few lines of obsolete code.
This commit is contained in:
parent
37b4d55a86
commit
b3d681ef16
|
@ -113,9 +113,6 @@ class Dataset:
|
||||||
|
|
||||||
def asarray(self):
|
def asarray(self):
|
||||||
"""Returns the numeric array (data) of dataset"""
|
"""Returns the numeric array (data) of dataset"""
|
||||||
if not self.has_array:
|
|
||||||
raise ValueError, "Dataset is empty"
|
|
||||||
else:
|
|
||||||
return self._array
|
return self._array
|
||||||
|
|
||||||
def add_array(self,array):
|
def add_array(self,array):
|
||||||
|
@ -199,13 +196,6 @@ class CategoryDataset(Dataset):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
Dataset.__init__(self)
|
Dataset.__init__(self)
|
||||||
self.has_collection = False
|
|
||||||
|
|
||||||
def as_array(self):
|
|
||||||
"""Returns data as binary matrix"""
|
|
||||||
if not self.has_array and self.has_collection:
|
|
||||||
#build numeric array
|
|
||||||
pass
|
|
||||||
|
|
||||||
def as_collection(self,dim):
|
def as_collection(self,dim):
|
||||||
"""Returns data as collection along dim"""
|
"""Returns data as collection along dim"""
|
||||||
|
@ -236,7 +226,7 @@ class GraphDataset(Dataset):
|
||||||
self.has_graph = False
|
self.has_graph = False
|
||||||
|
|
||||||
def asnetworkx(self,nx_type='graph'):
|
def asnetworkx(self,nx_type='graph'):
|
||||||
dim = self.get_dim_names()[0]
|
dim = self.get_dim_name()[0]
|
||||||
ids = self.get_identifiers(dim)
|
ids = self.get_identifiers(dim)
|
||||||
adj_mat = self.asarray()
|
adj_mat = self.asarray()
|
||||||
G = self._graph_from_adj_matrix(adj_mat,labels=ids)
|
G = self._graph_from_adj_matrix(adj_mat,labels=ids)
|
||||||
|
@ -270,6 +260,7 @@ class GraphDataset(Dataset):
|
||||||
tail = labels[i]
|
tail = labels[i]
|
||||||
G.add_edge(head,tail)
|
G.add_edge(head,tail)
|
||||||
return G
|
return G
|
||||||
|
|
||||||
Dataset._all_dims=set()
|
Dataset._all_dims=set()
|
||||||
|
|
||||||
class Selection:
|
class Selection:
|
||||||
|
|
117
system/plots.py
117
system/plots.py
|
@ -11,6 +11,7 @@ from matplotlib.figure import Figure
|
||||||
from matplotlib.numerix import arange, sin, pi
|
from matplotlib.numerix import arange, sin, pi
|
||||||
from matplotlib.widgets import RectangleSelector
|
from matplotlib.widgets import RectangleSelector
|
||||||
from matplotlib import cm
|
from matplotlib import cm
|
||||||
|
import networkx
|
||||||
from system import logger
|
from system import logger
|
||||||
|
|
||||||
|
|
||||||
|
@ -471,44 +472,79 @@ class ScatterPlot(Plot):
|
||||||
|
|
||||||
# find indices of selected area
|
# find indices of selected area
|
||||||
if x1>x2:
|
if x1>x2:
|
||||||
if y1<y2:
|
x1, x2 = x2, x1
|
||||||
index =scipy.nonzero((xdata<x1) & (xdata>x2) & (ydata>y1) & (ydata<y2))
|
if y1>y2:
|
||||||
else:
|
y1, y2 = y2, y1
|
||||||
index =scipy.nonzero((xdata<x1) & (xdata>x2) & (ydata<y1) & (ydata>y2))
|
assert x1<=x2
|
||||||
else:
|
assert y1<=y2
|
||||||
#logger.log('debug','Selection x_start less than x_end')
|
|
||||||
if y1<y2:
|
index = scipy.nonzero((xdata>x1) & (xdata<x2) & (ydata>y1) & (ydata<y2))
|
||||||
#logger.log('debug','Selection y_start less than y_end')
|
print 'index:', index
|
||||||
index =scipy.nonzero((xdata>x1) & (xdata<x2) & (ydata>y1) & (ydata<y2))
|
print 'current_dim', self.current_dim
|
||||||
else:
|
|
||||||
#logger.log('debug','Selection y_start bigger than y_end')
|
|
||||||
index =scipy.nonzero((xdata>x1) & (xdata<x2) & (ydata<y1) & (ydata>y2))
|
|
||||||
|
|
||||||
ids = self.dataset_1.get_identifiers(self.current_dim, index)
|
ids = self.dataset_1.get_identifiers(self.current_dim, index)
|
||||||
|
print 'ids', ids
|
||||||
self.selection_listener(self.current_dim, ids)
|
self.selection_listener(self.current_dim, ids)
|
||||||
|
|
||||||
def selection_changed(self, selection):
|
def selection_changed(self, selection):
|
||||||
ids = selection[self.current_dim] # current identifiers
|
ids = selection[self.current_dim] # current identifiers
|
||||||
|
print 'ids: ', ids
|
||||||
|
|
||||||
index = self.dataset_1.get_indices(self.current_dim, ids)
|
index = self.dataset_1.get_indices(self.current_dim, ids)
|
||||||
xdata_new = scipy.take(self.xaxis_data,index) #take data
|
xdata_new = scipy.take(self.xaxis_data, index) #take data
|
||||||
ydata_new = scipy.take(self.yaxis_data,index)
|
ydata_new = scipy.take(self.yaxis_data, index)
|
||||||
self.ax.clear()
|
self.ax.clear()
|
||||||
self.ax.plot(self.xaxis_data,self.yaxis_data,'og')
|
self.ax.plot(self.xaxis_data,self.yaxis_data,'og')
|
||||||
self.ax.plot(xdata_new,ydata_new,'or')
|
self.ax.plot(xdata_new,ydata_new,'or')
|
||||||
self.canvas.draw()
|
self.canvas.draw()
|
||||||
|
|
||||||
class NetworkPlot(Plot):
|
class NetworkPlot(Plot):
|
||||||
def __init__(self, dataset, name='Network Plot'):
|
def __init__(self, graph, **kw):
|
||||||
Plot.__init__(self)
|
# Set member variables and call superclass' constructor
|
||||||
|
self.graph = graph.asnetworkx()
|
||||||
|
self.dataset = graph
|
||||||
|
self.keywords = kw
|
||||||
|
self.dim_name = self.dataset.get_dim_name(0)
|
||||||
|
|
||||||
fig = Figure(figsize=(5,4), dpi=72)
|
if not kw.has_key('name'):
|
||||||
self.ax = ax = fig.add_subplot(111)
|
kw['name'] = self.dataset.get_name()
|
||||||
|
if not kw.has_key('prog'):
|
||||||
|
kw['prog'] = 'neato'
|
||||||
|
if not kw.has_key('pos') or kw['pos']:
|
||||||
|
print 'graph: ', self.graph, type(self.graph)
|
||||||
|
print 'prog:', kw['prog']
|
||||||
|
kw['pos'] = networkx.drawing.nx_pydot.graphviz_layout(self.graph, kw['prog'])
|
||||||
|
Plot.__init__(self, kw['name'])
|
||||||
|
|
||||||
self.canvas = FigureCanvas(fig)
|
# Keep node size and color as dicts for fast lookup
|
||||||
|
if kw.has_key('node_size') and cb.iterable(kw['node_size']):
|
||||||
|
kw.remove('node_size')
|
||||||
|
self.node_size = {}
|
||||||
|
for id, size in zip(self.dataset[self.dim_name], kw['node_size']):
|
||||||
|
self.node_size[id] = size
|
||||||
|
|
||||||
|
if kw.has_key('node_color') and cb.iterable(kw['node_color']):
|
||||||
|
kw.remove('node_color')
|
||||||
|
self.node_color = {}
|
||||||
|
for id, color in zip(self.dataset[self.dim_name], kw['node_color']):
|
||||||
|
self.node_color[id] = color
|
||||||
|
|
||||||
|
# FIXME: What is figsize?
|
||||||
|
self.fig = Figure(figsize=(5, 4), dpi=72)
|
||||||
|
self.ax = self.fig.add_subplot(111)
|
||||||
|
# FIXME: ax shouldn't be in kw at all
|
||||||
|
if kw.has_key('ax'):
|
||||||
|
kw.pop('ax')
|
||||||
|
|
||||||
|
# Add canvas and show
|
||||||
|
self.canvas = FigureCanvas(self.fig)
|
||||||
self.add(self.canvas)
|
self.add(self.canvas)
|
||||||
self.canvas.show()
|
self.canvas.show()
|
||||||
|
|
||||||
|
# Initial draw
|
||||||
|
networkx.draw_networkx(self.graph, ax=self.ax, **kw)
|
||||||
|
|
||||||
|
# Setup toolbar
|
||||||
self._toolbar = NavToolbar(self.canvas, None)
|
self._toolbar = NavToolbar(self.canvas, None)
|
||||||
self._toolbar.set_property('show-arrow', False)
|
self._toolbar.set_property('show-arrow', False)
|
||||||
self._toolbar.set_select_callback(self.rectangle_select_callback)
|
self._toolbar.set_select_callback(self.rectangle_select_callback)
|
||||||
|
@ -516,8 +552,49 @@ class NetworkPlot(Plot):
|
||||||
def get_toolbar(self):
|
def get_toolbar(self):
|
||||||
return self._toolbar
|
return self._toolbar
|
||||||
|
|
||||||
|
def rectangle_select_callback(self, x1, y1, x2, y2):
|
||||||
|
'event1 and event2 are the press and release events'
|
||||||
|
pos = self.kw['pos']
|
||||||
|
ydata = zeros((len(pos),),'l')
|
||||||
|
xdata = zeros((len(pos),),'l')
|
||||||
|
node_ids = []
|
||||||
|
c = 0
|
||||||
|
for name,(x,y) in pos.items():
|
||||||
|
node_ids.append(name)
|
||||||
|
xdata[c] = x
|
||||||
|
ydata[c] = y
|
||||||
|
c+=1
|
||||||
|
|
||||||
|
# find indices of selected area
|
||||||
|
if x1 > x2:
|
||||||
|
x1, x2 = x2, x1
|
||||||
|
if y1 > y2:
|
||||||
|
y1, y2 = y2, y1
|
||||||
|
index = nonzero((xdata<x1) & (xdata>x2) & (ydata>y1) & (ydata<y2))
|
||||||
|
|
||||||
|
ids = [node_ids[i] for i in index]
|
||||||
|
self.selection_listener(self.dataset.get_dim_name(0), ids)
|
||||||
|
|
||||||
def selection_changed(self, selection):
|
def selection_changed(self, selection):
|
||||||
return None
|
ids = selection[self.dataset.get_dim_name(0)] # current identifiers
|
||||||
|
selected_nodes = list(ids.intersection(set(self.graph.nodes())))
|
||||||
|
unselected_nodes = list(ids.difference(selected_nodes))
|
||||||
|
|
||||||
|
unselected_colors = [self.node_color[x] for x in unselected_nodes]
|
||||||
|
unselected_sizes = [self.node_size[x] for x in unselected_nodes]
|
||||||
|
selected_sizes = [self.node_size[x] for x in selected_nodes]
|
||||||
|
|
||||||
|
self.ax.clear()
|
||||||
|
networkx.draw_networkx_edges(self.graph, self.kw['pos'], edge_list=self.graph.edges(), \
|
||||||
|
ax=self.ax, *kw)
|
||||||
|
|
||||||
|
networkx.draw_networkx_nodes(self.graph, self.kw['pos'], node_list=unselected_nodes, \
|
||||||
|
node_color=unselected_colors, node_size=unselected_sizes, ax=self.ax, *kw)
|
||||||
|
|
||||||
|
networkx.draw_networkx_nodes(self.graph, self.kw['pos'], node_list=selected_nodes, \
|
||||||
|
node_color='black', node_size=selected_sizes, ax=self.ax, *kw)
|
||||||
|
|
||||||
|
self.canvas.draw()
|
||||||
|
|
||||||
|
|
||||||
# Create a view-changed signal that should be emitted every time
|
# Create a view-changed signal that should be emitted every time
|
||||||
|
|
|
@ -4,6 +4,7 @@ from system import dataset, logger, plots, workflow
|
||||||
#import gostat
|
#import gostat
|
||||||
from scipy import array,randn,log
|
from scipy import array,randn,log
|
||||||
import cPickle
|
import cPickle
|
||||||
|
import networkx
|
||||||
|
|
||||||
class TestWorkflow (workflow.Workflow):
|
class TestWorkflow (workflow.Workflow):
|
||||||
|
|
||||||
|
@ -103,7 +104,15 @@ class TestDataFunction(workflow.Function):
|
||||||
x = randn(20,30)
|
x = randn(20,30)
|
||||||
X = dataset.Dataset(x)
|
X = dataset.Dataset(x)
|
||||||
p = plots.ScatterPlot(X, X, 'rows', 'rows', '0_1', '0_2')
|
p = plots.ScatterPlot(X, X, 'rows', 'rows', '0_1', '0_2')
|
||||||
return [X, plots.SinePlot(), p]
|
graph = networkx.XGraph()
|
||||||
|
for x in 'ABCDEF':
|
||||||
|
for y in 'ADE':
|
||||||
|
graph.add_edge(x, y, 3)
|
||||||
|
print networkx.adj_matrix(graph)
|
||||||
|
ds = dataset.GraphDataset(array(networkx.adj_matrix(graph)))
|
||||||
|
ds_plot = plots.NetworkPlot(ds)
|
||||||
|
|
||||||
|
return [X, ds, plots.SinePlot(), p, ds_plot]
|
||||||
|
|
||||||
class DatasetLog(workflow.Function):
|
class DatasetLog(workflow.Function):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
Reference in New Issue