AlexLipp / POOPy

Object Oriented Python package for interfacing with English Water Companies EDM data
GNU General Public License v3.0
7 stars 1 forks source link

Speed up channel profiling #4

Closed AlexLipp closed 10 months ago

AlexLipp commented 11 months ago

Use Cython

AlexLipp commented 11 months ago

The bottle neck is the _get_channel_segment method in the poopy.profiler.ChannelProfiler class. This method is in essence a big python for loop so can be sped up significantly if it can be compiled.

AlexLipp commented 10 months ago

Design a Cython function that takes a 2D array (and optionally a "threshold") and returns a geoJSON FeatureCollection object where each line-segment is a bit of a channel profile. This will need to be done via a DFS of the data-structure:

list nodes_to_visit = {root};
while( nodes_to_visit isn't empty ) {
  currentnode = nodes_to_visit.take_first();
  nodes_to_visit.prepend( currentnode.children );
  //do something
}
AlexLipp commented 10 months ago

Some pseudocode here:

    from libcpp.stack cimport stack
    cdef:
        stack[Node] node_stack
        Node current_node
        Node neighbor

    node_stack.push(start)

    all_segs = []
    current_seg = []
    while not node_stack.empty():
        current_node = node_stack.top()
        node_stack.pop()
        current_seg.append(node)

        print(current_node.value)
        for neighbor in current_node.neighbors:
            node_stack.push(neighbor)
        if len(current_node.neighbours) != 1: 
            all_segs.append(current_seg)
            current_seg = []