Skip to content

Commit

Permalink
Merge pull request bst-mug#94 from michelole/issue-45
Browse files Browse the repository at this point in the history
Issue 45
  • Loading branch information
michelole authored Jul 7, 2017
2 parents d9c2caf + 11f12c9 commit cff6b49
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 45 deletions.
87 changes: 53 additions & 34 deletions src/main/java/at/medunigraz/imi/bst/trec/RunnerDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,49 +27,68 @@ public class RunnerDemo {
private static final Logger LOG = LogManager.getLogger();

public static void main(String[] args) {
final String[] runIds = { "example", "extra" };
final String suffix = "pmid";

final File template = new File(RunnerDemo.class.getResource("/templates/must-match-disease.json").getFile());
String[] pmRuns = { "example-pmid", "extra-pmid" };

final File pmTemplate = new File(RunnerDemo.class.getResource("/templates/must-match-disease.json").getFile());
Gene.Field[] expandTo = { Gene.Field.SYMBOL, Gene.Field.DESCRIPTION };
Query decorator = new WordRemovalQueryDecorator(new GeneExpanderQueryDecorator(expandTo,
new TemplateQueryDecorator(template, new ElasticSearchQuery())));
Query pmDecorator = new WordRemovalQueryDecorator(new GeneExpanderQueryDecorator(expandTo,
new TemplateQueryDecorator(pmTemplate, new ElasticSearchQuery("trec"))));

for (String id : runIds) {
LOG.info("Running collection '" + id + "'...");
File example = new File(CSVStatsWriter.class.getResource("/topics/" + id + ".xml").getPath());
TopicSet topicSet = new TopicSet(example);
for (String id : pmRuns) {
runExperiment(id, pmDecorator);
}


// TODO DRY Issue #53
String[] ctRuns = { "extra-ct" };

File output = new File("results/" + id + "-" + suffix + ".trec_results");
TrecWriter tw = new TrecWriter(output);
final File ctTemplate = new File(RunnerDemo.class.getResource("/templates/baseline-ct.json").getFile());
Query ctDecorator = new TemplateQueryDecorator(ctTemplate, new ElasticSearchQuery("clinicaltrials"));

for (String id : ctRuns) {
runExperiment(id, ctDecorator);
}
}

private static void runExperiment(String id, Query decorator) {
final String collection = id.substring(0, id.indexOf('-'));

LOG.info("Running collection '" + id + "'...");

File example = new File(CSVStatsWriter.class.getResource("/topics/" + collection + ".xml").getPath());
TopicSet topicSet = new TopicSet(example);

// TODO DRY Issue #53
Set<ResultList> resultListSet = new HashSet<>();
for (Topic topic : topicSet.getTopics()) {
List<Result> results = decorator.query(topic);
File output = new File("results/" + id + ".trec_results");
TrecWriter tw = new TrecWriter(output);

ResultList resultList = new ResultList(topic);
resultList.setResults(results);
resultListSet.add(resultList);
}
// TODO DRY Issue #53
Set<ResultList> resultListSet = new HashSet<>();
for (Topic topic : topicSet.getTopics()) {
List<Result> results = decorator.query(topic);

tw.write(resultListSet);
tw.close();
ResultList resultList = new ResultList(topic);
resultList.setResults(results);
resultListSet.add(resultList);
}

File goldStandard = new File(
CSVStatsWriter.class.getResource("/gold-standard/" + id + "-" + suffix + ".qrels").getPath());
TrecEval te = new TrecEval(goldStandard, output);
tw.write(resultListSet);
tw.close();

LOG.debug("NDCG: " + te.getNDCG());
LOG.trace(te.getMetricsByTopic("all"));
File goldStandard = new File(
CSVStatsWriter.class.getResource("/gold-standard/" + id + ".qrels").getPath());
TrecEval te = new TrecEval(goldStandard, output);

XMLStatsWriter xsw = new XMLStatsWriter(new File("stats/" + id + "-" + suffix + ".xml"));
xsw.write(te.getMetrics());
xsw.close();
LOG.debug("NDCG: " + te.getNDCG());
LOG.trace(te.getMetricsByTopic("all"));

CSVStatsWriter csw = new CSVStatsWriter(new File("stats/" + id + "-" + suffix + ".csv"));
csw.write(te.getMetrics());
csw.close();
}
XMLStatsWriter xsw = new XMLStatsWriter(new File("stats/" + id + ".xml"));
xsw.write(te.getMetrics());
xsw.close();

CSVStatsWriter csw = new CSVStatsWriter(new File("stats/" + id + ".csv"));
csw.write(te.getMetrics());
csw.close();
}


}
2 changes: 1 addition & 1 deletion src/main/java/at/medunigraz/imi/bst/trec/model/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public Result(String id, float score) {
}

public String getId() {
return id;
return id.replace("NCT", "");
}

public float getScore() {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/at/medunigraz/imi/bst/trec/model/Topic.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import com.sun.glass.ui.View;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

Expand Down Expand Up @@ -162,6 +161,8 @@ public Map<String, String> getAttributes() {
ret.put("variant", gene); // Backwards compatibility
ret.put("demographic", demographic);
ret.put("other", other);
ret.put("sex", getSex());
ret.put("age", String.valueOf(getAge()));

return ret;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ public class ElasticSearchQuery implements Query {

private JSONObject jsonQuery;

public ElasticSearchQuery() {
private String index;

public ElasticSearchQuery(String index) {
this.index = index;
}

@Override
public List<Result> query(Topic topic) {
ElasticSearch es = new ElasticSearch();
ElasticSearch es = new ElasticSearch(index);
return es.query(jsonQuery);
}

Expand Down
14 changes: 12 additions & 2 deletions src/main/java/at/medunigraz/imi/bst/trec/search/ElasticSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ public class ElasticSearch implements SearchEngine {

private Client client = ElasticClientFactory.getClient();

private String index;

public ElasticSearch() {
this.index = "_all";
}

public ElasticSearch(String index) {
this.index = index;
}

public List<Result> query(JSONObject jsonQuery) {
QueryBuilder qb = QueryBuilders.wrapperQuery(jsonQuery.toString());
LOG.debug(JsonUtils.prettify(jsonQuery));
Expand All @@ -30,8 +40,8 @@ public List<Result> query(JSONObject jsonQuery) {
}

private List<Result> query(QueryBuilder qb) {
SearchRequestBuilder searchRequestBuilder = client.prepareSearch().setQuery(qb).setSize(1000)
.addStoredField("pubmedId");
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index).setQuery(qb).setSize(1000)
.addStoredField("_id");

SearchResponse response = searchRequestBuilder.get();
LOG.trace(JsonUtils.prettify(response.toString()));
Expand Down
53 changes: 53 additions & 0 deletions src/main/resources/templates/baseline-ct.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"bool": {
"must": [
{
"range": {
"minimum_age": {
"lte": {{age}}
}
}
},
{
"range": {
"maximum_age": {
"gte": {{age}}
}
}
},
{
"match": {
"sex": "{{sex}}"
}
}
],
"should": [
{
"multi_match": {
"query": "{{disease}}",
"fields": [
"title^2",
"summary",
"inclusion"
],
"tie_breaker": 0.3,
"type": "best_fields",
"boost": 1
}
},
{
"multi_match": {
"query": "{{gene}}",
"fields": [
"title^2",
"summary",
"inclusion"
],
"tie_breaker": 0.3,
"type": "best_fields",
"boost": 1
}
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

public class DummyElasticSearchQuery extends ElasticSearchQuery {

private Topic topic;

public DummyElasticSearchQuery() {
super("NOOP");
}

private Topic topic;

@Override
public List<Result> query(Topic topic) {
this.topic = topic;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class GeneExpanderQueryDecoratorTest extends QueryDecoratorTest {

public GeneExpanderQueryDecoratorTest() {
this.decoratedQuery = new GeneExpanderQueryDecorator(EXPAND_TO,
new TemplateQueryDecorator(template, new ElasticSearchQuery()));
new TemplateQueryDecorator(template, new ElasticSearchQuery("trec")));
this.topic = new Topic().withGene(GENE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class TemplateQueryDecoratorTest extends QueryDecoratorTest {
private final File template = new File(getClass().getResource("/templates/match-title.json").getFile());

public TemplateQueryDecoratorTest() {
this.decoratedQuery = new TemplateQueryDecorator(template, new ElasticSearchQuery());
this.decoratedQuery = new TemplateQueryDecorator(template, new ElasticSearchQuery("trec"));
this.topic = new Topic().withDisease(DISEASE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class WordRemovalQueryDecoratorTest extends QueryDecoratorTest {

public WordRemovalQueryDecoratorTest() {
this.decoratedQuery = new WordRemovalQueryDecorator(
new TemplateQueryDecorator(template, new ElasticSearchQuery()));
new TemplateQueryDecorator(template, new ElasticSearchQuery("trec")));
this.topic = new Topic().withDisease(DISEASE);
}

Expand Down

0 comments on commit cff6b49

Please sign in to comment.