changeset 13:e4963769076b draft

planemo upload for repository https://github.com/rolfverberg/galaxytools commit f8c4bdb31c20c468045ad5e6eb255a293244bc6c-dirty
author rv43
date Tue, 21 Mar 2023 14:07:35 +0000
parents fa511fc2cfd5
children da3116b620b3
files test.py test.xml tomo_find_center.py tomo_find_center.xml tomo_reduce.py tomo_reduce.xml workflow/.run_tomo.py.swp
diffstat 7 files changed, 142 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/test.py	Tue Mar 21 13:39:37 2023 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,21 +0,0 @@
-#!/usr/bin/env python3
-
-import sys
-import argparse
-
-def __main__():
-
-    # Parse command line arguments
-    parser = argparse.ArgumentParser(
-            description='Test')
-    parser.add_argument('-l', '--log', 
-            type=argparse.FileType('w'),
-            default=sys.stdout,
-            help='Log file')
-    args = parser.parse_args()
-
-    print('Hello world')
-
-if __name__ == "__main__":
-    __main__()
-
--- a/test.xml	Tue Mar 21 13:39:37 2023 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,21 +0,0 @@
-<tool id="test" name="Test" version="0.1.1" python_template_version="3.9">
-    <description>Test</description>
-    <command detect_errors="exit_code"><![CDATA[
-        $__tool_directory__/test.py
-        -l '$log'
-    ]]></command>
-    <outputs>
-        <data name="log" format="txt" label="Log"/>
-    </outputs>
-    <help><![CDATA[
-        Test.
-    ]]></help>
-    <citations>
-        <citation type="bibtex">
-@misc{test,
-  author = {Verberg, Rolf},
-  year = {2022},
-  title = {Test},
-}</citation>
-    </citations>
-</tool>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tomo_find_center.py	Tue Mar 21 14:07:35 2023 +0000
@@ -0,0 +1,98 @@
+#!/usr/bin/env python3
+
+import logging
+
+import argparse
+import pathlib
+import sys
+#import tracemalloc
+
+from workflow.run_tomo import Tomo
+
+#from memory_profiler import profile
+#@profile
+def __main__():
+    # Parse command line arguments
+    parser = argparse.ArgumentParser(
+            description='Find the center axis for a tomography reconstruction')
+    parser.add_argument('-i', '--input_file',
+            required=True,
+            type=pathlib.Path,
+            help='''Full or relative path to the input file (in Nexus format).''')
+    parser.add_argument('-o', '--output_file',
+            required=False,
+            type=pathlib.Path,
+            help='''Full or relative path to the output file (in yaml format).''')
+    parser.add_argument('--center_rows',
+            nargs=2,
+            type=int,
+            help='''Center finding rows.''')
+    parser.add_argument('--galaxy_flag',
+            action='store_true',
+            help='''Use this flag to run the scripts as a galaxy tool.''')
+    parser.add_argument('-l', '--log',
+#            type=argparse.FileType('w'),
+            default=sys.stdout,
+            help='Logging stream or filename')
+    parser.add_argument('--log_level',
+            choices=logging._nameToLevel.keys(),
+            default='INFO',
+            help='''Specify a preferred logging level.''')
+    args = parser.parse_args()
+
+    # Set log configuration
+    # When logging to file, the stdout log level defaults to WARNING
+    logging_format = '%(asctime)s : %(levelname)s - %(module)s : %(funcName)s - %(message)s'
+    level = logging.getLevelName(args.log_level)
+    if args.log is sys.stdout:
+        logging.basicConfig(format=logging_format, level=level, force=True,
+                handlers=[logging.StreamHandler()])
+    else:
+        if isinstance(args.log, str):
+            logging.basicConfig(filename=f'{args.log}', filemode='w',
+                    format=logging_format, level=level, force=True)
+        elif isinstance(args.log, io.TextIOWrapper):
+            logging.basicConfig(filemode='w', format=logging_format, level=level,
+                    stream=args.log, force=True)
+        else:
+            raise(ValueError(f'Invalid argument --log: {args.log}'))
+        stream_handler = logging.StreamHandler()
+        logging.getLogger().addHandler(stream_handler)
+        stream_handler.setLevel(logging.WARNING)
+        stream_handler.setFormatter(logging.Formatter(logging_format))
+
+    # Starting memory monitoring
+#    tracemalloc.start()
+
+    # Log command line arguments
+    logging.info(f'input_file = {args.input_file}')
+    logging.info(f'output_file = {args.output_file}')
+    logging.info(f'center_rows = {args.center_rows}')
+    logging.info(f'galaxy_flag = {args.galaxy_flag}')
+    logging.debug(f'log = {args.log}')
+    logging.debug(f'is log stdout? {args.log is sys.stdout}')
+    logging.debug(f'log_level = {args.log_level}')
+
+    # Instantiate Tomo object
+    tomo = Tomo(galaxy_flag=args.galaxy_flag)
+
+    # Read input file
+    data = tomo.read(args.input_file)
+
+    # Find the calibrated center axis info
+    data = tomo.find_centers(data, center_rows=args.center_rows)
+
+    # Write output file
+    data = tomo.write(data, args.output_file)
+
+    # Displaying memory usage
+#    logging.info(f'Memory usage: {tracemalloc.get_traced_memory()}')
+ 
+    # stopping memory monitoring
+#    tracemalloc.stop()
+
+    logger.info('Completed find center axis')
+
+
+if __name__ == "__main__":
+    __main__()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tomo_find_center.xml	Tue Mar 21 14:07:35 2023 +0000
@@ -0,0 +1,35 @@
+<tool id="tomo_find_center" name="Tomo Find Center Axis" version="0.3.0" python_template_version="3.9">
+    <description>Find the center axis for a tomography reconstruction</description>
+    <macros>
+        <import>tomo_macros.xml</import>
+    </macros>
+    <expand macro="requirements" />
+    <command detect_errors="exit_code">
+        <![CDATA[
+            mkdir find_center_pngs;
+            $__tool_directory__/tomo_find_center.py
+            --input_file '$input_file'
+            --output_file 'output.yaml'
+            --galaxy_flag
+            --center_rows $center_rows.low $center_rows.upp
+            -l '$log'
+    ]]></command>
+    <inputs>
+        <param name="input_file" type="data" optional="false" label="Input file"/>
+        <section name="center_rows" title="Reconstruction row bounds">
+            <param name="low" type="integer" value="-1" label="Lower bound"/>
+            <param name="upp" type="integer" value="-1" label="Upper bound"/>
+        </section>
+    </inputs>
+    <outputs>
+        <expand macro="common_outputs"/>
+        <collection name="find_center_pngs" type="list" label="Recontructed slice images">
+            <discover_datasets pattern="__name_and_ext__" directory="find_center_pngs"/>
+        </collection>
+        <data name="output_file" format="yaml" label="Center axis info" from_work_dir="output.yaml"/>
+    </outputs>
+    <help><![CDATA[
+        Find the center axis for a tomography reconstruction.
+    ]]></help>
+    <expand macro="citations"/>
+</tool>
--- a/tomo_reduce.py	Tue Mar 21 13:39:37 2023 +0000
+++ b/tomo_reduce.py	Tue Mar 21 14:07:35 2023 +0000
@@ -18,7 +18,7 @@
     parser.add_argument('-i', '--input_file',
             required=True,
             type=pathlib.Path,
-            help='''Full or relative path to the input file (in yaml or nxs format).''')
+            help='''Full or relative path to the input file (in yaml or Nexus format).''')
     parser.add_argument('-o', '--output_file',
             required=True,
             type=pathlib.Path,
@@ -92,5 +92,8 @@
     # Stop memory monitoring
 #    tracemalloc.stop()
 
+    logger.info('Completed tomography data reduction')
+
+
 if __name__ == "__main__":
     __main__()
--- a/tomo_reduce.xml	Tue Mar 21 13:39:37 2023 +0000
+++ b/tomo_reduce.xml	Tue Mar 21 14:07:35 2023 +0000
@@ -11,7 +11,7 @@
             --input_file '$input_file'
             --output_file 'output.nxs'
             --galaxy_flag
-            --img_x_bounds $x_bounds.x_bound_low $x_bounds.x_bound_upp
+            --img_x_bounds $img_x_bounds.low $img_x_bounds.upp
             -l '$log'
         ]]>
     </command>
@@ -20,14 +20,14 @@
         <!--
         <param name="input_file" type='data' format='yaml' optional='false' label="Input config"/>
         -->
-        <section name="x_bounds" title="Reduction x bounds">
-            <param name="x_bound_low" type="integer" value="-1" label="Lower bound"/>
-            <param name="x_bound_upp" type="integer" value="-1" label="Upper bound"/>
+        <section name="img_x_bounds" title="Reduction x bounds">
+            <param name="low" type="integer" value="-1" label="Lower bound"/>
+            <param name="upp" type="integer" value="-1" label="Upper bound"/>
         </section>
     </inputs>
     <outputs>
         <expand macro="common_outputs"/>
-        <collection name="tomo_reduce_plots" type="list" label="Tomo data reduction images">
+        <collection name="tomo_reduce_plots" type="list" label="Data reduction images">
             <discover_datasets pattern="__name_and_ext__" directory="tomo_reduce_plots"/>
         </collection>
         <data name="output_file" format="nxs" label="Reduced tomography data" from_work_dir="output.nxs"/>
Binary file workflow/.run_tomo.py.swp has changed