def endPlot(self, layout, legend, path): title = path2str(path) r = ResultBlock( text="#$hv {}$#".format(title), title=title) r.hv = layout return ResultBlocks(r)
def endPlot(self, plts, legends, path): """close plots. """ title = path2str(path) figid = 10 lines = [] figid = self.bokeh_figure._id lines.append("") lines.append("#$bkh %s$#" % figid) lines.append("") r = ResultBlock("\n".join(lines), title=title) r.bokeh = self.bokeh_figure return ResultBlocks(r)
def __call__(self, track, slice=None): # note there are spaces behind the %(image)s directive to accomodate # for path substitution block = ''' .. figure:: %(image)s :height: 300 ''' blocks = ResultBlocks() tracks = sorted([x.asFile() for x in TRACKS]) for track in tracks: files = glob.glob( os.path.join(EXPORTDIR, "fastqc", "%s*_fastqc" % track)) for x, fn in enumerate(sorted(files)): y = x + 1 image = os.path.abspath( os.path.join(fn, "Images", "%s.png" % slice)) if not os.path.exists(image): continue blocks.append( ResultBlock(text=block % locals(), title=os.path.basename(fn))) return odict( (("rst", "\n".join(Utils.layoutBlocks(blocks, layout="columns-2"))), ))
def __call__(self, track, slice=None): edir = EXPORTDIR method = "utr_extension" blocks = ResultBlocks() filepath = "%(edir)s/%(method)s/%(track)s.readextension_%(region)s_%(direction)s.%(slice)s.png" block = \ ''' .. figure:: %(filename)s :height: 300 ''' # append spaces for file extension block = "\n".join([x + " " * 200 for x in block.split("\n")]) for region, direction in itertools.product( ("downstream", "upstream"), ("sense", "antisense", "anysense")): filename = filepath % locals() if os.path.exists(filename): blocks.append( ResultBlock(text=block % locals(), title="%(track)s %(region)s %(direction)s" % locals())) # else: # blocks.append( ResultBlock( "", # title = "%(track)s %(region)s %(direction)s" % locals() ) ) return odict( (("rst", "\n".join(Utils.layoutBlocks(blocks, layout="columns-3"))), ))
def endPlot(self, plots, legends, path): """close plots. """ result = ResultBlocks() title = path2str(path) for plot in plots: figid = plot._id lines = [] lines.append("") lines.append("#$bkh %s$#" % figid) lines.append("") r = ResultBlock("\n".join(lines), title=title) r.bokeh = plot result.append(r) return result
def render(self, dataframe, path): R.library('ggplot2') # add all indices as columns dataframe.reset_index(inplace=True) rframe = pandas.rpy.common.convert_to_r_dataframe(dataframe) # for the issue below, see: # http://stackoverflow.com/questions/12865218/getting-rid-of-asis-class-attribute unAsIs = R('''function (x) { if(typeof(x) %in% c("integer","double")) { class(x) <- "numeric" return (x)} else if (typeof(x) == "character") { class(x) <- "character" return (x) } else { return(x) } }''') rframe = R["as.data.frame"](R.lapply(rframe, unAsIs)) R.assign("rframe", rframe) # start plot R('''gp = ggplot(rframe)''') # add aesthetics and geometries try: pp = R('''gp + %s ''' % self.statement) except ValueError as msg: raise ValueError( "could not interprete R statement: " "gp + %s; msg=%s" % (self.statement, msg)) figname = re.sub('/', '_', path2str(path)) r = ResultBlock('#$ggplot %s$#' % figname, title=path2str(path)) r.rggplot = pp r.figname = figname return ResultBlocks(r)
def asFile(self, dataframe, row_headers, col_headers, title): '''save the table as HTML file. Multiple files of the same Renderer/Tracker combination are distinguished by the title. ''' self.debug("%s: saving %i x %i table as file'" % (id(self), len(row_headers), len(col_headers))) lines = [] lines.append("`%i x %i table <#$html %s$#>`__" % (len(row_headers), len(col_headers), title)) r = ResultBlock("\n".join(lines) + "\n", title=title) out = StringIO.StringIO() dataframe.to_csv(out) lines = out.getvalue().split("\n") # create an html table data = ["<table>"] data.append("<tr><th></th><th>%s</th></tr>" % "</th><th>".join(map(str, lines[0].split(",")))) data.extend(["<tr><td>%s</td></tr>" % ("</td><td>".join(x.split(","))) for x in lines[1:]]) data.append("</table>\n") # substitute links data = [re.sub( "`(.*?(?:\".+\"|\'.+\')?.*?)\s<(.*?(?:\".+\"|\'.+\')?.*?)>`_", r'<a href="\2">\1</a>', x) for x in data] r.html = "\n".join(data) return r
def render(self, dataframe, path): R.library('ggplot2') # add all indices as columns dataframe.reset_index(inplace=True) rframe = rpy2.robjects.pandas2ri.py2ri(dataframe) # for the issue below, see: # http://stackoverflow.com/questions/12865218/getting-rid-of-asis-class-attribute unAsIs = R('''function (x) { if("AsIs" %in% class(x)) { class(x) <- class(x)[-match("AsIs", class(x))] } return (x) } ''') rframe = R["as.data.frame"](R.lapply(rframe, unAsIs)) R.assign("rframe", rframe) # start plot R('''gp = ggplot(rframe)''') # add aesthetics and geometries try: pp = R('''gp + %s ''' % self.statement) except ValueError as msg: raise ValueError( "could not interprete R statement: " "gp + %s; msg=%s" % (self.statement, msg)) figname = re.sub('/', '_', path2str(path)) r = ResultBlock('#$ggplot %s$#' % figname, title=path2str(path)) r.rggplot = pp r.figname = figname return ResultBlocks(r)
def __call__(self, data, path): colorbar, factors, unique, xkcd = self.getColorBar(data) n_samples = data.shape[0] data = data.iloc[:, :n_samples] col_dict = dict(list(zip(unique, xkcd))) print(data.head()) seaborn.set(font_scale=.5) ax = seaborn.clustermap(data, row_colors=colorbar, col_colors=colorbar) plt.setp(ax.ax_heatmap.yaxis.set_visible(False)) for label in unique: ax.ax_col_dendrogram.bar( 0, 0, color=seaborn.xkcd_rgb[col_dict[label]], label=label, linewidth=0) ax.ax_col_dendrogram.legend(loc="center", ncol=len(unique)) return ResultBlocks(ResultBlock( '''#$mpl %i$#\n''' % ax.cax.figure.number, title='ClusterMapPlot'))
def asSpreadSheet(self, dataframe, row_headers, col_headers, title): '''save the table as an xls file. Multiple files of the same Renderer/Tracker combination are distinguished by the title. ''' self.debug("%s: saving %i x %i table as spread-sheet'" % (id(self), len(row_headers), len(col_headers))) is_hierarchical = isinstance(dataframe.index, pandas.core.index.MultiIndex) split = is_hierarchical and len(dataframe.index.levels) > 1 quick = len(dataframe) > 10000 if quick and not split: # quick writing, only append method works wb = openpyxl.Workbook(optimized_write=True) def fillWorksheet(ws, dataframe, title): ws.append([""] + list(col_headers)) for x, row in enumerate(dataframe.iterrows()): ws.append([path2str(row[0])] + list(row[1])) # patch: maximum title length seems to be 31 ws.title = title[:30] else: # do it cell-by-cell, this might be slow wb = openpyxl.Workbook(optimized_write=False) def fillWorksheet(ws, dataframe, title): # regex to detect rst hypelinks regex_link = re.compile('`(.*) <(.*)>`_') # write row names for row, row_name in enumerate(dataframe.index): # rows and columns start at 1 c = ws.cell(row=row + 2, column=1) c.value = row_name # write columns for column, column_name in enumerate(dataframe.columns): # set column title # rows and columns start at 1 c = ws.cell(row=1, column=column + 2) c.value = column_name # set column values dataseries = dataframe[column_name] if dataseries.dtype == object: for row, value in enumerate(dataseries): c = ws.cell(row=row + 2, column=column + 2) value = str(value) if value.startswith('`'): c.value, c.hyperlink =\ regex_link.match(value).groups() else: c.value = value else: for row, value in enumerate(dataseries): c = ws.cell(row=row + 2, column=column + 2) c.value = value # patch: maximum title length seems to be 31 ws.title = re.sub("/", "_", title)[:30] if len(wb.worksheets) == 0: wb.create_sheet() if split: # create separate worksheets for nested indices nlevels = len(dataframe.index.levels) paths = map(tuple, DataTree.unique( [x[:nlevels - 1] for x in dataframe.index.unique()])) ws = wb.worksheets[0] ws.title = 'Summary' ws.append( [""] * (nlevels - 1) + ["Worksheet", "Rows"]) for row, path in enumerate(paths): # select data frame as cross-section work = dataframe.xs(path, axis=0) title = path2str(path) if len(title) > 30: title = "sheet%i" % row ws.append(list(path) + [title, len(work)]) c = ws.cell(row=row + 1, column=nlevels) # this does not work in oocalc c.hyperlink = "#%s!A1" % title fillWorksheet(wb.create_sheet(), work, title=title) else: fillWorksheet(wb.worksheets[0], dataframe, title=title) # write result block lines = [] lines.append("`%i x %i table <#$xls %s$#>`__" % (len(row_headers), len(col_headers), title)) lines.append("") r = ResultBlock("\n".join(lines), title=title) r.xls = wb self.debug("%s: saved %i x %i table as spread-sheet'" % (id(self), len(row_headers), len(col_headers))) return r