;-------------------------------------------------------------------- ; make balanced accuracy ; ; using the cloud phase which is defined as ; 0 = clear ; 1 = water ; 2 = ice ; ; ref: https://ml-cheatsheet.readthedocs.io/en/latest/loss_functions.html ;------------------------------------------------------------------------ pro make_acc_metrics, phase_true, prob_cloud, acc,bacc,wacc,bwacc,bce, tpr, tnr, fpr, fnr missing = -999.0 ;--- make input binary (0/1) masks p = prob_cloud t = phase_true ;--- make a binary probability pbin = p idx = where(p ge 0.5,cc) & if (cc gt 0) then pbin[idx] = 1.0 idx = where(p ne missing and p lt 0.5,cc) & if (cc gt 0) then pbin[idx] = 0.0 ;--- make truth be 0 or 1 (phase is actuall 0,1,2) idx = where(t ne missing and t le 0.5,cc) & if (cc gt 0) then t[idx] = 0.0 idx = where(t ge 0.5,cc) & if (cc gt 0) then t[idx] = 1.0 idx = where(p ge 0.0 and p le 1.0 and t ge 0.0 and t le 1.0,cc) if (cc gt 0) then begin p = p[idx] pbin = pbin[idx] t = t[idx] ;--- true positive idx = where(pbin eq 1.0 and t eq 1.0,tp) idx = where(t eq 1.0,pp) tpr = float(tp) / float(pp) ;--- true negative idx = where(pbin eq 0.0 and t eq 0.0,tn) idx = where(t eq 0.0,nn) tnr = float(tn) / float(nn) ;-- false positive idx = where(pbin eq 1.0 and t eq 0.0,fp) fpr = float(fp) / float(nn) ;-- false negative idx = where(pbin eq 0.0 and t eq 1.0,fn) fnr = float(fn) / float(pp) acc = float(tp + tn) / float(pp + nn) bacc = float(tpr + tnr) / 2.0 wacc = 1.0 - mean((1.0-t)*p + t*(1.0-p)) bwacc = 1.0 - (mean((1.0-t)*p)*float(nn) + mean(t*(1.0-p))*float(pp)) / (float(nn) + float(pp)) ;--- logs cant handle <= 0 or >= 1 idx = where(p le 0.00,cc) & if (cc gt 0) then p[idx] = 0.00001 idx = where(p ge 1.00,cc) & if (cc gt 0) then p[idx] = 0.99999 bce = mean(-1.0*t*alog(p) - (1.0-t)*alog(1.0-p)) bce = -1.0 * bce ; do this so more positive is good like all other metrics endif else begin acc = missing bacc = missing wacc = missing bwacc = missing bce = missing tpr = missing tnr = missing fpr = missing fnr = missing endelse end