From dc7f7dbde29a36d94c3a5a184ed263aff6506b76 Mon Sep 17 00:00:00 2001 From: einarr Date: Thu, 26 Jul 2007 15:45:42 +0000 Subject: [PATCH] Added existing_identifiers function to Dataset. Added colouring drag'n'drop to DAGPlot in gobrowser module. --- fluents/dataset.py | 6 ++++++ workflows/gobrowser.py | 45 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/fluents/dataset.py b/fluents/dataset.py index 4428c49..4a56761 100644 --- a/fluents/dataset.py +++ b/fluents/dataset.py @@ -194,6 +194,12 @@ class Dataset: for key in idents if self._map[dim].has_key(key)] return asarray(index) + def existing_identifiers(self, dim, idents): + if not isinstance(idents, list) and not isinstance(idents, set): + raise ValueError("idents needs to be a list/set got: %s" %type(idents)) + + return [key for key in idents if self._map[dim].has_key(key)] + def copy(self): """ Returns deepcopy of dataset. """ diff --git a/workflows/gobrowser.py b/workflows/gobrowser.py index d3a2f0a..ea1e6af 100644 --- a/workflows/gobrowser.py +++ b/workflows/gobrowser.py @@ -477,7 +477,6 @@ class PlotDagFunction(workflow.Function): def run(self, selection): g = self.get_network(list(selection['go-terms'])) -# print g.edges() ds = dataset.GraphDataset(networkx.adj_matrix(g), [('go-terms', g.nodes()), ('_go-terms', g.nodes())], name="DAG") @@ -674,3 +673,47 @@ class DagPlot(plots.Plot): self.selected_nodes.set_linewidth(linewidth) self.canvas.draw() + def is_mappable_with(self, obj): + """Returns True if dataset/selection is mappable with this plot. + """ + if isinstance(obj, fluents.dataset.Dataset): + if self.current_dim in obj.get_dim_name(): + return True + return False + + def _update_color_from_dataset(self, ds): + """Updates the facecolors from a dataset. + """ + + array = ds.asarray() + + #only support for 2d-arrays: + try: + m, n = array.shape + except: + raise ValueError, "No support for more than 2 dimensions." + # is dataset a vector or matrix? + if not n==1: + # we have a category dataset + if isinstance(ds, fluents.dataset.CategoryDataset): + vec = dot(array, diag(arange(n))).sum(1) + else: + vec = array.sum(1) + else: + vec = array.ravel() + + indices = ds.get_indices(self.current_dim, self.nodes) + nodes = ds.existing_identifiers(self.current_dim, self.nodes) + v = vec.take(indices, 0) + d = dict(zip(nodes, list(v))) + + map_vec = zeros(len(self.nodes)) + for i, n in enumerate(self.nodes): + map_vec[i] = d.get(n, -1) + + # update facecolors + self.node_collection.set_array(map_vec) + self.node_collection.set_clim(map_vec.min(), map_vec.max()) + self.node_collection.update_scalarmappable() #sets facecolors from array + self.canvas.draw() +