| 1 | package Algorithm::TrunkClassifier::DecisionTrunk; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | our $VERSION = 'v1.0.1'; | ||
| 7 | |||
| 8 | #Description: DecisionTrunk constructor | ||
| 9 | #Parameters: (1) TrunkClassifier::DecisionTrunk class | ||
| 10 | #Return value: TrunkClassifier::DecisionTrunk object | ||
| 11 | sub new($){ | ||
| 12 | my $class = shift(); | ||
| 13 | my @names; | ||
| 14 | my @lower; | ||
| 15 | my @higher; | ||
| 16 | my @lowerClass; | ||
| 17 | my @higherClass; | ||
| 18 | my $self = { | ||
| 19 | "level_name" => \@names, | ||
| 20 | "lower_threshold" => \@lower, | ||
| 21 | "higher_threshold" => \@higher, | ||
| 22 | "lower_class" => \@lowerClass, | ||
| 23 | "higher_class" => \@higherClass | ||
| 24 | }; | ||
| 25 | bless($self, $class); | ||
| 26 | return $self; | ||
| 27 | } | ||
| 28 | |||
| 29 | #Description: Adds a decision level to the trunk | ||
| 30 | #Parameters: (1) TrunkClassifier::DecisionTrunk object, (2) level name, (3) lower threshold, (4) higher threshold, (5) lower class, (6) higher class | ||
| 31 | #Return value: None | ||
| 32 | sub addLevel($ $ $ $ $ $){ | ||
| 33 | my ($self, $levelName, $lowerT, $higherT, $lowerC, $higherC) = @_; | ||
| 34 | push(@{$self->{"level_name"}}, $levelName); | ||
| 35 | push(@{$self->{"lower_threshold"}}, $lowerT); | ||
| 36 | push(@{$self->{"higher_threshold"}}, $higherT); | ||
| 37 | push(@{$self->{"lower_class"}}, $lowerC); | ||
| 38 | push(@{$self->{"higher_class"}}, $higherC); | ||
| 39 | } | ||
| 40 | |||
| 41 | #Description: Classifies the test set based on the thresholds in the trunk | ||
| 42 | #Parameters: (1) TrunkClassifier::DecisionTrunk object, (2) TrunkClassifier::DataWrapper object, (3) class one name, (4) class two name | ||
| 43 | # (5) class report array reference, (6) verbose flag | ||
| 44 | #Return value: Ratio of correct to total classification performance | ||
| 45 | sub classify($ $ $ $ $ $){ | ||
| 46 | my ($self, $testSet, $ClassOne, $classTwo, $classReport, $VERBOSE) = @_; | ||
| 47 | my $class; | ||
| 48 | my @classification; | ||
| 49 | my $ratioCorrect = 0; | ||
| 50 | for(my $sampleIndex = 0; $sampleIndex < $testSet->getNumSamples(); $sampleIndex++){ | ||
| 51 | $class = ""; | ||
| 52 | for(my $levelIndex = 0; $levelIndex < scalar(@{$self->{"level_name"}}); $levelIndex++){ | ||
| 53 | my $probeIndex = $testSet->getProbeIndex($self->{"level_name"}[$levelIndex]); | ||
| 54 | my @probeRow = $testSet->getMatrixRow($probeIndex); | ||
| 55 | if($probeRow[$sampleIndex] <= $self->{"lower_threshold"}[$levelIndex]){ | ||
| 56 | $class = $self->{"lower_class"}[$levelIndex]; | ||
| 57 | my $lvl = $levelIndex + 1; | ||
| 58 | my $sampleName = $testSet->getSampleName($sampleIndex); | ||
| 59 | push(@{$classReport}, "$sampleName in $lvl-$class"); | ||
| 60 | last; | ||
| 61 | } | ||
| 62 | elsif($probeRow[$sampleIndex] >= $self->{"higher_threshold"}[$levelIndex]){ | ||
| 63 | $class = $self->{"higher_class"}[$levelIndex]; | ||
| 64 | my $lvl = $levelIndex + 1; | ||
| 65 | my $sampleName = $testSet->getSampleName($sampleIndex); | ||
| 66 | push(@{$classReport}, "$sampleName in $lvl-$class"); | ||
| 67 | last; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | push(@classification, $class); | ||
| 71 | } | ||
| 72 | my @classVector = @{$testSet->getClassVector()}; | ||
| 73 | for(my $sampleIndex = 0; $sampleIndex < $testSet->getNumSamples(); $sampleIndex++){ | ||
| 74 | if($classification[$sampleIndex] eq $classVector[$sampleIndex]){ | ||
| 75 | $ratioCorrect++; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | $ratioCorrect /= $testSet->getNumSamples(); | ||
| 79 | return $ratioCorrect; | ||
| 80 | } | ||
| 81 | |||
| 82 | #Description: Returns a text report of the trunk structure | ||
| 83 | #Parameters: (1) TrunkClassifier::DecisionTrunk object | ||
| 84 | #Return value: String containing the trunk structure | ||
| 85 | sub report($){ | ||
| 86 | my $self = shift(); | ||
| 87 | my $report = ""; | ||
| 88 | for(my $level = 0; $level < scalar(@{$self->{"level_name"}}); $level++){ | ||
| 89 | my $name = $self->{"level_name"}[$level]; | ||
| 90 | my $lowerT = $self->{"lower_threshold"}[$level]; | ||
| 91 | my $lowerC = $self->{"lower_class"}[$level]; | ||
| 92 | my $higherT = $self->{"higher_threshold"}[$level]; | ||
| 93 | my $higherC = $self->{"higher_class"}[$level]; | ||
| 94 | $report .= "\t$name\n<= $lowerT ($lowerC)\t\t> $higherT ($higherC)\n\n"; | ||
| 95 | } | ||
| 96 | $report .= "--------------------------------------------------\n\n"; | ||
| 97 | return $report; | ||
| 98 | } | ||
| 99 | |||
| 100 | return 1; |