2
|
1 #!/usr/bin/env python3
|
|
2
|
|
3 import logging
|
|
4
|
|
5 import os
|
|
6 import sys
|
|
7 import re
|
|
8 import yaml
|
|
9 import argparse
|
|
10 import numpy as np
|
|
11
|
|
12 from tomo import Tomo
|
|
13 import msnc_tools as msnc
|
|
14
|
|
15 def __main__():
|
|
16
|
|
17 # Parse command line arguments
|
|
18 parser = argparse.ArgumentParser(
|
|
19 description='Setup tomography reconstruction')
|
|
20 parser.add_argument('-i', '--inputfiles',
|
|
21 default='inputfiles.txt',
|
|
22 help='Input file collections')
|
|
23 parser.add_argument('-c', '--config',
|
|
24 help='Input config')
|
|
25 parser.add_argument('--theta_range',
|
|
26 help='Theta range (lower bound, upper bound, number of angles)')
|
|
27 parser.add_argument('--dark',
|
|
28 help='Dark field')
|
|
29 parser.add_argument('--bright',
|
|
30 help='Bright field')
|
|
31 parser.add_argument('--tomo',
|
|
32 help='First tomography image')
|
|
33 parser.add_argument('--detectorbounds',
|
|
34 help='Detector bounds')
|
|
35 parser.add_argument('--output_config',
|
|
36 help='Output config')
|
|
37 parser.add_argument('--output_data',
|
|
38 help='Preprocessed tomography data')
|
|
39 parser.add_argument('-l', '--log',
|
|
40 type=argparse.FileType('w'),
|
|
41 default=sys.stdout,
|
|
42 help='Log file')
|
|
43 parser.add_argument('tomo_ranges', metavar='N', type=int, nargs='+')
|
|
44 args = parser.parse_args()
|
|
45
|
|
46 # Set basic log configuration
|
|
47 logging_format = '%(asctime)s : %(levelname)s - %(module)s : %(funcName)s - %(message)s'
|
|
48 log_level = 'INFO'
|
|
49 level = getattr(logging, log_level.upper(), None)
|
|
50 if not isinstance(level, int):
|
|
51 raise ValueError(f'Invalid log_level: {log_level}')
|
|
52 logging.basicConfig(format=logging_format, level=level, force=True,
|
|
53 handlers=[logging.StreamHandler()])
|
|
54
|
|
55 logging.info(f'config = {args.config}\n')
|
|
56 logging.info(f'theta_range = {args.theta_range.split()}\n')
|
|
57 logging.info(f'dark = {args.dark}\n')
|
|
58 logging.info(f'bright = {args.bright}\n')
|
|
59 logging.info(f'tomo = {args.tomo}\n')
|
|
60 logging.info(f'detectorbounds = {args.detectorbounds}\n')
|
|
61 logging.info(f'output_config = {args.output_config}\n')
|
|
62 logging.info(f'output_data = {args.output_data}\n')
|
|
63 logging.info(f'log = {args.log}')
|
|
64 logging.info(f'is log stdout? {args.log == sys.stdout}')
|
|
65 logging.info(f'tomoranges = {args.tomo_ranges}\n\n')
|
|
66
|
|
67 # Read input files and collect data files info
|
|
68 logging.debug('data files:\n')
|
|
69 datasets = []
|
|
70 with open(args.inputfiles) as cf:
|
|
71 for line in cf:
|
|
72 if not line.strip() or line.startswith('#'):
|
|
73 continue
|
|
74 fields = [x.strip() for x in line.split('\t')]
|
|
75 filepath = fields[0]
|
|
76 element_identifier = fields[1] if len(fields) > 1 else fields[0].split('/')[-1]
|
|
77 datasets.append({'element_identifier' : fields[1], 'filepath' : filepath})
|
|
78 logging.debug(f'\ndatasets: {datasets}\n')
|
|
79
|
|
80 # Read and sort data files
|
|
81 collections = []
|
|
82 for dataset in datasets:
|
|
83 element_identifier = [x.strip() for x in dataset['element_identifier'].split('_')]
|
|
84 if len(element_identifier) > 1:
|
|
85 name = element_identifier[0]
|
|
86 else:
|
|
87 name = 'other'
|
|
88 filepath = dataset['filepath']
|
|
89 if not len(collections):
|
|
90 collections = [{'name' : name, 'filepaths' : [filepath]}]
|
|
91 else:
|
|
92 collection = [c for c in collections if c['name'] == name]
|
|
93 if len(collection):
|
|
94 collection[0]['filepaths'].append(filepath)
|
|
95 else:
|
|
96 collection = {'name' : name, 'filepaths' : [filepath]}
|
|
97 collections.append(collection)
|
|
98 logging.debug(f'\ncollections:\n{collections}\n\n')
|
|
99 if len(args.tomo_ranges) != 2*len(collections):
|
|
100 raise ValueError('Inconsistent tomo ranges size.')
|
|
101
|
|
102 # Instantiate Tomo object
|
|
103 tomo = Tomo(config_file=args.config, config_out=args.output_config, log_level=log_level,
|
|
104 log_stream=args.log, galaxy_flag=True)
|
|
105 if not tomo.is_valid:
|
|
106 raise ValueError('Invalid config file provided.')
|
|
107 logging.debug(f'config:\n{tomo.config}\n\n')
|
|
108
|
|
109 # Set theta inputs
|
|
110 theta_range = args.theta_range.split()
|
|
111 config_theta_range = tomo.config.get('theta_range')
|
|
112 if config_theta_range is None:
|
|
113 config_tomo.config['theta_range'] = {'start' : float(theta_range[0]),
|
|
114 'end' : float(theta_range[1]), 'num' : int(theta_range[2])}
|
|
115 else:
|
|
116 config_theta_range['start'] = float(theta_range[0])
|
|
117 config_theta_range['end'] = float(theta_range[1])
|
|
118 config_theta_range['num'] = int(theta_range[2])
|
|
119 tomo.cf.saveFile(args.output_config)
|
|
120
|
|
121 # Find dark field files
|
|
122 dark_field = tomo.config['dark_field']
|
|
123 tdf_files = [c['filepaths'] for c in collections if c['name'] == 'tdf']
|
|
124 if len(tdf_files) != 1 or len(tdf_files[0]) < 1:
|
|
125 logging.warning('Unable to obtain dark field files')
|
|
126 assert(dark_field['data_path'] is None)
|
|
127 assert(dark_field['img_start'] == -1)
|
|
128 assert(not dark_field['num'])
|
|
129 tdf_files = [None]
|
|
130 num_collections = 0
|
|
131 else:
|
|
132 dark_field['img_offset'] = args.tomo_ranges[0]
|
|
133 dark_field['num'] = args.tomo_ranges[1]
|
|
134 num_collections = 1
|
|
135
|
|
136 # Find bright field files
|
|
137 bright_field = tomo.config['bright_field']
|
|
138 bright_field['img_offset'] = args.tomo_ranges[2*num_collections]
|
|
139 bright_field['num'] = args.tomo_ranges[2*num_collections+1]
|
|
140 tbf_files = [c['filepaths'] for c in collections if c['name'] == 'tbf']
|
|
141 if len(tbf_files) != 1 or len(tbf_files[0]) < 1:
|
|
142 exit('Unable to obtain bright field files')
|
|
143 num_collections += 1
|
|
144
|
|
145 # Find tomography files
|
|
146 stack_info = tomo.config['stack_info']
|
|
147 if stack_info['num'] != len(collections) - num_collections:
|
|
148 raise ValueError('Inconsistent number of tomography data image sets')
|
|
149 tomo_stack_files = []
|
|
150 for stack in stack_info['stacks']:
|
|
151 stack['img_offset'] = args.tomo_ranges[2*num_collections]
|
|
152 stack['num'] = args.tomo_ranges[2*num_collections+1]
|
|
153 tomo_files = [c['filepaths'] for c in collections if c['name'] == f'set{stack["index"]}']
|
|
154 if len(tomo_files) != 1 or len(tomo_files[0]) < 1:
|
|
155 exit(f'Unable to obtain tomography images for set {stack["index"]}\n')
|
|
156 tomo_stack_files.append(tomo_files[0])
|
|
157 num_collections += 1
|
|
158
|
|
159 # Preprocess the image files
|
|
160 tomo.genTomoStacks(tdf_files[0], tbf_files[0], tomo_stack_files, args.dark, args.bright,
|
|
161 args.tomo, args.detectorbounds, args.output_data)
|
|
162 if not tomo.is_valid:
|
|
163 IOError('Unable to load all required image files.')
|
|
164
|
|
165 #RV make start_theta, end_theta and num_theta inputs
|
|
166
|
|
167 if __name__ == "__main__":
|
|
168 __main__()
|
|
169
|