def params_prepare_for_save(fields: dict, previous_task: Task = None): """ If legacy hyper params or configuration is passed then replace the corresponding section in the new structure Escape all the section and param names for hyper params and configuration to make it mongo sage """ for old_params_field, new_params_field, default_section in ( ("execution/parameters", "hyperparams", hyperparams_default_section), ("execution/model_desc", "configuration", None), ): legacy_params = safe_get(fields, old_params_field) if legacy_params is None: continue if ( not safe_get(fields, new_params_field) and previous_task and previous_task[new_params_field] ): previous_data = previous_task.to_proper_dict().get(new_params_field) removed = _remove_legacy_params( previous_data, with_sections=default_section is not None ) if not legacy_params and not removed: # if we only need to delete legacy fields from the db # but they are not there then there is no point to proceed continue fields_update = {new_params_field: previous_data} params_unprepare_from_saved(fields_update) fields.update(fields_update) for full_name, value in legacy_params.items(): section, name = split_param_name(full_name, default_section) new_path = list(filter(None, (new_params_field, section, name))) new_param = dict(name=name, type=hyperparams_legacy_type, value=str(value)) if section is not None: new_param["section"] = section dpath.new(fields, new_path, new_param) dpath.delete(fields, old_params_field) for param_field in ("hyperparams", "configuration"): params = safe_get(fields, param_field) if params: escaped_params = { ParameterKeyEscaper.escape(key): { ParameterKeyEscaper.escape(k): v for k, v in value.items() } if isinstance(value, dict) else value for key, value in params.items() } dpath.set(fields, param_field, escaped_params)
def prepare_create_fields(call: APICall, valid_fields=None, output=None, previous_task: Task = None): valid_fields = valid_fields if valid_fields is not None else create_fields t_fields = task_fields t_fields.add("output_dest") fields = parse_from_call(call.data, valid_fields, t_fields) # Move output_dest to output.destination output_dest = fields.get("output_dest") if output_dest is not None: fields.pop("output_dest") if output: output.destination = output_dest else: output = Output(destination=output_dest) fields["output"] = output try: dpath.delete(fields, "script/requirements") except dpath.exceptions.PathNotFound: pass # Make sure there are no duplicate tags tags = fields.get("tags") if tags: fields["tags"] = list(set(tags)) # Strip all script fields (remove leading and trailing whitespace chars) to avoid unusable names and paths for field in task_script_fields: try: path = "script/%s" % field value = dpath.get(fields, path) if isinstance(value, six.string_types): value = value.strip() dpath.set(fields, path, value) except KeyError: pass parameters = safe_get(fields, "execution/parameters") if parameters is not None: parameters = {k.strip(): v for k, v in parameters.items()} dpath.set(fields, "execution/parameters", parameters) return fields
def delete(self, path): if (path == '/'): del self.info return True try: return dpath.delete(self.info, path) except dpath.exceptions.PathNotFound: return False
def _upgrade_task_data(task_data: dict): for old_param_field, new_param_field, default_section in ( ("execution/parameters", "hyperparams", hyperparams_default_section), ("execution/model_desc", "configuration", None), ): legacy = safe_get(task_data, old_param_field) if not legacy: continue for full_name, value in legacy.items(): section, name = split_param_name(full_name, default_section) new_path = list(filter(None, (new_param_field, section, name))) if not safe_get(task_data, new_path): new_param = dict( name=name, type=hyperparams_legacy_type, value=str(value) ) if section is not None: new_param["section"] = section dpath.new(task_data, new_path, new_param) dpath.delete(task_data, old_param_field)