Mercurial > repos > rv43 > tomo
diff tomo_find_center.py @ 69:fba792d5f83b draft
planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
author | rv43 |
---|---|
date | Fri, 10 Mar 2023 16:02:04 +0000 |
parents | ba5866d0251d |
children | 1cf15b61cd83 |
line wrap: on
line diff
--- a/tomo_find_center.py Fri Aug 19 20:16:56 2022 +0000 +++ b/tomo_find_center.py Fri Mar 10 16:02:04 2023 +0000 @@ -2,85 +2,95 @@ import logging +import argparse +import pathlib import sys -import argparse -import tracemalloc +#import tracemalloc -from tomo import Tomo +from workflow.run_tomo import Tomo +#from memory_profiler import profile +#@profile def __main__(): - # Parse command line arguments parser = argparse.ArgumentParser( - description='Find the center axis for a tomography reconstruction') - parser.add_argument('-i', '--input_stacks', - required=True, help='Preprocessed image file stacks') - parser.add_argument('-c', '--config', - required=True, help='Input config') - parser.add_argument('--row_bounds', - required=True, nargs=2, type=int, help='Reconstruction row bounds') + description='Reduce tomography data') + parser.add_argument('-i', '--input_file', + required=True, + type=pathlib.Path, + help='''Full or relative path to the input file (in Nexus format).''') + parser.add_argument('-o', '--output_file', + required=False, + type=pathlib.Path, + help='''Full or relative path to the output file (in yaml format).''') parser.add_argument('--center_rows', - required=True, nargs=2, type=int, help='Center finding rows') - parser.add_argument('--center_type_selector', - help='Reconstruct slices for a set of center positions?') - parser.add_argument('--set_center', - type=int, help='Set center ') - parser.add_argument('--set_range', - type=float, help='Set range') - parser.add_argument('--set_step', - type=float, help='Set step') - parser.add_argument('--output_config', - required=True, help='Output config') - parser.add_argument('-l', '--log', - type=argparse.FileType('w'), default=sys.stdout, help='Log file') + required=True, + nargs=2, + type=int, + help='''Center finding rows.''') + parser.add_argument('--galaxy_flag', + action='store_true', + help='''Use this flag to run the scripts as a galaxy tool.''') + parser.add_argument('-l', '--log', +# type=argparse.FileType('w'), + default=sys.stdout, + help='Logging stream or filename') + parser.add_argument('--log_level', + choices=logging._nameToLevel.keys(), + default='INFO', + help='''Specify a preferred logging level.''') args = parser.parse_args() + # Set log configuration + # When logging to file, the stdout log level defaults to WARNING + logging_format = '%(asctime)s : %(levelname)s - %(module)s : %(funcName)s - %(message)s' + level = logging.getLevelName(args.log_level) + if args.log is sys.stdout: + logging.basicConfig(format=logging_format, level=level, force=True, + handlers=[logging.StreamHandler()]) + else: + if isinstance(args.log, str): + logging.basicConfig(filename=f'{args.log}', filemode='w', + format=logging_format, level=level, force=True) + elif isinstance(args.log, io.TextIOWrapper): + logging.basicConfig(filemode='w', format=logging_format, level=level, + stream=args.log, force=True) + else: + raise(ValueError(f'Invalid argument --log: {args.log}')) + stream_handler = logging.StreamHandler() + logging.getLogger().addHandler(stream_handler) + stream_handler.setLevel(logging.WARNING) + stream_handler.setFormatter(logging.Formatter(logging_format)) + # Starting memory monitoring - tracemalloc.start() +# tracemalloc.start() - # Set basic log configuration - logging_format = '%(asctime)s : %(levelname)s - %(module)s : %(funcName)s - %(message)s' - log_level = 'INFO' - level = getattr(logging, log_level.upper(), None) - if not isinstance(level, int): - raise ValueError(f'Invalid log_level: {log_level}') - logging.basicConfig(format=logging_format, level=level, force=True, - handlers=[logging.StreamHandler()]) - - logging.debug(f'config = {args.config}') - logging.debug(f'input_stacks = {args.input_stacks}') - logging.debug(f'row_bounds = {args.row_bounds} {type(args.row_bounds)}') - logging.debug(f'center_rows = {args.center_rows} {type(args.center_rows)}') - logging.debug(f'center_type_selector = {args.center_type_selector}') - logging.debug(f'set_center = {args.set_center}') - logging.debug(f'set_range = {args.set_range}') - logging.debug(f'set_step = {args.set_step}') - logging.debug(f'output_config = {args.output_config}') + # Log command line arguments + logging.info(f'input_file = {args.input_file}') + logging.info(f'output_file = {args.output_file}') + logging.info(f'center_rows = {args.center_rows}') + logging.info(f'galaxy_flag = {args.galaxy_flag}') logging.debug(f'log = {args.log}') logging.debug(f'is log stdout? {args.log is sys.stdout}') + logging.debug(f'log_level = {args.log_level}') # Instantiate Tomo object - tomo = Tomo(config_file=args.config, config_out=args.output_config, log_level=log_level, - log_stream=args.log, galaxy_flag=True) - if not tomo.is_valid: - raise ValueError('Invalid config file provided.') - logging.debug(f'config:\n{tomo.config}') + tomo = Tomo(galaxy_flag=args.galaxy_flag) + + # Read input file + data = tomo.read(args.input_file) - # Load preprocessed image files - tomo.loadTomoStacks(args.input_stacks) + # Find the calibrated center axis info + data = tomo.find_centers(data, center_rows=tuple(args.center_rows)) - # Find centers - galaxy_param = {'row_bounds' : args.row_bounds, 'center_rows' : args.center_rows, - 'center_type_selector' : args.center_type_selector, 'set_center' : args.set_center, - 'set_range' : args.set_range, 'set_step' : args.set_step} - tomo.findCenters(galaxy_param) + # Write output file + data = tomo.write(data, args.output_file) # Displaying memory usage - logging.info(f'Memory usage: {tracemalloc.get_traced_memory()}') - +# logging.info(f'Memory usage: {tracemalloc.get_traced_memory()}') + # stopping memory monitoring - tracemalloc.stop() +# tracemalloc.stop() if __name__ == "__main__": __main__() -