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: Improved metadata artifact and execution creation using python / SDK #1430

Merged
merged 58 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from 57 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
5d109a7
rebase to master after ga merge
SinaChavoshi Jun 14, 2022
03b0b94
add support for artifact create
SinaChavoshi Jun 14, 2022
380135d
add unit tests for create from artifact parameters
SinaChavoshi Jun 14, 2022
49e34cc
update formatting
SinaChavoshi Jun 14, 2022
9471dbf
fix lint issues
SinaChavoshi Jun 14, 2022
8d0b9e5
Add integration tests
SinaChavoshi Jun 14, 2022
1bd08aa
add support for execution types
SinaChavoshi Jun 15, 2022
947249f
correct execution type in create
SinaChavoshi Jun 15, 2022
c23efa9
add execution support and unit tests
SinaChavoshi Jun 16, 2022
8c7350c
add support for state in artifacts
SinaChavoshi Jun 16, 2022
b2f2a91
add support for start_execution
SinaChavoshi Jun 16, 2022
1e85c2b
add support for metadata_store_id in start_execution
SinaChavoshi Jun 16, 2022
a93bdcd
lint and docs update based on review feedback
SinaChavoshi Jun 16, 2022
3babdef
merge with upstream/master
SinaChavoshi Jun 16, 2022
743a971
Add e2e integraton tests and lint update
SinaChavoshi Jun 16, 2022
edaf3e6
Update google/cloud/aiplatform/metadata/artifact.py
SinaChavoshi Jun 16, 2022
fb18610
remove the duplicate test_experiments_copy.py
SinaChavoshi Jun 16, 2022
f655741
refactor based on code review feedback
SinaChavoshi Jun 16, 2022
02d6069
regroup tests to match module names
SinaChavoshi Jun 16, 2022
abe3d48
fix e2e integration tests
SinaChavoshi Jun 16, 2022
8056a41
remove call to _temp_prefix = tmpvrtxsdk-e2e from E2E test
SinaChavoshi Jun 16, 2022
d2daa78
Update google/cloud/aiplatform/metadata/schema/base_execution.py
SinaChavoshi Jun 16, 2022
839b708
remove artifact and schema referencing the create result to self
SinaChavoshi Jun 16, 2022
81beab2
remove kwargs
SinaChavoshi Jun 16, 2022
a308bfd
fix typing for container spec
SinaChavoshi Jun 16, 2022
3122e7d
remove resouceName from system types
SinaChavoshi Jun 16, 2022
30c0fcd
metrics should default to None
SinaChavoshi Jun 16, 2022
3c9b5dc
Merge branch 'main' into minimum_schema_set_from_ga
SinaChavoshi Jun 17, 2022
476946f
change from using resouce_name to resource_id
SinaChavoshi Jun 17, 2022
4a2a3fc
fix e2e tests
SinaChavoshi Jun 17, 2022
e42cfa3
Merge remote-tracking branch 'upstream/main' into minimum_schema_set_…
SinaChavoshi Jun 21, 2022
6d89efc
change google and system to sub folders of schema
SinaChavoshi Jun 21, 2022
db1949c
Merge branch 'main' into minimum_schema_set_from_ga
SinaChavoshi Jun 21, 2022
53ba40b
use create_from_base_execution_schema instead of overloading create
SinaChavoshi Jun 22, 2022
1a8931a
update api docs
SinaChavoshi Jun 22, 2022
0265816
update docstring formatting
SinaChavoshi Jun 22, 2022
59afcd6
Update google/cloud/aiplatform/metadata/metadata.py
SinaChavoshi Jun 22, 2022
1e31adf
add return types and move args to constructor
SinaChavoshi Jun 22, 2022
c43fcbf
using forward reference for parameter and return types to resolve cir…
SinaChavoshi Jun 23, 2022
6311bfd
change base classes to abstract classes
SinaChavoshi Jun 23, 2022
04c9e88
Add tests for system.artifact type
SinaChavoshi Jun 23, 2022
1778a34
use resouce name instead of id and populate metadata with resourceNa…
SinaChavoshi Jun 23, 2022
bee8e93
remove start_execution from this pr and move to a separate PR
SinaChavoshi Jun 23, 2022
5e87e0a
change all args to keyword args
SinaChavoshi Jun 23, 2022
48f65dc
always make a copy of metadata instead of pass by reference
SinaChavoshi Jun 23, 2022
086df90
auto generate uri for google types
SinaChavoshi Jun 23, 2022
af765f3
fix e2e tests
SinaChavoshi Jun 23, 2022
e28fb7e
Merge branch 'googleapis:main' into minimum_schema_set_from_ga
SinaChavoshi Jun 23, 2022
f6e0b9d
Merge branch 'main' into minimum_schema_set_from_ga
rosiezou Jun 23, 2022
4e9d28b
Merge branch 'googleapis:main' into minimum_schema_set_from_ga
SinaChavoshi Jun 24, 2022
165ea43
Merge branch 'googleapis:main' into minimum_schema_set_from_ga
SinaChavoshi Jun 24, 2022
63a7e57
switch to using Artifact.create instead of _create
SinaChavoshi Jun 25, 2022
3faef4f
Merge branch 'googleapis:main' into minimum_schema_set_from_ga
SinaChavoshi Jun 27, 2022
246dfcc
change typing for state to Optional
SinaChavoshi Jun 27, 2022
575531e
change typing for state to Optional in artifact base file
SinaChavoshi Jun 27, 2022
3b17ce7
change to use the Execution.create instead of the private method
SinaChavoshi Jun 27, 2022
c9403bf
Merge branch 'googleapis:main' into minimum_schema_set_from_ga
SinaChavoshi Jun 27, 2022
323075e
chagne copy to deepcopy for metadata
SinaChavoshi Jun 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions google/cloud/aiplatform/metadata/artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from google.cloud.aiplatform.metadata import metadata_store
from google.cloud.aiplatform.metadata import resource
from google.cloud.aiplatform.metadata import utils as metadata_utils
from google.cloud.aiplatform.metadata.schema import base_artifact
SinaChavoshi marked this conversation as resolved.
Show resolved Hide resolved
from google.cloud.aiplatform.utils import rest_utils


Expand Down Expand Up @@ -326,6 +327,56 @@ def create(
credentials=credentials,
)

@classmethod
def create_from_base_artifact_schema(
cls,
*,
base_artifact_schema: "base_artifact.BaseArtifactSchema",
metadata_store_id: Optional[str] = "default",
project: Optional[str] = None,
location: Optional[str] = None,
credentials: Optional[auth_credentials.Credentials] = None,
) -> "Artifact":
"""Creates a new Metadata Artifact from a BaseArtifactSchema class instance.

Args:
base_artifact_schema (BaseArtifactSchema):
Required. An instance of the BaseArtifactType class that can be
provided instead of providing artifact specific parameters.
metadata_store_id (str):
Optional. The <metadata_store_id> portion of the resource name with
the format:
projects/123/locations/us-central1/metadataStores/<metadata_store_id>/artifacts/<resource_id>
If not provided, the MetadataStore's ID will be set to "default".
project (str):
Optional. Project used to create this Artifact. Overrides project set in
aiplatform.init.
location (str):
Optional. Location used to create this Artifact. Overrides location set in
aiplatform.init.
credentials (auth_credentials.Credentials):
Optional. Custom credentials used to create this Artifact. Overrides
credentials set in aiplatform.init.

Returns:
Artifact: Instantiated representation of the managed Metadata Artifact.
"""

return cls.create(
resource_id=base_artifact_schema.artifact_id,
schema_title=base_artifact_schema.schema_title,
uri=base_artifact_schema.uri,
display_name=base_artifact_schema.display_name,
schema_version=base_artifact_schema.schema_version,
description=base_artifact_schema.description,
metadata=base_artifact_schema.metadata,
state=base_artifact_schema.state,
metadata_store_id=metadata_store_id,
project=project,
location=location,
credentials=credentials,
)

@property
def uri(self) -> Optional[str]:
"Uri for this Artifact."
Expand Down
52 changes: 52 additions & 0 deletions google/cloud/aiplatform/metadata/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from google.cloud.aiplatform.metadata import artifact
from google.cloud.aiplatform.metadata import metadata_store
from google.cloud.aiplatform.metadata import resource
from google.cloud.aiplatform.metadata.schema import base_execution


class Execution(resource._Resource):
Expand Down Expand Up @@ -166,6 +167,57 @@ def create(

return self

@classmethod
def create_from_base_execution_schema(
cls,
*,
base_execution_schema: "base_execution.BaseExecutionSchema",
metadata_store_id: Optional[str] = "default",
project: Optional[str] = None,
location: Optional[str] = None,
credentials: Optional[auth_credentials.Credentials] = None,
) -> "Execution":
"""
Creates a new Metadata Execution.

Args:
base_execution_schema (BaseExecutionSchema):
An instance of the BaseExecutionSchema class that can be
provided instead of providing schema specific parameters.
metadata_store_id (str):
Optional. The <metadata_store_id> portion of the resource name with
the format:
projects/123/locations/us-central1/metadataStores/<metadata_store_id>/artifacts/<resource_id>
If not provided, the MetadataStore's ID will be set to "default".
project (str):
Optional. Project used to create this Execution. Overrides project set in
aiplatform.init.
location (str):
Optional. Location used to create this Execution. Overrides location set in
aiplatform.init.
credentials (auth_credentials.Credentials):
Optional. Custom credentials used to create this Execution. Overrides
credentials set in aiplatform.init.

Returns:
Execution: Instantiated representation of the managed Metadata Execution.

"""
resource = Execution.create(
state=base_execution_schema.state,
schema_title=base_execution_schema.schema_title,
resource_id=base_execution_schema.execution_id,
display_name=base_execution_schema.display_name,
schema_version=base_execution_schema.schema_version,
metadata=base_execution_schema.metadata,
description=base_execution_schema.description,
metadata_store_id=metadata_store_id,
project=project,
location=location,
credentials=credentials,
)
return resource

def __enter__(self):
if self.state is not gca_execution.Execution.State.RUNNING:
self.update(state=gca_execution.Execution.State.RUNNING)
Expand Down
126 changes: 126 additions & 0 deletions google/cloud/aiplatform/metadata/schema/base_artifact.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# -*- coding: utf-8 -*-

# 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
#
# http://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 abc

from typing import Optional, Dict
SinaChavoshi marked this conversation as resolved.
Show resolved Hide resolved

from google.auth import credentials as auth_credentials

from google.cloud.aiplatform.compat.types import artifact as gca_artifact
from google.cloud.aiplatform.metadata import artifact
from google.cloud.aiplatform.metadata import constants


class BaseArtifactSchema(metaclass=abc.ABCMeta):
"""Base class for Metadata Artifact types."""

@property
@classmethod
@abc.abstractmethod
def schema_title(cls) -> str:
"""Identifies the Vertex Metadta schema title used by the resource."""
pass

def __init__(
self,
*,
artifact_id: Optional[str] = None,
uri: Optional[str] = None,
display_name: Optional[str] = None,
schema_version: Optional[str] = None,
description: Optional[str] = None,
metadata: Optional[Dict] = None,
state: Optional[gca_artifact.Artifact.State] = gca_artifact.Artifact.State.LIVE,
):

"""Initializes the Artifact with the given name, URI and metadata.

This is the base class for defining various artifact types, which can be
passed to google.Artifact to create a corresponding resource.
Artifacts carry a `metadata` field, which is a dictionary for storing
metadata related to this artifact. Subclasses from ArtifactType can enforce
various structure and field requirements for the metadata field.

Args:
resource_id (str):
Optional. The <resource_id> portion of the Artifact name with
the following format, this is globally unique in a metadataStore:
projects/123/locations/us-central1/metadataStores/<metadata_store_id>/artifacts/<resource_id>.
uri (str):
Optional. The uniform resource identifier of the artifact file. May be empty if there is no actual
artifact file.
display_name (str):
Optional. The user-defined name of the Artifact.
schema_version (str):
Optional. schema_version specifies the version used by the Artifact.
If not set, defaults to use the latest version.
description (str):
Optional. Describes the purpose of the Artifact to be created.
metadata (Dict):
Optional. Contains the metadata information that will be stored in the Artifact.
state (google.cloud.gapic.types.Artifact.State):
Optional. The state of this Artifact. This is a
property of the Artifact, and does not imply or
capture any ongoing process. This property is
managed by clients (such as Vertex AI
Pipelines), and the system does not prescribe or
check the validity of state transitions.
"""
self.artifact_id = artifact_id
self.uri = uri
self.display_name = display_name
self.schema_version = schema_version or constants._DEFAULT_SCHEMA_VERSION
self.description = description
self.metadata = metadata
self.state = state

def create(
self,
*,
metadata_store_id: Optional[str] = "default",
project: Optional[str] = None,
location: Optional[str] = None,
credentials: Optional[auth_credentials.Credentials] = None,
) -> "artifact.Artifact":
"""Creates a new Metadata Artifact.

Args:
metadata_store_id (str):
Optional. The <metadata_store_id> portion of the resource name with
the format:
projects/123/locations/us-central1/metadataStores/<metadata_store_id>/artifacts/<resource_id>
If not provided, the MetadataStore's ID will be set to "default".
project (str):
Optional. Project used to create this Artifact. Overrides project set in
aiplatform.init.
location (str):
Optional. Location used to create this Artifact. Overrides location set in
aiplatform.init.
credentials (auth_credentials.Credentials):
Optional. Custom credentials used to create this Artifact. Overrides
credentials set in aiplatform.init.
Returns:
Artifact: Instantiated representation of the managed Metadata Artifact.
"""
return artifact.Artifact.create_from_base_artifact_schema(
base_artifact_schema=self,
metadata_store_id=metadata_store_id,
project=project,
location=location,
credentials=credentials,
)
114 changes: 114 additions & 0 deletions google/cloud/aiplatform/metadata/schema/base_execution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# -*- coding: utf-8 -*-

# 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
#
# http://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 abc

from typing import Optional, Dict

from google.auth import credentials as auth_credentials
SinaChavoshi marked this conversation as resolved.
Show resolved Hide resolved

from google.cloud.aiplatform.compat.types import execution as gca_execution
from google.cloud.aiplatform.metadata import constants
from google.cloud.aiplatform.metadata import execution


class BaseExecutionSchema(metaclass=abc.ABCMeta):
"""Base class for Metadata Execution schema."""

@property
@classmethod
@abc.abstractmethod
def schema_title(cls) -> str:
"""Identifies the Vertex Metadta schema title used by the resource."""
pass

def __init__(
self,
*,
state: Optional[
gca_execution.Execution.State
] = gca_execution.Execution.State.RUNNING,
execution_id: Optional[str] = None,
display_name: Optional[str] = None,
schema_version: Optional[str] = None,
metadata: Optional[Dict] = None,
description: Optional[str] = None,
):

"""Initializes the Execution with the given name, URI and metadata.

Args:
state (gca_execution.Execution.State.RUNNING):
Optional. State of this Execution. Defaults to RUNNING.
execution_id (str):
Optional. The <resource_id> portion of the Execution name with
the following format, this is globally unique in a metadataStore.
projects/123/locations/us-central1/metadataStores/<metadata_store_id>/executions/<resource_id>.
display_name (str):
Optional. The user-defined name of the Execution.
schema_version (str):
Optional. schema_version specifies the version used by the Execution.
If not set, defaults to use the latest version.
metadata (Dict):
Optional. Contains the metadata information that will be stored in the Execution.
description (str):
Optional. Describes the purpose of the Execution to be created.
"""
self.state = state
self.execution_id = execution_id
self.display_name = display_name
self.schema_version = schema_version or constants._DEFAULT_SCHEMA_VERSION
self.metadata = metadata
self.description = description

def create(
self,
*,
metadata_store_id: Optional[str] = "default",
project: Optional[str] = None,
location: Optional[str] = None,
credentials: Optional[auth_credentials.Credentials] = None,
) -> "execution.Execution":
"""Creates a new Metadata Execution.

Args:
metadata_store_id (str):
Optional. The <metadata_store_id> portion of the resource name with
the format:
projects/123/locations/us-central1/metadataStores/<metadata_store_id>/executions/<resource_id>
If not provided, the MetadataStore's ID will be set to "default".
project (str):
Optional. Project used to create this Execution. Overrides project set in
aiplatform.init.
location (str):
Optional. Location used to create this Execution. Overrides location set in
aiplatform.init.
credentials (auth_credentials.Credentials):
Optional. Custom credentials used to create this Execution. Overrides
credentials set in aiplatform.init.
Returns:
Execution: Instantiated representation of the managed Metadata Execution.

"""
self.execution = execution.Execution.create_from_base_execution_schema(
base_execution_schema=self,
metadata_store_id=metadata_store_id,
project=project,
location=location,
credentials=credentials,
)
return self.execution
Loading