Skip to content

Commit

Permalink
Version 1.0.3: delete assets
Browse files Browse the repository at this point in the history
  • Loading branch information
dsblank committed Jun 5, 2023
1 parent 1cf8593 commit 21f5123
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cometx/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
# the express permission of Comet ML Inc.
# *******************************************************

version_info = (1, 0, 2)
version_info = (1, 0, 3)
__version__ = ".".join(map(str, version_info))
4 changes: 3 additions & 1 deletion cometx/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
cometx log
cometx list
cometx reproduce
cometx delete
For more information:
cometx COMMAND --help
Expand All @@ -28,7 +29,7 @@
from cometx import __version__

# Import CLI commands:
from . import download, list_command, log, reproduce
from . import download, list_command, log, reproduce, delete


def add_subparser(subparsers, module, name):
Expand Down Expand Up @@ -69,6 +70,7 @@ def main(raw_args=sys.argv[1:]):
# Register CLI commands:
add_subparser(subparsers, download, "download")
add_subparser(subparsers, log, "log")
add_subparser(subparsers, delete, "delete")
add_subparser(subparsers, list_command, "list")
add_subparser(subparsers, reproduce, "reproduce")

Expand Down
133 changes: 133 additions & 0 deletions cometx/cli/delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ****************************************
# __
# _________ ____ ___ ___ / /__ __
# / ___/ __ \/ __ `__ \/ _ \/ __/ |/_/
# / /__/ /_/ / / / / / / __/ /__> <
# \___/\____/_/ /_/ /_/\___/\__/_/|_|
#
#
# Copyright (c) 2022 Cometx Development
# Team. All rights reserved.
# ****************************************
"""
Examples:
To delete experiments assets:
$ cometx delete WORKSPACE/PROJECT --type=image
$ cometx delete WORKSPACE/PROJECT/EXPERIMENT --type=all
Where TYPE is one of the following names:
* all
* asset
* audio
* code
* image
* notebook
* text-sample
* video
"""
import argparse
import glob
import json
import os
import sys

from comet_ml import API

from ..utils import get_file_extension

ADDITIONAL_ARGS = False
def get_parser_arguments(parser):
parser.add_argument(
"COMET_PATH",
help=(
"The Comet identifier, such as 'WORKSPACE', 'WORKSPACE/PROJECT', or "
+ "'WORKSPACE/PROJECT/EXPERIMENT'. Leave empty to use defaults."
),
nargs="?",
default=None,
type=str,
)
parser.add_argument(
"--type",
help="The type of item to log",
type=str,
default=None,
)
parser.add_argument(
"--debug",
help="If given, allow debugging",
default=False,
action="store_true"
)


def delete(parsed_args, remaining=None):
# Called via `cometx delete ...`
try:
delete_cli(parsed_args)
except KeyboardInterrupt:
print("Canceled by CONTROL+C")
except Exception as exc:
if parsed_args.debug:
raise
else:
print("ERROR: " + str(exc))


def delete_cli(parsed_args):
comet_path = (
parsed_args.COMET_PATH.split("/") if parsed_args.COMET_PATH is not None else []
)

if not comet_path:
workspace = None
project_name = None
experiment_key = None
elif len(comet_path) == 1:
workspace = comet_path[0]
project_name = None
experiment_key = None
elif len(comet_path) == 2:
workspace, project_name = comet_path
experiment_key = None
elif len(comet_path) == 3:
workspace, project_name, experiment_key = comet_path
else:
raise Exception("invalid COMET_PATH: %r" % parsed_args.COMET_PATH)

api = API()

if experiment_key:
experiments = [api.get_experiment(workspace, project_name, experiment_key)]
else:
experiments = api.get_experiments(workspace, project_name)

delete_experiment_assets(api, experiments, parsed_args.type)

def delete_experiment_assets(api, experiments, asset_type):
count = 0
for experiment in experiments:
print("Looking in %s..." % experiment.url)
assets = experiment.get_asset_list(asset_type)
for asset in assets:
api._client.delete_experiment_asset(experiment.id, asset["assetId"])
count += 1
print("Deleted %d assets of type %r" % (count, asset_type))

def main(args):
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
get_parser_arguments(parser)
parsed_args = parser.parse_args(args)
delete(parsed_args)


if __name__ == "__main__":
# Called via `python -m cometx.cli.delete ...`
main(sys.argv[1:])

0 comments on commit 21f5123

Please sign in to comment.