def _boxplot(self, data_list, names, colors, id_, neg_ctrl): fig = pyplot.figure(figsize=(len(data_list)/3+5, 20)) ax1 = fig.add_subplot(111) ax1.set_title('IBB Analysis %s' % self.PLOT_LABELS[id_]) bp = pyplot.boxplot(data_list, patch_artist=True) pyplot.setp(bp['boxes'], color='black') pyplot.setp(bp['whiskers'], color='black') pyplot.setp(bp['fliers'], markerfacecolor='w', marker='o', markeredgecolor='k') i = 0 for c, d in zip(colors, data_list): if len(d) > 0: b = bp['boxes'][i] pyplot.setp(b, facecolor=rgb2hex(c, mpl=False)) i += 1 #ax1.set_xticks([x+0.8 for x in range(len(data_list))]) if len(neg_ctrl) > 0: neg_line = numpy.concatenate(neg_ctrl).mean() ax1.plot([0, len(data_list)],[neg_line, neg_line], 'k--', label="Neg. Ctrl.") xtickNames = pyplot.setp(ax1, xticklabels=names) pyplot.setp(xtickNames, rotation=45, horizontalalignment='right', fontsize=10) data_max = numpy.nanmax(numpy.array(map(numpy.mean, numpy.array(data_list)))) if data_max < 0.5 * self.timeing_ylim_range[1]: pyplot.ylim(self.timeing_ylim_range[0], self.timeing_ylim_range[1]/2.0) if data_max < 0.25 * self.timeing_ylim_range[1]: pyplot.ylim(self.timeing_ylim_range[0], self.timeing_ylim_range[1]/4.0) if data_max < 0.125 * self.timeing_ylim_range[1]: pyplot.ylim(self.timeing_ylim_range[0], self.timeing_ylim_range[1]/8.0) fig.savefig(os.path.join(self.path_out, '_timing_ibb_events_%s_box.pdf' % id_), format='pdf')
def _plot_valid_bars(self, result): data = [] names = [] bar_labels = ('valid', 'signal', 'split', 'ibb_onset', 'nebd_onset', 'prophase_onset') bar_colors = map(lambda x: rgb2hex(x, mpl=False), [ [int(x * 255) for x in pyplot.cm.Greens(2)[0:3]], ] + [ tuple(x) for x in (pyplot.cm.RdBu(range(0, 256, 62)) * 255)[:, :3].astype('uint8')[:, :3] ]) def mycmp(x, y): return cmp( result[x]['positions'][0].__getattribute__( self.color_sort_by).lower(), result[y]['positions'][0].__getattribute__( self.color_sort_by).lower()) for group_name in sorted(result, cmp=mycmp): data.append(result[group_name]['valid']) names.append(group_name) self._plot_multi_bars(data, bar_labels, bar_colors, names, show_number=False)
def _barplot(self, data_list, names, colors, id_, neg_ctrl): fig = pyplot.figure(figsize=(len(data_list)/3+5, 20)) ax1 = fig.add_subplot(111) ax1.set_title('IBB Analysis %s' % self.PLOT_LABELS[id_]) ax1.bar(range(len(data_list)), map(numpy.mean, numpy.array(data_list)), yerr=map(numpy.std, numpy.array(data_list)), color=map(lambda x:rgb2hex(x, mpl=False), colors), ecolor='k', align='center', ) #ax1.set_xticks([x+0.8 for x in range(len(data_list))]) if len(neg_ctrl) > 0: neg_line = numpy.concatenate(neg_ctrl).mean() ax1.plot([0, len(data_list)],[neg_line, neg_line], 'k--', label="Neg. Ctrl.") #xtickNames = pyplot.setp(ax1, xticklabels=names) #pyplot.setp(xtickNames, rotation=45, horizontalalignment='right', fontsize=10) ax1.set_xticks(range(len(data_list))) xticks = ax1.set_xticklabels(names) pyplot.setp(xticks, rotation=45, horizontalalignment='right') pyplot.ylim(*self.timeing_ylim_range) data_max = numpy.nanmax(numpy.array(map(numpy.mean, numpy.array(data_list)))) if data_max < 0.5 * self.timeing_ylim_range[1]: pyplot.ylim(self.timeing_ylim_range[0], self.timeing_ylim_range[1]/2.0) if data_max < 0.25 * self.timeing_ylim_range[1]: pyplot.ylim(self.timeing_ylim_range[0], self.timeing_ylim_range[1]/4.0) if data_max < 0.125 * self.timeing_ylim_range[1]: pyplot.ylim(self.timeing_ylim_range[0], self.timeing_ylim_range[1]/8.0) fig.savefig(os.path.join(self.path_out, '_timing_ibb_events_%s_bar.pdf' % id_), format='pdf')
def binary_classification(self, data): """Perform an initial binary classifcation to distinguish between mitotic and non-mitotic objects/cells. 1) Perform kmeans clustering (at least 5 initialisation, emperically determined) 2) and assing mitotic labels (0 means "non-mitotic", 1 "mitotic") assuming is that non-mitotic cell are the larger population. """ km = KMeans(n_clusters=2, n_init=5) labels = km.fit_predict(data) # assign labels, 0 for non-mitotic, 1 for mitotic # from now on labels a biologial meaning binary_class_names = {0: "non-mitotic", 1: "mitotic"} if labels[labels==1].size > labels[labels==0].size: labels = np.where(labels, 0, 1) for i, node in enumerate(self.graph.node_list()): obj = self.graph.node_data(node) obj.iLabel = labels[i] obj.strClassName = binary_class_names[obj.iLabel] obj.strHexColor = rgb2hex(BINARY_CMAP(obj.iLabel), mpl=True) obj.dctProb.update({0:np.nan, 1:np.nan})
def binary_classification(self, data): """Perform an initial binary classifcation to distinguish between mitotic and non-mitotic objects/cells. 1) Perform kmeans clustering (at least 5 initialisation, emperically determined) 2) and assing mitotic labels (0 means "non-mitotic", 1 "mitotic") assuming is that non-mitotic cell are the larger population. """ km = KMeans(n_clusters=2, n_init=5) labels = km.fit_predict(data) # assign labels, 0 for non-mitotic, 1 for mitotic # from now on labels a biologial meaning binary_class_names = {0: "non-mitotic", 1: "mitotic"} if labels[labels == 1].size > labels[labels == 0].size: labels = np.where(labels, 0, 1) for i, node in enumerate(self.graph.node_list()): obj = self.graph.node_data(node) obj.iLabel = labels[i] obj.strClassName = binary_class_names[obj.iLabel] obj.strHexColor = rgb2hex(BINARY_CMAP(obj.iLabel), mpl=True) obj.dctProb.update({0: np.nan, 1: np.nan})
def _save_class_labels(self, labels, nodes, probabilities, prefix="unsupervised"): # clear labels from binary classification for node in self.graph.node_list(): obj = self.graph.node_data(node) obj.iLabel = None obj.strClassName = None obj.strHexColor = None obj.dctProb.clear() for node, label, probs in zip(nodes, labels.flatten(), probabilities): obj = self.graph.node_data(node) obj.iLabel = label obj.strClassName = "%s-%d" % (prefix, label) obj.dctProb = dict((i, v) for i, v in enumerate(probs)) rgb = self.classdef.colormap(label) obj.strHexColor = rgb2hex(rgb)
def _boxplot(self, data_list, names, colors, id_, neg_ctrl): fig = pyplot.figure(figsize=(len(data_list) / 3 + 5, 20)) ax1 = fig.add_subplot(111) ax1.set_title('IBB Analysis %s' % self.PLOT_LABELS[id_]) bp = pyplot.boxplot(data_list, patch_artist=True) pyplot.setp(bp['boxes'], color='black') pyplot.setp(bp['whiskers'], color='black') pyplot.setp(bp['fliers'], markerfacecolor='w', marker='o', markeredgecolor='k') i = 0 for c, d in zip(colors, data_list): if len(d) > 0: b = bp['boxes'][i] pyplot.setp(b, facecolor=rgb2hex(c, mpl=False)) i += 1 #ax1.set_xticks([x+0.8 for x in range(len(data_list))]) if len(neg_ctrl) > 0: neg_line = numpy.concatenate(neg_ctrl).mean() ax1.plot([0, len(data_list)], [neg_line, neg_line], 'k--', label="Neg. Ctrl.") xtickNames = pyplot.setp(ax1, xticklabels=names) pyplot.setp(xtickNames, rotation=45, horizontalalignment='right', fontsize=10) data_max = numpy.nanmax( numpy.array(map(numpy.mean, numpy.array(data_list)))) if data_max < 0.5 * self.timeing_ylim_range[1]: pyplot.ylim(self.timeing_ylim_range[0], self.timeing_ylim_range[1] / 2.0) if data_max < 0.25 * self.timeing_ylim_range[1]: pyplot.ylim(self.timeing_ylim_range[0], self.timeing_ylim_range[1] / 4.0) if data_max < 0.125 * self.timeing_ylim_range[1]: pyplot.ylim(self.timeing_ylim_range[0], self.timeing_ylim_range[1] / 8.0) fig.savefig(os.path.join(self.path_out, '_timing_ibb_events_%s_box.pdf' % id_), format='pdf')
def _plot_valid_bars(self, result): data = [] names = [] bar_labels = ('valid', 'signal', 'split', 'ibb_onset', 'nebd_onset', 'prophase_onset') bar_colors = map(lambda x:rgb2hex(x, mpl=False), [[int(x*255) for x in pyplot.cm.Greens(2)[0:3]],] + [tuple(x) for x in (pyplot.cm.RdBu(range(0,256,62))*255)[:,:3].astype('uint8')[:,:3]] ) def mycmp(x, y): return cmp(result[x]['positions'][0].__getattribute__(self.color_sort_by).lower(), result[y]['positions'][0].__getattribute__(self.color_sort_by).lower()) for group_name in sorted(result, cmp=mycmp): data.append(result[group_name]['valid']) names.append(group_name) self._plot_multi_bars(data, bar_labels, bar_colors, names, show_number=False)
def _barplot(self, data_list, names, colors, id_, neg_ctrl): fig = pyplot.figure(figsize=(len(data_list) / 3 + 5, 20)) ax1 = fig.add_subplot(111) ax1.set_title('IBB Analysis %s' % self.PLOT_LABELS[id_]) ax1.bar( range(len(data_list)), map(numpy.mean, numpy.array(data_list)), yerr=map(numpy.std, numpy.array(data_list)), color=map(lambda x: rgb2hex(x, mpl=False), colors), ecolor='k', align='center', ) #ax1.set_xticks([x+0.8 for x in range(len(data_list))]) if len(neg_ctrl) > 0: neg_line = numpy.concatenate(neg_ctrl).mean() ax1.plot([0, len(data_list)], [neg_line, neg_line], 'k--', label="Neg. Ctrl.") #xtickNames = pyplot.setp(ax1, xticklabels=names) #pyplot.setp(xtickNames, rotation=45, horizontalalignment='right', fontsize=10) ax1.set_xticks(range(len(data_list))) xticks = ax1.set_xticklabels(names) pyplot.setp(xticks, rotation=45, horizontalalignment='right') pyplot.ylim(*self.timeing_ylim_range) data_max = numpy.nanmax( numpy.array(map(numpy.mean, numpy.array(data_list)))) if data_max < 0.5 * self.timeing_ylim_range[1]: pyplot.ylim(self.timeing_ylim_range[0], self.timeing_ylim_range[1] / 2.0) if data_max < 0.25 * self.timeing_ylim_range[1]: pyplot.ylim(self.timeing_ylim_range[0], self.timeing_ylim_range[1] / 4.0) if data_max < 0.125 * self.timeing_ylim_range[1]: pyplot.ylim(self.timeing_ylim_range[0], self.timeing_ylim_range[1] / 8.0) fig.savefig(os.path.join(self.path_out, '_timing_ibb_events_%s_bar.pdf' % id_), format='pdf')
def _save_class_labels(self, labels, nodes, probabilities, prefix='unsupervised'): # clear labels from binary classification for node in self.graph.node_list(): obj = self.graph.node_data(node) obj.iLabel = None obj.strClassName = None obj.strHexColor = None obj.dctProb.clear() for node, label, probs in zip(nodes, labels.flatten(), probabilities): obj = self.graph.node_data(node) obj.iLabel = label obj.strClassName = "%s-%d" % (prefix, label) obj.dctProb = dict((i, v) for i, v in enumerate(probs)) rgb = self.classdef.colormap(label) obj.strHexColor = rgb2hex(rgb)
def qcolor_to_hex(qcolor): return rgb2hex((qcolor.red(), qcolor.green(), qcolor.blue()), mpl=False)