Mercurial > repos > rv43 > chess_tomo
comparison tomo_reconstruct.py @ 18:dee0b25fcabe draft
planemo upload for repository https://github.com/rolfverberg/galaxytools commit f8c4bdb31c20c468045ad5e6eb255a293244bc6c-dirty
| author | rv43 |
|---|---|
| date | Tue, 21 Mar 2023 15:35:48 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 17:e89919e468cd | 18:dee0b25fcabe |
|---|---|
| 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='Perform a tomography reconstruction') | |
| 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('-c', '--center_file', | |
| 23 required=True, | |
| 24 type=pathlib.Path, | |
| 25 help='''Full or relative path to the center info file (in yaml format).''') | |
| 26 parser.add_argument('-o', '--output_file', | |
| 27 required=False, | |
| 28 type=pathlib.Path, | |
| 29 help='''Full or relative path to the output file (in Nexus format).''') | |
| 30 parser.add_argument('--galaxy_flag', | |
| 31 action='store_true', | |
| 32 help='''Use this flag to run the scripts as a galaxy tool.''') | |
| 33 parser.add_argument('-l', '--log', | |
| 34 # type=argparse.FileType('w'), | |
| 35 default=sys.stdout, | |
| 36 help='Logging stream or filename') | |
| 37 parser.add_argument('--log_level', | |
| 38 choices=logging._nameToLevel.keys(), | |
| 39 default='INFO', | |
| 40 help='''Specify a preferred logging level.''') | |
| 41 parser.add_argument('--x_bounds', | |
| 42 required=False, | |
| 43 nargs=2, | |
| 44 type=int, | |
| 45 help='''Boundaries of reconstructed images in x-direction.''') | |
| 46 parser.add_argument('--y_bounds', | |
| 47 required=False, | |
| 48 nargs=2, | |
| 49 type=int, | |
| 50 help='''Boundaries of reconstructed images in y-direction.''') | |
| 51 args = parser.parse_args() | |
| 52 | |
| 53 # Set log configuration | |
| 54 # When logging to file, the stdout log level defaults to WARNING | |
| 55 logging_format = '%(asctime)s : %(levelname)s - %(module)s : %(funcName)s - %(message)s' | |
| 56 level = logging.getLevelName(args.log_level) | |
| 57 if args.log is sys.stdout: | |
| 58 logging.basicConfig(format=logging_format, level=level, force=True, | |
| 59 handlers=[logging.StreamHandler()]) | |
| 60 else: | |
| 61 if isinstance(args.log, str): | |
| 62 logging.basicConfig(filename=f'{args.log}', filemode='w', | |
| 63 format=logging_format, level=level, force=True) | |
| 64 elif isinstance(args.log, io.TextIOWrapper): | |
| 65 logging.basicConfig(filemode='w', format=logging_format, level=level, | |
| 66 stream=args.log, force=True) | |
| 67 else: | |
| 68 raise(ValueError(f'Invalid argument --log: {args.log}')) | |
| 69 stream_handler = logging.StreamHandler() | |
| 70 logging.getLogger().addHandler(stream_handler) | |
| 71 stream_handler.setLevel(logging.WARNING) | |
| 72 stream_handler.setFormatter(logging.Formatter(logging_format)) | |
| 73 | |
| 74 # Starting memory monitoring | |
| 75 # tracemalloc.start() | |
| 76 | |
| 77 # Log command line arguments | |
| 78 logging.info(f'input_file = {args.input_file}') | |
| 79 logging.info(f'center_file = {args.center_file}') | |
| 80 logging.info(f'output_file = {args.output_file}') | |
| 81 logging.info(f'galaxy_flag = {args.galaxy_flag}') | |
| 82 logging.debug(f'log = {args.log}') | |
| 83 logging.debug(f'is log stdout? {args.log is sys.stdout}') | |
| 84 logging.debug(f'log_level = {args.log_level}') | |
| 85 logging.info(f'x_bounds = {args.x_bounds}') | |
| 86 logging.info(f'y_bounds = {args.y_bounds}') | |
| 87 | |
| 88 # Instantiate Tomo object | |
| 89 tomo = Tomo(galaxy_flag=args.galaxy_flag) | |
| 90 | |
| 91 # Read input file | |
| 92 data = tomo.read(args.input_file) | |
| 93 | |
| 94 # Read center data | |
| 95 center_data = tomo.read(args.center_file) | |
| 96 | |
| 97 # Find the calibrated center axis info | |
| 98 data = tomo.reconstruct_data(data, center_data, x_bounds=args.x_bounds, y_bounds=args.y_bounds) | |
| 99 | |
| 100 # Write output file | |
| 101 data = tomo.write(data, args.output_file) | |
| 102 | |
| 103 # Displaying memory usage | |
| 104 # logging.info(f'Memory usage: {tracemalloc.get_traced_memory()}') | |
| 105 | |
| 106 # stopping memory monitoring | |
| 107 # tracemalloc.stop() | |
| 108 | |
| 109 logging.info('Completed tomography reconstruction') | |
| 110 | |
| 111 | |
| 112 if __name__ == "__main__": | |
| 113 __main__() |
