def __save(self): os.makedirs(self.__curr_dir) for file, md5 in fo.get_meta()["files"].items(): copyfile(file, os.path.join(self.__curr_dir, md5)) with open(os.path.join(self.__curr_dir, "meta.json"), "w+") as fp: json.dump(self.__meta, fp, indent=2) config = fo.get_config() meta = fo.get_meta() meta["current_trial"] = self.__id try: if self.__model_name and self.__api: meta["model_name"] = self.__model_name project_id = config["project_id"] if self.__model_name in meta["models"]: model_id = meta["models"][self.__model_name] else: model_id = self.__api.post_model(self.__model_name, project_id) meta["models"][self.__model_name] = model_id self.__api.post_run(project_id, model_id, meta) except Exception as e: logging.exception("Problem using tracker.ml API") finally: fo.set_meta(meta)
def echo_status(sort_key: str, reverse: bool, limit: int, ctx=None): current_trial = fo.get_meta(ctx=ctx)["current_trial"] metas = [fo.get_meta(tid) for tid in fo.get_trial_ids()] click.echo(" Total trials: {}".format(len(metas))) if len(metas) == 0: return for meta in metas: for key in list(meta.keys()): if isinstance(meta[key], list): meta.pop(key) click.echo((" Reverse sorted" if reverse else " Sorted") + " by: {}".format(sort_key)) metas.sort(key=lambda x: int(x[sort_key]), reverse=bool(not reverse)) metas = [m for m in metas if list(m.keys()) == list(metas[0].keys())] if len(metas) > limit: click.echo(" Only displaying {} results".format(limit)) metas = metas[:limit] max_len_values = dict([(k, len(k)) for k, _ in metas[0].items()]) for meta in metas: for k, v in meta.items(): if max_len_values[k] < len(str(v)): max_len_values[k] = len(str(v)) data_str = [] total_width = 1 for key, max_len in max_len_values.items(): width = max_len + 4 total_width += width data_str.append("{:^" + str(width) + "}") click.echo("") click.echo( "|".join(data_str).format(*[k.title() for k in max_len_values.keys()])) click.echo("-" * total_width) for meta in metas: color = "blue" if meta["id"] == current_trial else None click.secho("|".join(data_str).format(*list(meta.values())), fg=color)
def remove_file(path: str, ctx=None): meta = fo.get_meta(ctx) if os.path.isfile(path): if path not in meta["files"]: click.secho("Error: {} is not tracked".format(path), fg="red") return meta["files"].pop(path) else: for (dir_path, _, files) in os.walk(path): for f in files: p = os.path.join(dir_path, f) if p in meta["files"]: meta["files"].pop(p) fo.set_meta(meta, ctx)
def add_file(path: str, ctx=None): meta = fo.get_meta(ctx=ctx) if not os.path.exists(path): click.secho("Error: {} could not be located".format(path), fg="red") return elif os.path.isfile(path): if path not in meta["files"]: meta["files"][path] = __hash(path) click.echo("Added {}".format(path)) else: for (dir_path, _, files) in os.walk(path): for f in files: p = os.path.join(dir_path, f) if p not in meta["files"]: meta["files"][p] = __hash(p) click.echo("Added {}".format(p)) fo.set_meta(meta, ctx)
def deploy_trial(run_id: int, ctx=None): trial_path = os.path.join(fo.get_trials_dir(ctx), str(run_id)) if not os.path.exists(trial_path): raise FileNotFoundError( "Run with ID {} couldn't be located".format(run_id)) meta = fo.get_meta(ctx=ctx) files = dict([(v, k) for k, v in meta["files"].items()]) for src_path in os.listdir(trial_path): if src_path not in files: continue dst_path = files[src_path] if not os.path.exists(dst_path): continue os.remove(dst_path) copyfile(os.path.join(trial_path, src_path), dst_path) meta["current_trial"] = run_id fo.set_meta(meta, ctx)
def redo(ctx, increment): """ Revert files to incremented run id """ tools.deploy_trial(fo.get_meta(ctx=ctx)["current_trial"] + increment, ctx)
def undo(ctx, decrement): """ Revert files to decremented run id """ tools.deploy_trial(fo.get_meta(ctx=ctx)["current_trial"] - decrement, ctx)