-
Notifications
You must be signed in to change notification settings - Fork 26
Add custom inference model #51
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
Merged
MatteoGiomi
merged 7 commits into
statice:main
from
itrajanovska:add_custom_inference_model
Dec 9, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f8f1df9
Support an ML model with an implemented predict() for inference_evalu…
itrajanovska f490f7a
Ruff - Tuple.
itrajanovska 83386c9
Add an InferencePredictor Protocol;
itrajanovska 46c3fa3
Add a custom inference model in the inference_evaluator.py;
itrajanovska fd4b88d
Lint
itrajanovska 2c40a2e
Add docstrings.
itrajanovska 3c53dfd
Keep target series only; extend docstring.
itrajanovska File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -128,7 +128,8 @@ dmypy.json | |
| # Pyre type checker | ||
| .pyre/ | ||
|
|
||
| # vscode | ||
| # IDE | ||
| .vscode/ | ||
| .idea/ | ||
|
|
||
| **/*DS_Store* | ||
| **/*DS_Store* | ||
This file contains hidden or 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 hidden or 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 hidden or 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,31 @@ | ||
| # This file is part of Anonymeter and is released under BSD 3-Clause Clear License. | ||
| # Copyright (c) 2022 Anonos IP LLC. | ||
| # See https://github.com/statice/anonymeter/blob/main/LICENSE.md for details. | ||
| """A protocol for a custom inference predictor.""" | ||
| from typing import Protocol | ||
|
|
||
| import pandas as pd | ||
|
|
||
|
|
||
| class InferencePredictor(Protocol): | ||
itrajanovska marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Interface for custom inference models. | ||
|
|
||
| It is used as `inference_model` in the InferenceEvaluator in inference_evaluator.py. | ||
|
|
||
| For an example usage refer to the SklearnInferencePredictor in sklearn_inference_predictor.py. | ||
| """ | ||
| def predict(self, x: pd.DataFrame) -> pd.Series: | ||
| """Predict the targets for input `x`. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| x : pd.DataFrame | ||
| The input data to predict. | ||
|
|
||
| Returns | ||
| ------- | ||
| pd.Series | ||
| The predictions as pd.Series. | ||
|
|
||
| """ | ||
| ... | ||
This file contains hidden or 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,44 @@ | ||
| # This file is part of Anonymeter and is released under BSD 3-Clause Clear License. | ||
| # Copyright (c) 2022 Anonos IP LLC. | ||
| # See https://github.com/statice/anonymeter/blob/main/LICENSE.md for details. | ||
| """A wrapper class around a sklearn model implementing the InferencePredictor.""" | ||
| import pandas as pd | ||
| from sklearn.base import BaseEstimator, is_classifier, is_regressor | ||
|
|
||
| from anonymeter.evaluators.inference_predictor import InferencePredictor | ||
|
|
||
|
|
||
| class SklearnInferencePredictor(InferencePredictor): | ||
| """Wrapper class to use sklearn methods in the inference evaluator. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| model : sklearn.base.BaseEstimator | ||
| A classifier or regressor which implements ::predict(). | ||
itrajanovska marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| The model needs to be fitted, it must contain its own preprocessing pipeline, | ||
| and it needs to respect the index of the input data. | ||
|
|
||
| """ | ||
| def __init__(self, model: BaseEstimator): | ||
| if not (is_classifier(estimator=model) or is_regressor(estimator=model)): | ||
| raise ValueError("Model must be classifier or regressor %s", model) | ||
| if not hasattr(model, "predict"): | ||
| raise ValueError("Model must have a predict method, %s", model) | ||
| self._model = model | ||
|
|
||
| def predict(self, x: pd.DataFrame) -> pd.Series: | ||
| """Predict the targets for input `x`. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| x : pd.DataFrame | ||
| The input data to predict. | ||
|
|
||
| Returns | ||
| ------- | ||
| pd.Series | ||
| The predictions as pd.Series. | ||
|
|
||
| """ | ||
| prediction = self._model.predict(x) | ||
| return pd.Series(prediction, index=x.index) | ||
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.