changeset 60:52db7707ff48 draft

"planemo upload for repository https://github.com/rolfverberg/galaxytools commit 9a07ab3099737ee0d99e82739b55048f89c36bc6-dirty"
author rv43
date Tue, 16 Aug 2022 21:23:10 +0000
parents feb2a5fc7c76
children 75dd6e15f628
files __pycache__/general.cpython-39.pyc tomo_setup.py tomo_setup.xml
diffstat 3 files changed, 145 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
Binary file __pycache__/general.cpython-39.pyc has changed
--- a/tomo_setup.py	Tue Aug 16 16:55:50 2022 +0000
+++ b/tomo_setup.py	Tue Aug 16 21:23:10 2022 +0000
@@ -11,6 +11,7 @@
 import tracemalloc
 
 from tomo import Tomo
+from general import get_trailing_int
 
 #from memory_profiler import profile
 #@profile
@@ -27,6 +28,8 @@
             help='Input file collections')
     parser.add_argument('-c', '--config',
             help='Input config file')
+    parser.add_argument('--detector',
+            help='Detector info (number of rows and columns, and pixel size)')
     parser.add_argument('--num_theta',
             help='Number of theta angles')
     parser.add_argument('--theta_range',
@@ -53,7 +56,12 @@
     logging.basicConfig(format=logging_format, level=level, force=True,
             handlers=[logging.StreamHandler()])
 
+    # Check command line arguments
     logging.info(f'config = {args.config}')
+    if args.detector is None:
+        logging.info(f'detector = {args.detector}')
+    else:
+        logging.info(f'detector = {args.detector.split()}')
     logging.info(f'num_theta = {args.num_theta}')
     if args.theta_range is None:
         logging.info(f'theta_range = {args.theta_range}')
@@ -63,8 +71,15 @@
     logging.info(f'output_data = {args.output_data}')
     logging.info(f'log = {args.log}')
     logging.debug(f'is log stdout? {args.log is sys.stdout}')
+    if args.detector is not None and len(args.detector.split()) != 3:
+        raise ValueError(f'Invalid detector: {args.detector}')
+    if args.num_theta is None or int(args.num_theta) < 1:
+        raise ValueError(f'Invalid num_theta: {args.num_theta}')
+    if args.theta_range is not None and len(args.theta_range.split()) != 2:
+        raise ValueError(f'Invalid theta_range: {args.theta_range}')
+    num_theta = int(args.num_theta)
 
-    # Read tool config input
+    # Read and check tool config input
     inputconfig = []
     with open(args.inputconfig) as f:
         inputconfig = [line.strip() for line in f if line.strip() and not line.startswith('#')]
@@ -78,6 +93,8 @@
     if config_type == 'config_manual':
         assert(len(inputconfig) == 7)
         ref_heights = [float(x.strip()) for x in inputconfig[6].split()]
+        assert(args.detector is not None)
+        assert(args.theta_range is not None)
     else:
         ref_heights = None
     logging.info(f'config_type = {config_type} {type(config_type)}')
@@ -87,6 +104,18 @@
     logging.info(f'num_imgs = {num_imgs} {type(num_imgs)}')
     logging.info(f'img_offsets = {img_offsets} {type(img_offsets)}')
     logging.info(f'ref_heights = {ref_heights} {type(ref_heights)}')
+    if config_type != 'config_file' and config_type != 'config_manual':
+        raise ValueError('Invalid input config provided.')
+    if input_type != 'collections' and input_type != 'files':
+        raise ValueError('Invalid input config provided.')
+    if len(stack_types) != num_stack:
+        raise ValueError('Invalid input config provided.')
+    if len(num_imgs) != num_stack:
+        raise ValueError('Invalid input config provided.')
+    if len(img_offsets) != num_stack:
+        raise ValueError('Invalid input config provided.')
+    if ref_heights is not None and len(ref_heights) != num_stack:
+        raise ValueError('Invalid input config provided.')
 
     # Read input files and collect data files info
     datasets = []
@@ -97,22 +126,31 @@
             fields = [x.strip() for x in line.split('\t')]
             filepath = fields[0]
             element_identifier = fields[1] if len(fields) > 1 else fields[0].split('/')[-1]
-            datasets.append({'element_identifier' : fields[1], 'filepath' : filepath})
+            datasets.append({'element_identifier' : element_identifier, 'filepath' : filepath})
+    print(f'datasets:\n{datasets}')
     logging.debug(f'datasets:\n{datasets}')
-    print(f'datasets:\n{datasets}')
+    if input_type == 'files' and len(datasets) != num_stack:
+        raise ValueError('Inconsistent number of input files provided.')
 
     # Read and sort data files
     collections = []
-    for dataset in datasets:
-        element_identifier = [x.strip() for x in dataset['element_identifier'].split('_')]
-        if len(element_identifier) > 1:
-            name = element_identifier[0]
+    stack_index = 1
+    for i, dataset in enumerate(datasets):
+        if input_type == 'collections':
+            element_identifier = [x.strip() for x in dataset['element_identifier'].split('_')]
+            if len(element_identifier) > 1:
+                name = element_identifier[0]
+            else:
+                name = 'other'
         else:
-            name = 'other'
+            if stack_types[i] == 'tdf' or stack_types[i] == 'tbf':
+                name = stack_types[i]
+            elif stack_types[i] == 'data':
+                name = f'set{stack_index}'
+                stack_index += 1
+            else:
+                raise ValueError('Invalid input config provided.')
         filepath = dataset['filepath']
-        print(f'element_identifier = {element_identifier} {len(element_identifier)}')
-        print(f'name = {name}')
-        print(f'filepath = {filepath}')
         if not len(collections):
             collections = [{'name' : name, 'filepaths' : [filepath]}]
         else:
@@ -122,65 +160,130 @@
             else:
                 collection = {'name' : name, 'filepaths' : [filepath]}
                 collections.append(collection)
+    print(f'collections:\n{collections}')
     logging.debug(f'collections:\n{collections}')
-    print(f'collections:\n{collections}')
-    return
 
     # Instantiate Tomo object
     tomo = Tomo(config_file=args.config, config_out=args.output_config, log_level=log_level,
             log_stream=args.log, galaxy_flag=True)
-    if not tomo.is_valid:
-        raise ValueError('Invalid config file provided.')
+    if config_type == 'config_file':
+        if not tomo.is_valid:
+            raise ValueError('Invalid config file provided.')
+    else:
+        assert(tomo.config is None)
+        tomo.config = {}
     logging.debug(f'config:\n{tomo.config}')
 
+    # Set detector inputs
+    if config_type == 'config_manual':
+        detector = args.detector.split()
+        tomo.config['detector'] = {'rows' : int(detector[0]),
+                'columns' : int(detector[1]), 'pixel_size' : float(detector[2])}
+
     # Set theta inputs
-    theta_range = args.theta_range.split()
     config_theta_range = tomo.config.get('theta_range')
     if config_theta_range is None:
-        config_tomo.config['theta_range'] = {'start' : float(theta_range[0]),
-            'end' : float(theta_range[1]), 'num' : int(theta_range[2])}
+        tomo.config['theta_range'] = {'num' : num_theta}
+        config_theta_range = tomo.config['theta_range']
     else:
+        config_theta_range['num'] = num_theta
+    if config_type == 'config_manual':
+        theta_range = args.theta_range.split()
         config_theta_range['start'] = float(theta_range[0])
         config_theta_range['end'] = float(theta_range[1])
-        config_theta_range['num'] = int(theta_range[2])
 
     # Find dark field files
-    dark_field = tomo.config['dark_field']
+    dark_field = tomo.config.get('dark_field')
     tdf_files = [c['filepaths'] for c in collections if c['name'] == 'tdf']
     if len(tdf_files) != 1 or len(tdf_files[0]) < 1:
         logging.warning('Unable to obtain dark field files')
-        assert(dark_field['data_path'] is None)
-        assert(dark_field['img_start'] == -1)
-        assert(not dark_field['num'])
+        if config_type == 'config_file':
+            assert(dark_field is not None)
+            assert(dark_field['data_path'] is None)
+            assert(dark_field['img_start'] == -1)
+            assert(not dark_field['num'])
+        else:
+            tomo.config['dark_field'] = {'data_path' : None, 'img_start' : -1, 'num' : 0}
         tdf_files = [None]
         num_collections = 0
     else:
-        dark_field['img_offset'] = args.tomo_ranges[0]
-        dark_field['num'] = args.tomo_ranges[1]
+        if config_type == 'config_file':
+            assert(dark_field is not None)
+            assert(dark_field['data_path'] is not None)
+            assert(dark_field.get('img_start') is not None)
+        else:
+            tomo.config['dark_field'] = {'data_path' : tdf_files[0], 'img_start' : 0}
+            dark_field = tomo.config['dark_field']
+        tdf_index = [i for i,c in enumerate(collections) if c['name'] == 'tdf']
+        tdf_index_check = [i for i,s in enumerate(stack_types) if s == 'tdf']
+        if tdf_index != tdf_index_check:
+            raise ValueError(f'Inconsistent tdf_index ({tdf_index} vs. {tdf_index_check}).')
+        tdf_index = tdf_index[0]
+        dark_field['img_offset'] = img_offsets[tdf_index]
+        dark_field['num'] = num_imgs[tdf_index]
         num_collections = 1
 
     # Find bright field files
-    bright_field = tomo.config['bright_field']
-    bright_field['img_offset'] = args.tomo_ranges[2*num_collections]
-    bright_field['num'] = args.tomo_ranges[2*num_collections+1]
+    bright_field = tomo.config.get('bright_field')
     tbf_files = [c['filepaths'] for c in collections if c['name'] == 'tbf']
     if len(tbf_files) != 1 or len(tbf_files[0]) < 1:
         exit('Unable to obtain bright field files')
+    if config_type == 'config_file':
+        assert(bright_field is not None)
+        assert(bright_field['data_path'] is not None)
+        assert(bright_field.get('img_start') is not None)
+    else:
+        tomo.config['bright_field'] = {'data_path' : tbf_files[0], 'img_start' : 0}
+        bright_field = tomo.config['bright_field']
+    tbf_index = [i for i,c in enumerate(collections) if c['name'] == 'tbf']
+    tbf_index_check = [i for i,s in enumerate(stack_types) if s == 'tbf']
+    if tbf_index != tbf_index_check:
+        raise ValueError(f'Inconsistent tbf_index ({tbf_index} vs. {tbf_index_check}).')
+    tbf_index = tbf_index[0]
+    bright_field['img_offset'] = img_offsets[tbf_index]
+    bright_field['num'] = num_imgs[tbf_index]
     num_collections += 1
 
     # Find tomography files
-    stack_info = tomo.config['stack_info']
-    if stack_info['num'] != len(collections) - num_collections:
-        raise ValueError('Inconsistent number of tomography data image sets')
+    stack_info = tomo.config.get('stack_info')
+    if config_type == 'config_file':
+        assert(stack_info is not None)
+        if stack_info['num'] != len(collections) - num_collections:
+            raise ValueError('Inconsistent number of tomography data image sets')
+        assert(stack_info.get('stacks') is not None)
+        for stack in stack_info['stacks']:
+            assert(stack['data_path'] is not None)
+            assert(stack.get('img_start') is not None)
+            assert(stack.get('index') is not None)
+            assert(stack.get('ref_height') is not None)
+    else:
+        tomo.config['stack_info'] = {'num' : len(collections) - num_collections, 'stacks' : []}
+        stack_info = tomo.config['stack_info']
+        for i in range(stack_info['num']):
+            stack_info['stacks'].append({'img_start' : 0, 'index' : i+1})
     tomo_stack_files = []
     for stack in stack_info['stacks']:
-        stack['img_offset'] = args.tomo_ranges[2*num_collections]
-        stack['num'] = args.tomo_ranges[2*num_collections+1]
-        tomo_files = [c['filepaths'] for c in collections if c['name'] == f'set{stack["index"]}']
+        index = stack['index']
+        tomo_files = [c['filepaths'] for c in collections if c['name'] == f'set{index}']
         if len(tomo_files) != 1 or len(tomo_files[0]) < 1:
-            exit(f'Unable to obtain tomography images for set {stack["index"]}')
+            exit(f'Unable to obtain tomography images for set {index}')
+        tomo_index = [i for i,c in enumerate(collections) if c['name'] == f'set{index}']
+        if len(tomo_index) != 1:
+            raise ValueError(f'Illegal tomo_index ({tomo_index}).')
+        tomo_index = tomo_index[0]
+        stack['img_offset'] = img_offsets[tomo_index]
+        assert(num_imgs[tomo_index] == -1)
+        stack['num'] = num_theta
+        if config_type == 'config_manual':
+            if len(tomo_files) == 1:
+                stack['data_path'] = tomo_files[0]
+            stack['ref_height'] = ref_heights[tomo_index]
         tomo_stack_files.append(tomo_files[0])
         num_collections += 1
+    if num_collections != num_stack:
+        raise ValueError('Inconsistent number of data image sets')
+    print(f'config:\n{tomo.config}')
+    return
 
     # Preprocess the image files
     galaxy_param = {'tdf_files' : tdf_files[0], 'tbf_files' : tbf_files[0],
--- a/tomo_setup.xml	Tue Aug 16 16:55:50 2022 +0000
+++ b/tomo_setup.xml	Tue Aug 16 21:23:10 2022 +0000
@@ -16,6 +16,7 @@
                 -c '$config'
                 --num_theta '$config_type.num_theta'
             #else
+                --detector '$config_type.detector.num_row $config_type.detector.num_column $config_type.detector.pixel_size'
                 --num_theta '$config_type.thetas.num_theta'
                 --theta_range '$config_type.thetas.theta_start $config_type.thetas.theta_end'
             #end if
@@ -72,7 +73,7 @@
                     #end for
                 #else
                     #for $s in $config_type.input.tomo_sets
-                        #echo str($s.inputs) + '\t' + str($s.set_type.set_selector) #
+                        #echo str($s.inputs) #
                     #end for
                 #end if
              ]]>
@@ -146,6 +147,11 @@
                     <param name="theta_start" type="float" min="0.0" max="360.0" value="0.0" optional="false" label="Start angle"/>
                     <param name="theta_end" type="float" min="0.0" max="360.0" value="180.0" optional="false" label="End angle"/>
                 </section>
+                <section name="detector" title="Detector parameters">
+                    <param name="num_row" type="integer" min="1" value="0" optional="false" label="Number of pixel rows"/>
+                    <param name="num_column" type="integer" min="1" value="0" optional="false" label="Number of pixel columns"/>
+                    <param name="pixel_size" type="float" min="0.0" value="0.0" optional="false" label="Pixel size (corrected for lens magnification)(RV: needs unit)"/>
+                </section>
                 <conditional name="input">
                     <param name="type_selector" type="select" label="Choose the dataset format">
                         <option value="collections">datasets as collections</option>