-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor significance heuristic tests for easier extensability #75264
Merged
benwtrent
merged 5 commits into
elastic:master
from
benwtrent:tests/heuristic-test-refactor
Jul 13, 2021
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
35e573f
Refactor significance heuristic tests for easier extensability
benwtrent d9b0ced
reverting whitespace changes
benwtrent 152ad25
moar whitespace
benwtrent cd5a8d7
Merge branch 'master' into tests/heuristic-test-refactor
elasticmachine 0c925fd
addressing PR comments
benwtrent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
265 changes: 88 additions & 177 deletions
265
...ket/terms/SignificanceHeuristicTests.java → ...s/AbstractSignificanceHeuristicTests.java
Large diffs are not rendered by default.
Oops, something went wrong.
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
28 changes: 28 additions & 0 deletions
28
...est/java/org/elasticsearch/search/aggregations/bucket/terms/heuristic/ChiSquareTests.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,28 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.search.aggregations.bucket.terms.heuristic; | ||
|
||
import org.elasticsearch.search.aggregations.bucket.terms.AbstractSignificanceHeuristicTests; | ||
|
||
public class ChiSquareTests extends AbstractSignificanceHeuristicTests { | ||
@Override | ||
protected SignificanceHeuristic getHeuristic() { | ||
return new ChiSquare(randomBoolean(), randomBoolean()); | ||
} | ||
|
||
@Override | ||
protected boolean testZeroScore() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void testAssertions() { | ||
testBackgroundAssertions(new ChiSquare(true, true), new ChiSquare(true, false)); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
.../src/test/java/org/elasticsearch/search/aggregations/bucket/terms/heuristic/GNDTests.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,46 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.search.aggregations.bucket.terms.heuristic; | ||
|
||
import org.elasticsearch.search.aggregations.bucket.terms.AbstractSignificanceHeuristicTests; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class GNDTests extends AbstractSignificanceHeuristicTests { | ||
@Override | ||
protected SignificanceHeuristic getHeuristic() { | ||
return new GND(randomBoolean()); | ||
} | ||
|
||
@Override | ||
protected boolean testZeroScore() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void testAssertions() { | ||
testBackgroundAssertions(new GND(true), new GND(false)); | ||
} | ||
|
||
public void testGNDCornerCases() { | ||
GND gnd = new GND(true); | ||
//term is only in the subset, not at all in the other set but that is because the other set is empty. | ||
// this should actually not happen because only terms that are in the subset are considered now, | ||
// however, in this case the score should be 0 because a term that does not exist cannot be relevant... | ||
assertThat(gnd.getScore(0, randomIntBetween(1, 2), 0, randomIntBetween(2,3)), equalTo(0.0)); | ||
// the terms do not co-occur at all - should be 0 | ||
assertThat(gnd.getScore(0, randomIntBetween(1, 2), randomIntBetween(2, 3), randomIntBetween(5,6)), equalTo(0.0)); | ||
// comparison between two terms that do not exist - probably not relevant | ||
assertThat(gnd.getScore(0, 0, 0, randomIntBetween(1,2)), equalTo(0.0)); | ||
// terms co-occur perfectly - should be 1 | ||
assertThat(gnd.getScore(1, 1, 1, 1), equalTo(1.0)); | ||
gnd = new GND(false); | ||
assertThat(gnd.getScore(0, 0, 0, 0), equalTo(0.0)); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...test/java/org/elasticsearch/search/aggregations/bucket/terms/heuristic/JLHScoreTests.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,28 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.search.aggregations.bucket.terms.heuristic; | ||
|
||
import org.elasticsearch.search.aggregations.bucket.terms.AbstractSignificanceHeuristicTests; | ||
|
||
public class JLHScoreTests extends AbstractSignificanceHeuristicTests { | ||
@Override | ||
protected SignificanceHeuristic getHeuristic() { | ||
return new JLHScore(); | ||
} | ||
|
||
@Override | ||
protected boolean testZeroScore() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void testAssertions() { | ||
testAssertions(new JLHScore()); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
.../org/elasticsearch/search/aggregations/bucket/terms/heuristic/MutualInformationTests.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,71 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.search.aggregations.bucket.terms.heuristic; | ||
|
||
import org.elasticsearch.search.aggregations.bucket.terms.AbstractSignificanceHeuristicTests; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.greaterThan; | ||
import static org.hamcrest.Matchers.greaterThanOrEqualTo; | ||
import static org.hamcrest.Matchers.lessThan; | ||
import static org.hamcrest.Matchers.lessThanOrEqualTo; | ||
|
||
public class MutualInformationTests extends AbstractSignificanceHeuristicTests { | ||
@Override | ||
protected SignificanceHeuristic getHeuristic() { | ||
return new MutualInformation(randomBoolean(), randomBoolean()); | ||
} | ||
|
||
@Override | ||
protected boolean testZeroScore() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void testAssertions() { | ||
testBackgroundAssertions(new MutualInformation(true, true), new MutualInformation(true, false)); | ||
} | ||
|
||
public void testScoreMutual() { | ||
SignificanceHeuristic heuristic = new MutualInformation(true, true); | ||
assertThat(heuristic.getScore(1, 1, 1, 3), greaterThan(0.0)); | ||
assertThat(heuristic.getScore(1, 1, 2, 3), lessThan(heuristic.getScore(1, 1, 1, 3))); | ||
assertThat(heuristic.getScore(2, 2, 2, 4), equalTo(1.0)); | ||
assertThat(heuristic.getScore(0, 2, 2, 4), equalTo(1.0)); | ||
assertThat(heuristic.getScore(2, 2, 4, 4), equalTo(0.0)); | ||
assertThat(heuristic.getScore(1, 2, 2, 4), equalTo(0.0)); | ||
assertThat(heuristic.getScore(3, 6, 9, 18), equalTo(0.0)); | ||
|
||
double score = 0.0; | ||
try { | ||
long a = randomLong(); | ||
long b = randomLong(); | ||
long c = randomLong(); | ||
long d = randomLong(); | ||
score = heuristic.getScore(a, b, c, d); | ||
} catch (IllegalArgumentException e) { | ||
} | ||
assertThat(score, lessThanOrEqualTo(1.0)); | ||
assertThat(score, greaterThanOrEqualTo(0.0)); | ||
heuristic = new MutualInformation(false, true); | ||
assertThat(heuristic.getScore(0, 1, 2, 3), equalTo(Double.NEGATIVE_INFINITY)); | ||
|
||
heuristic = new MutualInformation(true, false); | ||
score = heuristic.getScore(2, 3, 1, 4); | ||
assertThat(score, greaterThanOrEqualTo(0.0)); | ||
assertThat(score, lessThanOrEqualTo(1.0)); | ||
score = heuristic.getScore(1, 4, 2, 3); | ||
assertThat(score, greaterThanOrEqualTo(0.0)); | ||
assertThat(score, lessThanOrEqualTo(1.0)); | ||
score = heuristic.getScore(1, 3, 4, 4); | ||
assertThat(score, greaterThanOrEqualTo(0.0)); | ||
assertThat(score, lessThanOrEqualTo(1.0)); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
...va/org/elasticsearch/search/aggregations/bucket/terms/heuristic/PercentageScoreTests.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,28 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.search.aggregations.bucket.terms.heuristic; | ||
|
||
import org.elasticsearch.search.aggregations.bucket.terms.AbstractSignificanceHeuristicTests; | ||
|
||
public class PercentageScoreTests extends AbstractSignificanceHeuristicTests { | ||
@Override | ||
protected SignificanceHeuristic getHeuristic() { | ||
return new PercentageScore(); | ||
} | ||
|
||
@Override | ||
protected boolean testZeroScore() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void testAssertions() { | ||
testAssertions(new PercentageScore()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpicking, but multi-line comments should use the
/*
block comment style