Skip to content

Commit

Permalink
update pyterrier dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjleo committed Dec 15, 2024
1 parent b84757e commit de010fc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ classifiers = [
]

[project.optional-dependencies]
pyterrier = ["python-terrier>=0.10.0, <1"]
pyterrier = ["python-terrier>=0.12.0, <0.13"]

[project.urls]
Repository = "https://github.com/mrjleo/fast-forward-indexes"
Expand Down
19 changes: 8 additions & 11 deletions src/fast_forward/util/pyterrier.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ def __init__(self, index: "Index") -> None:
self._index = index
super().__init__()

def transform(self, topics_or_res: "pd.DataFrame") -> "pd.DataFrame":
def transform(self, inp: "pd.DataFrame") -> "pd.DataFrame":
"""Compute the scores for all query-document pairs in the data frame.
The previous scores are moved to the "score_0" column.
:param topics_or_res: The PyTerrier data frame.
:param inp: The PyTerrier data frame.
:return: A data frame with the computed scores.
"""
ff_scores = self._index(
Ranking(
topics_or_res.rename(columns={"qid": "q_id", "docno": "id"}),
inp.rename(columns={"qid": "q_id", "docno": "id"}),
copy=False,
is_sorted=True,
)
)._df.rename(columns={"q_id": "qid", "id": "docno"})

result = topics_or_res[["qid", "docno", "score"]].merge(
result = inp[["qid", "docno", "score"]].merge(
ff_scores[["qid", "docno", "score", "query"]],
on=["qid", "docno"],
suffixes=("_0", None),
Expand Down Expand Up @@ -71,15 +71,12 @@ def __init__(self, alpha: float) -> None:
self.alpha = alpha
super().__init__()

def transform(self, topics_or_res: "pd.DataFrame") -> "pd.DataFrame":
def transform(self, inp: "pd.DataFrame") -> "pd.DataFrame":
"""Interpolate the scores as `alpha * score_0 + (1 - alpha) * score`.
:param topics_or_res: The PyTerrier data frame.
:param inp: The PyTerrier data frame.
:return: A data frame with the interpolated scores.
"""
new_df = topics_or_res[["qid", "docno", "query"]].copy()
new_df["score"] = (
self.alpha * topics_or_res["score_0"]
+ (1 - self.alpha) * topics_or_res["score"]
)
new_df = inp[["qid", "docno", "query"]].copy()
new_df["score"] = self.alpha * inp["score_0"] + (1 - self.alpha) * inp["score"]
return pt.model.add_ranks(new_df, single_query=False)

0 comments on commit de010fc

Please sign in to comment.