-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
215 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package de.viadee.sonarIssueScoring; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.ApplicationArguments; | ||
import org.springframework.boot.ApplicationRunner; | ||
import org.springframework.stereotype.Component; | ||
|
||
import de.viadee.sonarIssueScoring.service.PredictionParams; | ||
import de.viadee.sonarIssueScoring.service.desirability.ServerInfo; | ||
import de.viadee.sonarIssueScoring.service.desirability.ServerInfo.Builder; | ||
import de.viadee.sonarIssueScoring.service.prediction.EvaluationResult; | ||
import de.viadee.sonarIssueScoring.service.prediction.PredictionService; | ||
|
||
@Component | ||
public class Evaluator implements ApplicationRunner { | ||
private static final Logger log = LoggerFactory.getLogger(Evaluator.class); | ||
|
||
private final PredictionService predictionService; | ||
|
||
public Evaluator(PredictionService predictionService) {this.predictionService = predictionService;} | ||
|
||
/** | ||
* Evaluates the prediction quality versus the actual future on a given sample project. | ||
*/ | ||
@Override | ||
public void run(ApplicationArguments args) { | ||
if (willRunEvaluation(args)) { | ||
log.info("Starting evaluation. No web server is started."); //Web server is disabled in SonarIssueScoringApplication | ||
|
||
Builder builder = ServerInfo.builder(); | ||
|
||
if (args.containsOption("repo")) | ||
builder.url(args.getOptionValues("repo").get(0)); | ||
else { | ||
log.info("No repository provided, using default"); | ||
builder.url("https://github.com/apache/commons-lang"); | ||
} | ||
|
||
if (args.containsOption("user")) | ||
builder.user(args.getOptionValues("user").get(0)); | ||
|
||
if (args.containsOption("password")) | ||
builder.password(args.getOptionValues("password").get(0)); | ||
|
||
int horizon = 384; | ||
if (args.containsOption("horizon")) | ||
horizon = Integer.parseInt(args.getOptionValues("horizon").get(0)); | ||
|
||
ServerInfo server = builder.build(); | ||
|
||
log.info("Running evaluation for {} with horizon", predictionService); //Password is redacted automatically | ||
EvaluationResult result = predictionService.evaluate(PredictionParams.of(server, horizon), "http://localhost:54321"); | ||
log.info("Evaluation result {}", result); | ||
} | ||
} | ||
|
||
/** Static, because the context is not yet setup when this is called. */ | ||
public static boolean willRunEvaluation(ApplicationArguments args) { | ||
return args.containsOption("evaluate"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/de/viadee/sonarIssueScoring/service/prediction/BaseEvaluationResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package de.viadee.sonarIssueScoring.service.prediction; | ||
|
||
import org.immutables.value.Value.Immutable; | ||
|
||
import com.google.common.collect.Table; | ||
|
||
import de.viadee.sonarIssueScoring.misc.ImmutableStyle; | ||
|
||
@Immutable | ||
@ImmutableStyle | ||
public interface BaseEvaluationResult { | ||
public double rmse(); | ||
|
||
public double r2(); | ||
|
||
/** Rows: actual value > 80% percentile, Cols: predicted value > 80% percentile */ | ||
public Table<Boolean, Boolean, Integer> confusionMatrix(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/test/java/de/viadee/sonarIssueScoring/service/prediction/PredictionServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package de.viadee.sonarIssueScoring.service.prediction; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.Table; | ||
|
||
import de.viadee.sonarIssueScoring.service.prediction.PredictionService.ResultPair; | ||
|
||
public class PredictionServiceTest { | ||
|
||
@Test | ||
public void confusionMatrix() { | ||
List<ResultPair> values = new ArrayList<>(ImmutableList.of(// | ||
new ResultPair(0, 1),// | ||
new ResultPair(0, 1),// | ||
new ResultPair(1, 0),// | ||
new ResultPair(1, 0),// | ||
new ResultPair(1, 0),// | ||
new ResultPair(1, 1)));// | ||
|
||
for (int i = 0; i < 8; i++) | ||
values.add(new ResultPair(0, 0)); | ||
|
||
Table<Boolean, Boolean, Integer> matrix = PredictionService.confusionMatrix(values); | ||
// actual, predicted | ||
Assert.assertEquals(8, (int) matrix.get(false, false)); | ||
Assert.assertEquals(1, (int) matrix.get(true, true)); | ||
Assert.assertEquals(3, (int) matrix.get(false, true)); | ||
Assert.assertEquals(2, (int) matrix.get(true, false)); | ||
} | ||
|
||
@Test | ||
public void rmse() { | ||
ImmutableList<ResultPair> values = ImmutableList.of(// | ||
new ResultPair(90, 80),// 10 * 10 = 100 | ||
new ResultPair(50, 70),// 20 * 20 = 400 | ||
new ResultPair(50, 50));// 0 * 0 = 0 | ||
|
||
//sqrt(500/3) | ||
Assert.assertEquals(12.909, PredictionService.rmse(values), 1.0e-3); | ||
} | ||
|
||
@Test | ||
public void r2() { | ||
ImmutableList<ResultPair> values = ImmutableList.of(// | ||
new ResultPair(90, 80),// | ||
new ResultPair(76, 70),// | ||
new ResultPair(50, 0),// | ||
new ResultPair(33, 30),// | ||
new ResultPair(40, 40)); | ||
|
||
Assert.assertEquals(0.58920, PredictionService.r2(values), 5.0e-5); | ||
} | ||
} |