Exemple #1
0
    def log_param(self, run_id, param):
        """
        Log a param for the specified run

        :param run_id: String id for the run
        :param param: :py:class:`mlflow.entities.Param` instance to log
        """
        _validate_run_id(run_id)
        _validate_param_name(param.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.PARAMS_TABLE])
        table = dynamodb.Table(table_name)
        response = table.put_item(
            Item={
                "run_id": run_id,
                "key": param.key,
                "value": param.value
            },
            ReturnConsumedCapacity="TOTAL",
            ReturnValues="ALL_OLD",
        )
        if response["ResponseMetadata"]["HTTPStatusCode"] != 200:
            raise MlflowException("DynamoDB connection error")
        return True
Exemple #2
0
    def record_logged_model(self, run_id, mlflow_model):
        from mlflow.models import Model

        if not isinstance(mlflow_model, Model):
            raise TypeError(
                "Argument 'mlflow_model' should be mlflow.models.Model, got '{}'"
                .format(type(mlflow_model)))
        _validate_run_id(run_id)
        run_info = self._get_run_info(run_id)
        check_run_is_active(run_info)
        model_dict = mlflow_model.to_dict()
        run_info = self._get_run_info(run_id)
        path = self._get_tag_path(run_info.experiment_id, run_info.run_id,
                                  MLFLOW_LOGGED_MODELS)
        if os.path.exists(path):
            with open(path, "r") as f:
                model_list = json.loads(f.read())
        else:
            model_list = []
        tag = RunTag(MLFLOW_LOGGED_MODELS,
                     json.dumps(model_list + [model_dict]))

        try:
            self._set_run_tag(run_info, tag)
        except Exception as e:
            raise MlflowException(e, INTERNAL_ERROR)
Exemple #3
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
Exemple #4
0
 def delete_tag(self, run_id, key):
     """
     Delete a tag from a run. This is irreversible.
     :param run_id: String ID of the run
     :param key: Name of the tag
     """
     _validate_run_id(run_id)
     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.get_item(Key={"run_id": run_id, "key": key})
     if not "Item" in response:
         raise MlflowException(
             "No tag with name: {} in run with id {}".format(key, run_id),
             error_code=RESOURCE_DOES_NOT_EXIST,
         )
     response = table.delete_item(Key={
         "run_id": run_id,
         "key": key
     },
                                  ReturnConsumedCapacity="TOTAL")
     if response["ResponseMetadata"]["HTTPStatusCode"] != 200:
         raise MlflowException("DynamoDB connection error")
     return True
Exemple #5
0
 def update_run_info(self, run_id, run_status, end_time):
     _validate_run_id(run_id)
     run_info = self.get_run(run_id).info
     check_run_is_active(run_info)
     new_info = run_info._copy_with_overrides(run_status, end_time)
     self._overwrite_run_info(new_info)
     return new_info
Exemple #6
0
 def log_metric(self, run_id, metric):
     _validate_run_id(run_id)
     _validate_metric_name(metric.key)
     run = self.get_run(run_id)
     check_run_is_active(run.info)
     metric_path = self._get_metric_path(run.info.experiment_id, run_id, metric.key)
     make_containing_dirs(metric_path)
     append_to(metric_path, "%s %s %s\n" % (metric.timestamp, metric.value, metric.step))
Exemple #7
0
 def delete_run(self, run_id):
     run_info = self._get_run_info(run_id)
     if run_info is None:
         raise MlflowException("Run '%s' metadata is in invalid state." % run_id,
                               databricks_pb2.INVALID_STATE)
     check_run_is_active(run_info)
     new_info = run_info._copy_with_overrides(lifecycle_stage=LifecycleStage.DELETED)
     self._overwrite_run_info(new_info)
Exemple #8
0
 def log_param(self, run_id, param):
     _validate_run_id(run_id)
     _validate_param_name(param.key)
     run = self.get_run(run_id)
     check_run_is_active(run.info)
     param_path = self._get_param_path(run.info.experiment_id, run_id, param.key)
     make_containing_dirs(param_path)
     write_to(param_path, self._writeable_value(param.value))
Exemple #9
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))
Exemple #10
0
    def update_run_info(self, run_id, run_status, end_time):
        """
        Update the metadata of the specified run.

        :return: :py:class:`mlflow.entities.RunInfo` describing the updated run.
        """
        _validate_run_id(run_id)
        run_info = self._get_run_info(run_id)
        check_run_is_active(run_info)
        return self._update_run_info(run_id, run_status, end_time)
Exemple #11
0
    def delete_run(self, run_id):
        """
        Delete a run.

        :param run_id
        """
        _validate_run_id(run_id)
        run_info = self._get_run_info(run_id)
        check_run_is_active(run_info)
        return self._update_run_status(run_id, LifecycleStage.ACTIVE,
                                       LifecycleStage.DELETED)
Exemple #12
0
 def log_batch(self, run_id, metrics, params, tags):
     _validate_run_id(run_id)
     run = self.get_run(run_id)
     check_run_is_active(run.info)
     try:
         for param in params:
             self.log_param(run_id, param)
         for metric in metrics:
             self.log_metric(run_id, metric)
         for tag in tags:
             self.set_tag(run_id, tag)
     except Exception as e:
         raise MlflowException(e, INTERNAL_ERROR)
Exemple #13
0
 def delete_tag(self, run_id, key):
     """
     Delete a tag from a run. This is irreversible.
     :param run_id: String ID of the run
     :param key: Name of the tag
     """
     _validate_run_id(run_id)
     run = self.get_run(run_id)
     check_run_is_active(run.info)
     if key not in run.data.tags.keys():
         raise MlflowException(
             "No tag with name: {} in run with id {}".format(key, run_id),
             error_code=RESOURCE_DOES_NOT_EXIST)
     tag_path = self._get_tag_path(run.info.experiment_id, run_id, key)
     os.remove(tag_path)
Exemple #14
0
 def log_batch(self, run_id, metrics, params, tags):
     _validate_run_id(run_id)
     _validate_batch_log_data(metrics, params, tags)
     _validate_batch_log_limits(metrics, params, tags)
     run_info = self._get_run_info(run_id)
     check_run_is_active(run_info)
     try:
         for param in params:
             self._log_run_param(run_info, param)
         for metric in metrics:
             self._log_run_metric(run_info, metric)
         for tag in tags:
             self._set_run_tag(run_info, tag)
     except Exception as e:
         raise MlflowException(e, INTERNAL_ERROR)
Exemple #15
0
    def log_metric(self, run_id, metric):
        """
        Log a metric for the specified run

        :param run_id: String id for the run
        :param metric: :py:class:`mlflow.entities.Metric` instance to log
        """
        _validate_run_id(run_id)
        _validate_metric_name(metric.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.METRICS_TABLE])
        table = dynamodb.Table(table_name)
        # Append metrics to head of list, so the first element is most recent
        response = table.update_item(
            Key={
                "run_id": run_id,
                "key": metric.key
            },
            UpdateExpression="SET #m = list_append(:m, if_not_exists(#m, :e))",
            ExpressionAttributeNames={"#m": "metrics"},
            ExpressionAttributeValues={
                ":e": [],
                ":m": [{
                    "value": Decimal(str(metric.value)),
                    "timestamp": metric.timestamp,
                    "step": metric.step,
                }],
            },
            ReturnValues="NONE",
            ReturnConsumedCapacity="TOTAL",
        )
        if response["ResponseMetadata"]["HTTPStatusCode"] != 200:
            raise MlflowException("DynamoDB connection error")
        return True
Exemple #16
0
 def delete_run(self, run_id):
     run_info = self._get_run_info(run_id)
     check_run_is_active(run_info)
     new_info = run_info._copy_with_overrides(
         lifecycle_stage=RunInfo.DELETED_LIFECYCLE)
     self._overwrite_run_info(new_info)
Exemple #17
0
 def log_metric(self, run_id, metric):
     _validate_run_id(run_id)
     _validate_metric_name(metric.key)
     run_info = self._get_run_info(run_id)
     check_run_is_active(run_info)
     self._log_run_metric(run_info, metric)
Exemple #18
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)
Exemple #19
0
 def log_param(self, run_id, param):
     _validate_run_id(run_id)
     _validate_param_name(param.key)
     run_info = self._get_run_info(run_id)
     check_run_is_active(run_info)
     self._log_run_param(run_info, param)