From bc905f4cfa7eb70955e411eaf8126996f30e41dc Mon Sep 17 00:00:00 2001 From: Trent Smith <1429913+Bento007@users.noreply.github.com> Date: Tue, 3 Dec 2024 11:19:15 -0800 Subject: [PATCH] fix: clean up the cli (#1108) --- cellxgene_schema_cli/cellxgene_schema/cli.py | 37 +++++++++---------- .../cellxgene_schema/validate.py | 4 -- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/cellxgene_schema_cli/cellxgene_schema/cli.py b/cellxgene_schema_cli/cellxgene_schema/cli.py index 33fce82b6..1254a7ba1 100644 --- a/cellxgene_schema_cli/cellxgene_schema/cli.py +++ b/cellxgene_schema_cli/cellxgene_schema/cli.py @@ -1,7 +1,10 @@ +import logging import sys import click +logger = logging.getLogger("cellxgene_schema") + @click.group( name="schema", @@ -9,11 +12,13 @@ short_help="Apply and validate the cellxgene data integration schema to an h5ad file.", context_settings=dict(max_content_width=85, help_option_names=["-h", "--help"]), ) -def schema_cli(): - pass +@click.option("-v", "--verbose", help="When present will set logging level to debug", is_flag=True) +def schema_cli(verbose): + logging.basicConfig(level=logging.ERROR) + logger.setLevel(logging.DEBUG if verbose else logging.INFO) -@click.command( +@schema_cli.command( name="validate", short_help="Check that an h5ad follows the cellxgene data integration schema.", help="Check that an h5ad follows the cellxgene data integration schema. If validation fails this command will " @@ -31,27 +36,25 @@ def schema_cli(): type=click.Path(exists=False, dir_okay=False, writable=True), ) @click.option("-i", "--ignore-labels", help="Ignore ontology labels when validating", is_flag=True) -@click.option("-v", "--verbose", help="When present will set logging level to debug", is_flag=True) -def schema_validate(h5ad_file, add_labels_file, ignore_labels, verbose): +def schema_validate(h5ad_file, add_labels_file, ignore_labels): # Imports are very slow so we defer loading until Click arg validation has passed - - print("Loading dependencies") + logger.info("Loading dependencies") try: import anndata # noqa: F401 except ImportError: raise click.ClickException("[cellxgene] cellxgene-schema requires anndata") from None - print("Loading validator modules") + logger.info("Loading validator modules") from .validate import validate - is_valid, _, _ = validate(h5ad_file, add_labels_file, ignore_labels=ignore_labels, verbose=verbose) + is_valid, _, _ = validate(h5ad_file, add_labels_file, ignore_labels=ignore_labels) if is_valid: sys.exit(0) else: sys.exit(1) -@click.command( +@schema_cli.command( name="remove-labels", short_help="Create a copy of an h5ad without portal-added labels", help="Create a copy of an h5ad without portal-added labels.", @@ -61,24 +64,24 @@ def schema_validate(h5ad_file, add_labels_file, ignore_labels, verbose): def remove_labels(input_file, output_file): from .remove_labels import AnnDataLabelRemover - print("Loading dependencies") + logger.info("Loading dependencies") try: import anndata # noqa: F401 except ImportError: raise click.ClickException("[cellxgene] cellxgene-schema requires anndata") from None - print(f"Loading h5ad from {input_file}") + logger.info(f"Loading h5ad from {input_file}") adata = anndata.read_h5ad(input_file) anndata_label_remover = AnnDataLabelRemover(adata) if not anndata_label_remover.schema_def: return - print("Removing labels") + logger.info("Removing labels") anndata_label_remover.remove_labels() - print(f"Labels have been removed. Writing to {output_file}") + logger.info(f"Labels have been removed. Writing to {output_file}") anndata_label_remover.adata.write(output_file, compression="gzip") -@click.command( +@schema_cli.command( name="migrate", short_help="Convert an h5ad to the latest schema version.", help="Convert an h5ad from the previous to latest minor schema version. No validation will be " @@ -94,9 +97,5 @@ def migrate(input_file, output_file, collection_id, dataset_id): migrate(input_file, output_file, collection_id, dataset_id) -schema_cli.add_command(schema_validate) -schema_cli.add_command(migrate) -schema_cli.add_command(remove_labels) - if __name__ == "__main__": schema_cli() diff --git a/cellxgene_schema_cli/cellxgene_schema/validate.py b/cellxgene_schema_cli/cellxgene_schema/validate.py index 434983bc0..4f92ef053 100644 --- a/cellxgene_schema_cli/cellxgene_schema/validate.py +++ b/cellxgene_schema_cli/cellxgene_schema/validate.py @@ -2130,10 +2130,6 @@ def validate( # Perform validation start = datetime.now() - if verbose: - logging.basicConfig(level=logging.DEBUG) - else: - logging.basicConfig(level=logging.INFO, format="%(message)s") validator = Validator( ignore_labels=ignore_labels, )