Mercurial > repos > rv43 > tomo
comparison tomo_combine.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='Combine reconstructed tomography stacks') | 17 description='Reduce tomography data') |
16 parser.add_argument('-i', '--input_stacks', | 18 parser.add_argument('-i', '--input_file', |
17 help='Reconstructed 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('--x_bounds', | 22 parser.add_argument('-o', '--output_file', |
21 required=True, nargs=2, type=int, help='Reconstructed range in x direction') | 23 required=False, |
22 parser.add_argument('--y_bounds', | 24 type=pathlib.Path, |
23 required=True, nargs=2, type=int, help='Reconstructed range in y direction') | 25 help='''Full or relative path to the output file (in yaml format).''') |
24 parser.add_argument('--z_bounds', | 26 parser.add_argument('-l', '--log', |
25 required=True, nargs=2, type=int, help='Reconstructed range in z direction') | 27 # type=argparse.FileType('w'), |
26 parser.add_argument('--output_config', | |
27 help='Output config') | |
28 parser.add_argument('--output_data', | |
29 help='Combined tomography stacks') | |
30 parser.add_argument('-l', '--log', | |
31 type=argparse.FileType('w'), | |
32 default=sys.stdout, | 28 default=sys.stdout, |
33 help='Log file') | 29 help='Logging stream or filename') |
30 parser.add_argument('--log_level', | |
31 choices=logging._nameToLevel.keys(), | |
32 default='INFO', | |
33 help='''Specify a preferred logging level.''') | |
34 args = parser.parse_args() | 34 args = parser.parse_args() |
35 | 35 |
36 # Set log configuration | |
37 # When logging to file, the stdout log level defaults to WARNING | |
38 logging_format = '%(asctime)s : %(levelname)s - %(module)s : %(funcName)s - %(message)s' | |
39 level = logging.getLevelName(args.log_level) | |
40 if args.log is sys.stdout: | |
41 logging.basicConfig(format=logging_format, level=level, force=True, | |
42 handlers=[logging.StreamHandler()]) | |
43 else: | |
44 if isinstance(args.log, str): | |
45 logging.basicConfig(filename=f'{args.log}', filemode='w', | |
46 format=logging_format, level=level, force=True) | |
47 elif isinstance(args.log, io.TextIOWrapper): | |
48 logging.basicConfig(filemode='w', format=logging_format, level=level, | |
49 stream=args.log, force=True) | |
50 else: | |
51 raise(ValueError(f'Invalid argument --log: {args.log}')) | |
52 stream_handler = logging.StreamHandler() | |
53 logging.getLogger().addHandler(stream_handler) | |
54 stream_handler.setLevel(logging.WARNING) | |
55 stream_handler.setFormatter(logging.Formatter(logging_format)) | |
56 | |
36 # Starting memory monitoring | 57 # Starting memory monitoring |
37 tracemalloc.start() | 58 # tracemalloc.start() |
38 | 59 |
39 # Set basic log configuration | 60 # Log command line arguments |
40 logging_format = '%(asctime)s : %(levelname)s - %(module)s : %(funcName)s - %(message)s' | 61 logging.info(f'input_file = {args.input_file}') |
41 log_level = 'INFO' | 62 logging.info(f'output_file = {args.output_file}') |
42 level = getattr(logging, log_level.upper(), None) | |
43 if not isinstance(level, int): | |
44 raise ValueError(f'Invalid log_level: {log_level}') | |
45 logging.basicConfig(format=logging_format, level=level, force=True, | |
46 handlers=[logging.StreamHandler()]) | |
47 | |
48 logging.debug(f'config = {args.config}') | |
49 logging.debug(f'input_stacks = {args.input_stacks}') | |
50 logging.debug(f'x_bounds = {args.x_bounds} {type(args.x_bounds)}') | |
51 logging.debug(f'y_bounds = {args.y_bounds} {type(args.y_bounds)}') | |
52 logging.debug(f'z_bounds = {args.z_bounds} {type(args.z_bounds)}') | |
53 logging.debug(f'output_config = {args.output_config}') | |
54 logging.debug(f'output_data = {args.output_data}') | |
55 logging.debug(f'log = {args.log}') | 63 logging.debug(f'log = {args.log}') |
56 logging.debug(f'is log stdout? {args.log is sys.stdout}') | 64 logging.debug(f'is log stdout? {args.log is sys.stdout}') |
65 logging.debug(f'log_level = {args.log_level}') | |
57 | 66 |
58 # Instantiate Tomo object | 67 # Instantiate Tomo object |
59 tomo = Tomo(config_file=args.config, config_out=args.output_config, log_level=log_level, | 68 tomo = Tomo() |
60 log_stream=args.log, galaxy_flag=True) | |
61 if not tomo.is_valid: | |
62 raise ValueError('Invalid config file provided.') | |
63 logging.debug(f'config:\n{tomo.config}') | |
64 | 69 |
65 # Load reconstructed image files | 70 # Read input file |
66 tomo.loadTomoStacks(args.input_stacks, recon_flag=True) | 71 data = tomo.read(args.input_file) |
67 | 72 |
68 # Combined reconstructed tomography stacks | 73 # Combine the reconstructed tomography stacks |
69 galaxy_param = {'x_bounds' : args.x_bounds, 'y_bounds' : args.y_bounds, | 74 data = tomo.combine_data(data) |
70 'z_bounds' : args.z_bounds, 'output_name' : args.output_data} | 75 |
71 logging.debug(f'galaxy_param = {galaxy_param}') | 76 # Write output file |
72 tomo.combineTomoStacks(galaxy_param) | 77 data = tomo.write(data, args.output_file) |
73 | 78 |
74 # Displaying memory usage | 79 # Displaying memory usage |
75 logging.info(f'Memory usage: {tracemalloc.get_traced_memory()}') | 80 # logging.info(f'Memory usage: {tracemalloc.get_traced_memory()}') |
76 | 81 |
77 # stopping memory monitoring | 82 # stopping memory monitoring |
78 tracemalloc.stop() | 83 # tracemalloc.stop() |
79 | 84 |
80 if __name__ == "__main__": | 85 if __name__ == "__main__": |
81 __main__() | 86 __main__() |
82 |