Mercurial > repos > rv43 > tomo
comparison tomo_setup.py @ 49:26f99fdd8d61 draft
"planemo upload for repository https://github.com/rolfverberg/galaxytools commit 4f7738d02f4a3fd91373f43937ed311b6fe11a12"
| author | rv43 |
|---|---|
| date | Thu, 28 Jul 2022 16:05:24 +0000 |
| parents | ef5c2f7b49ec |
| children | 79c216516ef9 |
comparison
equal
deleted
inserted
replaced
| 48:059819ea1f0e | 49:26f99fdd8d61 |
|---|---|
| 9 import argparse | 9 import argparse |
| 10 import numpy as np | 10 import numpy as np |
| 11 import tracemalloc | 11 import tracemalloc |
| 12 | 12 |
| 13 from tomo import Tomo | 13 from tomo import Tomo |
| 14 import msnc_tools as msnc | |
| 15 | 14 |
| 16 #from memory_profiler import profile | 15 #from memory_profiler import profile |
| 17 #@profile | 16 #@profile |
| 18 def __main__(): | 17 def __main__(): |
| 19 | 18 |
| 20 # Parse command line arguments | 19 # Parse command line arguments |
| 21 parser = argparse.ArgumentParser( | 20 parser = argparse.ArgumentParser( |
| 22 description='Setup tomography reconstruction') | 21 description='Setup tomography reconstruction') |
| 23 parser.add_argument('-i', '--inputfiles', | 22 parser.add_argument('-i', '--inputfiles', |
| 23 nargs='+', | |
| 24 default='inputfiles.txt', | 24 default='inputfiles.txt', |
| 25 help='Input file collections') | 25 help='Input file datasets or collections') |
| 26 parser.add_argument('-c', '--config', | 26 parser.add_argument('-c', '--config', |
| 27 help='Input config') | 27 help='Input config') |
| 28 parser.add_argument('-l', '--log', | |
| 29 type=argparse.FileType('w'), | |
| 30 default=sys.stdout, | |
| 31 help='Log file') | |
| 32 parser.add_argument('-t', '--inputfile_types', | |
| 33 nargs='+', | |
| 34 default='collection', | |
| 35 help='Input files type (collection or a list of set types: dark, bright, or data)') | |
| 28 parser.add_argument('--theta_range', | 36 parser.add_argument('--theta_range', |
| 29 help='Theta range (lower bound, upper bound, number of angles)') | 37 help='Theta range (lower bound, upper bound, number of angles)') |
| 30 parser.add_argument('--output_config', | 38 parser.add_argument('--output_config', |
| 31 help='Output config') | 39 help='Output config') |
| 32 parser.add_argument('--output_data', | 40 parser.add_argument('--output_data', |
| 33 help='Preprocessed tomography data') | 41 help='Preprocessed tomography data') |
| 34 parser.add_argument('-l', '--log', | |
| 35 type=argparse.FileType('w'), | |
| 36 default=sys.stdout, | |
| 37 help='Log file') | |
| 38 parser.add_argument('tomo_ranges', metavar='N', type=int, nargs='+') | 42 parser.add_argument('tomo_ranges', metavar='N', type=int, nargs='+') |
| 39 args = parser.parse_args() | 43 args = parser.parse_args() |
| 40 | 44 |
| 41 # Starting memory monitoring | 45 # Starting memory monitoring |
| 42 tracemalloc.start() | 46 tracemalloc.start() |
| 49 raise ValueError(f'Invalid log_level: {log_level}') | 53 raise ValueError(f'Invalid log_level: {log_level}') |
| 50 logging.basicConfig(format=logging_format, level=level, force=True, | 54 logging.basicConfig(format=logging_format, level=level, force=True, |
| 51 handlers=[logging.StreamHandler()]) | 55 handlers=[logging.StreamHandler()]) |
| 52 | 56 |
| 53 logging.debug(f'config = {args.config}') | 57 logging.debug(f'config = {args.config}') |
| 58 logging.debug(f'inputfiles = {args.inputfiles}') | |
| 59 logging.debug(f'inputfile_types = {args.inputfile_types}') | |
| 60 logging.debug(f'log = {args.log}') | |
| 61 logging.debug(f'is log stdout? {args.log is sys.stdout}') | |
| 54 logging.debug(f'theta_range = {args.theta_range.split()}') | 62 logging.debug(f'theta_range = {args.theta_range.split()}') |
| 55 logging.debug(f'output_config = {args.output_config}') | 63 logging.debug(f'output_config = {args.output_config}') |
| 56 logging.debug(f'output_data = {args.output_data}') | 64 logging.debug(f'output_data = {args.output_data}') |
| 57 logging.debug(f'log = {args.log}') | |
| 58 logging.debug(f'is log stdout? {args.log is sys.stdout}') | |
| 59 logging.debug(f'tomoranges = {args.tomo_ranges}') | 65 logging.debug(f'tomoranges = {args.tomo_ranges}') |
| 60 | 66 |
| 61 # Read input files and collect data files info | 67 # Check input file type |
| 68 if isinstance(args.inputfile_types, str) and args.inputfile_types == 'collection': | |
| 69 input_as_collection = True | |
| 70 elif isinstance(args.inputfile_types, list): | |
| 71 input_as_collection = False | |
| 72 else: | |
| 73 raise ValueError(f'Invalid args.inputfile_types: {args.inputfile_types} '+ | |
| 74 f'{type(args.inputfile_types)}') | |
| 75 | |
| 62 datasets = [] | 76 datasets = [] |
| 63 with open(args.inputfiles) as cf: | 77 collections = [] |
| 64 for line in cf: | 78 if input_as_collection: |
| 65 if not line.strip() or line.startswith('#'): | 79 # Read input file collections and collect data files info |
| 66 continue | 80 with open(args.inputfiles) as cf: |
| 67 fields = [x.strip() for x in line.split('\t')] | 81 for line in cf: |
| 68 filepath = fields[0] | 82 if not line.strip() or line.startswith('#'): |
| 69 element_identifier = fields[1] if len(fields) > 1 else fields[0].split('/')[-1] | 83 continue |
| 70 datasets.append({'element_identifier' : fields[1], 'filepath' : filepath}) | 84 fields = [x.strip() for x in line.split('\t')] |
| 71 logging.debug(f'datasets:\n{datasets}') | 85 filepath = fields[0] |
| 86 element_identifier = fields[1] if len(fields) > 1 else fields[0].split('/')[-1] | |
| 87 datasets.append({'element_identifier' : fields[1], 'filepath' : filepath}) | |
| 88 logging.debug(f'datasets:\n{datasets}') | |
| 72 | 89 |
| 73 # Read and sort data files | 90 # Read and sort data files |
| 74 collections = [] | 91 for dataset in datasets: |
| 75 for dataset in datasets: | 92 element_identifier = [x.strip() for x in dataset['element_identifier'].split('_')] |
| 76 element_identifier = [x.strip() for x in dataset['element_identifier'].split('_')] | 93 if len(element_identifier) > 1: |
| 77 if len(element_identifier) > 1: | 94 name = element_identifier[0] |
| 78 name = element_identifier[0] | |
| 79 else: | |
| 80 name = 'other' | |
| 81 filepath = dataset['filepath'] | |
| 82 if not len(collections): | |
| 83 collections = [{'name' : name, 'filepaths' : [filepath]}] | |
| 84 else: | |
| 85 collection = [c for c in collections if c['name'] == name] | |
| 86 if len(collection): | |
| 87 collection[0]['filepaths'].append(filepath) | |
| 88 else: | 95 else: |
| 89 collection = {'name' : name, 'filepaths' : [filepath]} | 96 name = 'other' |
| 90 collections.append(collection) | 97 filepath = dataset['filepath'] |
| 98 if not len(collections): | |
| 99 collections = [{'name' : name, 'filepaths' : [filepath]}] | |
| 100 else: | |
| 101 collection = [c for c in collections if c['name'] == name] | |
| 102 if len(collection): | |
| 103 collection[0]['filepaths'].append(filepath) | |
| 104 else: | |
| 105 collection = {'name' : name, 'filepaths' : [filepath]} | |
| 106 collections.append(collection) | |
| 107 else: | |
| 108 # Collect input file datasets info | |
| 109 collections = [{'name' : filetype, 'filepaths' : [filepath]} | |
| 110 for filetype, filepath in zip(args.inputfile_types, args.inputfiles)] | |
| 91 logging.debug(f'collections:\n{collections}') | 111 logging.debug(f'collections:\n{collections}') |
| 92 if len(args.tomo_ranges) != 2*len(collections): | 112 if len(args.tomo_ranges) != 2*len(collections): |
| 93 raise ValueError('Inconsistent tomo ranges size.') | 113 raise ValueError('Inconsistent tomo ranges size.') |
| 94 | 114 |
| 95 # Instantiate Tomo object | 115 # Instantiate Tomo object |
| 140 raise ValueError('Inconsistent number of tomography data image sets') | 160 raise ValueError('Inconsistent number of tomography data image sets') |
| 141 tomo_stack_files = [] | 161 tomo_stack_files = [] |
| 142 for stack in stack_info['stacks']: | 162 for stack in stack_info['stacks']: |
| 143 stack['img_offset'] = args.tomo_ranges[2*num_collections] | 163 stack['img_offset'] = args.tomo_ranges[2*num_collections] |
| 144 stack['num'] = args.tomo_ranges[2*num_collections+1] | 164 stack['num'] = args.tomo_ranges[2*num_collections+1] |
| 145 tomo_files = [c['filepaths'] for c in collections if c['name'] == f'set{stack["index"]}'] | 165 if input_as_collection: |
| 166 tomo_files = [c['filepaths'] for c in collections | |
| 167 if c['name'] == f'set{stack["index"]}'] | |
| 168 else: | |
| 169 assert(collections[num_collections]['name'] == 'data') | |
| 170 tomo_files = [collections[num_collections]['filepaths']] | |
| 146 if len(tomo_files) != 1 or len(tomo_files[0]) < 1: | 171 if len(tomo_files) != 1 or len(tomo_files[0]) < 1: |
| 147 exit(f'Unable to obtain tomography images for set {stack["index"]}') | 172 exit(f'Unable to obtain tomography images for set {stack["index"]}') |
| 148 tomo_stack_files.append(tomo_files[0]) | 173 tomo_stack_files.append(tomo_files[0]) |
| 149 num_collections += 1 | 174 num_collections += 1 |
| 150 | 175 |
