Mercurial > repos > rv43 > chess_tomo
comparison tomo_combine.py @ 30:3342c0518d4c draft
planemo upload for repository https://github.com/rolfverberg/galaxytools commit f8c4bdb31c20c468045ad5e6eb255a293244bc6c-dirty
| author | rv43 |
|---|---|
| date | Wed, 22 Mar 2023 12:52:20 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 29:551d1ea2416d | 30:3342c0518d4c |
|---|---|
| 1 #!/usr/bin/env python3 | |
| 2 | |
| 3 import logging | |
| 4 | |
| 5 import argparse | |
| 6 import pathlib | |
| 7 import sys | |
| 8 #import tracemalloc | |
| 9 | |
| 10 from workflow.run_tomo import Tomo | |
| 11 | |
| 12 #from memory_profiler import profile | |
| 13 #@profile | |
| 14 def __main__(): | |
| 15 # Parse command line arguments | |
| 16 parser = argparse.ArgumentParser( | |
| 17 description='Reduce tomography data') | |
| 18 parser.add_argument('-i', '--input_file', | |
| 19 required=True, | |
| 20 type=pathlib.Path, | |
| 21 help='''Full or relative path to the input file (in Nexus format).''') | |
| 22 parser.add_argument('-o', '--output_file', | |
| 23 required=False, | |
| 24 type=pathlib.Path, | |
| 25 help='''Full or relative path to the output file (in yaml format).''') | |
| 26 parser.add_argument('--galaxy_flag', | |
| 27 action='store_true', | |
| 28 help='''Use this flag to run the scripts as a galaxy tool.''') | |
| 29 parser.add_argument('-l', '--log', | |
| 30 # type=argparse.FileType('w'), | |
| 31 default=sys.stdout, | |
| 32 help='Logging stream or filename') | |
| 33 parser.add_argument('--log_level', | |
| 34 choices=logging._nameToLevel.keys(), | |
| 35 default='INFO', | |
| 36 help='''Specify a preferred logging level.''') | |
| 37 parser.add_argument('--x_bounds', | |
| 38 required=False, | |
| 39 nargs=2, | |
| 40 type=int, | |
| 41 help='''Boundaries of reconstructed images in x-direction.''') | |
| 42 parser.add_argument('--y_bounds', | |
| 43 required=False, | |
| 44 nargs=2, | |
| 45 type=int, | |
| 46 help='''Boundaries of reconstructed images in y-direction.''') | |
| 47 args = parser.parse_args() | |
| 48 | |
| 49 # Set log configuration | |
| 50 # When logging to file, the stdout log level defaults to WARNING | |
| 51 logging_format = '%(asctime)s : %(levelname)s - %(module)s : %(funcName)s - %(message)s' | |
| 52 level = logging.getLevelName(args.log_level) | |
| 53 if args.log is sys.stdout: | |
| 54 logging.basicConfig(format=logging_format, level=level, force=True, | |
| 55 handlers=[logging.StreamHandler()]) | |
| 56 else: | |
| 57 if isinstance(args.log, str): | |
| 58 logging.basicConfig(filename=f'{args.log}', filemode='w', | |
| 59 format=logging_format, level=level, force=True) | |
| 60 elif isinstance(args.log, io.TextIOWrapper): | |
| 61 logging.basicConfig(filemode='w', format=logging_format, level=level, | |
| 62 stream=args.log, force=True) | |
| 63 else: | |
| 64 raise(ValueError(f'Invalid argument --log: {args.log}')) | |
| 65 stream_handler = logging.StreamHandler() | |
| 66 logging.getLogger().addHandler(stream_handler) | |
| 67 stream_handler.setLevel(logging.WARNING) | |
| 68 stream_handler.setFormatter(logging.Formatter(logging_format)) | |
| 69 | |
| 70 # Starting memory monitoring | |
| 71 # tracemalloc.start() | |
| 72 | |
| 73 # Log command line arguments | |
| 74 logging.info(f'input_file = {args.input_file}') | |
| 75 logging.info(f'output_file = {args.output_file}') | |
| 76 logging.debug(f'log = {args.log}') | |
| 77 logging.debug(f'is log stdout? {args.log is sys.stdout}') | |
| 78 logging.debug(f'log_level = {args.log_level}') | |
| 79 logging.info(f'x_bounds = {args.x_bounds}') | |
| 80 logging.info(f'y_bounds = {args.y_bounds}') | |
| 81 | |
| 82 # Instantiate Tomo object | |
| 83 tomo = Tomo(galaxy_flag=args.galaxy_flag) | |
| 84 | |
| 85 # Read input file | |
| 86 data = tomo.read(args.input_file) | |
| 87 | |
| 88 # Combine the reconstructed tomography stacks | |
| 89 data = tomo.combine_data(data, x_bounds=args.x_bounds, y_bounds=args.y_bounds) | |
| 90 | |
| 91 # Write output file | |
| 92 if data is not None: | |
| 93 data = tomo.write(data, args.output_file) | |
| 94 | |
| 95 # Displaying memory usage | |
| 96 # logging.info(f'Memory usage: {tracemalloc.get_traced_memory()}') | |
| 97 | |
| 98 # stopping memory monitoring | |
| 99 # tracemalloc.stop() | |
| 100 | |
| 101 if __name__ == "__main__": | |
| 102 __main__() |
