| 1 | #!perl -w | ||
| 2 | #LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL | ||
| 3 | # | ||
| 4 | # Graphical User Interface for drawing and printing | ||
| 5 | # | ||
| 6 | # ROC curves with nonparametric confidence bounds | ||
| 7 | # | ||
| 8 | # | ||
| 9 | # | ||
| 10 | # | ||
| 11 | # copyright 1998-2007 by Hans A. Kestler | ||
| 12 | # | ||
| 13 | # | ||
| 14 | # Locations of Perl and modules have to be adapted to local configurations. | ||
| 15 | # | ||
| 16 | #LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL | ||
| 17 | |||
| 18 | # change paths if needed (probably) | ||
| 19 | #use lib '/opt/perl5/lib/site_perl/5.005/PA-RISC2.0/Tk'; | ||
| 20 | #use lib '/home/kestler/PL/ROC.core/'; | ||
| 21 | #use lib '/Applications/perl2exe/perl5/lib/site_perl/5.8.8/Tk'; | ||
| 22 | |||
| 23 | #LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL | ||
| 24 | use Statistics::ROC; | ||
| 25 | |||
| 26 | use Carp; | ||
| 27 | use strict; | ||
| 28 | use Cwd; | ||
| 29 | use Cwd 'chdir'; | ||
| 30 | use File::Basename; | ||
| 31 | |||
| 32 | use Tk; | ||
| 33 | require Tk::Dialog; | ||
| 34 | |||
| 35 | #use Tk::DialogBox; | ||
| 36 | #use Tk::DummyEncode; | ||
| 37 | #use Tk::Canvas; | ||
| 38 | #use utf8; | ||
| 39 | #use Tk::Menubutton; | ||
| 40 | #use Tk::Scale; | ||
| 41 | #use Tk::Optionmenu; | ||
| 42 | #use Tk::Bitmap; | ||
| 43 | #use Tk::FBox; | ||
| 44 | #LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL | ||
| 45 | |||
| 46 | ##################### global variables ################ | ||
| 47 | use vars qw/$VERSION $undef $loadfirst $fname $DIALOG_ABOUT $DIALOG_USAGE | ||
| 48 | $DIALOG_LOAD_ERROR $WAIT_BOX/; | ||
| 49 | |||
| 50 | |||
| 51 | $,=" "; | ||
| 52 | $VERSION='0.04'; | ||
| 53 | $undef=0; | ||
| 54 | $loadfirst=0; | ||
| 55 | $fname=""; | ||
| 56 | |||
| 57 | |||
| 58 | ########################################################################### | ||
| 59 | ########################################################################### | ||
| 60 | ###### | ||
| 61 | ###### Graphical User Interface | ||
| 62 | ###### | ||
| 63 | ########################################################################### | ||
| 64 | ########################################################################### | ||
| 65 | |||
| 66 | # predeclare subroutines | ||
| 67 | sub make_menubutton; | ||
| 68 | sub roc_save; | ||
| 69 | sub fileSelector; | ||
| 70 | sub draw_roc; | ||
| 71 | sub initialize_messages; | ||
| 72 | |||
| 73 | |||
| 74 | sub make_menubutton { | ||
| 75 | # This function is courtesy of Steve Lidie, The Perl Journal, vol 1, no 1, | ||
| 76 | # 1996. | ||
| 77 | # | ||
| 78 | # Make a Menubutton widget; note that the Menu is automatically created. | ||
| 79 | # If the label is '', make a separator. | ||
| 80 | |||
| 81 | my($mbf, $mb_label, $mb_label_underline, $pack, $mb_list_ref) = @_; | ||
| 82 | |||
| 83 | my $mb = $mbf->Menubutton( | ||
| 84 | -text => $mb_label, | ||
| 85 | -underline => $mb_label_underline, | ||
| 86 | ); | ||
| 87 | my $mb_list; | ||
| 88 | foreach $mb_list (@{$mb_list_ref}) { | ||
| 89 | $mb_list->[0] eq '' ? $mb->separator : | ||
| 90 | $mb->command( | ||
| 91 | -label => $mb_list->[0], | ||
| 92 | -command => $mb_list->[1], | ||
| 93 | -underline => $mb_list->[2], | ||
| 94 | ); | ||
| 95 | } | ||
| 96 | $mb->pack(-side => $pack); | ||
| 97 | return $mb; | ||
| 98 | } # end make_menubutton | ||
| 99 | |||
| 100 | |||
| 101 | |||
| 102 | |||
| 103 | sub roc_save{ | ||
| 104 | # Saves or prints the ROC curve (canvas widget, drawing area) | ||
| 105 | # | ||
| 106 | # If the value of the hash is a string with "lpr" at the beginning | ||
| 107 | # the drawing area will be piped to the postscript printer otherwise | ||
| 108 | # it will be saved as a postscript file. | ||
| 109 | # Arguments: * handle to the canvas widget | ||
| 110 | # * value of entry field (string) | ||
| 111 | |||
| 112 | my($w, $pinfo) = @_; | ||
| 113 | my($a,$psfname); | ||
| 114 | |||
| 115 | $a = $w->postscript; | ||
| 116 | |||
| 117 | if($pinfo->{'prcmd'}=~/^lpr/){ | ||
| 118 | open(LPR, "| $pinfo->{'prcmd'}"); | ||
| 119 | #print "yes lpr \n"; | ||
| 120 | } | ||
| 121 | else{ | ||
| 122 | #print "no lpr file\n"; | ||
| 123 | #print dirname($fname)."/".$pinfo->{'prcmd'},"\n"; | ||
| 124 | #open(LPR, ">$pinfo->{'prcmd'}"); | ||
| 125 | $psfname = dirname($fname)."/".$pinfo->{'prcmd'}; | ||
| 126 | open(LPR, ">$psfname"); | ||
| 127 | } | ||
| 128 | print LPR $a;close(LPR); | ||
| 129 | } # end roc_save | ||
| 130 | |||
| 131 | |||
| 132 | sub fileSelector{ | ||
| 133 | # File selection widget. | ||
| 134 | # | ||
| 135 | # Selects and loads a datafile and draws the ROC curve with | ||
| 136 | # default values. Makes checks on data. | ||
| 137 | # Lines beginning with $ and # are treated as commentaries. | ||
| 138 | # Uses the Tk::getOpenFile widget for file selection. | ||
| 139 | # | ||
| 140 | # Arguments: * handle of the main window | ||
| 141 | # * handle of the canvas widget | ||
| 142 | # * (0,0)- and (1,1)-points in pixels | ||
| 143 | # of the drawing area (this is the complete drawing area). | ||
| 144 | # It is assumed that y-coordinates increase from top | ||
| 145 | # to bottom of the widget, the x-coord. increase as expected | ||
| 146 | # from left to right. | ||
| 147 | # * the model type, this is a reference to string | ||
| 148 | # * the 2-sided confidence interval in % | ||
| 149 | # * the reference to the data (list-of-list) | ||
| 150 | |||
| 151 | my $but=shift; | ||
| 152 | my($MW,$c,$xzero,$yzero,$xone,$yone,$model_type_ref, | ||
| 153 | $conf_ref,$var_grp_ref) = @$but; | ||
| 154 | my($Horiz) = 1; | ||
| 155 | # my $fname; | ||
| 156 | my $dir=cwd(); | ||
| 157 | my @line=(); | ||
| 158 | |||
| 159 | @$$var_grp_ref=(); # reinitialize data array | ||
| 160 | |||
| 161 | |||
| 162 | |||
| 163 | |||
| 164 | $fname = $MW->getOpenFile(-title =>'Select a datafile!', -initialdir =>'.'); # $fname needs to be global for draw_roc | ||
| 165 | |||
| 166 | |||
| 167 | |||
| 168 | return if !defined($fname); # check if filename is valid | ||
| 169 | |||
| 170 | $main::name=fileparse($fname); | ||
| 171 | $MW->title("ROC with confidence: $main::name"); | ||
| 172 | |||
| 173 | |||
| 174 | |||
| 175 | # open file and read in data | ||
| 176 | open(DATA, "$fname"); | ||
| 177 | LINE: | ||
| 178 | while(<DATA>){ | ||
| 179 | next LINE if /^#/ || /^$/; | ||
| 180 | @line=split; | ||
| 181 | if(($line[1] != 1 && $line[1] != 0) || | ||
| 182 | $line[0] !~ /^(\+|-)?(\d+(\.\d*)?|\.\d+)(E|e)?(\+|-)?\d*$/) | ||
| 183 | { | ||
| 184 | $DIALOG_LOAD_ERROR->Show; return; | ||
| 185 | } | ||
| 186 | push @$$var_grp_ref, [ @line ]; | ||
| 187 | } | ||
| 188 | |||
| 189 | # check for not existing data | ||
| 190 | if(!scalar(@$$var_grp_ref)){$DIALOG_LOAD_ERROR->Show; return;} | ||
| 191 | |||
| 192 | # set loadfirst flag to indicate that data is loaded | ||
| 193 | $loadfirst=1; | ||
| 194 | |||
| 195 | draw_roc([$c,$xzero,$yzero,$xone,$yone,$model_type_ref,$conf_ref,$var_grp_ref]); | ||
| 196 | |||
| 197 | } # end of fileSelector | ||
| 198 | |||
| 199 | |||
| 200 | sub draw_roc{ | ||
| 201 | # Draws the receiver-operator characteristic curve with confidence bounds. | ||
| 202 | # | ||
| 203 | # Arguments: * handle of the canvas widget | ||
| 204 | # * (0,0)- and (1,1)-points in pixels | ||
| 205 | # of the drawing area (this is the complete drawing area). | ||
| 206 | # It is assumed that y-coordinates increase from top | ||
| 207 | # to bottom of the widget, the x-coord. increases as expected | ||
| 208 | # from left to right. | ||
| 209 | # * the model type, this is a reference to string | ||
| 210 | # * the 2-sided confidence interval in % | ||
| 211 | # * the reference to the data (list-of-list) | ||
| 212 | |||
| 213 | my $but=shift; | ||
| 214 | my ($c,$xzero,$yzero,$xone,$yone,$model_type,$conf,$var_grp)=@$but; | ||
| 215 | my $m_type; | ||
| 216 | |||
| 217 | #print $$model_type,"\n";print $$conf,"\n"; | ||
| 218 | #print ref($$model_type),"\n";print ref($$conf),"\n"; | ||
| 219 | |||
| 220 | |||
| 221 | # check for not existing data | ||
| 222 | if(!$loadfirst){$DIALOG_LOAD_ERROR->Show; return;} | ||
| 223 | |||
| 224 | ($main::state_b,$main::state_o,$main::state_p,$main::state_r,$main::state_v)= | ||
| 225 | (1,1,1,1,1); # reset checkbuttons | ||
| 226 | |||
| 227 | |||
| 228 | |||
| 229 | if($$model_type eq 'grp0 > grp1'){$m_type='decrease'} | ||
| 230 | elsif($$model_type eq 'grp0 < grp1'){$m_type='increase'} | ||
| 231 | else{ croak "Wrong model type in userinterface\n";} | ||
| 232 | |||
| 233 | |||
| 234 | my @ROC=roc($m_type,$$conf/100,@$$var_grp); | ||
| 235 | my $label; | ||
| 236 | |||
| 237 | for(my $j=0,my $width;$j<3;$j++) | ||
| 238 | { | ||
| 239 | if($j==1){$width=2; $label='plot';}else{$width=1;$label='bounds';} # set ROC line width to 2 | ||
| 240 | for(my $i=0;$i<@{$ROC[0]}-1;$i++) # step thru (x,y)-pairs | ||
| 241 | { | ||
| 242 | $c->create('line', | ||
| 243 | ($xone-$xzero)*$ROC[$j][$i][0]+$xzero, | ||
| 244 | ($yone-$yzero)*$ROC[$j][$i][1]+$yzero, | ||
| 245 | ($xone-$xzero)*$ROC[$j][$i+1][0]+$xzero, | ||
| 246 | ($yone-$yzero)*$ROC[$j][$i+1][1]+$yzero, | ||
| 247 | -fill=>'red', -tags=>[$label], | ||
| 248 | -width=>$width); | ||
| 249 | } | ||
| 250 | } | ||
| 251 | |||
| 252 | |||
| 253 | my ($i,$tmp); | ||
| 254 | |||
| 255 | |||
| 256 | |||
| 257 | # calculate optimal cutoff value of empirical ROC curve | ||
| 258 | my($rp,$fp,$rn,$fn)=(0,0,0,0); | ||
| 259 | my($acc,$sensi,$speci,$ppv,$npv); | ||
| 260 | my @ss2=(); | ||
| 261 | my $g; | ||
| 262 | my $gmax; | ||
| 263 | my ($text1,$text2); | ||
| 264 | |||
| 265 | for( my $j=0;$j<@$$var_grp;$j++) | ||
| 266 | { | ||
| 267 | $g = $$$var_grp[$j][0]; | ||
| 268 | #print $g, "\n"; | ||
| 269 | ($rp,$fp,$rn,$fn)=(0,0,0,0); | ||
| 270 | for($i=0;$i<@$$var_grp;$i++) | ||
| 271 | { | ||
| 272 | if($m_type eq 'increase') | ||
| 273 | { | ||
| 274 | if($$$var_grp[$i][0] > $g) | ||
| 275 | { | ||
| 276 | if($$$var_grp[$i][1] == 1){$rp++;} | ||
| 277 | else {$fp++;} | ||
| 278 | } | ||
| 279 | else | ||
| 280 | { | ||
| 281 | if($$$var_grp[$i][1] == 0){$rn++;} | ||
| 282 | else {$fn++;} | ||
| 283 | } | ||
| 284 | } | ||
| 285 | else # modeltype decrease | ||
| 286 | { | ||
| 287 | if($$$var_grp[$i][0] < $g) | ||
| 288 | { | ||
| 289 | if($$$var_grp[$i][1] == 1){$rp++;} | ||
| 290 | else {$fp++;} | ||
| 291 | } | ||
| 292 | else | ||
| 293 | { | ||
| 294 | if($$$var_grp[$i][1] == 0){$rn++;} | ||
| 295 | else {$fn++;} | ||
| 296 | } | ||
| 297 | } | ||
| 298 | } | ||
| 299 | #print "$rp $rn $fn $fp \n"; | ||
| 300 | # acc sensi speci ppv npv | ||
| 301 | if(!eval{$acc = ($rn + $rp) / ($rn + $rp + $fn + $fp);}) {$acc=$undef;} | ||
| 302 | if(!eval{$sensi = $rp / ($fn + $rp);}) {$sensi=$undef;} | ||
| 303 | if(!eval{$speci = $rn / ($fp + $rn);}) {$speci=$undef;} | ||
| 304 | if(!eval{$ppv = $rp / ($fp + $rp);}) {$ppv=$undef;} | ||
| 305 | if(!eval{$npv = $rn / ($fn + $rn);}) {$npv=$undef;} | ||
| 306 | #print "Threshold: $g \n"; | ||
| 307 | #print "$acc, $sensi, $speci, $ppv, $npv \n"; | ||
| 308 | #print "---------------\n"; | ||
| 309 | push @ss2, [$acc, $sensi, $speci, $ppv, $npv]; | ||
| 310 | } | ||
| 311 | |||
| 312 | for($i=0,$tmp=$ss2[0][1]+$ss2[0][2],$gmax=$tmp;$i<@ss2;$i++) | ||
| 313 | { | ||
| 314 | if($ss2[$i][1]+$ss2[$i][2]>$tmp) {$tmp=$ss2[$i][1]+$ss2[$i][2]; $gmax=$i;} | ||
| 315 | } | ||
| 316 | |||
| 317 | $c->createText($xzero+300, $yone-20, | ||
| 318 | -text=>"Threshold: $$$var_grp[$gmax][0]", | ||
| 319 | -tags=>['values']); | ||
| 320 | |||
| 321 | |||
| 322 | # rounding routine because 0.8125 rounds falsely with %.3 to 0.812 | ||
| 323 | sub round {$_[0] >0 ? int $_[0]+0.5 : int $_[0]-0.5} | ||
| 324 | $text1= sprintf "ACC SENSI SPECI PPV NPV\n%.3f %.3f %.3f %.3f %.3f", | ||
| 325 | map {round(int($_ *10000)/10)/1000} @{$ss2[$gmax]}; | ||
| 326 | $c->createText($xzero+100, $yone-20, | ||
| 327 | -text=>$text1, | ||
| 328 | -tags=>['values']); | ||
| 329 | |||
| 330 | $c->createLine(($xone-$xzero)*(1-$ss2[$gmax][2])+$xzero, | ||
| 331 | $yzero, | ||
| 332 | ($xone-$xzero)*(1-$ss2[$gmax][2])+$xzero, | ||
| 333 | ($yone-$yzero)*$ss2[$gmax][1]+$yzero, | ||
| 334 | -fill=>'blue', -tags=>['opt'], | ||
| 335 | -width=>1); | ||
| 336 | |||
| 337 | $c->createLine($xzero, | ||
| 338 | ($yone-$yzero)*$ss2[$gmax][1]+$yzero, | ||
| 339 | ($xone-$xzero)*(1-$ss2[$gmax][2])+$xzero, | ||
| 340 | ($yone-$yzero)*$ss2[$gmax][1]+$yzero, | ||
| 341 | -fill=>'blue', -tags=>['opt'], | ||
| 342 | -width=>1); | ||
| 343 | |||
| 344 | $c->createOval(($xone-$xzero)*(1-$ss2[$gmax][2])+$xzero-6, | ||
| 345 | ($yone-$yzero)*$ss2[$gmax][1]+$yzero-6, | ||
| 346 | ($xone-$xzero)*(1-$ss2[$gmax][2])+$xzero+6, | ||
| 347 | ($yone-$yzero)*$ss2[$gmax][1]+$yzero+6, | ||
| 348 | -width=>1,-fill=>'blue',-tags=>['opt']); | ||
| 349 | |||
| 350 | $text2= sprintf "%1.3f\n%1.3f", 1-$ss2[$gmax][2], $ss2[$gmax][1]; | ||
| 351 | $c->createText(($xone-$xzero)*(1-$ss2[$gmax][2])+$xzero, | ||
| 352 | ($yone-$yzero)*$ss2[$gmax][1]+$yzero-30, | ||
| 353 | -text=>$text2, | ||
| 354 | -tags=>['opt','values']); | ||
| 355 | |||
| 356 | |||
| 357 | |||
| 358 | |||
| 359 | my @s=sort{(1-$ss2[$a][2]) <=> (1-$ss2[$b][2]) || $ss2[$a][1] <=> $ss2[$b][1]} 0..$#ss2; | ||
| 360 | #print " $#ss2 $#s \n"; | ||
| 361 | @ss2=@ss2[@s]; | ||
| 362 | #for($i=0;$i<@ss2;$i++){print 1-$ss2[$i][2], $ss2[$i][1] ,"\n";} | ||
| 363 | |||
| 364 | for($i=0;$i<$#ss2;$i++) # step thru (x,y)-pairs | ||
| 365 | { | ||
| 366 | $c->createLine( | ||
| 367 | ($xone-$xzero)*(1-$ss2[$i][2])+$xzero, | ||
| 368 | ($yone-$yzero)*$ss2[$i][1]+$yzero, | ||
| 369 | ($xone-$xzero)*(1-$ss2[$i+1][2])+$xzero, | ||
| 370 | ($yone-$yzero)*$ss2[$i+1][1]+$yzero, | ||
| 371 | -fill=>'green4', -tags=>['realroc'], | ||
| 372 | -width=>2); | ||
| 373 | } | ||
| 374 | |||
| 375 | |||
| 376 | # save ROC curves to file | ||
| 377 | if($main::state_s) | ||
| 378 | { | ||
| 379 | #$c->messageBox(-icon => 'error', -message => 'Cannot create roc-file.'); | ||
| 380 | unless (open CURVES, ">$fname.txt") {die "Cannot create roc-file: $!";} | ||
| 381 | print CURVES "$fname.txt\n"; # print filename | ||
| 382 | print CURVES "Modeltype: $$model_type, 2-sided confidence interval: ",$$conf/100,"\n"; | ||
| 383 | print CURVES "Threshold: $$$var_grp[$gmax][0] \n"; | ||
| 384 | print CURVES "$text1\n"; | ||
| 385 | print CURVES "#######\n"; | ||
| 386 | print CURVES "Empirical ROC curve (x,y):\n"; | ||
| 387 | for($i=0;$i<$#ss2+1;$i++) # step thru (x,y)-pairs | ||
| 388 | { | ||
| 389 | print CURVES (1-$ss2[$i][2])," ",$ss2[$i][1],"\n"; | ||
| 390 | } | ||
| 391 | print CURVES "#######\n"; | ||
| 392 | print CURVES "Estimated ROC curve (x,y):\n"; | ||
| 393 | for(my $i=0;$i<@{$ROC[0]};$i++) # step thru (x,y)-pairs | ||
| 394 | { | ||
| 395 | print CURVES $ROC[1][$i][0]," ",$ROC[1][$i][1],"\n"; | ||
| 396 | } | ||
| 397 | print CURVES "#######\n"; | ||
| 398 | print CURVES "Upper bound to estimated ROC curve (x,y):\n"; | ||
| 399 | for(my $i=0;$i<@{$ROC[0]};$i++) # step thru (x,y)-pairs | ||
| 400 | { | ||
| 401 | print CURVES $ROC[0][$i][0]," ",$ROC[0][$i][1],"\n"; | ||
| 402 | } | ||
| 403 | print CURVES "#######\n"; | ||
| 404 | print CURVES "Lower bound to estimated ROC curve (x,y):\n"; | ||
| 405 | for(my $i=0;$i<@{$ROC[0]};$i++) # step thru (x,y)-pairs | ||
| 406 | { | ||
| 407 | print CURVES $ROC[2][$i][0]," ",$ROC[2][$i][1],"\n"; | ||
| 408 | } | ||
| 409 | close(CURVES); | ||
| 410 | } | ||
| 411 | |||
| 412 | } # end draw_roc | ||
| 413 | |||
| 414 | |||
| 415 | sub initialize_messages{ | ||
| 416 | |||
| 417 | my $MW=shift; | ||
| 418 | # Create all application Dialog objects. | ||
| 419 | $DIALOG_LOAD_ERROR=$MW->Dialog(-title => 'ERRROR',-text => | ||
| 420 | "The data has NOT been loaded or is NOT in the right format! | ||
| 421 | The datafile has to have the following structure with one | ||
| 422 | sample per row: \n <value> <class:0/1>", | ||
| 423 | -bitmap => 'info',-wraplength => '3i', | ||
| 424 | -buttons => ['Dismiss'], | ||
| 425 | #-font => 'Arialbold' | ||
| 426 | ); | ||
| 427 | $DIALOG_ABOUT = $MW->Dialog( | ||
| 428 | -title => 'About', | ||
| 429 | -text => | ||
| 430 | "ROC with confidence $VERSION \n\nApril 26. 2007\n\n | ||
| 431 | This program calculates receiver-operator characteristic | ||
| 432 | curves with nonparametric confidence bounds from data | ||
| 433 | separated into two groups.\n | ||
| 434 | Author: Hans A. Kestler, h.kestler\@ieee.org | ||
| 435 | hans.kestler\@uni-ulm.de | ||
| 436 | Copyright (c) 1998-2007 by Hans Kestler. All rights reserved. | ||
| 437 | This program is free software; it may be redistributed and/or | ||
| 438 | modified under the same terms as Perl itself.", | ||
| 439 | -bitmap => 'info',-wraplength => '6i', | ||
| 440 | -buttons => ['Dismiss'], | ||
| 441 | ); | ||
| 442 | #$DIALOG_ABOUT->configure(-wraplength => '6i'); | ||
| 443 | $DIALOG_USAGE = $MW->Dialog( | ||
| 444 | -title => 'Usage', | ||
| 445 | -buttons => ['Dismiss'], | ||
| 446 | ); | ||
| 447 | $DIALOG_USAGE->Subwidget('message')->configure( | ||
| 448 | -wraplength => '5i', | ||
| 449 | -text => | ||
| 450 | "This program calculates and displays ROC curves | ||
| 451 | with confidence bounds. These bounds are | ||
| 452 | calculated nonparametrically.\n | ||
| 453 | The inputfile from which the ROC curve is | ||
| 454 | determined may be loaded with the LOAD button | ||
| 455 | in the FILE menu. It has to have the following | ||
| 456 | structure with one sample per row: | ||
| 457 | <value> <class:0/1>.\n | ||
| 458 | The model assumption may be selected below the | ||
| 459 | drawing area. The confidence limits are set | ||
| 460 | with the scales. The curve won't be redrawn | ||
| 461 | after changing this interval. Either the model | ||
| 462 | has to be reselected or the BOUNDS ON/OFF | ||
| 463 | button in the OPTIONS menu has to be toggled | ||
| 464 | to redraw the curve.\n | ||
| 465 | The ROC curve may be saved or printed by selecting | ||
| 466 | the <Print/Save as Postscript> button below the | ||
| 467 | canvas. If the entry field just above this | ||
| 468 | button is set to <lpr> the curve will be sent to | ||
| 469 | the printer otherwise it will saved in the file | ||
| 470 | specified (so don't use a filename with the | ||
| 471 | string lpr at the begining).\n | ||
| 472 | The <Options> menu gives some restricted | ||
| 473 | possibilities of changing the appearance of the | ||
| 474 | graph. The <Optimium> is calculated by maximizing | ||
| 475 | simultaneously the sensitivity and specificity. | ||
| 476 | The performance values displayed above the canvas | ||
| 477 | give the optimal dicriminative ability of the | ||
| 478 | threshold value maximizing sensitivity and | ||
| 479 | specificity. If the model is grp0 < grp1 the | ||
| 480 | threshold value belongs to grp0, i.e. | ||
| 481 | grp1 if value > threshold."); | ||
| 482 | |||
| 483 | |||
| 484 | } # end initialize_messages | ||
| 485 | |||
| 486 | |||
| 487 | sub draw_grid{ | ||
| 488 | # Draws a grid inside the canvas widget | ||
| 489 | # | ||
| 490 | # The available space is evenly divided into 10x10 | ||
| 491 | # rectangels. | ||
| 492 | # Arguments: * handle of the canvas widget | ||
| 493 | # * (0,0)- and (1,1)-points in pixles | ||
| 494 | # of the drawing area (this is the complete drawing area) | ||
| 495 | # It is assumed that y-coordinates increase from top | ||
| 496 | # to bottom of the widget, the x-coord. increase as expected | ||
| 497 | # from left to right. | ||
| 498 | |||
| 499 | my ($c,$xzero,$yzero,$xone,$yone)=@{shift()}; | ||
| 500 | |||
| 501 | for(my $i=0,my $inc=($xone-$xzero)/10;$i<=10;$i++){ | ||
| 502 | $c->createLine($xzero+$i*$inc,$yzero+4, | ||
| 503 | $xzero+$i*$inc,$yone,-width=>1,-tags=>['grid']); | ||
| 504 | } | ||
| 505 | for(my $i=0,my $inc=($yone-$yzero)/10;$i<=10;$i++){ | ||
| 506 | $c->createLine($xzero-4,$yzero+$i*$inc, | ||
| 507 | $xone,$yzero+$i*$inc,-width=>1,-tags=>['grid']); | ||
| 508 | } | ||
| 509 | } # end draw_grid | ||
| 510 | |||
| 511 | |||
| 512 | sub draw_small_ticks{ | ||
| 513 | # Draws small ticks. | ||
| 514 | # | ||
| 515 | # Draws 100 small ticks on the x- and y- axis. | ||
| 516 | # Arguments: * handle of the canvas widget | ||
| 517 | # * (0,0)- and (1,1)-points in pixles | ||
| 518 | # of the drawing area (this is the complete drawing area) | ||
| 519 | # It is assumed that y-coordinates increase from top | ||
| 520 | # to bottom of the widget, the x-coord. increase as expected | ||
| 521 | # from left to right. | ||
| 522 | |||
| 523 | my ($c,$xzero,$yzero,$xone,$yone)=@{shift()}; | ||
| 524 | |||
| 525 | for(my $i=0,my $inc=($xone-$xzero)/100;$i<=100;$i++){ | ||
| 526 | $c->createLine($xzero+$i*$inc,$yzero+3, | ||
| 527 | $xzero+$i*$inc,$yzero,-width=>.5); | ||
| 528 | } | ||
| 529 | for(my $i=0,my $inc=($yone-$yzero)/100;$i<=100;$i++){ | ||
| 530 | $c->createLine($xzero-3,$yzero+$i*$inc, | ||
| 531 | $xzero,$yzero+$i*$inc,-width=>.5); | ||
| 532 | } | ||
| 533 | } # end draw_small_ticks | ||
| 534 | |||
| 535 | |||
| 536 | sub draw_numbers{ | ||
| 537 | # Draws the numbers {0, 0.1,..., 0.9, 1.0} the x- and y-axis. | ||
| 538 | # | ||
| 539 | # Arguments: * handle of the canvas widget | ||
| 540 | # * (0,0)- and (1,1)-points in pixles | ||
| 541 | # of the drawing area (this is the complete drawing area) | ||
| 542 | # It is assumed that y-coordinates increase from top | ||
| 543 | # to bottom of the widget, the x-coord. increase as expected | ||
| 544 | # from left to right. | ||
| 545 | |||
| 546 | my ($c,$xzero,$yzero,$xone,$yone)=@{shift()}; | ||
| 547 | |||
| 548 | for(my $i=0,my $inc=($xone-$xzero)/10;$i<=10;$i+=2){ | ||
| 549 | $c->create('text',$xzero+$i*$inc,$yzero+4+10, | ||
| 550 | -text=>$i/10); | ||
| 551 | } | ||
| 552 | for(my $i=0,my $inc=($yone-$yzero)/10;$i<=10;$i+=2){ | ||
| 553 | $c->create('text',$xzero-4-10,$yzero+$i*$inc, | ||
| 554 | -text=>$i/10); | ||
| 555 | } | ||
| 556 | } # end draw_numbers | ||
| 557 | |||
| 558 | |||
| 559 | sub draw_diagonal{ | ||
| 560 | # Draws a diagonal from (0,0) to (1,1). | ||
| 561 | # | ||
| 562 | # Arguments: * handle of the canvas widget | ||
| 563 | # * (0,0)- and (1,1)-points in pixles | ||
| 564 | # of the drawing area (this is the complete drawing area) | ||
| 565 | # It is assumed that y-coordinates increase from top | ||
| 566 | # to bottom of the widget, the x-coord. increase as expected | ||
| 567 | # from left to right. | ||
| 568 | |||
| 569 | my ($c,$xzero,$yzero,$xone,$yone)=@{shift()}; | ||
| 570 | |||
| 571 | $c->createLine($xzero,$yzero,$xone,$yone,-width=>1, -tags=>['diag']); | ||
| 572 | |||
| 573 | } # end draw_diagonal | ||
| 574 | |||
| 575 | |||
| 576 | |||
| 577 | |||
| 578 | |||
| 579 | my @var_grp=(); | ||
| 580 | my $var_grp_ref=\@var_grp; | ||
| 581 | my $MW = new MainWindow; | ||
| 582 | $MW->title("ROC with confidence"); | ||
| 583 | my $MBF=$MW->Frame(-relief=>'raised',-borderwidth=>1)->pack(-fill=>'x'); | ||
| 584 | |||
| 585 | |||
| 586 | my ($xsize,$ysize)=(600,580); | ||
| 587 | my $area=500; # actually the length of the quadratic area | ||
| 588 | |||
| 589 | # derived values | ||
| 590 | my ($xzero,$yzero)=(($xsize-$area)/2,$ysize-($ysize-$area)/2); | ||
| 591 | my ($xone,$yone)=($xsize-($xsize-$area)/2,($ysize-$area)/2); | ||
| 592 | my @points=($xzero,$yzero,$xone,$yone); | ||
| 593 | my $model_type ='grp0 < grp1'; | ||
| 594 | my $conf=95; | ||
| 595 | $main::name=""; | ||
| 596 | |||
| 597 | |||
| 598 | my $c=$MW->Canvas(-width=>$xsize,-height=>$ysize)->pack; | ||
| 599 | $c->create('text',($xone-$xzero)/2+$xzero,$yzero+4+10+12,-text=>"1-SPECIFICITY"); | ||
| 600 | #my (@i)=qw/S e n s i t i v i t y/; | ||
| 601 | for(my $i=0,my (@i)=qw/S E N S I T I V I T Y/;$i<@i;$i++){ | ||
| 602 | $c->create('text',$xzero-36, ($yone-$yzero)/2+$yzero+$i*14-50,-text=>"$i[$i]");} | ||
| 603 | |||
| 604 | initialize_messages($MW); | ||
| 605 | draw_grid([$c,@points]); | ||
| 606 | draw_numbers([$c,@points]); | ||
| 607 | draw_small_ticks([$c,@points]); | ||
| 608 | draw_diagonal([$c,@points]); | ||
| 609 | |||
| 610 | |||
| 611 | |||
| 612 | ###### File Menu Button ###### | ||
| 613 | make_menubutton($MBF,'File',0,'left',[ | ||
| 614 | ['Load',[\&fileSelector,[$MW,$c,@points,\$model_type,\$conf,\$var_grp_ref]],0], | ||
| 615 | ['Quit',\&exit,0] ]); | ||
| 616 | ############################## | ||
| 617 | |||
| 618 | ###### Options Menu Button ###### | ||
| 619 | my $mb_o=$MBF->Menubutton(-text=>'Options',-underline=>0)->pack(-side=>'left'); | ||
| 620 | ($main::state_b,$main::state_g,$main::state_d,$main::state_o,$main::state_p,$main::state_r, | ||
| 621 | $main::state_v,$main::state_s)=(1,1,1,1,1,1,1,0); | ||
| 622 | $mb_o->checkbutton( | ||
| 623 | -label=>'Bounds on/off', | ||
| 624 | -variable=>\$main::state_b, | ||
| 625 | -command=>sub{if(!$main::state_b){$c->delete('bounds')} | ||
| 626 | else{draw_roc([$c,@points,\$model_type,\$conf,\$var_grp_ref]);}} | ||
| 627 | ); | ||
| 628 | $mb_o->checkbutton( | ||
| 629 | -label=>'Grid on/off', | ||
| 630 | -variable=>\$main::state_g, | ||
| 631 | -command=>sub{if(!$main::state_g){$c->delete('grid')} | ||
| 632 | #else{draw_grid([$c,@points]);}} | ||
| 633 | else{draw_grid([$c,@points]); | ||
| 634 | draw_roc([$c,@points,\$model_type,\$conf,\$var_grp_ref]);}} | ||
| 635 | ); | ||
| 636 | $mb_o->checkbutton( | ||
| 637 | -label=>'Diagonal on/off', | ||
| 638 | -variable=>\$main::state_d, | ||
| 639 | -command=>sub{if(!$main::state_d){$c->delete('diag')} | ||
| 640 | else{draw_diagonal([$c,@points]);}} | ||
| 641 | ); | ||
| 642 | $mb_o->checkbutton( | ||
| 643 | -label=>'Optimum on/off', | ||
| 644 | -variable=>\$main::state_o, | ||
| 645 | -command=>sub{if(!$main::state_o){$c->delete('opt')} | ||
| 646 | else{draw_roc([$c,@points,\$model_type,\$conf,\$var_grp_ref]);}} | ||
| 647 | ); | ||
| 648 | $mb_o->checkbutton( | ||
| 649 | -label=>'Estimated ROC on/off', | ||
| 650 | -variable=>\$main::state_p, | ||
| 651 | -command=>sub{if(!$main::state_p){$c->delete('plot')} | ||
| 652 | else{draw_roc([$c,@points,\$model_type,\$conf,\$var_grp_ref]);}} | ||
| 653 | ); | ||
| 654 | $mb_o->checkbutton( | ||
| 655 | -label=>'Empirical ROC on/off', | ||
| 656 | -variable=>\$main::state_r, | ||
| 657 | -command=>sub{if(!$main::state_r){$c->delete('realroc')} | ||
| 658 | else{draw_roc([$c,@points,\$model_type,\$conf,\$var_grp_ref]);}} | ||
| 659 | ); | ||
| 660 | $mb_o->checkbutton( | ||
| 661 | -label=>'Values on/off', | ||
| 662 | -variable=>\$main::state_v, | ||
| 663 | -command=>sub{if(!$main::state_v){$c->delete('values')} | ||
| 664 | else{draw_roc([$c,@points,\$model_type,\$conf,\$var_grp_ref]);}} | ||
| 665 | ); | ||
| 666 | $mb_o->checkbutton( | ||
| 667 | -label=>"Save curve values to file (suffix \".txt\" added) off/on", | ||
| 668 | -variable=>\$main::state_s, | ||
| 669 | -command=>sub{if($main::state_s) | ||
| 670 | {draw_roc([$c,@points,\$model_type,\$conf,\$var_grp_ref]);}} | ||
| 671 | ); | ||
| 672 | # $save_cur->configure(label=>'Save curves values to file: $main::name.dat on/off'); | ||
| 673 | ############################## | ||
| 674 | |||
| 675 | ###### Help Menu Button ###### | ||
| 676 | make_menubutton($MBF, 'Help', 0, 'right', | ||
| 677 | [ | ||
| 678 | ['About', [$DIALOG_ABOUT => 'Show'], 0], | ||
| 679 | ['', undef, 0], | ||
| 680 | ['Usage', [$DIALOG_USAGE => 'Show'], 0], | ||
| 681 | ], | ||
| 682 | ); | ||
| 683 | ############################## | ||
| 684 | |||
| 685 | |||
| 686 | |||
| 687 | # create border of curve | ||
| 688 | $c->create('rectangle', @points , -width=>2); # line width of 2 | ||
| 689 | |||
| 690 | |||
| 691 | |||
| 692 | #### lower part: below drawing area (canvas) | ||
| 693 | my $controls=$MW->Frame(qw/ -relief ridge/)->pack(-fill=>'x'); | ||
| 694 | $controls->gridColumnconfigure(1,-weight=>1); | ||
| 695 | my $left=$controls->Frame(qw/-bd 5 -relief ridge/)->grid(qw/-row 0 -column 0 -sticky nsw/); | ||
| 696 | my $right=$controls->Frame(qw/-bd 5 -relief ridge/)->grid(qw/-row 0 -column 1 -sticky ew/); | ||
| 697 | |||
| 698 | |||
| 699 | ######## Print/Save as PostScript ####### | ||
| 700 | my %pinfo=('prcmd','lpr'); | ||
| 701 | my $w_prcmd = $left->Entry( | ||
| 702 | -textvariable => \$pinfo{'prcmd'},); | ||
| 703 | $MW->Advertise('entry' => $w_prcmd); | ||
| 704 | $w_prcmd->grid(qw/-row 0 -column 0 -sticky ew/); | ||
| 705 | |||
| 706 | my $w_print = $left->Button( | ||
| 707 | -text => 'Print/Save as PostScript', | ||
| 708 | -command => [\&roc_save, $c, \%pinfo],); | ||
| 709 | $MW->Advertise('PostScript_button' => $w_print); | ||
| 710 | $w_print->grid(qw/-row 1 -column 0 -sticky w /); | ||
| 711 | $w_prcmd->bind('<Return>' => [$w_print => 'invoke']); | ||
| 712 | ########################################## | ||
| 713 | |||
| 714 | ######## Delete ROC curves ############### | ||
| 715 | my $del_roc=$left->Button(-text=>'Delete ROC curve!', | ||
| 716 | -command=>sub{$c->delete('plot'); | ||
| 717 | $c->delete('opt'); | ||
| 718 | $c->delete('bounds'); | ||
| 719 | $c->delete('realroc'); | ||
| 720 | $c->delete('values')}, | ||
| 721 | -relief=>'raised') | ||
| 722 | ->grid(qw/-row 2 -column 0 -sticky ew/); | ||
| 723 | |||
| 724 | ########################################## | ||
| 725 | |||
| 726 | |||
| 727 | |||
| 728 | ######## Confidence Interval ######### | ||
| 729 | my $conf_scale=$right->Scale('-orient'=> 'horizontal', | ||
| 730 | '-from'=> 0, '-to'=> 100, '-tickinterval'=> 0, '-width'=> 15, | ||
| 731 | '-length'=> 340, | ||
| 732 | '-label'=> "2-sided Confidence Interval (%)", | ||
| 733 | -variable=>\$conf, | ||
| 734 | #-command=> [\&draw_roc, [$c, @points,\$model_type,\$conf,\$var_grp_ref]], | ||
| 735 | )->grid(qw/-row 0 -column 0 -columnspan 2 -sticky ew/); | ||
| 736 | ###################################### | ||
| 737 | |||
| 738 | |||
| 739 | ######## Model option button ######### | ||
| 740 | my $model_button=$right->Menubutton(-text=>'Model: ', | ||
| 741 | -relief=>'raised' )->grid(qw/-row 1 -column 0 -sticky ew/); | ||
| 742 | my $model=$right->Optionmenu(-variable => \$model_type, | ||
| 743 | -options => ['grp0 < grp1', 'grp0 > grp1'], | ||
| 744 | # -options=>['GGGGGGG', 'CCCCCC'], | ||
| 745 | #-command=>sub{print "$model_type \n";}, | ||
| 746 | # -command=>sub{print $model_type," \n";}, | ||
| 747 | #-command=> [\&draw_roc, [$c, @points,\$model_type,0.95,@var_grp]], | ||
| 748 | -command=> [\&draw_roc, [$c, @points,\$model_type,\$conf,\$var_grp_ref]], | ||
| 749 | -relief=>'raised'); | ||
| 750 | $model->grid(qw/-row 1 -column 1 -sticky ew/); | ||
| 751 | ####################################### | ||
| 752 | |||
| 753 | |||
| 754 | |||
| 755 | MainLoop; | ||
| 756 | |||
| 757 | |||
| 758 | |||
| 759 | |||
| 760 | |||
| 761 |