Exemple #1
0
  def _log(self, artifact):
    """
    Log an artifact.

    This method expects `artifact` to have
      - ARTIFACT_TYPE_NAME string field the form of
        <namespace>/<name>.
      - serialization() method to return a openapi_client.MlMetadataArtifact.

    This method will set artifact.id.
    """
    serialization = artifact.serialization()
    if serialization.custom_properties is None:
          serialization.custom_properties = {}
    if WORKSPACE_PROPERTY_NAME in serialization.custom_properties:
          raise ValueError("custom_properties contains reserved key %s"
                           % WORKSPACE_PROPERTY_NAME)
    if RUN_PROPERTY_NAME in serialization.custom_properties:
      raise ValueError("custom_properties contains reserved key %s"
                       % RUN_PROPERTY_NAME)
    if self.workspace is not None:
      serialization.custom_properties[
          WORKSPACE_PROPERTY_NAME] = openapi_client.MlMetadataValue(
          string_value=self.workspace.name)
    if self.run is not None:
      serialization.custom_properties[
          RUN_PROPERTY_NAME] = openapi_client.MlMetadataValue(
          string_value=self.run.name)
    response = self.workspace.client.create_artifact(
        parent=artifact.ARTIFACT_TYPE_NAME,
        body=serialization,
    )
    artifact.id = response.artifact.id
    return artifact
Exemple #2
0
    def log(self, artifact):
        """
    Log an artifact as an input or output of this run to
    metadata backend serivce.

    This method expects `artifact` to have
      - ARTIFACT_TYPE_NAME string field the form of
        <namespace>/<name>.
      - serialization() method to return a openapi_client.MlMetadataArtifact.

    This method will set artifact.id.
    """

        # TODO(zhenghui): log this artifact as the input or output of an execution.
        serialization = artifact.serialization()
        if serialization.custom_properties == None:
            serialization.custom_properties = {}
        if WORKSPACE_PROPERTY_NAME in serialization.custom_properties:
            raise ValueError("custom_properties contains reserved key %s" %
                             WORKSPACE_PROPERTY_NAME)
        if RUN_PROPERTY_NAME in serialization.custom_properties:
            raise ValueError("custom_properties contains reserved key %s" %
                             RUN_PROPERTY_NAME)
        serialization.custom_properties[
            WORKSPACE_PROPERTY_NAME] = openapi_client.MlMetadataValue(
                string_value=self.workspace.name)
        serialization.custom_properties[
            RUN_PROPERTY_NAME] = openapi_client.MlMetadataValue(
                string_value=self.name)
        response = self.workspace._client.create_artifact(
            parent=artifact.ARTIFACT_TYPE_NAME,
            body=serialization,
        )
        artifact.id = response.artifact.id
        return artifact
Exemple #3
0
 def test_log_invalid_artifacts_should_fail(self):
     r = metadata.Run(name="test run")
     artifact1 = ArtifactFixture(
         openapi_client.MlMetadataArtifact(
             uri="gs://uri",
             custom_properties={
                 metadata.WORKSPACE_PROPERTY_NAME:
                 openapi_client.MlMetadataValue(string_value="ws1"),
             }))
     self.assertRaises(ValueError, r.log, artifact1)
     artifact2 = ArtifactFixture(
         openapi_client.MlMetadataArtifact(
             uri="gs://uri",
             custom_properties={
                 metadata.RUN_PROPERTY_NAME:
                 openapi_client.MlMetadataValue(string_value="run1"),
             }))
     self.assertRaises(ValueError, r.log, artifact2)
Exemple #4
0
 def test_log_invalid_artifacts_should_fail(self):
     ws = metadata.Workspace(backend_url_prefix="127.0.0.1:8080",
                             name="ws_1",
                             description="a workspace for testing",
                             labels={"n1": "v1"})
     e = metadata.Execution(name="test execution", workspace=ws)
     artifact1 = ArtifactFixture(
         openapi_client.MlMetadataArtifact(
             uri="gs://uri",
             custom_properties={
                 metadata.WORKSPACE_PROPERTY_NAME:
                 openapi_client.MlMetadataValue(string_value="ws1"),
             }))
     self.assertRaises(ValueError, e.log_input, artifact1)
     artifact2 = ArtifactFixture(
         openapi_client.MlMetadataArtifact(
             uri="gs://uri",
             custom_properties={
                 metadata.RUN_PROPERTY_NAME:
                 openapi_client.MlMetadataValue(string_value="run1"),
             }))
     self.assertRaises(ValueError, e.log_output, artifact2)
Exemple #5
0
  def serialized(self):
    execution = openapi_client.MlMetadataExecution(
    properties={
        "name":
            openapi_client.MlMetadataValue(string_value=self.name),
        "create_time":
            openapi_client.MlMetadataValue(string_value=self.create_time),
        "description":
            openapi_client.MlMetadataValue(string_value=self.description),
    })
    if self.description is None:
      del execution.properties["description"]

    execution.custom_properties = {}
    if self.workspace is not None:
      execution.custom_properties[
          WORKSPACE_PROPERTY_NAME] = openapi_client.MlMetadataValue(
          string_value=self.workspace.name)
    if self.run is not None:
      execution.custom_properties[
          RUN_PROPERTY_NAME] = openapi_client.MlMetadataValue(
          string_value=self.run.name)
    return execution
Exemple #6
0
 def serialization(self):
   model_artifact = openapi_client.MlMetadataArtifact(
       uri=self.uri,
       properties={
           "name":
               openapi_client.MlMetadataValue(string_value=self.name),
           "create_time":
               openapi_client.MlMetadataValue(string_value=self.create_time),
           "description":
               openapi_client.MlMetadataValue(string_value=self.description),
           "metrics_type":
               openapi_client.MlMetadataValue(string_value=self.metrics_type),
           "data_set_id":
               openapi_client.MlMetadataValue(string_value=self.data_set_id),
           "model_id":
               openapi_client.MlMetadataValue(string_value=self.model_id),
           "owner":
               openapi_client.MlMetadataValue(string_value=self.owner),
           ALL_META_PROPERTY_NAME:
               openapi_client.MlMetadataValue(string_value=json.dumps(self.__dict__)),
       })
   return model_artifact
Exemple #7
0
 def serialization(self):
   data_set_artifact = openapi_client.MlMetadataArtifact(
       uri=self.uri,
       properties={
           "name":
               openapi_client.MlMetadataValue(string_value=self.name),
           "create_time":
               openapi_client.MlMetadataValue(string_value=self.create_time),
           "description":
               openapi_client.MlMetadataValue(string_value=self.description),
           "query":
               openapi_client.MlMetadataValue(string_value=self.query),
           "version":
               openapi_client.MlMetadataValue(string_value=self.version),
           "owner":
               openapi_client.MlMetadataValue(string_value=self.owner),
           ALL_META_PROPERTY_NAME:
               openapi_client.MlMetadataValue(string_value=json.dumps(self.__dict__)),
       })
   return data_set_artifact