Mercurial > repos > rv43 > tomo
comparison read_image.py @ 70:97c4e2cbbad9 draft
planemo upload for repository https://github.com/rolfverberg/galaxytools commit b891edb4a5881959112ff276f7696bbc36dcea33
author | rv43 |
---|---|
date | Fri, 10 Mar 2023 16:39:22 +0000 |
parents | 26f99fdd8d61 |
children |
comparison
equal
deleted
inserted
replaced
69:fba792d5f83b | 70:97c4e2cbbad9 |
---|---|
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 | |
7 import numpy as np | |
8 | 8 |
9 def __main__(): | 9 def __main__(): |
10 | 10 |
11 # Parse command line arguments | 11 # Parse command line arguments |
12 parser = argparse.ArgumentParser( | 12 parser = argparse.ArgumentParser( |
13 description='Read a reconstructed image') | 13 description='Read a raw or reconstructed image') |
14 parser.add_argument('-i', '--input_image', | 14 parser.add_argument('-i', '--input_file', |
15 help='Reconstructed image file') | 15 required=True, |
16 parser.add_argument('-l', '--log', | 16 type=pathlib.Path, |
17 type=argparse.FileType('w'), | 17 help='''Full or relative path to the input file (in yaml or nxs format).''') |
18 parser.add_argument('--image_type', | |
19 required=False, | |
20 help='Image type (dark, bright, tomo_raw, tomo_reduced, or reconstructed') | |
21 parser.add_argument('--image_index', | |
22 required=False, | |
23 type=int, | |
24 help='Image index (only for raw or reduced images') | |
25 parser.add_argument('-l', '--log', | |
26 # type=argparse.FileType('w'), | |
18 default=sys.stdout, | 27 default=sys.stdout, |
19 help='Log file') | 28 help='Logging stream or filename') |
29 parser.add_argument('--log_level', | |
30 choices=logging._nameToLevel.keys(), | |
31 default='INFO', | |
32 help='''Specify a preferred logging level.''') | |
20 args = parser.parse_args() | 33 args = parser.parse_args() |
21 | 34 |
22 # Set basic log configuration | 35 # Set log configuration |
36 # When logging to file, the stdout log level defaults to WARNING | |
23 logging_format = '%(asctime)s : %(levelname)s - %(module)s : %(funcName)s - %(message)s' | 37 logging_format = '%(asctime)s : %(levelname)s - %(module)s : %(funcName)s - %(message)s' |
24 log_level = 'INFO' | 38 level = logging.getLevelName(args.log_level) |
25 level = getattr(logging, log_level.upper(), None) | 39 if args.log is sys.stdout: |
26 if not isinstance(level, int): | 40 logging.basicConfig(format=logging_format, level=level, force=True, |
27 raise ValueError(f'Invalid log_level: {log_level}') | 41 handlers=[logging.StreamHandler()]) |
28 logging.basicConfig(format=logging_format, level=level, force=True, | 42 else: |
29 handlers=[logging.StreamHandler()]) | 43 if isinstance(args.log, str): |
44 logging.basicConfig(filename=f'{args.log}', filemode='w', | |
45 format=logging_format, level=level, force=True) | |
46 elif isinstance(args.log, io.TextIOWrapper): | |
47 logging.basicConfig(filemode='w', format=logging_format, level=level, | |
48 stream=args.log, force=True) | |
49 else: | |
50 raise(ValueError(f'Invalid argument --log: {args.log}')) | |
51 stream_handler = logging.StreamHandler() | |
52 logging.getLogger().addHandler(stream_handler) | |
53 stream_handler.setLevel(logging.WARNING) | |
54 stream_handler.setFormatter(logging.Formatter(logging_format)) | |
30 | 55 |
31 logging.info(f'input_image = {args.input_image}') | 56 # Log command line arguments |
57 logging.info(f'input_file = {args.input_file}') | |
58 logging.info(f'image_type = {args.image_type}') | |
59 logging.info(f'image_index = {args.image_index}') | |
32 logging.debug(f'log = {args.log}') | 60 logging.debug(f'log = {args.log}') |
33 logging.debug(f'is log stdout? {args.log is sys.stdout}') | 61 logging.debug(f'is log stdout? {args.log is sys.stdout}') |
34 | 62 |
35 # Load image | 63 # Instantiate Tomo object |
36 f = np.load(args.input_image) | 64 tomo = Tomo(galaxy_flag=args.galaxy_flag) |
37 logging.info(f'f shape = {f.shape}') | 65 |
66 # Read input file | |
67 data = tomo.read(args.input_file) | |
68 print(f'data:\n{data}') | |
38 | 69 |
39 if __name__ == "__main__": | 70 if __name__ == "__main__": |
40 __main__() | 71 __main__() |
41 | 72 |