Mercurial > repos > rv43 > tomo
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 68:ba5866d0251d | 69:fba792d5f83b |
|---|---|
| 1 #!/usr/bin/env python3 | 1 #!/usr/bin/env python3 |
| 2 | 2 |
| 3 import logging | 3 import logging |
| 4 | 4 |
| 5 import argparse | |
| 6 import pathlib | |
| 5 import sys | 7 import sys |
| 6 import argparse | 8 #import tracemalloc |
| 7 import tracemalloc | |
| 8 | 9 |
| 9 from tomo import Tomo | 10 from workflow.run_tomo import Tomo |
| 10 | 11 |
| 12 #from memory_profiler import profile | |
| 13 #@profile | |
| 11 def __main__(): | 14 def __main__(): |
| 12 | |
| 13 # Parse command line arguments | 15 # Parse command line arguments |
| 14 parser = argparse.ArgumentParser( | 16 parser = argparse.ArgumentParser( |
| 15 description='Find the center axis for a tomography reconstruction') | 17 description='Reduce tomography data') |
| 16 parser.add_argument('-i', '--input_stacks', | 18 parser.add_argument('-i', '--input_file', |
| 17 required=True, help='Preprocessed image file stacks') | 19 required=True, |
| 18 parser.add_argument('-c', '--config', | 20 type=pathlib.Path, |
| 19 required=True, help='Input config') | 21 help='''Full or relative path to the input file (in Nexus format).''') |
| 20 parser.add_argument('--row_bounds', | 22 parser.add_argument('-o', '--output_file', |
| 21 required=True, nargs=2, type=int, help='Reconstruction row bounds') | 23 required=False, |
| 24 type=pathlib.Path, | |
| 25 help='''Full or relative path to the output file (in yaml format).''') | |
| 22 parser.add_argument('--center_rows', | 26 parser.add_argument('--center_rows', |
| 23 required=True, nargs=2, type=int, help='Center finding rows') | 27 required=True, |
| 24 parser.add_argument('--center_type_selector', | 28 nargs=2, |
| 25 help='Reconstruct slices for a set of center positions?') | 29 type=int, |
| 26 parser.add_argument('--set_center', | 30 help='''Center finding rows.''') |
| 27 type=int, help='Set center ') | 31 parser.add_argument('--galaxy_flag', |
| 28 parser.add_argument('--set_range', | 32 action='store_true', |
| 29 type=float, help='Set range') | 33 help='''Use this flag to run the scripts as a galaxy tool.''') |
| 30 parser.add_argument('--set_step', | 34 parser.add_argument('-l', '--log', |
| 31 type=float, help='Set step') | 35 # type=argparse.FileType('w'), |
| 32 parser.add_argument('--output_config', | 36 default=sys.stdout, |
| 33 required=True, help='Output config') | 37 help='Logging stream or filename') |
| 34 parser.add_argument('-l', '--log', | 38 parser.add_argument('--log_level', |
| 35 type=argparse.FileType('w'), default=sys.stdout, help='Log file') | 39 choices=logging._nameToLevel.keys(), |
| 40 default='INFO', | |
| 41 help='''Specify a preferred logging level.''') | |
| 36 args = parser.parse_args() | 42 args = parser.parse_args() |
| 37 | 43 |
| 44 # Set log configuration | |
| 45 # When logging to file, the stdout log level defaults to WARNING | |
| 46 logging_format = '%(asctime)s : %(levelname)s - %(module)s : %(funcName)s - %(message)s' | |
| 47 level = logging.getLevelName(args.log_level) | |
| 48 if args.log is sys.stdout: | |
| 49 logging.basicConfig(format=logging_format, level=level, force=True, | |
| 50 handlers=[logging.StreamHandler()]) | |
| 51 else: | |
| 52 if isinstance(args.log, str): | |
| 53 logging.basicConfig(filename=f'{args.log}', filemode='w', | |
| 54 format=logging_format, level=level, force=True) | |
| 55 elif isinstance(args.log, io.TextIOWrapper): | |
| 56 logging.basicConfig(filemode='w', format=logging_format, level=level, | |
| 57 stream=args.log, force=True) | |
| 58 else: | |
| 59 raise(ValueError(f'Invalid argument --log: {args.log}')) | |
| 60 stream_handler = logging.StreamHandler() | |
| 61 logging.getLogger().addHandler(stream_handler) | |
| 62 stream_handler.setLevel(logging.WARNING) | |
| 63 stream_handler.setFormatter(logging.Formatter(logging_format)) | |
| 64 | |
| 38 # Starting memory monitoring | 65 # Starting memory monitoring |
| 39 tracemalloc.start() | 66 # tracemalloc.start() |
| 40 | 67 |
| 41 # Set basic log configuration | 68 # Log command line arguments |
| 42 logging_format = '%(asctime)s : %(levelname)s - %(module)s : %(funcName)s - %(message)s' | 69 logging.info(f'input_file = {args.input_file}') |
| 43 log_level = 'INFO' | 70 logging.info(f'output_file = {args.output_file}') |
| 44 level = getattr(logging, log_level.upper(), None) | 71 logging.info(f'center_rows = {args.center_rows}') |
| 45 if not isinstance(level, int): | 72 logging.info(f'galaxy_flag = {args.galaxy_flag}') |
| 46 raise ValueError(f'Invalid log_level: {log_level}') | |
| 47 logging.basicConfig(format=logging_format, level=level, force=True, | |
| 48 handlers=[logging.StreamHandler()]) | |
| 49 | |
| 50 logging.debug(f'config = {args.config}') | |
| 51 logging.debug(f'input_stacks = {args.input_stacks}') | |
| 52 logging.debug(f'row_bounds = {args.row_bounds} {type(args.row_bounds)}') | |
| 53 logging.debug(f'center_rows = {args.center_rows} {type(args.center_rows)}') | |
| 54 logging.debug(f'center_type_selector = {args.center_type_selector}') | |
| 55 logging.debug(f'set_center = {args.set_center}') | |
| 56 logging.debug(f'set_range = {args.set_range}') | |
| 57 logging.debug(f'set_step = {args.set_step}') | |
| 58 logging.debug(f'output_config = {args.output_config}') | |
| 59 logging.debug(f'log = {args.log}') | 73 logging.debug(f'log = {args.log}') |
| 60 logging.debug(f'is log stdout? {args.log is sys.stdout}') | 74 logging.debug(f'is log stdout? {args.log is sys.stdout}') |
| 75 logging.debug(f'log_level = {args.log_level}') | |
| 61 | 76 |
| 62 # Instantiate Tomo object | 77 # Instantiate Tomo object |
| 63 tomo = Tomo(config_file=args.config, config_out=args.output_config, log_level=log_level, | 78 tomo = Tomo(galaxy_flag=args.galaxy_flag) |
| 64 log_stream=args.log, galaxy_flag=True) | |
| 65 if not tomo.is_valid: | |
| 66 raise ValueError('Invalid config file provided.') | |
| 67 logging.debug(f'config:\n{tomo.config}') | |
| 68 | 79 |
| 69 # Load preprocessed image files | 80 # Read input file |
| 70 tomo.loadTomoStacks(args.input_stacks) | 81 data = tomo.read(args.input_file) |
| 71 | 82 |
| 72 # Find centers | 83 # Find the calibrated center axis info |
| 73 galaxy_param = {'row_bounds' : args.row_bounds, 'center_rows' : args.center_rows, | 84 data = tomo.find_centers(data, center_rows=tuple(args.center_rows)) |
| 74 'center_type_selector' : args.center_type_selector, 'set_center' : args.set_center, | 85 |
| 75 'set_range' : args.set_range, 'set_step' : args.set_step} | 86 # Write output file |
| 76 tomo.findCenters(galaxy_param) | 87 data = tomo.write(data, args.output_file) |
| 77 | 88 |
| 78 # Displaying memory usage | 89 # Displaying memory usage |
| 79 logging.info(f'Memory usage: {tracemalloc.get_traced_memory()}') | 90 # logging.info(f'Memory usage: {tracemalloc.get_traced_memory()}') |
| 80 | 91 |
| 81 # stopping memory monitoring | 92 # stopping memory monitoring |
| 82 tracemalloc.stop() | 93 # tracemalloc.stop() |
| 83 | 94 |
| 84 if __name__ == "__main__": | 95 if __name__ == "__main__": |
| 85 __main__() | 96 __main__() |
| 86 |
