Beispiel #1
0
def test_validate_tag_name():
    for good_name in GOOD_METRIC_OR_PARAM_NAMES:
        _validate_tag_name(good_name)
    for bad_name in BAD_METRIC_OR_PARAM_NAMES:
        with pytest.raises(MlflowException, match="Invalid tag name") as e:
            _validate_tag_name(bad_name)
        assert e.value.error_code == ErrorCode.Name(INVALID_PARAMETER_VALUE)
Beispiel #2
0
 def set_tag(self, run_id, key, value):
     """
     Set a tag on the run ID. Value is converted to a string.
     """
     _validate_tag_name(key)
     tag = RunTag(key, str(value))
     self.store.set_tag(run_id, tag)
Beispiel #3
0
    def log_batch(self, run_id, metrics=(), params=(), tags=()):
        """
        Log multiple metrics, params, and/or tags.

        :param run_id: String ID of the run
        :param metrics: If provided, List of Metric(key, value, timestamp) instances.
        :param params: If provided, List of Param(key, value) instances.
        :param tags: If provided, List of RunTag(key, value) instances.

        Raises an MlflowException if any errors occur.
        :return: None
        """
        if len(metrics) == 0 and len(params) == 0 and len(tags) == 0:
            return
        for metric in metrics:
            _validate_metric(metric.key, metric.value, metric.timestamp,
                             metric.step)
        for param in params:
            _validate_param_name(param.key)
        for tag in tags:
            _validate_tag_name(tag.key)
        self.store.log_batch(run_id=run_id,
                             metrics=metrics,
                             params=params,
                             tags=tags)
Beispiel #4
0
    def set_tag(self, run_id, tag):
        """
        Set a tag for the specified run

        :param run_id: String id for the run
        :param tag: :py:class:`mlflow.entities.RunTag` instance to set
        """
        _validate_run_id(run_id)
        _validate_tag_name(tag.key)
        run_info = self._get_run_info(run_id)
        check_run_is_active(run_info)
        dynamodb = self._get_dynamodb_resource()
        table_name = "_".join([self.table_prefix, DynamodbStore.TAGS_TABLE])
        table = dynamodb.Table(table_name)
        response = table.put_item(
            Item={
                "run_id": run_id,
                "key": tag.key,
                "value": tag.value
            },
            ReturnConsumedCapacity="TOTAL",
        )
        if response["ResponseMetadata"]["HTTPStatusCode"] != 200:
            raise MlflowException("DynamoDB connection error")
        return True
Beispiel #5
0
 def set_tag(self, run_uuid, tag):
     _validate_run_id(run_uuid)
     _validate_tag_name(tag.key)
     run = self.get_run(run_uuid)
     tag_path = self._get_tag_path(run.info.experiment_id, run_uuid,
                                   tag.key)
     make_containing_dirs(tag_path)
     write_to(tag_path, "%s\n" % tag.value)
Beispiel #6
0
 def set_tag(self, run_id, tag):
     _validate_run_id(run_id)
     _validate_tag_name(tag.key)
     run = self.get_run(run_id)
     check_run_is_active(run.info)
     tag_path = self._get_tag_path(run.info.experiment_id, run_id, tag.key)
     make_containing_dirs(tag_path)
     # Don't add trailing newline
     write_to(tag_path, self._writeable_value(tag.value))
Beispiel #7
0
 def set_tag(self, run_id, key, value):
     """
     Set a tag on the run with the specified ID. Value is converted to a string.
     :param run_id: String ID of the run.
     :param key: Name of the tag.
     :param value: Tag value (converted to a string)
     """
     _validate_tag_name(key)
     tag = RunTag(key, str(value))
     self.store.set_tag(run_id, tag)
Beispiel #8
0
 def set_experiment_tag(self, experiment_id, key, value):
     """
     Set a tag on the experiment with the specified ID. Value is converted to a string.
     :param experiment_id: String ID of the experiment.
     :param key: Name of the tag.
     :param value: Tag value (converted to a string).
     """
     _validate_tag_name(key)
     tag = ExperimentTag(key, str(value))
     self.store.set_experiment_tag(experiment_id, tag)
Beispiel #9
0
 def _get_tag_from_file(parent_path, tag_name):
     _validate_tag_name(tag_name)
     tag_data = read_file(parent_path, tag_name)
     if len(tag_data) == 0:
         raise Exception("Tag '%s' is malformed. No data found." % tag_name)
     if len(tag_data) > 1:
         raise Exception(
             "Unexpected data for tag '%s'. Tag recorded more than once" %
             tag_name)
     return RunTag(tag_name, str(tag_data[0].strip()))
Beispiel #10
0
    def set_experiment_tag(self, experiment_id, tag):
        """
        Set a tag for the specified experiment

        :param experiment_id: String id for the experiment
        :param tag: :py:class:`mlflow.entities.ExperimentTag` instance to set
        """
        """
        Set a tag for the specified experiment

        :param experiment_id: String ID of the experiment
        :param tag: ExperimentRunTag instance to log
        """
        _validate_tag_name(tag.key)
        experiment = self.get_experiment(experiment_id)
        if experiment.lifecycle_stage != LifecycleStage.ACTIVE:
            raise MlflowException(
                "The experiment {} must be in the 'active'"
                "lifecycle_stage to set tags".format(experiment.experiment_id),
                error_code=INVALID_PARAMETER_VALUE,
            )
        # Set the tags on the table
        dynamodb = self._get_dynamodb_resource()
        table_name = "_".join(
            [self.table_prefix, DynamodbStore.EXPERIMENT_TABLE])
        table = dynamodb.Table(table_name)
        try:
            # Create the tags if not exists
            response = table.update_item(
                Key={"experiment_id": experiment_id},
                UpdateExpression="SET #a = :v",
                ConditionExpression="attribute_not_exists(#a)",
                ExpressionAttributeNames={"#a": "tags"},
                ExpressionAttributeValues={":v": {}},
            )
        except botocore.exceptions.ClientError as e:
            if e.response["Error"]["Code"] != "ConditionalCheckFailedException":
                raise e
        # Update the tags key/value
        response = table.update_item(
            Key={"experiment_id": experiment_id},
            UpdateExpression="SET #a.#b = :v",
            ConditionExpression="attribute_exists(#a)",
            ExpressionAttributeNames={
                "#a": "tags",
                "#b": tag.key
            },
            ExpressionAttributeValues={":v": tag.value},
        )
        if response["ResponseMetadata"]["HTTPStatusCode"] != 200:
            raise MlflowException("DynamoDB connection error")
        print(response)
Beispiel #11
0
 def set_experiment_tag(self, experiment_id, key, value):
     """
     Set a tag on the experiment with the specified ID. Value is converted to a string.
     :param experiment_id: String ID of the experiment.
     :param key: Name of the tag.
     :param value: Tag value (converted to a string).
     """
     if not self.ranger_can_authorize_experiment_id(experiment_id,
                                                    'update'):
         print("Access denied.")
         raise
     _validate_tag_name(key)
     tag = ExperimentTag(key, str(value))
     self.store.set_experiment_tag(experiment_id, tag)
Beispiel #12
0
    def set_tag(self, run_id, key, value):
        """
        Set a tag on the run with the specified ID. Value is converted to a string.

        :param run_id: String ID of the run.
        :param key: Tag name (string). This string may only contain alphanumerics, underscores
                    (_), dashes (-), periods (.), spaces ( ), and slashes (/).
                    All backend stores will support keys up to length 250, but some may
                    support larger keys.
        :param value: Tag value (string, but will be string-ified if not).
                      All backend stores will support values up to length 5000, but some
                      may support larger values.
        """
        _validate_tag_name(key)
        tag = RunTag(key, str(value))
        self.store.set_tag(run_id, tag)
Beispiel #13
0
    def delete_registered_model_tag(self, name, key):
        """
        Delete a tag associated with the registered model.

        :param name: Registered model name.
        :param key: Registered model tag key.
        :return: None
        """
        _validate_model_name(name)
        _validate_tag_name(key)
        with self.ManagedSessionMaker() as session:
            # check if registered model exists
            self._get_registered_model(session, name)
            existing_tag = self._get_registered_model_tag(session, name, key)
            if existing_tag is not None:
                session.delete(existing_tag)
Beispiel #14
0
    def set_experiment_tag(self, experiment_id, tag):
        """
        Set a tag for the specified experiment

        :param experiment_id: String ID of the experiment
        :param tag: ExperimentRunTag instance to log
        """
        _validate_tag_name(tag.key)
        experiment = self.get_experiment(experiment_id)
        if experiment.lifecycle_stage != LifecycleStage.ACTIVE:
            raise MlflowException(
                "The experiment {} must be in the 'active'"
                "lifecycle_stage to set tags".format(experiment.experiment_id),
                error_code=databricks_pb2.INVALID_PARAMETER_VALUE)
        tag_path = self._get_experiment_tag_path(experiment_id, tag.key)
        make_containing_dirs(tag_path)
        write_to(tag_path, self._writeable_value(tag.value))
Beispiel #15
0
    def delete_model_version_tag(self, name, version, key):
        """
        Delete a tag associated with the model version.

        :param name: Registered model name.
        :param version: Registered model version.
        :param key: Tag key.
        :return: None
        """
        _validate_model_name(name)
        _validate_model_version(version)
        _validate_tag_name(key)
        with self.ManagedSessionMaker() as session:
            # check if model version exists
            self._get_sql_model_version(session, name, version)
            existing_tag = self._get_model_version_tag(session, name, version, key)
            if existing_tag is not None:
                session.delete(existing_tag)
Beispiel #16
0
    def log_batch(self, run_id, metrics, params, tags):
        """
        Log multiple metrics, params, and/or tags.

        :param metrics: List of Metric(key, value, timestamp) instances.
        :param params: List of Param(key, value) instances.
        :param tags: List of RunTag(key, value) instances.

        Raises an MlflowException if any errors occur.
        :returns: None
        """
        for metric in metrics:
            _validate_metric(metric.key, metric.value, metric.timestamp)
        for param in params:
            _validate_param_name(param.key)
        for tag in tags:
            _validate_tag_name(tag.key)
        self.store.log_batch(run_id=run_id, metrics=metrics, params=params, tags=tags)
Beispiel #17
0
 def _get_tag_from_file(parent_path, tag_name):
     _validate_tag_name(tag_name)
     tag_data = read_file(parent_path, tag_name)
     return RunTag(tag_name, tag_data)
Beispiel #18
0
 def _get_tag_path(self, experiment_id, run_uuid, tag_name):
     _validate_run_id(run_uuid)
     _validate_tag_name(tag_name)
     return os.path.join(self._get_run_dir(experiment_id, run_uuid),
                         FileStore.TAGS_FOLDER_NAME, tag_name)
Beispiel #19
0
def test_validate_tag_name():
    for good_name in GOOD_METRIC_OR_PARAM_NAMES:
        _validate_tag_name(good_name)
    for bad_name in BAD_METRIC_OR_PARAM_NAMES:
        with pytest.raises(Exception, match="Invalid tag name"):
            _validate_tag_name(bad_name)
Beispiel #20
0
 def set_tag(self, run_id, key, value):
     """Sets a tag on the given run id. Value will be converted to a string."""
     _validate_tag_name(key)
     tag = RunTag(key, str(value))
     self.store.set_tag(run_id, tag)
Beispiel #21
0
 def set_tag(self, run_id, tag):
     _validate_run_id(run_id)
     _validate_tag_name(tag.key)
     run_info = self._get_run_info(run_id)
     check_run_is_active(run_info)
     self._set_run_tag(run_info, tag)