diff --git a/apis/python/README-dev.md b/apis/python/README-dev.md index 78967ed2de..a1adc432ae 100644 --- a/apis/python/README-dev.md +++ b/apis/python/README-dev.md @@ -1,8 +1,8 @@ * Most things are configured using GitHub Actions at `../../.github/workflows` * Pre-push suggestions: - * `black .` - * `isort .` - * `flake8` + * `black . tools/[a-z]*` + * `isort . tools/[a-z]*` + * `flake8 . tools/[a-z]*` * `python -m pytest tests` * PyPI: * https://pypi.org/project/tiledbsc/ diff --git a/apis/python/examples/uniformizer.py b/apis/python/examples/uniformizer.py index 69d2f970f3..807d154b74 100755 --- a/apis/python/examples/uniformizer.py +++ b/apis/python/examples/uniformizer.py @@ -37,7 +37,7 @@ import scipy.stats import tiledb -import tiledbsc.logger +import tiledbsc.logging from tiledbsc import SOMA, SOMACollection from tiledbsc import io as SOMAio @@ -50,8 +50,14 @@ def main() -> int: parser = _create_args_parser() args = parser.parse_args() - # XXX if args.verbose: + # tiledbsc.logging.logger.setLevel(logging.INFO) + # logger = logging.getLogger('tiledbsc') + # logger.setLevel(logging.INFO) + # logging.getLogger("tiledbsc").setLevel(logging.INFO) + # Not able to get any of the above to 'stick'. The following sets level for the whole app, + # not just the tiledbsc library, but that's an acceptable workaround since this CLI does + # nothing except invoke the tiledbsc library. logging.basicConfig(level=logging.INFO) uniformizer = Uniformizer( @@ -198,7 +204,7 @@ def add_h5ad(self, input_dataset_id: Optional[str], input_h5ad_path) -> int: if soma_name in soco: raise Exception(f"SOMA {soma_name} is already in SOMACollection {soco.uri}") - tiledbsc.logger.info("Loading H5AD") + tiledbsc.logging.logger.info("Loading H5AD") ann = anndata.read_h5ad(input_h5ad_path) self._clean_and_add(ann, soma_name, soco) @@ -213,7 +219,7 @@ def add_soma(self, input_dataset_id: Optional[str], input_soma_uri: str) -> int: if soma_name in soco: raise Exception(f"SOMA {soma_name} is already in SOMACollection {soco.uri}") - tiledbsc.logger.info("Loading SOMA") + tiledbsc.logging.logger.info("Loading SOMA") input_soma = SOMA(input_soma_uri) ann = SOMAio.to_anndata(input_soma) @@ -250,24 +256,24 @@ def _clean_and_add( Cleans and uniformizes the data (whether obtained from H5AD or SOMA), writes a new SOMA, adds an X/rankit layer, and adds the new SOMA to the SOMACollection. """ - tiledbsc.logger.info("Cleaning data") + tiledbsc.logging.logger.info("Cleaning data") ann = self._clean_and_uniformize(ann) - tiledbsc.logger.info("Creating rankit") + tiledbsc.logging.logger.info("Creating rankit") X_rankit = _rankit(ann.X) - tiledbsc.logger.info("Saving SOMA") + tiledbsc.logging.logger.info("Saving SOMA") soma_uri = f"{self.atlas_uri}/{soma_name}" atlas_soma = SOMA(uri=soma_uri, name=soma_name, ctx=self.ctx) SOMAio.from_anndata(atlas_soma, ann) - tiledbsc.logger.info( + tiledbsc.logging.logger.info( f"Adding SOMA name {atlas_soma.name} at SOMA URI {atlas_soma.uri}" ) soco.add(atlas_soma) # Create rankit X layer and save - tiledbsc.logger.info("Saving rankit layer") + tiledbsc.logging.logger.info("Saving rankit layer") if "rankit" in atlas_soma.X.keys(): raise Exception( f"rankit layer already exists in the SOMA {atlas_soma.name} {atlas_soma.uri}" diff --git a/apis/python/src/tiledbsc/logging.py b/apis/python/src/tiledbsc/logging.py new file mode 100644 index 0000000000..e1bbe445e4 --- /dev/null +++ b/apis/python/src/tiledbsc/logging.py @@ -0,0 +1,3 @@ +import logging + +logger = logging.getLogger(__name__) # Nominally __name__ is 'tiledbsc' diff --git a/apis/python/tools/ingestor b/apis/python/tools/ingestor index 4db38080e0..c2e7f225fc 100755 --- a/apis/python/tools/ingestor +++ b/apis/python/tools/ingestor @@ -23,7 +23,7 @@ import tiledb import tiledbsc import tiledbsc.io -import tiledbsc.logger +import tiledbsc.logging import tiledbsc.util @@ -98,7 +98,16 @@ select `relative=True`. (This is the default.) soma_options = tiledbsc.SOMAOptions() write_soco = args.soco - # XXX verbose = not args.quiet + verbose = not args.quiet + if verbose: + # tiledbsc.logging.logger.setLevel(logging.INFO) + # logger = logging.getLogger('tiledbsc') + # logger.setLevel(logging.INFO) + # logging.getLogger("tiledbsc").setLevel(logging.INFO) + # Not able to get any of the above to 'stick'. The following sets level for the whole app, + # not just the tiledbsc library, but that's an acceptable workaround since this CLI does + # nothing except invoke the tiledbsc library. + logging.basicConfig(level=logging.INFO) if args.relative is not None: relative = args.relative[0] @@ -208,7 +217,9 @@ def ingest_one( else: if os.path.exists(output_path): if ifexists == "continue": - tiledbsc.logger.info(f"Already exists; continuing: {output_path}") + tiledbsc.logging.logger.info( + f"Already exists; continuing: {output_path}" + ) return elif ifexists == "abort": logging.error(f"Already exists; aborting: {output_path}") @@ -229,7 +240,7 @@ def ingest_one( # Do the ingest into TileDB. tiledbsc.io.from_h5ad(soma, input_path) - tiledbsc.logger.info(f"Wrote {output_path}") + tiledbsc.logging.logger.info(f"Wrote {output_path}") if write_soco: soco = tiledbsc.SOMACollection(soco_dir, soma_options=soma_options) @@ -238,8 +249,10 @@ def ingest_one( raise Exception(f"Could not create SOCO at {soco.uri}") soco.add(soma) - tiledbsc.logger.info("") - tiledbsc.logger.info(f"Added SOMA {soma.name} to SOMACollection {soco_dir}") + tiledbsc.logging.logger.info("") + tiledbsc.logging.logger.info( + f"Added SOMA {soma.name} to SOMACollection {soco_dir}" + ) # ================================================================ diff --git a/apis/python/tools/outgestor b/apis/python/tools/outgestor index 5225baa645..e0e241d74a 100755 --- a/apis/python/tools/outgestor +++ b/apis/python/tools/outgestor @@ -22,7 +22,7 @@ import tiledb import tiledbsc import tiledbsc.io -import tiledbsc.logger +import tiledbsc.logging import tiledbsc.util @@ -76,11 +76,21 @@ def main(): if not os.path.exists(outdir): os.mkdir(outdir) - # XXX verbose = not args.quiet + verbose = not args.quiet + if verbose: + # tiledbsc.logging.logger.setLevel(logging.INFO) + # logger = logging.getLogger('tiledbsc') + # logger.setLevel(logging.INFO) + # logging.getLogger("tiledbsc").setLevel(logging.INFO) + # Not able to get any of the above to 'stick'. The following sets level for the whole app, + # not just the tiledbsc library, but that's an acceptable workaround since this CLI does + # nothing except invoke the tiledbsc library. + logging.basicConfig(level=logging.INFO) + soma = tiledbsc.SOMA(input_path) tiledbsc.io.to_h5ad(soma, output_path) - tiledbsc.logger.info(f"Wrote {output_path}") + tiledbsc.logging.logger.info(f"Wrote {output_path}") if __name__ == "__main__": diff --git a/apis/python/tools/populate-soco b/apis/python/tools/populate-soco index ff8649e277..7264074de8 100755 --- a/apis/python/tools/populate-soco +++ b/apis/python/tools/populate-soco @@ -12,7 +12,7 @@ import os import sys import tiledbsc -import tiledbsc.logger +import tiledbsc.logging # ================================================================ @@ -111,7 +111,7 @@ select `relative=True`. (This is the default.) soma_uri = soma_uri.rstrip("/") name = os.path.basename(soma_uri) soma = tiledbsc.SOMA(uri=soma_uri, name=name, parent=soco) - tiledbsc.logger.info(f"Adding {name} to {soma_uri}") + tiledbsc.logging.logger.info(f"Adding {name} to {soma_uri}") soco.add(soma) if args.remove: @@ -119,7 +119,7 @@ select `relative=True`. (This is the default.) soma_uri = soma_uri.rstrip("/") name = os.path.basename(soma_uri) soma = tiledbsc.SOMA(uri=soma_uri, name=name, parent=soco) - tiledbsc.logger.info(f"Removing {name} from {soma_uri}") + tiledbsc.logging.logger.info(f"Removing {name} from {soma_uri}") soco.remove(soma) if args.list: