GeezerVison is showing github.com — chore: add sample tests for Training & Experiments Integration · Russ76/python-aiplatform@13190e0
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: samples/model-builder/conftest.py +22-2Lines changed: 22 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -372,6 +372,19 @@ def mock_get_custom_job(mock_custom_job): | 372 | 372 | yield mock | 373 | 373 |
| 374 | 374 |
| | 375 | +@pytest.fixture | | 376 | +def mock_get_custom_job_from_local_script(mock_custom_job): | | 377 | + with patch.object(aiplatform.CustomJob, "from_local_script") as mock: | | 378 | + mock.return_value = mock_custom_job | | 379 | + yield mock | | 380 | + | | 381 | + | | 382 | +@pytest.fixture | | 383 | +def mock_run_custom_job(mock_custom_job): | | 384 | + with patch.object(mock_custom_job, "run") as mock: | | 385 | + yield mock | | 386 | + | | 387 | + | 375 | 388 | """ | 376 | 389 | ---------------------------------------------------------------------------- | 377 | 390 | Model Fixtures | @@ -433,8 +446,15 @@ def mock_create_batch_prediction_job(): | 433 | 446 |
| 434 | 447 |
| 435 | 448 | @pytest.fixture | 436 | | -def mock_create_tensorboard(): | 437 | | - with patch.object(aiplatform.tensorboard.Tensorboard, "create") as mock: | | 449 | +def mock_tensorboard(): | | 450 | + mock = MagicMock(aiplatform.Tensorboard) | | 451 | + yield mock | | 452 | + | | 453 | + | | 454 | +@pytest.fixture | | 455 | +def mock_create_tensorboard(mock_tensorboard): | | 456 | + with patch.object(aiplatform.Tensorboard, "create") as mock: | | 457 | + mock.return_value = mock_tensorboard | 438 | 458 | yield mock | 439 | 459 |
| 440 | 460 |
| |
Collapse file Copy file name to clipboard +53Lines changed: 53 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Copyright 2023 Google LLC | | 2 | +# | | 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); | | 4 | +# you may not use this file except in compliance with the License. | | 5 | +# You may obtain a copy of the License at | | 6 | +# | | 7 | +# https://www.apache.org/licenses/LICENSE-2.0 | | 8 | +# | | 9 | +# Unless required by applicable law or agreed to in writing, software | | 10 | +# distributed under the License is distributed on an "AS IS" BASIS, | | 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | | 12 | +# See the License for the specific language governing permissions and | | 13 | +# limitations under the License. | | 14 | + | | 15 | +from typing import Optional | | 16 | + | | 17 | +from google.cloud import aiplatform | | 18 | + | | 19 | + | | 20 | +# [START aiplatform_sdk_create_custom_job_with_experiment_autologging_sample] | | 21 | +def create_custom_job_with_experiment_autologging_sample( | | 22 | + project: str, | | 23 | + location: str, | | 24 | + staging_bucket: str, | | 25 | + display_name: str, | | 26 | + script_path: str, | | 27 | + container_uri: str, | | 28 | + service_account: str, | | 29 | + experiment: str, | | 30 | + experiment_run: Optional[str] = None, | | 31 | +) -> None: | | 32 | + aiplatform.init(project=project, location=location, staging_bucket=staging_bucket) | | 33 | + | | 34 | + # Ignore the next two lines of code if the experiment you are using already | | 35 | + # has backing tensorboard instance. | | 36 | + tb_instance = aiplatform.Tensorboard.create() | | 37 | + aiplatform.init(experiment=experiment, experiment_tensorboard=tb_instance) | | 38 | + | | 39 | + job = aiplatform.CustomJob.from_local_script( | | 40 | + display_name=display_name, | | 41 | + script_path=script_path, | | 42 | + container_uri=container_uri, | | 43 | + enable_autolog=True, | | 44 | + ) | | 45 | + | | 46 | + job.run( | | 47 | + service_account=service_account, | | 48 | + experiment=experiment, | | 49 | + experiment_run=experiment_run, | | 50 | + ) | | 51 | + | | 52 | + | | 53 | +# [END aiplatform_sdk_create_custom_job_with_experiment_autologging_sample] |
Collapse file Copy file name to clipboard +63Lines changed: 63 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Copyright 2023 Google LLC | | 2 | +# | | 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); | | 4 | +# you may not use this file except in compliance with the License. | | 5 | +# You may obtain a copy of the License at | | 6 | +# | | 7 | +# https://www.apache.org/licenses/LICENSE-2.0 | | 8 | +# | | 9 | +# Unless required by applicable law or agreed to in writing, software | | 10 | +# distributed under the License is distributed on an "AS IS" BASIS, | | 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | | 12 | +# See the License for the specific language governing permissions and | | 13 | +# limitations under the License. | | 14 | + | | 15 | + | | 16 | +import create_custom_job_with_experiment_autologging_sample | | 17 | +import test_constants as constants | | 18 | + | | 19 | + | | 20 | +def test_create_custom_job_with_experiment_autologging_sample( | | 21 | + mock_sdk_init, | | 22 | + mock_create_tensorboard, | | 23 | + mock_get_custom_job_from_local_script, | | 24 | + mock_run_custom_job, | | 25 | + mock_tensorboard, | | 26 | +): | | 27 | + create_custom_job_with_experiment_autologging_sample.create_custom_job_with_experiment_autologging_sample( | | 28 | + project=constants.PROJECT, | | 29 | + location=constants.LOCATION, | | 30 | + staging_bucket=constants.STAGING_BUCKET, | | 31 | + display_name=constants.DISPLAY_NAME, | | 32 | + script_path=constants.SCRIPT_PATH, | | 33 | + container_uri=constants.CONTAINER_URI, | | 34 | + service_account=constants.SERVICE_ACCOUNT, | | 35 | + experiment=constants.EXPERIMENT_NAME, | | 36 | + experiment_run=constants.EXPERIMENT_RUN_NAME, | | 37 | + ) | | 38 | + | | 39 | + mock_sdk_init.assert_any_call( | | 40 | + project=constants.PROJECT, | | 41 | + location=constants.LOCATION, | | 42 | + staging_bucket=constants.STAGING_BUCKET, | | 43 | + ) | | 44 | + | | 45 | + mock_create_tensorboard.assert_called_once() | | 46 | + | | 47 | + mock_sdk_init.assert_any_call( | | 48 | + experiment=constants.EXPERIMENT_NAME, | | 49 | + experiment_tensorboard=mock_tensorboard, | | 50 | + ) | | 51 | + | | 52 | + mock_get_custom_job_from_local_script.assert_called_once_with( | | 53 | + display_name=constants.DISPLAY_NAME, | | 54 | + script_path=constants.SCRIPT_PATH, | | 55 | + container_uri=constants.CONTAINER_URI, | | 56 | + enable_autolog=True, | | 57 | + ) | | 58 | + | | 59 | + mock_run_custom_job.assert_called_once_with( | | 60 | + service_account=constants.SERVICE_ACCOUNT, | | 61 | + experiment=constants.EXPERIMENT_NAME, | | 62 | + experiment_run=constants.EXPERIMENT_RUN_NAME, | | 63 | + ) |
Collapse file Copy file name to clipboard +47Lines changed: 47 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Copyright 2023 Google LLC | | 2 | +# | | 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); | | 4 | +# you may not use this file except in compliance with the License. | | 5 | +# You may obtain a copy of the License at | | 6 | +# | | 7 | +# https://www.apache.org/licenses/LICENSE-2.0 | | 8 | +# | | 9 | +# Unless required by applicable law or agreed to in writing, software | | 10 | +# distributed under the License is distributed on an "AS IS" BASIS, | | 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | | 12 | +# See the License for the specific language governing permissions and | | 13 | +# limitations under the License. | | 14 | + | | 15 | +from typing import Optional | | 16 | + | | 17 | +from google.cloud import aiplatform | | 18 | + | | 19 | + | | 20 | +# [START aiplatform_sdk_create_custom_job_with_experiment_sample] | | 21 | +def create_custom_job_with_experiment_sample( | | 22 | + project: str, | | 23 | + location: str, | | 24 | + staging_bucket: str, | | 25 | + display_name: str, | | 26 | + script_path: str, | | 27 | + container_uri: str, | | 28 | + service_account: str, | | 29 | + experiment: str, | | 30 | + experiment_run: Optional[str] = None, | | 31 | +) -> None: | | 32 | + aiplatform.init(project=project, location=location, staging_bucket=staging_bucket) | | 33 | + | | 34 | + job = aiplatform.CustomJob.from_local_script( | | 35 | + display_name=display_name, | | 36 | + script_path=script_path, | | 37 | + container_uri=container_uri, | | 38 | + ) | | 39 | + | | 40 | + job.run( | | 41 | + service_account=service_account, | | 42 | + experiment=experiment, | | 43 | + experiment_run=experiment_run, | | 44 | + ) | | 45 | + | | 46 | + | | 47 | +# [END aiplatform_sdk_create_custom_job_with_experiment_sample] |
Collapse file Copy file name to clipboard +53Lines changed: 53 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Copyright 2023 Google LLC | | 2 | +# | | 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); | | 4 | +# you may not use this file except in compliance with the License. | | 5 | +# You may obtain a copy of the License at | | 6 | +# | | 7 | +# https://www.apache.org/licenses/LICENSE-2.0 | | 8 | +# | | 9 | +# Unless required by applicable law or agreed to in writing, software | | 10 | +# distributed under the License is distributed on an "AS IS" BASIS, | | 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | | 12 | +# See the License for the specific language governing permissions and | | 13 | +# limitations under the License. | | 14 | + | | 15 | + | | 16 | +import create_custom_job_with_experiment_sample | | 17 | +import test_constants as constants | | 18 | + | | 19 | + | | 20 | +def test_create_custom_job_with_experiment_sample( | | 21 | + mock_sdk_init, | | 22 | + mock_get_custom_job_from_local_script, | | 23 | + mock_run_custom_job, | | 24 | +): | | 25 | + create_custom_job_with_experiment_sample.create_custom_job_with_experiment_sample( | | 26 | + project=constants.PROJECT, | | 27 | + location=constants.LOCATION, | | 28 | + staging_bucket=constants.STAGING_BUCKET, | | 29 | + display_name=constants.DISPLAY_NAME, | | 30 | + script_path=constants.SCRIPT_PATH, | | 31 | + container_uri=constants.CONTAINER_URI, | | 32 | + service_account=constants.SERVICE_ACCOUNT, | | 33 | + experiment=constants.EXPERIMENT_NAME, | | 34 | + experiment_run=constants.EXPERIMENT_RUN_NAME, | | 35 | + ) | | 36 | + | | 37 | + mock_sdk_init.assert_called_once_with( | | 38 | + project=constants.PROJECT, | | 39 | + location=constants.LOCATION, | | 40 | + staging_bucket=constants.STAGING_BUCKET, | | 41 | + ) | | 42 | + | | 43 | + mock_get_custom_job_from_local_script.assert_called_once_with( | | 44 | + display_name=constants.DISPLAY_NAME, | | 45 | + script_path=constants.SCRIPT_PATH, | | 46 | + container_uri=constants.CONTAINER_URI, | | 47 | + ) | | 48 | + | | 49 | + mock_run_custom_job.assert_called_once_with( | | 50 | + service_account=constants.SERVICE_ACCOUNT, | | 51 | + experiment=constants.EXPERIMENT_NAME, | | 52 | + experiment_run=constants.EXPERIMENT_RUN_NAME, | | 53 | + ) |
Collapse file Copy file name to clipboardExpand all lines: samples/model-builder/test_constants.py +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 36 | 36 | EXPERIMENT_NAME = "fraud-detection-trial-72" | 37 | 37 | CREDENTIALS = credentials.AnonymousCredentials() | 38 | 38 |
| | 39 | + | | 40 | + | 39 | 41 | RESOURCE_ID = str(randint(10000000, 99999999)) # Create random resource ID | 40 | 42 | RESOURCE_ID_2 = str(randint(10000000, 99999999)) | 41 | 43 |
| |
|