forked from bst-mug/trec-2017-precision-medicine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduces Experiment class to run experiments
This refs bst-mug#53.
- Loading branch information
Michel Oleynik
committed
Jul 14, 2017
1 parent
f6a338a
commit 764ec9c
Showing
2 changed files
with
99 additions
and
62 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
82 changes: 82 additions & 0 deletions
82
src/main/java/at/medunigraz/imi/bst/trec/experiment/Experiment.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,82 @@ | ||
package at.medunigraz.imi.bst.trec.experiment; | ||
|
||
import java.io.File; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import at.medunigraz.imi.bst.trec.evaluator.TrecEval; | ||
import at.medunigraz.imi.bst.trec.evaluator.TrecWriter; | ||
import at.medunigraz.imi.bst.trec.model.Result; | ||
import at.medunigraz.imi.bst.trec.model.ResultList; | ||
import at.medunigraz.imi.bst.trec.model.Topic; | ||
import at.medunigraz.imi.bst.trec.model.TopicSet; | ||
import at.medunigraz.imi.bst.trec.query.Query; | ||
import at.medunigraz.imi.bst.trec.stats.CSVStatsWriter; | ||
import at.medunigraz.imi.bst.trec.stats.XMLStatsWriter; | ||
|
||
public class Experiment extends Thread { | ||
|
||
private static final Logger LOG = LogManager.getLogger(); | ||
|
||
private String id; | ||
private Query decorator; | ||
|
||
public Experiment() { | ||
} | ||
|
||
public Experiment withId(String id) { | ||
this.id = id; | ||
return this; | ||
} | ||
|
||
public Experiment withDecorator(Query decorator) { | ||
this.decorator = decorator; | ||
return this; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
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); | ||
|
||
File output = new File("results/" + id + ".trec_results"); | ||
TrecWriter tw = new TrecWriter(output); | ||
|
||
// TODO DRY Issue #53 | ||
Set<ResultList> resultListSet = new HashSet<>(); | ||
for (Topic topic : topicSet.getTopics()) { | ||
List<Result> results = decorator.query(topic); | ||
|
||
ResultList resultList = new ResultList(topic); | ||
resultList.setResults(results); | ||
resultListSet.add(resultList); | ||
} | ||
|
||
tw.write(resultListSet); | ||
tw.close(); | ||
|
||
File goldStandard = new File(CSVStatsWriter.class.getResource("/gold-standard/" + id + ".qrels").getPath()); | ||
TrecEval te = new TrecEval(goldStandard, output); | ||
|
||
LOG.debug("NDCG: " + te.getNDCG()); | ||
LOG.trace(te.getMetricsByTopic("all")); | ||
|
||
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(); | ||
|
||
LOG.info("Collection '" + id + "' finished."); | ||
} | ||
} |