Projects/laydi
Projects
/
laydi
Archived
7
0
Fork 0

* 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:
Einar Ryeng 2006-08-01 13:22:39 +00:00
parent 37b4d55a86
commit b3d681ef16
3 changed files with 113 additions and 36 deletions

View File

@ -113,9 +113,6 @@ class Dataset:
def asarray(self):
"""Returns the numeric array (data) of dataset"""
if not self.has_array:
raise ValueError, "Dataset is empty"
else:
return self._array
def add_array(self,array):
@ -199,13 +196,6 @@ class CategoryDataset(Dataset):
def __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):
"""Returns data as collection along dim"""
@ -236,7 +226,7 @@ class GraphDataset(Dataset):
self.has_graph = False
def asnetworkx(self,nx_type='graph'):
dim = self.get_dim_names()[0]
dim = self.get_dim_name()[0]
ids = self.get_identifiers(dim)
adj_mat = self.asarray()
G = self._graph_from_adj_matrix(adj_mat,labels=ids)
@ -270,6 +260,7 @@ class GraphDataset(Dataset):
tail = labels[i]
G.add_edge(head,tail)
return G
Dataset._all_dims=set()
class Selection:

View File

@ -11,6 +11,7 @@ from matplotlib.figure import Figure
from matplotlib.numerix import arange, sin, pi
from matplotlib.widgets import RectangleSelector
from matplotlib import cm
import networkx
from system import logger
@ -471,24 +472,23 @@ class ScatterPlot(Plot):
# find indices of selected area
if x1>x2:
if y1<y2:
index =scipy.nonzero((xdata<x1) & (xdata>x2) & (ydata>y1) & (ydata<y2))
else:
index =scipy.nonzero((xdata<x1) & (xdata>x2) & (ydata<y1) & (ydata>y2))
else:
#logger.log('debug','Selection x_start less than x_end')
if y1<y2:
#logger.log('debug','Selection y_start less than y_end')
x1, x2 = x2, x1
if y1>y2:
y1, y2 = y2, y1
assert x1<=x2
assert y1<=y2
index = scipy.nonzero((xdata>x1) & (xdata<x2) & (ydata>y1) & (ydata<y2))
else:
#logger.log('debug','Selection y_start bigger than y_end')
index =scipy.nonzero((xdata>x1) & (xdata<x2) & (ydata<y1) & (ydata>y2))
print 'index:', index
print 'current_dim', self.current_dim
ids = self.dataset_1.get_identifiers(self.current_dim, index)
print 'ids', ids
self.selection_listener(self.current_dim, ids)
def selection_changed(self, selection):
ids = selection[self.current_dim] # current identifiers
print 'ids: ', ids
index = self.dataset_1.get_indices(self.current_dim, ids)
xdata_new = scipy.take(self.xaxis_data, index) #take data
@ -499,16 +499,52 @@ class ScatterPlot(Plot):
self.canvas.draw()
class NetworkPlot(Plot):
def __init__(self, dataset, name='Network Plot'):
Plot.__init__(self)
def __init__(self, graph, **kw):
# 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)
self.ax = ax = fig.add_subplot(111)
if not kw.has_key('name'):
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.canvas.show()
# Initial draw
networkx.draw_networkx(self.graph, ax=self.ax, **kw)
# Setup toolbar
self._toolbar = NavToolbar(self.canvas, None)
self._toolbar.set_property('show-arrow', False)
self._toolbar.set_select_callback(self.rectangle_select_callback)
@ -516,8 +552,49 @@ class NetworkPlot(Plot):
def get_toolbar(self):
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):
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

View File

@ -4,6 +4,7 @@ from system import dataset, logger, plots, workflow
#import gostat
from scipy import array,randn,log
import cPickle
import networkx
class TestWorkflow (workflow.Workflow):
@ -103,7 +104,15 @@ class TestDataFunction(workflow.Function):
x = randn(20,30)
X = dataset.Dataset(x)
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):
def __init__(self):