Skip to content
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

feat: MBSDK Tabular samples #338

Merged
merged 16 commits into from Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions samples/model-builder/conftest.py
Expand Up @@ -137,6 +137,23 @@ def mock_import_text_dataset(mock_text_dataset):
"""


@pytest.fixture
def mock_init_automl_tabular_training_job():
with patch.object(
aiplatform.training_jobs.AutoMLTabularTrainingJob, "__init__"
) as mock:
mock.return_value = None
yield mock


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


@pytest.fixture
def mock_init_automl_image_training_job():
with patch.object(
Expand Down Expand Up @@ -192,6 +209,12 @@ def mock_create_batch_prediction_job():
"""


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


@pytest.fixture
def mock_endpoint():
mock = MagicMock(aiplatform.models.Endpoint)
Expand Down
@@ -0,0 +1,40 @@
# 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,41 @@
# 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,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.


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,
)

39 changes: 39 additions & 0 deletions samples/model-builder/create_endpoint_sample.py
@@ -0,0 +1,39 @@
# 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]
38 changes: 38 additions & 0 deletions samples/model-builder/create_endpoint_sample_test.py
@@ -0,0 +1,38 @@
# 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,
)
@@ -0,0 +1,61 @@
# 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_training_pipeline_tabular_classification_sample]
def create_training_pipeline_tabular_classification_sample(
project: str,
display_name: str,
dataset_id: int,
location: str = "us-central1",
optimization_prediction_type: str = 'classification',
aribray marked this conversation as resolved.
Show resolved Hide resolved
model_display_name: str = None,
training_fraction_split: float = 0.8,
validation_fraction_split: float = 0.1,
test_fraction_split: float = 0.1,
budget_milli_node_hours: int = 8000,
disable_early_stopping: bool = False,
sync: bool = True,
):
aiplatform.init(project=project, location=location)

tabular_classification_job = aiplatform.AutoMLTabularTrainingJob(
display_name=display_name,
optimization_prediction_type=optimization_prediction_type,
)

my_tabular_dataset = aiplatform.TabularDataset(dataset_id)

model = tabular_classification_job.run(
dataset=my_tabular_dataset,
training_fraction_split=training_fraction_split,
validation_fraction_split=validation_fraction_split,
test_fraction_split=test_fraction_split,
budget_milli_node_hours=budget_milli_node_hours,
model_display_name=model_display_name,
disable_early_stopping=disable_early_stopping,
sync=sync,
)

model.wait()

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


# [END aiplatform_sdk_create_training_pipeline_tabular_classification_sample]
@@ -0,0 +1,58 @@
# 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_training_pipeline_tabular_classification_sample
import test_constants as constants


def test_create_training_pipeline_tabular_classification_sample(
mock_sdk_init,
mock_tabular_dataset,
mock_init_automl_tabular_training_job,
mock_run_automl_tabular_training_job,
mock_get_tabular_dataset,
):

create_training_pipeline_tabular_classification_sample.create_training_pipeline_tabular_classification_sample(
project=constants.PROJECT,
display_name=constants.DISPLAY_NAME,
dataset_id=constants.RESOURCE_ID,
model_display_name=constants.DISPLAY_NAME_2,
training_fraction_split=constants.TRAINING_FRACTION_SPLIT,
validation_fraction_split=constants.VALIDATION_FRACTION_SPLIT,
test_fraction_split=constants.TEST_FRACTION_SPLIT,
budget_milli_node_hours=constants.BUDGET_MILLI_NODE_HOURS_8000,
disable_early_stopping=False,
)

mock_get_tabular_dataset.assert_called_once_with(constants.RESOURCE_ID)

mock_sdk_init.assert_called_once_with(
project=constants.PROJECT, location=constants.LOCATION
)
mock_init_automl_tabular_training_job.assert_called_once_with(
display_name=constants.DISPLAY_NAME,
optimization_prediction_type=constants.TABULAR_CLASSIFICATION_OPTIMIZATOIN_PREDICTION_TYPE,
)
mock_run_automl_tabular_training_job.assert_called_once_with(
dataset=mock_tabular_dataset,
model_display_name=constants.DISPLAY_NAME_2,
training_fraction_split=constants.TRAINING_FRACTION_SPLIT,
validation_fraction_split=constants.VALIDATION_FRACTION_SPLIT,
test_fraction_split=constants.TEST_FRACTION_SPLIT,
budget_milli_node_hours=constants.BUDGET_MILLI_NODE_HOURS_8000,
disable_early_stopping=False,
sync=True,
)