-
Notifications
You must be signed in to change notification settings - Fork 63
refactor: ml model load read from class type hints #656
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
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f2704f1
refactor: ml model load read from class type hints
GarrettWu da19a27
exclude unrelated files
GarrettWu 13c1b85
fix NoneType
GarrettWu a94b8d9
fix tests
GarrettWu 4e2559d
fix tests
GarrettWu 4b57c9e
fix param mappings
GarrettWu 90f0001
Merge remote-tracking branch 'github/main' into garrettwu-fix
GarrettWu cc509db
fix tests
GarrettWu 7a60375
Merge remote-tracking branch 'github/main' into garrettwu-fix
GarrettWu 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
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 |
|---|---|---|
|
|
@@ -27,6 +27,8 @@ | |
| from bigframes.ml import base, core, globals, utils | ||
| import bigframes.pandas as bpd | ||
|
|
||
| _BQML_PARAMS_MAPPING = {"svd_solver": "pcaSolver"} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one isn't a new adding, but moved piece. |
||
|
|
||
|
|
||
| @log_adapter.class_logger | ||
| class PCA( | ||
|
|
@@ -47,23 +49,22 @@ def __init__( | |
| self._bqml_model_factory = globals.bqml_model_factory() | ||
|
|
||
| @classmethod | ||
| def _from_bq(cls, session: bigframes.Session, model: bigquery.Model) -> PCA: | ||
| assert model.model_type == "PCA" | ||
| def _from_bq(cls, session: bigframes.Session, bq_model: bigquery.Model) -> PCA: | ||
| assert bq_model.model_type == "PCA" | ||
|
|
||
| kwargs: dict = {} | ||
| kwargs = utils.retrieve_params_from_bq_model( | ||
| cls, bq_model, _BQML_PARAMS_MAPPING | ||
| ) | ||
|
|
||
| # See https://cloud.google.com/bigquery/docs/reference/rest/v2/models#trainingrun | ||
| last_fitting = model.training_runs[-1]["trainingOptions"] | ||
| last_fitting = bq_model.training_runs[-1]["trainingOptions"] | ||
| if "numPrincipalComponents" in last_fitting: | ||
| kwargs["n_components"] = int(last_fitting["numPrincipalComponents"]) | ||
| if "pcaExplainedVarianceRatio" in last_fitting: | ||
| elif "pcaExplainedVarianceRatio" in last_fitting: | ||
| kwargs["n_components"] = float(last_fitting["pcaExplainedVarianceRatio"]) | ||
| if "pcaSolver" in last_fitting: | ||
| kwargs["svd_solver"] = str(last_fitting["pcaSolver"]) | ||
|
|
||
| new_pca = cls(**kwargs) | ||
| new_pca._bqml_model = core.BqmlModel(session, model) | ||
| return new_pca | ||
| model = cls(**kwargs) | ||
| model._bqml_model = core.BqmlModel(session, bq_model) | ||
| return model | ||
|
|
||
| @property | ||
| def _bqml_options(self) -> dict: | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add the unit test for all these newly-added params. Same for all other BQML models, but feel free to address in a follow-up PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, will do a follow up.