comparison imagej2_analyze_skeleton_jython_script.py @ 2:756e062741dc draft

planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit 57a0433defa3cbc37ab34fbb0ebcfaeb680db8d5
author imgteam
date Sun, 05 Nov 2023 10:57:39 +0000
parents 7a44772cc89f
children 73cc4e6a741d
comparison
equal deleted inserted replaced
1:7a44772cc89f 2:756e062741dc
2 import sys 2 import sys
3 3
4 from ij import IJ 4 from ij import IJ
5 from sc.fiji.analyzeSkeleton import AnalyzeSkeleton_ 5 from sc.fiji.analyzeSkeleton import AnalyzeSkeleton_
6 6
7 BASIC_NAMES = ['Branches', 'Junctions', 'End-point Voxels', 7 BASIC_NAMES = [
8 'Junction Voxels', 'Slab Voxels', 'Average branch length', 8 "Branches",
9 'Triple Points', 'Quadruple Points', 'Maximum Branch Length'] 9 "Junctions",
10 DETAIL_NAMES = ['Skeleton ID', 'Branch length', 'V1 x', 'V1 y', 'V1 z', 'V2 x', 10 "End-point Voxels",
11 'V2 y', 'V2 z', 'Euclidean distance'] 11 "Junction Voxels",
12 OPTIONS = ['edm=Overwrite', 'iterations=1', 'count=1'] 12 "Slab Voxels",
13 "Average branch length",
14 "Triple Points",
15 "Quadruple Points",
16 "Maximum Branch Length",
17 ]
18 DETAIL_NAMES = [
19 "Skeleton ID",
20 "Branch length",
21 "V1 x",
22 "V1 y",
23 "V1 z",
24 "V2 x",
25 "V2 y",
26 "V2 z",
27 "Euclidean distance",
28 ]
29 OPTIONS = ["edm=Overwrite", "iterations=1", "count=1"]
13 30
14 31
15 def get_euclidean_distance(vertex1, vertex2): 32 def get_euclidean_distance(vertex1, vertex2):
16 x1, y1, z1 = get_points(vertex1) 33 x1, y1, z1 = get_points(vertex1)
17 x2, y2, z2 = get_points(vertex2) 34 x2, y2, z2 = get_points(vertex2)
18 return math.sqrt(math.pow((x2 - x1), 2) + math.pow((y2 - y1), 2) + math.pow((z2 - z1), 2)) 35 return math.sqrt(
36 math.pow((x2 - x1), 2) + math.pow((y2 - y1), 2) + math.pow((z2 - z1), 2)
37 )
19 38
20 39
21 def get_graph_length(graph): 40 def get_graph_length(graph):
22 length = 0 41 length = 0
23 for edge in graph.getEdges(): 42 for edge in graph.getEdges():
44 # Sort graphs from longest to shortest. 63 # Sort graphs from longest to shortest.
45 graphs = sorted(graphs, key=lambda g: get_graph_length(g), reverse=True) 64 graphs = sorted(graphs, key=lambda g: get_graph_length(g), reverse=True)
46 return graphs 65 return graphs
47 66
48 67
49 def save(result, output, show_detailed_info, calculate_largest_shortest_path, sep='\t'): 68 def save(result, output, show_detailed_info, calculate_largest_shortest_path, sep="\t"):
50 num_trees = int(result.getNumOfTrees()) 69 num_trees = int(result.getNumOfTrees())
51 outf = open(output, 'wb') 70 outf = open(output, "wb")
52 outf.write('# %s\n' % sep.join(BASIC_NAMES)) 71 outf.write("# %s\n" % sep.join(BASIC_NAMES))
53 for index in range(num_trees): 72 for index in range(num_trees):
54 outf.write('%d%s' % (result.getBranches()[index], sep)) 73 outf.write("%d%s" % (result.getBranches()[index], sep))
55 outf.write('%d%s' % (result.getJunctions()[index], sep)) 74 outf.write("%d%s" % (result.getJunctions()[index], sep))
56 outf.write('%d%s' % (result.getEndPoints()[index], sep)) 75 outf.write("%d%s" % (result.getEndPoints()[index], sep))
57 outf.write('%d%s' % (result.getJunctionVoxels()[index], sep)) 76 outf.write("%d%s" % (result.getJunctionVoxels()[index], sep))
58 outf.write('%d%s' % (result.getSlabs()[index], sep)) 77 outf.write("%d%s" % (result.getSlabs()[index], sep))
59 outf.write('%.3f%s' % (result.getAverageBranchLength()[index], sep)) 78 outf.write("%.3f%s" % (result.getAverageBranchLength()[index], sep))
60 outf.write('%d%s' % (result.getTriples()[index], sep)) 79 outf.write("%d%s" % (result.getTriples()[index], sep))
61 outf.write('%d%s' % (result.getQuadruples()[index], sep)) 80 outf.write("%d%s" % (result.getQuadruples()[index], sep))
62 outf.write('%.3f' % result.getMaximumBranchLength()[index]) 81 outf.write("%.3f" % result.getMaximumBranchLength()[index])
63 if calculate_largest_shortest_path: 82 if calculate_largest_shortest_path:
64 outf.write('%s%.3f%s' % (sep, result.shortestPathList.get(index), sep)) 83 outf.write("%s%.3f%s" % (sep, result.shortestPathList.get(index), sep))
65 outf.write('%d%s' % (result.spStartPosition[index][0], sep)) 84 outf.write("%d%s" % (result.spStartPosition[index][0], sep))
66 outf.write('%d%s' % (result.spStartPosition[index][1], sep)) 85 outf.write("%d%s" % (result.spStartPosition[index][1], sep))
67 outf.write('%d\n' % result.spStartPosition[index][2]) 86 outf.write("%d\n" % result.spStartPosition[index][2])
68 else: 87 else:
69 outf.write('\n') 88 outf.write("\n")
70 if show_detailed_info: 89 if show_detailed_info:
71 outf.write('# %s\n' % sep.join(DETAIL_NAMES)) 90 outf.write("# %s\n" % sep.join(DETAIL_NAMES))
72 # The following index is a placeholder for the skeleton ID. 91 # The following index is a placeholder for the skeleton ID.
73 # The terms "graph" and "skeleton" refer to the same thing. 92 # The terms "graph" and "skeleton" refer to the same thing.
74 # Also, the SkeletonResult.java code states that the 93 # Also, the SkeletonResult.java code states that the
75 # private Graph[] graph object is an array of graphs (one 94 # private Graph[] graph object is an array of graphs (one
76 # per tree). 95 # per tree).
78 for edge in get_sorted_edge_lengths(graph): 97 for edge in get_sorted_edge_lengths(graph):
79 vertex1 = edge.getV1() 98 vertex1 = edge.getV1()
80 x1, y1, z1 = get_points(vertex1) 99 x1, y1, z1 = get_points(vertex1)
81 vertex2 = edge.getV2() 100 vertex2 = edge.getV2()
82 x2, y2, z2 = get_points(vertex2) 101 x2, y2, z2 = get_points(vertex2)
83 outf.write('%d%s' % (index + 1, sep)) 102 outf.write("%d%s" % (index + 1, sep))
84 outf.write('%.3f%s' % (edge.getLength(), sep)) 103 outf.write("%.3f%s" % (edge.getLength(), sep))
85 outf.write('%d%s' % (x1, sep)) 104 outf.write("%d%s" % (x1, sep))
86 outf.write('%d%s' % (y1, sep)) 105 outf.write("%d%s" % (y1, sep))
87 outf.write('%d%s' % (z1, sep)) 106 outf.write("%d%s" % (z1, sep))
88 outf.write('%d%s' % (x2, sep)) 107 outf.write("%d%s" % (x2, sep))
89 outf.write('%d%s' % (y2, sep)) 108 outf.write("%d%s" % (y2, sep))
90 outf.write('%d%s' % (z2, sep)) 109 outf.write("%d%s" % (z2, sep))
91 outf.write('%.3f' % get_euclidean_distance(vertex1, vertex2)) 110 outf.write("%.3f" % get_euclidean_distance(vertex1, vertex2))
92 if calculate_largest_shortest_path: 111 if calculate_largest_shortest_path:
93 # Keep number of separated items the same for each line. 112 # Keep number of separated items the same for each line.
94 outf.write('%s %s' % (sep, sep)) 113 outf.write("%s %s" % (sep, sep))
95 outf.write(' %s' % sep) 114 outf.write(" %s" % sep)
96 outf.write(' %s' % sep) 115 outf.write(" %s" % sep)
97 outf.write(' \n') 116 outf.write(" \n")
98 else: 117 else:
99 outf.write('\n') 118 outf.write("\n")
100 outf.close() 119 outf.close()
101 120
102 121
103 # Fiji Jython interpreter implements Python 2.5 which does not 122 # Fiji Jython interpreter implements Python 2.5 which does not
104 # provide support for argparse. 123 # provide support for argparse.
107 black_background = sys.argv[-6] == "yes" 126 black_background = sys.argv[-6] == "yes"
108 prune_cycle_method = sys.argv[-5] 127 prune_cycle_method = sys.argv[-5]
109 prune_ends = sys.argv[-4] == "yes" 128 prune_ends = sys.argv[-4] == "yes"
110 calculate_largest_shortest_path = sys.argv[-3] == "yes" 129 calculate_largest_shortest_path = sys.argv[-3] == "yes"
111 if calculate_largest_shortest_path: 130 if calculate_largest_shortest_path:
112 BASIC_NAMES.extend(['Longest Shortest Path', 'spx', 'spy', 'spz']) 131 BASIC_NAMES.extend(["Longest Shortest Path", "spx", "spy", "spz"])
113 DETAIL_NAMES.extend([' ', ' ', ' ', ' ']) 132 DETAIL_NAMES.extend([" ", " ", " ", " "])
114 show_detailed_info = sys.argv[-2] == "yes" 133 show_detailed_info = sys.argv[-2] == "yes"
115 output = sys.argv[-1] 134 output = sys.argv[-1]
116 135
117 # Open the input image file. 136 # Open the input image file.
118 input_image_plus = IJ.openImage(input) 137 input_image_plus = IJ.openImage(input)
133 IJ.run(input_image_plus_copy, "Make Binary", "") 152 IJ.run(input_image_plus_copy, "Make Binary", "")
134 153
135 # Run AnalyzeSkeleton 154 # Run AnalyzeSkeleton
136 analyze_skeleton = AnalyzeSkeleton_() 155 analyze_skeleton = AnalyzeSkeleton_()
137 analyze_skeleton.setup("", input_image_plus_copy) 156 analyze_skeleton.setup("", input_image_plus_copy)
138 if prune_cycle_method == 'none': 157 if prune_cycle_method == "none":
139 prune_index = analyze_skeleton.NONE 158 prune_index = analyze_skeleton.NONE
140 elif prune_cycle_method == 'shortest_branch': 159 elif prune_cycle_method == "shortest_branch":
141 prune_index = analyze_skeleton.SHORTEST_BRANCH 160 prune_index = analyze_skeleton.SHORTEST_BRANCH
142 elif prune_cycle_method == 'lowest_intensity_voxel': 161 elif prune_cycle_method == "lowest_intensity_voxel":
143 prune_index = analyze_skeleton.LOWEST_INTENSITY_VOXEL 162 prune_index = analyze_skeleton.LOWEST_INTENSITY_VOXEL
144 elif prune_cycle_method == 'lowest_intensity_branch': 163 elif prune_cycle_method == "lowest_intensity_branch":
145 prune_index = analyze_skeleton.LOWEST_INTENSITY_BRANCH 164 prune_index = analyze_skeleton.LOWEST_INTENSITY_BRANCH
146 result = analyze_skeleton.run(prune_index, prune_ends, calculate_largest_shortest_path, input_image_plus_copy, True, True) 165 result = analyze_skeleton.run(
166 prune_index,
167 prune_ends,
168 calculate_largest_shortest_path,
169 input_image_plus_copy,
170 True,
171 True,
172 )
147 # Save the results. 173 # Save the results.
148 save(result, output, show_detailed_info, calculate_largest_shortest_path) 174 save(result, output, show_detailed_info, calculate_largest_shortest_path)