-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #596 from mdekstrand/feature/pydantic-config
Use Pydantic models for component configurations
- Loading branch information
Showing
66 changed files
with
1,227 additions
and
986 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
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 |
---|---|---|
|
@@ -22,3 +22,6 @@ RecSys | |
PyArrow | ||
Numba | ||
DuckDB | ||
ItemList | ||
Pydantic | ||
dataclass |
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
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 |
---|---|---|
|
@@ -17,4 +17,4 @@ Entity Identifiers | |
Containers | ||
~~~~~~~~~~ | ||
|
||
.. autoclass:: UITuple | ||
.. autoclass:: UIPair |
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
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
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
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
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
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,43 @@ | ||
from pydantic import BaseModel | ||
|
||
from lenskit.data import ItemList | ||
from lenskit.pipeline import Component | ||
|
||
|
||
class LinearBlendConfig(BaseModel): | ||
"Configuration for :class:`LinearBlendScorer`." | ||
|
||
# define the parameter with a type, default value, and docstring. | ||
mix_weight: float = 0.5 | ||
r""" | ||
Linear blending mixture weight :math:`\alpha`. | ||
""" | ||
|
||
|
||
class LinearBlendScorer(Component): | ||
r""" | ||
Score items with a linear blend of two other scores. | ||
Given a mixture weight :math:`\alpha` and two scores | ||
:math:`s_i^{\mathrm{left}}` and :math:`s_i^{\mathrm{right}}`, this | ||
computes :math:`s_i = \alpha s_i^{\mathrm{left}} + (1 - \alpha) | ||
s_i^{\mathrm{right}}`. Missing values propagate, so only items | ||
scored in both inputs have scores in the output. | ||
""" | ||
|
||
# define the configuration attribute, with a docstring to make sure | ||
# it shows up in component docs. | ||
config: LinearBlendConfig | ||
"Configuration parameters for the linear blend." | ||
|
||
# the __call__ method defines the component's operation | ||
def __call__(self, left: ItemList, right: ItemList) -> ItemList: | ||
""" | ||
Blend the scores of two item lists. | ||
""" | ||
ls = left.scores("pandas", index="ids") | ||
rs = right.scores("pandas", index="ids") | ||
ls, rs = ls.align(rs) | ||
alpha = self.config.mix_weight | ||
combined = ls * alpha + rs * (1 - alpha) | ||
return ItemList(item_ids=combined.index, scores=combined.values) |
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
Oops, something went wrong.