def visualise_triples(triples, triple_args_function=None, ignore_properties=VIS_IGNORE_PROPERTIES): """ Visualise a triples representation of Triples such as retrieved from TreeTransformer.get_triples @param triple_args_function: a function of Triple to dict that gives optional arguments for a triple """ g = dot.Graph() nodes = {} # Node -> dot.Node # create nodes for n in iterate_nodes(triples): label = "%s: %s" % (n.position, n.label) for k, v in n.__dict__.iteritems(): if k not in ignore_properties: label += "\\n%s: %s" % (k, v) node = dot.Node(id="node_%s" % n.position, label=label) g.addNode(node) nodes[n] = node # create edges for triple in triples: kargs = triple_args_function(triple) if triple_args_function else {} if 'label' not in kargs: kargs['label'] = triple.predicate g.addEdge(nodes[triple.subject], nodes[triple.object], **kargs) # some theme options g.theme.graphattrs["rankdir"] = "BT" g.theme.shape = "rect" g.theme.edgesize = 10 g.theme.fontsize = 10 return g
def get_graph(self, r): g = dot.Graph() self.add_edges(r, g) if not self.options['blue']: g.theme.green = True elif self.options['bw']: g.theme = dot.BWDotTheme() return g
def run(self): assocTable = self.get_table() intervals = sorted({i for (i,q,q2,p) in assocTable}) if self.options['network_output'] == 'ool': self.output = 'json-html' assocTable = table3.WrappedTable(assocTable, cellfunc = lambda a: self.format(a) if isinstance(a, float) else a) return self.outputResponse(assocTable, self.output_type) elif self.options['network_output'] == 'oo': if self._interval: cols = {} assocs = {(x,y) for (i,x,y,s) in assocTable} cols = {u"{x}\u2192{y}".format(x=x, y=y) : (x,y) for (x,y) in assocs} def cn(colname): return colname.replace(u"\u2192", "_") colnames = sorted(cols) columns = [{"sTitle": "x", "sWidth": "100px", "sType": "objid", "mDataProp": "x"}] columns += [{"mData": cn(colname), "sTitle": colname, "sWidth": "70px", "mDataProp": cn(colname)} for colname in colnames] data = [] scores = {(i,x,y) : self.format(s) for (i,x,y,s) in assocTable} for i in intervals: row = {"x" : i} for c in colnames: row[cn(c)] = scores.get((i, ) + cols[c], 0) data.append(row) form_proxy = dict( outputType={"data" : "table"}, dateInterval={"data" : self._interval}, xAxis={"data" : "date"}, yAxis={"data" : "searchTerm"} ) scriptoutput = render_to_string('api/webscripts/aggregation.html', { 'dataJson':json.dumps(data), 'columnsJson':json.dumps(columns), 'aggregationType':'association', 'datesDict': json.dumps({}), 'graphOnly': 'false', 'labels' : '{}', 'ownForm': form_proxy, 'relative': '1', }) return self.outputJsonHtml(scriptoutput) else: # convert list to dict and make into dict table result = table3.DictTable() result.rowNamesRequired=True for i,x,y,a in assocTable: result.addValue(x,y,self.format(a)) result.columns = sorted(result.columns) result.rows = sorted(result.rows) self.output = 'json-html' res = self.outputResponse(result, self.output_type) return res elif self.options['network_output'] == 'oon': html = "" for interval in intervals: g = dot.Graph() threshold = self.options.get('graph_threshold') if not threshold: threshold = 0 nodes = {} def getnode(x): if not x in nodes: id = "node_%i_%s" % (len(nodes), re.sub("\W","",x)) nodes[x] = dot.Node(id, x) return nodes[x] for i, x,y,a in assocTable: if i != interval: continue if threshold and a < threshold: continue opts = {} if self.options['graph_label']: opts['label'] = self.format(a) w = 1 + 10 * a g.addEdge(getnode(x),getnode(y), weight=w, **opts) if interval is not None: html += "<h1>{interval}</h1>".format(**locals()) g.normalizeWeights() html += g.getHTMLObject() self.output = 'json-html' return self.outputResponse(html, unicode)
def run(self): es = amcates.ES() filters = dict(keywordsearch.filters_from_form(self.data)) queries = list(keywordsearch.queries_from_form(self.data)) qargs = dict(filters=filters, score=True, fields=[]) probs = {} p = lambda f: 1 - (.5 ** f) probs = {q.label : {r.id : p(r.score) for r in es.query_all(query=q.query, **qargs)} for q in queries} assocTable = table3.ListTable(colnames=["From", "To", "Association"]) for q in probs: sumprob1 = float(sum(probs[q].values())) if sumprob1 == 0: continue for q2 in probs: if q == q2: continue sumproduct = 0 for id, p1 in probs[q].iteritems(): p2 = probs[q2].get(id) if not p2: continue sumproduct += p1*p2 p = sumproduct / sumprob1 assocTable.addRow(q, q2, p) if self.options['network_output'] == 'ool': self.output = 'json-html' assocTable = table3.WrappedTable(assocTable, cellfunc = lambda a: self.format(a) if isinstance(a, float) else a) return self.outputResponse(assocTable, AssociationsScript.output_type) elif self.options['network_output'] == 'oo': # convert list to dict and make into dict table result = table3.DictTable() result.rowNamesRequired=True for x,y,a in assocTable: result.addValue(x,y,self.format(a)) self.output = 'json-html' return self.outputResponse(result, AssociationsScript.output_type) elif self.options['network_output'] == 'oon': g = dot.Graph() threshold = self.options.get('graph_threshold') if not threshold: threshold = 0 nodes = {} def getnode(x): if not x in nodes: id = "node_%i_%s" % (len(nodes), re.sub("\W","",x)) nodes[x] = dot.Node(id, x) return nodes[x] for x,y,a in assocTable: if threshold and a < threshold: continue opts = {} if self.options['graph_label']: opts['label'] = self.format(a) w = 1 + 10 * a g.addEdge(getnode(x),getnode(y), weight=w, **opts) html = g.getHTMLObject() self.output = 'json-html' return self.outputResponse(html, unicode)