示例#1
0
def test_save(trial_component_obj, sagemaker_boto_client):
    trial_component_obj.display_name = str(uuid.uuid4())
    trial_component_obj.status = api_types.TrialComponentStatus(
        primary_status="InProgress", message="Message")
    trial_component_obj.start_time = datetime.datetime.now(
        datetime.timezone.utc) - datetime.timedelta(days=1)
    trial_component_obj.end_time = datetime.datetime.now(datetime.timezone.utc)
    trial_component_obj.parameters = {"foo": "bar", "whizz": 100.1}
    trial_component_obj.input_artifacts = {
        "snizz":
        api_types.TrialComponentArtifact(value="s3:/foo/bar",
                                         media_type="text/plain")
    }
    trial_component_obj.output_artifacts = {
        "fly":
        api_types.TrialComponentArtifact(value="s3:/sky/far",
                                         media_type="away/tomorrow")
    }
    trial_component_obj.save()

    loaded = trial_component.TrialComponent.load(
        trial_component_name=trial_component_obj.trial_component_name,
        sagemaker_boto_client=sagemaker_boto_client)

    assert trial_component_obj.trial_component_name == loaded.trial_component_name
    assert trial_component_obj.status == loaded.status

    assert trial_component_obj.start_time - loaded.start_time < datetime.timedelta(
        seconds=1)
    assert trial_component_obj.end_time - loaded.end_time < datetime.timedelta(
        seconds=1)

    assert trial_component_obj.parameters == loaded.parameters
    assert trial_component_obj.input_artifacts == loaded.input_artifacts
    assert trial_component_obj.output_artifacts == loaded.output_artifacts
def test_save(trial_component_obj, sagemaker_boto_client):
    trial_component_obj.display_name = str(uuid.uuid4())
    trial_component_obj.status = api_types.TrialComponentStatus(primary_status='InProgress', message='Message')
    trial_component_obj.start_time = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(days=1)
    trial_component_obj.end_time = datetime.datetime.now(datetime.timezone.utc)
    trial_component_obj.parameters = {
        'foo': 'bar',
        'whizz': 100.1
    }
    trial_component_obj.input_artifacts = {
        'snizz': api_types.TrialComponentArtifact(value='s3:/foo/bar', media_type='text/plain')
    }
    trial_component_obj.output_artifacts = {
        'fly': api_types.TrialComponentArtifact(value='s3:/sky/far', media_type='away/tomorrow')
    }
    trial_component_obj.save()

    loaded = trial_component.TrialComponent.load(trial_component_name = trial_component_obj.trial_component_name,
                                                 sagemaker_boto_client=sagemaker_boto_client)

    assert trial_component_obj.trial_component_name == loaded.trial_component_name
    assert trial_component_obj.status == loaded.status

    assert trial_component_obj.start_time - loaded.start_time < datetime.timedelta(seconds=1)
    assert trial_component_obj.end_time - loaded.end_time < datetime.timedelta(seconds=1)

    assert trial_component_obj.parameters == loaded.parameters
    assert trial_component_obj.input_artifacts == loaded.input_artifacts
    assert trial_component_obj.output_artifacts == loaded.output_artifacts
def test_track_from_processing_job(sagemaker_boto_client, processing_job_name):
    processing_job = sagemaker_boto_client.describe_processing_job(
        ProcessingJobName=processing_job_name)

    source_arn = processing_job['ProcessingJobArn']
    wait_for_job(processing_job_name, sagemaker_boto_client)
    trial_component_name = sagemaker_boto_client.list_trial_components(
        SourceArn=source_arn
    )['TrialComponentSummaries'][0]['TrialComponentName']

    trial_component_obj = trial_component.TrialComponent.load(
        trial_component_name=trial_component_name)
    assert {
        'p1': 1.0,
        'SageMaker.InstanceType': 'ml.m5.large',
        'SageMaker.InstanceCount': 1.0,
        'SageMaker.VolumeSizeInGB': 10.0
    } == trial_component_obj.parameters

    image_uri = processing_job['AppSpecification']['ImageUri']
    assert {
        'SageMaker.ImageUri': api_types.TrialComponentArtifact(value=image_uri)
    } == trial_component_obj.input_artifacts

    assert not trial_component_obj.output_artifacts
    assert not trial_component_obj.metrics
    assert source_arn == trial_component_obj.source.source_arn
    assert 'Completed' == trial_component_obj.status.primary_status
示例#4
0
def test_load(sagemaker_boto_client):
    now = datetime.datetime.now(datetime.timezone.utc)

    sagemaker_boto_client.describe_trial_component.return_value = {
        "TrialComponentArn": "A",
        "TrialComponentName": "B",
        "DisplayName": "C",
        "Status": {"PrimaryStatus": "InProgress", "Message": "D"},
        "Parameters": {"E": {"NumberValue": 1.0}, "F": {"StringValue": "G"}},
        "InputArtifacts": {"H": {"Value": "s3://foo/bar", "MediaType": "text/plain"}},
        "OutputArtifacts": {"I": {"Value": "s3://whizz/bang", "MediaType": "text/plain"}},
        "Metrics": [
            {
                "MetricName": "J",
                "Count": 1,
                "Min": 1.0,
                "Max": 2.0,
                "Avg": 3.0,
                "StdDev": 4.0,
                "SourceArn": "K",
                "Timestamp": now,
            }
        ],
    }
    obj = trial_component.TrialComponent.load(trial_component_name="foo", sagemaker_boto_client=sagemaker_boto_client)
    sagemaker_boto_client.describe_trial_component.assert_called_with(TrialComponentName="foo")
    assert "A" == obj.trial_component_arn
    assert "B" == obj.trial_component_name
    assert "C" == obj.display_name
    assert api_types.TrialComponentStatus(primary_status="InProgress", message="D") == obj.status
    assert {"E": 1.0, "F": "G"} == obj.parameters
    assert {"H": api_types.TrialComponentArtifact(value="s3://foo/bar", media_type="text/plain")}
    assert {"I": api_types.TrialComponentArtifact(value="s3://whizz/bang", media_type="text/plain")}
    assert [
        api_types.TrialComponentMetricSummary(
            metric_name="J", count=1, min=1.0, max=2.0, avg=3.0, std_dev=4.0, source_arn="K", timestamp=now
        )
    ]
示例#5
0
    def log_output(self, name, value, media_type=None):
        """Record a single output artifact for this trial component.

        Overwrites any previous value recorded for the specified output name.

        Examples
            .. code-block:: python

                # log input dataset s3 location
                my_tracker.log_output(name='prediction', value='s3://outputs/path')

        Args:
            name (str): The name of the output value.
            value (str): The value.
            media_type (str, optional): The MediaType (MIME type) of the value.
        """
        self.trial_component.output_artifacts[
            name] = api_types.TrialComponentArtifact(value,
                                                     media_type=media_type)
示例#6
0
    def log_artifact(self, file_path, name=None, media_type=None):
        """Upload a local file to s3 and store it as an artifact in this trial component.

        Examples
            .. code-block:: python

                # log local artifact
                my_tracker.log_artifact(file_path='/local/path/artifact.tar.gz')

        Args:
            file_path (str): The path of the local file to upload.
            name (str, optional): The name of the artifact.
            media_type (str, optional): The MediaType (MIME type) of the file. If not specified, this library
                will attempt to infer the media type from the file extension of ``file_path``.
        """
        media_type = media_type or _guess_media_type(file_path)
        name = name or _resolve_artifact_name(file_path)
        s3_uri = self._artifact_uploader.upload_artifact(file_path)
        self.trial_component.output_artifacts[
            name] = api_types.TrialComponentArtifact(value=s3_uri,
                                                     media_type=media_type)
示例#7
0
def test_track_from_processing_job(sagemaker_boto_client, processing_job_name):
    get_job = lambda: sagemaker_boto_client.describe_processing_job(
        ProcessingJobName=processing_job_name)
    processing_job = get_job()

    source_arn = processing_job["ProcessingJobArn"]
    wait_for_job(processing_job_name, get_job, "ProcessingJobStatus")

    print(processing_job)
    if "ProcessingStartTime" in processing_job:
        start = to_seconds(processing_job["ProcessingStartTime"])
        end = to_seconds(processing_job["ProcessingEndTime"])
        print("Processing job took: {} seconds".format(end - start))

    trial_component_name = sagemaker_boto_client.list_trial_components(
        SourceArn=source_arn
    )["TrialComponentSummaries"][0]["TrialComponentName"]

    trial_component_obj = trial_component.TrialComponent.load(
        trial_component_name=trial_component_name)
    assert {
        "SageMaker.InstanceType": "ml.m5.large",
        "SageMaker.InstanceCount": 1.0,
        "SageMaker.VolumeSizeInGB": 10.0,
    } == trial_component_obj.parameters

    image_uri = processing_job["AppSpecification"]["ImageUri"]
    assert {
        "SageMaker.ImageUri": api_types.TrialComponentArtifact(value=image_uri)
    } == trial_component_obj.input_artifacts

    assert not trial_component_obj.output_artifacts, "should be no output artifacts"
    assert not trial_component_obj.metrics, "should be no metrics"
    assert source_arn == trial_component_obj.source.source_arn, "source ARNs should match"
    assert trial_component_obj.status.primary_status in [
        "Completed", "Failed"
    ], "Status should be Completed or Failed"
def test_log_output(under_test):
    under_test.log_output('foo', 'baz', 'text/text')
    assert under_test.trial_component.output_artifacts == {
        'foo':
        api_types.TrialComponentArtifact(value='baz', media_type='text/text')
    }
def test_load(sagemaker_boto_client):
    now = datetime.datetime.now(datetime.timezone.utc)

    sagemaker_boto_client.describe_trial_component.return_value = {
        'TrialComponentArn':
        'A',
        'TrialComponentName':
        'B',
        'DisplayName':
        'C',
        'Status': {
            'PrimaryStatus': 'InProgress',
            'Message': 'D'
        },
        'Parameters': {
            'E': {
                'NumberValue': 1.0
            },
            'F': {
                'StringValue': 'G'
            }
        },
        'InputArtifacts': {
            'H': {
                'Value': 's3://foo/bar',
                'MediaType': 'text/plain'
            }
        },
        'OutputArtifacts': {
            'I': {
                'Value': 's3://whizz/bang',
                'MediaType': 'text/plain'
            }
        },
        'Metrics': [{
            'MetricName': 'J',
            'Count': 1,
            'Min': 1.0,
            'Max': 2.0,
            'Avg': 3.0,
            'StdDev': 4.0,
            'SourceArn': 'K',
            'Timestamp': now
        }]
    }
    obj = trial_component.TrialComponent.load(
        trial_component_name='foo',
        sagemaker_boto_client=sagemaker_boto_client)
    sagemaker_boto_client.describe_trial_component.assert_called_with(
        TrialComponentName='foo')
    assert 'A' == obj.trial_component_arn
    assert 'B' == obj.trial_component_name
    assert 'C' == obj.display_name
    assert api_types.TrialComponentStatus(primary_status='InProgress',
                                          message='D') == obj.status
    assert {'E': 1.0, 'F': 'G'} == obj.parameters
    assert {
        'H':
        api_types.TrialComponentArtifact(value='s3://foo/bar',
                                         media_type='text/plain')
    }
    assert {
        'I':
        api_types.TrialComponentArtifact(value='s3://whizz/bang',
                                         media_type='text/plain')
    }
    assert [
        api_types.TrialComponentMetricSummary(metric_name='J',
                                              count=1,
                                              min=1.0,
                                              max=2.0,
                                              avg=3.0,
                                              std_dev=4.0,
                                              source_arn='K',
                                              timestamp=now)
    ]
示例#10
0
def test_log_output(under_test):
    under_test.log_output("foo", "baz", "text/text")
    assert under_test.trial_component.output_artifacts == {
        "foo": api_types.TrialComponentArtifact(value="baz", media_type="text/text")
    }