Mercurial > repos > rv43 > tomo
changeset 15:1bcca1f2adb4 draft
"planemo upload for repository https://github.com/rolfverberg/galaxytools commit 38c19bf5addbc46f45d598f981bb1a48f7bca691"
author | rv43 |
---|---|
date | Wed, 13 Apr 2022 16:02:44 +0000 |
parents | 50c8d19d5f89 |
children | 44f168e1e14f |
files | tomo.py tomo_find_center.py tomo_reconstruct.py tomo_reconstruct.xml tomo_setup.py |
diffstat | 5 files changed, 91 insertions(+), 50 deletions(-) [+] |
line wrap: on
line diff
--- a/tomo.py Wed Apr 13 15:53:06 2022 +0000 +++ b/tomo.py Wed Apr 13 16:02:44 2022 +0000 @@ -983,9 +983,11 @@ np.save(tomo_file, stack) logging.info(f'... done in {time()-t0:.2f} seconds!') - def _genTomo(self, tomo_stack_files, available_stacks): + def _genTomo(self, tomo_stack_files, available_stacks, num_core=None): """Generate tomography fields. """ + if num_core is None: + num_core = self.num_core stacks = self.config['stack_info']['stacks'] assert(len(self.tomo_stacks) == self.config['stack_info']['num']) assert(len(self.tomo_stacks) == len(stacks)) @@ -1445,11 +1447,11 @@ f'{self.tomo_stacks[i].shape}') logging.info(f'... done in {time()-t0:.2f} seconds!') - def genTomoStacks(self, tdf_files=None, tbf_files=None, tomo_stack_files=None, - dark_field_pngname=None, bright_field_pngname=None, tomo_field_pngname=None, - detectorbounds_pngname=None, output_name=None): + def genTomoStacks(self, galaxy_param=None, num_core=None): """Preprocess tomography images. """ + if num_core is None: + num_core = self.num_core # Try loading any already preprocessed stacks (skip in Galaxy) # preprocessed stack order for each one in stack: row,theta,column stack_info = self.config['stack_info'] @@ -1458,22 +1460,15 @@ assert(num_tomo_stacks == len(self.tomo_stacks)) available_stacks = [False]*num_tomo_stacks if self.galaxy_flag: - assert(tdf_files is None or isinstance(tdf_files, list)) - assert(isinstance(tbf_files, list)) - assert(isinstance(tomo_stack_files, list)) + assert(isinstance(galaxy_param, dict)) + tdf_files = galaxy_param['tdf_files'] + tbf_files = galaxy_param['tbf_files'] + tomo_stack_files = galaxy_param['tomo_stack_files'] assert(num_tomo_stacks == len(tomo_stack_files)) - assert(isinstance(dark_field_pngname, str)) - assert(isinstance(bright_field_pngname, str)) - assert(isinstance(tomo_field_pngname, str)) - assert(isinstance(detectorbounds_pngname, str)) - assert(isinstance(output_name, str)) else: - if tdf_files: - logging.warning('Ignoring tdf_files in genTomoStacks (only for Galaxy)') - if tbf_files: - logging.warning('Ignoring tbf_files in genTomoStacks (only for Galaxy)') - if tomo_stack_files: - logging.warning('Ignoring tomo_stack_files in genTomoStacks (only for Galaxy)') + if galaxy_param: + logging.warning('Ignoring galaxy_param in findCenters (only for Galaxy)') + galaxy_param = None tdf_files, tbf_files, tomo_stack_files = self.findImageFiles() if not self.is_valid: return @@ -1481,18 +1476,6 @@ if not self.tomo_stacks[i].size and stack.get('preprocessed', False): self.tomo_stacks[i], available_stacks[i] = \ self._loadTomo('red stack', stack['index']) - if dark_field_pngname: - logging.warning('Ignoring dark_field_pngname in genTomoStacks (only for Galaxy)') - if bright_field_pngname: - logging.warning('Ignoring bright_field_pngname in genTomoStacks (only for Galaxy)') - if tomo_field_pngname: - logging.warning('Ignoring tomo_field_pngname in genTomoStacks (only for Galaxy)') - if detectorbounds_pngname: - logging.warning('Ignoring detectorbounds_pngname in genTomoStacks '+ - '(only used in Galaxy)') - if output_name: - logging.warning('Ignoring output_name in genTomoStacks '+ - '(only used in Galaxy)') # Preprocess any unloaded stacks if False in available_stacks: @@ -1506,23 +1489,25 @@ # Generate dark field if tdf_files: - self._genDark(tdf_files, dark_field_pngname) + self._genDark(tdf_files, galaxy_param['dark_field_pngname']) # Generate bright field - self._genBright(tbf_files, bright_field_pngname) + self._genBright(tbf_files, galaxy_param['bright_field_pngname']) # Set vertical detector bounds for image stack - self._setDetectorBounds(tomo_stack_files, tomo_field_pngname, detectorbounds_pngname) + self._setDetectorBounds(tomo_stack_files, galaxy_param['tomo_field_pngname'], + galaxy_param['detectorbounds_pngname']) # Set zoom and/or theta skip to reduce memory the requirement self._setZoomOrSkip() # Generate tomography fields - self._genTomo(tomo_stack_files, available_stacks) + self._genTomo(tomo_stack_files, available_stacks, num_core) # Save tomography stack to file if self.galaxy_flag: t0 = time() + output_name = galaxy_param['output_name'] logging.info(f'Saving preprocessed tomography stack to {output_name} ...') save_stacks = {f'set_{stack["index"]}':tomo_stack for stack,tomo_stack in zip(stacks,self.tomo_stacks)} @@ -1889,7 +1874,7 @@ # Update config file self.config = msnc.update('config.txt', 'check_centers', True, 'find_centers') - def reconstructTomoStacks(self, output_name=None, num_core=None): + def reconstructTomoStacks(self, galaxy_param=None, num_core=None): """Reconstruct tomography stacks. """ if num_core is None: @@ -1900,11 +1885,20 @@ assert(len(self.tomo_stacks) == len(stacks)) assert(len(self.tomo_recon_stacks) == len(stacks)) if self.galaxy_flag: - assert(isinstance(output_name, str)) + assert(isinstance(galaxy_param, dict)) + # Get rotation axis centers + center_offsets = galaxy_param['center_offsets'] + assert(isinstance(center_offsets, list) and len(center_offsets) == 2) + lower_center_offset = center_offsets[0] + assert(msnc.is_num(lower_center_offset)) + upper_center_offset = center_offsets[1] + assert(msnc.is_num(upper_center_offset)) else: - if output_name: - logging.warning('Ignoring output_name in reconstructTomoStacks '+ - '(only used in Galaxy)') + if galaxy_param: + logging.warning('Ignoring galaxy_param in reconstructTomoStacks (only for Galaxy)') + galaxy_param = None + lower_center_offset = None + upper_center_offset = None # Get rotation axis rows and centers find_center = self.config['find_center'] @@ -1912,20 +1906,22 @@ if lower_row is None: logging.error('Unable to read lower_row from config') return - lower_center_offset = find_center.get('lower_center_offset') - if lower_center_offset is None: - logging.error('Unable to read lower_center_offset from config') - return upper_row = find_center.get('upper_row') if upper_row is None: logging.error('Unable to read upper_row from config') return - upper_center_offset = find_center.get('upper_center_offset') - if upper_center_offset is None: - logging.error('Unable to read upper_center_offset from config') - return logging.debug(f'lower_row = {lower_row} upper_row = {upper_row}') assert(lower_row < upper_row) + if lower_center_offset is None: + lower_center_offset = find_center.get('lower_center_offset') + if lower_center_offset is None: + logging.error('Unable to read lower_center_offset from config') + return + if upper_center_offset is None: + upper_center_offset = find_center.get('upper_center_offset') + if upper_center_offset is None: + logging.error('Unable to read upper_center_offset from config') + return center_slope = (upper_center_offset-lower_center_offset)/(upper_row-lower_row) # Set thetas (in radians) @@ -2004,6 +2000,7 @@ # Save reconstructed tomography stack to file if self.galaxy_flag: t0 = time() + output_name = galaxy_param['output_name'] logging.info(f'Saving reconstructed tomography stack to {output_name} ...') save_stacks = {f'set_{stack["index"]}':tomo_stack for stack,tomo_stack in zip(stacks,self.tomo_recon_stacks)}
--- a/tomo_find_center.py Wed Apr 13 15:53:06 2022 +0000 +++ b/tomo_find_center.py Wed Apr 13 16:02:44 2022 +0000 @@ -4,6 +4,7 @@ import sys import argparse +import tracemalloc from tomo import Tomo @@ -34,6 +35,9 @@ type=argparse.FileType('w'), default=sys.stdout, help='Log file') args = parser.parse_args() + # Starting memory monitoring + tracemalloc.start() + # Set basic log configuration logging_format = '%(asctime)s : %(levelname)s - %(module)s : %(funcName)s - %(message)s' log_level = 'INFO' @@ -71,6 +75,12 @@ 'set_range' : args.set_range, 'set_step' : args.set_step} tomo.findCenters(galaxy_param) + # Displaying memory usage + logging.info(f'Memory usage: {tracemalloc.get_traced_memory()}') + + # stopping memory monitoring + tracemalloc.stop() + if __name__ == "__main__": __main__()
--- a/tomo_reconstruct.py Wed Apr 13 15:53:06 2022 +0000 +++ b/tomo_reconstruct.py Wed Apr 13 16:02:44 2022 +0000 @@ -4,6 +4,7 @@ import sys import argparse +import tracemalloc from tomo import Tomo @@ -16,6 +17,8 @@ help='Preprocessed image file stacks') parser.add_argument('-c', '--config', help='Input config') + parser.add_argument('--center_offsets', + required=True, nargs=2, type=float, help='Reconstruction center axis offsets') parser.add_argument('--output_config', help='Output config') parser.add_argument('--output_data', @@ -26,6 +29,9 @@ help='Log file') args = parser.parse_args() + # Starting memory monitoring + tracemalloc.start() + # Set basic log configuration logging_format = '%(asctime)s : %(levelname)s - %(module)s : %(funcName)s - %(message)s' log_level = 'INFO' @@ -37,6 +43,7 @@ logging.debug(f'input_stacks = {args.input_stacks}') logging.debug(f'config = {args.config}') + logging.debug(f'center_offsets = {args.center_offsets} {type(args.center_offsets)}') logging.debug(f'output_config = {args.output_config}') logging.debug(f'output_data = {args.output_data}') logging.debug(f'log = {args.log}') @@ -53,7 +60,14 @@ tomo.loadTomoStacks(args.input_stacks) # Reconstruct tomography stacks - tomo.reconstructTomoStacks(args.output_data) + galaxy_param = {'center_offsets' : args.center_offsets, 'output_name' : args.output_data} + tomo.reconstructTomoStacks(galaxy_param) + + # Displaying memory usage + logging.info(f'Memory usage: {tracemalloc.get_traced_memory()}') + + # stopping memory monitoring + tracemalloc.stop() if __name__ == "__main__": __main__()
--- a/tomo_reconstruct.xml Wed Apr 13 15:53:06 2022 +0000 +++ b/tomo_reconstruct.xml Wed Apr 13 16:02:44 2022 +0000 @@ -8,6 +8,7 @@ $__tool_directory__/tomo_reconstruct.py -i '$red_stacks' -c '$config' + --center_offsets $center_offsets.lower_center_offset $center_offsets.upper_center_offset --output_data 'output.npz' --output_config 'output_config.yaml' -l '$log' @@ -15,6 +16,10 @@ <inputs> <expand macro="common_inputs"/> <param name="red_stacks" type='data' format='npz' optional='false' label="Reduced stacks"/> + <section name="center_offsets" title="Reconstruction center axis offsets"> + <param name="lower_center_offset" type="float" optional="false" label="Lower center offset"/> + <param name="upper_center_offset" type="float" optional="false" label="Upper center offset"/> + </section> </inputs> <outputs> <expand macro="common_outputs"/>
--- a/tomo_setup.py Wed Apr 13 15:53:06 2022 +0000 +++ b/tomo_setup.py Wed Apr 13 16:02:44 2022 +0000 @@ -8,10 +8,13 @@ import yaml import argparse import numpy as np +import tracemalloc from tomo import Tomo import msnc_tools as msnc +#from memory_profiler import profile +#@profile def __main__(): # Parse command line arguments @@ -43,6 +46,9 @@ parser.add_argument('tomo_ranges', metavar='N', type=int, nargs='+') args = parser.parse_args() + # Starting memory monitoring + tracemalloc.start() + # Set basic log configuration logging_format = '%(asctime)s : %(levelname)s - %(module)s : %(funcName)s - %(message)s' log_level = 'INFO' @@ -155,11 +161,20 @@ num_collections += 1 # Preprocess the image files - tomo.genTomoStacks(tdf_files[0], tbf_files[0], tomo_stack_files, args.dark, args.bright, - args.tomo, args.detectorbounds, args.output_data) + galaxy_param = {'tdf_files' : tdf_files[0], 'tbf_files' : tbf_files[0], + 'tomo_stack_files' : tomo_stack_files, 'dark_field_pngname' : args.dark, + 'bright_field_pngname' : args.bright, 'tomo_field_pngname' : args.tomo, + 'detectorbounds_pngname' : args.detectorbounds, 'output_name' : args.output_data} + tomo.genTomoStacks(galaxy_param) if not tomo.is_valid: IOError('Unable to load all required image files.') + # Displaying memory usage + logging.info(f'Memory usage: {tracemalloc.get_traced_memory()}') + + # stopping memory monitoring + tracemalloc.stop() + if __name__ == "__main__": __main__()