Mercurial > repos > rv43 > tomo
comparison tomo_reduce.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 | |
| children | 97c4e2cbbad9 |
comparison
equal
deleted
inserted
replaced
| 68:ba5866d0251d | 69:fba792d5f83b |
|---|---|
| 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 yaml 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 Nexus 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('--img_x_bounds', | |
| 30 required=False, | |
| 31 nargs=2, | |
| 32 type=int, | |
| 33 help='Vertical data reduction image range') | |
| 34 parser.add_argument('-l', '--log', | |
| 35 # type=argparse.FileType('w'), | |
| 36 default=sys.stdout, | |
| 37 help='Logging stream or filename') | |
| 38 parser.add_argument('--log_level', | |
| 39 choices=logging._nameToLevel.keys(), | |
| 40 default='INFO', | |
| 41 help='''Specify a preferred logging level.''') | |
| 42 args = parser.parse_args() | |
| 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 | |
| 65 # Start memory monitoring | |
| 66 # tracemalloc.start() | |
| 67 | |
| 68 # Log command line arguments | |
| 69 logging.info(f'input_file = {args.input_file}') | |
| 70 logging.info(f'output_file = {args.output_file}') | |
| 71 logging.info(f'galaxy_flag = {args.galaxy_flag}') | |
| 72 logging.info(f'img_x_bounds = {args.img_x_bounds}') | |
| 73 logging.debug(f'log = {args.log}') | |
| 74 logging.debug(f'is log stdout? {args.log is sys.stdout}') | |
| 75 logging.debug(f'log_level = {args.log_level}') | |
| 76 | |
| 77 # Instantiate Tomo object | |
| 78 tomo = Tomo(galaxy_flag=args.galaxy_flag) | |
| 79 | |
| 80 # Read input file | |
| 81 data = tomo.read(args.input_file) | |
| 82 | |
| 83 # Generate reduced tomography images | |
| 84 data = tomo.gen_reduced_data(data, img_x_bounds=args.img_x_bounds) | |
| 85 | |
| 86 # Write output file | |
| 87 data = tomo.write(data, args.output_file) | |
| 88 | |
| 89 # Displaying memory usage | |
| 90 # logging.info(f'Memory usage: {tracemalloc.get_traced_memory()}') | |
| 91 | |
| 92 # Stop memory monitoring | |
| 93 # tracemalloc.stop() | |
| 94 | |
| 95 if __name__ == "__main__": | |
| 96 __main__() |
