annotate tomo_combine.py @ 68:ba5866d0251d draft

"planemo upload for repository https://github.com/rolfverberg/galaxytools commit 3fde3e85030608a8a4d8e59308353b0144314dfe"
author rv43
date Fri, 19 Aug 2022 20:16:56 +0000
parents 059819ea1f0e
children fba792d5f83b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
48
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
1 #!/usr/bin/env python3
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
2
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
3 import logging
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
4
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
5 import sys
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
6 import argparse
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
7 import tracemalloc
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
8
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
9 from tomo import Tomo
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
10
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
11 def __main__():
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
12
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
13 # Parse command line arguments
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
14 parser = argparse.ArgumentParser(
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
15 description='Combine reconstructed tomography stacks')
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
16 parser.add_argument('-i', '--input_stacks',
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
17 help='Reconstructed image file stacks')
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
18 parser.add_argument('-c', '--config',
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
19 help='Input config')
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
20 parser.add_argument('--x_bounds',
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
21 required=True, nargs=2, type=int, help='Reconstructed range in x direction')
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
22 parser.add_argument('--y_bounds',
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
23 required=True, nargs=2, type=int, help='Reconstructed range in y direction')
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
24 parser.add_argument('--z_bounds',
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
25 required=True, nargs=2, type=int, help='Reconstructed range in z direction')
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
26 parser.add_argument('--output_config',
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
27 help='Output config')
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
28 parser.add_argument('--output_data',
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
29 help='Combined tomography stacks')
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
30 parser.add_argument('-l', '--log',
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
31 type=argparse.FileType('w'),
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
32 default=sys.stdout,
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
33 help='Log file')
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
34 args = parser.parse_args()
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
35
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
36 # Starting memory monitoring
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
37 tracemalloc.start()
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
38
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
39 # Set basic log configuration
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
40 logging_format = '%(asctime)s : %(levelname)s - %(module)s : %(funcName)s - %(message)s'
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
41 log_level = 'INFO'
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
42 level = getattr(logging, log_level.upper(), None)
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
43 if not isinstance(level, int):
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
44 raise ValueError(f'Invalid log_level: {log_level}')
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
45 logging.basicConfig(format=logging_format, level=level, force=True,
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
46 handlers=[logging.StreamHandler()])
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
47
68
ba5866d0251d "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 3fde3e85030608a8a4d8e59308353b0144314dfe"
rv43
parents: 48
diff changeset
48 logging.debug(f'config = {args.config}')
48
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
49 logging.debug(f'input_stacks = {args.input_stacks}')
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
50 logging.debug(f'x_bounds = {args.x_bounds} {type(args.x_bounds)}')
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
51 logging.debug(f'y_bounds = {args.y_bounds} {type(args.y_bounds)}')
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
52 logging.debug(f'z_bounds = {args.z_bounds} {type(args.z_bounds)}')
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
53 logging.debug(f'output_config = {args.output_config}')
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
54 logging.debug(f'output_data = {args.output_data}')
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
55 logging.debug(f'log = {args.log}')
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
56 logging.debug(f'is log stdout? {args.log is sys.stdout}')
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
57
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
58 # Instantiate Tomo object
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
59 tomo = Tomo(config_file=args.config, config_out=args.output_config, log_level=log_level,
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
60 log_stream=args.log, galaxy_flag=True)
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
61 if not tomo.is_valid:
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
62 raise ValueError('Invalid config file provided.')
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
63 logging.debug(f'config:\n{tomo.config}')
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
64
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
65 # Load reconstructed image files
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
66 tomo.loadTomoStacks(args.input_stacks, recon_flag=True)
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
67
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
68 # Combined reconstructed tomography stacks
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
69 galaxy_param = {'x_bounds' : args.x_bounds, 'y_bounds' : args.y_bounds,
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
70 'z_bounds' : args.z_bounds, 'output_name' : args.output_data}
68
ba5866d0251d "planemo upload for repository https://github.com/rolfverberg/galaxytools commit 3fde3e85030608a8a4d8e59308353b0144314dfe"
rv43
parents: 48
diff changeset
71 logging.debug(f'galaxy_param = {galaxy_param}')
48
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
72 tomo.combineTomoStacks(galaxy_param)
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
73
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
74 # Displaying memory usage
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
75 logging.info(f'Memory usage: {tracemalloc.get_traced_memory()}')
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
76
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
77 # stopping memory monitoring
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
78 tracemalloc.stop()
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
79
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
80 if __name__ == "__main__":
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
81 __main__()
059819ea1f0e "planemo upload for repository https://github.com/rolfverberg/galaxytools commit b97c6a8f181dea6d2f9cfa2069b86d30dcc47b4d"
rv43
parents:
diff changeset
82