Mercurial > repos > rv43 > tomo
comparison tomo_reconstruct.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='Perfrom a tomography reconstruction') | 17 description='Reduce tomography data') |
16 parser.add_argument('-i', '--input_stacks', | 18 parser.add_argument('-i', '--input_file', |
17 help='Preprocessed image file stacks') | 19 required=True, |
18 parser.add_argument('-c', '--config', | 20 type=pathlib.Path, |
19 help='Input config') | 21 help='''Full or relative path to the input file (in Nexus format).''') |
20 parser.add_argument('--center_offsets', | 22 parser.add_argument('-c', '--center_file', |
21 nargs=2, type=float, help='Reconstruction center axis offsets') | 23 required=True, |
22 parser.add_argument('--output_config', | 24 type=pathlib.Path, |
23 help='Output config') | 25 help='''Full or relative path to the center info file (in Nexus format).''') |
24 parser.add_argument('--output_data', | 26 parser.add_argument('-o', '--output_file', |
25 help='Reconstructed tomography data') | 27 required=False, |
26 parser.add_argument('-l', '--log', | 28 type=pathlib.Path, |
27 type=argparse.FileType('w'), | 29 help='''Full or relative path to the output file (in yaml 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'), | |
28 default=sys.stdout, | 35 default=sys.stdout, |
29 help='Log file') | 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.''') | |
30 args = parser.parse_args() | 51 args = parser.parse_args() |
31 | 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 | |
32 # Starting memory monitoring | 74 # Starting memory monitoring |
33 tracemalloc.start() | 75 # tracemalloc.start() |
34 | 76 |
35 # Set basic log configuration | 77 # Log command line arguments |
36 logging_format = '%(asctime)s : %(levelname)s - %(module)s : %(funcName)s - %(message)s' | 78 logging.info(f'input_file = {args.input_file}') |
37 log_level = 'INFO' | 79 logging.info(f'center_file = {args.center_file}') |
38 level = getattr(logging, log_level.upper(), None) | 80 logging.info(f'output_file = {args.output_file}') |
39 if not isinstance(level, int): | 81 logging.info(f'galaxy_flag = {args.galaxy_flag}') |
40 raise ValueError(f'Invalid log_level: {log_level}') | |
41 logging.basicConfig(format=logging_format, level=level, force=True, | |
42 handlers=[logging.StreamHandler()]) | |
43 | |
44 logging.debug(f'config = {args.config}') | |
45 logging.debug(f'input_stacks = {args.input_stacks}') | |
46 logging.debug(f'center_offsets = {args.center_offsets} {type(args.center_offsets)}') | |
47 logging.debug(f'output_config = {args.output_config}') | |
48 logging.debug(f'output_data = {args.output_data}') | |
49 logging.debug(f'log = {args.log}') | 82 logging.debug(f'log = {args.log}') |
50 logging.debug(f'is log stdout? {args.log is sys.stdout}') | 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}') | |
51 | 87 |
52 # Instantiate Tomo object | 88 # Instantiate Tomo object |
53 tomo = Tomo(config_file=args.config, config_out=args.output_config, log_level=log_level, | 89 tomo = Tomo(galaxy_flag=args.galaxy_flag) |
54 log_stream=args.log, galaxy_flag=True) | |
55 if not tomo.is_valid: | |
56 raise ValueError('Invalid config file provided.') | |
57 logging.debug(f'config:\n{tomo.config}') | |
58 | 90 |
59 # Set reconstruction center axis offsets | 91 # Read input file |
60 if args.center_offsets is None: | 92 data = tomo.read(args.input_file) |
61 find_center = tomo.config.get('find_center') | |
62 if find_center is None: | |
63 raise ValueError('Invalid config file provided (missing find_center).') | |
64 center_offsets = [float(find_center.get('lower_center_offset')), | |
65 float(find_center.get('upper_center_offset'))] | |
66 else: | |
67 center_offsets = args.center_offsets | |
68 | 93 |
69 # Load preprocessed image files | 94 # Read center data |
70 tomo.loadTomoStacks(args.input_stacks) | 95 center_data = tomo.read(args.center_file) |
71 | 96 |
72 # Reconstruct tomography stacks | 97 # Find the calibrated center axis info |
73 galaxy_param = {'center_offsets' : center_offsets, 'output_name' : args.output_data} | 98 data = tomo.reconstruct_data(data, center_data, x_bounds=args.x_bounds, y_bounds=args.y_bounds) |
74 logging.debug(f'galaxy_param = {galaxy_param}') | 99 |
75 tomo.reconstructTomoStacks(galaxy_param) | 100 # Write output file |
101 data = tomo.write(data, args.output_file) | |
76 | 102 |
77 # Displaying memory usage | 103 # Displaying memory usage |
78 logging.info(f'Memory usage: {tracemalloc.get_traced_memory()}') | 104 # logging.info(f'Memory usage: {tracemalloc.get_traced_memory()}') |
79 | 105 |
80 # stopping memory monitoring | 106 # stopping memory monitoring |
81 tracemalloc.stop() | 107 # tracemalloc.stop() |
82 | 108 |
83 if __name__ == "__main__": | 109 if __name__ == "__main__": |
84 __main__() | 110 __main__() |
85 |