diff --git a/mesmerize_core/algorithms/_utils.py b/mesmerize_core/algorithms/_utils.py new file mode 100644 index 0000000..892b22e --- /dev/null +++ b/mesmerize_core/algorithms/_utils.py @@ -0,0 +1,10 @@ +import logging +from typing import Union + +def setup_logging(log_level: Union[int, str] = logging.INFO): + if isinstance(log_level, str): + log_level = getattr(logging, log_level) + logging.basicConfig( + format="{asctime} - {levelname} - [{filename} {funcName}() {lineno}] - pid {process} - {message}", + filename=None, force=True, + level=log_level, style="{") # logging level can be DEBUG, INFO, WARNING, ERROR, CRITICAL diff --git a/mesmerize_core/algorithms/cnmf.py b/mesmerize_core/algorithms/cnmf.py index dd7381a..0f8b753 100644 --- a/mesmerize_core/algorithms/cnmf.py +++ b/mesmerize_core/algorithms/cnmf.py @@ -16,12 +16,16 @@ if __name__ in ["__main__", "__mp_main__"]: # when running in subprocess from mesmerize_core import set_parent_raw_data_path, load_batch from mesmerize_core.utils import IS_WINDOWS + from mesmerize_core.algorithms._utils import setup_logging else: # when running with local backend from ..batch_utils import set_parent_raw_data_path, load_batch from ..utils import IS_WINDOWS + from ._utils import setup_logging -def run_algo(batch_path, uuid, data_path: str = None): +def run_algo(batch_path, uuid, data_path: str = None, log_level=None): + if log_level is not None: + setup_logging(log_level) algo_start = time.time() set_parent_raw_data_path(data_path) @@ -147,9 +151,10 @@ def run_algo(batch_path, uuid, data_path: str = None): @click.command() @click.option("--batch-path", type=str) @click.option("--uuid", type=str) -@click.option("--data-path") -def main(batch_path, uuid, data_path: str = None): - run_algo(batch_path, uuid, data_path) +@click.option("--data-path", default=None) +@click.option("--log-level", type=int, default=None) +def main(batch_path, uuid, data_path, log_level): + run_algo(batch_path, uuid, data_path, log_level=log_level) if __name__ == "__main__": diff --git a/mesmerize_core/algorithms/cnmfe.py b/mesmerize_core/algorithms/cnmfe.py index d4f1858..641fa31 100644 --- a/mesmerize_core/algorithms/cnmfe.py +++ b/mesmerize_core/algorithms/cnmfe.py @@ -13,12 +13,16 @@ if __name__ in ["__main__", "__mp_main__"]: # when running in subprocess from mesmerize_core import set_parent_raw_data_path, load_batch from mesmerize_core.utils import IS_WINDOWS + from mesmerize_core.algorithms._utils import setup_logging else: # when running with local backend from ..batch_utils import set_parent_raw_data_path, load_batch from ..utils import IS_WINDOWS + from ._utils import setup_logging -def run_algo(batch_path, uuid, data_path: str = None): +def run_algo(batch_path, uuid, data_path: str = None, log_level=None): + if log_level is not None: + setup_logging(log_level) algo_start = time.time() set_parent_raw_data_path(data_path) @@ -128,9 +132,10 @@ def run_algo(batch_path, uuid, data_path: str = None): @click.command() @click.option("--batch-path", type=str) @click.option("--uuid", type=str) -@click.option("--data-path") -def main(batch_path, uuid, data_path: str = None): - run_algo(batch_path, uuid, data_path) +@click.option("--data-path", default=None) +@click.option("--log-level", type=int, default=None) +def main(batch_path, uuid, data_path, log_level): + run_algo(batch_path, uuid, data_path, log_level=log_level) if __name__ == "__main__": diff --git a/mesmerize_core/algorithms/mcorr.py b/mesmerize_core/algorithms/mcorr.py index 484130d..92432dc 100644 --- a/mesmerize_core/algorithms/mcorr.py +++ b/mesmerize_core/algorithms/mcorr.py @@ -14,11 +14,15 @@ # prevent circular import if __name__ in ["__main__", "__mp_main__"]: # when running in subprocess from mesmerize_core import set_parent_raw_data_path, load_batch + from mesmerize_core.algorithms._utils import setup_logging else: # when running with local backend from ..batch_utils import set_parent_raw_data_path, load_batch + from ._utils import setup_logging -def run_algo(batch_path, uuid, data_path: str = None): +def run_algo(batch_path, uuid, data_path: str = None, log_level=None): + if log_level is not None: + setup_logging(log_level) algo_start = time.time() set_parent_raw_data_path(data_path) @@ -156,9 +160,10 @@ def run_algo(batch_path, uuid, data_path: str = None): @click.command() @click.option("--batch-path", type=str) @click.option("--uuid", type=str) -@click.option("--data-path", type=str) -def main(batch_path, uuid, data_path: str = None): - run_algo(batch_path, uuid, data_path) +@click.option("--data-path", default=None) +@click.option("--log-level", type=int, default=None) +def main(batch_path, uuid, data_path, log_level): + run_algo(batch_path, uuid, data_path, log_level=log_level) if __name__ == "__main__": diff --git a/mesmerize_core/caiman_extensions/common.py b/mesmerize_core/caiman_extensions/common.py index 8e2b4cd..984de3c 100644 --- a/mesmerize_core/caiman_extensions/common.py +++ b/mesmerize_core/caiman_extensions/common.py @@ -9,6 +9,7 @@ from datetime import datetime import time from copy import deepcopy +import logging import numpy as np import pandas as pd @@ -536,10 +537,14 @@ def _run_local( batch_path: Path, uuid: UUID, data_path: Union[Path, None], + log_level=None, ): algo_module = getattr(algorithms, algo) algo_module.run_algo( - batch_path=str(batch_path), uuid=str(uuid), data_path=str(data_path) + batch_path=str(batch_path), + uuid=str(uuid), + data_path=str(data_path), + log_level=log_level ) return DummyProcess() @@ -643,6 +648,7 @@ def run(self, backend: Optional[str] = None, wait: bool = True, **kwargs): batch_path=batch_path, uuid=self._series["uuid"], data_path=get_parent_raw_data_path(), + log_level=kwargs.get("log_level") ) # Create the runfile in the batch dir using this Series' UUID as the filename @@ -660,6 +666,15 @@ def run(self, backend: Optional[str] = None, wait: bool = True, **kwargs): if get_parent_raw_data_path() is not None: args_str += f" --data-path {lex.quote(str(get_parent_raw_data_path()))}" + # set log level + if 'log_level' in kwargs: + level = kwargs['log_level'] + if isinstance(level, str): + level = getattr(logging, level) + else: + level = logging.getLogger().getEffectiveLevel() + args_str += f" --log-level {level}" + # make the runfile runfile_path = make_runfile( module_path=os.path.abspath(