def plot_roc_curves_with_ps(Y,Y_cv,Xs,p0,p1,p,pathological,diagnoses=None): if diagnoses is None: diagnoses = pathological.columns.values fig,ax = plt.subplots(len(diagnoses),4) sns.set_context("notebook", font_scale=1, rc={"lines.linewidth": 1.5, 'legend.fontsize': 12}) fig.set_size_inches(15,2*len(diagnoses)) for i in range(len(diagnoses)): diagnosis = diagnoses[i] ix = list(pathological.columns.values).index(diagnosis) if diagnoses is not None and diagnosis not in diagnoses: continue for j,key in enumerate(['basic','total','all']): X = Xs[key] if len(p1[key][ix]) and len(p0[key][ix]): ax[i,j].hist(p1[key][ix],bins=1000,range=(0,1),color='r', normed=True,cumulative=True,histtype='step') ax[i,j].hist(p0[key][ix],bins=1000,range=(0,1),color='k', normed=True,cumulative=True,histtype='step') ax[i,j].set_title(diagnosis) if i==ax.shape[0]-1: ax[i,j].set_xlabel('Predicted p(pathology)') if j==0: ax[i,j].set_ylabel('Cumulative fraction') ax[i,j].set_xlim(0,1) ax[i,j].set_ylim(0,1) plot_roc_curves({key:Y_cv[key][ix] for key in Y_cv}, {key:p[key][ix] for key in p}, ax=ax[i,3],label='sparse') fig.tight_layout()
def plot_roc_curves(Y,p,ax=None,label='full',title='AUC',color=None): if ax is None: fig,ax = plt.subplots(1,1) colors = {'basic':'gray','total':'pink','all':'red'} aucs = {} for key in p: fpr,tpr,auc = get_roc_curve(Y[key],p[key]) if color is None: color = 'red' if key not in colors else colors[key] ax.plot(fpr, tpr, lw=2, color=color, label={'full':'AUC using\n%s = %0.2f' % (key,auc), 'sparse':'%s = %.2f' % (title,auc)}[label]) aucs[key] = auc ax.set_xlim(-0.01,1.01) ax.set_ylim(-0.01,1.01) ax.set_xlabel('False Positive Rate')#, fontsize='large', fontweight='bold') ax.set_ylabel('True Positive Rate')#, fontsize='large', fontweight='bold') ax.set_title(title)#, fontsize='large', fontweight='bold') # Set the tick labels font for label in (ax.get_xticklabels() + ax.get_yticklabels()): pass #label.set_fontsize('large') #label.set_fontweight('bold') ax.legend(loc="lower right") return aucs
def plot_just_rocs(Y_clean,ps,ys,imps): fig,axes = plt.subplots(4,4,sharex=True,sharey=True,figsize=(10,10)) y = {} p = {} for i in range(len(list(Y_clean))): ax = axes.flat[i] for imp in imps: pimp = ps[imp][i,:,:].ravel() # Unravel all predictions of test data over all cv splits. yimp = ys[imp][i,:,:].ravel() # Unravel all test data ground truth over all cv splits. pimp = pimp[np.isnan(yimp)==0] # Remove NaNs (no ground truth) yimp = yimp[np.isnan(yimp)==0] # Remove NaNs (no ground truth) p[imp] = pimp[yimp.mask==False]#.compressed() y[imp] = yimp[yimp.mask==False]#.compressed() plot_roc_curves(y,p,ax=ax) # Plot on the given axes. ax.set_title(list(Y_clean)[i]) # Set the title. for i in range(i+1,len(axes.flat)): ax = axes.flat[i] ax.set_axis_off() plt.tight_layout()
def plot_just_rocs(props,ps,ys,imps,plot=True,axes=None,m=None,n=None,color='k',title='AUC'): if plot: if axes is None: if m is None: m = max(1,1+int(len(props)/4)) if n is None: n = min(4,len(props)) fig,axes = plt.subplots(m,n,sharex=True,sharey=True, squeeze=False,figsize=(m*3.5,n*3.5)) y = {} p = {} aucs = {} for i,prop in enumerate(props): if plot: ax = axes.flat[i] for imp in imps: pimp = ps[imp][i,:,:].ravel() # Unravel all predictions of test data over all cv splits. yimp = ys[imp][i,:,:].ravel() # Unravel all test data ground truth over all cv splits. pimp = pimp[np.isnan(yimp)==0] # Remove NaNs (no ground truth) yimp = yimp[np.isnan(yimp)==0] # Remove NaNs (no ground truth) p[imp] = pimp[yimp.mask==False]#.compressed() y[imp] = yimp[yimp.mask==False]#.compressed() if plot: aucs[prop] = plot_roc_curves(y,p,ax=ax,label='sparse',title=title,color=color) # Plot on the given axes. else: aucsi = {} for imp in p: fpr,tpr,auc = get_roc_curve(y[imp],p[imp]) aucsi[imp] = auc aucs[prop] = aucsi if plot: ax.set_title(props[i].replace('Clinpath ','').replace('Nos','NOS')) # Set the title. if plot: for i in range(i+1,len(axes.flat)): ax = axes.flat[i] ax.set_axis_off() plt.tight_layout() return aucs,axes