Skip to content

Commit

Permalink
feat: MBSDK Tabular samples (#338)
Browse files Browse the repository at this point in the history
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

Tracking Bug: [MB SDK Samples - Milestone 1](https://buganizer.corp.google.com/issues/180729765)
- [ ] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/python-aiplatform/issues/new/choose) before writing your code!  That way we can discuss the change, evaluate designs, and agree on the general idea
- [ ] Ensure the tests and linter pass
- [ ] Code coverage does not decrease (if any source code was changed)
- [ ] Appropriate docs were updated (if necessary)

Fixes #<issue_number_goes_here> 🦕
  • Loading branch information
aribray committed Apr 30, 2021
1 parent c057083 commit 4241738
Show file tree
Hide file tree
Showing 23 changed files with 693 additions and 31 deletions.
77 changes: 65 additions & 12 deletions samples/model-builder/conftest.py
Expand Up @@ -138,17 +138,58 @@ def mock_import_text_dataset(mock_text_dataset):


@pytest.fixture
def mock_init_automl_image_training_job():
with patch.object(
aiplatform.training_jobs.AutoMLImageTrainingJob, "__init__"
) as mock:
mock.return_value = None
def mock_custom_training_job():
mock = MagicMock(aiplatform.training_jobs.CustomTrainingJob)
yield mock


@pytest.fixture
def mock_image_training_job():
mock = MagicMock(aiplatform.training_jobs.AutoMLImageTrainingJob)
yield mock


@pytest.fixture
def mock_tabular_training_job():
mock = MagicMock(aiplatform.training_jobs.AutoMLTabularTrainingJob)
yield mock


@pytest.fixture
def mock_text_training_job():
mock = MagicMock(aiplatform.training_jobs.AutoMLTextTrainingJob)
yield mock


@pytest.fixture
def mock_video_training_job():
mock = MagicMock(aiplatform.training_jobs.AutoMLVideoTrainingJob)
yield mock


@pytest.fixture
def mock_get_automl_tabular_training_job(mock_tabular_training_job):
with patch.object(aiplatform, "AutoMLTabularTrainingJob") as mock:
mock.return_value = mock_tabular_training_job
yield mock


@pytest.fixture
def mock_run_automl_tabular_training_job(mock_tabular_training_job):
with patch.object(mock_tabular_training_job, "run") as mock:
yield mock


@pytest.fixture
def mock_get_automl_image_training_job(mock_image_training_job):
with patch.object(aiplatform, "AutoMLImageTrainingJob") as mock:
mock.return_value = mock_image_training_job
yield mock


@pytest.fixture
def mock_run_automl_image_training_job():
with patch.object(aiplatform.training_jobs.AutoMLImageTrainingJob, "run") as mock:
def mock_run_automl_image_training_job(mock_image_training_job):
with patch.object(mock_image_training_job, "run") as mock:
yield mock


Expand All @@ -173,15 +214,21 @@ def mock_run_custom_training_job():


@pytest.fixture
def mock_init_model():
with patch.object(aiplatform.models.Model, "__init__") as mock:
mock.return_value = None
def mock_model():
mock = MagicMock(aiplatform.models.Model)
yield mock


@pytest.fixture
def mock_get_model(mock_model):
with patch.object(aiplatform, "Model") as mock:
mock.return_value = mock_model
yield mock


@pytest.fixture
def mock_batch_predict_model():
with patch.object(aiplatform.models.Model, "batch_predict") as mock:
def mock_batch_predict_model(mock_model):
with patch.object(mock_model, "batch_predict") as mock:
yield mock


Expand Down Expand Up @@ -211,6 +258,12 @@ def mock_endpoint():
yield mock


@pytest.fixture
def mock_create_endpoint():
with patch.object(aiplatform.Endpoint, "create") as mock:
yield mock


@pytest.fixture
def mock_get_endpoint(mock_endpoint):
with patch.object(aiplatform, "Endpoint") as mock_get_endpoint:
Expand Down
@@ -0,0 +1,36 @@
# 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_and_import_dataset_tabular_bigquery_sample]
def create_and_import_dataset_tabular_bigquery_sample(
display_name: str, project: str, location: str, bigquery_source: str,
):

aiplatform.init(project=project, location=location)

dataset = aiplatform.TabularDataset.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_tabular_bigquery_sample]
@@ -0,0 +1,36 @@
# 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_and_import_dataset_tabular_bigquery_sample
import test_constants as constants


def test_create_and_import_dataset_tabular_bigquery_sample(
mock_sdk_init, mock_create_tabular_dataset
):

create_and_import_dataset_tabular_bigquery_sample.create_and_import_dataset_tabular_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_tabular_dataset.assert_called_once_with(
display_name=constants.DISPLAY_NAME, bigquery_source=constants.BIGQUERY_SOURCE,
)
@@ -0,0 +1,37 @@
# 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 typing import List, Union

from google.cloud import aiplatform


# [START aiplatform_sdk_create_and_import_dataset_tabular_gcs_sample]
def create_and_import_dataset_tabular_gcs_sample(
display_name: str, project: str, location: str, gcs_source: Union[str, List[str]],
):

aiplatform.init(project=project, location=location)

dataset = aiplatform.TabularDataset.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_tabular_gcs_sample]
@@ -0,0 +1,36 @@
# 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_and_import_dataset_tabular_gcs_sample
import test_constants as constants


def test_create_and_import_dataset_tabular_gcs_sample(
mock_sdk_init, mock_create_tabular_dataset
):

create_and_import_dataset_tabular_gcs_sample.create_and_import_dataset_tabular_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_tabular_dataset.assert_called_once_with(
display_name=constants.DISPLAY_NAME, gcs_source=constants.GCS_SOURCES,
)
Expand Up @@ -18,7 +18,7 @@


def test_create_batch_prediction_job_sample(
mock_sdk_init, mock_init_model, mock_batch_predict_model
mock_sdk_init, mock_get_model, mock_batch_predict_model
):

create_batch_prediction_job_sample.create_batch_prediction_job_sample(
Expand All @@ -33,7 +33,7 @@ def test_create_batch_prediction_job_sample(
mock_sdk_init.assert_called_once_with(
project=constants.PROJECT, location=constants.LOCATION
)
mock_init_model.assert_called_once_with(constants.MODEL_NAME)
mock_get_model.assert_called_once_with(constants.MODEL_NAME)
mock_batch_predict_model.assert_called_once_with(
job_display_name=constants.DISPLAY_NAME,
gcs_source=constants.GCS_SOURCES,
Expand Down
34 changes: 34 additions & 0 deletions samples/model-builder/create_endpoint_sample.py
@@ -0,0 +1,34 @@
# 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_endpoint_sample]
def create_endpoint_sample(
project: str, display_name: str, location: str, sync: bool = True,
):
aiplatform.init(project=project, location=location)

endpoint = aiplatform.Endpoint.create(
display_name=display_name, project=project, location=location,
)

print(endpoint.display_name)
print(endpoint.resource_name)
print(endpoint.uri)
return endpoint


# [END aiplatform_sdk_create_endpoint_sample]
36 changes: 36 additions & 0 deletions samples/model-builder/create_endpoint_sample_test.py
@@ -0,0 +1,36 @@
# 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_endpoint_sample
import test_constants as constants


def test_create_endpoint_sample(mock_sdk_init, mock_create_endpoint):

create_endpoint_sample.create_endpoint_sample(
project=constants.PROJECT,
display_name=constants.DISPLAY_NAME,
location=constants.LOCATION,
)

mock_sdk_init.assert_called_once_with(
project=constants.PROJECT, location=constants.LOCATION
)

mock_create_endpoint.assert_called_once_with(
display_name=constants.DISPLAY_NAME,
project=constants.PROJECT,
location=constants.LOCATION,
)
Expand Up @@ -20,7 +20,7 @@
def test_create_training_pipeline_image_classification_sample(
mock_sdk_init,
mock_image_dataset,
mock_init_automl_image_training_job,
mock_get_automl_image_training_job,
mock_run_automl_image_training_job,
mock_get_image_dataset,
):
Expand All @@ -43,7 +43,7 @@ def test_create_training_pipeline_image_classification_sample(
mock_sdk_init.assert_called_once_with(
project=constants.PROJECT, location=constants.LOCATION
)
mock_init_automl_image_training_job.assert_called_once_with(
mock_get_automl_image_training_job.assert_called_once_with(
display_name=constants.DISPLAY_NAME
)
mock_run_automl_image_training_job.assert_called_once_with(
Expand Down

0 comments on commit 4241738

Please sign in to comment.