-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added forecasting snippets and fixed bugs with existing snippets (
#1210) * Added dataset snippets * Fixed typehint and missing parameter bugs as well as added new samples * Fixed lint issues * Added bq batch_prediction bq snippets * Removed unneeded fixture * Renamed bq_source to bigquery_source * Added back explain_tabular_sample.py for now * Fixed tests * Fixed lint issues
- Loading branch information
Showing
24 changed files
with
727 additions
and
31 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
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
40 changes: 40 additions & 0 deletions
40
samples/model-builder/create_and_import_dataset_time_series_bigquery_sample.py
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,40 @@ | ||
# Copyright 2022 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
from google.cloud import aiplatform | ||
|
||
|
||
# [START aiplatform_sdk_create_and_import_dataset_time_series_bigquery_sample] | ||
def create_and_import_dataset_time_series_bigquery_sample( | ||
display_name: str, | ||
project: str, | ||
location: str, | ||
bigquery_source: str, | ||
): | ||
|
||
aiplatform.init(project=project, location=location) | ||
|
||
dataset = aiplatform.TimeSeriesDataset.create( | ||
display_name=display_name, | ||
bigquery_source=bigquery_source, | ||
) | ||
|
||
dataset.wait() | ||
|
||
print(f'\tDataset: "{dataset.display_name}"') | ||
print(f'\tname: "{dataset.resource_name}"') | ||
|
||
|
||
# [END aiplatform_sdk_create_and_import_dataset_time_series_bigquery_sample] |
37 changes: 37 additions & 0 deletions
37
samples/model-builder/create_and_import_dataset_time_series_bigquery_sample_test.py
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,37 @@ | ||
# Copyright 2022 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
import create_and_import_dataset_time_series_bigquery_sample | ||
import test_constants as constants | ||
|
||
|
||
def test_create_and_import_dataset_time_series_bigquery_sample( | ||
mock_sdk_init, mock_create_time_series_dataset | ||
): | ||
|
||
create_and_import_dataset_time_series_bigquery_sample.create_and_import_dataset_time_series_bigquery_sample( | ||
project=constants.PROJECT, | ||
location=constants.LOCATION, | ||
bigquery_source=constants.BIGQUERY_SOURCE, | ||
display_name=constants.DISPLAY_NAME, | ||
) | ||
|
||
mock_sdk_init.assert_called_once_with( | ||
project=constants.PROJECT, location=constants.LOCATION | ||
) | ||
mock_create_time_series_dataset.assert_called_once_with( | ||
display_name=constants.DISPLAY_NAME, | ||
bigquery_source=constants.BIGQUERY_SOURCE, | ||
) |
41 changes: 41 additions & 0 deletions
41
samples/model-builder/create_and_import_dataset_time_series_gcs_sample.py
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,41 @@ | ||
# Copyright 2022 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import List, Union | ||
|
||
from google.cloud import aiplatform | ||
|
||
|
||
# [START aiplatform_sdk_create_and_import_dataset_time_series_gcs_sample] | ||
def create_and_import_dataset_time_series_gcs_sample( | ||
display_name: str, | ||
project: str, | ||
location: str, | ||
gcs_source: Union[str, List[str]], | ||
): | ||
|
||
aiplatform.init(project=project, location=location) | ||
|
||
dataset = aiplatform.TimeSeriesDataset.create( | ||
display_name=display_name, | ||
gcs_source=gcs_source, | ||
) | ||
|
||
dataset.wait() | ||
|
||
print(f'\tDataset: "{dataset.display_name}"') | ||
print(f'\tname: "{dataset.resource_name}"') | ||
|
||
|
||
# [END aiplatform_sdk_create_and_import_dataset_time_series_gcs_sample] |
37 changes: 37 additions & 0 deletions
37
samples/model-builder/create_and_import_dataset_time_series_gcs_sample_test.py
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,37 @@ | ||
# Copyright 2022 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
import create_and_import_dataset_time_series_gcs_sample | ||
import test_constants as constants | ||
|
||
|
||
def test_create_and_import_dataset_time_series_gcs_sample( | ||
mock_sdk_init, mock_create_time_series_dataset | ||
): | ||
|
||
create_and_import_dataset_time_series_gcs_sample.create_and_import_dataset_time_series_gcs_sample( | ||
project=constants.PROJECT, | ||
location=constants.LOCATION, | ||
gcs_source=constants.GCS_SOURCES, | ||
display_name=constants.DISPLAY_NAME, | ||
) | ||
|
||
mock_sdk_init.assert_called_once_with( | ||
project=constants.PROJECT, location=constants.LOCATION | ||
) | ||
mock_create_time_series_dataset.assert_called_once_with( | ||
display_name=constants.DISPLAY_NAME, | ||
gcs_source=constants.GCS_SOURCES, | ||
) |
47 changes: 47 additions & 0 deletions
47
samples/model-builder/create_batch_prediction_job_bigquery_sample.py
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,47 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from google.cloud import aiplatform | ||
|
||
|
||
# [START aiplatform_sdk_create_batch_prediction_job_bigquery_sample] | ||
def create_batch_prediction_job_bigquery_sample( | ||
project: str, | ||
location: str, | ||
model_resource_name: str, | ||
job_display_name: str, | ||
bigquery_source: str, | ||
bigquery_destination_prefix: str, | ||
sync: bool = True, | ||
): | ||
aiplatform.init(project=project, location=location) | ||
|
||
my_model = aiplatform.Model(model_resource_name) | ||
|
||
batch_prediction_job = my_model.batch_predict( | ||
job_display_name=job_display_name, | ||
bigquery_source=bigquery_source, | ||
bigquery_destination_prefix=bigquery_destination_prefix, | ||
sync=sync, | ||
) | ||
|
||
batch_prediction_job.wait() | ||
|
||
print(batch_prediction_job.display_name) | ||
print(batch_prediction_job.resource_name) | ||
print(batch_prediction_job.state) | ||
return batch_prediction_job | ||
|
||
|
||
# [END aiplatform_sdk_create_batch_prediction_job_bigquery_sample] |
42 changes: 42 additions & 0 deletions
42
samples/model-builder/create_batch_prediction_job_bigquery_sample_test.py
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,42 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
import create_batch_prediction_job_bigquery_sample | ||
import test_constants as constants | ||
|
||
|
||
def test_create_batch_prediction_job_bigquery_sample( | ||
mock_sdk_init, mock_model, mock_init_model, mock_batch_predict_model | ||
): | ||
|
||
create_batch_prediction_job_bigquery_sample.create_batch_prediction_job_bigquery_sample( | ||
project=constants.PROJECT, | ||
location=constants.LOCATION, | ||
model_resource_name=constants.MODEL_NAME, | ||
job_display_name=constants.DISPLAY_NAME, | ||
bigquery_source=constants.BIGQUERY_SOURCE, | ||
bigquery_destination_prefix=constants.BIGQUERY_DESTINATION_PREFIX, | ||
) | ||
|
||
mock_sdk_init.assert_called_once_with( | ||
project=constants.PROJECT, location=constants.LOCATION | ||
) | ||
mock_init_model.assert_called_once_with(constants.MODEL_NAME) | ||
mock_batch_predict_model.assert_called_once_with( | ||
job_display_name=constants.DISPLAY_NAME, | ||
bigquery_source=constants.BIGQUERY_SOURCE, | ||
bigquery_destination_prefix=constants.BIGQUERY_DESTINATION_PREFIX, | ||
sync=True, | ||
) |
Oops, something went wrong.