@@ -146,7 +146,7 @@ def measure_run_length_sgns(centroids: np.ndarray, scale_factor=10):
146146 return total_distance , path , path_dict
147147
148148
149- def measure_run_length_ihcs (centroids ):
149+ def measure_run_length_ihcs (centroids , max_edge_distance = 50 ):
150150 """Measure the run lengths of the IHC segmentation
151151 by finding the shortest path between the most distant nodes in a Steiner Tree.
152152
@@ -159,15 +159,25 @@ def measure_run_length_ihcs(centroids):
159159 A dictionary containing the position and the length fraction of each point in the path.
160160 """
161161 graph = nx .Graph ()
162- for num , pos in enumerate (centroids ):
162+ coords = {}
163+ labels = [int (i ) for i in range (len (centroids ))]
164+ for index , element in zip (labels , centroids ):
165+ coords [index ] = element
166+
167+ for num , pos in coords .items ():
163168 graph .add_node (num , pos = pos )
164- # approximate Steiner tree and find shortest path between the two most distant nodes
165- terminals = set (graph .nodes ()) # All nodes are required
166- # Approximate Steiner Tree over all nodes
167- T = steiner_tree (graph , terminals )
168- u , v = find_most_distant_nodes (T )
169- path = nx .shortest_path (T , source = u , target = v )
170- total_distance = nx .path_weight (T , path , weight = "weight" )
169+
170+ # create edges between points whose distance is less than threshold max_edge_distance
171+ for num_i , pos_i in coords .items ():
172+ for num_j , pos_j in coords .items ():
173+ if num_i < num_j :
174+ dist = math .dist (pos_i , pos_j )
175+ if dist <= max_edge_distance :
176+ graph .add_edge (num_i , num_j , weight = dist )
177+
178+ u , v = find_most_distant_nodes (graph )
179+ path = nx .shortest_path (graph , source = u , target = v )
180+ total_distance = nx .path_weight (graph , path , weight = "weight" )
171181
172182 # assign relative distance to points on path
173183 path_dict = {}
@@ -180,6 +190,9 @@ def measure_run_length_ihcs(centroids):
180190 path_dict [num + 1 ] = {"pos" : graph .nodes [p ]["pos" ], "length_fraction" : rel_dist }
181191 path_dict [len (path )] = {"pos" : graph .nodes [path [- 1 ]]["pos" ], "length_fraction" : 1 }
182192
193+ path_pos = np .array ([graph .nodes [p ]["pos" ] for p in path ])
194+ path = moving_average_3d (path_pos , window = 5 )
195+
183196 return total_distance , path , path_dict
184197
185198
0 commit comments