Mercurial > repos > imgteam > imagej2_noise
comparison imagej2_analyze_skeleton_jython_script.py @ 0:3f6599ec7d30 draft
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
author | imgteam |
---|---|
date | Tue, 17 Sep 2019 16:58:28 -0400 |
parents | |
children | 7a44772cc89f |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3f6599ec7d30 |
---|---|
1 import jython_utils | |
2 import math | |
3 import sys | |
4 from ij import IJ | |
5 from sc.fiji.analyzeSkeleton import AnalyzeSkeleton_ | |
6 | |
7 BASIC_NAMES = [ 'Branches', 'Junctions', 'End-point Voxels', | |
8 'Junction Voxels', 'Slab Voxels', 'Average branch length', | |
9 'Triple Points', 'Quadruple Points', 'Maximum Branch Length' ] | |
10 DETAIL_NAMES = [ 'Skeleton ID', 'Branch length', 'V1 x', 'V1 y', 'V1 z', 'V2 x', | |
11 'V2 y', 'V2 z', 'Euclidean distance' ] | |
12 | |
13 def get_euclidean_distance( vertex1, vertex2 ): | |
14 x1, y1, z1 = get_points( vertex1 ) | |
15 x2, y2, z2 = get_points( vertex2 ) | |
16 return math.sqrt( math.pow( ( x2 - x1 ), 2 ) + | |
17 math.pow( ( y2 - y1 ), 2 ) + | |
18 math.pow( ( z2 - z1 ), 2 ) ) | |
19 | |
20 def get_graph_length( graph ): | |
21 length = 0 | |
22 for edge in graph.getEdges(): | |
23 length = length + edge.getLength() | |
24 return length | |
25 | |
26 def get_points( vertex ): | |
27 # An array of Point, which has attributes x,y,z. | |
28 point = vertex.getPoints()[ 0 ] | |
29 return point.x, point.y, point.z | |
30 | |
31 def get_sorted_edge_lengths( graph ): | |
32 # Return graph edges sorted from longest to shortest. | |
33 edges = graph.getEdges() | |
34 edges = sorted( edges, key=lambda edge: edge.getLength(), reverse=True ) | |
35 return edges | |
36 | |
37 def get_sorted_graph_lengths( result ): | |
38 # Get the separate graphs (skeletons). | |
39 graphs = result.getGraph() | |
40 # Sort graphs from longest to shortest. | |
41 graphs = sorted( graphs, key=lambda g: get_graph_length( g ), reverse=True ) | |
42 return graphs | |
43 | |
44 def save( result, output, show_detailed_info, calculate_largest_shortest_path, sep='\t' ): | |
45 num_trees = int( result.getNumOfTrees() ) | |
46 outf = open( output, 'wb' ) | |
47 outf.write( '# %s\n' % sep.join( BASIC_NAMES ) ) | |
48 for index in range( num_trees ): | |
49 outf.write( '%d%s' % ( result.getBranches()[ index ], sep ) ) | |
50 outf.write( '%d%s' % ( result.getJunctions()[ index ], sep ) ) | |
51 outf.write( '%d%s' % ( result.getEndPoints()[ index ], sep ) ) | |
52 outf.write( '%d%s' % ( result.getJunctionVoxels()[ index ], sep ) ) | |
53 outf.write( '%d%s' % ( result.getSlabs()[ index ], sep ) ) | |
54 outf.write( '%.3f%s' % ( result.getAverageBranchLength()[ index ], sep ) ) | |
55 outf.write( '%d%s' % ( result.getTriples()[ index ], sep ) ) | |
56 outf.write( '%d%s' % ( result.getQuadruples()[ index ], sep ) ) | |
57 outf.write( '%.3f' % result.getMaximumBranchLength()[ index ] ) | |
58 if calculate_largest_shortest_path: | |
59 outf.write( '%s%.3f%s' % ( sep, result.shortestPathList.get( index ), sep ) ) | |
60 outf.write( '%d%s' % ( result.spStartPosition[ index ][ 0 ], sep ) ) | |
61 outf.write( '%d%s' % ( result.spStartPosition[ index ][ 1 ], sep ) ) | |
62 outf.write( '%d\n' % result.spStartPosition[ index ][ 2 ] ) | |
63 else: | |
64 outf.write( '\n' ) | |
65 if show_detailed_info: | |
66 outf.write( '# %s\n' % sep.join( DETAIL_NAMES ) ) | |
67 # The following index is a placeholder for the skeleton ID. | |
68 # The terms "graph" and "skeleton" refer to the same thing. | |
69 # Also, the SkeletonResult.java code states that the | |
70 # private Graph[] graph object is an array of graphs (one | |
71 # per tree). | |
72 for index, graph in enumerate( get_sorted_graph_lengths( result ) ): | |
73 for edge in get_sorted_edge_lengths( graph ): | |
74 vertex1 = edge.getV1() | |
75 x1, y1, z1 = get_points( vertex1 ) | |
76 vertex2 = edge.getV2() | |
77 x2, y2, z2 = get_points( vertex2 ) | |
78 outf.write( '%d%s' % ( index+1, sep ) ) | |
79 outf.write( '%.3f%s' % ( edge.getLength(), sep ) ) | |
80 outf.write( '%d%s' % ( x1, sep ) ) | |
81 outf.write( '%d%s' % ( y1, sep ) ) | |
82 outf.write( '%d%s' % ( z1, sep ) ) | |
83 outf.write( '%d%s' % ( x2, sep ) ) | |
84 outf.write( '%d%s' % ( y2, sep ) ) | |
85 outf.write( '%d%s' % ( z2, sep ) ) | |
86 outf.write( '%.3f' % get_euclidean_distance( vertex1, vertex2 ) ) | |
87 if calculate_largest_shortest_path: | |
88 # Keep number of separated items the same for each line. | |
89 outf.write( '%s %s' % ( sep, sep ) ) | |
90 outf.write( ' %s' % sep ) | |
91 outf.write( ' %s' % sep ) | |
92 outf.write( ' \n' ) | |
93 else: | |
94 outf.write( '\n' ) | |
95 outf.close() | |
96 | |
97 # Fiji Jython interpreter implements Python 2.5 which does not | |
98 # provide support for argparse. | |
99 error_log = sys.argv[ -8 ] | |
100 input = sys.argv[ -7 ] | |
101 black_background = jython_utils.asbool( sys.argv[ -6 ] ) | |
102 prune_cycle_method = sys.argv[ -5 ] | |
103 prune_ends = jython_utils.asbool( sys.argv[ -4 ] ) | |
104 calculate_largest_shortest_path = jython_utils.asbool( sys.argv[ -3 ] ) | |
105 if calculate_largest_shortest_path: | |
106 BASIC_NAMES.extend( [ 'Longest Shortest Path', 'spx', 'spy', 'spz' ] ) | |
107 DETAIL_NAMES.extend( [ ' ', ' ', ' ', ' ' ] ) | |
108 show_detailed_info = jython_utils.asbool( sys.argv[ -2 ] ) | |
109 output = sys.argv[ -1 ] | |
110 | |
111 # Open the input image file. | |
112 input_image_plus = IJ.openImage( input ) | |
113 | |
114 # Create a copy of the image. | |
115 input_image_plus_copy = input_image_plus.duplicate() | |
116 image_processor_copy = input_image_plus_copy.getProcessor() | |
117 | |
118 try: | |
119 # Set binary options. | |
120 options = jython_utils.get_binary_options( black_background=black_background ) | |
121 IJ.run( input_image_plus_copy, "Options...", options ) | |
122 | |
123 # Convert image to binary if necessary. | |
124 if not image_processor_copy.isBinary(): | |
125 IJ.run( input_image_plus_copy, "Make Binary", "" ) | |
126 | |
127 # Run AnalyzeSkeleton | |
128 analyze_skeleton = AnalyzeSkeleton_() | |
129 analyze_skeleton.setup( "", input_image_plus_copy ) | |
130 if prune_cycle_method == 'none': | |
131 prune_index = analyze_skeleton.NONE | |
132 elif prune_cycle_method == 'shortest_branch': | |
133 prune_index = analyze_skeleton.SHORTEST_BRANCH | |
134 elif prune_cycle_method == 'lowest_intensity_voxel': | |
135 prune_index = analyze_skeleton.LOWEST_INTENSITY_VOXEL | |
136 elif prune_cycle_method == 'lowest_intensity_branch': | |
137 prune_index = analyze_skeleton.LOWEST_INTENSITY_BRANCH | |
138 result = analyze_skeleton.run( prune_index, | |
139 prune_ends, | |
140 calculate_largest_shortest_path, | |
141 input_image_plus_copy, | |
142 True, | |
143 True ) | |
144 # Save the results. | |
145 save( result, output, show_detailed_info, calculate_largest_shortest_path ) | |
146 except Exception, e: | |
147 jython_utils.handle_error( error_log, str( e ) ) |