skimage2.graph.MCP_Flexible#
- class skimage2.graph.MCP_Flexible(costs, offsets=None, fully_connected=True)#
Bases:
MCPFind minimum cost paths through an N-d costs array.
See the documentation for MCP for full details. This class differs from MCP in that several methods can be overloaded (from pure Python) to modify the behavior of the algorithm and/or create custom algorithms based on MCP. Note that goal_reached can also be overloaded in the MCP class.
- __init__(costs, offsets=None, fully_connected=True, sampling=None)#
See class documentation.
- examine_neighbor(index, new_index, offset_length)#
This method is called once for every pair of neighboring nodes, as soon as both nodes are frozen.
This method can be overloaded to obtain information about neighboring nodes, and/or to modify the behavior of the MCP algorithm. One example is the MCP_Connect class, which checks for meeting fronts using this hook.
- find_costs(starts, ends=None, find_all_ends=True, max_coverage=1.0, max_cumulative_cost=<DEPRECATED>, max_cost=<DEPRECATED>, *, max_step_cost=None)#
Find the minimum-cost path from the given starting points.
This method finds the minimum-cost path to the specified ending indices from any one of the specified starting indices. If no end positions are given, then the minimum-cost path to every position in the costs array will be found.
- Parameters:
- startsiterable
A list of n-d starting indices (where n is the dimension of the
costsarray). The minimum cost path to the closest/cheapest starting point will be found.- endsiterable, optional
A list of n-d ending indices.
- find_all_endsbool, optional
If ‘True’ (default), the minimum-cost-path to every specified end-position will be found; otherwise the algorithm will stop when a a path is found to any end-position. (If no
endswere specified, then this parameter has no effect.)- max_step_costfloat, optional
Cost limit for each step between points. Costs higher than this will form a barrier.
- Returns:
- cumulative_costsndarray
Same shape as the
costsarray; this array records the minimum cost path from the nearest/cheapest starting index to each index considered. (Ifendswere specified, not all elements in the array will necessarily be considered: positions not evaluated will have a cumulative cost of inf. Iffind_all_endsis ‘False’, only one of the specified end-positions will have a finite cumulative cost.)- tracebackndarray
Same shape as the
costsarray; this array contains the offset to any given index from its predecessor index. The offset indices index into theoffsetsattribute, which is a array of n-d offsets. In the 2-d case, if offsets[traceback[x, y]] is (-1, -1), that means that the predecessor of [x, y] in the minimum cost path to some start position is [x+1, y+1]. Note that if the offset_index is -1, then the given index was not considered.
- Other Parameters:
- max_costDEPRECATED
Deprecated in favor of
max_step_cost.Deprecated since version 0.26.
- max_cumulative_costDEPRECATED
max_cumulative_costis deprecated.Deprecated since version 0.26.
- goal_reached(index, cumcost)#
int goal_reached(int index, float cumcost) This method is called each iteration after popping an index from the heap, before examining the neighbors.
This method can be overloaded to modify the behavior of the MCP algorithm. An example might be to stop the algorithm when a certain cumulative cost is reached, or when the front is a certain distance away from the seed point.
This method should return 1 if the algorithm should not check the current point’s neighbors and 2 if the algorithm is now done.
- offsets#
- traceback(end)#
Trace a minimum cost path through the pre-calculated traceback array.
This convenience function reconstructs the the minimum cost path to a given end position from one of the starting indices provided to find_costs(), which must have been called previously. This function can be called as many times as desired after find_costs() has been run.
- Parameters:
- enditerable
An n-d index into the
costsarray.
- Returns:
- tracebacklist of n-d tuples
A list of indices into the
costsarray, starting with one of the start positions passed to find_costs(), and ending with the givenendindex. These indices specify the minimum-cost path from any given start index to theendindex. (The total cost of that path can be read out from thecumulative_costsarray returned by find_costs().)
- travel_cost(old_cost, new_cost, offset_length)#
This method calculates the travel cost for going from the current node to the next. The default implementation returns new_cost. Overload this method to adapt the behaviour of the algorithm.
- update_node(index, new_index, offset_length)#
This method is called when a node is updated, right after new_index is pushed onto the heap and the traceback map is updated.
This method can be overloaded to keep track of other arrays that are used by a specific implementation of the algorithm. For instance the MCP_Connect class uses it to update an id map.