# HG changeset patch # User imgteam # Date 1601311783 0 # Node ID e2622ebb5ce42b0f22e387ce9b0281365f37c375 # Parent 29b39f5b0e6997ce805d8eaefdc9e98eb240854a "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 2afb24f3c81d625312186750a714d702363012b5" diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_adjust_threshold_binary.py --- a/imagej2_adjust_threshold_binary.py Tue Sep 17 17:01:44 2019 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,63 +0,0 @@ -#!/usr/bin/env python -import argparse -import os -import shutil -import subprocess -import tempfile -import imagej2_base_utils - -parser = argparse.ArgumentParser() -parser.add_argument( '--input', dest='input', help='Path to the input file' ) -parser.add_argument( '--input_datatype', dest='input_datatype', help='Datatype of the input image' ) -parser.add_argument( '--threshold_min', dest='threshold_min', type=float, help='Minimum threshold value' ) -parser.add_argument( '--threshold_max', dest='threshold_max', type=float, help='Maximum threshold value' ) -parser.add_argument( '--method', dest='method', help='Threshold method' ) -parser.add_argument( '--display', dest='display', help='Display mode' ) -parser.add_argument( '--black_background', dest='black_background', help='Black background' ) -parser.add_argument( '--stack_histogram', dest='stack_histogram', help='Stack histogram' ) -parser.add_argument( '--jython_script', dest='jython_script', help='Path to the Jython script' ) -parser.add_argument( '--output', dest='output', help='Path to the output file' ) -parser.add_argument( '--output_datatype', dest='output_datatype', help='Datatype of the output image' ) -args = parser.parse_args() - -tmp_dir = imagej2_base_utils.get_temp_dir() -# ImageJ expects valid image file extensions, so the Galaxy .dat extension does not -# work for some features. The following creates a symlink with an appropriate file -# extension that points to the Galaxy dataset. This symlink is used by ImageJ. -tmp_input_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.input, args.input_datatype ) -tmp_output_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, args.output_datatype ) -# Define command response buffers. -tmp_out = tempfile.NamedTemporaryFile().name -tmp_stdout = open( tmp_out, 'wb' ) -tmp_err = tempfile.NamedTemporaryFile().name -tmp_stderr = open( tmp_err, 'wb' ) -# Java writes a lot of stuff to stderr, so we'll specify a file for handling actual errors. -error_log = tempfile.NamedTemporaryFile( delete=False ).name -# Build the command line. -cmd = imagej2_base_utils.get_base_command_imagej2( None, jython_script=args.jython_script ) -if cmd is None: - imagej2_base_utils.stop_err( "ImageJ not found!" ) -cmd += ' %s' % error_log -cmd += ' %s' % tmp_input_path -cmd += ' %.3f' % args.threshold_min -cmd += ' %.3f' % args.threshold_max -cmd += ' %s' % args.method -cmd += ' %s' % args.display -cmd += ' %s' % args.black_background -cmd += ' %s' % args.stack_histogram -cmd += ' %s' % tmp_output_path -cmd += ' %s' % args.output_datatype -# Run the command. -proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True ) -rc = proc.wait() -# Handle execution errors. -if rc != 0: - error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout ) - imagej2_base_utils.stop_err( error_message ) -# Handle processing errors. -if os.path.getsize( error_log ) > 0: - error_message = open( error_log, 'r' ).read() - imagej2_base_utils.stop_err( error_message ) -# Save the output image. -shutil.move( tmp_output_path, args.output ) -imagej2_base_utils.cleanup_before_exit( tmp_dir ) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_adjust_threshold_binary_jython_script.py --- a/imagej2_adjust_threshold_binary_jython_script.py Tue Sep 17 17:01:44 2019 -0400 +++ b/imagej2_adjust_threshold_binary_jython_script.py Mon Sep 28 16:49:43 2020 +0000 @@ -1,49 +1,46 @@ -import jython_utils import sys + from ij import IJ # Fiji Jython interpreter implements Python 2.5 which does not # provide support for argparse. -error_log = sys.argv[ -10 ] -input = sys.argv[ -9 ] -threshold_min = float( sys.argv[ -8 ] ) -threshold_max = float( sys.argv[ -7 ] ) -method = sys.argv[ -6 ] -display = sys.argv[ -5 ] -black_background = jython_utils.asbool( sys.argv[ -4 ] ) +error_log = sys.argv[-10] +input_file = sys.argv[-9] +threshold_min = float(sys.argv[-8]) +threshold_max = float(sys.argv[-7]) +method = sys.argv[-6] +display = sys.argv[-5] +black_background = sys.argv[-4] == "yes" # TODO: this is not being used. -stack_histogram = jython_utils.asbool( sys.argv[ -3 ] ) -tmp_output_path = sys.argv[ -2 ] -output_datatype = sys.argv[ -1 ] +stack_histogram = sys.argv[-3] == "yes" +output_filename = sys.argv[-2] +output_datatype = sys.argv[-1] # Open the input image file. -input_image_plus = IJ.openImage( input ) +input_image_plus = IJ.openImage(input_file) # Create a copy of the image. input_image_plus_copy = input_image_plus.duplicate() image_processor_copy = input_image_plus_copy.getProcessor() -try: - # Convert image to binary if necessary. - if not image_processor_copy.isBinary(): - # Convert the image to binary grayscale. - IJ.run( input_image_plus_copy, "Make Binary","iterations=1 count=1 edm=Overwrite do=Nothing" ) - # Set the options. - if black_background: - method_str = "%s dark" % method - else: - method_str = method - IJ.setAutoThreshold( input_image_plus_copy, method_str ) - if display == "red": - display_mode = "Red" - elif display == "bw": - display_mode = "Black & White" - elif display == "over_under": - display_mode = "Over/Under" - IJ.setThreshold( input_image_plus_copy, threshold_min, threshold_max, display_mode ) - # Run the command. - IJ.run( input_image_plus_copy, "threshold", "" ) - # Save the ImagePlus object as a new image. - IJ.saveAs( input_image_plus_copy, output_datatype, tmp_output_path ) -except Exception, e: - jython_utils.handle_error( error_log, str( e ) ) +# Convert image to binary if necessary. +if not image_processor_copy.isBinary(): + # Convert the image to binary grayscale. + IJ.run(input_image_plus_copy, "Make Binary", "iterations=1 count=1 edm=Overwrite do=Nothing") +# Set the options. +if black_background: + method_str = "%s dark" % method +else: + method_str = method +IJ.setAutoThreshold(input_image_plus_copy, method_str) +if display == "red": + display_mode = "Red" +elif display == "bw": + display_mode = "Black & White" +elif display == "over_under": + display_mode = "Over/Under" +IJ.setThreshold(input_image_plus_copy, threshold_min, threshold_max, display_mode) +# Run the command. +IJ.run(input_image_plus_copy, "threshold", "") +# Save the ImagePlus object as a new image. +IJ.saveAs(input_image_plus_copy, output_datatype, output_filename) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_analyze_particles_binary.py --- a/imagej2_analyze_particles_binary.py Tue Sep 17 17:01:44 2019 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,81 +0,0 @@ -#!/usr/bin/env python -import argparse -import os -import shutil -import subprocess -import tempfile -import imagej2_base_utils - -parser = argparse.ArgumentParser() -parser.add_argument( '--input', dest='input', help='Path to the input file' ) -parser.add_argument( '--input_datatype', dest='input_datatype', help='Datatype of the input image' ) -parser.add_argument( '--black_background', dest='black_background', help='Black background' ) -parser.add_argument( '--size', dest='size', help='Size (pixel^2)' ) -parser.add_argument( '--circularity_min', dest='circularity_min', type=float, help='Circularity minimum' ) -parser.add_argument( '--circularity_max', dest='circularity_max', type=float, help='Circularity maximum' ) -parser.add_argument( '--show', dest='show', help='Show' ) -parser.add_argument( '--display_results', dest='display_results', help='Display results' ) -parser.add_argument( '--all_results', dest='all_results', help='All results' ) -parser.add_argument( '--exclude_edges', dest='exclude_edges', help='Exclude edges' ) -parser.add_argument( '--include_holes', dest='include_holes', help='Include holes' ) -parser.add_argument( '--jython_script', dest='jython_script', help='Path to the Jython script' ) -parser.add_argument( '--results', dest='results', default=None, help='Path to the output results file' ) -parser.add_argument( '--output', dest='output', default=None, help='Path to the output image file' ) -parser.add_argument( '--output_datatype', dest='output_datatype', default='data', help='Datatype of the output image' ) -args = parser.parse_args() - -tmp_dir = imagej2_base_utils.get_temp_dir() -# ImageJ expects valid image file extensions, so the Galaxy .dat extension does not -# work for some features. The following creates a symlink with an appropriate file -# extension that points to the Galaxy dataset. This symlink is used by ImageJ. -tmp_input_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.input, args.input_datatype ) -if args.output is None: - tmp_output_path = None -else: - tmp_output_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, args.output_datatype ) - -# Define command response buffers. -tmp_out = tempfile.NamedTemporaryFile().name -tmp_stdout = open( tmp_out, 'wb' ) -tmp_err = tempfile.NamedTemporaryFile().name -tmp_stderr = open( tmp_err, 'wb' ) -# Java writes a lot of stuff to stderr, so we'll specify a file for handling actual errors. -error_log = tempfile.NamedTemporaryFile( delete=False ).name - -# Build the command line. -cmd = imagej2_base_utils.get_base_command_imagej2( None, jython_script=args.jython_script ) -if cmd is None: - imagej2_base_utils.stop_err( "ImageJ not found!" ) -cmd += ' %s' % error_log -cmd += ' %s' % tmp_input_path -cmd += ' %s' % args.black_background -cmd += ' %s' % args.size -cmd += ' %.3f' % args.circularity_min -cmd += ' %.3f' % args.circularity_max -cmd += ' %s' % args.show -cmd += ' %s' % args.display_results -cmd += '%s' % imagej2_base_utils.handle_none_type( args.all_results, val_type='str' ) -cmd += ' %s' % args.exclude_edges -cmd += ' %s' % args.include_holes -cmd += '%s' % imagej2_base_utils.handle_none_type( tmp_output_path, val_type='str' ) -cmd += ' %s' % args.output_datatype -cmd += '%s' % imagej2_base_utils.handle_none_type( args.results, val_type='str' ) - -# Run the command. -proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True ) -rc = proc.wait() - -# Handle execution errors. -if rc != 0: - error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout ) - imagej2_base_utils.stop_err( error_message ) - -# Handle processing errors. -if os.path.getsize( error_log ) > 0: - error_message = open( error_log, 'r' ).read() - imagej2_base_utils.stop_err( error_message ) - -if tmp_output_path is not None: - # Save the output image. - shutil.move( tmp_output_path, args.output ) -imagej2_base_utils.cleanup_before_exit( tmp_dir ) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_analyze_particles_binary_jython_script.py --- a/imagej2_analyze_particles_binary_jython_script.py Tue Sep 17 17:01:44 2019 -0400 +++ b/imagej2_analyze_particles_binary_jython_script.py Mon Sep 28 16:49:43 2020 +0000 @@ -1,72 +1,74 @@ -import jython_utils import sys + from ij import IJ from ij.plugin.filter import Analyzer + +OPTIONS = ['edm=Overwrite', 'iterations=1', 'count=1'] + # Fiji Jython interpreter implements Python 2.5 which does not # provide support for argparse. -error_log = sys.argv[ -14 ] -input = sys.argv[ -13 ] -black_background = jython_utils.asbool( sys.argv[ -12 ] ) -size = sys.argv[ -11 ] -circularity_min = float( sys.argv[ -10 ] ) -circularity_max = float( sys.argv[ -9 ] ) -show = sys.argv[ -8 ] -display_results = jython_utils.asbool( sys.argv[ -7 ] ) -all_results = jython_utils.asbool( sys.argv[ -6 ] ) -exclude_edges = jython_utils.asbool( sys.argv[ -5 ] ) -include_holes = jython_utils.asbool( sys.argv[ -4 ] ) -tmp_output_path = sys.argv[ -3 ] -output_datatype = sys.argv[ -2 ] -results_path = sys.argv[ -1 ] +error_log = sys.argv[-14] +input_file = sys.argv[-13] +black_background = sys.argv[-12] == "yes" +size = sys.argv[-11] +circularity_min = float(sys.argv[-10]) +circularity_max = float(sys.argv[-9]) +show = sys.argv[-8] +display_results = sys.argv[-7] == "yes" +all_results = sys.argv[-6] == "yes" +exclude_edges = sys.argv[-5] == "yes" +include_holes = sys.argv[-4] == "yes" +output_filename = sys.argv[-3] +output_datatype = sys.argv[-2] +results_path = sys.argv[-1] # Open the input image file. -input_image_plus = IJ.openImage( input ) +input_image_plus = IJ.openImage(input_file) # Create a copy of the image. input_image_plus_copy = input_image_plus.duplicate() image_processor_copy = input_image_plus_copy.getProcessor() -analyzer = Analyzer( input_image_plus_copy ) +analyzer = Analyzer(input_image_plus_copy) -try: - # Set binary options. - options = jython_utils.get_binary_options( black_background=black_background ) - IJ.run( input_image_plus_copy, "Options...", options ) +# Set binary options. +options_list = OPTIONS +if black_background: + options_list.append("black") +options = " ".join(options_list) +IJ.run(input_image_plus_copy, "Options...", options) - # Convert image to binary if necessary. - if not image_processor_copy.isBinary(): - # Convert the image to binary grayscale. - IJ.run( input_image_plus_copy, "Make Binary", "" ) +if not image_processor_copy.isBinary(): + # Convert the image to binary grayscale. + IJ.run(input_image_plus_copy, "Make Binary", "") - # Set the options. - options = [ 'size=%s' % size ] - circularity_str = '%.3f-%.3f' % ( circularity_min, circularity_max ) - options.append( 'circularity=%s' % circularity_str ) - if show.find( '_' ) >= 0: - show_str = '[%s]' % show.replace( '_', ' ' ) - else: - show_str = show - options.append( 'show=%s' % show_str ) - if display_results: - options.append( 'display' ) - if not all_results: - options.append( 'summarize' ) - if exclude_edges: - options.append( 'exclude' ) - if include_holes: - options.append( 'include' ) - # Always run "in_situ". - options.append( 'in_situ' ) +# Set the options. +options = ['size=%s' % size] +circularity_str = '%.3f-%.3f' % (circularity_min, circularity_max) +options.append('circularity=%s' % circularity_str) +if show.find('_') >= 0: + show_str = '[%s]' % show.replace('_', ' ') +else: + show_str = show +options.append('show=%s' % show_str) +if display_results: + options.append('display') + if not all_results: + options.append('summarize') +if exclude_edges: + options.append('exclude') +if include_holes: + options.append('include') +# Always run "in_situ". +options.append('in_situ') - # Run the command. - IJ.run( input_image_plus_copy, "Analyze Particles...", " ".join( options ) ) +# Run the command. +IJ.run(input_image_plus_copy, "Analyze Particles...", " ".join(options)) - # Save outputs. - if tmp_output_path not in [ None, 'None' ]: - # Save the ImagePlus object as a new image. - IJ.saveAs( input_image_plus_copy, output_datatype, tmp_output_path ) - if display_results and results_path not in [ None, 'None' ]: - results_table = analyzer.getResultsTable() - results_table.saveAs( results_path ) -except Exception, e: - jython_utils.handle_error( error_log, str( e ) ) +# Save outputs. +if len(output_filename) > 0: + # Save the ImagePlus object as a new image. + IJ.saveAs(input_image_plus_copy, output_datatype, output_filename) +if display_results and len(results_path) > 0: + results_table = analyzer.getResultsTable() + results_table.saveAs(results_path) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_analyze_skeleton.py --- a/imagej2_analyze_skeleton.py Tue Sep 17 17:01:44 2019 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,61 +0,0 @@ -#!/usr/bin/env python -import argparse -import os -import shutil -import subprocess -import tempfile -import imagej2_base_utils - -parser = argparse.ArgumentParser() -parser.add_argument( '--input', dest='input', help='Path to the input file' ) -parser.add_argument( '--input_datatype', dest='input_datatype', help='Datatype of the input image' ) -parser.add_argument( '--black_background', dest='black_background', help='Black background' ) -parser.add_argument( '--prune_cycle_method', dest='prune_cycle_method', default='none', help='Prune cycle method' ) -parser.add_argument( '--prune_ends', dest='prune_ends', default='no', help='Prune ends' ) -parser.add_argument( '--calculate_largest_shortest_path', dest='calculate_largest_shortest_path', default='no', help='Calculate largest shortest path' ) -parser.add_argument( '--show_detailed_info', dest='show_detailed_info', default='no', help='Show detailed info' ) -parser.add_argument( '--jython_script', dest='jython_script', help='Path to the Jython script' ) -parser.add_argument( '--output', dest='output', help='Path to the output file' ) -args = parser.parse_args() - -tmp_dir = imagej2_base_utils.get_temp_dir() -# ImageJ expects valid image file extensions, so the Galaxy .dat extension does not -# work for some features. The following creates a symlink with an appropriate file -# extension that points to the Galaxy dataset. This symlink is used by ImageJ. -tmp_input_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.input, args.input_datatype ) - -# Define command response buffers. -tmp_out = tempfile.NamedTemporaryFile().name -tmp_stdout = open( tmp_out, 'wb' ) -tmp_err = tempfile.NamedTemporaryFile().name -tmp_stderr = open( tmp_err, 'wb' ) -# Java writes a lot of stuff to stderr, so we'll specify a file for handling actual errors. -error_log = tempfile.NamedTemporaryFile( delete=False ).name - -# Build the command line. -cmd = imagej2_base_utils.get_base_command_imagej2( None, jython_script=args.jython_script ) -if cmd is None: - imagej2_base_utils.stop_err( "ImageJ not found!" ) -cmd += ' %s' % error_log -cmd += ' %s' % tmp_input_path -cmd += ' %s' % args.black_background -cmd += ' %s' % args.prune_cycle_method -cmd += ' %s' % args.prune_ends -cmd += ' %s' % args.calculate_largest_shortest_path -cmd += ' %s' % args.show_detailed_info -cmd += ' %s' % args.output - -# Run the command. -proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True ) -rc = proc.wait() - -# Handle execution errors. -if rc != 0: - error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout ) - imagej2_base_utils.stop_err( error_message ) -# Handle processing errors. -if os.path.getsize( error_log ) > 0: - error_message = open( error_log, 'r' ).read() - imagej2_base_utils.stop_err( error_message ) - -imagej2_base_utils.cleanup_before_exit( tmp_dir ) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_analyze_skeleton_jython_script.py --- a/imagej2_analyze_skeleton_jython_script.py Tue Sep 17 17:01:44 2019 -0400 +++ b/imagej2_analyze_skeleton_jython_script.py Mon Sep 28 16:49:43 2020 +0000 @@ -1,147 +1,148 @@ -import jython_utils import math import sys + from ij import IJ from sc.fiji.analyzeSkeleton import AnalyzeSkeleton_ -BASIC_NAMES = [ 'Branches', 'Junctions', 'End-point Voxels', - 'Junction Voxels', 'Slab Voxels', 'Average branch length', - 'Triple Points', 'Quadruple Points', 'Maximum Branch Length' ] -DETAIL_NAMES = [ 'Skeleton ID', 'Branch length', 'V1 x', 'V1 y', 'V1 z', 'V2 x', - 'V2 y', 'V2 z', 'Euclidean distance' ] +BASIC_NAMES = ['Branches', 'Junctions', 'End-point Voxels', + 'Junction Voxels', 'Slab Voxels', 'Average branch length', + 'Triple Points', 'Quadruple Points', 'Maximum Branch Length'] +DETAIL_NAMES = ['Skeleton ID', 'Branch length', 'V1 x', 'V1 y', 'V1 z', 'V2 x', + 'V2 y', 'V2 z', 'Euclidean distance'] +OPTIONS = ['edm=Overwrite', 'iterations=1', 'count=1'] + -def get_euclidean_distance( vertex1, vertex2 ): - x1, y1, z1 = get_points( vertex1 ) - x2, y2, z2 = get_points( vertex2 ) - return math.sqrt( math.pow( ( x2 - x1 ), 2 ) + - math.pow( ( y2 - y1 ), 2 ) + - math.pow( ( z2 - z1 ), 2 ) ) +def get_euclidean_distance(vertex1, vertex2): + x1, y1, z1 = get_points(vertex1) + x2, y2, z2 = get_points(vertex2) + return math.sqrt(math.pow((x2 - x1), 2) + math.pow((y2 - y1), 2) + math.pow((z2 - z1), 2)) -def get_graph_length( graph ): + +def get_graph_length(graph): length = 0 for edge in graph.getEdges(): length = length + edge.getLength() return length -def get_points( vertex ): + +def get_points(vertex): # An array of Point, which has attributes x,y,z. - point = vertex.getPoints()[ 0 ] + point = vertex.getPoints()[0] return point.x, point.y, point.z - -def get_sorted_edge_lengths( graph ): + + +def get_sorted_edge_lengths(graph): # Return graph edges sorted from longest to shortest. edges = graph.getEdges() - edges = sorted( edges, key=lambda edge: edge.getLength(), reverse=True ) + edges = sorted(edges, key=lambda edge: edge.getLength(), reverse=True) return edges -def get_sorted_graph_lengths( result ): + +def get_sorted_graph_lengths(result): # Get the separate graphs (skeletons). graphs = result.getGraph() # Sort graphs from longest to shortest. - graphs = sorted( graphs, key=lambda g: get_graph_length( g ), reverse=True ) + graphs = sorted(graphs, key=lambda g: get_graph_length(g), reverse=True) return graphs -def save( result, output, show_detailed_info, calculate_largest_shortest_path, sep='\t' ): - num_trees = int( result.getNumOfTrees() ) - outf = open( output, 'wb' ) - outf.write( '# %s\n' % sep.join( BASIC_NAMES ) ) - for index in range( num_trees ): - outf.write( '%d%s' % ( result.getBranches()[ index ], sep ) ) - outf.write( '%d%s' % ( result.getJunctions()[ index ], sep ) ) - outf.write( '%d%s' % ( result.getEndPoints()[ index ], sep ) ) - outf.write( '%d%s' % ( result.getJunctionVoxels()[ index ], sep ) ) - outf.write( '%d%s' % ( result.getSlabs()[ index ], sep ) ) - outf.write( '%.3f%s' % ( result.getAverageBranchLength()[ index ], sep ) ) - outf.write( '%d%s' % ( result.getTriples()[ index ], sep ) ) - outf.write( '%d%s' % ( result.getQuadruples()[ index ], sep ) ) - outf.write( '%.3f' % result.getMaximumBranchLength()[ index ] ) + +def save(result, output, show_detailed_info, calculate_largest_shortest_path, sep='\t'): + num_trees = int(result.getNumOfTrees()) + outf = open(output, 'wb') + outf.write('# %s\n' % sep.join(BASIC_NAMES)) + for index in range(num_trees): + outf.write('%d%s' % (result.getBranches()[index], sep)) + outf.write('%d%s' % (result.getJunctions()[index], sep)) + outf.write('%d%s' % (result.getEndPoints()[index], sep)) + outf.write('%d%s' % (result.getJunctionVoxels()[index], sep)) + outf.write('%d%s' % (result.getSlabs()[index], sep)) + outf.write('%.3f%s' % (result.getAverageBranchLength()[index], sep)) + outf.write('%d%s' % (result.getTriples()[index], sep)) + outf.write('%d%s' % (result.getQuadruples()[index], sep)) + outf.write('%.3f' % result.getMaximumBranchLength()[index]) if calculate_largest_shortest_path: - outf.write( '%s%.3f%s' % ( sep, result.shortestPathList.get( index ), sep ) ) - outf.write( '%d%s' % ( result.spStartPosition[ index ][ 0 ], sep ) ) - outf.write( '%d%s' % ( result.spStartPosition[ index ][ 1 ], sep ) ) - outf.write( '%d\n' % result.spStartPosition[ index ][ 2 ] ) + outf.write('%s%.3f%s' % (sep, result.shortestPathList.get(index), sep)) + outf.write('%d%s' % (result.spStartPosition[index][0], sep)) + outf.write('%d%s' % (result.spStartPosition[index][1], sep)) + outf.write('%d\n' % result.spStartPosition[index][2]) else: - outf.write( '\n' ) + outf.write('\n') if show_detailed_info: - outf.write( '# %s\n' % sep.join( DETAIL_NAMES ) ) + outf.write('# %s\n' % sep.join(DETAIL_NAMES)) # The following index is a placeholder for the skeleton ID. # The terms "graph" and "skeleton" refer to the same thing. # Also, the SkeletonResult.java code states that the # private Graph[] graph object is an array of graphs (one # per tree). - for index, graph in enumerate( get_sorted_graph_lengths( result ) ): - for edge in get_sorted_edge_lengths( graph ): + for index, graph in enumerate(get_sorted_graph_lengths(result)): + for edge in get_sorted_edge_lengths(graph): vertex1 = edge.getV1() - x1, y1, z1 = get_points( vertex1 ) + x1, y1, z1 = get_points(vertex1) vertex2 = edge.getV2() - x2, y2, z2 = get_points( vertex2 ) - outf.write( '%d%s' % ( index+1, sep ) ) - outf.write( '%.3f%s' % ( edge.getLength(), sep ) ) - outf.write( '%d%s' % ( x1, sep ) ) - outf.write( '%d%s' % ( y1, sep ) ) - outf.write( '%d%s' % ( z1, sep ) ) - outf.write( '%d%s' % ( x2, sep ) ) - outf.write( '%d%s' % ( y2, sep ) ) - outf.write( '%d%s' % ( z2, sep ) ) - outf.write( '%.3f' % get_euclidean_distance( vertex1, vertex2 ) ) + x2, y2, z2 = get_points(vertex2) + outf.write('%d%s' % (index + 1, sep)) + outf.write('%.3f%s' % (edge.getLength(), sep)) + outf.write('%d%s' % (x1, sep)) + outf.write('%d%s' % (y1, sep)) + outf.write('%d%s' % (z1, sep)) + outf.write('%d%s' % (x2, sep)) + outf.write('%d%s' % (y2, sep)) + outf.write('%d%s' % (z2, sep)) + outf.write('%.3f' % get_euclidean_distance(vertex1, vertex2)) if calculate_largest_shortest_path: # Keep number of separated items the same for each line. - outf.write( '%s %s' % ( sep, sep ) ) - outf.write( ' %s' % sep ) - outf.write( ' %s' % sep ) - outf.write( ' \n' ) + outf.write('%s %s' % (sep, sep)) + outf.write(' %s' % sep) + outf.write(' %s' % sep) + outf.write(' \n') else: - outf.write( '\n' ) + outf.write('\n') outf.close() + # Fiji Jython interpreter implements Python 2.5 which does not # provide support for argparse. -error_log = sys.argv[ -8 ] -input = sys.argv[ -7 ] -black_background = jython_utils.asbool( sys.argv[ -6 ] ) -prune_cycle_method = sys.argv[ -5 ] -prune_ends = jython_utils.asbool( sys.argv[ -4 ] ) -calculate_largest_shortest_path = jython_utils.asbool( sys.argv[ -3 ] ) +error_log = sys.argv[-8] +input = sys.argv[-7] +black_background = sys.argv[-6] == "yes" +prune_cycle_method = sys.argv[-5] +prune_ends = sys.argv[-4] == "yes" +calculate_largest_shortest_path = sys.argv[-3] == "yes" if calculate_largest_shortest_path: - BASIC_NAMES.extend( [ 'Longest Shortest Path', 'spx', 'spy', 'spz' ] ) - DETAIL_NAMES.extend( [ ' ', ' ', ' ', ' ' ] ) -show_detailed_info = jython_utils.asbool( sys.argv[ -2 ] ) -output = sys.argv[ -1 ] + BASIC_NAMES.extend(['Longest Shortest Path', 'spx', 'spy', 'spz']) + DETAIL_NAMES.extend([' ', ' ', ' ', ' ']) +show_detailed_info = sys.argv[-2] == "yes" +output = sys.argv[-1] # Open the input image file. -input_image_plus = IJ.openImage( input ) +input_image_plus = IJ.openImage(input) # Create a copy of the image. input_image_plus_copy = input_image_plus.duplicate() image_processor_copy = input_image_plus_copy.getProcessor() -try: - # Set binary options. - options = jython_utils.get_binary_options( black_background=black_background ) - IJ.run( input_image_plus_copy, "Options...", options ) +# Set binary options. +options_list = OPTIONS +if black_background: + options_list.append("black") +options = " ".join(options_list) +IJ.run(input_image_plus_copy, "Options...", options) - # Convert image to binary if necessary. - if not image_processor_copy.isBinary(): - IJ.run( input_image_plus_copy, "Make Binary", "" ) +# Convert image to binary if necessary. +if not image_processor_copy.isBinary(): + IJ.run(input_image_plus_copy, "Make Binary", "") - # Run AnalyzeSkeleton - analyze_skeleton = AnalyzeSkeleton_() - analyze_skeleton.setup( "", input_image_plus_copy ) - if prune_cycle_method == 'none': - prune_index = analyze_skeleton.NONE - elif prune_cycle_method == 'shortest_branch': - prune_index = analyze_skeleton.SHORTEST_BRANCH - elif prune_cycle_method == 'lowest_intensity_voxel': - prune_index = analyze_skeleton.LOWEST_INTENSITY_VOXEL - elif prune_cycle_method == 'lowest_intensity_branch': - prune_index = analyze_skeleton.LOWEST_INTENSITY_BRANCH - result = analyze_skeleton.run( prune_index, - prune_ends, - calculate_largest_shortest_path, - input_image_plus_copy, - True, - True ) - # Save the results. - save( result, output, show_detailed_info, calculate_largest_shortest_path ) -except Exception, e: - jython_utils.handle_error( error_log, str( e ) ) +# Run AnalyzeSkeleton +analyze_skeleton = AnalyzeSkeleton_() +analyze_skeleton.setup("", input_image_plus_copy) +if prune_cycle_method == 'none': + prune_index = analyze_skeleton.NONE +elif prune_cycle_method == 'shortest_branch': + prune_index = analyze_skeleton.SHORTEST_BRANCH +elif prune_cycle_method == 'lowest_intensity_voxel': + prune_index = analyze_skeleton.LOWEST_INTENSITY_VOXEL +elif prune_cycle_method == 'lowest_intensity_branch': + prune_index = analyze_skeleton.LOWEST_INTENSITY_BRANCH +result = analyze_skeleton.run(prune_index, prune_ends, calculate_largest_shortest_path, input_image_plus_copy, True, True) +# Save the results. +save(result, output, show_detailed_info, calculate_largest_shortest_path) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_base_utils.py --- a/imagej2_base_utils.py Tue Sep 17 17:01:44 2019 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,169 +0,0 @@ -import os -import shutil -import sys -import tempfile - -BUFF_SIZE = 1048576 - - -def cleanup_before_exit(tmp_dir): - """ - Remove temporary files and directories prior to tool exit. - """ - if tmp_dir and os.path.exists(tmp_dir): - shutil.rmtree(tmp_dir) - - -def get_base_cmd_bunwarpj(jvm_memory): - if jvm_memory in [None, 'None']: - jvm_memory_str = '' - else: - jvm_memory_str = '-Xmx%s' % jvm_memory - # The following bunwarpj_base_cmd string will look something like this: - # "java %s -cp $JAR_DIR/ij-1.49k.jar:$PLUGINS_DIR/bUnwarpJ_-2.6.1.jar \ - # bunwarpj.bUnwarpJ_" % (jvm_memory_str) - # See the bunwarpj.sh script for the fiji 20151222 - # bioconda recipe in github. - bunwarpj_base_cmd = "bunwarpj %s" % jvm_memory_str - return bunwarpj_base_cmd - - -def get_base_command_imagej2(memory_size=None, macro=None, jython_script=None): - imagej2_executable = get_imagej2_executable() - if imagej2_executable is None: - return None - cmd = '%s --ij2 --headless --debug' % imagej2_executable - if memory_size is not None: - memory_size_cmd = ' -DXms=%s -DXmx=%s' % (memory_size, memory_size) - cmd += memory_size_cmd - if macro is not None: - cmd += ' --macro %s' % os.path.abspath(macro) - if jython_script is not None: - cmd += ' --jython %s' % os.path.abspath(jython_script) - return cmd - - -def get_file_extension(image_format): - """ - Return a valid bioformats file extension based on the received - value of image_format(e.g., "gif" is returned as ".gif". - """ - return '.%s' % image_format - - -def get_file_name_without_extension(file_path): - """ - Eliminate the .ext from the received file name, assuming that - the file name consists of only a single '.'. - """ - if os.path.exists(file_path): - path, name = os.path.split(file_path) - name_items = name.split('.') - return name_items[0] - return None - - -def get_imagej2_executable(): - """ - Fiji names the ImageJ executable different names for different - architectures, but our bioconda recipe allows us to do this. - """ - return 'ImageJ' - - -def get_input_image_path(tmp_dir, input_file, image_format): - """ - Bioformats uses file extensions (e.g., .job, .gif, etc) - when reading and writing image files, so the Galaxy dataset - naming convention of setting all file extensions as .dat - must be handled. - """ - image_path = get_temporary_image_path(tmp_dir, image_format) - # Remove the file so we can create a symlink. - os.remove(image_path) - os.symlink(input_file, image_path) - return image_path - - -def get_platform_info_dict(): - '''Return a dict with information about the current platform.''' - platform_dict = {} - sysname, nodename, release, version, machine = os.uname() - platform_dict['os'] = sysname.lower() - platform_dict['architecture'] = machine.lower() - return platform_dict - - -def get_stderr_exception(tmp_err, tmp_stderr, tmp_out, tmp_stdout, include_stdout=False): - tmp_stderr.close() - """ - Return a stderr string of reasonable size. - """ - # Get stderr, allowing for case where it's very large. - tmp_stderr = open(tmp_err, 'rb') - stderr_str = '' - buffsize = BUFF_SIZE - try: - while True: - stderr_str += tmp_stderr.read(buffsize) - if not stderr_str or len(stderr_str) % buffsize != 0: - break - except OverflowError: - pass - tmp_stderr.close() - if include_stdout: - tmp_stdout = open(tmp_out, 'rb') - stdout_str = '' - buffsize = BUFF_SIZE - try: - while True: - stdout_str += tmp_stdout.read(buffsize) - if not stdout_str or len(stdout_str) % buffsize != 0: - break - except OverflowError: - pass - tmp_stdout.close() - if include_stdout: - return 'STDOUT\n%s\n\nSTDERR\n%s\n' % (stdout_str, stderr_str) - return stderr_str - - -def get_temp_dir(prefix='tmp-imagej-', dir=None): - """ - Return a temporary directory. - """ - return tempfile.mkdtemp(prefix=prefix, dir=dir) - - -def get_tempfilename(dir=None, suffix=None): - """ - Return a temporary file name. - """ - fd, name = tempfile.mkstemp(suffix=suffix, dir=dir) - os.close(fd) - return name - - -def get_temporary_image_path(tmp_dir, image_format): - """ - Return the path to a temporary file with a valid image format - file extension that can be used with bioformats. - """ - file_extension = get_file_extension(image_format) - return get_tempfilename(tmp_dir, file_extension) - - -def handle_none_type(val, val_type='float'): - if val is None: - return ' None' - else: - if val_type == 'float': - return ' %.3f' % val - elif val_type == 'int': - return ' %d' % val - return ' %s' % val - - -def stop_err(msg): - sys.stderr.write(msg) - sys.exit(1) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_binary_to_edm.py --- a/imagej2_binary_to_edm.py Tue Sep 17 17:01:44 2019 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,65 +0,0 @@ -#!/usr/bin/env python -import argparse -import os -import shutil -import subprocess -import tempfile -import imagej2_base_utils - -parser = argparse.ArgumentParser() -parser.add_argument( '--input', dest='input', help='Path to the input file' ) -parser.add_argument( '--input_datatype', dest='input_datatype', help='Datatype of the input image' ) -parser.add_argument( '--iterations', dest='iterations', type=int, help='Iterations' ) -parser.add_argument( '--count', dest='count', type=int, help='Count' ) -parser.add_argument( '--black_background', dest='black_background', help='Black background' ) -parser.add_argument( '--pad_edges_when_eroding', dest='pad_edges_when_eroding', help='Pad edges when eroding' ) -parser.add_argument( '--jython_script', dest='jython_script', help='Path to the Jython script' ) -parser.add_argument( '--output', dest='output', help='Path to the output file' ) -parser.add_argument( '--output_datatype', dest='output_datatype', help='Datatype of the output image' ) -args = parser.parse_args() - -tmp_dir = imagej2_base_utils.get_temp_dir() -# ImageJ expects valid image file extensions, so the Galaxy .dat extension does not -# work for some features. The following creates a symlink with an appropriate file -# extension that points to the Galaxy dataset. This symlink is used by ImageJ. -tmp_input_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.input, args.input_datatype ) -tmp_output_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, args.output_datatype ) - -# Define command response buffers. -tmp_out = tempfile.NamedTemporaryFile().name -tmp_stdout = open( tmp_out, 'wb' ) -tmp_err = tempfile.NamedTemporaryFile().name -tmp_stderr = open( tmp_err, 'wb' ) -# Java writes a lot of stuff to stderr, so we'll specify a file for handling actual errors. -error_log = tempfile.NamedTemporaryFile( delete=False ).name - -# Build the command line. -cmd = imagej2_base_utils.get_base_command_imagej2( None, jython_script=args.jython_script ) -if cmd is None: - imagej2_base_utils.stop_err( "ImageJ not found!" ) -cmd += ' %s' % error_log -cmd += ' %s' % tmp_input_path -cmd += ' %d' % args.iterations -cmd += ' %d' % args.count -cmd += ' %s' % args.black_background -cmd += ' %s' % args.pad_edges_when_eroding -cmd += ' %s' % tmp_output_path -cmd += ' %s' % args.output_datatype - -# Run the command. -proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True ) -rc = proc.wait() - -# Handle execution errors. -if rc != 0: - error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout ) - imagej2_base_utils.stop_err( error_message ) - -# Handle processing errors. -if os.path.getsize( error_log ) > 0: - error_message = open( error_log, 'r' ).read() - imagej2_base_utils.stop_err( error_message ) - -# Save the output image. -shutil.move( tmp_output_path, args.output ) -imagej2_base_utils.cleanup_before_exit( tmp_dir ) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_binary_to_edm_jython_script.py --- a/imagej2_binary_to_edm_jython_script.py Tue Sep 17 17:01:44 2019 -0400 +++ b/imagej2_binary_to_edm_jython_script.py Mon Sep 28 16:49:43 2020 +0000 @@ -1,44 +1,40 @@ -import jython_utils import sys + from ij import IJ -from ij import ImagePlus -from ij.plugin.filter import Analyzer -from ij.plugin.filter import EDM # Fiji Jython interpreter implements Python 2.5 which does not # provide support for argparse. -error_log = sys.argv[ -8 ] -input = sys.argv[ -7 ] -iterations = int( sys.argv[ -6 ] ) -count = int( sys.argv[ -5 ] ) -black_background = jython_utils.asbool( sys.argv[ -4 ] ) -pad_edges_when_eroding = jython_utils.asbool( sys.argv[ -3 ] ) -tmp_output_path = sys.argv[ -2 ] -output_datatype = sys.argv[ -1 ] +error_log = sys.argv[-8] +input_file = sys.argv[-7] +iterations = int(sys.argv[-6]) +count = int(sys.argv[-5]) +black_background = sys.argv[-4] == "yes" +pad_edges_when_eroding = sys.argv[-3] == "yes" +output_filename = sys.argv[-2] +output_datatype = sys.argv[-1] # Open the input image file. -input_image_plus = IJ.openImage( input ) +input_image_plus = IJ.openImage(input_file) # Create a copy of the image. input_image_plus_copy = input_image_plus.duplicate() image_processor_copy = input_image_plus_copy.getProcessor() -try: - # Set binary options. - options = jython_utils.get_binary_options( black_background=black_background, - iterations=iterations, - count=count, - pad_edges_when_eroding=pad_edges_when_eroding ) - IJ.run( input_image_plus_copy, "Options...", options ) +# Set binary options. +options_list = ['edm=Overwrite', 'iterations=%d' % iterations, 'count=%d' % count] +if black_background: + options_list.append("black") +if pad_edges_when_eroding: + options_list.append("pad") +options = " ".join(options_list) +IJ.run(input_image_plus_copy, "Options...", options) - # Convert image to binary if necessary. - if not image_processor_copy.isBinary(): - # Convert the image to binary grayscale. - IJ.run( input_image_plus_copy, "Make Binary", "" ) +# Convert image to binary if necessary. +if not image_processor_copy.isBinary(): + # Convert the image to binary grayscale. + IJ.run(input_image_plus_copy, "Make Binary", "") - # Run the command. - IJ.run( input_image_plus_copy, "Distance Map", "" ) - # Save the ImagePlus object as a new image. - IJ.saveAs( input_image_plus_copy, output_datatype, tmp_output_path ) -except Exception, e: - jython_utils.handle_error( error_log, str( e ) ) +# Run the command. +IJ.run(input_image_plus_copy, "Distance Map", "") +# Save the ImagePlus object as a new image. +IJ.saveAs(input_image_plus_copy, output_datatype, output_filename) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_bunwarpj_adapt_transform.py --- a/imagej2_bunwarpj_adapt_transform.py Tue Sep 17 17:01:44 2019 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,65 +0,0 @@ -#!/usr/bin/env python -import argparse -import subprocess -import tempfile -import imagej2_base_utils - -# Parse Command Line. -parser = argparse.ArgumentParser() -parser.add_argument( '--source_image', dest='source_image', help='Source image' ) -parser.add_argument( '--source_image_format', dest='source_image_format', help='Source image format' ) -parser.add_argument( '--target_image', dest='target_image', help='Target image' ) -parser.add_argument( '--target_image_format', dest='target_image_format', help='Target image format' ) -parser.add_argument( '--input_elastic_transformation', dest='input_elastic_transformation', help='Input elastic transformation matrix' ) -parser.add_argument( '--image_size_factor', dest='image_size_factor', type=float, help='Image size factor' ) -parser.add_argument( '--output', dest='output', help='Warping index' ) - -args = parser.parse_args() - -tmp_dir = imagej2_base_utils.get_temp_dir() -source_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_image, args.source_image_format ) -target_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_image, args.target_image_format ) -input_elastic_transformation_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.input_elastic_transformation, 'txt' ) - -# Define command response buffers. -tmp_out = tempfile.NamedTemporaryFile().name -tmp_stdout = open( tmp_out, 'wb' ) -tmp_err = tempfile.NamedTemporaryFile().name -tmp_stderr = open( tmp_err, 'wb' ) - -def is_power2( val ): - if val < 0: - return False - if val < 1: - val = 1.0 / val - val = int( val ) - return ( ( val & ( val - 1 ) ) == 0 ) - -# Build the command line to adapt the transformation. -cmd = imagej2_base_utils.get_base_cmd_bunwarpj( None ) -if cmd is None: - imagej2_base_utils.stop_err( "bUnwarpJ not found!" ) -cmd += ' -adapt_transform' - -# Make sure the value of image_size_factor is a power of 2 (positive or negative). -if is_power2( args.image_size_factor ): - image_size_factor = args.image_size_factor -else: - msg = "Image size factor must be a positive or negative power of 2 (0.25, 0.5, 2, 4, 8, etc)." - imagej2_base_utils.stop_err( msg ) - -# Target is sent before source. -cmd += ' %s' % target_image_path -cmd += ' %s' % source_image_path -cmd += ' %s' % input_elastic_transformation_path -cmd += ' %s' % args.output -cmd += ' %2.f' % image_size_factor - -# Adapt the transformation based on the image size factor using bUnwarpJ. -proc = subprocess.Popen( args=cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True ) -rc = proc.wait() -if rc != 0: - error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout ) - imagej2_base_utils.stop_err( error_message ) - -imagej2_base_utils.cleanup_before_exit( tmp_dir ) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_bunwarpj_align.py --- a/imagej2_bunwarpj_align.py Tue Sep 17 17:01:44 2019 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,178 +0,0 @@ -#!/usr/bin/env python -import argparse -import os -import shutil -import subprocess -import tempfile -import imagej2_base_utils - -# Parse Command Line. -parser = argparse.ArgumentParser() -parser.add_argument( '--source_image', dest='source_image', help='Source image' ) -parser.add_argument( '--source_image_format', dest='source_image_format', help='Source image format' ) -parser.add_argument( '--source_mask', dest='source_mask', default=None, help='Source mask' ) -parser.add_argument( '--source_mask_format', dest='source_mask_format', default=None, help='Source mask image format' ) -parser.add_argument( '--target_image', dest='target_image', help='Target image' ) -parser.add_argument( '--target_image_format', dest='target_image_format', help='Target image format' ) -parser.add_argument( '--target_mask', dest='target_mask', default=None, help='Target mask' ) -parser.add_argument( '--target_mask_format', dest='target_mask_format', default=None, help='Target mask image format' ) -parser.add_argument( '--min_scale_def', dest='min_scale_def', type=int, help='Initial deformation' ) -parser.add_argument( '--max_scale_def', dest='max_scale_def', type=int, help='Final deformation' ) -parser.add_argument( '--max_subsamp_fact', dest='max_subsamp_fact', type=int, help='Image sub-sample factor' ) -parser.add_argument( '--divergence_weight', dest='divergence_weight', type=float, help='Divergence weight' ) -parser.add_argument( '--curl_weight', dest='curl_weight', type=float, help='Curl weight' ) -parser.add_argument( '--image_weight', dest='image_weight', type=float, help='Image weight' ) -parser.add_argument( '--consistency_weight', dest='consistency_weight', type=float, help='Consistency weight' ) -parser.add_argument( '--landmarks_weight', dest='landmarks_weight', type=float, help='Landmarks weight' ) -parser.add_argument( '--landmarks_file', dest='landmarks_file', default=None, help='Landmarks file' ) -parser.add_argument( '--source_affine_file', dest='source_affine_file', default=None, help='Initial source affine matrix transformation' ) -parser.add_argument( '--target_affine_file', dest='target_affine_file', default=None, help='Initial target affine matrix transformation' ) -parser.add_argument( '--mono', dest='mono', default=False, help='Unidirectional registration (source to target)' ) -parser.add_argument( '--source_trans_out', dest='source_trans_out', default=None, help='Direct source transformation matrix' ) -parser.add_argument( '--target_trans_out', dest='target_trans_out', default=None, help='Inverse target transformation matrix' ) -parser.add_argument( '--source_out', help='Output source image' ) -parser.add_argument( '--source_out_datatype', help='Output registered source image format' ) -parser.add_argument( '--target_out', default=None, help='Output target image' ) -parser.add_argument( '--target_out_datatype', default=None, help='Output registered target image format' ) -parser.add_argument( '--jython_script', dest='jython_script', help='Path to the Jython script' ) - -args = parser.parse_args() - -if args.source_trans_out is not None and args.target_trans_out is not None: - save_transformation = True -else: - save_transformation = False - -tmp_dir = imagej2_base_utils.get_temp_dir() -source_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_image, args.source_image_format ) -tmp_source_out_tiff_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, 'tiff' ) -tmp_source_out_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, args.source_out_datatype ) -target_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_image, args.target_image_format ) -if not args.mono: - tmp_target_out_tiff_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, 'tiff' ) - tmp_target_out_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, args.target_out_datatype ) -if args.source_mask is not None and args.target_mask is not None: - tmp_source_mask_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_mask, args.source_mask_format ) - tmp_target_mask_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_mask, args.target_mask_format ) -if save_transformation: - # bUnwarpJ automatically names the transformation files based on the names - # of the source and target image file names. We've defined symlinks to - # temporary files with valid image extensions since ImageJ does not handle - # the Galaxy "dataset.dat" file extensions. - source_file_name = imagej2_base_utils.get_file_name_without_extension( tmp_source_out_tiff_path ) - tmp_source_out_transf_path = os.path.join( tmp_dir, '%s_transf.txt' % source_file_name ) - target_file_name = imagej2_base_utils.get_file_name_without_extension( tmp_target_out_tiff_path ) - tmp_target_out_transf_path = os.path.join( tmp_dir, '%s_transf.txt' % target_file_name ) - -# Define command response buffers. -tmp_out = tempfile.NamedTemporaryFile().name -tmp_stdout = open( tmp_out, 'wb' ) -tmp_err = tempfile.NamedTemporaryFile().name -tmp_stderr = open( tmp_err, 'wb' ) - -# Build the command line to align the two images. -cmd = imagej2_base_utils.get_base_cmd_bunwarpj( None ) -if cmd is None: - imagej2_base_utils.stop_err( "bUnwarpJ not found!" ) -cmd += ' -align' -# Target is sent before source. -cmd += ' %s' % target_image_path -if args.target_mask is None: - target_mask_str = ' NULL' -else: - target_mask_str = ' %s' % tmp_target_mask_path -cmd += target_mask_str -cmd += ' %s' % source_image_path -if args.source_mask is None: - source_mask_str = ' NULL' -else: - source_mask_str = ' %s' % tmp_source_mask_path -cmd += source_mask_str -cmd += ' %d' % args.min_scale_def -cmd += ' %d' % args.max_scale_def -cmd += ' %d' % args.max_subsamp_fact -cmd += ' %.1f' % args.divergence_weight -cmd += ' %.1f' % args.curl_weight -cmd += ' %.1f' % args.image_weight -cmd += ' %.1f' % args.consistency_weight -# Source is produced before target. -cmd += ' %s' % tmp_source_out_tiff_path -if not args.mono: - cmd += ' %s' % tmp_target_out_tiff_path -if args.landmarks_file is not None: - # We have to create a temporary file with a .txt extension here so that - # bUnwarpJ will not ignore the Galaxy "dataset.dat" file. - tmp_landmarks_file_path = imagej2_base_utils.get_input_image_path( tmp_dir, - args.landmarks_file, - 'txt' ) - cmd += ' -landmarks' - cmd += ' %.1f' % args.landmarks_weight - cmd += ' %s' % tmp_landmarks_file_path -if args.source_affine_file is not None and args.target_affine_file is not None: - # Target is sent before source. - cmd += ' -affine' - cmd += ' %s' % args.target_affine_file - cmd += ' %s' % args.source_affine_file -if args.mono: - cmd += ' -mono' -if save_transformation: - cmd += ' -save_transformation' - -# Align the two images using bUnwarpJ. -proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True ) -rc = proc.wait() -if rc != 0: - error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout ) - imagej2_base_utils.stop_err( error_message ) - -# bUnwarpJ produces tiff image stacks consisting of 3 slices which can be viewed in ImageJ. -# The 3 slices are:: 1) the registered image, 2) the target image and 3) the black/white -# warp image. Galaxy supports only single-layered images, so we'll convert the images so they -# can be viewed in Galaxy. - -# Define command response buffers. -tmp_out = tempfile.NamedTemporaryFile().name -tmp_stdout = open( tmp_out, 'wb' ) -tmp_err = tempfile.NamedTemporaryFile().name -tmp_stderr = open( tmp_err, 'wb' ) - -# Build the command line to handle the multi-slice tiff images. -cmd = imagej2_base_utils.get_base_command_imagej2( None, jython_script=args.jython_script ) -if cmd is None: - imagej2_base_utils.stop_err( "ImageJ not found!" ) -if args.mono: - # bUnwarpJ will produce only a registered source image. - cmd += ' %s %s %s %s' % ( tmp_source_out_tiff_path, - args.source_out_datatype, - tmp_source_out_path, - args.mono ) -else: - # bUnwarpJ will produce registered source and target images. - cmd += ' %s %s %s %s %s %s %s' % ( tmp_source_out_tiff_path, - args.source_out_datatype, - tmp_source_out_path, - tmp_target_out_tiff_path, - args.target_out_datatype, - tmp_target_out_path, - args.mono ) - -# Merge the multi-slice tiff layers into an image that can be viewed in Galaxy. -proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True ) -rc = proc.wait() -if rc != 0: - error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout ) - imagej2_base_utils.stop_err( error_message ) - -# Save the Registered Source Image to the output dataset. -shutil.move( tmp_source_out_path, args.source_out ) -if not args.mono: - # Move the Registered Target Image to the output dataset. - shutil.move( tmp_target_out_path, args.target_out ) - -# If requested, save matrix transformations as additional datasets. -if save_transformation: - shutil.move( tmp_source_out_transf_path, args.source_trans_out ) - if not args.mono: - shutil.move( tmp_target_out_transf_path, args.target_trans_out ) - -imagej2_base_utils.cleanup_before_exit( tmp_dir ) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_bunwarpj_align_jython_script.py --- a/imagej2_bunwarpj_align_jython_script.py Tue Sep 17 17:01:44 2019 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,37 +0,0 @@ -import sys -import jython_utils -from ij import IJ - -# Fiji Jython interpreter implements Python 2.5 which does not -# provide support for argparse. - -if sys.argv[ -1 ].lower() in [ 'true' ]: - mono = True -else: - mono = False - -if mono: - # bUnwarpJ has been called with the -mono param. - source_tiff_path = sys.argv[ -4 ] - source_datatype = sys.argv[ -3 ] - source_path = sys.argv[ -2 ] -else: - source_tiff_path = sys.argv[ -7 ] - source_datatype = sys.argv[ -6 ] - source_path = sys.argv[ -5 ] - target_tiff_path = sys.argv[ -4 ] - target_datatype = sys.argv[ -3 ] - target_path = sys.argv[ -2 ] - -# Save the Registered Source Image. -registered_source_image = IJ.openImage( source_tiff_path ) -if source_datatype == 'tiff': - registered_source_image = jython_utils.convert_before_saving_as_tiff( registered_source_image ) -IJ.saveAs( registered_source_image, source_datatype, source_path ) - -if not mono: - # Save the Registered Target Image. - registered_target_image = IJ.openImage( target_tiff_path ) - if target_datatype == 'tiff': - registered_target_image = jython_utils.convert_before_saving_as_tiff( registered_target_image ) - IJ.saveAs( registered_target_image, target_datatype, target_path ) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_bunwarpj_compare_elastic.py --- a/imagej2_bunwarpj_compare_elastic.py Tue Sep 17 17:01:44 2019 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,65 +0,0 @@ -#!/usr/bin/env python -import argparse -import subprocess -import tempfile -import imagej2_base_utils - -# Parse Command Line. -parser = argparse.ArgumentParser() -parser.add_argument( '--source_image', dest='source_image', help='Source image' ) -parser.add_argument( '--source_image_format', dest='source_image_format', help='Source image format' ) -parser.add_argument( '--target_image', dest='target_image', help='Target image' ) -parser.add_argument( '--target_image_format', dest='target_image_format', help='Target image format' ) -parser.add_argument( '--source_transformation', dest='source_transformation', help='Direct source transformation matrix' ) -parser.add_argument( '--target_transformation', dest='target_transformation', help='Inverse target transformation matrix' ) -parser.add_argument( '--output', dest='output', help='Warping index' ) - -args = parser.parse_args() - -tmp_dir = imagej2_base_utils.get_temp_dir() -source_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_image, args.source_image_format ) -target_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_image, args.target_image_format ) -source_transformation_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_transformation, 'txt' ) -target_transformation_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_transformation, 'txt' ) -# bUnwarpJ produces several lines of output that we need to discard, so -# we'll use a temporary output file from which we'll read only the last line. -tmp_output_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.output, 'txt' ) - -# Define command response buffers. -tmp_out = tempfile.NamedTemporaryFile().name -tmp_stdout = open( tmp_out, 'wb' ) -tmp_err = tempfile.NamedTemporaryFile().name -tmp_stderr = open( tmp_err, 'wb' ) - -# Build the command line to calculate the warping index. -cmd = imagej2_base_utils.get_base_cmd_bunwarpj( None ) -if cmd is None: - imagej2_base_utils.stop_err( "bUnwarpJ not found!" ) -cmd += ' -compare_elastic' -# Target is sent before source. -cmd += ' %s' % target_image_path -cmd += ' %s' % source_image_path -cmd += ' %s' % target_transformation_path -cmd += ' %s' % source_transformation_path -cmd += ' > %s' % tmp_output_path - -# Calculate the warping index of two elastic transformations using bUnwarpJ. -proc = subprocess.Popen( args=cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True ) -rc = proc.wait() -if rc != 0: - error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout ) - imagej2_base_utils.stop_err( error_message ) - -# Example contents of tmp_output_path: -# ['Target image : ~/tmpKAYF1P.jpg\n', -# 'Source image : ~/tmpgQX0dy.gif\n', -# 'Target Transformation file : ~/tmpZJC_4B.txt\n', -# 'Source Transformation file : ~/tmphsSojl.txt\n', -# ' Warping index = 14.87777347388348\n'] -results = open( tmp_output_path, 'r' ).readlines() -warp_index = results[ -1 ].split( ' ' )[ -1 ] -outf = open( args.output, 'wb' ) -outf.write( '%s' % warp_index ) -outf.close() - -imagej2_base_utils.cleanup_before_exit( tmp_dir ) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_bunwarpj_compare_elastic_raw.py --- a/imagej2_bunwarpj_compare_elastic_raw.py Tue Sep 17 17:01:44 2019 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,64 +0,0 @@ -#!/usr/bin/env python -import argparse -import subprocess -import tempfile -import imagej2_base_utils - -# Parse Command Line. -parser = argparse.ArgumentParser() -parser.add_argument( '--source_image', dest='source_image', help='Source image' ) -parser.add_argument( '--source_image_format', dest='source_image_format', help='Source image format' ) -parser.add_argument( '--target_image', dest='target_image', help='Target image' ) -parser.add_argument( '--target_image_format', dest='target_image_format', help='Target image format' ) -parser.add_argument( '--target_elastic_transformation', dest='target_elastic_transformation', help='Target elastic transformation matrix' ) -parser.add_argument( '--source_raw_transformation', dest='source_raw_transformation', help='Source raw transformation matrix' ) -parser.add_argument( '--output', dest='output', help='Warping index' ) - -args = parser.parse_args() - -tmp_dir = imagej2_base_utils.get_temp_dir() -source_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_image, args.source_image_format ) -target_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_image, args.target_image_format ) -target_elastic_transformation_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_elastic_transformation, 'txt' ) -source_raw_transformation_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_raw_transformation, 'txt' ) -# bUnwarpJ produces several lines of output that we need to discard, so -# we'll use a temporary output file from which we'll read only the last line. -tmp_output_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.output, 'txt' ) - -# Define command response buffers. -tmp_out = tempfile.NamedTemporaryFile().name -tmp_stdout = open( tmp_out, 'wb' ) -tmp_err = tempfile.NamedTemporaryFile().name -tmp_stderr = open( tmp_err, 'wb' ) - -# Build the command line to calculate the warping index. -cmd = imagej2_base_utils.get_base_cmd_bunwarpj( None ) -if cmd is None: - imagej2_base_utils.stop_err( "bUnwarpJ not found!" ) -cmd += ' -compare_elastic_raw' -cmd += ' %s' % target_image_path -cmd += ' %s' % source_image_path -cmd += ' %s' % target_elastic_transformation_path -cmd += ' %s' % source_raw_transformation_path -cmd += ' > %s' % tmp_output_path - -# Calculate the warping index of elastic and raw transformations using bUnwarpJ. -proc = subprocess.Popen( args=cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True ) -rc = proc.wait() -if rc != 0: - error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout ) - imagej2_base_utils.stop_err( error_message ) - -# Example contents of tmp_output_path: -# ['Target image : ~/tmpHdt9Cs.jpg\n', -# 'Source image : ~/tmpu6kyfc.gif\n', -# 'Elastic Transformation file : ~/tmp4vZurG.txt\n', -# 'Raw Transformation file : ~/tmp2PNQcT.txt\n', -# ' Warping index = 25.007467512204983\n'] -results = open( tmp_output_path, 'r' ).readlines() -warp_index = results[ -1 ].split( ' ' )[ -1 ] -outf = open( args.output, 'wb' ) -outf.write( '%s' % warp_index ) -outf.close() - -imagej2_base_utils.cleanup_before_exit( tmp_dir ) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_bunwarpj_compare_raw.py --- a/imagej2_bunwarpj_compare_raw.py Tue Sep 17 17:01:44 2019 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,64 +0,0 @@ -#!/usr/bin/env python -import argparse -import subprocess -import tempfile -import imagej2_base_utils - -# Parse Command Line. -parser = argparse.ArgumentParser() -parser.add_argument( '--source_image', dest='source_image', help='Source image' ) -parser.add_argument( '--source_image_format', dest='source_image_format', help='Source image format' ) -parser.add_argument( '--target_image', dest='target_image', help='Target image' ) -parser.add_argument( '--target_image_format', dest='target_image_format', help='Target image format' ) -parser.add_argument( '--target_raw_transformation', dest='target_raw_transformation', help='First raw transformation matrix' ) -parser.add_argument( '--source_raw_transformation', dest='source_raw_transformation', help='Second raw transformation matrix' ) -parser.add_argument( '--output', dest='output', help='Warping index' ) - -args = parser.parse_args() - -tmp_dir = imagej2_base_utils.get_temp_dir() -source_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_image, args.source_image_format ) -target_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_image, args.target_image_format ) -target_raw_transformation_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_raw_transformation, 'txt' ) -source_raw_transformation_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_raw_transformation, 'txt' ) -# bUnwarpJ produces several lines of output that we need to discard, so -# we'll use a temporary output file from which we'll read only the last line. -tmp_output_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.output, 'txt' ) - -# Define command response buffers. -tmp_out = tempfile.NamedTemporaryFile().name -tmp_stdout = open( tmp_out, 'wb' ) -tmp_err = tempfile.NamedTemporaryFile().name -tmp_stderr = open( tmp_err, 'wb' ) - -# Build the command line to calculate the warping index. -cmd = imagej2_base_utils.get_base_cmd_bunwarpj( None ) -if cmd is None: - imagej2_base_utils.stop_err( "bUnwarpJ not found!" ) -cmd += ' -compare_raw' -cmd += ' %s' % target_image_path -cmd += ' %s' % source_image_path -cmd += ' %s' % target_raw_transformation_path -cmd += ' %s' % source_raw_transformation_path -cmd += ' > %s' % tmp_output_path - -# Calculate the warping index of two raw transformations using bUnwarpJ. -proc = subprocess.Popen( args=cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True ) -rc = proc.wait() -if rc != 0: - error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout ) - imagej2_base_utils.stop_err( error_message ) - -# Example contents of tmp_output_path: -# ['Target image : ~/tmp5WmDku.jpg\n', -# 'Source image : ~/tmps74U40.gif\n', -# 'Target Transformation file : ~/tmpXofC1x.txt\n', -# 'Source Transformation file : ~/tmpFqNYe4.txt\n', -# ' Warping index = 24.111209027033937\n'] -results = open( tmp_output_path, 'r' ).readlines() -warp_index = results[ -1 ].split( ' ' )[ -1 ] -outf = open( args.output, 'wb' ) -outf.write( '%s' % warp_index ) -outf.close() - -imagej2_base_utils.cleanup_before_exit( tmp_dir ) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_bunwarpj_compose_elastic.py --- a/imagej2_bunwarpj_compose_elastic.py Tue Sep 17 17:01:44 2019 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,50 +0,0 @@ -#!/usr/bin/env python -import argparse -import subprocess -import tempfile -import imagej2_base_utils - -# Parse Command Line. -parser = argparse.ArgumentParser() -parser.add_argument( '--source_image', dest='source_image', help='Source image' ) -parser.add_argument( '--source_image_format', dest='source_image_format', help='Source image format' ) -parser.add_argument( '--target_image', dest='target_image', help='Target image' ) -parser.add_argument( '--target_image_format', dest='target_image_format', help='Target image format' ) -parser.add_argument( '--source_elastic_transformation', dest='source_elastic_transformation', help='Direct source transformation matrix' ) -parser.add_argument( '--target_elastic_transformation', dest='target_elastic_transformation', help='Inverse target transformation matrix' ) -parser.add_argument( '--output', dest='output', help='Warping index' ) - -args = parser.parse_args() - -tmp_dir = imagej2_base_utils.get_temp_dir() -source_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_image, args.source_image_format ) -target_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_image, args.target_image_format ) -source_elastic_transformation_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_elastic_transformation, 'txt' ) -target_elastic_transformation_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_elastic_transformation, 'txt' ) - -# Define command response buffers. -tmp_out = tempfile.NamedTemporaryFile().name -tmp_stdout = open( tmp_out, 'wb' ) -tmp_err = tempfile.NamedTemporaryFile().name -tmp_stderr = open( tmp_err, 'wb' ) - -# Build the command line to compose the transformations. -cmd = imagej2_base_utils.get_base_cmd_bunwarpj( None ) -if cmd is None: - imagej2_base_utils.stop_err( "bUnwarpJ not found!" ) -cmd += ' -compose_elastic' -# Target is sent before source. -cmd += ' %s' % target_image_path -cmd += ' %s' % source_image_path -cmd += ' %s' % target_elastic_transformation_path -cmd += ' %s' % source_elastic_transformation_path -cmd += ' %s' % args.output - -# Compose the two elastic transformations into a raw transformation using bUnwarpJ. -proc = subprocess.Popen( args=cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True ) -rc = proc.wait() -if rc != 0: - error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout ) - imagej2_base_utils.stop_err( error_message ) - -imagej2_base_utils.cleanup_before_exit( tmp_dir ) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_bunwarpj_compose_raw.py --- a/imagej2_bunwarpj_compose_raw.py Tue Sep 17 17:01:44 2019 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,50 +0,0 @@ -#!/usr/bin/env python -import argparse -import subprocess -import tempfile -import imagej2_base_utils - -# Parse Command Line. -parser = argparse.ArgumentParser() -parser.add_argument( '--source_image', dest='source_image', help='Source image' ) -parser.add_argument( '--source_image_format', dest='source_image_format', help='Source image format' ) -parser.add_argument( '--target_image', dest='target_image', help='Target image' ) -parser.add_argument( '--target_image_format', dest='target_image_format', help='Target image format' ) -parser.add_argument( '--source_raw_transformation', dest='source_raw_transformation', help='Direct source transformation matrix' ) -parser.add_argument( '--target_raw_transformation', dest='target_raw_transformation', help='Inverse target transformation matrix' ) -parser.add_argument( '--output', dest='output', help='Warping index' ) - -args = parser.parse_args() - -tmp_dir = imagej2_base_utils.get_temp_dir() -source_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_image, args.source_image_format ) -target_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_image, args.target_image_format ) -source_raw_transformation_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_raw_transformation, 'txt' ) -target_raw_transformation_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_raw_transformation, 'txt' ) - -# Define command response buffers. -tmp_out = tempfile.NamedTemporaryFile().name -tmp_stdout = open( tmp_out, 'wb' ) -tmp_err = tempfile.NamedTemporaryFile().name -tmp_stderr = open( tmp_err, 'wb' ) - -# Build the command line to compose the two raw transformations. -cmd = imagej2_base_utils.get_base_cmd_bunwarpj( None ) -if cmd is None: - imagej2_base_utils.stop_err( "bUnwarpJ not found!" ) -cmd += ' -compose_raw' -# Target is sent before source. -cmd += ' %s' % target_image_path -cmd += ' %s' % source_image_path -cmd += ' %s' % target_raw_transformation_path -cmd += ' %s' % source_raw_transformation_path -cmd += ' %s' % args.output - -# Compose the two raw transformations into another raw transformation using bUnwarpJ. -proc = subprocess.Popen( args=cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True ) -rc = proc.wait() -if rc != 0: - error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout ) - imagej2_base_utils.stop_err( error_message ) - -imagej2_base_utils.cleanup_before_exit( tmp_dir ) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_bunwarpj_compose_raw_elastic.py --- a/imagej2_bunwarpj_compose_raw_elastic.py Tue Sep 17 17:01:44 2019 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,50 +0,0 @@ -#!/usr/bin/env python -import argparse -import subprocess -import tempfile -import imagej2_base_utils - -# Parse Command Line. -parser = argparse.ArgumentParser() -parser.add_argument( '--source_image', dest='source_image', help='Source image' ) -parser.add_argument( '--source_image_format', dest='source_image_format', help='Source image format' ) -parser.add_argument( '--target_image', dest='target_image', help='Target image' ) -parser.add_argument( '--target_image_format', dest='target_image_format', help='Target image format' ) -parser.add_argument( '--source_elastic_transformation', dest='source_elastic_transformation', help='Direct source transformation matrix' ) -parser.add_argument( '--target_raw_transformation', dest='target_raw_transformation', help='Inverse target transformation matrix' ) -parser.add_argument( '--output', dest='output', help='Warping index' ) - -args = parser.parse_args() - -tmp_dir = imagej2_base_utils.get_temp_dir() -source_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_image, args.source_image_format ) -target_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_image, args.target_image_format ) -source_elastic_transformation_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_elastic_transformation, 'txt' ) -target_raw_transformation_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_raw_transformation, 'txt' ) - -# Define command response buffers. -tmp_out = tempfile.NamedTemporaryFile().name -tmp_stdout = open( tmp_out, 'wb' ) -tmp_err = tempfile.NamedTemporaryFile().name -tmp_stderr = open( tmp_err, 'wb' ) - -# Build the command line to compose the raw and elastic transformations. -cmd = imagej2_base_utils.get_base_cmd_bunwarpj( None ) -if cmd is None: - imagej2_base_utils.stop_err( "bUnwarpJ not found!" ) -cmd += ' -compose_raw_elastic' -# Target is sent before source. -cmd += ' %s' % target_image_path -cmd += ' %s' % source_image_path -cmd += ' %s' % target_raw_transformation_path -cmd += ' %s' % source_elastic_transformation_path -cmd += ' %s' % args.output - -# Compose the raw and elastic transformations into another raw transformation using bUnwarpJ. -proc = subprocess.Popen( args=cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True ) -rc = proc.wait() -if rc != 0: - error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout ) - imagej2_base_utils.stop_err( error_message ) - -imagej2_base_utils.cleanup_before_exit( tmp_dir ) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_bunwarpj_convert_to_raw.py --- a/imagej2_bunwarpj_convert_to_raw.py Tue Sep 17 17:01:44 2019 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,47 +0,0 @@ -#!/usr/bin/env python -import argparse -import subprocess -import tempfile -import imagej2_base_utils - -# Parse Command Line. -parser = argparse.ArgumentParser() -parser.add_argument( '--source_image', dest='source_image', help='Source image' ) -parser.add_argument( '--source_image_format', dest='source_image_format', help='Source image format' ) -parser.add_argument( '--target_image', dest='target_image', help='Target image' ) -parser.add_argument( '--target_image_format', dest='target_image_format', help='Target image format' ) -parser.add_argument( '--elastic_transformation', dest='elastic_transformation', help='Elastic transformation as saved by bUnwarpJ in elastic format' ) -parser.add_argument( '--raw_transformation', dest='raw_transformation', help='Raw transformation' ) - -args = parser.parse_args() - -tmp_dir = imagej2_base_utils.get_temp_dir() -source_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_image, args.source_image_format ) -target_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_image, args.target_image_format ) -elastic_transformation_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.elastic_transformation, 'txt' ) - -# Define command response buffers. -tmp_out = tempfile.NamedTemporaryFile().name -tmp_stdout = open( tmp_out, 'wb' ) -tmp_err = tempfile.NamedTemporaryFile().name -tmp_stderr = open( tmp_err, 'wb' ) - -# Build the command line to convert the B-spline (i.e., elastic) transformation to raw. -cmd = imagej2_base_utils.get_base_cmd_bunwarpj( None ) -if cmd is None: - imagej2_base_utils.stop_err( "bUnwarpJ not found!" ) -cmd += ' -convert_to_raw' -# Target is sent before source. -cmd += ' %s' % target_image_path -cmd += ' %s' % source_image_path -cmd += ' %s' % elastic_transformation_path -cmd += ' %s' % args.raw_transformation - -# Convert the elastic transformation to raw using bUnwarpJ. -proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True ) -rc = proc.wait() -if rc != 0: - error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout ) - imagej2_base_utils.stop_err( error_message ) - -imagej2_base_utils.cleanup_before_exit( tmp_dir ) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_bunwarpj_elastic_transform.py --- a/imagej2_bunwarpj_elastic_transform.py Tue Sep 17 17:01:44 2019 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,73 +0,0 @@ -#!/usr/bin/env python -import argparse -import shutil -import subprocess -import tempfile -import imagej2_base_utils - -# Parse Command Line. -parser = argparse.ArgumentParser() -parser.add_argument( '--source_image', dest='source_image', help='Source image' ) -parser.add_argument( '--source_image_format', dest='source_image_format', help='Source image format' ) -parser.add_argument( '--target_image', dest='target_image', help='Target image' ) -parser.add_argument( '--target_image_format', dest='target_image_format', help='Target image format' ) -parser.add_argument( '--elastic_transformation', dest='elastic_transformation', help='Elastic transformation as saved by bUnwarpJ in elastic format' ) -parser.add_argument( '--source_out', help='Output source image' ) -parser.add_argument( '--source_out_datatype', help='Output registered source image format' ) -parser.add_argument( '--jython_script', dest='jython_script', help='Path to the Jython script' ) - -args = parser.parse_args() - -tmp_dir = imagej2_base_utils.get_temp_dir() -source_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_image, args.source_image_format ) -tmp_source_out_tiff_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, 'tiff' ) -tmp_source_out_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, args.source_out_datatype ) -target_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_image, args.target_image_format ) -elastic_transformation_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.elastic_transformation, 'txt' ) - -# Define command response buffers. -tmp_out = tempfile.NamedTemporaryFile().name -tmp_stdout = open( tmp_out, 'wb' ) -tmp_err = tempfile.NamedTemporaryFile().name -tmp_stderr = open( tmp_err, 'wb' ) - -# Build the command line to apply the transformation. -cmd = imagej2_base_utils.get_base_cmd_bunwarpj( None ) -if cmd is None: - imagej2_base_utils.stop_err( "bUnwarpJ not found!" ) -cmd += ' -elastic_transform' -# Target is sent before source. -cmd += ' %s' % target_image_path -cmd += ' %s' % source_image_path -cmd += ' %s' % elastic_transformation_path -cmd += ' %s' % tmp_source_out_tiff_path - -# Apply the elastic transformation using bUnwarpJ. -proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True ) -rc = proc.wait() -if rc != 0: - error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout ) - imagej2_base_utils.stop_err( error_message ) - -# Convert the registered image to the specified output format. -tmp_out = tempfile.NamedTemporaryFile().name -tmp_stdout = open( tmp_out, 'wb' ) -tmp_err = tempfile.NamedTemporaryFile().name -tmp_stderr = open( tmp_err, 'wb' ) - -cmd = imagej2_base_utils.get_base_command_imagej2( None, jython_script=args.jython_script ) -if cmd is None: - imagej2_base_utils.stop_err( "ImageJ not found!" ) -cmd += ' %s %s %s' % ( tmp_source_out_tiff_path, - args.source_out_datatype, - tmp_source_out_path ) - -proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True ) -rc = proc.wait() -if rc != 0: - error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout ) - imagej2_base_utils.stop_err( error_message ) - -# Save the Registered Source Image to the defined output. -shutil.move( tmp_source_out_path, args.source_out ) -imagej2_base_utils.cleanup_before_exit( tmp_dir ) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_bunwarpj_elastic_transform_jython_script.py --- a/imagej2_bunwarpj_elastic_transform_jython_script.py Tue Sep 17 17:01:44 2019 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,16 +0,0 @@ -import sys -import jython_utils -from ij import IJ - -# Fiji Jython interpreter implements Python 2.5 which does not -# provide support for argparse. - -source_tiff_path = sys.argv[ -3 ] -source_datatype = sys.argv[ -2 ] -source_path = sys.argv[ -1 ] - -# Save the Registered Source Image. -registered_source_image = IJ.openImage( source_tiff_path ) -if source_datatype == 'tiff': - registered_source_image = jython_utils.convert_before_saving_as_tiff( registered_source_image ) -IJ.saveAs( registered_source_image, source_datatype, source_path ) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_bunwarpj_raw_transform.py --- a/imagej2_bunwarpj_raw_transform.py Tue Sep 17 17:01:44 2019 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,73 +0,0 @@ -#!/usr/bin/env python -import argparse -import shutil -import subprocess -import tempfile -import imagej2_base_utils - -# Parse Command Line. -parser = argparse.ArgumentParser() -parser.add_argument( '--source_image', dest='source_image', help='Source image' ) -parser.add_argument( '--source_image_format', dest='source_image_format', help='Source image format' ) -parser.add_argument( '--target_image', dest='target_image', help='Target image' ) -parser.add_argument( '--target_image_format', dest='target_image_format', help='Target image format' ) -parser.add_argument( '--raw_transformation', dest='raw_transformation', help='Raw transformation as saved by bUnwarpJ' ) -parser.add_argument( '--source_out', help='Output source image' ) -parser.add_argument( '--source_out_datatype', help='Output registered source image format' ) -parser.add_argument( '--jython_script', dest='jython_script', help='Path to the Jython script' ) - -args = parser.parse_args() - -tmp_dir = imagej2_base_utils.get_temp_dir() -source_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_image, args.source_image_format ) -tmp_source_out_tiff_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, 'tiff' ) -tmp_source_out_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, args.source_out_datatype ) -target_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_image, args.target_image_format ) -raw_transformation_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.raw_transformation, 'txt' ) - -# Define command response buffers. -tmp_out = tempfile.NamedTemporaryFile().name -tmp_stdout = open( tmp_out, 'wb' ) -tmp_err = tempfile.NamedTemporaryFile().name -tmp_stderr = open( tmp_err, 'wb' ) - -# Build the command line to apply the raw transformation. -cmd = imagej2_base_utils.get_base_cmd_bunwarpj( None ) -if cmd is None: - imagej2_base_utils.stop_err( "bUnwarpJ not found!" ) -cmd += ' -raw_transform' -# Target is sent before source. -cmd += ' %s' % target_image_path -cmd += ' %s' % source_image_path -cmd += ' %s' % raw_transformation_path -cmd += ' %s' % tmp_source_out_tiff_path - -# Apply the raw transformation using bUnwarpJ. -proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True ) -rc = proc.wait() -if rc != 0: - error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout ) - imagej2_base_utils.stop_err( error_message ) - -# Convert the registered image to the specified output format. -tmp_out = tempfile.NamedTemporaryFile().name -tmp_stdout = open( tmp_out, 'wb' ) -tmp_err = tempfile.NamedTemporaryFile().name -tmp_stderr = open( tmp_err, 'wb' ) - -cmd = imagej2_base_utils.get_base_command_imagej2( None, jython_script=args.jython_script ) -if cmd is None: - imagej2_base_utils.stop_err( "ImageJ not found!" ) -cmd += ' %s %s %s' % ( tmp_source_out_tiff_path, - args.source_out_datatype, - tmp_source_out_path ) - -proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True ) -rc = proc.wait() -if rc != 0: - error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout ) - imagej2_base_utils.stop_err( error_message ) - -# Save the Registered Source Image to the defined output. -shutil.move( tmp_source_out_path, args.source_out ) -imagej2_base_utils.cleanup_before_exit( tmp_dir ) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_bunwarpj_raw_transform_jython_script.py --- a/imagej2_bunwarpj_raw_transform_jython_script.py Tue Sep 17 17:01:44 2019 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,16 +0,0 @@ -import sys -import jython_utils -from ij import IJ - -# Fiji Jython interpreter implements Python 2.5 which does not -# provide support for argparse. - -source_tiff_path = sys.argv[ -3 ] -source_datatype = sys.argv[ -2 ] -source_path = sys.argv[ -1 ] - -# Save the Registered Source Image. -registered_source_image = IJ.openImage( source_tiff_path ) -if source_datatype == 'tiff': - registered_source_image = jython_utils.convert_before_saving_as_tiff( registered_source_image ) -IJ.saveAs( registered_source_image, source_datatype, source_path ) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_create_image.py --- a/imagej2_create_image.py Tue Sep 17 17:01:44 2019 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,40 +0,0 @@ -#!/usr/bin/env python -import argparse -import shutil -import subprocess -import tempfile -import imagej2_base_utils - -if __name__=="__main__": - # Parse Command Line. - parser = argparse.ArgumentParser() - parser.add_argument( '--width', dest='width', type=int, help='Image width in pixels' ) - parser.add_argument( '--height', dest='height', type=int, help='Image height in pixels' ) - parser.add_argument( '--depth', dest='depth', type=int, help='Image depth (specifies the number of stack slices)' ) - parser.add_argument( '--image_type', dest='image_type', help='Image type' ) - parser.add_argument( '--image_title', dest='image_title', default='', help='Image title' ) - parser.add_argument( '--output_datatype', dest='output_datatype', help='Output image format' ) - parser.add_argument( '--jython_script', dest='jython_script', help='Path to the Jython script' ) - parser.add_argument( '--out_fname', help='Path to the output file' ) - args = parser.parse_args() - - tmp_dir = imagej2_base_utils.get_temp_dir() - tmp_image_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, args.output_datatype ) - - # Define command response buffers. - tmp_out = tempfile.NamedTemporaryFile().name - tmp_stdout = open( tmp_out, 'wb' ) - tmp_err = tempfile.NamedTemporaryFile().name - tmp_stderr = open( tmp_err, 'wb' ) - # Build the command line. - cmd = imagej2_base_utils.get_base_command_imagej2( None, jython_script=args.jython_script ) - if cmd is None: - imagej2_base_utils.stop_err( "ImageJ not found!" ) - cmd += ' %s %d %d %d %s %s' % ( args.image_title, args.width, args.height, args.depth, args.image_type, tmp_image_path ) - proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True ) - rc = proc.wait() - if rc != 0: - error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout ) - imagej2_base_utils.stop_err( error_message ) - shutil.move( tmp_image_path, args.out_fname ) - imagej2_base_utils.cleanup_before_exit( tmp_dir ) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_create_image_jython_script.py --- a/imagej2_create_image_jython_script.py Tue Sep 17 17:01:44 2019 -0400 +++ b/imagej2_create_image_jython_script.py Mon Sep 28 16:49:43 2020 +0000 @@ -1,14 +1,15 @@ import sys + from ij import IJ # Fiji Jython interpreter implements Python 2.5 which does not # provide support for argparse. -title = sys.argv[ -6 ] -width = int( sys.argv[ -5 ] ) -height = int( sys.argv[ -4 ] ) -depth = int( sys.argv[ -3 ] ) -type = sys.argv[ -2 ].replace( '_', ' ' ) -tmp_image_path = sys.argv[ -1 ] +title = sys.argv[-6] +width = int(sys.argv[-5]) +height = int(sys.argv[-4]) +depth = int(sys.argv[-3]) +type = sys.argv[-2].replace('_', ' ') +tmp_image_path = sys.argv[-1] -imp = IJ.newImage( title, type, width, height, depth ) -IJ.save( imp, "%s" % tmp_image_path ) +imp = IJ.newImage(title, type, width, height, depth) +IJ.save(imp, "%s" % tmp_image_path) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_enhance_contrast.py --- a/imagej2_enhance_contrast.py Tue Sep 17 17:01:44 2019 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,63 +0,0 @@ -#!/usr/bin/env python -import argparse -import os -import shutil -import subprocess -import tempfile -import imagej2_base_utils - -parser = argparse.ArgumentParser() -parser.add_argument( '--input', dest='input', help='Path to the input file' ) -parser.add_argument( '--input_datatype', dest='input_datatype', help='Datatype of the input image' ) -parser.add_argument( '--equalize_histogram', dest='equalize_histogram', help='Equalize_histogram' ) -parser.add_argument( '--saturated_pixels', dest='saturated_pixels', type=float, default=None, help='Saturated pixel pct' ) -parser.add_argument( '--normalize', dest='normalize', help='Normalize' ) -parser.add_argument( '--jython_script', dest='jython_script', help='Path to the Jython script' ) -parser.add_argument( '--output', dest='output', help='Path to the output file' ) -parser.add_argument( '--output_datatype', dest='output_datatype', help='Datatype of the output image' ) -args = parser.parse_args() - -tmp_dir = imagej2_base_utils.get_temp_dir() -# ImageJ expects valid image file extensions, so the Galaxy .dat extension does not -# work for some features. The following creates a symlink with an appropriate file -# extension that points to the Galaxy dataset. This symlink is used by ImageJ. -tmp_input_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.input, args.input_datatype ) -tmp_output_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, args.output_datatype ) - -# Define command response buffers. -tmp_out = tempfile.NamedTemporaryFile().name -tmp_stdout = open( tmp_out, 'wb' ) -tmp_err = tempfile.NamedTemporaryFile().name -tmp_stderr = open( tmp_err, 'wb' ) -# Java writes a lot of stuff to stderr, so we'll specify a file for handling actual errors. -error_log = tempfile.NamedTemporaryFile( delete=False ).name - -# Build the command line. -cmd = imagej2_base_utils.get_base_command_imagej2( None, jython_script=args.jython_script ) -if cmd is None: - imagej2_base_utils.stop_err( "ImageJ not found!" ) -cmd += ' %s' % error_log -cmd += ' %s' % tmp_input_path -cmd += ' %s' % args.equalize_histogram -cmd += imagej2_base_utils.handle_none_type( args.saturated_pixels ) -cmd += ' %s' % args.normalize -cmd += ' %s' % tmp_output_path -cmd += ' %s' % args.output_datatype - -# Run the command. -proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True ) -rc = proc.wait() - -# Handle execution errors. -if rc != 0: - error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout ) - imagej2_base_utils.stop_err( error_message ) - -# Handle processing errors. -if os.path.getsize( error_log ) > 0: - error_message = open( error_log, 'r' ).read() - imagej2_base_utils.stop_err( error_message ) - -# Save the output image. -shutil.move( tmp_output_path, args.output ) -imagej2_base_utils.cleanup_before_exit( tmp_dir ) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_enhance_contrast_jython_script.py --- a/imagej2_enhance_contrast_jython_script.py Tue Sep 17 17:01:44 2019 -0400 +++ b/imagej2_enhance_contrast_jython_script.py Mon Sep 28 16:49:43 2020 +0000 @@ -1,19 +1,19 @@ -import jython_utils import sys + from ij import IJ # Fiji Jython interpreter implements Python 2.5 which does not # provide support for argparse. -error_log = sys.argv[ -7 ] -input = sys.argv[ -6 ] -equalize_histogram = jython_utils.asbool( sys.argv[ -5 ] ) -saturated_pixels = sys.argv[ -4 ] -normalize = jython_utils.asbool( sys.argv[ -3 ] ) -tmp_output_path = sys.argv[ -2 ] -output_datatype = sys.argv[ -1 ] +error_log = sys.argv[-7] +input = sys.argv[-6] +equalize_histogram = sys.argv[-5] == "yes" +saturated_pixels = sys.argv[-4] +normalize = sys.argv[-3] == "yes" +tmp_output_path = sys.argv[-2] +output_datatype = sys.argv[-1] # Open the input image file. -input_image_plus = IJ.openImage( input ) +input_image_plus = IJ.openImage(input) # Create a copy of the image. input_image_plus_copy = input_image_plus.duplicate() @@ -24,19 +24,16 @@ options = [] # If equalize_histogram, saturated_pixels and normalize are ignored. if equalize_histogram: - options.append( 'equalize' ) + options.append('equalize') else: - if saturated_pixels not in [ None, 'None' ]: + if saturated_pixels not in [None, 'None']: # Fiji allows only a single decimal place for this value. - options.append( 'saturated=%.3f' % float( saturated_pixels ) ) + options.append('saturated=%.3f' % float(saturated_pixels)) # Normalization of RGB images is not supported. if bit_depth != 24 and normalize: - options.append( 'normalize' ) -try: - # Run the command. - options = "%s" % ' '.join( options ) - IJ.run( input_image_plus_copy, "Enhance Contrast...", options ) - # Save the ImagePlus object as a new image. - IJ.saveAs( input_image_plus_copy, output_datatype, tmp_output_path ) -except Exception, e: - jython_utils.handle_error( error_log, str( e ) ) + options.append('normalize') +# Run the command. +options = "%s" % ' '.join(options) +IJ.run(input_image_plus_copy, "Enhance Contrast...", options) +# Save the ImagePlus object as a new image. +IJ.saveAs(input_image_plus_copy, output_datatype, tmp_output_path) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_find_edges.py --- a/imagej2_find_edges.py Tue Sep 17 17:01:44 2019 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,57 +0,0 @@ -#!/usr/bin/env python -import argparse -import os -import shutil -import subprocess -import tempfile -import imagej2_base_utils - -parser = argparse.ArgumentParser() -parser.add_argument( '--input', dest='input', help='Path to the input file' ) -parser.add_argument( '--input_datatype', dest='input_datatype', help='Datatype of the input image' ) -parser.add_argument( '--jython_script', dest='jython_script', help='Path to the Jython script' ) -parser.add_argument( '--output', dest='output', help='Path to the output file' ) -parser.add_argument( '--output_datatype', dest='output_datatype', help='Datatype of the output image' ) -args = parser.parse_args() - -tmp_dir = imagej2_base_utils.get_temp_dir() -# ImageJ expects valid image file extensions, so the Galaxy .dat extension does not -# work for some features. The following creates a symlink with an appropriate file -# extension that points to the Galaxy dataset. This symlink is used by ImageJ. -tmp_input_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.input, args.input_datatype ) -tmp_output_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, args.output_datatype ) - -# Define command response buffers. -tmp_out = tempfile.NamedTemporaryFile().name -tmp_stdout = open( tmp_out, 'wb' ) -tmp_err = tempfile.NamedTemporaryFile().name -tmp_stderr = open( tmp_err, 'wb' ) -# Java writes a lot of stuff to stderr, so we'll specify a file for handling actual errors. -error_log = tempfile.NamedTemporaryFile( delete=False ).name - -# Build the command line. -cmd = imagej2_base_utils.get_base_command_imagej2( None, jython_script=args.jython_script ) -if cmd is None: - imagej2_base_utils.stop_err( "ImageJ not found!" ) -cmd += ' %s' % error_log -cmd += ' %s' % tmp_input_path -cmd += ' %s' % tmp_output_path -cmd += ' %s' % args.output_datatype - -# Run the command. -proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True ) -rc = proc.wait() - -# Handle execution errors. -if rc != 0: - error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout ) - imagej2_base_utils.stop_err( error_message ) - -# Handle processing errors. -if os.path.getsize( error_log ) > 0: - error_message = open( error_log, 'r' ).read() - imagej2_base_utils.stop_err( error_message ) - -# Save the output image. -shutil.move( tmp_output_path, args.output ) -imagej2_base_utils.cleanup_before_exit( tmp_dir ) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_find_edges_jython_script.py --- a/imagej2_find_edges_jython_script.py Tue Sep 17 17:01:44 2019 -0400 +++ b/imagej2_find_edges_jython_script.py Mon Sep 28 16:49:43 2020 +0000 @@ -1,25 +1,22 @@ -import jython_utils import sys + from ij import IJ # Fiji Jython interpreter implements Python 2.5 which does not # provide support for argparse. -error_log = sys.argv[ -4 ] -input = sys.argv[ -3 ] -tmp_output_path = sys.argv[ -2 ] -output_datatype = sys.argv[ -1 ] +error_log = sys.argv[-4] +input = sys.argv[-3] +tmp_output_path = sys.argv[-2] +output_datatype = sys.argv[-1] # Open the input image file. -input_image_plus = IJ.openImage( input ) +input_image_plus = IJ.openImage(input) # Create a copy of the image. input_image_plus_copy = input_image_plus.duplicate() image_processor_copy = input_image_plus_copy.getProcessor() -try: - # Run the command. - IJ.run( input_image_plus_copy, "Find Edges", "" ) - # Save the ImagePlus object as a new image. - IJ.saveAs( input_image_plus_copy, output_datatype, tmp_output_path ) -except Exception, e: - jython_utils.handle_error( error_log, str( e ) ) +# Run the command. +IJ.run(input_image_plus_copy, "Find Edges", "") +# Save the ImagePlus object as a new image. +IJ.saveAs(input_image_plus_copy, output_datatype, tmp_output_path) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_find_maxima.py --- a/imagej2_find_maxima.py Tue Sep 17 17:01:44 2019 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,69 +0,0 @@ -#!/usr/bin/env python -import argparse -import os -import shutil -import subprocess -import tempfile -import imagej2_base_utils - -parser = argparse.ArgumentParser() -parser.add_argument( '--input', dest='input', help='Path to the input file' ) -parser.add_argument( '--input_datatype', dest='input_datatype', help='Datatype of the input image' ) -parser.add_argument( '--scale_when_converting', dest='scale_when_converting', help='Scale when converting RGB image' ) -parser.add_argument( '--weighted_rgb_conversions', dest='weighted_rgb_conversions', help='Weighted RGB conversions for RGB image' ) -parser.add_argument( '--noise_tolerance', dest='noise_tolerance', type=int, help='Noise tolerance' ) -parser.add_argument( '--output_type', dest='output_type', help='Output type' ) -parser.add_argument( '--exclude_edge_maxima', dest='exclude_edge_maxima', help='Exclude edge maxima' ) -parser.add_argument( '--light_background', dest='light_background', help='Light background' ) -parser.add_argument( '--jython_script', dest='jython_script', help='Path to the Jython script' ) -parser.add_argument( '--output', dest='output', help='Path to the output file' ) -parser.add_argument( '--output_datatype', dest='output_datatype', help='Datatype of the output image' ) -args = parser.parse_args() - -tmp_dir = imagej2_base_utils.get_temp_dir() -# ImageJ expects valid image file extensions, so the Galaxy .dat extension does not -# work for some features. The following creates a symlink with an appropriate file -# extension that points to the Galaxy dataset. This symlink is used by ImageJ. -tmp_input_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.input, args.input_datatype ) -tmp_output_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, args.output_datatype ) - -# Define command response buffers. -tmp_out = tempfile.NamedTemporaryFile().name -tmp_stdout = open( tmp_out, 'wb' ) -tmp_err = tempfile.NamedTemporaryFile().name -tmp_stderr = open( tmp_err, 'wb' ) -# Java writes a lot of stuff to stderr, so we'll specify a file for handling actual errors. -error_log = tempfile.NamedTemporaryFile( delete=False ).name - -# Build the command line. -cmd = imagej2_base_utils.get_base_command_imagej2( None, jython_script=args.jython_script ) -if cmd is None: - imagej2_base_utils.stop_err( "ImageJ not found!" ) -cmd += ' %s' % error_log -cmd += ' %s' % tmp_input_path -cmd += ' %s' % args.scale_when_converting -cmd += ' %s' % args.weighted_rgb_conversions -cmd += ' %d' % args.noise_tolerance -cmd += ' %s' % args.output_type -cmd += ' %s' % args.exclude_edge_maxima -cmd += ' %s' % args.light_background -cmd += ' %s' % tmp_output_path -cmd += ' %s' % args.output_datatype - -# Run the command. -proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True ) -rc = proc.wait() - -# Handle execution errors. -if rc != 0: - error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout ) - imagej2_base_utils.stop_err( error_message ) - -# Handle processing errors. -if os.path.getsize( error_log ) > 0: - error_message = open( error_log, 'r' ).read() - imagej2_base_utils.stop_err( error_message ) - -# Save the output image. -shutil.move( tmp_output_path, args.output ) -imagej2_base_utils.cleanup_before_exit( tmp_dir ) diff -r 29b39f5b0e69 -r e2622ebb5ce4 imagej2_find_maxima.xml --- a/imagej2_find_maxima.xml Tue Sep 17 17:01:44 2019 -0400 +++ b/imagej2_find_maxima.xml Mon Sep 28 16:49:43 2020 +0000 @@ -1,33 +1,53 @@ - - + imagej2_macros.xml - - - + '$error_log'; +if [[ $? -ne 0 ]]; then + cat '$error_log' >&2; +else + #if $list_or_count: + mv '$results' '$output'; #else: - --output "$output" - --output_datatype $output.ext + mv '$output_filename' '$output'; #end if -]]> - +fi +]]> - + @@ -59,30 +79,30 @@ - + output_type != "List" and output_type != "Count" - + output_type == "List" or output_type == "Count" - - + + - - - - + + + + - - - - - + + + + +