def test_validate_param_name(): for good_name in GOOD_METRIC_OR_PARAM_NAMES: _validate_param_name(good_name) for bad_name in BAD_METRIC_OR_PARAM_NAMES: with pytest.raises(MlflowException, match="Invalid parameter name") as e: _validate_param_name(bad_name) assert e.value.error_code == ErrorCode.Name(INVALID_PARAMETER_VALUE)
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))
def log_param(self, run_uuid, param): _validate_run_id(run_uuid) _validate_param_name(param.key) run = self.get_run(run_uuid) param_path = self._get_param_path(run.info.experiment_id, run_uuid, param.key) make_containing_dirs(param_path) write_to(param_path, "%s\n" % param.value)
def get_param(self, run_uuid, param_name): _validate_run_id(run_uuid) _validate_param_name(param_name) parent_path, param_files = self._get_run_files(run_uuid, "param") if param_name not in param_files: raise MlflowException("Param '%s' not found under run '%s'" % (param_name, run_uuid), databricks_pb2.RESOURCE_DOES_NOT_EXIST) return self._get_param_from_file(parent_path, param_name)
def _get_experiment_tag_path(self, experiment_id, tag_name): _validate_experiment_id(experiment_id) _validate_param_name(tag_name) if not self._has_experiment(experiment_id): return None return os.path.join( self._get_experiment_path(experiment_id, assert_exists=True), FileStore.TAGS_FOLDER_NAME, tag_name)
def get_param(self, run_uuid, param_name): _validate_run_id(run_uuid) _validate_param_name(param_name) parent_path, param_files = self._get_run_files(run_uuid, "param") if param_name not in param_files: raise Exception("Param '%s' not found under run '%s'" % (param_name, run_uuid)) return self._get_param_from_file(parent_path, param_name)
def _get_param_from_file(parent_path, param_name): _validate_param_name(param_name) param_data = read_file_lines(parent_path, param_name) if len(param_data) == 0: raise Exception("Param '%s' is malformed. No data found." % param_name) if len(param_data) > 1: raise Exception("Unexpected data for param '%s'. Param recorded more than once" % param_name) return Param(param_name, str(param_data[0].strip()))
def _get_param_from_file(parent_path, param_name): _validate_param_name(param_name) param_data = read_file_lines(parent_path, param_name) if len(param_data) > 1: raise Exception("Unexpected data for param '%s'. Param recorded more than once" % param_name) # The only cause for param_data's length to be zero is the param's # value is an empty string value = '' if len(param_data) == 0 else str(param_data[0].strip()) return Param(param_name, value)
def get_param(self, run_id, param_name): _validate_run_id(run_id) _validate_param_name(param_name) params = self._get_run_params(run_id, param_name) if not params: raise MlflowException( "Param '%s' not found under run '%s'" % (param_name, run_id), RESOURCE_DOES_NOT_EXIST, ) return _list_to_run_param(params)[0]
def log_param(self, run_id, key, value): """ Log a parameter against the run ID. Value is converted to a string. """ _validate_param_name(key) param = Param(key, str(value)) try: self.store.log_param(run_id, param) except MlflowException as e: if e.error_code == ErrorCode.Name(INVALID_PARAMETER_VALUE): msg = f"{e.message}{PARAM_VALIDATION_MSG}'" raise MlflowException(msg, INVALID_PARAMETER_VALUE) else: raise e
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)
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)
def _get_param_path(self, experiment_id, run_uuid, param_name): _validate_run_id(run_uuid) _validate_param_name(param_name) return os.path.join(self._get_run_dir(experiment_id, run_uuid), FileStore.PARAMS_FOLDER_NAME, param_name)
def log_param(self, run_id, key, value): """Logs a parameter against the given run id. Value will be converted to a string.""" _validate_param_name(key) param = Param(key, str(value)) self.store.log_param(run_id, param)
def log_param(self, param): _validate_param_name(param.key) self.store.log_param(self.run_info.run_uuid, param)
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)
def test_validate_param_name(): for good_name in GOOD_METRIC_OR_PARAM_NAMES: _validate_param_name(good_name) for bad_name in BAD_METRIC_OR_PARAM_NAMES: with pytest.raises(Exception, match="Invalid parameter name"): _validate_param_name(bad_name)
def _get_param_from_file(parent_path, param_name): _validate_param_name(param_name) value = read_file(parent_path, param_name) return Param(param_name, value)