Skip to content
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

Add fusion regression #2611

Merged
merged 34 commits into from
Oct 9, 2024
Merged
Changes from 3 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
1cb5ea1
add fusion
cadurosar Jan 26, 2024
fe7e529
Merge branch 'castorini:master' into master
cadurosar Jan 26, 2024
c626a7c
added cadurosar's code via a copy + paste and made changes to match p…
DanielKohn1208 May 1, 2024
4d702d0
merged cadurosar's code
DanielKohn1208 May 1, 2024
7db24a9
moved FuseRuns
DanielKohn1208 May 1, 2024
e7efd1b
merged cadurosar's code with modifications
DanielKohn1208 May 1, 2024
ebd8ed4
added run fusion to match pyserini implementation
DanielKohn1208 May 8, 2024
1b398af
added fusion feature
Stefan824 Sep 6, 2024
27b44df
modified arguments; added test cases
Stefan824 Sep 6, 2024
72e6e06
modified TrecRun class code style
Stefan824 Sep 6, 2024
5f7ec35
added comment
Stefan824 Sep 6, 2024
509049c
deleted test file from previous version
Stefan824 Sep 7, 2024
39f62a9
Added dependency for junit test
Stefan824 Sep 7, 2024
37e89fa
resolved formatting; merged trectools module to fusion
Stefan824 Sep 7, 2024
54c74b4
remove unused test cases
Stefan824 Sep 8, 2024
32e13c2
removed unused test files
Stefan824 Sep 8, 2024
6c648f7
Merge remote-tracking branch 'origin/master' into add-fusion
Stefan824 Sep 16, 2024
a9d7804
added fusion regression script paired with two yaml test files
Stefan824 Sep 23, 2024
e049e48
added md for test
Stefan824 Sep 23, 2024
bd0ce76
add cmd on test instruction
Stefan824 Sep 23, 2024
17ceb49
removed abundant dependency
Stefan824 Sep 23, 2024
6f550b1
revert unecessary change
Stefan824 Sep 23, 2024
f4644e1
resolved a minor decoding issue
Stefan824 Sep 23, 2024
0ea8369
added a yaml that is based on regression test run results
Stefan824 Sep 23, 2024
ec57e96
added doc for test2
Stefan824 Sep 23, 2024
042b678
typo
Stefan824 Sep 23, 2024
f2b6f4c
changed name for test yamls
Stefan824 Sep 23, 2024
d94c0f9
second attempt to revert src/main/resources/regression/beir-v1.0.0-ro…
Stefan824 Sep 24, 2024
f5871b9
fixed precision and added run_origins for fusion yaml
Stefan824 Sep 25, 2024
b7961f3
removed two yamls that use runs not from current regression experiments
Stefan824 Sep 29, 2024
ab33853
modified test instructions according to last commit
Stefan824 Sep 29, 2024
db12c79
add yaml file
Stefan824 Sep 30, 2024
9a419aa
removed old yaml
Stefan824 Sep 30, 2024
d9cff54
changed output naming
Stefan824 Oct 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
249 changes: 249 additions & 0 deletions src/main/java/io/anserini/search/FuseRuns.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
/*
* Anserini: A Lucene toolkit for reproducible information retrieval research
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.anserini.search;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.Map;
import java.util.Collections;
import java.util.Set;
import java.util.HashSet;
import java.io.Closeable;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Locale;

import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.ParserProperties;
import org.kohsuke.args4j.Option;

class FusedRunOutputWriter implements Closeable {
private final PrintWriter out;
private final String format;
private final String runtag;

public FusedRunOutputWriter(String output, String format, String runtag) throws IOException {
this.out = new PrintWriter(Files.newBufferedWriter(Paths.get(output), StandardCharsets.UTF_8));
this.format = format;
this.runtag = runtag;
}

private class Document implements Comparable<Document>{
String docid;
double score;

public Document(String docid, double score)
{
this.docid = docid;
this.score = score;
}

@Override public int compareTo(Document a)
{
return Double.compare(a.score,this.score);
}

}

public void writeTopic(String qid, HashMap<String,Double> results) {
int rank = 1;
ArrayList<Document> documents = new ArrayList<>();

for (Map.Entry<String, Double> entry : results.entrySet()) {
documents.add(new Document(entry.getKey(), entry.getValue()));
}
Collections.sort(documents);
for (Document r : documents) {
if ("msmarco".equals(format)) {
// MS MARCO output format:
out.append(String.format(Locale.US, "%s\t%s\t%d\n", qid, r.docid, rank));
} else {
// Standard TREC format:
// + the first column is the topic number.
// + the second column is currently unused and should always be "Q0".
// + the third column is the official document identifier of the retrieved document.
// + the fourth column is the rank the document is retrieved.
// + the fifth column shows the score (integer or floating point) that generated the ranking.
// + the sixth column is called the "run tag" and should be a unique identifier for your
out.append(String.format(Locale.US, "%s Q0 %s %d %f %s\n", qid, r.docid, rank, r.score, runtag));
}
rank++;
}
}

@Override
public void close() {
out.flush();
out.close();
}
}


public class FuseRuns {

public static class Args {
@Option(name = "-options", usage = "Print information about options.")
public Boolean options = false;

@Option(name = "-filename_a", metaVar = "[filename_a]", required = true, usage = "Path to the first run to fuse")
public String filename_a;

@Option(name = "-filename_b", metaVar = "[filename_b]", required = true, usage = "Path to the second run to fuse")
public String filename_b;

@Option(name = "-filename_output", metaVar = "[filename_output]", required = true, usage = "Path to save the output")
public String filename_output;

@Option(name = "-runtag", metaVar = "[runtag]", usage = "Run tag for the fusion")
public String runtag="fused";

}

public static TreeMap<String,HashMap<String,Double>> createRunMap(String filename){

TreeMap<String, HashMap<String, Double>> twoTierHashMap = new TreeMap<>();
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = br.readLine()) != null) {
String[] data = line.split(" ");
HashMap<String, Double> innerHashMap = new HashMap<String,Double>();
HashMap<String, Double> innerHashMap_ = twoTierHashMap.putIfAbsent(data[0], innerHashMap);
if (innerHashMap_ != null){
innerHashMap = innerHashMap_;
}
innerHashMap.put(data[2], Double.valueOf(data[4]));
}
} catch (FileNotFoundException ex){
System.out.println(ex);
} catch (IOException ex){
System.out.println(ex);
}
return twoTierHashMap;
}

public static void normalize_min_max(TreeMap<String, HashMap<String, Double>> hashMap) {
for (String outerKey : hashMap.keySet()) {
Map<String, Double> innerHashMap = hashMap.get(outerKey);
Double min = Double.MAX_VALUE;
Double max = -1.0;
for (String innerKey : innerHashMap.keySet()) {
Double innerValue = innerHashMap.get(innerKey);
if (innerValue < min) {
min = innerValue;
}
if (innerValue > max) {
max = innerValue;
}
}
for (String innerKey : innerHashMap.keySet()) {
Double innerValue = innerHashMap.get(innerKey);
Double newValue = (innerValue - min) / (max-min);
innerHashMap.replace(innerKey,innerValue,newValue);
}
}
}

public static HashMap<String,Double> aggregateQuery(HashMap<String, Double> hashMap1, HashMap<String, Double> hashMap2) {
HashMap<String,Double> mergedHashMap = new HashMap<String,Double>();
for (String key : hashMap1.keySet()) {
mergedHashMap.put(key, hashMap1.get(key));
}
for (String key : hashMap2.keySet()) {
Double existingValue = mergedHashMap.getOrDefault(key,0.0);
mergedHashMap.put(key, hashMap2.get(key) + existingValue);
}
return mergedHashMap;
}

public static TreeMap<String,HashMap<String, Double>> aggregateHashMap(TreeMap<String,HashMap<String, Double>> hashMap1, TreeMap<String,HashMap<String, Double>> hashMap2) {
Set<String> queries = new HashSet<String>();
TreeMap<String,HashMap<String, Double>> finalHashMap = new TreeMap<String,HashMap<String, Double>>();
for (String key : hashMap1.keySet()) {
queries.add(key);
}
for (String key : hashMap2.keySet()) {
queries.add(key);
}
Iterator<String> queryIterator = queries.iterator();
while(queryIterator.hasNext()) {
String query = queryIterator.next();
HashMap<String,Double> aggregated = aggregateQuery(hashMap1.getOrDefault(query,new HashMap<String,Double>()), hashMap2.getOrDefault(query,new HashMap<String,Double>()));
finalHashMap.put(query,aggregated);
}
return finalHashMap;
}


public static void main(String[] args) {

Args fuseArgs = new Args();
CmdLineParser parser = new CmdLineParser(fuseArgs, ParserProperties.defaults().withUsageWidth(120));

try {
parser.parseArgument(args);
} catch (CmdLineException e) {
if (fuseArgs.options) {
System.err.printf("Options for %s:\n\n", FuseRuns.class.getSimpleName());
parser.printUsage(System.err);

ArrayList<String> required = new ArrayList<String>();
parser.getOptions().forEach((option) -> {
if (option.option.required()) {
required.add(option.option.toString());
}
});

System.err.printf("\nRequired options are %s\n", required);
} else {
System.err.printf("Error: %s. For help, use \"-options\" to print out information about options.\n",
e.getMessage());
}

return;
}

try {
TreeMap<String,HashMap<String,Double>> runA = createRunMap(fuseArgs.filename_a);
TreeMap<String,HashMap<String,Double>> runB = createRunMap(fuseArgs.filename_b);
normalize_min_max(runA);
normalize_min_max(runB);
TreeMap<String,HashMap<String,Double>> finalRun = aggregateHashMap(runA,runB);

// Merge and output
FusedRunOutputWriter out = new FusedRunOutputWriter(fuseArgs.filename_output, "trec", fuseArgs.runtag);
for (String key : finalRun.keySet()) {
out.writeTopic(key, finalRun.get(key));
}
out.close();
System.out.println("File " + fuseArgs.filename_output + " successfully created!");

} catch (IOException e) {
System.out.println("Error occurred: " + e.getMessage());
}
}
}