GeezerVison is showing github.com — docs: fix cast method shown on public docs (#2436) · google/bigframes@ad0f33c
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: bigframes/ml/base.py +5-4Lines changed: 5 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 24 | 24 | """ | 25 | 25 |
| 26 | 26 | import abc | 27 | | -from typing import cast, Optional, TypeVar, Union | | 27 | +import typing | | 28 | +from typing import Optional, TypeVar, Union | 28 | 29 | import warnings | 29 | 30 |
| 30 | 31 | import bigframes_vendored.sklearn.base | @@ -133,7 +134,7 @@ def register(self: _T, vertex_ai_model_id: Optional[str] = None) -> _T: | 133 | 134 | self._bqml_model = self._create_bqml_model() # type: ignore | 134 | 135 | except AttributeError: | 135 | 136 | raise RuntimeError("A model must be trained before register.") | 136 | | - self._bqml_model = cast(core.BqmlModel, self._bqml_model) | | 137 | + self._bqml_model = typing.cast(core.BqmlModel, self._bqml_model) | 137 | 138 |
| 138 | 139 | self._bqml_model.register(vertex_ai_model_id) | 139 | 140 | return self | @@ -286,7 +287,7 @@ def _predict_and_retry( | 286 | 287 | bpd.concat([df_result, df_succ]) if df_result is not None else df_succ | 287 | 288 | ) | 288 | 289 |
| 289 | | - df_result = cast( | | 290 | + df_result = typing.cast( | 290 | 291 | bpd.DataFrame, | 291 | 292 | bpd.concat([df_result, df_fail]) if df_result is not None else df_fail, | 292 | 293 | ) | @@ -306,7 +307,7 @@ def _extract_output_names(self): | 306 | 307 |
| 307 | 308 | output_names = [] | 308 | 309 | for transform_col in self._bqml_model._model._properties["transformColumns"]: | 309 | | - transform_col_dict = cast(dict, transform_col) | | 310 | + transform_col_dict = typing.cast(dict, transform_col) | 310 | 311 | # pass the columns that are not transformed | 311 | 312 | if "transformSql" not in transform_col_dict: | 312 | 313 | continue | |
Collapse file Copy file name to clipboardExpand all lines: bigframes/ml/compose.py +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 21 | 21 | import re | 22 | 22 | import types | 23 | 23 | import typing | 24 | | -from typing import cast, Iterable, List, Optional, Set, Tuple, Union | | 24 | +from typing import Iterable, List, Optional, Set, Tuple, Union | 25 | 25 |
| 26 | 26 | from bigframes_vendored import constants | 27 | 27 | import bigframes_vendored.sklearn.compose._column_transformer | @@ -218,7 +218,7 @@ def camel_to_snake(name): | 218 | 218 |
| 219 | 219 | output_names = [] | 220 | 220 | for transform_col in bq_model._properties["transformColumns"]: | 221 | | - transform_col_dict = cast(dict, transform_col) | | 221 | + transform_col_dict = typing.cast(dict, transform_col) | 222 | 222 | # pass the columns that are not transformed | 223 | 223 | if "transformSql" not in transform_col_dict: | 224 | 224 | continue | @@ -282,7 +282,7 @@ def _merge( | 282 | 282 | return self # SQLScalarColumnTransformer only work inside ColumnTransformer | 283 | 283 | feature_columns_sorted = sorted( | 284 | 284 | [ | 285 | | - cast(str, feature_column.name) | | 285 | + typing.cast(str, feature_column.name) | 286 | 286 | for feature_column in bq_model.feature_columns | 287 | 287 | ] | 288 | 288 | ) | |
Collapse file Copy file name to clipboardExpand all lines: bigframes/ml/core.py +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 18 | 18 |
| 19 | 19 | import dataclasses | 20 | 20 | import datetime | 21 | | -from typing import Callable, cast, Iterable, Mapping, Optional, Union | | 21 | +import typing | | 22 | +from typing import Callable, Iterable, Mapping, Optional, Union | 22 | 23 | import uuid | 23 | 24 |
| 24 | 25 | from google.cloud import bigquery | @@ -376,7 +377,7 @@ def copy(self, new_model_name: str, replace: bool = False) -> BqmlModel: | 376 | 377 | def register(self, vertex_ai_model_id: Optional[str] = None) -> BqmlModel: | 377 | 378 | if vertex_ai_model_id is None: | 378 | 379 | # vertex id needs to start with letters. https://cloud.google.com/vertex-ai/docs/general/resource-naming | 379 | | - vertex_ai_model_id = "bigframes_" + cast(str, self._model.model_id) | | 380 | + vertex_ai_model_id = "bigframes_" + typing.cast(str, self._model.model_id) | 380 | 381 |
| 381 | 382 | # truncate as Vertex ID only accepts 63 characters, easily exceeding the limit for temp models. | 382 | 383 | # The possibility of conflicts should be low. | |
Collapse file Copy file name to clipboardExpand all lines: bigframes/ml/imported.py +8-7Lines changed: 8 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 16 | 16 |
| 17 | 17 | from __future__ import annotations | 18 | 18 |
| 19 | | -from typing import cast, Mapping, Optional | | 19 | +import typing | | 20 | +from typing import Mapping, Optional | 20 | 21 |
| 21 | 22 | from google.cloud import bigquery | 22 | 23 |
| @@ -78,7 +79,7 @@ def predict(self, X: utils.ArrayType) -> bpd.DataFrame: | 78 | 79 | if self.model_path is None: | 79 | 80 | raise ValueError("Model GCS path must be provided.") | 80 | 81 | self._bqml_model = self._create_bqml_model() | 81 | | - self._bqml_model = cast(core.BqmlModel, self._bqml_model) | | 82 | + self._bqml_model = typing.cast(core.BqmlModel, self._bqml_model) | 82 | 83 |
| 83 | 84 | (X,) = utils.batch_convert_to_dataframe(X) | 84 | 85 |
| @@ -99,7 +100,7 @@ def to_gbq(self, model_name: str, replace: bool = False) -> TensorFlowModel: | 99 | 100 | if self.model_path is None: | 100 | 101 | raise ValueError("Model GCS path must be provided.") | 101 | 102 | self._bqml_model = self._create_bqml_model() | 102 | | - self._bqml_model = cast(core.BqmlModel, self._bqml_model) | | 103 | + self._bqml_model = typing.cast(core.BqmlModel, self._bqml_model) | 103 | 104 |
| 104 | 105 | new_model = self._bqml_model.copy(model_name, replace) | 105 | 106 | return new_model.session.read_gbq_model(model_name) | @@ -157,7 +158,7 @@ def predict(self, X: utils.ArrayType) -> bpd.DataFrame: | 157 | 158 | if self.model_path is None: | 158 | 159 | raise ValueError("Model GCS path must be provided.") | 159 | 160 | self._bqml_model = self._create_bqml_model() | 160 | | - self._bqml_model = cast(core.BqmlModel, self._bqml_model) | | 161 | + self._bqml_model = typing.cast(core.BqmlModel, self._bqml_model) | 161 | 162 |
| 162 | 163 | (X,) = utils.batch_convert_to_dataframe(X, session=self._bqml_model.session) | 163 | 164 |
| @@ -178,7 +179,7 @@ def to_gbq(self, model_name: str, replace: bool = False) -> ONNXModel: | 178 | 179 | if self.model_path is None: | 179 | 180 | raise ValueError("Model GCS path must be provided.") | 180 | 181 | self._bqml_model = self._create_bqml_model() | 181 | | - self._bqml_model = cast(core.BqmlModel, self._bqml_model) | | 182 | + self._bqml_model = typing.cast(core.BqmlModel, self._bqml_model) | 182 | 183 |
| 183 | 184 | new_model = self._bqml_model.copy(model_name, replace) | 184 | 185 | return new_model.session.read_gbq_model(model_name) | @@ -276,7 +277,7 @@ def predict(self, X: utils.ArrayType) -> bpd.DataFrame: | 276 | 277 | if self.model_path is None: | 277 | 278 | raise ValueError("Model GCS path must be provided.") | 278 | 279 | self._bqml_model = self._create_bqml_model() | 279 | | - self._bqml_model = cast(core.BqmlModel, self._bqml_model) | | 280 | + self._bqml_model = typing.cast(core.BqmlModel, self._bqml_model) | 280 | 281 |
| 281 | 282 | (X,) = utils.batch_convert_to_dataframe(X, session=self._bqml_model.session) | 282 | 283 |
| @@ -297,7 +298,7 @@ def to_gbq(self, model_name: str, replace: bool = False) -> XGBoostModel: | 297 | 298 | if self.model_path is None: | 298 | 299 | raise ValueError("Model GCS path must be provided.") | 299 | 300 | self._bqml_model = self._create_bqml_model() | 300 | | - self._bqml_model = cast(core.BqmlModel, self._bqml_model) | | 301 | + self._bqml_model = typing.cast(core.BqmlModel, self._bqml_model) | 301 | 302 |
| 302 | 303 | new_model = self._bqml_model.copy(model_name, replace) | 303 | 304 | return new_model.session.read_gbq_model(model_name) |
Collapse file Copy file name to clipboardExpand all lines: bigframes/ml/llm.py +12-8Lines changed: 12 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 16 | 16 |
| 17 | 17 | from __future__ import annotations | 18 | 18 |
| 19 | | -from typing import cast, Iterable, Literal, Mapping, Optional, Union | | 19 | +import typing | | 20 | +from typing import Iterable, Literal, Mapping, Optional, Union | 20 | 21 | import warnings | 21 | 22 |
| 22 | 23 | import bigframes_vendored.constants as constants | @@ -252,7 +253,7 @@ def predict( | 252 | 253 |
| 253 | 254 | if len(X.columns) == 1: | 254 | 255 | # BQML identified the column by name | 255 | | - col_label = cast(blocks.Label, X.columns[0]) | | 256 | + col_label = typing.cast(blocks.Label, X.columns[0]) | 256 | 257 | X = X.rename(columns={col_label: "content"}) | 257 | 258 |
| 258 | 259 | options: dict = {} | @@ -391,7 +392,7 @@ def predict( | 391 | 392 |
| 392 | 393 | if len(X.columns) == 1: | 393 | 394 | # BQML identified the column by name | 394 | | - col_label = cast(blocks.Label, X.columns[0]) | | 395 | + col_label = typing.cast(blocks.Label, X.columns[0]) | 395 | 396 | X = X.rename(columns={col_label: "content"}) | 396 | 397 |
| 397 | 398 | # TODO(garrettwu): remove transform to ObjRefRuntime when BQML supports ObjRef as input | @@ -604,7 +605,10 @@ def fit( | 604 | 605 | options["prompt_col"] = X.columns.tolist()[0] | 605 | 606 |
| 606 | 607 | self._bqml_model = self._bqml_model_factory.create_llm_remote_model( | 607 | | - X, y, options=options, connection_name=cast(str, self.connection_name) | | 608 | + X, | | 609 | + y, | | 610 | + options=options, | | 611 | + connection_name=typing.cast(str, self.connection_name), | 608 | 612 | ) | 609 | 613 | return self | 610 | 614 |
| @@ -735,7 +739,7 @@ def predict( | 735 | 739 |
| 736 | 740 | if len(X.columns) == 1: | 737 | 741 | # BQML identified the column by name | 738 | | - col_label = cast(blocks.Label, X.columns[0]) | | 742 | + col_label = typing.cast(blocks.Label, X.columns[0]) | 739 | 743 | X = X.rename(columns={col_label: "prompt"}) | 740 | 744 |
| 741 | 745 | options: dict = { | @@ -820,8 +824,8 @@ def score( | 820 | 824 | ) | 821 | 825 |
| 822 | 826 | # BQML identified the column by name | 823 | | - X_col_label = cast(blocks.Label, X.columns[0]) | 824 | | - y_col_label = cast(blocks.Label, y.columns[0]) | | 827 | + X_col_label = typing.cast(blocks.Label, X.columns[0]) | | 828 | + y_col_label = typing.cast(blocks.Label, y.columns[0]) | 825 | 829 | X = X.rename(columns={X_col_label: "input_text"}) | 826 | 830 | y = y.rename(columns={y_col_label: "output_text"}) | 827 | 831 |
| @@ -1033,7 +1037,7 @@ def predict( | 1033 | 1037 |
| 1034 | 1038 | if len(X.columns) == 1: | 1035 | 1039 | # BQML identified the column by name | 1036 | | - col_label = cast(blocks.Label, X.columns[0]) | | 1040 | + col_label = typing.cast(blocks.Label, X.columns[0]) | 1037 | 1041 | X = X.rename(columns={col_label: "prompt"}) | 1038 | 1042 |
| 1039 | 1043 | options = { | |
Collapse file Copy file name to clipboardExpand all lines: bigframes/ml/model_selection.py +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 20 | 20 | import inspect | 21 | 21 | from itertools import chain | 22 | 22 | import time | 23 | | -from typing import cast, Generator, List, Optional, Union | | 23 | +import typing | | 24 | +from typing import Generator, List, Optional, Union | 24 | 25 |
| 25 | 26 | import bigframes_vendored.sklearn.model_selection._split as vendored_model_selection_split | 26 | 27 | import bigframes_vendored.sklearn.model_selection._validation as vendored_model_selection_validation | @@ -99,10 +100,10 @@ def _stratify_split(df: bpd.DataFrame, stratify: bpd.Series) -> List[bpd.DataFra | 99 | 100 | train_dfs.append(train) | 100 | 101 | test_dfs.append(test) | 101 | 102 |
| 102 | | - train_df = cast( | | 103 | + train_df = typing.cast( | 103 | 104 | bpd.DataFrame, bpd.concat(train_dfs).drop(columns="bigframes_stratify_col") | 104 | 105 | ) | 105 | | - test_df = cast( | | 106 | + test_df = typing.cast( | 106 | 107 | bpd.DataFrame, bpd.concat(test_dfs).drop(columns="bigframes_stratify_col") | 107 | 108 | ) | 108 | 109 | return [train_df, test_df] | |
Collapse file Copy file name to clipboardExpand all lines: bigframes/ml/preprocessing.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 18 | 18 | from __future__ import annotations | 19 | 19 |
| 20 | 20 | import typing | 21 | | -from typing import cast, Iterable, List, Literal, Optional, Union | | 21 | +from typing import Iterable, List, Literal, Optional, Union | 22 | 22 |
| 23 | 23 | import bigframes_vendored.sklearn.preprocessing._data | 24 | 24 | import bigframes_vendored.sklearn.preprocessing._discretization | @@ -470,7 +470,7 @@ def _parse_from_sql(cls, sql: str) -> tuple[OneHotEncoder, str]: | 470 | 470 | s = sql[sql.find("(") + 1 : sql.find(")")] | 471 | 471 | col_label, drop_str, top_k, frequency_threshold = s.split(", ") | 472 | 472 | drop = ( | 473 | | - cast(Literal["most_frequent"], "most_frequent") | | 473 | + typing.cast(Literal["most_frequent"], "most_frequent") | 474 | 474 | if drop_str.lower() == "'most_frequent'" | 475 | 475 | else None | 476 | 476 | ) | |
|