Skip to content

Commit

Permalink
paquo._cli: implement create command
Browse files Browse the repository at this point in the history
  • Loading branch information
ap-- committed Aug 11, 2020
1 parent d1678da commit 26f33b8
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
48 changes: 47 additions & 1 deletion paquo/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
import functools
import sys
from contextlib import redirect_stdout
from itertools import repeat
from pathlib import Path

from paquo._cli import subcommand, argument, DirectoryType, \
config_print_settings, config_print_defaults, list_project, export_annotations
config_print_settings, config_print_defaults, list_project, export_annotations, create_project
from paquo._config import PAQUO_CONFIG_FILENAME, get_searchtree

# noinspection PyTypeChecker
Expand Down Expand Up @@ -104,6 +105,51 @@ def list_(args, subparser):
return 1


@subcommand(
argument('project_path', nargs='?', default=None, help="path to new qupath project"),
argument('--classes', nargs='*', help="path classes to add to project"),
argument('--class-colors', nargs='*', help="path class colors in order of classes"),
argument('--remove-default-classes', action="store_true", help="don't add default classes"),
argument('--images', nargs='*', help="images to add to project"),
argument('--force', action="store_true", help="force overwrite existing projects"),
)
def create(args, subparser):
"""create a new qupath project"""
if not args.project_path:
print(subparser.format_help())
return 0

if args.class_colors and len(args.class_colors) != len(args.classes):
print("ERROR: need to specify same number of colors as classes")
return 1

if args.classes and len(args.classes) != len(set(args.classes)):
print("ERROR: classes have to be unique")
return 1

class_names_colors = zip(args.classes or [], args.class_colors or repeat(None))

images = args.images or []
for image in images:
if not Path(image).is_file():
print(f"ERROR: image {image} is not a file")
return 1

try:
name = create_project(
args.project_path,
class_names_colors=class_names_colors,
images=images,
remove_default_classes=args.remove_default_classes,
force_write=args.force
)
except FileExistsError:
print(f"ERROR: project {args.project_path} exists! use --force to overwrite")
return 1
print(f"project '{name}' created")
return 0


@subcommand(
argument('project_path', nargs='?', default=None, help="path to your qupath project file/folder"),
argument('--image-idx', '-i', required=True, type=int, help="index of a qupath image"),
Expand Down
28 changes: 28 additions & 0 deletions paquo/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,34 @@ def list_project(path):
print(" ".join(line))


# -- create related commands -------------------------------------------

def create_project(project_path, class_names_colors, images,
remove_default_classes=False, force_write=False):
"""create a qupath project"""
from paquo.classes import QuPathPathClass
from paquo.projects import QuPathProject

mode = 'x' if not force_write else 'w'

# noinspection PyTypeChecker
with QuPathProject(project_path, mode=mode) as qp:
if remove_default_classes:
qp.path_classes = ()
path_classes = list(qp.path_classes)
for name, color in class_names_colors:
path_classes.append(
QuPathPathClass.create(name, color=color)
)
qp.path_classes = path_classes

for image in images:
qp.add_image(image)

name = qp.name
return name


# -- export related commands -------------------------------------------

def export_annotations(path, image_idx, pretty=False):
Expand Down

0 comments on commit 26f33b8

Please sign in to comment.