| 1 | package Algorithm::TrunkClassifier::Classification; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use Algorithm::TrunkClassifier::DataWrapper; | ||
| 7 | use Algorithm::TrunkClassifier::FeatureSelection; | ||
| 8 | use Algorithm::TrunkClassifier::DecisionTrunk; | ||
| 9 | use Algorithm::TrunkClassifier::Util; | ||
| 10 | use POSIX; | ||
| 11 | |||
| 12 | our $VERSION = "v1.0.1"; | ||
| 13 | |||
| 14 | #Description: Function responsible for building decision trunks and classifying test samples using LOOCV | ||
| 15 | #Parameters: (1) Package, (2) input dataset, (3) test dataset, (4) classification procedure, (5) split percent, | ||
| 16 | # (6) testset data file name, (7) classification variable name, (8) output folder name, | ||
| 17 | # (9) number of levels, (10) verbose flag, (11) input data file name (12) useall flag | ||
| 18 | #Return value: None | ||
| 19 | sub trainAndClassify($ $ $ $ $ $ $ $ $ $ $ $ $){ | ||
| 20 | shift(@_); | ||
| 21 | my ($dataWrapper, $testset, $CLASSIFY, $SPLITPERCENT, $TESTFILE, $CLASSNAME, $OUTPUT, $LEVELS, $VERBOSE, $DATAFILE, $USEALL) = @_; | ||
| 22 | |||
| 23 | #Create output files | ||
| 24 | if(!-e $OUTPUT && $OUTPUT ne "."){ | ||
| 25 | system("mkdir $OUTPUT"); | ||
| 26 | } | ||
| 27 | open(PERFORMANCE, ">$OUTPUT/performance.txt") or die "Error: Unable to create output file\n"; | ||
| 28 | open(LOO_TRUNKS, ">$OUTPUT/loo_trunks.txt") or die "Error: Unable to create output file\n"; | ||
| 29 | open(CTS_TRUNKS, ">$OUTPUT/cts_trunks.txt") or die "Error: Unable to create output file\n"; | ||
| 30 | open(REPORT, ">$OUTPUT/class_report.txt") or die "Error: Unable to create output file\n"; | ||
| 31 | open(LOG, ">$OUTPUT/log.txt") or die "Error: Unable to create output file\n"; | ||
| 32 | |||
| 33 | #Establish training and test set | ||
| 34 | my $trainingSet; | ||
| 35 | my $testSet; | ||
| 36 | if($CLASSIFY eq "loocv"){ | ||
| 37 | $trainingSet = $dataWrapper->copy(); | ||
| 38 | } | ||
| 39 | elsif($CLASSIFY eq "split"){ | ||
| 40 | my $containsBoth = 0; | ||
| 41 | while(!$containsBoth){ | ||
| 42 | $trainingSet = $dataWrapper->copy(); | ||
| 43 | $testSet = $trainingSet->splitSamples($SPLITPERCENT); | ||
| 44 | my $class1 = $trainingSet->getClassOneName(); | ||
| 45 | my $class2 = $trainingSet->getClassTwoName(); | ||
| 46 | if($trainingSet->getClassSize($class1) && $trainingSet->getClassSize($class2)){ | ||
| 47 | $containsBoth = 1; | ||
| 48 | } | ||
| 49 | } | ||
| 50 | } | ||
| 51 | elsif($CLASSIFY eq "dual"){ | ||
| 52 | $trainingSet = $dataWrapper->copy(); | ||
| 53 | $testSet = $testset->copy(); | ||
| 54 | } | ||
| 55 | |||
| 56 | #Build trunks using leave-one-out | ||
| 57 | my %featureOccurrence; | ||
| 58 | my %selectedFeatures; | ||
| 59 | my %looTrunks = ("1" => [], "2" => [], "3" => [], "4" => [], "5" => []); | ||
| 60 | my $levelBreak = 0; | ||
| 61 | for(my $levelLimit = 1; $levelLimit <= 5; $levelLimit++){ | ||
| 62 | if($VERBOSE){ | ||
| 63 | print("Trunk classifier: Building decision trunks with $levelLimit level(s) using leave-one-out\n"); | ||
| 64 | } | ||
| 65 | |||
| 66 | #Build one trunk for each left out sample | ||
| 67 | for(my $sampleIndex = 0; $sampleIndex < $trainingSet->getNumSamples(); $sampleIndex++){ | ||
| 68 | if($VERBOSE){ | ||
| 69 | print("Trunk classifier: Fold ", $sampleIndex + 1, " of ", $dataWrapper->getNumSamples(), "\n"); | ||
| 70 | } | ||
| 71 | my $buildSet = $trainingSet->copy(); | ||
| 72 | $buildSet->leaveOneOut($sampleIndex); | ||
| 73 | my $decisionTrunk = buildTrunk($buildSet, $levelLimit, $sampleIndex, \%featureOccurrence, \%selectedFeatures, \$levelBreak, $VERBOSE); | ||
| 74 | |||
| 75 | #Add trunk to hash | ||
| 76 | push(@{$looTrunks{$levelLimit}}, $decisionTrunk); | ||
| 77 | |||
| 78 | } | ||
| 79 | |||
| 80 | if($levelBreak){ | ||
| 81 | undef $featureOccurrence{$levelLimit}; | ||
| 82 | $looTrunks{$levelLimit} = []; | ||
| 83 | last; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | #Build trunks using complete training set | ||
| 88 | my %ctsTrunks = ("1" => 0, "2" => 0, "3" => 0, "4" => 0, "5" => 0); | ||
| 89 | my %selFeats; | ||
| 90 | my %dummyHash; | ||
| 91 | $levelBreak = 0; | ||
| 92 | if($VERBOSE){ | ||
| 93 | print("Trunk classifier: Building decision trunks using complete training set\n"); | ||
| 94 | } | ||
| 95 | for(my $levelLimit = 1; $levelLimit <= 5; $levelLimit++){ | ||
| 96 | my $buildSet = $trainingSet->copy(); | ||
| 97 | my $decisionTrunk = buildTrunk($buildSet, $levelLimit, 0, \%dummyHash, \%selFeats, \$levelBreak, $VERBOSE); | ||
| 98 | |||
| 99 | #Add trunk to hash | ||
| 100 | $ctsTrunks{$levelLimit} = $decisionTrunk; | ||
| 101 | |||
| 102 | if($levelBreak){ | ||
| 103 | last; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | #Determine number of levels to use for classification | ||
| 108 | my @numTrunkLevels; | ||
| 109 | my $trunkType = ""; | ||
| 110 | if($CLASSIFY eq "loocv"){ | ||
| 111 | $trunkType = "LOO"; | ||
| 112 | } | ||
| 113 | elsif($CLASSIFY eq "split" || $CLASSIFY eq "dual"){ | ||
| 114 | $trunkType = "CTS" | ||
| 115 | } | ||
| 116 | if(!$USEALL && $LEVELS){ | ||
| 117 | if(!@{$looTrunks{$LEVELS}}){ | ||
| 118 | for(my $levelIndex = $LEVELS - 1; $levelIndex > 0; $levelIndex--){ | ||
| 119 | if(@{$looTrunks{$LEVELS}}){ | ||
| 120 | push(@numTrunkLevels, $levelIndex); | ||
| 121 | warn "Warning: Supplied level is to high, using $trunkType trunks with $levelIndex level(s) instead\n"; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | } | ||
| 125 | else{ | ||
| 126 | push(@numTrunkLevels, $LEVELS); | ||
| 127 | if($VERBOSE){ | ||
| 128 | print("Trunk classifier: Using $trunkType trunks with $numTrunkLevels[0] level(s) (forced)\n"); | ||
| 129 | } | ||
| 130 | } | ||
| 131 | } | ||
| 132 | elsif(!$USEALL){ | ||
| 133 | push(@numTrunkLevels, stabilityCheck(\%featureOccurrence, $dataWrapper->getNumSamples())); | ||
| 134 | if($VERBOSE){ | ||
| 135 | print("Trunk classifier: Using $trunkType trunks with $numTrunkLevels[0] level(s) (stability)\n"); | ||
| 136 | } | ||
| 137 | } | ||
| 138 | else{ | ||
| 139 | push(@numTrunkLevels, (1, 2, 3, 4, 5)); | ||
| 140 | if($VERBOSE){ | ||
| 141 | print("Trunk classifier: Using all $trunkType trunks for classification\n"); | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | #Classify test set | ||
| 146 | my @performance; | ||
| 147 | my @classReport; | ||
| 148 | my $avePerformance; | ||
| 149 | my $procedure = ""; | ||
| 150 | if($CLASSIFY eq "loocv"){ | ||
| 151 | $procedure = "LOOCV on input dataset"; | ||
| 152 | } | ||
| 153 | elsif($CLASSIFY eq "split"){ | ||
| 154 | $procedure = "split-sample testset"; | ||
| 155 | } | ||
| 156 | elsif($CLASSIFY eq "dual"){ | ||
| 157 | $procedure = "supplied test dataset"; | ||
| 158 | } | ||
| 159 | if($VERBOSE){ | ||
| 160 | print("Trunk classifier: Performing classification using $procedure\n"); | ||
| 161 | } | ||
| 162 | foreach my $numLevels (@numTrunkLevels){ | ||
| 163 | $avePerformance = 0; | ||
| 164 | my @perArray; | ||
| 165 | if(!@{$looTrunks{$numLevels}}){ | ||
| 166 | last; | ||
| 167 | } | ||
| 168 | if($USEALL){ | ||
| 169 | if($numLevels > 1){ | ||
| 170 | push(@performance, "\n"); | ||
| 171 | push(@classReport, "\n"); | ||
| 172 | } | ||
| 173 | push(@performance, "Performance for trunks with $numLevels levels\n"); | ||
| 174 | push(@classReport, "Classification using trunks with $numLevels levels\n"); | ||
| 175 | } | ||
| 176 | |||
| 177 | #Leave-one-out cross validation | ||
| 178 | if($CLASSIFY eq "loocv"){ | ||
| 179 | for(my $sampleIndex = 0; $sampleIndex < $trainingSet->getNumSamples(); $sampleIndex++){ | ||
| 180 | if($VERBOSE){ | ||
| 181 | print("Trunk classifier: Fold ", $sampleIndex + 1, " of ", $dataWrapper->getNumSamples(), "\n"); | ||
| 182 | } | ||
| 183 | my $trainingBuffer = $trainingSet->copy(); | ||
| 184 | $testSet = $trainingBuffer->leaveOneOut($sampleIndex); | ||
| 185 | |||
| 186 | #Classify test set | ||
| 187 | my $perBuffer = ${$looTrunks{$numLevels}}[$sampleIndex]->classify($testSet, $testSet->getClassOneName(), | ||
| 188 | $testSet->getClassTwoName(), \@classReport, $VERBOSE); | ||
| 189 | $avePerformance += $perBuffer; | ||
| 190 | my $indexBuffer = $sampleIndex + 1; | ||
| 191 | $perBuffer *= 100; | ||
| 192 | $perBuffer = "Fold $indexBuffer: $perBuffer %"; | ||
| 193 | push(@perArray, $perBuffer); | ||
| 194 | } | ||
| 195 | $avePerformance = ($avePerformance / $dataWrapper->getNumSamples()) * 100; | ||
| 196 | unshift(@perArray, "Average: $avePerformance %"); | ||
| 197 | push(@performance, @perArray); | ||
| 198 | } | ||
| 199 | elsif($CLASSIFY eq "split" || $CLASSIFY eq "dual"){ | ||
| 200 | my $perBuffer = $ctsTrunks{$numLevels}->classify($testSet, $testSet->getClassOneName(), | ||
| 201 | $testSet->getClassTwoName(), \@classReport, $VERBOSE); | ||
| 202 | $perBuffer *= 100; | ||
| 203 | $perBuffer = "Performance: $perBuffer %"; | ||
| 204 | push(@performance, $perBuffer); | ||
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 | #Write results to output files | ||
| 209 | if($VERBOSE){ | ||
| 210 | print("Trunk classifier: Writing output\n"); | ||
| 211 | } | ||
| 212 | my $trunkCount; | ||
| 213 | foreach my $numLevels (@numTrunkLevels){ | ||
| 214 | $trunkCount = 0; | ||
| 215 | if(!@{$looTrunks{$numLevels}}){ | ||
| 216 | last; | ||
| 217 | } | ||
| 218 | print(LOO_TRUNKS ">Trunks with $numLevels level(s)\n\n"); | ||
| 219 | foreach my $trunk (@{$looTrunks{$numLevels}}){ | ||
| 220 | $trunkCount++; | ||
| 221 | print(LOO_TRUNKS ">Trunk $trunkCount\n", $trunk->report()); | ||
| 222 | } | ||
| 223 | print(CTS_TRUNKS ">Trunk with $numLevels level(s)\n\n"); | ||
| 224 | print(CTS_TRUNKS $ctsTrunks{$numLevels}->report()); | ||
| 225 | } | ||
| 226 | if($USEALL){ | ||
| 227 | $numTrunkLevels[0] = "USEALL"; | ||
| 228 | } | ||
| 229 | print(PERFORMANCE join("\n", @performance)); | ||
| 230 | print(REPORT join("\n", @classReport)); | ||
| 231 | if($CLASSIFY ne "dual"){ | ||
| 232 | $TESTFILE = "NA"; | ||
| 233 | } | ||
| 234 | if($CLASSIFY ne "split"){ | ||
| 235 | $SPLITPERCENT = "NA"; | ||
| 236 | } | ||
| 237 | my $name1 = $dataWrapper->getClassOneName(); | ||
| 238 | my $name2 = $dataWrapper->getClassTwoName(); | ||
| 239 | my $log = "Trunk classifier log\n"; | ||
| 240 | $log .= "Input data file: $DATAFILE\n"; | ||
| 241 | $log .= "Testset data file: $TESTFILE\n"; | ||
| 242 | $log .= "Procedure: $CLASSIFY\n"; | ||
| 243 | $log .= "Split percent: $SPLITPERCENT\n"; | ||
| 244 | $log .= "Number of levels: $numTrunkLevels[0]\n"; | ||
| 245 | $log .= "Classification variable: $CLASSNAME\n"; | ||
| 246 | $log .= "Training set classes:\n"; | ||
| 247 | if($CLASSIFY eq "loocv"){ | ||
| 248 | $log .= "\tClass one size: " . $dataWrapper->getClassSize($name1) . " ($name1)\n"; | ||
| 249 | $log .= "\tClass two size: " . $dataWrapper->getClassSize($name2) . " ($name2)\n"; | ||
| 250 | } | ||
| 251 | else{ | ||
| 252 | $log .= "\tClass one size: " . $trainingSet->getClassSize($name1) . " ($name1)\n"; | ||
| 253 | $log .= "\tClass two size: " . $trainingSet->getClassSize($name2) . " ($name2)\n"; | ||
| 254 | } | ||
| 255 | $log .= "Test set classes:\n"; | ||
| 256 | if($CLASSIFY eq "loocv"){ | ||
| 257 | $log .= "\tClass one size: NA\n"; | ||
| 258 | $log .= "\tClass two size: NA\n"; | ||
| 259 | } | ||
| 260 | else{ | ||
| 261 | $log .= "\tClass one size: " . $testSet->getClassSize($name1) . " ($name1)\n"; | ||
| 262 | $log .= "\tClass two size: " . $testSet->getClassSize($name2) . " ($name2)\n"; | ||
| 263 | } | ||
| 264 | $log .= "Version: $VERSION"; | ||
| 265 | print(LOG $log); | ||
| 266 | close(PERFORMANCE); | ||
| 267 | close(LOO_TRUNKS); | ||
| 268 | close(CTS_TRUNKS); | ||
| 269 | close(REPORT); | ||
| 270 | close(LOG); | ||
| 271 | if($VERBOSE){ | ||
| 272 | print("Trunk classifier: Job finished\n"); | ||
| 273 | } | ||
| 274 | } | ||
| 275 | |||
| 276 | #Description: Wrapper for the trunk build loop | ||
| 277 | #Parameters: (1) Training dataset, (2) level limit, (3) sample index, (4) feature occurrence hash ref, | ||
| 278 | # (5) selected features hash ref, (6) level break flag ref, (7) verbose flag | ||
| 279 | #Return value: Decision trunk object | ||
| 280 | sub buildTrunk($ $ $ $ $ $ $){ | ||
| 281 | my ($buildSet, $levelLimit, $sampleIndex, $featOccurRef, $selFeatRef, $levelBreakRef, $VERBOSE) = @_; | ||
| 282 | |||
| 283 | #Trunk build loop | ||
| 284 | my $decisionTrunk = Algorithm::TrunkClassifier::DecisionTrunk->new(); | ||
| 285 | my $noSampleBreak = 0; | ||
| 286 | for(my $levelIndex = 1; $levelIndex <= $levelLimit; $levelIndex++){ | ||
| 287 | |||
| 288 | #Perform feature selection | ||
| 289 | my $featureName; | ||
| 290 | my $featureIndex; | ||
| 291 | my @expRow; | ||
| 292 | if(!$selFeatRef->{$sampleIndex}{$levelIndex}){ | ||
| 293 | $featureIndex = Algorithm::TrunkClassifier::FeatureSelection::indTTest( | ||
| 294 | $buildSet->getDataMatrix(), $buildSet->getNumProbes(), | ||
| 295 | $buildSet->getNumSamples(), $buildSet->getClassVector(), | ||
| 296 | $buildSet->getClassOneName(), $buildSet->getClassTwoName()); | ||
| 297 | $featureName = $buildSet->getProbeName($featureIndex); | ||
| 298 | @expRow = $buildSet->getMatrixRow($featureIndex); | ||
| 299 | my @savedRow = $buildSet->getMatrixRow($featureIndex); | ||
| 300 | $buildSet->removeProbe($featureIndex); | ||
| 301 | $selFeatRef->{$sampleIndex}{$levelIndex} = {"feature" => $featureName, "index" => $featureIndex, "row" => \@savedRow}; | ||
| 302 | if(!$featOccurRef->{$levelIndex}{$featureName}){ | ||
| 303 | $featOccurRef->{$levelIndex}{$featureName} = 1; | ||
| 304 | } | ||
| 305 | else{ | ||
| 306 | $featOccurRef->{$levelIndex}{$featureName}++; | ||
| 307 | } | ||
| 308 | } | ||
| 309 | else{ | ||
| 310 | $featureName = $selFeatRef->{$sampleIndex}{$levelIndex}{"feature"}; | ||
| 311 | $featureIndex = $selFeatRef->{$sampleIndex}{$levelIndex}{"index"}; | ||
| 312 | @expRow = @{$selFeatRef->{$sampleIndex}{$levelIndex}{"row"}}; | ||
| 313 | $buildSet->removeProbe($featureIndex); | ||
| 314 | } | ||
| 315 | |||
| 316 | #Initialise variables | ||
| 317 | my @expBuffer = @expRow; | ||
| 318 | my @classSetInd = (0 .. ($buildSet->getNumSamples() - 1)); | ||
| 319 | my @classVector = @{$buildSet->getClassVector()}; | ||
| 320 | my $numSamples = $buildSet->getNumSamples(); | ||
| 321 | Algorithm::TrunkClassifier::Util::dataSort(\@expRow, \@classVector); | ||
| 322 | Algorithm::TrunkClassifier::Util::dataSort(\@expBuffer, \@classSetInd); | ||
| 323 | |||
| 324 | #Determine quartile thresholds | ||
| 325 | my $quantStep = $numSamples / 4; | ||
| 326 | my $lowerThresh; | ||
| 327 | my $higherThresh; | ||
| 328 | my $lowFloor = floor($quantStep); | ||
| 329 | $lowerThresh = ($expRow[$lowFloor] + $expRow[$lowFloor+1]) / 2; | ||
| 330 | my $highFloor = floor($quantStep * 3); | ||
| 331 | if(!$expRow[$highFloor+1]){ | ||
| 332 | $higherThresh = $expRow[$highFloor]; | ||
| 333 | } | ||
| 334 | else{ | ||
| 335 | $higherThresh = ($expRow[$highFloor] + $expRow[$highFloor+1]) / 2; | ||
| 336 | } | ||
| 337 | |||
| 338 | #Determine low and high class | ||
| 339 | my $lowerClass = ""; | ||
| 340 | my $higherClass = ""; | ||
| 341 | if($classVector[0] eq $buildSet->getClassOneName()){ | ||
| 342 | $lowerClass = $buildSet->getClassOneName(); | ||
| 343 | $higherClass = $buildSet->getClassTwoName(); | ||
| 344 | } | ||
| 345 | elsif($classVector[0] eq $buildSet->getClassTwoName()){ | ||
| 346 | $lowerClass = $buildSet->getClassTwoName(); | ||
| 347 | $higherClass = $buildSet->getClassOneName(); | ||
| 348 | } | ||
| 349 | |||
| 350 | #Determine samples in quartiles | ||
| 351 | my @indToRemove; | ||
| 352 | my $decisionBuffer; | ||
| 353 | my $lowerDecision; | ||
| 354 | my $higherDecision; | ||
| 355 | if($levelIndex < $levelLimit){ | ||
| 356 | |||
| 357 | #Lower quartile | ||
| 358 | $decisionBuffer = ""; | ||
| 359 | for(my $classSample = 0; $classSample < $numSamples; $classSample++){ | ||
| 360 | if($classVector[$classSample] ne $lowerClass){ | ||
| 361 | if($expRow[$classSample] > $lowerThresh){ | ||
| 362 | $decisionBuffer = $lowerThresh; | ||
| 363 | } | ||
| 364 | else{ | ||
| 365 | $decisionBuffer = ($expRow[$classSample - 1] + $expRow[$classSample]) / 2; | ||
| 366 | } | ||
| 367 | last; | ||
| 368 | } | ||
| 369 | if($expRow[$classSample] <= $lowerThresh){ | ||
| 370 | push(@indToRemove, $classSetInd[$classSample]); | ||
| 371 | } | ||
| 372 | else{ | ||
| 373 | $decisionBuffer = $lowerThresh; | ||
| 374 | last; | ||
| 375 | } | ||
| 376 | } | ||
| 377 | $lowerDecision = $decisionBuffer; | ||
| 378 | |||
| 379 | #Higher quartile | ||
| 380 | $decisionBuffer = ""; | ||
| 381 | for(my $classSample = $numSamples - 1; $classSample >= 0; $classSample--){ | ||
| 382 | if($classVector[$classSample] ne $higherClass){ | ||
| 383 | if($expRow[$classSample] < $higherThresh){ | ||
| 384 | $decisionBuffer = $higherThresh; | ||
| 385 | } | ||
| 386 | elsif($classSample != $numSamples - 1){ | ||
| 387 | $decisionBuffer = ($expRow[$classSample] + $expRow[$classSample + 1]) / 2; | ||
| 388 | } | ||
| 389 | else{ | ||
| 390 | $decisionBuffer = $expRow[$classSample]; | ||
| 391 | } | ||
| 392 | last; | ||
| 393 | } | ||
| 394 | if($expRow[$classSample] >= $higherThresh){ | ||
| 395 | push(@indToRemove, $classSetInd[$classSample]); | ||
| 396 | } | ||
| 397 | else{ | ||
| 398 | $decisionBuffer = $higherThresh; | ||
| 399 | last; | ||
| 400 | } | ||
| 401 | } | ||
| 402 | $higherDecision = $decisionBuffer; | ||
| 403 | } | ||
| 404 | else{ | ||
| 405 | #Do not use quartiles at last level of trunk | ||
| 406 | $decisionBuffer = ""; | ||
| 407 | for(my $classSample = 0; $buildSet->getNumSamples(); $classSample++){ | ||
| 408 | if($classVector[$classSample] ne $lowerClass){ | ||
| 409 | $decisionBuffer = ($expRow[$classSample - 1] + $expRow[$classSample]) / 2; | ||
| 410 | last; | ||
| 411 | } | ||
| 412 | } | ||
| 413 | $lowerDecision = $decisionBuffer; | ||
| 414 | for(my $classSample = $numSamples - 1; $classSample >= 0; $classSample--){ | ||
| 415 | if($classVector[$classSample] ne $higherClass){ | ||
| 416 | if($classSample != $numSamples - 1){ | ||
| 417 | $decisionBuffer = ($expRow[$classSample] + $expRow[$classSample + 1]) / 2; | ||
| 418 | } | ||
| 419 | else{ | ||
| 420 | $decisionBuffer = $expRow[$classSample]; | ||
| 421 | } | ||
| 422 | last; | ||
| 423 | } | ||
| 424 | } | ||
| 425 | $higherDecision = $decisionBuffer; | ||
| 426 | $lowerDecision = ($lowerDecision + $higherDecision) / 2; | ||
| 427 | $higherDecision = $lowerDecision; | ||
| 428 | } | ||
| 429 | |||
| 430 | #Remove samples in quartiles | ||
| 431 | @indToRemove = sort {$b <=> $a} @indToRemove; | ||
| 432 | foreach my $index (@indToRemove){ | ||
| 433 | $buildSet->removeSample($index); | ||
| 434 | } | ||
| 435 | |||
| 436 | #Check that there are at least one sample left in each class and at least four samples left in total | ||
| 437 | my $classOneSize = $buildSet->getClassSize($buildSet->getClassOneName()); | ||
| 438 | my $classTwoSize = $buildSet->getClassSize($buildSet->getClassTwoName()); | ||
| 439 | if($levelIndex < $levelLimit && ($classOneSize < 1 || $classTwoSize < 1 || $classOneSize + $classTwoSize < 4)){ | ||
| 440 | if($VERBOSE){ | ||
| 441 | print("Trunk classifier: Not enough samples, stopping at level $levelIndex of $levelLimit\n"); | ||
| 442 | } | ||
| 443 | $noSampleBreak = 1; | ||
| 444 | ${$levelBreakRef} = 1; | ||
| 445 | $lowerDecision = ($lowerDecision + $higherDecision) / 2; | ||
| 446 | $higherDecision = $lowerDecision; | ||
| 447 | } | ||
| 448 | |||
| 449 | #Add level to decision trunk | ||
| 450 | $decisionTrunk->addLevel($featureName, $lowerDecision, $higherDecision, $lowerClass, $higherClass); | ||
| 451 | |||
| 452 | if($noSampleBreak){ | ||
| 453 | last; | ||
| 454 | } | ||
| 455 | } | ||
| 456 | return $decisionTrunk; | ||
| 457 | } | ||
| 458 | |||
| 459 | #Description: Determine the decision trunk level with highest feature selection stability | ||
| 460 | #Parameters: (1) Hash reference containing selected features, (2) number of samples in the dataset | ||
| 461 | #Return value: Number of decision trunk levels to use for classification | ||
| 462 | sub stabilityCheck($ $){ | ||
| 463 | my ($hashRef, $numSamples) = @_; | ||
| 464 | my %featOccurrence = %{$hashRef}; | ||
| 465 | my $numThresh = 6; | ||
| 466 | my $chosenLevel = 0; | ||
| 467 | foreach my $levelIndex (1 .. 5){ | ||
| 468 | if(!$featOccurrence{$levelIndex}){ | ||
| 469 | next; | ||
| 470 | } | ||
| 471 | my %features = %{$featOccurrence{$levelIndex}}; | ||
| 472 | my $numFeats = scalar(keys(%features)); | ||
| 473 | if($numFeats > $numThresh){ | ||
| 474 | next; | ||
| 475 | } | ||
| 476 | $chosenLevel = $levelIndex; | ||
| 477 | } | ||
| 478 | if(!$chosenLevel){ | ||
| 479 | $chosenLevel = 1; | ||
| 480 | } | ||
| 481 | return $chosenLevel; | ||
| 482 | } | ||
| 483 | |||
| 484 | return 1; |